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);
|
76
|
15 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s);
|
90
|
16 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len);
|
76
|
17
|
88
|
18 #if (NGX_IMAP_SSL)
|
132
|
19 static void ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c);
|
96
|
20 static void ngx_imap_ssl_handshake_handler(ngx_connection_t *c);
|
88
|
21 #endif
|
|
22
|
76
|
23
|
|
24 static ngx_str_t greetings[] = {
|
|
25 ngx_string("+OK POP3 ready" CRLF),
|
78
|
26 ngx_string("* OK IMAP4 ready" CRLF)
|
76
|
27 };
|
|
28
|
|
29 static ngx_str_t internal_server_errors[] = {
|
|
30 ngx_string("-ERR internal server error" CRLF),
|
|
31 ngx_string("* BAD internal server error" CRLF),
|
|
32 };
|
|
33
|
|
34 static u_char pop3_ok[] = "+OK" CRLF;
|
|
35 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
|
|
36
|
92
|
37 static u_char imap_star[] = "* ";
|
78
|
38 static u_char imap_ok[] = "OK completed" CRLF;
|
76
|
39 static u_char imap_next[] = "+ OK" CRLF;
|
|
40 static u_char imap_bye[] = "* BYE" CRLF;
|
|
41 static u_char imap_invalid_command[] = "BAD invalid command" CRLF;
|
|
42
|
|
43
|
|
44 void
|
|
45 ngx_imap_init_connection(ngx_connection_t *c)
|
|
46 {
|
190
|
47 in_addr_t in_addr;
|
|
48 socklen_t len;
|
|
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;
|
92
|
55 #if (NGX_IMAP_SSL)
|
132
|
56 ngx_imap_ssl_conf_t *sslcf;
|
92
|
57 #endif
|
90
|
58
|
190
|
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;
|
90
|
83
|
190
|
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) {
|
90
|
110 ngx_imap_close_connection(c);
|
|
111 return;
|
126
|
112 }
|
90
|
113
|
190
|
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
|
|
122 ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client %V connected to %V",
|
|
123 c->number, &c->addr_text, s->addr_text);
|
|
124
|
|
125 ctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t));
|
|
126 if (ctx == NULL) {
|
|
127 ngx_imap_close_connection(c);
|
|
128 return;
|
|
129 }
|
|
130
|
|
131 ctx->client = &c->addr_text;
|
|
132 ctx->session = s;
|
90
|
133
|
|
134 c->log->connection = c->number;
|
|
135 c->log->handler = ngx_imap_log_error;
|
190
|
136 c->log->data = ctx;
|
90
|
137 c->log->action = "sending client greeting line";
|
|
138
|
|
139 c->log_error = NGX_ERROR_INFO;
|
|
140
|
92
|
141 #if (NGX_IMAP_SSL)
|
|
142
|
190
|
143 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
92
|
144
|
|
145 if (sslcf->enable) {
|
132
|
146 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
|
96
|
147 return;
|
92
|
148 }
|
|
149
|
|
150 #endif
|
|
151
|
96
|
152 ngx_imap_init_session(c);
|
90
|
153 }
|
|
154
|
|
155
|
96
|
156 #if (NGX_IMAP_SSL)
|
|
157
|
90
|
158 static void
|
132
|
159 ngx_imap_starttls_handler(ngx_event_t *rev)
|
|
160 {
|
|
161 ngx_connection_t *c;
|
|
162 ngx_imap_session_t *s;
|
|
163 ngx_imap_ssl_conf_t *sslcf;
|
|
164
|
|
165 c = rev->data;
|
|
166 s = c->data;
|
190
|
167 s->starttls = 1;
|
132
|
168
|
|
169 c->log->action = "in starttls state";
|
|
170
|
|
171 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
172
|
|
173 ngx_imap_ssl_init_connection(&sslcf->ssl, c);
|
|
174 }
|
|
175
|
|
176
|
|
177 static void
|
|
178 ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
|
|
179 {
|
190
|
180 ngx_imap_session_t *s;
|
132
|
181 ngx_imap_core_srv_conf_t *cscf;
|
|
182
|
|
183 if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
|
|
184 ngx_imap_close_connection(c);
|
|
185 return;
|
|
186 }
|
|
187
|
|
188 if (ngx_ssl_handshake(c) == NGX_AGAIN) {
|
|
189
|
190
|
190 s = c->data;
|
|
191
|
|
192 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
193
|
|
194 ngx_add_timer(c->read, cscf->timeout);
|
|
195
|
|
196 c->ssl->handler = ngx_imap_ssl_handshake_handler;
|
|
197
|
|
198 return;
|
|
199 }
|
|
200
|
|
201 ngx_imap_ssl_handshake_handler(c);
|
|
202 }
|
|
203
|
|
204
|
|
205 static void
|
96
|
206 ngx_imap_ssl_handshake_handler(ngx_connection_t *c)
|
126
|
207 {
|
190
|
208 ngx_imap_session_t *s;
|
|
209
|
96
|
210 if (c->ssl->handshaked) {
|
132
|
211
|
190
|
212 s = c->data;
|
|
213
|
|
214 if (s->starttls) {
|
132
|
215 c->read->handler = ngx_imap_init_protocol;
|
|
216 c->write->handler = ngx_imap_send;
|
|
217
|
|
218 ngx_imap_init_protocol(c->read);
|
|
219
|
|
220 return;
|
|
221 }
|
|
222
|
96
|
223 ngx_imap_init_session(c);
|
|
224 return;
|
|
225 }
|
|
226
|
|
227 ngx_imap_close_connection(c);
|
|
228 }
|
|
229
|
|
230 #endif
|
|
231
|
|
232
|
|
233 static void
|
|
234 ngx_imap_init_session(ngx_connection_t *c)
|
90
|
235 {
|
88
|
236 ngx_imap_session_t *s;
|
90
|
237 ngx_imap_core_srv_conf_t *cscf;
|
76
|
238
|
96
|
239 c->read->handler = ngx_imap_init_protocol;
|
|
240 c->write->handler = ngx_imap_send;
|
88
|
241
|
190
|
242 s = c->data;
|
76
|
243
|
190
|
244 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
88
|
245
|
|
246 s->protocol = cscf->protocol;
|
|
247
|
|
248 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module);
|
|
249 if (s->ctx == NULL) {
|
|
250 ngx_imap_session_internal_server_error(s);
|
|
251 return;
|
|
252 }
|
|
253
|
|
254 s->out = greetings[s->protocol];
|
|
255
|
96
|
256 ngx_add_timer(c->read, cscf->timeout);
|
76
|
257
|
96
|
258 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
76
|
259 ngx_imap_close_connection(c);
|
|
260 }
|
88
|
261
|
|
262 ngx_imap_send(c->write);
|
|
263 }
|
|
264
|
|
265
|
|
266 void
|
|
267 ngx_imap_send(ngx_event_t *wev)
|
|
268 {
|
90
|
269 ngx_int_t n;
|
|
270 ngx_connection_t *c;
|
|
271 ngx_imap_session_t *s;
|
|
272 ngx_imap_core_srv_conf_t *cscf;
|
88
|
273
|
|
274 c = wev->data;
|
|
275 s = c->data;
|
|
276
|
|
277 if (wev->timedout) {
|
|
278 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
279 c->timedout = 1;
|
88
|
280 ngx_imap_close_connection(c);
|
|
281 return;
|
|
282 }
|
|
283
|
|
284 if (s->out.len == 0) {
|
|
285 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
286 ngx_imap_close_connection(c);
|
|
287 }
|
|
288
|
|
289 return;
|
|
290 }
|
|
291
|
|
292 n = c->send(c, s->out.data, s->out.len);
|
|
293
|
|
294 if (n > 0) {
|
|
295 s->out.len -= n;
|
|
296
|
90
|
297 if (wev->timer_set) {
|
|
298 ngx_del_timer(wev);
|
|
299 }
|
|
300
|
88
|
301 if (s->quit) {
|
|
302 ngx_imap_close_connection(c);
|
|
303 return;
|
|
304 }
|
|
305
|
|
306 if (s->blocked) {
|
|
307 c->read->handler(c->read);
|
|
308 }
|
|
309
|
|
310 return;
|
|
311 }
|
|
312
|
|
313 if (n == NGX_ERROR) {
|
|
314 ngx_imap_close_connection(c);
|
|
315 return;
|
|
316 }
|
|
317
|
|
318 /* n == NGX_AGAIN */
|
|
319
|
90
|
320 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
321
|
|
322 ngx_add_timer(c->write, cscf->timeout);
|
|
323
|
88
|
324 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
325 ngx_imap_close_connection(c);
|
|
326 return;
|
|
327 }
|
76
|
328 }
|
|
329
|
|
330
|
|
331 static void
|
90
|
332 ngx_imap_init_protocol(ngx_event_t *rev)
|
76
|
333 {
|
|
334 size_t size;
|
|
335 ngx_connection_t *c;
|
|
336 ngx_imap_session_t *s;
|
|
337 ngx_imap_core_srv_conf_t *cscf;
|
|
338
|
|
339 c = rev->data;
|
|
340
|
90
|
341 c->log->action = "in auth state";
|
|
342
|
76
|
343 if (rev->timedout) {
|
|
344 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
345 c->timedout = 1;
|
76
|
346 ngx_imap_close_connection(c);
|
|
347 return;
|
|
348 }
|
|
349
|
88
|
350 s = c->data;
|
76
|
351
|
88
|
352 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
76
|
353 size = 128;
|
|
354 s->imap_state = ngx_pop3_start;
|
|
355 c->read->handler = ngx_pop3_auth_state;
|
|
356
|
|
357 } else {
|
88
|
358 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
76
|
359 size = cscf->imap_client_buffer_size;
|
|
360 s->imap_state = ngx_imap_start;
|
|
361 c->read->handler = ngx_imap_auth_state;
|
|
362 }
|
|
363
|
|
364 if (s->buffer == NULL) {
|
132
|
365 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
|
|
366 == NGX_ERROR)
|
|
367 {
|
|
368 ngx_imap_session_internal_server_error(s);
|
|
369 return;
|
|
370 }
|
|
371
|
|
372 s->buffer = ngx_create_temp_buf(c->pool, size);
|
|
373 if (s->buffer == NULL) {
|
|
374 ngx_imap_session_internal_server_error(s);
|
|
375 return;
|
|
376 }
|
76
|
377 }
|
|
378
|
|
379 c->read->handler(rev);
|
|
380 }
|
|
381
|
|
382
|
|
383 void
|
|
384 ngx_imap_auth_state(ngx_event_t *rev)
|
|
385 {
|
92
|
386 u_char *text, *last, *p, *dst, *src, *end;
|
88
|
387 ssize_t text_len, last_len;
|
76
|
388 ngx_str_t *arg;
|
|
389 ngx_int_t rc;
|
92
|
390 ngx_uint_t tag, i;
|
76
|
391 ngx_connection_t *c;
|
|
392 ngx_imap_session_t *s;
|
|
393 ngx_imap_core_srv_conf_t *cscf;
|
132
|
394 #if (NGX_IMAP_SSL)
|
|
395 ngx_imap_ssl_conf_t *sslcf;
|
|
396 #endif
|
76
|
397
|
|
398 c = rev->data;
|
|
399 s = c->data;
|
|
400
|
|
401 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state");
|
|
402
|
|
403 if (rev->timedout) {
|
|
404 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
405 c->timedout = 1;
|
76
|
406 ngx_imap_close_connection(c);
|
|
407 return;
|
|
408 }
|
|
409
|
88
|
410 if (s->out.len) {
|
|
411 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
412 s->blocked = 1;
|
|
413 return;
|
|
414 }
|
|
415
|
|
416 s->blocked = 0;
|
|
417
|
76
|
418 rc = ngx_imap_read_command(s);
|
|
419
|
|
420 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth: %i", rc);
|
|
421
|
|
422 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
423 return;
|
|
424 }
|
|
425
|
|
426 tag = 1;
|
|
427
|
|
428 text = NULL;
|
|
429 text_len = 0;
|
|
430
|
|
431 last = imap_ok;
|
|
432 last_len = sizeof(imap_ok) - 1;
|
|
433
|
|
434 if (rc == NGX_OK) {
|
|
435
|
|
436 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth command: %i",
|
|
437 s->command);
|
|
438
|
92
|
439 if (s->backslash) {
|
|
440
|
|
441 arg = s->args.elts;
|
|
442
|
|
443 for (i = 0; i < s->args.nelts; i++) {
|
|
444 dst = arg[i].data;
|
|
445 end = dst + arg[i].len;
|
|
446
|
|
447 for (src = dst; src < end; dst++) {
|
|
448 *dst = *src;
|
|
449 if (*src++ == '\\') {
|
|
450 *dst = *src++;
|
|
451 }
|
|
452 }
|
|
453
|
|
454 arg[i].len = dst - arg[i].data;
|
|
455 }
|
|
456
|
|
457 s->backslash = 0;
|
|
458 }
|
|
459
|
76
|
460 switch (s->command) {
|
|
461
|
|
462 case NGX_IMAP_LOGIN:
|
132
|
463
|
|
464 #if (NGX_IMAP_SSL)
|
|
465
|
|
466 if (c->ssl == NULL) {
|
|
467 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
468
|
|
469 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
|
|
470 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
471 break;
|
|
472 }
|
|
473 }
|
|
474 #endif
|
|
475
|
118
|
476 arg = s->args.elts;
|
76
|
477
|
118
|
478 if (s->args.nelts == 2 && arg[0].len) {
|
76
|
479
|
|
480 s->login.len = arg[0].len;
|
|
481 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
482 if (s->login.data == NULL) {
|
|
483 ngx_imap_session_internal_server_error(s);
|
|
484 return;
|
|
485 }
|
|
486
|
|
487 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
488
|
|
489 s->passwd.len = arg[1].len;
|
|
490 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
491 if (s->passwd.data == NULL) {
|
|
492 ngx_imap_session_internal_server_error(s);
|
|
493 return;
|
|
494 }
|
|
495
|
|
496 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
497
|
96
|
498 #if (NGX_DEBUG_IMAP_PASSWD)
|
76
|
499 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
500 "imap login:\"%V\" passwd:\"%V\"",
|
|
501 &s->login, &s->passwd);
|
96
|
502 #else
|
|
503 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
504 "imap login:\"%V\"", &s->login);
|
|
505 #endif
|
76
|
506
|
|
507 s->args.nelts = 0;
|
|
508 s->buffer->pos = s->buffer->start;
|
|
509 s->buffer->last = s->buffer->start;
|
|
510
|
|
511 if (rev->timer_set) {
|
|
512 ngx_del_timer(rev);
|
|
513 }
|
|
514
|
|
515 s->login_attempt++;
|
|
516
|
|
517 ngx_imap_auth_http_init(s);
|
|
518
|
|
519 return;
|
|
520
|
|
521 } else {
|
|
522 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
523 }
|
|
524
|
|
525 break;
|
|
526
|
|
527 case NGX_IMAP_CAPABILITY:
|
|
528 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
529
|
|
530 #if (NGX_IMAP_SSL)
|
|
531
|
|
532 if (c->ssl == NULL) {
|
|
533 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
534
|
|
535 if (sslcf->starttls == NGX_IMAP_STARTTLS_ON) {
|
|
536 text_len = cscf->imap_starttls_capability.len;
|
|
537 text = cscf->imap_starttls_capability.data;
|
|
538 break;
|
|
539 }
|
|
540
|
|
541 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
|
|
542 text_len = cscf->imap_starttls_only_capability.len;
|
|
543 text = cscf->imap_starttls_only_capability.data;
|
|
544 break;
|
|
545 }
|
|
546 }
|
|
547 #endif
|
|
548
|
|
549 text_len = cscf->imap_capability.len;
|
|
550 text = cscf->imap_capability.data;
|
76
|
551 break;
|
|
552
|
|
553 case NGX_IMAP_LOGOUT:
|
88
|
554 s->quit = 1;
|
76
|
555 text = imap_bye;
|
|
556 text_len = sizeof(imap_bye) - 1;
|
|
557 break;
|
|
558
|
|
559 case NGX_IMAP_NOOP:
|
|
560 break;
|
|
561
|
132
|
562 #if (NGX_IMAP_SSL)
|
|
563
|
|
564 case NGX_IMAP_STARTTLS:
|
|
565 if (c->ssl == NULL) {
|
|
566 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
567 if (sslcf->starttls) {
|
|
568 c->read->handler = ngx_imap_starttls_handler;
|
|
569 break;
|
|
570 }
|
|
571 }
|
|
572
|
|
573 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
574 break;
|
|
575 #endif
|
|
576
|
76
|
577 default:
|
|
578 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
579 break;
|
|
580 }
|
|
581
|
|
582 } else if (rc == NGX_IMAP_NEXT) {
|
|
583 last = imap_next;
|
|
584 last_len = sizeof(imap_next) - 1;
|
|
585 tag = 0;
|
|
586 }
|
|
587
|
|
588 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
589 last = imap_invalid_command;
|
|
590 last_len = sizeof(imap_invalid_command) - 1;
|
|
591 }
|
|
592
|
|
593 if (tag) {
|
92
|
594 if (s->tag.len == 0) {
|
|
595 s->tag.len = sizeof(imap_star) - 1;
|
|
596 s->tag.data = (u_char *) imap_star;
|
|
597 }
|
|
598
|
88
|
599 if (s->tagged_line.len < s->tag.len + text_len + last_len) {
|
|
600 s->tagged_line.len = s->tag.len + text_len + last_len;
|
|
601 s->tagged_line.data = ngx_palloc(c->pool, s->tagged_line.len);
|
|
602 if (s->tagged_line.data == NULL) {
|
76
|
603 ngx_imap_close_connection(c);
|
|
604 return;
|
|
605 }
|
|
606 }
|
|
607
|
88
|
608 s->out.data = s->tagged_line.data;
|
|
609 s->out.len = s->tag.len + text_len + last_len;
|
|
610
|
|
611 p = s->out.data;
|
76
|
612
|
|
613 if (text) {
|
|
614 p = ngx_cpymem(p, text, text_len);
|
|
615 }
|
|
616 p = ngx_cpymem(p, s->tag.data, s->tag.len);
|
|
617 ngx_memcpy(p, last, last_len);
|
|
618
|
|
619
|
|
620 } else {
|
88
|
621 s->out.data = last;
|
|
622 s->out.len = last_len;
|
76
|
623 }
|
|
624
|
88
|
625 if (rc != NGX_IMAP_NEXT) {
|
|
626 s->args.nelts = 0;
|
|
627 s->buffer->pos = s->buffer->start;
|
|
628 s->buffer->last = s->buffer->start;
|
|
629 s->tag.len = 0;
|
76
|
630 }
|
|
631
|
88
|
632 ngx_imap_send(c->write);
|
76
|
633 }
|
|
634
|
|
635
|
|
636 void
|
|
637 ngx_pop3_auth_state(ngx_event_t *rev)
|
|
638 {
|
|
639 u_char *text;
|
|
640 ssize_t size;
|
|
641 ngx_int_t rc;
|
|
642 ngx_str_t *arg;
|
|
643 ngx_connection_t *c;
|
|
644 ngx_imap_session_t *s;
|
|
645 ngx_imap_core_srv_conf_t *cscf;
|
132
|
646 #if (NGX_IMAP_SSL)
|
|
647 ngx_imap_ssl_conf_t *sslcf;
|
|
648 #endif
|
76
|
649
|
|
650 c = rev->data;
|
|
651 s = c->data;
|
|
652
|
|
653 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state");
|
|
654
|
|
655 if (rev->timedout) {
|
|
656 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
657 c->timedout = 1;
|
76
|
658 ngx_imap_close_connection(c);
|
|
659 return;
|
|
660 }
|
|
661
|
88
|
662 if (s->out.len) {
|
|
663 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
664 s->blocked = 1;
|
|
665 return;
|
|
666 }
|
|
667
|
|
668 s->blocked = 0;
|
|
669
|
76
|
670 rc = ngx_imap_read_command(s);
|
|
671
|
|
672 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
673 return;
|
|
674 }
|
|
675
|
|
676 text = pop3_ok;
|
|
677 size = sizeof(pop3_ok) - 1;
|
|
678
|
|
679 if (rc == NGX_OK) {
|
|
680 switch (s->imap_state) {
|
|
681
|
|
682 case ngx_pop3_start:
|
|
683
|
|
684 switch (s->command) {
|
|
685
|
|
686 case NGX_POP3_USER:
|
|
687 if (s->args.nelts == 1) {
|
|
688 s->imap_state = ngx_pop3_user;
|
|
689
|
|
690 arg = s->args.elts;
|
|
691 s->login.len = arg[0].len;
|
|
692 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
693 if (s->login.data == NULL) {
|
|
694 ngx_imap_session_internal_server_error(s);
|
|
695 return;
|
|
696 }
|
|
697
|
|
698 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
699
|
|
700 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
701 "pop3 login: \"%V\"", &s->login);
|
|
702
|
|
703 } else {
|
|
704 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
705 }
|
|
706
|
|
707 break;
|
|
708
|
|
709 case NGX_POP3_CAPA:
|
|
710 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
711
|
|
712 #if (NGX_IMAP_SSL)
|
|
713
|
|
714 if (c->ssl == NULL) {
|
|
715 sslcf = ngx_imap_get_module_srv_conf(s,
|
|
716 ngx_imap_ssl_module);
|
|
717 if (sslcf->starttls) {
|
|
718 size = cscf->pop3_starttls_capability.len;
|
|
719 text = cscf->pop3_starttls_capability.data;
|
|
720 break;
|
|
721 }
|
|
722 }
|
|
723 #endif
|
|
724
|
|
725 size = cscf->pop3_capability.len;
|
|
726 text = cscf->pop3_capability.data;
|
76
|
727 break;
|
|
728
|
|
729 case NGX_POP3_QUIT:
|
88
|
730 s->quit = 1;
|
76
|
731 break;
|
|
732
|
|
733 case NGX_POP3_NOOP:
|
|
734 break;
|
|
735
|
132
|
736 #if (NGX_IMAP_SSL)
|
|
737
|
|
738 case NGX_POP3_STLS:
|
|
739 if (c->ssl == NULL) {
|
|
740 sslcf = ngx_imap_get_module_srv_conf(s,
|
|
741 ngx_imap_ssl_module);
|
|
742 if (sslcf->starttls) {
|
|
743 c->read->handler = ngx_imap_starttls_handler;
|
|
744 break;
|
|
745 }
|
|
746 }
|
|
747
|
|
748 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
749 break;
|
|
750 #endif
|
|
751
|
76
|
752 default:
|
|
753 s->imap_state = ngx_pop3_start;
|
|
754 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
755 break;
|
|
756 }
|
|
757
|
|
758 break;
|
|
759
|
|
760 case ngx_pop3_user:
|
|
761
|
|
762 switch (s->command) {
|
|
763
|
|
764 case NGX_POP3_PASS:
|
|
765 if (s->args.nelts == 1) {
|
|
766 /* STUB */ s->imap_state = ngx_pop3_start;
|
|
767
|
|
768 arg = s->args.elts;
|
|
769 s->passwd.len = arg[0].len;
|
|
770 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
771 if (s->passwd.data == NULL) {
|
|
772 ngx_imap_session_internal_server_error(s);
|
|
773 return;
|
|
774 }
|
|
775
|
|
776 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
|
|
777
|
96
|
778 #if (NGX_DEBUG_IMAP_PASSWD)
|
76
|
779 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
780 "pop3 passwd: \"%V\"", &s->passwd);
|
96
|
781 #endif
|
76
|
782
|
|
783 s->args.nelts = 0;
|
|
784 s->buffer->pos = s->buffer->start;
|
|
785 s->buffer->last = s->buffer->start;
|
|
786
|
|
787 if (rev->timer_set) {
|
|
788 ngx_del_timer(rev);
|
|
789 }
|
|
790
|
|
791 ngx_imap_auth_http_init(s);
|
|
792
|
|
793 return;
|
|
794
|
|
795 } else {
|
|
796 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
797 }
|
|
798
|
|
799 break;
|
|
800
|
|
801 case NGX_POP3_CAPA:
|
|
802 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
803 size = cscf->pop3_capability.len;
|
|
804 text = cscf->pop3_capability.data;
|
76
|
805 break;
|
|
806
|
|
807 case NGX_POP3_QUIT:
|
88
|
808 s->quit = 1;
|
76
|
809 break;
|
|
810
|
|
811 case NGX_POP3_NOOP:
|
|
812 break;
|
|
813
|
|
814 default:
|
|
815 s->imap_state = ngx_pop3_start;
|
|
816 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
817 break;
|
|
818 }
|
|
819
|
|
820 break;
|
|
821
|
|
822 /* suppress warinings */
|
|
823 case ngx_pop3_passwd:
|
|
824 break;
|
|
825 }
|
|
826 }
|
|
827
|
|
828 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
829 text = pop3_invalid_command;
|
|
830 size = sizeof(pop3_invalid_command) - 1;
|
|
831 }
|
|
832
|
|
833 s->args.nelts = 0;
|
|
834 s->buffer->pos = s->buffer->start;
|
|
835 s->buffer->last = s->buffer->start;
|
88
|
836
|
|
837 s->out.data = text;
|
|
838 s->out.len = size;
|
|
839
|
|
840 ngx_imap_send(c->write);
|
76
|
841 }
|
|
842
|
|
843
|
|
844 static ngx_int_t
|
|
845 ngx_imap_read_command(ngx_imap_session_t *s)
|
|
846 {
|
|
847 ssize_t n;
|
|
848 ngx_int_t rc;
|
|
849
|
88
|
850 n = s->connection->recv(s->connection, s->buffer->last,
|
|
851 s->buffer->end - s->buffer->last);
|
76
|
852
|
|
853 if (n == NGX_ERROR || n == 0) {
|
|
854 ngx_imap_close_connection(s->connection);
|
|
855 return NGX_ERROR;
|
|
856 }
|
|
857
|
|
858 if (n > 0) {
|
|
859 s->buffer->last += n;
|
|
860 }
|
|
861
|
|
862 if (n == NGX_AGAIN) {
|
|
863 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
|
|
864 ngx_imap_session_internal_server_error(s);
|
|
865 return NGX_ERROR;
|
|
866 }
|
|
867
|
|
868 return NGX_AGAIN;
|
|
869 }
|
|
870
|
|
871 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
872 rc = ngx_pop3_parse_command(s);
|
|
873 } else {
|
|
874 rc = ngx_imap_parse_command(s);
|
|
875 }
|
|
876
|
|
877 if (rc == NGX_AGAIN
|
|
878 || rc == NGX_IMAP_NEXT
|
|
879 || rc == NGX_IMAP_PARSE_INVALID_COMMAND)
|
|
880 {
|
|
881 return rc;
|
|
882 }
|
|
883
|
|
884 if (rc == NGX_ERROR) {
|
|
885 ngx_imap_close_connection(s->connection);
|
|
886 return NGX_ERROR;
|
|
887 }
|
|
888
|
|
889 return NGX_OK;
|
|
890 }
|
|
891
|
|
892
|
|
893 void
|
|
894 ngx_imap_session_internal_server_error(ngx_imap_session_t *s)
|
|
895 {
|
88
|
896 s->out = internal_server_errors[s->protocol];
|
|
897 s->quit = 1;
|
76
|
898
|
88
|
899 ngx_imap_send(s->connection->write);
|
76
|
900 }
|
|
901
|
|
902
|
|
903 void
|
|
904 ngx_imap_close_connection(ngx_connection_t *c)
|
|
905 {
|
|
906 ngx_pool_t *pool;
|
|
907
|
|
908 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
909 "close imap connection: %d", c->fd);
|
|
910
|
88
|
911 #if (NGX_IMAP_SSL)
|
|
912
|
|
913 if (c->ssl) {
|
|
914 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
915 c->ssl->handler = ngx_imap_close_connection;
|
88
|
916 return;
|
|
917 }
|
|
918 }
|
|
919
|
|
920 #endif
|
|
921
|
132
|
922 c->destroyed = 1;
|
92
|
923
|
76
|
924 pool = c->pool;
|
|
925
|
|
926 ngx_close_connection(c);
|
|
927
|
|
928 ngx_destroy_pool(pool);
|
|
929 }
|
88
|
930
|
|
931
|
90
|
932 static u_char *
|
|
933 ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
|
934 {
|
116
|
935 u_char *p;
|
|
936 ngx_imap_session_t *s;
|
|
937 ngx_imap_log_ctx_t *ctx;
|
90
|
938
|
|
939 if (log->action) {
|
|
940 p = ngx_snprintf(buf, len, " while %s", log->action);
|
|
941 len -= p - buf;
|
|
942 buf = p;
|
|
943 }
|
126
|
944
|
90
|
945 ctx = log->data;
|
|
946
|
|
947 p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
|
948 len -= p - buf;
|
|
949 buf = p;
|
|
950
|
|
951 s = ctx->session;
|
|
952
|
|
953 if (s == NULL) {
|
|
954 return p;
|
|
955 }
|
|
956
|
190
|
957 p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
|
90
|
958 len -= p - buf;
|
|
959 buf = p;
|
|
960
|
|
961 if (s->login.len == 0) {
|
|
962 return p;
|
|
963 }
|
|
964
|
|
965 p = ngx_snprintf(buf, len, ", login: \"%V\"", &s->login);
|
|
966 len -= p - buf;
|
|
967 buf = p;
|
|
968
|
|
969 if (s->proxy == NULL) {
|
|
970 return p;
|
|
971 }
|
|
972
|
|
973 p = ngx_snprintf(buf, len, ", upstream: %V",
|
|
974 &s->proxy->upstream.peers->peer[0].name);
|
|
975
|
|
976 return p;
|
|
977 }
|