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);
|
258
|
15 static void ngx_imap_do_auth(ngx_imap_session_t *s);
|
76
|
16 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s);
|
90
|
17 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
76
|
18
|
88
|
19 #if (NGX_IMAP_SSL)
|
132
|
20 static void ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c);
|
96
|
21 static void ngx_imap_ssl_handshake_handler(ngx_connection_t *c);
|
88
|
22 #endif
|
|
23
|
76
|
24
|
|
25 static ngx_str_t greetings[] = {
|
|
26 ngx_string("+OK POP3 ready" CRLF),
|
78
|
27 ngx_string("* OK IMAP4 ready" CRLF)
|
76
|
28 };
|
|
29
|
|
30 static ngx_str_t internal_server_errors[] = {
|
|
31 ngx_string("-ERR internal server error" CRLF),
|
|
32 ngx_string("* BAD internal server error" CRLF),
|
|
33 };
|
|
34
|
|
35 static u_char pop3_ok[] = "+OK" CRLF;
|
252
|
36 static u_char pop3_next[] = "+ " CRLF;
|
|
37 static u_char pop3_username[] = "+ VXNlcm5hbWU6" CRLF;
|
|
38 static u_char pop3_password[] = "+ UGFzc3dvcmQ6" CRLF;
|
76
|
39 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
|
|
40
|
92
|
41 static u_char imap_star[] = "* ";
|
78
|
42 static u_char imap_ok[] = "OK completed" CRLF;
|
76
|
43 static u_char imap_next[] = "+ OK" CRLF;
|
|
44 static u_char imap_bye[] = "* BYE" CRLF;
|
|
45 static u_char imap_invalid_command[] = "BAD invalid command" CRLF;
|
|
46
|
|
47
|
|
48 void
|
|
49 ngx_imap_init_connection(ngx_connection_t *c)
|
|
50 {
|
190
|
51 in_addr_t in_addr;
|
|
52 socklen_t len;
|
|
53 ngx_uint_t i;
|
|
54 struct sockaddr_in sin;
|
|
55 ngx_imap_log_ctx_t *ctx;
|
|
56 ngx_imap_in_port_t *imip;
|
|
57 ngx_imap_in_addr_t *imia;
|
|
58 ngx_imap_session_t *s;
|
92
|
59 #if (NGX_IMAP_SSL)
|
132
|
60 ngx_imap_ssl_conf_t *sslcf;
|
92
|
61 #endif
|
90
|
62
|
190
|
63
|
|
64 /* find the server configuration for the address:port */
|
|
65
|
|
66 /* AF_INET only */
|
|
67
|
|
68 imip = c->listening->servers;
|
|
69 imia = imip->addrs;
|
|
70
|
|
71 i = 0;
|
|
72
|
|
73 if (imip->naddrs > 1) {
|
|
74
|
|
75 /*
|
|
76 * There are several addresses on this port and one of them
|
|
77 * is the "*:port" wildcard so getsockname() is needed to determine
|
|
78 * the server address.
|
|
79 *
|
|
80 * AcceptEx() already gave this address.
|
|
81 */
|
|
82
|
|
83 #if (NGX_WIN32)
|
|
84 if (c->local_sockaddr) {
|
|
85 in_addr =
|
|
86 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
|
90
|
87
|
190
|
88 } else
|
|
89 #endif
|
|
90 {
|
|
91 len = sizeof(struct sockaddr_in);
|
|
92 if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
|
|
93 ngx_connection_error(c, ngx_socket_errno,
|
|
94 "getsockname() failed");
|
|
95 ngx_imap_close_connection(c);
|
|
96 return;
|
|
97 }
|
|
98
|
|
99 in_addr = sin.sin_addr.s_addr;
|
|
100 }
|
|
101
|
|
102 /* the last address is "*" */
|
|
103
|
|
104 for ( /* void */ ; i < imip->naddrs - 1; i++) {
|
|
105 if (in_addr == imia[i].addr) {
|
|
106 break;
|
|
107 }
|
|
108 }
|
|
109 }
|
|
110
|
|
111
|
|
112 s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t));
|
|
113 if (s == NULL) {
|
90
|
114 ngx_imap_close_connection(c);
|
|
115 return;
|
126
|
116 }
|
90
|
117
|
190
|
118 s->main_conf = imia[i].ctx->main_conf;
|
|
119 s->srv_conf = imia[i].ctx->srv_conf;
|
|
120
|
|
121 s->addr_text = &imia[i].addr_text;
|
|
122
|
|
123 c->data = s;
|
|
124 s->connection = c;
|
|
125
|
|
126 ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client %V connected to %V",
|
|
127 c->number, &c->addr_text, s->addr_text);
|
|
128
|
|
129 ctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t));
|
|
130 if (ctx == NULL) {
|
|
131 ngx_imap_close_connection(c);
|
|
132 return;
|
|
133 }
|
|
134
|
|
135 ctx->client = &c->addr_text;
|
|
136 ctx->session = s;
|
90
|
137
|
|
138 c->log->connection = c->number;
|
|
139 c->log->handler = ngx_imap_log_error;
|
190
|
140 c->log->data = ctx;
|
90
|
141 c->log->action = "sending client greeting line";
|
|
142
|
|
143 c->log_error = NGX_ERROR_INFO;
|
|
144
|
92
|
145 #if (NGX_IMAP_SSL)
|
|
146
|
190
|
147 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
92
|
148
|
|
149 if (sslcf->enable) {
|
132
|
150 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
|
96
|
151 return;
|
92
|
152 }
|
|
153
|
|
154 #endif
|
|
155
|
96
|
156 ngx_imap_init_session(c);
|
90
|
157 }
|
|
158
|
|
159
|
96
|
160 #if (NGX_IMAP_SSL)
|
|
161
|
90
|
162 static void
|
132
|
163 ngx_imap_starttls_handler(ngx_event_t *rev)
|
|
164 {
|
|
165 ngx_connection_t *c;
|
|
166 ngx_imap_session_t *s;
|
|
167 ngx_imap_ssl_conf_t *sslcf;
|
|
168
|
|
169 c = rev->data;
|
|
170 s = c->data;
|
190
|
171 s->starttls = 1;
|
132
|
172
|
|
173 c->log->action = "in starttls state";
|
|
174
|
|
175 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
176
|
|
177 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
|
|
178 }
|
|
179
|
|
180
|
|
181 static void
|
|
182 ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
|
|
183 {
|
190
|
184 ngx_imap_session_t *s;
|
132
|
185 ngx_imap_core_srv_conf_t *cscf;
|
|
186
|
|
187 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
|
|
188 ngx_imap_close_connection(c);
|
|
189 return;
|
|
190 }
|
|
191
|
|
192 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
|
|
193
|
190
|
194 s = c->data;
|
|
195
|
|
196 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
197
|
|
198 ngx_add_timer(c->read, cscf->timeout);
|
|
199
|
|
200 c->ssl->handler = ngx_imap_ssl_handshake_handler;
|
|
201
|
|
202 return;
|
|
203 }
|
|
204
|
|
205 ngx_imap_ssl_handshake_handler(c);
|
|
206 }
|
|
207
|
|
208
|
|
209 static void
|
96
|
210 ngx_imap_ssl_handshake_handler(ngx_connection_t *c)
|
126
|
211 {
|
190
|
212 ngx_imap_session_t *s;
|
|
213
|
96
|
214 if (c->ssl->handshaked) {
|
132
|
215
|
190
|
216 s = c->data;
|
|
217
|
|
218 if (s->starttls) {
|
132
|
219 c->read->handler = ngx_imap_init_protocol;
|
|
220 c->write->handler = ngx_imap_send;
|
|
221
|
|
222 ngx_imap_init_protocol(c->read);
|
|
223
|
|
224 return;
|
|
225 }
|
|
226
|
96
|
227 ngx_imap_init_session(c);
|
|
228 return;
|
|
229 }
|
|
230
|
|
231 ngx_imap_close_connection(c);
|
|
232 }
|
|
233
|
|
234 #endif
|
|
235
|
|
236
|
|
237 static void
|
|
238 ngx_imap_init_session(ngx_connection_t *c)
|
90
|
239 {
|
250
|
240 u_char *p;
|
88
|
241 ngx_imap_session_t *s;
|
90
|
242 ngx_imap_core_srv_conf_t *cscf;
|
76
|
243
|
96
|
244 c->read->handler = ngx_imap_init_protocol;
|
|
245 c->write->handler = ngx_imap_send;
|
88
|
246
|
190
|
247 s = c->data;
|
76
|
248
|
190
|
249 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
88
|
250
|
|
251 s->protocol = cscf->protocol;
|
|
252
|
|
253 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module);
|
|
254 if (s->ctx == NULL) {
|
|
255 ngx_imap_session_internal_server_error(s);
|
|
256 return;
|
|
257 }
|
|
258
|
|
259 s->out = greetings[s->protocol];
|
|
260
|
250
|
261 if ((cscf->auth_methods & NGX_IMAP_AUTH_APOP_ENABLED)
|
|
262 && s->protocol == NGX_IMAP_POP3_PROTOCOL)
|
|
263 {
|
|
264 s->salt.data = ngx_palloc(c->pool,
|
|
265 sizeof(" <18446744073709551616.@>" CRLF) - 1
|
|
266 + NGX_TIME_T_LEN
|
|
267 + cscf->server_name.len);
|
|
268 if (s->salt.data == NULL) {
|
|
269 ngx_imap_session_internal_server_error(s);
|
|
270 return;
|
|
271 }
|
|
272
|
|
273 s->salt.len = ngx_sprintf(s->salt.data, "<%ul.%T@%V>" CRLF,
|
|
274 ngx_random(), ngx_time(), &cscf->server_name)
|
|
275 - s->salt.data;
|
|
276
|
|
277 s->out.data = ngx_palloc(c->pool, greetings[0].len + 1 + s->salt.len);
|
|
278 if (s->out.data == NULL) {
|
|
279 ngx_imap_session_internal_server_error(s);
|
|
280 return;
|
|
281 }
|
|
282
|
|
283 p = ngx_cpymem(s->out.data, greetings[0].data, greetings[0].len - 2);
|
|
284 *p++ = ' ';
|
|
285 p = ngx_cpymem(p, s->salt.data, s->salt.len);
|
|
286
|
|
287 s->out.len = p - s->out.data;
|
|
288 }
|
|
289
|
96
|
290 ngx_add_timer(c->read, cscf->timeout);
|
76
|
291
|
96
|
292 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
76
|
293 ngx_imap_close_connection(c);
|
|
294 }
|
88
|
295
|
|
296 ngx_imap_send(c->write);
|
|
297 }
|
|
298
|
|
299
|
|
300 void
|
|
301 ngx_imap_send(ngx_event_t *wev)
|
|
302 {
|
90
|
303 ngx_int_t n;
|
|
304 ngx_connection_t *c;
|
|
305 ngx_imap_session_t *s;
|
|
306 ngx_imap_core_srv_conf_t *cscf;
|
88
|
307
|
|
308 c = wev->data;
|
|
309 s = c->data;
|
|
310
|
|
311 if (wev->timedout) {
|
|
312 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
313 c->timedout = 1;
|
88
|
314 ngx_imap_close_connection(c);
|
|
315 return;
|
|
316 }
|
|
317
|
|
318 if (s->out.len == 0) {
|
|
319 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
320 ngx_imap_close_connection(c);
|
|
321 }
|
|
322
|
|
323 return;
|
|
324 }
|
|
325
|
|
326 n = c->send(c, s->out.data, s->out.len);
|
|
327
|
|
328 if (n > 0) {
|
|
329 s->out.len -= n;
|
|
330
|
90
|
331 if (wev->timer_set) {
|
|
332 ngx_del_timer(wev);
|
|
333 }
|
|
334
|
88
|
335 if (s->quit) {
|
|
336 ngx_imap_close_connection(c);
|
|
337 return;
|
|
338 }
|
|
339
|
|
340 if (s->blocked) {
|
|
341 c->read->handler(c->read);
|
|
342 }
|
|
343
|
|
344 return;
|
|
345 }
|
|
346
|
|
347 if (n == NGX_ERROR) {
|
|
348 ngx_imap_close_connection(c);
|
|
349 return;
|
|
350 }
|
|
351
|
|
352 /* n == NGX_AGAIN */
|
|
353
|
90
|
354 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
355
|
|
356 ngx_add_timer(c->write, cscf->timeout);
|
|
357
|
88
|
358 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
359 ngx_imap_close_connection(c);
|
|
360 return;
|
|
361 }
|
76
|
362 }
|
|
363
|
|
364
|
|
365 static void
|
90
|
366 ngx_imap_init_protocol(ngx_event_t *rev)
|
76
|
367 {
|
|
368 size_t size;
|
|
369 ngx_connection_t *c;
|
|
370 ngx_imap_session_t *s;
|
|
371 ngx_imap_core_srv_conf_t *cscf;
|
|
372
|
|
373 c = rev->data;
|
|
374
|
90
|
375 c->log->action = "in auth state";
|
|
376
|
76
|
377 if (rev->timedout) {
|
|
378 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
379 c->timedout = 1;
|
76
|
380 ngx_imap_close_connection(c);
|
|
381 return;
|
|
382 }
|
|
383
|
88
|
384 s = c->data;
|
76
|
385
|
88
|
386 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
76
|
387 size = 128;
|
|
388 s->imap_state = ngx_pop3_start;
|
|
389 c->read->handler = ngx_pop3_auth_state;
|
|
390
|
|
391 } else {
|
88
|
392 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
76
|
393 size = cscf->imap_client_buffer_size;
|
|
394 s->imap_state = ngx_imap_start;
|
|
395 c->read->handler = ngx_imap_auth_state;
|
|
396 }
|
|
397
|
|
398 if (s->buffer == NULL) {
|
132
|
399 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
|
|
400 == NGX_ERROR)
|
|
401 {
|
|
402 ngx_imap_session_internal_server_error(s);
|
|
403 return;
|
|
404 }
|
|
405
|
|
406 s->buffer = ngx_create_temp_buf(c->pool, size);
|
|
407 if (s->buffer == NULL) {
|
|
408 ngx_imap_session_internal_server_error(s);
|
|
409 return;
|
|
410 }
|
76
|
411 }
|
|
412
|
|
413 c->read->handler(rev);
|
|
414 }
|
|
415
|
|
416
|
|
417 void
|
|
418 ngx_imap_auth_state(ngx_event_t *rev)
|
|
419 {
|
92
|
420 u_char *text, *last, *p, *dst, *src, *end;
|
88
|
421 ssize_t text_len, last_len;
|
76
|
422 ngx_str_t *arg;
|
|
423 ngx_int_t rc;
|
92
|
424 ngx_uint_t tag, i;
|
76
|
425 ngx_connection_t *c;
|
|
426 ngx_imap_session_t *s;
|
|
427 ngx_imap_core_srv_conf_t *cscf;
|
132
|
428 #if (NGX_IMAP_SSL)
|
|
429 ngx_imap_ssl_conf_t *sslcf;
|
|
430 #endif
|
76
|
431
|
|
432 c = rev->data;
|
|
433 s = c->data;
|
|
434
|
|
435 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state");
|
|
436
|
|
437 if (rev->timedout) {
|
|
438 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
439 c->timedout = 1;
|
76
|
440 ngx_imap_close_connection(c);
|
|
441 return;
|
|
442 }
|
|
443
|
88
|
444 if (s->out.len) {
|
|
445 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
446 s->blocked = 1;
|
|
447 return;
|
|
448 }
|
|
449
|
|
450 s->blocked = 0;
|
|
451
|
76
|
452 rc = ngx_imap_read_command(s);
|
|
453
|
|
454 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth: %i", rc);
|
|
455
|
|
456 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
457 return;
|
|
458 }
|
|
459
|
|
460 tag = 1;
|
|
461
|
|
462 text = NULL;
|
|
463 text_len = 0;
|
|
464
|
|
465 last = imap_ok;
|
|
466 last_len = sizeof(imap_ok) - 1;
|
|
467
|
|
468 if (rc == NGX_OK) {
|
|
469
|
|
470 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth command: %i",
|
|
471 s->command);
|
|
472
|
92
|
473 if (s->backslash) {
|
|
474
|
|
475 arg = s->args.elts;
|
|
476
|
|
477 for (i = 0; i < s->args.nelts; i++) {
|
|
478 dst = arg[i].data;
|
|
479 end = dst + arg[i].len;
|
|
480
|
|
481 for (src = dst; src < end; dst++) {
|
|
482 *dst = *src;
|
|
483 if (*src++ == '\\') {
|
|
484 *dst = *src++;
|
|
485 }
|
|
486 }
|
|
487
|
|
488 arg[i].len = dst - arg[i].data;
|
|
489 }
|
|
490
|
|
491 s->backslash = 0;
|
|
492 }
|
|
493
|
76
|
494 switch (s->command) {
|
|
495
|
|
496 case NGX_IMAP_LOGIN:
|
132
|
497
|
|
498 #if (NGX_IMAP_SSL)
|
|
499
|
|
500 if (c->ssl == NULL) {
|
|
501 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
502
|
|
503 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
|
|
504 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
505 break;
|
|
506 }
|
|
507 }
|
|
508 #endif
|
|
509
|
118
|
510 arg = s->args.elts;
|
76
|
511
|
118
|
512 if (s->args.nelts == 2 && arg[0].len) {
|
76
|
513
|
|
514 s->login.len = arg[0].len;
|
|
515 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
516 if (s->login.data == NULL) {
|
|
517 ngx_imap_session_internal_server_error(s);
|
|
518 return;
|
|
519 }
|
|
520
|
|
521 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
522
|
|
523 s->passwd.len = arg[1].len;
|
|
524 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
525 if (s->passwd.data == NULL) {
|
|
526 ngx_imap_session_internal_server_error(s);
|
|
527 return;
|
|
528 }
|
|
529
|
|
530 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
531
|
96
|
532 #if (NGX_DEBUG_IMAP_PASSWD)
|
76
|
533 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
534 "imap login:\"%V\" passwd:\"%V\"",
|
|
535 &s->login, &s->passwd);
|
96
|
536 #else
|
|
537 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
538 "imap login:\"%V\"", &s->login);
|
|
539 #endif
|
76
|
540
|
258
|
541 ngx_imap_do_auth(s);
|
76
|
542 return;
|
|
543 }
|
|
544
|
252
|
545 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
76
|
546 break;
|
|
547
|
|
548 case NGX_IMAP_CAPABILITY:
|
|
549 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
550
|
|
551 #if (NGX_IMAP_SSL)
|
|
552
|
|
553 if (c->ssl == NULL) {
|
|
554 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
555
|
|
556 if (sslcf->starttls == NGX_IMAP_STARTTLS_ON) {
|
|
557 text_len = cscf->imap_starttls_capability.len;
|
|
558 text = cscf->imap_starttls_capability.data;
|
|
559 break;
|
|
560 }
|
|
561
|
|
562 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
|
|
563 text_len = cscf->imap_starttls_only_capability.len;
|
|
564 text = cscf->imap_starttls_only_capability.data;
|
|
565 break;
|
|
566 }
|
|
567 }
|
|
568 #endif
|
|
569
|
|
570 text_len = cscf->imap_capability.len;
|
|
571 text = cscf->imap_capability.data;
|
76
|
572 break;
|
|
573
|
|
574 case NGX_IMAP_LOGOUT:
|
88
|
575 s->quit = 1;
|
76
|
576 text = imap_bye;
|
|
577 text_len = sizeof(imap_bye) - 1;
|
|
578 break;
|
|
579
|
|
580 case NGX_IMAP_NOOP:
|
|
581 break;
|
|
582
|
132
|
583 #if (NGX_IMAP_SSL)
|
|
584
|
|
585 case NGX_IMAP_STARTTLS:
|
|
586 if (c->ssl == NULL) {
|
|
587 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
588 if (sslcf->starttls) {
|
|
589 c->read->handler = ngx_imap_starttls_handler;
|
|
590 break;
|
|
591 }
|
|
592 }
|
|
593
|
|
594 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
595 break;
|
|
596 #endif
|
|
597
|
76
|
598 default:
|
|
599 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
600 break;
|
|
601 }
|
|
602
|
|
603 } else if (rc == NGX_IMAP_NEXT) {
|
|
604 last = imap_next;
|
|
605 last_len = sizeof(imap_next) - 1;
|
|
606 tag = 0;
|
|
607 }
|
|
608
|
|
609 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
610 last = imap_invalid_command;
|
|
611 last_len = sizeof(imap_invalid_command) - 1;
|
|
612 }
|
|
613
|
|
614 if (tag) {
|
92
|
615 if (s->tag.len == 0) {
|
|
616 s->tag.len = sizeof(imap_star) - 1;
|
|
617 s->tag.data = (u_char *) imap_star;
|
|
618 }
|
|
619
|
88
|
620 if (s->tagged_line.len < s->tag.len + text_len + last_len) {
|
|
621 s->tagged_line.len = s->tag.len + text_len + last_len;
|
|
622 s->tagged_line.data = ngx_palloc(c->pool, s->tagged_line.len);
|
|
623 if (s->tagged_line.data == NULL) {
|
76
|
624 ngx_imap_close_connection(c);
|
|
625 return;
|
|
626 }
|
|
627 }
|
|
628
|
88
|
629 s->out.data = s->tagged_line.data;
|
|
630 s->out.len = s->tag.len + text_len + last_len;
|
|
631
|
|
632 p = s->out.data;
|
76
|
633
|
|
634 if (text) {
|
|
635 p = ngx_cpymem(p, text, text_len);
|
|
636 }
|
|
637 p = ngx_cpymem(p, s->tag.data, s->tag.len);
|
|
638 ngx_memcpy(p, last, last_len);
|
|
639
|
|
640
|
|
641 } else {
|
88
|
642 s->out.data = last;
|
|
643 s->out.len = last_len;
|
76
|
644 }
|
|
645
|
88
|
646 if (rc != NGX_IMAP_NEXT) {
|
|
647 s->args.nelts = 0;
|
|
648 s->buffer->pos = s->buffer->start;
|
|
649 s->buffer->last = s->buffer->start;
|
|
650 s->tag.len = 0;
|
76
|
651 }
|
|
652
|
88
|
653 ngx_imap_send(c->write);
|
76
|
654 }
|
|
655
|
|
656
|
|
657 void
|
|
658 ngx_pop3_auth_state(ngx_event_t *rev)
|
|
659 {
|
252
|
660 u_char *text, *p, *last;
|
76
|
661 ssize_t size;
|
|
662 ngx_int_t rc;
|
252
|
663 ngx_str_t *arg, salt, plain;
|
76
|
664 ngx_connection_t *c;
|
|
665 ngx_imap_session_t *s;
|
|
666 ngx_imap_core_srv_conf_t *cscf;
|
132
|
667 #if (NGX_IMAP_SSL)
|
|
668 ngx_imap_ssl_conf_t *sslcf;
|
|
669 #endif
|
76
|
670
|
|
671 c = rev->data;
|
|
672 s = c->data;
|
|
673
|
|
674 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state");
|
|
675
|
|
676 if (rev->timedout) {
|
|
677 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
678 c->timedout = 1;
|
76
|
679 ngx_imap_close_connection(c);
|
|
680 return;
|
|
681 }
|
|
682
|
88
|
683 if (s->out.len) {
|
|
684 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
685 s->blocked = 1;
|
|
686 return;
|
|
687 }
|
|
688
|
|
689 s->blocked = 0;
|
|
690
|
76
|
691 rc = ngx_imap_read_command(s);
|
|
692
|
|
693 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
694 return;
|
|
695 }
|
|
696
|
|
697 text = pop3_ok;
|
|
698 size = sizeof(pop3_ok) - 1;
|
|
699
|
|
700 if (rc == NGX_OK) {
|
|
701 switch (s->imap_state) {
|
|
702
|
|
703 case ngx_pop3_start:
|
|
704
|
|
705 switch (s->command) {
|
|
706
|
|
707 case NGX_POP3_USER:
|
|
708 if (s->args.nelts == 1) {
|
|
709 s->imap_state = ngx_pop3_user;
|
|
710
|
|
711 arg = s->args.elts;
|
|
712 s->login.len = arg[0].len;
|
|
713 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
714 if (s->login.data == NULL) {
|
|
715 ngx_imap_session_internal_server_error(s);
|
|
716 return;
|
|
717 }
|
|
718
|
|
719 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
720
|
|
721 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
722 "pop3 login: \"%V\"", &s->login);
|
|
723
|
252
|
724 break;
|
76
|
725 }
|
|
726
|
252
|
727 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
76
|
728 break;
|
|
729
|
|
730 case NGX_POP3_CAPA:
|
|
731 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
732
|
|
733 #if (NGX_IMAP_SSL)
|
|
734
|
|
735 if (c->ssl == NULL) {
|
|
736 sslcf = ngx_imap_get_module_srv_conf(s,
|
|
737 ngx_imap_ssl_module);
|
|
738 if (sslcf->starttls) {
|
|
739 size = cscf->pop3_starttls_capability.len;
|
|
740 text = cscf->pop3_starttls_capability.data;
|
|
741 break;
|
|
742 }
|
|
743 }
|
|
744 #endif
|
|
745
|
|
746 size = cscf->pop3_capability.len;
|
|
747 text = cscf->pop3_capability.data;
|
76
|
748 break;
|
|
749
|
250
|
750 case NGX_POP3_APOP:
|
|
751 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
752
|
|
753 if ((cscf->auth_methods & NGX_IMAP_AUTH_APOP_ENABLED)
|
|
754 && s->args.nelts == 2)
|
|
755 {
|
|
756 arg = s->args.elts;
|
|
757
|
|
758 s->login.len = arg[0].len;
|
|
759 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
760 if (s->login.data == NULL) {
|
|
761 ngx_imap_session_internal_server_error(s);
|
|
762 return;
|
|
763 }
|
|
764
|
|
765 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
766
|
|
767 s->passwd.len = arg[1].len;
|
|
768 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
769 if (s->passwd.data == NULL) {
|
|
770 ngx_imap_session_internal_server_error(s);
|
|
771 return;
|
|
772 }
|
|
773
|
|
774 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
775
|
|
776 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
777 "pop3 apop: \"%V\" \"%V\"",
|
|
778 &s->login, &s->passwd);
|
|
779
|
|
780 s->auth_method = NGX_IMAP_AUTH_APOP;
|
|
781
|
258
|
782 ngx_imap_do_auth(s);
|
250
|
783 return;
|
252
|
784 }
|
250
|
785
|
252
|
786 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
787 break;
|
|
788
|
|
789 case NGX_POP3_AUTH:
|
|
790 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
791
|
|
792 if (s->args.nelts == 0) {
|
|
793 size = cscf->pop3_auth_capability.len;
|
|
794 text = cscf->pop3_auth_capability.data;
|
|
795 s->state = 0;
|
|
796 break;
|
|
797 }
|
|
798
|
|
799 if (s->args.nelts != 1) {
|
250
|
800 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
252
|
801 break;
|
250
|
802 }
|
|
803
|
252
|
804 arg = s->args.elts;
|
|
805
|
|
806 if (arg[0].len == 5) {
|
|
807
|
|
808 if (ngx_strncasecmp(arg[0].data, "LOGIN", 5) == 0) {
|
|
809 s->imap_state = ngx_pop3_auth_login_username;
|
|
810
|
|
811 size = sizeof(pop3_username) - 1;
|
|
812 text = pop3_username;
|
|
813
|
|
814 break;
|
|
815
|
|
816 } else if (ngx_strncasecmp(arg[0].data, "PLAIN", 5) == 0) {
|
|
817 s->imap_state = ngx_pop3_auth_plain;
|
|
818
|
|
819 size = sizeof(pop3_next) - 1;
|
|
820 text = pop3_next;
|
|
821
|
|
822 break;
|
|
823 }
|
|
824
|
|
825 } else if (arg[0].len == 8
|
|
826 && ngx_strncasecmp(arg[0].data, "CRAM-MD5", 8) == 0)
|
|
827 {
|
|
828 s->imap_state = ngx_pop3_auth_cram_md5;
|
|
829
|
|
830 text = ngx_palloc(c->pool,
|
|
831 sizeof("+ " CRLF) - 1
|
|
832 + ngx_base64_encoded_length(s->salt.len));
|
|
833 if (text == NULL) {
|
|
834 ngx_imap_session_internal_server_error(s);
|
|
835 return;
|
|
836 }
|
|
837
|
|
838 text[0] = '+'; text[1]= ' ';
|
|
839 salt.data = &text[2];
|
|
840 s->salt.len -= 2;
|
|
841
|
|
842 ngx_encode_base64(&salt, &s->salt);
|
|
843
|
|
844 s->salt.len += 2;
|
|
845 size = 2 + salt.len;
|
|
846 text[size++] = CR; text[size++] = LF;
|
|
847
|
|
848 break;
|
|
849 }
|
|
850
|
|
851 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
250
|
852 break;
|
|
853
|
76
|
854 case NGX_POP3_QUIT:
|
88
|
855 s->quit = 1;
|
76
|
856 break;
|
|
857
|
|
858 case NGX_POP3_NOOP:
|
|
859 break;
|
|
860
|
132
|
861 #if (NGX_IMAP_SSL)
|
|
862
|
|
863 case NGX_POP3_STLS:
|
|
864 if (c->ssl == NULL) {
|
|
865 sslcf = ngx_imap_get_module_srv_conf(s,
|
|
866 ngx_imap_ssl_module);
|
|
867 if (sslcf->starttls) {
|
|
868 c->read->handler = ngx_imap_starttls_handler;
|
|
869 break;
|
|
870 }
|
|
871 }
|
|
872
|
|
873 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
874 break;
|
|
875 #endif
|
|
876
|
76
|
877 default:
|
|
878 s->imap_state = ngx_pop3_start;
|
|
879 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
880 break;
|
|
881 }
|
|
882
|
|
883 break;
|
|
884
|
|
885 case ngx_pop3_user:
|
|
886
|
|
887 switch (s->command) {
|
|
888
|
|
889 case NGX_POP3_PASS:
|
|
890 if (s->args.nelts == 1) {
|
|
891 arg = s->args.elts;
|
|
892 s->passwd.len = arg[0].len;
|
|
893 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
894 if (s->passwd.data == NULL) {
|
|
895 ngx_imap_session_internal_server_error(s);
|
|
896 return;
|
|
897 }
|
|
898
|
|
899 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
|
|
900
|
96
|
901 #if (NGX_DEBUG_IMAP_PASSWD)
|
76
|
902 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
903 "pop3 passwd: \"%V\"", &s->passwd);
|
96
|
904 #endif
|
76
|
905
|
258
|
906 ngx_imap_do_auth(s);
|
76
|
907 return;
|
|
908 }
|
|
909
|
252
|
910 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
76
|
911 break;
|
|
912
|
|
913 case NGX_POP3_CAPA:
|
|
914 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
915 size = cscf->pop3_capability.len;
|
|
916 text = cscf->pop3_capability.data;
|
76
|
917 break;
|
|
918
|
|
919 case NGX_POP3_QUIT:
|
88
|
920 s->quit = 1;
|
76
|
921 break;
|
|
922
|
|
923 case NGX_POP3_NOOP:
|
|
924 break;
|
|
925
|
|
926 default:
|
|
927 s->imap_state = ngx_pop3_start;
|
|
928 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
929 break;
|
|
930 }
|
|
931
|
|
932 break;
|
|
933
|
|
934 /* suppress warinings */
|
|
935 case ngx_pop3_passwd:
|
|
936 break;
|
252
|
937
|
|
938 case ngx_pop3_auth_login_username:
|
|
939 arg = s->args.elts;
|
|
940 s->imap_state = ngx_pop3_auth_login_password;
|
|
941
|
|
942 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
943 "pop3 auth login username: \"%V\"", &arg[0]);
|
|
944
|
|
945 s->login.data = ngx_palloc(c->pool,
|
|
946 ngx_base64_decoded_length(arg[0].len));
|
|
947 if (s->login.data == NULL){
|
|
948 ngx_imap_session_internal_server_error(s);
|
|
949 return;
|
|
950 }
|
|
951
|
|
952 if (ngx_decode_base64(&s->login, &arg[0]) != NGX_OK) {
|
|
953 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
954 "client sent invalid base64 encoding "
|
|
955 "in AUTH LOGIN command");
|
|
956 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
957 break;
|
|
958 }
|
|
959
|
|
960 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
961 "pop3 auth login username: \"%V\"", &s->login);
|
|
962
|
|
963 size = sizeof(pop3_password) - 1;
|
|
964 text = pop3_password;
|
|
965
|
|
966 break;
|
|
967
|
|
968 case ngx_pop3_auth_login_password:
|
|
969 arg = s->args.elts;
|
|
970
|
|
971 #if (NGX_DEBUG_IMAP_PASSWD)
|
|
972 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
973 "pop3 auth login password: \"%V\"", &arg[0]);
|
|
974 #endif
|
|
975
|
|
976 s->passwd.data = ngx_palloc(c->pool,
|
|
977 ngx_base64_decoded_length(arg[0].len));
|
|
978 if (s->passwd.data == NULL){
|
|
979 ngx_imap_session_internal_server_error(s);
|
|
980 return;
|
|
981 }
|
|
982
|
|
983 if (ngx_decode_base64(&s->passwd, &arg[0]) != NGX_OK) {
|
|
984 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
985 "client sent invalid base64 encoding "
|
|
986 "in AUTH LOGIN command");
|
|
987 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
988 break;
|
|
989 }
|
|
990
|
|
991 #if (NGX_DEBUG_IMAP_PASSWD)
|
|
992 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
993 "pop3 auth login password: \"%V\"", &s->passwd);
|
|
994 #endif
|
|
995
|
258
|
996 ngx_imap_do_auth(s);
|
252
|
997 return;
|
|
998
|
|
999 case ngx_pop3_auth_plain:
|
|
1000 arg = s->args.elts;
|
|
1001
|
|
1002 #if (NGX_DEBUG_IMAP_PASSWD)
|
|
1003 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
1004 "pop3 auth plain: \"%V\"", &arg[0]);
|
|
1005 #endif
|
|
1006
|
|
1007 plain.data = ngx_palloc(c->pool,
|
|
1008 ngx_base64_decoded_length(arg[0].len));
|
|
1009 if (plain.data == NULL){
|
|
1010 ngx_imap_session_internal_server_error(s);
|
|
1011 return;
|
|
1012 }
|
|
1013
|
|
1014 if (ngx_decode_base64(&plain, &arg[0]) != NGX_OK) {
|
|
1015 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1016 "client sent invalid base64 encoding "
|
|
1017 "in AUTH PLAIN command");
|
|
1018 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
1019 break;
|
|
1020 }
|
|
1021
|
|
1022 p = plain.data;
|
|
1023 last = p + plain.len;
|
|
1024
|
|
1025 while (p < last && *p++) { /* void */ }
|
|
1026
|
|
1027 if (p == last) {
|
|
1028 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1029 "client sent invalid login "
|
|
1030 "in AUTH PLAIN command");
|
|
1031 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
1032 break;
|
|
1033 }
|
|
1034
|
|
1035 s->login.data = p;
|
|
1036
|
|
1037 while (p < last && *p) { p++; }
|
|
1038
|
|
1039 if (p == last) {
|
|
1040 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1041 "client sent invalid password "
|
|
1042 "in AUTH PLAIN command");
|
|
1043 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
1044 break;
|
|
1045 }
|
|
1046
|
|
1047 s->login.len = p++ - s->login.data;
|
|
1048
|
|
1049 s->passwd.len = last - p;
|
|
1050 s->passwd.data = p;
|
|
1051
|
|
1052 #if (NGX_DEBUG_IMAP_PASSWD)
|
|
1053 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
1054 "pop3 auth plain: \"%V\" \"%V\"",
|
|
1055 &s->login, &s->passwd);
|
|
1056 #endif
|
|
1057
|
258
|
1058 ngx_imap_do_auth(s);
|
252
|
1059 return;
|
|
1060
|
|
1061 case ngx_pop3_auth_cram_md5:
|
|
1062 arg = s->args.elts;
|
|
1063
|
|
1064 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
1065 "pop3 auth cram-md5: \"%V\"", &arg[0]);
|
|
1066
|
|
1067 s->login.data = ngx_palloc(c->pool,
|
|
1068 ngx_base64_decoded_length(arg[0].len));
|
|
1069 if (s->login.data == NULL){
|
|
1070 ngx_imap_session_internal_server_error(s);
|
|
1071 return;
|
|
1072 }
|
|
1073
|
|
1074 if (ngx_decode_base64(&s->login, &arg[0]) != NGX_OK) {
|
|
1075 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1076 "client sent invalid base64 encoding "
|
|
1077 "in AUTH CRAM-MD5 command");
|
|
1078 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
1079 break;
|
|
1080 }
|
|
1081
|
|
1082 p = s->login.data;
|
|
1083 last = p + s->login.len;
|
|
1084
|
|
1085 while (p < last) {
|
|
1086 if (*p++ == ' ') {
|
|
1087 s->login.len = p - s->login.data - 1;
|
|
1088 s->passwd.len = last - p;
|
|
1089 s->passwd.data = p;
|
|
1090 break;
|
|
1091 }
|
|
1092 }
|
|
1093
|
|
1094 if (s->passwd.len != 32) {
|
|
1095 ngx_log_error(NGX_LOG_INFO, c->log, 0,
|
|
1096 "client sent invalid CRAM-MD5 hash "
|
|
1097 "in AUTH CRAM-MD5 command");
|
|
1098 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
1099 break;
|
|
1100 }
|
|
1101
|
|
1102 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
1103 "pop3 auth cram-md5: \"%V\" \"%V\"",
|
|
1104 &s->login, &s->passwd);
|
|
1105
|
|
1106 s->auth_method = NGX_IMAP_AUTH_CRAM_MD5;
|
|
1107
|
258
|
1108 ngx_imap_do_auth(s);
|
252
|
1109 return;
|
76
|
1110 }
|
|
1111 }
|
|
1112
|
|
1113 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
252
|
1114 s->imap_state = ngx_pop3_start;
|
|
1115 s->state = 0;
|
76
|
1116 text = pop3_invalid_command;
|
|
1117 size = sizeof(pop3_invalid_command) - 1;
|
|
1118 }
|
|
1119
|
|
1120 s->args.nelts = 0;
|
|
1121 s->buffer->pos = s->buffer->start;
|
|
1122 s->buffer->last = s->buffer->start;
|
88
|
1123
|
252
|
1124 if (s->state) {
|
|
1125 s->arg_start = s->buffer->start;
|
|
1126 }
|
|
1127
|
88
|
1128 s->out.data = text;
|
|
1129 s->out.len = size;
|
|
1130
|
|
1131 ngx_imap_send(c->write);
|
76
|
1132 }
|
|
1133
|
|
1134
|
258
|
1135 static void
|
|
1136 ngx_imap_do_auth(ngx_imap_session_t *s)
|
|
1137 {
|
|
1138 s->args.nelts = 0;
|
|
1139 s->buffer->pos = s->buffer->start;
|
|
1140 s->buffer->last = s->buffer->start;
|
|
1141 s->state = 0;
|
|
1142
|
|
1143 if (s->connection->read->timer_set) {
|
|
1144 ngx_del_timer(s->connection->read);
|
|
1145 }
|
|
1146
|
|
1147 s->login_attempt++;
|
|
1148
|
|
1149 ngx_imap_auth_http_init(s);
|
|
1150 }
|
|
1151
|
|
1152
|
76
|
1153 static ngx_int_t
|
|
1154 ngx_imap_read_command(ngx_imap_session_t *s)
|
|
1155 {
|
|
1156 ssize_t n;
|
|
1157 ngx_int_t rc;
|
|
1158
|
88
|
1159 n = s->connection->recv(s->connection, s->buffer->last,
|
|
1160 s->buffer->end - s->buffer->last);
|
76
|
1161
|
|
1162 if (n == NGX_ERROR || n == 0) {
|
|
1163 ngx_imap_close_connection(s->connection);
|
|
1164 return NGX_ERROR;
|
|
1165 }
|
|
1166
|
|
1167 if (n > 0) {
|
|
1168 s->buffer->last += n;
|
|
1169 }
|
|
1170
|
|
1171 if (n == NGX_AGAIN) {
|
|
1172 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
|
|
1173 ngx_imap_session_internal_server_error(s);
|
|
1174 return NGX_ERROR;
|
|
1175 }
|
|
1176
|
|
1177 return NGX_AGAIN;
|
|
1178 }
|
|
1179
|
|
1180 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
1181 rc = ngx_pop3_parse_command(s);
|
|
1182 } else {
|
|
1183 rc = ngx_imap_parse_command(s);
|
|
1184 }
|
|
1185
|
|
1186 if (rc == NGX_AGAIN
|
|
1187 || rc == NGX_IMAP_NEXT
|
|
1188 || rc == NGX_IMAP_PARSE_INVALID_COMMAND)
|
|
1189 {
|
|
1190 return rc;
|
|
1191 }
|
|
1192
|
|
1193 if (rc == NGX_ERROR) {
|
|
1194 ngx_imap_close_connection(s->connection);
|
|
1195 return NGX_ERROR;
|
|
1196 }
|
|
1197
|
|
1198 return NGX_OK;
|
|
1199 }
|
|
1200
|
|
1201
|
|
1202 void
|
|
1203 ngx_imap_session_internal_server_error(ngx_imap_session_t *s)
|
|
1204 {
|
88
|
1205 s->out = internal_server_errors[s->protocol];
|
|
1206 s->quit = 1;
|
76
|
1207
|
88
|
1208 ngx_imap_send(s->connection->write);
|
76
|
1209 }
|
|
1210
|
|
1211
|
|
1212 void
|
|
1213 ngx_imap_close_connection(ngx_connection_t *c)
|
|
1214 {
|
|
1215 ngx_pool_t *pool;
|
|
1216
|
|
1217 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
1218 "close imap connection: %d", c->fd);
|
|
1219
|
88
|
1220 #if (NGX_IMAP_SSL)
|
|
1221
|
|
1222 if (c->ssl) {
|
|
1223 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
1224 c->ssl->handler = ngx_imap_close_connection;
|
88
|
1225 return;
|
|
1226 }
|
|
1227 }
|
|
1228
|
|
1229 #endif
|
|
1230
|
132
|
1231 c->destroyed = 1;
|
92
|
1232
|
76
|
1233 pool = c->pool;
|
|
1234
|
|
1235 ngx_close_connection(c);
|
|
1236
|
|
1237 ngx_destroy_pool(pool);
|
|
1238 }
|
88
|
1239
|
|
1240
|
90
|
1241 static u_char *
|
|
1242 ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
|
1243 {
|
116
|
1244 u_char *p;
|
|
1245 ngx_imap_session_t *s;
|
|
1246 ngx_imap_log_ctx_t *ctx;
|
90
|
1247
|
|
1248 if (log->action) {
|
|
1249 p = ngx_snprintf(buf, len, " while %s", log->action);
|
|
1250 len -= p - buf;
|
|
1251 buf = p;
|
|
1252 }
|
126
|
1253
|
90
|
1254 ctx = log->data;
|
|
1255
|
|
1256 p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
|
1257 len -= p - buf;
|
|
1258 buf = p;
|
|
1259
|
|
1260 s = ctx->session;
|
|
1261
|
|
1262 if (s == NULL) {
|
|
1263 return p;
|
|
1264 }
|
|
1265
|
190
|
1266 p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
|
90
|
1267 len -= p - buf;
|
|
1268 buf = p;
|
|
1269
|
|
1270 if (s->login.len == 0) {
|
|
1271 return p;
|
|
1272 }
|
|
1273
|
|
1274 p = ngx_snprintf(buf, len, ", login: \"%V\"", &s->login);
|
|
1275 len -= p - buf;
|
|
1276 buf = p;
|
|
1277
|
|
1278 if (s->proxy == NULL) {
|
|
1279 return p;
|
|
1280 }
|
|
1281
|
|
1282 p = ngx_snprintf(buf, len, ", upstream: %V",
|
|
1283 &s->proxy->upstream.peers->peer[0].name);
|
|
1284
|
|
1285 return p;
|
|
1286 }
|