2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 bounce(struct qitem *it, const char *reason)
51 /* Don't bounce bounced mails */
52 if (it->sender[0] == 0) {
53 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
57 bzero(&bounceq, sizeof(bounceq));
58 LIST_INIT(&bounceq.queue);
60 if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0)
63 if (newspoolf(&bounceq) != 0)
66 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
67 setlogident("%s", bounceq.id);
69 error = fprintf(bounceq.mailf,
70 "Received: from MAILER-DAEMON\n"
74 "X-Original-To: <%s>\n"
75 "From: MAILER-DAEMON <>\n"
77 "Subject: Mail delivery failed\n"
78 "Message-Id: <%s@%s>\n"
81 "This is the %s at %s.\n"
83 "There was an error delivering your mail to <%s>.\n"
94 bounceq.id, hostname(),
99 config.features & FULLBOUNCE ?
100 "Original message follows." :
101 "Message headers follow.");
105 if (fseek(it->mailf, 0, SEEK_SET) != 0)
107 if (config.features & FULLBOUNCE) {
108 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
109 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
113 while (!feof(it->mailf)) {
114 if (fgets(line, sizeof(line), it->mailf) == NULL)
118 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
123 if (linkspool(&bounceq) != 0)
133 syslog(LOG_CRIT, "error creating bounce: %m");
156 * Simplified RFC2822 header/address parsing.
157 * We copy escapes and quoted strings directly, since
158 * we have to pass them like this to the mail server anyways.
159 * XXX local addresses will need treatment
162 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
173 bzero(ps, sizeof(*ps));
175 /* skip over header name */
205 for (; *s != 0; s++) {
269 /* ignore whitespace */
273 /* this is the real address now */
294 * Next address, copy previous one.
295 * However, we might be directly after
296 * a <address>, or have two consecutive
298 * Skip the comma unless there is
299 * really something to copy.
314 if (ps->pos + 1 == sizeof(ps->addr))
316 ps->addr[ps->pos++] = *s;
331 ps->addr[ps->pos] = 0;
333 addr = strdup(ps->addr);
337 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
338 errlogx(1, "invalid recipient `%s'", addr);
344 readmail(struct queue *queue, int nodot, int recp_from_header)
346 struct parse_state parse_state;
347 char line[1000]; /* by RFC2822 */
352 int had_messagid = 0;
354 int had_last_line = 0;
357 parse_state.state = NONE;
359 error = fprintf(queue->mailf,
360 "Received: from %s (uid %d)\n"
361 "\t(envelope-from %s)\n"
370 if ((ssize_t)error < 0)
373 while (!feof(stdin)) {
374 if (fgets(line, sizeof(line) - 1, stdin) == NULL)
377 errlogx(1, "bad mail input format");
378 linelen = strlen(line);
379 if (linelen == 0 || line[linelen - 1] != '\n') {
381 * This line did not end with a newline character.
382 * If we fix it, it better be the last line of
385 line[linelen] = '\n';
386 line[linelen + 1] = 0;
391 * Unless this is a continuation, switch of
392 * the Bcc: nocopy flag.
394 if (!(line[0] == ' ' || line[0] == '\t'))
397 if (strprefixcmp(line, "Date:") == 0)
399 else if (strprefixcmp(line, "Message-Id:") == 0)
401 else if (strprefixcmp(line, "From:") == 0)
403 else if (strprefixcmp(line, "Bcc:") == 0)
406 if (parse_state.state != NONE) {
407 if (parse_addrs(&parse_state, line, queue) < 0) {
408 errlogx(1, "invalid address in header\n");
413 if (recp_from_header && (
414 strprefixcmp(line, "To:") == 0 ||
415 strprefixcmp(line, "Cc:") == 0 ||
416 strprefixcmp(line, "Bcc:") == 0)) {
417 parse_state.state = START;
418 if (parse_addrs(&parse_state, line, queue) < 0) {
419 errlogx(1, "invalid address in header\n");
425 if (strcmp(line, "\n") == 0 && !had_headers) {
427 while (!had_date || !had_messagid || !had_from) {
430 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
431 } else if (!had_messagid) {
432 /* XXX msgid, assign earlier and log? */
434 snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
435 (uintmax_t)time(NULL),
439 } else if (!had_from) {
441 snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
443 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
448 if (!nodot && linelen == 2 && line[0] == '.')
451 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)