]>
Commit | Line | Data |
---|---|---|
86e4d161 | 1 | /* |
e458e5f0 | 2 | * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>. |
86e4d161 MS |
3 | * Copyright (c) 2008 The DragonFly Project. All rights reserved. |
4 | * | |
5 | * This code is derived from software contributed to The DragonFly Project | |
e458e5f0 | 6 | * by Simon Schubert <2@0x2c.org>. |
86e4d161 MS |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. Neither the name of The DragonFly Project nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific, prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
86e4d161 MS |
34 | */ |
35 | ||
9b921550 SS |
36 | #include "dfcompat.h" |
37 | ||
86e4d161 | 38 | #include <sys/param.h> |
4dd56cbc | 39 | #include <sys/types.h> |
86e4d161 MS |
40 | #include <sys/queue.h> |
41 | #include <sys/stat.h> | |
9b921550 | 42 | #include <sys/time.h> |
c507d897 | 43 | #include <sys/wait.h> |
86e4d161 | 44 | |
86e4d161 MS |
45 | #include <dirent.h> |
46 | #include <err.h> | |
47 | #include <errno.h> | |
48 | #include <fcntl.h> | |
49 | #include <inttypes.h> | |
1cc2afab | 50 | #include <libgen.h> |
86e4d161 MS |
51 | #include <paths.h> |
52 | #include <pwd.h> | |
53 | #include <signal.h> | |
54 | #include <stdarg.h> | |
55 | #include <stdio.h> | |
56 | #include <stdlib.h> | |
57 | #include <string.h> | |
58 | #include <syslog.h> | |
59 | #include <unistd.h> | |
60 | ||
61 | #include "dma.h" | |
62 | ||
63 | ||
e0a95d28 | 64 | static void deliver(struct qitem *); |
86e4d161 MS |
65 | |
66 | struct aliases aliases = LIST_HEAD_INITIALIZER(aliases); | |
fd1de51a | 67 | struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs); |
86e4d161 | 68 | struct authusers authusers = LIST_HEAD_INITIALIZER(authusers); |
249ee966 | 69 | char username[USERNAME_SIZE]; |
75fdf182 | 70 | uid_t useruid; |
de13a881 | 71 | const char *logident_base; |
249ee966 | 72 | char errmsg[ERRMSG_SIZE]; |
86e4d161 | 73 | |
87bdbc01 | 74 | static int daemonize = 1; |
e33faafe | 75 | static int doqueue = 0; |
dd561a09 | 76 | |
a0c4afa6 SS |
77 | struct config config = { |
78 | .smarthost = NULL, | |
79 | .port = 25, | |
db93ed9b | 80 | .aliases = "/etc/aliases", |
a0c4afa6 | 81 | .spooldir = "/var/spool/dma", |
a0c4afa6 SS |
82 | .authpath = NULL, |
83 | .certfile = NULL, | |
84 | .features = 0, | |
85 | .mailname = NULL, | |
5bc72de8 SS |
86 | .masquerade_host = NULL, |
87 | .masquerade_user = NULL, | |
a0c4afa6 SS |
88 | }; |
89 | ||
90 | ||
247523d6 SS |
91 | static void |
92 | sighup_handler(int signo) | |
93 | { | |
94 | (void)signo; /* so that gcc doesn't complain */ | |
95 | } | |
96 | ||
86e4d161 | 97 | static char * |
057afad5 | 98 | set_from(struct queue *queue, const char *osender) |
86e4d161 | 99 | { |
0ecb0ebe | 100 | const char *addr; |
86e4d161 MS |
101 | char *sender; |
102 | ||
86e4d161 | 103 | if (osender) { |
0ecb0ebe | 104 | addr = osender; |
09a2b07e | 105 | } else if (getenv("EMAIL") != NULL) { |
0ecb0ebe | 106 | addr = getenv("EMAIL"); |
86e4d161 | 107 | } else { |
0ecb0ebe SS |
108 | if (config.masquerade_user) |
109 | addr = config.masquerade_user; | |
110 | else | |
111 | addr = username; | |
112 | } | |
113 | ||
114 | if (!strchr(addr, '@')) { | |
5bc72de8 SS |
115 | const char *from_host = hostname(); |
116 | ||
5bc72de8 SS |
117 | if (config.masquerade_host) |
118 | from_host = config.masquerade_host; | |
0ecb0ebe SS |
119 | |
120 | if (asprintf(&sender, "%s@%s", addr, from_host) <= 0) | |
121 | return (NULL); | |
122 | } else { | |
123 | sender = strdup(addr); | |
124 | if (sender == NULL) | |
e0a95d28 | 125 | return (NULL); |
86e4d161 MS |
126 | } |
127 | ||
128 | if (strchr(sender, '\n') != NULL) { | |
129 | errno = EINVAL; | |
e0a95d28 | 130 | return (NULL); |
86e4d161 MS |
131 | } |
132 | ||
057afad5 | 133 | queue->sender = sender; |
e0a95d28 | 134 | return (sender); |
86e4d161 MS |
135 | } |
136 | ||
137 | static int | |
138 | read_aliases(void) | |
139 | { | |
a0c4afa6 SS |
140 | yyin = fopen(config.aliases, "r"); |
141 | if (yyin == NULL) { | |
142 | /* | |
143 | * Non-existing aliases file is not a fatal error | |
144 | */ | |
145 | if (errno == ENOENT) | |
146 | return (0); | |
147 | /* Other problems are. */ | |
148 | return (-1); | |
149 | } | |
86e4d161 | 150 | if (yyparse()) |
e0a95d28 | 151 | return (-1); /* fatal error, probably malloc() */ |
86e4d161 | 152 | fclose(yyin); |
e0a95d28 | 153 | return (0); |
86e4d161 MS |
154 | } |
155 | ||
a803c7a6 SS |
156 | static int |
157 | do_alias(struct queue *queue, const char *addr) | |
158 | { | |
159 | struct alias *al; | |
160 | struct stritem *sit; | |
161 | int aliased = 0; | |
162 | ||
163 | LIST_FOREACH(al, &aliases, next) { | |
164 | if (strcmp(al->alias, addr) != 0) | |
165 | continue; | |
166 | SLIST_FOREACH(sit, &al->dests, next) { | |
167 | if (add_recp(queue, sit->str, EXPAND_ADDR) != 0) | |
168 | return (-1); | |
169 | } | |
170 | aliased = 1; | |
171 | } | |
172 | ||
173 | return (aliased); | |
174 | } | |
175 | ||
fd1de51a | 176 | int |
057afad5 | 177 | add_recp(struct queue *queue, const char *str, int expand) |
86e4d161 MS |
178 | { |
179 | struct qitem *it, *tit; | |
86e4d161 MS |
180 | struct passwd *pw; |
181 | char *host; | |
182 | int aliased = 0; | |
183 | ||
184 | it = calloc(1, sizeof(*it)); | |
185 | if (it == NULL) | |
e0a95d28 | 186 | return (-1); |
86e4d161 MS |
187 | it->addr = strdup(str); |
188 | if (it->addr == NULL) | |
e0a95d28 | 189 | return (-1); |
86e4d161 | 190 | |
057afad5 | 191 | it->sender = queue->sender; |
86e4d161 MS |
192 | host = strrchr(it->addr, '@'); |
193 | if (host != NULL && | |
194 | (strcmp(host + 1, hostname()) == 0 || | |
195 | strcmp(host + 1, "localhost") == 0)) { | |
196 | *host = 0; | |
197 | } | |
198 | LIST_FOREACH(tit, &queue->queue, next) { | |
199 | /* weed out duplicate dests */ | |
200 | if (strcmp(tit->addr, it->addr) == 0) { | |
201 | free(it->addr); | |
202 | free(it); | |
e0a95d28 | 203 | return (0); |
86e4d161 MS |
204 | } |
205 | } | |
206 | LIST_INSERT_HEAD(&queue->queue, it, next); | |
e882bde8 BD |
207 | |
208 | /** | |
209 | * Do local delivery if there is no @. | |
210 | * Do not do local delivery when NULLCLIENT is set. | |
211 | */ | |
212 | if (strrchr(it->addr, '@') == NULL && (config.features & NULLCLIENT) == 0) { | |
e0a95d28 | 213 | it->remote = 0; |
86e4d161 | 214 | if (expand) { |
a803c7a6 SS |
215 | aliased = do_alias(queue, it->addr); |
216 | if (!aliased && expand == EXPAND_WILDCARD) | |
217 | aliased = do_alias(queue, "*"); | |
218 | if (aliased < 0) | |
219 | return (-1); | |
86e4d161 MS |
220 | if (aliased) { |
221 | LIST_REMOVE(it, next); | |
222 | } else { | |
e0a95d28 | 223 | /* Local destination, check */ |
86e4d161 MS |
224 | pw = getpwnam(it->addr); |
225 | if (pw == NULL) | |
226 | goto out; | |
8d291e4d | 227 | /* XXX read .forward */ |
e0a95d28 | 228 | endpwent(); |
86e4d161 MS |
229 | } |
230 | } | |
231 | } else { | |
e0a95d28 | 232 | it->remote = 1; |
86e4d161 MS |
233 | } |
234 | ||
e0a95d28 | 235 | return (0); |
86e4d161 MS |
236 | |
237 | out: | |
238 | free(it->addr); | |
239 | free(it); | |
e0a95d28 | 240 | return (-1); |
86e4d161 MS |
241 | } |
242 | ||
e0a95d28 MS |
243 | static struct qitem * |
244 | go_background(struct queue *queue) | |
86e4d161 MS |
245 | { |
246 | struct sigaction sa; | |
247 | struct qitem *it; | |
248 | pid_t pid; | |
249 | ||
250 | if (daemonize && daemon(0, 0) != 0) { | |
e0a95d28 | 251 | syslog(LOG_ERR, "can not daemonize: %m"); |
de196e33 | 252 | exit(EX_OSERR); |
86e4d161 MS |
253 | } |
254 | daemonize = 0; | |
e0a95d28 | 255 | |
86e4d161 | 256 | bzero(&sa, sizeof(sa)); |
86e4d161 MS |
257 | sa.sa_handler = SIG_IGN; |
258 | sigaction(SIGCHLD, &sa, NULL); | |
259 | ||
94389971 | 260 | LIST_FOREACH(it, &queue->queue, next) { |
e0a95d28 | 261 | /* No need to fork for the last dest */ |
87bdbc01 SS |
262 | if (LIST_NEXT(it, next) == NULL) |
263 | goto retit; | |
e0a95d28 | 264 | |
86e4d161 MS |
265 | pid = fork(); |
266 | switch (pid) { | |
267 | case -1: | |
268 | syslog(LOG_ERR, "can not fork: %m"); | |
de196e33 | 269 | exit(EX_OSERR); |
86e4d161 MS |
270 | break; |
271 | ||
272 | case 0: | |
273 | /* | |
274 | * Child: | |
275 | * | |
276 | * return and deliver mail | |
277 | */ | |
87bdbc01 SS |
278 | retit: |
279 | /* | |
9aec6ad8 | 280 | * If necessary, acquire the queue and * mail files. |
87bdbc01 | 281 | * If this fails, we probably were raced by another |
e33faafe SS |
282 | * process. It is okay to be raced if we're supposed |
283 | * to flush the queue. | |
87bdbc01 | 284 | */ |
4d5af2b0 | 285 | setlogident("%s", it->queueid); |
e33faafe SS |
286 | switch (acquirespool(it)) { |
287 | case 0: | |
288 | break; | |
289 | case 1: | |
290 | if (doqueue) | |
de196e33 | 291 | exit(EX_OK); |
e33faafe | 292 | syslog(LOG_WARNING, "could not lock queue file"); |
de196e33 | 293 | exit(EX_SOFTWARE); |
e33faafe | 294 | default: |
de196e33 | 295 | exit(EX_SOFTWARE); |
e33faafe | 296 | } |
87bdbc01 | 297 | dropspool(queue, it); |
e0a95d28 | 298 | return (it); |
86e4d161 MS |
299 | |
300 | default: | |
301 | /* | |
302 | * Parent: | |
303 | * | |
304 | * fork next child | |
305 | */ | |
306 | break; | |
307 | } | |
308 | } | |
309 | ||
310 | syslog(LOG_CRIT, "reached dead code"); | |
de196e33 | 311 | exit(EX_SOFTWARE); |
86e4d161 MS |
312 | } |
313 | ||
86e4d161 | 314 | static void |
e0a95d28 | 315 | deliver(struct qitem *it) |
86e4d161 MS |
316 | { |
317 | int error; | |
3686aecc | 318 | unsigned int backoff = MIN_RETRY, slept; |
86e4d161 MS |
319 | struct timeval now; |
320 | struct stat st; | |
321 | ||
249ee966 SS |
322 | snprintf(errmsg, sizeof(errmsg), "unknown bounce reason"); |
323 | ||
86e4d161 | 324 | retry: |
af455b4c | 325 | syslog(LOG_INFO, "<%s> trying delivery", it->addr); |
86e4d161 | 326 | |
e0a95d28 | 327 | if (it->remote) |
249ee966 | 328 | error = deliver_remote(it); |
e0a95d28 | 329 | else |
249ee966 | 330 | error = deliver_local(it); |
86e4d161 MS |
331 | |
332 | switch (error) { | |
333 | case 0: | |
fd1de51a | 334 | delqueue(it); |
af455b4c | 335 | syslog(LOG_INFO, "<%s> delivery successful", it->addr); |
de196e33 | 336 | exit(EX_OK); |
86e4d161 MS |
337 | |
338 | case 1: | |
339 | if (stat(it->queuefn, &st) != 0) { | |
4d5af2b0 | 340 | syslog(LOG_ERR, "lost queue file `%s'", it->queuefn); |
de196e33 | 341 | exit(EX_SOFTWARE); |
86e4d161 MS |
342 | } |
343 | if (gettimeofday(&now, NULL) == 0 && | |
2fbb30b2 | 344 | (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) { |
249ee966 | 345 | snprintf(errmsg, sizeof(errmsg), |
e0a95d28 | 346 | "Could not deliver for the last %d seconds. Giving up.", |
a43346d5 | 347 | MAX_TIMEOUT); |
86e4d161 MS |
348 | goto bounce; |
349 | } | |
3686aecc SS |
350 | for (slept = 0; slept < backoff;) { |
351 | slept += SLEEP_TIMEOUT - sleep(SLEEP_TIMEOUT); | |
352 | if (flushqueue_since(slept)) { | |
353 | backoff = MIN_RETRY; | |
354 | goto retry; | |
355 | } | |
356 | } | |
357 | if (slept >= backoff) { | |
0de88752 SS |
358 | /* pick the next backoff between [1.5, 2.5) times backoff */ |
359 | backoff = backoff + backoff / 2 + random() % backoff; | |
360 | if (backoff > MAX_RETRY) | |
361 | backoff = MAX_RETRY; | |
362 | } | |
86e4d161 MS |
363 | goto retry; |
364 | ||
365 | case -1: | |
366 | default: | |
367 | break; | |
368 | } | |
369 | ||
370 | bounce: | |
e0a95d28 | 371 | bounce(it, errmsg); |
86e4d161 MS |
372 | /* NOTREACHED */ |
373 | } | |
374 | ||
28be0b96 | 375 | void |
86e4d161 MS |
376 | run_queue(struct queue *queue) |
377 | { | |
e0a95d28 MS |
378 | struct qitem *it; |
379 | ||
86e4d161 MS |
380 | if (LIST_EMPTY(&queue->queue)) |
381 | return; | |
382 | ||
e0a95d28 MS |
383 | it = go_background(queue); |
384 | deliver(it); | |
86e4d161 MS |
385 | /* NOTREACHED */ |
386 | } | |
387 | ||
388 | static void | |
389 | show_queue(struct queue *queue) | |
390 | { | |
391 | struct qitem *it; | |
87bdbc01 | 392 | int locked = 0; /* XXX */ |
86e4d161 MS |
393 | |
394 | if (LIST_EMPTY(&queue->queue)) { | |
395 | printf("Mail queue is empty\n"); | |
396 | return; | |
397 | } | |
398 | ||
399 | LIST_FOREACH(it, &queue->queue, next) { | |
5896fefc SS |
400 | printf("ID\t: %s%s\n" |
401 | "From\t: %s\n" | |
553ab44c | 402 | "To\t: %s\n", |
5896fefc | 403 | it->queueid, |
87bdbc01 | 404 | locked ? "*" : "", |
5896fefc | 405 | it->sender, it->addr); |
553ab44c SS |
406 | |
407 | if (LIST_NEXT(it, next) != NULL) | |
408 | printf("--\n"); | |
86e4d161 MS |
409 | } |
410 | } | |
411 | ||
412 | /* | |
413 | * TODO: | |
414 | * | |
415 | * - alias processing | |
416 | * - use group permissions | |
417 | * - proper sysexit codes | |
418 | */ | |
419 | ||
e0a95d28 MS |
420 | int |
421 | main(int argc, char **argv) | |
86e4d161 | 422 | { |
247523d6 | 423 | struct sigaction act; |
86e4d161 | 424 | char *sender = NULL; |
86e4d161 | 425 | struct queue queue; |
86e4d161 | 426 | int i, ch; |
e33faafe | 427 | int nodot = 0, showq = 0, queue_only = 0; |
f734c0ae | 428 | int recp_from_header = 0; |
86e4d161 | 429 | |
75fdf182 SS |
430 | set_username(); |
431 | ||
432 | /* | |
433 | * We never run as root. If called by root, drop permissions | |
434 | * to the mail user. | |
435 | */ | |
436 | if (geteuid() == 0 || getuid() == 0) { | |
437 | struct passwd *pw; | |
438 | ||
04947c64 | 439 | errno = 0; |
75fdf182 | 440 | pw = getpwnam(DMA_ROOT_USER); |
04947c64 EM |
441 | if (pw == NULL) { |
442 | if (errno == 0) | |
de196e33 | 443 | errx(EX_CONFIG, "user '%s' not found", DMA_ROOT_USER); |
04947c64 | 444 | else |
de196e33 | 445 | err(EX_OSERR, "cannot drop root privileges"); |
04947c64 | 446 | } |
75fdf182 SS |
447 | |
448 | if (setuid(pw->pw_uid) != 0) | |
de196e33 | 449 | err(EX_OSERR, "cannot drop root privileges"); |
75fdf182 SS |
450 | |
451 | if (geteuid() == 0 || getuid() == 0) | |
de196e33 | 452 | errx(EX_OSERR, "cannot drop root privileges"); |
75fdf182 SS |
453 | } |
454 | ||
86e4d161 | 455 | atexit(deltmp); |
ee613428 | 456 | init_random(); |
6f5efe4f SS |
457 | |
458 | bzero(&queue, sizeof(queue)); | |
86e4d161 | 459 | LIST_INIT(&queue.queue); |
86e4d161 | 460 | |
1cc2afab | 461 | if (strcmp(basename(argv[0]), "mailq") == 0) { |
0f779ba6 SS |
462 | argv++; argc--; |
463 | showq = 1; | |
464 | if (argc != 0) | |
de196e33 | 465 | errx(EX_USAGE, "invalid arguments"); |
0f779ba6 | 466 | goto skipopts; |
e9c1c02f SS |
467 | } else if (strcmp(argv[0], "newaliases") == 0) { |
468 | logident_base = "dma"; | |
469 | setlogident(NULL); | |
470 | ||
471 | if (read_aliases() != 0) | |
de196e33 M |
472 | errx(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases); |
473 | exit(EX_OK); | |
0f779ba6 SS |
474 | } |
475 | ||
87fa1cc5 | 476 | opterr = 0; |
f734c0ae | 477 | 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 |
478 | switch (ch) { |
479 | case 'A': | |
480 | /* -AX is being ignored, except for -A{c,m} */ | |
481 | if (optarg[0] == 'c' || optarg[0] == 'm') { | |
482 | break; | |
483 | } | |
484 | /* else FALLTRHOUGH */ | |
485 | case 'b': | |
486 | /* -bX is being ignored, except for -bp */ | |
487 | if (optarg[0] == 'p') { | |
488 | showq = 1; | |
489 | break; | |
bca4c72a SS |
490 | } else if (optarg[0] == 'q') { |
491 | queue_only = 1; | |
492 | break; | |
86e4d161 MS |
493 | } |
494 | /* else FALLTRHOUGH */ | |
495 | case 'D': | |
496 | daemonize = 0; | |
497 | break; | |
498 | case 'L': | |
4d5af2b0 | 499 | logident_base = optarg; |
86e4d161 MS |
500 | break; |
501 | case 'f': | |
502 | case 'r': | |
503 | sender = optarg; | |
504 | break; | |
505 | ||
f734c0ae SS |
506 | case 't': |
507 | recp_from_header = 1; | |
508 | break; | |
509 | ||
86e4d161 MS |
510 | case 'o': |
511 | /* -oX is being ignored, except for -oi */ | |
512 | if (optarg[0] != 'i') | |
513 | break; | |
514 | /* else FALLTRHOUGH */ | |
87fa1cc5 MS |
515 | case 'O': |
516 | break; | |
86e4d161 MS |
517 | case 'i': |
518 | nodot = 1; | |
519 | break; | |
520 | ||
521 | case 'q': | |
af31dade SS |
522 | /* Don't let getopt slup up other arguments */ |
523 | if (optarg && *optarg == '-') | |
524 | optind--; | |
86e4d161 MS |
525 | doqueue = 1; |
526 | break; | |
527 | ||
b6bb4e2d SS |
528 | /* Ignored options */ |
529 | case 'B': | |
530 | case 'C': | |
531 | case 'd': | |
532 | case 'F': | |
533 | case 'h': | |
534 | case 'N': | |
535 | case 'n': | |
536 | case 'R': | |
537 | case 'U': | |
538 | case 'V': | |
539 | case 'v': | |
540 | case 'X': | |
541 | break; | |
542 | ||
f514a0d9 SS |
543 | case ':': |
544 | if (optopt == 'q') { | |
545 | doqueue = 1; | |
546 | break; | |
547 | } | |
548 | /* FALLTHROUGH */ | |
549 | ||
86e4d161 | 550 | default: |
f514a0d9 | 551 | fprintf(stderr, "invalid argument: `-%c'\n", optopt); |
de196e33 | 552 | exit(EX_USAGE); |
86e4d161 MS |
553 | } |
554 | } | |
555 | argc -= optind; | |
556 | argv += optind; | |
87fa1cc5 | 557 | opterr = 1; |
86e4d161 | 558 | |
8d291e4d | 559 | if (argc != 0 && (showq || doqueue)) |
de196e33 | 560 | errx(EX_USAGE, "sending mail and queue operations are mutually exclusive"); |
8d291e4d SS |
561 | |
562 | if (showq + doqueue > 1) | |
de196e33 | 563 | errx(EX_USAGE, "conflicting queue operations"); |
8d291e4d | 564 | |
0f779ba6 | 565 | skipopts: |
4d5af2b0 SS |
566 | if (logident_base == NULL) |
567 | logident_base = "dma"; | |
568 | setlogident(NULL); | |
4602c807 | 569 | |
247523d6 SS |
570 | act.sa_handler = sighup_handler; |
571 | act.sa_flags = 0; | |
572 | sigemptyset(&act.sa_mask); | |
573 | if (sigaction(SIGHUP, &act, NULL) != 0) | |
574 | syslog(LOG_WARNING, "can not set signal handler: %m"); | |
575 | ||
d84c3daa | 576 | parse_conf(CONF_PATH "/dma.conf"); |
86e4d161 | 577 | |
a0c4afa6 SS |
578 | if (config.authpath != NULL) |
579 | parse_authfile(config.authpath); | |
86e4d161 MS |
580 | |
581 | if (showq) { | |
6f5efe4f | 582 | if (load_queue(&queue) < 0) |
de196e33 | 583 | errlog(EX_NOINPUT, "can not load queue"); |
6f5efe4f | 584 | show_queue(&queue); |
e0a95d28 | 585 | return (0); |
86e4d161 MS |
586 | } |
587 | ||
588 | if (doqueue) { | |
3686aecc | 589 | flushqueue_signal(); |
6f5efe4f | 590 | if (load_queue(&queue) < 0) |
de196e33 | 591 | errlog(EX_NOINPUT, "can not load queue"); |
6f5efe4f | 592 | run_queue(&queue); |
e0a95d28 | 593 | return (0); |
86e4d161 MS |
594 | } |
595 | ||
e0a95d28 | 596 | if (read_aliases() != 0) |
de196e33 | 597 | errlog(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases); |
86e4d161 | 598 | |
057afad5 | 599 | if ((sender = set_from(&queue, sender)) == NULL) |
de196e33 | 600 | errlog(EX_SOFTWARE, NULL); |
86e4d161 | 601 | |
f734c0ae | 602 | if (newspoolf(&queue) != 0) |
de196e33 | 603 | errlog(EX_CANTCREAT, "can not create temp file in `%s'", config.spooldir); |
f734c0ae SS |
604 | |
605 | setlogident("%s", queue.id); | |
606 | ||
86e4d161 | 607 | for (i = 0; i < argc; i++) { |
a803c7a6 | 608 | if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0) |
de196e33 | 609 | errlogx(EX_DATAERR, "invalid recipient `%s'", argv[i]); |
86e4d161 MS |
610 | } |
611 | ||
f734c0ae | 612 | if (LIST_EMPTY(&queue.queue) && !recp_from_header) |
de196e33 | 613 | errlogx(EX_NOINPUT, "no recipients"); |
86e4d161 | 614 | |
f734c0ae | 615 | if (readmail(&queue, nodot, recp_from_header) != 0) |
de196e33 | 616 | errlog(EX_NOINPUT, "can not read mail"); |
86e4d161 | 617 | |
f734c0ae | 618 | if (LIST_EMPTY(&queue.queue)) |
de196e33 | 619 | errlogx(EX_NOINPUT, "no recipients"); |
f734c0ae | 620 | |
057afad5 | 621 | if (linkspool(&queue) != 0) |
de196e33 | 622 | errlog(EX_CANTCREAT, "can not create spools"); |
86e4d161 MS |
623 | |
624 | /* From here on the mail is safe. */ | |
625 | ||
a0c4afa6 | 626 | if (config.features & DEFER || queue_only) |
e0a95d28 | 627 | return (0); |
86e4d161 | 628 | |
28be0b96 | 629 | run_queue(&queue); |
86e4d161 MS |
630 | |
631 | /* NOTREACHED */ | |
e0a95d28 | 632 | return (0); |
94389971 | 633 | } |