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
44 #define MAX_LINE_RFC822 1000
47 bounce(struct qitem *it, const char *reason)
54 /* Don't bounce bounced mails */
55 if (it->sender[0] == 0) {
56 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
60 bzero(&bounceq, sizeof(bounceq));
61 LIST_INIT(&bounceq.queue);
63 if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0)
66 if (newspoolf(&bounceq) != 0)
69 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
70 setlogident("%s", bounceq.id);
72 error = fprintf(bounceq.mailf,
73 "Received: from MAILER-DAEMON\n"
77 "X-Original-To: <%s>\n"
78 "From: MAILER-DAEMON <>\n"
80 "Subject: Mail delivery failed\n"
81 "Message-Id: <%s@%s>\n"
84 "This is the %s at %s.\n"
86 "There was an error delivering your mail to <%s>.\n"
97 bounceq.id, hostname(),
102 config.features & FULLBOUNCE ?
103 "Original message follows." :
104 "Message headers follow.");
108 if (fseek(it->mailf, 0, SEEK_SET) != 0)
110 if (config.features & FULLBOUNCE) {
111 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
112 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
116 while (!feof(it->mailf)) {
117 if (fgets(line, sizeof(line), it->mailf) == NULL)
121 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
126 if (linkspool(&bounceq) != 0)
136 syslog(LOG_CRIT, "error creating bounce: %m");
159 * Simplified RFC2822 header/address parsing.
160 * We copy escapes and quoted strings directly, since
161 * we have to pass them like this to the mail server anyways.
162 * XXX local addresses will need treatment
165 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
176 bzero(ps, sizeof(*ps));
178 /* skip over header name */
208 for (; *s != 0; s++) {
272 /* ignore whitespace */
276 /* this is the real address now */
297 * Next address, copy previous one.
298 * However, we might be directly after
299 * a <address>, or have two consecutive
301 * Skip the comma unless there is
302 * really something to copy.
317 if (ps->pos + 1 == sizeof(ps->addr))
319 ps->addr[ps->pos++] = *s;
334 ps->addr[ps->pos] = 0;
336 addr = strdup(ps->addr);
338 errlog(EX_SOFTWARE, "strdup");
340 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
341 errlogx(EX_DATAERR, "invalid recipient `%s'", addr);
347 writeline(struct queue *queue, const char *line, ssize_t linelen)
351 while (linelen > 0) {
353 if (linelen > MAX_LINE_RFC822) {
354 len = MAX_LINE_RFC822 - 10;
357 if (fwrite(line, len, 1, queue->mailf) != 1)
360 if (linelen <= MAX_LINE_RFC822)
363 if (fwrite("\n", 1, 1, queue->mailf) != 1)
366 line += MAX_LINE_RFC822 - 10;
367 linelen = strlen(line);
373 readmail(struct queue *queue, int nodot, int recp_from_header)
375 struct parse_state parse_state;
379 char newline[MAX_LINE_RFC822];
383 int had_messagid = 0;
388 parse_state.state = NONE;
390 error = fprintf(queue->mailf,
391 "Received: from %s (uid %d)\n"
392 "\t(envelope-from %s)\n"
401 if ((ssize_t)error < 0)
404 while (!feof(stdin)) {
406 if ((linelen = getline(&line, &linecap, stdin)) <= 0)
410 if (linelen > MAX_LINE_RFC822) {
411 /* XXX also split headers */
412 errlogx(EX_DATAERR, "bad mail input format:"
413 " from %s (uid %d) (envelope-from %s)",
414 username, useruid, queue->sender);
417 * Unless this is a continuation, switch of
418 * the Bcc: nocopy flag.
420 if (!(line[0] == ' ' || line[0] == '\t'))
423 if (strprefixcmp(line, "Date:") == 0)
425 else if (strprefixcmp(line, "Message-Id:") == 0)
427 else if (strprefixcmp(line, "From:") == 0)
429 else if (strprefixcmp(line, "Bcc:") == 0)
432 if (parse_state.state != NONE) {
433 if (parse_addrs(&parse_state, line, queue) < 0) {
434 errlogx(EX_DATAERR, "invalid address in header\n");
439 if (recp_from_header && (
440 strprefixcmp(line, "To:") == 0 ||
441 strprefixcmp(line, "Cc:") == 0 ||
442 strprefixcmp(line, "Bcc:") == 0)) {
443 parse_state.state = START;
444 if (parse_addrs(&parse_state, line, queue) < 0) {
445 errlogx(EX_DATAERR, "invalid address in header\n");
451 if (strcmp(line, "\n") == 0 && !had_headers) {
453 while (!had_date || !had_messagid || !had_from) {
456 snprintf(newline, sizeof(newline), "Date: %s\n", rfc822date());
457 } else if (!had_messagid) {
458 /* XXX msgid, assign earlier and log? */
460 snprintf(newline, sizeof(newline), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
461 (uintmax_t)time(NULL),
465 } else if (!had_from) {
467 snprintf(newline, sizeof(newline), "From: <%s>\n", queue->sender);
469 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
472 strlcpy(newline, "\n", sizeof(newline));
474 if (!nodot && linelen == 2 && line[0] == '.')
478 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
481 if (writeline(queue, line, linelen) != 0)