]> git.ipfire.org Git - people/ms/dma.git/blame - dma.c
set_from: always fully qualify envelope from
[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;
e33faafe 73static int doqueue = 0;
dd561a09 74
a0c4afa6
SS
75struct config config = {
76 .smarthost = NULL,
77 .port = 25,
db93ed9b 78 .aliases = "/etc/aliases",
a0c4afa6 79 .spooldir = "/var/spool/dma",
a0c4afa6
SS
80 .authpath = NULL,
81 .certfile = NULL,
82 .features = 0,
83 .mailname = NULL,
5bc72de8
SS
84 .masquerade_host = NULL,
85 .masquerade_user = NULL,
a0c4afa6
SS
86};
87
88
247523d6
SS
89static void
90sighup_handler(int signo)
91{
92 (void)signo; /* so that gcc doesn't complain */
93}
94
86e4d161 95static char *
057afad5 96set_from(struct queue *queue, const char *osender)
86e4d161 97{
0ecb0ebe 98 const char *addr;
86e4d161
MS
99 char *sender;
100
86e4d161 101 if (osender) {
0ecb0ebe 102 addr = osender;
09a2b07e 103 } else if (getenv("EMAIL") != NULL) {
0ecb0ebe 104 addr = getenv("EMAIL");
86e4d161 105 } else {
0ecb0ebe
SS
106 if (config.masquerade_user)
107 addr = config.masquerade_user;
108 else
109 addr = username;
110 }
111
112 if (!strchr(addr, '@')) {
5bc72de8
SS
113 const char *from_host = hostname();
114
5bc72de8
SS
115 if (config.masquerade_host)
116 from_host = config.masquerade_host;
0ecb0ebe
SS
117
118 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
119 return (NULL);
120 } else {
121 sender = strdup(addr);
122 if (sender == NULL)
e0a95d28 123 return (NULL);
86e4d161
MS
124 }
125
126 if (strchr(sender, '\n') != NULL) {
127 errno = EINVAL;
e0a95d28 128 return (NULL);
86e4d161
MS
129 }
130
057afad5 131 queue->sender = sender;
e0a95d28 132 return (sender);
86e4d161
MS
133}
134
135static int
136read_aliases(void)
137{
a0c4afa6
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 }
86e4d161 148 if (yyparse())
e0a95d28 149 return (-1); /* fatal error, probably malloc() */
86e4d161 150 fclose(yyin);
e0a95d28 151 return (0);
86e4d161
MS
152}
153
a803c7a6
SS
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
fd1de51a 174int
057afad5 175add_recp(struct queue *queue, const char *str, int expand)
86e4d161
MS
176{
177 struct qitem *it, *tit;
86e4d161
MS
178 struct passwd *pw;
179 char *host;
180 int aliased = 0;
181
182 it = calloc(1, sizeof(*it));
183 if (it == NULL)
e0a95d28 184 return (-1);
86e4d161
MS
185 it->addr = strdup(str);
186 if (it->addr == NULL)
e0a95d28 187 return (-1);
86e4d161 188
057afad5 189 it->sender = queue->sender;
86e4d161
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);
e0a95d28 201 return (0);
86e4d161
MS
202 }
203 }
204 LIST_INSERT_HEAD(&queue->queue, it, next);
205 if (strrchr(it->addr, '@') == NULL) {
e0a95d28 206 it->remote = 0;
86e4d161 207 if (expand) {
a803c7a6
SS
208 aliased = do_alias(queue, it->addr);
209 if (!aliased && expand == EXPAND_WILDCARD)
210 aliased = do_alias(queue, "*");
211 if (aliased < 0)
212 return (-1);
86e4d161
MS
213 if (aliased) {
214 LIST_REMOVE(it, next);
215 } else {
e0a95d28 216 /* Local destination, check */
86e4d161
MS
217 pw = getpwnam(it->addr);
218 if (pw == NULL)
219 goto out;
8d291e4d 220 /* XXX read .forward */
e0a95d28 221 endpwent();
86e4d161
MS
222 }
223 }
224 } else {
e0a95d28 225 it->remote = 1;
86e4d161
MS
226 }
227
e0a95d28 228 return (0);
86e4d161
MS
229
230out:
231 free(it->addr);
232 free(it);
e0a95d28 233 return (-1);
86e4d161
MS
234}
235
e0a95d28
MS
236static struct qitem *
237go_background(struct queue *queue)
86e4d161
MS
238{
239 struct sigaction sa;
240 struct qitem *it;
241 pid_t pid;
242
243 if (daemonize && daemon(0, 0) != 0) {
e0a95d28 244 syslog(LOG_ERR, "can not daemonize: %m");
86e4d161
MS
245 exit(1);
246 }
247 daemonize = 0;
e0a95d28 248
86e4d161 249 bzero(&sa, sizeof(sa));
86e4d161
MS
250 sa.sa_handler = SIG_IGN;
251 sigaction(SIGCHLD, &sa, NULL);
252
94389971 253 LIST_FOREACH(it, &queue->queue, next) {
e0a95d28 254 /* No need to fork for the last dest */
87bdbc01
SS
255 if (LIST_NEXT(it, next) == NULL)
256 goto retit;
e0a95d28 257
86e4d161
MS
258 pid = fork();
259 switch (pid) {
260 case -1:
261 syslog(LOG_ERR, "can not fork: %m");
262 exit(1);
263 break;
264
265 case 0:
266 /*
267 * Child:
268 *
269 * return and deliver mail
270 */
87bdbc01
SS
271retit:
272 /*
9aec6ad8 273 * If necessary, acquire the queue and * mail files.
87bdbc01 274 * If this fails, we probably were raced by another
e33faafe
SS
275 * process. It is okay to be raced if we're supposed
276 * to flush the queue.
87bdbc01 277 */
4d5af2b0 278 setlogident("%s", it->queueid);
e33faafe
SS
279 switch (acquirespool(it)) {
280 case 0:
281 break;
282 case 1:
283 if (doqueue)
284 exit(0);
285 syslog(LOG_WARNING, "could not lock queue file");
87bdbc01 286 exit(1);
e33faafe
SS
287 default:
288 exit(1);
289 }
87bdbc01 290 dropspool(queue, it);
e0a95d28 291 return (it);
86e4d161
MS
292
293 default:
294 /*
295 * Parent:
296 *
297 * fork next child
298 */
299 break;
300 }
301 }
302
303 syslog(LOG_CRIT, "reached dead code");
304 exit(1);
305}
306
86e4d161 307static void
e0a95d28 308deliver(struct qitem *it)
86e4d161
MS
309{
310 int error;
3686aecc 311 unsigned int backoff = MIN_RETRY, slept;
86e4d161
MS
312 struct timeval now;
313 struct stat st;
314
249ee966
SS
315 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
316
86e4d161 317retry:
4d5af2b0 318 syslog(LOG_INFO, "trying delivery");
86e4d161 319
e0a95d28 320 if (it->remote)
249ee966 321 error = deliver_remote(it);
e0a95d28 322 else
249ee966 323 error = deliver_local(it);
86e4d161
MS
324
325 switch (error) {
326 case 0:
fd1de51a 327 delqueue(it);
4d5af2b0 328 syslog(LOG_INFO, "delivery successful");
86e4d161
MS
329 exit(0);
330
331 case 1:
332 if (stat(it->queuefn, &st) != 0) {
4d5af2b0 333 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
86e4d161
MS
334 exit(1);
335 }
336 if (gettimeofday(&now, NULL) == 0 &&
2fbb30b2 337 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
249ee966 338 snprintf(errmsg, sizeof(errmsg),
e0a95d28 339 "Could not deliver for the last %d seconds. Giving up.",
a43346d5 340 MAX_TIMEOUT);
86e4d161
MS
341 goto bounce;
342 }
3686aecc
SS
343 for (slept = 0; slept < backoff;) {
344 slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT);
345 if (flushqueue_since(slept)) {
346 backoff = MIN_RETRY;
347 goto retry;
348 }
349 }
350 if (slept >= backoff) {
0de88752
SS
351 /* pick the next backoff between [1.5, 2.5) times backoff */
352 backoff = backoff + backoff / 2 + random() % backoff;
353 if (backoff > MAX_RETRY)
354 backoff = MAX_RETRY;
355 }
86e4d161
MS
356 goto retry;
357
358 case -1:
359 default:
360 break;
361 }
362
363bounce:
e0a95d28 364 bounce(it, errmsg);
86e4d161
MS
365 /* NOTREACHED */
366}
367
28be0b96 368void
86e4d161
MS
369run_queue(struct queue *queue)
370{
e0a95d28
MS
371 struct qitem *it;
372
86e4d161
MS
373 if (LIST_EMPTY(&queue->queue))
374 return;
375
e0a95d28
MS
376 it = go_background(queue);
377 deliver(it);
86e4d161
MS
378 /* NOTREACHED */
379}
380
381static void
382show_queue(struct queue *queue)
383{
384 struct qitem *it;
87bdbc01 385 int locked = 0; /* XXX */
86e4d161
MS
386
387 if (LIST_EMPTY(&queue->queue)) {
388 printf("Mail queue is empty\n");
389 return;
390 }
391
392 LIST_FOREACH(it, &queue->queue, next) {
5896fefc
SS
393 printf("ID\t: %s%s\n"
394 "From\t: %s\n"
553ab44c 395 "To\t: %s\n",
5896fefc 396 it->queueid,
87bdbc01 397 locked ? "*" : "",
5896fefc 398 it->sender, it->addr);
553ab44c
SS
399
400 if (LIST_NEXT(it, next) != NULL)
401 printf("--\n");
86e4d161
MS
402 }
403}
404
405/*
406 * TODO:
407 *
408 * - alias processing
409 * - use group permissions
410 * - proper sysexit codes
411 */
412
e0a95d28
MS
413int
414main(int argc, char **argv)
86e4d161 415{
247523d6 416 struct sigaction act;
86e4d161 417 char *sender = NULL;
86e4d161 418 struct queue queue;
86e4d161 419 int i, ch;
e33faafe 420 int nodot = 0, showq = 0, queue_only = 0;
f734c0ae 421 int recp_from_header = 0;
86e4d161 422
75fdf182
SS
423 set_username();
424
425 /*
426 * We never run as root. If called by root, drop permissions
427 * to the mail user.
428 */
429 if (geteuid() == 0 || getuid() == 0) {
430 struct passwd *pw;
431
04947c64 432 errno = 0;
75fdf182 433 pw = getpwnam(DMA_ROOT_USER);
04947c64
EM
434 if (pw == NULL) {
435 if (errno == 0)
436 errx(1, "user '%s' not found", DMA_ROOT_USER);
437 else
438 err(1, "cannot drop root privileges");
439 }
75fdf182
SS
440
441 if (setuid(pw->pw_uid) != 0)
442 err(1, "cannot drop root privileges");
443
444 if (geteuid() == 0 || getuid() == 0)
445 errx(1, "cannot drop root privileges");
446 }
447
86e4d161 448 atexit(deltmp);
ee613428 449 init_random();
6f5efe4f
SS
450
451 bzero(&queue, sizeof(queue));
86e4d161 452 LIST_INIT(&queue.queue);
86e4d161 453
0f779ba6
SS
454 if (strcmp(argv[0], "mailq") == 0) {
455 argv++; argc--;
456 showq = 1;
457 if (argc != 0)
458 errx(1, "invalid arguments");
459 goto skipopts;
460 }
461
87fa1cc5 462 opterr = 0;
f734c0ae 463 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
464 switch (ch) {
465 case 'A':
466 /* -AX is being ignored, except for -A{c,m} */
467 if (optarg[0] == 'c' || optarg[0] == 'm') {
468 break;
469 }
470 /* else FALLTRHOUGH */
471 case 'b':
472 /* -bX is being ignored, except for -bp */
473 if (optarg[0] == 'p') {
474 showq = 1;
475 break;
bca4c72a
SS
476 } else if (optarg[0] == 'q') {
477 queue_only = 1;
478 break;
86e4d161
MS
479 }
480 /* else FALLTRHOUGH */
481 case 'D':
482 daemonize = 0;
483 break;
484 case 'L':
4d5af2b0 485 logident_base = optarg;
86e4d161
MS
486 break;
487 case 'f':
488 case 'r':
489 sender = optarg;
490 break;
491
f734c0ae
SS
492 case 't':
493 recp_from_header = 1;
494 break;
495
86e4d161
MS
496 case 'o':
497 /* -oX is being ignored, except for -oi */
498 if (optarg[0] != 'i')
499 break;
500 /* else FALLTRHOUGH */
87fa1cc5
MS
501 case 'O':
502 break;
86e4d161
MS
503 case 'i':
504 nodot = 1;
505 break;
506
507 case 'q':
af31dade
SS
508 /* Don't let getopt slup up other arguments */
509 if (optarg && *optarg == '-')
510 optind--;
86e4d161
MS
511 doqueue = 1;
512 break;
513
b6bb4e2d
SS
514 /* Ignored options */
515 case 'B':
516 case 'C':
517 case 'd':
518 case 'F':
519 case 'h':
520 case 'N':
521 case 'n':
522 case 'R':
523 case 'U':
524 case 'V':
525 case 'v':
526 case 'X':
527 break;
528
f514a0d9
SS
529 case ':':
530 if (optopt == 'q') {
531 doqueue = 1;
532 break;
533 }
534 /* FALLTHROUGH */
535
86e4d161 536 default:
f514a0d9 537 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
86e4d161
MS
538 exit(1);
539 }
540 }
541 argc -= optind;
542 argv += optind;
87fa1cc5 543 opterr = 1;
86e4d161 544
8d291e4d
SS
545 if (argc != 0 && (showq || doqueue))
546 errx(1, "sending mail and queue operations are mutually exclusive");
547
548 if (showq + doqueue > 1)
549 errx(1, "conflicting queue operations");
550
0f779ba6 551skipopts:
4d5af2b0
SS
552 if (logident_base == NULL)
553 logident_base = "dma";
554 setlogident(NULL);
4602c807 555
247523d6
SS
556 act.sa_handler = sighup_handler;
557 act.sa_flags = 0;
558 sigemptyset(&act.sa_mask);
559 if (sigaction(SIGHUP, &act, NULL) != 0)
560 syslog(LOG_WARNING, "can not set signal handler: %m");
561
d84c3daa 562 parse_conf(CONF_PATH "/dma.conf");
86e4d161 563
a0c4afa6
SS
564 if (config.authpath != NULL)
565 parse_authfile(config.authpath);
86e4d161
MS
566
567 if (showq) {
6f5efe4f 568 if (load_queue(&queue) < 0)
de13a881 569 errlog(1, "can not load queue");
6f5efe4f 570 show_queue(&queue);
e0a95d28 571 return (0);
86e4d161
MS
572 }
573
574 if (doqueue) {
3686aecc 575 flushqueue_signal();
6f5efe4f 576 if (load_queue(&queue) < 0)
de13a881 577 errlog(1, "can not load queue");
6f5efe4f 578 run_queue(&queue);
e0a95d28 579 return (0);
86e4d161
MS
580 }
581
e0a95d28 582 if (read_aliases() != 0)
a0c4afa6 583 errlog(1, "can not read aliases file `%s'", config.aliases);
86e4d161 584
057afad5 585 if ((sender = set_from(&queue, sender)) == NULL)
de13a881 586 errlog(1, NULL);
86e4d161 587
f734c0ae 588 if (newspoolf(&queue) != 0)
c8674a69 589 errlog(1, "can not create temp file in `%s'", config.spooldir);
f734c0ae
SS
590
591 setlogident("%s", queue.id);
592
86e4d161 593 for (i = 0; i < argc; i++) {
a803c7a6 594 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
de13a881 595 errlogx(1, "invalid recipient `%s'", argv[i]);
86e4d161
MS
596 }
597
f734c0ae 598 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
de13a881 599 errlogx(1, "no recipients");
86e4d161 600
f734c0ae 601 if (readmail(&queue, nodot, recp_from_header) != 0)
de13a881 602 errlog(1, "can not read mail");
86e4d161 603
f734c0ae
SS
604 if (LIST_EMPTY(&queue.queue))
605 errlogx(1, "no recipients");
606
057afad5 607 if (linkspool(&queue) != 0)
de13a881 608 errlog(1, "can not create spools");
86e4d161
MS
609
610 /* From here on the mail is safe. */
611
a0c4afa6 612 if (config.features & DEFER || queue_only)
e0a95d28 613 return (0);
86e4d161 614
28be0b96 615 run_queue(&queue);
86e4d161
MS
616
617 /* NOTREACHED */
e0a95d28 618 return (0);
94389971 619}