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