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