]> git.ipfire.org Git - people/ms/dma.git/blame - dma.c
partially adopt 34-manpage-defaults.patch: AUTHPATH is not set by default
[people/ms/dma.git] / dma.c
CommitLineData
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 62static void deliver(struct qitem *);
86e4d161
MS
63
64struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
fd1de51a 65struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
86e4d161 66struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
249ee966 67char username[USERNAME_SIZE];
de13a881 68const char *logident_base;
249ee966 69char errmsg[ERRMSG_SIZE];
86e4d161 70
87bdbc01 71static int daemonize = 1;
dd561a09 72
a0c4afa6
SS
73struct config config = {
74 .smarthost = NULL,
75 .port = 25,
db93ed9b 76 .aliases = "/etc/aliases",
a0c4afa6 77 .spooldir = "/var/spool/dma",
a0c4afa6
SS
78 .authpath = NULL,
79 .certfile = NULL,
80 .features = 0,
81 .mailname = NULL,
a0c4afa6
SS
82};
83
84
247523d6
SS
85static void
86sighup_handler(int signo)
87{
88 (void)signo; /* so that gcc doesn't complain */
89}
90
86e4d161 91static char *
057afad5 92set_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
118static int
119read_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 137int
057afad5 138add_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
199out:
200 free(it->addr);
201 free(it);
e0a95d28 202 return (-1);
86e4d161
MS
203}
204
e0a95d28
MS
205static struct qitem *
206go_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));
86e4d161
MS
219 sa.sa_handler = SIG_IGN;
220 sigaction(SIGCHLD, &sa, NULL);
221
94389971 222 LIST_FOREACH(it, &queue->queue, next) {
e0a95d28 223 /* No need to fork for the last dest */
87bdbc01
SS
224 if (LIST_NEXT(it, next) == NULL)
225 goto retit;
e0a95d28 226
86e4d161
MS
227 pid = fork();
228 switch (pid) {
229 case -1:
230 syslog(LOG_ERR, "can not fork: %m");
231 exit(1);
232 break;
233
234 case 0:
235 /*
236 * Child:
237 *
238 * return and deliver mail
239 */
87bdbc01
SS
240retit:
241 /*
9aec6ad8 242 * If necessary, acquire the queue and * mail files.
87bdbc01
SS
243 * If this fails, we probably were raced by another
244 * process.
245 */
4d5af2b0 246 setlogident("%s", it->queueid);
9aec6ad8 247 if (acquirespool(it) < 0)
87bdbc01
SS
248 exit(1);
249 dropspool(queue, it);
e0a95d28 250 return (it);
86e4d161
MS
251
252 default:
253 /*
254 * Parent:
255 *
256 * fork next child
257 */
258 break;
259 }
260 }
261
262 syslog(LOG_CRIT, "reached dead code");
263 exit(1);
264}
265
86e4d161 266static void
e0a95d28 267deliver(struct qitem *it)
86e4d161
MS
268{
269 int error;
f54b5114 270 unsigned int backoff = MIN_RETRY;
86e4d161
MS
271 struct timeval now;
272 struct stat st;
273
249ee966
SS
274 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
275
86e4d161 276retry:
4d5af2b0 277 syslog(LOG_INFO, "trying delivery");
86e4d161 278
e0a95d28 279 if (it->remote)
249ee966 280 error = deliver_remote(it);
e0a95d28 281 else
249ee966 282 error = deliver_local(it);
86e4d161
MS
283
284 switch (error) {
285 case 0:
fd1de51a 286 delqueue(it);
4d5af2b0 287 syslog(LOG_INFO, "delivery successful");
86e4d161
MS
288 exit(0);
289
290 case 1:
291 if (stat(it->queuefn, &st) != 0) {
4d5af2b0 292 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
86e4d161
MS
293 exit(1);
294 }
295 if (gettimeofday(&now, NULL) == 0 &&
2fbb30b2 296 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
249ee966 297 snprintf(errmsg, sizeof(errmsg),
e0a95d28 298 "Could not deliver for the last %d seconds. Giving up.",
a43346d5 299 MAX_TIMEOUT);
86e4d161
MS
300 goto bounce;
301 }
0de88752
SS
302 if (sleep(backoff) == 0) {
303 /* pick the next backoff between [1.5, 2.5) times backoff */
304 backoff = backoff + backoff / 2 + random() % backoff;
305 if (backoff > MAX_RETRY)
306 backoff = MAX_RETRY;
307 }
86e4d161
MS
308 goto retry;
309
310 case -1:
311 default:
312 break;
313 }
314
315bounce:
e0a95d28 316 bounce(it, errmsg);
86e4d161
MS
317 /* NOTREACHED */
318}
319
28be0b96 320void
86e4d161
MS
321run_queue(struct queue *queue)
322{
e0a95d28
MS
323 struct qitem *it;
324
86e4d161
MS
325 if (LIST_EMPTY(&queue->queue))
326 return;
327
e0a95d28
MS
328 it = go_background(queue);
329 deliver(it);
86e4d161
MS
330 /* NOTREACHED */
331}
332
333static void
334show_queue(struct queue *queue)
335{
336 struct qitem *it;
87bdbc01 337 int locked = 0; /* XXX */
86e4d161
MS
338
339 if (LIST_EMPTY(&queue->queue)) {
340 printf("Mail queue is empty\n");
341 return;
342 }
343
344 LIST_FOREACH(it, &queue->queue, next) {
5896fefc
SS
345 printf("ID\t: %s%s\n"
346 "From\t: %s\n"
553ab44c 347 "To\t: %s\n",
5896fefc 348 it->queueid,
87bdbc01 349 locked ? "*" : "",
5896fefc 350 it->sender, it->addr);
553ab44c
SS
351
352 if (LIST_NEXT(it, next) != NULL)
353 printf("--\n");
86e4d161
MS
354 }
355}
356
357/*
358 * TODO:
359 *
360 * - alias processing
361 * - use group permissions
362 * - proper sysexit codes
363 */
364
e0a95d28
MS
365int
366main(int argc, char **argv)
86e4d161 367{
247523d6 368 struct sigaction act;
86e4d161 369 char *sender = NULL;
86e4d161 370 struct queue queue;
86e4d161 371 int i, ch;
bca4c72a 372 int nodot = 0, doqueue = 0, showq = 0, queue_only = 0;
f734c0ae 373 int recp_from_header = 0;
86e4d161
MS
374
375 atexit(deltmp);
ee613428 376 init_random();
6f5efe4f
SS
377
378 bzero(&queue, sizeof(queue));
86e4d161 379 LIST_INIT(&queue.queue);
86e4d161 380
0f779ba6
SS
381 if (strcmp(argv[0], "mailq") == 0) {
382 argv++; argc--;
383 showq = 1;
384 if (argc != 0)
385 errx(1, "invalid arguments");
386 goto skipopts;
387 }
388
87fa1cc5 389 opterr = 0;
f734c0ae 390 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
391 switch (ch) {
392 case 'A':
393 /* -AX is being ignored, except for -A{c,m} */
394 if (optarg[0] == 'c' || optarg[0] == 'm') {
395 break;
396 }
397 /* else FALLTRHOUGH */
398 case 'b':
399 /* -bX is being ignored, except for -bp */
400 if (optarg[0] == 'p') {
401 showq = 1;
402 break;
bca4c72a
SS
403 } else if (optarg[0] == 'q') {
404 queue_only = 1;
405 break;
86e4d161
MS
406 }
407 /* else FALLTRHOUGH */
408 case 'D':
409 daemonize = 0;
410 break;
411 case 'L':
4d5af2b0 412 logident_base = optarg;
86e4d161
MS
413 break;
414 case 'f':
415 case 'r':
416 sender = optarg;
417 break;
418
f734c0ae
SS
419 case 't':
420 recp_from_header = 1;
421 break;
422
86e4d161
MS
423 case 'o':
424 /* -oX is being ignored, except for -oi */
425 if (optarg[0] != 'i')
426 break;
427 /* else FALLTRHOUGH */
87fa1cc5
MS
428 case 'O':
429 break;
86e4d161
MS
430 case 'i':
431 nodot = 1;
432 break;
433
434 case 'q':
435 doqueue = 1;
436 break;
437
b6bb4e2d
SS
438 /* Ignored options */
439 case 'B':
440 case 'C':
441 case 'd':
442 case 'F':
443 case 'h':
444 case 'N':
445 case 'n':
446 case 'R':
447 case 'U':
448 case 'V':
449 case 'v':
450 case 'X':
451 break;
452
f514a0d9
SS
453 case ':':
454 if (optopt == 'q') {
455 doqueue = 1;
456 break;
457 }
458 /* FALLTHROUGH */
459
86e4d161 460 default:
f514a0d9 461 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
86e4d161
MS
462 exit(1);
463 }
464 }
465 argc -= optind;
466 argv += optind;
87fa1cc5 467 opterr = 1;
86e4d161 468
8d291e4d
SS
469 if (argc != 0 && (showq || doqueue))
470 errx(1, "sending mail and queue operations are mutually exclusive");
471
472 if (showq + doqueue > 1)
473 errx(1, "conflicting queue operations");
474
0f779ba6 475skipopts:
4d5af2b0
SS
476 if (logident_base == NULL)
477 logident_base = "dma";
478 setlogident(NULL);
dd561a09 479 set_username();
86e4d161 480
4602c807
SS
481 /* XXX fork root here */
482
247523d6
SS
483 act.sa_handler = sighup_handler;
484 act.sa_flags = 0;
485 sigemptyset(&act.sa_mask);
486 if (sigaction(SIGHUP, &act, NULL) != 0)
487 syslog(LOG_WARNING, "can not set signal handler: %m");
488
a0c4afa6 489 parse_conf(CONF_PATH);
86e4d161 490
a0c4afa6
SS
491 if (config.authpath != NULL)
492 parse_authfile(config.authpath);
86e4d161
MS
493
494 if (showq) {
6f5efe4f 495 if (load_queue(&queue) < 0)
de13a881 496 errlog(1, "can not load queue");
6f5efe4f 497 show_queue(&queue);
e0a95d28 498 return (0);
86e4d161
MS
499 }
500
501 if (doqueue) {
6f5efe4f 502 if (load_queue(&queue) < 0)
de13a881 503 errlog(1, "can not load queue");
6f5efe4f 504 run_queue(&queue);
e0a95d28 505 return (0);
86e4d161
MS
506 }
507
e0a95d28 508 if (read_aliases() != 0)
a0c4afa6 509 errlog(1, "can not read aliases file `%s'", config.aliases);
86e4d161 510
057afad5 511 if ((sender = set_from(&queue, sender)) == NULL)
de13a881 512 errlog(1, NULL);
86e4d161 513
f734c0ae
SS
514 if (newspoolf(&queue) != 0)
515 errlog(1, "can not create temp file");
516
517 setlogident("%s", queue.id);
518
86e4d161 519 for (i = 0; i < argc; i++) {
057afad5 520 if (add_recp(&queue, argv[i], 1) != 0)
de13a881 521 errlogx(1, "invalid recipient `%s'", argv[i]);
86e4d161
MS
522 }
523
f734c0ae 524 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
de13a881 525 errlogx(1, "no recipients");
86e4d161 526
f734c0ae 527 if (readmail(&queue, nodot, recp_from_header) != 0)
de13a881 528 errlog(1, "can not read mail");
86e4d161 529
f734c0ae
SS
530 if (LIST_EMPTY(&queue.queue))
531 errlogx(1, "no recipients");
532
057afad5 533 if (linkspool(&queue) != 0)
de13a881 534 errlog(1, "can not create spools");
86e4d161
MS
535
536 /* From here on the mail is safe. */
537
a0c4afa6 538 if (config.features & DEFER || queue_only)
e0a95d28 539 return (0);
86e4d161 540
28be0b96 541 run_queue(&queue);
86e4d161
MS
542
543 /* NOTREACHED */
e0a95d28 544 return (0);
94389971 545}