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
42 bounce(struct qitem *it, const char *reason)
49 /* Don't bounce bounced mails */
50 if (it->sender[0] == 0) {
51 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
55 bzero(&bounceq, sizeof(bounceq));
56 LIST_INIT(&bounceq.queue);
58 if (add_recp(&bounceq, it->sender, 1) != 0)
61 if (newspoolf(&bounceq) != 0)
64 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
65 setlogident("%s", bounceq.id);
67 error = fprintf(bounceq.mailf,
68 "Received: from MAILER-DAEMON\n"
72 "X-Original-To: <%s>\n"
73 "From: MAILER-DAEMON <>\n"
75 "Subject: Mail delivery failed\n"
76 "Message-Id: <%s@%s>\n"
79 "This is the %s at %s.\n"
81 "There was an error delivering your mail to <%s>.\n"
92 bounceq.id, hostname(),
97 config.features & FULLBOUNCE ?
98 "Original message follows." :
99 "Message headers follow.");
103 if (fseek(it->mailf, 0, SEEK_SET) != 0)
105 if (config.features & FULLBOUNCE) {
106 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
107 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
111 while (!feof(it->mailf)) {
112 if (fgets(line, sizeof(line), it->mailf) == NULL)
116 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
121 if (linkspool(&bounceq) != 0)
131 syslog(LOG_CRIT, "error creating bounce: %m");
154 * Simplified RFC2822 header/address parsing.
155 * We copy escapes and quoted strings directly, since
156 * we have to pass them like this to the mail server anyways.
157 * XXX local addresses will need treatment
160 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
171 bzero(ps, sizeof(*ps));
173 /* skip over header name */
203 for (; *s != 0; s++) {
267 /* ignore whitespace */
301 if (ps->pos + 1 == sizeof(ps->addr))
303 ps->addr[ps->pos++] = *s;
318 ps->addr[ps->pos] = 0;
320 addr = strdup(ps->addr);
324 if (add_recp(queue, addr, 1) != 0)
325 errlogx(1, "invalid recipient `%s'", addr);
326 fprintf(stderr, "parsed `%s'\n", addr);
331 readmail(struct queue *queue, int nodot, int recp_from_header)
333 struct parse_state parse_state;
334 char line[1000]; /* by RFC2822 */
339 int had_messagid = 0;
343 parse_state.state = NONE;
345 error = fprintf(queue->mailf,
346 "Received: from %s (uid %d)\n"
347 "\t(envelope-from %s)\n"
356 if ((ssize_t)error < 0)
359 while (!feof(stdin)) {
360 if (fgets(line, sizeof(line), stdin) == NULL)
362 linelen = strlen(line);
363 if (linelen == 0 || line[linelen - 1] != '\n') {
364 errno = EINVAL; /* XXX mark permanent errors */
369 * Unless this is a continuation, switch of
370 * the Bcc: nocopy flag.
372 if (!(line[0] == ' ' || line[0] == '\t'))
375 if (strprefixcmp(line, "Date:") == 0)
377 else if (strprefixcmp(line, "Message-Id:") == 0)
379 else if (strprefixcmp(line, "From:") == 0)
381 else if (strprefixcmp(line, "Bcc:") == 0)
384 if (parse_state.state != NONE) {
385 if (parse_addrs(&parse_state, line, queue) < 0) {
386 errlogx(1, "invalid address in header\n");
391 if (recp_from_header && (
392 strprefixcmp(line, "To:") == 0 ||
393 strprefixcmp(line, "Cc:") == 0 ||
394 strprefixcmp(line, "Bcc:") == 0)) {
395 parse_state.state = START;
396 if (parse_addrs(&parse_state, line, queue) < 0) {
397 errlogx(1, "invalid address in header\n");
403 if (strcmp(line, "\n") == 0 && !had_headers) {
405 while (!had_date || !had_messagid || !had_from) {
408 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
409 } else if (!had_messagid) {
410 /* XXX better msgid, assign earlier and log? */
412 snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
413 queue->id, hostname());
414 } else if (!had_from) {
416 snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
418 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
423 if (!nodot && linelen == 2 && line[0] == '.')
426 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)