]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
ctrlvalues: rework to use fd and getline()
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 4 Jan 2023 15:15:01 +0000 (16:15 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 4 Jan 2023 15:15:26 +0000 (16:15 +0100)
This simplifies the code and reduce the memory allocation

contrib/receivestrip/mlmmj-receive-strip.c
include/ctrlvalues.h
src/ctrlvalues.c
src/listcontrol.c
src/mlmmj-process.c
src/mlmmj-sub.c

index fe0ef3379899ef41222d88cdaa8da36cdd369de0..b03442ea183e6842885e4f788644ab35a8f7a5c9 100644 (file)
@@ -304,12 +304,16 @@ static int dump_mail(int infd, int outfd,char* listdir) {
        strlist *denymime;
        char* boundary=NULL;
        int deny = 0;
-       int result;
+       int result, ctrlfd;
        off_t unwantedmime_hdr_pos = 0;
+       char *dir;
+
+       xasprintf(&dir, "%s/control", listdir);
+       ctrlfd = open(dir, O_RDONLY|O_CLOEXEC);
 
        /* get list control values */
-       delmime = ctrlvalues(listdir, "mimestrip");
-       denymime = ctrlvalues(listdir, "mimedeny");
+       delmime = ctrlvalues(ctrlfd, "mimestrip");
+       denymime = ctrlvalues(ctrlfd, "mimedeny");
 
        /* read mail header */
        result = read_hdrs(infd, &hdrs,delmime,denymime,&deny,&boundary);
index 9605527672b436c6c276bad178574e412488608b..207896c1b65ffc8efe8e7f31a8a5dbe1fd321676 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
- *
- * $Id$
+/*
+ * Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
+ * Copyright (C) 2023 Baptiste Daroussin <bapt@FreeBSD.org>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -26,6 +26,6 @@
 
 #include "mlmmj.h"
 
-strlist *ctrlvalues(const char *listdir, const char *ctrlstr);
+strlist *ctrlvalues(int ctrlfd, const char *ctrlstr);
 
 #endif /* CTRLVALUES_H */
index 0f7893e99f0d35a4b033dc5f73e028cf2e08994b..f68b4ead26c4262f78ad3bc8222c0ef10b1bd28f 100644 (file)
@@ -1,6 +1,6 @@
-/* Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
- *
- * $Id$
+/*
+ * Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj.dk>
+ * Copyright (C) 2023 Baptiste Daroussin <bapt@FreeBSD.org>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
  * IN THE SOFTWARE.
  */
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdlib.h>
 #include <fcntl.h>
-#include <unistd.h>
 
 #include "xmalloc.h"
-#include "strgen.h"
 #include "ctrlvalues.h"
-#include "mygetline.h"
 #include "chomp.h"
 #include "mlmmj.h"
 
-strlist *ctrlvalues(const char *listdir, const char *ctrlstr)
+strlist *ctrlvalues(int ctrlfd, const char *ctrlstr)
 {
-       char *filename = concatstr(3, listdir, "/control/", ctrlstr);
-       char *value;
-       int ctrlfd;
+       char *line = NULL;
+       size_t linecap = 0;
+       FILE *f;
+       int fd;
        strlist *ret;
 
-       ctrlfd = open(filename, O_RDONLY);
-       free(filename);
+       fd = openat(ctrlfd, ctrlstr, O_RDONLY|O_CLOEXEC);
+       if (fd == -1 || (f = fdopen(fd, "r")) == NULL)
+               return (NULL);
 
-       if(ctrlfd < 0)
-               return NULL;
-               
        ret = xcalloc(1, sizeof(*ret));
-       while((value = mygetline(ctrlfd)) != NULL) {
-               chomp(value);
+       while (getline(&line, &linecap, f) > 0) {
+               chomp(line);
                /* Ignore empty lines */
-               if (*value == '\0')
+               if (*line == '\0')
                        continue;
-               tll_push_back(*ret, value);
+               tll_push_back(*ret, xstrdup(line));
        }
-
-       close(ctrlfd);
+       free(line);
+       fclose(f);
 
        return ret;
 }
index f48785a81c0f01b4f1331b820a53766537292373..753bb25e0ff11fbb49f7cf4793a3c1ed0527cf31 100644 (file)
@@ -770,7 +770,7 @@ permit:
                if(statctrl(ctrlfd, "nolistsubsemail"))
                        return -1;
                const char *owner = NULL;
-               owners = ctrlvalues(listdir, "owner");
+               owners = ctrlvalues(ctrlfd, "owner");
                if (owners != NULL) {
                        tll_foreach(*owners, it) {
                                if(strcasecmp(tll_front(*fromemails),
index 9dec2021c1dff3e64b42ea0c9bd73cf350e08c05..6b1e2bd17362c90d3f6f92ad87d83b3ca2a29eca 100644 (file)
@@ -554,7 +554,7 @@ int main(int argc, char **argv)
        footfd = open(footerfilename, O_RDONLY);
        free(footerfilename);
 
-       delheaders = ctrlvalues(listdir, "delheaders");
+       delheaders = ctrlvalues(ctrlfd, "delheaders");
        if(delheaders == NULL)
                delheaders = xcalloc(1, sizeof(*delheaders));
 
@@ -622,7 +622,7 @@ int main(int argc, char **argv)
                listdelim = getlistdelim(ctrlfd);
        }
        if(addrtocc || findaddress) {
-               listaddrs = ctrlvalues(listdir, "listaddress");
+               listaddrs = ctrlvalues(ctrlfd, "listaddress");
                tll_foreach(dtemails, it) {
                        tll_foreach(*listaddrs, la) {
                                if(addrmatch(la->item, it->item,
@@ -894,7 +894,7 @@ int main(int argc, char **argv)
                    posteraddr, "-F", fromaddr, "-m", queuefilename, NULL);
        }
 
-       access_rules = ctrlvalues(listdir, "access");
+       access_rules = ctrlvalues(ctrlfd, "access");
        if (access_rules != NULL) {
                enum action accret;
                /* Don't send a mail about denial to the list, but silently
@@ -964,7 +964,7 @@ int main(int argc, char **argv)
                }
        }
 
-       list_rules = ctrlvalues(listdir, "send");
+       list_rules = ctrlvalues(ctrlfd, "send");
        if (list_rules != NULL) {
                tll_foreach(*list_rules, lr) {
                        if (strcasecmp(posteraddr, lr->item) == 0) {
index beca632d2fa7ef9ae1139dbaa327ea9439d36140..5c8ae6eff619a9eaa19df26f70778e378dbe4afe 100644 (file)
@@ -110,7 +110,7 @@ static void moderate_sub(const char *listdir, const char *listaddr,
        
        free(str);
 
-       submods = ctrlvalues(listdir, "submod");
+       submods = ctrlvalues(ctrlfd, "submod");
        mods = concatstr(2, listdir, "/control/submod");
        /* check to see if there's adresses in the submod control file */
        tll_foreach(*submods, it)
@@ -121,7 +121,7 @@ static void moderate_sub(const char *listdir, const char *listaddr,
                /* free the submods struct from above */
                tll_free_and_free(*submods, free);
                free(submods);
-               submods = ctrlvalues(listdir, "owner");
+               submods = ctrlvalues(ctrlfd, "owner");
                free(mods);
                mods = concatstr(2, listdir, "/control/owner");
        }