]> git.ipfire.org Git - people/ms/dma.git/blobdiff - dma.c
newaliases: provide command alias
[people/ms/dma.git] / dma.c
diff --git a/dma.c b/dma.c
index a77fb8c52a915e63105358ce159bb8bc088293d1..25af9b0569f7bfe4467c7a1ecda53d1f60f2249d 100644 (file)
--- a/dma.c
+++ b/dma.c
  * SUCH DAMAGE.
  */
 
+#include "dfcompat.h"
+
 #include <sys/param.h>
+#include <sys/types.h>
 #include <sys/queue.h>
 #include <sys/stat.h>
-#include <sys/types.h>
+#include <sys/time.h>
 #include <sys/wait.h>
 
 #include <dirent.h>
 #include "dma.h"
 
 
-
 static void deliver(struct qitem *);
 
 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
-struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
-struct config *config;
-const char *username;
+char username[USERNAME_SIZE];
+uid_t useruid;
 const char *logident_base;
+char errmsg[ERRMSG_SIZE];
 
 static int daemonize = 1;
+static int doqueue = 0;
+
+struct config config = {
+       .smarthost      = NULL,
+       .port           = 25,
+       .aliases        = "/etc/aliases",
+       .spooldir       = "/var/spool/dma",
+       .authpath       = NULL,
+       .certfile       = NULL,
+       .features       = 0,
+       .mailname       = NULL,
+       .masquerade_host = NULL,
+       .masquerade_user = NULL,
+};
+
+
+static void
+sighup_handler(int signo)
+{
+       (void)signo;    /* so that gcc doesn't complain */
+}
 
 static char *
 set_from(struct queue *queue, const char *osender)
 {
-       struct virtuser *v;
+       const char *addr;
        char *sender;
 
-       if ((config->features & VIRTUAL) != 0) {
-               SLIST_FOREACH(v, &virtusers, next) {
-                       if (strcmp(v->login, username) == 0) {
-                               sender = strdup(v->address);
-                               if (sender == NULL)
-                                       return(NULL);
-                               goto out;
-                       }
-               }
+       if (osender) {
+               addr = osender;
+       } else if (getenv("EMAIL") != NULL) {
+               addr = getenv("EMAIL");
+       } else {
+               if (config.masquerade_user)
+                       addr = config.masquerade_user;
+               else
+                       addr = username;
        }
 
-       if (osender) {
-               sender = strdup(osender);
-               if (sender == NULL)
+       if (!strchr(addr, '@')) {
+               const char *from_host = hostname();
+
+               if (config.masquerade_host)
+                       from_host = config.masquerade_host;
+
+               if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
                        return (NULL);
        } else {
-               if (asprintf(&sender, "%s@%s", username, hostname()) <= 0)
+               sender = strdup(addr);
+               if (sender == NULL)
                        return (NULL);
        }
 
@@ -100,7 +128,6 @@ set_from(struct queue *queue, const char *osender)
                return (NULL);
        }
 
-out:
        queue->sender = sender;
        return (sender);
 }
@@ -108,21 +135,46 @@ out:
 static int
 read_aliases(void)
 {
-       yyin = fopen(config->aliases, "r");
-       if (yyin == NULL)
-               return (0);     /* not fatal */
+       yyin = fopen(config.aliases, "r");
+       if (yyin == NULL) {
+               /*
+                * Non-existing aliases file is not a fatal error
+                */
+               if (errno == ENOENT)
+                       return (0);
+               /* Other problems are. */
+               return (-1);
+       }
        if (yyparse())
                return (-1);    /* fatal error, probably malloc() */
        fclose(yyin);
        return (0);
 }
 
+static int
+do_alias(struct queue *queue, const char *addr)
+{
+       struct alias *al;
+        struct stritem *sit;
+       int aliased = 0;
+
+        LIST_FOREACH(al, &aliases, next) {
+                if (strcmp(al->alias, addr) != 0)
+                        continue;
+               SLIST_FOREACH(sit, &al->dests, next) {
+                       if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
+                               return (-1);
+               }
+               aliased = 1;
+        }
+
+        return (aliased);
+}
+
 int
 add_recp(struct queue *queue, const char *str, int expand)
 {
        struct qitem *it, *tit;
-       struct stritem *sit;
-       struct alias *al;
        struct passwd *pw;
        char *host;
        int aliased = 0;
@@ -153,15 +205,11 @@ add_recp(struct queue *queue, const char *str, int expand)
        if (strrchr(it->addr, '@') == NULL) {
                it->remote = 0;
                if (expand) {
-                       LIST_FOREACH(al, &aliases, next) {
-                               if (strcmp(al->alias, it->addr) != 0)
-                                       continue;
-                               SLIST_FOREACH(sit, &al->dests, next) {
-                                       if (add_recp(queue, sit->str, 1) != 0)
-                                               return (-1);
-                               }
-                               aliased = 1;
-                       }
+                       aliased = do_alias(queue, it->addr);
+                       if (!aliased && expand == EXPAND_WILDCARD)
+                               aliased = do_alias(queue, "*");
+                       if (aliased < 0)
+                               return (-1);
                        if (aliased) {
                                LIST_REMOVE(it, next);
                        } else {
@@ -199,7 +247,6 @@ go_background(struct queue *queue)
        daemonize = 0;
 
        bzero(&sa, sizeof(sa));
-       sa.sa_flags = SA_NOCLDWAIT;
        sa.sa_handler = SIG_IGN;
        sigaction(SIGCHLD, &sa, NULL);
 
@@ -223,13 +270,23 @@ go_background(struct queue *queue)
                         */
 retit:
                        /*
-                        * If necessary, aquire the queue and * mail files.
+                        * If necessary, acquire the queue and * mail files.
                         * If this fails, we probably were raced by another
-                        * process.
+                        * process.  It is okay to be raced if we're supposed
+                        * to flush the queue.
                         */
                        setlogident("%s", it->queueid);
-                       if (aquirespool(it) < 0)
+                       switch (acquirespool(it)) {
+                       case 0:
+                               break;
+                       case 1:
+                               if (doqueue)
+                                       exit(0);
+                               syslog(LOG_WARNING, "could not lock queue file");
+                               exit(1);
+                       default:
                                exit(1);
+                       }
                        dropspool(queue, it);
                        return (it);
 
@@ -251,18 +308,19 @@ static void
 deliver(struct qitem *it)
 {
        int error;
-       unsigned int backoff = MIN_RETRY;
-       const char *errmsg = "unknown bounce reason";
+       unsigned int backoff = MIN_RETRY, slept;
        struct timeval now;
        struct stat st;
 
+       snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
+
 retry:
        syslog(LOG_INFO, "trying delivery");
 
        if (it->remote)
-               error = deliver_remote(it, &errmsg);
+               error = deliver_remote(it);
        else
-               error = deliver_local(it, &errmsg);
+               error = deliver_local(it);
 
        switch (error) {
        case 0:
@@ -276,16 +334,25 @@ retry:
                        exit(1);
                }
                if (gettimeofday(&now, NULL) == 0 &&
-                   (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
-                       asprintf(__DECONST(void *, &errmsg),
+                   (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
+                       snprintf(errmsg, sizeof(errmsg),
                                 "Could not deliver for the last %d seconds. Giving up.",
                                 MAX_TIMEOUT);
                        goto bounce;
                }
-               sleep(backoff);
-               backoff *= 2;
-               if (backoff > MAX_RETRY)
-                       backoff = MAX_RETRY;
+               for (slept = 0; slept < backoff;) {
+                       slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT);
+                       if (flushqueue_since(slept)) {
+                               backoff = MIN_RETRY;
+                               goto retry;
+                       }
+               }
+               if (slept >= backoff) {
+                       /* pick the next backoff between [1.5, 2.5) times backoff */
+                       backoff = backoff + backoff / 2 + random() % backoff;
+                       if (backoff > MAX_RETRY)
+                               backoff = MAX_RETRY;
+               }
                goto retry;
 
        case -1:
@@ -346,13 +413,40 @@ show_queue(struct queue *queue)
 int
 main(int argc, char **argv)
 {
+       struct sigaction act;
        char *sender = NULL;
        struct queue queue;
        int i, ch;
-       int nodot = 0, doqueue = 0, showq = 0, queue_only = 0;
+       int nodot = 0, showq = 0, queue_only = 0;
        int recp_from_header = 0;
 
+       set_username();
+
+       /*
+        * We never run as root.  If called by root, drop permissions
+        * to the mail user.
+        */
+       if (geteuid() == 0 || getuid() == 0) {
+               struct passwd *pw;
+
+               errno = 0;
+               pw = getpwnam(DMA_ROOT_USER);
+               if (pw == NULL) {
+                       if (errno == 0)
+                               errx(1, "user '%s' not found", DMA_ROOT_USER);
+                       else
+                               err(1, "cannot drop root privileges");
+               }
+
+               if (setuid(pw->pw_uid) != 0)
+                       err(1, "cannot drop root privileges");
+
+               if (geteuid() == 0 || getuid() == 0)
+                       errx(1, "cannot drop root privileges");
+       }
+
        atexit(deltmp);
+       init_random();
 
        bzero(&queue, sizeof(queue));
        LIST_INIT(&queue.queue);
@@ -363,6 +457,13 @@ main(int argc, char **argv)
                if (argc != 0)
                        errx(1, "invalid arguments");
                goto skipopts;
+       } else if (strcmp(argv[0], "newaliases") == 0) {
+               logident_base = "dma";
+               setlogident(NULL);
+
+               if (read_aliases() != 0)
+                       errx(1, "could not parse aliases file `%s'", config.aliases);
+               exit(0);
        }
 
        opterr = 0;
@@ -411,6 +512,9 @@ main(int argc, char **argv)
                        break;
 
                case 'q':
+                       /* Don't let getopt slup up other arguments */
+                       if (optarg && *optarg == '-')
+                               optind--;
                        doqueue = 1;
                        break;
 
@@ -455,26 +559,17 @@ skipopts:
        if (logident_base == NULL)
                logident_base = "dma";
        setlogident(NULL);
-       set_username();
-
-       /* XXX fork root here */
 
-       config = calloc(1, sizeof(*config));
-       if (config == NULL)
-               errlog(1, NULL);
-
-       if (parse_conf(CONF_PATH) < 0) {
-               free(config);
-               errlog(1, "can not read config file");
-       }
+       act.sa_handler = sighup_handler;
+       act.sa_flags = 0;
+       sigemptyset(&act.sa_mask);
+       if (sigaction(SIGHUP, &act, NULL) != 0)
+               syslog(LOG_WARNING, "can not set signal handler: %m");
 
-       if (config->features & VIRTUAL)
-               if (parse_virtuser(config->virtualpath) < 0)
-                       errlog(1, "can not read virtual user file `%s'",
-                               config->virtualpath);
+       parse_conf(CONF_PATH "/dma.conf");
 
-       if (parse_authfile(config->authpath) < 0)
-               errlog(1, "can not read SMTP authentication file");
+       if (config.authpath != NULL)
+               parse_authfile(config.authpath);
 
        if (showq) {
                if (load_queue(&queue) < 0)
@@ -484,6 +579,7 @@ skipopts:
        }
 
        if (doqueue) {
+               flushqueue_signal();
                if (load_queue(&queue) < 0)
                        errlog(1, "can not load queue");
                run_queue(&queue);
@@ -491,18 +587,18 @@ skipopts:
        }
 
        if (read_aliases() != 0)
-               errlog(1, "can not read aliases file `%s'", config->aliases);
+               errlog(1, "could not parse aliases file `%s'", config.aliases);
 
        if ((sender = set_from(&queue, sender)) == NULL)
                errlog(1, NULL);
 
        if (newspoolf(&queue) != 0)
-               errlog(1, "can not create temp file");
+               errlog(1, "can not create temp file in `%s'", config.spooldir);
 
        setlogident("%s", queue.id);
 
        for (i = 0; i < argc; i++) {
-               if (add_recp(&queue, argv[i], 1) != 0)
+               if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
                        errlogx(1, "invalid recipient `%s'", argv[i]);
        }
 
@@ -520,7 +616,7 @@ skipopts:
 
        /* From here on the mail is safe. */
 
-       if (config->features & DEFER || queue_only)
+       if (config.features & DEFER || queue_only)
                return (0);
 
        run_queue(&queue);