From: Baptiste Daroussin Date: Wed, 5 Jul 2023 14:17:45 +0000 (+0200) Subject: subcription: now moderation goes into a subdirectory "subscribe" X-Git-Tag: RELEASE_1_4_0rc1~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a8ade017a445f1a63eb402a910ef34c83ed444cb;p=thirdparty%2Fmlmmj.git subcription: now moderation goes into a subdirectory "subscribe" doing this allows us to avoid manipulating memory and use fd instead as a fallback mlmmj still lookup for subscribe --- diff --git a/ChangeLog b/ChangeLog index 19aaf191..611d3375 100644 --- 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 diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index b8592c80..a30171d8 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -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); } diff --git a/tests/mlmmj.c b/tests/mlmmj.c index ff8ddb89..d228adfc 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -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)