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
46 #define MAX_LINE_RFC822 1000
49 bounce(struct qitem *it, const char *reason)
56 /* Don't bounce bounced mails */
57 if (it->sender[0] == 0) {
58 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
62 bzero(&bounceq, sizeof(bounceq));
63 LIST_INIT(&bounceq.queue);
65 if (add_recp(&bounceq, it->sender, EXPAND_WILDCARD) != 0)
68 if (newspoolf(&bounceq) != 0)
71 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
72 setlogident("%s", bounceq.id);
74 error = fprintf(bounceq.mailf,
75 "Received: from MAILER-DAEMON\n"
77 "\tby %s (%s on %s);\n"
79 "X-Original-To: <%s>\n"
80 "From: MAILER-DAEMON <>\n"
82 "Subject: Mail delivery failed\n"
83 "Message-Id: <%s@%s>\n"
86 "This is the %s at %s.\n"
88 "There was an error delivering your mail to <%s>.\n"
95 hostname(), VERSION, systemhostname(),
99 bounceq.id, hostname(),
104 config.features & FULLBOUNCE ?
105 "Original message follows." :
106 "Message headers follow.");
110 if (fseek(it->mailf, 0, SEEK_SET) != 0)
112 if (config.features & FULLBOUNCE) {
113 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
114 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
118 while (!feof(it->mailf)) {
119 if (fgets(line, sizeof(line), it->mailf) == NULL)
123 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
128 if (linkspool(&bounceq) != 0)
138 syslog(LOG_CRIT, "error creating bounce: %m");
161 * Simplified RFC2822 header/address parsing.
162 * We copy escapes and quoted strings directly, since
163 * we have to pass them like this to the mail server anyways.
164 * XXX local addresses will need treatment
167 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
178 bzero(ps, sizeof(*ps));
180 /* skip over header name */
210 for (; *s != 0; s++) {
274 /* ignore whitespace */
278 /* this is the real address now */
299 * Next address, copy previous one.
300 * However, we might be directly after
301 * a <address>, or have two consecutive
303 * Skip the comma unless there is
304 * really something to copy.
319 if (ps->pos + 1 == sizeof(ps->addr))
321 ps->addr[ps->pos++] = *s;
336 ps->addr[ps->pos] = 0;
338 addr = strdup(ps->addr);
340 errlog(EX_SOFTWARE, "strdup");
342 if (add_recp(queue, addr, EXPAND_WILDCARD) != 0)
343 errlogx(EX_DATAERR, "invalid recipient `%s'", addr);
349 writeline(struct queue *queue, const char *line, ssize_t linelen)
353 while (linelen > 0) {
355 if (linelen > MAX_LINE_RFC822) {
356 len = MAX_LINE_RFC822 - 10;
359 if (fwrite(line, len, 1, queue->mailf) != 1)
362 if (linelen <= MAX_LINE_RFC822)
365 if (fwrite("\n", 1, 1, queue->mailf) != 1)
368 line += MAX_LINE_RFC822 - 10;
369 linelen = strlen(line);
375 readmail(struct queue *queue, int nodot, int recp_from_header)
377 struct parse_state parse_state;
381 char newline[MAX_LINE_RFC822];
385 int had_messagid = 0;
387 int had_first_line = 0;
388 int had_last_line = 0;
392 parse_state.state = NONE;
394 error = fprintf(queue->mailf,
395 "Received: from %s (uid %d)\n"
396 "\t(envelope-from %s)\n"
398 "\tby %s (%s on %s);\n"
403 hostname(), VERSION, systemhostname(),
405 if ((ssize_t)error < 0)
408 while (!feof(stdin)) {
410 if ((linelen = getline(&line, &linecap, stdin)) <= 0)
413 errlogx(EX_DATAERR, "bad mail input format:"
414 " from %s (uid %d) (envelope-from %s)",
415 username, useruid, queue->sender);
416 linelen = strlen(line);
417 if (linelen == 0 || line[linelen - 1] != '\n') {
419 * This line did not end with a newline character.
420 * If we fix it, it better be the last line of
423 line[linelen] = '\n';
424 line[linelen + 1] = 0;
427 if (!had_first_line) {
429 * Ignore a leading RFC-976 From_ or >From_ line mistakenly
430 * inserted by some programs.
432 if (strprefixcmp(line, "From ") == 0 || strprefixcmp(line, ">From ") == 0)
437 if (linelen > MAX_LINE_RFC822) {
438 /* XXX also split headers */
439 errlogx(EX_DATAERR, "bad mail input format:"
440 " from %s (uid %d) (envelope-from %s)",
441 username, useruid, queue->sender);
444 * Unless this is a continuation, switch of
445 * the Bcc: nocopy flag.
447 if (!(line[0] == ' ' || line[0] == '\t'))
450 if (strprefixcmp(line, "Date:") == 0)
452 else if (strprefixcmp(line, "Message-Id:") == 0)
454 else if (strprefixcmp(line, "From:") == 0)
456 else if (strprefixcmp(line, "Bcc:") == 0)
459 if (parse_state.state != NONE) {
460 if (parse_addrs(&parse_state, line, queue) < 0) {
461 errlogx(EX_DATAERR, "invalid address in header\n");
466 if (recp_from_header && (
467 strprefixcmp(line, "To:") == 0 ||
468 strprefixcmp(line, "Cc:") == 0 ||
469 strprefixcmp(line, "Bcc:") == 0)) {
470 parse_state.state = START;
471 if (parse_addrs(&parse_state, line, queue) < 0) {
472 errlogx(EX_DATAERR, "invalid address in header\n");
478 if (strcmp(line, "\n") == 0 && !had_headers) {
480 while (!had_date || !had_messagid || !had_from) {
483 snprintf(newline, sizeof(newline), "Date: %s\n", rfc822date());
484 } else if (!had_messagid) {
485 /* XXX msgid, assign earlier and log? */
487 snprintf(newline, sizeof(newline), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
488 (uintmax_t)time(NULL),
492 } else if (!had_from) {
494 snprintf(newline, sizeof(newline), "From: <%s>\n", queue->sender);
496 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
499 strlcpy(newline, "\n", sizeof(newline));
501 if (!nodot && linelen == 2 && line[0] == '.')
505 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
508 if (writeline(queue, line, linelen) != 0)