blob: 90f2b1b2b16922abd3f29df7073d58b6c9e5f4a5 [file] [log] [blame]
Igor Sysoevceb99292005-09-06 16:09:32 +00001
2/*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7#include <ngx_config.h>
8#include <ngx_core.h>
Igor Sysoev02c8d182007-03-19 13:36:56 +00009#include <ngx_mail.h>
Igor Sysoevceb99292005-09-06 16:09:32 +000010
11
Igor Sysoev3bed0da2009-10-07 14:46:13 +000012#define NGX_DEFAULT_CIPHERS "HIGH:!ADH:!MD5"
Igor Sysoevceb99292005-09-06 16:09:32 +000013
14
Igor Sysoev02c8d182007-03-19 13:36:56 +000015static void *ngx_mail_ssl_create_conf(ngx_conf_t *cf);
16static char *ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child);
Igor Sysoevf100c782008-09-01 14:19:01 +000017
18static char *ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd,
19 void *conf);
20static char *ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd,
21 void *conf);
Igor Sysoev02c8d182007-03-19 13:36:56 +000022static char *ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoeva4c84c52007-01-03 16:11:53 +000023 void *conf);
Igor Sysoevceb99292005-09-06 16:09:32 +000024
25
Igor Sysoevd3283ff2005-12-05 13:18:09 +000026static ngx_conf_enum_t ngx_http_starttls_state[] = {
Igor Sysoev02c8d182007-03-19 13:36:56 +000027 { ngx_string("off"), NGX_MAIL_STARTTLS_OFF },
28 { ngx_string("on"), NGX_MAIL_STARTTLS_ON },
29 { ngx_string("only"), NGX_MAIL_STARTTLS_ONLY },
Igor Sysoevd3283ff2005-12-05 13:18:09 +000030 { ngx_null_string, 0 }
31};
32
33
34
Igor Sysoev02c8d182007-03-19 13:36:56 +000035static ngx_conf_bitmask_t ngx_mail_ssl_protocols[] = {
Igor Sysoev9fa5a822005-09-30 14:41:25 +000036 { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
37 { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
38 { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
39 { ngx_null_string, 0 }
40};
41
42
Igor Sysoev02c8d182007-03-19 13:36:56 +000043static ngx_command_t ngx_mail_ssl_commands[] = {
Igor Sysoevceb99292005-09-06 16:09:32 +000044
45 { ngx_string("ssl"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000046 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
Igor Sysoevf100c782008-09-01 14:19:01 +000047 ngx_mail_ssl_enable,
Igor Sysoev02c8d182007-03-19 13:36:56 +000048 NGX_MAIL_SRV_CONF_OFFSET,
49 offsetof(ngx_mail_ssl_conf_t, enable),
Igor Sysoevceb99292005-09-06 16:09:32 +000050 NULL },
51
Igor Sysoevd3283ff2005-12-05 13:18:09 +000052 { ngx_string("starttls"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000053 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevf100c782008-09-01 14:19:01 +000054 ngx_mail_ssl_starttls,
Igor Sysoev02c8d182007-03-19 13:36:56 +000055 NGX_MAIL_SRV_CONF_OFFSET,
56 offsetof(ngx_mail_ssl_conf_t, starttls),
Igor Sysoevd3283ff2005-12-05 13:18:09 +000057 ngx_http_starttls_state },
58
Igor Sysoevceb99292005-09-06 16:09:32 +000059 { ngx_string("ssl_certificate"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000060 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevceb99292005-09-06 16:09:32 +000061 ngx_conf_set_str_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000062 NGX_MAIL_SRV_CONF_OFFSET,
63 offsetof(ngx_mail_ssl_conf_t, certificate),
Igor Sysoevceb99292005-09-06 16:09:32 +000064 NULL },
65
66 { ngx_string("ssl_certificate_key"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000067 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevceb99292005-09-06 16:09:32 +000068 ngx_conf_set_str_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000069 NGX_MAIL_SRV_CONF_OFFSET,
70 offsetof(ngx_mail_ssl_conf_t, certificate_key),
Igor Sysoevceb99292005-09-06 16:09:32 +000071 NULL },
72
Igor Sysoevdf83e6f2008-06-16 05:51:32 +000073 { ngx_string("ssl_dhparam"),
74 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
75 ngx_conf_set_str_slot,
76 NGX_MAIL_SRV_CONF_OFFSET,
77 offsetof(ngx_mail_ssl_conf_t, dhparam),
78 NULL },
79
Igor Sysoev9fa5a822005-09-30 14:41:25 +000080 { ngx_string("ssl_protocols"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000081 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
Igor Sysoev9fa5a822005-09-30 14:41:25 +000082 ngx_conf_set_bitmask_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000083 NGX_MAIL_SRV_CONF_OFFSET,
84 offsetof(ngx_mail_ssl_conf_t, protocols),
85 &ngx_mail_ssl_protocols },
Igor Sysoev9fa5a822005-09-30 14:41:25 +000086
Igor Sysoevceb99292005-09-06 16:09:32 +000087 { ngx_string("ssl_ciphers"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000088 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoevceb99292005-09-06 16:09:32 +000089 ngx_conf_set_str_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000090 NGX_MAIL_SRV_CONF_OFFSET,
91 offsetof(ngx_mail_ssl_conf_t, ciphers),
Igor Sysoevceb99292005-09-06 16:09:32 +000092 NULL },
93
Igor Sysoev9fa5a822005-09-30 14:41:25 +000094 { ngx_string("ssl_prefer_server_ciphers"),
Igor Sysoev02c8d182007-03-19 13:36:56 +000095 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
Igor Sysoev9fa5a822005-09-30 14:41:25 +000096 ngx_conf_set_flag_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +000097 NGX_MAIL_SRV_CONF_OFFSET,
98 offsetof(ngx_mail_ssl_conf_t, prefer_server_ciphers),
Igor Sysoev9fa5a822005-09-30 14:41:25 +000099 NULL },
Igor Sysoevc2068d02005-10-19 12:33:58 +0000100
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000101 { ngx_string("ssl_session_cache"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000102 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE12,
103 ngx_mail_ssl_session_cache,
104 NGX_MAIL_SRV_CONF_OFFSET,
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000105 0,
106 NULL },
107
Igor Sysoev09c684b2005-11-09 17:25:55 +0000108 { ngx_string("ssl_session_timeout"),
Igor Sysoev02c8d182007-03-19 13:36:56 +0000109 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
Igor Sysoev09c684b2005-11-09 17:25:55 +0000110 ngx_conf_set_sec_slot,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000111 NGX_MAIL_SRV_CONF_OFFSET,
112 offsetof(ngx_mail_ssl_conf_t, session_timeout),
Igor Sysoev09c684b2005-11-09 17:25:55 +0000113 NULL },
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000114
Igor Sysoevceb99292005-09-06 16:09:32 +0000115 ngx_null_command
116};
117
118
Igor Sysoev02c8d182007-03-19 13:36:56 +0000119static ngx_mail_module_t ngx_mail_ssl_module_ctx = {
Igor Sysoev48714082007-09-15 16:51:16 +0000120 NULL, /* protocol */
121
Igor Sysoevceb99292005-09-06 16:09:32 +0000122 NULL, /* create main configuration */
123 NULL, /* init main configuration */
124
Igor Sysoev02c8d182007-03-19 13:36:56 +0000125 ngx_mail_ssl_create_conf, /* create server configuration */
126 ngx_mail_ssl_merge_conf /* merge server configuration */
Igor Sysoevceb99292005-09-06 16:09:32 +0000127};
128
129
Igor Sysoev02c8d182007-03-19 13:36:56 +0000130ngx_module_t ngx_mail_ssl_module = {
Igor Sysoevceb99292005-09-06 16:09:32 +0000131 NGX_MODULE_V1,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000132 &ngx_mail_ssl_module_ctx, /* module context */
133 ngx_mail_ssl_commands, /* module directives */
134 NGX_MAIL_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000135 NULL, /* init master */
Igor Sysoevceb99292005-09-06 16:09:32 +0000136 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000137 NULL, /* init process */
138 NULL, /* init thread */
139 NULL, /* exit thread */
140 NULL, /* exit process */
141 NULL, /* exit master */
142 NGX_MODULE_V1_PADDING
Igor Sysoevceb99292005-09-06 16:09:32 +0000143};
144
145
Igor Sysoev02c8d182007-03-19 13:36:56 +0000146static ngx_str_t ngx_mail_ssl_sess_id_ctx = ngx_string("MAIL");
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000147
148
Igor Sysoevceb99292005-09-06 16:09:32 +0000149static void *
Igor Sysoev02c8d182007-03-19 13:36:56 +0000150ngx_mail_ssl_create_conf(ngx_conf_t *cf)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000151{
Igor Sysoev02c8d182007-03-19 13:36:56 +0000152 ngx_mail_ssl_conf_t *scf;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000153
Igor Sysoev02c8d182007-03-19 13:36:56 +0000154 scf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_ssl_conf_t));
Igor Sysoevceb99292005-09-06 16:09:32 +0000155 if (scf == NULL) {
Igor Sysoev260c4322009-06-02 16:09:44 +0000156 return NULL;
Igor Sysoevceb99292005-09-06 16:09:32 +0000157 }
158
159 /*
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000160 * set by ngx_pcalloc():
Igor Sysoevceb99292005-09-06 16:09:32 +0000161 *
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000162 * scf->protocols = 0;
Igor Sysoevdf83e6f2008-06-16 05:51:32 +0000163 * scf->certificate = { 0, NULL };
164 * scf->certificate_key = { 0, NULL };
165 * scf->dhparam = { 0, NULL };
Igor Sysoevceb99292005-09-06 16:09:32 +0000166 * scf->ciphers.len = 0;
167 * scf->ciphers.data = NULL;
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000168 * scf->shm_zone = NULL;
Igor Sysoevceb99292005-09-06 16:09:32 +0000169 */
170
171 scf->enable = NGX_CONF_UNSET;
Igor Sysoev2ac565f2009-04-27 11:33:34 +0000172 scf->starttls = NGX_CONF_UNSET_UINT;
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000173 scf->prefer_server_ciphers = NGX_CONF_UNSET;
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000174 scf->builtin_session_cache = NGX_CONF_UNSET;
175 scf->session_timeout = NGX_CONF_UNSET;
Igor Sysoevceb99292005-09-06 16:09:32 +0000176
177 return scf;
178}
179
180
181static char *
Igor Sysoev02c8d182007-03-19 13:36:56 +0000182ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
Igor Sysoevceb99292005-09-06 16:09:32 +0000183{
Igor Sysoev02c8d182007-03-19 13:36:56 +0000184 ngx_mail_ssl_conf_t *prev = parent;
185 ngx_mail_ssl_conf_t *conf = child;
Igor Sysoevceb99292005-09-06 16:09:32 +0000186
Igor Sysoevf100c782008-09-01 14:19:01 +0000187 char *mode;
Igor Sysoevc2068d02005-10-19 12:33:58 +0000188 ngx_pool_cleanup_t *cln;
189
Igor Sysoevceb99292005-09-06 16:09:32 +0000190 ngx_conf_merge_value(conf->enable, prev->enable, 0);
Igor Sysoevf100c782008-09-01 14:19:01 +0000191 ngx_conf_merge_uint_value(conf->starttls, prev->starttls,
192 NGX_MAIL_STARTTLS_OFF);
Igor Sysoevceb99292005-09-06 16:09:32 +0000193
Igor Sysoev09c684b2005-11-09 17:25:55 +0000194 ngx_conf_merge_value(conf->session_timeout,
195 prev->session_timeout, 300);
196
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000197 ngx_conf_merge_value(conf->prefer_server_ciphers,
198 prev->prefer_server_ciphers, 0);
199
200 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
Igor Sysoevcf9dd762009-10-06 14:24:53 +0000201 (NGX_CONF_BITMASK_SET|NGX_SSL_SSLv3|NGX_SSL_TLSv1));
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000202
Igor Sysoevf100c782008-09-01 14:19:01 +0000203 ngx_conf_merge_str_value(conf->certificate, prev->certificate, "");
204 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, "");
Igor Sysoevceb99292005-09-06 16:09:32 +0000205
Igor Sysoevdf83e6f2008-06-16 05:51:32 +0000206 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, "");
207
Igor Sysoev4c756c42008-07-29 14:31:03 +0000208 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS);
Igor Sysoevceb99292005-09-06 16:09:32 +0000209
210
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000211 conf->ssl.log = cf->log;
Igor Sysoevceb99292005-09-06 16:09:32 +0000212
Igor Sysoevf100c782008-09-01 14:19:01 +0000213 if (conf->enable) {
214 mode = "ssl";
215
216 } else if (conf->starttls != NGX_MAIL_STARTTLS_OFF) {
217 mode = "starttls";
218
219 } else {
220 mode = "";
221 }
222
223 if (*mode) {
224
225 if (conf->certificate.len == 0) {
226 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
227 "no \"ssl_certificate\" is defined for "
228 "the \"%s\" directive in %s:%ui",
229 mode, conf->file, conf->line);
230 return NGX_CONF_ERROR;
231 }
232
233 if (conf->certificate_key.len == 0) {
234 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
235 "no \"ssl_certificate_key\" is defined for "
236 "the \"%s\" directive in %s:%ui",
237 mode, conf->file, conf->line);
238 return NGX_CONF_ERROR;
239 }
240
241 } else {
242
243 if (conf->certificate.len == 0) {
244 return NGX_CONF_OK;
245 }
246
247 if (conf->certificate_key.len == 0) {
248 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
249 "no \"ssl_certificate_key\" is defined "
250 "for certificate \"%V\"",
251 &conf->certificate);
252 return NGX_CONF_ERROR;
253 }
254 }
255
Igor Sysoevebf2bbc2007-01-02 23:37:25 +0000256 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) {
Igor Sysoevceb99292005-09-06 16:09:32 +0000257 return NGX_CONF_ERROR;
258 }
259
Igor Sysoevc2068d02005-10-19 12:33:58 +0000260 cln = ngx_pool_cleanup_add(cf->pool, 0);
261 if (cln == NULL) {
Igor Sysoevceb99292005-09-06 16:09:32 +0000262 return NGX_CONF_ERROR;
263 }
264
Igor Sysoevc2068d02005-10-19 12:33:58 +0000265 cln->handler = ngx_ssl_cleanup_ctx;
266 cln->data = &conf->ssl;
267
268 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
269 &conf->certificate_key)
270 != NGX_OK)
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000271 {
272 return NGX_CONF_ERROR;
273 }
Igor Sysoevceb99292005-09-06 16:09:32 +0000274
275 if (conf->ciphers.len) {
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000276 if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
Igor Sysoevc2068d02005-10-19 12:33:58 +0000277 (const char *) conf->ciphers.data)
278 == 0)
Igor Sysoevceb99292005-09-06 16:09:32 +0000279 {
280 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
281 "SSL_CTX_set_cipher_list(\"%V\") failed",
282 &conf->ciphers);
283 }
284 }
285
Igor Sysoevc2068d02005-10-19 12:33:58 +0000286 if (conf->prefer_server_ciphers) {
287 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
288 }
289
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000290 if (ngx_ssl_generate_rsa512_key(&conf->ssl) != NGX_OK) {
Igor Sysoevceb99292005-09-06 16:09:32 +0000291 return NGX_CONF_ERROR;
292 }
293
Igor Sysoevdf83e6f2008-06-16 05:51:32 +0000294 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) {
295 return NGX_CONF_ERROR;
296 }
297
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000298 ngx_conf_merge_value(conf->builtin_session_cache,
Igor Sysoevd6548fa2008-05-26 07:14:13 +0000299 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE);
Igor Sysoevceb99292005-09-06 16:09:32 +0000300
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000301 if (conf->shm_zone == NULL) {
302 conf->shm_zone = prev->shm_zone;
303 }
Igor Sysoeve5733802005-09-08 14:36:09 +0000304
Igor Sysoev02c8d182007-03-19 13:36:56 +0000305 if (ngx_ssl_session_cache(&conf->ssl, &ngx_mail_ssl_sess_id_ctx,
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000306 conf->builtin_session_cache,
307 conf->shm_zone, conf->session_timeout)
308 != NGX_OK)
309 {
310 return NGX_CONF_ERROR;
311 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000312
Igor Sysoevceb99292005-09-06 16:09:32 +0000313 return NGX_CONF_OK;
314}
Igor Sysoevc2068d02005-10-19 12:33:58 +0000315
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000316
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000317static char *
Igor Sysoevf100c782008-09-01 14:19:01 +0000318ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
319{
320 ngx_mail_ssl_conf_t *scf = conf;
321
322 char *rv;
323
324 rv = ngx_conf_set_flag_slot(cf, cmd, conf);
325
326 if (rv != NGX_CONF_OK) {
327 return rv;
328 }
329
330 if (scf->enable && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) {
331 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
332 "\"starttls\" directive conflicts with \"ssl on\"");
333 return NGX_CONF_ERROR;
334 }
335
336 scf->file = cf->conf_file->file.name.data;
337 scf->line = cf->conf_file->line;
338
339 return NGX_CONF_OK;
340}
341
342
343static char *
344ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
345{
346 ngx_mail_ssl_conf_t *scf = conf;
347
348 char *rv;
349
350 rv = ngx_conf_set_enum_slot(cf, cmd, conf);
351
352 if (rv != NGX_CONF_OK) {
353 return rv;
354 }
355
356 if (scf->enable == 1 && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) {
357 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
358 "\"ssl\" directive conflicts with \"starttls\"");
359 return NGX_CONF_ERROR;
360 }
361
362 scf->file = cf->conf_file->file.name.data;
363 scf->line = cf->conf_file->line;
364
365 return NGX_CONF_OK;
366}
367
368
369static char *
Igor Sysoev02c8d182007-03-19 13:36:56 +0000370ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000371{
Igor Sysoev02c8d182007-03-19 13:36:56 +0000372 ngx_mail_ssl_conf_t *scf = conf;
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000373
374 size_t len;
375 ngx_str_t *value, name, size;
376 ngx_int_t n;
377 ngx_uint_t i, j;
378
379 value = cf->args->elts;
380
381 for (i = 1; i < cf->args->nelts; i++) {
382
Igor Sysoev6ff850b2007-12-26 20:27:22 +0000383 if (ngx_strcmp(value[i].data, "off") == 0) {
384 scf->builtin_session_cache = NGX_SSL_NO_SCACHE;
385 continue;
386 }
387
Igor Sysoevd6548fa2008-05-26 07:14:13 +0000388 if (ngx_strcmp(value[i].data, "none") == 0) {
389 scf->builtin_session_cache = NGX_SSL_NONE_SCACHE;
390 continue;
391 }
392
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000393 if (ngx_strcmp(value[i].data, "builtin") == 0) {
394 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
395 continue;
396 }
397
398 if (value[i].len > sizeof("builtin:") - 1
399 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
400 == 0)
401 {
402 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
403 value[i].len - (sizeof("builtin:") - 1));
404
405 if (n == NGX_ERROR) {
406 goto invalid;
407 }
408
409 scf->builtin_session_cache = n;
410
411 continue;
412 }
413
414 if (value[i].len > sizeof("shared:") - 1
415 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
416 == 0)
417 {
418 len = 0;
419
420 for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
421 if (value[i].data[j] == ':') {
422 break;
423 }
424
425 len++;
426 }
427
428 if (len == 0) {
429 goto invalid;
430 }
431
432 name.len = len;
433 name.data = value[i].data + sizeof("shared:") - 1;
434
435 size.len = value[i].len - j - 1;
436 size.data = name.data + len + 1;
437
438 n = ngx_parse_size(&size);
439
440 if (n == NGX_ERROR) {
441 goto invalid;
442 }
443
444 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
445 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
446 "session cache \"%V\" is too small",
447 &value[i]);
448
449 return NGX_CONF_ERROR;
450 }
451
452 scf->shm_zone = ngx_shared_memory_add(cf, &name, n,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000453 &ngx_mail_ssl_module);
Igor Sysoeva4c84c52007-01-03 16:11:53 +0000454 if (scf->shm_zone == NULL) {
455 return NGX_CONF_ERROR;
456 }
457
458 continue;
459 }
460
461 goto invalid;
462 }
463
464 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) {
465 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
466 }
467
468 return NGX_CONF_OK;
469
470invalid:
471
472 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
473 "invalid session cache \"%V\"", &value[i]);
474
475 return NGX_CONF_ERROR;
476}