3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
10 #include <ngx_event_connect.h>
16 ngx_flag_t pass_error_message;
20 } ngx_mail_proxy_conf_t;
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,
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,
39 static ngx_command_t ngx_mail_proxy_commands[] = {
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),
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),
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),
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),
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),
80 static ngx_mail_module_t ngx_mail_proxy_module_ctx = {
83 NULL, /* create main configuration */
84 NULL, /* init main configuration */
86 ngx_mail_proxy_create_conf, /* create server configuration */
87 ngx_mail_proxy_merge_conf /* merge server configuration */
91 ngx_module_t ngx_mail_proxy_module = {
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
107 static u_char smtp_auth_ok[] = "235 2.0.0 OK" CRLF;
111 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer)
115 ngx_mail_proxy_ctx_t *p;
116 ngx_mail_proxy_conf_t *pcf;
117 ngx_mail_core_srv_conf_t *cscf;
119 s->connection->log->action = "connecting to upstream";
121 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
123 if (cscf->so_keepalive) {
126 if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE,
127 (const void *) &keepalive, sizeof(int))
130 ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno,
131 "setsockopt(SO_KEEPALIVE) failed");
135 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t));
137 ngx_mail_session_internal_server_error(s);
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;
150 rc = ngx_event_connect_peer(&p->upstream);
152 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
153 ngx_mail_proxy_internal_server_error(s);
157 ngx_add_timer(p->upstream.connection->read, cscf->timeout);
159 p->upstream.connection->data = s;
160 p->upstream.connection->pool = s->connection->pool;
162 s->connection->read->handler = ngx_mail_proxy_block_read;
163 p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler;
165 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
167 s->proxy->buffer = ngx_create_temp_buf(s->connection->pool,
169 if (s->proxy->buffer == NULL) {
170 ngx_mail_proxy_internal_server_error(s);
176 switch (s->protocol) {
178 case NGX_MAIL_POP3_PROTOCOL:
179 p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler;
180 s->mail_state = ngx_pop3_start;
183 case NGX_MAIL_IMAP_PROTOCOL:
184 p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler;
185 s->mail_state = ngx_imap_start;
188 default: /* NGX_MAIL_SMTP_PROTOCOL */
189 p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler;
190 s->mail_state = ngx_smtp_start;
197 ngx_mail_proxy_block_read(ngx_event_t *rev)
200 ngx_mail_session_t *s;
202 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read");
204 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
208 ngx_mail_proxy_close_session(s);
214 ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
220 ngx_mail_session_t *s;
221 ngx_mail_proxy_conf_t *pcf;
223 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
224 "mail proxy pop3 auth handler");
230 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
231 "upstream timed out");
233 ngx_mail_proxy_internal_server_error(s);
237 rc = ngx_mail_proxy_read_response(s, 0);
239 if (rc == NGX_AGAIN) {
243 if (rc == NGX_ERROR) {
244 ngx_mail_proxy_upstream_error(s);
248 switch (s->mail_state) {
251 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
253 s->connection->log->action = "sending user name to upstream";
255 line.len = sizeof("USER ") - 1 + s->login.len + 2;
256 line.data = ngx_pnalloc(c->pool, line.len);
257 if (line.data == NULL) {
258 ngx_mail_proxy_internal_server_error(s);
262 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
263 p = ngx_cpymem(p, s->login.data, s->login.len);
266 s->mail_state = ngx_pop3_user;
270 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass");
272 s->connection->log->action = "sending password to upstream";
274 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
275 line.data = ngx_pnalloc(c->pool, line.len);
276 if (line.data == NULL) {
277 ngx_mail_proxy_internal_server_error(s);
281 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
282 p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
285 s->mail_state = ngx_pop3_passwd;
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;
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);
298 c->log->action = NULL;
299 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
301 ngx_mail_proxy_handler(s->connection->write);
306 #if (NGX_SUPPRESS_WARN)
313 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
315 * we treat the incomplete sending as NGX_ERROR
316 * because it is very strange here
318 ngx_mail_proxy_internal_server_error(s);
322 s->proxy->buffer->pos = s->proxy->buffer->start;
323 s->proxy->buffer->last = s->proxy->buffer->start;
328 ngx_mail_proxy_imap_handler(ngx_event_t *rev)
334 ngx_mail_session_t *s;
335 ngx_mail_proxy_conf_t *pcf;
337 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
338 "mail proxy imap auth handler");
344 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
345 "upstream timed out");
347 ngx_mail_proxy_internal_server_error(s);
351 rc = ngx_mail_proxy_read_response(s, s->mail_state);
353 if (rc == NGX_AGAIN) {
357 if (rc == NGX_ERROR) {
358 ngx_mail_proxy_upstream_error(s);
362 switch (s->mail_state) {
365 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
366 "mail proxy send login");
368 s->connection->log->action = "sending LOGIN command to upstream";
370 line.len = s->tag.len + sizeof("LOGIN ") - 1
371 + 1 + NGX_SIZE_T_LEN + 1 + 2;
372 line.data = ngx_pnalloc(c->pool, line.len);
373 if (line.data == NULL) {
374 ngx_mail_proxy_internal_server_error(s);
378 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
379 &s->tag, s->login.len)
382 s->mail_state = ngx_imap_login;
386 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
388 s->connection->log->action = "sending user name to upstream";
390 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
391 line.data = ngx_pnalloc(c->pool, line.len);
392 if (line.data == NULL) {
393 ngx_mail_proxy_internal_server_error(s);
397 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
398 &s->login, s->passwd.len)
401 s->mail_state = ngx_imap_user;
405 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
406 "mail proxy send passwd");
408 s->connection->log->action = "sending password to upstream";
410 line.len = s->passwd.len + 2;
411 line.data = ngx_pnalloc(c->pool, line.len);
412 if (line.data == NULL) {
413 ngx_mail_proxy_internal_server_error(s);
417 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
420 s->mail_state = ngx_imap_passwd;
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;
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);
433 c->log->action = NULL;
434 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
436 ngx_mail_proxy_handler(s->connection->write);
441 #if (NGX_SUPPRESS_WARN)
448 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
450 * we treat the incomplete sending as NGX_ERROR
451 * because it is very strange here
453 ngx_mail_proxy_internal_server_error(s);
457 s->proxy->buffer->pos = s->proxy->buffer->start;
458 s->proxy->buffer->last = s->proxy->buffer->start;
463 ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
470 ngx_mail_session_t *s;
471 ngx_mail_proxy_conf_t *pcf;
472 ngx_mail_core_srv_conf_t *cscf;
474 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
475 "mail proxy smtp auth handler");
481 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
482 "upstream timed out");
484 ngx_mail_proxy_internal_server_error(s);
488 rc = ngx_mail_proxy_read_response(s, s->mail_state);
490 if (rc == NGX_AGAIN) {
494 if (rc == NGX_ERROR) {
495 ngx_mail_proxy_upstream_error(s);
499 switch (s->mail_state) {
502 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo");
504 s->connection->log->action = "sending HELO/EHLO to upstream";
506 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
508 line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2;
509 line.data = ngx_pnalloc(c->pool, line.len);
510 if (line.data == NULL) {
511 ngx_mail_proxy_internal_server_error(s);
515 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
517 p = ngx_cpymem(line.data,
518 ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "),
519 sizeof("HELO ") - 1);
521 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
525 s->mail_state = ngx_smtp_helo_xclient;
527 } else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
528 s->mail_state = ngx_smtp_helo_from;
531 s->mail_state = ngx_smtp_helo;
536 case ngx_smtp_helo_xclient:
537 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
538 "mail proxy send xclient");
540 s->connection->log->action = "sending XCLIENT to upstream";
542 line.len = sizeof("XCLIENT PROTO=SMTP HELO= ADDR= LOGIN= NAME="
544 + s->esmtp + s->smtp_helo.len
545 + s->connection->addr_text.len + s->login.len + s->host.len;
547 line.data = ngx_pnalloc(c->pool, line.len);
548 if (line.data == NULL) {
549 ngx_mail_proxy_internal_server_error(s);
553 line.len = ngx_sprintf(line.data,
554 "XCLIENT PROTO=%sSMTP%s%V ADDR=%V%s%V NAME=%V" CRLF,
555 (s->esmtp ? "E" : ""),
556 (s->smtp_helo.len ? " HELO=" : ""), &s->smtp_helo,
557 &s->connection->addr_text,
558 (s->login.len ? " LOGIN=" : ""), &s->login, &s->host)
561 s->mail_state = (s->auth_method == NGX_MAIL_AUTH_NONE) ?
562 ngx_smtp_xclient_from : ngx_smtp_xclient;
566 case ngx_smtp_helo_from:
567 case ngx_smtp_xclient_from:
568 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
569 "mail proxy send mail from");
571 s->connection->log->action = "sending MAIL FROM to upstream";
573 line.len = s->smtp_from.len + sizeof(CRLF) - 1;
574 line.data = ngx_pnalloc(c->pool, line.len);
575 if (line.data == NULL) {
576 ngx_mail_proxy_internal_server_error(s);
580 p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len);
583 s->mail_state = ngx_smtp_from;
588 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
589 "mail proxy send rcpt to");
591 s->connection->log->action = "sending RCPT TO to upstream";
593 line.len = s->smtp_to.len + sizeof(CRLF) - 1;
594 line.data = ngx_pnalloc(c->pool, line.len);
595 if (line.data == NULL) {
596 ngx_mail_proxy_internal_server_error(s);
600 p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len);
603 s->mail_state = ngx_smtp_to;
608 case ngx_smtp_xclient:
611 b = s->proxy->buffer;
613 if (s->auth_method == NGX_MAIL_AUTH_NONE) {
617 ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1);
618 b->last = b->start + sizeof(smtp_auth_ok) - 1;
621 s->connection->read->handler = ngx_mail_proxy_handler;
622 s->connection->write->handler = ngx_mail_proxy_handler;
623 rev->handler = ngx_mail_proxy_handler;
624 c->write->handler = ngx_mail_proxy_handler;
626 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
627 ngx_add_timer(s->connection->read, pcf->timeout);
628 ngx_del_timer(c->read);
630 c->log->action = NULL;
631 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
633 ngx_mail_proxy_handler(s->connection->write);
638 #if (NGX_SUPPRESS_WARN)
645 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
647 * we treat the incomplete sending as NGX_ERROR
648 * because it is very strange here
650 ngx_mail_proxy_internal_server_error(s);
654 s->proxy->buffer->pos = s->proxy->buffer->start;
655 s->proxy->buffer->last = s->proxy->buffer->start;
660 ngx_mail_proxy_dummy_handler(ngx_event_t *wev)
663 ngx_mail_session_t *s;
665 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler");
667 if (ngx_handle_write_event(wev, 0) != NGX_OK) {
671 ngx_mail_proxy_close_session(s);
677 ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state)
682 ngx_mail_proxy_conf_t *pcf;
684 s->connection->log->action = "reading response from upstream";
686 b = s->proxy->buffer;
688 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
689 b->last, b->end - b->last);
691 if (n == NGX_ERROR || n == 0) {
695 if (n == NGX_AGAIN) {
701 if (b->last - b->pos < 5) {
705 if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
706 if (b->last == b->end) {
707 *(b->last - 1) = '\0';
708 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
709 "upstream sent too long response line: \"%s\"",
719 switch (s->protocol) {
721 case NGX_MAIL_POP3_PROTOCOL:
722 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
727 case NGX_MAIL_IMAP_PROTOCOL:
731 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
743 case ngx_imap_passwd:
744 if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) {
746 if (p[0] == 'O' && p[1] == 'K') {
755 default: /* NGX_MAIL_SMTP_PROTOCOL */
759 if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
765 case ngx_smtp_helo_xclient:
766 case ngx_smtp_helo_from:
768 if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
773 case ngx_smtp_xclient:
774 case ngx_smtp_xclient_from:
775 if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') {
787 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
789 if (pcf->pass_error_message == 0) {
790 *(b->last - 2) = '\0';
791 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
792 "upstream sent invalid response: \"%s\"", p);
796 s->out.len = b->last - p - 2;
799 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
800 "upstream sent invalid response: \"%V\"", &s->out);
802 s->out.len = b->last - b->pos;
803 s->out.data = b->pos;
810 ngx_mail_proxy_handler(ngx_event_t *ev)
812 char *action, *recv_action, *send_action;
817 ngx_connection_t *c, *src, *dst;
818 ngx_mail_session_t *s;
819 ngx_mail_proxy_conf_t *pcf;
825 c->log->action = "proxying";
827 if (c == s->connection) {
828 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
833 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
834 "upstream timed out");
837 ngx_mail_proxy_close_session(s);
841 if (c == s->connection) {
843 recv_action = "proxying and reading from upstream";
844 send_action = "proxying and sending to client";
845 src = s->proxy->upstream.connection;
847 b = s->proxy->buffer;
850 recv_action = "proxying and reading from client";
851 send_action = "proxying and sending to upstream";
853 dst = s->proxy->upstream.connection;
859 recv_action = "proxying and reading from client";
860 send_action = "proxying and sending to upstream";
866 recv_action = "proxying and reading from upstream";
867 send_action = "proxying and sending to client";
870 b = s->proxy->buffer;
874 do_write = ev->write ? 1 : 0;
876 ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0,
877 "mail proxy handler: %d, #%d > #%d",
878 do_write, src->fd, dst->fd);
884 size = b->last - b->pos;
886 if (size && dst->write->ready) {
887 c->log->action = send_action;
889 n = dst->send(dst, b->pos, size);
891 if (n == NGX_ERROR) {
892 ngx_mail_proxy_close_session(s);
899 if (b->pos == b->last) {
907 size = b->end - b->last;
909 if (size && src->read->ready) {
910 c->log->action = recv_action;
912 n = src->recv(src, b->last, size);
914 if (n == NGX_AGAIN || n == 0) {
925 if (n == NGX_ERROR) {
933 c->log->action = "proxying";
935 if ((s->connection->read->eof && s->buffer->pos == s->buffer->last)
936 || (s->proxy->upstream.connection->read->eof
937 && s->proxy->buffer->pos == s->proxy->buffer->last)
938 || (s->connection->read->eof
939 && s->proxy->upstream.connection->read->eof))
941 action = c->log->action;
942 c->log->action = NULL;
943 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
944 c->log->action = action;
946 ngx_mail_proxy_close_session(s);
950 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
951 ngx_mail_proxy_close_session(s);
955 if (ngx_handle_read_event(dst->read, 0) != NGX_OK) {
956 ngx_mail_proxy_close_session(s);
960 if (ngx_handle_write_event(src->write, 0) != NGX_OK) {
961 ngx_mail_proxy_close_session(s);
965 if (ngx_handle_read_event(src->read, 0) != NGX_OK) {
966 ngx_mail_proxy_close_session(s);
970 if (c == s->connection) {
971 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
972 ngx_add_timer(c->read, pcf->timeout);
978 ngx_mail_proxy_upstream_error(ngx_mail_session_t *s)
980 if (s->proxy->upstream.connection) {
981 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
982 "close mail proxy connection: %d",
983 s->proxy->upstream.connection->fd);
985 ngx_close_connection(s->proxy->upstream.connection);
988 if (s->out.len == 0) {
989 ngx_mail_session_internal_server_error(s);
994 ngx_mail_send(s->connection->write);
999 ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s)
1001 if (s->proxy->upstream.connection) {
1002 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
1003 "close mail proxy connection: %d",
1004 s->proxy->upstream.connection->fd);
1006 ngx_close_connection(s->proxy->upstream.connection);
1009 ngx_mail_session_internal_server_error(s);
1014 ngx_mail_proxy_close_session(ngx_mail_session_t *s)
1016 if (s->proxy->upstream.connection) {
1017 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
1018 "close mail proxy connection: %d",
1019 s->proxy->upstream.connection->fd);
1021 ngx_close_connection(s->proxy->upstream.connection);
1024 ngx_mail_close_connection(s->connection);
1029 ngx_mail_proxy_create_conf(ngx_conf_t *cf)
1031 ngx_mail_proxy_conf_t *pcf;
1033 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t));
1035 return NGX_CONF_ERROR;
1038 pcf->enable = NGX_CONF_UNSET;
1039 pcf->pass_error_message = NGX_CONF_UNSET;
1040 pcf->xclient = NGX_CONF_UNSET;
1041 pcf->buffer_size = NGX_CONF_UNSET_SIZE;
1042 pcf->timeout = NGX_CONF_UNSET_MSEC;
1049 ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
1051 ngx_mail_proxy_conf_t *prev = parent;
1052 ngx_mail_proxy_conf_t *conf = child;
1054 ngx_conf_merge_value(conf->enable, prev->enable, 0);
1055 ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
1056 ngx_conf_merge_value(conf->xclient, prev->xclient, 1);
1057 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
1058 (size_t) ngx_pagesize);
1059 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);