1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/* $OpenBSD: bounce.c,v 1.43 2012/08/08 08:50:42 eric Exp $ */
/*
* Copyright (c) 2009 Gilles Chehade <gilles@openbsd.org>
* Copyright (c) 2009 Jacek Masiulaniec <jacekm@dobremiasto.net>
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <err.h>
#include <event.h>
#include <imsg.h>
#include <inttypes.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "smtpd.h"
#include "log.h"
#define BOUNCE_HIWAT 65535
enum {
BOUNCE_EHLO,
BOUNCE_MAIL,
BOUNCE_RCPT,
BOUNCE_DATA,
BOUNCE_DATA_NOTICE,
BOUNCE_DATA_MESSAGE,
BOUNCE_QUIT,
BOUNCE_CLOSE,
};
struct bounce {
struct envelope evp;
FILE *msgfp;
int state;
struct iobuf iobuf;
struct io io;
};
/* XXX remove later */
void scheduler_envelope_update(struct envelope *);
void scheduler_envelope_delete(struct envelope *);
static void bounce_send(struct bounce *, const char *, ...);
static int bounce_next(struct bounce *);
static void bounce_status(struct bounce *, const char *, ...);
static void bounce_io(struct io *, int);
static void
bounce_send(struct bounce *bounce, const char *fmt, ...)
{
va_list ap;
char *p;
int len;
va_start(ap, fmt);
if ((len = vasprintf(&p, fmt, ap)) == -1)
fatal("bounce: vasprintf");
va_end(ap);
log_trace(TRACE_BOUNCE, "bounce: %p: >>> %s", bounce, p);
iobuf_fqueue(&bounce->iobuf, "%s\n", p);
free(p);
}
/* This can simplified once we support PIPELINING */
static int
bounce_next(struct bounce *bounce)
{
char *line;
size_t len, s;
switch(bounce->state) {
case BOUNCE_EHLO:
bounce_send(bounce, "EHLO %s", env->sc_hostname);
bounce->state = BOUNCE_MAIL;
break;
case BOUNCE_MAIL:
bounce_send(bounce, "MAIL FROM: <>");
bounce->state = BOUNCE_RCPT;
break;
case BOUNCE_RCPT:
bounce_send(bounce, "RCPT TO: <%s@%s>",
bounce->evp.sender.user, bounce->evp.sender.domain);
bounce->state = BOUNCE_DATA;
break;
case BOUNCE_DATA:
bounce_send(bounce, "DATA");
bounce->state = BOUNCE_DATA_NOTICE;
break;
case BOUNCE_DATA_NOTICE:
/* Construct an appropriate reason line. */
line = bounce->evp.errorline;
if (strlen(line) > 4 && (*line == '1' || *line == '6'))
line += 4;
iobuf_fqueue(&bounce->iobuf,
"Subject: Delivery status notification\n"
"From: Mailer Daemon <MAILER-DAEMON@%s>\n"
"To: %s@%s\n"
"Date: %s\n"
"\n"
"Hi !\n"
"\n"
"This is the MAILER-DAEMON, please DO NOT REPLY to this e-mail.\n"
"An error has occurred while attempting to deliver a message.\n"
"\n"
"Recipient: %s@%s\n"
"Reason:\n"
"%s\n"
"\n"
"Below is a copy of the original message:\n"
"\n",
env->sc_hostname,
bounce->evp.sender.user, bounce->evp.sender.domain,
time_to_text(time(NULL)),
bounce->evp.dest.user, bounce->evp.dest.domain,
line);
log_trace(TRACE_BOUNCE, "bounce: %p: >>> [... %zu bytes ...]",
bounce, iobuf_queued(&bounce->iobuf));
bounce->state = BOUNCE_DATA_MESSAGE;
break;
case BOUNCE_DATA_MESSAGE:
s = iobuf_queued(&bounce->iobuf);
while (iobuf_len(&bounce->iobuf) < BOUNCE_HIWAT) {
line = fgetln(bounce->msgfp, &len);
if (line == NULL)
break;
line[len - 1] = '\0';
if(len == 2 && line[0] == '.')
iobuf_queue(&bounce->iobuf, ".", 1);
iobuf_queue(&bounce->iobuf, line, len);
iobuf_queue(&bounce->iobuf, "\n", 1);
}
if (ferror(bounce->msgfp)) {
bounce_status(bounce, "460 Error reading message");
return (-1);
}
log_trace(TRACE_BOUNCE, "bounce: %p: >>> [... %zu bytes ...]",
bounce, iobuf_queued(&bounce->iobuf) - s);
if (feof(bounce->msgfp)) {
bounce_send(bounce, ".");
bounce->state = BOUNCE_QUIT;
}
break;
case BOUNCE_QUIT:
bounce_send(bounce, "QUIT");
bounce->state = BOUNCE_CLOSE;
break;
default:
fatalx("bounce: bad state");
}
return (0);
}
static void
bounce_status(struct bounce *bounce, const char *fmt, ...)
{
va_list ap;
char *status;
int len;
/* ignore if the envelope has already been updated/deleted */
if (bounce->evp.id == 0)
return;
va_start(ap, fmt);
if ((len = vasprintf(&status, fmt, ap)) == -1)
fatal("bounce: vasprintf");
va_end(ap);
if (*status == '2' || *status == '5' || *status == '6') {
log_debug("#### %s: queue_envelope_delete: %016" PRIx64,
__func__, bounce->evp.id);
queue_envelope_delete(&bounce->evp);
scheduler_envelope_delete(&bounce->evp);
} else {
bounce->evp.retry++;
envelope_set_errormsg(&bounce->evp, "%s", status);
queue_envelope_update(&bounce->evp);
scheduler_envelope_update(&bounce->evp);
}
bounce->evp.id = 0;
free(status);
}
static void
bounce_free(struct bounce *bounce)
{
log_debug("bounce: %p: deleting session", bounce);
fclose(bounce->msgfp);
iobuf_clear(&bounce->iobuf);
io_clear(&bounce->io);
free(bounce);
stat_decrement(STATS_SCHEDULER);
stat_decrement(STATS_SCHEDULER_BOUNCES);
}
static void
bounce_io(struct io *io, int evt)
{
struct bounce *bounce = io->arg;
const char *error;
char *line, *msg;
int cont;
size_t len;
log_trace(TRACE_IO, "bounce: %p: %s %s",
bounce, io_strevent(evt), io_strio(io));
switch (evt) {
case IO_DATAIN:
nextline:
line = iobuf_getline(&bounce->iobuf, &len);
if (line == NULL) {
if (iobuf_len(&bounce->iobuf) >= SMTP_LINE_MAX) {
bounce_status(bounce, "150 Input too long");
bounce_free(bounce);
return;
}
iobuf_normalize(&bounce->iobuf);
break;
}
log_trace(TRACE_BOUNCE, "bounce: %p: <<< %s", bounce, line);
if ((error = parse_smtp_response(line, len, &msg, &cont))) {
bounce_status(bounce, "150 Bad response: %s", error);
bounce_free(bounce);
return;
}
if (cont)
goto nextline;
if (bounce->state == BOUNCE_CLOSE) {
bounce_free(bounce);
return;
}
if (line[0] != '2' && line[0] != '3') { /* fail */
bounce_status(bounce, "%s", line);
bounce->state = BOUNCE_QUIT;
} else if (bounce->state == BOUNCE_QUIT) { /* accepted */
bounce_status(bounce, "%s", line);
}
if (bounce_next(bounce) == -1) {
bounce_free(bounce);
return;
}
io_set_write(io);
break;
case IO_LOWAT:
if (bounce->state == BOUNCE_DATA_MESSAGE)
bounce_next(bounce);
if (iobuf_queued(&bounce->iobuf) == 0)
io_set_read(io);
break;
default:
bounce_status(bounce, "442 i/o error %i", evt);
bounce_free(bounce);
break;
}
}
int
bounce_session(int fd, struct envelope *evp)
{
struct bounce *bounce = NULL;
int msgfd = -1;
FILE *msgfp = NULL;
u_int32_t msgid;
msgid = evpid_to_msgid(evp->id);
log_debug("bounce: bouncing envelope id %016" PRIx64 "", evp->id);
/* get message content */
if ((msgfd = queue_message_fd_r(msgid)) == -1)
return (0);
msgfp = fdopen(msgfd, "r");
if (msgfp == NULL) {
log_warn("bounce: fdopen");
close(msgfd);
return (0);
}
if ((bounce = calloc(1, sizeof(*bounce))) == NULL) {
log_warn("bounce: calloc");
fclose(msgfp);
return (0);
}
bounce->evp = *evp;
bounce->msgfp = msgfp;
bounce->state = BOUNCE_EHLO;
iobuf_init(&bounce->iobuf, 0, 0);
io_init(&bounce->io, fd, bounce, bounce_io, &bounce->iobuf);
io_set_timeout(&bounce->io, 30000);
io_set_read(&bounce->io);
return (1);
}
int
bounce_record_message(struct envelope *e, struct envelope *bounce)
{
if (e->type == D_BOUNCE) {
log_debug("mailer daemons loop detected !");
return 0;
}
*bounce = *e;
bounce->type = D_BOUNCE;
bounce->retry = 0;
bounce->lasttry = 0;
return (queue_envelope_create(bounce));
}
|