]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
access: accept -<qualifier> after actions
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 27 Aug 2024 15:10:53 +0000 (17:10 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 27 Aug 2024 15:10:53 +0000 (17:10 +0200)
include/mlmmj.h
src/access.c
src/mlmmj-process.c
tests/mlmmj.c

index efd32aff8c476d61add9c471e1fe8674e77e958d..3c6f2d21fe5b8a66c5966476695198e11019b4b6 100644 (file)
@@ -146,7 +146,7 @@ typedef enum {
        ACT_DISCARD,
 } actions_t;
 
-actions_t do_access(strlist *rules, strlist *headers, const char *from, char **msg);
+actions_t do_access(strlist *rules, strlist *headers, const char *from, char **msg, char **qualifier);
 
 #define MY_ASSERT(expression) if (!(expression)) { \
                        errno = 0; \
index dfd317a53f8366ba23cd48461825063c5bd39fd9..69bcc7ec05f73ae5b70d1709a771d59fe2bf12ce 100644 (file)
@@ -36,7 +36,7 @@ static char *action_strs[] = {
 };
 
 actions_t
-do_access(strlist *rules, strlist *headers, const char *from, char **msg)
+do_access(strlist *rules, strlist *headers, const char *from, char **msg, char **qualifier)
 {
        unsigned int match;
        char *rule_ptr;
@@ -49,6 +49,8 @@ do_access(strlist *rules, strlist *headers, const char *from, char **msg)
        size_t i = 0;
        if (msg != NULL)
                *msg = NULL;
+       if (qualifier != NULL)
+               *qualifier = NULL;
 
        if (rules == NULL)
                return (ACT_ALLOW);
@@ -81,6 +83,14 @@ do_access(strlist *rules, strlist *headers, const char *from, char **msg)
 
                if (*rule_ptr == ' ') {
                        rule_ptr++;
+               } else if (*rule_ptr == '-') {
+                       rule_ptr++;
+                       const char *qual = rule_ptr;
+                       while (*rule_ptr != ' ' && *rule_ptr != '\0')
+                               rule_ptr++;
+                       if (rule_ptr - qual != 0 && qualifier != NULL)
+                               *qualifier = xstrndup(qual, rule_ptr - qual);
+                       rule_ptr++;
                } else if (*rule_ptr == '\0') {
                        /* the rule is a keyword and no regexp */
                        if (msg != NULL)
index 12f6b7d426c3b5586b5dd1b722669084f7946fa1..bf7e5670120b767ecdf27b1e84778367401ec0d2 100644 (file)
@@ -605,10 +605,11 @@ int main(int argc, char **argv)
        if (access_rules != NULL) {
                actions_t accret;
                char *error = NULL;
+               char *qualifier = NULL;
                /* Don't send a mail about denial to the list, but silently
                 * discard and exit. Also do this in case it's turned off */
                accret = do_access(access_rules, &allheaders,
-                                       posteraddr, &error);
+                                       posteraddr, &error, &qualifier);
                if (error != NULL) {
                        log_oper(ml.fd, OPLOGFNAME, "%s", error);
                }
index 3c714b41cbe3776641ee170c5aa7d7668a9c1b6a..704c45638ec550e921260cd37c2b2e87feb5b25f 100644 (file)
@@ -2930,25 +2930,43 @@ ATF_TC_BODY(do_access, tc)
        strlist headers = tll_init();
        tll_push_back(rules, "allow");
        char *msg = NULL;
+       char *qualifier = NULL;
 
-       ATF_REQUIRE_EQ(do_access(NULL, NULL, "test@plop", &msg), ACT_ALLOW);
+       ATF_REQUIRE_EQ(do_access(NULL, NULL, "test@plop", &msg, &qualifier), ACT_ALLOW);
        ATF_REQUIRE(msg == NULL);
+       ATF_REQUIRE(qualifier == NULL);
 
-       ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg), ACT_ALLOW);
+       ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg, &qualifier), ACT_ALLOW);
        ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" was allowed by rule #0 \"allow\"");
+       ATF_REQUIRE(qualifier == NULL);
        tll_pop_back(rules);
        tll_push_back(rules, "deny");
-       ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg), ACT_DENY);
+       ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg, &qualifier), ACT_DENY);
        ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" was denied by rule #0 \"deny\"");
+       ATF_REQUIRE(qualifier == NULL);
        tll_pop_back(rules);
        tll_push_back(rules, "crap");
-       ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg), ACT_DENY);
+       ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg, &qualifier), ACT_DENY);
        ATF_REQUIRE_STREQ(msg, "Unable to parse rule #0 \"crap\": Missing action keyword. Denying post from \"test@plop\"");
+       ATF_REQUIRE(qualifier == NULL);
        tll_pop_back(rules);
        tll_push_back(headers, "from: toto@bla");
        tll_push_back(rules, "moderate ^from: toto@");
-       ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", &msg), ACT_MODERATE);
+       ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", &msg, &qualifier), ACT_MODERATE);
        ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" with header \"from: toto@bla\" was moderated by rule #0 \"moderate ^from: toto@\"");
+       ATF_REQUIRE(qualifier == NULL);
+       tll_push_front(rules, "moderatemeh ^from: toto@");
+       ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", &msg, &qualifier), ACT_DENY);
+       ATF_REQUIRE_STREQ(msg, "Unable to parse rule #0 \"moderatemeh ^from: toto@\": Invalid character after action keyword. Denying post from \"test@plop\"");
+       ATF_REQUIRE(qualifier == NULL);
+       tll_push_front(rules, "moderate- ^from: toto@");
+       ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", &msg, &qualifier), ACT_MODERATE);
+       ATF_REQUIRE(qualifier == NULL);
+       ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" with header \"from: toto@bla\" was moderated by rule #0 \"moderate- ^from: toto@\"");
+       tll_push_front(rules, "moderate-meh ^from: toto@");
+       ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", &msg, &qualifier), ACT_MODERATE);
+       ATF_REQUIRE_STREQ(qualifier, "meh");
+       ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" with header \"from: toto@bla\" was moderated by rule #0 \"moderate-meh ^from: toto@\"");
 }
 
 ATF_TP_ADD_TCS(tp)