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