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