]> git.ipfire.org Git - people/ms/dma.git/blobdiff - spool.c
Merge pull request #34 from mtremer/better-authentication
[people/ms/dma.git] / spool.c
diff --git a/spool.c b/spool.c
index 9a74620220ce7364ca5a2845427a636c14569102..e9c9c4355ab45e62c26b0582aa81228c9653fa32 100644 (file)
--- a/spool.c
+++ b/spool.c
@@ -1,8 +1,9 @@
 /*
+ * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
  *
  * This code is derived from software contributed to The DragonFly Project
- * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
+ * by Simon Schubert <2@0x2c.org>.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SUCH DAMAGE.
  */
 
+#include "dfcompat.h"
+
+#include <sys/file.h>
 #include <sys/stat.h>
 
+#include <ctype.h>
 #include <dirent.h>
 #include <err.h>
 #include <errno.h>
  * Spool file format:
  *
  * 'Q'id files (queue):
- *   id envelope-to
+ *   Organized like an RFC822 header, field: value.  Ignores unknown fields.
+ *   ID: id
+ *   Sender: envelope-from
+ *   Recipient: envelope-to
  *
  * 'M'id files (data):
- *   envelope-from
  *   mail data
  *
  * Each queue file needs to have a corresponding data file.
  */
 
 int
-newspoolf(struct queue *queue, const char *sender)
+newspoolf(struct queue *queue)
 {
        char fn[PATH_MAX+1];
        struct stat st;
        struct stritem *t;
-       struct qitem *it;
-       off_t hdrlen;
        int fd;
 
-       if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
+       if (snprintf(fn, sizeof(fn), "%s/%s", config.spooldir, "tmp_XXXXXXXXXX") <= 0)
                return (-1);
 
        fd = mkstemp(fn);
        if (fd < 0)
                return (-1);
+       /* XXX group rights */
+       if (fchmod(fd, 0660) < 0)
+               goto fail;
        if (flock(fd, LOCK_EX) == -1)
                goto fail;
        queue->tmpf = strdup(fn);
@@ -88,23 +96,13 @@ newspoolf(struct queue *queue, const char *sender)
         */
        if (fstat(fd, &st) != 0)
                goto fail;
-       if (asprintf(&queue->id, "%"PRIxMAX, st.st_ino) < 0)
+       if (asprintf(&queue->id, "%"PRIxMAX, (uintmax_t)st.st_ino) < 0)
                goto fail;
 
        queue->mailf = fdopen(fd, "r+");
        if (queue->mailf == NULL)
                goto fail;
 
-       if (fprintf(queue->mailf, "%s\n", sender) < 0)
-               goto fail;
-
-       hdrlen = ftello(queue->mailf);
-
-       LIST_FOREACH(it, &queue->queue, next) {
-               it->mailf = queue->mailf;
-               it->hdrlen = hdrlen;
-       }
-
        t = malloc(sizeof(*t));
        if (t != NULL) {
                t->str = queue->tmpf;
@@ -120,47 +118,141 @@ fail:
        return (-1);
 }
 
+static int
+writequeuef(struct qitem *it)
+{
+       int error;
+       int queuefd;
+
+       queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0660);
+       if (queuefd == -1)
+               return (-1);
+       if (fchmod(queuefd, 0660) < 0)
+               return (-1);
+       it->queuef = fdopen(queuefd, "w+");
+       if (it->queuef == NULL)
+               return (-1);
+
+       error = fprintf(it->queuef,
+                       "ID: %s\n"
+                       "Sender: %s\n"
+                       "Recipient: %s\n",
+                        it->queueid,
+                        it->sender,
+                        it->addr);
+
+       if (error <= 0)
+               return (-1);
+
+       if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
+               return (-1);
+
+       return (0);
+}
+
+static struct qitem *
+readqueuef(struct queue *queue, char *queuefn)
+{
+       char line[1000];
+       struct queue itmqueue;
+       FILE *queuef = NULL;
+       char *s;
+       char *queueid = NULL, *sender = NULL, *addr = NULL;
+       struct qitem *it = NULL;
+
+       bzero(&itmqueue, sizeof(itmqueue));
+       LIST_INIT(&itmqueue.queue);
+
+       queuef = fopen(queuefn, "r");
+       if (queuef == NULL)
+               goto out;
+
+       while (!feof(queuef)) {
+               if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
+                       break;
+               line[strlen(line) - 1] = 0;     /* chop newline */
+
+               s = strchr(line, ':');
+               if (s == NULL)
+                       goto malformed;
+               *s = 0;
+
+               s++;
+               while (isspace(*s))
+                       s++;
+
+               s = strdup(s);
+               if (s == NULL)
+                       goto malformed;
+
+               if (strcmp(line, "ID") == 0) {
+                       queueid = s;
+               } else if (strcmp(line, "Sender") == 0) {
+                       sender = s;
+               } else if (strcmp(line, "Recipient") == 0) {
+                       addr = s;
+               } else {
+                       syslog(LOG_DEBUG, "ignoring unknown queue info `%s' in `%s'",
+                              line, queuefn);
+                       free(s);
+               }
+       }
+
+       if (queueid == NULL || sender == NULL || addr == NULL ||
+           *queueid == 0 || *addr == 0) {
+malformed:
+               errno = EINVAL;
+               syslog(LOG_ERR, "malformed queue file `%s'", queuefn);
+               goto out;
+       }
+
+       if (add_recp(&itmqueue, addr, 0) != 0)
+               goto out;
+
+       it = LIST_FIRST(&itmqueue.queue);
+       it->sender = sender; sender = NULL;
+       it->queueid = queueid; queueid = NULL;
+       it->queuefn = queuefn; queuefn = NULL;
+       LIST_INSERT_HEAD(&queue->queue, it, next);
+
+out:
+       if (sender != NULL)
+               free(sender);
+       if (queueid != NULL)
+               free(queueid);
+       if (addr != NULL)
+               free(addr);
+       if (queuef != NULL)
+               fclose(queuef);
+
+       return (it);
+}
+
 int
