From d0b15701a8d4c6ee09040cdca89ac56724e7471a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 18 Feb 2025 13:24:39 +0900 Subject: [PATCH] WIP white/black-list: change "contains" to any/all Changed `--whitelist-contains` and `--blacklist-contains` into: `--whitelist-contains-any` `--whitelist-contains-all` `--blacklist-contains-any` `--blacklist-contains-all` --- README.md | 6 ++-- src/helpers.c | 41 ++++++++++++++++++---- src/parser.c | 96 ++++++++++++++++++++++++++++++++++++++++----------- src/parser.h | 6 ++-- 4 files changed, 118 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 0eaab95..e9852dd 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,12 @@ API calls. --force-dir-permissions <3-octal-values> : Force set permissions for directories on archive creation/extraction Must be three octal characters like "755" or "440" --force-empty-dir-permissions <3-octal-values> : Force set EMPTY dir permissions. Like "--force-dir-permissions", but for empty directories. - --whitelist-contains : Whitelist entries to contain "", specify multiple times to require multiple "" entries at once. + --whitelist-contains-any : Whitelist entries to contain "", specify multiple times to allow entries that contain any of the specified ""s. + --whitelist-contains-all : Whitelist entries to contain "", specify multiple times to allow entries that contain all of the specified ""s. --whitelist-begins-with : Whitelist entries to start with "", specify multiple times to allow different entries to start with different "" entries. --whitelist-ends-with : Whitelist entries to end with "", specify multiple times to allow different entries to end with different "" entries. - --blacklist-contains : blacklist entries that contains "", specify multiple times to deny entries that contain all the specified ""s. + --blacklist-contains-any : blacklist entries that contains "", specify multiple times to deny entries that contain any of the specified ""s. + --blacklist-contains-all : blacklist entries that contains "", specify multiple times to deny entries that contain all of the specified ""s. --blacklist-begins-with : blacklist entries that starts with "", specify multiple times to deny multiple entries starting with different "" entries. --blacklist-ends-with : blacklist entries that ends with "", specify multiple times to deny multiple entries ending with different "" entries. --wb-case-insensitive : Makes white/black-list checking case insensitive. diff --git a/src/helpers.c b/src/helpers.c index d8e1068..fc17982 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -723,9 +723,26 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( const char *cstring, uint_fast8_t case_i, const SDArchiverParsed *parsed) { - if (parsed->whitelist_contains) { - for (const SDArchiverLLNode *node = parsed->whitelist_contains->head->next; - node != parsed->whitelist_contains->tail; + if (parsed->whitelist_contains_any) { + uint_fast8_t contains_any = 0; + for (const SDArchiverLLNode *node = parsed->whitelist_contains_any->head->next; + node != parsed->whitelist_contains_any->tail; + node = node->next) { + if (node->data) { + if (simple_archiver_helper_string_contains( + cstring, node->data, case_i)) { + contains_any = 1; + break; + } + } + } + if (!contains_any) { + return 0; + } + } + if (parsed->whitelist_contains_all) { + for (const SDArchiverLLNode *node = parsed->whitelist_contains_all->head->next; + node != parsed->whitelist_contains_all->tail; node = node->next) { if (node->data) { if (!simple_archiver_helper_string_contains( @@ -759,10 +776,22 @@ uint_fast8_t simple_archiver_helper_string_allowed_lists( } } - if (parsed->blacklist_contains) { + if (parsed->blacklist_contains_any) { + for (const SDArchiverLLNode *node = parsed->blacklist_contains_any->head->next; + node != parsed->blacklist_contains_any->tail; + node = node->next) { + if (node->data) { + if (simple_archiver_helper_string_contains( + cstring, node->data, case_i)) { + return 0; + } + } + } + } + if (parsed->blacklist_contains_all) { uint_fast8_t contains_all = 1; - for (const SDArchiverLLNode *node = parsed->blacklist_contains->head->next; - node != parsed->blacklist_contains->tail; + for (const SDArchiverLLNode *node = parsed->blacklist_contains_all->head->next; + node != parsed->blacklist_contains_all->tail; node = node->next) { if (node->data) { if (!simple_archiver_helper_string_contains( diff --git a/src/parser.c b/src/parser.c index 7a52ccd..8374f2d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -244,9 +244,13 @@ void simple_archiver_print_usage(void) { "dir permissions. Like \"--force-dir-permissions\", but for empty " "directories.\n"); fprintf(stderr, - "--whitelist-contains : Whitelist entries to contain " - "\"\", specify multiple times to require multiple \"\" " - "entries at once.\n"); + "--whitelist-contains-any : Whitelist entries to contain " + "\"\", specify multiple times to allow entries that contain " + "any of the specified \"\"s.\n"); + fprintf(stderr, + "--whitelist-contains-all : Whitelist entries to contain " + "\"\", specify multiple times to allow entries that contain " + "all of the specified \"\"s.\n"); fprintf(stderr, "--whitelist-begins-with : Whitelist entries to start with " "\"\", specify multiple times to allow different entries to " @@ -256,9 +260,13 @@ void simple_archiver_print_usage(void) { "\"\", specify multiple times to allow different entries to end" " with different \"\" entries.\n"); fprintf(stderr, - "--blacklist-contains : blacklist entries that contains " + "--blacklist-contains-any : blacklist entries that contains " + "\"\", specify multiple times to deny entries that contain any" + " of the specified \"\"s.\n"); + fprintf(stderr, + "--blacklist-contains-all : blacklist entries that contains " "\"\", specify multiple times to deny entries that contain all" - " the specified \"\"s.\n"); + " of the specified \"\"s.\n"); fprintf(stderr, "--blacklist-begins-with : blacklist entries that starts with " "\"\", specify multiple times to deny multiple entries " @@ -312,10 +320,12 @@ SDArchiverParsed simple_archiver_create_parsed(void) { parsed.mappings.GidToGid = simple_archiver_hash_map_init(); parsed.mappings.GnameToGname = simple_archiver_hash_map_init(); parsed.prefix = NULL; - parsed.whitelist_contains = NULL; + parsed.whitelist_contains_any = NULL; + parsed.whitelist_contains_all = NULL; parsed.whitelist_begins = NULL; parsed.whitelist_ends = NULL; - parsed.blacklist_contains = NULL; + parsed.blacklist_contains_any = NULL; + parsed.blacklist_contains_all = NULL; parsed.blacklist_begins = NULL; parsed.blacklist_ends = NULL; @@ -718,18 +728,37 @@ int simple_archiver_parse_args(int argc, const char **argv, --argc; ++argv; - } else if (strcmp(argv[0], "--whitelist-contains") == 0) { + } else if (strcmp(argv[0], "--whitelist-contains-any") == 0) { + if (argc < 2) { + fprintf(stderr, + "ERROR: --whitelist-contains-any expects an argument!\n"); + simple_archiver_print_usage(); + return 1; + } + + if (!out->whitelist_contains_any) { + out->whitelist_contains_any = simple_archiver_list_init(); + } + simple_archiver_list_add( + out->whitelist_contains_any, + (void *)argv[1], + simple_archiver_helper_datastructure_cleanup_nop); + + --argc; + ++argv; + } else if (strcmp(argv[0], "--whitelist-contains-all") == 0) { if (argc < 2) { - fprintf(stderr, "ERROR: --whitelist-contains expects an argument!\n"); + fprintf(stderr, + "ERROR: --whitelist-contains-all expects an argument!\n"); simple_archiver_print_usage(); return 1; } - if (!out->whitelist_contains) { - out->whitelist_contains = simple_archiver_list_init(); + if (!out->whitelist_contains_all) { + out->whitelist_contains_all = simple_archiver_list_init(); } simple_archiver_list_add( - out->whitelist_contains, + out->whitelist_contains_all, (void *)argv[1], simple_archiver_helper_datastructure_cleanup_nop); @@ -771,18 +800,37 @@ int simple_archiver_parse_args(int argc, const char **argv, --argc; ++argv; - } else if (strcmp(argv[0], "--blacklist-contains") == 0) { + } else if (strcmp(argv[0], "--blacklist-contains-any") == 0) { if (argc < 2) { - fprintf(stderr, "ERROR: --blacklist-contains expects an argument!\n"); + fprintf(stderr, + "ERROR: --blacklist-contains-any expects an argument!\n"); + simple_archiver_print_usage(); + return 1; + } + + if (!out->blacklist_contains_any) { + out->blacklist_contains_any = simple_archiver_list_init(); + } + simple_archiver_list_add( + out->blacklist_contains_any, + (void *)argv[1], + simple_archiver_helper_datastructure_cleanup_nop); + + --argc; + ++argv; + } else if (strcmp(argv[0], "--blacklist-contains-all") == 0) { + if (argc < 2) { + fprintf(stderr, + "ERROR: --blacklist-contains-all expects an argument!\n"); simple_archiver_print_usage(); return 1; } - if (!out->blacklist_contains) { - out->blacklist_contains = simple_archiver_list_init(); + if (!out->blacklist_contains_all) { + out->blacklist_contains_all = simple_archiver_list_init(); } simple_archiver_list_add( - out->blacklist_contains, + out->blacklist_contains_all, (void *)argv[1], simple_archiver_helper_datastructure_cleanup_nop); @@ -939,8 +987,11 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) { free(parsed->prefix); } - if (parsed->whitelist_contains) { - simple_archiver_list_free(&parsed->whitelist_contains); + if (parsed->whitelist_contains_any) { + simple_archiver_list_free(&parsed->whitelist_contains_any); + } + if (parsed->whitelist_contains_all) { + simple_archiver_list_free(&parsed->whitelist_contains_all); } if (parsed->whitelist_begins) { simple_archiver_list_free(&parsed->whitelist_begins); @@ -948,8 +999,11 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) { if (parsed->whitelist_ends) { simple_archiver_list_free(&parsed->whitelist_ends); } - if (parsed->blacklist_contains) { - simple_archiver_list_free(&parsed->blacklist_contains); + if (parsed->blacklist_contains_any) { + simple_archiver_list_free(&parsed->blacklist_contains_any); + } + if (parsed->blacklist_contains_all) { + simple_archiver_list_free(&parsed->blacklist_contains_all); } if (parsed->blacklist_begins) { simple_archiver_list_free(&parsed->blacklist_begins); diff --git a/src/parser.h b/src/parser.h index 241d595..7c6a9c5 100644 --- a/src/parser.h +++ b/src/parser.h @@ -99,10 +99,12 @@ typedef struct SDArchiverParsed { SDA_UGMapping mappings; /// Prefix for archived/extracted paths. char *prefix; - SDArchiverLinkedList *whitelist_contains; + SDArchiverLinkedList *whitelist_contains_any; + SDArchiverLinkedList *whitelist_contains_all; SDArchiverLinkedList *whitelist_begins; SDArchiverLinkedList *whitelist_ends; - SDArchiverLinkedList *blacklist_contains; + SDArchiverLinkedList *blacklist_contains_any; + SDArchiverLinkedList *blacklist_contains_all; SDArchiverLinkedList *blacklist_begins; SDArchiverLinkedList *blacklist_ends; } SDArchiverParsed; -- 2.49.0