| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2006, 2009-2011, 2015-2016, 2019-2025 Rami Lehti, |
| 5 | * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "file.h" |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <pthread.h> |
| 27 | #define PCRE2_CODE_UNIT_WIDTH 8 |
| 28 | #include <pcre2.h> |
| 29 | #include "attributes.h" |
| 30 | #include "list.h" |
| 31 | #include "log.h" |
| 32 | #include <string.h> |
| 33 | #include "rx_rule.h" |
| 34 | #include "seltree.h" |
| 35 | #include "seltree_struct.h" |
| 36 | #include "util.h" |
| 37 | #include "errorcodes.h" |
| 38 | #include "db.h" |
| 39 | |
| 40 | void log_tree(LOG_LEVEL log_level, seltree* node, int depth) { |
| 41 | |
| 42 | list* r; |
| 43 | rx_rule* rxc; |
| 44 | |
| 45 | pthread_rwlock_rdlock(&node->rwlock); |
| 46 | |
| 47 | log_msg(log_level, "%-*s %s:", depth, depth?"\u251d":"\u250c", node->path); |
| 48 | |
| 49 | char *attr_str, *rs_str; |
| 50 | |
| 51 | for(r=node->equ_rx_lst;r!=NULL;r=r->next) { |
| 52 | rxc=r->data; |
| 53 | log_msg(log_level, "%-*s '=%s %s %s' (%s:%d: '%s%s%s')", depth+2, "\u2502", rxc->rx, rs_str = get_restriction_string(rxc->restriction), attr_str = diff_attributes(0, rxc->attr), rxc->config_filename, rxc->config_linenumber, rxc->config_line, rxc->prefix?"', prefix: '":"", rxc->prefix?rxc->prefix:""); |
| 54 | free(rs_str); |
| 55 | free(attr_str); |
| 56 | } |
| 57 | for(r=node->sel_rx_lst;r!=NULL;r=r->next) { |
| 58 | rxc=r->data; |
| 59 | log_msg(log_level, "%-*s '%s %s %s' (%s:%d: '%s%s%s')", depth+2, "\u2502", rxc->rx, rs_str = get_restriction_string(rxc->restriction), attr_str = diff_attributes(0, rxc->attr), rxc->config_filename, rxc->config_linenumber, rxc->config_line, rxc->prefix?"', prefix: '":"", rxc->prefix?rxc->prefix:""); |
| 60 | free(rs_str); |
| 61 | free(attr_str); |
| 62 | } |
| 63 | for(r=node->neg_rx_lst;r!=NULL;r=r->next) { |
| 64 | rxc=r->data; |
| 65 | log_msg(log_level, "%-*s '%s%s %s' (%s:%d: '%s%s%s')", depth+2, "\u2502", get_rule_type_char(rxc->type), rxc->rx, rs_str = get_restriction_string(rxc->restriction), rxc->config_filename, rxc->config_linenumber, rxc->config_line, rxc->prefix?"', prefix: '":"", rxc->prefix?rxc->prefix:""); |
| 66 | free(rs_str); |
| 67 | } |
| 68 | |
| 69 | for(tree_node *n = tree_walk_first(node->children); n != NULL ; n = tree_walk_next(n)) { |
| 70 | log_tree(log_level, tree_get_data(n), depth+2); |
| 71 | } |
| 72 | |
| 73 | pthread_rwlock_unlock(&node->rwlock); |
| 74 | |
| 75 | if (depth == 0) { |
| 76 | log_msg(log_level, "%s", "\u2514"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * strrxtok() |
| 82 | * return a pointer to a copy of the non-regexp path part of the argument |
| 83 | */ |
| 84 | static char* strrxtok(char* rx) |
| 85 | { |
| 86 | char*p=NULL; |
| 87 | size_t i=0; |
| 88 | |
| 89 | /* The following code assumes that the first character is a slash */ |
| 90 | size_t lastslash=1; |
| 91 | |
| 92 | p=checked_strdup(rx); |
| 93 | p[0]='/'; |
| 94 | |
| 95 | for(i=1;i<strlen(p);i++){ |
| 96 | switch(p[i]) |
| 97 | { |
| 98 | case '/': |
| 99 | lastslash=i; |
| 100 | break; |
| 101 | case '(': |
| 102 | case '^': |
| 103 | case '$': |
| 104 | case '?': |
| 105 | case '*': |
| 106 | case '[': |
| 107 | i=strlen(p); |
| 108 | break; |
| 109 | case '\\': |
| 110 | for (int j = i; p[j]; j++) { |
| 111 | p[j] = p[j+1]; |
| 112 | } |
| 113 | break; |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | p[lastslash]='\0'; |
| 120 | |
| 121 | return p; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static seltree *create_seltree_node(char *path, seltree *parent) { |
| 126 | seltree *node = checked_malloc(sizeof(seltree)); /* not to be freed */ |
| 127 | |
| 128 | node->path = checked_strdup(path); /* not to be freed */ |
| 129 | node->parent = parent; |
| 130 | |
| 131 | pthread_rwlock_init(&node->rwlock, NULL); |
| 132 | |
| 133 | node->sel_rx_lst = NULL; |
| 134 | node->neg_rx_lst = NULL; |
| 135 | node->equ_rx_lst = NULL; |
| 136 | |
| 137 | node->children = NULL; |
| 138 | |
| 139 | node->checked = 0; |
| 140 | node->new_data = NULL; |
| 141 | node->old_data = NULL; |
| 142 | node->changed_attrs = 0; |
| 143 | |
| 144 | return node; |
| 145 | } |
| 146 | |
| 147 | static seltree* _get_seltree_node(seltree* node, char *path, bool create) { |
| 148 | LOG_LEVEL log_level = LOG_LEVEL_TRACE; |
| 149 | pthread_rwlock_rdlock(&node->rwlock); |
| 150 | log_msg(log_level, "_get_seltree_node(): %s> node: '%s' (%p), create: %s", path, node->path, (void*) node, btoa(create)); |
| 151 | pthread_rwlock_unlock(&node->rwlock); |
| 152 | seltree *parent = NULL; |
| 153 | char *tmp = checked_strdup(path); |
| 154 | |
| 155 | if (node && strcmp(node->path, path) != 0) { |
| 156 | char *next_dir = path;; |
| 157 | do { |
| 158 | parent = node; |
| 159 | next_dir = strchr(&next_dir[1], '/'); |
| 160 | if (next_dir) { tmp[next_dir-path] = '\0'; } |
| 161 | pthread_rwlock_rdlock(&parent->rwlock); |
| 162 | log_msg(log_level, "_get_seltree_node(): %s> search for child node '%s' (parent: '%s' (%p))", path, strrchr(tmp,'/'), parent->path, (void*) parent); |
| 163 | node = tree_search(parent->children, strrchr(tmp,'/'), (tree_cmp_f) strcmp); |
| 164 | pthread_rwlock_unlock(&parent->rwlock); |
| 165 | if (create && node == NULL) { |
| 166 | pthread_rwlock_wrlock(&parent->rwlock); |
| 167 | node = tree_search(parent->children, strrchr(tmp,'/'), (tree_cmp_f) strcmp); |
| 168 | if (node == NULL) { |
| 169 | node = create_seltree_node(tmp, parent); |
| 170 | parent ->children = tree_insert(parent->children, strrchr(node->path,'/'), (void*)node, (tree_cmp_f) strcmp); |
| 171 | log_msg(log_level, "_get_seltree_node(): %s> created new %s node '%s' (%p) (parent: %p)", path, next_dir?"inner":"leaf", tmp, (void*) node, (void*) parent); |
| 172 | } |
| 173 | pthread_rwlock_unlock(&parent->rwlock); |
| 174 | } |
| 175 | if (next_dir) { tmp[next_dir-path] = '/'; } |
| 176 | } while (node != NULL && next_dir); |
| 177 | } |
| 178 | free(tmp); |
| 179 | if (node == NULL) { |
| 180 | log_msg(log_level, "_get_seltree_node(): %s> return NULL (node == NULL)", path); |
| 181 | } else { |
| 182 | pthread_rwlock_rdlock(&node->rwlock); |
| 183 | log_msg(log_level, "_get_seltree_node(): %s> return node: '%s' (%p)", path, node->path, (void*) node); |
| 184 | pthread_rwlock_unlock(&node->rwlock); |
| 185 | } |
| 186 | return node; |
| 187 | } |
| 188 | |
| 189 | seltree* get_or_create_seltree_node(seltree* node, char *path) { |
| 190 | return _get_seltree_node(node, path, true); |
| 191 | } |
| 192 | |
| 193 | seltree* get_seltree_node(seltree* node, char *path) { |
| 194 | return _get_seltree_node(node, path, false); |
| 195 | } |
| 196 | |
| 197 | seltree *init_tree(void) { |
| 198 | seltree *node = create_seltree_node("/", NULL); |
| 199 | log_msg(LOG_LEVEL_DEBUG, "created root node '%s' (%p)", node->path, (void*) node); |
| 200 | return node; |
| 201 | } |
| 202 | |
| 203 | bool is_tree_empty(seltree *node) { |
| 204 | pthread_rwlock_rdlock(&node->rwlock); |
| 205 | bool is_empty = (node->children == NULL |
| 206 | && node->equ_rx_lst == NULL |
| 207 | && node->sel_rx_lst == NULL |
| 208 | && node->neg_rx_lst == NULL |
| 209 | ); |
| 210 | pthread_rwlock_unlock(&node->rwlock); |
| 211 | return is_empty; |
| 212 | } |
| 213 | |
| 214 | rx_rule * add_rx_to_tree(char * rx, rx_restriction_t restriction, AIDE_RULE_TYPE rule_type, seltree *tree, int linenumber, char* filename, char* linebuf, char **node_path) { |
| 215 | rx_rule* r = NULL; |
| 216 | seltree *curnode = NULL; |
| 217 | char *rxtok = NULL; |
| 218 | |
| 219 | r=(rx_rule*)checked_malloc(sizeof(rx_rule)); |
| 220 | |
| 221 | r->rx=rx; |
| 222 | r->type = rule_type; |
| 223 | r->restriction = restriction; |
| 224 | |
| 225 | r->config_linenumber = linenumber; |
| 226 | r->config_filename = filename; |
| 227 | r->config_line = checked_strdup(linebuf); |
| 228 | r->prefix = NULL; |
| 229 | r->attr = 0; |
| 230 | |
| 231 | int pcre2_errorcode; |
| 232 | PCRE2_SIZE pcre2_erroffset; |
| 233 | |
| 234 | if((r->crx=pcre2_compile((PCRE2_SPTR) r->rx, PCRE2_ZERO_TERMINATED, PCRE2_UTF|PCRE2_ANCHORED, &pcre2_errorcode, &pcre2_erroffset, NULL)) == NULL) { |
| 235 | PCRE2_UCHAR pcre2_error[128]; |
| 236 | pcre2_get_error_message(pcre2_errorcode, pcre2_error, 128); |
| 237 | log_msg(LOG_LEVEL_ERROR, "%s:%d:%zu: error in rule '%s': %s (line: '%s')", filename, linenumber, pcre2_erroffset, rx, pcre2_error, linebuf); |
| 238 | free(r); |
| 239 | return NULL; |
| 240 | } else { |
| 241 | r->md = pcre2_match_data_create_from_pattern(r->crx, NULL); |
| 242 | if (r->md == NULL) { |
| 243 | log_msg(LOG_LEVEL_ERROR, "pcre2_match_data_create_from_pattern: failed to allocate memory"); |
| 244 | exit(MEMORY_ALLOCATION_FAILURE); |
| 245 | } |
| 246 | int pcre2_jit = pcre2_jit_compile(r->crx, PCRE2_JIT_PARTIAL_SOFT); |
| 247 | if (pcre2_jit < 0) { |
| 248 | PCRE2_UCHAR pcre2_error[128]; |
| 249 | pcre2_get_error_message(pcre2_jit, pcre2_error, 128); |
| 250 | log_msg(LOG_LEVEL_DEBUG, "JIT compilation for regex '%s' failed: %s (fall back to interpreted matching)", r->rx, pcre2_error); |
| 251 | } else { |
| 252 | log_msg(LOG_LEVEL_DEBUG, "JIT compilation for regex '%s' successful", r->rx); |
| 253 | } |
| 254 | |
| 255 | rxtok=strrxtok(r->rx); |
| 256 | |
| 257 | for(size_t i=1;i < strlen(rxtok); ++i){ |
| 258 | if (rxtok[i] == '/' && rxtok[i-1] == '/') { |
| 259 | log_msg(LOG_LEVEL_ERROR, "%s:%d:1: error in rule '%s': invalid double slash (line: '%s')", filename, linenumber, rx, linebuf); |
| 260 | free(r); |
| 261 | return NULL; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | curnode = get_or_create_seltree_node(tree, rxtok); |
| 266 | |
| 267 | pthread_rwlock_wrlock(&curnode->rwlock); |
| 268 | *node_path = curnode->path; |
| 269 | switch (rule_type){ |
| 270 | case AIDE_RECURSIVE_NEGATIVE_RULE: |
| 271 | case AIDE_NON_RECURSIVE_NEGATIVE_RULE:{ |
| 272 | curnode->neg_rx_lst=list_append(curnode->neg_rx_lst,(void*)r); |
| 273 | break; |
| 274 | } |
| 275 | case AIDE_EQUAL_RULE:{ |
| 276 | curnode->equ_rx_lst=list_append(curnode->equ_rx_lst,(void*)r); |
| 277 | break; |
| 278 | } |
| 279 | case AIDE_SELECTIVE_RULE:{ |
| 280 | curnode->sel_rx_lst=list_append(curnode->sel_rx_lst,(void*)r); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | pthread_rwlock_unlock(&curnode->rwlock); |
| 285 | free(rxtok); |
| 286 | |
| 287 | while (curnode) { |
| 288 | pthread_rwlock_t *rwlock = &curnode->rwlock; |
| 289 | pthread_rwlock_wrlock(rwlock); |
| 290 | if(curnode->checked&NODE_HAS_SUB_RULES) { |
| 291 | curnode = NULL; |
| 292 | } else { |
| 293 | log_msg(LOG_LEVEL_DEBUG, "set NODE_HAS_SUB_RULES for node '%s' (%p)", curnode->path, (void*) curnode); |
| 294 | curnode->checked |= NODE_HAS_SUB_RULES; |
| 295 | curnode = curnode->parent; |
| 296 | } |
| 297 | pthread_rwlock_unlock(rwlock); |
| 298 | } |
| 299 | } |
| 300 | return r; |
| 301 | } |
| 302 | |
| 303 | #define LOG_MATCH(log_level, border, format, ...) \ |
| 304 | log_msg(log_level, "%*s%s%s %*c'%s' " #format " of %s (%s:%d: '%s%s%s')", whoami?10:0, whoami?whoami:"", whoami?": ":"", border, depth, ' ', file.name, __VA_ARGS__, get_rule_type_long_string(rx->type), rx->config_filename, rx->config_linenumber, rx->config_line, rx->prefix?"', prefix: '":"", rx->prefix?rx->prefix:""); |
| 305 | |
| 306 | static int check_list_for_match(list* rxrlist, file_t file, rx_rule* *rule, int depth, const char* whoami) |
| 307 | { |
| 308 | list* r=NULL; |
| 309 | int retval=RESULT_NO_RULE_MATCH; |
| 310 | int pcre_retval; |
| 311 | char *rs_str = NULL; |
| 312 | for(r=rxrlist;r;r=r->next){ |
| 313 | rx_rule *rx = (rx_rule*)r->data; |
| 314 | |
| 315 | pcre_retval = pcre2_match(rx->crx, (PCRE2_SPTR) file.name, PCRE2_ZERO_TERMINATED, 0, PCRE2_PARTIAL_SOFT, rx->md, NULL); |
| 316 | if (pcre_retval >= 0) { /* matching regex */ |
| 317 | if (!rx->restriction.f_type || file.type&rx->restriction.f_type) { /* no file type restriction OR matching file type */ |
| 318 | #ifdef HAVE_FSTYPE |
| 319 | if (rx->restriction.fs_type && file.fs_type == 0) { /* no file system data but file system type restricted rule */ |
| 320 | log_msg(LOG_LEVEL_WARNING, "unable to match the file system type restriction of %s (%s:%d: '%s%s%s'): file system type missing for '%s'", get_rule_type_long_string(rx->type), rx->config_filename, rx->config_linenumber, rx->config_line, rx->prefix?"', prefix: '":"", rx->prefix?rx->prefix:"", file.name); |
| 321 | /* RESULT_NO_RULE_MATCH */ |
| 322 | } else if (!rx->restriction.fs_type || rx->restriction.fs_type == file.fs_type) { /* no file system restriction OR matching file sytem */ |
| 323 | #endif |
| 324 | *rule = rx; |
| 325 | LOG_MATCH(LOG_LEVEL_RULE, "\u251d", matches regex '%s' and restriction '%s', rx->rx, rs_str = get_restriction_string(rx->restriction)) |
| 326 | free(rs_str); |
| 327 | switch(rx->type) { |
| 328 | case AIDE_SELECTIVE_RULE: |
| 329 | retval = RESULT_SELECTIVE_MATCH; |
| 330 | break; |
| 331 | case AIDE_EQUAL_RULE: |
| 332 | retval = RESULT_EQUAL_MATCH; |
| 333 | break; |
| 334 | case AIDE_RECURSIVE_NEGATIVE_RULE: |
| 335 | retval = RESULT_RECURSIVE_NEGATIVE_MATCH; |
| 336 | break; |
| 337 | case AIDE_NON_RECURSIVE_NEGATIVE_RULE: |
| 338 | retval = RESULT_NON_RECURSIVE_NEGATIVE_MATCH; |
| 339 | break; |
| 340 | } |
| 341 | break; |
| 342 | #ifdef HAVE_FSTYPE |
| 343 | } else { /* file system restriction does not match */ |
| 344 | LOG_MATCH(LOG_LEVEL_DEBUG, "\u2502", does not match file system of restriction '%s', rs_str = get_restriction_string(rx->restriction)) |
| 345 | free(rs_str); |
| 346 | retval=RESULT_PARTIAL_MATCH; |
| 347 | } |
| 348 | #endif |
| 349 | } else { /* file type restriction does not match */ |
| 350 | LOG_MATCH(LOG_LEVEL_DEBUG, "\u2502", does not match file type of rule restriction '%s', rs_str = get_restriction_string(rx->restriction)) |
| 351 | free(rs_str); |
| 352 | retval=RESULT_PARTIAL_MATCH; |
| 353 | } |
| 354 | } else if (pcre_retval == PCRE2_ERROR_PARTIAL) { /* partial match of regex */ |
| 355 | LOG_MATCH(LOG_LEVEL_DEBUG, "\u2502", partially matches regex '%s', rx->rx) |
| 356 | retval=RESULT_PARTIAL_MATCH; |
| 357 | } else { /* regex does not match */ |
| 358 | LOG_MATCH(LOG_LEVEL_DEBUG, "\u2502", does not match regex '%s', rx->rx) |
| 359 | /* RESULT_NO_RULE_MATCH */ |
| 360 | } |
| 361 | } |
| 362 | return retval; |
| 363 | } |
| 364 | |
| 365 | static match_result _get_default_match_result(const seltree *node, int depth, const char* whoami) { |
| 366 | if (node->checked&NODE_HAS_SUB_RULES) { |
| 367 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cdirectory node '%s' (%p) has NODE_HAS_SUB_RULES set (set default match result to RESULT_PARTIAL_MATCH)", depth, ' ', node->path, (void*) node); |
| 368 | return RESULT_PARTIAL_MATCH; |
| 369 | } |
| 370 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cdirectory node '%s' (%p) has NODE_HAS_SUB_RULES NOT set (keep default match result at RESULT_NO_RULE_MATCH)", depth, ' ', node->path, (void*) node); |
| 371 | return RESULT_NO_RULE_MATCH; |
| 372 | } |
| 373 | |
| 374 | static match_t check_node_for_match(seltree *pnode, file_t file, const char* whoami) { |
| 375 | |
| 376 | match_t match = { RESULT_NO_RULE_MATCH, NULL, 0 }; |
| 377 | match_result result; |
| 378 | int depth = 1; |
| 379 | |
| 380 | char *last_slash = strrchr(file.name,'/'); |
| 381 | int parent_length = (last_slash != file.name?last_slash-file.name:0); |
| 382 | |
| 383 | pthread_rwlock_rdlock(&pnode->rwlock); |
| 384 | LOG_WHOAMI(LOG_LEVEL_TRACE, "\u2502 check_node_for_match: pnode: '%s' (%p), filename: '%s', file_type: %c", pnode->path, (void*) pnode, file.name, get_f_type_char_from_f_type(file.type)); |
| 385 | if (strncmp(pnode->path, file.name, parent_length) == 0) { |
| 386 | |
| 387 | if (file.type == FT_DIR) { |
| 388 | if (strcmp(pnode->path, file.name) == 0) { |
| 389 | match.result = _get_default_match_result(pnode, depth, whoami); |
| 390 | } else { |
| 391 | seltree * child_node = tree_search(pnode->children, last_slash, (tree_cmp_f) strcmp); |
| 392 | if (child_node) { |
| 393 | pthread_rwlock_rdlock(&child_node->rwlock); |
| 394 | match.result = _get_default_match_result(child_node, depth, whoami); |
| 395 | pthread_rwlock_unlock(&child_node->rwlock); |
| 396 | } else { |
| 397 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cno node for directory '%s' exists (keep default match result at RESULT_NO_RULE_MATCH)", depth, ' ', file.name); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (pnode->equ_rx_lst) { |
| 403 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2502 %*cnode: '%s': check equal list", depth, ' ', pnode->path); |
| 404 | result = check_list_for_match(pnode->equ_rx_lst, file, &match.rule, depth+2, whoami); |
| 405 | if (result == RESULT_EQUAL_MATCH || result == RESULT_PARTIAL_MATCH) { |
| 406 | match.result = result; |
| 407 | } |
| 408 | } else { |
| 409 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip equal list (reason: list is empty)", depth, ' ', pnode->path); |
| 410 | } |
| 411 | } else { |
| 412 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s' skip equal list (reason: not on top level)", depth, ' ', pnode->path); |
| 413 | } |
| 414 | pthread_rwlock_unlock(&pnode->rwlock); |
| 415 | |
| 416 | /* seltree* stack for negative rules */ |
| 417 | int i = 0; |
| 418 | seltree * p = pnode; |
| 419 | while (p) { |
| 420 | pthread_rwlock_t *rwlock = &p->rwlock; |
| 421 | pthread_rwlock_rdlock(rwlock); |
| 422 | p = p->parent; |
| 423 | i++; |
| 424 | pthread_rwlock_unlock(rwlock); |
| 425 | } |
| 426 | seltree* *nodes = checked_malloc(sizeof(seltree*)*i); |
| 427 | |
| 428 | i = 0; |
| 429 | seltree *next_parent = NULL;; |
| 430 | /* check selective rules down -> top */ |
| 431 | do { |
| 432 | pthread_rwlock_rdlock(&pnode->rwlock); |
| 433 | if (pnode->sel_rx_lst || pnode->neg_rx_lst) { |
| 434 | nodes[i++] = pnode; |
| 435 | |
| 436 | if (match.result != RESULT_EQUAL_MATCH && match.result != RESULT_SELECTIVE_MATCH) { |
| 437 | if (pnode->sel_rx_lst) { |
| 438 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2502 %*cnode: '%s': check selective list", depth, ' ', pnode->path); |
| 439 | result = check_list_for_match(pnode->sel_rx_lst, file, &match.rule, depth+2, whoami); |
| 440 | if (result == RESULT_SELECTIVE_MATCH || result == RESULT_PARTIAL_MATCH) { |
| 441 | match.result = result; |
| 442 | } |
| 443 | } else { |
| 444 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip selective list (reason: list is empty)", depth, ' ', pnode->path); |
| 445 | } |
| 446 | } else { |
| 447 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip selective list (reason: previous positive match)", depth, ' ', pnode->path); |
| 448 | } |
| 449 | depth++; |
| 450 | } else { |
| 451 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip selective and negative list (reason: lists are empty)", depth, ' ', pnode->path); |
| 452 | } |
| 453 | next_parent = pnode->parent; |
| 454 | pthread_rwlock_unlock(&pnode->rwlock); |
| 455 | } while ((pnode = next_parent)); |
| 456 | |
| 457 | /* check negative rules top -> down */ |
| 458 | while (--i >=0) { |
| 459 | pnode = nodes[i]; |
| 460 | depth--; |
| 461 | pthread_rwlock_rdlock(&pnode->rwlock); |
| 462 | if (match.result == RESULT_EQUAL_MATCH || match.result == RESULT_SELECTIVE_MATCH || match.result == RESULT_PARTIAL_MATCH) { |
| 463 | if (pnode->neg_rx_lst) { |
| 464 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2502 %*cnode: '%s': check negative list (reason: previous positive/partial match)", depth, ' ', pnode->path); |
| 465 | result = check_list_for_match(pnode->neg_rx_lst, file, &match.rule, depth+2, whoami); |
| 466 | if ((match.result != RESULT_PARTIAL_MATCH && result == RESULT_RECURSIVE_NEGATIVE_MATCH) || result == RESULT_NON_RECURSIVE_NEGATIVE_MATCH) { |
| 467 | match.result = result; |
| 468 | } |
| 469 | } else { |
| 470 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip negative list (reason: list is empty)", depth, ' ', pnode->path); |
| 471 | } |
| 472 | } else if (match.result == RESULT_NON_RECURSIVE_NEGATIVE_MATCH || match.result == RESULT_RECURSIVE_NEGATIVE_MATCH) { |
| 473 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip negative list (reason: previous negative match)", depth, ' ', pnode->path); |
| 474 | } else { |
| 475 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 %*cnode: '%s': skip negative list (reason: no previous positive/partial match)", depth, ' ', pnode->path); |
| 476 | } |
| 477 | pthread_rwlock_unlock(&pnode->rwlock); |
| 478 | } |
| 479 | free(nodes); |
| 480 | LOG_WHOAMI(LOG_LEVEL_TRACE, "\u2502 check_node_for_match: match result %s (%d) for '%s'", get_match_result_string(match.result), match.result, file.name); |
| 481 | return match; |
| 482 | } |
| 483 | |
| 484 | match_t check_seltree(seltree *tree, file_t file, bool check_parent_dirs, const char * whoami) { |
| 485 | seltree* pnode=NULL; |
| 486 | match_t match = { RESULT_NO_RULE_MATCH, NULL, 0 }; |
| 487 | bool parent_negative_match = false; |
| 488 | |
| 489 | const char *next_dir = file.name; |
| 490 | char *parent = checked_strdup(file.name); /* freed below */ |
| 491 | pnode = tree; |
| 492 | seltree *node = tree; |
| 493 | |
| 494 | LOG_WHOAMI(LOG_LEVEL_TRACE, "\u2502 search for parent node for '%s' (tree: '%s' (%p))", file.name, tree->path, (void*) tree); |
| 495 | |
| 496 | if (strcmp(file.name, "/") == 0) { check_parent_dirs = false; } /* do not check parent directories for '/' */ |
| 497 | |
| 498 | char *relative_child_path = parent; |
| 499 | while ((next_dir = strchr(next_dir, '/'))) { |
| 500 | int parent_length = next_dir-file.name; |
| 501 | int relative_child_path_start = parent_length; |
| 502 | if (next_dir == file.name) { /* handle "/" directory */ |
| 503 | parent_length = 1; |
| 504 | relative_child_path_start = 0; |
| 505 | } |
| 506 | parent[parent_length] = '\0'; |
| 507 | |
| 508 | if (node && relative_child_path_start) { |
| 509 | node = get_seltree_node(pnode, relative_child_path); |
| 510 | if (node) { |
| 511 | pthread_rwlock_rdlock(&node->rwlock); |
| 512 | LOG_WHOAMI(LOG_LEVEL_TRACE, "\u2502 got %s (%p) for '%s'", node->path, (void*) node, relative_child_path); |
| 513 | pthread_rwlock_unlock(&node->rwlock); |
| 514 | pnode = node; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | if (check_parent_dirs) { |
| 519 | pthread_rwlock_rdlock(&pnode->rwlock); |
| 520 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2502 check parent directory '%s' for non-recurse match (node: '%s' (%p))", parent, pnode->path, (void*) pnode); |
| 521 | match = check_node_for_match(pnode, (file_t) { .name = parent, .type = FT_DIR, |
| 522 | #ifdef HAVE_FSTYPE |
| 523 | .fs_type = 0UL |
| 524 | #endif |
| 525 | }, whoami); |
| 526 | pthread_rwlock_unlock(&pnode->rwlock); |
| 527 | if (match.result == RESULT_NON_RECURSIVE_NEGATIVE_MATCH) { |
| 528 | match.result = RESULT_NEGATIVE_PARENT_MATCH; |
| 529 | match.length = parent_length; |
| 530 | parent_negative_match = true; |
| 531 | break; |
| 532 | } else if (match.result == RESULT_NO_RULE_MATCH) { |
| 533 | parent_negative_match = true; |
| 534 | break; |
| 535 | } else { |
| 536 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2502 no non-recurse match found for parent directory '%s'", parent); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | parent[parent_length] = file.name[parent_length]; |
| 541 | relative_child_path = &parent[relative_child_path_start]; |
| 542 | next_dir += 1; |
| 543 | } |
| 544 | pthread_rwlock_rdlock(&pnode->rwlock); |
| 545 | LOG_WHOAMI(LOG_LEVEL_TRACE, "\u2502 got parent node '%s' (%p) for parent name '%s'", pnode->path, (void*) pnode, parent); |
| 546 | pthread_rwlock_unlock(&pnode->rwlock); |
| 547 | free(parent); |
| 548 | if (!parent_negative_match) { |
| 549 | LOG_WHOAMI(LOG_LEVEL_RULE, "\u2502 check '%s' (filetype: %c)", file.name, get_f_type_char_from_f_type(file.type)); |
| 550 | match = check_node_for_match(pnode, file, whoami); |
| 551 | } |
| 552 | |
| 553 | LOG_WHOAMI(LOG_LEVEL_DEBUG, "\u2502 check_selree: match result %s (%d) for '%s'", get_match_result_string(match.result), match.result, file.name); |
| 554 | return match; |
| 555 | } |