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
|
/* $OpenBSD: mfa_session.c,v 1.5 2011/10/23 09:30:07 gilles Exp $ */
/*
* Copyright (c) 2011 Gilles Chehade <gilles@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 <sys/wait.h>
#include <netinet/in.h>
#include <ctype.h>
#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <resolv.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "smtpd.h"
#include "log.h"
void mfa_session(struct submit_status *, enum session_state);
struct mfa_session *mfa_session_init(struct submit_status *, enum session_state);
struct mfa_session *mfa_session_find(u_int64_t);
struct mfa_session *mfa_session_xfind(u_int64_t);
int mfa_session_proceed(struct mfa_session *);
void mfa_session_pickup(struct mfa_session *);
void mfa_session_fail(struct mfa_session *);
void mfa_session_destroy(struct mfa_session *);
void mfa_session_done(struct mfa_session *);
void mfa_session_imsg(int, short, void *);
void
mfa_session(struct submit_status *ss, enum session_state state)
{
struct mfa_session *ms;
ms = mfa_session_init(ss, state);
if (ms->filter == NULL) {
mfa_session_done(ms);
return;
}
if (! mfa_session_proceed(ms))
mfa_session_fail(ms);
}
struct mfa_session *
mfa_session_init(struct submit_status *ss, enum session_state state)
{
struct mfa_session *ms;
ms = calloc(1, sizeof(*ms));
if (ms == NULL)
fatal("mfa_session_init: calloc");
ms->id = generate_uid();
ms->ss = *ss;
ms->ss.code = 250;
ms->state = state;
ms->filter = TAILQ_FIRST(env->sc_filters);
SPLAY_INSERT(mfatree, &env->mfa_sessions, ms);
return ms;
}
int
mfa_session_proceed(struct mfa_session *ms)
{
struct filter_msg fm;
fm.id = ms->id;
fm.cl_id = ms->ss.id;
fm.version = FILTER_API_VERSION;
switch (ms->state) {
case S_HELO:
fm.type = FILTER_HELO;
if (strlcpy(fm.u.helo.buffer, ms->ss.envelope.helo,
sizeof(fm.u.helo.buffer)) >= sizeof(fm.u.helo.buffer))
fatalx("mfa_session_proceed: HELO: truncation");
break;
case S_MAIL_MFA:
fm.type = FILTER_MAIL;
if (strlcpy(fm.u.mail.user, ms->ss.u.maddr.user,
sizeof(fm.u.mail.user)) >= sizeof(fm.u.mail.user))
fatalx("mfa_session_proceed: MAIL: user truncation");
if (strlcpy(fm.u.mail.domain, ms->ss.u.maddr.domain,
sizeof(fm.u.mail.domain)) >= sizeof(fm.u.mail.domain))
fatalx("mfa_session_proceed: MAIL: domain truncation");
break;
case S_RCPT_MFA:
fm.type = FILTER_RCPT;
if (strlcpy(fm.u.mail.user, ms->ss.u.maddr.user,
sizeof(fm.u.mail.user)) >= sizeof(fm.u.mail.user))
fatalx("mfa_session_proceed: RCPT: user truncation");
if (strlcpy(fm.u.mail.domain, ms->ss.u.maddr.domain,
sizeof(fm.u.mail.domain)) >= sizeof(fm.u.mail.domain))
fatalx("mfa_session_proceed: RCPT: domain truncation");
break;
case S_DATACONTENT:
fm.type = FILTER_DATALINE;
if (strlcpy(fm.u.dataline.line, ms->ss.u.dataline,
sizeof(fm.u.dataline.line)) >= sizeof(fm.u.dataline.line))
fatalx("mfa_session_proceed: DATA: line truncation");
break;
default:
fatalx("mfa_session_proceed: no such state");
}
imsg_compose(ms->filter->ibuf, fm.type, 0, 0, -1,
&fm, sizeof(fm));
event_set(&ms->filter->ev, ms->filter->ibuf->fd, EV_READ|EV_WRITE, mfa_session_imsg, ms->filter);
event_add(&ms->filter->ev, NULL);
return 1;
}
void
mfa_session_pickup(struct mfa_session *ms)
{
if (ms->fm.code == -1) {
mfa_session_fail(ms);
return;
}
ms->filter = TAILQ_NEXT(ms->filter, f_entry);
if (ms->filter == NULL)
mfa_session_done(ms);
else
mfa_session_proceed(ms);
}
void
mfa_session_done(struct mfa_session *ms)
{
enum imsg_type imsg_type;
switch (ms->state) {
case S_HELO:
imsg_type = IMSG_MFA_HELO;
break;
case S_MAIL_MFA:
if (ms->ss.code != 530) {
imsg_compose_event(env->sc_ievs[PROC_LKA], IMSG_LKA_MAIL, 0,
0, -1, &ms->ss, sizeof(ms->ss));
mfa_session_destroy(ms);
return;
}
imsg_type = IMSG_MFA_MAIL;
break;
case S_RCPT_MFA:
if (ms->ss.code != 530) {
imsg_compose_event(env->sc_ievs[PROC_LKA], IMSG_LKA_RULEMATCH,
0, 0, -1, &ms->ss, sizeof(ms->ss));
mfa_session_destroy(ms);
return;
}
imsg_type = IMSG_MFA_RCPT;
break;
case S_DATACONTENT:
if (ms->ss.code != 530 && ms->fm.code != 0)
(void)strlcpy(ms->ss.u.dataline, ms->fm.u.dataline.line,
sizeof(ms->ss.u.dataline));
imsg_type = IMSG_MFA_DATALINE;
break;
default:
fatalx("mfa_session_done: unsupported state");
}
imsg_compose_event(env->sc_ievs[PROC_SMTP], imsg_type, 0, 0,
-1, &ms->ss, sizeof(struct submit_status));
mfa_session_destroy(ms);
}
struct mfa_session *
mfa_session_find(u_int64_t id)
{
struct mfa_session key;
key.id = id;
return SPLAY_FIND(mfatree, &env->mfa_sessions, &key);
}
struct mfa_session *
mfa_session_xfind(u_int64_t id)
{
struct mfa_session *ms;
ms = mfa_session_find(id);
if (ms == NULL)
fatalx("mfa_session_xfind: mfa session missing");
return ms;
}
void
mfa_session_fail(struct mfa_session *ms)
{
ms->ss.code = 530;
mfa_session_done(ms);
}
void
mfa_session_destroy(struct mfa_session *ms)
{
SPLAY_REMOVE(mfatree, &env->mfa_sessions, ms);
free(ms);
}
void
mfa_session_imsg(int fd, short event, void *p)
{
struct filter *filter = p;
struct mfa_session *ms;
struct imsg imsg;
ssize_t n;
struct filter_msg fm;
short evflags = EV_READ;
if (event & EV_READ) {
n = imsg_read(filter->ibuf);
if (n == -1)
fatal("imsg_read");
if (n == 0) {
event_del(&filter->ev);
event_loopexit(NULL);
return;
}
}
if (event & EV_WRITE) {
if (msgbuf_write(&filter->ibuf->w) == -1)
fatal("msgbuf_write");
if (filter->ibuf->w.queued)
evflags |= EV_WRITE;
}
for (;;) {
n = imsg_get(filter->ibuf, &imsg);
if (n == -1)
fatalx("imsg_get");
if (n == 0)
break;
if ((imsg.hdr.len - IMSG_HEADER_SIZE)
!= sizeof(fm))
fatalx("corrupted imsg");
memcpy(&fm, imsg.data, sizeof (fm));
if (fm.version != FILTER_API_VERSION)
fatalx("API version mismatch");
switch (imsg.hdr.type) {
case FILTER_HELO:
case FILTER_EHLO:
case FILTER_MAIL:
case FILTER_RCPT:
case FILTER_DATALINE:
ms = mfa_session_xfind(fm.id);
/* overwrite filter code */
ms->fm.code = fm.code;
/* success, overwrite */
if (fm.code == 1)
ms->fm = fm;
mfa_session_pickup(ms);
break;
default:
fatalx("unsupported imsg");
}
imsg_free(&imsg);
}
event_set(&filter->ev, filter->ibuf->fd, evflags,
mfa_session_imsg, filter);
event_add(&filter->ev, NULL);
}
int
mfa_session_cmp(struct mfa_session *s1, struct mfa_session *s2)
{
/*
* do not return u_int64_t's
*/
if (s1->id < s2->id)
return -1;
if (s1->id > s2->id)
return 1;
return 0;
}
SPLAY_GENERATE(mfatree, mfa_session, nodes, mfa_session_cmp);
|