2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <malloc_np.h>
47 #define MAX_LINE_RFC822 999 /* 998 characters plus \n */
50 bounce(struct qitem *it, const char *reason)
57 /* Don't bounce bounced mails */
58 if (it->sender[0] == 0) {
59 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
63 bzero(&bounceq, sizeof(bounceq));
64 LIST_INIT(&bounceq.queue);
66 if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0)
69 if (newspoolf(&bounceq) != 0)
72 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
73 setlogident("%s", bounceq.id);
75 error = fprintf(bounceq.mailf,
76 "Received: from MAILER-DAEMON\n"
78 "\tby %s (%s on %s);\n"
80 "X-Original-To: <%s>\n"
81 "From: MAILER-DAEMON <>\n"
83 "Subject: Mail delivery failed\n"
84 "Message-Id: <%s@%s>\n"
87 "This is the %s at %s.\n"
89 "There was an error delivering your mail to <%s>.\n"
96 hostname(), VERSION, systemhostname(),
100 bounceq.id, hostname(),
105 config.features & FULLBOUNCE ?
106 "Original message follows." :
107 "Message headers follow.");
111 if (fseek(it->mailf, 0, SEEK_SET) != 0)
113 if (config.features & FULLBOUNCE) {
114 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
115 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
119 while (!feof(it->mailf)) {
120 if (fgets(line, sizeof(line), it->mailf) == NULL)
124 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
129 if (linkspool(&bounceq) != 0)
139 syslog(LOG_CRIT, "error creating bounce: %m");
162 * Simplified RFC2822 header/address parsing.
163 * We copy escapes and quoted strings directly, since
164 * we have to pass them like this to the mail server anyways.
165 * XXX local addresses will need treatment
168 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
179 bzero(ps, sizeof(*ps));
181 /* skip over header name */
211 for (; *s != 0; s++) {
275 /* ignore whitespace */
279 /* this is the real address now */
300 * Next address, copy previous one.
301 * However, we might be directly after
302 * a <address>, or have two consecutive
304 * Skip the comma unless there is
305 * really something to copy.
320 if (ps->pos + 1 == sizeof(ps->addr))
322 ps->addr[ps->pos++] = *s;
337 ps->addr[ps->pos] = 0;
339 addr = strdup(ps->addr);
341 errlog(EX_SOFTWARE, "strdup");
343 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
344 errlogx(EX_DATAERR, "invalid recipient `%s'", addr);
350 writeline(struct queue *queue, const char *line, ssize_t linelen)
354 while (linelen > 0) {
356 if (linelen > MAX_LINE_RFC822) {
357 len = MAX_LINE_RFC822 - 10;
360 if (fwrite(line, len, 1, queue->mailf) != 1)
363 if (linelen <= MAX_LINE_RFC822)
366 if (fwrite("\n", 1, 1, queue->mailf) != 1)
369 line += MAX_LINE_RFC822 - 10;
370 linelen = strlen(line);
376 readmail(struct queue *queue, int nodot, int recp_from_header)
378 struct parse_state parse_state;
382 char newline[MAX_LINE_RFC822 + 1];
386 int had_messagid = 0;
388 int had_first_line = 0;
389 int had_last_line = 0;
393 parse_state.state = NONE;
395 error = fprintf(queue->mailf,
396 "Received: from %s (uid %d)\n"
397 "\t(envelope-from %s)\n"
399 "\tby %s (%s on %s);\n"
404 hostname(), VERSION, systemhostname(),
406 if ((ssize_t)error < 0)
409 while ((linelen = getline(&line, &linecap, stdin)) > 0) {
412 errlogx(EX_DATAERR, "bad mail input format:"
413 " from %s (uid %d) (envelope-from %s)",
414 username, useruid, queue->sender);
415 linelen = strlen(line);
416 if (linelen == 0 || line[linelen - 1] != '\n') {
418 * This line did not end with a newline character.
419 * If we fix it, it better be the last line of
422 if ((size_t)linelen + 1 > linecap) {
423 line = realloc(line, linelen + 2);
425 errlogx(EX_SOFTWARE, "realloc");
426 linecap = malloc_usable_size(line);
428 line[linelen++] = '\n';
432 if (!had_first_line) {
434 * Ignore a leading RFC-976 From_ or >From_ line mistakenly
435 * inserted by some programs.
437 if (strprefixcmp(line, "From ") == 0 || strprefixcmp(line, ">From ") == 0)
442 if (linelen > MAX_LINE_RFC822) {
443 /* XXX also split headers */
444 errlogx(EX_DATAERR, "bad mail input format:"
445 " from %s (uid %d) (envelope-from %s)",
446 username, useruid, queue->sender);
449 * Unless this is a continuation, switch of
450 * the Bcc: nocopy flag.
452 if (!(line[0] == ' ' || line[0] == '\t'))
455 if (strprefixcmp(line, "Date:") == 0)
457 else if (strprefixcmp(line, "Message-Id:") == 0)
459 else if (strprefixcmp(line, "From:") == 0)
461 else if (strprefixcmp(line, "Bcc:") == 0)
464 if (parse_state.state != NONE) {
465 if (parse_addrs(&parse_state, line, queue) < 0) {
466 errlogx(EX_DATAERR, "invalid address in header\n");
471 if (recp_from_header && (
472 strprefixcmp(line, "To:") == 0 ||
473 strprefixcmp(line, "Cc:") == 0 ||
474 strprefixcmp(line, "Bcc:") == 0)) {
475 parse_state.state = START;
476 if (parse_addrs(&parse_state, line, queue) < 0) {
477 errlogx(EX_DATAERR, "invalid address in header\n");
483 if (strcmp(line, "\n") == 0 && !had_headers) {
485 while (!had_date || !had_messagid || !had_from) {
488 snprintf(newline, sizeof(newline), "Date: %s\n", rfc822date());
489 } else if (!had_messagid) {
490 /* XXX msgid, assign earlier and log? */
492 snprintf(newline, sizeof(newline), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
493 (uintmax_t)time(NULL),
497 } else if (!had_from) {
499 snprintf(newline, sizeof(newline), "From: <%s>\n", queue->sender);
501 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
504 strlcpy(newline, "\n", sizeof(newline));
506 if (!nodot && linelen == 2 && line[0] == '.')
510 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
513 if (writeline(queue, line, linelen) != 0)
518 if (ferror(stdin) == 0)