src/mail/ngx_mail_imap_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_imap_module.h>


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


  13. static ngx_str_t  ngx_mail_imap_default_capabilities[] = {
  14.     ngx_string("IMAP4"),
  15.     ngx_string("IMAP4rev1"),
  16.     ngx_string("UIDPLUS"),
  17.     ngx_null_string
  18. };


  19. static ngx_conf_bitmask_t  ngx_mail_imap_auth_methods[] = {
  20.     { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
  21.     { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
  22.     { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
  23.     { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
  24.     { ngx_null_string, 0 }
  25. };


  26. static ngx_str_t  ngx_mail_imap_auth_methods_names[] = {
  27.     ngx_string("AUTH=PLAIN"),
  28.     ngx_string("AUTH=LOGIN"),
  29.     ngx_null_string/* APOP */
  30.     ngx_string("AUTH=CRAM-MD5"),
  31.     ngx_string("AUTH=EXTERNAL"),
  32.     ngx_null_string   /* NONE */
  33. };


  34. static ngx_mail_protocol_t  ngx_mail_imap_protocol = {
  35.     ngx_string("imap"),
  36.     ngx_string("\x04imap"),
  37.     { 143, 993, 0, 0 },
  38.     NGX_MAIL_IMAP_PROTOCOL,

  39.     ngx_mail_imap_init_session,
  40.     ngx_mail_imap_init_protocol,
  41.     ngx_mail_imap_parse_command,
  42.     ngx_mail_imap_auth_state,

  43.     ngx_string("* BAD internal server error" CRLF),
  44.     ngx_string("* BYE SSL certificate error" CRLF),
  45.     ngx_string("* BYE No required SSL certificate" CRLF)
  46. };


  47. static ngx_command_t  ngx_mail_imap_commands[] = {

  48.     { ngx_string("imap_client_buffer"),
  49.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
  50.       ngx_conf_set_size_slot,
  51.       NGX_MAIL_SRV_CONF_OFFSET,
  52.       offsetof(ngx_mail_imap_srv_conf_t, client_buffer_size),
  53.       NULL },

  54.     { ngx_string("imap_capabilities"),
  55.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  56.       ngx_mail_capabilities,
  57.       NGX_MAIL_SRV_CONF_OFFSET,
  58.       offsetof(ngx_mail_imap_srv_conf_t, capabilities),
  59.       NULL },

  60.     { ngx_string("imap_auth"),
  61.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  62.       ngx_conf_set_bitmask_slot,
  63.       NGX_MAIL_SRV_CONF_OFFSET,
  64.       offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
  65.       &ngx_mail_imap_auth_methods },

  66.       ngx_null_command
  67. };


  68. static ngx_mail_module_t  ngx_mail_imap_module_ctx = {
  69.     &ngx_mail_imap_protocol,               /* protocol */

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

  72.     ngx_mail_imap_create_srv_conf,         /* create server configuration */
  73.     ngx_mail_imap_merge_srv_conf           /* merge server configuration */
  74. };


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


  89. static void *
  90. ngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
  91. {
  92.     ngx_mail_imap_srv_conf_t  *iscf;

  93.     iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
  94.     if (iscf == NULL) {
  95.         return NULL;
  96.     }

  97.     iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;

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

  103.     return iscf;
  104. }


  105. static char *
  106. ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  107. {
  108.     ngx_mail_imap_srv_conf_t *prev = parent;
  109.     ngx_mail_imap_srv_conf_t *conf = child;

  110.     u_char      *p, *auth;
  111.     size_t       size;
  112.     ngx_str_t   *c, *d;
  113.     ngx_uint_t   i, m;

  114.     ngx_conf_merge_size_value(conf->client_buffer_size,
  115.                               prev->client_buffer_size,
  116.                               (size_t) ngx_pagesize);

  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.     if (conf->capabilities.nelts == 0) {
  122.         conf->capabilities = prev->capabilities;
  123.     }

  124.     if (conf->capabilities.nelts == 0) {

  125.         for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
  126.             c = ngx_array_push(&conf->capabilities);
  127.             if (c == NULL) {
  128.                 return NGX_CONF_ERROR;
  129.             }

  130.             *c = *d;
  131.         }
  132.     }

  133.     size = sizeof("* CAPABILITY" CRLF) - 1;

  134.     c = conf->capabilities.elts;
  135.     for (i = 0; i < conf->capabilities.nelts; i++) {
  136.         size += 1 + c[i].len;
  137.     }

  138.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  139.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  140.          m <<= 1, i++)
  141.     {
  142.         if (m & conf->auth_methods) {
  143.             size += 1 + ngx_mail_imap_auth_methods_names[i].len;
  144.         }
  145.     }

  146.     p = ngx_pnalloc(cf->pool, size);
  147.     if (p == NULL) {
  148.         return NGX_CONF_ERROR;
  149.     }

  150.     conf->capability.len = size;
  151.     conf->capability.data = p;

  152.     p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);

  153.     for (i = 0; i < conf->capabilities.nelts; i++) {
  154.         *p++ = ' ';
  155.         p = ngx_cpymem(p, c[i].data, c[i].len);
  156.     }

  157.     auth = p;

  158.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  159.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  160.          m <<= 1, i++)
  161.     {
  162.         if (m & conf->auth_methods) {
  163.             *p++ = ' ';
  164.             p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
  165.                            ngx_mail_imap_auth_methods_names[i].len);
  166.         }
  167.     }

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


  169.     size += sizeof(" STARTTLS") - 1;

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

  174.     conf->starttls_capability.len = size;
  175.     conf->starttls_capability.data = p;

  176.     p = ngx_cpymem(p, conf->capability.data,
  177.                    conf->capability.len - (sizeof(CRLF) - 1));
  178.     p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
  179.     *p++ = CR; *p = LF;


  180.     size = (auth - conf->capability.data) + sizeof(CRLF) - 1
  181.             + sizeof(" STARTTLS LOGINDISABLED") - 1;

  182.     p = ngx_pnalloc(cf->pool, size);
  183.     if (p == NULL) {
  184.         return NGX_CONF_ERROR;
  185.     }

  186.     conf->starttls_only_capability.len = size;
  187.     conf->starttls_only_capability.data = p;

  188.     p = ngx_cpymem(p, conf->capability.data,
  189.                    auth - conf->capability.data);
  190.     p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
  191.                    sizeof(" STARTTLS LOGINDISABLED") - 1);
  192.     *p++ = CR; *p = LF;

  193.     return NGX_CONF_OK;
  194. }