]> git.ipfire.org Git - people/ms/dma.git/blame - dma.c
dma(8): Raise WARNS to 6.
[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.
33 *
c507d897 34 * $DragonFly: src/libexec/dma/dma.c,v 1.5 2008/09/30 17:47:21 swildner Exp $
86e4d161
MS
35 */
36
37#include <sys/param.h>
38#include <sys/queue.h>
39#include <sys/stat.h>
40#include <sys/types.h>
c507d897 41#include <sys/wait.h>
86e4d161
MS
42
43#ifdef HAVE_CRYPTO
44#include <openssl/ssl.h>
45#endif /* HAVE_CRYPTO */
46
47#include <dirent.h>
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <inttypes.h>
52#include <netdb.h>
53#include <paths.h>
54#include <pwd.h>
55#include <signal.h>
56#include <stdarg.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <syslog.h>
61#include <unistd.h>
62
63#include "dma.h"
64
65
66
e0a95d28 67static void deliver(struct qitem *);
86e4d161
MS
68static int add_recp(struct queue *, const char *, const char *, int);
69
70struct aliases aliases = LIST_HEAD_INITIALIZER(aliases);
71static struct strlist tmpfs = SLIST_HEAD_INITIALIZER(tmpfs);
72struct virtusers virtusers = LIST_HEAD_INITIALIZER(virtusers);
73struct authusers authusers = LIST_HEAD_INITIALIZER(authusers);
74static int daemonize = 1;
75struct config *config;
76
77char *
78hostname(void)
79{
80 static char name[MAXHOSTNAMELEN+1];
81
82 if (gethostname(name, sizeof(name)) != 0)
83 strcpy(name, "(unknown hostname)");
84
85 return name;
86}
87
88static char *
89set_from(const char *osender)
90{
91 struct virtuser *v;
92 char *sender;
93
94 if ((config->features & VIRTUAL) != 0) {
95 SLIST_FOREACH(v, &virtusers, next) {
96 if (strcmp(v->login, getlogin()) == 0) {
97 sender = strdup(v->address);
98 if (sender == NULL)
99 return(NULL);
100 goto out;
101 }
102 }
103 }
104
105 if (osender) {
106 sender = strdup(osender);
107 if (sender == NULL)
e0a95d28 108 return (NULL);
86e4d161
MS
109 } else {
110 if (asprintf(&sender, "%s@%s", getlogin(), hostname()) <= 0)
e0a95d28 111 return (NULL);
86e4d161
MS
112 }
113
114 if (strchr(sender, '\n') != NULL) {
115 errno = EINVAL;
e0a95d28 116 return (NULL);
86e4d161
MS
117 }
118
119out:
e0a95d28 120 return (sender);
86e4d161
MS
121}
122
123static int
124read_aliases(void)
125{
126 yyin = fopen(config->aliases, "r");
127 if (yyin == NULL)
e0a95d28 128 return (0); /* not fatal */
86e4d161 129 if (yyparse())
e0a95d28 130 return (-1); /* fatal error, probably malloc() */
86e4d161 131 fclose(yyin);
e0a95d28 132 return (0);
86e4d161
MS
133}
134
135static int
136add_recp(struct queue *queue, const char *str, const char *sender, int expand)
137{
138 struct qitem *it, *tit;
139 struct stritem *sit;
140 struct alias *al;
141 struct passwd *pw;
142 char *host;
143 int aliased = 0;
144
145 it = calloc(1, sizeof(*it));
146 if (it == NULL)
e0a95d28 147 return (-1);
86e4d161
MS
148 it->addr = strdup(str);
149 if (it->addr == NULL)
e0a95d28 150 return (-1);
86e4d161
MS
151
152 it->sender = sender;
153 host = strrchr(it->addr, '@');
154 if (host != NULL &&
155 (strcmp(host + 1, hostname()) == 0 ||
156 strcmp(host + 1, "localhost") == 0)) {
157 *host = 0;
158 }
159 LIST_FOREACH(tit, &queue->queue, next) {
160 /* weed out duplicate dests */
161 if (strcmp(tit->addr, it->addr) == 0) {
162 free(it->addr);
163 free(it);
e0a95d28 164 return (0);
86e4d161
MS
165 }
166 }
167 LIST_INSERT_HEAD(&queue->queue, it, next);
168 if (strrchr(it->addr, '@') == NULL) {
e0a95d28 169 it->remote = 0;
86e4d161
MS
170 if (expand) {
171 LIST_FOREACH(al, &aliases, next) {
172 if (strcmp(al->alias, it->addr) != 0)
173 continue;
174 SLIST_FOREACH(sit, &al->dests, next) {
e0a95d28
MS
175 if (add_recp(queue, sit->str, sender, 1) != 0)
176 return (-1);
86e4d161
MS
177 }
178 aliased = 1;
179 }
180 if (aliased) {
181 LIST_REMOVE(it, next);
182 } else {
e0a95d28 183 /* Local destination, check */
86e4d161
MS
184 pw = getpwnam(it->addr);
185 if (pw == NULL)
186 goto out;
e0a95d28 187 endpwent();
86e4d161
MS
188 }
189 }
190 } else {
e0a95d28 191 it->remote = 1;
86e4d161
MS
192 }
193
e0a95d28 194 return (0);
86e4d161
MS
195
196out:
197 free(it->addr);
198 free(it);
e0a95d28 199 return (-1);
86e4d161
MS
200}
201
202static void
203deltmp(void)
204{
205 struct stritem *t;
206
207 SLIST_FOREACH(t, &tmpfs, next) {
208 unlink(t->str);
209 }
210}
211
212static int
213gentempf(struct queue *queue)
214{
215 char fn[PATH_MAX+1];
216 struct stritem *t;
217 int fd;
218
219 if (snprintf(fn, sizeof(fn), "%s/%s", config->spooldir, "tmp_XXXXXXXXXX") <= 0)
e0a95d28 220 return (-1);
86e4d161
MS
221 fd = mkstemp(fn);
222 if (fd < 0)
e0a95d28 223 return (-1);
86e4d161
MS
224 queue->mailfd = fd;
225 queue->tmpf = strdup(fn);
226 if (queue->tmpf == NULL) {
227 unlink(fn);
e0a95d28 228 return (-1);
86e4d161
MS
229 }
230 t = malloc(sizeof(*t));
231 if (t != NULL) {
232 t->str = queue->tmpf;
233 SLIST_INSERT_HEAD(&tmpfs, t, next);
234 }
e0a95d28 235 return (0);
86e4d161
MS
236}
237
238/*
239 * spool file format:
240 *
241 * envelope-from
242 * queue-id1 envelope-to1
243 * queue-id2 envelope-to2
244 * ...
245 * <empty line>
246 * mail data
247 *
248 * queue ids are unique, formed from the inode of the spool file
249 * and a unique identifier.
250 */
251static int
252preparespool(struct queue *queue, const char *sender)
253{
254 char line[1000]; /* by RFC2822 */
255 struct stat st;
256 int error;
257 struct qitem *it;
258 FILE *queuef;
259 off_t hdrlen;
260
261 error = snprintf(line, sizeof(line), "%s\n", sender);
262 if (error < 0 || (size_t)error >= sizeof(line)) {
263 errno = E2BIG;
e0a95d28 264 return (-1);
86e4d161
MS
265 }
266 if (write(queue->mailfd, line, error) != error)
e0a95d28 267 return (-1);
86e4d161
MS
268
269 queuef = fdopen(queue->mailfd, "r+");
270 if (queuef == NULL)
e0a95d28 271 return (-1);
86e4d161
MS
272
273 /*
274 * Assign queue id to each dest.
275 */
276 if (fstat(queue->mailfd, &st) != 0)
e0a95d28 277 return (-1);
86e4d161
MS
278 queue->id = st.st_ino;
279 LIST_FOREACH(it, &queue->queue, next) {
280 if (asprintf(&it->queueid, "%"PRIxMAX".%"PRIxPTR,
281 queue->id, (uintptr_t)it) <= 0)
e0a95d28 282 return (-1);
86e4d161
MS
283 if (asprintf(&it->queuefn, "%s/%s",
284 config->spooldir, it->queueid) <= 0)
e0a95d28
MS
285 return (-1);
286 /* File may not exist yet */
287 if (stat(it->queuefn, &st) == 0)
288 return (-1);
86e4d161
MS
289 it->queuef = queuef;
290 error = snprintf(line, sizeof(line), "%s %s\n",
291 it->queueid, it->addr);
292 if (error < 0 || (size_t)error >= sizeof(line))
e0a95d28 293 return (-1);
86e4d161 294 if (write(queue->mailfd, line, error) != error)
e0a95d28 295 return (-1);
86e4d161
MS
296 }
297 line[0] = '\n';
298 if (write(queue->mailfd, line, 1) != 1)
e0a95d28 299 return (-1);
86e4d161
MS
300
301 hdrlen = lseek(queue->mailfd, 0, SEEK_CUR);
302 LIST_FOREACH(it, &queue->queue, next) {
303 it->hdrlen = hdrlen;
304 }
e0a95d28 305 return (0);
86e4d161
MS
306}
307
308static char *
309rfc822date(void)
310{
311 static char str[50];
312 size_t error;
313 time_t now;
314
315 now = time(NULL);
316 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
317 localtime(&now));
318 if (error == 0)
319 strcpy(str, "(date fail)");
e0a95d28 320 return (str);
86e4d161
MS
321}
322
323static int
324readmail(struct queue *queue, const char *sender, int nodot)
325{
326 char line[1000]; /* by RFC2822 */
327 size_t linelen;
328 int error;
329
330 error = snprintf(line, sizeof(line), "\
331Received: from %s (uid %d)\n\
332\t(envelope-from %s)\n\
333\tid %"PRIxMAX"\n\
334\tby %s (%s)\n\
335\t%s\n",
336 getlogin(), getuid(),
337 sender,
338 queue->id,
339 hostname(), VERSION,
340 rfc822date());
341 if (error < 0 || (size_t)error >= sizeof(line))
e0a95d28 342 return (-1);
86e4d161 343 if (write(queue->mailfd, line, error) != error)
e0a95d28 344 return (-1);
86e4d161
MS
345
346 while (!feof(stdin)) {
347 if (fgets(line, sizeof(line), stdin) == NULL)
348 break;
349 linelen = strlen(line);
350 if (linelen == 0 || line[linelen - 1] != '\n') {
351 errno = EINVAL; /* XXX mark permanent errors */
e0a95d28 352 return (-1);
86e4d161
MS
353 }
354 if (!nodot && linelen == 2 && line[0] == '.')
355 break;
356 if ((size_t)write(queue->mailfd, line, linelen) != linelen)
e0a95d28 357 return (-1);
86e4d161
MS
358 }
359 if (fsync(queue->mailfd) != 0)
e0a95d28
MS
360 return (-1);
361 return (0);
86e4d161
MS
362}
363
364static int
365linkspool(struct queue *queue)
366{
367 struct qitem *it;
368
369 LIST_FOREACH(it, &queue->queue, next) {
370 if (link(queue->tmpf, it->queuefn) != 0)
371 goto delfiles;
372 }
e0a95d28
MS
373 unlink(queue->tmpf);
374 return (0);
86e4d161
MS
375
376delfiles:
377 LIST_FOREACH(it, &queue->queue, next) {
378 unlink(it->queuefn);
379 }
e0a95d28 380 return (-1);
86e4d161
MS
381}
382
e0a95d28
MS
383static struct qitem *
384go_background(struct queue *queue)
86e4d161
MS
385{
386 struct sigaction sa;
387 struct qitem *it;
388 pid_t pid;
389
390 if (daemonize && daemon(0, 0) != 0) {
e0a95d28 391 syslog(LOG_ERR, "can not daemonize: %m");
86e4d161
MS
392 exit(1);
393 }
394 daemonize = 0;
e0a95d28 395
86e4d161
MS
396 bzero(&sa, sizeof(sa));
397 sa.sa_flags = SA_NOCLDWAIT;
398 sa.sa_handler = SIG_IGN;
399 sigaction(SIGCHLD, &sa, NULL);
400
94389971 401 LIST_FOREACH(it, &queue->queue, next) {
e0a95d28
MS
402 /* No need to fork for the last dest */
403 if (LIST_NEXT(it, next) == NULL)
404 return (it);
405
86e4d161
MS
406 pid = fork();
407 switch (pid) {
408 case -1:
409 syslog(LOG_ERR, "can not fork: %m");
410 exit(1);
411 break;
412
413 case 0:
414 /*
415 * Child:
416 *
417 * return and deliver mail
418 */
e0a95d28 419 return (it);
86e4d161
MS
420
421 default:
422 /*
423 * Parent:
424 *
425 * fork next child
426 */
427 break;
428 }
429 }
430
431 syslog(LOG_CRIT, "reached dead code");
432 exit(1);
433}
434
435static void
e0a95d28 436bounce(struct qitem *it, const char *reason)
86e4d161
MS
437{
438 struct queue bounceq;
439 struct qitem *bit;
440 char line[1000];
441 int error;
442
443 /* Don't bounce bounced mails */
444 if (it->sender[0] == 0) {
445 syslog(LOG_CRIT, "%s: delivery panic: can't bounce a bounce",
e0a95d28 446 it->queueid);
86e4d161
MS
447 exit(1);
448 }
449
450 syslog(LOG_ERR, "%s: delivery failed, bouncing",
451 it->queueid);
452
453 LIST_INIT(&bounceq.queue);
454 if (add_recp(&bounceq, it->sender, "", 1) != 0)
455 goto fail;
456 if (gentempf(&bounceq) != 0)
457 goto fail;
458 if (preparespool(&bounceq, "") != 0)
459 goto fail;
460
461 bit = LIST_FIRST(&bounceq.queue);
462 error = fprintf(bit->queuef, "\
463Received: from MAILER-DAEMON\n\
464\tid %"PRIxMAX"\n\
465\tby %s (%s)\n\
466\t%s\n\
467X-Original-To: <%s>\n\
468From: MAILER-DAEMON <>\n\
469To: %s\n\
470Subject: Mail delivery failed\n\
471Message-Id: <%"PRIxMAX"@%s>\n\
472Date: %s\n\
473\n\
474This is the %s at %s.\n\
475\n\
476There was an error delivering your mail to <%s>.\n\
477\n\
478%s\n\
479\n\
480Message headers follow.\n\
481\n\
482",
483 bounceq.id,
484 hostname(), VERSION,
485 rfc822date(),
486 it->addr,
487 it->sender,
488 bounceq.id, hostname(),
489 rfc822date(),
490 VERSION, hostname(),
491 it->addr,
492 reason);
493 if (error < 0)
494 goto fail;
495 if (fflush(bit->queuef) != 0)
496 goto fail;
497
498 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0)
499 goto fail;
500 while (!feof(it->queuef)) {
501 if (fgets(line, sizeof(line), it->queuef) == NULL)
502 break;
503 if (line[0] == '\n')
504 break;
505 write(bounceq.mailfd, line, strlen(line));
506 }
507 if (fsync(bounceq.mailfd) != 0)
508 goto fail;
509 if (linkspool(&bounceq) != 0)
510 goto fail;
511 /* bounce is safe */
512
513 unlink(it->queuefn);
514 fclose(it->queuef);
515
e0a95d28
MS
516 bit = go_background(&bounceq);
517 deliver(bit);
86e4d161
MS
518 /* NOTREACHED */
519
520fail:
521 syslog(LOG_CRIT, "%s: error creating bounce: %m", it->queueid);
522 unlink(it->queuefn);
523 exit(1);
524}
525
526static int
527deliver_local(struct qitem *it, const char **errmsg)
528{
94389971 529 char fn[PATH_MAX+1];
e0a95d28
MS
530 char line[1000];
531 size_t linelen;
532 int mbox;
533 int error;
534 off_t mboxlen;
86e4d161 535 time_t now = time(NULL);
94389971 536
e0a95d28
MS
537 error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, it->addr);
538 if (error < 0 || (size_t)error >= sizeof(fn)) {
539 syslog(LOG_ERR, "%s: local delivery deferred: %m",
540 it->queueid);
541 return (1);
86e4d161
MS
542 }
543
e0a95d28
MS
544 /* mailx removes users mailspool file if empty, so open with O_CREAT */
545 mbox = open(fn, O_WRONLY | O_EXLOCK | O_APPEND | O_CREAT);
546 if (mbox < 0) {
547 syslog(LOG_ERR, "%s: local delivery deferred: can not open `%s': %m",
548 it->queueid, fn);
549 return (1);
86e4d161 550 }
e0a95d28 551 mboxlen = lseek(mbox, 0, SEEK_CUR);
86e4d161
MS
552
553 if (fseek(it->queuef, it->hdrlen, SEEK_SET) != 0) {
554 syslog(LOG_ERR, "%s: local delivery deferred: can not seek: %m",
555 it->queueid);
e0a95d28 556 return (1);
86e4d161
MS
557 }
558
e0a95d28
MS
559 error = snprintf(line, sizeof(line), "From %s\t%s", it->sender, ctime(&now));
560 if (error < 0 || (size_t)error >= sizeof(line)) {
86e4d161
MS
561 syslog(LOG_ERR, "%s: local delivery deferred: can not write header: %m",
562 it->queueid);
e0a95d28 563 return (1);
86e4d161 564 }
e0a95d28 565 if (write(mbox, line, error) != error)
86e4d161
MS
566 goto wrerror;
567
568 while (!feof(it->queuef)) {
569 if (fgets(line, sizeof(line), it->queuef) == NULL)
570 break;
571 linelen = strlen(line);
572 if (linelen == 0 || line[linelen - 1] != '\n') {
e0a95d28
MS
573 syslog(LOG_CRIT, "%s: local delivery failed: corrupted queue file",
574 it->queueid);
86e4d161 575 *errmsg = "corrupted queue file";
e0a95d28 576 error = -1;
86e4d161
MS
577 goto chop;
578 }
579
580 if (strncmp(line, "From ", 5) == 0) {
581 const char *gt = ">";
582
e0a95d28 583 if (write(mbox, gt, 1) != 1)
86e4d161
MS
584 goto wrerror;
585 }
e0a95d28 586 if ((size_t)write(mbox, line, linelen) != linelen)
86e4d161
MS
587 goto wrerror;
588 }
589 line[0] = '\n';
e0a95d28 590 if (write(mbox, line, 1) != 1)
94389971 591 goto wrerror;
e0a95d28
MS
592 close(mbox);
593 return (0);
86e4d161
MS
594
595wrerror:
596 syslog(LOG_ERR, "%s: local delivery failed: write error: %m",
597 it->queueid);
e0a95d28 598 error = 1;
86e4d161 599chop:
e0a95d28 600 if (ftruncate(mbox, mboxlen) != 0)
86e4d161 601 syslog(LOG_WARNING, "%s: error recovering mbox `%s': %m",
e0a95d28
MS
602 it->queueid, fn);
603 close(mbox);
604 return (error);
86e4d161
MS
605}
606
607static void
e0a95d28 608deliver(struct qitem *it)
86e4d161
MS
609{
610 int error;
611 unsigned int backoff = MIN_RETRY;
612 const char *errmsg = "unknown bounce reason";
613 struct timeval now;
614 struct stat st;
615
e0a95d28
MS
616 syslog(LOG_INFO, "%s: mail from=<%s> to=<%s>",
617 it->queueid, it->sender, it->addr);
86e4d161
MS
618
619retry:
620 syslog(LOG_INFO, "%s: trying delivery",
621 it->queueid);
622
e0a95d28
MS
623 if (it->remote)
624 error = deliver_remote(it, &errmsg);
625 else
86e4d161
MS
626 error = deliver_local(it, &errmsg);
627
628 switch (error) {
629 case 0:
e0a95d28 630 unlink(it->queuefn);
86e4d161
MS
631 syslog(LOG_INFO, "%s: delivery successful",
632 it->queueid);
633 exit(0);
634
635 case 1:
636 if (stat(it->queuefn, &st) != 0) {
637 syslog(LOG_ERR, "%s: lost queue file `%s'",
638 it->queueid, it->queuefn);
639 exit(1);
640 }
641 if (gettimeofday(&now, NULL) == 0 &&
642 (now.tv_sec - st.st_mtimespec.tv_sec > MAX_TIMEOUT)) {
643 char *msg;
644
645 if (asprintf(&msg,
e0a95d28
MS
646 "Could not deliver for the last %d seconds. Giving up.",
647 MAX_TIMEOUT) > 0)
86e4d161
MS
648 errmsg = msg;
649 goto bounce;
650 }
651 sleep(backoff);
652 backoff *= 2;
653 if (backoff > MAX_RETRY)
654 backoff = MAX_RETRY;
655 goto retry;
656
657 case -1:
658 default:
659 break;
660 }
661
662bounce:
e0a95d28 663 bounce(it, errmsg);
86e4d161
MS
664 /* NOTREACHED */
665}
666
667static void
668load_queue(struct queue *queue)
669{
670 struct stat st;
671 struct qitem *it;
672 //struct queue queue, itmqueue;
673 struct queue itmqueue;
674 DIR *spooldir;
675 struct dirent *de;
676 char line[1000];
677 char *fn;
678 FILE *queuef;
679 char *sender;
680 char *addr;
681 char *queueid;
682 char *queuefn;
683 off_t hdrlen;
684 int fd;
685
686 LIST_INIT(&queue->queue);
687
688 spooldir = opendir(config->spooldir);
689 if (spooldir == NULL)
690 err(1, "reading queue");
691
692 while ((de = readdir(spooldir)) != NULL) {
693 sender = NULL;
694 queuef = NULL;
695 queueid = NULL;
696 queuefn = NULL;
697 fn = NULL;
698 LIST_INIT(&itmqueue.queue);
699
700 /* ignore temp files */
701 if (strncmp(de->d_name, "tmp_", 4) == 0 ||
702 de->d_type != DT_REG)
703 continue;
704 if (asprintf(&queuefn, "%s/%s", config->spooldir, de->d_name) < 0)
705 goto fail;
706 fd = open(queuefn, O_RDONLY|O_EXLOCK|O_NONBLOCK);
707 if (fd < 0) {
708 /* Ignore locked files */
709 if (errno == EWOULDBLOCK)
710 continue;
711 goto skip_item;
712 }
713
714 queuef = fdopen(fd, "r");
715 if (queuef == NULL)
716 goto skip_item;
717 if (fgets(line, sizeof(line), queuef) == NULL ||
718 line[0] == 0)
719 goto skip_item;
720 line[strlen(line) - 1] = 0; /* chop newline */
721 sender = strdup(line);
722 if (sender == NULL)
723 goto skip_item;
724
725 for (;;) {
726 if (fgets(line, sizeof(line), queuef) == NULL ||
727 line[0] == 0)
728 goto skip_item;
729 if (line[0] == '\n')
730 break;
731 line[strlen(line) - 1] = 0;
732 queueid = strdup(line);
733 if (queueid == NULL)
734 goto skip_item;
735 addr = strchr(queueid, ' ');
736 if (addr == NULL)
737 goto skip_item;
738 *addr++ = 0;
739 if (fn != NULL)
740 free(fn);
741 if (asprintf(&fn, "%s/%s", config->spooldir, queueid) < 0)
742 goto skip_item;
743 /* Item has already been delivered? */
744 if (stat(fn, &st) != 0)
745 continue;
746 if (add_recp(&itmqueue, addr, sender, 0) != 0)
747 goto skip_item;
748 it = LIST_FIRST(&itmqueue.queue);
749 it->queuef = queuef;
750 it->queueid = queueid;
751 it->queuefn = fn;
752 fn = NULL;
753 }
754 if (LIST_EMPTY(&itmqueue.queue)) {
755 warnx("queue file without items: `%s'", queuefn);
756 goto skip_item2;
757 }
758 hdrlen = ftell(queuef);
759 while ((it = LIST_FIRST(&itmqueue.queue)) != NULL) {
760 it->hdrlen = hdrlen;
761 LIST_REMOVE(it, next);
762 LIST_INSERT_HEAD(&queue->queue, it, next);
763 }
764 continue;
765
766skip_item:
767 warn("reading queue: `%s'", queuefn);
768skip_item2:
769 if (sender != NULL)
770 free(sender);
771 if (queuefn != NULL)
772 free(queuefn);
773 if (fn != NULL)
774 free(fn);
775 if (queueid != NULL)
776 free(queueid);
777 close(fd);
778 }
779 closedir(spooldir);
780 return;
781
782fail:
783 err(1, "reading queue");
784}
785
786static void
787run_queue(struct queue *queue)
788{
e0a95d28
MS
789 struct qitem *it;
790
86e4d161
MS
791 if (LIST_EMPTY(&queue->queue))
792 return;
793
e0a95d28
MS
794 it = go_background(queue);
795 deliver(it);
86e4d161
MS
796 /* NOTREACHED */
797}
798
799static void
800show_queue(struct queue *queue)
801{
802 struct qitem *it;
803
804 if (LIST_EMPTY(&queue->queue)) {
805 printf("Mail queue is empty\n");
806 return;
807 }
808
809 LIST_FOREACH(it, &queue->queue, next) {
810 printf("\
811ID\t: %s\n\
812From\t: %s\n\
813To\t: %s\n--\n", it->queueid, it->sender, it->addr);
814 }
815}
816
817/*
818 * TODO:
819 *
820 * - alias processing
821 * - use group permissions
822 * - proper sysexit codes
823 */
824
e0a95d28
MS
825int
826main(int argc, char **argv)
86e4d161
MS
827{
828 char *sender = NULL;
829 char tag[255];
e0a95d28 830 struct qitem *it;
86e4d161
MS
831 struct queue queue;
832 struct queue lqueue;
86e4d161
MS
833 int i, ch;
834 int nodot = 0, doqueue = 0, showq = 0;
835
836 atexit(deltmp);
837 LIST_INIT(&queue.queue);
838 snprintf(tag, 254, "dma");
839
87fa1cc5
MS
840 opterr = 0;
841 while ((ch = getopt(argc, argv, "A:b:Df:iL:o:O:q:r:")) != -1) {
86e4d161
MS
842 switch (ch) {
843 case 'A':
844 /* -AX is being ignored, except for -A{c,m} */
845 if (optarg[0] == 'c' || optarg[0] == 'm') {
846 break;
847 }
848 /* else FALLTRHOUGH */
849 case 'b':
850 /* -bX is being ignored, except for -bp */
851 if (optarg[0] == 'p') {
852 showq = 1;
853 break;
854 }
855 /* else FALLTRHOUGH */
856 case 'D':
857 daemonize = 0;
858 break;
859 case 'L':
860 if (optarg != NULL)
861 snprintf(tag, 254, "%s", optarg);
862 break;
863 case 'f':
864 case 'r':
865 sender = optarg;
866 break;
867
868 case 'o':
869 /* -oX is being ignored, except for -oi */
870 if (optarg[0] != 'i')
871 break;
872 /* else FALLTRHOUGH */
87fa1cc5
MS
873 case 'O':
874 break;
86e4d161
MS
875 case 'i':
876 nodot = 1;
877 break;
878
879 case 'q':
880 doqueue = 1;
881 break;
882
883 default:
884 exit(1);
885 }
886 }
887 argc -= optind;
888 argv += optind;
87fa1cc5 889 opterr = 1;
86e4d161
MS
890
891 openlog(tag, LOG_PID | LOG_PERROR, LOG_MAIL);
892
893 config = malloc(sizeof(struct config));
894 if (config == NULL)
895 errx(1, "Cannot allocate enough memory");
896
897 memset(config, 0, sizeof(struct config));
012adf14 898 if (parse_conf(CONF_PATH, config) < 0) {
86e4d161
MS
899 free(config);
900 errx(1, "reading config file");
901 }
902
903 if (config->features & VIRTUAL)
e0a95d28 904 if (parse_virtuser(config->virtualpath) < 0)
86e4d161
MS
905 errx(1, "error reading virtual user file: %s",
906 config->virtualpath);
907
e0a95d28 908 if (parse_authfile(config->authpath) < 0)
86e4d161
MS
909 err(1, "reading SMTP authentication file");
910
911 if (showq) {
912 if (argc != 0)
913 errx(1, "sending mail and displaying queue is"
914 " mutually exclusive");
915 load_queue(&lqueue);
916 show_queue(&lqueue);
e0a95d28 917 return (0);
86e4d161
MS
918 }
919
920 if (doqueue) {
921 if (argc != 0)
922 errx(1, "sending mail and queue pickup is mutually exclusive");
923 load_queue(&lqueue);
924 run_queue(&lqueue);
e0a95d28 925 return (0);
86e4d161
MS
926 }
927
e0a95d28 928 if (read_aliases() != 0)
86e4d161
MS
929 err(1, "reading aliases");
930
e0a95d28 931 if ((sender = set_from(sender)) == NULL)
86e4d161
MS
932 err(1, "setting from address");
933
934 for (i = 0; i < argc; i++) {
e0a95d28 935 if (add_recp(&queue, argv[i], sender, 1) != 0)
86e4d161
MS
936 errx(1, "invalid recipient `%s'\n", argv[i]);
937 }
938
e0a95d28 939 if (LIST_EMPTY(&queue.queue))
86e4d161
MS
940 errx(1, "no recipients");
941
e0a95d28
MS
942 if (gentempf(&queue) != 0)
943 err(1, "create temp file");
944
945 if (preparespool(&queue, sender) != 0)
86e4d161
MS
946 err(1, "creating spools (1)");
947
e0a95d28 948 if (readmail(&queue, sender, nodot) != 0)
86e4d161
MS
949 err(1, "reading mail");
950
e0a95d28 951 if (linkspool(&queue) != 0)
86e4d161
MS
952 err(1, "creating spools (2)");
953
954 /* From here on the mail is safe. */
955
956 if (config->features & DEFER)
e0a95d28 957 return (0);
86e4d161 958
e0a95d28
MS
959 it = go_background(&queue);
960 deliver(it);
86e4d161
MS
961
962 /* NOTREACHED */
e0a95d28 963 return (0);
94389971 964}