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 {
|
250
|
236 u_char *p;
|
88
|
237 ngx_imap_session_t *s;
|
90
|
238 ngx_imap_core_srv_conf_t *cscf;
|
76
|
239
|
96
|
240 c->read->handler = ngx_imap_init_protocol;
|
|
241 c->write->handler = ngx_imap_send;
|
88
|
242
|
190
|
243 s = c->data;
|
76
|
244
|
190
|
245 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
88
|
246
|
|
247 s->protocol = cscf->protocol;
|
|
248
|
|
249 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module);
|
|
250 if (s->ctx == NULL) {
|
|
251 ngx_imap_session_internal_server_error(s);
|
|
252 return;
|
|
253 }
|
|
254
|
|
255 s->out = greetings[s->protocol];
|
|
256
|
250
|
257 if ((cscf->auth_methods & NGX_IMAP_AUTH_APOP_ENABLED)
|
|
258 && s->protocol == NGX_IMAP_POP3_PROTOCOL)
|
|
259 {
|
|
260 s->salt.data = ngx_palloc(c->pool,
|
|
261 sizeof(" <18446744073709551616.@>" CRLF) - 1
|
|
262 + NGX_TIME_T_LEN
|
|
263 + cscf->server_name.len);
|
|
264 if (s->salt.data == NULL) {
|
|
265 ngx_imap_session_internal_server_error(s);
|
|
266 return;
|
|
267 }
|
|
268
|
|
269 s->salt.len = ngx_sprintf(s->salt.data, "<%ul.%T@%V>" CRLF,
|
|
270 ngx_random(), ngx_time(), &cscf->server_name)
|
|
271 - s->salt.data;
|
|
272
|
|
273 s->out.data = ngx_palloc(c->pool, greetings[0].len + 1 + s->salt.len);
|
|
274 if (s->out.data == NULL) {
|
|
275 ngx_imap_session_internal_server_error(s);
|
|
276 return;
|
|
277 }
|
|
278
|
|
279 p = ngx_cpymem(s->out.data, greetings[0].data, greetings[0].len - 2);
|
|
280 *p++ = ' ';
|
|
281 p = ngx_cpymem(p, s->salt.data, s->salt.len);
|
|
282
|
|
283 s->out.len = p - s->out.data;
|
|
284 }
|
|
285
|
96
|
286 ngx_add_timer(c->read, cscf->timeout);
|
76
|
287
|
96
|
288 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
76
|
289 ngx_imap_close_connection(c);
|
|
290 }
|
88
|
291
|
|
292 ngx_imap_send(c->write);
|
|
293 }
|
|
294
|
|
295
|
|
296 void
|
|
297 ngx_imap_send(ngx_event_t *wev)
|
|
298 {
|
90
|
299 ngx_int_t n;
|
|
300 ngx_connection_t *c;
|
|
301 ngx_imap_session_t *s;
|
|
302 ngx_imap_core_srv_conf_t *cscf;
|
88
|
303
|
|
304 c = wev->data;
|
|
305 s = c->data;
|
|
306
|
|
307 if (wev->timedout) {
|
|
308 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
309 c->timedout = 1;
|
88
|
310 ngx_imap_close_connection(c);
|
|
311 return;
|
|
312 }
|
|
313
|
|
314 if (s->out.len == 0) {
|
|
315 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
316 ngx_imap_close_connection(c);
|
|
317 }
|
|
318
|
|
319 return;
|
|
320 }
|
|
321
|
|
322 n = c->send(c, s->out.data, s->out.len);
|
|
323
|
|
324 if (n > 0) {
|
|
325 s->out.len -= n;
|
|
326
|
90
|
327 if (wev->timer_set) {
|
|
328 ngx_del_timer(wev);
|
|
329 }
|
|
330
|
88
|
331 if (s->quit) {
|
|
332 ngx_imap_close_connection(c);
|
|
333 return;
|
|
334 }
|
|
335
|
|
336 if (s->blocked) {
|
|
337 c->read->handler(c->read);
|
|
338 }
|
|
339
|
|
340 return;
|
|
341 }
|
|
342
|
|
343 if (n == NGX_ERROR) {
|
|
344 ngx_imap_close_connection(c);
|
|
345 return;
|
|
346 }
|
|
347
|
|
348 /* n == NGX_AGAIN */
|
|
349
|
90
|
350 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
351
|
|
352 ngx_add_timer(c->write, cscf->timeout);
|
|
353
|
88
|
354 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
355 ngx_imap_close_connection(c);
|
|
356 return;
|
|
357 }
|
76
|
358 }
|
|
359
|
|
360
|
|
361 static void
|
90
|
362 ngx_imap_init_protocol(ngx_event_t *rev)
|
76
|
363 {
|
|
364 size_t size;
|
|
365 ngx_connection_t *c;
|
|
366 ngx_imap_session_t *s;
|
|
367 ngx_imap_core_srv_conf_t *cscf;
|
|
368
|
|
369 c = rev->data;
|
|
370
|
90
|
371 c->log->action = "in auth state";
|
|
372
|
76
|
373 if (rev->timedout) {
|
|
374 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
375 c->timedout = 1;
|
76
|
376 ngx_imap_close_connection(c);
|
|
377 return;
|
|
378 }
|
|
379
|
88
|
380 s = c->data;
|
76
|
381
|
88
|
382 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
76
|
383 size = 128;
|
|
384 s->imap_state = ngx_pop3_start;
|
|
385 c->read->handler = ngx_pop3_auth_state;
|
|
386
|
|
387 } else {
|
88
|
388 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
76
|
389 size = cscf->imap_client_buffer_size;
|
|
390 s->imap_state = ngx_imap_start;
|
|
391 c->read->handler = ngx_imap_auth_state;
|
|
392 }
|
|
393
|
|
394 if (s->buffer == NULL) {
|
132
|
395 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
|
|
396 == NGX_ERROR)
|
|
397 {
|
|
398 ngx_imap_session_internal_server_error(s);
|
|
399 return;
|
|
400 }
|
|
401
|
|
402 s->buffer = ngx_create_temp_buf(c->pool, size);
|
|
403 if (s->buffer == NULL) {
|
|
404 ngx_imap_session_internal_server_error(s);
|
|
405 return;
|
|
406 }
|
76
|
407 }
|
|
408
|
|
409 c->read->handler(rev);
|
|
410 }
|
|
411
|
|
412
|
|
413 void
|
|
414 ngx_imap_auth_state(ngx_event_t *rev)
|
|
415 {
|
92
|
416 u_char *text, *last, *p, *dst, *src, *end;
|
88
|
417 ssize_t text_len, last_len;
|
76
|
418 ngx_str_t *arg;
|
|
419 ngx_int_t rc;
|
92
|
420 ngx_uint_t tag, i;
|
76
|
421 ngx_connection_t *c;
|
|
422 ngx_imap_session_t *s;
|
|
423 ngx_imap_core_srv_conf_t *cscf;
|
132
|
424 #if (NGX_IMAP_SSL)
|
|
425 ngx_imap_ssl_conf_t *sslcf;
|
|
426 #endif
|
76
|
427
|
|
428 c = rev->data;
|
|
429 s = c->data;
|
|
430
|
|
431 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state");
|
|
432
|
|
433 if (rev->timedout) {
|
|
434 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
435 c->timedout = 1;
|
76
|
436 ngx_imap_close_connection(c);
|
|
437 return;
|
|
438 }
|
|
439
|
88
|
440 if (s->out.len) {
|
|
441 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
442 s->blocked = 1;
|
|
443 return;
|
|
444 }
|
|
445
|
|
446 s->blocked = 0;
|
|
447
|
76
|
448 rc = ngx_imap_read_command(s);
|
|
449
|
|
450 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth: %i", rc);
|
|
451
|
|
452 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
453 return;
|
|
454 }
|
|
455
|
|
456 tag = 1;
|
|
457
|
|
458 text = NULL;
|
|
459 text_len = 0;
|
|
460
|
|
461 last = imap_ok;
|
|
462 last_len = sizeof(imap_ok) - 1;
|
|
463
|
|
464 if (rc == NGX_OK) {
|
|
465
|
|
466 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth command: %i",
|
|
467 s->command);
|
|
468
|
92
|
469 if (s->backslash) {
|
|
470
|
|
471 arg = s->args.elts;
|
|
472
|
|
473 for (i = 0; i < s->args.nelts; i++) {
|
|
474 dst = arg[i].data;
|
|
475 end = dst + arg[i].len;
|
|
476
|
|
477 for (src = dst; src < end; dst++) {
|
|
478 *dst = *src;
|
|
479 if (*src++ == '\\') {
|
|
480 *dst = *src++;
|
|
481 }
|
|
482 }
|
|
483
|
|
484 arg[i].len = dst - arg[i].data;
|
|
485 }
|
|
486
|
|
487 s->backslash = 0;
|
|
488 }
|
|
489
|
76
|
490 switch (s->command) {
|
|
491
|
|
492 case NGX_IMAP_LOGIN:
|
132
|
493
|
|
494 #if (NGX_IMAP_SSL)
|
|
495
|
|
496 if (c->ssl == NULL) {
|
|
497 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
498
|
|
499 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
|
|
500 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
501 break;
|
|
502 }
|
|
503 }
|
|
504 #endif
|
|
505
|
118
|
506 arg = s->args.elts;
|
76
|
507
|
118
|
508 if (s->args.nelts == 2 && arg[0].len) {
|
76
|
509
|
|
510 s->login.len = arg[0].len;
|
|
511 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
512 if (s->login.data == NULL) {
|
|
513 ngx_imap_session_internal_server_error(s);
|
|
514 return;
|
|
515 }
|
|
516
|
|
517 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
518
|
|
519 s->passwd.len = arg[1].len;
|
|
520 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
521 if (s->passwd.data == NULL) {
|
|
522 ngx_imap_session_internal_server_error(s);
|
|
523 return;
|
|
524 }
|
|
525
|
|
526 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
527
|
96
|
528 #if (NGX_DEBUG_IMAP_PASSWD)
|
76
|
529 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
530 "imap login:\"%V\" passwd:\"%V\"",
|
|
531 &s->login, &s->passwd);
|
96
|
532 #else
|
|
533 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
534 "imap login:\"%V\"", &s->login);
|
|
535 #endif
|
76
|
536
|
|
537 s->args.nelts = 0;
|
|
538 s->buffer->pos = s->buffer->start;
|
|
539 s->buffer->last = s->buffer->start;
|
|
540
|
|
541 if (rev->timer_set) {
|
|
542 ngx_del_timer(rev);
|
|
543 }
|
|
544
|
|
545 s->login_attempt++;
|
|
546
|
|
547 ngx_imap_auth_http_init(s);
|
|
548
|
|
549 return;
|
|
550
|
|
551 } else {
|
|
552 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
553 }
|
|
554
|
|
555 break;
|
|
556
|
|
557 case NGX_IMAP_CAPABILITY:
|
|
558 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
559
|
|
560 #if (NGX_IMAP_SSL)
|
|
561
|
|
562 if (c->ssl == NULL) {
|
|
563 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
564
|
|
565 if (sslcf->starttls == NGX_IMAP_STARTTLS_ON) {
|
|
566 text_len = cscf->imap_starttls_capability.len;
|
|
567 text = cscf->imap_starttls_capability.data;
|
|
568 break;
|
|
569 }
|
|
570
|
|
571 if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
|
|
572 text_len = cscf->imap_starttls_only_capability.len;
|
|
573 text = cscf->imap_starttls_only_capability.data;
|
|
574 break;
|
|
575 }
|
|
576 }
|
|
577 #endif
|
|
578
|
|
579 text_len = cscf->imap_capability.len;
|
|
580 text = cscf->imap_capability.data;
|
76
|
581 break;
|
|
582
|
|
583 case NGX_IMAP_LOGOUT:
|
88
|
584 s->quit = 1;
|
76
|
585 text = imap_bye;
|
|
586 text_len = sizeof(imap_bye) - 1;
|
|
587 break;
|
|
588
|
|
589 case NGX_IMAP_NOOP:
|
|
590 break;
|
|
591
|
132
|
592 #if (NGX_IMAP_SSL)
|
|
593
|
|
594 case NGX_IMAP_STARTTLS:
|
|
595 if (c->ssl == NULL) {
|
|
596 sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
|
|
597 if (sslcf->starttls) {
|
|
598 c->read->handler = ngx_imap_starttls_handler;
|
|
599 break;
|
|
600 }
|
|
601 }
|
|
602
|
|
603 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
604 break;
|
|
605 #endif
|
|
606
|
76
|
607 default:
|
|
608 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
609 break;
|
|
610 }
|
|
611
|
|
612 } else if (rc == NGX_IMAP_NEXT) {
|
|
613 last = imap_next;
|
|
614 last_len = sizeof(imap_next) - 1;
|
|
615 tag = 0;
|
|
616 }
|
|
617
|
|
618 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
619 last = imap_invalid_command;
|
|
620 last_len = sizeof(imap_invalid_command) - 1;
|
|
621 }
|
|
622
|
|
623 if (tag) {
|
92
|
624 if (s->tag.len == 0) {
|
|
625 s->tag.len = sizeof(imap_star) - 1;
|
|
626 s->tag.data = (u_char *) imap_star;
|
|
627 }
|
|
628
|
88
|
629 if (s->tagged_line.len < s->tag.len + text_len + last_len) {
|
|
630 s->tagged_line.len = s->tag.len + text_len + last_len;
|
|
631 s->tagged_line.data = ngx_palloc(c->pool, s->tagged_line.len);
|
|
632 if (s->tagged_line.data == NULL) {
|
76
|
633 ngx_imap_close_connection(c);
|
|
634 return;
|
|
635 }
|
|
636 }
|
|
637
|
88
|
638 s->out.data = s->tagged_line.data;
|
|
639 s->out.len = s->tag.len + text_len + last_len;
|
|
640
|
|
641 p = s->out.data;
|
76
|
642
|
|
643 if (text) {
|
|
644 p = ngx_cpymem(p, text, text_len);
|
|
645 }
|
|
646 p = ngx_cpymem(p, s->tag.data, s->tag.len);
|
|
647 ngx_memcpy(p, last, last_len);
|
|
648
|
|
649
|
|
650 } else {
|
88
|
651 s->out.data = last;
|
|
652 s->out.len = last_len;
|
76
|
653 }
|
|
654
|
88
|
655 if (rc != NGX_IMAP_NEXT) {
|
|
656 s->args.nelts = 0;
|
|
657 s->buffer->pos = s->buffer->start;
|
|
658 s->buffer->last = s->buffer->start;
|
|
659 s->tag.len = 0;
|
76
|
660 }
|
|
661
|
88
|
662 ngx_imap_send(c->write);
|
76
|
663 }
|
|
664
|
|
665
|
|
666 void
|
|
667 ngx_pop3_auth_state(ngx_event_t *rev)
|
|
668 {
|
|
669 u_char *text;
|
|
670 ssize_t size;
|
|
671 ngx_int_t rc;
|
|
672 ngx_str_t *arg;
|
|
673 ngx_connection_t *c;
|
|
674 ngx_imap_session_t *s;
|
|
675 ngx_imap_core_srv_conf_t *cscf;
|
132
|
676 #if (NGX_IMAP_SSL)
|
|
677 ngx_imap_ssl_conf_t *sslcf;
|
|
678 #endif
|
76
|
679
|
|
680 c = rev->data;
|
|
681 s = c->data;
|
|
682
|
|
683 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state");
|
|
684
|
|
685 if (rev->timedout) {
|
|
686 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
126
|
687 c->timedout = 1;
|
76
|
688 ngx_imap_close_connection(c);
|
|
689 return;
|
|
690 }
|
|
691
|
88
|
692 if (s->out.len) {
|
|
693 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
|
|
694 s->blocked = 1;
|
|
695 return;
|
|
696 }
|
|
697
|
|
698 s->blocked = 0;
|
|
699
|
76
|
700 rc = ngx_imap_read_command(s);
|
|
701
|
|
702 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
703 return;
|
|
704 }
|
|
705
|
|
706 text = pop3_ok;
|
|
707 size = sizeof(pop3_ok) - 1;
|
|
708
|
|
709 if (rc == NGX_OK) {
|
|
710 switch (s->imap_state) {
|
|
711
|
|
712 case ngx_pop3_start:
|
|
713
|
|
714 switch (s->command) {
|
|
715
|
|
716 case NGX_POP3_USER:
|
|
717 if (s->args.nelts == 1) {
|
|
718 s->imap_state = ngx_pop3_user;
|
|
719
|
|
720 arg = s->args.elts;
|
|
721 s->login.len = arg[0].len;
|
|
722 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
723 if (s->login.data == NULL) {
|
|
724 ngx_imap_session_internal_server_error(s);
|
|
725 return;
|
|
726 }
|
|
727
|
|
728 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
729
|
|
730 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
731 "pop3 login: \"%V\"", &s->login);
|
|
732
|
|
733 } else {
|
|
734 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
735 }
|
|
736
|
|
737 break;
|
|
738
|
|
739 case NGX_POP3_CAPA:
|
|
740 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
741
|
|
742 #if (NGX_IMAP_SSL)
|
|
743
|
|
744 if (c->ssl == NULL) {
|
|
745 sslcf = ngx_imap_get_module_srv_conf(s,
|
|
746 ngx_imap_ssl_module);
|
|
747 if (sslcf->starttls) {
|
|
748 size = cscf->pop3_starttls_capability.len;
|
|
749 text = cscf->pop3_starttls_capability.data;
|
|
750 break;
|
|
751 }
|
|
752 }
|
|
753 #endif
|
|
754
|
|
755 size = cscf->pop3_capability.len;
|
|
756 text = cscf->pop3_capability.data;
|
76
|
757 break;
|
|
758
|
250
|
759 case NGX_POP3_APOP:
|
|
760 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
761
|
|
762 if ((cscf->auth_methods & NGX_IMAP_AUTH_APOP_ENABLED)
|
|
763 && s->args.nelts == 2)
|
|
764 {
|
|
765 arg = s->args.elts;
|
|
766
|
|
767 s->login.len = arg[0].len;
|
|
768 s->login.data = ngx_palloc(c->pool, s->login.len);
|
|
769 if (s->login.data == NULL) {
|
|
770 ngx_imap_session_internal_server_error(s);
|
|
771 return;
|
|
772 }
|
|
773
|
|
774 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
775
|
|
776 s->passwd.len = arg[1].len;
|
|
777 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
778 if (s->passwd.data == NULL) {
|
|
779 ngx_imap_session_internal_server_error(s);
|
|
780 return;
|
|
781 }
|
|
782
|
|
783 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
784
|
|
785 ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
786 "pop3 apop: \"%V\" \"%V\"",
|
|
787 &s->login, &s->passwd);
|
|
788
|
|
789 s->auth_method = NGX_IMAP_AUTH_APOP;
|
|
790
|
|
791 s->args.nelts = 0;
|
|
792 s->buffer->pos = s->buffer->start;
|
|
793 s->buffer->last = s->buffer->start;
|
|
794
|
|
795 if (rev->timer_set) {
|
|
796 ngx_del_timer(rev);
|
|
797 }
|
|
798
|
|
799 ngx_imap_auth_http_init(s);
|
|
800
|
|
801 return;
|
|
802
|
|
803 } else {
|
|
804 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
805 }
|
|
806
|
|
807 break;
|
|
808
|
76
|
809 case NGX_POP3_QUIT:
|
88
|
810 s->quit = 1;
|
76
|
811 break;
|
|
812
|
|
813 case NGX_POP3_NOOP:
|
|
814 break;
|
|
815
|
132
|
816 #if (NGX_IMAP_SSL)
|
|
817
|
|
818 case NGX_POP3_STLS:
|
|
819 if (c->ssl == NULL) {
|
|
820 sslcf = ngx_imap_get_module_srv_conf(s,
|
|
821 ngx_imap_ssl_module);
|
|
822 if (sslcf->starttls) {
|
|
823 c->read->handler = ngx_imap_starttls_handler;
|
|
824 break;
|
|
825 }
|
|
826 }
|
|
827
|
|
828 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
829 break;
|
|
830 #endif
|
|
831
|
76
|
832 default:
|
|
833 s->imap_state = ngx_pop3_start;
|
|
834 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
835 break;
|
|
836 }
|
|
837
|
|
838 break;
|
|
839
|
|
840 case ngx_pop3_user:
|
|
841
|
|
842 switch (s->command) {
|
|
843
|
|
844 case NGX_POP3_PASS:
|
|
845 if (s->args.nelts == 1) {
|
|
846 arg = s->args.elts;
|
|
847 s->passwd.len = arg[0].len;
|
|
848 s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
|
|
849 if (s->passwd.data == NULL) {
|
|
850 ngx_imap_session_internal_server_error(s);
|
|
851 return;
|
|
852 }
|
|
853
|
|
854 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
|
|
855
|
96
|
856 #if (NGX_DEBUG_IMAP_PASSWD)
|
76
|
857 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
858 "pop3 passwd: \"%V\"", &s->passwd);
|
96
|
859 #endif
|
76
|
860
|
|
861 s->args.nelts = 0;
|
|
862 s->buffer->pos = s->buffer->start;
|
|
863 s->buffer->last = s->buffer->start;
|
|
864
|
|
865 if (rev->timer_set) {
|
|
866 ngx_del_timer(rev);
|
|
867 }
|
|
868
|
|
869 ngx_imap_auth_http_init(s);
|
|
870
|
|
871 return;
|
|
872
|
|
873 } else {
|
|
874 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
875 }
|
|
876
|
|
877 break;
|
|
878
|
|
879 case NGX_POP3_CAPA:
|
|
880 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
132
|
881 size = cscf->pop3_capability.len;
|
|
882 text = cscf->pop3_capability.data;
|
76
|
883 break;
|
|
884
|
|
885 case NGX_POP3_QUIT:
|
88
|
886 s->quit = 1;
|
76
|
887 break;
|
|
888
|
|
889 case NGX_POP3_NOOP:
|
|
890 break;
|
|
891
|
|
892 default:
|
|
893 s->imap_state = ngx_pop3_start;
|
|
894 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
|
|
895 break;
|
|
896 }
|
|
897
|
|
898 break;
|
|
899
|
|
900 /* suppress warinings */
|
|
901 case ngx_pop3_passwd:
|
|
902 break;
|
|
903 }
|
|
904 }
|
|
905
|
|
906 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
|
|
907 text = pop3_invalid_command;
|
|
908 size = sizeof(pop3_invalid_command) - 1;
|
|
909 }
|
|
910
|
|
911 s->args.nelts = 0;
|
|
912 s->buffer->pos = s->buffer->start;
|
|
913 s->buffer->last = s->buffer->start;
|
88
|
914
|
|
915 s->out.data = text;
|
|
916 s->out.len = size;
|
|
917
|
|
918 ngx_imap_send(c->write);
|
76
|
919 }
|
|
920
|
|
921
|
|
922 static ngx_int_t
|
|
923 ngx_imap_read_command(ngx_imap_session_t *s)
|
|
924 {
|
|
925 ssize_t n;
|
|
926 ngx_int_t rc;
|
|
927
|
88
|
928 n = s->connection->recv(s->connection, s->buffer->last,
|
|
929 s->buffer->end - s->buffer->last);
|
76
|
930
|
|
931 if (n == NGX_ERROR || n == 0) {
|
|
932 ngx_imap_close_connection(s->connection);
|
|
933 return NGX_ERROR;
|
|
934 }
|
|
935
|
|
936 if (n > 0) {
|
|
937 s->buffer->last += n;
|
|
938 }
|
|
939
|
|
940 if (n == NGX_AGAIN) {
|
|
941 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
|
|
942 ngx_imap_session_internal_server_error(s);
|
|
943 return NGX_ERROR;
|
|
944 }
|
|
945
|
|
946 return NGX_AGAIN;
|
|
947 }
|
|
948
|
|
949 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
950 rc = ngx_pop3_parse_command(s);
|
|
951 } else {
|
|
952 rc = ngx_imap_parse_command(s);
|
|
953 }
|
|
954
|
|
955 if (rc == NGX_AGAIN
|
|
956 || rc == NGX_IMAP_NEXT
|
|
957 || rc == NGX_IMAP_PARSE_INVALID_COMMAND)
|
|
958 {
|
|
959 return rc;
|
|
960 }
|
|
961
|
|
962 if (rc == NGX_ERROR) {
|
|
963 ngx_imap_close_connection(s->connection);
|
|
964 return NGX_ERROR;
|
|
965 }
|
|
966
|
|
967 return NGX_OK;
|
|
968 }
|
|
969
|
|
970
|
|
971 void
|
|
972 ngx_imap_session_internal_server_error(ngx_imap_session_t *s)
|
|
973 {
|
88
|
974 s->out = internal_server_errors[s->protocol];
|
|
975 s->quit = 1;
|
76
|
976
|
88
|
977 ngx_imap_send(s->connection->write);
|
76
|
978 }
|
|
979
|
|
980
|
|
981 void
|
|
982 ngx_imap_close_connection(ngx_connection_t *c)
|
|
983 {
|
|
984 ngx_pool_t *pool;
|
|
985
|
|
986 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
|
|
987 "close imap connection: %d", c->fd);
|
|
988
|
88
|
989 #if (NGX_IMAP_SSL)
|
|
990
|
|
991 if (c->ssl) {
|
|
992 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
|
126
|
993 c->ssl->handler = ngx_imap_close_connection;
|
88
|
994 return;
|
|
995 }
|
|
996 }
|
|
997
|
|
998 #endif
|
|
999
|
132
|
1000 c->destroyed = 1;
|
92
|
1001
|
76
|
1002 pool = c->pool;
|
|
1003
|
|
1004 ngx_close_connection(c);
|
|
1005
|
|
1006 ngx_destroy_pool(pool);
|
|
1007 }
|
88
|
1008
|
|
1009
|
90
|
1010 static u_char *
|
|
1011 ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
|
1012 {
|
116
|
1013 u_char *p;
|
|
1014 ngx_imap_session_t *s;
|
|
1015 ngx_imap_log_ctx_t *ctx;
|
90
|
1016
|
|
1017 if (log->action) {
|
|
1018 p = ngx_snprintf(buf, len, " while %s", log->action);
|
|
1019 len -= p - buf;
|
|
1020 buf = p;
|
|
1021 }
|
126
|
1022
|
90
|
1023 ctx = log->data;
|
|
1024
|
|
1025 p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
|
|
1026 len -= p - buf;
|
|
1027 buf = p;
|
|
1028
|
|
1029 s = ctx->session;
|
|
1030
|
|
1031 if (s == NULL) {
|
|
1032 return p;
|
|
1033 }
|
|
1034
|
190
|
1035 p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
|
90
|
1036 len -= p - buf;
|
|
1037 buf = p;
|
|
1038
|
|
1039 if (s->login.len == 0) {
|
|
1040 return p;
|
|
1041 }
|
|
1042
|
|
1043 p = ngx_snprintf(buf, len, ", login: \"%V\"", &s->login);
|
|
1044 len -= p - buf;
|
|
1045 buf = p;
|
|
1046
|
|
1047 if (s->proxy == NULL) {
|
|
1048 return p;
|
|
1049 }
|
|
1050
|
|
1051 p = ngx_snprintf(buf, len, ", upstream: %V",
|
|
1052 &s->proxy->upstream.peers->peer[0].name);
|
|
1053
|
|
1054 return p;
|
|
1055 }
|