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 */
206 for (; *s != 0; s++) {
270 /* ignore whitespace */
274 /* this is the real address now */
295 * Next address, copy previous one.
296 * However, we might be directly after
297 * a <address>, or have two consecutive
299 * Skip the comma unless there is
300 * really something to copy.
315 if (ps->pos + 1 == sizeof(ps->addr))
317 ps->addr[ps->pos++] = *s;
332 ps->addr[ps->pos] = 0;
334 addr = strdup(ps->addr);
338 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
339 errlogx(1, "invalid recipient `%s'", addr);
345 readmail(struct queue *queue, int nodot, int recp_from_header)
347 struct parse_state parse_state;
348 char line[1000]; /* by RFC2822 */
353 int had_messagid = 0;
355 int had_last_line = 0;
358 parse_state.state = NONE;
360 error = fprintf(queue->mailf,
361 "Received: from %s (uid %d)\n"
362 "\t(envelope-from %s)\n"
371 if ((ssize_t)error < 0)
374 while (!feof(stdin)) {
375 if (fgets(line, sizeof(line) - 1, stdin) == NULL)
378 errlogx(1, "bad mail input format:"
379 " from %s (uid %d) (envelope-from %s)",
380 username, useruid, queue->sender);
381 linelen = strlen(line);
382 if (linelen == 0 || line[linelen - 1] != '\n') {
384 * This line did not end with a newline character.
385 * If we fix it, it better be the last line of
388 line[linelen] = '\n';
389 line[linelen + 1] = 0;
394 * Unless this is a continuation, switch of
395 * the Bcc: nocopy flag.
397 if (!(line[0] == ' ' || line[0] == '\t'))
400 if (strprefixcmp(line, "Date:") == 0)
402 else if (strprefixcmp(line, "Message-Id:") == 0)
404 else if (strprefixcmp(line, "From:") == 0)
406 else if (strprefixcmp(line, "Bcc:") == 0)
409 if (parse_state.state != NONE) {
410 if (parse_addrs(&parse_state, line, queue) < 0) {
411 errlogx(1, "invalid address in header\n");
416 if (recp_from_header && (
417 strprefixcmp(line, "To:") == 0 ||
418 strprefixcmp(line, "Cc:") == 0 ||
419 strprefixcmp(line, "Bcc:") == 0)) {
420 parse_state.state = START;
421 if (parse_addrs(&parse_state, line, queue) < 0) {
422 errlogx(1, "invalid address in header\n");
428 if (strcmp(line, "\n") == 0 && !had_headers) {
430 while (!had_date || !had_messagid || !had_from) {
433 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
434 } else if (!had_messagid) {
435 /* XXX msgid, assign earlier and log? */
437 snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
438 (uintmax_t)time(NULL),
442 } else if (!had_from) {
444 snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
446 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
451 if (!nodot && linelen == 2 && line[0] == '.')
454 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)