]> git.ipfire.org Git - people/ms/dma.git/blame - dma.c
newaliases: provide command alias
[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 66struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
249ee966 67char username[USERNAME_SIZE];
75fdf182 68uid_t useruid;
de13a881 69const char *logident_base;
249ee966 70char errmsg[ERRMSG_SIZE];
86e4d161 71
87bdbc01 72static int daemonize = 1;
e33faafe 73static int doqueue = 0;
dd561a09 74
a0c4afa6
SS
75struct config config = {
76 .smarthost = NULL,
77 .port = 25,
db93ed9b 78 .aliases = "/etc/aliases",
a0c4afa6 79 .spooldir = "/var/spool/dma",
a0c4afa6
SS
80 .authpath = NULL,
81 .certfile = NULL,
82 .features = 0,
83 .mailname = NULL,
5bc72de8
SS
84 .masquerade_host = NULL,
85 .masquerade_user = NULL,
a0c4afa6
SS
86};
87
88
247523d6
SS
89static void
90sighup_handler(int signo)
91{
92 (void)signo; /* so that gcc doesn't complain */
93}
94
86e4d161 95static char *
057afad5 96set_from(struct queue *queue, const char *osender)
86e4d161 97{
0ecb0ebe 98 const char *addr;
86e4d161
MS
99 char *sender;
100
86e4d161 101 if (osender) {
0ecb0ebe 102 addr = osender;
09a2b07e 103 } else if (getenv("EMAIL") != NULL) {
0ecb0ebe 104 addr = getenv("EMAIL");
86e4d161 105 } else {
0ecb0ebe
SS
106 if (config.masquerade_user)
107 addr = config.masquerade_user;
108 else
109 addr = username;
110 }
111
112 if (!strchr(addr, '@')) {
5bc72de8
SS
113 const char *from_host = hostname();
114
5bc72de8
SS
115 if (config.masquerade_host)
116 from_host = config.masquerade_host;
0ecb0ebe
SS
117
118 if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
119 return (NULL);
120 } else {
121 sender = strdup(addr);
122 if (sender == NULL)
e0a95d28 123 return (NULL);
86e4d161
MS
124 }
125
126 if (strchr(sender, '\n') != NULL) {
127 errno = EINVAL;
e0a95d28 128 return (NULL);
86e4d161
MS
129 }
130
057afad5 131 queue->sender = sender;
e0a95d28 132 return (sender);
86e4d161
MS
133}
134
135static int
136read_aliases(void)
137{
a0c4afa6
SS
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 }
86e4d161 148 if (yyparse())
e0a95d28 149 return (-1); /* fatal error, probably malloc() */
86e4d161 150 fclose(yyin);
e0a95d28 151 return (0);
86e4d161
MS
152}
153
a803c7a6
SS
154static int
155do_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
fd1de51a 174int
057afad5 175add_recp(struct queue *queue, const char *str, int expand)
86e4d161
MS
176{
177 struct qitem *it, *tit;
86e4d161
MS
178 struct passwd *pw;
179 char *host;
180 int aliased = 0;
181
182 it = calloc(1, sizeof(*it));
183 if (it == NULL)
e0a95d28 184 return (-1);
86e4d161
MS
185 it->addr = strdup(str);
186 if (it->addr == NULL)
e0a95d28 187 return (-1);
86e4d161 188
057afad5 189 it->sender = queue->sender;
86e4d161
MS
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);
e0a95d28 201 return (0);
86e4d161
MS
202 }
203 }
204 LIST_INSERT_HEAD(&queue->queue, it, next);
205 if (strrchr(it->addr, '@') == NULL) {
e0a95d28 206 it->remote = 0;
86e4d161 207 if (expand) {
a803c7a6
SS
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);
86e4d161
MS
213 if (aliased) {
214 LIST_REMOVE(it, next);
215 } else {
e0a95d28 216 /* Local destination, check */
86e4d161
MS
217 pw = getpwnam(it->addr);
218 if (pw == NULL)
219 goto out;
8d291e4d 220 /* XXX read .forward */
e0a95d28 221 endpwent();
86e4d161
MS
222 }
223 }
224 } else {
e0a95d28 225 it->remote = 1;
86e4d161
MS
226 }
227
e0a95d28 228 return (0);
86e4d161
MS
229
230out:
231 free(it->addr);
232 free(it);
e0a95d28 233 return (-1);
86e4d161
MS
234}
235
e0a95d28
MS
236static struct qitem *
237go_background(struct queue *queue)
86e4d161
MS
238{
239 struct sigaction sa;
240 struct qitem *it;
241 pid_t pid;
242
243 if (daemonize && daemon(0, 0) != 0) {
e0a95d28 244 syslog(LOG_ERR, "can not daemonize: %m");
86e4d161
MS
245 exit(1);
246 }
247 daemonize = 0;
e0a95d28 248
86e4d161 249 bzero(&sa, sizeof(sa));
86e4d161
MS
250 sa.sa_handler = SIG_IGN;
251 sigaction(SIGCHLD, &sa, NULL);
252
94389971 253 LIST_FOREACH(it, &queue->queue, next) {
e0a95d28 254 /* No need to fork for the last dest */
87bdbc01
SS
255 if (LIST_NEXT(it, next) == NULL)
256 goto retit;
e0a95d28 257
86e4d161
MS
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 */
87bdbc01
SS
271retit:
272 /*
9aec6ad8 273 * If necessary, acquire the queue and * mail files.
87bdbc01 274 * If this fails, we probably were raced by another
e33faafe
SS
275 * process. It is okay to be raced if we're supposed
276 * to flush the queue.
87bdbc01 277 */
4d5af2b0 278 setlogident("%s", it->queueid);
e33faafe
SS
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");
87bdbc01 286 exit(1);
e33faafe
SS
287 default:
288 exit(1);
289 }
87bdbc01 290 dropspool(queue, it);
e0a95d28 291 return (it);
86e4d161
MS
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
86e4d161 307static void
e0a95d28 308deliver(struct qitem *it)
86e4d161
MS
309{
310 int error;
3686aecc 311 unsigned int backoff = MIN_RETRY, slept;
86e4d161
MS
312 struct timeval now;
313 struct stat st;
314
249ee966
SS
315 snprintf(errmsg, sizeof(errmsg), "unknown bounce reason");
316
86e4d161 317retry:
4d5af2b0 318 syslog(LOG_INFO, "trying delivery");
86e4d161 319
e0a95d28 320 if (it->remote)
249ee966 321 error = deliver_remote(it);
e0a95d28 322 else
249ee966 323 error = deliver_local(it);
86e4d161
MS
324
325 switch (error) {
326 case 0:
fd1de51a 327 delqueue(it);
4d5af2b0 328 syslog(LOG_INFO, "delivery successful");
86e4d161
MS
329 exit(0);
330
331 case 1:
332 if (stat(it->queuefn, &st) != 0) {
4d5af2b0 333 syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
86e4d161
MS
334 exit(1);
335 }
336 if (gettimeofday(&now, NULL) == 0 &&
2fbb30b2 337 (now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
249ee966 338 snprintf(errmsg, sizeof(errmsg),
e0a95d28 339 "Could not deliver for the last %d seconds. Giving up.",
a43346d5 340 MAX_TIMEOUT);
86e4d161
MS
341 goto bounce;
342 }
3686aecc
SS
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) {
0de88752
SS
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 }
86e4d161
MS
356 goto retry;
357
358 case -1:
359 default:
360 break;
361 }
362
363bounce:
e0a95d28 364 bounce(it, errmsg);
86e4d161
MS
365 /* NOTREACHED */
366}
367
28be0b96 368void
86e4d161
MS
369run_queue(struct queue *queue)
370{
e0a95d28
MS
371 struct qitem *it;
372
86e4d161
MS
373 if (LIST_EMPTY(&queue->queue))
374 return;
375
e0a95d28
MS
376 it = go_background(queue);
377 deliver(it);
86e4d161
MS
378 /* NOTREACHED */
379}
380
381static void
382show_queue(struct queue *queue)
383{
384 struct qitem *it;
87bdbc01 385 int locked = 0; /* XXX */
86e4d161
MS
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) {
5896fefc
SS
393 printf("ID\t: %s%s\n"
394 "From\t: %s\n"
553ab44c 395 "To\t: %s\n",
5896fefc 396 it->queueid,
87bdbc01 397 locked ? "*" : "",
5896fefc 398 it->sender, it->addr);
553ab44c
SS
399
400 if (LIST_NEXT(it, next) != NULL)
401 printf("--\n");
86e4d161
MS
402 }
403}
404
405/*
406 * TODO:
407 *
408 * - alias processing
409 * - use group permissions
410 * - proper sysexit codes
411 */
412
e0a95d28
MS
413int
414main(int argc, char **argv)
86e4d161 415{
247523d6 416 struct sigaction act;
86e4d161 417 char *sender = NULL;
86e4d161 418 struct queue queue;
86e4d161 419 int i, ch;
e33faafe 420 int nodot = 0, showq = 0, queue_only = 0;
f734c0ae 421 int recp_from_header = 0;
86e4d161 422
75fdf182
SS
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
04947c64 432 errno = 0;
75fdf182 433 pw = getpwnam(DMA_ROOT_USER);
04947c64
EM
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 }
75fdf182
SS
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
86e4d161 448 atexit(deltmp);
ee613428 449 init_random();
6f5efe4f
SS
450
451 bzero(&queue, sizeof(queue));
86e4d161 452 LIST_INIT(&queue.queue);
86e4d161 453
0f779ba6
SS
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;
e9c1c02f
SS
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);
0f779ba6
SS
467 }
468
87fa1cc5 469 opterr = 0;
f734c0ae 470 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
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;
bca4c72a
SS
483 } else if (optarg[0] == 'q') {
484 queue_only = 1;
485 break;
86e4d161
MS
486 }
487 /* else FALLTRHOUGH */
488 case 'D':
489 daemonize = 0;
490 break;
491 case 'L':
4d5af2b0 492 logident_base = optarg;
86e4d161
MS
493 break;
494 case 'f':
495 case 'r':
496 sender = optarg;
497 break;
498
f734c0ae
SS
499 case 't':
500 recp_from_header = 1;
501 break;
502
86e4d161
MS
503 case 'o':
504 /* -oX is being ignored, except for -oi */
505 if (optarg[0] != 'i')
506 break;
507 /* else FALLTRHOUGH */
87fa1cc5
MS
508 case 'O':
509 break;
86e4d161
MS
510 case 'i':
511 nodot = 1;
512 break;
513
514 case 'q':
af31dade
SS
515 /* Don't let getopt slup up other arguments */
516 if (optarg && *optarg == '-')
517 optind--;
86e4d161
MS
518 doqueue = 1;
519 break;
520
b6bb4e2d
SS
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
f514a0d9
SS
536 case ':':
537 if (optopt == 'q') {
538 doqueue = 1;
539 break;
540 }
541 /* FALLTHROUGH */
542
86e4d161 543 default:
f514a0d9 544 fprintf(stderr, "invalid argument: `-%c'\n", optopt);
86e4d161
MS
545 exit(1);
546 }
547 }
548 argc -= optind;
549 argv += optind;
87fa1cc5 550 opterr = 1;
86e4d161 551
8d291e4d
SS
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
0f779ba6 558skipopts:
4d5af2b0
SS
559 if (logident_base == NULL)
560 logident_base = "dma";
561 setlogident(NULL);
4602c807 562
247523d6
SS
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
d84c3daa 569 parse_conf(CONF_PATH "/dma.conf");
86e4d161 570
a0c4afa6
SS
571 if (config.authpath != NULL)
572 parse_authfile(config.authpath);
86e4d161
MS
573
574 if (showq) {
6f5efe4f 575 if (load_queue(&queue) < 0)
de13a881 576 errlog(1, "can not load queue");
6f5efe4f 577 show_queue(&queue);
e0a95d28 578 return (0);
86e4d161
MS
579 }
580
581 if (doqueue) {
3686aecc 582 flushqueue_signal();
6f5efe4f 583 if (load_queue(&queue) < 0)
de13a881 584 errlog(1, "can not load queue");
6f5efe4f 585 run_queue(&queue);
e0a95d28 586 return (0);
86e4d161
MS
587 }
588
e0a95d28 589 if (read_aliases() != 0)
e9c1c02f 590 errlog(1, "could not parse aliases file `%s'", config.aliases);
86e4d161 591
057afad5 592 if ((sender = set_from(&queue, sender)) == NULL)
de13a881 593 errlog(1, NULL);
86e4d161 594
f734c0ae 595 if (newspoolf(&queue) != 0)
c8674a69 596 errlog(1, "can not create temp file in `%s'", config.spooldir);
f734c0ae
SS
597
598 setlogident("%s", queue.id);
599
86e4d161 600 for (i = 0; i < argc; i++) {
a803c7a6 601 if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
de13a881 602 errlogx(1, "invalid recipient `%s'", argv[i]);
86e4d161
MS
603 }
604
f734c0ae 605 if (LIST_EMPTY(&queue.queue) && !recp_from_header)
de13a881 606 errlogx(1, "no recipients");
86e4d161 607
f734c0ae 608 if (readmail(&queue, nodot, recp_from_header) != 0)
de13a881 609 errlog(1, "can not read mail");
86e4d161 610
f734c0ae
SS
611 if (LIST_EMPTY(&queue.queue))
612 errlogx(1, "no recipients");
613
057afad5 614 if (linkspool(&queue) != 0)
de13a881 615 errlog(1, "can not create spools");
86e4d161
MS
616
617 /* From here on the mail is safe. */
618
a0c4afa6 619 if (config.features & DEFER || queue_only)
e0a95d28 620 return (0);
86e4d161 621
28be0b96 622 run_queue(&queue);
86e4d161
MS
623
624 /* NOTREACHED */
e0a95d28 625 return (0);
94389971 626}