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