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