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