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, NULL);
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;
391 parse_state.state = NONE;
393 error = fprintf(queue->mailf,
394 "Received: from %s (uid %d)\n"
395 "\t(envelope-from %s)\n"
397 "\tby %s (%s on %s);\n"
402 hostname(), VERSION, systemhostname(),
404 if ((ssize_t)error < 0)
407 while (!feof(stdin)) {
409 if ((linelen = getline(&line, &linecap, stdin)) <= 0)
411 if (!had_first_line) {
413 * Ignore a leading RFC-976 From_ or >From_ line mistakenly
414 * inserted by some programs.
416 if (strprefixcmp(line, "From ") == 0 || strprefixcmp(line, ">From ") == 0)
421 if (linelen > MAX_LINE_RFC822) {
422 errlogx(EX_DATAERR, "bad mail input format:"
423 " from %s (uid %d) (envelope-from %s)",
424 username, useruid, queue->sender);
427 * Unless this is a continuation, switch of
428 * the Bcc: nocopy flag.
430 if (!(line[0] == ' ' || line[0] == '\t'))
433 if (strprefixcmp(line, "Date:") == 0)
435 else if (strprefixcmp(line, "Message-Id:") == 0)
437 else if (strprefixcmp(line, "From:") == 0)
439 else if (strprefixcmp(line, "Bcc:") == 0)
442 if (parse_state.state != NONE) {
443 if (parse_addrs(&parse_state, line, queue) < 0) {
444 errlogx(EX_DATAERR, "invalid address in header\n");
449 if (recp_from_header && (
450 strprefixcmp(line, "To:") == 0 ||
451 strprefixcmp(line, "Cc:") == 0 ||
452 strprefixcmp(line, "Bcc:") == 0)) {
453 parse_state.state = START;
454 if (parse_addrs(&parse_state, line, queue) < 0) {
455 errlogx(EX_DATAERR, "invalid address in header\n");
461 if (strcmp(line, "\n") == 0 && !had_headers) {
463 while (!had_date || !had_messagid || !had_from) {
466 snprintf(newline, sizeof(newline), "Date: %s\n", rfc822date());
467 } else if (!had_messagid) {
468 /* XXX msgid, assign earlier and log? */
470 snprintf(newline, sizeof(newline), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
471 (uintmax_t)time(NULL),
475 } else if (!had_from) {
477 snprintf(newline, sizeof(newline), "From: <%s>\n", queue->sender);
479 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
482 strlcpy(newline, "\n", sizeof(newline));
484 if (!nodot && linelen == 2 && line[0] == '.')
488 if (fwrite(newline, strlen(newline), 1, queue->mailf) != 1)
491 if (writeline(queue, line, linelen) != 0)