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