]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
subcription: now moderation goes into a subdirectory "subscribe"
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 5 Jul 2023 14:17:45 +0000 (16:17 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 5 Jul 2023 14:24:25 +0000 (16:24 +0200)
doing this allows us to avoid manipulating memory and use fd instead
as a fallback mlmmj still lookup for subscribe<cookie>

ChangeLog
src/subscriberfuncs.c
tests/mlmmj.c

index 19aaf19160caf721123455af87df28c65504e26b..611d33754a7560f5b09a14dc67cfcbe6355ae5ac 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+1.4.0-b2
+ o moderation cookie for subscription is now under the sub directory
+   moderation/subscribe
 1.4.0-b1
  o manpage converted from man(7) to mdoc(7)
  o Fix a potential crash with mail without separator between headers and body
index b8592c80f2a138868581a94dc11298fe018772e6..a30171d837a1afe75479a98891e1dee2d0b82f91 100644 (file)
@@ -403,24 +403,36 @@ do_unsubscribe(struct ml *ml, const char *addr, enum subtype typesub,
 void
 mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum subtype *subtypeptr)
 {
-       int fd;
-       char *readtype, *modfilename;
+       int fd, dfd;
+       char *readtype, *modfilename = NULL;
        char *buf, *walk;
        size_t i;
 
        if (strncmp(modstr, "subscribe", 9) == 0)
                        modstr += 9;
 
-       xasprintf(&modfilename, "moderation/subscribe%s", modstr);
-
-       fd = openat(ml->fd, modfilename, O_RDONLY);
+       dfd = openat(ml->fd, "moderation/subscribe", O_DIRECTORY);
+       if (dfd != -1) {
+               fd = openat(dfd, modstr, O_RDONLY);
+       }
+       if (dfd == -1 || fd == -1) {
+               xasprintf(&modfilename, "moderation/subscribe%s", modstr);
+               fd = openat(ml->fd, modfilename, O_RDONLY);
+       }
        if(fd < 0) {
-               log_error(LOG_ARGS, "Could not open %s/%s", ml->dir, modfilename);
+               log_error(LOG_ARGS, "Could not open "
+                   "%s/moderation/subscribe/%s nor %s/%s", ml->dir, modstr,
+                   ml->dir, modfilename);
                exit(EXIT_FAILURE);
        }
        walk = buf = readlf(fd, false);
        if (buf == NULL) {
-               log_error(LOG_ARGS, "Could not open %s/%s", ml->dir, modfilename);
+               if (modfilename == NULL)
+                       log_error(LOG_ARGS, "Invalid %s/moderation/subscribe/%s",
+                           ml->dir, modstr);
+               else
+                       log_error(LOG_ARGS, "Invalid %s/%s", ml->dir,
+                           modfilename);
                exit(EXIT_FAILURE);
        }
        *addrptr = xstrdup(strsep(&walk, "\n"));
@@ -442,6 +454,11 @@ mod_get_addr_and_type(struct ml *ml, const char *modstr, char **addrptr, enum su
        }
 
        free(buf);
-       unlinkat(ml->fd, modfilename, 0);
+       if (modfilename != NULL)
+               unlinkat(ml->fd, modfilename, 0);
+       else
+               unlinkat(dfd, modstr, 0);
+       if (dfd != -1)
+               close(dfd);
        free(modfilename);
 }
index ff8ddb89b32240db64eb7f1ea3414252d9500464..d228adfc81b843bc37a5f3700e8db3c061ca0dc9 100644 (file)
@@ -2664,6 +2664,59 @@ ATF_TC_BODY(mod_get_addr_type, tc)
                exit(EXIT_SUCCESS);
        }
        atf_utils_wait(p, 1, "", "");
+
+       mkdir("moderation/subscribe", 0755);
+       atf_utils_create_file("moderation/subscribe/valid2", "user1\nSUB_BOTH\n");
+       atf_utils_create_file("moderation/subscribevalid2", "user2\nSUB_BOTH\n");
+       p = atf_utils_fork();
+       if (p == 0) {
+               struct ml ml;
+               enum subtype subtype = 0;
+               char *addr;
+               ml.fd = open(".", O_DIRECTORY);
+               mod_get_addr_and_type(&ml, "valid2", &addr, &subtype);
+               ATF_REQUIRE_STREQ(addr, "user1");
+               ATF_REQUIRE_EQ(subtype, SUB_BOTH);
+               exit(EXIT_SUCCESS);
+       }
+       atf_utils_wait(p, 0, "", "");
+
+       atf_utils_create_file("moderation/subscribevalid3", "user2\nSUB_BOTH\n");
+       p = atf_utils_fork();
+       if (p == 0) {
+               struct ml ml;
+               enum subtype subtype = 0;
+               char *addr;
+               ml.fd = open(".", O_DIRECTORY);
+               mod_get_addr_and_type(&ml, "valid3", &addr, &subtype);
+               ATF_REQUIRE_STREQ(addr, "user2");
+               ATF_REQUIRE_EQ(subtype, SUB_BOTH);
+               exit(EXIT_SUCCESS);
+       }
+       atf_utils_wait(p, 0, "", "");
+
+       p = atf_utils_fork();
+       if (p == 0) {
+               struct ml ml;
+               enum subtype subtype = 0;
+               char *addr;
+               ml.fd = open(".", O_DIRECTORY);
+               mod_get_addr_and_type(&ml, "valid4", &addr, &subtype);
+               exit(EXIT_SUCCESS);
+       }
+       atf_utils_wait(p, 1, "", "");
+
+       atf_utils_create_file("moderation/subscribe/invalid5", "");
+       p = atf_utils_fork();
+       if (p == 0) {
+               struct ml ml;
+               enum subtype subtype = 0;
+               char *addr;
+               ml.fd = open(".", O_DIRECTORY);
+               mod_get_addr_and_type(&ml, "invalid5", &addr, &subtype);
+               exit(EXIT_SUCCESS);
+       }
+       atf_utils_wait(p, 1, "", "");
 }
 
 ATF_TP_ADD_TCS(tp)