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