Debian Codegraph @

src/do_md.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 1999-2002, 2004-2006, 2009-2011, 2013, 2018-2025 Rami Lehti,
5 * Pablo Virolainen, Mike Markley, Richard van den Berg,
6 * Hannes von Haugwitz
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24 #include "db_disk.h"
25 #include <stdbool.h>
26
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <sys/mman.h>
36 #include <sys/wait.h>
37
38 #ifdef WITH_ZLIB
39 #include <zlib.h>
40 #endif
41
42 #ifdef WITH_XATTR
43 #include <sys/xattr.h>
44 #include <attr/attributes.h>
45 #endif
46 #ifdef WITH_SELINUX
47 #include <selinux/selinux.h>
48 #endif
49 #ifdef WITH_POSIX_ACL
50 #include <sys/acl.h>
51 #endif
52 #ifdef WITH_E2FSATTRS
53 #include <e2p/e2p.h>
54 #endif
55 #ifdef HAVE_SYS_PRCTL_H
56 #include <sys/prctl.h>
57 #endif
58
59 #ifdef WITH_CAPABILITIES
60 #include <sys/capability.h>
61 #endif
62
63
64 #include "md.h"
65 #include "do_md.h"
66
67 #include "hashsum.h"
68 #include "db_line.h"
69 #include "db_config.h"
70 #include "util.h"
71 #include "log.h"
72 #include "attributes.h"
73
74 /* This define should be somewhere else */
75 #define READ_BLOCK_SIZE 16777216
76
77 typedef union fd {
78 int plain;
79 #ifdef WITH_ZLIB
80 gzFile gzip;
81 #endif
82 } _fd;
83
84 typedef enum compression {
85 COMPRESSION_PLAIN,
86 #ifdef WITH_ZLIB
87 COMPRESSION_GZIP,
88 #endif
89 COMPRESSION_ERROR
90 } compression;
91
92 typedef struct hashsums_file {
93 _fd fd;
94 compression compression;
95 } hashsums_file;
96
97 int stat_cmp(struct stat* f1,struct stat* f2, bool growing) {
98 if (f1==NULL || f2==NULL) {
99 return RETFAIL;
100 }
101 #define stat_cmp_helper(n,attribute) ((f1->n!=f2->n)*ATTR(attribute))
102
103 #define stat_growing_cmp_helper(n,attribute) ((growing?f1->n<f2->n:f1->n!=f2->n)*ATTR(attribute))
104
105 return (stat_cmp_helper(st_ino,attr_inode)|
106 stat_cmp_helper(st_mode,attr_perm)|
107 stat_cmp_helper(st_nlink,attr_linkcount)|
108 stat_growing_cmp_helper(st_size,attr_size)|
109 stat_growing_cmp_helper(st_mtime,attr_mtime)|
110 stat_growing_cmp_helper(st_ctime,attr_ctime)|
111 stat_growing_cmp_helper(st_blocks,attr_bcount)|
112 stat_cmp_helper(st_blksize,attr_bsize)|
113 stat_cmp_helper(st_rdev,attr_rdev)|
114 stat_cmp_helper(st_gid,attr_gid)|
115 stat_cmp_helper(st_uid,attr_uid)|
116 stat_cmp_helper(st_dev,attr_dev));
117 }
118
119 static hashsums_file hashsum_open(int filedes, char* fullpath, bool uncompress, const char *whoami) {
120 hashsums_file file;
121
122 if (uncompress) {
123 char head[2];
124 char *magic_gzip = "\037\213";
125 ssize_t magic_length = strlen(magic_gzip);
126 ssize_t bytes = read(filedes, head, magic_length);
127 if (bytes == magic_length && strncmp(head, magic_gzip, magic_length) == 0) {
128 LOG_WHOAMI(LOG_LEVEL_COMPARE, "│ '%s' is gzip compressed", fullpath);
129 lseek(filedes, 0, SEEK_SET);
130 #ifdef WITH_ZLIB
131 int dupfd = dup(filedes);
132 if (dupfd == -1) {
133 log_msg(LOG_LEVEL_NOTICE, "'%s': failed to duplicate file descriptor: %s", fullpath, strerror(errno));
134 file.compression = COMPRESSION_ERROR;
135 return file;
136 }
137 file.fd.gzip = gzdopen(dupfd, "rb");
138 if (file.fd.gzip == NULL){
139 log_msg(LOG_LEVEL_WARNING, "hash calculation: gzdopen() failed for %s (uncompressed hashsums could not be calculated)", fullpath);
140 close(dupfd);
141 file.compression = COMPRESSION_ERROR;
142 return file;
143 }
144 file.compression = COMPRESSION_GZIP;
145 return file;
146 #else
147 log_msg(LOG_LEVEL_WARNING, "'%s': gzip support not compiled in, recompile AIDE with '--with-zlib' (uncompressed hashsums could not be calculated)", fullpath);
148 file.compression = COMPRESSION_ERROR;
149 return file;
150 #endif
151 } else {
152 log_msg(LOG_LEVEL_NOTICE, "'%s': no supported compression algorithm found (uncompressed hashsums could not be calculated)", fullpath);
153 file.compression = COMPRESSION_ERROR;
154 return file;
155 }
156 } else {
157 file.fd.plain = filedes;
158 file.compression = COMPRESSION_PLAIN;
159 return file;
160 }
161 }
162
163 static off_t hashsum_read(hashsums_file file, void *buf, size_t count) {
164 off_t size = -1;
165 do {
166 switch (file.compression) {
167 case COMPRESSION_PLAIN:
168 size = read(file.fd.plain, buf, count);
169 break;
170 #ifdef WITH_ZLIB
171 case COMPRESSION_GZIP:
172 size = gzread(file.fd.gzip, buf, count);
173 break;
174 #endif
175 case COMPRESSION_ERROR:
176 size = -2;
177 break;
178 }
179 } while (size == -1 && errno == EINTR); /* retry on EINTR */
180 return size;
181 }
182
183 static int hashsum_close(hashsums_file file) {
184 switch (file.compression) {
185 case COMPRESSION_PLAIN:
186 return 0;
187 #ifdef WITH_ZLIB
188 case COMPRESSION_GZIP:
189 return gzclose(file.fd.gzip);
190 #endif
191 case COMPRESSION_ERROR:
192 return -1;
193 }
194 return -1;
195 }
196
197 md_hashsums calc_hashsums(disk_entry *entry, DB_ATTR_TYPE attr, ssize_t limit_size, bool uncompress, const char *whoami) {
198 md_hashsums md_hash;
199 md_hash.attrs = 0LU;
200
201 if (lseek(entry->fd, 0, SEEK_SET) == -1) {
202 log_msg(LOG_LEVEL_WARNING, "hash calculation: lseek() failed to failed for '%s': %s (hashsum could not be calculated)", entry->filename, strerror(errno));
203 return md_hash;
204 }
205 #ifdef HAVE_POSIX_FADVISE
206 if (posix_fadvise(entry->fd,0,entry->fs.st_size,POSIX_FADV_SEQUENTIAL)!=0) {
207 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> calc_hashsums: posix_fadvise error: %s", entry->filename, strerror(errno));
208 }
209 #endif
210 hashsums_file file = hashsum_open(entry->fd, entry->filename, uncompress, whoami);
211 if (file.compression == COMPRESSION_ERROR) {
212 return md_hash;
213 }
214
215 off_t r_size=0;
216 off_t size=0;
217 char* buf;
218
219 struct md_container mdc;
220 mdc.todo_attr = attr;
221 if (init_md(&mdc, entry->filename, whoami)==RETOK) {
222 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> calculate hashes", entry->filename);
223 buf=checked_malloc(READ_BLOCK_SIZE);
224 #if READ_BLOCK_SIZE>SSIZE_MAX
225 #error "READ_BLOCK_SIZE" is too large. Max value is SSIZE_MAX, and current is READ_BLOCK_SIZE
226 #endif
227 while ((size = hashsum_read(file,buf,READ_BLOCK_SIZE)) > 0) {
228
229 off_t update_md_size;
230 if (limit_size > 0 && r_size+size > limit_size) {
231 /* the last read exceeded limited size */
232 update_md_size = limit_size-r_size;
233 } else if(attr&ATTR(attr_growing) && r_size+size > entry->fs.st_size) {
234 update_md_size = entry->fs.st_size-r_size;
235 } else {
236 update_md_size = size;
237 }
238
239 if (update_md(&mdc,buf,update_md_size)!=RETOK) {
240 log_msg(LOG_LEVEL_WARNING, "hash calculation: update_md() failed for '%s' (hashsums could not be calculated)", entry->filename);
241 free(buf);
242 hashsum_close(file);
243 close_md(&mdc, NULL, entry->filename, whoami);
244 return md_hash;
245 }
246 r_size+=update_md_size;
247 if (limit_size > 0 && r_size == limit_size) {
248 LOG_WHOAMI(LOG_LEVEL_DEBUG, "hash calculation: limited size (%zi) reached for '%s'", limit_size, entry->filename);
249 break;
250 } else if (attr&ATTR(attr_growing) && r_size == entry->fs.st_size) {
251 LOG_WHOAMI(LOG_LEVEL_DEBUG, "hash calculation: stat size (%zi) reached for growing file '%s'", entry->fs.st_size, entry->filename);
252 break;
253 }
254 }
255 free(buf);
256 hashsum_close(file);
257 if (size == -1) {
258 log_msg(LOG_LEVEL_WARNING, "hash calculation: failed to read file content of '%s': %s (hashsums could not be calculated)", entry->filename, strerror(errno));
259 close_md(&mdc, NULL, entry->filename, whoami);
260 return md_hash;
261 }
262
263 struct stat new_fs;
264 if (fstat(entry->fd,&new_fs) != 0) {
265 log_msg(LOG_LEVEL_WARNING, "hash calculation: fstat() failed for '%s': %s (hashsums could not be calculated)", entry->filename, strerror(errno));
266 close_md(&mdc, NULL, entry->filename, whoami);
267 return md_hash;
268 }
269 if(!(attr&ATTR(attr_rdev))) {
270 new_fs.st_rdev=0;
271 }
272 int stat_diff;
273 if ((stat_diff = stat_cmp(&new_fs, &entry->fs, attr&ATTR(attr_growing))) != RETOK) {
274 DB_ATTR_TYPE changed_attribures = 0ULL;
275 for(ATTRIBUTE i=0;i<num_attrs;i++) {
276 if (((1LLU<<i)&stat_diff)!=0) {
277 changed_attribures |= 1LLU<<i;
278 }
279 }
280
281 char *attrs_str = diff_attributes(0, changed_attribures);
282 # define WARN_COMMON_FORMAT "'%s' has changed (changed fields: %s%s) during hash calculation"
283 if (new_fs.st_size < entry->fs.st_size) {
284 log_msg(LOG_LEVEL_WARNING, WARN_COMMON_FORMAT ", was file truncated while AIDE was running? (discarding calculated hashsums)", entry->filename, attrs_str,", decreased size");
285 } else if (new_fs.st_size > entry->fs.st_size) {
286 log_msg(LOG_LEVEL_WARNING, WARN_COMMON_FORMAT ", was file growing while AIDE was running? (consider adding 'growing' attribute) (discarding calculated hashsums)", entry->filename, attrs_str, ", increased size");
287 } else {
288 log_msg(LOG_LEVEL_WARNING, WARN_COMMON_FORMAT " (discarding calculated hashsums)", entry->filename, attrs_str, "");
289 }
290 free(attrs_str);
291 close_md(&mdc, NULL, entry->filename, whoami);
292 return md_hash;
293 }
294 if (uncompress == false) {
295 long long target_size = limit_size > 0?limit_size: entry->fs.st_size;
296 if (r_size != target_size) {
297 log_msg(LOG_LEVEL_WARNING, "number of bytes read for hash calculation (%lld) mismatches expected %s size (%lld) for '%s' (discarding calculated hashsums)",
298 (long long) r_size,
299 limit_size > 0?"limited":"stat",
300 target_size,
301 entry->filename
302 );
303 close_md(&mdc, NULL, entry->filename, whoami);
304 return md_hash;
305 }
306 }
307 close_md(&mdc, &md_hash, entry->filename, whoami);
308 return md_hash;
309 } else {
310 log_msg(LOG_LEVEL_WARNING, "hash calculation: init_md() failed for '%s' (hashsums could not be calculated)", entry->filename);
311 hashsum_close(file);
312 return md_hash;
313 }
314 }
315
316 void fs2db_line(struct stat* fs,db_line* line) {
317
318 /* inode is always needed for ignoring changed filename */
319 line->inode=fs->st_ino;
320
321 if(ATTR(attr_uid)&line->attr) {
322 line->uid=fs->st_uid;
323 }else {
324 line->uid=0;
325 }
326
327 if(ATTR(attr_gid)&line->attr){
328 line->gid=fs->st_gid;
329 }else{
330 line->gid=0;
331 }
332
333 /* permissions are always needed for file type detection */
334 line->perm=fs->st_mode;
335
336 if(ATTR(attr_size)&line->attr
337 || ATTR(attr_sizeg)&line->attr
338 || (ATTR(attr_growing)&line->attr && line->attr&get_hashes(true))
339 ){
340 line->size=fs->st_size;
341 }else{
342 line->size=0;
343 }
344
345 if(ATTR(attr_linkcount)&line->attr){
346 line->nlink=fs->st_nlink;
347 }else {
348 line->nlink=0;
349 }
350
351 if(ATTR(attr_mtime)&line->attr){
352 line->mtime=fs->st_mtime;
353 }else{
354 line->mtime=0;
355 }
356
357 if(ATTR(attr_ctime)&line->attr){
358 line->ctime=fs->st_ctime;
359 }else{
360 line->ctime=0;
361 }
362
363 if(ATTR(attr_atime)&line->attr){
364 line->atime=fs->st_atime;
365 }else{
366 line->atime=0;
367 }
368
369 if(ATTR(attr_bcount)&line->attr){
370 line->bcount=fs->st_blocks;
371 } else {
372 line->bcount=0;
373 }
374
375 }
376
377 #ifdef WITH_ACL
378 void acl2line(db_line* line, int fd, const char *whoami) {
379 acl_type *ret = NULL;
380
381 #ifdef WITH_POSIX_ACL
382 if(ATTR(attr_acl)&line->attr) {
383 acl_t acl_a = NULL;
384 acl_t acl_d = NULL;
385 char *tmp = NULL;
386 #ifdef O_PATH
387 char proc_self_path[256];
388 #endif
389
390 acl_a = acl_get_fd(fd);
391 #ifdef O_PATH
392 int fd_flags = fcntl(fd, F_GETFL);
393 if (acl_a == NULL && errno == EBADF && fd_flags != -1 && fd_flags&O_PATH) {
394 snprintf(proc_self_path, sizeof(proc_self_path), "/proc/self/fd/%d", fd);
395 LOG_WHOAMI(LOG_LEVEL_DEBUG,"%s> acl_get_fd() failed: Bad file descriptor (emulating O_PATH support via '%s')", line->fullpath, proc_self_path);
396 acl_a = acl_get_file(proc_self_path, ACL_TYPE_ACCESS);
397 }
398 #endif
399 if (acl_a == NULL) {
400 switch (errno) {
401 case ENOSYS:
402 case ENOTSUP:
403 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> acl_get_fd() failed to get ACL: %s (disabling acl attribute)", line->fullpath, strerror(errno));
404 break;
405 default:
406 log_msg(LOG_LEVEL_WARNING, "acl_get_fd() failed to get ACL for '%s': %s (disabling acl attribute)", line->fullpath, strerror(errno));
407 }
408 line->attr&=(~ATTR(attr_acl));
409 return;
410 }
411 #ifdef O_PATH
412 if (S_ISDIR(line->perm)) {
413 snprintf(proc_self_path, sizeof(proc_self_path), "/proc/self/fd/%d", fd);
414 acl_d = acl_get_file(proc_self_path, ACL_TYPE_DEFAULT);
415 if (acl_d == NULL) {
416 log_msg(LOG_LEVEL_WARNING, "failed to get default ACL for directory '%s': %s (disabling acl attribute)", line->fullpath, strerror(errno));
417 acl_free(acl_a);
418 line->attr&=(~ATTR(attr_acl));
419 return;
420 }
421 }
422 #endif
423
424 ret = checked_malloc(sizeof(acl_type));
425
426 /* use tmp, so free() can be called instead of acl_free() */
427 tmp = acl_to_text(acl_a, NULL);
428 if (!tmp || !*tmp)
429 ret->acl_a = NULL;
430 else
431 ret->acl_a = checked_strdup(tmp);
432 acl_free(tmp);
433
434 if (!acl_d)
435 ret->acl_d = NULL;
436 else
437 {
438 tmp = acl_to_text(acl_d, NULL);
439 if (!tmp || !*tmp)
440 ret->acl_d = NULL;
441 else
442 ret->acl_d = checked_strdup(tmp);
443 acl_free(tmp);
444 }
445
446 acl_free(acl_a);
447 acl_free(acl_d);
448 }
449 line->acl = ret;
450 #endif
451 }
452 #endif
453
454 #ifdef WITH_XATTR
455 static xattrs_type *xattr_new(void) {
456 xattrs_type *ret = NULL;
457
458 ret = checked_malloc(sizeof(xattrs_type));
459 ret->num = 0;
460 ret->sz = 2;
461 ret->ents = checked_malloc(sizeof(xattr_node) * ret->sz);
462
463 return (ret);
464 }
465
466 static void *xzmemdup(const void *ptr, size_t len) {
467 /* always keeps a 0 at the end... */
468 void *ret = NULL;
469
470 ret = checked_malloc(len+1);
471 memcpy(ret, ptr, len);
472 ((char*)ret)[len] = 0;
473
474 return (ret);
475 }
476
477 static void xattr_add(xattrs_type *xattrs, const char *key, const char
478 *val, size_t vsz) {
479 if (xattrs->num >= xattrs->sz) {
480 xattrs->sz <<= 1;
481 xattrs->ents = checked_realloc(xattrs->ents, sizeof(xattr_node) * xattrs->sz);
482 }
483
484 xattrs->ents[xattrs->num].key = checked_strdup(key);
485 xattrs->ents[xattrs->num].val = xzmemdup(val, vsz);
486 xattrs->ents[xattrs->num].vsz = vsz;
487
488 xattrs->num += 1;
489 }
490
491 void xattrs2line(db_line *line, int fd, const char *whoami) {
492 xattrs_type *xattrs = NULL;
493 ssize_t xret = -1;
494
495 if ((ATTR(attr_xattrs)&line->attr)) {
496 ssize_t xsz = 1024;
497 char *xatrs = checked_malloc(xsz);
498 #ifdef O_PATH
499 char proc_self_path[256];
500 #endif
501
502 while (((xret = flistxattr(fd, xatrs, xsz)) == -1) && (errno == ERANGE)) {
503 xsz <<= 1;
504 xatrs = checked_realloc(xatrs, xsz);
505 }
506 #ifdef O_PATH
507 int fd_flags = fcntl(fd, F_GETFL);
508 if (xret == -1 && errno == EBADF && fd_flags != -1 && fd_flags&O_PATH) {
509 snprintf(proc_self_path, sizeof(proc_self_path), "/proc/self/fd/%d", fd);
510 LOG_WHOAMI(LOG_LEVEL_DEBUG,"%s> flistxattr() failed: Bad file descriptor (emulating O_PATH support via '%s')", line->fullpath, proc_self_path);
511 while (((xret = listxattr(proc_self_path, xatrs, xsz)) == -1) && (errno == ERANGE)) {
512 xsz <<= 1;
513 xatrs = checked_realloc(xatrs, xsz);
514 }
515 }
516 #endif
517 if (xret == -1) {
518 switch (errno) {
519 case ENOSYS:
520 case ENOTSUP:
521 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> listxattr() failed to get list of extended attribute names: %s (disabling xattr attribute)", line->fullpath, strerror(errno));
522 break;
523 default:
524 log_msg(LOG_LEVEL_WARNING, "listxattr() failed to get list of extended attribute names for '%s': %s (disabling xattr attribute)", line->fullpath, strerror(errno));
525 }
526 line->attr&=(~ATTR(attr_xattrs));
527 } else if (xret > 0) {
528 const char *attr = xatrs;
529 ssize_t asz = 1024;
530
531 char *val = checked_malloc(asz);
532 xattrs = xattr_new();
533 while (xret > 0) {
534 size_t len = strlen(attr);
535 ssize_t aret = 0;
536 if (strncmp(attr, "user.", strlen("user.")) == 0
537 || strncmp(attr, "security.", strlen("security.")) == 0
538 || strncmp(attr, "trusted.", strlen("trusted.")) == 0
539 ) {
540 #ifdef O_PATH
541 if (fd_flags&O_PATH) {
542 while (((aret = getxattr(proc_self_path, attr, val, asz)) == -1) && (errno == ERANGE)) {
543 asz <<= 1;
544 val = checked_realloc (val, asz);
545 }
546 } else {
547 #endif
548 while (((aret = fgetxattr(fd, attr, val, asz)) == -1) && (errno == ERANGE)) {
549 asz <<= 1;
550 val = checked_realloc (val, asz);
551 }
552 #ifdef O_PATH
553 }
554 #endif
555
556 if (aret != -1) {
557 xattr_add(xattrs, attr, val, aret);
558 } else {
559 log_msg(LOG_LEVEL_WARNING, "lgetxattr() failed to get value of extended attribute '%s' for '%s': %s (discarding extended attribute)", attr, line->fullpath, strerror(errno));
560 }
561 }
562 attr += len + 1;
563 xret -= len + 1;
564 }
565 free(val);
566 } else {
567 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> llistxattr() returned empty list of extended attribute names", line->fullpath);
568 }
569 free(xatrs);
570 }
571
572 line->xattrs = xattrs;
573 }
574 #endif
575
576 #ifdef WITH_SELINUX
577 void selinux2line(db_line *line, int fd, const char *whoami) {
578 char *cntx = NULL;
579
580 if ((ATTR(attr_selinux)&line->attr)) {
581 if (fgetfilecon_raw(fd, &cntx) == -1) { /* files opened via O_PATH are supported since libselinux 3.4. */
582 switch (errno) {
583 case ENODATA:
584 case ENOTSUP:
585 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> fgetfilecon_raw() failed to get SELinux security context: %s (disabling selinux attribute)", line->fullpath, strerror(errno));
586 break;
587 default:
588 log_msg(LOG_LEVEL_WARNING, "fgetfilecon_raw() failed to get SELinux security context for '%s': %s (disabling selinux attribute)", line->fullpath, strerror(errno));
589 }
590 line->attr&=(~ATTR(attr_selinux));
591 line->cntx = NULL;
592 return;
593 } else {
594 line->cntx = checked_strdup(cntx);
595 freecon(cntx);
596 }
597 } else {
598 line->cntx = NULL;
599 }
600 }
601 #endif
602
603 #ifdef WITH_E2FSATTRS
604 void e2fsattrs2line(db_line* line, int fd, const char *whoami) {
605 unsigned long flags;
606 if (ATTR(attr_e2fsattrs)&line->attr) {
607 if (getflags(fd, &flags) == 0) {
608 line->e2fsattrs=flags;
609 } else {
610 switch (errno) {
611 case ENOTTY:
612 case ENOTSUP:
613 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> fgetflags() failed to get file attributes: %s (disabling e2fsattrs attribute)", line->fullpath, strerror(errno));
614 break;
615 default:
616 log_msg(LOG_LEVEL_WARNING, "fgetflags() failed to get file attributes for '%s': %s (disabling e2fsattrs attribute)", line->fullpath, strerror(errno));
617 }
618 line->attr&=(~ATTR(attr_e2fsattrs));
619 line->e2fsattrs=0;
620 }
621 } else {
622 line->e2fsattrs=0;
623 }
624 }
625 #endif
626
627 #ifdef WITH_CAPABILITIES
628 void capabilities2line(db_line* line, int fd, const char *whoami) {
629 cap_t caps;
630 char *txt_caps;
631
632 if ((ATTR(attr_capabilities)&line->attr)) {
633 caps = cap_get_fd(fd);
634 #ifdef O_PATH
635 int fd_flags = fcntl(fd, F_GETFL);
636 if (caps == NULL && errno == EBADF && fd_flags != -1 && fd_flags&O_PATH) {
637 char proc_self_path[256];
638 snprintf(proc_self_path, sizeof(proc_self_path), "/proc/self/fd/%d", fd);
639 LOG_WHOAMI(LOG_LEVEL_DEBUG,"%s> cap_get_fd() failed: Bad file descriptor (emulating O_PATH support via '%s')", line->fullpath, proc_self_path);
640 caps = cap_get_file(proc_self_path);
641 }
642 #endif
643 if (caps != NULL) {
644 txt_caps = cap_to_text(caps, NULL);
645 if (txt_caps == NULL) {
646 log_msg(LOG_LEVEL_WARNING, "cap_to_text() failed to get textual representation for capability state of '%s': %s (disabling caps attribute)", line->fullpath, strerror(errno));
647 line->attr&=(~ATTR(attr_capabilities));
648 line->capabilities=NULL;
649 } else {
650 line->capabilities = checked_strdup(txt_caps);
651 cap_free(txt_caps);
652 }
653 cap_free(caps);
654 } else {
655 switch (errno) {
656 case ENOSYS:
657 case ENODATA:
658 case ENOTSUP:
659 LOG_WHOAMI(LOG_LEVEL_DEBUG, "%s> cap_get_fd() failed to get capability state: %s (disabling e2fsattrs attribute)", line->fullpath, strerror(errno));
660 break;
661 default:
662 log_msg(LOG_LEVEL_WARNING, "cap_get_fd() failed to get capability state for '%s': %s (disabling caps attribute)", line->fullpath, strerror(errno));
663 }
664 line->attr&=(~ATTR(attr_capabilities));
665 line->capabilities=NULL;
666 }
667 } else {
668 line->capabilities=NULL;
669 }
670 }
671 #endif