-linkspool(struct queue *queue, const char *sender)
+linkspool(struct queue *queue)
 {
-       char line[1000];        /* by RFC2822 */
        struct stat st;
-       size_t error;
-       int queuefd;
        struct qitem *it;
 
        if (fflush(queue->mailf) != 0 || fsync(fileno(queue->mailf)) != 0)
                goto delfiles;
 
        syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
-              username, getuid(), sender);
+              username, getuid(), queue->sender);
 
        LIST_FOREACH(it, &queue->queue, next) {
                if (asprintf(&it->queueid, "%s.%"PRIxPTR, queue->id, (uintptr_t)it) <= 0)
                        goto delfiles;
-               if (asprintf(&it->queuefn, "%s/Q%s", config->spooldir, it->queueid) <= 0)
+               if (asprintf(&it->queuefn, "%s/Q%s", config.spooldir, it->queueid) <= 0)
                        goto delfiles;
-               if (asprintf(&it->mailfn, "%s/M%s", config->spooldir, it->queueid) <= 0)
+               if (asprintf(&it->mailfn, "%s/M%s", config.spooldir, it->queueid) <= 0)
                        goto delfiles;
 
                /* Neither file may not exist yet */
                if (stat(it->queuefn, &st) == 0 || stat(it->mailfn, &st) == 0)
                        goto delfiles;
 
-               error = snprintf(line, sizeof(line), "%s %s\n", it->queueid, it->addr);
-               if ((ssize_t)error < 0 || error >= sizeof(line))
-                       goto delfiles;
-
-               queuefd = open_locked(it->queuefn, O_CREAT|O_EXCL|O_RDWR, 0600);
-               if (queuefd == -1)
-                       goto delfiles;
-               it->queuef = fdopen(queuefd, "w+");
-               if (it->queuef == NULL)
-                       goto delfiles;
-
-               if (fwrite(line, strlen(line), 1, it->queuef) != 1)
-                       goto delfiles;
-               if (fflush(it->queuef) != 0 || fsync(fileno(it->queuef)) != 0)
+               if (writequeuef(it) != 0)
                        goto delfiles;
 
                if (link(queue->tmpf, it->mailfn) != 0)
@@ -186,102 +278,60 @@ delfiles:
 int
 load_queue(struct queue *queue)
 {
+       struct stat sb;
        struct qitem *it;
-       //struct queue queue, itmqueue;
-       struct queue itmqueue;
        DIR *spooldir;
        struct dirent *de;
-       char line[1000];
-       FILE *queuef;
-       FILE *mailf;
-       char *sender;
-       char *addr;
-       char *queueid;
        char *queuefn;
        char *mailfn;
-       off_t hdrlen;
 
+       bzero(queue, sizeof(*queue));
        LIST_INIT(&queue->queue);
 
-       spooldir = opendir(config->spooldir);
+       spooldir = opendir(config.spooldir);
        if (spooldir == NULL)
-               err(1, "reading queue");
+               err(EX_NOINPUT, "reading queue");
 
        while ((de = readdir(spooldir)) != NULL) {
-               sender = NULL;
-               queuef = NULL;
-               mailf = NULL;
-               queueid = NULL;
                queuefn = NULL;
-               LIST_INIT(&itmqueue.queue);
+               mailfn = NULL;
 
-               /* ignore temp files */
-               if (strncmp(de->d_name, "tmp_", 4) == 0 || de->d_type != DT_REG)
-                       continue;
+               /* ignore non-queue files */
                if (de->d_name[0] != 'Q')
                        continue;
-               if (asprintf(&queuefn, "%s/Q%s", config->spooldir, de->d_name + 1) < 0)
+               if (asprintf(&queuefn, "%s/Q%s", config.spooldir, de->d_name + 1) < 0)
                        goto fail;
-               if (asprintf(&mailfn, "%s/M%s", config->spooldir, de->d_name + 1) < 0)
+               if (asprintf(&mailfn, "%s/M%s", config.spooldir, de->d_name + 1) < 0)
                        goto fail;
 
-               mailf = fopen(mailfn, "r");
-               if (mailf == NULL)
+               /*
+                * Some file systems don't provide a de->d_type, so we have to
+                * do an explicit stat on the queue file.
+                * Move on if it turns out to be something else than a file.
+                */
+               if (stat(queuefn, &sb) != 0)
                        goto skip_item;
-               if (fgets(line, sizeof(line), mailf) == NULL || line[0] == 0)
+               if (!S_ISREG(sb.st_mode)) {
+                       errno = EINVAL;
                        goto skip_item;
-               line[strlen(line) - 1] = 0;     /* chop newline */
-               sender = strdup(line);
-               if (sender == NULL)
-                       goto skip_item;
-
-               hdrlen = ftell(mailf);
+               }
 
-               queuef = fopen(queuefn, "r");
-               if (queuef == NULL)
+               if (stat(mailfn, &sb) != 0)
                        goto skip_item;
 
-               if (fgets(line, sizeof(line), queuef) == NULL || line[0] == 0)
-                       goto skip_item;
-               line[strlen(line) - 1] = 0;
-               queueid = strdup(line);
-               if (queueid == NULL)
+               it = readqueuef(queue, queuefn);
+               if (it == NULL)
                        goto skip_item;
-               addr = strchr(queueid, ' ');
-               if (addr == NULL)
-                       goto skip_item;
-               *addr++ = 0;
 
-               if (add_recp(&itmqueue, addr, sender, 0) != 0)
-                       goto skip_item;
-
-               it = LIST_FIRST(&itmqueue.queue);
-               it->queueid = queueid;
-               it->queuefn = queuefn;
                it->mailfn = mailfn;
-               it->hdrlen = hdrlen;
-               LIST_INSERT_HEAD(&queue->queue, it, next);
-
-               if (queuef != NULL)
-                       fclose(queuef);
-               if (mailf != NULL)
-                       fclose(mailf);
                continue;
 
 skip_item:
                syslog(LOG_INFO, "could not pick up queue file: `%s'/`%s': %m", queuefn, mailfn);
-               if (sender != NULL)
-                       free(sender);
                if (queuefn != NULL)
                        free(queuefn);
                if (mailfn != NULL)
-                       free(queuefn);
-               if (queueid != NULL)
-                       free(queueid);
-               if (queuef != NULL)
-                       fclose(queuef);
-               if (mailf != NULL)
-                       fclose(mailf);
+                       free(mailfn);
        }
        closedir(spooldir);
        return (0);
@@ -303,7 +353,7 @@ delqueue(struct qitem *it)
 }
 
 int
-aquirespool(struct qitem *it)
+acquirespool(struct qitem *it)
 {
        int queuefd;
 
@@ -325,7 +375,9 @@ aquirespool(struct qitem *it)
        return (0);
 
 fail:
-       syslog(LOG_INFO, "could not aquire queue file: %m");
+       if (errno == EWOULDBLOCK)
+               return (1);
+       syslog(LOG_INFO, "could not acquire queue file: %m");
        return (-1);
 }
 
@@ -344,3 +396,46 @@ dropspool(struct queue *queue, struct qitem *keep)
                        fclose(it->mailf);
        }
 }
+
+int
+flushqueue_since(unsigned int period)
+{
+        struct stat st;
+       struct timeval now;
+        char *flushfn = NULL;
+
+       if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
+               return (0);
+       if (stat(flushfn, &st) < 0) {
+               free(flushfn);
+               return (0);
+       }
+       free(flushfn);
+       flushfn = NULL;
+       if (gettimeofday(&now, 0) != 0)
+               return (0);
+
+       /* Did the flush file get touched within the last period seconds? */
+       if (st.st_mtim.tv_sec + period >= now.tv_sec)
+               return (1);
+       else
+               return (0);
+}
+
+int
+flushqueue_signal(void)
+{
+        char *flushfn = NULL;
+       int fd;
+
+        if (asprintf(&flushfn, "%s/%s", config.spooldir, SPOOL_FLUSHFILE) < 0)
+               return (-1);
+       fd = open(flushfn, O_CREAT|O_WRONLY|O_TRUNC, 0660);
+       free(flushfn);
+       if (fd < 0) {
+               syslog(LOG_ERR, "could not open flush file: %m");
+               return (-1);
+       }
+        close(fd);
+       return (0);
+}