]> git.ipfire.org Git - people/ms/dma.git/blame - mail.c
dma: factor out mail handling code
[people/ms/dma.git] / mail.c
CommitLineData
28be0b96
SS
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 */
34
35#include <errno.h>
36#include <syslog.h>
37#include <unistd.h>
38
39#include "dma.h"
40
41void
42bounce(struct qitem *it, const char *reason)
43{
44 struct queue bounceq;
45 char line[1000];
46 size_t pos;
47 int error;
48
49 /* Don't bounce bounced mails */
50 if (it->sender[0] == 0) {
51 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
52 exit(1);
53 }
54
55 LIST_INIT(&bounceq.queue);
56 if (add_recp(&bounceq, it->sender, "", 1) != 0)
57 goto fail;
58
59 if (newspoolf(&bounceq, "") != 0)
60 goto fail;
61
62 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
63 setlogident("%s", bounceq.id);
64
65 error = fprintf(bounceq.mailf,
66 "Received: from MAILER-DAEMON\n"
67 "\tid %s\n"
68 "\tby %s (%s)\n"
69 "\t%s\n"
70 "X-Original-To: <%s>\n"
71 "From: MAILER-DAEMON <>\n"
72 "To: %s\n"
73 "Subject: Mail delivery failed\n"
74 "Message-Id: <%s@%s>\n"
75 "Date: %s\n"
76 "\n"
77 "This is the %s at %s.\n"
78 "\n"
79 "There was an error delivering your mail to <%s>.\n"
80 "\n"
81 "%s\n"
82 "\n"
83 "%s\n"
84 "\n",
85 bounceq.id,
86 hostname(), VERSION,
87 rfc822date(),
88 it->addr,
89 it->sender,
90 bounceq.id, hostname(),
91 rfc822date(),
92 VERSION, hostname(),
93 it->addr,
94 reason,
95 config->features & FULLBOUNCE ?
96 "Original message follows." :
97 "Message headers follow.");
98 if (error < 0)
99 goto fail;
100
101 if (fseek(it->mailf, it->hdrlen, SEEK_SET) != 0)
102 goto fail;
103 if (config->features & FULLBOUNCE) {
104 while ((pos = fread(line, 1, sizeof(line), it->mailf)) > 0) {
105 if (fwrite(line, 1, pos, bounceq.mailf) != pos)
106 goto fail;
107 }
108 } else {
109 while (!feof(it->mailf)) {
110 if (fgets(line, sizeof(line), it->mailf) == NULL)
111 break;
112 if (line[0] == '\n')
113 break;
114 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
115 goto fail;
116 }
117 }
118
119 if (linkspool(&bounceq, "") != 0)
120 goto fail;
121 /* bounce is safe */
122
123 delqueue(it);
124
125 run_queue(&bounceq);
126 /* NOTREACHED */
127
128fail:
129 syslog(LOG_CRIT, "error creating bounce: %m");
130 delqueue(it);
131 exit(1);
132}
133
134int
135readmail(struct queue *queue, const char *sender, int nodot)
136{
137 char line[1000]; /* by RFC2822 */
138 size_t linelen;
139 size_t error;
140 int had_headers = 0;
141 int had_from = 0;
142 int had_messagid = 0;
143 int had_date = 0;
144
145 error = fprintf(queue->mailf,
146 "Received: from %s (uid %d)\n"
147 "\t(envelope-from %s)\n"
148 "\tid %s\n"
149 "\tby %s (%s)\n"
150 "\t%s\n",
151 username, getuid(),
152 sender,
153 queue->id,
154 hostname(), VERSION,
155 rfc822date());
156 if ((ssize_t)error < 0)
157 return (-1);
158
159 while (!feof(stdin)) {
160 if (fgets(line, sizeof(line), stdin) == NULL)
161 break;
162 linelen = strlen(line);
163 if (linelen == 0 || line[linelen - 1] != '\n') {
164 errno = EINVAL; /* XXX mark permanent errors */
165 return (-1);
166 }
167 if (!had_headers) {
168 if (strprefixcmp(line, "Date:") == 0)
169 had_date = 1;
170 else if (strprefixcmp(line, "Message-Id:") == 0)
171 had_messagid = 1;
172 else if (strprefixcmp(line, "From:") == 0)
173 had_from = 1;
174 }
175 if (strcmp(line, "\n") == 0 && !had_headers) {
176 had_headers = 1;
177 while (!had_date || !had_messagid || !had_from) {
178 if (!had_date) {
179 had_date = 1;
180 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
181 } else if (!had_messagid) {
182 /* XXX better msgid, assign earlier and log? */
183 had_messagid = 1;
184 snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
185 queue->id, hostname());
186 } else if (!had_from) {
187 had_from = 1;
188 snprintf(line, sizeof(line), "From: <%s>\n", sender);
189 }
190 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
191 return (-1);
192 }
193 strcpy(line, "\n");
194 }
195 if (!nodot && linelen == 2 && line[0] == '.')
196 break;
197 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
198 return (-1);
199 }
200
201 return (0);
202}