]> git.ipfire.org Git - people/ms/dma.git/blobdiff - dma.c
Added recipient email addresses to log
[people/ms/dma.git] / dma.c
diff --git a/dma.c b/dma.c
index cdeb5d93992750be95e855ecb8a3bddabfd9ccfa..b87b20245583314f67b038f56db3dc1ae6c35f5a 100644 (file)
--- a/dma.c
+++ b/dma.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
@@ -95,25 +96,31 @@ sighup_handler(int signo)
 static char *
 set_from(struct queue *queue, const char *osender)
 {
+       const char *addr;
        char *sender;
 
        if (osender) {
-               sender = strdup(osender);
-               if (sender == NULL)
-                       return (NULL);
+               addr = osender;
        } else if (getenv("EMAIL") != NULL) {
-               sender = strdup(getenv("EMAIL"));
-               if (sender == NULL)
-                       return (NULL);
+               addr = getenv("EMAIL");
        } else {
-               const char *from_user = username;
+               if (config.masquerade_user)
+                       addr = config.masquerade_user;
+               else
+                       addr = username;
+       }
+
+       if (!strchr(addr, '@')) {
                const char *from_host = hostname();
 
-               if (config.masquerade_user)
-                       from_user = config.masquerade_user;
                if (config.masquerade_host)
                        from_host = config.masquerade_host;
-               if (asprintf(&sender, "%s@%s", from_user, from_host) <= 0)
+
+               if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
+                       return (NULL);
+       } else {
+               sender = strdup(addr);
+               if (sender == NULL)
                        return (NULL);
        }
 
@@ -196,7 +203,12 @@ add_recp(struct queue *queue, const char *str, int expand)
                }
        }
        LIST_INSERT_HEAD(&queue->queue, it, next);
