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