]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-send: reduce memory manipulation
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 4 Jan 2022 21:35:40 +0000 (22:35 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 4 Jan 2022 21:35:40 +0000 (22:35 +0100)
src/mlmmj-send.c

index 5aa42a6b7fa5189e9621afcab1f0aec1bf1e6d75..6a6b4fdf1b5b610b3cb9f2b37e618a4d65d4338b 100644 (file)
@@ -339,14 +339,14 @@ int send_mail_many_fd(int sockfd, const char *from, const char *replyto,
        return 0;
 }
 
-int requeuemail(const char *listdir, const char *index, struct strlist *addrs,
+int requeuemail(struct mlmmj_list *list, const char *index, struct strlist *addrs,
                int addrcount)
 {
        int addrfd, i;
        const char *addr;
        char *dirname, *addrfilename;
-       
-       dirname = concatstr(3, listdir, "/requeue/", index);
+
+       xasprintf(&dirname, "%s/requeue/%s", list->dir, index);
        if(mkdir(dirname, 0750) < 0 && errno != EEXIST) {
                log_error(LOG_ARGS, "Could not mkdir(%s) for "
                                "requeueing. Mail cannot "
@@ -354,7 +354,7 @@ int requeuemail(const char *listdir, const char *index, struct strlist *addrs,
                free(dirname);
                return -1;
        }
-       addrfilename = concatstr(2, dirname, "/subscribers");
+       xasprintf(&addrfilename, "%s/subscribers");
        free(dirname);
        addrfd = open(addrfilename, O_WRONLY|O_CREAT|O_APPEND,
                        S_IRUSR|S_IWUSR);
@@ -411,7 +411,7 @@ int send_mail_many_list(int sockfd, const char *from, const char *replyto,
                        log_error(LOG_ARGS, "TERM signal received, "
                                                "shutting down.");
                        index = get_index_from_filename(archivefilename);
-                       status = requeuemail(list->dir, index, addrs, i);
+                       status = requeuemail(list, index, addrs, i);
                        free(index);
                        return status;
                }
@@ -429,7 +429,7 @@ int send_mail_many_list(int sockfd, const char *from, const char *replyto,
                if(res && list->addr && archivefilename) {
                        /* we failed, so save the addresses and bail */
                        index = get_index_from_filename(archivefilename);
-                       status = requeuemail(list->dir, index, addrs, i);
+                       status = requeuemail(list, index, addrs, i);
                        free(index);
                        return status;
                }
@@ -475,7 +475,7 @@ int main(int argc, char **argv)
        char *mailfilename = NULL, *subfilename = NULL, *omit = NULL;
        char *replyto = NULL, *bounceaddr = NULL, *to_addr = NULL;
        char *relayhost = NULL, *archivefilename = NULL, *tmpstr;
-       char *listctrl = NULL, *subddirname = NULL;
+       char *listctrl = NULL;
        char *mlmmjbounce = NULL, *bindir, *mailmap, *probefile, *a;
        char *body = NULL, *hdrs = NULL, *verp = NULL;
        char relay[16], *verpfrom;
@@ -490,6 +490,8 @@ int main(int argc, char **argv)
        struct mlmmj_list list;
        unsigned short smtpport = 25;
        struct sigaction sigact;
+       int subdirfd;
+       const char *dir;
 
        mlmmj_list_init(&list);
        CHECKFULLPATH(argv[0]);
@@ -672,13 +674,11 @@ int main(int argc, char **argv)
        case '5':
                break;
        case '2': /* Moderators */
-               subfilename = concatstr(2, list.dir, "/control/moderators");
-               if((subfd = open(subfilename, O_RDONLY)) < 0) {
-                       log_error(LOG_ARGS, "Could not open '%s':",
-                                           subfilename);
+               if((subfd = openat(list.fd, "control/moderators", O_RDONLY)) < 0) {
+                       log_error(LOG_ARGS, "Could not open "
+                           "'%s/control/moderators':", list.dir);
                        free(hdrs);
                        free(body);
-                       free(subfilename);
                        /* No moderators is no error. Could be the sysadmin
                         * likes to do it manually.
                         */
@@ -862,15 +862,14 @@ int main(int argc, char **argv)
                archivefilename = "digest";
                /* fall through */
        default: /* normal list mail */
-               if (!digest) {
-                       subddirname = concatstr(2, list.dir, "/subscribers.d/");
-               } else {
-                       subddirname = concatstr(2, list.dir, "/digesters.d/");
-               }
-               if((subddir = opendir(subddirname)) == NULL) {
+               dir = "digesters.d";
+               if (!digest)
+                       dir = "subscribers.d";
+               subdirfd = openat(list.fd, dir, O_DIRECTORY);
+               if((subddir = fdopendir(subdirfd)) == NULL) {
                        log_error(LOG_ARGS, "Could not opendir(%s)",
-                                           subddirname);
-                       free(subddirname);
+                                           dir);
+                       close(subdirfd);
                        free(hdrs);
                        free(body);
                        exit(EXIT_FAILURE);
@@ -930,11 +929,9 @@ int main(int argc, char **argv)
                                continue;
                        if(!strcmp(dp->d_name, ".."))
                                continue;
-                       subfilename = concatstr(2, subddirname, dp->d_name);
-                       if((subfd = open(subfilename, O_RDONLY)) < 0) {
-                               log_error(LOG_ARGS, "Could not open '%s'",
-                                                   subfilename);
-                               free(subfilename);
+                       if((subfd = openat(subdirfd, dp->d_name, O_RDONLY)) < 0) {
+                               log_error(LOG_ARGS, "Could not open '%s/%s'",
+                                   dir, dp->d_name);
                                continue;
                        }
                        do {
@@ -970,7 +967,7 @@ int main(int argc, char **argv)
                                                                hdrslen, body,
                                                                bodylen, verp);
                                                if(sendres)
-                                                       requeuemail(list.dir,
+                                                       requeuemail(&list,
                                                                strindex,
                                                                &stl, 0);
                                        } else {
@@ -996,7 +993,6 @@ int main(int argc, char **argv)
                                        stl.count = 0;
                                }
                        } while(res > 0);
-                       free(subfilename);
                        close(subfd);
 
                }
@@ -1008,7 +1004,7 @@ int main(int argc, char **argv)
                                                hdrs, hdrslen, body, bodylen,
                                                verp);
                                if(sendres)
-                                       requeuemail(list.dir, strindex, &stl,
+                                       requeuemail(&list, strindex, &stl,
                                                        0);
                        } else {
                                sendres = send_mail_many_list(sockfd, NULL,
@@ -1030,7 +1026,6 @@ int main(int argc, char **argv)
                free(stl.strs);
                free(verpfrom);
                closedir(subddir);
-               free(subddirname);
                break;
        }