-       if (strrchr(it->addr, '@') == NULL) {
+
+       /**
+        * Do local delivery if there is no @.
+        * Do not do local delivery when NULLCLIENT is set.
+        */
+       if (strrchr(it->addr, '@') == NULL && (config.features & NULLCLIENT) == 0) {
                it->remote = 0;
                if (expand) {
                        aliased = do_alias(queue, it->addr);
@@ -236,7 +248,7 @@ go_background(struct queue *queue)
 
        if (daemonize && daemon(0, 0) != 0) {
                syslog(LOG_ERR, "can not daemonize: %m");
-               exit(1);
+               exit(EX_OSERR);
        }
        daemonize = 0;
 
@@ -253,7 +265,7 @@ go_background(struct queue *queue)
                switch (pid) {
                case -1:
                        syslog(LOG_ERR, "can not fork: %m");
-                       exit(1);
+                       exit(EX_OSERR);
                        break;
 
                case 0:
@@ -275,11 +287,11 @@ retit:
                                break;
                        case 1:
                                if (doqueue)
-                                       exit(0);
+                                       exit(EX_OK);
                                syslog(LOG_WARNING, "could not lock queue file");
-                               exit(1);
+                               exit(EX_SOFTWARE);
                        default:
-                               exit(1);
+                               exit(EX_SOFTWARE);
                        }
                        dropspool(queue, it);
                        return (it);
@@ -295,7 +307,7 @@ retit:
        }
 
        syslog(LOG_CRIT, "reached dead code");
-       exit(1);
+       exit(EX_SOFTWARE);
 }
 
 static void
@@ -309,7 +321,7 @@ deliver(struct qitem *it)
        snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
 
 retry:
-       syslog(LOG_INFO, "trying delivery");
+       syslog(LOG_INFO, "<%s> trying delivery", it->addr);
 
        if (it->remote)
                error = deliver_remote(it);
@@ -319,13 +331,13 @@ retry:
        switch (error) {
        case 0:
                delqueue(it);
-               syslog(LOG_INFO, "delivery successful");
-               exit(0);
+               syslog(LOG_INFO, "<%s> delivery successful", it->addr);
+               exit(EX_OK);
 
        case 1:
                if (stat(it->queuefn, &st) != 0) {
                        syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
-                       exit(1);
+                       exit(EX_SOFTWARE);
                }
                if (gettimeofday(&now, NULL) == 0 &&
                    (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
@@ -427,16 +439,16 @@ main(int argc, char **argv)
                pw = getpwnam(DMA_ROOT_USER);
                if (pw == NULL) {
                        if (errno == 0)
-                               errx(1, "user '%s' not found", DMA_ROOT_USER);
+                               errx(EX_CONFIG, "user '%s' not found", DMA_ROOT_USER);
                        else
-                               err(1, "cannot drop root privileges");
+                               err(EX_OSERR, "cannot drop root privileges");
                }
 
                if (setuid(pw->pw_uid) != 0)
-                       err(1, "cannot drop root privileges");
+                       err(EX_OSERR, "cannot drop root privileges");
 
                if (geteuid() == 0 || getuid() == 0)
-                       errx(1, "cannot drop root privileges");
+                       errx(EX_OSERR, "cannot drop root privileges");
        }
 
        atexit(deltmp);
@@ -449,8 +461,15 @@ main(int argc, char **argv)
                argv++; argc--;
                showq = 1;
                if (argc != 0)
-                       errx(1, "invalid arguments");
+                       errx(EX_USAGE, "invalid arguments");
                goto skipopts;
+       } else if (strcmp(argv[0], "newaliases") == 0) {
+               logident_base = "dma";
+               setlogident(NULL);
+
+               if (read_aliases() != 0)
+                       errx(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
+               exit(EX_OK);
        }
 
        opterr = 0;
@@ -529,7 +548,7 @@ main(int argc, char **argv)
 
                default:
                        fprintf(stderr, "invalid argument: `-%c'\n", optopt);
-                       exit(1);
+                       exit(EX_USAGE);
                }
        }
        argc -= optind;
@@ -537,10 +556,10 @@ main(int argc, char **argv)
        opterr = 1;
 
        if (argc != 0 && (showq || doqueue))
-               errx(1, "sending mail and queue operations are mutually exclusive");
+               errx(EX_USAGE, "sending mail and queue operations are mutually exclusive");
 
        if (showq + doqueue > 1)
-               errx(1, "conflicting queue operations");
+               errx(EX_USAGE, "conflicting queue operations");
 
 skipopts:
        if (logident_base == NULL)
@@ -560,7 +579,7 @@ skipopts:
 
        if (showq) {
                if (load_queue(&queue) < 0)
-                       errlog(1, "can not load queue");
+                       errlog(EX_NOINPUT, "can not load queue");
                show_queue(&queue);
                return (0);
        }
@@ -568,38 +587,38 @@ skipopts:
        if (doqueue) {
                flushqueue_signal();
                if (load_queue(&queue) < 0)
-                       errlog(1, "can not load queue");
+                       errlog(EX_NOINPUT, "can not load queue");
                run_queue(&queue);
                return (0);
        }
 
        if (read_aliases() != 0)
-               errlog(1, "can not read aliases file `%s'", config.aliases);
+               errlog(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
 
        if ((sender = set_from(&queue, sender)) == NULL)
-               errlog(1, NULL);
+               errlog(EX_SOFTWARE, NULL);
 
        if (newspoolf(&queue) != 0)
-               errlog(1, "can not create temp file in `%s'", config.spooldir);
+               errlog(EX_CANTCREAT, "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], EXPAND_WILDCARD) != 0)
-                       errlogx(1, "invalid recipient `%s'", argv[i]);
+                       errlogx(EX_DATAERR, "invalid recipient `%s'", argv[i]);
        }
 
        if (LIST_EMPTY(&queue.queue) && !recp_from_header)
-               errlogx(1, "no recipients");
+               errlogx(EX_NOINPUT, "no recipients");
 
        if (readmail(&queue, nodot, recp_from_header) != 0)
-               errlog(1, "can not read mail");
+               errlog(EX_NOINPUT, "can not read mail");
 
        if (LIST_EMPTY(&queue.queue))
-               errlogx(1, "no recipients");
+               errlogx(EX_NOINPUT, "no recipients");
 
        if (linkspool(&queue) != 0)
-               errlog(1, "can not create spools");
+               errlog(EX_CANTCREAT, "can not create spools");
 
        /* From here on the mail is safe. */