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 bounce(struct qitem *it, const char *reason)
52 /* Don't bounce bounced mails */
53 if (it->sender[0] == 0) {
54 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
58 bzero(&bounceq, sizeof(bounceq));
59 LIST_INIT(&bounceq.queue);
61 if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0)
64 if (newspoolf(&bounceq) != 0)
67 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
68 setlogident("%s", bounceq.id);
70 error = fprintf(bounceq.mailf,
71 "Received: from MAILER-DAEMON\n"
75 "X-Original-To: <%s>\n"
76 "From: MAILER-DAEMON <>\n"
78 "Subject: Mail delivery failed\n"
79 "Message-Id: <%s@%s>\n"
82 "This is the %s at %s.\n"
84 "There was an error delivering your mail to <%s>.\n"
95 bounceq.id, hostname(),
100 config.features & FULLBOUNCE ?
101 "Original message follows." :
102 "Message headers follow.");
106 if (fseek(it->mailf, 0, SEEK_SET) != 0)
108 if (config.features & FULLBOUNCE) {
109 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
110 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
114 while (!feof(it->mailf)) {
115 if (fgets(line, sizeof(line), it->mailf) == NULL)
119 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
124 if (linkspool(&bounceq) != 0)
134 syslog(LOG_CRIT, "error creating bounce: %m");
157 * Simplified RFC2822 header/address parsing.
158 * We copy escapes and quoted strings directly, since
159 * we have to pass them like this to the mail server anyways.
160 * XXX local addresses will need treatment
163 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
174 bzero(ps, sizeof(*ps));
176 /* skip over header name */
207 for (; *s != 0; s++) {
271 /* ignore whitespace */
275 /* this is the real address now */
296 * Next address, copy previous one.
297 * However, we might be directly after
298 * a <address>, or have two consecutive
300 * Skip the comma unless there is
301 * really something to copy.
316 if (ps->pos + 1 == sizeof(ps->addr))
318 ps->addr[ps->pos++] = *s;
333 ps->addr[ps->pos] = 0;
335 addr = strdup(ps->addr);
339 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
340 errlogx(1, "invalid recipient `%s'", addr);
346 readmail(struct queue *queue, int nodot, int recp_from_header)
348 struct parse_state parse_state;
349 char line[1000]; /* by RFC2822 */
354 int had_messagid = 0;
356 int had_last_line = 0;
359 parse_state.state = NONE;
361 error = fprintf(queue->mailf,
362 "Received: from %s (uid %d)\n"
363 "\t(envelope-from %s)\n"
372 if ((ssize_t)error < 0)
375 while (!feof(stdin)) {
376 if (fgets(line, sizeof(line) - 1, stdin) == NULL)
379 errlogx(1, "bad mail input format:"
380 " from %s (uid %d) (envelope-from %s)",
381 username, useruid, queue->sender);
382 linelen = strlen(line);
383 if (linelen == 0 || line[linelen - 1] != '\n') {
385 * This line did not end with a newline character.
386 * If we fix it, it better be the last line of
389 line[linelen] = '\n';
390 line[linelen + 1] = 0;
395 * Unless this is a continuation, switch of
396 * the Bcc: nocopy flag.
398 if (!(line[0] == ' ' || line[0] == '\t'))
401 if (strprefixcmp(line, "Date:") == 0)
403 else if (strprefixcmp(line, "Message-Id:") == 0)
405 else if (strprefixcmp(line, "From:") == 0)
407 else if (strprefixcmp(line, "Bcc:") == 0)
410 if (parse_state.state != NONE) {
411 if (parse_addrs(&parse_state, line, queue) < 0) {
412 errlogx(1, "invalid address in header\n");
417 if (recp_from_header && (
418 strprefixcmp(line, "To:") == 0 ||
419 strprefixcmp(line, "Cc:") == 0 ||
420 strprefixcmp(line, "Bcc:") == 0)) {
421 parse_state.state = START;
422 if (parse_addrs(&parse_state, line, queue) < 0) {
423 errlogx(1, "invalid address in header\n");
429 if (strcmp(line, "\n") == 0 && !had_headers) {
431 while (!had_date || !had_messagid || !had_from) {
434 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
435 } else if (!had_messagid) {
436 /* XXX msgid, assign earlier and log? */
438 snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
439 (uintmax_t)time(NULL),
443 } else if (!had_from) {
445 snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
447 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
452 if (!nodot && linelen == 2 && line[0] == '.')
455 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)