]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
text: use file descriptors when possible to avoid manipulating memory
authorBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 3 Nov 2021 13:43:07 +0000 (14:43 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Wed, 3 Nov 2021 13:43:07 +0000 (14:43 +0100)
include/mlmmj.h
src/mlmmj.c
src/prepstdreply.c

index 797e47a82abc219616a4905517ec986340a91850..5fb05dc954dca694c3e3948e337c8acd0d79ce65 100644 (file)
@@ -65,6 +65,10 @@ struct mlmmj_list {
        int fd;
        int controlfd;
        int textfd;
+       int defaulttextfd;
+       int entextfd;
+       const char *defaulttext;
+       const char *entext;
        char *addr;
        char *delim;
        char *name;
index f8a036deebeb069f861fcb8877f20e0e71635a11..e169b1c55f7d757ce7eb457a1de08e7f8ca7bb6f 100644 (file)
@@ -38,6 +38,10 @@ mlmmj_list_init(struct mlmmj_list *list)
        list->fd = -1;
        list->controlfd = -1;
        list->textfd = -1;
+       list->defaulttextfd = -1;
+       list->entextfd = -1;
+       list->defaulttext = DEFAULTTEXTDIR "/default";
+       list->entext = DEFAULTTEXTDIR "/en";
        list->delim = NULL;
        list->addr = NULL;
        list->fqdn = NULL;
index a020eb66c0f1702b774400323e5eacd41f5e0ee8..0e5fef6abc4f763a1f1e0f31bb0f24be1c385537 100644 (file)
@@ -31,6 +31,7 @@
 #include <fcntl.h>
 #include <string.h>
 #include <errno.h>
+#include <err.h>
 
 #include "prepstdreply.h"
 #include "controls.h"
@@ -452,9 +453,22 @@ char *substitute(const char *line, struct mlmmj_list *list, text *txt)
 }
 
 
+static bool
+open_textdirs(struct mlmmj_list *list)
+{
+       if (list->textfd == -1)
+               list->textfd = openat(list->fd, "text", O_DIRECTORY|O_CLOEXEC);
+       if (list->defaulttextfd == -1)
+               list->defaulttextfd = openat(list->fd, list->defaulttext, O_DIRECTORY|O_CLOEXEC);
+       if (list->entextfd == -1)
+               list->entextfd = openat(list->fd, list->entext, O_DIRECTORY|O_CLOEXEC);
+       if (list->textfd == -1 && list->defaulttextfd == -1 && list->entextfd == -1)
+               return (false);
+       return (true);
+}
+
 text *open_text_file(struct mlmmj_list *list, const char *filename)
 {
-       char *tmp;
        text *txt;
 
        txt = mymalloc(sizeof(text));
@@ -484,19 +498,18 @@ text *open_text_file(struct mlmmj_list *list, const char *filename)
        txt->cond = NULL;
        txt->skip = NULL;
 
-       myasprintf(&tmp, "text/%s", filename);
-       txt->src->fd = openat(list->fd, tmp, O_RDONLY);
-       myfree(tmp);
+       if (!open_textdirs(list)) {
+               warnx("Unable to open any text directory");
+               return (NULL);
+       }
+
+       txt->src->fd = openat(list->textfd, filename, O_RDONLY);
        if (txt->src->fd >= 0) return txt;
 
-       tmp = concatstr(2, DEFAULTTEXTDIR "/default/", filename);
-       txt->src->fd = open(tmp, O_RDONLY);
-       myfree(tmp);
+       txt->src->fd = openat(list->defaulttextfd, filename, O_RDONLY);
        if (txt->src->fd >= 0) return txt;
 
-       tmp = concatstr(2, DEFAULTTEXTDIR "/en/", filename);
-       txt->src->fd = open(tmp, O_RDONLY);
-       myfree(tmp);
+       txt->src->fd = openat(list->entextfd, filename, O_RDONLY);
        if (txt->src->fd >= 0) return txt;
 
        return NULL;