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