]> git.ipfire.org Git - people/ms/dma.git/blame - dma.c
implement the "*" catch-all alias
[people/ms/dma.git] / dma.c
CommitLineData
86e4d161
MS
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
86e4d161
MS
33 */
34
9b921550
SS
35#include "dfcompat.h"
36
86e4d161 37#include <sys/param.h>
4dd56cbc 38#include <sys/types.h>
86e4d161
MS
39#include <sys/queue.h>
40#include <sys/stat.h>
9b921550 41#include <sys/time.h>
c507d897 42#include <sys/wait.h>
86e4d161 43
86e4d161
MS
44#include <dirent.h>
45#include <err.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <inttypes.h>
86e4d161
MS
49#include <paths.h>
50#include <pwd.h>
51#include <signal.h>
52#include <stdarg.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <syslog.h>
57#include <unistd.h>
58
59#include "dma.h"
60
61
e0a95d28 62static void deliver(struct qitem *);
86e4d161
MS
63
64struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
fd1de51a 65struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
86e4d161 66struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
249ee966 67char username[USERNAME_SIZE];
75fdf182 68uid_t useruid;
de13a881 69const char *logident_base;
249ee966 70char errmsg[ERRMSG_SIZE];
86e4d161 71
87bdbc01 72static int daemonize = 1;
dd561a09 73
a0c4afa6
SS
74struct config config = {
75 .smarthost = NULL,
76 .port = 25,
db93ed9b 77 .aliases = "/etc/aliases",
a0c4afa6 78 .spooldir = "/var/spool/dma",
a0c4afa6
SS
79 .authpath = NULL,
80 .certfile = NULL,
81 .features = 0,
82 .mailname = NULL,
5bc72de8
SS
83 .masquerade_host = NULL,
84 .masquerade_user = NULL,
a0c4afa6
SS
85};
86
87
247523d6
SS
88static void
89sighup_handler(int signo)
90{
91 (void)signo; /* so that gcc doesn't complain */
92}
93
86e4d161 94static char *
057afad5 95set_from(struct queue *queue, const char *osender)
86e4d161 96{
86e4d161
MS
97 char *sender;
98
86e4d161
MS
99 if (osender) {
100 sender = strdup(osender);
101 if (sender == NULL)
e0a95d28 102 return (NULL);
09a2b07e
SS
103 } else if (getenv("EMAIL") != NULL) {
104 sender = strdup(getenv("EMAIL"));
105 if (sender == NULL)
106 return (NULL);
86e4d161 107 } else {
5bc72de8
SS
108 const char *from_user = username;
109 const char *from_host = hostname();
110
111 if (config.masquerade_user)
112 from_user = config.masquerade_user;
113 if (config.masquerade_host)
114 from_host = config.masquerade_host;
115 if (asprintf(&sender, "%s@%s", from_user, from_host) <= 0)
e0a95d28 116 return (NULL);
86e4d161
MS
117 }
118
119 if (strchr(sender, '\n') != NULL) {
120 errno = EINVAL;
e0a95d28 121 return (NULL);
86e4d161
MS
122 }
123
057afad5 124 queue->sender = sender;
e0a95d28 125 return (sender);
86e4d161
MS
126}
127
128static int
129read_aliases(void)
130{
a0c4afa6
SS
131 yyin = fopen(config.aliases, "r");
132 if (yyin == NULL) {
133 /*
134 * Non-existing aliases file is not a fatal error
135 */
136 if (errno == ENOENT)
137 return (0);
138 /* Other problems are. */
139 return (-1);
140 }
86e4d161 141 if (yyparse())
e0a95d28 142 return (-1); /* fatal error, probably malloc() */
86e4d161 143 fclose(yyin);
e0a95d28 144 return (0);
86e4d161
MS
145}
146
a803c7a6
SS
147static int
148do_alias(struct queue *queue, const char *addr)
149{
150 struct alias *al;
151 struct stritem *sit;
152 int aliased = 0;
153
154 LIST_FOREACH(al, &aliases, next) {
155 if (strcmp(al->alias, addr) != 0)
156 continue;
157 SLIST_FOREACH(sit, &al->dests, next) {
158 if (add_recp(queue, sit->str, EXPAND_ADDR) != 0)
159 return (-1);
160 }
161 aliased = 1;
162 }
163
164 return (aliased);
165}
166
fd1de51a 167int
057afad5 168add_recp(struct queue *queue, const char *str, int expand)
86e4d161
MS
169{
170 struct qitem *it, *tit;
86e4d161
MS
171 struct passwd *pw;
172 char *host;
173 int aliased = 0;
174
175 it = calloc(1, sizeof(*it));
176 if (it == NULL)
e0a95d28 177 return (-1);
86e4d161
MS
178 it->addr = strdup(str);
179 if (it->addr == NULL)
e0a95d28 180 return (-1);
86e4d161 181
057afad5 182 it->sender = queue->sender;
86e4d161
MS
183 host = strrchr(it->addr, '@');
184 if (host != NULL &&
185 (strcmp(host + 1, hostname()) == 0 ||
186 strcmp(host + 1, "localhost") == 0)) {
187 *host = 0;
188 }
189 LIST_FOREACH(tit, &queue->queue, next) {
190 /* weed out duplicate dests */
191 if (strcmp(tit->addr, it->addr) == 0) {
192 free(it->addr);
193 free(it);
e0a95d28 194 return (0);
86e4d161
MS
195 }
196 }
197 LIST_INSERT_HEAD(&queue->queue, it, next);
198 if (strrchr(it->addr, '@') == NULL) {
e0a95d28 199 it->remote = 0;
86e4d161 200 if (expand) {
a803c7a6
SS
201 aliased = do_alias(queue, it->addr);
202 if (!aliased && expand == EXPAND_WILDCARD)
203 aliased = do_alias(queue, "*");
204 if (aliased < 0)
205 return (-1);
86e4d161
MS
206 if (aliased) {
207 LIST_REMOVE(it, next);
208 } else {
e0a95d28 209 /* Local destination, check */
86e4d161
MS
210 pw = getpwnam(it->addr);
211 if (pw == NULL)
212 goto out;
8d291e4d 213 /* XXX read .forward */
e0a95d28 214 endpwent();
86e4d161
MS
215 }
216 }
217 } else {
e0a95d28 218 it->remote = 1;
86e4d161
MS
219 }
220
e0a95d28 221 return (0);
86e4d161
MS
222
223out:
224 free(it->addr);
225 free(it);
e0a95d28 226 return (-1);
86e4d161
MS
227}
228
e0a95d28
MS
229static struct qitem *
230go_background(struct queue *queue)
86e4d161
MS
231{
232 struct sigaction sa;
233 struct qitem *it;
234 pid_t pid;
235
236 if (daemonize && daemon(0, 0) != 0) {
e0a95d28 237 syslog(LOG_ERR, "can not daemonize: %m");
86e4d161
MS
238 exit(1);
239 }
240 daemonize = 0;
e0a95d28 241
86e4d161 242 bzero(&sa, sizeof(sa));
86e4d161
MS
243 sa.sa_handler = SIG_IGN;
244 sigaction(SIGCHLD, &sa, NULL);
245
94389971 246 LIST_FOREACH(it, &queue->queue, next) {
e0a95d28 247 /* No need to fork for the last dest */
87bdbc01
SS
248 if (LIST_NEXT(it, next) == NULL)
249 goto retit;
e0a95d28 250
86e4d161
MS
251 pid = fork();
252 switch (pid) {
253 case -1:
254 syslog(LOG_ERR, "can not fork: %m");
255 exit(1);
256 break;
257
258 case 0:
259 /*
260 * Child:
261 *
262 * return and deliver mail
263 */
87bdbc01
SS
264retit:
265 /*
9aec6ad8 266 * If necessary, acquire the queue and * mail files.
87bdbc01
SS
267 * If this fails, we probably were raced by another
268 * process.
269 */
4d5af2b0 270 setlogident("%s", it->queueid);
9aec6ad8 271 if (acquirespool(it) < 0)
87bdbc01
SS
272 exit(1);
273 dropspool(queue, it);
e0a95d28 274 return (it);
86e4d161
MS
275
276 default:
277 /*
278 * Parent:
279 *
280 * fork next child
281 */
282 break;
283 }
284 }
285
286 syslog(LOG_CRIT, "reached dead code");
287 exit(1);
288}
289
86e4d161 290static void
e0a95d28 291deliver(struct qitem *it)
86e4d161
MS
292{
293 int error;
f54b5114 294 unsigned int backoff = MIN_RETRY;
86e4d161
MS
295 struct timeval now;
296 struct stat st;
297
249ee966
SS
298 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
299
86e4d161 300retry:
4d5af2b0 301 syslog(LOG_INFO, "trying delivery");
86e4d161 302
e0a95d28 303 if (it->remote)
249ee966 304 error = deliver_remote(it);
e0a95d28 305 else
249ee966 306 error = deliver_local(it);
86e4d161
MS
307
308 switch (error) {
309 case 0:
fd1de51a 310 delqueue(it);
4d5af2b0 311 syslog(LOG_INFO, "delivery successful");
86e4d161
MS
312 exit(0);
313
314 case 1:
315 if (stat(it->queuefn, &st) != 0) {
4d5af2b0 316 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
86e4d161
MS
317 exit(1);
318 }
319 if (gettimeofday(&now, NULL) == 0 &&
2fbb30b2 320 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
249ee966 321 snprintf(errmsg, sizeof(errmsg),
e0a95d28 322 "Could not deliver for the last %d seconds. Giving up.",
a43346d5 323 MAX_TIMEOUT);
86e4d161
MS
324 goto bounce;
325 }
0de88752
SS
326 if (sleep(backoff) == 0) {
327 /* pick the next backoff between [1.5, 2.5) times backoff */
328 backoff = backoff + backoff / 2 + random() % backoff;
329 if (backoff > MAX_RETRY)
330 backoff = MAX_RETRY;
331 }
86e4d161
MS
332 goto retry;
333
334 case -1:
335 default:
336 break;
337 }
338
339bounce:
e0a95d28 340 bounce(it, errmsg);
86e4d161
MS
341 /* NOTREACHED */
342}
343
28be0b96 344void
86e4d161
MS
345run_queue(struct queue *queue)
346{
e0a95d28
MS
347 struct qitem *it;
348
86e4d161
MS
349 if (LIST_EMPTY(&queue->queue))
350 return;
351
e0a95d28
MS
352 it = go_background(queue);
353 deliver(it);
86e4d161
MS
354 /* NOTREACHED */
355}
356
357static void
358show_queue(struct queue *queue)
359{
360 struct qitem *it;
87bdbc01 361 int locked = 0; /* XXX */
86e4d161
MS
362
363 if (LIST_EMPTY(&queue->queue)) {
364 printf("Mail queue is empty\n");
365 return;
366 }
367
368 LIST_FOREACH(it, &queue->queue, next) {
5896fefc
SS
369 printf("ID\t: %s%s\n"
370 "From\t: %s\n"
553ab44c 371 "To\t: %s\n",
5896fefc 372 it->queueid,
87bdbc01 373 locked ? "*" : "",
5896fefc 374 it->sender, it->addr);
553ab44c
SS
375
376 if (LIST_NEXT(it, next) != NULL)
377 printf("--\n");
86e4d161
MS
378 }
379}
380
381/*
382 * TODO:
383 *
384 * - alias processing
385 * - use group permissions
386 * - proper sysexit codes
387 */
388
e0a95d28
MS
389int
390main(int argc, char **argv)
86e4d161 391{
247523d6 392 struct sigaction act;
86e4d161 393 char *sender = NULL;
86e4d161 394 struct queue queue;
86e4d161 395 int i, ch;
bca4c72a 396 int nodot = 0, doqueue = 0, showq = 0, queue_only = 0;
f734c0ae 397 int recp_from_header = 0;
86e4d161 398
75fdf182
SS
399 set_username();
400
401 /*
402 * We never run as root. If called by root, drop permissions
403 * to the mail user.
404 */
405 if (geteuid() == 0 || getuid() == 0) {
406 struct passwd *pw;
407
408 pw = getpwnam(DMA_ROOT_USER);
409 if (pw == NULL)
410 err(1, "cannot drop root privileges");
411
412 if (setuid(pw->pw_uid) != 0)
413 err(1, "cannot drop root privileges");
414
415 if (geteuid() == 0 || getuid() == 0)
416 errx(1, "cannot drop root privileges");
417 }
418
86e4d161 419 atexit(deltmp);
ee613428 420 init_random();
6f5efe4f
SS
421
422 bzero(&queue, sizeof(queue));
86e4d161 423 LIST_INIT(&queue.queue);
86e4d161 424
0f779ba6
SS
425 if (strcmp(argv[0], "mailq") == 0) {
426 argv++; argc--;
427 showq = 1;
428 if (argc != 0)
429 errx(1, "invalid arguments");
430 goto skipopts;
431 }
432
87fa1cc5 433 opterr = 0;
f734c0ae 434 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
86e4d161
MS
435 switch (ch) {
436 case 'A':
437 /* -AX is being ignored, except for -A{c,m} */
438 if (optarg[0] == 'c' || optarg[0] == 'm') {
439 break;
440 }
441 /* else FALLTRHOUGH */
442 case 'b':
443 /* -bX is being ignored, except for -bp */
444 if (optarg[0] == 'p') {
445 showq = 1;
446 break;
bca4c72a
SS
447 } else if (optarg[0] == 'q') {
448 queue_only = 1;
449 break;
86e4d161
MS
450 }
451 /* else FALLTRHOUGH */
452 case 'D':
453 daemonize = 0;
454 break;
455 case 'L':
4d5af2b0 456 logident_base = optarg;
86e4d161
MS
457 break;
458 case 'f':
459 case 'r':
460 sender = optarg;
461 break;
462
f734c0ae
SS
463 case 't':
464 recp_from_header = 1;
465 break;
466
86e4d161
MS
467 case 'o':
468 /* -oX is being ignored, except for -oi */
469 if (optarg[0] != 'i')
470 break;
471 /* else FALLTRHOUGH */
87fa1cc5
MS
472 case 'O':
473 break;
86e4d161
MS
474 case 'i':
475 nodot = 1;
476 break;
477
478 case 'q':
479 doqueue = 1;
480 break;
481
b6bb4e2d
SS
482 /* Ignored options */
483 case 'B':
484 case 'C':
485 case 'd':
486 case 'F':
487 case 'h':
488 case 'N':
489 case 'n':
490 case 'R':
491 case 'U':
492 case 'V':
493 case 'v':
494 case 'X':
495 break;
496
f514a0d9
SS
497 case ':':
498 if (optopt == 'q') {
499 doqueue = 1;
500 break;
501 }
502 /* FALLTHROUGH */
503
86e4d161 504 default:
f514a0d9 505 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
86e4d161
MS
506 exit(1);
507 }
508 }
509 argc -= optind;
510 argv += optind;
87fa1cc5 511 opterr = 1;
86e4d161 512
8d291e4d
SS
513 if (argc != 0 && (showq || doqueue))
514 errx(1, "sending mail and queue operations are mutually exclusive");
515
516 if (showq + doqueue > 1)
517 errx(1, "conflicting queue operations");
518
0f779ba6 519skipopts:
4d5af2b0
SS
520 if (logident_base == NULL)
521 logident_base = "dma";
522 setlogident(NULL);
4602c807 523
247523d6
SS
524 act.sa_handler = sighup_handler;
525 act.sa_flags = 0;
526 sigemptyset(&act.sa_mask);
527 if (sigaction(SIGHUP, &act, NULL) != 0)
528 syslog(LOG_WARNING, "can not set signal handler: %m");
529
d84c3daa 530 parse_conf(CONF_PATH "/dma.conf");
86e4d161 531
a0c4afa6
SS
532 if (config.authpath != NULL)
533 parse_authfile(config.authpath);
86e4d161
MS
534
535 if (showq) {
6f5efe4f 536 if (load_queue(&queue) < 0)
de13a881 537 errlog(1, "can not load queue");
6f5efe4f 538 show_queue(&queue);
e0a95d28 539 return (0);
86e4d161
MS
540 }
541
542 if (doqueue) {
6f5efe4f 543 if (load_queue(&queue) < 0)
de13a881 544 errlog(1, "can not load queue");
6f5efe4f 545 run_queue(&queue);
e0a95d28 546 return (0);
86e4d161
MS
547 }
548
e0a95d28 549 if (read_aliases() != 0)
a0c4afa6 550 errlog(1, "can not read aliases file `%s'", config.aliases);
86e4d161 551
057afad5 552 if ((sender = set_from(&queue, sender)) == NULL)
de13a881 553 errlog(1, NULL);
86e4d161 554
f734c0ae
SS
555 if (newspoolf(&queue) != 0)
556 errlog(1, "can not create temp file");
557
558 setlogident("%s", queue.id);
559
86e4d161 560 for (i = 0; i < argc; i++) {
a803c7a6 561 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
de13a881 562 errlogx(1, "invalid recipient `%s'", argv[i]);
86e4d161
MS
563 }
564
f734c0ae 565 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
de13a881 566 errlogx(1, "no recipients");
86e4d161 567
f734c0ae 568 if (readmail(&queue, nodot, recp_from_header) != 0)
de13a881 569 errlog(1, "can not read mail");
86e4d161 570
f734c0ae
SS
571 if (LIST_EMPTY(&queue.queue))
572 errlogx(1, "no recipients");
573
057afad5 574 if (linkspool(&queue) != 0)
de13a881 575 errlog(1, "can not create spools");
86e4d161
MS
576
577 /* From here on the mail is safe. */
578
a0c4afa6 579 if (config.features & DEFER || queue_only)
e0a95d28 580 return (0);
86e4d161 581
28be0b96 582 run_queue(&queue);
86e4d161
MS
583
584 /* NOTREACHED */
e0a95d28 585 return (0);
94389971 586}