]> git.ipfire.org Git - people/ms/dma.git/blob - dma.c
Linux -Wall fixes
[people/ms/dma.git] / dma.c
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.
33 */
34
35 #include "dfcompat.h"
36
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/wait.h>
43
44 #include <dirent.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <inttypes.h>
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
62 static void deliver(struct qitem *);
63
64 struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
65 struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
66 struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
67 struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
68 const char *username;
69 const char *logident_base;
70
71 static int daemonize = 1;
72
73 struct 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
87 static char *
88 set_from(struct queue *queue, const char *osender)
89 {
90 struct virtuser *v;
91 char *sender;
92
93 if ((config.features & VIRTUAL) != 0) {
94 SLIST_FOREACH(v, &virtusers, next) {
95 if (strcmp(v->login, username) == 0) {
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)
107 return (NULL);
108 } else if (getenv("EMAIL") != NULL) {
109 sender = strdup(getenv("EMAIL"));
110 if (sender == NULL)
111 return (NULL);
112 } else {
113 if (asprintf(&sender, "%s@%s", username, hostname()) <= 0)
114 return (NULL);
115 }
116
117 if (strchr(sender, '\n') != NULL) {
118 errno = EINVAL;
119 return (NULL);
120 }
121
122 out:
123 queue->sender = sender;
124 return (sender);
125 }
126
127 static int
128 read_aliases(void)
129 {
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 }
140 if (yyparse())
141 return (-1); /* fatal error, probably malloc() */
142 fclose(yyin);
143 return (0);
144 }
145
146 int
147 add_recp(struct queue *queue, const char *str, int expand)
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)
158 return (-1);
159 it->addr = strdup(str);
160 if (it->addr == NULL)
161 return (-1);
162
163 it->sender = queue->sender;
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);
175 return (0);
176 }
177 }
178 LIST_INSERT_HEAD(&queue->queue, it, next);
179 if (strrchr(it->addr, '@') == NULL) {
180 it->remote = 0;
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) {
186 if (add_recp(queue, sit->str, 1) != 0)
187 return (-1);
188 }
189 aliased = 1;
190 }
191 if (aliased) {
192 LIST_REMOVE(it, next);
193 } else {
194 /* Local destination, check */
195 pw = getpwnam(it->addr);
196 if (pw == NULL)
197 goto out;
198 /* XXX read .forward */
199 endpwent();
200 }
201 }
202 } else {
203 it->remote = 1;
204 }
205
206 return (0);
207
208 out:
209 free(it->addr);
210 free(it);
211 return (-1);
212 }
213
214 static struct qitem *
215 go_background(struct queue *queue)
216 {
217 struct sigaction sa;
218 struct qitem *it;
219 pid_t pid;
220
221 if (daemonize && daemon(0, 0) != 0) {
222 syslog(LOG_ERR, "can not daemonize: %m");
223 exit(1);
224 }
225 daemonize = 0;
226
227 bzero(&sa, sizeof(sa));
228 sa.sa_flags = SA_NOCLDWAIT;
229 sa.sa_handler = SIG_IGN;
230 sigaction(SIGCHLD, &sa, NULL);
231
232 LIST_FOREACH(it, &queue->queue, next) {
233 /* No need to fork for the last dest */
234 if (LIST_NEXT(it, next) == NULL)
235 goto retit;
236
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 */
250 retit:
251 /*
252 * If necessary, acquire the queue and * mail files.
253 * If this fails, we probably were raced by another
254 * process.
255 */
256 setlogident("%s", it->queueid);
257 if (acquirespool(it) < 0)
258 exit(1);
259 dropspool(queue, it);
260 return (it);
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
276 static void
277 deliver(struct qitem *it)
278 {
279 int error;
280 unsigned int backoff = MIN_RETRY;
281 const char *errmsg = "unknown bounce reason";
282 struct timeval now;
283 struct stat st;
284
285 retry:
286 syslog(LOG_INFO, "trying delivery");
287
288 if (it->remote)
289 error = deliver_remote(it, &errmsg);
290 else
291 error = deliver_local(it, &errmsg);
292
293 switch (error) {
294 case 0:
295 delqueue(it);
296 syslog(LOG_INFO, "delivery successful");
297 exit(0);
298
299 case 1:
300 if (stat(it->queuefn, &st) != 0) {
301 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
302 exit(1);
303 }
304 if (gettimeofday(&now, NULL) == 0 &&
305 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
306 asprintf(__DECONST(void *, &errmsg),
307 "Could not deliver for the last %d seconds. Giving up.",
308 MAX_TIMEOUT);
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
322 bounce:
323 bounce(it, errmsg);
324 /* NOTREACHED */
325 }
326
327 void
328 run_queue(struct queue *queue)
329 {
330 struct qitem *it;
331
332 if (LIST_EMPTY(&queue->queue))
333 return;
334
335 it = go_background(queue);
336 deliver(it);
337 /* NOTREACHED */
338 }
339
340 static void
341 show_queue(struct queue *queue)
342 {
343 struct qitem *it;
344 int locked = 0; /* XXX */
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) {
352 printf("ID\t: %s%s\n"
353 "From\t: %s\n"
354 "To\t: %s\n",
355 it->queueid,
356 locked ? "*" : "",
357 it->sender, it->addr);
358
359 if (LIST_NEXT(it, next) != NULL)
360 printf("--\n");
361 }
362 }
363
364 /*
365 * TODO:
366 *
367 * - alias processing
368 * - use group permissions
369 * - proper sysexit codes
370 */
371
372 int
373 main(int argc, char **argv)
374 {
375 char *sender = NULL;
376 struct queue queue;
377 int i, ch;
378 int nodot = 0, doqueue = 0, showq = 0, queue_only = 0;
379 int recp_from_header = 0;
380
381 atexit(deltmp);
382
383 bzero(&queue, sizeof(queue));
384 LIST_INIT(&queue.queue);
385
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
394 opterr = 0;
395 while ((ch = getopt(argc, argv, ":A:b:B:C:d:Df:F:h:iL:N:no:O:q:r:R:tUV:vX:")) != -1) {
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;
408 } else if (optarg[0] == 'q') {
409 queue_only = 1;
410 break;
411 }
412 /* else FALLTRHOUGH */
413 case 'D':
414 daemonize = 0;
415 break;
416 case 'L':
417 logident_base = optarg;
418 break;
419 case 'f':
420 case 'r':
421 sender = optarg;
422 break;
423
424 case 't':
425 recp_from_header = 1;
426 break;
427
428 case 'o':
429 /* -oX is being ignored, except for -oi */
430 if (optarg[0] != 'i')
431 break;
432 /* else FALLTRHOUGH */
433 case 'O':
434 break;
435 case 'i':
436 nodot = 1;
437 break;
438
439 case 'q':
440 doqueue = 1;
441 break;
442
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
458 case ':':
459 if (optopt == 'q') {
460 doqueue = 1;
461 break;
462 }
463 /* FALLTHROUGH */
464
465 default:
466 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
467 exit(1);
468 }
469 }
470 argc -= optind;
471 argv += optind;
472 opterr = 1;
473
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
480 skipopts:
481 if (logident_base == NULL)
482 logident_base = "dma";
483 setlogident(NULL);
484 set_username();
485
486 /* XXX fork root here */
487
488 parse_conf(CONF_PATH);
489
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);
494 }
495
496 if (config.authpath != NULL)
497 parse_authfile(config.authpath);
498
499 if (showq) {
500 if (load_queue(&queue) < 0)
501 errlog(1, "can not load queue");
502 show_queue(&queue);
503 return (0);
504 }
505
506 if (doqueue) {
507 if (load_queue(&queue) < 0)
508 errlog(1, "can not load queue");
509 run_queue(&queue);
510 return (0);
511 }
512
513 if (read_aliases() != 0)
514 errlog(1, "can not read aliases file `%s'", config.aliases);
515
516 if ((sender = set_from(&queue, sender)) == NULL)
517 errlog(1, NULL);
518
519 if (newspoolf(&queue) != 0)
520 errlog(1, "can not create temp file");
521
522 setlogident("%s", queue.id);
523
524 for (i = 0; i < argc; i++) {
525 if (add_recp(&queue, argv[i], 1) != 0)
526 errlogx(1, "invalid recipient `%s'", argv[i]);
527 }
528
529 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
530 errlogx(1, "no recipients");
531
532 if (readmail(&queue, nodot, recp_from_header) != 0)
533 errlog(1, "can not read mail");
534
535 if (LIST_EMPTY(&queue.queue))
536 errlogx(1, "no recipients");
537
538 if (linkspool(&queue) != 0)
539 errlog(1, "can not create spools");
540
541 /* From here on the mail is safe. */
542
543 if (config.features & DEFER || queue_only)
544 return (0);
545
546 run_queue(&queue);
547
548 /* NOTREACHED */
549 return (0);
550 }