Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

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_smtp_module.h>
0013 
0014 
0015 static void *ngx_mail_smtp_create_srv_conf(ngx_conf_t *cf);
0016 static char *ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent,
0017     void *child);
0018 
0019 
0020 static ngx_conf_bitmask_t  ngx_mail_smtp_auth_methods[] = {
0021     { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
0022     { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
0023     { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
0024     { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
0025     { ngx_string("none"), NGX_MAIL_AUTH_NONE_ENABLED },
0026     { ngx_null_string, 0 }
0027 };
0028 
0029 
0030 static ngx_str_t  ngx_mail_smtp_auth_methods_names[] = {
0031     ngx_string("PLAIN"),
0032     ngx_string("LOGIN"),
0033     ngx_null_string,  /* APOP */
0034     ngx_string("CRAM-MD5"),
0035     ngx_string("EXTERNAL"),
0036     ngx_null_string   /* NONE */
0037 };
0038 
0039 
0040 static ngx_mail_protocol_t  ngx_mail_smtp_protocol = {
0041     ngx_string("smtp"),
0042     { 25, 465, 587, 0 },
0043     NGX_MAIL_SMTP_PROTOCOL,
0044 
0045     ngx_mail_smtp_init_session,
0046     ngx_mail_smtp_init_protocol,
0047     ngx_mail_smtp_parse_command,
0048     ngx_mail_smtp_auth_state,
0049 
0050     ngx_string("451 4.3.2 Internal server error" CRLF),
0051     ngx_string("421 4.7.1 SSL certificate error" CRLF),
0052     ngx_string("421 4.7.1 No required SSL certificate" CRLF)
0053 };
0054 
0055 
0056 static ngx_command_t  ngx_mail_smtp_commands[] = {
0057 
0058     { ngx_string("smtp_client_buffer"),
0059       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
0060       ngx_conf_set_size_slot,
0061       NGX_MAIL_SRV_CONF_OFFSET,
0062       offsetof(ngx_mail_smtp_srv_conf_t, client_buffer_size),
0063       NULL },
0064 
0065     { ngx_string("smtp_greeting_delay"),
0066       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
0067       ngx_conf_set_msec_slot,
0068       NGX_MAIL_SRV_CONF_OFFSET,
0069       offsetof(ngx_mail_smtp_srv_conf_t, greeting_delay),
0070       NULL },
0071 
0072     { ngx_string("smtp_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_smtp_srv_conf_t, capabilities),
0077       NULL },
0078 
0079     { ngx_string("smtp_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_smtp_srv_conf_t, auth_methods),
0084       &ngx_mail_smtp_auth_methods },
0085 
0086       ngx_null_command
0087 };
0088 
0089 
0090 static ngx_mail_module_t  ngx_mail_smtp_module_ctx = {
0091     &ngx_mail_smtp_protocol,               /* protocol */
0092 
0093     NULL,                                  /* create main configuration */
0094     NULL,                                  /* init main configuration */
0095 
0096     ngx_mail_smtp_create_srv_conf,         /* create server configuration */
0097     ngx_mail_smtp_merge_srv_conf           /* merge server configuration */
0098 };
0099 
0100 
0101 ngx_module_t  ngx_mail_smtp_module = {
0102     NGX_MODULE_V1,
0103     &ngx_mail_smtp_module_ctx,             /* module context */
0104     ngx_mail_smtp_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_smtp_create_srv_conf(ngx_conf_t *cf)
0119 {
0120     ngx_mail_smtp_srv_conf_t  *sscf;
0121 
0122     sscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_smtp_srv_conf_t));
0123     if (sscf == NULL) {
0124         return NULL;
0125     }
0126 
0127     sscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
0128     sscf->greeting_delay = NGX_CONF_UNSET_MSEC;
0129 
0130     if (ngx_array_init(&sscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
0131         != NGX_OK)
0132     {
0133         return NULL;
0134     }
0135 
0136     return sscf;
0137 }
0138 
0139 
0140 static char *
0141 ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
0142 {
0143     ngx_mail_smtp_srv_conf_t *prev = parent;
0144     ngx_mail_smtp_srv_conf_t *conf = child;
0145 
0146     u_char                    *p, *auth, *last;
0147     size_t                     size;
0148     ngx_str_t                 *c;
0149     ngx_uint_t                 i, m, auth_enabled;
0150     ngx_mail_core_srv_conf_t  *cscf;
0151 
0152     ngx_conf_merge_size_value(conf->client_buffer_size,
0153                               prev->client_buffer_size,
0154                               (size_t) ngx_pagesize);
0155 
0156     ngx_conf_merge_msec_value(conf->greeting_delay,
0157                               prev->greeting_delay, 0);
0158 
0159     ngx_conf_merge_bitmask_value(conf->auth_methods,
0160                               prev->auth_methods,
0161                               (NGX_CONF_BITMASK_SET
0162                                |NGX_MAIL_AUTH_PLAIN_ENABLED
0163                                |NGX_MAIL_AUTH_LOGIN_ENABLED));
0164 
0165 
0166     cscf = ngx_mail_conf_get_module_srv_conf(cf, ngx_mail_core_module);
0167 
0168     size = sizeof("220  ESMTP ready" CRLF) - 1 + cscf->server_name.len;
0169 
0170     p = ngx_pnalloc(cf->pool, size);
0171     if (p == NULL) {
0172         return NGX_CONF_ERROR;
0173     }
0174 
0175     conf->greeting.len = size;
0176     conf->greeting.data = p;
0177 
0178     *p++ = '2'; *p++ = '2'; *p++ = '0'; *p++ = ' ';
0179     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
0180     ngx_memcpy(p, " ESMTP ready" CRLF, sizeof(" ESMTP ready" CRLF) - 1);
0181 
0182 
0183     size = sizeof("250 " CRLF) - 1 + cscf->server_name.len;
0184 
0185     p = ngx_pnalloc(cf->pool, size);
0186     if (p == NULL) {
0187         return NGX_CONF_ERROR;
0188     }
0189 
0190     conf->server_name.len = size;
0191     conf->server_name.data = p;
0192 
0193     *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
0194     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
0195     *p++ = CR; *p = LF;
0196 
0197 
0198     if (conf->capabilities.nelts == 0) {
0199         conf->capabilities = prev->capabilities;
0200     }
0201 
0202     size = sizeof("250-") - 1 + cscf->server_name.len + sizeof(CRLF) - 1;
0203 
0204     c = conf->capabilities.elts;
0205     for (i = 0; i < conf->capabilities.nelts; i++) {
0206         size += sizeof("250 ") - 1 + c[i].len + sizeof(CRLF) - 1;
0207     }
0208 
0209     auth_enabled = 0;
0210 
0211     for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
0212          m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
0213          m <<= 1, i++)
0214     {
0215         if (m & conf->auth_methods) {
0216             size += 1 + ngx_mail_smtp_auth_methods_names[i].len;
0217             auth_enabled = 1;
0218         }
0219     }
0220 
0221     if (auth_enabled) {
0222         size += sizeof("250 AUTH") - 1 + sizeof(CRLF) - 1;
0223     }
0224 
0225     p = ngx_pnalloc(cf->pool, size);
0226     if (p == NULL) {
0227         return NGX_CONF_ERROR;
0228     }
0229 
0230     conf->capability.len = size;
0231     conf->capability.data = p;
0232 
0233     last = p;
0234 
0235     *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
0236     p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
0237     *p++ = CR; *p++ = LF;
0238 
0239     for (i = 0; i < conf->capabilities.nelts; i++) {
0240         last = p;
0241         *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
0242         p = ngx_cpymem(p, c[i].data, c[i].len);
0243         *p++ = CR; *p++ = LF;
0244     }
0245 
0246     auth = p;
0247 
0248     if (auth_enabled) {
0249         last = p;
0250 
0251         *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
0252         *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H';
0253 
0254         for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
0255              m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
0256              m <<= 1, i++)
0257         {
0258             if (m & conf->auth_methods) {
0259                 *p++ = ' ';
0260                 p = ngx_cpymem(p, ngx_mail_smtp_auth_methods_names[i].data,
0261                                ngx_mail_smtp_auth_methods_names[i].len);
0262             }
0263         }
0264 
0265         *p++ = CR; *p = LF;
0266 
0267     } else {
0268         last[3] = ' ';
0269     }
0270 
0271     size += sizeof("250 STARTTLS" CRLF) - 1;
0272 
0273     p = ngx_pnalloc(cf->pool, size);
0274     if (p == NULL) {
0275         return NGX_CONF_ERROR;
0276     }
0277 
0278     conf->starttls_capability.len = size;
0279     conf->starttls_capability.data = p;
0280 
0281     p = ngx_cpymem(p, conf->capability.data, conf->capability.len);
0282 
0283     ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);
0284 
0285     p = conf->starttls_capability.data
0286         + (last - conf->capability.data) + 3;
0287     *p = '-';
0288 
0289     size = (auth - conf->capability.data)
0290             + sizeof("250 STARTTLS" CRLF) - 1;
0291 
0292     p = ngx_pnalloc(cf->pool, size);
0293     if (p == NULL) {
0294         return NGX_CONF_ERROR;
0295     }
0296 
0297     conf->starttls_only_capability.len = size;
0298     conf->starttls_only_capability.data = p;
0299 
0300     p = ngx_cpymem(p, conf->capability.data, auth - conf->capability.data);
0301 
0302     ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);
0303 
0304     if (last < auth) {
0305         p = conf->starttls_only_capability.data
0306             + (last - conf->capability.data) + 3;
0307         *p = '-';
0308     }
0309 
0310     return NGX_CONF_OK;
0311 }