forked from dbmail/dbmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientsession.c
315 lines (254 loc) · 7.24 KB
/
clientsession.c
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
/*
Copyright (C) 2004 IC & S dbmail@ic-s.nl
Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
*
* implementation for lmtp commands according to RFC 1081 */
#include "dbmail.h"
#define THIS_MODULE "clientsession"
extern ServerConfig_T *server_conf;
extern struct event_base *evbase;
ClientSession_T * client_session_new(client_sock *c)
{
ClientBase_T *ci;
Mempool_T pool = c->pool;
char unique_id[UID_SIZE];
ci = client_init(c);
ClientSession_T * session = mempool_pop(pool, sizeof(ClientSession_T));
session->state = CLIENTSTATE_INITIAL_CONNECT;
session->pool = pool;
session->args = p_list_new(pool);
session->from = p_list_new(pool);
session->rbuff = p_string_new(pool, "");
session->messagelst = p_list_new(pool);
gethostname(session->hostname, sizeof(session->hostname));
memset(unique_id,0,sizeof(unique_id));
create_unique_id(unique_id, 0);
session->apop_stamp = g_strdup_printf("<%s@%s>", unique_id, session->hostname);
assert(evbase);
ci->rev = event_new(evbase, ci->rx, EV_READ|EV_PERSIST, socket_read_cb, (void *)session);
ci->wev = event_new(evbase, ci->tx, EV_WRITE, socket_write_cb, (void *)session);
ci_cork(ci);
session->ci = ci;
return session;
}
void client_session_reset(ClientSession_T * session)
{
List_T from;
if (session->rcpt)
dsnuser_free_list(session->rcpt);
session->rcpt = p_list_new(session->pool);
if (session->from) {
from = p_list_first(session->from);
while (from) {
String_T s = p_list_data(from);
if (s) p_string_free(s, TRUE);
from = p_list_next(from);
}
from = p_list_first(session->from);
p_list_free(&from);
}
session->from = p_list_new(session->pool);
if (session->apop_stamp) {
g_free(session->apop_stamp);
session->apop_stamp = NULL;
}
if (session->username) {
g_free(session->username);
session->username = NULL;
}
if (session->password) {
g_free(session->password);
session->password = NULL;
}
session->state = CLIENTSTATE_INITIAL_CONNECT;
client_session_reset_parser(session);
}
void client_session_reset_parser(ClientSession_T *session)
{
session->parser_state = FALSE;
session->command_type = FALSE;
if (session->rbuff) {
p_string_truncate(session->rbuff,0);
}
if (session->args) {
List_T args = p_list_first(session->args);
while (p_list_data(args)) {
String_T s = p_list_data(args);
p_string_free(s, TRUE);
if (! p_list_next(args))
break;
args = p_list_next(args);
}
p_list_free(&args);
}
session->args = p_list_new(session->pool);
}
void client_session_bailout(ClientSession_T **session)
{
ClientSession_T *c = *session;
Mempool_T pool;
size_t before = 0, after = 0;
int failcount = 0;
List_T args = NULL;
List_T from = NULL;
List_T rcpt = NULL;
List_T messagelst = NULL;
assert(c);
before = after = ci_wbuf_len(c->ci);
while (after && (! (c->ci->client_state & CLIENT_ERR)) && (failcount < 100)) {
before = ci_wbuf_len(c->ci);
ci_write_cb(c->ci);
after = ci_wbuf_len(c->ci);
if (before == after)
failcount++;
else
failcount = 0;
}
ci_cork(c->ci);
TRACE(TRACE_DEBUG,"[%p]", c);
// brute force:
if (server_conf->no_daemonize == 1) _exit(0);
client_session_reset(c);
c->state = CLIENTSTATE_QUIT_QUEUED;
ci_close(c->ci);
p_string_free(c->rbuff, TRUE);
if (c->from) {
from = p_list_first(c->from);
while (p_list_data(from)) {
String_T s = p_list_data(from);
p_string_free(s, TRUE);
if (! p_list_next(from))
break;
from = p_list_next(from);
}
from = p_list_first(from);
p_list_free(&from);
}
if (c->rcpt) {
rcpt = p_list_first(c->rcpt);
while (p_list_data(rcpt)) {
Delivery_T *s = p_list_data(rcpt);
g_free(s);
if (! p_list_next(rcpt))
break;
rcpt = p_list_next(rcpt);
}
rcpt = p_list_first(rcpt);
p_list_free(&rcpt);
}
if (c->args) {
args = p_list_first(c->args);
while (p_list_data(args)) {
String_T s = p_list_data(args);
p_string_free(s, TRUE);
if (! p_list_next(args))
break;
args = p_list_next(args);
}
args = p_list_first(args);
p_list_free(&args);
}
if (c->messagelst) {
messagelst = p_list_first(c->messagelst);
while (p_list_data(messagelst)) {
struct message *m = p_list_data(messagelst);
mempool_push(c->pool, m, sizeof(struct message));
if (! p_list_next(messagelst))
break;
messagelst = p_list_next(messagelst);
}
messagelst = p_list_first(messagelst);
p_list_free(&messagelst);
}
c->args = NULL;
c->from = NULL;
c->rcpt = NULL;
c->messagelst = NULL;
pool = c->pool;
mempool_push(pool, c, sizeof(ClientSession_T));
mempool_close(&pool);
c = NULL;
}
void client_session_read(void *arg)
{
ClientSession_T *session = (ClientSession_T *)arg;
ci_read_cb(session->ci);
uint64_t have = p_string_len(session->ci->read_buffer);
uint64_t need = session->ci->rbuff_size;
int enough = (need>0?(have >= need):(have > 0));
int state = session->ci->client_state;
if (state & CLIENT_ERR) {
TRACE(TRACE_DEBUG,"client_state ERROR");
client_session_bailout(&session);
} else if (state & CLIENT_EOF) {
ci_cork(session->ci);
if (enough)
session->handle_input(session);
else
client_session_bailout(&session);
}
else if (have > 0)
session->handle_input(session);
}
void client_session_set_timeout(ClientSession_T *session, int timeout)
{
if (session && session->ci) {
int current = session->ci->timeout.tv_sec;
if (timeout != current)
session->ci->timeout.tv_sec = timeout;
}
}
void socket_read_cb(int fd UNUSED, short what, void *arg)
{
ClientSession_T *session = (ClientSession_T *)arg;
if (what == EV_READ)
client_session_read(session); // drain the read-event handle
else if (what == EV_TIMEOUT && session->ci->cb_time)
session->ci->cb_time(session);
}
void socket_write_cb(int fd UNUSED, short what, void *arg)
{
ClientSession_T *session = (ClientSession_T *)arg;
if (! session->ci->cb_write)
return;
if (what == EV_TIMEOUT && session->ci->cb_time) {
session->ci->cb_time(session);
return;
}
session->ci->cb_write(session);
switch(session->state) {
case CLIENTSTATE_INITIAL_CONNECT:
case CLIENTSTATE_NON_AUTHENTICATED:
TRACE(TRACE_DEBUG,"reset timeout [%d]", server_conf->login_timeout);
client_session_set_timeout(session, server_conf->login_timeout);
break;
case CLIENTSTATE_AUTHENTICATED:
case CLIENTSTATE_SELECTED:
TRACE(TRACE_DEBUG,"reset timeout [%d]", server_conf->timeout);
client_session_set_timeout(session, server_conf->timeout);
break;
case CLIENTSTATE_LOGOUT:
case CLIENTSTATE_QUIT:
case CLIENTSTATE_ERROR:
client_session_bailout(&session);
break;
default:
case CLIENTSTATE_ANY:
case CLIENTSTATE_QUIT_QUEUED:
break;
}
}