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