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