From 87dc242dc9bcfd074574e4d8eb23e460e4ed91a3 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 15 Feb 2023 13:45:48 +0100 Subject: [PATCH] notify-sub: do not call mlmmj-send anymore --- include/subscriberfuncs.h | 13 ++-- src/mlmmj-sub.c | 2 +- src/mlmmj-unsub.c | 2 +- src/subscriberfuncs.c | 16 +++- tests/mlmmj.c | 149 +++++++++++++------------------------- tests/mlmmj_tests.c | 71 +++++++++--------- tests/mlmmj_tests.h | 2 +- 7 files changed, 107 insertions(+), 148 deletions(-) diff --git a/include/subscriberfuncs.h b/include/subscriberfuncs.h index 0489430d..d608b0b8 100644 --- a/include/subscriberfuncs.h +++ b/include/subscriberfuncs.h @@ -1,6 +1,6 @@ -/* Copyright (C) 2003 Mads Martin Joergensen - * - * $Id$ +/* + * Copyright (C) 2003 Mads Martin Joergensen + * Copyright (C) 2023 Baptiste Daroussin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -21,8 +21,7 @@ * IN THE SOFTWARE. */ -#ifndef SUBSCRIBERFUNC_H -#define SUBSCRIBERFUNC_H +#pragma once #include @@ -30,7 +29,5 @@ bool find_subscriber(int fd, const char *address); int is_subbed_in(int fd, const char *subddirname, const char *address); enum subtype is_subbed(int listfd, const char *address, bool both); char *get_subcookie_content(int listfd, bool unsub, const char *param); -void notify_sub(struct ml *ml, const char *subaddr, - const char *mlmmjsend, enum subtype typesub, +void notify_sub(struct ml *ml, const char *subaddr, enum subtype typesub, enum subreason reasonsub, bool sub); -#endif /* SUBSCRIBERFUNC_H */ diff --git a/src/mlmmj-sub.c b/src/mlmmj-sub.c index 523d3df2..cbc0eb91 100644 --- a/src/mlmmj-sub.c +++ b/src/mlmmj-sub.c @@ -674,7 +674,7 @@ int main(int argc, char **argv) /* Notify list owner about subscription */ if (notifysub) - notify_sub(&ml, address, mlmmjsend, typesub, reasonsub, true); + notify_sub(&ml, address, typesub, reasonsub, true); free(address); free(mlmmjsend); diff --git a/src/mlmmj-unsub.c b/src/mlmmj-unsub.c index b407e153..94f80b0b 100644 --- a/src/mlmmj-unsub.c +++ b/src/mlmmj-unsub.c @@ -372,7 +372,7 @@ int main(int argc, char **argv) /* Notify list owner about subscription */ if (notifysub) - notify_sub(&ml, address, mlmmjsend, typesub, reasonsub, false); + notify_sub(&ml, address, typesub, reasonsub, false); free(address); free(mlmmjsend); diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index e8c89c2c..23b9fcef 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -34,6 +34,7 @@ #include "utils.h" #include "prepstdreply.h" #include "xmalloc.h" +#include "send_mail.h" char *subtype_strs[] = { "normal", @@ -165,15 +166,16 @@ get_subcookie_content(int listfd, bool unsub, const char *param) return (NULL); } -void notify_sub(struct ml *ml, const char *subaddr, - const char *mlmmjsend, enum subtype typesub, +void notify_sub(struct ml *ml, const char *subaddr, enum subtype typesub, enum subreason reasonsub, bool sub) { char *fromaddr, *tostr; text *txt; char *queuefilename = NULL; const char *listtext = NULL; + struct mail mail; + memset(&mail, 0, sizeof(mail)); gen_addr(fromaddr, ml, "bounces-help"); gen_addr(tostr, ml, "owner"); @@ -205,7 +207,13 @@ void notify_sub(struct ml *ml, const char *subaddr, MY_ASSERT(queuefilename); close_text(txt); - exec_or_die(mlmmjsend, "-l", "1", "-L", ml->dir, "-T", tostr, - "-F", fromaddr, "-m", queuefilename, NULL); + mail.to = tostr; + mail.from = fromaddr; + mail.fp = fopen(queuefilename, "r"); + if (!send_single_mail(&mail, ml, false)) { + save_queue(queuefilename, &mail); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); } diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 6a5921d8..1d34cbcd 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -1780,9 +1780,8 @@ ATF_TC_BODY(list_subs, tc) ATF_TC_BODY(notify_sub, tc) { - int fd; - char *dir, *content; - const char *path, *pattern; + char *dir; + const char *path; init_ml(true); struct ml ml; ml_init(&ml); @@ -1793,22 +1792,19 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_get_config_var(tc, "srcdir")); symlink(dir, "list/text"); - + atf_utils_create_file("list/control/smtpport", "25678"); + atf_utils_create_file("list/control/smtphelo", "heloname"); + int smtppipe[2]; + ATF_REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, smtppipe) >= 0); + pid_t p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); pid_t p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_NORMAL, - SUB_ADMIN, true); + notify_sub(&ml, "test@plop", SUB_NORMAL, SUB_ADMIN, true); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - ATF_REQUIRE(path != NULL); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); + path = "mailout.txt"; if (!atf_utils_grep_file(".*The address has been subscribed to the normal.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -1819,20 +1815,14 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_DIGEST, - SUB_ADMIN, true); + notify_sub(&ml, "test@plop", SUB_DIGEST, SUB_ADMIN, true); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been subscribed to the digest.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -1842,20 +1832,14 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_NOMAIL, - SUB_CONFIRM, true); + notify_sub(&ml, "test@plop", SUB_NOMAIL, SUB_CONFIRM, true); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been subscribed to the no-mail.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -1865,20 +1849,14 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_BOTH, - SUB_REQUEST, true); + notify_sub(&ml, "test@plop", SUB_BOTH, SUB_REQUEST, true); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been subscribed to the version.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -1888,21 +1866,14 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_NORMAL, - SUB_ADMIN, false); + notify_sub(&ml, "test@plop", SUB_NORMAL, SUB_ADMIN, false); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - ATF_REQUIRE(path != NULL); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been unsubscribed from the list.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -1913,20 +1884,14 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_DIGEST, - SUB_ADMIN, false); + notify_sub(&ml, "test@plop", SUB_DIGEST, SUB_ADMIN, false); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been unsubscribed from the list.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -1936,43 +1901,31 @@ ATF_TC_BODY(notify_sub, tc) atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_NOMAIL, - SUB_CONFIRM, false); + notify_sub(&ml, "test@plop", SUB_NOMAIL, SUB_CONFIRM, false); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been unsubscribed from the list.*", path)) { atf_utils_cat_file(path, ""); - atf_tc_fail("invalid file"); + atf_tc_fail("invalid file unsubscribe nomail confirm"); } if (!atf_utils_grep_file(".*request to unsubscribe was confirmed.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); } + p2 = single_mail_reception(smtppipe[1]); + atf_utils_readline(smtppipe[0]); p = atf_utils_fork(); if (p == 0) { - notify_sub(&ml, "test@plop", "/bin/echo", SUB_BOTH, - SUB_REQUEST, false); + notify_sub(&ml, "test@plop", SUB_BOTH, SUB_REQUEST, false); } - atf_utils_wait(p, 0, "save:plop.txt", ""); - fd = open("plop.txt", O_RDONLY); - content = atf_utils_readline(fd); - close(fd); - pattern = "-l 1 -L list -T test+owner@test -F test+bounces-help@test -m list/queue/"; - if (strncmp(content, pattern, strlen(pattern)) != 0) - atf_tc_fail("Invalid command: '%s'", content); - path = strrchr(content, ' '); - path++; + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); if (!atf_utils_grep_file(".*The address has been unsubscribed from the list.*", path)) { atf_utils_cat_file(path, ""); atf_tc_fail("invalid file"); @@ -2129,11 +2082,7 @@ ATF_TC_BODY(send_single_mail, tc) atf_utils_create_file("list/control/smtpport", "25678"); int smtppipe[2]; ATF_REQUIRE(socketpair(AF_UNIX, SOCK_STREAM, 0, smtppipe) >= 0); - pid_t p = atf_utils_fork(); - if (p == 0) { - single_mail_reception(smtppipe[1]); - exit(0); - } + pid_t p = single_mail_reception(smtppipe[1]); close(smtppipe[1]); atf_utils_readline(smtppipe[0]); atf_utils_create_file("list/control/smtphelo", "heloname"); diff --git a/tests/mlmmj_tests.c b/tests/mlmmj_tests.c index 89130197..5bef57eb 100644 --- a/tests/mlmmj_tests.c +++ b/tests/mlmmj_tests.c @@ -107,42 +107,47 @@ read_print_reply(int fd, const char **replies, size_t nreplies) } } -void +pid_t single_mail_reception(int fd) { - int s = fakesmtp(fd); - int c; - char *r; - struct sockaddr_in cl; - socklen_t clsize = 0; - c = accept(s, (struct sockaddr *) &cl, &clsize); - if (c == -1) - exit(5); - dprintf(c, "220 me fake smtp\n"); + pid_t p = atf_utils_fork(); + if (p == 0) { + int s = fakesmtp(fd); + int c; + char *r; + struct sockaddr_in cl; + socklen_t clsize = 0; + c = accept(s, (struct sockaddr *) &cl, &clsize); + if (c == -1) + exit(5); + dprintf(c, "220 me fake smtp\n"); - const char *replies[] = { - "250-hostname.net\n" - "250-PIPELINEING\n" - "250-SIZE 20480000\n" - "250-ETRN\n" - "250-STARTTLS\n" - "250-ENHANCEDSTATUSCODES\n" - "250-8BITMIME\n" - "250-DSN\n" - "250-SMTPUTF8\n" - "250 CHUNKING\n", - "250 2.1.0 OK\n", - "250 2.1.0 OK\n", - "350 2.1.0 OK\n", - }; - read_print_reply(c, replies, NELEM(replies)); - while ((r = atf_utils_readline(c)) != NULL) { - printf("%s\n", r); - if (strcmp(r, ".\r") == 0) { - dprintf(c, "250 2.1.0 OK\n"); - break; + const char *replies[] = { + "250-hostname.net\n" + "250-PIPELINEING\n" + "250-SIZE 20480000\n" + "250-ETRN\n" + "250-STARTTLS\n" + "250-ENHANCEDSTATUSCODES\n" + "250-8BITMIME\n" + "250-DSN\n" + "250-SMTPUTF8\n" + "250 CHUNKING\n", + "250 2.1.0 OK\n", + "250 2.1.0 OK\n", + "350 2.1.0 OK\n", + }; + read_print_reply(c, replies, NELEM(replies)); + while ((r = atf_utils_readline(c)) != NULL) { + printf("%s\n", r); + if (strcmp(r, ".\r") == 0) { + dprintf(c, "250 2.1.0 OK\n"); + break; + } } + const char *rep = "221 2.0.0 bye\n"; + read_print_reply(c, &rep, 1); + exit(0); } - const char *rep = "221 2.0.0 bye\n"; - read_print_reply(c, &rep, 1); + return (p); } diff --git a/tests/mlmmj_tests.h b/tests/mlmmj_tests.h index f6431d7e..ced30cc4 100644 --- a/tests/mlmmj_tests.h +++ b/tests/mlmmj_tests.h @@ -32,4 +32,4 @@ void init_ml(bool complete); int fakesmtp(int pipe); void read_print_reply(int fd, const char **replies, size_t nreplies); -void single_mail_reception(int fd); +pid_t single_mail_reception(int fd); -- 2.47.3