comparison src/imap/ngx_imap_handler.c @ 583:4e296b7d25bf release-0.3.13

nginx-0.3.13-RELEASE import *) Feature: the IMAP/POP3 proxy supports STARTTLS and STLS. *) Bugfix: the IMAP/POP3 proxy did not work with the select, poll, and /dev/poll methods. *) Bugfix: in SSI handling. *) Bugfix: now Solaris sendfilev() is not used to transfer the client request body to FastCGI-server via the unix domain socket. *) Bugfix: the "auth_basic" directive did not disable the authorization; the bug had appeared in 0.3.11.
author Igor Sysoev <igor@sysoev.ru>
date Mon, 05 Dec 2005 13:18:09 +0000
parents 4d9ea73a627a
children 5e8fb59c18c1
comparison
equal deleted inserted replaced
582:6646640ac20b 583:4e296b7d25bf
14 static void ngx_imap_init_protocol(ngx_event_t *rev); 14 static void ngx_imap_init_protocol(ngx_event_t *rev);
15 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s); 15 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s);
16 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len); 16 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len);
17 17
18 #if (NGX_IMAP_SSL) 18 #if (NGX_IMAP_SSL)
19 static void ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c);
19 static void ngx_imap_ssl_handshake_handler(ngx_connection_t *c); 20 static void ngx_imap_ssl_handshake_handler(ngx_connection_t *c);
20 #endif 21 #endif
21 22
22 23
23 static ngx_str_t greetings[] = { 24 static ngx_str_t greetings[] = {
41 42
42 43
43 void 44 void
44 ngx_imap_init_connection(ngx_connection_t *c) 45 ngx_imap_init_connection(ngx_connection_t *c)
45 { 46 {
46 ngx_imap_log_ctx_t *lctx; 47 ngx_imap_log_ctx_t *lctx;
47 #if (NGX_IMAP_SSL) 48 #if (NGX_IMAP_SSL)
48 ngx_imap_conf_ctx_t *ctx; 49 ngx_imap_conf_ctx_t *ctx;
49 ngx_imap_ssl_conf_t *sslcf; 50 ngx_imap_ssl_conf_t *sslcf;
50 ngx_imap_core_srv_conf_t *cscf;
51 #endif 51 #endif
52 52
53 ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client %V connected to %V", 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); 54 c->number, &c->addr_text, &c->listening->addr_text);
55 55
73 73
74 ctx = c->ctx; 74 ctx = c->ctx;
75 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module); 75 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module);
76 76
77 if (sslcf->enable) { 77 if (sslcf->enable) {
78 if (ngx_ssl_create_connection(&sslcf->ssl, c, 0) == NGX_ERROR) { 78 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
79 ngx_imap_close_connection(c);
80 return;
81 }
82
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; 79 return;
96 } 80 }
97 81
98 #endif 82 #endif
99 83
100 ngx_imap_init_session(c); 84 ngx_imap_init_session(c);
101 } 85 }
102 86
103 87
104 #if (NGX_IMAP_SSL) 88 #if (NGX_IMAP_SSL)
89
90 static void
91 ngx_imap_starttls_handler(ngx_event_t *rev)
92 {
93 ngx_connection_t *c;
94 ngx_imap_session_t *s;
95 ngx_imap_ssl_conf_t *sslcf;
96
97 c = rev->data;
98 s = c->data;
99
100 c->log->action = "in starttls state";
101
102 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
103
104 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
105 }
106
107
108 static void
109 ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
110 {
111 ngx_imap_conf_ctx_t *ctx;
112 ngx_imap_core_srv_conf_t *cscf;
113
114 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
115 ngx_imap_close_connection(c);
116 return;
117 }
118
119 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
120
121 ctx = c->ctx;
122 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module);
123
124 ngx_add_timer(c->read, cscf->timeout);
125
126 c->ssl->handler = ngx_imap_ssl_handshake_handler;
127
128 return;
129 }
130
131 ngx_imap_ssl_handshake_handler(c);
132 }
133
105 134
106 static void 135 static void
107 ngx_imap_ssl_handshake_handler(ngx_connection_t *c) 136 ngx_imap_ssl_handshake_handler(ngx_connection_t *c)
108 { 137 {
109 if (c->ssl->handshaked) { 138 if (c->ssl->handshaked) {
139
140 if (c->data) {
141 c->read->handler = ngx_imap_init_protocol;
142 c->write->handler = ngx_imap_send;
143
144 ngx_imap_init_protocol(c->read);
145
146 return;
147 }
148
110 ngx_imap_init_session(c); 149 ngx_imap_init_session(c);
111 return; 150 return;
112 } 151 }
113 152
114 ngx_imap_close_connection(c); 153 ngx_imap_close_connection(c);
115 return;
116 } 154 }
117 155
118 #endif 156 #endif
119 157
120 158
250 ngx_imap_close_connection(c); 288 ngx_imap_close_connection(c);
251 return; 289 return;
252 } 290 }
253 291
254 s = c->data; 292 s = c->data;
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 293
261 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) { 294 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
262 size = 128; 295 size = 128;
263 s->imap_state = ngx_pop3_start; 296 s->imap_state = ngx_pop3_start;
264 c->read->handler = ngx_pop3_auth_state; 297 c->read->handler = ngx_pop3_auth_state;
268 size = cscf->imap_client_buffer_size; 301 size = cscf->imap_client_buffer_size;
269 s->imap_state = ngx_imap_start; 302 s->imap_state = ngx_imap_start;
270 c->read->handler = ngx_imap_auth_state; 303 c->read->handler = ngx_imap_auth_state;
271 } 304 }
272 305
273 s->buffer = ngx_create_temp_buf(c->pool, size);
274 if (s->buffer == NULL) { 306 if (s->buffer == NULL) {
275 ngx_imap_session_internal_server_error(s); 307 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
276 return; 308 == NGX_ERROR)
309 {
310 ngx_imap_session_internal_server_error(s);
311 return;
312 }
313
314 s->buffer = ngx_create_temp_buf(c->pool, size);
315 if (s->buffer == NULL) {
316 ngx_imap_session_internal_server_error(s);
317 return;
318 }
277 } 319 }
278 320
279 c->read->handler(rev); 321 c->read->handler(rev);
280 } 322 }
281 323
289 ngx_int_t rc; 331 ngx_int_t rc;
290 ngx_uint_t tag, i; 332 ngx_uint_t tag, i;
291 ngx_connection_t *c; 333 ngx_connection_t *c;
292 ngx_imap_session_t *s; 334 ngx_imap_session_t *s;
293 ngx_imap_core_srv_conf_t *cscf; 335 ngx_imap_core_srv_conf_t *cscf;
336 #if (NGX_IMAP_SSL)
337 ngx_imap_ssl_conf_t *sslcf;
338 #endif
294 339
295 c = rev->data; 340 c = rev->data;
296 s = c->data; 341 s = c->data;
297 342
298 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state"); 343 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state");
355 } 400 }
356 401
357 switch (s->command) { 402 switch (s->command) {
358 403
359 case NGX_IMAP_LOGIN: 404 case NGX_IMAP_LOGIN:
405
406 #if (NGX_IMAP_SSL)
407
408 if (c->ssl == NULL) {
409 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
410
411 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
412 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
413 break;
414 }
415 }
416 #endif
417
360 arg = s->args.elts; 418 arg = s->args.elts;
361 419
362 if (s->args.nelts == 2 && arg[0].len) { 420 if (s->args.nelts == 2 && arg[0].len) {
363 421
364 s->login.len = arg[0].len; 422 s->login.len = arg[0].len;
408 466
409 break; 467 break;
410 468
411 case NGX_IMAP_CAPABILITY: 469 case NGX_IMAP_CAPABILITY:
412 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); 470 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
413 text = cscf->imap_capability->pos; 471
414 text_len = cscf->imap_capability->last - cscf->imap_capability->pos; 472 #if (NGX_IMAP_SSL)
473
474 if (c->ssl == NULL) {
475 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
476
477 if (sslcf->starttls == NGX_IMAP_STARTTLS_ON) {
478 text_len = cscf->imap_starttls_capability.len;
479 text = cscf->imap_starttls_capability.data;
480 break;
481 }
482
483 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
484 text_len = cscf->imap_starttls_only_capability.len;
485 text = cscf->imap_starttls_only_capability.data;
486 break;
487 }
488 }
489 #endif
490
491 text_len = cscf->imap_capability.len;
492 text = cscf->imap_capability.data;
415 break; 493 break;
416 494
417 case NGX_IMAP_LOGOUT: 495 case NGX_IMAP_LOGOUT:
418 s->quit = 1; 496 s->quit = 1;
419 text = imap_bye; 497 text = imap_bye;
420 text_len = sizeof(imap_bye) - 1; 498 text_len = sizeof(imap_bye) - 1;
421 break; 499 break;
422 500
423 case NGX_IMAP_NOOP: 501 case NGX_IMAP_NOOP:
424 break; 502 break;
503
504 #if (NGX_IMAP_SSL)
505
506 case NGX_IMAP_STARTTLS:
507 if (c->ssl == NULL) {
508 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
509 if (sslcf->starttls) {
510 c->read->handler = ngx_imap_starttls_handler;
511 break;
512 }
513 }
514
515 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
516 break;
517 #endif
425 518
426 default: 519 default:
427 rc = NGX_IMAP_PARSE_INVALID_COMMAND; 520 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
428 break; 521 break;
429 } 522 }
490 ngx_int_t rc; 583 ngx_int_t rc;
491 ngx_str_t *arg; 584 ngx_str_t *arg;
492 ngx_connection_t *c; 585 ngx_connection_t *c;
493 ngx_imap_session_t *s; 586 ngx_imap_session_t *s;
494 ngx_imap_core_srv_conf_t *cscf; 587 ngx_imap_core_srv_conf_t *cscf;
588 #if (NGX_IMAP_SSL)
589 ngx_imap_ssl_conf_t *sslcf;
590 #endif
495 591
496 c = rev->data; 592 c = rev->data;
497 s = c->data; 593 s = c->data;
498 594
499 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state"); 595 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state");
552 648
553 break; 649 break;
554 650
555 case NGX_POP3_CAPA: 651 case NGX_POP3_CAPA:
556 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); 652 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
557 text = cscf->pop3_capability->pos; 653
558 size = cscf->pop3_capability->last - cscf->pop3_capability->pos; 654 #if (NGX_IMAP_SSL)
655
656 if (c->ssl == NULL) {
657 sslcf = ngx_imap_get_module_srv_conf(s,
658 ngx_imap_ssl_module);
659 if (sslcf->starttls) {
660 size = cscf->pop3_starttls_capability.len;
661 text = cscf->pop3_starttls_capability.data;
662 break;
663 }
664 }
665 #endif
666
667 size = cscf->pop3_capability.len;
668 text = cscf->pop3_capability.data;
559 break; 669 break;
560 670
561 case NGX_POP3_QUIT: 671 case NGX_POP3_QUIT:
562 s->quit = 1; 672 s->quit = 1;
563 break; 673 break;
564 674
565 case NGX_POP3_NOOP: 675 case NGX_POP3_NOOP:
566 break; 676 break;
677
678 #if (NGX_IMAP_SSL)
679
680 case NGX_POP3_STLS:
681 if (c->ssl == NULL) {
682 sslcf = ngx_imap_get_module_srv_conf(s,
683 ngx_imap_ssl_module);
684 if (sslcf->starttls) {
685 c->read->handler = ngx_imap_starttls_handler;
686 break;
687 }
688 }
689
690 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
691 break;
692 #endif
567 693
568 default: 694 default:
569 s->imap_state = ngx_pop3_start; 695 s->imap_state = ngx_pop3_start;
570 rc = NGX_IMAP_PARSE_INVALID_COMMAND; 696 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
571 break; 697 break;
614 740
615 break; 741 break;
616 742
617 case NGX_POP3_CAPA: 743 case NGX_POP3_CAPA:
618 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); 744 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
619 text = cscf->pop3_capability->pos; 745 size = cscf->pop3_capability.len;
620 size = cscf->pop3_capability->last - cscf->pop3_capability->pos; 746 text = cscf->pop3_capability.data;
621 break; 747 break;
622 748
623 case NGX_POP3_QUIT: 749 case NGX_POP3_QUIT:
624 s->quit = 1; 750 s->quit = 1;
625 break; 751 break;
733 } 859 }
734 } 860 }
735 861
736 #endif 862 #endif
737 863
738 c->closed = 1; 864 c->destroyed = 1;
739 865
740 pool = c->pool; 866 pool = c->pool;
741 867
742 ngx_close_connection(c); 868 ngx_close_connection(c);
743 869