eBPF-based security platform
string: add name_glob_match() for a file name and a pattern
John Fastabend7460870bf239
diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 41a7db40..51ca1201 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -87,6 +87,13 @@ int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)*/bool name_has_suffix(const char *name, size_t len, const char *suffix);+/*+ * Whether the len bytes at name are a file name that pattern matches: '*'+ * in pattern stands for any run of bytes and '?' for any one, every other+ * byte for itself. "." and ".." match no pattern.+ */+bool name_glob_match(const char *pattern, const char *name, size_t len);+/** The length of prefix where s starts with it, else 0. prefix is a string* literal, whose length the compiler takes from its size: anything elsediff --git a/testing/unit/string.c b/testing/unit/string.cindex 6cb10599..e2119f72 100644--- a/testing/unit/string.c+++ b/testing/unit/string.c@@ -510,6 +510,60 @@ static void name_has_suffix_wants_a_stem(void)EXPECT_TRUE(name_has_suffix("a", 1, ""));}+static bool glob(const char *pattern, const char *name)+{+ return name_glob_match(pattern, name, strlen(name));+}++static void name_glob_match_takes_stars_and_question_marks(void)+{+ EXPECT_TRUE(glob("php-fpm", "php-fpm"));+ EXPECT_FALSE(glob("php-fpm", "php-fpm8"));+ EXPECT_FALSE(glob("php-fpm8", "php-fpm"));+ EXPECT_TRUE(glob("php-fpm*", "php-fpm"));+ EXPECT_TRUE(glob("php-fpm*", "php-fpm8.3"));+ EXPECT_FALSE(glob("php-fpm*", "php-cgi8.3"));+ EXPECT_TRUE(glob("*.php", "index.php"));+ EXPECT_TRUE(glob("*.php", ".php"));+ EXPECT_FALSE(glob("*.php", "index.php.bak"));+ EXPECT_TRUE(glob("a*c", "ac"));+ EXPECT_TRUE(glob("a*c", "abcbc"));+ EXPECT_FALSE(glob("a*c", "abcb"));+ EXPECT_TRUE(glob("*a*b*", "xaybz"));+ EXPECT_TRUE(glob("a**b", "ab"));+ EXPECT_FALSE(glob("*a*b*", "xbya"));+ EXPECT_TRUE(glob("php?.?", "php8.3"));+ EXPECT_FALSE(glob("php?.?", "php8.31"));+ EXPECT_FALSE(glob("php?", "php"));+ EXPECT_TRUE(glob("*", "a"));+ EXPECT_TRUE(glob("*", ""));+ EXPECT_FALSE(glob("?", ""));+ EXPECT_FALSE(glob("", "a"));+ EXPECT_TRUE(glob("", ""));+}++/* A pattern is for the entries of a directory, which these two are not. */+static void name_glob_match_leaves_out_dot_and_dotdot(void)+{+ EXPECT_FALSE(glob("*", "."));+ EXPECT_FALSE(glob("*", ".."));+ EXPECT_FALSE(glob(".", "."));+ EXPECT_FALSE(glob("?", "."));+ EXPECT_FALSE(glob("??", ".."));+ EXPECT_TRUE(glob("*", "..."));+ EXPECT_TRUE(glob(".*", ".profile"));+ EXPECT_TRUE(glob("??", "a."));+}++/* The name comes with a length and need not end where its buffer does. */+static void name_glob_match_reads_len_bytes(void)+{+ EXPECT_TRUE(name_glob_match("php*", "php-fpm/junk", 7));+ EXPECT_FALSE(name_glob_match("*junk", "php-fpm/junk", 7));+ EXPECT_FALSE(name_glob_match("a", "a\0b", 3));+ EXPECT_TRUE(name_glob_match("a?b", "a\0b", 3));+}+#define TEST_DIR "/run"static void str_has_prefix_gives_its_length(void)@@ -880,6 +934,9 @@ static const struct unit_case cases[] = {UNIT_CASE(memzero_explicit_clears_its_span_only),UNIT_CASE(memneq_explicit_tells_every_bit_apart),UNIT_CASE(name_has_suffix_wants_a_stem),+ UNIT_CASE(name_glob_match_takes_stars_and_question_marks),+ UNIT_CASE(name_glob_match_leaves_out_dot_and_dotdot),+ UNIT_CASE(name_glob_match_reads_len_bytes),UNIT_CASE(str_has_prefix_gives_its_length),UNIT_CASE(str_skip_prefix_moves_only_on_a_match),UNIT_CASE(span_equals_wants_the_whole_literal),diff --git a/util/string.c b/util/string.cindex b1aefc52..245d7e89 100644--- a/util/string.c+++ b/util/string.c@@ -175,6 +175,38 @@ bool name_has_suffix(const char *name, size_t len, const char *suffix)return len > n && !memcmp(name + len - n, suffix, n);}+static bool name_is_dot_or_dotdot(const char *name, size_t len)+{+ return (len == 1 || len == 2) && name[0] == '.' && name[len - 1] == '.';+}++bool name_glob_match(const char *pattern, const char *name, size_t len)+{+ const char *after_star = NULL;+ size_t i = 0, retry_at = 0;++ if (name_is_dot_or_dotdot(name, len))+ return false;+ while (i < len) {+ if (*pattern == '*') {+ after_star = ++pattern;+ retry_at = i;+ } else if (*pattern == '?' ||+ (*pattern && *pattern == name[i])) {+ pattern++;+ i++;+ } else if (after_star) {+ pattern = after_star;+ i = ++retry_at;+ } else {+ return false;+ }+ }+ while (*pattern == '*')+ pattern++;+ return !*pattern;+}+bool dns_name_valid(const char *name){size_t len = strlen(name), label_len = 0, label_start = 0;