]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
listcontrol: stop calling mlmmj-send for +get-<index>@
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 12 Apr 2023 16:45:08 +0000 (18:45 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Thu, 13 Apr 2023 07:09:00 +0000 (09:09 +0200)
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

include/send_mail.h
src/listcontrol.c
src/mlmmj-send.c
src/send_mail.c

index 8daa623a7f89e250f7284aa51613099bdbf32a24..2b7e8db22b2730c78423ee9d0f4622c7b68069bc 100644 (file)
@@ -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);
+
index d2e1ee8dd27e8961b93c2c21f7c8a4b2f1022e15..405093c3d6b184291d0a23e6a71c23d98d5376ee 100644 (file)
  * IN THE SOFTWARE.
  */
 
+#include <sys/param.h>
+#include <sys/types.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <sys/types.h>
 #include <ctype.h>
 
 #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 */
index e2dfcfea4dab6a6832aeca86db59d520915e8dc0..24f397baa3db89307b79d7e97d73ef6a3b8b3579 100644 (file)
@@ -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)
 {
index feac8d16d1848315e1449d71fca6811068cf340a..b5a6f0b997a31c6229d9f17005bf282649fbdbe0 100644 (file)
@@ -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;
+}
+