src/mail/ngx_mail_smtp_module.c - nginx source code

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("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
  18.     { ngx_string("none"), NGX_MAIL_AUTH_NONE_ENABLED },
  19.     { ngx_null_string, 0 }
  20. };


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


  29. static ngx_mail_protocol_t  ngx_mail_smtp_protocol = {
  30.     ngx_string("smtp"),
  31.     ngx_string("\x04smtp"),
  32.     { 25, 465, 587, 0 },
  33.     NGX_MAIL_SMTP_PROTOCOL,

  34.     ngx_mail_smtp_init_session,
  35.     ngx_mail_smtp_init_protocol,
  36.     ngx_mail_smtp_parse_command,
  37.     ngx_mail_smtp_auth_state,

  38.     ngx_string("451 4.3.2 Internal server error" CRLF),
  39.     ngx_string("421 4.7.1 SSL certificate error" CRLF),
  40.     ngx_string("421 4.7.1 No required SSL certificate" CRLF)
  41. };


  42. static ngx_command_t  ngx_mail_smtp_commands[] = {

  43.     { ngx_string("smtp_client_buffer"),
  44.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  45.       ngx_conf_set_size_slot,
  46.       NGX_MAIL_SRV_CONF_OFFSET,
  47.       offsetof(ngx_mail_smtp_srv_conf_t, client_buffer_size),
  48.       NULL },

  49.     { ngx_string("smtp_greeting_delay"),
  50.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  51.       ngx_conf_set_msec_slot,
  52.       NGX_MAIL_SRV_CONF_OFFSET,
  53.       offsetof(ngx_mail_smtp_srv_conf_t, greeting_delay),
  54.       NULL },

  55.     { ngx_string("smtp_capabilities"),
  56.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  57.       ngx_mail_capabilities,
  58.       NGX_MAIL_SRV_CONF_OFFSET,
  59.       offsetof(ngx_mail_smtp_srv_conf_t, capabilities),
  60.       NULL },

  61.     { ngx_string("smtp_auth"),
  62.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  63.       ngx_conf_set_bitmask_slot,
  64.       NGX_MAIL_SRV_CONF_OFFSET,
  65.       offsetof(ngx_mail_smtp_srv_conf_t, auth_methods),
  66.       &ngx_mail_smtp_auth_methods },

  67.       ngx_null_command
  68. };


  69. static ngx_mail_module_t  ngx_mail_smtp_module_ctx = {
  70.     &ngx_mail_smtp_protocol,               /* protocol */

  71.     NULL,                                  /* create main configuration */
  72.     NULL,                                  /* init main configuration */

  73.     ngx_mail_smtp_create_srv_conf,         /* create server configuration */
  74.     ngx_mail_smtp_merge_srv_conf           /* merge server configuration */
  75. };


  76. ngx_module_t  ngx_mail_smtp_module = {
  77.     NGX_MODULE_V1,
  78.     &ngx_mail_smtp_module_ctx,             /* module context */
  79.     ngx_mail_smtp_commands,                /* module directives */
  80.     NGX_MAIL_MODULE,                       /* module type */
  81.     NULL,                                  /* init master */
  82.     NULL,                                  /* init module */
  83.     NULL,                                  /* init process */
  84.     NULL,                                  /* init thread */
  85.     NULL,                                  /* exit thread */
  86.     NULL,                                  /* exit process */
  87.     NULL,                                  /* exit master */
  88.     NGX_MODULE_V1_PADDING
  89. };


  90. static void *
  91. ngx_mail_smtp_create_srv_conf(ngx_conf_t *cf)
  92. {
  93.     ngx_mail_smtp_srv_conf_t  *sscf;

  94.     sscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_smtp_srv_conf_t));
  95.     if (sscf == NULL) {
  96.         return NULL;
  97.     }

  98.     sscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
  99.     sscf->greeting_delay = NGX_CONF_UNSET_MSEC;

  100.     if (ngx_array_init(&sscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
  101.         != NGX_OK)
  102.     {
  103.         return NULL;
  104.     }

  105.     return sscf;
  106. }


  107. static char *
  108. ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  109. {
  110.     ngx_mail_smtp_srv_conf_t *prev = parent;
  111.     ngx_mail_smtp_srv_conf_t *conf = child;

  112.     u_char                    *p, *auth, *last;
  113.     size_t                     size;
  114.     ngx_str_t                 *c;
  115.     ngx_uint_t                 i, m, auth_enabled;
  116.     ngx_mail_core_srv_conf_t  *cscf;

  117.     ngx_conf_merge_size_value(conf->client_buffer_size,
  118.                               prev->client_buffer_size,
  119.                               (size_t) ngx_pagesize);

  120.     ngx_conf_merge_msec_value(conf->greeting_delay,
  121.                               prev->greeting_delay, 0);

  122.     ngx_conf_merge_bitmask_value(conf->auth_methods,
  123.                               prev->auth_methods,
  124.                               (NGX_CONF_BITMASK_SET
  125.                                |NGX_MAIL_AUTH_PLAIN_ENABLED
  126.                                |NGX_MAIL_AUTH_LOGIN_ENABLED));


  127.     cscf = ngx_mail_conf_get_module_srv_conf(cf, ngx_mail_core_module);

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

  129.     p = ngx_pnalloc(cf->pool, size);
  130.     if (p == NULL) {
  131.         return NGX_CONF_ERROR;
  132.     }

  133.     conf->greeting.len = size;
  134.     conf->greeting.data = p;

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


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

  139.     p = ngx_pnalloc(cf->pool, size);
  140.     if (p == NULL) {
  141.         return NGX_CONF_ERROR;
  142.     }

  143.     conf->server_name.len = size;
  144.     conf->server_name.data = p;

  145.     *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
  146.     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
  147.     *p++ = CR; *p = LF;


  148.     if (conf->capabilities.nelts == 0) {
  149.         conf->capabilities = prev->capabilities;
  150.     }

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

  152.     c = conf->capabilities.elts;
  153.     for (i = 0; i < conf->capabilities.nelts; i++) {
  154.         size += sizeof("250 ") - 1 + c[i].len + sizeof(CRLF) - 1;
  155.     }

  156.     auth_enabled = 0;

  157.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  158.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  159.          m <<= 1, i++)
  160.     {
  161.         if (m & conf->auth_methods) {
  162.             size += 1 + ngx_mail_smtp_auth_methods_names[i].len;
  163.             auth_enabled = 1;
  164.         }
  165.     }

  166.     if (auth_enabled) {
  167.         size += sizeof("250 AUTH") - 1 + sizeof(CRLF) - 1;
  168.     }

  169.     p = ngx_pnalloc(cf->pool, size);
  170.     if (p == NULL) {
  171.         return NGX_CONF_ERROR;
  172.     }

  173.     conf->capability.len = size;
  174.     conf->capability.data = p;

  175.     last = p;

  176.     *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
  177.     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
  178.     *p++ = CR; *p++ = LF;

  179.     for (i = 0; i < conf->capabilities.nelts; i++) {
  180.         last = p;
  181.         *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
  182.         p = ngx_cpymem(p, c[i].data, c[i].len);
  183.         *p++ = CR; *p++ = LF;
  184.     }

  185.     auth = p;

  186.     if (auth_enabled) {
  187.         last = p;

  188.         *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
  189.         *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H';

  190.         for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  191.              m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  192.              m <<= 1, i++)
  193.         {
  194.             if (m & conf->auth_methods) {
  195.                 *p++ = ' ';
  196.                 p = ngx_cpymem(p, ngx_mail_smtp_auth_methods_names[i].data,
  197.                                ngx_mail_smtp_auth_methods_names[i].len);
  198.             }
  199.         }

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

  201.     } else {
  202.         last[3] = ' ';
  203.     }

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

  205.     p = ngx_pnalloc(cf->pool, size);
  206.     if (p == NULL) {
  207.         return NGX_CONF_ERROR;
  208.     }

  209.     conf->starttls_capability.len = size;
  210.     conf->starttls_capability.data = p;

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

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

  213.     p = conf->starttls_capability.data
  214.         + (last - conf->capability.data) + 3;
  215.     *p = '-';

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

  218.     p = ngx_pnalloc(cf->pool, size);
  219.     if (p == NULL) {
  220.         return NGX_CONF_ERROR;
  221.     }

  222.     conf->starttls_only_capability.len = size;
  223.     conf->starttls_only_capability.data = p;

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

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

  226.     if (last < auth) {
  227.         p = conf->starttls_only_capability.data
  228.             + (last - conf->capability.data) + 3;
  229.         *p = '-';
  230.     }

  231.     return NGX_CONF_OK;
  232. }