From a803c7a612ae72092e04016c58a03af5121213ea Mon Sep 17 00:00:00 2001 From: Simon Schubert <2@0x2c.org> Date: Wed, 16 Nov 2011 13:32:05 +0100 Subject: [PATCH] implement the "*" catch-all alias --- dma.8 | 16 ++++++++++++++++ dma.c | 38 ++++++++++++++++++++++++++------------ dma.h | 2 ++ mail.c | 4 ++-- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/dma.8 b/dma.8 index d34b3d6..e5c7182 100644 --- a/dma.8 +++ b/dma.8 @@ -185,6 +185,22 @@ or not. .Xc Path to the local aliases file. Just stick with the default. +The aliases file is of the format +.Dl nam: dest1 dest2 ... +In this case, mails to +.Li nam +will instead be delivered to +.Li dest1 +and +.Li dest2 , +which in turn could be entries in +.Pa /etc/aliases . +The special name +.Ql * +can be used to create a catch-all alias, which gets used if no other +matching alias is found. +Use the catch-all alias only if you don't want any local mail to be +delivered. .It Ic SPOOLDIR Xo (string, default=/var/spool/dma) .Xc diff --git a/dma.c b/dma.c index 3b1aeb1..48c429c 100644 --- a/dma.c +++ b/dma.c @@ -144,12 +144,30 @@ read_aliases(void) 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; @@ -180,15 +198,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 { @@ -544,7 +558,7 @@ skipopts: 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]); } diff --git a/dma.h b/dma.h index 69c981b..a1e4b8c 100644 --- a/dma.h +++ b/dma.h @@ -187,6 +187,8 @@ int base64_encode(const void *, int, char **); int base64_decode(const char *, void *); /* dma.c */ +#define EXPAND_ADDR 1 +#define EXPAND_WILDCARD 2 int add_recp(struct queue *, const char *, int); void run_queue(struct queue *); diff --git a/mail.c b/mail.c index e0a22af..b9e13e0 100644 --- a/mail.c +++ b/mail.c @@ -57,7 +57,7 @@ bounce(struct qitem *it, const char *reason) bzero(&bounceq, sizeof(bounceq)); LIST_INIT(&bounceq.queue); bounceq.sender = ""; - if (add_recp(&bounceq, it->sender, 1) != 0) + if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0) goto fail; if (newspoolf(&bounceq) != 0) @@ -334,7 +334,7 @@ newaddr: if (addr == NULL) errlog(1, NULL); - if (add_recp(queue, addr, 1) != 0) + if (add_recp(queue, addr, EXPAND_WILDCARD) != 0) errlogx(1, "invalid recipient `%s'", addr); goto again; -- 2.39.2