comparison src/imap/ngx_imap_handler.c @ 96:ca4f70b3ccc6 NGINX_0_2_2

nginx 0.2.2 *) Feature: the "config errmsg" command of the ngx_http_ssi_module. *) Change: the ngx_http_geo_module variables can be overridden by the "set" directive. *) Feature: the "ssl_protocols" and "ssl_prefer_server_ciphers" directives of the ngx_http_ssl_module and ngx_imap_ssl_module. *) Bugfix: the ngx_http_autoindex_module did not show correctly the long file names; *) Bugfix: the ngx_http_autoindex_module now do not show the files starting by dot. *) Bugfix: if the SSL handshake failed then another connection may be closed too. Thanks to Rob Mueller. *) Bugfix: the export versions of MSIE 5.x could not connect via HTTPS.
author Igor Sysoev <http://sysoev.ru>
date Fri, 30 Sep 2005 00:00:00 +0400
parents 45945fa8b8ba
children e38f51cd0905
comparison
equal deleted inserted replaced
95:2f95911bc4b4 96:ca4f70b3ccc6
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_event.h> 9 #include <ngx_event.h>
10 #include <ngx_imap.h> 10 #include <ngx_imap.h>
11 11
12 12
13 static void ngx_imap_init_session(ngx_event_t *rev); 13 static void ngx_imap_init_session(ngx_connection_t *c);
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_handshake_handler(ngx_connection_t *c);
19 static void ngx_imap_ssl_close_handler(ngx_event_t *ev); 20 static void ngx_imap_ssl_close_handler(ngx_event_t *ev);
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;
51 ngx_imap_core_srv_conf_t *cscf;
50 #endif 52 #endif
51 53
52 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap init connection"); 54 ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client %V connected to %V",
55 c->number, &c->addr_text, &c->listening->addr_text);
53 56
54 lctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t)); 57 lctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t));
55 if (lctx == NULL) { 58 if (lctx == NULL) {
56 ngx_imap_close_connection(c); 59 ngx_imap_close_connection(c);
57 return; 60 return;
71 74
72 ctx = c->ctx; 75 ctx = c->ctx;
73 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module); 76 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module);
74 77
75 if (sslcf->enable) { 78 if (sslcf->enable) {
76 if (ngx_ssl_create_connection(sslcf->ssl_ctx, c, 0) == NGX_ERROR) { 79 if (ngx_ssl_create_connection(&sslcf->ssl, c, 0) == NGX_ERROR) {
77 ngx_imap_close_connection(c); 80 ngx_imap_close_connection(c);
78 return; 81 return;
79 } 82 }
80 83
81 c->recv = ngx_ssl_recv; 84 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
82 c->send = ngx_ssl_write; 85
83 c->send_chain = ngx_ssl_send_chain; 86 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module);
87
88 ngx_add_timer(c->read, cscf->timeout);
89
90 c->ssl->handler = ngx_imap_ssl_handshake_handler;
91
92 return;
93 }
94
95 ngx_imap_ssl_handshake_handler(c);
96 return;
84 } 97 }
85 98
86 #endif 99 #endif
87 100
88 ngx_imap_init_session(c->read); 101 ngx_imap_init_session(c);
89 } 102 }
90 103
104
105 #if (NGX_IMAP_SSL)
91 106
92 static void 107 static void
93 ngx_imap_init_session(ngx_event_t *rev) 108 ngx_imap_ssl_handshake_handler(ngx_connection_t *c)
94 { 109 {
95 ngx_connection_t *c; 110 if (c->ssl->handshaked) {
111 ngx_imap_init_session(c);
112 return;
113 }
114
115 ngx_imap_close_connection(c);
116 return;
117 }
118
119 #endif
120
121
122 static void
123 ngx_imap_init_session(ngx_connection_t *c)
124 {
96 ngx_imap_session_t *s; 125 ngx_imap_session_t *s;
97 ngx_imap_log_ctx_t *lctx; 126 ngx_imap_log_ctx_t *lctx;
98 ngx_imap_conf_ctx_t *ctx; 127 ngx_imap_conf_ctx_t *ctx;
99 ngx_imap_core_srv_conf_t *cscf; 128 ngx_imap_core_srv_conf_t *cscf;
100 #if (NGX_IMAP_SSL) 129
101 ngx_int_t rc; 130 c->read->handler = ngx_imap_init_protocol;
102 #endif 131 c->write->handler = ngx_imap_send;
103
104 c = rev->data;
105 ctx = c->ctx;
106 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module);
107
108 #if (NGX_IMAP_SSL)
109
110 if (c->ssl) {
111
112 rc = ngx_ssl_handshake(c);
113
114 if (rc == NGX_ERROR) {
115 ngx_imap_close_connection(c);
116 return;
117 }
118
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
130 }
131
132 #endif
133 132
134 s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)); 133 s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t));
135 if (s == NULL) { 134 if (s == NULL) {
136 ngx_imap_close_connection(c); 135 ngx_imap_close_connection(c);
137 return; 136 return;
138 } 137 }
139 138
139 ctx = c->ctx;
140 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module);
141
140 c->data = s; 142 c->data = s;
141 s->connection = c; 143 s->connection = c;
142 144
143 s->protocol = cscf->protocol; 145 s->protocol = cscf->protocol;
144 146
154 s->out = greetings[s->protocol]; 156 s->out = greetings[s->protocol];
155 157
156 lctx = c->log->data; 158 lctx = c->log->data;
157 lctx->session = s; 159 lctx->session = s;
158 160
159 c->read->handler = ngx_imap_init_protocol; 161 ngx_add_timer(c->read, cscf->timeout);
160 c->write->handler = ngx_imap_send; 162
161 163 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
162 ngx_add_timer(rev, cscf->timeout);
163
164 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
165 ngx_imap_close_connection(c); 164 ngx_imap_close_connection(c);
166 } 165 }
167 166
168 ngx_imap_send(c->write); 167 ngx_imap_send(c->write);
169 } 168 }
376 return; 375 return;
377 } 376 }
378 377
379 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len); 378 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
380 379
380 #if (NGX_DEBUG_IMAP_PASSWD)
381 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0, 381 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
382 "imap login:\"%V\" passwd:\"%V\"", 382 "imap login:\"%V\" passwd:\"%V\"",
383 &s->login, &s->passwd); 383 &s->login, &s->passwd);
384 #else
385 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
386 "imap login:\"%V\"", &s->login);
387 #endif
384 388
385 s->args.nelts = 0; 389 s->args.nelts = 0;
386 s->buffer->pos = s->buffer->start; 390 s->buffer->pos = s->buffer->start;
387 s->buffer->last = s->buffer->start; 391 s->buffer->last = s->buffer->start;
388 392
582 return; 586 return;
583 } 587 }
584 588
585 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len); 589 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
586 590
591 #if (NGX_DEBUG_IMAP_PASSWD)
587 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, 592 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
588 "pop3 passwd: \"%V\"", &s->passwd); 593 "pop3 passwd: \"%V\"", &s->passwd);
594 #endif
589 595
590 s->args.nelts = 0; 596 s->args.nelts = 0;
591 s->buffer->pos = s->buffer->start; 597 s->buffer->pos = s->buffer->start;
592 s->buffer->last = s->buffer->start; 598 s->buffer->last = s->buffer->start;
593 599