]> git.ipfire.org Git - people/ms/dma.git/blob - mail.c
make ppa: force lower version number
[people/ms/dma.git] / mail.c
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 <inttypes.h>
37 #include <signal.h>
38 #include <syslog.h>
39 #include <unistd.h>
40
41 #include "dma.h"
42
43 void
44 bounce(struct qitem *it, const char *reason)
45 {
46 struct queue bounceq;
47 char line[1000];
48 size_t pos;
49 int error;
50
51 /* Don't bounce bounced mails */
52 if (it->sender[0] == 0) {
53 syslog(LOG_INFO, "can not bounce a bounce message, discarding");
54 exit(1);
55 }
56
57 bzero(&bounceq, sizeof(bounceq));
58 LIST_INIT(&bounceq.queue);
59 bounceq.sender = "";
60 if (add_recp(&bounceq, it->sender, 1) != 0)
61 goto fail;
62
63 if (newspoolf(&bounceq) != 0)
64 goto fail;
65
66 syslog(LOG_ERR, "delivery failed, bouncing as %s", bounceq.id);
67 setlogident("%s", bounceq.id);
68
69 error = fprintf(bounceq.mailf,
70 "Received: from MAILER-DAEMON\n"
71 "\tid %s\n"
72 "\tby %s (%s)\n"
73 "\t%s\n"
74 "X-Original-To: <%s>\n"
75 "From: MAILER-DAEMON <>\n"
76 "To: %s\n"
77 "Subject: Mail delivery failed\n"
78 "Message-Id: <%s@%s>\n"
79 "Date: %s\n"
80 "\n"
81 "This is the %s at %s.\n"
82 "\n"
83 "There was an error delivering your mail to <%s>.\n"
84 "\n"
85 "%s\n"
86 "\n"
87 "%s\n"
88 "\n",
89 bounceq.id,
90 hostname(), VERSION,
91 rfc822date(),
92 it->addr,
93 it->sender,
94 bounceq.id, hostname(),
95 rfc822date(),
96 VERSION, hostname(),
97 it->addr,
98 reason,
99 config.features & FULLBOUNCE ?
100 "Original message follows." :
101 "Message headers follow.");
102 if (error < 0)
103 goto fail;
104
105 if (fseek(it->mailf, 0, SEEK_SET) != 0)
106 goto fail;
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)
110 goto fail;
111 }
112 } else {
113 while (!feof(it->mailf)) {
114 if (fgets(line, sizeof(line), it->mailf) == NULL)
115 break;
116 if (line[0] == '\n')
117 break;
118 if (fwrite(line, strlen(line), 1, bounceq.mailf) != 1)
119 goto fail;
120 }
121 }
122
123 if (linkspool(&bounceq) != 0)
124 goto fail;
125 /* bounce is safe */
126
127 delqueue(it);
128
129 run_queue(&bounceq);
130 /* NOTREACHED */
131
132 fail:
133 syslog(LOG_CRIT, "error creating bounce: %m");
134 delqueue(it);
135 exit(1);
136 }
137
138 struct parse_state {
139 char addr[1000];
140 int pos;
141
142 enum {
143 NONE = 0,
144 START,
145 MAIN,
146 EOL,
147 QUIT
148 } state;
149 int comment;
150 int quote;
151 int brackets;
152 int esc;
153 };
154
155 /*
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
160 */
161 static int
162 parse_addrs(struct parse_state *ps, char *s, struct queue *queue)
163 {
164 char *addr;
165
166 again:
167 switch (ps->state) {
168 case NONE:
169 return (-1);
170
171 case START:
172 /* init our data */
173 bzero(ps, sizeof(*ps));
174
175 /* skip over header name */
176 while (*s != ':')
177 s++;
178 s++;
179 ps->state = MAIN;
180 break;
181
182 case MAIN:
183 /* all fine */
184 break;
185
186 case EOL:
187 switch (*s) {
188 case ' ':
189 case '\t':
190 s++;
191 /* continue */
192 break;
193
194 default:
195 ps->state = QUIT;
196 if (ps->pos != 0)
197 goto newaddr;
198 return (0);
199 }
200
201 case QUIT:
202 return (0);
203 }
204
205 for (; *s != 0; s++) {
206 if (ps->esc) {
207 ps->esc = 0;
208
209 switch (*s) {
210 case '\r':
211 case '\n':
212 goto err;
213
214 default:
215 goto copy;
216 }
217 }
218
219 if (ps->quote) {
220 switch (*s) {
221 case '"':
222 ps->quote = 0;
223 goto copy;
224
225 case '\\':
226 ps->esc = 1;
227 goto copy;
228
229 case '\r':
230 case '\n':
231 goto eol;
232
233 default:
234 goto copy;
235 }
236 }
237
238 switch (*s) {
239 case '(':
240 ps->comment++;
241 break;
242
243 case ')':
244 if (ps->comment)
245 ps->comment--;
246 else
247 goto err;
248 goto skip;
249
250 case '"':
251 ps->quote = 1;
252 goto copy;
253
254 case '\\':
255 ps->esc = 1;
256 goto copy;
257
258 case '\r':
259 case '\n':
260 goto eol;
261 }
262
263 if (ps->comment)
264 goto skip;
265
266 switch (*s) {
267 case ' ':
268 case '\t':
269 /* ignore whitespace */
270 goto skip;
271
272 case '<':
273 /* this is the real address now */
274 ps->brackets = 1;
275 ps->pos = 0;
276 goto skip;
277
278 case '>':
279 if (!ps->brackets)
280 goto err;
281 ps->brackets = 0;
282
283 s++;
284 goto newaddr;
285
286 case ':':
287 /* group - ignore */
288 ps->pos = 0;
289 goto skip;
290
291 case ',':
292 case ';':
293 s++;
294 goto newaddr;
295
296 default:
297 goto copy;
298 }
299
300 copy:
301 if (ps->comment)
302 goto skip;
303
304 if (ps->pos + 1 == sizeof(ps->addr))
305 goto err;
306 ps->addr[ps->pos++] = *s;
307
308 skip:
309 ;
310 }
311
312 eol:
313 ps->state = EOL;
314 return (0);
315
316 err:
317 ps->state = QUIT;
318 return (-1);
319
320 newaddr:
321 ps->addr[ps->pos] = 0;
322 ps->pos = 0;
323 addr = strdup(ps->addr);
324 if (addr == NULL)
325 errlog(1, NULL);
326
327 if (add_recp(queue, addr, 1) != 0)
328 errlogx(1, "invalid recipient `%s'", addr);
329
330 goto again;
331 }
332
333 int
334 readmail(struct queue *queue, int nodot, int recp_from_header)
335 {
336 struct parse_state parse_state;
337 char line[1000]; /* by RFC2822 */
338 size_t linelen;
339 size_t error;
340 int had_headers = 0;
341 int had_from = 0;
342 int had_messagid = 0;
343 int had_date = 0;
344 int nocopy = 0;
345
346 parse_state.state = NONE;
347
348 error = fprintf(queue->mailf,
349 "Received: from %s (uid %d)\n"
350 "\t(envelope-from %s)\n"
351 "\tid %s\n"
352 "\tby %s (%s)\n"
353 "\t%s\n",
354 username, useruid,
355 queue->sender,
356 queue->id,
357 hostname(), VERSION,
358 rfc822date());
359 if ((ssize_t)error < 0)
360 return (-1);
361
362 while (!feof(stdin)) {
363 if (fgets(line, sizeof(line), stdin) == NULL)
364 break;
365 linelen = strlen(line);
366 if (linelen == 0 || line[linelen - 1] != '\n') {
367 errno = EINVAL; /* XXX mark permanent errors */
368 return (-1);
369 }
370 if (!had_headers) {
371 /*
372 * Unless this is a continuation, switch of
373 * the Bcc: nocopy flag.
374 */
375 if (!(line[0] == ' ' || line[0] == '\t'))
376 nocopy = 0;
377
378 if (strprefixcmp(line, "Date:") == 0)
379 had_date = 1;
380 else if (strprefixcmp(line, "Message-Id:") == 0)
381 had_messagid = 1;
382 else if (strprefixcmp(line, "From:") == 0)
383 had_from = 1;
384 else if (strprefixcmp(line, "Bcc:") == 0)
385 nocopy = 1;
386
387 if (parse_state.state != NONE) {
388 if (parse_addrs(&parse_state, line, queue) < 0) {
389 errlogx(1, "invalid address in header\n");
390 /* NOTREACHED */
391 }
392 }
393
394 if (recp_from_header && (
395 strprefixcmp(line, "To:") == 0 ||
396 strprefixcmp(line, "Cc:") == 0 ||
397 strprefixcmp(line, "Bcc:") == 0)) {
398 parse_state.state = START;
399 if (parse_addrs(&parse_state, line, queue) < 0) {
400 errlogx(1, "invalid address in header\n");
401 /* NOTREACHED */
402 }
403 }
404 }
405
406 if (strcmp(line, "\n") == 0 && !had_headers) {
407 had_headers = 1;
408 while (!had_date || !had_messagid || !had_from) {
409 if (!had_date) {
410 had_date = 1;
411 snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
412 } else if (!had_messagid) {
413 /* XXX msgid, assign earlier and log? */
414 had_messagid = 1;
415 snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n",
416 (uintmax_t)time(NULL),
417 queue->id,
418 random(),
419 hostname());
420 } else if (!had_from) {
421 had_from = 1;
422 snprintf(line, sizeof(line), "From: <%s>\n", queue->sender);
423 }
424 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
425 return (-1);
426 }
427 strcpy(line, "\n");
428 }
429 if (!nodot && linelen == 2 && line[0] == '.')
430 break;
431 if (!nocopy) {
432 if (fwrite(line, strlen(line), 1, queue->mailf) != 1)
433 return (-1);
434 }
435 }
436
437 return (0);
438 }