src/mail/ngx_mail_proxy_module.c - nginx

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.     uintptr_t                  n;
  410.     ngx_connection_t          *c;
  411.     ngx_mail_session_t        *s;
  412.     ngx_mail_proxy_conf_t     *pcf;
  413.     ngx_mail_core_srv_conf_t  *cscf;

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

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

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

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

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

  431.         return;
  432.     }

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

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

  439.         return;
  440.     }

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

  445.     switch (s->mail_state) {

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

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

  449.         cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);

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

  456.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

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

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

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

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

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

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

  471.         break;

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

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

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

  479.         n = ngx_escape_uri(NULL, s->login.data, s->login.len,
  480.                            NGX_ESCAPE_MAIL_XTEXT);
  481.         line.len += n * 2;

  482. #if (NGX_HAVE_INET6)
  483.         if (s->connection->sockaddr->sa_family == AF_INET6) {
  484.             line.len += sizeof("IPV6:") - 1;
  485.         }
  486. #endif

  487.         line.data = ngx_pnalloc(c->pool, line.len);
  488.         if (line.data == NULL) {
  489.             ngx_mail_proxy_internal_server_error(s);
  490.             return;
  491.         }

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

  493. #if (NGX_HAVE_INET6)
  494.         if (s->connection->sockaddr->sa_family == AF_INET6) {
  495.             p = ngx_cpymem(p, "IPV6:", sizeof("IPV6:") - 1);
  496.         }
  497. #endif

  498.         p = ngx_copy(p, s->connection->addr_text.data,
  499.                      s->connection->addr_text.len);

  500.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  501.         if (s->login.len && !pcf->smtp_auth) {
  502.             p = ngx_cpymem(p, " LOGIN=", sizeof(" LOGIN=") - 1);

  503.             if (n == 0) {
  504.                 p = ngx_copy(p, s->login.data, s->login.len);

  505.             } else {
  506.                 p = (u_char *) ngx_escape_uri(p, s->login.data, s->login.len,
  507.                                               NGX_ESCAPE_MAIL_XTEXT);
  508.             }
  509.         }

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

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

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

  514.         if (s->smtp_helo.len) {
  515.             s->mail_state = ngx_smtp_xclient_helo;

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

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

  520.         } else {
  521.             s->mail_state = ngx_smtp_xclient;
  522.         }

  523.         break;

  524.     case ngx_smtp_xclient_helo:
  525.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  526.                        "mail proxy send client ehlo");

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

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

  529.         line.data = ngx_pnalloc(c->pool, line.len);
  530.         if (line.data == NULL) {
  531.             ngx_mail_proxy_internal_server_error(s);
  532.             return;
  533.         }

  534.         line.len = ngx_sprintf(line.data,
  535.                        ((s->esmtp) ? "EHLO %V" CRLF : "HELO %V" CRLF),
  536.                        &s->smtp_helo)
  537.                    - line.data;

  538.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  539.         if (s->auth_method == NGX_MAIL_AUTH_NONE) {
  540.             s->mail_state = ngx_smtp_helo_from;

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

  543.         } else {
  544.             s->mail_state = ngx_smtp_helo;
  545.         }

  546.         break;

  547.     case ngx_smtp_helo_auth:
  548.     case ngx_smtp_xclient_auth:
  549.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  550.                        "mail proxy send auth");

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

  552.         if (s->passwd.data == NULL) {
  553.             ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  554.                           "no password available");
  555.             ngx_mail_proxy_internal_server_error(s);
  556.             return;
  557.         }

  558.         auth.len = 1 + s->login.len + 1 + s->passwd.len;
  559.         auth.data = ngx_pnalloc(c->pool, auth.len);
  560.         if (auth.data == NULL) {
  561.             ngx_mail_proxy_internal_server_error(s);
  562.             return;
  563.         }

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

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

  568.         line.data = ngx_pnalloc(c->pool, line.len);
  569.         if (line.data == NULL) {
  570.             ngx_mail_proxy_internal_server_error(s);
  571.             return;
  572.         }

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

  575.         ngx_encode_base64(&encoded, &auth);

  576.         p = encoded.data + encoded.len;
  577.         *p++ = CR; *p = LF;

  578.         s->mail_state = ngx_smtp_auth_plain;

  579.         break;

  580.     case ngx_smtp_helo_from:
  581.     case ngx_smtp_xclient_from:
  582.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  583.                        "mail proxy send mail from");

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

  585.         line.len = s->smtp_from.len + sizeof(CRLF) - 1;
  586.         line.data = ngx_pnalloc(c->pool, line.len);
  587.         if (line.data == NULL) {
  588.             ngx_mail_proxy_internal_server_error(s);
  589.             return;
  590.         }

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

  593.         s->mail_state = ngx_smtp_from;

  594.         break;

  595.     case ngx_smtp_from:
  596.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
  597.                        "mail proxy send rcpt to");

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

  599.         line.len = s->smtp_to.len + sizeof(CRLF) - 1;
  600.         line.data = ngx_pnalloc(c->pool, line.len);
  601.         if (line.data == NULL) {
  602.             ngx_mail_proxy_internal_server_error(s);
  603.             return;
  604.         }

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

  607.         s->mail_state = ngx_smtp_to;

  608.         break;

  609.     case ngx_smtp_helo:
  610.     case ngx_smtp_xclient:
  611.     case ngx_smtp_auth_plain:
  612.     case ngx_smtp_to:

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

  614.         if (s->auth_method == NGX_MAIL_AUTH_NONE) {
  615.             b->pos = b->start;

  616.         } else {
  617.             ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1);
  618.             b->last = b->start + sizeof(smtp_auth_ok) - 1;
  619.         }

  620.         s->connection->read->handler = ngx_mail_proxy_handler;
  621.         s->connection->write->handler = ngx_mail_proxy_handler;
  622.         rev->handler = ngx_mail_proxy_handler;
  623.         c->write->handler = ngx_mail_proxy_handler;

  624.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
  625.         ngx_add_timer(s->connection->read, pcf->timeout);
  626.         ngx_del_timer(c->read);

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

  629.         if (s->buffer->pos < s->buffer->last
  630.             || s->connection->read->ready)
  631.         {
  632.             ngx_post_event(c->write, &ngx_posted_events);
  633.         }

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

  635.         return;

  636.     default:
  637. #if (NGX_SUPPRESS_WARN)
  638.         ngx_str_null(&line);
  639. #endif
  640.         break;
  641.     }

  642.     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
  643.         /*
  644.          * we treat the incomplete sending as NGX_ERROR
  645.          * because it is very strange here
  646.          */
  647.         ngx_mail_proxy_internal_server_error(s);
  648.         return;
  649.     }

  650.     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  651.         ngx_mail_proxy_internal_server_error(s);
  652.         return;
  653.     }

  654.     s->proxy->buffer->pos = s->proxy->buffer->start;
  655.     s->proxy->buffer->last = s->proxy->buffer->start;
  656. }


  657. static void
  658. ngx_mail_proxy_write_handler(ngx_event_t *wev)
  659. {
  660.     ngx_connection_t    *c;
  661.     ngx_mail_session_t  *s;

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

  663.     c = wev->data;
  664.     s = c->data;

  665.     if (s->proxy->proxy_protocol) {
  666.         if (ngx_mail_proxy_send_proxy_protocol(s) != NGX_OK) {
  667.             return;
  668.         }

  669.         s->proxy->proxy_protocol = 0;
  670.     }

  671.     if (ngx_handle_write_event(wev, 0) != NGX_OK) {
  672.         ngx_mail_proxy_internal_server_error(s);
  673.     }

  674.     if (c->read->ready) {
  675.         ngx_post_event(c->read, &ngx_posted_events);
  676.     }
  677. }


  678. static ngx_int_t
  679. ngx_mail_proxy_send_proxy_protocol(ngx_mail_session_t *s)
  680. {
  681.     u_char            *p;
  682.     ssize_t            n, size;
  683.     ngx_connection_t  *c;
  684.     u_char             buf[NGX_PROXY_PROTOCOL_V1_MAX_HEADER];

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

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

  688.     p = ngx_proxy_protocol_write(s->connection, buf,
  689.                                  buf + NGX_PROXY_PROTOCOL_V1_MAX_HEADER);
  690.     if (p == NULL) {
  691.         ngx_mail_proxy_internal_server_error(s);
  692.         return NGX_ERROR;
  693.     }

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

  695.     size = p - buf;

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

  697.     if (n == NGX_AGAIN) {
  698.         if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
  699.             ngx_mail_proxy_internal_server_error(s);
  700.             return NGX_ERROR;
  701.         }

  702.         return NGX_AGAIN;
  703.     }

  704.     if (n == NGX_ERROR) {
  705.         ngx_mail_proxy_internal_server_error(s);
  706.         return NGX_ERROR;
  707.     }

  708.     if (n != size) {

  709.         /*
  710.          * PROXY protocol specification:
  711.          * The sender must always ensure that the header
  712.          * is sent at once, so that the transport layer
  713.          * maintains atomicity along the path to the receiver.
  714.          */

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

  717.         ngx_mail_proxy_internal_server_error(s);

  718.         return NGX_ERROR;
  719.     }

  720.     return NGX_OK;
  721. }


  722. static ngx_int_t
  723. ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state)
  724. {
  725.     u_char                 *p, *m;
  726.     ssize_t                 n;
  727.     ngx_buf_t              *b;
  728.     ngx_mail_proxy_conf_t  *pcf;

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

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

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

  733.     if (n == NGX_ERROR || n == 0) {
  734.         return NGX_ERROR;
  735.     }

  736.     if (n == NGX_AGAIN) {
  737.         return NGX_AGAIN;
  738.     }

  739.     b->last += n;

  740.     if (b->last - b->pos < 4) {
  741.         return NGX_AGAIN;
  742.     }

  743.     if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
  744.         if (b->last == b->end) {
  745.             *(b->last - 1) = '\0';
  746.             ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  747.                           "upstream sent too long response line: \"%s\"",
  748.                           b->pos);
  749.             return NGX_ERROR;
  750.         }

  751.         return NGX_AGAIN;
  752.     }

  753.     p = b->pos;

  754.     switch (s->protocol) {

  755.     case NGX_MAIL_POP3_PROTOCOL:
  756.         if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
  757.             return NGX_OK;
  758.         }
  759.         break;

  760.     case NGX_MAIL_IMAP_PROTOCOL:
  761.         switch (state) {

  762.         case ngx_imap_start:
  763.             if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
  764.                 return NGX_OK;
  765.             }
  766.             break;

  767.         case ngx_imap_login:
  768.         case ngx_imap_user:
  769.             if (p[0] == '+') {
  770.                 return NGX_OK;
  771.             }
  772.             break;

  773.         case ngx_imap_passwd:

  774.             /*
  775.              * untagged CAPABILITY response (draft-crispin-imapv-16),
  776.              * known to be sent by SmarterMail and Gmail
  777.              */

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

  780.                 while (p < b->last - 1) {
  781.                     if (p[0] == CR && p[1] == LF) {
  782.                         p += 2;
  783.                         break;
  784.                     }

  785.                     p++;
  786.                 }

  787.                 if (b->last - p < 4) {
  788.                     return NGX_AGAIN;
  789.                 }
  790.             }

  791.             if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) {
  792.                 p += s->tag.len;
  793.                 if (p[0] == 'O' && p[1] == 'K') {
  794.                     return NGX_OK;
  795.                 }
  796.             }

  797.             break;
  798.         }

  799.         break;

  800.     default: /* NGX_MAIL_SMTP_PROTOCOL */

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

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

  804.             while (m > p) {
  805.                 if (m[0] == CR && m[1] == LF) {
  806.                     break;
  807.                 }

  808.                 m--;
  809.             }

  810.             if (m <= p || m[5] == '-') {
  811.                 return NGX_AGAIN;
  812.             }
  813.         }

  814.         switch (state) {

  815.         case ngx_smtp_start:
  816.             if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
  817.                 return NGX_OK;
  818.             }
  819.             break;

  820.         case ngx_smtp_helo:
  821.         case ngx_smtp_helo_xclient:
  822.         case ngx_smtp_helo_from:
  823.         case ngx_smtp_helo_auth:
  824.         case ngx_smtp_from:
  825.             if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
  826.                 return NGX_OK;
  827.             }
  828.             break;

  829.         case ngx_smtp_xclient:
  830.         case ngx_smtp_xclient_from:
  831.         case ngx_smtp_xclient_helo:
  832.         case ngx_smtp_xclient_auth:
  833.             if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') {
  834.                 return NGX_OK;
  835.             }
  836.             break;

  837.         case ngx_smtp_auth_plain:
  838.             if (p[0] == '2' && p[1] == '3' && p[2] == '5') {
  839.                 return NGX_OK;
  840.             }
  841.             break;

  842.         case ngx_smtp_to:
  843.             return NGX_OK;
  844.         }

  845.         break;
  846.     }

  847.     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);

  848.     if (pcf->pass_error_message == 0) {
  849.         *(b->last - 2) = '\0';
  850.         ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
  851.                       "upstream sent invalid response: \"%s\"", p);
  852.         return NGX_ERROR;
  853.     }

  854.     s->out.len = b->last - p - 2;
  855.     s->out.data = p;

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

  858.     s->out.len = b->last - b->pos;
  859.     s->out.data = b->pos;

  860.     return NGX_ERROR;
  861. }


  862. static void
  863. ngx_mail_proxy_handler(ngx_event_t *ev)
  864. {
  865.     char                   *action, *recv_action, *send_action;
  866.     size_t                  size;
  867.     ssize_t                 n;
  868.     ngx_buf_t              *b;
  869.     ngx_uint_t              do_write;
  870.     ngx_connection_t       *c, *src, *dst;
  871.     ngx_mail_session_t     *s;
  872.     ngx_mail_proxy_conf_t  *pcf;

  873.     c = ev->data;
  874.     s = c->data;

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

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

  879.         } else if (c == s->connection) {
  880.             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  881.                           "client timed out");
  882.             c->timedout = 1;

  883.         } else {
  884.             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  885.                           "upstream timed out");
  886.         }

  887.         ngx_mail_proxy_close_session(s);
  888.         return;
  889.     }

  890.     if (c == s->connection) {
  891.         if (ev->write) {
  892.             recv_action = "proxying and reading from upstream";
  893.             send_action = "proxying and sending to client";
  894.             src = s->proxy->upstream.connection;
  895.             dst = c;
  896.             b = s->proxy->buffer;

  897.         } else {
  898.             recv_action = "proxying and reading from client";
  899.             send_action = "proxying and sending to upstream";
  900.             src = c;
  901.             dst = s->proxy->upstream.connection;
  902.             b = s->buffer;
  903.         }

  904.     } else {
  905.         if (ev->write) {
  906.             recv_action = "proxying and reading from client";
  907.             send_action = "proxying and sending to upstream";
  908.             src = s->connection;
  909.             dst = c;
  910.             b = s->buffer;

  911.         } else {
  912.             recv_action = "proxying and reading from upstream";
  913.             send_action = "proxying and sending to client";
  914.             src = c;
  915.             dst = s->connection;
  916.             b = s->proxy->buffer;
  917.         }
  918.     }

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

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

  923.     for ( ;; ) {

  924.         if (do_write) {

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

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

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

  929.                 if (n == NGX_ERROR) {
  930.                     ngx_mail_proxy_close_session(s);
  931.                     return;
  932.                 }

  933.                 if (n > 0) {
  934.                     b->pos += n;

  935.                     if (b->pos == b->last) {
  936.                         b->pos = b->start;
  937.                         b->last = b->start;
  938.                     }
  939.                 }
  940.             }
  941.         }

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

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

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

  946.             if (n == NGX_AGAIN || n == 0) {
  947.                 break;
  948.             }

  949.             if (n > 0) {
  950.                 do_write = 1;
  951.                 b->last += n;

  952.                 continue;
  953.             }

  954.             if (n == NGX_ERROR) {
  955.                 src->read->eof = 1;
  956.             }
  957.         }

  958.         break;
  959.     }

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

  961.     if ((s->connection->read->eof && s->buffer->pos == s->buffer->last)
  962.         || (s->proxy->upstream.connection->read->eof
  963.             && s->proxy->buffer->pos == s->proxy->buffer->last)
  964.         || (s->connection->read->eof
  965.             && s->proxy->upstream.connection->read->eof))
  966.     {
  967.         action = c->log->action;
  968.         c->log->action = NULL;
  969.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
  970.         c->log->action = action;

  971.         ngx_mail_proxy_close_session(s);
  972.         return;
  973.     }

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

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

  982.     if (ngx_handle_write_event(src->write, 0) != NGX_OK) {
  983.         ngx_mail_proxy_close_session(s);
  984.         return;
  985.     }

  986.     if (ngx_handle_read_event(src->read, 0) != NGX_OK) {
  987.         ngx_mail_proxy_close_session(s);
  988.         return;
  989.     }

  990.     if (c == s->connection) {
  991.         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
  992.         ngx_add_timer(c->read, pcf->timeout);
  993.     }
  994. }


  995. static void
  996. ngx_mail_proxy_upstream_error(ngx_mail_session_t *s)
  997. {
  998.     if (s->proxy->upstream.connection) {
  999.         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
  1000.                        "close mail proxy connection: %d",
  1001.                        s->proxy->upstream.connection->fd);

  1002.         ngx_close_connection(s->proxy->upstream.connection);
  1003.     }

  1004.     if (s->out.len == 0) {
  1005.         ngx_mail_session_internal_server_error(s);
  1006.         return;
  1007.     }

  1008.     s->quit = 1;
  1009.     ngx_mail_send(s->connection->write);
  1010. }


  1011. static void
  1012. ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s)
  1013. {
  1014.     if (s->proxy->upstream.connection) {
  1015.         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
  1016.                        "close mail proxy connection: %d",
  1017.                        s->proxy->upstream.connection->fd);

  1018.         ngx_close_connection(s->proxy->upstream.connection);
  1019.     }

  1020.     ngx_mail_session_internal_server_error(s);
  1021. }


  1022. static void
  1023. ngx_mail_proxy_close_session(ngx_mail_session_t *s)
  1024. {
  1025.     if (s->proxy->upstream.connection) {
  1026.         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
  1027.                        "close mail proxy connection: %d",
  1028.                        s->proxy->upstream.connection->fd);

  1029.         ngx_close_connection(s->proxy->upstream.connection);
  1030.     }

  1031.     ngx_mail_close_connection(s->connection);
  1032. }


  1033. static void *
  1034. ngx_mail_proxy_create_conf(ngx_conf_t *cf)
  1035. {
  1036.     ngx_mail_proxy_conf_t  *pcf;

  1037.     pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t));
  1038.     if (pcf == NULL) {
  1039.         return NULL;
  1040.     }

  1041.     pcf->enable = NGX_CONF_UNSET;
  1042.     pcf->pass_error_message = NGX_CONF_UNSET;
  1043.     pcf->xclient = NGX_CONF_UNSET;
  1044.     pcf->smtp_auth = NGX_CONF_UNSET;
  1045.     pcf->proxy_protocol = NGX_CONF_UNSET;
  1046.     pcf->buffer_size = NGX_CONF_UNSET_SIZE;
  1047.     pcf->timeout = NGX_CONF_UNSET_MSEC;

  1048.     return pcf;
  1049. }


  1050. static char *
  1051. ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  1052. {
  1053.     ngx_mail_proxy_conf_t *prev = parent;
  1054.     ngx_mail_proxy_conf_t *conf = child;

  1055.     ngx_conf_merge_value(conf->enable, prev->enable, 0);
  1056.     ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
  1057.     ngx_conf_merge_value(conf->xclient, prev->xclient, 1);
  1058.     ngx_conf_merge_value(conf->smtp_auth, prev->smtp_auth, 0);
  1059.     ngx_conf_merge_value(conf->proxy_protocol, prev->proxy_protocol, 0);
  1060.     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
  1061.                               (size_t) ngx_pagesize);
  1062.     ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);

  1063.     return NGX_CONF_OK;
  1064. }