From: Baptiste Daroussin Date: Wed, 12 Apr 2023 16:45:08 +0000 (+0200) Subject: listcontrol: stop calling mlmmj-send for +get-@ X-Git-Tag: RELEASE_1_4_0b1~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d29f59239a870ee29a98cda651f76b02d741563;p=thirdparty%2Fmlmmj.git listcontrol: stop calling mlmmj-send for +get-@ This rewrite allow to reduce memory allocation and prevent fork/exec move some functions from mlmmj-send to a library to allow reuse, Duplicate bounce_from_adr temporary to allow rewrite using struct ml --- diff --git a/include/send_mail.h b/include/send_mail.h index 8daa623a..2b7e8db2 100644 --- a/include/send_mail.h +++ b/include/send_mail.h @@ -44,3 +44,7 @@ int do_bouncemail(int listfd, int ctrlfd, const char *from); bool send_single_mail(struct mail *mail, struct ml *ml, bool bounce); void save_queue(const char *queuefilename, struct mail *mail); bool requeuemail(int listfd, int index, strlist *addrs, const char *addr); +char *bounce_from_adr(const char *recipient, const char *listadr, const char *listdelim, const char *mailfilename, int ctrlfd); +char *get_bounce_from_adr(const char *recipient, struct ml *ml, const char *mailfilename, int index); +int get_index_from_filename(const char *filename); + diff --git a/src/listcontrol.c b/src/listcontrol.c index d2e1ee8d..405093c3 100644 --- a/src/listcontrol.c +++ b/src/listcontrol.c @@ -21,13 +21,15 @@ * IN THE SOFTWARE. */ +#include +#include + #include #include #include #include #include #include -#include #include #include "xmalloc.h" @@ -36,6 +38,7 @@ #include "prepstdreply.h" #include "send_help.h" #include "send_list.h" +#include "send_mail.h" #include "log_error.h" #include "statctrl.h" #include "mygetline.h" @@ -627,16 +630,22 @@ permit: " non-digits in index. Ignoring mail"); return -1; } - xasprintf(&archivefilename, "%s/archive/%s", ml->dir, param); - if(access(archivefilename, R_OK) < 0) { + int index = strtoim(param, 0, INT_MAX, NULL); + xasprintf(&archivefilename, "archive/%d", index); + int fd = openat(ml->fd, archivefilename, O_RDONLY|O_CLOEXEC); + if (fd == -1) { log_error(LOG_ARGS, "Unable to open archive file"); + free(archivefilename); return -1; } + struct mail mail = { 0 }; + mail.to = tll_front(*fromemails); + mail.from = get_bounce_from_adr(mail.to, ml, archivefilename, index); + mail.fp = fdopen(fd, "r"); log_oper(ml->fd, OPLOGFNAME, "%s got archive/%s", - tll_front(*fromemails), archivefilename); - exec_or_die(mlmmjsend, "-T", tll_front(*fromemails), "-L", - ml->dir, "-l", "6", "-m", archivefilename, "-a", "-D", - NULL); + tll_front(*fromemails), param); + if (!send_single_mail(&mail, ml, false)) + return (-1); break; /* listname+list@domain.tld */ diff --git a/src/mlmmj-send.c b/src/mlmmj-send.c index e2dfcfea..24f397ba 100644 --- a/src/mlmmj-send.c +++ b/src/mlmmj-send.c @@ -70,90 +70,6 @@ void catch_sig_term(int sig __unused) gotsigterm = 1; } -int -get_index_from_filename(const char *filename) -{ - char *myfilename, *indexstr; - int ret; - size_t len; - - myfilename = xstrdup(filename); - len = strlen(myfilename); - if (len > 9 && (strcmp(myfilename + len - 9, "/mailfile") == 0)) { - myfilename[len - 9] = '\0'; - } - - indexstr = strrchr(myfilename, '/'); - if (indexstr) { - indexstr++; /* skip the slash */ - } else { - indexstr = myfilename; - } - - ret = strtoim(indexstr, 0, INT_MAX, NULL); - free(myfilename); - - return ret; -} - -char *bounce_from_adr(const char *recipient, const char *listadr, - const char *listdelim, const char *mailfilename, - int ctrlfd) -{ - char *bounceaddr, *myrecipient, *mylistadr; - char *listdomain, *a = NULL, *mymailfilename; - char *staticbounceaddr, *staticbounceaddr_localpart; - char *staticbounceaddr_domain; - int index; - - mymailfilename = xstrdup(mailfilename); - - index = get_index_from_filename(mymailfilename); - - myrecipient = xstrdup(recipient); - a = strchr(myrecipient, '@'); - if (a) - *a = '='; - - mylistadr = xstrdup(listadr); - listdomain = strchr(mylistadr, '@'); - if (!listdomain) { - free(mymailfilename); - free(myrecipient); - free(mylistadr); - return NULL; - } - *listdomain++ = '\0'; - - staticbounceaddr = ctrlvalue(ctrlfd, "staticbounceaddr"); - if (staticbounceaddr) { - staticbounceaddr_localpart = genlistname(staticbounceaddr); - staticbounceaddr_domain = genlistfqdn(staticbounceaddr); - } else { - staticbounceaddr_localpart = NULL; - staticbounceaddr_domain = NULL; - } - - if (staticbounceaddr) { - xasprintf(&bounceaddr, "%s%s%s-bounces-%d-%s@%s", - staticbounceaddr_localpart, listdelim, mylistadr, - index, myrecipient, staticbounceaddr_domain); - - free(staticbounceaddr); - free(staticbounceaddr_localpart); - free(staticbounceaddr_domain); - } else { - xasprintf(&bounceaddr, "%s%sbounces-%d-%s@%s", mylistadr, listdelim, - index, myrecipient, listdomain); - } - - free(myrecipient); - free(mylistadr); - free(mymailfilename); - - return bounceaddr; -} - int send_mail_verp(int sockfd, strlist *addrs, struct mail *mail, const char *verpextra) { diff --git a/src/send_mail.c b/src/send_mail.c index feac8d16..b5a6f0b9 100644 --- a/src/send_mail.c +++ b/src/send_mail.c @@ -38,6 +38,7 @@ #include "tllist.h" #include "xmalloc.h" #include "ctrlvalue.h" +#include "utils.h" int initsmtp(int *sockfd, const char *relayhost, unsigned short port, const char *heloname) @@ -453,3 +454,132 @@ requeuemail(int listfd, int index, strlist *addrs, const char *addr) return (true); } +char * +get_bounce_from_adr(const char *recipient, struct ml *ml, + const char *mailfilename, int index) +{ + char *bounceaddr, *myrecipient; + char *a = NULL, *mymailfilename; + char *staticbounceaddr, *staticbounceaddr_localpart; + char *staticbounceaddr_domain; + + mymailfilename = xstrdup(mailfilename); + + myrecipient = xstrdup(recipient); + a = strchr(myrecipient, '@'); + if (a) + *a = '='; + + staticbounceaddr = ctrlvalue(ml->ctrlfd, "staticbounceaddr"); + if (staticbounceaddr) { + staticbounceaddr_localpart = genlistname(staticbounceaddr); + staticbounceaddr_domain = genlistfqdn(staticbounceaddr); + } else { + staticbounceaddr_localpart = NULL; + staticbounceaddr_domain = NULL; + } + + if (staticbounceaddr) { + xasprintf(&bounceaddr, "%s%s%s-bounces-%d-%s@%s", + staticbounceaddr_localpart, ml->delim, ml->name, + index, myrecipient, staticbounceaddr_domain); + + free(staticbounceaddr); + free(staticbounceaddr_localpart); + free(staticbounceaddr_domain); + } else { + xasprintf(&bounceaddr, "%s%sbounces-%d-%s@%s", ml->name, + ml->delim, index, myrecipient, ml->fqdn); + } + + free(myrecipient); + free(mymailfilename); + + return bounceaddr; +} + +char * +bounce_from_adr(const char *recipient, const char *listadr, + const char *listdelim, const char *mailfilename, + int ctrlfd) +{ + char *bounceaddr, *myrecipient, *mylistadr; + char *listdomain, *a = NULL, *mymailfilename; + char *staticbounceaddr, *staticbounceaddr_localpart; + char *staticbounceaddr_domain; + int index; + + mymailfilename = xstrdup(mailfilename); + + index = get_index_from_filename(mymailfilename); + + myrecipient = xstrdup(recipient); + a = strchr(myrecipient, '@'); + if (a) + *a = '='; + + mylistadr = xstrdup(listadr); + listdomain = strchr(mylistadr, '@'); + if (!listdomain) { + free(mymailfilename); + free(myrecipient); + free(mylistadr); + return NULL; + } + *listdomain++ = '\0'; + + staticbounceaddr = ctrlvalue(ctrlfd, "staticbounceaddr"); + if (staticbounceaddr) { + staticbounceaddr_localpart = genlistname(staticbounceaddr); + staticbounceaddr_domain = genlistfqdn(staticbounceaddr); + } else { + staticbounceaddr_localpart = NULL; + staticbounceaddr_domain = NULL; + } + + if (staticbounceaddr) { + xasprintf(&bounceaddr, "%s%s%s-bounces-%d-%s@%s", + staticbounceaddr_localpart, listdelim, mylistadr, + index, myrecipient, staticbounceaddr_domain); + + free(staticbounceaddr); + free(staticbounceaddr_localpart); + free(staticbounceaddr_domain); + } else { + xasprintf(&bounceaddr, "%s%sbounces-%d-%s@%s", mylistadr, listdelim, + index, myrecipient, listdomain); + } + + free(myrecipient); + free(mylistadr); + free(mymailfilename); + + return bounceaddr; +} + +int +get_index_from_filename(const char *filename) +{ + char *myfilename, *indexstr; + int ret; + size_t len; + + myfilename = xstrdup(filename); + len = strlen(myfilename); + if (len > 9 && (strcmp(myfilename + len - 9, "/mailfile") == 0)) { + myfilename[len - 9] = '\0'; + } + + indexstr = strrchr(myfilename, '/'); + if (indexstr) { + indexstr++; /* skip the slash */ + } else { + indexstr = myfilename; + } + + ret = strtoim(indexstr, 0, INT_MAX, NULL); + free(myfilename); + + return ret; +} +