]>
Commit | Line | Data |
---|---|---|
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> | |
ee613428 SS |
36 | #include <inttypes.h> |
37 | #include <signal.h> | |
28be0b96 SS |
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 | ||
6f5efe4f | 57 | bzero(&bounceq, sizeof(bounceq)); |
28be0b96 | 58 | LIST_INIT(&bounceq.queue); |
057afad5 SS |
59 | bounceq.sender = ""; |
60 | if (add_recp(&bounceq, it->sender, 1) != 0) | |
28be0b96 SS |
61 | goto fail; |
62 | ||
057afad5 | 63 | if (newspoolf(&bounceq) != 0) |
28be0b96 SS |
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, | |
a0c4afa6 | 99 | config.features & FULLBOUNCE ? |
28be0b96 SS |
100 | "Original message follows." : |
101 | "Message headers follow."); | |
102 | if (error < 0) | |
103 | goto fail; | |
104 | ||
6ddd63e1 | 105 | if (fseek(it->mailf, 0, SEEK_SET) != 0) |
28be0b96 | 106 | goto fail; |
a0c4afa6 | 107 | if (config.features & FULLBOUNCE) { |
28be0b96 SS |
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 | ||
057afad5 | 123 | if (linkspool(&bounceq) != 0) |
28be0b96 SS |
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 | ||
f734c0ae SS |
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 '<': | |
1aa8a6d7 | 273 | /* this is the real address now */ |
f734c0ae | 274 | ps->brackets = 1; |
1aa8a6d7 | 275 | ps->pos = 0; |
f734c0ae SS |
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 | ||
72ff257f PP |
327 | if (add_recp(queue, addr, 1) != 0) |
328 | errlogx(1, "invalid recipient `%s'", addr); | |
329 | ||
f734c0ae SS |
330 | goto again; |
331 | } | |
332 | ||
28be0b96 | 333 | int |
f734c0ae | 334 | readmail(struct queue *queue, int nodot, int recp_from_header) |
28be0b96 | 335 | { |
f734c0ae | 336 | struct parse_state parse_state; |
28be0b96 SS |
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; | |
807f777a | 344 | int had_last_line = 0; |
f734c0ae SS |
345 | int nocopy = 0; |
346 | ||
347 | parse_state.state = NONE; | |
28be0b96 SS |
348 | |
349 | error = fprintf(queue->mailf, | |
350 | "Received: from %s (uid %d)\n" | |
351 | "\t(envelope-from %s)\n" | |
352 | "\tid %s\n" | |
353 | "\tby %s (%s)\n" | |
354 | "\t%s\n", | |
75fdf182 | 355 | username, useruid, |
057afad5 | 356 | queue->sender, |
28be0b96 SS |
357 | queue->id, |
358 | hostname(), VERSION, | |
359 | rfc822date()); | |
360 | if ((ssize_t)error < 0) | |
361 | return (-1); | |
362 | ||
363 | while (!feof(stdin)) { | |
807f777a | 364 | if (fgets(line, sizeof(line) - 1, stdin) == NULL) |
28be0b96 | 365 | break; |
807f777a SS |
366 | if (had_last_line) |
367 | errlogx(1, "bad mail input format"); | |
28be0b96 SS |
368 | linelen = strlen(line); |
369 | if (linelen == 0 || line[linelen - 1] != '\n') { | |
807f777a SS |
370 | /* |
371 | * This line did not end with a newline character. | |
372 | * If we fix it, it better be the last line of | |
373 | * the file. | |
374 | */ | |
375 | line[linelen] = '\n'; | |
376 | line[linelen + 1] = 0; | |
377 | had_last_line = 1; | |
28be0b96 SS |
378 | } |
379 | if (!had_headers) { | |
f734c0ae SS |
380 | /* |
381 | * Unless this is a continuation, switch of | |
382 | * the Bcc: nocopy flag. | |
383 | */ | |
384 | if (!(line[0] == ' ' || line[0] == '\t')) | |
385 | nocopy = 0; | |
386 | ||
28be0b96 SS |
387 | if (strprefixcmp(line, "Date:") == 0) |
388 | had_date = 1; | |
389 | else if (strprefixcmp(line, "Message-Id:") == 0) | |
390 | had_messagid = 1; | |
391 | else if (strprefixcmp(line, "From:") == 0) | |
392 | had_from = 1; | |
f734c0ae SS |
393 | else if (strprefixcmp(line, "Bcc:") == 0) |
394 | nocopy = 1; | |
395 | ||
396 | if (parse_state.state != NONE) { | |
397 | if (parse_addrs(&parse_state, line, queue) < 0) { | |
398 | errlogx(1, "invalid address in header\n"); | |
399 | /* NOTREACHED */ | |
400 | } | |
401 | } | |
402 | ||
403 | if (recp_from_header && ( | |
404 | strprefixcmp(line, "To:") == 0 || | |
405 | strprefixcmp(line, "Cc:") == 0 || | |
406 | strprefixcmp(line, "Bcc:") == 0)) { | |
407 | parse_state.state = START; | |
408 | if (parse_addrs(&parse_state, line, queue) < 0) { | |
409 | errlogx(1, "invalid address in header\n"); | |
410 | /* NOTREACHED */ | |
411 | } | |
412 | } | |
28be0b96 | 413 | } |
f734c0ae | 414 | |
28be0b96 SS |
415 | if (strcmp(line, "\n") == 0 && !had_headers) { |
416 | had_headers = 1; | |
417 | while (!had_date || !had_messagid || !had_from) { | |
418 | if (!had_date) { | |
419 | had_date = 1; | |
420 | snprintf(line, sizeof(line), "Date: %s\n", rfc822date()); | |
421 | } else if (!had_messagid) { | |
ee613428 | 422 | /* XXX msgid, assign earlier and log? */ |
28be0b96 | 423 | had_messagid = 1; |
ee613428 SS |
424 | snprintf(line, sizeof(line), "Message-Id: <%"PRIxMAX".%s.%"PRIxMAX"@%s>\n", |
425 | (uintmax_t)time(NULL), | |
426 | queue->id, | |
427 | random(), | |
428 | hostname()); | |
28be0b96 SS |
429 | } else if (!had_from) { |
430 | had_from = 1; | |
057afad5 | 431 | snprintf(line, sizeof(line), "From: <%s>\n", queue->sender); |
28be0b96 SS |
432 | } |
433 | if (fwrite(line, strlen(line), 1, queue->mailf) != 1) | |
434 | return (-1); | |
435 | } | |
436 | strcpy(line, "\n"); | |
437 | } | |
438 | if (!nodot && linelen == 2 && line[0] == '.') | |
439 | break; | |
f734c0ae SS |
440 | if (!nocopy) { |
441 | if (fwrite(line, strlen(line), 1, queue->mailf) != 1) | |
442 | return (-1); | |
443 | } | |
28be0b96 SS |
444 | } |
445 | ||
446 | return (0); | |
447 | } |