dma(8): Use FALLTHROUGH consistently.
[dragonfly.git] / libexec / dma / dma.c
CommitLineData
f67beddd 1/*
37d59876 2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
f67beddd
MS
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
37d59876 6 * by Simon Schubert <2@0x2c.org>.
f67beddd
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.
f67beddd
MS
34 */
35
c8b07ee5
SW
36#include "dfcompat.h"
37
f67beddd 38#include <sys/param.h>
4a6863a4 39#include <sys/types.h>
f67beddd
MS
40#include <sys/queue.h>
41#include <sys/stat.h>
c8b07ee5 42#include <sys/time.h>
bc7baf1d 43#include <sys/wait.h>
f67beddd 44
f67beddd
MS
45#include <dirent.h>
46#include <err.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <inttypes.h>
f67beddd
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
52e9aa73 62extern int yyparse(void);
4a23bd3d 63static void deliver(struct qitem *);
f67beddd
MS
64
65struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
f4e61a9f 66struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
f67beddd 67struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
c8b07ee5
SW
68char username[USERNAME_SIZE];
69uid_t useruid;
1da0a9f2 70const char *logident_base;
c8b07ee5 71char errmsg[ERRMSG_SIZE];
f67beddd 72
9afa363f 73static int daemonize = 1;
14dfb991 74static int doqueue = 0;
f8633e71 75
ca259d14
SS
76struct config config = {
77 .smarthost = NULL,
78 .port = 25,
c8b07ee5 79 .aliases = "/etc/aliases",
ca259d14 80 .spooldir = "/var/spool/dma",
ca259d14
SS
81 .authpath = NULL,
82 .certfile = NULL,
83 .features = 0,
84 .mailname = NULL,
c8b07ee5
SW
85 .masquerade_host = NULL,
86 .masquerade_user = NULL,
ca259d14
SS
87};
88
89
c8b07ee5
SW
90static void
91sighup_handler(int signo)
92{
93 (void)signo; /* so that gcc doesn't complain */
94}
95
f67beddd 96static char *
1c9e6b7b 97set_from(struct queue *queue, const char *osender)
f67beddd 98{
14dfb991 99 const char *addr;
f67beddd
MS
100 char *sender;
101
f67beddd 102 if (osender) {
14dfb991 103 addr = osender;
c8b07ee5 104 } else if (getenv("EMAIL") != NULL) {
14dfb991 105 addr = getenv("EMAIL");
f67beddd 106 } else {
14dfb991
JG
107 if (config.masquerade_user)
108 addr = config.masquerade_user;
109 else
110 addr = username;
111 }
112
113 if (!strchr(addr, '@')) {
c8b07ee5
SW
114 const char *from_host = hostname();
115
c8b07ee5
SW
116 if (config.masquerade_host)
117 from_host = config.masquerade_host;
14dfb991
JG
118
119 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
120 return (NULL);
121 } else {
122 sender = strdup(addr);
123 if (sender == NULL)
4a23bd3d 124 return (NULL);
f67beddd
MS
125 }
126
127 if (strchr(sender, '\n') != NULL) {
128 errno = EINVAL;
4a23bd3d 129 return (NULL);
f67beddd
MS
130 }
131
1c9e6b7b 132 queue->sender = sender;
4a23bd3d 133 return (sender);
f67beddd
MS
134}
135
136static int
137read_aliases(void)
138{
ca259d14
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 }
f67beddd 149 if (yyparse())
4a23bd3d 150 return (-1); /* fatal error, probably malloc() */
f67beddd 151 fclose(yyin);
4a23bd3d 152 return (0);
f67beddd
MS
153}
154
c8b07ee5
SW
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
f4e61a9f 175int
1c9e6b7b 176add_recp(struct queue *queue, const char *str, int expand)
f67beddd
MS
177{
178 struct qitem *it, *tit;
f67beddd
MS
179 struct passwd *pw;
180 char *host;
181 int aliased = 0;
182
183 it = calloc(1, sizeof(*it));
184 if (it == NULL)
4a23bd3d 185 return (-1);
f67beddd
MS
186 it->addr = strdup(str);
187 if (it->addr == NULL)
4a23bd3d 188 return (-1);
f67beddd 189
1c9e6b7b 190 it->sender = queue->sender;
f67beddd
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);
4a23bd3d 202 return (0);
f67beddd
MS
203 }
204 }
205 LIST_INSERT_HEAD(&queue->queue, it, next);
14dfb991
JG
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) {
4a23bd3d 212 it->remote = 0;
f67beddd 213 if (expand) {
c8b07ee5
SW
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);
f67beddd
MS
219 if (aliased) {
220 LIST_REMOVE(it, next);
221 } else {
4a23bd3d 222 /* Local destination, check */
f67beddd
MS
223 pw = getpwnam(it->addr);
224 if (pw == NULL)
225 goto out;
13e288a1 226 /* XXX read .forward */
4a23bd3d 227 endpwent();
f67beddd
MS
228 }
229 }
230 } else {
4a23bd3d 231 it->remote = 1;
f67beddd
MS
232 }
233
4a23bd3d 234 return (0);
f67beddd
MS
235
236out:
237 free(it->addr);
238 free(it);
4a23bd3d 239 return (-1);
f67beddd
MS
240}
241
4a23bd3d
MS
242static struct qitem *
243go_background(struct queue *queue)
f67beddd
MS
244{
245 struct sigaction sa;
246 struct qitem *it;
247 pid_t pid;
248
249 if (daemonize && daemon(0, 0) != 0) {
4a23bd3d 250 syslog(LOG_ERR, "can not daemonize: %m");
f67beddd
MS
251 exit(1);
252 }
253 daemonize = 0;
4a23bd3d 254
f67beddd 255 bzero(&sa, sizeof(sa));
f67beddd
MS
256 sa.sa_handler = SIG_IGN;
257 sigaction(SIGCHLD, &sa, NULL);
258
4dcaa51b 259 LIST_FOREACH(it, &queue->queue, next) {
4a23bd3d 260 /* No need to fork for the last dest */
9afa363f
SS
261 if (LIST_NEXT(it, next) == NULL)
262 goto retit;
4a23bd3d 263
f67beddd
MS
264 pid = fork();
265 switch (pid) {
266 case -1:
267 syslog(LOG_ERR, "can not fork: %m");
268 exit(1);
269 break;
270
271 case 0:
272 /*
273 * Child:
274 *
275 * return and deliver mail
276 */
9afa363f
SS
277retit:
278 /*
24c80b2b 279 * If necessary, acquire the queue and * mail files.
9afa363f 280 * If this fails, we probably were raced by another
14dfb991
JG
281 * process. It is okay to be raced if we're supposed
282 * to flush the queue.
9afa363f 283 */
405f48ee 284 setlogident("%s", it->queueid);
14dfb991
JG
285 switch (acquirespool(it)) {
286 case 0:
287 break;
288 case 1:
289 if (doqueue)
290 exit(0);
291 syslog(LOG_WARNING, "could not lock queue file");
9afa363f 292 exit(1);
14dfb991
JG
293 default:
294 exit(1);
295 }
9afa363f 296 dropspool(queue, it);
4a23bd3d 297 return (it);
f67beddd
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");
310 exit(1);
311}
312
f67beddd 313static void
4a23bd3d 314deliver(struct qitem *it)
f67beddd
MS
315{
316 int error;
14dfb991 317 unsigned int backoff = MIN_RETRY, slept;
f67beddd
MS
318 struct timeval now;
319 struct stat st;
320
c8b07ee5
SW
321 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
322
f67beddd 323retry:
405f48ee 324 syslog(LOG_INFO, "trying delivery");
f67beddd 325
4a23bd3d 326 if (it->remote)
c8b07ee5 327 error = deliver_remote(it);
4a23bd3d 328 else
c8b07ee5 329 error = deliver_local(it);
f67beddd
MS
330
331 switch (error) {
332 case 0:
f4e61a9f 333 delqueue(it);
405f48ee 334 syslog(LOG_INFO, "delivery successful");
f67beddd
MS
335 exit(0);
336
337 case 1:
338 if (stat(it->queuefn, &st) != 0) {
405f48ee 339 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
f67beddd
MS
340 exit(1);
341 }
342 if (gettimeofday(&now, NULL) == 0 &&
2e570fe1 343 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
c8b07ee5 344 snprintf(errmsg, sizeof(errmsg),
4a23bd3d 345 "Could not deliver for the last %d seconds. Giving up.",
39f7c85a 346 MAX_TIMEOUT);
f67beddd
MS
347 goto bounce;
348 }
14dfb991
JG
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) {
c8b07ee5
SW
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 }
f67beddd
MS
362 goto retry;
363
364 case -1:
365 default:
366 break;
367 }
368
369bounce:
4a23bd3d 370 bounce(it, errmsg);
f67beddd
MS
371 /* NOTREACHED */
372}
373
30833a29 374void
f67beddd
MS
375run_queue(struct queue *queue)
376{
4a23bd3d
MS
377 struct qitem *it;
378
f67beddd
MS
379 if (LIST_EMPTY(&queue->queue))
380 return;
381
4a23bd3d
MS
382 it = go_background(queue);
383 deliver(it);
f67beddd
MS
384 /* NOTREACHED */
385}
386
387static void
388show_queue(struct queue *queue)
389{
390 struct qitem *it;
9afa363f 391 int locked = 0; /* XXX */
f67beddd
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) {
7b5c40d5
SS
399 printf("ID\t: %s%s\n"
400 "From\t: %s\n"
71b5e57d 401 "To\t: %s\n",
7b5c40d5 402 it->queueid,
9afa363f 403 locked ? "*" : "",
7b5c40d5 404 it->sender, it->addr);
71b5e57d
SS
405
406 if (LIST_NEXT(it, next) != NULL)
407 printf("--\n");
f67beddd
MS
408 }
409}
410
411/*
412 * TODO:
413 *
414 * - alias processing
415 * - use group permissions
416 * - proper sysexit codes
417 */
418
4a23bd3d
MS
419int
420main(int argc, char **argv)
f67beddd 421{
c8b07ee5 422 struct sigaction act;
f67beddd 423 char *sender = NULL;
f67beddd 424 struct queue queue;
f67beddd 425 int i, ch;
14dfb991 426 int nodot = 0, showq = 0, queue_only = 0;
ff48fce6 427 int recp_from_header = 0;
f67beddd 428
c8b07ee5
SW
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
14dfb991 438 errno = 0;
c8b07ee5 439 pw = getpwnam(DMA_ROOT_USER);
14dfb991
JG
440 if (pw == NULL) {
441 if (errno == 0)
442 errx(1, "user '%s' not found", DMA_ROOT_USER);
443 else
444 err(1, "cannot drop root privileges");
445 }
c8b07ee5
SW
446
447 if (setuid(pw->pw_uid) != 0)
448 err(1, "cannot drop root privileges");
449
450 if (geteuid() == 0 || getuid() == 0)
451 errx(1, "cannot drop root privileges");
452 }
453
f67beddd 454 atexit(deltmp);
c8b07ee5 455 init_random();
e50076f8
SS
456
457 bzero(&queue, sizeof(queue));
f67beddd 458 LIST_INIT(&queue.queue);
f67beddd 459
eb870081
SS
460 if (strcmp(argv[0], "mailq") == 0) {
461 argv++; argc--;
462 showq = 1;
00db07ff 463 if (argc != 0 && strcmp(argv[0], "-Ac") != 0)
eb870081
SS
464 errx(1, "invalid arguments");
465 goto skipopts;
14dfb991
JG
466 } else if (strcmp(argv[0], "newaliases") == 0) {
467 logident_base = "dma";
468 setlogident(NULL);
469
470 if (read_aliases() != 0)
471 errx(1, "could not parse aliases file `%s'", config.aliases);
472 exit(0);
00db07ff
SW
473 } else if (strcmp(argv[0], "hoststat") == 0 ||
474 strcmp(argv[0], "purgestat") == 0) {
475 exit(0);
eb870081
SS
476 }
477
ae3c3bbd 478 opterr = 0;
ff48fce6 479 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
f67beddd
MS
480 switch (ch) {
481 case 'A':
482 /* -AX is being ignored, except for -A{c,m} */
483 if (optarg[0] == 'c' || optarg[0] == 'm') {
484 break;
485 }
c6b7f0da 486 /* FALLTHROUGH */
f67beddd
MS
487 case 'b':
488 /* -bX is being ignored, except for -bp */
489 if (optarg[0] == 'p') {
490 showq = 1;
491 break;
a9337db7
SS
492 } else if (optarg[0] == 'q') {
493 queue_only = 1;
494 break;
f67beddd 495 }
c6b7f0da 496 /* FALLTHROUGH */
f67beddd
MS
497 case 'D':
498 daemonize = 0;
499 break;
500 case 'L':
405f48ee 501 logident_base = optarg;
f67beddd
MS
502 break;
503 case 'f':
504 case 'r':
505 sender = optarg;
506 break;
507
ff48fce6
SS
508 case 't':
509 recp_from_header = 1;
510 break;
511
f67beddd
MS
512 case 'o':
513 /* -oX is being ignored, except for -oi */
514 if (optarg[0] != 'i')
515 break;
c6b7f0da 516 /* FALLTHROUGH */
ae3c3bbd
MS
517 case 'O':
518 break;
f67beddd
MS
519 case 'i':
520 nodot = 1;
521 break;
522
523 case 'q':
14dfb991
JG
524 /* Don't let getopt slup up other arguments */
525 if (optarg && *optarg == '-')
526 optind--;
f67beddd
MS
527 doqueue = 1;
528 break;
529
98dfc119
SS
530 /* Ignored options */
531 case 'B':
532 case 'C':
533 case 'd':
534 case 'F':
535 case 'h':
536 case 'N':
537 case 'n':
538 case 'R':
539 case 'U':
540 case 'V':
541 case 'v':
542 case 'X':
543 break;
544
abbe6a2c
SS
545 case ':':
546 if (optopt == 'q') {
547 doqueue = 1;
548 break;
549 }
550 /* FALLTHROUGH */
551
f67beddd 552 default:
abbe6a2c 553 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
f67beddd
MS
554 exit(1);
555 }
556 }
557 argc -= optind;
558 argv += optind;
ae3c3bbd 559 opterr = 1;
f67beddd 560
13e288a1
SS
561 if (argc != 0 && (showq || doqueue))
562 errx(1, "sending mail and queue operations are mutually exclusive");
563
564 if (showq + doqueue > 1)
565 errx(1, "conflicting queue operations");
566
eb870081 567skipopts:
405f48ee
SS
568 if (logident_base == NULL)
569 logident_base = "dma";
570 setlogident(NULL);
f67beddd 571
c8b07ee5
SW
572 act.sa_handler = sighup_handler;
573 act.sa_flags = 0;
574 sigemptyset(&act.sa_mask);
575 if (sigaction(SIGHUP, &act, NULL) != 0)
576 syslog(LOG_WARNING, "can not set signal handler: %m");
569443a9 577
c8b07ee5 578 parse_conf(CONF_PATH "/dma.conf");
f67beddd 579
ca259d14
SS
580 if (config.authpath != NULL)
581 parse_authfile(config.authpath);
f67beddd
MS
582
583 if (showq) {
e50076f8 584 if (load_queue(&queue) < 0)
1da0a9f2 585 errlog(1, "can not load queue");
e50076f8 586 show_queue(&queue);
4a23bd3d 587 return (0);
f67beddd
MS
588 }
589
590 if (doqueue) {
14dfb991 591 flushqueue_signal();
e50076f8 592 if (load_queue(&queue) < 0)
1da0a9f2 593 errlog(1, "can not load queue");
e50076f8 594 run_queue(&queue);
4a23bd3d 595 return (0);
f67beddd
MS
596 }
597
4a23bd3d 598 if (read_aliases() != 0)
14dfb991 599 errlog(1, "could not parse aliases file `%s'", config.aliases);
f67beddd 600
1c9e6b7b 601 if ((sender = set_from(&queue, sender)) == NULL)
1da0a9f2 602 errlog(1, NULL);
f67beddd 603
ff48fce6 604 if (newspoolf(&queue) != 0)
14dfb991 605 errlog(1, "can not create temp file in `%s'", config.spooldir);
ff48fce6
SS
606
607 setlogident("%s", queue.id);
608
f67beddd 609 for (i = 0; i < argc; i++) {
c8b07ee5 610 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
1da0a9f2 611 errlogx(1, "invalid recipient `%s'", argv[i]);
f67beddd
MS
612 }
613
ff48fce6 614 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
1da0a9f2 615 errlogx(1, "no recipients");
f67beddd 616
ff48fce6 617 if (readmail(&queue, nodot, recp_from_header) != 0)
1da0a9f2 618 errlog(1, "can not read mail");
f67beddd 619
ff48fce6
SS
620 if (LIST_EMPTY(&queue.queue))
621 errlogx(1, "no recipients");
622
1c9e6b7b 623 if (linkspool(&queue) != 0)
1da0a9f2 624 errlog(1, "can not create spools");
f67beddd
MS
625
626 /* From here on the mail is safe. */
627
ca259d14 628 if (config.features & DEFER || queue_only)
4a23bd3d 629 return (0);
f67beddd 630
30833a29 631 run_queue(&queue);
f67beddd
MS
632
633 /* NOTREACHED */
4a23bd3d 634 return (0);
4dcaa51b 635}