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
45 #define MAX_LINE_RFC822 1000
48 bounce(struct qitem *it, const char *reason)
55 /* Don't bounce bounced mails */
56 if (it->sender[0] == 0) {
57 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
61 bzero(&bounceq, sizeof(bounceq));
62 LIST_INIT(&bounceq.queue);
64 if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0)
67 if (newspoolf(&bounceq) != 0)
70 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
71 setlogident("%s", bounceq.id);
73 error = fprintf(bounceq.mailf,
74 "Received: from MAILER-DAEMON\n"
78 "X-Original-To: <%s>\n"
79 "From: MAILER-DAEMON <>\n"
81 "Subject: Mail delivery failed\n"
82 "Message-Id: <%s@%s>\n"
85 "This is the %s at %s.\n"
87 "There was an error delivering your mail to <%s>.\n"
98 bounceq.id, hostname(),
103 config.features & FULLBOUNCE ?
104 "Original message follows." :
105 "Message headers follow.");
109 if (fseek(it->mailf, 0, SEEK_SET) != 0)
111 if (config.features & FULLBOUNCE) {
112 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
113 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
117 while (!feof(it->mailf)) {
118 if (fgets(line, sizeof(line), it->mailf) == NULL)
122 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
127 if (linkspool(&bounceq) != 0)
137 syslog(LOG_CRIT, "error creating bounce: %m");
160 * Simplified RFC2822 header/address parsing.
161 * We copy escapes and quoted strings directly, since
162 * we have to pass them like this to the mail server anyways.
163 * XXX local addresses will need treatment
166 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
177 bzero(ps, sizeof(*ps));
179 /* skip over header name */
209 for (; *s != 0; s++) {
273 /* ignore whitespace */
277 /* this is the real address now */
298 * Next address, copy previous one.
299 * However, we might be directly after
300 * a <address>, or have two consecutive
302 * Skip the comma unless there is
303 * really something to copy.
318 if (ps->pos + 1 == sizeof(ps->addr))
320 ps->addr[ps->pos++] = *s;
335 ps->addr[ps->pos] = 0;
337 addr = strdup(ps->addr);
339 errlog(EX_SOFTWARE, "strdup");
341 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
342 errlogx(EX_DATAERR, "invalid recipient `%s'", addr);
348 writeline(struct queue *queue, const char *line, ssize_t linelen)
352 while (linelen > 0) {
354 if (linelen > MAX_LINE_RFC822) {
355 len = MAX_LINE_RFC822 - 10;
358 if (fwrite(line, len, 1, queue->mailf) != 1)
361 if (linelen <= MAX_LINE_RFC822)
364 if (fwrite("\n", 1, 1, queue->mailf) != 1)
367 line += MAX_LINE_RFC822 - 10;
368 linelen = strlen(line);
374 readmail(struct queue *queue, int nodot, int recp_from_header)
376 struct parse_state parse_state;
380 char newline[MAX_LINE_RFC822];
384 int had_messagid = 0;
389 parse_state.state = NONE;
391 error = fprintf(queue->mailf,
392 "Received: from %s (uid %d)\n"
393 "\t(envelope-from %s)\n"
402 if ((ssize_t)error < 0)
405 while (!feof(stdin)) {
407 if ((linelen = getline(&line, &linecap, stdin)) <= 0)
411 if (linelen > MAX_LINE_RFC822) {
412 /* XXX also split headers */
413 errlogx(EX_DATAERR, "bad mail input format:"
414 " from %s (uid %d) (envelope-from %s)",
415 username, useruid, queue->sender);
418 * Unless this is a continuation, switch of
419 * the Bcc: nocopy flag.
421 if (!(line[0] == ' ' || line[0] == '\t'))
424 if (strprefixcmp(line, "Date:") == 0)
426 else if (strprefixcmp(line, "Message-Id:") == 0)
428 else if (strprefixcmp(line, "From:") == 0)
430 else if (strprefixcmp(line, "Bcc:") == 0)
433 if (parse_state.state != NONE) {
434 if (parse_addrs(&parse_state, line, queue) < 0) {
435 errlogx(EX_DATAERR, "invalid address in header\n");
440 if (recp_from_header && (
441 strprefixcmp(line, "To:") == 0 ||
442 strprefixcmp(line, "Cc:") == 0 ||
443 strprefixcmp(line, "Bcc:") == 0)) {
444 parse_state.state = START;
445 if (parse_addrs(&parse_state, line, queue) < 0) {
446 errlogx(EX_DATAERR, "invalid address in header\n");
452 if (strcmp(line, "\n") == 0 && !had_headers) {
454 while (!had_date || !had_messagid || !had_from) {
457 snprintf(newline, sizeof(newline), "Date: %s\n", rfc822date());
458 } else if (!had_messagid) {
459 /* XXX msgid, assign earlier and log? */
461 snprintf(newline, sizeof(newline), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
462 (uintmax_t)time(NULL),
466 } else if (!had_from) {
468 snprintf(newline, sizeof(newline), "From: <%s>\n", queue->sender);
470 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
473 strlcpy(newline, "\n", sizeof(newline));
475 if (!nodot && linelen == 2 && line[0] == '.')
479 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
482 if (writeline(queue, line, linelen) != 0)