src/mail/ngx_mail_smtp_module.c - nginx-1.7.10

Global variables 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_mail.h>
  9. #include <ngx_mail_smtp_module.h>


  10. static void *ngx_mail_smtp_create_srv_conf(ngx_conf_t *cf);
  11. static char *ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent,
  12.     void *child);


  13. static ngx_conf_bitmask_t  ngx_mail_smtp_auth_methods[] = {
  14.     { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
  15.     { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
  16.     { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
  17.     { ngx_string("none"), NGX_MAIL_AUTH_NONE_ENABLED },
  18.     { ngx_null_string, 0 }
  19. };


  20. static ngx_str_t  ngx_mail_smtp_auth_methods_names[] = {
  21.     ngx_string("PLAIN"),
  22.     ngx_string("LOGIN"),
  23.     ngx_null_string/* APOP */
  24.     ngx_string("CRAM-MD5"),
  25.     ngx_null_string   /* NONE */
  26. };


  27. static ngx_mail_protocol_t  ngx_mail_smtp_protocol = {
  28.     ngx_string("smtp"),
  29.     { 25, 465, 587, 0 },
  30.     NGX_MAIL_SMTP_PROTOCOL,

  31.     ngx_mail_smtp_init_session,
  32.     ngx_mail_smtp_init_protocol,
  33.     ngx_mail_smtp_parse_command,
  34.     ngx_mail_smtp_auth_state,

  35.     ngx_string("451 4.3.2 Internal server error" CRLF)
  36. };


  37. static ngx_command_t  ngx_mail_smtp_commands[] = {

  38.     { ngx_string("smtp_client_buffer"),
  39.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  40.       ngx_conf_set_size_slot,
  41.       NGX_MAIL_SRV_CONF_OFFSET,
  42.       offsetof(ngx_mail_smtp_srv_conf_t, client_buffer_size),
  43.       NULL },

  44.     { ngx_string("smtp_greeting_delay"),
  45.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  46.       ngx_conf_set_msec_slot,
  47.       NGX_MAIL_SRV_CONF_OFFSET,
  48.       offsetof(ngx_mail_smtp_srv_conf_t, greeting_delay),
  49.       NULL },

  50.     { ngx_string("smtp_capabilities"),
  51.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  52.       ngx_mail_capabilities,
  53.       NGX_MAIL_SRV_CONF_OFFSET,
  54.       offsetof(ngx_mail_smtp_srv_conf_t, capabilities),
  55.       NULL },

  56.     { ngx_string("smtp_auth"),
  57.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  58.       ngx_conf_set_bitmask_slot,
  59.       NGX_MAIL_SRV_CONF_OFFSET,
  60.       offsetof(ngx_mail_smtp_srv_conf_t, auth_methods),
  61.       &ngx_mail_smtp_auth_methods },

  62.       ngx_null_command
  63. };


  64. static ngx_mail_module_t  ngx_mail_smtp_module_ctx = {
  65.     &ngx_mail_smtp_protocol,               /* protocol */

  66.     NULL,                                  /* create main configuration */
  67.     NULL,                                  /* init main configuration */

  68.     ngx_mail_smtp_create_srv_conf,         /* create server configuration */
  69.     ngx_mail_smtp_merge_srv_conf           /* merge server configuration */
  70. };


  71. ngx_module_t  ngx_mail_smtp_module = {
  72.     NGX_MODULE_V1,
  73.     &ngx_mail_smtp_module_ctx,             /* module context */
  74.     ngx_mail_smtp_commands,                /* module directives */
  75.     NGX_MAIL_MODULE,                       /* module type */
  76.     NULL,                                  /* init master */
  77.     NULL,                                  /* init module */
  78.     NULL,                                  /* init process */
  79.     NULL,                                  /* init thread */
  80.     NULL,                                  /* exit thread */
  81.     NULL,                                  /* exit process */
  82.     NULL,                                  /* exit master */
  83.     NGX_MODULE_V1_PADDING
  84. };


  85. static void *
  86. ngx_mail_smtp_create_srv_conf(ngx_conf_t *cf)
  87. {
  88.     ngx_mail_smtp_srv_conf_t  *sscf;

  89.     sscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_smtp_srv_conf_t));
  90.     if (sscf == NULL) {
  91.         return NULL;
  92.     }

  93.     sscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
  94.     sscf->greeting_delay = NGX_CONF_UNSET_MSEC;

  95.     if (ngx_array_init(&sscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
  96.         != NGX_OK)
  97.     {
  98.         return NULL;
  99.     }

  100.     return sscf;
  101. }


  102. static char *
  103. ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  104. {
  105.     ngx_mail_smtp_srv_conf_t *prev = parent;
  106.     ngx_mail_smtp_srv_conf_t *conf = child;

  107.     u_char                    *p, *auth, *last;
  108.     size_t                     size;
  109.     ngx_str_t                 *c;
  110.     ngx_uint_t                 i, m, auth_enabled;
  111.     ngx_mail_core_srv_conf_t  *cscf;

  112.     ngx_conf_merge_size_value(conf->client_buffer_size,
  113.                               prev->client_buffer_size,
  114.                               (size_t) ngx_pagesize);

  115.     ngx_conf_merge_msec_value(conf->greeting_delay,
  116.                               prev->greeting_delay, 0);

  117.     ngx_conf_merge_bitmask_value(conf->auth_methods,
  118.                               prev->auth_methods,
  119.                               (NGX_CONF_BITMASK_SET
  120.                                |NGX_MAIL_AUTH_PLAIN_ENABLED
  121.                                |NGX_MAIL_AUTH_LOGIN_ENABLED));


  122.     cscf = ngx_mail_conf_get_module_srv_conf(cf, ngx_mail_core_module);

  123.     size = sizeof("220  ESMTP ready" CRLF) - 1 + cscf->server_name.len;

  124.     p = ngx_pnalloc(cf->pool, size);
  125.     if (p == NULL) {
  126.         return NGX_CONF_ERROR;
  127.     }

  128.     conf->greeting.len = size;
  129.     conf->greeting.data = p;

  130.     *p++ = '2'; *p++ = '2'; *p++ = '0'; *p++ = ' ';
  131.     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
  132.     ngx_memcpy(p, " ESMTP ready" CRLF, sizeof(" ESMTP ready" CRLF) - 1);


  133.     size = sizeof("250 " CRLF) - 1 + cscf->server_name.len;

  134.     p = ngx_pnalloc(cf->pool, size);
  135.     if (p == NULL) {
  136.         return NGX_CONF_ERROR;
  137.     }

  138.     conf->server_name.len = size;
  139.     conf->server_name.data = p;

  140.     *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
  141.     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
  142.     *p++ = CR; *p = LF;


  143.     if (conf->capabilities.nelts == 0) {
  144.         conf->capabilities = prev->capabilities;
  145.     }

  146.     size = sizeof("250-") - 1 + cscf->server_name.len + sizeof(CRLF) - 1;

  147.     c = conf->capabilities.elts;
  148.     for (i = 0; i < conf->capabilities.nelts; i++) {
  149.         size += sizeof("250 ") - 1 + c[i].len + sizeof(CRLF) - 1;
  150.     }

  151.     auth_enabled = 0;

  152.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  153.          m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
  154.          m <<= 1, i++)
  155.     {
  156.         if (m & conf->auth_methods) {
  157.             size += 1 + ngx_mail_smtp_auth_methods_names[i].len;
  158.             auth_enabled = 1;
  159.         }
  160.     }

  161.     if (auth_enabled) {
  162.         size += sizeof("250 AUTH") - 1 + sizeof(CRLF) - 1;
  163.     }

  164.     p = ngx_pnalloc(cf->pool, size);
  165.     if (p == NULL) {
  166.         return NGX_CONF_ERROR;
  167.     }

  168.     conf->capability.len = size;
  169.     conf->capability.data = p;

  170.     last = p;

  171.     *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
  172.     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
  173.     *p++ = CR; *p++ = LF;

  174.     for (i = 0; i < conf->capabilities.nelts; i++) {
  175.         last = p;
  176.         *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
  177.         p = ngx_cpymem(p, c[i].data, c[i].len);
  178.         *p++ = CR; *p++ = LF;
  179.     }

  180.     auth = p;

  181.     if (auth_enabled) {
  182.         last = p;

  183.         *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
  184.         *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H';

  185.         for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  186.              m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
  187.              m <<= 1, i++)
  188.         {
  189.             if (m & conf->auth_methods) {
  190.                 *p++ = ' ';
  191.                 p = ngx_cpymem(p, ngx_mail_smtp_auth_methods_names[i].data,
  192.                                ngx_mail_smtp_auth_methods_names[i].len);
  193.             }
  194.         }

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

  196.     } else {
  197.         last[3] = ' ';
  198.     }

  199.     size += sizeof("250 STARTTLS" CRLF) - 1;

  200.     p = ngx_pnalloc(cf->pool, size);
  201.     if (p == NULL) {
  202.         return NGX_CONF_ERROR;
  203.     }

  204.     conf->starttls_capability.len = size;
  205.     conf->starttls_capability.data = p;

  206.     p = ngx_cpymem(p, conf->capability.data, conf->capability.len);

  207.     p = ngx_cpymem(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);

  208.     p = conf->starttls_capability.data
  209.         + (last - conf->capability.data) + 3;
  210.     *p = '-';

  211.     size = (auth - conf->capability.data)
  212.             + sizeof("250 STARTTLS" CRLF) - 1;

  213.     p = ngx_pnalloc(cf->pool, size);
  214.     if (p == NULL) {
  215.         return NGX_CONF_ERROR;
  216.     }

  217.     conf->starttls_only_capability.len = size;
  218.     conf->starttls_only_capability.data = p;

  219.     p = ngx_cpymem(p, conf->capability.data, auth - conf->capability.data);

  220.     ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);

  221.     if (last < auth) {
  222.         p = conf->starttls_only_capability.data
  223.             + (last - conf->capability.data) + 3;
  224.         *p = '-';
  225.     }

  226.     return NGX_CONF_OK;
  227. }