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