src/mail/ngx_mail_proxy_module.c - nginx source code

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>
  8. #include <ngx_event_connect.h>
  9. #include <ngx_mail.h>


  10. typedef struct {
  11.     ngx_flag_t  enable;
  12.     ngx_flag_t  pass_error_message;
  13.     ngx_flag_t  xclient;
  14.     ngx_flag_t  smtp_auth;
  15.     ngx_flag_t  proxy_protocol;
  16.     size_t      buffer_size;
  17.     ngx_msec_t  timeout;
  18. } ngx_mail_proxy_conf_t;


  19. static void ngx_mail_proxy_block_read(ngx_event_t *rev);
  20. static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev);
  21. static void ngx_mail_proxy_imap_handler(ngx_event_t *rev);
  22. static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev);
  23. static void ngx_mail_proxy_write_handler(ngx_event_t *wev);
  24. static ngx_int_t ngx_mail_proxy_send_proxy_protocol(ngx_mail_session_t *s);
  25. static ngx_int_t ngx_mail_proxy_read_response(ngx_mail_session_t *s,
  26.     ngx_uint_t state);
  27. static void ngx_mail_proxy_handler(ngx_event_t *ev);
  28. static void ngx_mail_proxy_upstream_error(ngx_mail_session_t *s);
  29. static void ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s);
  30. static void ngx_mail_proxy_close_session(ngx_mail_session_t *s);
  31. static void *ngx_mail_proxy_create_conf(ngx_conf_t *cf);
  32. static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent,
  33.     void *child);


  34. static ngx_command_t  ngx_mail_proxy_commands[] = {

  35.     { ngx_string("proxy"),
  36.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
  37.       ngx_conf_set_flag_slot,
  38.       NGX_MAIL_SRV_CONF_OFFSET,
  39.       offsetof(ngx_mail_proxy_conf_t, enable),
  40.       NULL },

  41.     { ngx_string("proxy_buffer"),
  42.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  43.       ngx_conf_set_size_slot,
  44.       NGX_MAIL_SRV_CONF_OFFSET,
  45.       offsetof(ngx_mail_proxy_conf_t, buffer_size),
  46.       NULL },

  47.     { ngx_string("proxy_timeout"),
  48.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  49.       ngx_conf_set_msec_slot,
  50.       NGX_MAIL_SRV_CONF_OFFSET,
  51.       offsetof(ngx_mail_proxy_conf_t, timeout),
  52.       NULL },

  53.     { ngx_string("proxy_pass_error_message"),
  54.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
  55.       ngx_conf_set_flag_slot,
  56.       NGX_MAIL_SRV_CONF_OFFSET,
  57.       offsetof(ngx_mail_proxy_conf_t, pass_error_message),
  58.       NULL },

  59.     { ngx_string("xclient"),
  60.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
  61.       ngx_conf_set_flag_slot,
  62.       NGX_MAIL_SRV_CONF_OFFSET,
  63.       offsetof(ngx_mail_proxy_conf_t, xclient),
  64.       NULL },

  65.     { ngx_string("proxy_smtp_auth"),
  66.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
  67.       ngx_conf_set_flag_slot,
  68.       NGX_MAIL_SRV_CONF_OFFSET,
  69.       offsetof(ngx_mail_proxy_conf_t, smtp_auth),
  70.       NULL },

  71.     { ngx_string("proxy_protocol"),
  72.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
  73.       ngx_conf_set_flag_slot,
  74.       NGX_MAIL_SRV_CONF_OFFSET,
  75.       offsetof(ngx_mail_proxy_conf_t, proxy_protocol),
  76.       NULL },

  77.       ngx_null_command
  78. };


  79. static ngx_mail_module_t  ngx_mail_proxy_module_ctx = {
  80.     NULL,                                  /* protocol */

  81.     NULL,                                  /* create main configuration */
  82.     NULL,                                  /* init main configuration */

  83.     ngx_mail_proxy_create_conf,            /* create server configuration */
  84.     ngx_mail_proxy_merge_conf              /* merge server configuration */
  85. };


  86. ngx_module_t  ngx_mail_proxy_module = {
  87.     NGX_MODULE_V1,
  88.     &ngx_mail_proxy_module_ctx,            /* module context */
  89.     ngx_mail_proxy_commands,               /* module directives */
  90.     NGX_MAIL_MODULE,                       /* module type */
  91.     NULL,                                  /* init master */
  92.     NULL,                                  /* init module */
  93.     NULL,                                  /* init process */
  94.     NULL,                                  /* init thread */
  95.     NULL,                                  /* exit thread */
  96.     NULL,                                  /* exit process */
  97.     NULL,                                  /* exit master */
  98.     NGX_MODULE_V1_PADDING
  99. };


  100. static u_char  smtp_auth_ok[] = "235 2.0.0 OK" CRLF;


  101. void
  102. ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer)
  103. {
  104.     ngx_int_t                  rc;
  105.     ngx_mail_proxy_ctx_t      *p;
  106.     ngx_mail_proxy_conf_t     *pcf;
  107.     ngx_mail_core_srv_conf_t  *cscf;

  108.     s->connection->log->action = "connecting to upstream";

  109.     cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);

  110.     p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t));
  111.     if (p == NULL) {
  112.         ngx_mail_session_internal_server_error(s);
  113.         return;
  114.     }

  115.     s->proxy = p;

  116.     p->upstream.sockaddr = peer->sockaddr;
  117.     p->upstream.socklen = peer->socklen;
  118.     p->upstream.name = &peer->name;
  119.     p->upstream.get = ngx_event_get_peer;
  120.     p->upstream.log = s->connection->log;
  121.     p->upstream.log_error = NGX_ERROR_ERR;

  122.     rc = ngx_event_connect_peer(&p->upstream);

  123.     if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
  124.         ngx_mail_proxy_internal_server_error(s);
  125.         return;
  126.     }

  127.     ngx_add_timer(p->upstream.connection->read, cscf->timeout);

  128.     p->upstream.connection->data = s;
  129.     p->upstream.connection->pool = s->connection->pool;

  130.     s->connection->read->handler = ngx_mail_proxy_block_read;
  131.     p->upstream.connection->write->handler = ngx_mail_proxy_write_handler;

  132.     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  133.     s->proxy->buffer = ngx_create_temp_buf(s->connection->pool,
  134.                                            pcf->buffer_size);
  135.     if (s->proxy->buffer == NULL) {
  136.         ngx_mail_proxy_internal_server_error(s);
  137.         return;
  138.     }

  139.     s->proxy->proxy_protocol = pcf->proxy_protocol;

  140.     s->out.len = 0;

  141.     switch (s->protocol) {

  142.     case NGX_MAIL_POP3_PROTOCOL:
  143.         p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler;
  144.         s->mail_state = ngx_pop3_start;
  145.         break;

  146.     case NGX_MAIL_IMAP_PROTOCOL:
  147.         p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler;
  148.         s->mail_state = ngx_imap_start;
  149.         break;

  150.     default: /* NGX_MAIL_SMTP_PROTOCOL */
  151.         p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler;
  152.         s->mail_state = ngx_smtp_start;
  153.         break;
  154.     }

  155.     if (rc == NGX_AGAIN) {
  156.         return;
  157.     }

  158.     ngx_mail_proxy_write_handler(p->upstream.connection->write);
  159. }


  160. static void
  161. ngx_mail_proxy_block_read(ngx_event_t *rev)
  162. {
  163.     ngx_connection_t    *c;
  164.     ngx_mail_session_t  *s;

  165.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read");

  166.     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  167.         c = rev->data;
  168.         s = c->data;

  169.         ngx_mail_proxy_close_session(s);
  170.     }
  171. }


  172. static void
  173. ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
  174. {
  175.     u_char                 *p;
  176.     ngx_int_t               rc;
  177.     ngx_str_t               line;
  178.     ngx_connection_t       *c;
  179.     ngx_mail_session_t     *s;
  180.     ngx_mail_proxy_conf_t  *pcf;

  181.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  182.                    "mail proxy pop3 auth handler");

  183.     c = rev->data;
  184.     s = c->data;

  185.     if (rev->timedout) {
  186.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  187.                       "upstream timed out");
  188.         c->timedout = 1;
  189.         ngx_mail_proxy_internal_server_error(s);
  190.         return;
  191.     }

  192.     if (s->proxy->proxy_protocol) {
  193.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "mail proxy pop3 busy");

  194.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  195.             ngx_mail_proxy_internal_server_error(s);
  196.             return;
  197.         }

  198.         return;
  199.     }

  200.     rc = ngx_mail_proxy_read_response(s, 0);

  201.     if (rc == NGX_AGAIN) {
  202.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  203.             ngx_mail_proxy_internal_server_error(s);
  204.             return;
  205.         }

  206.         return;
  207.     }

  208.     if (rc == NGX_ERROR) {
  209.         ngx_mail_proxy_upstream_error(s);
  210.         return;
  211.     }

  212.     switch (s->mail_state) {

  213.     case ngx_pop3_start:
  214.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");

  215.         s->connection->log->action = "sending user name to upstream";

  216.         line.len = sizeof("USER ")  - 1 + s->login.len + 2;
  217.         line.data = ngx_pnalloc(c->pool, line.len);
  218.         if (line.data == NULL) {
  219.             ngx_mail_proxy_internal_server_error(s);
  220.             return;
  221.         }

  222.         p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
  223.         p = ngx_cpymem(p, s->login.data, s->login.len);
  224.         *p++ = CR; *p = LF;

  225.         s->mail_state = ngx_pop3_user;
  226.         break;

  227.     case ngx_pop3_user:
  228.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass");

  229.         s->connection->log->action = "sending password to upstream";

  230.         line.len = sizeof("PASS ")  - 1 + s->passwd.len + 2;
  231.         line.data = ngx_pnalloc(c->pool, line.len);
  232.         if (line.data == NULL) {
  233.             ngx_mail_proxy_internal_server_error(s);
  234.             return;
  235.         }

  236.         p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
  237.         p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
  238.         *p++ = CR; *p = LF;

  239.         s->mail_state = ngx_pop3_passwd;
  240.         break;

  241.     case ngx_pop3_passwd:
  242.         s->connection->read->handler = ngx_mail_proxy_handler;
  243.         s->connection->write->handler = ngx_mail_proxy_handler;
  244.         rev->handler = ngx_mail_proxy_handler;
  245.         c->write->handler = ngx_mail_proxy_handler;

  246.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
  247.         ngx_add_timer(s->connection->read, pcf->timeout);
  248.         ngx_del_timer(c->read);

  249.         c->log->action = NULL;
  250.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");

  251.         if (s->buffer->pos < s->buffer->last
  252.             || s->connection->read->ready)
  253.         {
  254.             ngx_post_event(c->write, &ngx_posted_events);
  255.         }

  256.         ngx_mail_proxy_handler(s->connection->write);

  257.         return;

  258.     default:
  259. #if (NGX_SUPPRESS_WARN)
  260.         ngx_str_null(&line);
  261. #endif
  262.         break;
  263.     }

  264.     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
  265.         /*
  266.          * we treat the incomplete sending as NGX_ERROR
  267.          * because it is very strange here
  268.          */
  269.         ngx_mail_proxy_internal_server_error(s);
  270.         return;
  271.     }

  272.     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  273.         ngx_mail_proxy_internal_server_error(s);
  274.         return;
  275.     }

  276.     s->proxy->buffer->pos = s->proxy->buffer->start;
  277.     s->proxy->buffer->last = s->proxy->buffer->start;
  278. }


  279. static void
  280. ngx_mail_proxy_imap_handler(ngx_event_t *rev)
  281. {
  282.     u_char                 *p;
  283.     ngx_int_t               rc;
  284.     ngx_str_t               line;
  285.     ngx_connection_t       *c;
  286.     ngx_mail_session_t     *s;
  287.     ngx_mail_proxy_conf_t  *pcf;

  288.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  289.                    "mail proxy imap auth handler");

  290.     c = rev->data;
  291.     s = c->data;

  292.     if (rev->timedout) {
  293.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  294.                       "upstream timed out");
  295.         c->timedout = 1;
  296.         ngx_mail_proxy_internal_server_error(s);
  297.         return;
  298.     }

  299.     if (s->proxy->proxy_protocol) {
  300.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "mail proxy imap busy");

  301.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  302.             ngx_mail_proxy_internal_server_error(s);
  303.             return;
  304.         }

  305.         return;
  306.     }

  307.     rc = ngx_mail_proxy_read_response(s, s->mail_state);

  308.     if (rc == NGX_AGAIN) {
  309.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  310.             ngx_mail_proxy_internal_server_error(s);
  311.             return;
  312.         }

  313.         return;
  314.     }

  315.     if (rc == NGX_ERROR) {
  316.         ngx_mail_proxy_upstream_error(s);
  317.         return;
  318.     }

  319.     switch (s->mail_state) {

  320.     case ngx_imap_start:
  321.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  322.                        "mail proxy send login");

  323.         s->connection->log->action = "sending LOGIN command to upstream";

  324.         line.len = s->tag.len + sizeof("LOGIN ") - 1
  325.                    + 1 + NGX_SIZE_T_LEN + 1 + 2;
  326.         line.data = ngx_pnalloc(c->pool, line.len);
  327.         if (line.data == NULL) {
  328.             ngx_mail_proxy_internal_server_error(s);
  329.             return;
  330.         }

  331.         line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
  332.                                &s->tag, s->login.len)
  333.                    - line.data;

  334.         s->mail_state = ngx_imap_login;
  335.         break;

  336.     case ngx_imap_login:
  337.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");

  338.         s->connection->log->action = "sending user name to upstream";

  339.         line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
  340.         line.data = ngx_pnalloc(c->pool, line.len);
  341.         if (line.data == NULL) {
  342.             ngx_mail_proxy_internal_server_error(s);
  343.             return;
  344.         }

  345.         line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
  346.                                &s->login, s->passwd.len)
  347.                    - line.data;

  348.         s->mail_state = ngx_imap_user;
  349.         break;

  350.     case ngx_imap_user:
  351.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  352.                        "mail proxy send passwd");

  353.         s->connection->log->action = "sending password to upstream";

  354.         line.len = s->passwd.len + 2;
  355.         line.data = ngx_pnalloc(c->pool, line.len);
  356.         if (line.data == NULL) {
  357.             ngx_mail_proxy_internal_server_error(s);
  358.             return;
  359.         }

  360.         p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
  361.         *p++ = CR; *p = LF;

  362.         s->mail_state = ngx_imap_passwd;
  363.         break;

  364.     case ngx_imap_passwd:
  365.         s->connection->read->handler = ngx_mail_proxy_handler;
  366.         s->connection->write->handler = ngx_mail_proxy_handler;
  367.         rev->handler = ngx_mail_proxy_handler;
  368.         c->write->handler = ngx_mail_proxy_handler;

  369.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
  370.         ngx_add_timer(s->connection->read, pcf->timeout);
  371.         ngx_del_timer(c->read);

  372.         c->log->action = NULL;
  373.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");

  374.         if (s->buffer->pos < s->buffer->last
  375.             || s->connection->read->ready)
  376.         {
  377.             ngx_post_event(c->write, &ngx_posted_events);
  378.         }

  379.         ngx_mail_proxy_handler(s->connection->write);

  380.         return;

  381.     default:
  382. #if (NGX_SUPPRESS_WARN)
  383.         ngx_str_null(&line);
  384. #endif
  385.         break;
  386.     }

  387.     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
  388.         /*
  389.          * we treat the incomplete sending as NGX_ERROR
  390.          * because it is very strange here
  391.          */
  392.         ngx_mail_proxy_internal_server_error(s);
  393.         return;
  394.     }

  395.     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  396.         ngx_mail_proxy_internal_server_error(s);
  397.         return;
  398.     }

  399.     s->proxy->buffer->pos = s->proxy->buffer->start;
  400.     s->proxy->buffer->last = s->proxy->buffer->start;
  401. }


  402. static void
  403. ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
  404. {
  405.     u_char                    *p;
  406.     ngx_int_t                  rc;
  407.     ngx_str_t                  line, auth, encoded;
  408.     ngx_buf_t                 *b;
  409.     ngx_connection_t          *c;
  410.     ngx_mail_session_t        *s;
  411.     ngx_mail_proxy_conf_t     *pcf;
  412.     ngx_mail_core_srv_conf_t  *cscf;

  413.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  414.                    "mail proxy smtp auth handler");

  415.     c = rev->data;
  416.     s = c->data;

  417.     if (rev->timedout) {
  418.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  419.                       "upstream timed out");
  420.         c->timedout = 1;
  421.         ngx_mail_proxy_internal_server_error(s);
  422.         return;
  423.     }

  424.     if (s->proxy->proxy_protocol) {
  425.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "mail proxy smtp busy");

  426.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  427.             ngx_mail_proxy_internal_server_error(s);
  428.             return;
  429.         }

  430.         return;
  431.     }

  432.     rc = ngx_mail_proxy_read_response(s, s->mail_state);

  433.     if (rc == NGX_AGAIN) {
  434.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  435.             ngx_mail_proxy_internal_server_error(s);
  436.             return;
  437.         }

  438.         return;
  439.     }

  440.     if (rc == NGX_ERROR) {
  441.         ngx_mail_proxy_upstream_error(s);
  442.         return;
  443.     }

  444.     switch (s->mail_state) {

  445.     case ngx_smtp_start:
  446.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo");

  447.         s->connection->log->action = "sending HELO/EHLO to upstream";

  448.         cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);

  449.         line.len = sizeof("HELO ")  - 1 + cscf->server_name.len + 2;
  450.         line.data = ngx_pnalloc(c->pool, line.len);
  451.         if (line.data == NULL) {
  452.             ngx_mail_proxy_internal_server_error(s);
  453.             return;
  454.         }

  455.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  456.         p = ngx_cpymem(line.data,
  457.                        ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "),
  458.                        sizeof("HELO ") - 1);

  459.         p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
  460.         *p++ = CR; *p = LF;

  461.         if (pcf->xclient) {
  462.             s->mail_state = ngx_smtp_helo_xclient;

  463.         } else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
  464.             s->mail_state = ngx_smtp_helo_from;

  465.         } else if (pcf->smtp_auth) {
  466.             s->mail_state = ngx_smtp_helo_auth;

  467.         } else {
  468.             s->mail_state = ngx_smtp_helo;
  469.         }

  470.         break;

  471.     case ngx_smtp_helo_xclient:
  472.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  473.                        "mail proxy send xclient");

  474.         s->connection->log->action = "sending XCLIENT to upstream";

  475.         line.len = sizeof("XCLIENT ADDR= LOGIN= NAME="
  476.                           CRLF) - 1
  477.                    + s->connection->addr_text.len + s->login.len + s->host.len;

  478. #if (NGX_HAVE_INET6)
  479.         if (s->connection->sockaddr->sa_family == AF_INET6) {
  480.             line.len += sizeof("IPV6:") - 1;
  481.         }
  482. #endif

  483.         line.data = ngx_pnalloc(c->pool, line.len);
  484.         if (line.data == NULL) {
  485.             ngx_mail_proxy_internal_server_error(s);
  486.             return;
  487.         }

  488.         p = ngx_cpymem(line.data, "XCLIENT ADDR=", sizeof("XCLIENT ADDR=") - 1);

  489. #if (NGX_HAVE_INET6)
  490.         if (s->connection->sockaddr->sa_family == AF_INET6) {
  491.             p = ngx_cpymem(p, "IPV6:", sizeof("IPV6:") - 1);
  492.         }
  493. #endif

  494.         p = ngx_copy(p, s->connection->addr_text.data,
  495.                      s->connection->addr_text.len);

  496.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  497.         if (s->login.len && !pcf->smtp_auth) {
  498.             p = ngx_cpymem(p, " LOGIN=", sizeof(" LOGIN=") - 1);
  499.             p = ngx_copy(p, s->login.data, s->login.len);
  500.         }

  501.         p = ngx_cpymem(p, " NAME=", sizeof(" NAME=") - 1);
  502.         p = ngx_copy(p, s->host.data, s->host.len);

  503.         *p++ = CR; *p++ = LF;

  504.         line.len = p - line.data;

  505.         if (s->smtp_helo.len) {
  506.             s->mail_state = ngx_smtp_xclient_helo;

  507.         } else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
  508.             s->mail_state = ngx_smtp_xclient_from;

  509.         } else if (pcf->smtp_auth) {
  510.             s->mail_state = ngx_smtp_xclient_auth;

  511.         } else {
  512.             s->mail_state = ngx_smtp_xclient;
  513.         }

  514.         break;

  515.     case ngx_smtp_xclient_helo:
  516.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  517.                        "mail proxy send client ehlo");

  518.         s->connection->log->action = "sending client HELO/EHLO to upstream";

  519.         line.len = sizeof("HELO " CRLF) - 1 + s->smtp_helo.len;

  520.         line.data = ngx_pnalloc(c->pool, line.len);
  521.         if (line.data == NULL) {
  522.             ngx_mail_proxy_internal_server_error(s);
  523.             return;
  524.         }

  525.         line.len = ngx_sprintf(line.data,
  526.                        ((s->esmtp) ? "EHLO %V" CRLF : "HELO %V" CRLF),
  527.                        &s->smtp_helo)
  528.                    - line.data;

  529.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  530.         if (s->auth_method == NGX_MAIL_AUTH_NONE) {
  531.             s->mail_state = ngx_smtp_helo_from;

  532.         } else if (pcf->smtp_auth) {
  533.             s->mail_state = ngx_smtp_helo_auth;

  534.         } else {
  535.             s->mail_state = ngx_smtp_helo;
  536.         }

  537.         break;

  538.     case ngx_smtp_helo_auth:
  539.     case ngx_smtp_xclient_auth:
  540.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  541.                        "mail proxy send auth");

  542.         s->connection->log->action = "sending AUTH to upstream";

  543.         if (s->passwd.data == NULL) {
  544.             ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  545.                           "no password available");
  546.             ngx_mail_proxy_internal_server_error(s);
  547.             return;
  548.         }

  549.         auth.len = 1 + s->login.len + 1 + s->passwd.len;
  550.         auth.data = ngx_pnalloc(c->pool, auth.len);
  551.         if (auth.data == NULL) {
  552.             ngx_mail_proxy_internal_server_error(s);
  553.             return;
  554.         }

  555.         auth.len = ngx_sprintf(auth.data, "%Z%V%Z%V", &s->login, &s->passwd)
  556.                    - auth.data;

  557.         line.len = sizeof("AUTH PLAIN " CRLF) - 1
  558.                    + ngx_base64_encoded_length(auth.len);

  559.         line.data = ngx_pnalloc(c->pool, line.len);
  560.         if (line.data == NULL) {
  561.             ngx_mail_proxy_internal_server_error(s);
  562.             return;
  563.         }

  564.         encoded.data = ngx_cpymem(line.data, "AUTH PLAIN ",
  565.                                   sizeof("AUTH PLAIN ") - 1);

  566.         ngx_encode_base64(&encoded, &auth);

  567.         p = encoded.data + encoded.len;
  568.         *p++ = CR; *p = LF;

  569.         s->mail_state = ngx_smtp_auth_plain;

  570.         break;

  571.     case ngx_smtp_helo_from:
  572.     case ngx_smtp_xclient_from:
  573.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  574.                        "mail proxy send mail from");

  575.         s->connection->log->action = "sending MAIL FROM to upstream";

  576.         line.len = s->smtp_from.len + sizeof(CRLF) - 1;
  577.         line.data = ngx_pnalloc(c->pool, line.len);
  578.         if (line.data == NULL) {
  579.             ngx_mail_proxy_internal_server_error(s);
  580.             return;
  581.         }

  582.         p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len);
  583.         *p++ = CR; *p = LF;

  584.         s->mail_state = ngx_smtp_from;

  585.         break;

  586.     case ngx_smtp_from:
  587.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  588.                        "mail proxy send rcpt to");

  589.         s->connection->log->action = "sending RCPT TO to upstream";

  590.         line.len = s->smtp_to.len + sizeof(CRLF) - 1;
  591.         line.data = ngx_pnalloc(c->pool, line.len);
  592.         if (line.data == NULL) {
  593.             ngx_mail_proxy_internal_server_error(s);
  594.             return;
  595.         }

  596.         p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len);
  597.         *p++ = CR; *p = LF;

  598.         s->mail_state = ngx_smtp_to;

  599.         break;

  600.     case ngx_smtp_helo:
  601.     case ngx_smtp_xclient:
  602.     case ngx_smtp_auth_plain:
  603.     case ngx_smtp_to:

  604.         b = s->proxy->buffer;

  605.         if (s->auth_method == NGX_MAIL_AUTH_NONE) {
  606.             b->pos = b->start;

  607.         } else {
  608.             ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1);
  609.             b->last = b->start + sizeof(smtp_auth_ok) - 1;
  610.         }

  611.         s->connection->read->handler = ngx_mail_proxy_handler;
  612.         s->connection->write->handler = ngx_mail_proxy_handler;
  613.         rev->handler = ngx_mail_proxy_handler;
  614.         c->write->handler = ngx_mail_proxy_handler;

  615.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
  616.         ngx_add_timer(s->connection->read, pcf->timeout);
  617.         ngx_del_timer(c->read);

  618.         c->log->action = NULL;
  619.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");

  620.         if (s->buffer->pos < s->buffer->last
  621.             || s->connection->read->ready)
  622.         {
  623.             ngx_post_event(c->write, &ngx_posted_events);
  624.         }

  625.         ngx_mail_proxy_handler(s->connection->write);

  626.         return;

  627.     default:
  628. #if (NGX_SUPPRESS_WARN)
  629.         ngx_str_null(&line);
  630. #endif
  631.         break;
  632.     }

  633.     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
  634.         /*
  635.          * we treat the incomplete sending as NGX_ERROR
  636.          * because it is very strange here
  637.          */
  638.         ngx_mail_proxy_internal_server_error(s);
  639.         return;
  640.     }

  641.     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  642.         ngx_mail_proxy_internal_server_error(s);
  643.         return;
  644.     }

  645.     s->proxy->buffer->pos = s->proxy->buffer->start;
  646.     s->proxy->buffer->last = s->proxy->buffer->start;
  647. }


  648. static void
  649. ngx_mail_proxy_write_handler(ngx_event_t *wev)
  650. {
  651.     ngx_connection_t    *c;
  652.     ngx_mail_session_t  *s;

  653.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy write handler");

  654.     c = wev->data;
  655.     s = c->data;

  656.     if (s->proxy->proxy_protocol) {
  657.         if (ngx_mail_proxy_send_proxy_protocol(s) != NGX_OK) {
  658.             return;
  659.         }

  660.         s->proxy->proxy_protocol = 0;
  661.     }

  662.     if (ngx_handle_write_event(wev, 0) != NGX_OK) {
  663.         ngx_mail_proxy_internal_server_error(s);
  664.     }

  665.     if (c->read->ready) {
  666.         ngx_post_event(c->read, &ngx_posted_events);
  667.     }
  668. }


  669. static ngx_int_t
  670. ngx_mail_proxy_send_proxy_protocol(ngx_mail_session_t *s)
  671. {
  672.     u_char            *p;
  673.     ssize_t            n, size;
  674.     ngx_connection_t  *c;
  675.     u_char             buf[NGX_PROXY_PROTOCOL_V1_MAX_HEADER];

  676.     s->connection->log->action = "sending PROXY protocol header to upstream";

  677.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
  678.                    "mail proxy send PROXY protocol header");

  679.     p = ngx_proxy_protocol_write(s->connection, buf,
  680.                                  buf + NGX_PROXY_PROTOCOL_V1_MAX_HEADER);
  681.     if (p == NULL) {
  682.         ngx_mail_proxy_internal_server_error(s);
  683.         return NGX_ERROR;
  684.     }

  685.     c = s->proxy->upstream.connection;

  686.     size = p - buf;

  687.     n = c->send(c, buf, size);

  688.     if (n == NGX_AGAIN) {
  689.         if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
  690.             ngx_mail_proxy_internal_server_error(s);
  691.             return NGX_ERROR;
  692.         }

  693.         return NGX_AGAIN;
  694.     }

  695.     if (n == NGX_ERROR) {
  696.         ngx_mail_proxy_internal_server_error(s);
  697.         return NGX_ERROR;
  698.     }

  699.     if (n != size) {

  700.         /*
  701.          * PROXY protocol specification:
  702.          * The sender must always ensure that the header
  703.          * is sent at once, so that the transport layer
  704.          * maintains atomicity along the path to the receiver.
  705.          */

  706.         ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  707.                       "could not send PROXY protocol header at once");

  708.         ngx_mail_proxy_internal_server_error(s);

  709.         return NGX_ERROR;
  710.     }

  711.     return NGX_OK;
  712. }


  713. static ngx_int_t
  714. ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state)
  715. {
  716.     u_char                 *p, *m;
  717.     ssize_t                 n;
  718.     ngx_buf_t              *b;
  719.     ngx_mail_proxy_conf_t  *pcf;

  720.     s->connection->log->action = "reading response from upstream";

  721.     b = s->proxy->buffer;

  722.     n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
  723.                                             b->last, b->end - b->last);

  724.     if (n == NGX_ERROR || n == 0) {
  725.         return NGX_ERROR;
  726.     }

  727.     if (n == NGX_AGAIN) {
  728.         return NGX_AGAIN;
  729.     }

  730.     b->last += n;

  731.     if (b->last - b->pos < 4) {
  732.         return NGX_AGAIN;
  733.     }

  734.     if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
  735.         if (b->last == b->end) {
  736.             *(b->last - 1) = '\0';
  737.             ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  738.                           "upstream sent too long response line: \"%s\"",
  739.                           b->pos);
  740.             return NGX_ERROR;
  741.         }

  742.         return NGX_AGAIN;
  743.     }

  744.     p = b->pos;

  745.     switch (s->protocol) {

  746.     case NGX_MAIL_POP3_PROTOCOL:
  747.         if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
  748.             return NGX_OK;
  749.         }
  750.         break;

  751.     case NGX_MAIL_IMAP_PROTOCOL:
  752.         switch (state) {

  753.         case ngx_imap_start:
  754.             if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
  755.                 return NGX_OK;
  756.             }
  757.             break;

  758.         case ngx_imap_login:
  759.         case ngx_imap_user:
  760.             if (p[0] == '+') {
  761.                 return NGX_OK;
  762.             }
  763.             break;

  764.         case ngx_imap_passwd:

  765.             /*
  766.              * untagged CAPABILITY response (draft-crispin-imapv-16),
  767.              * known to be sent by SmarterMail and Gmail
  768.              */

  769.             if (p[0] == '*' && p[1] == ' ') {
  770.                 p += 2;

  771.                 while (p < b->last - 1) {
  772.                     if (p[0] == CR && p[1] == LF) {
  773.                         p += 2;
  774.                         break;
  775.                     }

  776.                     p++;
  777.                 }

  778.                 if (b->last - p < 4) {
  779.                     return NGX_AGAIN;
  780.                 }
  781.             }

  782.             if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) {
  783.                 p += s->tag.len;
  784.                 if (p[0] == 'O' && p[1] == 'K') {
  785.                     return NGX_OK;
  786.                 }
  787.             }

  788.             break;
  789.         }

  790.         break;

  791.     default: /* NGX_MAIL_SMTP_PROTOCOL */

  792.         if (p[3] == '-') {
  793.             /* multiline reply, check if we got last line */

  794.             m = b->last - (sizeof(CRLF "200" CRLF) - 1);

  795.             while (m > p) {
  796.                 if (m[0] == CR && m[1] == LF) {
  797.                     break;
  798.                 }

  799.                 m--;
  800.             }

  801.             if (m <= p || m[5] == '-') {
  802.                 return NGX_AGAIN;
  803.             }
  804.         }

  805.         switch (state) {

  806.         case ngx_smtp_start:
  807.             if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
  808.                 return NGX_OK;
  809.             }
  810.             break;

  811.         case ngx_smtp_helo:
  812.         case ngx_smtp_helo_xclient:
  813.         case ngx_smtp_helo_from:
  814.         case ngx_smtp_helo_auth:
  815.         case ngx_smtp_from:
  816.             if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
  817.                 return NGX_OK;
  818.             }
  819.             break;

  820.         case ngx_smtp_xclient:
  821.         case ngx_smtp_xclient_from:
  822.         case ngx_smtp_xclient_helo:
  823.         case ngx_smtp_xclient_auth:
  824.             if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') {
  825.                 return NGX_OK;
  826.             }
  827.             break;

  828.         case ngx_smtp_auth_plain:
  829.             if (p[0] == '2' && p[1] == '3' && p[2] == '5') {
  830.                 return NGX_OK;
  831.             }
  832.             break;

  833.         case ngx_smtp_to:
  834.             return NGX_OK;
  835.         }

  836.         break;
  837.     }

  838.     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  839.     if (pcf->pass_error_message == 0) {
  840.         *(b->last - 2) = '\0';
  841.         ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  842.                       "upstream sent invalid response: \"%s\"", p);
  843.         return NGX_ERROR;
  844.     }

  845.     s->out.len = b->last - p - 2;
  846.     s->out.data = p;

  847.     ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
  848.                   "upstream sent invalid response: \"%V\"", &s->out);

  849.     s->out.len = b->last - b->pos;
  850.     s->out.data = b->pos;

  851.     return NGX_ERROR;
  852. }


  853. static void
  854. ngx_mail_proxy_handler(ngx_event_t *ev)
  855. {
  856.     char                   *action, *recv_action, *send_action;
  857.     size_t                  size;
  858.     ssize_t                 n;
  859.     ngx_buf_t              *b;
  860.     ngx_uint_t              do_write;
  861.     ngx_connection_t       *c, *src, *dst;
  862.     ngx_mail_session_t     *s;
  863.     ngx_mail_proxy_conf_t  *pcf;

  864.     c = ev->data;
  865.     s = c->data;

  866.     if (ev->timedout || c->close) {
  867.         c->log->action = "proxying";

  868.         if (c->close) {
  869.             ngx_log_error(NGX_LOG_INFO, c->log, 0, "shutdown timeout");

  870.         } else if (c == s->connection) {
  871.             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  872.                           "client timed out");
  873.             c->timedout = 1;

  874.         } else {
  875.             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  876.                           "upstream timed out");
  877.         }

  878.         ngx_mail_proxy_close_session(s);
  879.         return;
  880.     }

  881.     if (c == s->connection) {
  882.         if (ev->write) {
  883.             recv_action = "proxying and reading from upstream";
  884.             send_action = "proxying and sending to client";
  885.             src = s->proxy->upstream.connection;
  886.             dst = c;
  887.             b = s->proxy->buffer;

  888.         } else {
  889.             recv_action = "proxying and reading from client";
  890.             send_action = "proxying and sending to upstream";
  891.             src = c;
  892.             dst = s->proxy->upstream.connection;
  893.             b = s->buffer;
  894.         }

  895.     } else {
  896.         if (ev->write) {
  897.             recv_action = "proxying and reading from client";
  898.             send_action = "proxying and sending to upstream";
  899.             src = s->connection;
  900.             dst = c;
  901.             b = s->buffer;

  902.         } else {
  903.             recv_action = "proxying and reading from upstream";
  904.             send_action = "proxying and sending to client";
  905.             src = c;
  906.             dst = s->connection;
  907.             b = s->proxy->buffer;
  908.         }
  909.     }

  910.     do_write = ev->write ? 1 : 0;

  911.     ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0,
  912.                    "mail proxy handler: %ui, #%d > #%d",
  913.                    do_write, src->fd, dst->fd);

  914.     for ( ;; ) {

  915.         if (do_write) {

  916.             size = b->last - b->pos;

  917.             if (size && dst->write->ready) {
  918.                 c->log->action = send_action;

  919.                 n = dst->send(dst, b->pos, size);

  920.                 if (n == NGX_ERROR) {
  921.                     ngx_mail_proxy_close_session(s);
  922.                     return;
  923.                 }

  924.                 if (n > 0) {
  925.                     b->pos += n;

  926.                     if (b->pos == b->last) {
  927.                         b->pos = b->start;
  928.                         b->last = b->start;
  929.                     }
  930.                 }
  931.             }
  932.         }

  933.         size = b->end - b->last;

  934.         if (size && src->read->ready) {
  935.             c->log->action = recv_action;

  936.             n = src->recv(src, b->last, size);

  937.             if (n == NGX_AGAIN || n == 0) {
  938.                 break;
  939.             }

  940.             if (n > 0) {
  941.                 do_write = 1;
  942.                 b->last += n;

  943.                 continue;
  944.             }

  945.             if (n == NGX_ERROR) {
  946.                 src->read->eof = 1;
  947.             }
  948.         }

  949.         break;
  950.     }

  951.     c->log->action = "proxying";

  952.     if ((s->connection->read->eof && s->buffer->pos == s->buffer->last)
  953.         || (s->proxy->upstream.connection->read->eof
  954.             && s->proxy->buffer->pos == s->proxy->buffer->last)
  955.         || (s->connection->read->eof
  956.             && s->proxy->upstream.connection->read->eof))
  957.     {
  958.         action = c->log->action;
  959.         c->log->action = NULL;
  960.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
  961.         c->log->action = action;

  962.         ngx_mail_proxy_close_session(s);
  963.         return;
  964.     }

  965.     if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
  966.         ngx_mail_proxy_close_session(s);
  967.         return;
  968.     }

  969.     if (ngx_handle_read_event(dst->read, 0) != NGX_OK) {
  970.         ngx_mail_proxy_close_session(s);
  971.         return;
  972.     }

  973.     if (ngx_handle_write_event(src->write, 0) != NGX_OK) {
  974.         ngx_mail_proxy_close_session(s);
  975.         return;
  976.     }

  977.     if (ngx_handle_read_event(src->read, 0) != NGX_OK) {
  978.         ngx_mail_proxy_close_session(s);
  979.         return;
  980.     }

  981.     if (c == s->connection) {
  982.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
  983.         ngx_add_timer(c->read, pcf->timeout);
  984.     }
  985. }


  986. static void
  987. ngx_mail_proxy_upstream_error(ngx_mail_session_t *s)
  988. {
  989.     if (s->proxy->upstream.connection) {
  990.         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
  991.                        "close mail proxy connection: %d",
  992.                        s->proxy->upstream.connection->fd);

  993.         ngx_close_connection(s->proxy->upstream.connection);
  994.     }

  995.     if (s->out.len == 0) {
  996.         ngx_mail_session_internal_server_error(s);
  997.         return;
  998.     }

  999.     s->quit = 1;
  1000.     ngx_mail_send(s->connection->write);
  1001. }


  1002. static void
  1003. ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s)
  1004. {
  1005.     if (s->proxy->upstream.connection) {
  1006.         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
  1007.                        "close mail proxy connection: %d",
  1008.                        s->proxy->upstream.connection->fd);

  1009.         ngx_close_connection(s->proxy->upstream.connection);
  1010.     }

  1011.     ngx_mail_session_internal_server_error(s);
  1012. }


  1013. static void
  1014. ngx_mail_proxy_close_session(ngx_mail_session_t *s)
  1015. {
  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);

  1020.         ngx_close_connection(s->proxy->upstream.connection);
  1021.     }

  1022.     ngx_mail_close_connection(s->connection);
  1023. }


  1024. static void *
  1025. ngx_mail_proxy_create_conf(ngx_conf_t *cf)
  1026. {
  1027.     ngx_mail_proxy_conf_t  *pcf;

  1028.     pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t));
  1029.     if (pcf == NULL) {
  1030.         return NULL;
  1031.     }

  1032.     pcf->enable = NGX_CONF_UNSET;
  1033.     pcf->pass_error_message = NGX_CONF_UNSET;
  1034.     pcf->xclient = NGX_CONF_UNSET;
  1035.     pcf->smtp_auth = NGX_CONF_UNSET;
  1036.     pcf->proxy_protocol = NGX_CONF_UNSET;
  1037.     pcf->buffer_size = NGX_CONF_UNSET_SIZE;
  1038.     pcf->timeout = NGX_CONF_UNSET_MSEC;

  1039.     return pcf;
  1040. }


  1041. static char *
  1042. ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  1043. {
  1044.     ngx_mail_proxy_conf_t *prev = parent;
  1045.     ngx_mail_proxy_conf_t *conf = child;

  1046.     ngx_conf_merge_value(conf->enable, prev->enable, 0);
  1047.     ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
  1048.     ngx_conf_merge_value(conf->xclient, prev->xclient, 1);
  1049.     ngx_conf_merge_value(conf->smtp_auth, prev->smtp_auth, 0);
  1050.     ngx_conf_merge_value(conf->proxy_protocol, prev->proxy_protocol, 0);
  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.     return NGX_CONF_OK;
  1055. }