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