76
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_imap.h>
|
|
11
|
|
12
|
|
13 static void ngx_imap_init_session(ngx_event_t *rev);
|
90
|
14 static void ngx_imap_init_protocol(ngx_event_t *rev);
|
76
|
15 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s);
|
90
|
16 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
76
|
17
|
88
|
18 #if (NGX_IMAP_SSL)
|
|
19 static void ngx_imap_ssl_close_handler(ngx_event_t *ev);
|
|
20 #endif
|
|
21
|
76
|
22
|
|
23 static ngx_str_t greetings[] = {
|
|
24 ngx_string("+OK POP3 ready" CRLF),
|
78
|
25 ngx_string("* OK IMAP4 ready" CRLF)
|
76
|
26 };
|
|
27
|
|
28 static ngx_str_t internal_server_errors[] = {
|
|
29 ngx_string("-ERR internal server error" CRLF),
|
|
30 ngx_string("* BAD internal server error" CRLF),
|
|
31 };
|
|
32
|
|
33 static u_char pop3_ok[] = "+OK" CRLF;
|
|
34 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
|
|
35
|
92
|
36 static u_char imap_star[] = "* ";
|
78
|
37 static u_char imap_ok[] = "OK completed" CRLF;
|
76
|
38 static u_char imap_next[] = "+ OK" CRLF;
|
|
39 static u_char imap_bye[] = "* BYE" CRLF;
|
|
40 static u_char imap_invalid_command[] = "BAD invalid command" CRLF;
|
|
41
|
|
42
|
|
43 void
|
|
44 ngx_imap_init_connection(ngx_connection_t *c)
|
|
45 {
|
92
|
46 ngx_imap_log_ctx_t *lctx;
|
|
47 #if (NGX_IMAP_SSL)
|
|
48 ngx_imap_conf_ctx_t *ctx;
|
|
49 ngx_imap_ssl_conf_t *sslcf;
|
|
50 #endif
|
90
|
51
|
|
52 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap init connection");
|
|
53
|
92
|
54 lctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t));
|
|
55 if (lctx == NULL) {
|
90
|
56 ngx_imap_close_connection(c);
|
|
57 return;
|
|
58 }
|
|
59
|
92
|
60 lctx->client = &c->addr_text;
|
|
61 lctx->session = NULL;
|
90
|
62
|
|
63 c->log->connection = c->number;
|
|
64 c->log->handler = ngx_imap_log_error;
|
92
|
65 c->log->data = lctx;
|
90
|
66 c->log->action = "sending client greeting line";
|
|
67
|
|
68 c->log_error = NGX_ERROR_INFO;
|
|
69
|
92
|
70 #if (NGX_IMAP_SSL)
|
|
71
|
|
72 ctx = c->ctx;
|
|
73 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module);
|
|
74
|
|
75 if (sslcf->enable) {
|
|
76 if (ngx_ssl_create_connection(sslcf->ssl_ctx, c, 0) == NGX_ERROR) {
|
|
77 ngx_imap_close_connection(c);
|
|
78 return;
|
|
79 }
|
|
80
|
|
81 c->recv = ngx_ssl_recv;
|
|
82 c->send = ngx_ssl_write;
|
|
83 c->send_chain = ngx_ssl_send_chain;
|
|
84 }
|
|
85
|
|
86 #endif
|
|
87
|
90
|
88 ngx_imap_init_session(c->read);
|
|
89 }
|
|
90
|
|
91
|
|
92 static void
|
|
93 ngx_imap_init_session(ngx_event_t *rev)
|
|
94 {
|
|
95 ngx_connection_t *c;
|
88
|
96 ngx_imap_session_t *s;
|
90
|
97 ngx_imap_log_ctx_t *lctx;
|
76
|
98 ngx_imap_conf_ctx_t *ctx;
|
90
|
99 ngx_imap_core_srv_conf_t *cscf;
|
88
|
100 #if (NGX_IMAP_SSL)
|
|
101 ngx_int_t rc;
|
|
102 #endif
|
76
|
103
|
90
|
104 c = rev->data;
|
76
|
105 ctx = c->ctx;
|
90
|
106 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module);
|
|
107
|
88
|
108 #if (NGX_IMAP_SSL)
|
|
109
|
92
|
110 if (c->ssl) {
|
76
|
111
|
88
|
112 rc = ngx_ssl_handshake(c);
|
|
113
|
|
114 if (rc == NGX_ERROR) {
|
|
115 ngx_imap_close_connection(c);
|
|
116 return;
|
|
117 }
|
|
118
|
90
|
119 if (rc == NGX_AGAIN) {
|
|
120 ngx_add_timer(rev, cscf->timeout);
|
|
121 c->read->handler = ngx_imap_init_session;
|
|
122
|
|
123 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
124 ngx_imap_close_connection(c);
|
|
125 }
|
|
126
|
|
127 return;
|
|
128 }
|
|
129
|
88
|
130 }
|
|
131
|
|
132 #endif
|
|
133
|
|
134 s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t));
|
|
135 if (s == NULL) {
|
76
|
136 ngx_imap_close_connection(c);
|
|
137 return;
|
|
138 }
|
|
139
|
88
|
140 c->data = s;
|
|
141 s->connection = c;
|
|
142
|
|
143 s->protocol = cscf->protocol;
|
|
144
|
|
145 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module);
|
|
146 if (s->ctx == NULL) {
|
|
147 ngx_imap_session_internal_server_error(s);
|
|
148 return;
|
|
149 }
|
|
150
|
|
151 s->main_conf = ctx->main_conf;
|
|
152 s->srv_conf = ctx->srv_conf;
|
|
153
|
|
154 s->out = greetings[s->protocol];
|
|
155
|
90
|
156 lctx = c->log->data;
|
|
157 lctx->session = s;
|
|
158
|
|
159 c->read->handler = ngx_imap_init_protocol;
|
88
|
160 c->write->handler = ngx_imap_send;
|
76
|
161
|
90
|
162 ngx_add_timer(rev, cscf->timeout);
|
76
|
163
|
90
|
164 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
76
|
165 ngx_imap_close_connection(c);
|
|
166 }
|
88
|
167
|
|
168 ngx_imap_send(c->write);
|
|
169 }
|
|
170
|
|
171
|
|
172 void
|
|
173 ngx_imap_send(ngx_event_t *wev)
|
|
174 {
|
90
|
175 ngx_int_t n;
|
|
176 ngx_connection_t *c;
|
|
177 ngx_imap_session_t *s;
|
|
178 ngx_imap_core_srv_conf_t *cscf;
|
88
|
179
|
|
180 c = wev->data;
|
|
181 s = c->data;
|
|
182
|
|
183 if (wev->timedout) {
|
|
184 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
185 ngx_imap_close_connection(c);
|
|
186 return;
|
|
187 }
|
|
188
|
|
189 if (s->out.len == 0) {
|
|
190 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
191 ngx_imap_close_connection(c);
|
|
192 }
|
|
193
|
|
194 return;
|
|
195 }
|
|
196
|
|
197 n = c->send(c, s->out.data, s->out.len);
|
|
198
|
|
199 if (n > 0) {
|
|
200 s->out.len -= n;
|
|
201
|
90
|
202 if (wev->timer_set) {
|
|
203 ngx_del_timer(wev);
|
|
204 }
|
|
205
|
88
|
206 if (s->quit) {
|
|
207 ngx_imap_close_connection(c);
|
|
208 return;
|
|
209 }
|
|
210
|
|
211 if (s->blocked) {
|
|
212 c->read->handler(c->read);
|
|
213 }
|
|
214
|
|
215 return;
|
|
216 }
|
|
217
|
|
218 if (n == NGX_ERROR) {
|
|
219 ngx_imap_close_connection(c);
|
|
220 return;
|
|
221 }
|
|
222
|
|
223 /* n == NGX_AGAIN */
|
|
224
|
90
|
225 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
226
|
|
227 ngx_add_timer(c->write, cscf->timeout);
|
|
228
|
88
|
229 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
230 ngx_imap_close_connection(c);
|
|
231 return;
|
|
232 }
|
76
|
233 }
|
|
234
|
|
235
|
|
236 static void
|
90
|
237 ngx_imap_init_protocol(ngx_event_t *rev)
|
76
|
238 {
|
|
239 size_t size;
|
|
240 ngx_connection_t *c;
|
|
241 ngx_imap_session_t *s;
|
|
242 ngx_imap_core_srv_conf_t *cscf;
|
|
243
|
|
244 c = rev->data;
|
|
245
|
90
|
246 c->log->action = "in auth state";
|
|
247
|
76
|
248 if (rev->timedout) {
|
|
249 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
250 ngx_imap_close_connection(c);
|
|
251 return;
|
|
252 }
|
|
253
|
88
|
254 s = c->data;
|
76
|
255
|
|
256 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) {
|
|
257 ngx_imap_session_internal_server_error(s);
|
|
258 return;
|
|
259 }
|
|
260
|
88
|
261 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
76
|
262 size = 128;
|
|
263 s->imap_state = ngx_pop3_start;
|
|
264 c->read->handler = ngx_pop3_auth_state;
|
|
265
|
|
266 } else {
|
88
|
267 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
76
|
268 size = cscf->imap_client_buffer_size;
|
|
269 s->imap_state = ngx_imap_start;
|
|
270 c->read->handler = ngx_imap_auth_state;
|
|
271 }
|
|
272
|
|
273 s->buffer = ngx_create_temp_buf(c->pool, size);
|
|
274 if (s->buffer == NULL) {
|
|
275 ngx_imap_session_internal_server_error(s);
|
|
276 return;
|
|
277 }
|
|
278
|
|
279 c->read->handler(rev);
|
|
280 }
|
|
281
|
|
282
|
|
283 void
|
|
284 ngx_imap_auth_state(ngx_event_t *rev)
|
|
285 {
|
92
|
286 u_char *text, *last, *p, *dst, *src, *end;
|
88
|
287 ssize_t text_len, last_len;
|
76
|
288 ngx_str_t *arg;
|
|
289 ngx_int_t rc;
|
92
|
290 ngx_uint_t tag, i;
|
76
|
291 ngx_connection_t *c;
|
|
292 ngx_imap_session_t *s;
|
|
293 ngx_imap_core_srv_conf_t *cscf;
|
|
294
|
|
295 c = rev->data;
|
|
296 s = c->data;
|
|
297
|
|
298 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state");
|
|
299
|
|
300 if (rev->timedout) {
|
|
301 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
302 ngx_imap_close_connection(c);
|
|
303 return;
|
|
304 }
|
|
305
|
88
|
306 if (s->out.len) {
|
|
307 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
308 s->blocked = 1;
|
|
309 return;
|
|
310 }
|
|
311
|
|
312 s->blocked = 0;
|
|
313
|
76
|
314 rc = ngx_imap_read_command(s);
|
|
315
|
|
316 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth: %i", rc);
|
|
317
|
|
318 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
319 return;
|
|
320 }
|
|
321
|
|
322 tag = 1;
|
|
323
|
|
324 text = NULL;
|
|
325 text_len = 0;
|
|
326
|
|
327 last = imap_ok;
|
|
328 last_len = sizeof(imap_ok) - 1;
|
|
329
|
|
330 if (rc == NGX_OK) {
|
|
331
|
|
332 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth command: %i",
|
|
333 s->command);
|
|
334
|
92
|
335 if (s->backslash) {
|
|
336
|
|
337 arg = s->args.elts;
|
|
338
|
|
339 for (i = 0; i < s->args.nelts; i++) {
|
|
340 dst = arg[i].data;
|
|
341 end = dst + arg[i].len;
|
|
342
|
|
343 for (src = dst; src < end; dst++) {
|
|
344 *dst = *src;
|
|
345 if (*src++ == '\\') {
|
|
346 *dst = *src++;
|
|
347 }
|
|
348 }
|
|
349
|
|
350 arg[i].len = dst - arg[i].data;
|
|
351 }
|
|
352
|
|
353 s->backslash = 0;
|
|
354 }
|
|
355
|
76
|
356 switch (s->command) {
|
|
357
|
|
358 case NGX_IMAP_LOGIN:
|
|
359 if (s->args.nelts == 2) {
|
|
360
|
|
361 arg = s->args.elts;
|
|
362
|
|
363 s->login.len = arg[0].len;
|
|
364 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
365 if (s->login.data == NULL) {
|
|
366 ngx_imap_session_internal_server_error(s);
|
|
367 return;
|
|
368 }
|
|
369
|
|
370 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
371
|
|
372 s->passwd.len = arg[1].len;
|
|
373 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
374 if (s->passwd.data == NULL) {
|
|
375 ngx_imap_session_internal_server_error(s);
|
|
376 return;
|
|
377 }
|
|
378
|
|
379 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
380
|
|
381 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
382 "imap login:\"%V\" passwd:\"%V\"",
|
|
383 &s->login, &s->passwd);
|
|
384
|
|
385 s->args.nelts = 0;
|
|
386 s->buffer->pos = s->buffer->start;
|
|
387 s->buffer->last = s->buffer->start;
|
|
388
|
|
389 if (rev->timer_set) {
|
|
390 ngx_del_timer(rev);
|
|
391 }
|
|
392
|
|
393 s->login_attempt++;
|
|
394
|
|
395 ngx_imap_auth_http_init(s);
|
|
396
|
|
397 return;
|
|
398
|
|
399 } else {
|
|
400 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
401 }
|
|
402
|
|
403 break;
|
|
404
|
|
405 case NGX_IMAP_CAPABILITY:
|
|
406 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
407 text = cscf->imap_capability->pos;
|
|
408 text_len = cscf->imap_capability->last - cscf->imap_capability->pos;
|
|
409 break;
|
|
410
|
|
411 case NGX_IMAP_LOGOUT:
|
88
|
412 s->quit = 1;
|
76
|
413 text = imap_bye;
|
|
414 text_len = sizeof(imap_bye) - 1;
|
|
415 break;
|
|
416
|
|
417 case NGX_IMAP_NOOP:
|
|
418 break;
|
|
419
|
|
420 default:
|
|
421 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
422 break;
|
|
423 }
|
|
424
|
|
425 } else if (rc == NGX_IMAP_NEXT) {
|
|
426 last = imap_next;
|
|
427 last_len = sizeof(imap_next) - 1;
|
|
428 tag = 0;
|
|
429 }
|
|
430
|
|
431 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
432 last = imap_invalid_command;
|
|
433 last_len = sizeof(imap_invalid_command) - 1;
|
|
434 }
|
|
435
|
|
436 if (tag) {
|
92
|
437 if (s->tag.len == 0) {
|
|
438 s->tag.len = sizeof(imap_star) - 1;
|
|
439 s->tag.data = (u_char *) imap_star;
|
|
440 }
|
|
441
|
88
|
442 if (s->tagged_line.len < s->tag.len + text_len + last_len) {
|
|
443 s->tagged_line.len = s->tag.len + text_len + last_len;
|
|
444 s->tagged_line.data = ngx_palloc(c->pool, s->tagged_line.len);
|
|
445 if (s->tagged_line.data == NULL) {
|
76
|
446 ngx_imap_close_connection(c);
|
|
447 return;
|
|
448 }
|
|
449 }
|
|
450
|
88
|
451 s->out.data = s->tagged_line.data;
|
|
452 s->out.len = s->tag.len + text_len + last_len;
|
|
453
|
|
454 p = s->out.data;
|
76
|
455
|
|
456 if (text) {
|
|
457 p = ngx_cpymem(p, text, text_len);
|
|
458 }
|
|
459 p = ngx_cpymem(p, s->tag.data, s->tag.len);
|
|
460 ngx_memcpy(p, last, last_len);
|
|
461
|
|
462
|
|
463 } else {
|
88
|
464 s->out.data = last;
|
|
465 s->out.len = last_len;
|
76
|
466 }
|
|
467
|
88
|
468 if (rc != NGX_IMAP_NEXT) {
|
|
469 s->args.nelts = 0;
|
|
470 s->buffer->pos = s->buffer->start;
|
|
471 s->buffer->last = s->buffer->start;
|
|
472 s->tag.len = 0;
|
76
|
473 }
|
|
474
|
88
|
475 ngx_imap_send(c->write);
|
76
|
476 }
|
|
477
|
|
478
|
|
479 void
|
|
480 ngx_pop3_auth_state(ngx_event_t *rev)
|
|
481 {
|
|
482 u_char *text;
|
|
483 ssize_t size;
|
|
484 ngx_int_t rc;
|
|
485 ngx_str_t *arg;
|
|
486 ngx_connection_t *c;
|
|
487 ngx_imap_session_t *s;
|
|
488 ngx_imap_core_srv_conf_t *cscf;
|
|
489
|
|
490 c = rev->data;
|
|
491 s = c->data;
|
|
492
|
|
493 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state");
|
|
494
|
|
495 if (rev->timedout) {
|
|
496 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
497 ngx_imap_close_connection(c);
|
|
498 return;
|
|
499 }
|
|
500
|
88
|
501 if (s->out.len) {
|
|
502 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
503 s->blocked = 1;
|
|
504 return;
|
|
505 }
|
|
506
|
|
507 s->blocked = 0;
|
|
508
|
76
|
509 rc = ngx_imap_read_command(s);
|
|
510
|
|
511 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
512 return;
|
|
513 }
|
|
514
|
|
515 text = pop3_ok;
|
|
516 size = sizeof(pop3_ok) - 1;
|
|
517
|
|
518 if (rc == NGX_OK) {
|
|
519 switch (s->imap_state) {
|
|
520
|
|
521 case ngx_pop3_start:
|
|
522
|
|
523 switch (s->command) {
|
|
524
|
|
525 case NGX_POP3_USER:
|
|
526 if (s->args.nelts == 1) {
|
|
527 s->imap_state = ngx_pop3_user;
|
|
528
|
|
529 arg = s->args.elts;
|
|
530 s->login.len = arg[0].len;
|
|
531 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
532 if (s->login.data == NULL) {
|
|
533 ngx_imap_session_internal_server_error(s);
|
|
534 return;
|
|
535 }
|
|
536
|
|
537 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
538
|
|
539 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
540 "pop3 login: \"%V\"", &s->login);
|
|
541
|
|
542 } else {
|
|
543 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
544 }
|
|
545
|
|
546 break;
|
|
547
|
|
548 case NGX_POP3_CAPA:
|
|
549 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
550 text = cscf->pop3_capability->pos;
|
|
551 size = cscf->pop3_capability->last - cscf->pop3_capability->pos;
|
|
552 break;
|
|
553
|
|
554 case NGX_POP3_QUIT:
|
88
|
555 s->quit = 1;
|
76
|
556 break;
|
|
557
|
|
558 case NGX_POP3_NOOP:
|
|
559 break;
|
|
560
|
|
561 default:
|
|
562 s->imap_state = ngx_pop3_start;
|
|
563 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
564 break;
|
|
565 }
|
|
566
|
|
567 break;
|
|
568
|
|
569 case ngx_pop3_user:
|
|
570
|
|
571 switch (s->command) {
|
|
572
|
|
573 case NGX_POP3_PASS:
|
|
574 if (s->args.nelts == 1) {
|
|
575 /* STUB */ s->imap_state = ngx_pop3_start;
|
|
576
|
|
577 arg = s->args.elts;
|
|
578 s->passwd.len = arg[0].len;
|
|
579 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
580 if (s->passwd.data == NULL) {
|
|
581 ngx_imap_session_internal_server_error(s);
|
|
582 return;
|
|
583 }
|
|
584
|
|
585 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
|
|
586
|
|
587 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
588 "pop3 passwd: \"%V\"", &s->passwd);
|
|
589
|
|
590 s->args.nelts = 0;
|
|
591 s->buffer->pos = s->buffer->start;
|
|
592 s->buffer->last = s->buffer->start;
|
|
593
|
|
594 if (rev->timer_set) {
|
|
595 ngx_del_timer(rev);
|
|
596 }
|
|
597
|
|
598 ngx_imap_auth_http_init(s);
|
|
599
|
|
600 return;
|
|
601
|
|
602 } else {
|
|
603 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
604 }
|
|
605
|
|
606 break;
|
|
607
|
|
608 case NGX_POP3_CAPA:
|
|
609 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
610 text = cscf->pop3_capability->pos;
|
|
611 size = cscf->pop3_capability->last - cscf->pop3_capability->pos;
|
|
612 break;
|
|
613
|
|
614 case NGX_POP3_QUIT:
|
88
|
615 s->quit = 1;
|
76
|
616 break;
|
|
617
|
|
618 case NGX_POP3_NOOP:
|
|
619 break;
|
|
620
|
|
621 default:
|
|
622 s->imap_state = ngx_pop3_start;
|
|
623 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
624 break;
|
|
625 }
|
|
626
|
|
627 break;
|
|
628
|
|
629 /* suppress warinings */
|
|
630 case ngx_pop3_passwd:
|
|
631 break;
|
|
632 }
|
|
633 }
|
|
634
|
|
635 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
636 text = pop3_invalid_command;
|
|
637 size = sizeof(pop3_invalid_command) - 1;
|
|
638 }
|
|
639
|
|
640 s->args.nelts = 0;
|
|
641 s->buffer->pos = s->buffer->start;
|
|
642 s->buffer->last = s->buffer->start;
|
88
|
643
|
|
644 s->out.data = text;
|
|
645 s->out.len = size;
|
|
646
|
|
647 ngx_imap_send(c->write);
|
76
|
648 }
|
|
649
|
|
650
|
|
651 static ngx_int_t
|
|
652 ngx_imap_read_command(ngx_imap_session_t *s)
|
|
653 {
|
|
654 ssize_t n;
|
|
655 ngx_int_t rc;
|
|
656
|
88
|
657 n = s->connection->recv(s->connection, s->buffer->last,
|
|
658 s->buffer->end - s->buffer->last);
|
76
|
659
|
|
660 if (n == NGX_ERROR || n == 0) {
|
|
661 ngx_imap_close_connection(s->connection);
|
|
662 return NGX_ERROR;
|
|
663 }
|
|
664
|
|
665 if (n > 0) {
|
|
666 s->buffer->last += n;
|
|
667 }
|
|
668
|
|
669 if (n == NGX_AGAIN) {
|
|
670 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
|
|
671 ngx_imap_session_internal_server_error(s);
|
|
672 return NGX_ERROR;
|
|
673 }
|
|
674
|
|
675 return NGX_AGAIN;
|
|
676 }
|
|
677
|
|
678 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
679 rc = ngx_pop3_parse_command(s);
|
|
680 } else {
|
|
681 rc = ngx_imap_parse_command(s);
|
|
682 }
|
|
683
|
|
684 if (rc == NGX_AGAIN
|
|
685 || rc == NGX_IMAP_NEXT
|
|
686 || rc == NGX_IMAP_PARSE_INVALID_COMMAND)
|
|
687 {
|
|
688 return rc;
|
|
689 }
|
|
690
|
|
691 if (rc == NGX_ERROR) {
|
|
692 ngx_imap_close_connection(s->connection);
|
|
693 return NGX_ERROR;
|
|
694 }
|
|
695
|
|
696 return NGX_OK;
|
|
697 }
|
|
698
|
|
699
|
|
700 void
|
|
701 ngx_imap_session_internal_server_error(ngx_imap_session_t *s)
|
|
702 {
|
88
|
703 s->out = internal_server_errors[s->protocol];
|
|
704 s->quit = 1;
|
76
|
705
|
88
|
706 ngx_imap_send(s->connection->write);
|
76
|
707 }
|
|
708
|
|
709
|
|
710 void
|
|
711 ngx_imap_close_connection(ngx_connection_t *c)
|
|
712 {
|
|
713 ngx_pool_t *pool;
|
|
714
|
|
715 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
716 "close imap connection: %d", c->fd);
|
|
717
|
88
|
718 #if (NGX_IMAP_SSL)
|
|
719
|
|
720 if (c->ssl) {
|
|
721 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
|
722 c->read->handler = ngx_imap_ssl_close_handler;
|
|
723 c->write->handler = ngx_imap_ssl_close_handler;
|
|
724 return;
|
|
725 }
|
|
726 }
|
|
727
|
|
728 #endif
|
|
729
|
92
|
730 c->closed = 1;
|
|
731
|
76
|
732 pool = c->pool;
|
|
733
|
|
734 ngx_close_connection(c);
|
|
735
|
|
736 ngx_destroy_pool(pool);
|
|
737 }
|
88
|
738
|
|
739
|
|
740 #if (NGX_IMAP_SSL)
|
|
741
|
|
742 static void
|
|
743 ngx_imap_ssl_close_handler(ngx_event_t *ev)
|
|
744 {
|
|
745 ngx_connection_t *c;
|
|
746
|
|
747 c = ev->data;
|
|
748
|
|
749 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, ev->log, 0, "http ssl close handler");
|
|
750
|
|
751 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
|
752 return;
|
|
753 }
|
|
754
|
|
755 ngx_imap_close_connection(c);
|
|
756 }
|
|
757
|
|
758 #endif
|
90
|
759
|
|
760
|
|
761 static u_char *
|
|
762 ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
|
763 {
|
|
764 u_char *p;
|
|
765 ngx_imap_session_t *s;
|
|
766 ngx_imap_log_ctx_t *ctx;
|
|
767
|
|
768 if (log->action) {
|
|
769 p = ngx_snprintf(buf, len, " while %s", log->action);
|
|
770 len -= p - buf;
|
|
771 buf = p;
|
|
772 }
|
|
773
|
|
774 ctx = log->data;
|
|
775
|
|
776 p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
|
777 len -= p - buf;
|
|
778 buf = p;
|
|
779
|
|
780 s = ctx->session;
|
|
781
|
|
782 if (s == NULL) {
|
|
783 return p;
|
|
784 }
|
|
785
|
|
786 p = ngx_snprintf(buf, len, ", server: %V",
|
|
787 &s->connection->listening->addr_text);
|
|
788 len -= p - buf;
|
|
789 buf = p;
|
|
790
|
|
791 if (s->login.len == 0) {
|
|
792 return p;
|
|
793 }
|
|
794
|
|
795 p = ngx_snprintf(buf, len, ", login: \"%V\"", &s->login);
|
|
796 len -= p - buf;
|
|
797 buf = p;
|
|
798
|
|
799 if (s->proxy == NULL) {
|
|
800 return p;
|
|
801 }
|
|
802
|
|
803 p = ngx_snprintf(buf, len, ", upstream: %V",
|
|
804 &s->proxy->upstream.peers->peer[0].name);
|
|
805
|
|
806 return p;
|
|
807 }
|