comparison src/imap/ngx_imap_handler.c @ 641:5e8fb59c18c1 release-0.3.42

nginx-0.3.42-RELEASE import *) Feature: the "bind" option of the "listen" directive in IMAP/POP3 proxy. *) Bugfix: if the same capture in the "rewrite" directive was used more then once. *) Bugfix: the $sent_http_content_type, $sent_http_content_length, $sent_http_last_modified, $sent_http_connection, $sent_http_keep_alive, and $sent_http_transfer_encoding variables were not written to access log. *) Bugfix: the $sent_http_cache_control returned value of the single "Cache-Control" response header line.
author Igor Sysoev <igor@sysoev.ru>
date Wed, 26 Apr 2006 09:52:47 +0000
parents 4e296b7d25bf
children 887d8dec72dc
comparison
equal deleted inserted replaced
640:5fd31c2fe4a8 641:5e8fb59c18c1
42 42
43 43
44 void 44 void
45 ngx_imap_init_connection(ngx_connection_t *c) 45 ngx_imap_init_connection(ngx_connection_t *c)
46 { 46 {
47 ngx_imap_log_ctx_t *lctx; 47 in_addr_t in_addr;
48 #if (NGX_IMAP_SSL) 48 socklen_t len;
49 ngx_imap_conf_ctx_t *ctx; 49 ngx_uint_t i;
50 struct sockaddr_in sin;
51 ngx_imap_log_ctx_t *ctx;
52 ngx_imap_in_port_t *imip;
53 ngx_imap_in_addr_t *imia;
54 ngx_imap_session_t *s;
55 #if (NGX_IMAP_SSL)
50 ngx_imap_ssl_conf_t *sslcf; 56 ngx_imap_ssl_conf_t *sslcf;
51 #endif 57 #endif
52 58
59
60 /* find the server configuration for the address:port */
61
62 /* AF_INET only */
63
64 imip = c->listening->servers;
65 imia = imip->addrs;
66
67 i = 0;
68
69 if (imip->naddrs > 1) {
70
71 /*
72 * There are several addresses on this port and one of them
73 * is the "*:port" wildcard so getsockname() is needed to determine
74 * the server address.
75 *
76 * AcceptEx() already gave this address.
77 */
78
79 #if (NGX_WIN32)
80 if (c->local_sockaddr) {
81 in_addr =
82 ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
83
84 } else
85 #endif
86 {
87 len = sizeof(struct sockaddr_in);
88 if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
89 ngx_connection_error(c, ngx_socket_errno,
90 "getsockname() failed");
91 ngx_imap_close_connection(c);
92 return;
93 }
94
95 in_addr = sin.sin_addr.s_addr;
96 }
97
98 /* the last address is "*" */
99
100 for ( /* void */ ; i < imip->naddrs - 1; i++) {
101 if (in_addr == imia[i].addr) {
102 break;
103 }
104 }
105 }
106
107
108 s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t));
109 if (s == NULL) {
110 ngx_imap_close_connection(c);
111 return;
112 }
113
114 s->main_conf = imia[i].ctx->main_conf;
115 s->srv_conf = imia[i].ctx->srv_conf;
116
117 s->addr_text = &imia[i].addr_text;
118
119 c->data = s;
120 s->connection = c;
121
53 ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client %V connected to %V", 122 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); 123 c->number, &c->addr_text, s->addr_text);
55 124
56 lctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t)); 125 ctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t));
57 if (lctx == NULL) { 126 if (ctx == NULL) {
58 ngx_imap_close_connection(c); 127 ngx_imap_close_connection(c);
59 return; 128 return;
60 } 129 }
61 130
62 lctx->client = &c->addr_text; 131 ctx->client = &c->addr_text;
63 lctx->session = NULL; 132 ctx->session = s;
64 133
65 c->log->connection = c->number; 134 c->log->connection = c->number;
66 c->log->handler = ngx_imap_log_error; 135 c->log->handler = ngx_imap_log_error;
67 c->log->data = lctx; 136 c->log->data = ctx;
68 c->log->action = "sending client greeting line"; 137 c->log->action = "sending client greeting line";
69 138
70 c->log_error = NGX_ERROR_INFO; 139 c->log_error = NGX_ERROR_INFO;
71 140
72 #if (NGX_IMAP_SSL) 141 #if (NGX_IMAP_SSL)
73 142
74 ctx = c->ctx; 143 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
75 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module);
76 144
77 if (sslcf->enable) { 145 if (sslcf->enable) {
78 ngx_imap_ssl_init_connection(&sslcf->ssl, c); 146 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
79 return; 147 return;
80 } 148 }
94 ngx_imap_session_t *s; 162 ngx_imap_session_t *s;
95 ngx_imap_ssl_conf_t *sslcf; 163 ngx_imap_ssl_conf_t *sslcf;
96 164
97 c = rev->data; 165 c = rev->data;
98 s = c->data; 166 s = c->data;
167 s->starttls = 1;
99 168
100 c->log->action = "in starttls state"; 169 c->log->action = "in starttls state";
101 170
102 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module); 171 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
103 172
106 175
107 176
108 static void 177 static void
109 ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c) 178 ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
110 { 179 {
111 ngx_imap_conf_ctx_t *ctx; 180 ngx_imap_session_t *s;
112 ngx_imap_core_srv_conf_t *cscf; 181 ngx_imap_core_srv_conf_t *cscf;
113 182
114 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) { 183 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
115 ngx_imap_close_connection(c); 184 ngx_imap_close_connection(c);
116 return; 185 return;
117 } 186 }
118 187
119 if (ngx_ssl_handshake(c) == NGX_AGAIN) { 188 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
120 189
121 ctx = c->ctx; 190 s = c->data;
122 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module); 191
192 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
123 193
124 ngx_add_timer(c->read, cscf->timeout); 194 ngx_add_timer(c->read, cscf->timeout);
125 195
126 c->ssl->handler = ngx_imap_ssl_handshake_handler; 196 c->ssl->handler = ngx_imap_ssl_handshake_handler;
127 197
133 203
134 204
135 static void 205 static void
136 ngx_imap_ssl_handshake_handler(ngx_connection_t *c) 206 ngx_imap_ssl_handshake_handler(ngx_connection_t *c)
137 { 207 {
208 ngx_imap_session_t *s;
209
138 if (c->ssl->handshaked) { 210 if (c->ssl->handshaked) {
139 211
140 if (c->data) { 212 s = c->data;
213
214 if (s->starttls) {
141 c->read->handler = ngx_imap_init_protocol; 215 c->read->handler = ngx_imap_init_protocol;
142 c->write->handler = ngx_imap_send; 216 c->write->handler = ngx_imap_send;
143 217
144 ngx_imap_init_protocol(c->read); 218 ngx_imap_init_protocol(c->read);
145 219
158 232
159 static void 233 static void
160 ngx_imap_init_session(ngx_connection_t *c) 234 ngx_imap_init_session(ngx_connection_t *c)
161 { 235 {
162 ngx_imap_session_t *s; 236 ngx_imap_session_t *s;
163 ngx_imap_log_ctx_t *lctx;
164 ngx_imap_conf_ctx_t *ctx;
165 ngx_imap_core_srv_conf_t *cscf; 237 ngx_imap_core_srv_conf_t *cscf;
166 238
167 c->read->handler = ngx_imap_init_protocol; 239 c->read->handler = ngx_imap_init_protocol;
168 c->write->handler = ngx_imap_send; 240 c->write->handler = ngx_imap_send;
169 241
170 s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)); 242 s = c->data;
171 if (s == NULL) { 243
172 ngx_imap_close_connection(c); 244 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
173 return;
174 }
175
176 ctx = c->ctx;
177 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module);
178
179 c->data = s;
180 s->connection = c;
181 245
182 s->protocol = cscf->protocol; 246 s->protocol = cscf->protocol;
183 247
184 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module); 248 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module);
185 if (s->ctx == NULL) { 249 if (s->ctx == NULL) {
186 ngx_imap_session_internal_server_error(s); 250 ngx_imap_session_internal_server_error(s);
187 return; 251 return;
188 } 252 }
189 253
190 s->main_conf = ctx->main_conf;
191 s->srv_conf = ctx->srv_conf;
192
193 s->out = greetings[s->protocol]; 254 s->out = greetings[s->protocol];
194
195 lctx = c->log->data;
196 lctx->session = s;
197 255
198 ngx_add_timer(c->read, cscf->timeout); 256 ngx_add_timer(c->read, cscf->timeout);
199 257
200 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { 258 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
201 ngx_imap_close_connection(c); 259 ngx_imap_close_connection(c);
894 952
895 if (s == NULL) { 953 if (s == NULL) {
896 return p; 954 return p;
897 } 955 }
898 956
899 p = ngx_snprintf(buf, len, ", server: %V", 957 p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
900 &s->connection->listening->addr_text);
901 len -= p - buf; 958 len -= p - buf;
902 buf = p; 959 buf = p;
903 960
904 if (s->login.len == 0) { 961 if (s->login.len == 0) {
905 return p; 962 return p;