| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2006, 2009-2012, 2015-2016, 2019-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 "aide.h" |
| 25 | #include "file.h" |
| 26 | #include <pthread.h> |
| 27 | #include <string.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <limits.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <errno.h> |
| 35 | #include <time.h> |
| 36 | #define PCRE2_CODE_UNIT_WIDTH 8 |
| 37 | #include <pcre2.h> |
| 38 | |
| 39 | #include "attributes.h" |
| 40 | #include "hashsum.h" |
| 41 | #include "seltree_struct.h" |
| 42 | #include "rx_rule.h" |
| 43 | #include "url.h" |
| 44 | #include "gen_list.h" |
| 45 | #include "md.h" |
| 46 | #include "db.h" |
| 47 | #include "db_line.h" |
| 48 | #include "db_config.h" |
| 49 | #include "db_disk.h" |
| 50 | #include "do_md.h" |
| 51 | #include "log.h" |
| 52 | #include "progress.h" |
| 53 | #include "util.h" |
| 54 | /*for locale support*/ |
| 55 | #include "locale-aide.h" |
| 56 | /*for locale support*/ |
| 57 | |
| 58 | void hsymlnk(db_line* line); |
| 59 | void fs2db_line(struct stat* fs,db_line* line); |
| 60 | |
| 61 | LOG_LEVEL compare_log_level = LOG_LEVEL_COMPARE; |
| 62 | |
| 63 | static int bytecmp(byte *b1, byte *b2, size_t len) { |
| 64 | return strncmp((char *)b1, (char *)b2, len); |
| 65 | } |
| 66 | |
| 67 | static int has_str_changed(char* old,char* new) { |
| 68 | return (((old!=NULL && new!=NULL) && |
| 69 | strcmp(old,new)!=0 ) || |
| 70 | ((old!=NULL && new==NULL) || |
| 71 | (old==NULL && new!=NULL))); |
| 72 | } |
| 73 | |
| 74 | #ifdef WITH_ACL |
| 75 | static int has_acl_changed(const acl_type* old, const acl_type* new) { |
| 76 | if (old==NULL && new==NULL) { |
| 77 | return RETOK; |
| 78 | } |
| 79 | if (old==NULL || new==NULL) { |
| 80 | return RETFAIL; |
| 81 | } |
| 82 | #ifdef WITH_POSIX_ACL |
| 83 | if ((!old->acl_a != !new->acl_a) |
| 84 | || (!old->acl_d != !new->acl_d) |
| 85 | || (old->acl_a && strcmp(old->acl_a, new->acl_a)) |
| 86 | || (old->acl_d && strcmp(old->acl_d, new->acl_d))){ |
| 87 | return RETFAIL; |
| 88 | } |
| 89 | #endif |
| 90 | return RETOK; |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | #ifdef WITH_XATTR |
| 95 | static int cmp_xattr_node(const void *c1, const void *c2) |
| 96 | { |
| 97 | const xattr_node *x1 = c1; |
| 98 | const xattr_node *x2 = c2; |
| 99 | |
| 100 | return (strcmp(x1->key, x2->key)); |
| 101 | } |
| 102 | static int have_xattrs_changed(xattrs_type* x1,xattrs_type* x2) { |
| 103 | size_t num = 0; |
| 104 | |
| 105 | if (x1 && (x1->num == 0)) x1 = NULL; |
| 106 | if (x2 && (x2->num == 0)) x2 = NULL; |
| 107 | |
| 108 | if (x1==NULL && x2==NULL) { |
| 109 | return RETOK; |
| 110 | } |
| 111 | if (x1==NULL || x2==NULL) { |
| 112 | return RETFAIL; |
| 113 | } |
| 114 | |
| 115 | if (x1->num != x2->num) { |
| 116 | return RETFAIL; |
| 117 | } |
| 118 | |
| 119 | qsort(x1->ents, x1->num, sizeof(xattr_node), cmp_xattr_node); |
| 120 | qsort(x2->ents, x2->num, sizeof(xattr_node), cmp_xattr_node); |
| 121 | |
| 122 | while (num++ < x1->num) { |
| 123 | const char *x1key = NULL; |
| 124 | const byte *x1val = NULL; |
| 125 | size_t x1vsz = 0; |
| 126 | const char *x2key = NULL; |
| 127 | const byte *x2val = NULL; |
| 128 | size_t x2vsz = 0; |
| 129 | |
| 130 | x1key = x1->ents[num - 1].key; |
| 131 | x1val = x1->ents[num - 1].val; |
| 132 | x1vsz = x1->ents[num - 1].vsz; |
| 133 | |
| 134 | x2key = x2->ents[num - 1].key; |
| 135 | x2val = x2->ents[num - 1].val; |
| 136 | x2vsz = x2->ents[num - 1].vsz; |
| 137 | |
| 138 | if (strcmp(x1key, x2key) || |
| 139 | x1vsz != x2vsz || |
| 140 | memcmp(x1val, x2val, x1vsz)) |
| 141 | return RETFAIL; |
| 142 | } |
| 143 | |
| 144 | return RETOK; |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #ifdef WITH_E2FSATTRS |
| 149 | static int has_e2fsattrs_changed(unsigned long old, unsigned long new) { |
| 150 | return (old^new); |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | static DB_ATTR_TYPE get_changed_hashsums(byte** old_hashsums, byte** new_hashsums, db_line* old, db_line* new, const char* whoami) { |
| 155 | DB_ATTR_TYPE changed_hashsums = 0; |
| 156 | bool no_hashsums_compared = true; |
| 157 | for (int i = 0 ; i < num_hashes ; ++i) { |
| 158 | DB_ATTR_TYPE attr = ATTR(hashsums[i].attribute); |
| 159 | if (old_hashsums[i] || new_hashsums[i]) { |
| 160 | if (old_hashsums[i] && new_hashsums[i]) { |
| 161 | no_hashsums_compared = false; |
| 162 | bool hash_has_changed = (bytecmp(old_hashsums[i], new_hashsums[i], hashsums[i].length) != 0); |
| 163 | LOG_WHOAMI(LOG_LEVEL_TRACE,"│ %s hashsum %s changed (old: %p, new: %p)", attributes[hashsums[i].attribute].db_name, hash_has_changed?"has":"has NOT", (void*) old_hashsums[i], (void*) new_hashsums[i]); |
| 164 | if(hash_has_changed) { |
| 165 | changed_hashsums|=attr; |
| 166 | } |
| 167 | } else { |
| 168 | LOG_WHOAMI(LOG_LEVEL_TRACE,"│ %s hashsum comparison skipped (old: %p, new: %p)", attributes[hashsums[i].attribute].db_name, (void*) old_hashsums[i], (void*) new_hashsums[i]); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | if (no_hashsums_compared) { |
| 173 | log_msg(LOG_LEVEL_WARNING,"cannot compare hashsums of old:'%s' and new:'%s' (no common hashsum(s) available)", old->filename, new->filename); |
| 174 | } |
| 175 | return changed_hashsums; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Returns the changed attributes for two database lines. |
| 180 | * |
| 181 | * Attributes are only compared if they exist in both database lines. |
| 182 | */ |
| 183 | static DB_ATTR_TYPE get_changed_attributes(db_line* l1,db_line* l2, DB_ATTR_TYPE ignore_attrs, disk_entry *entry, bool compare_hashsums, const char* whoami) { |
| 184 | |
| 185 | #define easy_growing_compare(a,b) \ |
| 186 | if(((a&l1->attr) && (a&l2->attr))) { \ |
| 187 | if (l1->attr&ATTR(attr_growing)) { \ |
| 188 | if (l1->b < l2->b) { \ |
| 189 | LOG_WHOAMI(compare_log_level, "│ ignore growing " #b " change of old:'%s' and new:'%s'", l1->filename, l2->filename); \ |
| 190 | } else if (l1->b > l2->b) { \ |
| 191 | ret|=a; \ |
| 192 | } \ |
| 193 | } else if (l1->b != l2->b) { \ |
| 194 | ret|=a; \ |
| 195 | } \ |
| 196 | } |
| 197 | |
| 198 | #define easy_compare(a,b) \ |
| 199 | if((a&l1->attr && (a&l2->attr)) && l1->b!=l2->b){\ |
| 200 | ret|=a;\ |
| 201 | } |
| 202 | |
| 203 | #define easy_function_compare(a,b,c) \ |
| 204 | if((a&l1->attr && (a&l2->attr)) && c(l1->b,l2->b)){ \ |
| 205 | ret|=a; \ |
| 206 | } |
| 207 | |
| 208 | DB_ATTR_TYPE ret=0; |
| 209 | |
| 210 | if (l1->attr&ATTR(attr_growing)) { |
| 211 | LOG_WHOAMI(compare_log_level, "│ old:'%s' has growing attribute set, ignore growing changes", l1->filename); |
| 212 | } |
| 213 | |
| 214 | if ((ATTR(attr_ftype)&l1->attr && ATTR(attr_ftype)&l2->attr) && (l1->perm&S_IFMT)!=(l2->perm&S_IFMT)) { ret|=ATTR(attr_ftype); } |
| 215 | easy_function_compare(ATTR(attr_linkname),linkname,has_str_changed); |
| 216 | if ((ATTR(attr_sizeg)&l1->attr && ATTR(attr_sizeg)&l2->attr) && l1->size>l2->size){ ret|=ATTR(attr_sizeg); } |
| 217 | easy_growing_compare(ATTR(attr_size),size); |
| 218 | easy_growing_compare(ATTR(attr_bcount),bcount); |
| 219 | easy_compare(ATTR(attr_perm),perm); |
| 220 | easy_compare(ATTR(attr_uid),uid); |
| 221 | easy_compare(ATTR(attr_gid),gid); |
| 222 | easy_growing_compare(ATTR(attr_atime),atime); |
| 223 | easy_growing_compare(ATTR(attr_mtime),mtime); |
| 224 | easy_growing_compare(ATTR(attr_ctime),ctime); |
| 225 | easy_compare(ATTR(attr_inode),inode); |
| 226 | easy_compare(ATTR(attr_linkcount),nlink); |
| 227 | |
| 228 | #ifdef HAVE_FSTYPE |
| 229 | easy_compare(ATTR(attr_fs_type), fs_type); |
| 230 | #endif |
| 231 | |
| 232 | DB_ATTR_TYPE all_hashsums = get_hashes(true); |
| 233 | if (compare_hashsums && l1->attr&all_hashsums && l2->attr&all_hashsums) { |
| 234 | LOG_WHOAMI(LOG_LEVEL_TRACE, "│ compare hashsums of old:'%s' and new:'%s'", l1->filename, l2->filename); |
| 235 | DB_ATTR_TYPE changed_hashsums = get_changed_hashsums(l1->hashsums, l2->hashsums, l1, l2, whoami); |
| 236 | if (changed_hashsums) { |
| 237 | char *str; |
| 238 | str = diff_attributes(0,changed_hashsums); |
| 239 | LOG_WHOAMI(compare_log_level, "│ old:'%s' and new:'%s' have CHANGED hashsum(s): %s", l1->filename, l2->filename, str); |
| 240 | free(str); |
| 241 | if (l1->attr&ATTR(attr_growing)) { |
| 242 | if (conf->action&DO_COMPARE) { |
| 243 | if(l1->size < l2->size) { |
| 244 | if (l1->size) { |
| 245 | LOG_WHOAMI(compare_log_level, "┝ old:'%s' has growing attribute set, check for growing hashsums", l1->filename); |
| 246 | LOG_WHOAMI(compare_log_level, "│ compare hashsums of old:'%s' and new:'%s' (limited to old size %lld)", l1->filename, l2->filename, l1->size); |
| 247 | DB_ATTR_TYPE transition_hashsums = get_transition_hashsums(l1->filename, l1->attr, l2->filename, l2->attr); |
| 248 | md_hashsums hs = calc_hashsums(entry, l2->attr|transition_hashsums, l1->size, false, whoami); |
| 249 | |
| 250 | byte* new_hashsums[num_hashes]; |
| 251 | copy_hashsums(l2->fullpath, &hs, &new_hashsums, whoami); |
| 252 | |
| 253 | DB_ATTR_TYPE new_changed = get_changed_hashsums(l1->hashsums, new_hashsums, l1, l2, whoami); |
| 254 | |
| 255 | for (int i = 0 ; i < num_hashes ; ++i) { |
| 256 | free(new_hashsums[i]); |
| 257 | } |
| 258 | |
| 259 | if (new_changed) { |
| 260 | str = diff_attributes(0,new_changed); |
| 261 | LOG_WHOAMI(compare_log_level, "│ keep hashsums as CHANGED (hashsums of new:'%s' limited to old size %lld have been changed: %s)", l2->filename, l1->size, str); |
| 262 | free(str); |
| 263 | } else { |
| 264 | LOG_WHOAMI(compare_log_level, "│ set hashsums as UNCHANGED (hashsums of new:'%s' limited to old size %lld have NOT been changed)", l2->filename, l1->size); |
| 265 | changed_hashsums = 0; |
| 266 | } |
| 267 | } else { |
| 268 | LOG_WHOAMI(compare_log_level, "│ old:'%s' has growing attribute set, but skip hashsums calculation (file was empty before)", l1->filename); |
| 269 | LOG_WHOAMI(compare_log_level, "%s", "│ set hashsums as UNCHANGED (old size equals zero)"); |
| 270 | changed_hashsums = 0; |
| 271 | } |
| 272 | } else { |
| 273 | LOG_WHOAMI(compare_log_level, "┝ old:'%s' has growing attribute set, but skip hashsum calculation (old size is greater than or equal to new size)", l1->filename); |
| 274 | } |
| 275 | } else { |
| 276 | LOG_WHOAMI(compare_log_level, "┝ old:'%s' has growing attribute set, but skip hashsum calculation (NOT supported in dataase compare mode)", l1->filename); |
| 277 | } |
| 278 | } |
| 279 | } else { |
| 280 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ old:'%s' and new:'%s' have NO changed hashsum(s)", l1->filename, l2->filename); |
| 281 | } |
| 282 | ret |= changed_hashsums; |
| 283 | } |
| 284 | |
| 285 | #ifdef WITH_ACL |
| 286 | easy_function_compare(ATTR(attr_acl),acl,has_acl_changed); |
| 287 | #endif |
| 288 | #ifdef WITH_XATTR |
| 289 | easy_function_compare(ATTR(attr_xattrs),xattrs,have_xattrs_changed); |
| 290 | #endif |
| 291 | #ifdef WITH_SELINUX |
| 292 | easy_function_compare(ATTR(attr_selinux),cntx,has_str_changed); |
| 293 | #endif |
| 294 | #ifdef WITH_E2FSATTRS |
| 295 | easy_function_compare(ATTR(attr_e2fsattrs),e2fsattrs,has_e2fsattrs_changed); |
| 296 | #endif |
| 297 | #ifdef WITH_CAPABILITIES |
| 298 | easy_function_compare(ATTR(attr_capabilities),capabilities,has_str_changed); |
| 299 | #endif |
| 300 | |
| 301 | char *str; |
| 302 | if (ignore_attrs) { |
| 303 | str = diff_attributes(0, ignore_attrs); |
| 304 | LOG_WHOAMI(compare_log_level, "│ attribute changes to ignore: %s", str); |
| 305 | free(str); |
| 306 | } |
| 307 | DB_ATTR_TYPE ignored_attributes = ret&ignore_attrs; |
| 308 | char *ignored_attrs_str = ignored_attributes?diff_attributes(0, ignored_attributes):NULL; |
| 309 | ret &= ~ignore_attrs; |
| 310 | if (ret) { |
| 311 | str = diff_attributes(0, ret); |
| 312 | LOG_WHOAMI(compare_log_level, "│ old:'%s' and new:'%s' have CHANGED attributes: %s (ignored attributes: %s)", l1->filename, l2->filename, str, ignored_attrs_str?ignored_attrs_str:"<none>"); |
| 313 | free(str); |
| 314 | } else { |
| 315 | LOG_WHOAMI(compare_log_level, "│ old:'%s' and new:'%s' have NO changed attributes (ignored attributes: %s)", l1->filename, l2->filename, ignored_attrs_str?ignored_attrs_str:"<none>"); |
| 316 | } |
| 317 | free(ignored_attrs_str); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static DB_ATTR_TYPE get_different_attributes(db_line* l1, db_line* l2, DB_ATTR_TYPE ignore_attrs, const char *whoami) { |
| 322 | DB_ATTR_TYPE ret = l1->attr^l2->attr; |
| 323 | char *str; |
| 324 | if (ignore_attrs) { |
| 325 | str = diff_attributes(0, ignore_attrs); |
| 326 | LOG_WHOAMI(compare_log_level, "│ attribute differences to ignore: %s", str); |
| 327 | free(str); |
| 328 | } |
| 329 | DB_ATTR_TYPE ignored_attributes = ret&ignore_attrs; |
| 330 | char *ignored_attrs_str = ignored_attributes?diff_attributes(0, ignored_attributes):NULL; |
| 331 | ret &= ~ignore_attrs; |
| 332 | if (ret) { |
| 333 | str = diff_attributes(l1->attr&~ignore_attrs, l2->attr&~ignore_attrs); |
| 334 | LOG_WHOAMI(compare_log_level, "│ old:'%s' and new:'%s' have different attributes: %s (ignored attributes: %s)", l1->filename, l2->filename, str, ignored_attrs_str?ignored_attrs_str:"<none>"); |
| 335 | free(str); |
| 336 | } else { |
| 337 | LOG_WHOAMI(compare_log_level, "│ old:'%s' and new:'%s' have NO different attributes (ignored attributes: %s)", l1->filename, l2->filename, ignored_attrs_str?ignored_attrs_str:"<none>"); |
| 338 | } |
| 339 | free(ignored_attrs_str); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | #ifdef HAVE_FSTYPE |
| 344 | #define PRINT_RULE_MATCH(format, c, ...) \ |
| 345 | if (file.fs_type) { \ |
| 346 | fs_type_str = get_fs_type_string_from_magic(file.fs_type); \ |
| 347 | fprintf(stdout, "[%c] %c=%s:%s: " format "\n", c, file_type, fs_type_str, filename_safe, __VA_ARGS__); \ |
| 348 | free(fs_type_str); \ |
| 349 | } else { \ |
| 350 | fprintf(stdout, "[%c] %c:%s: " format "\n", c, file_type, filename_safe, __VA_ARGS__); \ |
| 351 | } |
| 352 | #else |
| 353 | #define PRINT_RULE_MATCH(format, c, ...) \ |
| 354 | fprintf(stdout, "[%c] %c:%s: " format "\n", c, file_type, filename_safe, __VA_ARGS__); |
| 355 | #endif |
| 356 | |
| 357 | void print_match(file_t file, match_t match) { |
| 358 | char * str; |
| 359 | char* attr_str; |
| 360 | char file_type = get_f_type_char_from_f_type(file.type); |
| 361 | #ifdef HAVE_FSTYPE |
| 362 | char *fs_type_str = NULL; |
| 363 | #endif |
| 364 | rx_rule *rule = match.rule; |
| 365 | char *filename_safe = stresc(file.name); |
| 366 | char *limit_safe = conf->limit?stresc(conf->limit):NULL; |
| 367 | switch (match.result) { |
| 368 | case RESULT_SELECTIVE_MATCH: |
| 369 | case RESULT_EQUAL_MATCH: |
| 370 | str = get_restriction_string(rule->restriction); |
| 371 | attr_str = diff_attributes(0, rule->attr); |
| 372 | PRINT_RULE_MATCH("%s: '%s%s %s %s' (%s:%d: '%s%s%s')", 'x', get_rule_type_long_string(rule->type), get_rule_type_char(rule->type), rule->rx, str, attr_str, rule->config_filename, rule->config_linenumber, rule->config_line, rule->prefix?"', prefix: '":"", rule->prefix?rule->prefix:"") |
| 373 | free(attr_str); |
| 374 | free(str); |
| 375 | break; |
| 376 | case RESULT_RECURSIVE_NEGATIVE_MATCH: |
| 377 | case RESULT_NON_RECURSIVE_NEGATIVE_MATCH: |
| 378 | str = get_restriction_string(rule->restriction); |
| 379 | PRINT_RULE_MATCH("%s: '%s%s %s' (%s:%d: '%s%s%s')", ' ', get_rule_type_long_string(rule->type), get_rule_type_char(rule->type), rule->rx, str, rule->config_filename, rule->config_linenumber, rule->config_line, rule->prefix?"', prefix: '":"", rule->prefix?rule->prefix:"") |
| 380 | free(str); |
| 381 | break; |
| 382 | case RESULT_NEGATIVE_PARENT_MATCH: |
| 383 | str = get_restriction_string(rule->restriction); |
| 384 | PRINT_RULE_MATCH("parent directory '%.*s' matches %s: '%s%s %s' (%s:%d: '%s%s%s')", ' ', match.length, filename_safe, get_rule_type_long_string(rule->type), get_rule_type_char(rule->type), rule->rx, str, rule->config_filename, rule->config_linenumber, rule->config_line, rule->prefix?"', prefix: '":"", rule->prefix?rule->prefix:"") |
| 385 | free(str); |
| 386 | break; |
| 387 | case RESULT_PARTIAL_MATCH: |
| 388 | case RESULT_NO_RULE_MATCH: |
| 389 | PRINT_RULE_MATCH("%s", ' ', "no matching rule") |
| 390 | break; |
| 391 | case RESULT_PARTIAL_LIMIT_MATCH: |
| 392 | PRINT_RULE_MATCH("parital limit match (limit '%s')", ' ', limit_safe); |
| 393 | break; |
| 394 | case RESULT_PART_LIMIT_AND_NO_RECURSE_MATCH: |
| 395 | if (rule) { |
| 396 | str = get_restriction_string(rule->restriction); |
| 397 | PRINT_RULE_MATCH("partial limit match (limit '%s') but %s: '%s%s %s' (%s:%d: '%s%s%s')", ' ', limit_safe, get_rule_type_long_string(rule->type), get_rule_type_char(rule->type), rule->rx, str, rule->config_filename, rule->config_linenumber, rule->config_line, rule->prefix?"', prefix: '":"", rule->prefix?rule->prefix:"") |
| 398 | free(str); |
| 399 | } else { |
| 400 | PRINT_RULE_MATCH("partial limit match (limit '%s') but no matching rule", ' ', limit_safe) |
| 401 | } |
| 402 | break; |
| 403 | case RESULT_NO_LIMIT_MATCH: |
| 404 | PRINT_RULE_MATCH("outside of limit '%s'", ' ', limit_safe); |
| 405 | break; |
| 406 | } |
| 407 | free(filename_safe); |
| 408 | free(limit_safe); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * add_file_to_tree |
| 413 | */ |
| 414 | void add_file_to_tree(seltree* tree,db_line* file,int db_flags, const database *db, disk_entry *entry, const char *whoami) |
| 415 | { |
| 416 | LOG_WHOAMI(LOG_LEVEL_TRACE, "add_file_to_tree: '%s'", file->filename); |
| 417 | seltree* node=NULL; |
| 418 | |
| 419 | node = get_or_create_seltree_node(tree,file->filename); |
| 420 | |
| 421 | pthread_rwlock_rdlock(&node->rwlock); |
| 422 | int node_flags = node->checked&db_flags; |
| 423 | pthread_rwlock_unlock(&node->rwlock); |
| 424 | if (db && node_flags) { |
| 425 | LOG_DB_FORMAT_LINE(LOG_LEVEL_WARNING, "duplicate database entry found for '%s' (skip line)", file->filename) |
| 426 | free_db_line(file); |
| 427 | free(file); |
| 428 | } else { |
| 429 | pthread_rwlock_wrlock(&node->rwlock); |
| 430 | |
| 431 | /* add note to this node which db has modified it */ |
| 432 | node->checked|=db_flags; |
| 433 | |
| 434 | LOG_LEVEL add_entry_log_level = LOG_LEVEL_DEBUG; |
| 435 | |
| 436 | switch (db_flags) { |
| 437 | case DB_OLD: { |
| 438 | progress_status(PROGRESS_OLDDB, file->filename); |
| 439 | LOG_WHOAMI(add_entry_log_level, "add old database entry '%s' (%c) to node '%s' (%p) as old data", file->filename, get_f_type_char_from_perm(file->perm), node->path, (void*) node); |
| 440 | node->old_data=file; |
| 441 | break; |
| 442 | } |
| 443 | case DB_NEW|DB_DISK: { |
| 444 | progress_status(PROGRESS_DISK, file->filename); |
| 445 | LOG_WHOAMI(add_entry_log_level, "add disk entry '%s' (%c) to node '%s' (%p) as new data", file->filename, get_f_type_char_from_perm(file->perm), node->path, (void*) node); |
| 446 | node->new_data=file; |
| 447 | break; |
| 448 | } |
| 449 | case DB_NEW: { |
| 450 | progress_status(PROGRESS_NEWDB, file->filename); |
| 451 | LOG_WHOAMI(add_entry_log_level, "add new database entry '%s' (%c) to node '%s' (%p) as new data", file->filename, get_f_type_char_from_perm(file->perm), node->path, (void*) node); |
| 452 | node->new_data=file; |
| 453 | break; |
| 454 | } |
| 455 | case DB_OLD|DB_NEW: { |
| 456 | progress_status(PROGRESS_SKIPPED, NULL); |
| 457 | node->new_data=file; |
| 458 | node->checked|=NODE_FREE; |
| 459 | LOG_WHOAMI(LOG_LEVEL_LIMIT, "add old database entry '%s' (%c) to node (%p) as new data (entry does not match limit but keep it for database_out)", file->filename, get_f_type_char_from_perm(file->perm), (void*) node); |
| 460 | pthread_rwlock_unlock(&node->rwlock); |
| 461 | return; |
| 462 | } |
| 463 | } |
| 464 | pthread_rwlock_unlock(&node->rwlock); |
| 465 | |
| 466 | if (conf->action&(DO_COMPARE|DO_DIFF)) { |
| 467 | if (!(db_flags&DB_OLD)) { |
| 468 | pthread_rwlock_rdlock(&node->rwlock); |
| 469 | LOG_WHOAMI(compare_log_level, "┬ handle '%s' from %s", node->path, db_flags==DB_OLD ? "old database": (db_flags==DB_NEW ? "new database": "disk")); |
| 470 | pthread_rwlock_unlock(&node->rwlock); |
| 471 | } |
| 472 | |
| 473 | pthread_rwlock_wrlock(&node->rwlock); |
| 474 | if((node->checked&DB_OLD)&&(node->checked&DB_NEW)){ |
| 475 | LOG_WHOAMI(compare_log_level, "┝ compare attributes of '%s'", node->path); |
| 476 | get_different_attributes(node->old_data,node->new_data, 0, whoami); |
| 477 | node->changed_attrs=get_changed_attributes(node->old_data,node->new_data, 0, entry, true, whoami); |
| 478 | /* Free the data if same else leave as is for report_tree */ |
| 479 | if(node->changed_attrs==RETOK && !((node->old_data)->attr^(node->new_data)->attr)) { |
| 480 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ free old data (node '%s' is unchanged)", node->path); |
| 481 | node->changed_attrs=0; |
| 482 | |
| 483 | free_db_line(node->old_data); |
| 484 | free(node->old_data); |
| 485 | node->old_data=NULL; |
| 486 | |
| 487 | /* Free new data if not needed for write_tree */ |
| 488 | if(conf->action&DO_INIT) { |
| 489 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ keep new data (node '%s' is unchanged, but keep it for database_out)", node->path); |
| 490 | node->checked|=NODE_FREE; |
| 491 | } else { |
| 492 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ free new data (node '%s' is unchanged)", node->path); |
| 493 | free_db_line(node->new_data); |
| 494 | free(node->new_data); |
| 495 | node->new_data=NULL; |
| 496 | } |
| 497 | LOG_WHOAMI(compare_log_level, "┴ finished '%s'", node->path); |
| 498 | pthread_rwlock_unlock(&node->rwlock); |
| 499 | return; |
| 500 | } |
| 501 | } else if(node->checked&DB_NEW) { |
| 502 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ '%s' is new (no old data exists)", node->path); |
| 503 | } |
| 504 | pthread_rwlock_unlock(&node->rwlock); |
| 505 | |
| 506 | DB_ATTR_TYPE default_move_ignored_attr = ATTR(attr_allownewfile)|ATTR(attr_allowrmfile)|ATTR(attr_checkinode)|ATTR(attr_compressed)|ATTR(attr_growing); |
| 507 | if (db_flags&DB_NEW) { |
| 508 | pthread_rwlock_rdlock(&node->rwlock); |
| 509 | db_line *new_file = node->new_data; |
| 510 | pthread_rwlock_unlock(&node->rwlock); |
| 511 | if (new_file->attr&ATTR(attr_compressed)) { |
| 512 | DB_ATTR_TYPE available_hashsums = get_hashes(false); |
| 513 | if (new_file->attr&available_hashsums) { |
| 514 | if (conf->action&DO_COMPARE) { |
| 515 | LOG_WHOAMI(compare_log_level, "┝ '%s' has compressed attribute set, calculate uncompressed hashsums", new_file->filename); |
| 516 | |
| 517 | seltree *moved_node = NULL; |
| 518 | |
| 519 | md_hashsums hs = calc_hashsums(entry, new_file->attr, -1, true, whoami); |
| 520 | if (hs.attrs) { |
| 521 | byte* new_hashsums[num_hashes]; |
| 522 | copy_hashsums(new_file->fullpath, &hs, &new_hashsums, whoami); |
| 523 | LOG_WHOAMI(compare_log_level, "│ search for original file with uncompressed hashsums of new:'%s'", new_file->filename); |
| 524 | |
| 525 | pthread_rwlock_rdlock(&(node->parent)->rwlock); |
| 526 | for(tree_node *x = tree_walk_first((node->parent)->children); x != NULL ; x = tree_walk_next(x)) { |
| 527 | moved_node = tree_get_data(x); |
| 528 | if (moved_node != node) { |
| 529 | pthread_rwlock_rdlock(&moved_node->rwlock); |
| 530 | if (moved_node->old_data) { |
| 531 | if ((new_file->attr&(moved_node->old_data)->attr)&available_hashsums) { |
| 532 | LOG_WHOAMI(LOG_LEVEL_TRACE, "│ compare hashsums of old:'%s' with uncompressed hashsums of new:'%s'", (moved_node->old_data)->filename, new_file->filename); |
| 533 | DB_ATTR_TYPE uncompressed_changed = get_changed_hashsums((moved_node->old_data)->hashsums, new_hashsums, (moved_node->old_data), new_file, whoami); |
| 534 | if (uncompressed_changed) { |
| 535 | char *str = diff_attributes(0,uncompressed_changed); |
| 536 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ hashsums of old:'%s' and uncompressed hashsums of new:'%s' have been CHANGED: %s)", (moved_node->old_data)->filename, new_file->filename, str); |
| 537 | free(str); |
| 538 | } else { |
| 539 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ hashsums of old:'%s' and uncompressed hashsums of new:'%s' have NOT been changed)", (moved_node->old_data)->filename, new_file->filename); |
| 540 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 541 | break; |
| 542 | } |
| 543 | } else { |
| 544 | char *old_hashsums_str = diff_attributes(0,(moved_node->old_data)->attr&available_hashsums); |
| 545 | char *new_hashsums_str = diff_attributes(0,new_file->attr&available_hashsums); |
| 546 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ skip old:'%s' (no common hashsums with new:'%s', old hashsum(s): %s, new hashsum(s): %s)", (moved_node->old_data)->filename, new_file->filename, old_hashsums_str, new_hashsums_str); |
| 547 | free(old_hashsums_str); |
| 548 | free(new_hashsums_str); |
| 549 | } |
| 550 | } |
| 551 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 552 | } |
| 553 | moved_node = NULL; |
| 554 | } |
| 555 | pthread_rwlock_unlock(&(node->parent)->rwlock); |
| 556 | |
| 557 | for (int i = 0 ; i < num_hashes ; ++i) { |
| 558 | free(new_hashsums[i]); |
| 559 | } |
| 560 | if (moved_node) { |
| 561 | pthread_rwlock_wrlock(&moved_node->rwlock); |
| 562 | pthread_rwlock_wrlock(&node->rwlock); |
| 563 | if (!(moved_node->checked&NODE_MOVED_OUT)) { |
| 564 | LOG_WHOAMI(compare_log_level, "│ found old:'%s' with same common hashsum(s) as uncompressed file new:'%s'", (moved_node->old_data)->filename, new_file->filename); |
| 565 | LOG_WHOAMI(compare_log_level, "│ compare attributes of original file old:'%s' and compressed file new:'%s'", (moved_node->old_data)->filename, new_file->filename); |
| 566 | |
| 567 | DB_ATTR_TYPE compressed_ignored_attr = default_move_ignored_attr | get_hashsums_to_ignore((moved_node->old_data)->filename, (moved_node->old_data)->attr, new_file->filename, new_file->attr); |
| 568 | if (get_different_attributes(moved_node->old_data, new_file, compressed_ignored_attr, whoami)) { |
| 569 | LOG_WHOAMI(compare_log_level, "│ ignore old:'%s' as original file of compressed file new:'%s' (due to different attributes)", (moved_node->old_data)->filename, new_file->filename); |
| 570 | } else if (get_changed_attributes((moved_node->old_data), new_file, ATTR(attr_ctime)|ATTR(attr_size)|ATTR(attr_bcount)|ATTR(attr_inode), entry, false, whoami) == RETOK) { |
| 571 | node->checked |= NODE_MOVED_IN; |
| 572 | moved_node->checked |= NODE_MOVED_OUT; |
| 573 | LOG_WHOAMI(compare_log_level,_("│ accept old:'%s' as original file of compressed file new:'%s'"), (moved_node->old_data)->filename, new_file->filename); |
| 574 | LOG_WHOAMI(compare_log_level, "┴ finished '%s'", node->path); |
| 575 | pthread_rwlock_unlock(&node->rwlock); |
| 576 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 577 | return; |
| 578 | } else { |
| 579 | LOG_WHOAMI(compare_log_level,"│ ignore '%s' as original file of compressed file '%s' (due to changed attributes)", (moved_node->old_data)->filename, new_file->filename); |
| 580 | } |
| 581 | } else { |
| 582 | LOG_WHOAMI(compare_log_level, "│ '%s' has been already moved out", (moved_node->old_data)->filename); |
| 583 | } |
| 584 | pthread_rwlock_unlock(&node->rwlock); |
| 585 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 586 | } else { |
| 587 | LOG_WHOAMI(compare_log_level, "│ NO original file with same hashsum(s) found for compressed file new:'%s'", new_file->filename); |
| 588 | } |
| 589 | } else { |
| 590 | LOG_WHOAMI(compare_log_level, "│ calculation of uncompressed hashsums for comprressed file new:'%s' FAILED", new_file->filename); |
| 591 | } |
| 592 | } else { |
| 593 | LOG_WHOAMI(compare_log_level, "┝ new:'%s' has compressed attribute set, but skip hashsum calculation (NOT supported in dataase compare mode)", new_file->filename); |
| 594 | } |
| 595 | } else { |
| 596 | LOG_WHOAMI(compare_log_level, "┝ new:'%s' has compressed attribute set, but skip hashsum calculation (file has no hashsums set)", new_file->filename); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (node->parent != NULL) { /* root (/) has no parent */ |
| 602 | if (db_flags&DB_OLD) { |
| 603 | pthread_rwlock_wrlock(&(node->parent)->rwlock); |
| 604 | if(file->attr & ATTR(attr_checkinode)) { |
| 605 | LOG_WHOAMI(compare_log_level, "'%s' (inode: %li) has check inode attribute set, set NODE_CHECK_INODE_CHILD for parent '%s'", file->filename, file->inode, (node->parent)->path); |
| 606 | (node->parent)->checked |= NODE_CHECK_INODE; |
| 607 | } |
| 608 | pthread_rwlock_unlock(&(node->parent)->rwlock); |
| 609 | } else { |
| 610 | pthread_rwlock_rdlock(&node->rwlock); |
| 611 | db_line *new_file = node->new_data; |
| 612 | pthread_rwlock_unlock(&node->rwlock); |
| 613 | |
| 614 | pthread_rwlock_rdlock(&(node->parent)->rwlock); |
| 615 | if( (node->parent)->checked&NODE_CHECK_INODE && new_file != NULL ) { |
| 616 | LOG_WHOAMI(compare_log_level, "┝ parent directory (%s) of '%s' (inode: %li) has entries with check inode attribute set, search for source file with same inode", (node->parent)->path, new_file->filename, new_file->inode); |
| 617 | seltree* moved_node = NULL; |
| 618 | for(tree_node *x = tree_walk_first((node->parent)->children); x != NULL ; x = tree_walk_next(x)) { |
| 619 | moved_node = tree_get_data(x); |
| 620 | if (moved_node != node) { |
| 621 | pthread_rwlock_rdlock(&moved_node->rwlock); |
| 622 | if (moved_node->old_data != NULL && (moved_node->old_data)->attr & ATTR(attr_checkinode)) { |
| 623 | if ((moved_node->old_data)->inode == new_file->inode) { |
| 624 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 625 | break; |
| 626 | } else { |
| 627 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "│ '%s' has check inode attribute set but different inode", (moved_node->old_data)->filename); |
| 628 | } |
| 629 | } |
| 630 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 631 | } |
| 632 | moved_node = NULL; |
| 633 | } |
| 634 | if(moved_node != NULL) { |
| 635 | pthread_rwlock_wrlock(&moved_node->rwlock); |
| 636 | pthread_rwlock_wrlock(&node->rwlock); |
| 637 | db_line *newData = new_file; |
| 638 | db_line *oldData = moved_node->old_data; |
| 639 | if (!(moved_node->checked&NODE_MOVED_OUT)) { |
| 640 | LOG_WHOAMI(compare_log_level, "│ found old:'%s' with check inode attribute set and same inode as file new:'%s'", oldData->filename, newData->filename); |
| 641 | LOG_WHOAMI(compare_log_level, "│ compare attributes of source file old:'%s' and target file new:'%s'", oldData->filename, newData->filename); |
| 642 | DB_ATTR_TYPE move_ignored_attr = default_move_ignored_attr | get_hashsums_to_ignore(oldData->filename, oldData->attr, newData->filename, newData->attr); |
| 643 | if (get_different_attributes(oldData, newData, move_ignored_attr, whoami)) { |
| 644 | LOG_WHOAMI(compare_log_level, "│ ignore old:'%s' as source file of target file new:'%s' (due to different attributes)", oldData->filename, newData->filename); |
| 645 | } else if (get_changed_attributes(oldData, newData, ATTR(attr_ctime), entry, true, whoami) == RETOK) { |
| 646 | node->checked |= NODE_MOVED_IN; |
| 647 | moved_node->checked |= NODE_MOVED_OUT; |
| 648 | LOG_WHOAMI(compare_log_level, "│ accept old:'%s' as source file of target file new:'%s'", oldData->filename, newData->filename); |
| 649 | LOG_WHOAMI(compare_log_level, "┴ finished '%s'", node->path); |
| 650 | pthread_rwlock_unlock(&node->rwlock); |
| 651 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 652 | pthread_rwlock_unlock(&(node->parent)->rwlock); |
| 653 | return; |
| 654 | } else { |
| 655 | LOG_WHOAMI(compare_log_level, "│ ignore old:'%s' as source file of target file new:'%s' (due to changed attributes)", oldData->filename, newData->filename); |
| 656 | } |
| 657 | } else { |
| 658 | LOG_WHOAMI(compare_log_level, "│ '%s' has been already moved out", oldData->filename); |
| 659 | } |
| 660 | pthread_rwlock_unlock(&node->rwlock); |
| 661 | pthread_rwlock_unlock(&moved_node->rwlock); |
| 662 | } else { |
| 663 | LOG_WHOAMI(compare_log_level, "│ no source file found for target file '%s'", new_file->filename); |
| 664 | } |
| 665 | } |
| 666 | pthread_rwlock_unlock(&(node->parent)->rwlock); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | pthread_rwlock_wrlock(&node->rwlock); |
| 671 | if( (db_flags&DB_NEW) && |
| 672 | (node->new_data!=NULL) && |
| 673 | (file->attr & ATTR(attr_allownewfile)) ){ |
| 674 | node->checked|=NODE_ALLOW_NEW; |
| 675 | LOG_WHOAMI(compare_log_level,_("│ '%s' has ANF attribute set, ignore addition of entry in the report"), file->filename); |
| 676 | } |
| 677 | if( (db_flags&DB_OLD) && |
| 678 | (node->old_data!=NULL) && |
| 679 | (file->attr & ATTR(attr_allowrmfile)) ){ |
| 680 | node->checked|=NODE_ALLOW_RM; |
| 681 | LOG_WHOAMI(compare_log_level,_("'%s' has ARF attribute set, ignore removal of entry in the report"), file->filename); |
| 682 | } |
| 683 | if (!(db_flags&DB_OLD)) { |
| 684 | LOG_WHOAMI(compare_log_level,"┴ finished '%s'", node->path); |
| 685 | } |
| 686 | pthread_rwlock_unlock(&node->rwlock); |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | match_result check_limit(char* filename, bool log_partial_match, const char *whoami) { |
| 692 | if(conf->limit!=NULL) { |
| 693 | int match=pcre2_match(conf->limit_crx, (PCRE2_SPTR) filename, PCRE2_ZERO_TERMINATED, 0, PCRE2_PARTIAL_SOFT, conf->limit_md, NULL); |
| 694 | if (match >= 0) { |
| 695 | LOG_WHOAMI(LOG_LEVEL_TRACE, "'%s' does match limit '%s'", filename, conf->limit); |
| 696 | return 0; |
| 697 | } else if (match == PCRE2_ERROR_PARTIAL) { |
| 698 | if (log_partial_match) { |
| 699 | LOG_WHOAMI(LOG_LEVEL_LIMIT, "skip '%s' (reason: partial limit match, limit: '%s')", filename, conf->limit); |
| 700 | } |
| 701 | return RESULT_PARTIAL_LIMIT_MATCH; |
| 702 | } else { |
| 703 | LOG_WHOAMI(LOG_LEVEL_LIMIT, "skip '%s' (reason: no limit match, limit '%s')", filename, conf->limit); |
| 704 | return RESULT_NO_LIMIT_MATCH; |
| 705 | } |
| 706 | } |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | match_t check_rxtree(file_t file, seltree* tree, char* source, bool check_parent_dirs, const char *whoami) { |
| 711 | match_result limit_result = check_limit(file.name, !(file.type&FT_DIR), whoami); |
| 712 | match_t match; |
| 713 | if (limit_result) { |
| 714 | if (limit_result == RESULT_PARTIAL_LIMIT_MATCH && file.type&FT_DIR) { |
| 715 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u252c partial limit match (limit: '%s') for directory '%s', check for no-recurse match", conf->limit, file.name); |
| 716 | match = check_seltree(tree, file, check_parent_dirs, whoami); |
| 717 | if (match.result == RESULT_NON_RECURSIVE_NEGATIVE_MATCH || match.result == RESULT_NO_RULE_MATCH) { |
| 718 | match.result = RESULT_PART_LIMIT_AND_NO_RECURSE_MATCH; |
| 719 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2534 no-recurse match for '%s', stop directory processing", file.name); |
| 720 | LOG_WHOAMI(LOG_LEVEL_TRACE, "check_rxtree: match result %s (%d) for '%s'", get_match_result_string(match.result), match.result, file.name); |
| 721 | return match; |
| 722 | } else { |
| 723 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2534 no no-recurse match for '%s'", file.name); |
| 724 | } |
| 725 | } |
| 726 | match = (match_t) { limit_result, NULL, 0 }; |
| 727 | LOG_WHOAMI(LOG_LEVEL_TRACE, "check_rxtree: match result %s (%d) for '%s'", get_match_result_string(match.result), match.result, file.name); |
| 728 | return match; |
| 729 | } |
| 730 | |
| 731 | #ifdef HAVE_FSTYPE |
| 732 | char * fs_type_str = get_fs_type_string_from_magic(file.fs_type); |
| 733 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u252c process '%s' from %s (filetype: %c, file system type: %s)", file.name, source, get_f_type_char_from_f_type(file.type), fs_type_str); |
| 734 | free(fs_type_str); |
| 735 | #else |
| 736 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u252c process '%s' from %s (filetype: %c)", file.name, source, get_f_type_char_from_f_type(file.type)); |
| 737 | #endif |
| 738 | match = check_seltree(tree, file, check_parent_dirs, whoami); |
| 739 | if (match.result == RESULT_SELECTIVE_MATCH || match.result == RESULT_EQUAL_MATCH) { |
| 740 | char *str; |
| 741 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2534 ADD '%s' (attr: '%s')", file.name, str = diff_attributes(0, match.rule->attr)); |
| 742 | free(str); |
| 743 | } else { |
| 744 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2534 do NOT add '%s'", file.name); |
| 745 | } |
| 746 | LOG_WHOAMI(LOG_LEVEL_TRACE, "check_rxtree: match result %s (%d) for '%s'", get_match_result_string(match.result), match.result, file.name); |
| 747 | return match; |
| 748 | } |
| 749 | |
| 750 | db_line* get_file_attrs(disk_entry *file, DB_ATTR_TYPE attrs, DB_ATTR_TYPE extra_hashsums, const char *whoami) { |
| 751 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "get file attributes '%s' (fullpath: '%s')", &file->filename[conf->root_prefix_length], file->filename); |
| 752 | db_line* line=NULL; |
| 753 | time_t cur_time; |
| 754 | |
| 755 | /* |
| 756 | Get current time for future time notification. |
| 757 | */ |
| 758 | cur_time=time(NULL); |
| 759 | |
| 760 | if (cur_time==(time_t)-1) { |
| 761 | log_msg(LOG_LEVEL_WARNING, "can't get current time: %s", strerror(errno)); |
| 762 | } else { |
| 763 | |
| 764 | if(file->fs.st_atime>cur_time){ |
| 765 | log_msg(LOG_LEVEL_NOTICE,_("%s atime in future"),file->filename); |
| 766 | } |
| 767 | if(file->fs.st_mtime>cur_time){ |
| 768 | log_msg(LOG_LEVEL_NOTICE,_("%s mtime in future"),file->filename); |
| 769 | } |
| 770 | if(file->fs.st_ctime>cur_time){ |
| 771 | log_msg(LOG_LEVEL_NOTICE,_("%s ctime in future"),file->filename); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | line = checked_calloc(1, sizeof(db_line)); |
| 776 | |
| 777 | /* |
| 778 | We want filename |
| 779 | */ |
| 780 | |
| 781 | line->attr=attrs|ATTR(attr_filename); |
| 782 | |
| 783 | /* |
| 784 | Just copy some needed fields. |
| 785 | */ |
| 786 | |
| 787 | line->fullpath = checked_strdup(file->filename); |
| 788 | line->filename=&line->fullpath[conf->root_prefix_length]; |
| 789 | line->perm_o=file->fs.st_mode; |
| 790 | line->linkname=NULL; |
| 791 | |
| 792 | #ifdef HAVE_FSTYPE |
| 793 | if(ATTR(attr_fs_type)&line->attr) { |
| 794 | line->fs_type = file->fs_type; |
| 795 | } else { |
| 796 | line->fs_type = 0; |
| 797 | } |
| 798 | #endif |
| 799 | |
| 800 | /* |
| 801 | Handle symbolic link |
| 802 | */ |
| 803 | |
| 804 | hsymlnk(line); |
| 805 | |
| 806 | /* |
| 807 | Set normal part |
| 808 | */ |
| 809 | |
| 810 | fs2db_line(&file->fs, line); |
| 811 | |
| 812 | /* |
| 813 | ACL stuff |
| 814 | */ |
| 815 | |
| 816 | #ifdef WITH_ACL |
| 817 | acl2line(line, file->fd, whoami); |
| 818 | #endif |
| 819 | |
| 820 | #ifdef WITH_XATTR |
| 821 | xattrs2line(line, file->fd, whoami); |
| 822 | #endif |
| 823 | |
| 824 | #ifdef WITH_SELINUX |
| 825 | selinux2line(line, file->fd, whoami); |
| 826 | #endif |
| 827 | |
| 828 | #ifdef WITH_E2FSATTRS |
| 829 | e2fsattrs2line(line, file->fd, whoami); |
| 830 | #endif |
| 831 | |
| 832 | #ifdef WITH_CAPABILITIES |
| 833 | capabilities2line(line, file->fd, whoami); |
| 834 | #endif |
| 835 | |
| 836 | DB_ATTR_TYPE all_hashsums = get_hashes(true); |
| 837 | if (line->attr&all_hashsums) { |
| 838 | md_hashsums hs = calc_hashsums(file, line->attr|extra_hashsums, -1, false, whoami); |
| 839 | if (hs.attrs) { |
| 840 | hashsums2line(&hs,line, whoami); |
| 841 | } else { |
| 842 | line->attr&=~all_hashsums; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return line; |
| 847 | } |
| 848 | |
| 849 | void write_tree(seltree* node) { |
| 850 | pthread_rwlock_rdlock(&node->rwlock); |
| 851 | if (node->checked&DB_NEW) { |
| 852 | progress_status(PROGRESS_WRITEDB, (node->new_data)->filename); |
| 853 | db_writeline(node->new_data,conf); |
| 854 | if (node->checked&NODE_FREE) { |
| 855 | free_db_line(node->new_data); |
| 856 | free(node->new_data); |
| 857 | node->new_data=NULL; |
| 858 | } |
| 859 | } |
| 860 | for(tree_node *n = tree_walk_first(node->children); n != NULL ; n = tree_walk_next(n)) { |
| 861 | write_tree(tree_get_data(n)); |
| 862 | } |
| 863 | pthread_rwlock_unlock(&node->rwlock); |
| 864 | } |
| 865 | |
| 866 | void populate_tree(seltree* tree) { |
| 867 | db_entry_t entry; |
| 868 | if((conf->action&DO_COMPARE)||(conf->action&DO_DIFF)){ |
| 869 | progress_status(PROGRESS_OLDDB, NULL); |
| 870 | log_msg(LOG_LEVEL_INFO, "read old entries from database: %s", (conf->database_in.url)->raw); |
| 871 | while((entry = db_readline(&(conf->database_in), conf->action&DO_INIT)).line != NULL) { |
| 872 | if (entry.limit) { |
| 873 | add_file_to_tree(tree,entry.line,DB_OLD|DB_NEW, &(conf->database_in), NULL, NULL); |
| 874 | } else { |
| 875 | add_file_to_tree(tree,entry.line,DB_OLD, &(conf->database_in), NULL, NULL); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | if(conf->action&DO_DIFF){ |
| 880 | progress_status(PROGRESS_NEWDB, NULL); |
| 881 | log_msg(LOG_LEVEL_INFO, "read new entries from database: %s", (conf->database_new.url)->raw); |
| 882 | while((entry = db_readline(&(conf->database_new), false)).line != NULL){ |
| 883 | add_file_to_tree(tree,entry.line,DB_NEW, &(conf->database_new), NULL, NULL); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | if((conf->action&DO_INIT)||(conf->action&DO_COMPARE)){ |
| 888 | progress_status(PROGRESS_DISK, NULL); |
| 889 | log_msg(LOG_LEVEL_INFO, "read new entries from disk (limit: '%s', root prefix: '%s')", conf->limit?conf->limit:"(none)", conf->root_prefix); |
| 890 | |
| 891 | db_scan_disk(false); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | void hsymlnk(db_line* line) { |
| 896 | |
| 897 | line->linkname = NULL; |
| 898 | if (line->attr&ATTR(attr_linkname)) { |
| 899 | if((S_ISLNK(line->perm_o))){ |
| 900 | int len=0; |
| 901 | #ifdef WITH_ACL |
| 902 | if(conf->no_acl_on_symlinks!=1) { |
| 903 | line->attr&=(~ATTR(attr_acl)); |
| 904 | } |
| 905 | #endif |
| 906 | |
| 907 | if(conf->warn_dead_symlinks==1) { |
| 908 | struct stat fs; |
| 909 | int sres; |
| 910 | sres=stat(line->fullpath,&fs); |
| 911 | if (sres!=0 && sres!=EACCES) { |
| 912 | log_msg(LOG_LEVEL_WARNING,"Dead symlink detected at %s",line->fullpath); |
| 913 | } |
| 914 | if(!(line->attr&ATTR(attr_rdev))) { |
| 915 | fs.st_rdev=0; |
| 916 | } |
| 917 | } |
| 918 | /* |
| 919 | Is this valid?? |
| 920 | No, We should do this elsewhere. |
| 921 | */ |
| 922 | line->linkname=(char*)checked_malloc(_POSIX_PATH_MAX+1); |
| 923 | |
| 924 | /* |
| 925 | Remember to nullify the buffer, because man page says |
| 926 | |
| 927 | readlink places the contents of the symbolic link path in |
| 928 | the buffer buf, which has size bufsiz. readlink does not |
| 929 | append a NUL character to buf. It will truncate the con- |
| 930 | tents (to a length of bufsiz characters), in case the |
| 931 | buffer is too small to hold all of the contents. |
| 932 | |
| 933 | */ |
| 934 | memset(line->linkname,0,_POSIX_PATH_MAX+1); |
| 935 | |
| 936 | len=readlink(line->fullpath,line->linkname,_POSIX_PATH_MAX+1); |
| 937 | if (len < 0) { |
| 938 | log_msg(LOG_LEVEL_WARNING, "readlink() failed for '%s': %s", line->fullpath, strerror(errno)); |
| 939 | line->attr&=(~ATTR(attr_linkname)); |
| 940 | free(line->linkname); |
| 941 | line->linkname = NULL; |
| 942 | } else { |
| 943 | line->linkname=checked_realloc(line->linkname,len+1); |
| 944 | } |
| 945 | } else { |
| 946 | line->attr&=(~ATTR(attr_linkname)); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | } |
| 951 | // vi: ts=8 sw=2 |