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