From b458669ca2f5cd6fb3f5e3a1f1604ee254fd30fd Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Thu, 16 Feb 2023 15:16:56 +0100 Subject: [PATCH] send_help: test, use send_mail and generalize its usage --- include/send_help.h | 3 +-- include/send_list.h | 2 +- src/listcontrol.c | 17 ++++++--------- src/mlmmj-process.c | 21 +++++-------------- src/mlmmj-sub.c | 12 ++--------- src/send_help.c | 15 +++++++++---- src/send_list.c | 10 ++++----- src/send_mail.c | 2 +- src/subscriberfuncs.c | 49 +++++++------------------------------------ tests/mlmmj.c | 45 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 84 insertions(+), 92 deletions(-) diff --git a/include/send_help.h b/include/send_help.h index d8ce9b90..deea3d80 100644 --- a/include/send_help.h +++ b/include/send_help.h @@ -23,5 +23,4 @@ #pragma once -void send_help(struct ml *ml, const char *queuefilename, - const char *emailaddr, const char *mlmmjsend); +void send_help(struct ml *ml, const char *queuefilename, const char *emailaddr); diff --git a/include/send_list.h b/include/send_list.h index e79bf10c..6051ea5f 100644 --- a/include/send_list.h +++ b/include/send_list.h @@ -25,7 +25,7 @@ #include "mlmmj.h" -void send_list(struct ml *ml, const char *emailaddr, const char *mlmmjsend); +void send_list(struct ml *ml, const char *emailaddr); struct subs_list_state; struct subs_list_state *init_subs_list(int fd, const char *dirname); void rewind_subs_list(void *state); diff --git a/src/listcontrol.c b/src/listcontrol.c index 1d72a8c0..198e8b15 100644 --- a/src/listcontrol.c +++ b/src/listcontrol.c @@ -213,8 +213,7 @@ int listcontrol(strlist *fromemails, struct ml *ml, tll_front(*fromemails), NULL); MY_ASSERT(queuefilename); close_text(txt); - send_help(ml, queuefilename, - tll_front(*fromemails), mlmmjsend); + send_help(ml, queuefilename, tll_front(*fromemails)); return -1; } log_oper(ml->fd, OPLOGFNAME, "mlmmj-sub: request for digest" @@ -249,8 +248,7 @@ int listcontrol(strlist *fromemails, struct ml *ml, tll_front(*fromemails), NULL); MY_ASSERT(queuefilename); close_text(txt); - send_help(ml, queuefilename, - tll_front(*fromemails), mlmmjsend); + send_help(ml, queuefilename, tll_front(*fromemails)); return -1; } log_oper(ml->fd, OPLOGFNAME, "mlmmj-sub: request for nomail" @@ -285,8 +283,7 @@ int listcontrol(strlist *fromemails, struct ml *ml, tll_front(*fromemails), NULL); MY_ASSERT(queuefilename); close_text(txt); - send_help(ml, queuefilename, - tll_front(*fromemails), mlmmjsend); + send_help(ml, queuefilename, tll_front(*fromemails)); return -1; } log_oper(ml->fd, OPLOGFNAME, "mlmmj-sub: request for both" @@ -603,8 +600,7 @@ permit: "$listowner$", tll_front(*fromemails), NULL); MY_ASSERT(queuefilename); close_text(txt); - send_help(ml, queuefilename, - tll_front(*fromemails), mlmmjsend); + send_help(ml, queuefilename, tll_front(*fromemails)); break; /* listname+faq@domain.tld */ @@ -626,8 +622,7 @@ permit: "$listowner$", tll_front(*fromemails), NULL); MY_ASSERT(queuefilename); close_text(txt); - send_help(ml, queuefilename, - tll_front(*fromemails), mlmmjsend); + send_help(ml, queuefilename, tll_front(*fromemails)); break; /* listname+get-INDEX@domain.tld */ @@ -697,7 +692,7 @@ permit: " Ignoring mail"); return -1; } else { - send_list(ml, owner, mlmmjsend); + send_list(ml, owner); } if (owners != NULL) tll_free_and_free(*owners, free); diff --git a/src/mlmmj-process.c b/src/mlmmj-process.c index 8d0e803e..d72ab7ea 100644 --- a/src/mlmmj-process.c +++ b/src/mlmmj-process.c @@ -53,6 +53,7 @@ #include "mlmmj-process.h" #include "utils.h" #include "xstring.h" +#include "send_help.h" enum action { ALLOW, @@ -367,7 +368,6 @@ int main(int argc, char **argv) char *randomstr = NULL, *mqueuename, *omitfilename; char *mlmmjsend, *mlmmjsub, *mlmmjunsub; char *bindir, *subjectprefix, *discardname; - char *fromaddr; text *txt; char *queuefilename, *recipextra = NULL, *owner = NULL; char *maxmailsizestr; @@ -698,7 +698,6 @@ int main(int argc, char **argv) exit(EXIT_SUCCESS); } - gen_addr(fromaddr, &ml, "bounces-help"); txt = open_text(ml.fd, "deny", "post", "maxmailsize", NULL, "maxmailsize"); MY_ASSERT(txt); @@ -715,9 +714,7 @@ int main(int argc, char **argv) unlink(mailfile); free(donemailname); free(maxmailsizestr); - exec_or_die(mlmmjsend, "-l", "1", "-L", ml.dir, "-T", - posteraddr, "-F", fromaddr, "-m", queuefilename, - NULL); + send_help(&ml, queuefilename, posteraddr); } } @@ -755,7 +752,6 @@ int main(int argc, char **argv) free(donemailname); exit(EXIT_SUCCESS); } - gen_addr(fromaddr, &ml, "bounces-help"); txt = open_text(ml.fd, "deny", "post", "tocc", NULL, "notintocc"); MY_ASSERT(txt); @@ -769,8 +765,7 @@ int main(int argc, char **argv) close_text(txt); unlink(donemailname); free(donemailname); - exec_or_die(mlmmjsend, "-l", "1", "-L", ml.dir, "-T", - posteraddr, "-F", fromaddr, "-m", queuefilename, NULL); + send_help(&ml, queuefilename, posteraddr); } access_rules = ctrlvalues(ml.ctrlfd, "access"); @@ -793,7 +788,6 @@ int main(int argc, char **argv) free(donemailname); exit(EXIT_SUCCESS); } - gen_addr(fromaddr, &ml, "bounces-help"); txt = open_text(ml.fd, "deny", "post", "access", NULL, "access"); MY_ASSERT(txt); @@ -808,9 +802,7 @@ int main(int argc, char **argv) unlink(donemailname); free(donemailname); free(randomstr); - exec_or_die(mlmmjsend, "-l", "1", "-L", ml.dir, "-T", - posteraddr, "-F", fromaddr, "-m", queuefilename, - NULL); + send_help(&ml, queuefilename, posteraddr); } else if (accret == MODERATE) { moderated = 1; modreason = ACCESS; @@ -891,7 +883,6 @@ int main(int argc, char **argv) free(donemailname); exit(EXIT_SUCCESS); } - gen_addr(fromaddr, &ml, "bounces-help"); if (subonlypost) { txt = open_text(ml.fd, "deny", "post", "subonlypost", NULL, "subonlypost"); @@ -910,9 +901,7 @@ int main(int argc, char **argv) close_text(txt); unlink(donemailname); free(donemailname); - exec_or_die(mlmmjsend, "-L", ml.dir, "-l", "1", - "-T", testaddr, "-F", fromaddr, "-m", - queuefilename, NULL); + send_help(&ml, queuefilename, testaddr); } } } diff --git a/src/mlmmj-sub.c b/src/mlmmj-sub.c index 5c8bd914..c7943c18 100644 --- a/src/mlmmj-sub.c +++ b/src/mlmmj-sub.c @@ -48,6 +48,7 @@ #include "ctrlvalues.h" #include "chomp.h" #include "utils.h" +#include "send_help.h" static void moderate_sub(struct ml *ml, const char *subaddr, const char *mlmmjsend, enum subtype typesub, @@ -186,8 +187,6 @@ static void moderate_sub(struct ml *ml, const char *subaddr, /* send mail to requester that the list is submod'ed */ - gen_addr(from, ml, "bounces-help"); - txt = open_text(ml->fd, "wait", "sub", subreason_strs[reasonsub], subtype_strs[typesub], @@ -203,14 +202,7 @@ static void moderate_sub(struct ml *ml, const char *subaddr, close_text(txt); finish_memory_lines(mls); - execl(mlmmjsend, mlmmjsend, - "-l", "1", - "-L", ml->dir, - "-T", subaddr, - "-F", from, - "-m", queuefilename, (char *)NULL); - log_error(LOG_ARGS, "execl() of '%s' failed", mlmmjsend); - exit(EXIT_FAILURE); + send_help(ml, queuefilename, subaddr); } void getaddrandtype(const char *listdir, const char *modstr, diff --git a/src/send_help.c b/src/send_help.c index 9e34ab9c..c115c6f9 100644 --- a/src/send_help.c +++ b/src/send_help.c @@ -25,14 +25,21 @@ #include "send_help.h" #include "utils.h" #include "xmalloc.h" +#include "send_mail.h" -void send_help(struct ml *ml, const char *queuefilename, - const char *emailaddr, const char *mlmmjsend) +void +send_help(struct ml *ml, const char *queuefilename, const char *emailaddr) { char *fromaddr; + struct mail mail; + memset(&mail, 0, sizeof(mail)); gen_addr(fromaddr, ml, "bounces-help"); - exec_or_die(mlmmjsend, "-l", "1", "-L", ml->dir, "-T", emailaddr, - "-F", fromaddr, "-m", queuefilename, NULL); + mail.to = emailaddr; + mail.from = fromaddr; + mail.fp = fopen(queuefilename, "r"); + if (!send_single_mail(&mail, ml, false)) + save_queue(queuefilename, &mail); + exit(EXIT_SUCCESS); } diff --git a/src/send_list.c b/src/send_list.c index 3f741e4f..6b39f0ea 100644 --- a/src/send_list.c +++ b/src/send_list.c @@ -39,6 +39,7 @@ #include "wrappers.h" #include "prepstdreply.h" #include "utils.h" +#include "send_help.h" struct subs_list_state { @@ -137,15 +138,13 @@ void print_subs(int fd, struct subs_list_state *s) } -void send_list(struct ml *ml, const char *emailaddr, const char *mlmmjsend) +void send_list(struct ml *ml, const char *emailaddr) { text *txt; struct subs_list_state *normalsls, *digestsls, *nomailsls; - char *queuefilename, *fromaddr; + char *queuefilename; int fd; - gen_addr(fromaddr, ml, "bounces-help"); - normalsls = init_subs_list(ml->fd, "subscribers.d"); digestsls = init_subs_list(ml->fd, "digesters.d"); nomailsls = init_subs_list(ml->fd, "nomailsubs.d"); @@ -187,6 +186,5 @@ void send_list(struct ml *ml, const char *emailaddr, const char *mlmmjsend) finish_subs_list(digestsls); finish_subs_list(nomailsls); - exec_or_die(mlmmjsend, "-l", "1", "-L", ml->dir, "-T", emailaddr, "-F", - fromaddr, "-m", queuefilename, NULL); + send_help(ml, queuefilename, emailaddr); } diff --git a/src/send_mail.c b/src/send_mail.c index 577f0c68..bbe80d20 100644 --- a/src/send_mail.c +++ b/src/send_mail.c @@ -402,7 +402,7 @@ send_single_mail(struct mail *mail, struct ml *ml, bool bounce) sockfd = newsmtp(ml); if (sockfd == -1) return (false); - if (send_mail(sockfd, mail, ml->fd, ml->ctrlfd, bounce) != 0) { + if (send_mail(sockfd, mail, ml->fd, ml->ctrlfd, bounce)) { endsmtp(&sockfd); return (false); } diff --git a/src/subscriberfuncs.c b/src/subscriberfuncs.c index 0c546ab8..d5ea8da8 100644 --- a/src/subscriberfuncs.c +++ b/src/subscriberfuncs.c @@ -38,6 +38,7 @@ #include "xmalloc.h" #include "send_mail.h" #include "strgen.h" +#include "send_help.h" char *subtype_strs[] = { "normal", @@ -173,14 +174,11 @@ void notify_sub(struct ml *ml, const char *subaddr, enum subtype typesub, enum subreason reasonsub, bool sub) { - char *fromaddr, *tostr; + char *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"); switch(typesub) { @@ -211,13 +209,7 @@ notify_sub(struct ml *ml, const char *subaddr, enum subtype typesub, MY_ASSERT(queuefilename); close_text(txt); - 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); - } + send_help(ml, queuefilename, tostr); exit(EXIT_SUCCESS); } @@ -226,12 +218,8 @@ generate_subscription(struct ml *ml, const char *subaddr, enum subtype typesub, bool sub) { text *txt; - char *queuefilename, *fromaddr; - struct mail mail; + char *queuefilename; - memset(&mail, 0, sizeof(mail)); - - gen_addr(fromaddr, ml, "bounces-help"); txt = open_text(ml->fd, "deny", sub ? "sub" : "unsub", sub ? "subbed" : "unsubbed", subtype_strs[typesub], sub ? "sub-subscribed" : "unsub-notsubscribed"); @@ -242,14 +230,7 @@ generate_subscription(struct ml *ml, const char *subaddr, MY_ASSERT(queuefilename); close_text(txt); - mail.to = subaddr; - 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); + send_help(ml, queuefilename, subaddr); } void @@ -327,10 +308,8 @@ generate_subconfirm(struct ml *ml, const char *subaddr, enum subtype typesub, mail.to = subaddr; mail.from = fromaddr; mail.fp = fopen(queuefilename, "r"); - if (!send_single_mail(&mail, ml, false)) { + if (!send_single_mail(&mail, ml, false)) save_queue(queuefilename, &mail); - exit(EXIT_FAILURE); - } exit(EXIT_SUCCESS); } @@ -339,12 +318,8 @@ confirm_sub(struct ml *ml, const char *subaddr, enum subtype typesub, enum subreason reasonsub, bool sub) { text *txt; - char *queuefilename, *fromaddr; + char *queuefilename; const char *listtext; - struct mail mail; - - memset(&mail, 0, sizeof(mail)); - gen_addr(fromaddr, ml, "bounces-help"); switch(typesub) { default: @@ -370,13 +345,5 @@ confirm_sub(struct ml *ml, const char *subaddr, enum subtype typesub, MY_ASSERT(queuefilename); close_text(txt); - mail.to = subaddr; - 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); + send_help(ml, queuefilename, subaddr); } - diff --git a/tests/mlmmj.c b/tests/mlmmj.c index d44a7714..5ac410bf 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -64,6 +64,7 @@ #include "mlmmj-process.h" #include "prepstdreply.h" #include "mlmmj_tests.h" +#include "send_help.h" ATF_TC_WITHOUT_HEAD(random_int); ATF_TC_WITHOUT_HEAD(chomp); @@ -143,6 +144,7 @@ ATF_TC_WITHOUT_HEAD(generate_subscription); ATF_TC_WITHOUT_HEAD(generate_subconfirm); ATF_TC_WITHOUT_HEAD(confirm_sub); ATF_TC_WITHOUT_HEAD(listcontrol); +ATF_TC_WITHOUT_HEAD(send_help); ATF_TC_BODY(random_int, tc) { @@ -2486,6 +2488,48 @@ ATF_TC_BODY(listcontrol, tc) ATF_REQUIRE_EQ(listcontrol(&fromemails, &ml, "subscribe", NULL, NULL, NULL, "meh"), -1); } +ATF_TC_BODY(send_help, tc) +{ + char *dir; + init_ml(true); + struct ml ml; + ml_init(&ml); + ml.dir = "list"; + ml_open(&ml, false); + rmdir("list/text"); + xasprintf(&dir, "%s/../listtexts/en", + 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"); + atf_utils_create_file("mail", "headers\n\nbody\n"); + 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) { + send_help(&ml, "mail", "test@plop"); + } + atf_utils_wait(p2, 0, "save:mailout.txt", ""); + atf_utils_wait(p, 0, "", ""); + const char *output= "EHLO heloname\r\n" + "MAIL FROM:\r\n" + "RCPT TO:\r\n" + "DATA\r\n" + "headers\r\n" + "\r\n" + "body\r\n" + "\r\n" + ".\r\n" + "QUIT\r\n"; + if (!atf_utils_compare_file("mailout.txt", output)) { + atf_utils_cat_file("mailout.txt", ""); + atf_tc_fail("invalid file"); + } +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, random_int); @@ -2566,6 +2610,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, generate_subconfirm); ATF_TP_ADD_TC(tp, confirm_sub); ATF_TP_ADD_TC(tp, listcontrol); + ATF_TP_ADD_TC(tp, send_help); return (atf_no_error()); } -- 2.47.3