Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_event.h>
0011 #include <ngx_mail.h>
0012 #include <ngx_mail_imap_module.h>
0013 
0014 
0015 static void *ngx_mail_imap_create_srv_conf(ngx_conf_t *cf);
0016 static char *ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent,
0017     void *child);
0018 
0019 
0020 static ngx_str_t  ngx_mail_imap_default_capabilities[] = {
0021     ngx_string("IMAP4"),
0022     ngx_string("IMAP4rev1"),
0023     ngx_string("UIDPLUS"),
0024     ngx_null_string
0025 };
0026 
0027 
0028 static ngx_conf_bitmask_t  ngx_mail_imap_auth_methods[] = {
0029     { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
0030     { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
0031     { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
0032     { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
0033     { ngx_null_string, 0 }
0034 };
0035 
0036 
0037 static ngx_str_t  ngx_mail_imap_auth_methods_names[] = {
0038     ngx_string("AUTH=PLAIN"),
0039     ngx_string("AUTH=LOGIN"),
0040     ngx_null_string,  /* APOP */
0041     ngx_string("AUTH=CRAM-MD5"),
0042     ngx_string("AUTH=EXTERNAL"),
0043     ngx_null_string   /* NONE */
0044 };
0045 
0046 
0047 static ngx_mail_protocol_t  ngx_mail_imap_protocol = {
0048     ngx_string("imap"),
0049     { 143, 993, 0, 0 },
0050     NGX_MAIL_IMAP_PROTOCOL,
0051 
0052     ngx_mail_imap_init_session,
0053     ngx_mail_imap_init_protocol,
0054     ngx_mail_imap_parse_command,
0055     ngx_mail_imap_auth_state,
0056 
0057     ngx_string("* BAD internal server error" CRLF),
0058     ngx_string("* BYE SSL certificate error" CRLF),
0059     ngx_string("* BYE No required SSL certificate" CRLF)
0060 };
0061 
0062 
0063 static ngx_command_t  ngx_mail_imap_commands[] = {
0064 
0065     { ngx_string("imap_client_buffer"),
0066       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
0067       ngx_conf_set_size_slot,
0068       NGX_MAIL_SRV_CONF_OFFSET,
0069       offsetof(ngx_mail_imap_srv_conf_t, client_buffer_size),
0070       NULL },
0071 
0072     { ngx_string("imap_capabilities"),
0073       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
0074       ngx_mail_capabilities,
0075       NGX_MAIL_SRV_CONF_OFFSET,
0076       offsetof(ngx_mail_imap_srv_conf_t, capabilities),
0077       NULL },
0078 
0079     { ngx_string("imap_auth"),
0080       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
0081       ngx_conf_set_bitmask_slot,
0082       NGX_MAIL_SRV_CONF_OFFSET,
0083       offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
0084       &ngx_mail_imap_auth_methods },
0085 
0086       ngx_null_command
0087 };
0088 
0089 
0090 static ngx_mail_module_t  ngx_mail_imap_module_ctx = {
0091     &ngx_mail_imap_protocol,               /* protocol */
0092 
0093     NULL,                                  /* create main configuration */
0094     NULL,                                  /* init main configuration */
0095 
0096     ngx_mail_imap_create_srv_conf,         /* create server configuration */
0097     ngx_mail_imap_merge_srv_conf           /* merge server configuration */
0098 };
0099 
0100 
0101 ngx_module_t  ngx_mail_imap_module = {
0102     NGX_MODULE_V1,
0103     &ngx_mail_imap_module_ctx,             /* module context */
0104     ngx_mail_imap_commands,                /* module directives */
0105     NGX_MAIL_MODULE,                       /* module type */
0106     NULL,                                  /* init master */
0107     NULL,                                  /* init module */
0108     NULL,                                  /* init process */
0109     NULL,                                  /* init thread */
0110     NULL,                                  /* exit thread */
0111     NULL,                                  /* exit process */
0112     NULL,                                  /* exit master */
0113     NGX_MODULE_V1_PADDING
0114 };
0115 
0116 
0117 static void *
0118 ngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
0119 {
0120     ngx_mail_imap_srv_conf_t  *iscf;
0121 
0122     iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
0123     if (iscf == NULL) {
0124         return NULL;
0125     }
0126 
0127     iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
0128 
0129     if (ngx_array_init(&iscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
0130         != NGX_OK)
0131     {
0132         return NULL;
0133     }
0134 
0135     return iscf;
0136 }
0137 
0138 
0139 static char *
0140 ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
0141 {
0142     ngx_mail_imap_srv_conf_t *prev = parent;
0143     ngx_mail_imap_srv_conf_t *conf = child;
0144 
0145     u_char      *p, *auth;
0146     size_t       size;
0147     ngx_str_t   *c, *d;
0148     ngx_uint_t   i, m;
0149 
0150     ngx_conf_merge_size_value(conf->client_buffer_size,
0151                               prev->client_buffer_size,
0152                               (size_t) ngx_pagesize);
0153 
0154     ngx_conf_merge_bitmask_value(conf->auth_methods,
0155                               prev->auth_methods,
0156                               (NGX_CONF_BITMASK_SET
0157                                |NGX_MAIL_AUTH_PLAIN_ENABLED));
0158 
0159 
0160     if (conf->capabilities.nelts == 0) {
0161         conf->capabilities = prev->capabilities;
0162     }
0163 
0164     if (conf->capabilities.nelts == 0) {
0165 
0166         for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
0167             c = ngx_array_push(&conf->capabilities);
0168             if (c == NULL) {
0169                 return NGX_CONF_ERROR;
0170             }
0171 
0172             *c = *d;
0173         }
0174     }
0175 
0176     size = sizeof("* CAPABILITY" CRLF) - 1;
0177 
0178     c = conf->capabilities.elts;
0179     for (i = 0; i < conf->capabilities.nelts; i++) {
0180         size += 1 + c[i].len;
0181     }
0182 
0183     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
0184          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
0185          m <<= 1, i++)
0186     {
0187         if (m & conf->auth_methods) {
0188             size += 1 + ngx_mail_imap_auth_methods_names[i].len;
0189         }
0190     }
0191 
0192     p = ngx_pnalloc(cf->pool, size);
0193     if (p == NULL) {
0194         return NGX_CONF_ERROR;
0195     }
0196 
0197     conf->capability.len = size;
0198     conf->capability.data = p;
0199 
0200     p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
0201 
0202     for (i = 0; i < conf->capabilities.nelts; i++) {
0203         *p++ = ' ';
0204         p = ngx_cpymem(p, c[i].data, c[i].len);
0205     }
0206 
0207     auth = p;
0208 
0209     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
0210          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
0211          m <<= 1, i++)
0212     {
0213         if (m & conf->auth_methods) {
0214             *p++ = ' ';
0215             p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
0216                            ngx_mail_imap_auth_methods_names[i].len);
0217         }
0218     }
0219 
0220     *p++ = CR; *p = LF;
0221 
0222 
0223     size += sizeof(" STARTTLS") - 1;
0224 
0225     p = ngx_pnalloc(cf->pool, size);
0226     if (p == NULL) {
0227         return NGX_CONF_ERROR;
0228     }
0229 
0230     conf->starttls_capability.len = size;
0231     conf->starttls_capability.data = p;
0232 
0233     p = ngx_cpymem(p, conf->capability.data,
0234                    conf->capability.len - (sizeof(CRLF) - 1));
0235     p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
0236     *p++ = CR; *p = LF;
0237 
0238 
0239     size = (auth - conf->capability.data) + sizeof(CRLF) - 1
0240             + sizeof(" STARTTLS LOGINDISABLED") - 1;
0241 
0242     p = ngx_pnalloc(cf->pool, size);
0243     if (p == NULL) {
0244         return NGX_CONF_ERROR;
0245     }
0246 
0247     conf->starttls_only_capability.len = size;
0248     conf->starttls_only_capability.data = p;
0249 
0250     p = ngx_cpymem(p, conf->capability.data,
0251                    auth - conf->capability.data);
0252     p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
0253                    sizeof(" STARTTLS LOGINDISABLED") - 1);
0254     *p++ = CR; *p = LF;
0255 
0256     return NGX_CONF_OK;
0257 }