Mercurial > hg > nginx-mail
annotate src/mail/ngx_mail_proxy_module.c @ 570:9773720b845e
Merge with 0.8.16.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sat, 26 Sep 2009 01:25:07 +0400 |
parents | c78a94ba4ae1 f39b9e29530d |
children | 0b460e61bdcd |
rev | line source |
---|---|
290 | 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_event_connect.h> | |
11 #include <ngx_mail.h> | |
12 | |
13 | |
14 typedef struct { | |
15 ngx_flag_t enable; | |
16 ngx_flag_t pass_error_message; | |
17 ngx_flag_t xclient; | |
18 size_t buffer_size; | |
19 ngx_msec_t timeout; | |
20 } ngx_mail_proxy_conf_t; | |
21 | |
22 | |
23 static void ngx_mail_proxy_block_read(ngx_event_t *rev); | |
24 static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev); | |
25 static void ngx_mail_proxy_imap_handler(ngx_event_t *rev); | |
26 static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev); | |
27 static void ngx_mail_proxy_dummy_handler(ngx_event_t *ev); | |
28 static ngx_int_t ngx_mail_proxy_read_response(ngx_mail_session_t *s, | |
29 ngx_uint_t state); | |
30 static void ngx_mail_proxy_handler(ngx_event_t *ev); | |
31 static void ngx_mail_proxy_upstream_error(ngx_mail_session_t *s); | |
32 static void ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s); | |
33 static void ngx_mail_proxy_close_session(ngx_mail_session_t *s); | |
34 static void *ngx_mail_proxy_create_conf(ngx_conf_t *cf); | |
35 static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, | |
36 void *child); | |
37 | |
38 | |
39 static ngx_command_t ngx_mail_proxy_commands[] = { | |
40 | |
41 { ngx_string("proxy"), | |
42 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, | |
43 ngx_conf_set_flag_slot, | |
44 NGX_MAIL_SRV_CONF_OFFSET, | |
45 offsetof(ngx_mail_proxy_conf_t, enable), | |
46 NULL }, | |
47 | |
48 { ngx_string("proxy_buffer"), | |
49 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, | |
50 ngx_conf_set_size_slot, | |
51 NGX_MAIL_SRV_CONF_OFFSET, | |
52 offsetof(ngx_mail_proxy_conf_t, buffer_size), | |
53 NULL }, | |
54 | |
55 { ngx_string("proxy_timeout"), | |
56 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, | |
57 ngx_conf_set_msec_slot, | |
58 NGX_MAIL_SRV_CONF_OFFSET, | |
59 offsetof(ngx_mail_proxy_conf_t, timeout), | |
60 NULL }, | |
61 | |
62 { ngx_string("proxy_pass_error_message"), | |
63 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, | |
64 ngx_conf_set_flag_slot, | |
65 NGX_MAIL_SRV_CONF_OFFSET, | |
66 offsetof(ngx_mail_proxy_conf_t, pass_error_message), | |
67 NULL }, | |
68 | |
69 { ngx_string("xclient"), | |
70 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, | |
71 ngx_conf_set_flag_slot, | |
72 NGX_MAIL_SRV_CONF_OFFSET, | |
73 offsetof(ngx_mail_proxy_conf_t, xclient), | |
74 NULL }, | |
75 | |
76 ngx_null_command | |
77 }; | |
78 | |
79 | |
80 static ngx_mail_module_t ngx_mail_proxy_module_ctx = { | |
336 | 81 NULL, /* protocol */ |
82 | |
290 | 83 NULL, /* create main configuration */ |
84 NULL, /* init main configuration */ | |
85 | |
86 ngx_mail_proxy_create_conf, /* create server configuration */ | |
87 ngx_mail_proxy_merge_conf /* merge server configuration */ | |
88 }; | |
89 | |
90 | |
91 ngx_module_t ngx_mail_proxy_module = { | |
92 NGX_MODULE_V1, | |
93 &ngx_mail_proxy_module_ctx, /* module context */ | |
94 ngx_mail_proxy_commands, /* module directives */ | |
95 NGX_MAIL_MODULE, /* module type */ | |
96 NULL, /* init master */ | |
97 NULL, /* init module */ | |
98 NULL, /* init process */ | |
99 NULL, /* init thread */ | |
100 NULL, /* exit thread */ | |
101 NULL, /* exit process */ | |
102 NULL, /* exit master */ | |
103 NGX_MODULE_V1_PADDING | |
104 }; | |
105 | |
106 | |
473 | 107 static u_char smtp_auth_ok[] = "235 2.0.0 OK" CRLF; |
290 | 108 |
109 | |
110 void | |
111 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer) | |
112 { | |
113 int keepalive; | |
114 ngx_int_t rc; | |
115 ngx_mail_proxy_ctx_t *p; | |
116 ngx_mail_proxy_conf_t *pcf; | |
117 ngx_mail_core_srv_conf_t *cscf; | |
118 | |
119 s->connection->log->action = "connecting to upstream"; | |
120 | |
121 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
122 | |
123 if (cscf->so_keepalive) { | |
124 keepalive = 1; | |
125 | |
126 if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE, | |
127 (const void *) &keepalive, sizeof(int)) | |
128 == -1) | |
129 { | |
130 ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno, | |
131 "setsockopt(SO_KEEPALIVE) failed"); | |
132 } | |
133 } | |
134 | |
135 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t)); | |
136 if (p == NULL) { | |
137 ngx_mail_session_internal_server_error(s); | |
138 return; | |
139 } | |
140 | |
141 s->proxy = p; | |
142 | |
143 p->upstream.sockaddr = peer->sockaddr; | |
144 p->upstream.socklen = peer->socklen; | |
145 p->upstream.name = &peer->name; | |
146 p->upstream.get = ngx_event_get_peer; | |
147 p->upstream.log = s->connection->log; | |
148 p->upstream.log_error = NGX_ERROR_ERR; | |
149 | |
150 rc = ngx_event_connect_peer(&p->upstream); | |
151 | |
152 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { | |
153 ngx_mail_proxy_internal_server_error(s); | |
154 return; | |
155 } | |
156 | |
157 ngx_add_timer(p->upstream.connection->read, cscf->timeout); | |
158 | |
159 p->upstream.connection->data = s; | |
160 p->upstream.connection->pool = s->connection->pool; | |
161 | |
162 s->connection->read->handler = ngx_mail_proxy_block_read; | |
163 p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler; | |
164 | |
165 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
166 | |
167 s->proxy->buffer = ngx_create_temp_buf(s->connection->pool, | |
168 pcf->buffer_size); | |
169 if (s->proxy->buffer == NULL) { | |
170 ngx_mail_proxy_internal_server_error(s); | |
171 return; | |
172 } | |
173 | |
372 | 174 s->out.len = 0; |
175 | |
290 | 176 switch (s->protocol) { |
177 | |
178 case NGX_MAIL_POP3_PROTOCOL: | |
179 p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler; | |
180 s->mail_state = ngx_pop3_start; | |
181 break; | |
182 | |
183 case NGX_MAIL_IMAP_PROTOCOL: | |
184 p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler; | |
185 s->mail_state = ngx_imap_start; | |
186 break; | |
187 | |
188 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
189 p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler; | |
190 s->mail_state = ngx_smtp_start; | |
191 break; | |
192 } | |
193 } | |
194 | |
195 | |
196 static void | |
197 ngx_mail_proxy_block_read(ngx_event_t *rev) | |
198 { | |
199 ngx_connection_t *c; | |
200 ngx_mail_session_t *s; | |
201 | |
202 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read"); | |
203 | |
459 | 204 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
290 | 205 c = rev->data; |
206 s = c->data; | |
207 | |
208 ngx_mail_proxy_close_session(s); | |
209 } | |
210 } | |
211 | |
212 | |
213 static void | |
214 ngx_mail_proxy_pop3_handler(ngx_event_t *rev) | |
215 { | |
216 u_char *p; | |
217 ngx_int_t rc; | |
218 ngx_str_t line; | |
219 ngx_connection_t *c; | |
220 ngx_mail_session_t *s; | |
221 ngx_mail_proxy_conf_t *pcf; | |
222 | |
223 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
224 "mail proxy pop3 auth handler"); | |
225 | |
226 c = rev->data; | |
227 s = c->data; | |
228 | |
229 if (rev->timedout) { | |
230 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
231 "upstream timed out"); | |
232 c->timedout = 1; | |
233 ngx_mail_proxy_internal_server_error(s); | |
234 return; | |
235 } | |
236 | |
237 rc = ngx_mail_proxy_read_response(s, 0); | |
238 | |
239 if (rc == NGX_AGAIN) { | |
240 return; | |
241 } | |
242 | |
243 if (rc == NGX_ERROR) { | |
244 ngx_mail_proxy_upstream_error(s); | |
245 return; | |
246 } | |
247 | |
248 switch (s->mail_state) { | |
249 | |
250 case ngx_pop3_start: | |
251 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user"); | |
252 | |
253 s->connection->log->action = "sending user name to upstream"; | |
254 | |
255 line.len = sizeof("USER ") - 1 + s->login.len + 2; | |
382 | 256 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 257 if (line.data == NULL) { |
258 ngx_mail_proxy_internal_server_error(s); | |
259 return; | |
260 } | |
261 | |
262 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1); | |
263 p = ngx_cpymem(p, s->login.data, s->login.len); | |
264 *p++ = CR; *p = LF; | |
265 | |
266 s->mail_state = ngx_pop3_user; | |
267 break; | |
268 | |
269 case ngx_pop3_user: | |
270 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass"); | |
271 | |
272 s->connection->log->action = "sending password to upstream"; | |
273 | |
274 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2; | |
382 | 275 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 276 if (line.data == NULL) { |
277 ngx_mail_proxy_internal_server_error(s); | |
278 return; | |
279 } | |
280 | |
281 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1); | |
282 p = ngx_cpymem(p, s->passwd.data, s->passwd.len); | |
283 *p++ = CR; *p = LF; | |
284 | |
285 s->mail_state = ngx_pop3_passwd; | |
286 break; | |
287 | |
288 case ngx_pop3_passwd: | |
289 s->connection->read->handler = ngx_mail_proxy_handler; | |
290 s->connection->write->handler = ngx_mail_proxy_handler; | |
291 rev->handler = ngx_mail_proxy_handler; | |
292 c->write->handler = ngx_mail_proxy_handler; | |
293 | |
294 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
295 ngx_add_timer(s->connection->read, pcf->timeout); | |
296 ngx_del_timer(c->read); | |
297 | |
298 c->log->action = NULL; | |
299 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
300 | |
301 ngx_mail_proxy_handler(s->connection->write); | |
302 | |
303 return; | |
304 | |
305 default: | |
306 #if (NGX_SUPPRESS_WARN) | |
307 line.len = 0; | |
308 line.data = NULL; | |
309 #endif | |
310 break; | |
311 } | |
312 | |
313 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { | |
314 /* | |
315 * we treat the incomplete sending as NGX_ERROR | |
316 * because it is very strange here | |
317 */ | |
318 ngx_mail_proxy_internal_server_error(s); | |
319 return; | |
320 } | |
321 | |
322 s->proxy->buffer->pos = s->proxy->buffer->start; | |
323 s->proxy->buffer->last = s->proxy->buffer->start; | |
324 } | |
325 | |
326 | |
327 static void | |
328 ngx_mail_proxy_imap_handler(ngx_event_t *rev) | |
329 { | |
330 u_char *p; | |
331 ngx_int_t rc; | |
332 ngx_str_t line; | |
333 ngx_connection_t *c; | |
334 ngx_mail_session_t *s; | |
335 ngx_mail_proxy_conf_t *pcf; | |
336 | |
337 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
338 "mail proxy imap auth handler"); | |
339 | |
340 c = rev->data; | |
341 s = c->data; | |
342 | |
343 if (rev->timedout) { | |
344 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
345 "upstream timed out"); | |
346 c->timedout = 1; | |
347 ngx_mail_proxy_internal_server_error(s); | |
348 return; | |
349 } | |
350 | |
351 rc = ngx_mail_proxy_read_response(s, s->mail_state); | |
352 | |
353 if (rc == NGX_AGAIN) { | |
354 return; | |
355 } | |
356 | |
357 if (rc == NGX_ERROR) { | |
358 ngx_mail_proxy_upstream_error(s); | |
359 return; | |
360 } | |
361 | |
362 switch (s->mail_state) { | |
363 | |
364 case ngx_imap_start: | |
365 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
366 "mail proxy send login"); | |
367 | |
368 s->connection->log->action = "sending LOGIN command to upstream"; | |
369 | |
370 line.len = s->tag.len + sizeof("LOGIN ") - 1 | |
371 + 1 + NGX_SIZE_T_LEN + 1 + 2; | |
382 | 372 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 373 if (line.data == NULL) { |
374 ngx_mail_proxy_internal_server_error(s); | |
375 return; | |
376 } | |
377 | |
378 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF, | |
379 &s->tag, s->login.len) | |
380 - line.data; | |
381 | |
382 s->mail_state = ngx_imap_login; | |
383 break; | |
384 | |
385 case ngx_imap_login: | |
386 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user"); | |
387 | |
388 s->connection->log->action = "sending user name to upstream"; | |
389 | |
390 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2; | |
382 | 391 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 392 if (line.data == NULL) { |
393 ngx_mail_proxy_internal_server_error(s); | |
394 return; | |
395 } | |
396 | |
397 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF, | |
398 &s->login, s->passwd.len) | |
399 - line.data; | |
400 | |
401 s->mail_state = ngx_imap_user; | |
402 break; | |
403 | |
404 case ngx_imap_user: | |
405 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
406 "mail proxy send passwd"); | |
407 | |
408 s->connection->log->action = "sending password to upstream"; | |
409 | |
410 line.len = s->passwd.len + 2; | |
382 | 411 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 412 if (line.data == NULL) { |
413 ngx_mail_proxy_internal_server_error(s); | |
414 return; | |
415 } | |
416 | |
417 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len); | |
418 *p++ = CR; *p = LF; | |
419 | |
420 s->mail_state = ngx_imap_passwd; | |
421 break; | |
422 | |
423 case ngx_imap_passwd: | |
424 s->connection->read->handler = ngx_mail_proxy_handler; | |
425 s->connection->write->handler = ngx_mail_proxy_handler; | |
426 rev->handler = ngx_mail_proxy_handler; | |
427 c->write->handler = ngx_mail_proxy_handler; | |
428 | |
429 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
430 ngx_add_timer(s->connection->read, pcf->timeout); | |
431 ngx_del_timer(c->read); | |
432 | |
433 c->log->action = NULL; | |
434 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
435 | |
436 ngx_mail_proxy_handler(s->connection->write); | |
437 | |
438 return; | |
439 | |
440 default: | |
441 #if (NGX_SUPPRESS_WARN) | |
442 line.len = 0; | |
443 line.data = NULL; | |
444 #endif | |
445 break; | |
446 } | |
447 | |
448 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { | |
449 /* | |
450 * we treat the incomplete sending as NGX_ERROR | |
451 * because it is very strange here | |
452 */ | |
453 ngx_mail_proxy_internal_server_error(s); | |
454 return; | |
455 } | |
456 | |
457 s->proxy->buffer->pos = s->proxy->buffer->start; | |
458 s->proxy->buffer->last = s->proxy->buffer->start; | |
459 } | |
460 | |
461 | |
462 static void | |
463 ngx_mail_proxy_smtp_handler(ngx_event_t *rev) | |
464 { | |
465 u_char *p; | |
466 ngx_int_t rc; | |
467 ngx_str_t line; | |
449 | 468 ngx_buf_t *b; |
290 | 469 ngx_connection_t *c; |
470 ngx_mail_session_t *s; | |
471 ngx_mail_proxy_conf_t *pcf; | |
472 ngx_mail_core_srv_conf_t *cscf; | |
473 | |
474 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
475 "mail proxy smtp auth handler"); | |
476 | |
477 c = rev->data; | |
478 s = c->data; | |
479 | |
480 if (rev->timedout) { | |
481 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
482 "upstream timed out"); | |
483 c->timedout = 1; | |
484 ngx_mail_proxy_internal_server_error(s); | |
485 return; | |
486 } | |
487 | |
488 rc = ngx_mail_proxy_read_response(s, s->mail_state); | |
489 | |
490 if (rc == NGX_AGAIN) { | |
491 return; | |
492 } | |
493 | |
494 if (rc == NGX_ERROR) { | |
495 ngx_mail_proxy_upstream_error(s); | |
496 return; | |
497 } | |
498 | |
499 switch (s->mail_state) { | |
500 | |
501 case ngx_smtp_start: | |
502 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo"); | |
503 | |
504 s->connection->log->action = "sending HELO/EHLO to upstream"; | |
505 | |
506 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
507 | |
508 line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2; | |
382 | 509 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 510 if (line.data == NULL) { |
511 ngx_mail_proxy_internal_server_error(s); | |
512 return; | |
513 } | |
514 | |
515 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
516 | |
517 p = ngx_cpymem(line.data, | |
518 ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "), | |
519 sizeof("HELO ") - 1); | |
520 | |
521 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len); | |
522 *p++ = CR; *p = LF; | |
523 | |
449 | 524 if (pcf->xclient) { |
525 s->mail_state = ngx_smtp_helo_xclient; | |
526 | |
527 } else if (s->auth_method == NGX_MAIL_AUTH_NONE) { | |
528 s->mail_state = ngx_smtp_helo_from; | |
529 | |
530 } else { | |
531 s->mail_state = ngx_smtp_helo; | |
532 } | |
290 | 533 |
534 break; | |
535 | |
449 | 536 case ngx_smtp_helo_xclient: |
290 | 537 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, |
538 "mail proxy send xclient"); | |
539 | |
540 s->connection->log->action = "sending XCLIENT to upstream"; | |
541 | |
482 | 542 line.len = sizeof("XCLIENT ADDR= LOGIN= NAME=" |
366 | 543 CRLF) - 1 |
544 + s->connection->addr_text.len + s->login.len + s->host.len; | |
290 | 545 |
382 | 546 line.data = ngx_pnalloc(c->pool, line.len); |
290 | 547 if (line.data == NULL) { |
548 ngx_mail_proxy_internal_server_error(s); | |
549 return; | |
550 } | |
551 | |
449 | 552 line.len = ngx_sprintf(line.data, |
482 | 553 "XCLIENT ADDR=%V%s%V NAME=%V" CRLF, |
449 | 554 &s->connection->addr_text, |
555 (s->login.len ? " LOGIN=" : ""), &s->login, &s->host) | |
556 - line.data; | |
557 | |
482 | 558 if (s->smtp_helo.len) { |
559 s->mail_state = ngx_smtp_xclient_helo; | |
560 | |
561 } else if (s->auth_method == NGX_MAIL_AUTH_NONE) { | |
562 s->mail_state = ngx_smtp_xclient_from; | |
563 | |
564 } else { | |
565 s->mail_state = ngx_smtp_xclient; | |
566 } | |
567 | |
568 break; | |
569 | |
570 case ngx_smtp_xclient_helo: | |
571 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
572 "mail proxy send client ehlo"); | |
573 | |
574 s->connection->log->action = "sending client HELO/EHLO to upstream"; | |
575 | |
576 line.len = sizeof("HELO " CRLF) - 1 + s->smtp_helo.len; | |
577 | |
578 line.data = ngx_pnalloc(c->pool, line.len); | |
579 if (line.data == NULL) { | |
580 ngx_mail_proxy_internal_server_error(s); | |
581 return; | |
582 } | |
583 | |
584 line.len = ngx_sprintf(line.data, | |
585 ((s->esmtp) ? "EHLO %V" CRLF : "HELO %V" CRLF), | |
586 &s->smtp_helo) | |
587 - line.data; | |
588 | |
449 | 589 s->mail_state = (s->auth_method == NGX_MAIL_AUTH_NONE) ? |
482 | 590 ngx_smtp_helo_from : ngx_smtp_helo; |
449 | 591 |
592 break; | |
593 | |
594 case ngx_smtp_helo_from: | |
595 case ngx_smtp_xclient_from: | |
596 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
597 "mail proxy send mail from"); | |
598 | |
599 s->connection->log->action = "sending MAIL FROM to upstream"; | |
600 | |
601 line.len = s->smtp_from.len + sizeof(CRLF) - 1; | |
602 line.data = ngx_pnalloc(c->pool, line.len); | |
603 if (line.data == NULL) { | |
604 ngx_mail_proxy_internal_server_error(s); | |
605 return; | |
290 | 606 } |
607 | |
449 | 608 p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len); |
609 *p++ = CR; *p = LF; | |
610 | |
611 s->mail_state = ngx_smtp_from; | |
612 | |
290 | 613 break; |
614 | |
449 | 615 case ngx_smtp_from: |
616 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
617 "mail proxy send rcpt to"); | |
618 | |
619 s->connection->log->action = "sending RCPT TO to upstream"; | |
620 | |
621 line.len = s->smtp_to.len + sizeof(CRLF) - 1; | |
622 line.data = ngx_pnalloc(c->pool, line.len); | |
623 if (line.data == NULL) { | |
624 ngx_mail_proxy_internal_server_error(s); | |
625 return; | |
626 } | |
627 | |
628 p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len); | |
629 *p++ = CR; *p = LF; | |
290 | 630 |
449 | 631 s->mail_state = ngx_smtp_to; |
632 | |
633 break; | |
634 | |
635 case ngx_smtp_helo: | |
636 case ngx_smtp_xclient: | |
637 case ngx_smtp_to: | |
290 | 638 |
449 | 639 b = s->proxy->buffer; |
640 | |
641 if (s->auth_method == NGX_MAIL_AUTH_NONE) { | |
642 b->pos = b->start; | |
643 | |
644 } else { | |
473 | 645 ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1); |
646 b->last = b->start + sizeof(smtp_auth_ok) - 1; | |
449 | 647 } |
290 | 648 |
649 s->connection->read->handler = ngx_mail_proxy_handler; | |
650 s->connection->write->handler = ngx_mail_proxy_handler; | |
651 rev->handler = ngx_mail_proxy_handler; | |
652 c->write->handler = ngx_mail_proxy_handler; | |
653 | |
654 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
655 ngx_add_timer(s->connection->read, pcf->timeout); | |
656 ngx_del_timer(c->read); | |
657 | |
658 c->log->action = NULL; | |
659 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
660 | |
436
9b19e26b2660
Mail: smtp pipelining support.
Maxim Dounin <mdounin@mdounin.ru>
parents:
434
diff
changeset
|
661 if (s->buffer->pos == s->buffer->last) { |
9b19e26b2660
Mail: smtp pipelining support.
Maxim Dounin <mdounin@mdounin.ru>
parents:
434
diff
changeset
|
662 ngx_mail_proxy_handler(s->connection->write); |
9b19e26b2660
Mail: smtp pipelining support.
Maxim Dounin <mdounin@mdounin.ru>
parents:
434
diff
changeset
|
663 |
9b19e26b2660
Mail: smtp pipelining support.
Maxim Dounin <mdounin@mdounin.ru>
parents:
434
diff
changeset
|
664 } else { |
9b19e26b2660
Mail: smtp pipelining support.
Maxim Dounin <mdounin@mdounin.ru>
parents:
434
diff
changeset
|
665 ngx_mail_proxy_handler(c->write); |
9b19e26b2660
Mail: smtp pipelining support.
Maxim Dounin <mdounin@mdounin.ru>
parents:
434
diff
changeset
|
666 } |
290 | 667 |
668 return; | |
669 | |
670 default: | |
671 #if (NGX_SUPPRESS_WARN) | |
672 line.len = 0; | |
673 line.data = NULL; | |
674 #endif | |
675 break; | |
676 } | |
677 | |
678 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { | |
679 /* | |
680 * we treat the incomplete sending as NGX_ERROR | |
681 * because it is very strange here | |
682 */ | |
683 ngx_mail_proxy_internal_server_error(s); | |
684 return; | |
685 } | |
686 | |
687 s->proxy->buffer->pos = s->proxy->buffer->start; | |
688 s->proxy->buffer->last = s->proxy->buffer->start; | |
689 } | |
690 | |
691 | |
692 static void | |
693 ngx_mail_proxy_dummy_handler(ngx_event_t *wev) | |
694 { | |
695 ngx_connection_t *c; | |
696 ngx_mail_session_t *s; | |
697 | |
698 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler"); | |
699 | |
459 | 700 if (ngx_handle_write_event(wev, 0) != NGX_OK) { |
290 | 701 c = wev->data; |
702 s = c->data; | |
703 | |
704 ngx_mail_proxy_close_session(s); | |
705 } | |
706 } | |
707 | |
708 | |
709 static ngx_int_t | |
710 ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state) | |
711 { | |
712 u_char *p; | |
713 ssize_t n; | |
714 ngx_buf_t *b; | |
715 ngx_mail_proxy_conf_t *pcf; | |
716 | |
717 s->connection->log->action = "reading response from upstream"; | |
718 | |
719 b = s->proxy->buffer; | |
720 | |
721 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection, | |
722 b->last, b->end - b->last); | |
723 | |
724 if (n == NGX_ERROR || n == 0) { | |
725 return NGX_ERROR; | |
726 } | |
727 | |
728 if (n == NGX_AGAIN) { | |
729 return NGX_AGAIN; | |
730 } | |
731 | |
732 b->last += n; | |
733 | |
734 if (b->last - b->pos < 5) { | |
735 return NGX_AGAIN; | |
736 } | |
737 | |
738 if (*(b->last - 2) != CR || *(b->last - 1) != LF) { | |
739 if (b->last == b->end) { | |
740 *(b->last - 1) = '\0'; | |
741 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
742 "upstream sent too long response line: \"%s\"", | |
743 b->pos); | |
744 return NGX_ERROR; | |
745 } | |
746 | |
747 return NGX_AGAIN; | |
748 } | |
749 | |
750 p = b->pos; | |
751 | |
752 switch (s->protocol) { | |
753 | |
754 case NGX_MAIL_POP3_PROTOCOL: | |
755 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') { | |
756 return NGX_OK; | |
757 } | |
758 break; | |
759 | |
760 case NGX_MAIL_IMAP_PROTOCOL: | |
761 switch (state) { | |
762 | |
763 case ngx_imap_start: | |
764 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') { | |
765 return NGX_OK; | |
766 } | |
767 break; | |
768 | |
769 case ngx_imap_login: | |
770 case ngx_imap_user: | |
771 if (p[0] == '+') { | |
772 return NGX_OK; | |
773 } | |
774 break; | |
775 | |
776 case ngx_imap_passwd: | |
777 if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) { | |
778 p += s->tag.len; | |
779 if (p[0] == 'O' && p[1] == 'K') { | |
780 return NGX_OK; | |
781 } | |
782 } | |
783 break; | |
784 } | |
785 | |
786 break; | |
787 | |
788 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
789 switch (state) { | |
790 | |
449 | 791 case ngx_smtp_start: |
792 if (p[0] == '2' && p[1] == '2' && p[2] == '0') { | |
793 return NGX_OK; | |
794 } | |
795 break; | |
796 | |
290 | 797 case ngx_smtp_helo: |
449 | 798 case ngx_smtp_helo_xclient: |
799 case ngx_smtp_helo_from: | |
800 case ngx_smtp_from: | |
296 | 801 if (p[0] == '2' && p[1] == '5' && p[2] == '0') { |
802 return NGX_OK; | |
803 } | |
804 break; | |
290 | 805 |
806 case ngx_smtp_xclient: | |
449 | 807 case ngx_smtp_xclient_from: |
482 | 808 case ngx_smtp_xclient_helo: |
449 | 809 if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') { |
296 | 810 return NGX_OK; |
811 } | |
812 break; | |
449 | 813 |
814 case ngx_smtp_to: | |
815 return NGX_OK; | |
290 | 816 } |
817 | |
296 | 818 break; |
290 | 819 } |
820 | |
821 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
822 | |
823 if (pcf->pass_error_message == 0) { | |
824 *(b->last - 2) = '\0'; | |
825 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
826 "upstream sent invalid response: \"%s\"", p); | |
827 return NGX_ERROR; | |
828 } | |
829 | |
830 s->out.len = b->last - p - 2; | |
831 s->out.data = p; | |
832 | |
833 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, | |
834 "upstream sent invalid response: \"%V\"", &s->out); | |
835 | |
836 s->out.len = b->last - b->pos; | |
837 s->out.data = b->pos; | |
838 | |
839 return NGX_ERROR; | |
840 } | |
841 | |
842 | |
843 static void | |
844 ngx_mail_proxy_handler(ngx_event_t *ev) | |
845 { | |
846 char *action, *recv_action, *send_action; | |
847 size_t size; | |
848 ssize_t n; | |
849 ngx_buf_t *b; | |
850 ngx_uint_t do_write; | |
851 ngx_connection_t *c, *src, *dst; | |
852 ngx_mail_session_t *s; | |
853 ngx_mail_proxy_conf_t *pcf; | |
854 | |
855 c = ev->data; | |
856 s = c->data; | |
857 | |
858 if (ev->timedout) { | |
859 c->log->action = "proxying"; | |
860 | |
861 if (c == s->connection) { | |
862 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
863 "client timed out"); | |
864 c->timedout = 1; | |
865 | |
866 } else { | |
867 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
868 "upstream timed out"); | |
869 } | |
870 | |
871 ngx_mail_proxy_close_session(s); | |
872 return; | |
873 } | |
874 | |
875 if (c == s->connection) { | |
876 if (ev->write) { | |
877 recv_action = "proxying and reading from upstream"; | |
878 send_action = "proxying and sending to client"; | |
879 src = s->proxy->upstream.connection; | |
880 dst = c; | |
881 b = s->proxy->buffer; | |
882 | |
883 } else { | |
884 recv_action = "proxying and reading from client"; | |
885 send_action = "proxying and sending to upstream"; | |
886 src = c; | |
887 dst = s->proxy->upstream.connection; | |
888 b = s->buffer; | |
889 } | |
890 | |
891 } else { | |
892 if (ev->write) { | |
893 recv_action = "proxying and reading from client"; | |
894 send_action = "proxying and sending to upstream"; | |
895 src = s->connection; | |
896 dst = c; | |
897 b = s->buffer; | |
898 | |
899 } else { | |
900 recv_action = "proxying and reading from upstream"; | |
901 send_action = "proxying and sending to client"; | |
902 src = c; | |
903 dst = s->connection; | |
904 b = s->proxy->buffer; | |
905 } | |
906 } | |
907 | |
908 do_write = ev->write ? 1 : 0; | |
909 | |
910 ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0, | |
911 "mail proxy handler: %d, #%d > #%d", | |
912 do_write, src->fd, dst->fd); | |
913 | |
914 for ( ;; ) { | |
915 | |
916 if (do_write) { | |
917 | |
918 size = b->last - b->pos; | |
919 | |
920 if (size && dst->write->ready) { | |
921 c->log->action = send_action; | |
922 | |
923 n = dst->send(dst, b->pos, size); | |
924 | |
925 if (n == NGX_ERROR) { | |
926 ngx_mail_proxy_close_session(s); | |
927 return; | |
928 } | |
929 | |
930 if (n > 0) { | |
931 b->pos += n; | |
932 | |
933 if (b->pos == b->last) { | |
934 b->pos = b->start; | |
935 b->last = b->start; | |
936 } | |
937 } | |
938 } | |
939 } | |
940 | |
941 size = b->end - b->last; | |
942 | |
943 if (size && src->read->ready) { | |
944 c->log->action = recv_action; | |
945 | |
946 n = src->recv(src, b->last, size); | |
947 | |
948 if (n == NGX_AGAIN || n == 0) { | |
949 break; | |
950 } | |
951 | |
952 if (n > 0) { | |
953 do_write = 1; | |
954 b->last += n; | |
955 | |
956 continue; | |
957 } | |
958 | |
959 if (n == NGX_ERROR) { | |
960 src->read->eof = 1; | |
961 } | |
962 } | |
963 | |
964 break; | |
965 } | |
966 | |
967 c->log->action = "proxying"; | |
968 | |
324 | 969 if ((s->connection->read->eof && s->buffer->pos == s->buffer->last) |
970 || (s->proxy->upstream.connection->read->eof | |
971 && s->proxy->buffer->pos == s->proxy->buffer->last) | |
972 || (s->connection->read->eof | |
973 && s->proxy->upstream.connection->read->eof)) | |
290 | 974 { |
975 action = c->log->action; | |
976 c->log->action = NULL; | |
977 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done"); | |
978 c->log->action = action; | |
979 | |
980 ngx_mail_proxy_close_session(s); | |
981 return; | |
982 } | |
983 | |
459 | 984 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) { |
290 | 985 ngx_mail_proxy_close_session(s); |
986 return; | |
987 } | |
988 | |
459 | 989 if (ngx_handle_read_event(dst->read, 0) != NGX_OK) { |
290 | 990 ngx_mail_proxy_close_session(s); |
991 return; | |
992 } | |
993 | |
459 | 994 if (ngx_handle_write_event(src->write, 0) != NGX_OK) { |
290 | 995 ngx_mail_proxy_close_session(s); |
996 return; | |
997 } | |
998 | |
459 | 999 if (ngx_handle_read_event(src->read, 0) != NGX_OK) { |
290 | 1000 ngx_mail_proxy_close_session(s); |
1001 return; | |
1002 } | |
1003 | |
1004 if (c == s->connection) { | |
1005 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
1006 ngx_add_timer(c->read, pcf->timeout); | |
1007 } | |
1008 } | |
1009 | |
1010 | |
1011 static void | |
1012 ngx_mail_proxy_upstream_error(ngx_mail_session_t *s) | |
1013 { | |
1014 if (s->proxy->upstream.connection) { | |
1015 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
1016 "close mail proxy connection: %d", | |
1017 s->proxy->upstream.connection->fd); | |
1018 | |
1019 ngx_close_connection(s->proxy->upstream.connection); | |
1020 } | |
1021 | |
1022 if (s->out.len == 0) { | |
1023 ngx_mail_session_internal_server_error(s); | |
1024 return; | |
1025 } | |
1026 | |
1027 s->quit = 1; | |
1028 ngx_mail_send(s->connection->write); | |
1029 } | |
1030 | |
1031 | |
1032 static void | |
1033 ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s) | |
1034 { | |
1035 if (s->proxy->upstream.connection) { | |
1036 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
1037 "close mail proxy connection: %d", | |
1038 s->proxy->upstream.connection->fd); | |
1039 | |
1040 ngx_close_connection(s->proxy->upstream.connection); | |
1041 } | |
1042 | |
1043 ngx_mail_session_internal_server_error(s); | |
1044 } | |
1045 | |
1046 | |
1047 static void | |
1048 ngx_mail_proxy_close_session(ngx_mail_session_t *s) | |
1049 { | |
1050 if (s->proxy->upstream.connection) { | |
1051 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
1052 "close mail proxy connection: %d", | |
1053 s->proxy->upstream.connection->fd); | |
1054 | |
1055 ngx_close_connection(s->proxy->upstream.connection); | |
1056 } | |
1057 | |
1058 ngx_mail_close_connection(s->connection); | |
1059 } | |
1060 | |
1061 | |
1062 static void * | |
1063 ngx_mail_proxy_create_conf(ngx_conf_t *cf) | |
1064 { | |
1065 ngx_mail_proxy_conf_t *pcf; | |
1066 | |
1067 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t)); | |
1068 if (pcf == NULL) { | |
536 | 1069 return NULL; |
290 | 1070 } |
1071 | |
1072 pcf->enable = NGX_CONF_UNSET; | |
1073 pcf->pass_error_message = NGX_CONF_UNSET; | |
1074 pcf->xclient = NGX_CONF_UNSET; | |
1075 pcf->buffer_size = NGX_CONF_UNSET_SIZE; | |
1076 pcf->timeout = NGX_CONF_UNSET_MSEC; | |
1077 | |
1078 return pcf; | |
1079 } | |
1080 | |
1081 | |
1082 static char * | |
1083 ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
1084 { | |
1085 ngx_mail_proxy_conf_t *prev = parent; | |
1086 ngx_mail_proxy_conf_t *conf = child; | |
1087 | |
1088 ngx_conf_merge_value(conf->enable, prev->enable, 0); | |
1089 ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0); | |
1090 ngx_conf_merge_value(conf->xclient, prev->xclient, 1); | |
1091 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, | |
1092 (size_t) ngx_pagesize); | |
1093 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000); | |
1094 | |
1095 return NGX_CONF_OK; | |
1096 } |