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