| 1 | /* |
| 2 | * Copyright 2010-2011 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * "Daniel Kopecek" <[email protected]> |
| 21 | * "Tomas Heinrich" <[email protected]> |
| 22 | */ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include <config.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <string.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <limits.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #include "oscap_helpers.h" |
| 37 | #include "fsdev.h" |
| 38 | #include "_probe-api.h" |
| 39 | #include "probe/entcmp.h" |
| 40 | #include "debug_priv.h" |
| 41 | #include "oval_fts.h" |
| 42 | #if defined(OS_SOLARIS) |
| 43 | #include "fts_sun.h" |
| 44 | #include <sys/mntent.h> |
| 45 | #include <libzonecfg.h> |
| 46 | #include <sys/avl.h> |
| 47 | #elif defined(OS_AIX) |
| 48 | #include "fts_sun.h" |
| 49 | #else |
| 50 | #include <fts.h> |
| 51 | #endif |
| 52 | |
| 53 | #undef OSCAP_FTS_DEBUG |
| 54 | |
| 55 | static OVAL_FTS *OVAL_FTS_new() |
| 56 | { |
| 57 | OVAL_FTS *ofts = calloc(1, sizeof(OVAL_FTS)); |
| 58 | |
| 59 | ofts->max_depth = -1; |
| 60 | ofts->direction = -1; |
| 61 | ofts->filesystem = -1; |
| 62 | |
| 63 | return (ofts); |
| 64 | } |
| 65 | |
| 66 | static void OVAL_FTS_free(OVAL_FTS *ofts) |
| 67 | { |
| 68 | if (ofts->ofts_match_path_fts != NULL) |
| 69 | fts_close(ofts->ofts_match_path_fts); |
| 70 | if (ofts->ofts_recurse_path_fts != NULL) |
| 71 | fts_close(ofts->ofts_recurse_path_fts); |
| 72 | |
| 73 | free(ofts); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | static int pathlen_from_ftse(int fts_pathlen, int fts_namelen) |
| 78 | { |
| 79 | int pathlen; |
| 80 | |
| 81 | if (fts_pathlen > fts_namelen) { |
| 82 | pathlen = fts_pathlen - fts_namelen; |
| 83 | if (pathlen > 1) |
| 84 | pathlen--; /* strip last slash */ |
| 85 | } else { |
| 86 | pathlen = fts_pathlen; |
| 87 | } |
| 88 | |
| 89 | return pathlen; |
| 90 | } |
| 91 | |
| 92 | static OVAL_FTSENT *OVAL_FTSENT_new(OVAL_FTS *ofts, FTSENT *fts_ent) |
| 93 | { |
| 94 | OVAL_FTSENT *ofts_ent = calloc(1, sizeof(OVAL_FTSENT)); |
| 95 | |
| 96 | ofts_ent->fts_info = fts_ent->fts_info; |
| 97 | /* The 'shift' variable stores length of the prefix if the prefix |
| 98 | * is defined, otherwise it is set to 0. The value of 'shift' gives |
| 99 | * us information how many characters of the path string are part of |
| 100 | * the prefix and also where the actual path begins. |
| 101 | * We use it to remove the prefix from the path. |
| 102 | */ |
| 103 | const size_t shift = ofts->prefix ? strlen(ofts->prefix) : 0; |
| 104 | if (ofts->ofts_sfilename || ofts->ofts_sfilepath) { |
| 105 | ofts_ent->path_len = pathlen_from_ftse(fts_ent->fts_pathlen, fts_ent->fts_namelen) - shift; |
| 106 | if (ofts_ent->path_len > 0) { |
| 107 | ofts_ent->path = malloc(ofts_ent->path_len + 1); |
| 108 | strncpy(ofts_ent->path, fts_ent->fts_path + shift, ofts_ent->path_len); |
| 109 | ofts_ent->path[ofts_ent->path_len] = '\0'; |
| 110 | } else { |
| 111 | ofts_ent->path_len = 1; |
| 112 | ofts_ent->path = strdup("/"); |
| 113 | } |
| 114 | |
| 115 | ofts_ent->file_len = fts_ent->fts_namelen; |
| 116 | ofts_ent->file = strdup(fts_ent->fts_name); |
| 117 | } else { |
| 118 | ofts_ent->path_len = fts_ent->fts_pathlen - shift; |
| 119 | if (ofts_ent->path_len > 0) { |
| 120 | ofts_ent->path = strdup(fts_ent->fts_path + shift); |
| 121 | } else { |
| 122 | ofts_ent->path_len = 1; |
| 123 | ofts_ent->path = strdup("/"); |
| 124 | } |
| 125 | |
| 126 | ofts_ent->file_len = -1; |
| 127 | ofts_ent->file = NULL; |
| 128 | } |
| 129 | |
| 130 | #if defined(OSCAP_FTS_DEBUG) |
| 131 | dD("New OVAL_FTSENT: file: '%s', path: '%s'.", ofts_ent->file, ofts_ent->path); |
| 132 | #endif |
| 133 | return (ofts_ent); |
| 134 | } |
| 135 | |
| 136 | static void OVAL_FTSENT_free(OVAL_FTSENT *ofts_ent) |
| 137 | { |
| 138 | free(ofts_ent->path); |
| 139 | free(ofts_ent->file); |
| 140 | free(ofts_ent); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | #if defined(OS_SOLARIS) |
| 145 | #ifndef MNTTYPE_SMB |
| 146 | #define MNTTYPE_SMB "smb" |
| 147 | #endif |
| 148 | #ifndef MNTTYPE_SMBFS |
| 149 | #define MNTTYPE_SMBFS "smbfs" |
| 150 | #endif |
| 151 | #ifndef MNTTYPE_PROC |
| 152 | #define MNTTYPE_PROC "proc" |
| 153 | #endif |
| 154 | |
| 155 | typedef struct zone_path { |
| 156 | avl_node_t avl_link_next; |
| 157 | char zpath[MAXPATHLEN]; |
| 158 | } zone_path_t; |
| 159 | static avl_tree_t avl_tree_list; |
| 160 | |
| 161 | |
| 162 | static bool valid_remote_fs(char *fstype) |
| 163 | { |
| 164 | if (strcmp(fstype, MNTTYPE_NFS) == 0 || |
| 165 | strcmp(fstype, MNTTYPE_SMBFS) == 0 || |
| 166 | strcmp(fstype, MNTTYPE_SMB) == 0) |
| 167 | return (true); |
| 168 | return (false); |
| 169 | } |
| 170 | |
| 171 | static bool valid_local_fs(char *fstype) |
| 172 | { |
| 173 | if (strcmp(fstype, MNTTYPE_SWAP) == 0 || |
| 174 | strcmp(fstype, MNTTYPE_MNTFS) == 0 || |
| 175 | strcmp(fstype, MNTTYPE_CTFS) == 0 || |
| 176 | strcmp(fstype, MNTTYPE_OBJFS) == 0 || |
| 177 | strcmp(fstype, MNTTYPE_SHAREFS) == 0 || |
| 178 | strcmp(fstype, MNTTYPE_PROC) == 0 || |
| 179 | strcmp(fstype, MNTTYPE_LOFS) == 0 || |
| 180 | strcmp(fstype, MNTTYPE_AUTOFS) == 0) |
| 181 | return (false); |
| 182 | return (true); |
| 183 | } |
| 184 | |
| 185 | /* function to compare two avl nodes in the avl tree */ |
| 186 | static int compare_zoneroot(const void *entry1, const void *entry2) |
| 187 | { |
| 188 | zone_path_t *t1, *t2; |
| 189 | int comp; |
| 190 | |
| 191 | t1 = (zone_path_t *)entry1; |
| 192 | t2 = (zone_path_t *)entry2; |
| 193 | if ((comp = strcmp(t1->zpath, t2->zpath)) == 0) { |
| 194 | return (0); |
| 195 | } |
| 196 | return (comp > 0 ? 1 : -1); |
| 197 | } |
| 198 | |
| 199 | int load_zones_path_list() |
| 200 | { |
| 201 | FILE *cookie; |
| 202 | char *name; |
| 203 | zone_state_t state_num; |
| 204 | zone_path_t *temp = NULL; |
| 205 | avl_index_t where; |
| 206 | char rpath[MAXPATHLEN]; |
| 207 | |
| 208 | cookie = setzoneent(); |
| 209 | if (getzoneid() != GLOBAL_ZONEID) |
| 210 | return (0); |
| 211 | avl_create(&avl_tree_list, compare_zoneroot, |
| 212 | sizeof(zone_path_t), offsetof(zone_path_t, avl_link_next)); |
| 213 | while ((name = getzoneent(cookie)) != NULL) { |
| 214 | if (strcmp(name, "global") == 0) |
| 215 | continue; |
| 216 | if (zone_get_state(name, &state_num) != Z_OK) { |
| 217 | dE("Could not get zone state for %s", name); |
| 218 | continue; |
| 219 | } else if (state_num > ZONE_STATE_CONFIGURED) { |
| 220 | temp = malloc(sizeof(zone_path_t)); |
| 221 | if (temp == NULL) { |
| 222 | dE("Memory alloc failed"); |
| 223 | return(1); |
| 224 | } |
| 225 | if (zone_get_zonepath(name, rpath, |
| 226 | sizeof(rpath)) != Z_OK) { |
| 227 | dE("Could not get zone path for %s", |
| 228 | name); |
| 229 | continue; |
| 230 | } |
| 231 | if (oscap_realpath(rpath, temp->zpath) != NULL) |
| 232 | avl_add(&avl_tree_list, temp); |
| 233 | } |
| 234 | } |
| 235 | endzoneent(cookie); |
| 236 | return (0); |
| 237 | } |
| 238 | |
| 239 | static void free_zones_path_list() |
| 240 | { |
| 241 | zone_path_t *temp; |
| 242 | void* cookie = NULL; |
| 243 | |
| 244 | while ((temp = avl_destroy_nodes(&avl_tree_list, &cookie)) != NULL) { |
| 245 | free(temp); |
| 246 | } |
| 247 | avl_destroy(&avl_tree_list); |
| 248 | } |
| 249 | |
| 250 | static bool valid_local_zone(const char *path) |
| 251 | { |
| 252 | zone_path_t temp; |
| 253 | avl_index_t where; |
| 254 | |
| 255 | strlcpy(temp.zpath, path, sizeof(temp.zpath)); |
| 256 | if (avl_find(&avl_tree_list, &temp, &where) != NULL) |
| 257 | return (true); |
| 258 | |
| 259 | return (false); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | #endif |
| 264 | |
| 265 | static bool OVAL_FTS_localp(OVAL_FTS *ofts, const char *path, void *id) |
| 266 | { |
| 267 | #if defined(OS_SOLARIS) |
| 268 | if (id != NULL && (*(char*)id) != '\0') { |
| 269 | /* if not a valid local fs skip */ |
| 270 | if (valid_local_fs((char*)id)) { |
| 271 | /* if recurse is local , skip remote fs |
| 272 | and non-global zones */ |
| 273 | if (ofts->filesystem == OVAL_RECURSE_FS_LOCAL) { |
| 274 | return (!(valid_remote_fs((char*)id) || |
| 275 | valid_local_zone(path))); |
| 276 | } |
| 277 | return (true); |
| 278 | } |
| 279 | return (false); |
| 280 | } else if (path != NULL) { |
| 281 | /* id was not set, because fts_read failed to stat the node */ |
| 282 | struct stat sb; |
| 283 | if ((stat(path, &sb) == 0) && (valid_local_fs(sb.st_fstype))) { |
| 284 | /* if recurse is local , skip remote fs |
| 285 | and non-global zones */ |
| 286 | if (ofts->filesystem == OVAL_RECURSE_FS_LOCAL) { |
| 287 | return (!(valid_remote_fs(sb.st_fstype) || |
| 288 | valid_local_zone(path))); |
| 289 | } |
| 290 | return (true); |
| 291 | } |
| 292 | return (false); |
| 293 | } else { |
| 294 | return (false); |
| 295 | } |
| 296 | #else |
| 297 | if (id != NULL) |
| 298 | return (fsdev_search(ofts->localdevs, id) == 1 ? true : false); |
| 299 | else if (path != NULL) |
| 300 | return (fsdev_path(ofts->localdevs, path) == 1 ? true : false); |
| 301 | else |
| 302 | return (false); |
| 303 | #endif |
| 304 | } |
| 305 | |
| 306 | static char *__regex_locate(char *str) |
| 307 | { |
| 308 | char *regex_sch = "^*?$.(["; /*<< regex start chars */ |
| 309 | bool escaped = false; |
| 310 | |
| 311 | while (*str != '\0') { |
| 312 | if (*str == '\\') |
| 313 | escaped = !escaped; |
| 314 | else if (strchr(regex_sch, *str) != NULL) { |
| 315 | if (!escaped) |
| 316 | return (str); |
| 317 | else |
| 318 | escaped = false; |
| 319 | } |
| 320 | ++str; |
| 321 | } |
| 322 | |
| 323 | return (str); |
| 324 | } |
| 325 | |
| 326 | static char *__string_unescape(char *str, size_t len) |
| 327 | { |
| 328 | char *ret_str; |
| 329 | size_t i, j; |
| 330 | |
| 331 | if (str == NULL || len == 0) |
| 332 | return NULL; |
| 333 | |
| 334 | ret_str = strndup(str, len); |
| 335 | |
| 336 | if (ret_str == NULL) |
| 337 | return NULL; |
| 338 | |
| 339 | for (i = j = 0; i < len && j <= i; ++i) { |
| 340 | if (str[i] == '\\') { |
| 341 | if (str[i+1] == '\0') { |
| 342 | free(ret_str); |
| 343 | return NULL; |
| 344 | } |
| 345 | ret_str[j] = str[i+1]; |
| 346 | ++j; |
| 347 | ++i; |
| 348 | } else { |
| 349 | ret_str[j] = str[i]; |
| 350 | ++j; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ret_str[j] = '\0'; |
| 355 | |
| 356 | return ret_str; |
| 357 | } |
| 358 | |
| 359 | static char *extract_fixed_path_prefix(char *path) |
| 360 | { |
| 361 | char *s; |
| 362 | |
| 363 | if (path[0] == '^') |
| 364 | path++; |
| 365 | |
| 366 | s = __regex_locate(path); |
| 367 | if (*s != '\0') |
| 368 | for (s--; s > (path + 1) && *s != '/'; s--); |
| 369 | if (s > (path + 1)) { |
| 370 | s = __string_unescape(path, (size_t) (s - path)); |
| 371 | if (s != NULL) { |
| 372 | if (s[0] == '/') |
| 373 | return s; |
| 374 | free(s); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return strdup("/"); |
| 379 | } |
| 380 | |
| 381 | #define TEST_PATH1 "/" |
| 382 | #define TEST_PATH2 "x" |
| 383 | |
| 384 | /* Verify that the path is usable and try to craft a regex to speed up |
| 385 | the filesystem traversal. If the path to match is ill-designed, an |
| 386 | ugly heuristic is employed to obtain something meaningfull. */ |
| 387 | static int process_pattern_match(const char *path, oscap_pcre_t **regex_out) |
| 388 | { |
| 389 | int ret, errofs = 0; |
| 390 | char *pattern; |
| 391 | const char *test_path1 = TEST_PATH1; |
| 392 | //const char *test_path2 = TEST_PATH2; |
| 393 | char *errptr = NULL; |
| 394 | oscap_pcre_t *regex; |
| 395 | |
| 396 | if (path[0] != '^') { |
| 397 | /* Matching has to have a fixed starting point and thus |
| 398 | every pattern has to start with a caret. */ |
| 399 | size_t plen; |
| 400 | |
| 401 | plen = strlen(path) + 1; |
| 402 | pattern = malloc(plen + 1); |
| 403 | pattern[0] = '^'; |
| 404 | memcpy(pattern + 1, path, plen); |
| 405 | dI("The pattern '%s' doesn't contain a leading caret - added. " |
| 406 | "All paths with the 'pattern match' operation must begin " |
| 407 | "with a caret.", path); |
| 408 | } else { |
| 409 | pattern = strdup(path); |
| 410 | } |
| 411 | |
| 412 | regex = oscap_pcre_compile(pattern, 0, &errptr, &errofs); |
| 413 | if (regex == NULL) { |
| 414 | dE("Failed to validate the pattern: oscap_pcre_compile(): " |
| 415 | "error offset: %d, error: '%s', pattern: '%s'.\n", |
| 416 | errofs, errptr, pattern); |
| 417 | free(pattern); |
| 418 | oscap_pcre_err_free(errptr); |
| 419 | return -1; |
| 420 | } |
| 421 | ret = oscap_pcre_exec(regex, test_path1, strlen(test_path1), 0, |
| 422 | OSCAP_PCRE_OPTS_PARTIAL, NULL, 0); |
| 423 | |
| 424 | switch (ret) { |
| 425 | case OSCAP_PCRE_ERR_PARTIAL: |
| 426 | /* The pattern has matched a prefix of the test path |
| 427 | and probably begins with a slash. Make sure that it |
| 428 | doesn't match an arbitrary prefix. */ |
| 429 | |
| 430 | /* todo: |
| 431 | Convince folks that they should really fix their |
| 432 | OVAL definitions that use ".*" as 'path' and then |
| 433 | uncomment this. |
| 434 | |
| 435 | dD("pcre_exec() returned PCRE_ERROR_PARTIAL for pattern '%s' " |
| 436 | "and test path '%s'.\n", pattern, test_path1); |
| 437 | ret = pcre_exec(regex, NULL, test_path2, strlen(test_path2), |
| 438 | 0, PCRE_PARTIAL, NULL, 0); |
| 439 | if (ret == PCRE_ERROR_PARTIAL || ret >= 0) { |
| 440 | dE("Failed to validate the pattern: test path '%s' " |
| 441 | "matched by pattern '%s' - the pattern is too " |
| 442 | "general, i.e. inefficient. This could take a " |
| 443 | "lifetime to complete.\n", test_path2, pattern); |
| 444 | pcre_free(regex); |
| 445 | free(pattern); |
| 446 | return -2; |
| 447 | } |
| 448 | */ |
| 449 | break; |
| 450 | case OSCAP_PCRE_ERR_NOMATCH: |
| 451 | /* The pattern doesn't contain a leading slash (or |
| 452 | some part of this code is broken). Apologise to the |
| 453 | user and fail. */ |
| 454 | dE("Failed to validate the pattern: oscap_pcre_exec() returned " |
| 455 | "PCRE_ERROR_NOMATCH for pattern '%s' and a test path '%s'. " |
| 456 | "This indicates the pattern doesn't match a leading '/'.\n", |
| 457 | pattern, test_path1); |
| 458 | oscap_pcre_free(regex); |
| 459 | free(pattern); |
| 460 | return -2; |
| 461 | default: |
| 462 | if (ret >= 0) { |
| 463 | /* The pattern actually matches the test |
| 464 | path. Iteresting… Make sure that it doesn't |
| 465 | match an arbitrary prefix. */ |
| 466 | |
| 467 | /* todo: |
| 468 | Convince folks that they should really fix |
| 469 | their OVAL definitions that use ".*" as |
| 470 | 'path' and then uncomment this. |
| 471 | |
| 472 | ret = pcre_exec(regex, NULL, test_path2, strlen(test_path2), |
| 473 | 0, PCRE_PARTIAL, NULL, 0); |
| 474 | if (ret == PCRE_ERROR_PARTIAL || ret >= 0) { |
| 475 | dE("Failed to validate the pattern: test path '%s' " |
| 476 | "matched by pattern '%s' - the pattern is too " |
| 477 | "general, i.e. inefficient. This could take a " |
| 478 | "lifetime to complete.\n", test_path2, pattern); |
| 479 | pcre_free(regex); |
| 480 | free(pattern); |
| 481 | return -2; |
| 482 | } |
| 483 | */ |
| 484 | break; |
| 485 | } |
| 486 | /* Some other error. */ |
| 487 | dE("Failed to validate the pattern: oscap_pcre_exec() return " |
| 488 | "code: %d, pattern '%s', test path '%s'.\n", ret, |
| 489 | pattern, test_path1); |
| 490 | oscap_pcre_free(regex); |
| 491 | free(pattern); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | if (regex == NULL) { |
| 496 | dD("Disabling partial match optimization."); |
| 497 | } else { |
| 498 | dD("Enabling partial match optimization using " |
| 499 | "pattern: '%s'.", pattern); |
| 500 | if (regex_out != NULL) |
| 501 | *regex_out = regex; |
| 502 | else |
| 503 | oscap_pcre_free(regex); |
| 504 | } |
| 505 | |
| 506 | free(pattern); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | |
| 512 | #undef TEST_PATH1 |
| 513 | #undef TEST_PATH2 |
| 514 | |
| 515 | OVAL_FTS *oval_fts_open(SEXP_t *path, SEXP_t *filename, SEXP_t *filepath, SEXP_t *behaviors, SEXP_t* result) |
| 516 | { |
| 517 | return oval_fts_open_prefixed(NULL, path, filename, filepath, behaviors, result); |
| 518 | } |
| 519 | |
| 520 | OVAL_FTS *oval_fts_open_prefixed(const char *prefix, SEXP_t *path, SEXP_t *filename, SEXP_t *filepath, SEXP_t *behaviors, SEXP_t* result) |
| 521 | { |
| 522 | OVAL_FTS *ofts; |
| 523 | |
| 524 | char cstr_path[PATH_MAX+1]; |
| 525 | char cstr_file[PATH_MAX+1]; |
| 526 | char cstr_buff[32]; |
| 527 | const char *paths[2] = { NULL, NULL }; |
| 528 | |
| 529 | SEXP_t *r0; |
| 530 | |
| 531 | int mtc_fts_options = FTS_PHYSICAL | FTS_COMFOLLOW | FTS_NOCHDIR; |
| 532 | int rec_fts_options = FTS_PHYSICAL | FTS_COMFOLLOW | FTS_NOCHDIR; |
| 533 | int max_depth = -1; |
| 534 | int direction = -1; |
| 535 | int recurse = -1; |
| 536 | int filesystem = -1; |
| 537 | |
| 538 | uint32_t path_op; |
| 539 | bool nilfilename = false; |
| 540 | oscap_pcre_t *regex = NULL; |
| 541 | struct stat st; |
| 542 | |
| 543 | if ((path != NULL || filename != NULL || filepath == NULL) |
| 544 | && (path == NULL || filepath != NULL)) { |
| 545 | return NULL; |
| 546 | } |
| 547 | if (behaviors == NULL) { |
| 548 | return NULL; |
| 549 | } |
| 550 | |
| 551 | if (path) |
| 552 | PROBE_ENT_AREF(path, r0, "operation", /**/); |
| 553 | else |
| 554 | PROBE_ENT_AREF(filepath, r0, "operation", /**/); |
| 555 | |
| 556 | if (r0 != NULL) { |
| 557 | path_op = SEXP_number_getu(r0); |
| 558 | SEXP_free(r0); |
| 559 | } else { |
| 560 | path_op = OVAL_OPERATION_EQUALS; |
| 561 | } |
| 562 | #if defined(OSCAP_FTS_DEBUG) |
| 563 | dD("path_op: %u, '%s'.", path_op, oval_operation_get_text(path_op)); |
| 564 | #endif |
| 565 | if (path) { /* filepath == NULL */ |
| 566 | PROBE_ENT_STRVAL(path, cstr_path, sizeof cstr_path, |
| 567 | return NULL;, return NULL;); |
| 568 | if (probe_ent_getvals(filename, NULL) == 0) { |
| 569 | nilfilename = true; |
| 570 | } else { |
| 571 | PROBE_ENT_STRVAL(filename, cstr_file, sizeof cstr_file, |
| 572 | return NULL;, /* noop */;); |
| 573 | } |
| 574 | #if defined(OSCAP_FTS_DEBUG) |
| 575 | dD("path: '%s', filename: '%s', filename: %d.", cstr_path, nilfilename ? "" : cstr_file, nilfilename); |
| 576 | #endif |
| 577 | } else { /* filepath != NULL */ |
| 578 | PROBE_ENT_STRVAL(filepath, cstr_path, sizeof cstr_path, return NULL;, return NULL;); |
| 579 | } |
| 580 | |
| 581 | /* max_depth */ |
| 582 | PROBE_ENT_AREF(behaviors, r0, "max_depth", return NULL;); |
| 583 | SEXP_string_cstr_r(r0, cstr_buff, sizeof cstr_buff - 1); |
| 584 | errno = 0; |
| 585 | max_depth = strtol(cstr_buff, NULL, 10); |
| 586 | if (errno == EINVAL || errno == ERANGE) { |
| 587 | dE("Invalid value of the `%s' attribute: %s", "recurse_direction", cstr_buff); |
| 588 | SEXP_free(r0); |
| 589 | return (NULL); |
| 590 | } |
| 591 | #if defined(OSCAP_FTS_DEBUG) |
| 592 | dD("bh.max_depth: %s => max_depth: %d", cstr_buff, max_depth); |
| 593 | #endif |
| 594 | SEXP_free(r0); |
| 595 | |
| 596 | /* recurse_direction */ |
| 597 | PROBE_ENT_AREF(behaviors, r0, "recurse_direction", return NULL;); |
| 598 | SEXP_string_cstr_r(r0, cstr_buff, sizeof cstr_buff - 1); |
| 599 | /* todo: use oscap_string_to_enum() */ |
| 600 | if (strcmp(cstr_buff, "none") == 0) { |
| 601 | direction = OVAL_RECURSE_DIRECTION_NONE; |
| 602 | } else if (strcmp(cstr_buff, "down") == 0) { |
| 603 | direction = OVAL_RECURSE_DIRECTION_DOWN; |
| 604 | } else if (strcmp(cstr_buff, "up") == 0) { |
| 605 | direction = OVAL_RECURSE_DIRECTION_UP; |
| 606 | } else { |
| 607 | dE("Invalid direction: %s", cstr_buff); |
| 608 | SEXP_free(r0); |
| 609 | return (NULL); |
| 610 | } |
| 611 | #if defined(OSCAP_FTS_DEBUG) |
| 612 | dD("bh.direction: %s => direction: %d", cstr_buff, direction); |
| 613 | #endif |
| 614 | SEXP_free(r0); |
| 615 | |
| 616 | /* recurse */ |
| 617 | PROBE_ENT_AREF(behaviors, r0, "recurse", /**/); |
| 618 | if (r0 != NULL) { |
| 619 | SEXP_string_cstr_r(r0, cstr_buff, sizeof cstr_buff - 1); |
| 620 | /* todo: use oscap_string_to_enum() */ |
| 621 | if (strcmp(cstr_buff, "symlinks and directories") == 0) { |
| 622 | recurse = OVAL_RECURSE_SYMLINKS_AND_DIRS; |
| 623 | } else if (strcmp(cstr_buff, "files and directories") == 0) { |
| 624 | recurse = OVAL_RECURSE_FILES_AND_DIRS; |
| 625 | } else if (strcmp(cstr_buff, "symlinks") == 0) { |
| 626 | recurse = OVAL_RECURSE_SYMLINKS; |
| 627 | } else if (strcmp(cstr_buff, "directories") == 0) { |
| 628 | recurse = OVAL_RECURSE_DIRS; |
| 629 | } else { |
| 630 | dE("Invalid recurse: %s", cstr_buff); |
| 631 | SEXP_free(r0); |
| 632 | return (NULL); |
| 633 | } |
| 634 | } else { |
| 635 | recurse = OVAL_RECURSE_SYMLINKS_AND_DIRS; |
| 636 | } |
| 637 | #if defined(OSCAP_FTS_DEBUG) |
| 638 | dD("bh.recurse: %s => recurse: %d", cstr_buff, recurse); |
| 639 | #endif |
| 640 | SEXP_free(r0); |
| 641 | |
| 642 | /* recurse_file_system */ |
| 643 | PROBE_ENT_AREF(behaviors, r0, "recurse_file_system", /**/); |
| 644 | |
| 645 | if (r0 != NULL) { |
| 646 | SEXP_string_cstr_r(r0, cstr_buff, sizeof cstr_buff - 1); |
| 647 | /* todo: use oscap_string_to_enum() */ |
| 648 | if (strcmp(cstr_buff, "local") == 0) { |
| 649 | filesystem = OVAL_RECURSE_FS_LOCAL; |
| 650 | } else if (strcmp(cstr_buff, "all") == 0) { |
| 651 | filesystem = OVAL_RECURSE_FS_ALL; |
| 652 | } else if (strcmp(cstr_buff, "defined") == 0) { |
| 653 | filesystem = OVAL_RECURSE_FS_DEFINED; |
| 654 | rec_fts_options |= FTS_XDEV; |
| 655 | } else { |
| 656 | dE("Invalid recurse filesystem: %s", cstr_buff); |
| 657 | SEXP_free(r0); |
| 658 | return (NULL); |
| 659 | } |
| 660 | } else { |
| 661 | filesystem = OVAL_RECURSE_FS_ALL; |
| 662 | } |
| 663 | #if defined(OSCAP_FTS_DEBUG) |
| 664 | dD("bh.filesystem: %s => filesystem: %d", cstr_buff, filesystem); |
| 665 | #endif |
| 666 | SEXP_free(r0); |
| 667 | |
| 668 | /* todo: |
| 669 | Still missing is a propagation of the error to the |
| 670 | user. Currently, all the information is provided in the |
| 671 | debug log, but the oval_fts api has no way of passing this |
| 672 | information to the user. |
| 673 | */ |
| 674 | |
| 675 | if (path_op == OVAL_OPERATION_EQUALS) { |
| 676 | paths[0] = strdup(cstr_path); |
| 677 | } else if (path_op == OVAL_OPERATION_PATTERN_MATCH) { |
| 678 | if (process_pattern_match(cstr_path, ®ex) != 0) |
| 679 | return NULL; |
| 680 | paths[0] = extract_fixed_path_prefix(cstr_path); |
| 681 | dD("Extracted fixed path: '%s'.", paths[0]); |
| 682 | } else { |
| 683 | paths[0] = strdup("/"); |
| 684 | } |
| 685 | |
| 686 | if (prefix != NULL) { |
| 687 | char *path_with_prefix = oscap_path_join(prefix, paths[0]); |
| 688 | free((void *) paths[0]); |
| 689 | paths[0] = path_with_prefix; |
| 690 | } |
| 691 | dI("Opening file '%s'.", paths[0]); |
| 692 | /* Fail if the provided path doensn't actually exist. Symlinks |
| 693 | without targets are accepted. */ |
| 694 | if (lstat(paths[0], &st) == -1) { |
| 695 | if (errno) { |
| 696 | dD("lstat() failed: errno: %d, '%s'.", |
| 697 | errno, strerror(errno)); |
| 698 | } |
| 699 | free((void *) paths[0]); |
| 700 | oscap_pcre_free(regex); |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | ofts = OVAL_FTS_new(); |
| 705 | ofts->prefix = prefix; |
| 706 | |
| 707 | /* reset errno as fts_open() doesn't do it itself. */ |
| 708 | errno = 0; |
| 709 | ofts->ofts_match_path_fts = fts_open((char * const *) paths, mtc_fts_options, NULL); |
| 710 | free((void *) paths[0]); |
| 711 | /* fts_open() doesn't return NULL for all errors (e.g. nonexistent paths), |
| 712 | so check errno to detect it. Far from being perfect. */ |
| 713 | if (ofts->ofts_match_path_fts == NULL || errno != 0) { |
| 714 | dE("fts_open() failed, errno: %d \"%s\".", errno, strerror(errno)); |
| 715 | OVAL_FTS_free(ofts); |
| 716 | oscap_pcre_free(regex); |
| 717 | return (NULL); |
| 718 | } |
| 719 | |
| 720 | ofts->ofts_recurse_path_fts_opts = rec_fts_options; |
| 721 | ofts->ofts_path_op = path_op; |
| 722 | if (regex != NULL) { |
| 723 | ofts->ofts_path_regex = regex; |
| 724 | } |
| 725 | |
| 726 | if (filesystem == OVAL_RECURSE_FS_LOCAL) { |
| 727 | #if defined(OS_SOLARIS) |
| 728 | ofts->localdevs = NULL; |
| 729 | #else |
| 730 | ofts->localdevs = fsdev_init(); |
| 731 | if (ofts->localdevs == NULL) { |
| 732 | dE("fsdev_init() failed."); |
| 733 | /* One dummy read to get rid of an uninitialized |
| 734 | * value in the FTS data before calling |
| 735 | * fts_close() on it. */ |
| 736 | fts_read(ofts->ofts_match_path_fts); |
| 737 | oval_fts_close(ofts); |
| 738 | return (NULL); |
| 739 | } |
| 740 | #endif |
| 741 | } else if (filesystem == OVAL_RECURSE_FS_DEFINED) { |
| 742 | /* store the device id for future comparison */ |
| 743 | FTSENT *fts_ent; |
| 744 | |
| 745 | fts_ent = fts_read(ofts->ofts_match_path_fts); |
| 746 | if (fts_ent != NULL) { |
| 747 | ofts->ofts_recurse_path_devid = fts_ent->fts_statp->st_dev; |
| 748 | fts_set(ofts->ofts_match_path_fts, fts_ent, FTS_AGAIN); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | ofts->recurse = recurse; |
| 753 | ofts->filesystem = filesystem; |
| 754 | |
| 755 | if (path) { /* filepath == NULL */ |
| 756 | ofts->ofts_spath = SEXP_ref(path); /* path entity */ |
| 757 | if (!nilfilename) |
| 758 | ofts->ofts_sfilename = SEXP_ref(filename); /* filename entity */ |
| 759 | |
| 760 | ofts->max_depth = max_depth; |
| 761 | ofts->direction = direction; |
| 762 | } else { /* filepath != NULL */ |
| 763 | ofts->ofts_sfilepath = SEXP_ref(filepath); |
| 764 | } |
| 765 | |
| 766 | #if defined(OS_SOLARIS) |
| 767 | if (load_zones_path_list() != 0) { |
| 768 | dE("Failed to load zones path info. Recursing non-global zones."); |
| 769 | free_zones_path_list(); |
| 770 | } |
| 771 | #endif |
| 772 | |
| 773 | ofts->result = result; |
| 774 | |
| 775 | return (ofts); |
| 776 | } |
| 777 | |
| 778 | static inline int _oval_fts_is_local(OVAL_FTS *ofts, FTSENT *fts_ent) { |
| 779 | # if defined(OS_SOLARIS) |
| 780 | /* pseudo filesystems will be skipped */ |
| 781 | /* don't recurse into remote fs if local is specified */ |
| 782 | return ((fts_ent->fts_info == FTS_D || fts_ent->fts_info == FTS_SL) |
| 783 | && (!OVAL_FTS_localp(ofts, fts_ent->fts_path, |
| 784 | (fts_ent->fts_statp != NULL) ? |
| 785 | &fts_ent->fts_statp->st_fstype : NULL))); |
| 786 | #else |
| 787 | /* don't recurse into non-local filesystems */ |
| 788 | return (ofts->filesystem == OVAL_RECURSE_FS_LOCAL |
| 789 | && (fts_ent->fts_info == FTS_D || fts_ent->fts_info == FTS_SL) |
| 790 | && (!OVAL_FTS_localp(ofts, fts_ent->fts_path, |
| 791 | (fts_ent->fts_statp != NULL) ? |
| 792 | &fts_ent->fts_statp->st_dev : NULL))); |
| 793 | #endif |
| 794 | } |
| 795 | |
| 796 | /* find the first matching path or filepath */ |
| 797 | static FTSENT *oval_fts_read_match_path(OVAL_FTS *ofts) |
| 798 | { |
| 799 | FTSENT *fts_ent = NULL; |
| 800 | SEXP_t *stmp; |
| 801 | oval_result_t ores; |
| 802 | |
| 803 | /* iterate until a match is found or all elements have been traversed */ |
| 804 | for (;;) { |
| 805 | fts_ent = fts_read(ofts->ofts_match_path_fts); |
| 806 | if (fts_ent == NULL) |
| 807 | return NULL; |
| 808 | switch (fts_ent->fts_info) { |
| 809 | case FTS_DP: |
| 810 | continue; |
| 811 | case FTS_DC: |
| 812 | dW("Filesystem tree cycle detected at '%s'.", fts_ent->fts_path); |
| 813 | fts_set(ofts->ofts_match_path_fts, fts_ent, FTS_SKIP); |
| 814 | continue; |
| 815 | } |
| 816 | |
| 817 | #if defined(OSCAP_FTS_DEBUG) |
| 818 | dD("fts_path: '%s' (l=%d)." |
| 819 | "fts_name: '%s' (l=%d).\n" |
| 820 | "fts_info: %u.\n", fts_ent->fts_path, fts_ent->fts_pathlen, |
| 821 | fts_ent->fts_name, fts_ent->fts_namelen, fts_ent->fts_info); |
| 822 | #endif |
| 823 | |
| 824 | if (fts_ent->fts_info == FTS_SL) { |
| 825 | #if defined(OSCAP_FTS_DEBUG) |
| 826 | dD("Only the target of a symlink gets reported, skipping '%s'.", fts_ent->fts_path, fts_ent->fts_name); |
| 827 | #endif |
| 828 | fts_set(ofts->ofts_match_path_fts, fts_ent, FTS_FOLLOW); |
| 829 | continue; |
| 830 | } |
| 831 | if (_oval_fts_is_local(ofts, fts_ent)) { |
| 832 | dI("Don't recurse into non-local filesystems, skipping '%s'.", fts_ent->fts_path); |
| 833 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 834 | continue; |
| 835 | } |
| 836 | /* don't recurse beyond the initial filesystem */ |
| 837 | if (ofts->filesystem == OVAL_RECURSE_FS_DEFINED |
| 838 | && (fts_ent->fts_info == FTS_D || fts_ent->fts_info == FTS_SL) |
| 839 | && ofts->ofts_recurse_path_devid != fts_ent->fts_statp->st_dev) { |
| 840 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 841 | continue; |
| 842 | } |
| 843 | |
| 844 | const size_t shift = ofts->prefix ? strlen(ofts->prefix) : 0; |
| 845 | /* partial match optimization for OVAL_OPERATION_PATTERN_MATCH operation on path and filepath */ |
| 846 | if (ofts->ofts_path_regex != NULL && fts_ent->fts_info == FTS_D) { |
| 847 | int ret, svec[3]; |
| 848 | |
| 849 | ret = oscap_pcre_exec(ofts->ofts_path_regex, |
| 850 | fts_ent->fts_path+shift, fts_ent->fts_pathlen-shift, 0, OSCAP_PCRE_OPTS_PARTIAL, |
| 851 | svec, sizeof(svec) / sizeof(svec[0])); |
| 852 | if (ret < 0) { |
| 853 | switch (ret) { |
| 854 | case OSCAP_PCRE_ERR_NOMATCH: |
| 855 | dD("Partial match optimization: PCRE_ERROR_NOMATCH, skipping."); |
| 856 | fts_set(ofts->ofts_match_path_fts, fts_ent, FTS_SKIP); |
| 857 | continue; |
| 858 | case OSCAP_PCRE_ERR_PARTIAL: |
| 859 | dD("Partial match optimization: PCRE_ERROR_PARTIAL, continuing."); |
| 860 | continue; |
| 861 | default: |
| 862 | dE("oscap_pcre_exec() error: %d.", ret); |
| 863 | return NULL; |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | if ((ofts->ofts_sfilepath && fts_ent->fts_info == FTS_D) |
| 869 | || (!ofts->ofts_sfilepath && fts_ent->fts_info != FTS_D)) |
| 870 | continue; |
| 871 | |
| 872 | stmp = SEXP_string_newf("%s", fts_ent->fts_path + shift); |
| 873 | |
| 874 | if (ofts->ofts_sfilepath) |
| 875 | /* try to match filepath */ |
| 876 | ores = probe_entobj_cmp(ofts->ofts_sfilepath, stmp); |
| 877 | else |
| 878 | /* try to match path */ |
| 879 | ores = probe_entobj_cmp(ofts->ofts_spath, stmp); |
| 880 | SEXP_free(stmp); |
| 881 | |
| 882 | if (ores == OVAL_RESULT_TRUE) |
| 883 | break; |
| 884 | if (ofts->ofts_path_op == OVAL_OPERATION_EQUALS) { |
| 885 | /* At this point the comparison result isn't OVAL_RESULT_TRUE. Since |
| 886 | we passed the exact path (from filepath or path elements) to |
| 887 | fts_open() we surely know that we can't find other items that would |
| 888 | be equal. Therefore we can terminate the matching. This can happen |
| 889 | if the filepath or path element references a variable that has |
| 890 | multiple different values. */ |
| 891 | return NULL; |
| 892 | } |
| 893 | } /* for (;;) */ |
| 894 | |
| 895 | /* |
| 896 | * If we know that we are not going to return anything |
| 897 | * else, then we can close the path FTS and return NULL |
| 898 | * the next time... |
| 899 | */ |
| 900 | if (ofts->ofts_path_op == OVAL_OPERATION_EQUALS && |
| 901 | ofts->direction == OVAL_RECURSE_DIRECTION_NONE && |
| 902 | ofts->ofts_sfilename == NULL && |
| 903 | ofts->ofts_sfilepath == NULL) |
| 904 | { |
| 905 | fts_set(ofts->ofts_match_path_fts, fts_ent, FTS_SKIP); |
| 906 | } |
| 907 | |
| 908 | return fts_ent; |
| 909 | } |
| 910 | |
| 911 | /* find the first matching file or directory */ |
| 912 | static FTSENT *oval_fts_read_recurse_path(OVAL_FTS *ofts) |
| 913 | { |
| 914 | FTSENT *out_fts_ent = NULL; |
| 915 | /* the condition below is correct because ofts_sfilepath is NULL here */ |
| 916 | bool collect_dirs = (ofts->ofts_sfilename == NULL); |
| 917 | |
| 918 | switch (ofts->direction) { |
| 919 | |
| 920 | case OVAL_RECURSE_DIRECTION_DOWN: |
| 921 | case OVAL_RECURSE_DIRECTION_NONE: |
| 922 | if (ofts->direction == OVAL_RECURSE_DIRECTION_NONE |
| 923 | && collect_dirs) { |
| 924 | /* the target is the directory itself */ |
| 925 | out_fts_ent = ofts->ofts_match_path_fts_ent; |
| 926 | ofts->ofts_match_path_fts_ent = NULL; |
| 927 | break; |
| 928 | } |
| 929 | |
| 930 | /* initialize separate fts for recursion */ |
| 931 | if (ofts->ofts_recurse_path_fts == NULL) { |
| 932 | char * const paths[2] = { ofts->ofts_match_path_fts_ent->fts_path, NULL }; |
| 933 | |
| 934 | #if defined(OSCAP_FTS_DEBUG) |
| 935 | dD("fts_open args: path: \"%s\", options: %d.", |
| 936 | paths[0], ofts->ofts_recurse_path_fts_opts); |
| 937 | #endif |
| 938 | /* reset errno as fts_open() doesn't do it itself. */ |
| 939 | errno = 0; |
| 940 | ofts->ofts_recurse_path_fts = fts_open(paths, |
| 941 | ofts->ofts_recurse_path_fts_opts, NULL); |
| 942 | /* fts_open() doesn't return NULL for all errors |
| 943 | (e.g. nonexistent paths), so check errno to detect it. |
| 944 | Far from being perfect. */ |
| 945 | if (ofts->ofts_recurse_path_fts == NULL || errno != 0) { |
| 946 | dE("fts_open() failed, errno: %d \"%s\".", |
| 947 | errno, strerror(errno)); |
| 948 | #if !defined(OSCAP_FTS_DEBUG) |
| 949 | dE("fts_open args: path: \"%s\", options: %d.", |
| 950 | paths[0], ofts->ofts_recurse_path_fts_opts); |
| 951 | #endif |
| 952 | if (ofts->ofts_recurse_path_fts != NULL) { |
| 953 | fts_close(ofts->ofts_recurse_path_fts); |
| 954 | ofts->ofts_recurse_path_fts = NULL; |
| 955 | } |
| 956 | return (NULL); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /* iterate until a match is found or all elements have been traversed */ |
| 961 | while (out_fts_ent == NULL) { |
| 962 | FTSENT *fts_ent; |
| 963 | |
| 964 | fts_ent = fts_read(ofts->ofts_recurse_path_fts); |
| 965 | if (fts_ent == NULL) { |
| 966 | fts_close(ofts->ofts_recurse_path_fts); |
| 967 | ofts->ofts_recurse_path_fts = NULL; |
| 968 | |
| 969 | return NULL; |
| 970 | } |
| 971 | |
| 972 | switch (fts_ent->fts_info) { |
| 973 | case FTS_DP: |
| 974 | continue; |
| 975 | case FTS_DC: |
| 976 | dW("Filesystem tree cycle detected at '%s'.", fts_ent->fts_path); |
| 977 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 978 | continue; |
| 979 | } |
| 980 | |
| 981 | #if defined(OSCAP_FTS_DEBUG) |
| 982 | dD("fts_path: '%s' (l=%d)." |
| 983 | "fts_name: '%s' (l=%d).\n" |
| 984 | "fts_info: %u.\n", fts_ent->fts_path, fts_ent->fts_pathlen, |
| 985 | fts_ent->fts_name, fts_ent->fts_namelen, fts_ent->fts_info); |
| 986 | #endif |
| 987 | |
| 988 | /* collect matching target */ |
| 989 | if (collect_dirs) { |
| 990 | if (fts_ent->fts_info == FTS_D |
| 991 | && (ofts->max_depth == -1 || fts_ent->fts_level <= ofts->max_depth)) |
| 992 | out_fts_ent = fts_ent; |
| 993 | } else { |
| 994 | if (fts_ent->fts_info != FTS_D) { |
| 995 | SEXP_t *stmp; |
| 996 | |
| 997 | stmp = SEXP_string_newf("%s", fts_ent->fts_name); |
| 998 | oval_result_t result = probe_entobj_cmp(ofts->ofts_sfilename, stmp); |
| 999 | switch (result){ |
| 1000 | case OVAL_RESULT_TRUE: |
| 1001 | out_fts_ent = fts_ent; |
| 1002 | break; |
| 1003 | |
| 1004 | case OVAL_RESULT_ERROR: |
| 1005 | probe_cobj_set_flag(ofts->result, SYSCHAR_FLAG_ERROR); |
| 1006 | break; |
| 1007 | |
| 1008 | default: |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | SEXP_free(stmp); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | if (fts_ent->fts_level > 0) { /* don't skip fts root */ |
| 1017 | /* limit recursion depth */ |
| 1018 | if (ofts->direction == OVAL_RECURSE_DIRECTION_NONE |
| 1019 | || (ofts->max_depth != -1 && fts_ent->fts_level > ofts->max_depth)) { |
| 1020 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | /* limit recursion only to selected file types */ |
| 1025 | switch (fts_ent->fts_info) { |
| 1026 | case FTS_D: |
| 1027 | if (!(ofts->recurse & OVAL_RECURSE_DIRS) && !(ofts->recurse & OVAL_RECURSE_SYMLINKS && ofts->following)) { |
| 1028 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1029 | continue; |
| 1030 | } |
| 1031 | ofts->following = 0; |
| 1032 | break; |
| 1033 | case FTS_SL: |
| 1034 | if (!(ofts->recurse & OVAL_RECURSE_SYMLINKS)) { |
| 1035 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1036 | continue; |
| 1037 | } |
| 1038 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_FOLLOW); |
| 1039 | ofts->following = 1; |
| 1040 | break; |
| 1041 | default: |
| 1042 | continue; |
| 1043 | } |
| 1044 | } |
| 1045 | if (_oval_fts_is_local(ofts, fts_ent)) { |
| 1046 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1047 | continue; |
| 1048 | } |
| 1049 | /* don't recurse beyond the initial filesystem */ |
| 1050 | if (ofts->filesystem == OVAL_RECURSE_FS_DEFINED |
| 1051 | && (fts_ent->fts_info == FTS_D || fts_ent->fts_info == FTS_SL) |
| 1052 | && ofts->ofts_recurse_path_devid != fts_ent->fts_statp->st_dev) { |
| 1053 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1054 | continue; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | break; |
| 1059 | case OVAL_RECURSE_DIRECTION_UP: |
| 1060 | if (ofts->ofts_recurse_path_pthcpy == NULL) { |
| 1061 | ofts->ofts_recurse_path_pthcpy = \ |
| 1062 | ofts->ofts_recurse_path_curpth = strdup(ofts->ofts_match_path_fts_ent->fts_path); |
| 1063 | ofts->ofts_recurse_path_curdepth = 0; |
| 1064 | } |
| 1065 | |
| 1066 | while (ofts->max_depth == -1 || ofts->ofts_recurse_path_curdepth <= ofts->max_depth) { |
| 1067 | /* initialize separate fts for recursion */ |
| 1068 | if (ofts->ofts_recurse_path_fts == NULL) { |
| 1069 | char * const paths[2] = { ofts->ofts_recurse_path_curpth, NULL }; |
| 1070 | |
| 1071 | #if defined(OSCAP_FTS_DEBUG) |
| 1072 | dD("fts_open args: path: \"%s\", options: %d.", |
| 1073 | paths[0], ofts->ofts_recurse_path_fts_opts); |
| 1074 | #endif |
| 1075 | /* reset errno as fts_open() doesn't do it itself. */ |
| 1076 | errno = 0; |
| 1077 | /* fts_open() doesn't return NULL for all errors |
| 1078 | (e.g. nonexistent paths), so check errno to |
| 1079 | detect it. Far from being perfect. */ |
| 1080 | ofts->ofts_recurse_path_fts = fts_open(paths, |
| 1081 | ofts->ofts_recurse_path_fts_opts, NULL); |
| 1082 | if (ofts->ofts_recurse_path_fts == NULL || errno != 0) { |
| 1083 | dE("fts_open() failed, errno: %d \"%s\".", |
| 1084 | errno, strerror(errno)); |
| 1085 | #if !defined(OSCAP_FTS_DEBUG) |
| 1086 | dE("fts_open args: path: \"%s\", options: %d.", |
| 1087 | paths[0], ofts->ofts_recurse_path_fts_opts); |
| 1088 | #endif |
| 1089 | if (ofts->ofts_recurse_path_fts != NULL) { |
| 1090 | fts_close(ofts->ofts_recurse_path_fts); |
| 1091 | ofts->ofts_recurse_path_fts = NULL; |
| 1092 | } |
| 1093 | return (NULL); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /* iterate until a match is found or all elements have been traversed */ |
| 1098 | while (out_fts_ent == NULL) { |
| 1099 | FTSENT *fts_ent; |
| 1100 | |
| 1101 | fts_ent = fts_read(ofts->ofts_recurse_path_fts); |
| 1102 | if (fts_ent == NULL) |
| 1103 | break; |
| 1104 | |
| 1105 | /* |
| 1106 | it would be more accurate to obtain the device |
| 1107 | id here, but for the sake of supporting the |
| 1108 | comparison also in oval_fts_read_match_path(), |
| 1109 | the device id is obtained in oval_fts_open_prefixed() |
| 1110 | |
| 1111 | if (ofts->ofts_recurse_path_curdepth == 0) |
| 1112 | ofts->ofts_recurse_path_devid = fts_ent->fts_statp->st_dev; |
| 1113 | */ |
| 1114 | #if defined(OS_SOLARIS) |
| 1115 | if ((!OVAL_FTS_localp(ofts, fts_ent->fts_path, |
| 1116 | (fts_ent->fts_statp != NULL) ? |
| 1117 | &fts_ent->fts_statp->st_fstype : NULL))) |
| 1118 | break; |
| 1119 | #else |
| 1120 | if (ofts->filesystem == OVAL_RECURSE_FS_LOCAL |
| 1121 | && (!OVAL_FTS_localp(ofts, fts_ent->fts_path, |
| 1122 | (fts_ent->fts_statp != NULL) ? |
| 1123 | &fts_ent->fts_statp->st_dev : NULL))) |
| 1124 | break; |
| 1125 | #endif |
| 1126 | if (ofts->filesystem == OVAL_RECURSE_FS_DEFINED |
| 1127 | && ofts->ofts_recurse_path_devid != fts_ent->fts_statp->st_dev) |
| 1128 | break; |
| 1129 | |
| 1130 | /* collect matching target */ |
| 1131 | if (collect_dirs) { |
| 1132 | /* only fts root is collected */ |
| 1133 | if (fts_ent->fts_level == 0 && fts_ent->fts_info == FTS_D) { |
| 1134 | out_fts_ent = fts_ent; |
| 1135 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1136 | break; |
| 1137 | } |
| 1138 | } else { |
| 1139 | if (fts_ent->fts_info != FTS_D && fts_ent->fts_info != FTS_DP && fts_ent->fts_info != FTS_DC) { |
| 1140 | SEXP_t *stmp; |
| 1141 | |
| 1142 | stmp = SEXP_string_newf("%s", fts_ent->fts_name); |
| 1143 | if (probe_entobj_cmp(ofts->ofts_sfilename, stmp) == OVAL_RESULT_TRUE) |
| 1144 | out_fts_ent = fts_ent; |
| 1145 | SEXP_free(stmp); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (fts_ent->fts_info == FTS_SL) |
| 1150 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_FOLLOW); |
| 1151 | /* limit recursion only to fts root */ |
| 1152 | else if (fts_ent->fts_level > 0) |
| 1153 | fts_set(ofts->ofts_recurse_path_fts, fts_ent, FTS_SKIP); |
| 1154 | } |
| 1155 | |
| 1156 | if (out_fts_ent != NULL) |
| 1157 | break; |
| 1158 | |
| 1159 | fts_close(ofts->ofts_recurse_path_fts); |
| 1160 | ofts->ofts_recurse_path_fts = NULL; |
| 1161 | |
| 1162 | if (!strcmp(ofts->ofts_recurse_path_curpth, "/")) |
| 1163 | break; |
| 1164 | |
| 1165 | ofts->ofts_recurse_path_curpth = oscap_dirname(ofts->ofts_recurse_path_curpth); |
| 1166 | ofts->ofts_recurse_path_curdepth++; |
| 1167 | } |
| 1168 | |
| 1169 | if (out_fts_ent == NULL) { |
| 1170 | free(ofts->ofts_recurse_path_pthcpy); |
| 1171 | ofts->ofts_recurse_path_pthcpy = NULL; |
| 1172 | } |
| 1173 | |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | return out_fts_ent; |
| 1178 | } |
| 1179 | |
| 1180 | OVAL_FTSENT *oval_fts_read(OVAL_FTS *ofts) |
| 1181 | { |
| 1182 | FTSENT *fts_ent; |
| 1183 | |
| 1184 | #if defined(OSCAP_FTS_DEBUG) |
| 1185 | dD("ofts: %p.", ofts); |
| 1186 | #endif |
| 1187 | |
| 1188 | if (ofts == NULL) |
| 1189 | return NULL; |
| 1190 | |
| 1191 | for (;;) { |
| 1192 | if (ofts->ofts_match_path_fts_ent == NULL) { |
| 1193 | ofts->ofts_match_path_fts_ent = oval_fts_read_match_path(ofts); |
| 1194 | if (ofts->ofts_match_path_fts_ent == NULL) |
| 1195 | return NULL; |
| 1196 | } |
| 1197 | |
| 1198 | if (ofts->ofts_sfilepath) { |
| 1199 | fts_ent = ofts->ofts_match_path_fts_ent; |
| 1200 | ofts->ofts_match_path_fts_ent = NULL; |
| 1201 | if (ofts->filesystem == OVAL_RECURSE_FS_LOCAL |
| 1202 | && (!OVAL_FTS_localp(ofts, fts_ent->fts_path, |
| 1203 | (fts_ent->fts_statp != NULL) ? |
| 1204 | &fts_ent->fts_statp->st_dev : NULL))) { |
| 1205 | continue; |
| 1206 | } |
| 1207 | break; |
| 1208 | } else { |
| 1209 | fts_ent = oval_fts_read_recurse_path(ofts); |
| 1210 | if (fts_ent != NULL) |
| 1211 | break; |
| 1212 | |
| 1213 | ofts->ofts_match_path_fts_ent = NULL; |
| 1214 | |
| 1215 | // todo: is this true when variables are used? |
| 1216 | /* with 'equals', there's only one potential target */ |
| 1217 | if (ofts->ofts_path_op == OVAL_OPERATION_EQUALS) |
| 1218 | return (NULL); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | return OVAL_FTSENT_new(ofts, fts_ent); |
| 1223 | } |
| 1224 | |
| 1225 | void oval_ftsent_free(OVAL_FTSENT *ofts_ent) |
| 1226 | { |
| 1227 | OVAL_FTSENT_free(ofts_ent); |
| 1228 | } |
| 1229 | |
| 1230 | int oval_fts_close(OVAL_FTS *ofts) |
| 1231 | { |
| 1232 | if (ofts->ofts_recurse_path_pthcpy != NULL) |
| 1233 | free(ofts->ofts_recurse_path_pthcpy); |
| 1234 | |
| 1235 | if (ofts->ofts_path_regex) |
| 1236 | oscap_pcre_free(ofts->ofts_path_regex); |
| 1237 | |
| 1238 | if (ofts->ofts_spath != NULL) |
| 1239 | SEXP_free(ofts->ofts_spath); |
| 1240 | if (ofts->ofts_sfilename != NULL) |
| 1241 | SEXP_free(ofts->ofts_sfilename); |
| 1242 | if (ofts->ofts_sfilepath != NULL) |
| 1243 | SEXP_free(ofts->ofts_sfilepath); |
| 1244 | |
| 1245 | fsdev_free(ofts->localdevs); |
| 1246 | |
| 1247 | OVAL_FTS_free(ofts); |
| 1248 | #if defined(OS_SOLARIS) |
| 1249 | free_zones_path_list(); |
| 1250 | #endif |
| 1251 | |
| 1252 | return (0); |
| 1253 | } |