src/mail/ngx_mail_imap_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_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_null_string, 0 }
  24. };


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


  32. static ngx_mail_protocol_t  ngx_mail_imap_protocol = {
  33.     ngx_string("imap"),
  34.     { 143, 993, 0, 0 },
  35.     NGX_MAIL_IMAP_PROTOCOL,

  36.     ngx_mail_imap_init_session,
  37.     ngx_mail_imap_init_protocol,
  38.     ngx_mail_imap_parse_command,
  39.     ngx_mail_imap_auth_state,

  40.     ngx_string("* BAD internal server error" CRLF)
  41. };


  42. static ngx_command_t  ngx_mail_imap_commands[] = {

  43.     { ngx_string("imap_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_imap_srv_conf_t, client_buffer_size),
  48.       NULL },

  49.     { ngx_string("imap_capabilities"),
  50.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  51.       ngx_mail_capabilities,
  52.       NGX_MAIL_SRV_CONF_OFFSET,
  53.       offsetof(ngx_mail_imap_srv_conf_t, capabilities),
  54.       NULL },

  55.     { ngx_string("imap_auth"),
  56.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  57.       ngx_conf_set_bitmask_slot,
  58.       NGX_MAIL_SRV_CONF_OFFSET,
  59.       offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
  60.       &ngx_mail_imap_auth_methods },

  61.       ngx_null_command
  62. };


  63. static ngx_mail_module_t  ngx_mail_imap_module_ctx = {
  64.     &ngx_mail_imap_protocol,               /* protocol */

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

  67.     ngx_mail_imap_create_srv_conf,         /* create server configuration */
  68.     ngx_mail_imap_merge_srv_conf           /* merge server configuration */
  69. };


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


  84. static void *
  85. ngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
  86. {
  87.     ngx_mail_imap_srv_conf_t  *iscf;

  88.     iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
  89.     if (iscf == NULL) {
  90.         return NULL;
  91.     }

  92.     iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;

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

  98.     return iscf;
  99. }


  100. static char *
  101. ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  102. {
  103.     ngx_mail_imap_srv_conf_t *prev = parent;
  104.     ngx_mail_imap_srv_conf_t *conf = child;

  105.     u_char      *p, *auth;
  106.     size_t       size;
  107.     ngx_str_t   *c, *d;
  108.     ngx_uint_t   i, m;

  109.     ngx_conf_merge_size_value(conf->client_buffer_size,
  110.                               prev->client_buffer_size,
  111.                               (size_t) ngx_pagesize);

  112.     ngx_conf_merge_bitmask_value(conf->auth_methods,
  113.                               prev->auth_methods,
  114.                               (NGX_CONF_BITMASK_SET
  115.                                |NGX_MAIL_AUTH_PLAIN_ENABLED));


  116.     if (conf->capabilities.nelts == 0) {
  117.         conf->capabilities = prev->capabilities;
  118.     }

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

  120.         for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
  121.             c = ngx_array_push(&conf->capabilities);
  122.             if (c == NULL) {
  123.                 return NGX_CONF_ERROR;
  124.             }

  125.             *c = *d;
  126.         }
  127.     }

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

  129.     c = conf->capabilities.elts;
  130.     for (i = 0; i < conf->capabilities.nelts; i++) {
  131.         size += 1 + c[i].len;
  132.     }

  133.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  134.          m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
  135.          m <<= 1, i++)
  136.     {
  137.         if (m & conf->auth_methods) {
  138.             size += 1 + ngx_mail_imap_auth_methods_names[i].len;
  139.         }
  140.     }

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

  145.     conf->capability.len = size;
  146.     conf->capability.data = p;

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

  148.     for (i = 0; i < conf->capabilities.nelts; i++) {
  149.         *p++ = ' ';
  150.         p = ngx_cpymem(p, c[i].data, c[i].len);
  151.     }

  152.     auth = p;

  153.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  154.          m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
  155.          m <<= 1, i++)
  156.     {
  157.         if (m & conf->auth_methods) {
  158.             *p++ = ' ';
  159.             p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
  160.                            ngx_mail_imap_auth_methods_names[i].len);
  161.         }
  162.     }

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


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

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

  169.     conf->starttls_capability.len = size;
  170.     conf->starttls_capability.data = p;

  171.     p = ngx_cpymem(p, conf->capability.data,
  172.                    conf->capability.len - (sizeof(CRLF) - 1));
  173.     p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
  174.     *p++ = CR; *p = LF;


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

  177.     p = ngx_pnalloc(cf->pool, size);
  178.     if (p == NULL) {
  179.         return NGX_CONF_ERROR;
  180.     }

  181.     conf->starttls_only_capability.len = size;
  182.     conf->starttls_only_capability.data = p;

  183.     p = ngx_cpymem(p, conf->capability.data,
  184.                    auth - conf->capability.data);
  185.     p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
  186.                    sizeof(" STARTTLS LOGINDISABLED") - 1);
  187.     *p++ = CR; *p = LF;

  188.     return NGX_CONF_OK;
  189. }