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