Mercurial > hg > nginx-quic
annotate src/imap/ngx_imap_handler.c @ 856:0197d6aae54e
use correct auth method length
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 17 Nov 2006 09:30:46 +0000 |
parents | b006f30d6a2f |
children | 4d68c486fcb0 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
423
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
423
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
423
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
423
diff
changeset
|
5 |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_event.h> |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
10 #include <ngx_imap.h> |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
11 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
12 |
547 | 13 static void ngx_imap_init_session(ngx_connection_t *c); |
541 | 14 static void ngx_imap_init_protocol(ngx_event_t *rev); |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
15 static void ngx_imap_do_auth(ngx_imap_session_t *s); |
527 | 16 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s); |
541 | 17 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len); |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
18 |
539 | 19 #if (NGX_IMAP_SSL) |
583 | 20 static void ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c); |
547 | 21 static void ngx_imap_ssl_handshake_handler(ngx_connection_t *c); |
539 | 22 #endif |
23 | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
24 |
521 | 25 static ngx_str_t greetings[] = { |
527 | 26 ngx_string("+OK POP3 ready" CRLF), |
529 | 27 ngx_string("* OK IMAP4 ready" CRLF) |
521 | 28 }; |
29 | |
525 | 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 | |
521 | 35 static u_char pop3_ok[] = "+OK" CRLF; |
809 | 36 static u_char pop3_next[] = "+ " CRLF; |
37 static u_char pop3_username[] = "+ VXNlcm5hbWU6" CRLF; | |
38 static u_char pop3_password[] = "+ UGFzc3dvcmQ6" CRLF; | |
521 | 39 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF; |
40 | |
543 | 41 static u_char imap_star[] = "* "; |
529 | 42 static u_char imap_ok[] = "OK completed" CRLF; |
527 | 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 | |
521 | 47 |
48 void | |
49 ngx_imap_init_connection(ngx_connection_t *c) | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
50 { |
641 | 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; | |
543 | 59 #if (NGX_IMAP_SSL) |
583 | 60 ngx_imap_ssl_conf_t *sslcf; |
543 | 61 #endif |
541 | 62 |
641 | 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; | |
541 | 87 |
641 | 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) { | |
541 | 114 ngx_imap_close_connection(c); |
115 return; | |
577 | 116 } |
541 | 117 |
641 | 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; | |
541 | 137 |
138 c->log->connection = c->number; | |
139 c->log->handler = ngx_imap_log_error; | |
641 | 140 c->log->data = ctx; |
541 | 141 c->log->action = "sending client greeting line"; |
142 | |
143 c->log_error = NGX_ERROR_INFO; | |
144 | |
543 | 145 #if (NGX_IMAP_SSL) |
146 | |
641 | 147 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module); |
543 | 148 |
149 if (sslcf->enable) { | |
583 | 150 ngx_imap_ssl_init_connection(&sslcf->ssl, c); |
547 | 151 return; |
543 | 152 } |
153 | |
154 #endif | |
155 | |
547 | 156 ngx_imap_init_session(c); |
541 | 157 } |
158 | |
159 | |
547 | 160 #if (NGX_IMAP_SSL) |
161 | |
541 | 162 static void |
583 | 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; | |
641 | 171 s->starttls = 1; |
583 | 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 { | |
641 | 184 ngx_imap_session_t *s; |
583 | 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 | |
641 | 194 s = c->data; |
195 | |
196 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); | |
583 | 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 | |
547 | 210 ngx_imap_ssl_handshake_handler(ngx_connection_t *c) |
577 | 211 { |
641 | 212 ngx_imap_session_t *s; |
213 | |
547 | 214 if (c->ssl->handshaked) { |
583 | 215 |
641 | 216 s = c->data; |
217 | |
218 if (s->starttls) { | |
583 | 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 | |
547 | 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) | |
541 | 239 { |
800 | 240 u_char *p; |
539 | 241 ngx_imap_session_t *s; |
541 | 242 ngx_imap_core_srv_conf_t *cscf; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
243 |
547 | 244 c->read->handler = ngx_imap_init_protocol; |
245 c->write->handler = ngx_imap_send; | |
539 | 246 |
641 | 247 s = c->data; |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
248 |
641 | 249 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); |
539 | 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 | |
800 | 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 | |
547 | 290 ngx_add_timer(c->read, cscf->timeout); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
291 |
547 | 292 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
293 ngx_imap_close_connection(c); |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
294 } |
539 | 295 |
296 ngx_imap_send(c->write); | |
297 } | |
298 | |
299 | |
300 void | |
301 ngx_imap_send(ngx_event_t *wev) | |
302 { | |
541 | 303 ngx_int_t n; |
304 ngx_connection_t *c; | |
305 ngx_imap_session_t *s; | |
306 ngx_imap_core_srv_conf_t *cscf; | |
539 | 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"); | |
577 | 313 c->timedout = 1; |
539 | 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 | |
541 | 331 if (wev->timer_set) { |
332 ngx_del_timer(wev); | |
333 } | |
334 | |
539 | 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 | |
541 | 354 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); |
355 | |
356 ngx_add_timer(c->write, cscf->timeout); | |
357 | |
539 | 358 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { |
359 ngx_imap_close_connection(c); | |
360 return; | |
361 } | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
362 } |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
363 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
364 |
521 | 365 static void |
541 | 366 ngx_imap_init_protocol(ngx_event_t *rev) |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
367 { |
521 | 368 size_t size; |
369 ngx_connection_t *c; | |
370 ngx_imap_session_t *s; | |
371 ngx_imap_core_srv_conf_t *cscf; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
372 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
373 c = rev->data; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
374 |
541 | 375 c->log->action = "in auth state"; |
376 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
377 if (rev->timedout) { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
378 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
577 | 379 c->timedout = 1; |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
380 ngx_imap_close_connection(c); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
381 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
382 } |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
383 |
539 | 384 s = c->data; |
521 | 385 |
539 | 386 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) { |
521 | 387 size = 128; |
527 | 388 s->imap_state = ngx_pop3_start; |
521 | 389 c->read->handler = ngx_pop3_auth_state; |
390 | |
391 } else { | |
539 | 392 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); |
521 | 393 size = cscf->imap_client_buffer_size; |
527 | 394 s->imap_state = ngx_imap_start; |
521 | 395 c->read->handler = ngx_imap_auth_state; |
396 } | |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
397 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
398 if (s->buffer == NULL) { |
583 | 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 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
411 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
412 |
521 | 413 c->read->handler(rev); |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
414 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
415 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
416 |
527 | 417 void |
521 | 418 ngx_imap_auth_state(ngx_event_t *rev) |
419 { | |
543 | 420 u_char *text, *last, *p, *dst, *src, *end; |
539 | 421 ssize_t text_len, last_len; |
527 | 422 ngx_str_t *arg; |
423 ngx_int_t rc; | |
543 | 424 ngx_uint_t tag, i; |
527 | 425 ngx_connection_t *c; |
426 ngx_imap_session_t *s; | |
427 ngx_imap_core_srv_conf_t *cscf; | |
583 | 428 #if (NGX_IMAP_SSL) |
429 ngx_imap_ssl_conf_t *sslcf; | |
430 #endif | |
521 | 431 |
432 c = rev->data; | |
527 | 433 s = c->data; |
521 | 434 |
527 | 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"); | |
577 | 439 c->timedout = 1; |
527 | 440 ngx_imap_close_connection(c); |
441 return; | |
442 } | |
443 | |
539 | 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 | |
527 | 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 | |
543 | 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 | |
527 | 494 switch (s->command) { |
495 | |
496 case NGX_IMAP_LOGIN: | |
583 | 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 | |
569 | 510 arg = s->args.elts; |
527 | 511 |
569 | 512 if (s->args.nelts == 2 && arg[0].len) { |
527 | 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 | |
547 | 532 #if (NGX_DEBUG_IMAP_PASSWD) |
527 | 533 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0, |
534 "imap login:\"%V\" passwd:\"%V\"", | |
535 &s->login, &s->passwd); | |
547 | 536 #else |
537 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, | |
538 "imap login:\"%V\"", &s->login); | |
539 #endif | |
527 | 540 |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
541 ngx_imap_do_auth(s); |
527 | 542 return; |
543 } | |
544 | |
809 | 545 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
527 | 546 break; |
547 | |
548 case NGX_IMAP_CAPABILITY: | |
549 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); | |
583 | 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; | |
527 | 572 break; |
573 | |
574 case NGX_IMAP_LOGOUT: | |
539 | 575 s->quit = 1; |
527 | 576 text = imap_bye; |
577 text_len = sizeof(imap_bye) - 1; | |
578 break; | |
579 | |
580 case NGX_IMAP_NOOP: | |
581 break; | |
582 | |
583 | 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 | |
527 | 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) { | |
543 | 615 if (s->tag.len == 0) { |
616 s->tag.len = sizeof(imap_star) - 1; | |
617 s->tag.data = (u_char *) imap_star; | |
618 } | |
619 | |
539 | 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) { | |
527 | 624 ngx_imap_close_connection(c); |
625 return; | |
626 } | |
627 } | |
628 | |
539 | 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; | |
527 | 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 { | |
539 | 642 s->out.data = last; |
643 s->out.len = last_len; | |
527 | 644 } |
645 | |
539 | 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; | |
527 | 651 } |
652 | |
539 | 653 ngx_imap_send(c->write); |
521 | 654 } |
655 | |
656 | |
527 | 657 void |
521 | 658 ngx_pop3_auth_state(ngx_event_t *rev) |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
659 { |
809 | 660 u_char *text, *p, *last; |
527 | 661 ssize_t size; |
662 ngx_int_t rc; | |
809 | 663 ngx_str_t *arg, salt, plain; |
527 | 664 ngx_connection_t *c; |
665 ngx_imap_session_t *s; | |
666 ngx_imap_core_srv_conf_t *cscf; | |
583 | 667 #if (NGX_IMAP_SSL) |
668 ngx_imap_ssl_conf_t *sslcf; | |
669 #endif | |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
670 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
671 c = rev->data; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
672 s = c->data; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
673 |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
674 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state"); |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
675 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
676 if (rev->timedout) { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
677 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
577 | 678 c->timedout = 1; |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
679 ngx_imap_close_connection(c); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
680 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
681 } |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
682 |
539 | 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 | |
527 | 691 rc = ngx_imap_read_command(s); |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
692 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
693 if (rc == NGX_AGAIN || rc == NGX_ERROR) { |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
694 return; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
695 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
696 |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
697 text = pop3_ok; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
698 size = sizeof(pop3_ok) - 1; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
699 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
700 if (rc == NGX_OK) { |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
701 switch (s->imap_state) { |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
702 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
703 case ngx_pop3_start: |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
704 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
705 switch (s->command) { |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
706 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
707 case NGX_POP3_USER: |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
708 if (s->args.nelts == 1) { |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
709 s->imap_state = ngx_pop3_user; |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
710 |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
711 arg = s->args.elts; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
712 s->login.len = arg[0].len; |
527 | 713 s->login.data = ngx_palloc(c->pool, s->login.len); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
714 if (s->login.data == NULL) { |
527 | 715 ngx_imap_session_internal_server_error(s); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
716 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
717 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
718 |
527 | 719 ngx_memcpy(s->login.data, arg[0].data, s->login.len); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
720 |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
721 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, |
527 | 722 "pop3 login: \"%V\"", &s->login); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
723 |
809 | 724 break; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
725 } |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
726 |
809 | 727 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
728 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
729 |
527 | 730 case NGX_POP3_CAPA: |
731 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); | |
583 | 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; | |
527 | 748 break; |
749 | |
800 | 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 | |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
782 ngx_imap_do_auth(s); |
800 | 783 return; |
809 | 784 } |
800 | 785 |
809 | 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; | |
810 | 795 s->state = 0; |
809 | 796 break; |
797 } | |
798 | |
799 if (s->args.nelts != 1) { | |
800 | 800 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
809 | 801 break; |
800 | 802 } |
803 | |
809 | 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; | |
800 | 852 break; |
853 | |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
854 case NGX_POP3_QUIT: |
539 | 855 s->quit = 1; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
856 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
857 |
527 | 858 case NGX_POP3_NOOP: |
859 break; | |
860 | |
583 | 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 | |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
877 default: |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
878 s->imap_state = ngx_pop3_start; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
879 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
880 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
881 } |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
882 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
883 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
884 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
885 case ngx_pop3_user: |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
886 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
887 switch (s->command) { |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
888 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
889 case NGX_POP3_PASS: |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
890 if (s->args.nelts == 1) { |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
891 arg = s->args.elts; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
892 s->passwd.len = arg[0].len; |
527 | 893 s->passwd.data = ngx_palloc(c->pool, s->passwd.len); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
894 if (s->passwd.data == NULL) { |
527 | 895 ngx_imap_session_internal_server_error(s); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
896 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
897 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
898 |
527 | 899 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
900 |
547 | 901 #if (NGX_DEBUG_IMAP_PASSWD) |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
902 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, |
527 | 903 "pop3 passwd: \"%V\"", &s->passwd); |
547 | 904 #endif |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
905 |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
906 ngx_imap_do_auth(s); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
421
diff
changeset
|
907 return; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
908 } |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
909 |
809 | 910 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
911 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
912 |
527 | 913 case NGX_POP3_CAPA: |
914 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); | |
583 | 915 size = cscf->pop3_capability.len; |
916 text = cscf->pop3_capability.data; | |
527 | 917 break; |
918 | |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
919 case NGX_POP3_QUIT: |
539 | 920 s->quit = 1; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
921 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
922 |
527 | 923 case NGX_POP3_NOOP: |
924 break; | |
925 | |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
926 default: |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
927 s->imap_state = ngx_pop3_start; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
928 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
929 break; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
930 } |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
931 |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
932 break; |
527 | 933 |
934 /* suppress warinings */ | |
935 case ngx_pop3_passwd: | |
936 break; | |
809 | 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"); | |
810 | 956 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
957 break; | |
809 | 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"); | |
810 | 987 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
988 break; | |
809 | 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 | |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
996 ngx_imap_do_auth(s); |
809 | 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"); | |
810 | 1018 rc = NGX_IMAP_PARSE_INVALID_COMMAND; |
1019 break; | |
809 | 1020 } |
1021 | |
1022 p = plain.data; | |
1023 last = p + plain.len; | |
1024 | |
1025 while (p < last && *p++) { /* void */ } | |
1026 | |
810 | 1027 if (p == last) { |
1028 ngx_log_error(NGX_LOG_INFO, c->log, 0, | |
814 | 1029 "client sent invalid login " |
810 | 1030 "in AUTH PLAIN command"); |
1031 rc = NGX_IMAP_PARSE_INVALID_COMMAND; | |
1032 break; | |
1033 } | |
1034 | |
809 | 1035 s->login.data = p; |
1036 | |
1037 while (p < last && *p) { p++; } | |
1038 | |
810 | 1039 if (p == last) { |
1040 ngx_log_error(NGX_LOG_INFO, c->log, 0, | |
814 | 1041 "client sent invalid password " |
810 | 1042 "in AUTH PLAIN command"); |
1043 rc = NGX_IMAP_PARSE_INVALID_COMMAND; | |
1044 break; | |
1045 } | |
809 | 1046 |
810 | 1047 s->login.len = p++ - s->login.data; |
809 | 1048 |
810 | 1049 s->passwd.len = last - p; |
1050 s->passwd.data = p; | |
809 | 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 | |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1058 ngx_imap_do_auth(s); |
809 | 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 " | |
810 | 1077 "in AUTH CRAM-MD5 command"); |
1078 rc = NGX_IMAP_PARSE_INVALID_COMMAND; | |
1079 break; | |
809 | 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 | |
810 | 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 | |
809 | 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 | |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1108 ngx_imap_do_auth(s); |
809 | 1109 return; |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
1110 } |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
1111 } |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1112 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1113 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) { |
810 | 1114 s->imap_state = ngx_pop3_start; |
1115 s->state = 0; | |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1116 text = pop3_invalid_command; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1117 size = sizeof(pop3_invalid_command) - 1; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1118 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1119 |
421
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
1120 s->args.nelts = 0; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
1121 s->buffer->pos = s->buffer->start; |
01456a419cf9
nginx-0.0.10-2004-09-10-18:32:02 import
Igor Sysoev <igor@sysoev.ru>
parents:
420
diff
changeset
|
1122 s->buffer->last = s->buffer->start; |
539 | 1123 |
810 | 1124 if (s->state) { |
1125 s->arg_start = s->buffer->start; | |
1126 } | |
1127 | |
539 | 1128 s->out.data = text; |
1129 s->out.len = size; | |
1130 | |
1131 ngx_imap_send(c->write); | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1132 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1133 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1134 |
855
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1135 static void |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1136 ngx_imap_do_auth(ngx_imap_session_t *s) |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1137 { |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1138 s->args.nelts = 0; |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1139 s->buffer->pos = s->buffer->start; |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1140 s->buffer->last = s->buffer->start; |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1141 s->state = 0; |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1142 |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1143 if (s->connection->read->timer_set) { |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1144 ngx_del_timer(s->connection->read); |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1145 } |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1146 |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1147 s->login_attempt++; |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1148 |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1149 ngx_imap_auth_http_init(s); |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1150 } |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1151 |
b006f30d6a2f
count s->login_attempt for POP3, minimize code
Igor Sysoev <igor@sysoev.ru>
parents:
851
diff
changeset
|
1152 |
521 | 1153 static ngx_int_t |
527 | 1154 ngx_imap_read_command(ngx_imap_session_t *s) |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1155 { |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1156 ssize_t n; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1157 ngx_int_t rc; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1158 |
539 | 1159 n = s->connection->recv(s->connection, s->buffer->last, |
1160 s->buffer->end - s->buffer->last); | |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1161 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1162 if (n == NGX_ERROR || n == 0) { |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1163 ngx_imap_close_connection(s->connection); |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1164 return NGX_ERROR; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1165 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1166 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1167 if (n > 0) { |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1168 s->buffer->last += n; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1169 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1170 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1171 if (n == NGX_AGAIN) { |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1172 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) { |
527 | 1173 ngx_imap_session_internal_server_error(s); |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1174 return NGX_ERROR; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1175 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1176 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1177 return NGX_AGAIN; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1178 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1179 |
527 | 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 } | |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1185 |
527 | 1186 if (rc == NGX_AGAIN |
1187 || rc == NGX_IMAP_NEXT | |
1188 || rc == NGX_IMAP_PARSE_INVALID_COMMAND) | |
1189 { | |
420
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1190 return rc; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1191 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1192 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1193 if (rc == NGX_ERROR) { |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1194 ngx_imap_close_connection(s->connection); |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1195 return NGX_ERROR; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1196 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1197 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1198 return NGX_OK; |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1199 } |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1200 |
33a8253115b4
nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1201 |
521 | 1202 void |
525 | 1203 ngx_imap_session_internal_server_error(ngx_imap_session_t *s) |
1204 { | |
539 | 1205 s->out = internal_server_errors[s->protocol]; |
1206 s->quit = 1; | |
525 | 1207 |
539 | 1208 ngx_imap_send(s->connection->write); |
525 | 1209 } |
1210 | |
1211 | |
1212 void | |
521 | 1213 ngx_imap_close_connection(ngx_connection_t *c) |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1214 { |
479 | 1215 ngx_pool_t *pool; |
1216 | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1217 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1218 "close imap connection: %d", c->fd); |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1219 |
539 | 1220 #if (NGX_IMAP_SSL) |
1221 | |
1222 if (c->ssl) { | |
1223 if (ngx_ssl_shutdown(c) == NGX_AGAIN) { | |
577 | 1224 c->ssl->handler = ngx_imap_close_connection; |
539 | 1225 return; |
1226 } | |
1227 } | |
1228 | |
1229 #endif | |
1230 | |
583 | 1231 c->destroyed = 1; |
543 | 1232 |
479 | 1233 pool = c->pool; |
1234 | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1235 ngx_close_connection(c); |
479 | 1236 |
501 | 1237 ngx_destroy_pool(pool); |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
1238 } |
539 | 1239 |
1240 | |
541 | 1241 static u_char * |
1242 ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len) | |
1243 { | |
567 | 1244 u_char *p; |
1245 ngx_imap_session_t *s; | |
1246 ngx_imap_log_ctx_t *ctx; | |
541 | 1247 |
1248 if (log->action) { | |
1249 p = ngx_snprintf(buf, len, " while %s", log->action); | |
1250 len -= p - buf; | |
1251 buf = p; | |
1252 } | |
577 | 1253 |
541 | 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 | |
641 | 1266 p = ngx_snprintf(buf, len, ", server: %V", s->addr_text); |
541 | 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 } |