src/mail/ngx_mail_pop3_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_pop3_module.h>


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


  13. static ngx_str_t  ngx_mail_pop3_default_capabilities[] = {
  14.     ngx_string("TOP"),
  15.     ngx_string("USER"),
  16.     ngx_string("UIDL"),
  17.     ngx_null_string
  18. };


  19. static ngx_conf_bitmask_t  ngx_mail_pop3_auth_methods[] = {
  20.     { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
  21.     { ngx_string("apop"), NGX_MAIL_AUTH_APOP_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_pop3_auth_methods_names[] = {
  27.     ngx_string("PLAIN"),
  28.     ngx_string("LOGIN"),
  29.     ngx_null_string/* APOP */
  30.     ngx_string("CRAM-MD5"),
  31.     ngx_string("EXTERNAL"),
  32.     ngx_null_string   /* NONE */
  33. };


  34. static ngx_mail_protocol_t  ngx_mail_pop3_protocol = {
  35.     ngx_string("pop3"),
  36.     ngx_string("\x04pop3"),
  37.     { 110, 995, 0, 0 },
  38.     NGX_MAIL_POP3_PROTOCOL,

  39.     ngx_mail_pop3_init_session,
  40.     ngx_mail_pop3_init_protocol,
  41.     ngx_mail_pop3_parse_command,
  42.     ngx_mail_pop3_auth_state,

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


  47. static ngx_command_t  ngx_mail_pop3_commands[] = {

  48.     { ngx_string("pop3_capabilities"),
  49.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  50.       ngx_mail_capabilities,
  51.       NGX_MAIL_SRV_CONF_OFFSET,
  52.       offsetof(ngx_mail_pop3_srv_conf_t, capabilities),
  53.       NULL },

  54.     { ngx_string("pop3_auth"),
  55.       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
  56.       ngx_conf_set_bitmask_slot,
  57.       NGX_MAIL_SRV_CONF_OFFSET,
  58.       offsetof(ngx_mail_pop3_srv_conf_t, auth_methods),
  59.       &ngx_mail_pop3_auth_methods },

  60.       ngx_null_command
  61. };


  62. static ngx_mail_module_t  ngx_mail_pop3_module_ctx = {
  63.     &ngx_mail_pop3_protocol,               /* protocol */

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

  66.     ngx_mail_pop3_create_srv_conf,         /* create server configuration */
  67.     ngx_mail_pop3_merge_srv_conf           /* merge server configuration */
  68. };


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


  83. static void *
  84. ngx_mail_pop3_create_srv_conf(ngx_conf_t *cf)
  85. {
  86.     ngx_mail_pop3_srv_conf_t  *pscf;

  87.     pscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_pop3_srv_conf_t));
  88.     if (pscf == NULL) {
  89.         return NULL;
  90.     }

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

  96.     return pscf;
  97. }


  98. static char *
  99. ngx_mail_pop3_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
  100. {
  101.     ngx_mail_pop3_srv_conf_t *prev = parent;
  102.     ngx_mail_pop3_srv_conf_t *conf = child;

  103.     u_char      *p;
  104.     size_t       size, stls_only_size;
  105.     ngx_str_t   *c, *d;
  106.     ngx_uint_t   i, m;

  107.     ngx_conf_merge_bitmask_value(conf->auth_methods,
  108.                                  prev->auth_methods,
  109.                                  (NGX_CONF_BITMASK_SET
  110.                                   |NGX_MAIL_AUTH_PLAIN_ENABLED));

  111.     if (conf->auth_methods & NGX_MAIL_AUTH_PLAIN_ENABLED) {
  112.         conf->auth_methods |= NGX_MAIL_AUTH_LOGIN_ENABLED;
  113.     }

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

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

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

  123.             *c = *d;
  124.         }
  125.     }

  126.     size = sizeof("+OK Capability list follows" CRLF) - 1
  127.            + sizeof("." CRLF) - 1;

  128.     stls_only_size = size + sizeof("STLS" CRLF) - 1;

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

  132.         if (ngx_strcasecmp(c[i].data, (u_char *) "USER") == 0) {
  133.             continue;
  134.         }

  135.         stls_only_size += c[i].len + sizeof(CRLF) - 1;
  136.     }

  137.     size += sizeof("SASL") - 1 + sizeof(CRLF) - 1;

  138.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  139.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  140.          m <<= 1, i++)
  141.     {
  142.         if (ngx_mail_pop3_auth_methods_names[i].len == 0) {
  143.             continue;
  144.         }

  145.         if (m & conf->auth_methods) {
  146.             size += 1 + ngx_mail_pop3_auth_methods_names[i].len;
  147.         }
  148.     }

  149.     p = ngx_pnalloc(cf->pool, size);
  150.     if (p == NULL) {
  151.         return NGX_CONF_ERROR;
  152.     }

  153.     conf->capability.len = size;
  154.     conf->capability.data = p;

  155.     p = ngx_cpymem(p, "+OK Capability list follows" CRLF,
  156.                    sizeof("+OK Capability list follows" CRLF) - 1);

  157.     for (i = 0; i < conf->capabilities.nelts; i++) {
  158.         p = ngx_cpymem(p, c[i].data, c[i].len);
  159.         *p++ = CR; *p++ = LF;
  160.     }

  161.     p = ngx_cpymem(p, "SASL", sizeof("SASL") - 1);

  162.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  163.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  164.          m <<= 1, i++)
  165.     {
  166.         if (ngx_mail_pop3_auth_methods_names[i].len == 0) {
  167.             continue;
  168.         }

  169.         if (m & conf->auth_methods) {
  170.             *p++ = ' ';
  171.             p = ngx_cpymem(p, ngx_mail_pop3_auth_methods_names[i].data,
  172.                            ngx_mail_pop3_auth_methods_names[i].len);
  173.         }
  174.     }

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

  176.     *p++ = '.'; *p++ = CR; *p = LF;


  177.     size += sizeof("STLS" CRLF) - 1;

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

  182.     conf->starttls_capability.len = size;
  183.     conf->starttls_capability.data = p;

  184.     p = ngx_cpymem(p, conf->capability.data,
  185.                    conf->capability.len - (sizeof("." CRLF) - 1));

  186.     p = ngx_cpymem(p, "STLS" CRLF, sizeof("STLS" CRLF) - 1);
  187.     *p++ = '.'; *p++ = CR; *p = LF;


  188.     size = sizeof("+OK methods supported:" CRLF) - 1
  189.            + sizeof("." CRLF) - 1;

  190.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  191.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  192.          m <<= 1, i++)
  193.     {
  194.         if (ngx_mail_pop3_auth_methods_names[i].len == 0) {
  195.             continue;
  196.         }

  197.         if (m & conf->auth_methods) {
  198.             size += ngx_mail_pop3_auth_methods_names[i].len
  199.                     + sizeof(CRLF) - 1;
  200.         }
  201.     }

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

  206.     conf->auth_capability.data = p;
  207.     conf->auth_capability.len = size;

  208.     p = ngx_cpymem(p, "+OK methods supported:" CRLF,
  209.                    sizeof("+OK methods supported:" CRLF) - 1);

  210.     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
  211.          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
  212.          m <<= 1, i++)
  213.     {
  214.         if (ngx_mail_pop3_auth_methods_names[i].len == 0) {
  215.             continue;
  216.         }

  217.         if (m & conf->auth_methods) {
  218.             p = ngx_cpymem(p, ngx_mail_pop3_auth_methods_names[i].data,
  219.                            ngx_mail_pop3_auth_methods_names[i].len);
  220.             *p++ = CR; *p++ = LF;
  221.         }
  222.     }

  223.     *p++ = '.'; *p++ = CR; *p = LF;


  224.     p = ngx_pnalloc(cf->pool, stls_only_size);
  225.     if (p == NULL) {
  226.         return NGX_CONF_ERROR;
  227.     }

  228.     conf->starttls_only_capability.len = stls_only_size;
  229.     conf->starttls_only_capability.data = p;

  230.     p = ngx_cpymem(p, "+OK Capability list follows" CRLF,
  231.                    sizeof("+OK Capability list follows" CRLF) - 1);

  232.     for (i = 0; i < conf->capabilities.nelts; i++) {
  233.         if (ngx_strcasecmp(c[i].data, (u_char *) "USER") == 0) {
  234.             continue;
  235.         }

  236.         p = ngx_cpymem(p, c[i].data, c[i].len);
  237.         *p++ = CR; *p++ = LF;
  238.     }

  239.     p = ngx_cpymem(p, "STLS" CRLF, sizeof("STLS" CRLF) - 1);
  240.     *p++ = '.'; *p++ = CR; *p = LF;

  241.     return NGX_CONF_OK;
  242. }