Mercurial > hg > nginx
annotate src/mail/ngx_mail_ssl_module.c @ 2498:d3357bf23a5e
fix /?new=arg?old=arg redirect case
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 10 Feb 2009 16:03:42 +0000 |
parents | 109849282793 |
children | 38cb2238db13 |
rev | line source |
---|---|
539 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
1136 | 9 #include <ngx_mail.h> |
539 | 10 |
11 | |
2124 | 12 #define NGX_DEFAULT_CIPHERS "ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP" |
539 | 13 |
14 | |
1136 | 15 static void *ngx_mail_ssl_create_conf(ngx_conf_t *cf); |
16 static char *ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child); | |
2224 | 17 |
18 static char *ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, | |
19 void *conf); | |
20 static char *ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd, | |
21 void *conf); | |
1136 | 22 static char *ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, |
976 | 23 void *conf); |
539 | 24 |
563 | 25 #if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE) |
26 | |
1136 | 27 static char *ngx_mail_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd, |
563 | 28 void *conf); |
29 | |
1136 | 30 static char ngx_mail_ssl_openssl097[] = "OpenSSL 0.9.7 and higher"; |
563 | 31 |
32 #endif | |
33 | |
539 | 34 |
583 | 35 static ngx_conf_enum_t ngx_http_starttls_state[] = { |
1136 | 36 { ngx_string("off"), NGX_MAIL_STARTTLS_OFF }, |
37 { ngx_string("on"), NGX_MAIL_STARTTLS_ON }, | |
38 { ngx_string("only"), NGX_MAIL_STARTTLS_ONLY }, | |
583 | 39 { ngx_null_string, 0 } |
40 }; | |
41 | |
42 | |
43 | |
1136 | 44 static ngx_conf_bitmask_t ngx_mail_ssl_protocols[] = { |
547 | 45 { ngx_string("SSLv2"), NGX_SSL_SSLv2 }, |
46 { ngx_string("SSLv3"), NGX_SSL_SSLv3 }, | |
47 { ngx_string("TLSv1"), NGX_SSL_TLSv1 }, | |
48 { ngx_null_string, 0 } | |
49 }; | |
50 | |
51 | |
1136 | 52 static ngx_command_t ngx_mail_ssl_commands[] = { |
539 | 53 |
54 { ngx_string("ssl"), | |
1136 | 55 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, |
2224 | 56 ngx_mail_ssl_enable, |
1136 | 57 NGX_MAIL_SRV_CONF_OFFSET, |
58 offsetof(ngx_mail_ssl_conf_t, enable), | |
539 | 59 NULL }, |
60 | |
583 | 61 { ngx_string("starttls"), |
1136 | 62 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
2224 | 63 ngx_mail_ssl_starttls, |
1136 | 64 NGX_MAIL_SRV_CONF_OFFSET, |
65 offsetof(ngx_mail_ssl_conf_t, starttls), | |
583 | 66 ngx_http_starttls_state }, |
67 | |
539 | 68 { ngx_string("ssl_certificate"), |
1136 | 69 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 70 ngx_conf_set_str_slot, |
1136 | 71 NGX_MAIL_SRV_CONF_OFFSET, |
72 offsetof(ngx_mail_ssl_conf_t, certificate), | |
539 | 73 NULL }, |
74 | |
75 { ngx_string("ssl_certificate_key"), | |
1136 | 76 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 77 ngx_conf_set_str_slot, |
1136 | 78 NGX_MAIL_SRV_CONF_OFFSET, |
79 offsetof(ngx_mail_ssl_conf_t, certificate_key), | |
539 | 80 NULL }, |
81 | |
2044 | 82 { ngx_string("ssl_dhparam"), |
83 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, | |
84 ngx_conf_set_str_slot, | |
85 NGX_MAIL_SRV_CONF_OFFSET, | |
86 offsetof(ngx_mail_ssl_conf_t, dhparam), | |
87 NULL }, | |
88 | |
547 | 89 { ngx_string("ssl_protocols"), |
1136 | 90 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE, |
547 | 91 ngx_conf_set_bitmask_slot, |
1136 | 92 NGX_MAIL_SRV_CONF_OFFSET, |
93 offsetof(ngx_mail_ssl_conf_t, protocols), | |
94 &ngx_mail_ssl_protocols }, | |
547 | 95 |
539 | 96 { ngx_string("ssl_ciphers"), |
1136 | 97 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 98 ngx_conf_set_str_slot, |
1136 | 99 NGX_MAIL_SRV_CONF_OFFSET, |
100 offsetof(ngx_mail_ssl_conf_t, ciphers), | |
539 | 101 NULL }, |
102 | |
547 | 103 { ngx_string("ssl_prefer_server_ciphers"), |
1136 | 104 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, |
563 | 105 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
547 | 106 ngx_conf_set_flag_slot, |
1136 | 107 NGX_MAIL_SRV_CONF_OFFSET, |
108 offsetof(ngx_mail_ssl_conf_t, prefer_server_ciphers), | |
547 | 109 NULL }, |
563 | 110 #else |
1136 | 111 ngx_mail_ssl_nosupported, 0, 0, ngx_mail_ssl_openssl097 }, |
563 | 112 #endif |
113 | |
976 | 114 { ngx_string("ssl_session_cache"), |
1136 | 115 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE12, |
116 ngx_mail_ssl_session_cache, | |
117 NGX_MAIL_SRV_CONF_OFFSET, | |
976 | 118 0, |
119 NULL }, | |
120 | |
573 | 121 { ngx_string("ssl_session_timeout"), |
1136 | 122 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
573 | 123 ngx_conf_set_sec_slot, |
1136 | 124 NGX_MAIL_SRV_CONF_OFFSET, |
125 offsetof(ngx_mail_ssl_conf_t, session_timeout), | |
573 | 126 NULL }, |
547 | 127 |
539 | 128 ngx_null_command |
129 }; | |
130 | |
131 | |
1136 | 132 static ngx_mail_module_t ngx_mail_ssl_module_ctx = { |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
133 NULL, /* protocol */ |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
134 |
539 | 135 NULL, /* create main configuration */ |
136 NULL, /* init main configuration */ | |
137 | |
1136 | 138 ngx_mail_ssl_create_conf, /* create server configuration */ |
139 ngx_mail_ssl_merge_conf /* merge server configuration */ | |
539 | 140 }; |
141 | |
142 | |
1136 | 143 ngx_module_t ngx_mail_ssl_module = { |
539 | 144 NGX_MODULE_V1, |
1136 | 145 &ngx_mail_ssl_module_ctx, /* module context */ |
146 ngx_mail_ssl_commands, /* module directives */ | |
147 NGX_MAIL_MODULE, /* module type */ | |
541 | 148 NULL, /* init master */ |
539 | 149 NULL, /* init module */ |
541 | 150 NULL, /* init process */ |
151 NULL, /* init thread */ | |
152 NULL, /* exit thread */ | |
153 NULL, /* exit process */ | |
154 NULL, /* exit master */ | |
155 NGX_MODULE_V1_PADDING | |
539 | 156 }; |
157 | |
158 | |
1136 | 159 static ngx_str_t ngx_mail_ssl_sess_id_ctx = ngx_string("MAIL"); |
543 | 160 |
161 | |
539 | 162 static void * |
1136 | 163 ngx_mail_ssl_create_conf(ngx_conf_t *cf) |
577 | 164 { |
1136 | 165 ngx_mail_ssl_conf_t *scf; |
577 | 166 |
1136 | 167 scf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_ssl_conf_t)); |
539 | 168 if (scf == NULL) { |
169 return NGX_CONF_ERROR; | |
170 } | |
171 | |
172 /* | |
577 | 173 * set by ngx_pcalloc(): |
539 | 174 * |
547 | 175 * scf->protocols = 0; |
2044 | 176 * scf->certificate = { 0, NULL }; |
177 * scf->certificate_key = { 0, NULL }; | |
178 * scf->dhparam = { 0, NULL }; | |
539 | 179 * scf->ciphers.len = 0; |
180 * scf->ciphers.data = NULL; | |
976 | 181 * scf->shm_zone = NULL; |
539 | 182 */ |
183 | |
184 scf->enable = NGX_CONF_UNSET; | |
583 | 185 scf->starttls = NGX_CONF_UNSET; |
976 | 186 scf->prefer_server_ciphers = NGX_CONF_UNSET; |
187 scf->builtin_session_cache = NGX_CONF_UNSET; | |
573 | 188 scf->session_timeout = NGX_CONF_UNSET; |
539 | 189 |
190 return scf; | |
191 } | |
192 | |
193 | |
194 static char * | |
1136 | 195 ngx_mail_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
539 | 196 { |
1136 | 197 ngx_mail_ssl_conf_t *prev = parent; |
198 ngx_mail_ssl_conf_t *conf = child; | |
539 | 199 |
2224 | 200 char *mode; |
563 | 201 ngx_pool_cleanup_t *cln; |
202 | |
539 | 203 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
2224 | 204 ngx_conf_merge_uint_value(conf->starttls, prev->starttls, |
205 NGX_MAIL_STARTTLS_OFF); | |
539 | 206 |
573 | 207 ngx_conf_merge_value(conf->session_timeout, |
208 prev->session_timeout, 300); | |
209 | |
547 | 210 ngx_conf_merge_value(conf->prefer_server_ciphers, |
211 prev->prefer_server_ciphers, 0); | |
212 | |
213 ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols, | |
214 (NGX_CONF_BITMASK_SET | |
215 |NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1)); | |
216 | |
2224 | 217 ngx_conf_merge_str_value(conf->certificate, prev->certificate, ""); |
218 ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key, ""); | |
539 | 219 |
2044 | 220 ngx_conf_merge_str_value(conf->dhparam, prev->dhparam, ""); |
221 | |
2124 | 222 ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFAULT_CIPHERS); |
539 | 223 |
224 | |
547 | 225 conf->ssl.log = cf->log; |
539 | 226 |
2224 | 227 if (conf->enable) { |
228 mode = "ssl"; | |
229 | |
230 } else if (conf->starttls != NGX_MAIL_STARTTLS_OFF) { | |
231 mode = "starttls"; | |
232 | |
233 } else { | |
234 mode = ""; | |
235 } | |
236 | |
237 if (*mode) { | |
238 | |
239 if (conf->certificate.len == 0) { | |
240 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
241 "no \"ssl_certificate\" is defined for " | |
242 "the \"%s\" directive in %s:%ui", | |
243 mode, conf->file, conf->line); | |
244 return NGX_CONF_ERROR; | |
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 for " | |
250 "the \"%s\" directive in %s:%ui", | |
251 mode, conf->file, conf->line); | |
252 return NGX_CONF_ERROR; | |
253 } | |
254 | |
255 } else { | |
256 | |
257 if (conf->certificate.len == 0) { | |
258 return NGX_CONF_OK; | |
259 } | |
260 | |
261 if (conf->certificate_key.len == 0) { | |
262 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
263 "no \"ssl_certificate_key\" is defined " | |
264 "for certificate \"%V\"", | |
265 &conf->certificate); | |
266 return NGX_CONF_ERROR; | |
267 } | |
268 } | |
269 | |
969 | 270 if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) { |
539 | 271 return NGX_CONF_ERROR; |
272 } | |
273 | |
563 | 274 cln = ngx_pool_cleanup_add(cf->pool, 0); |
275 if (cln == NULL) { | |
539 | 276 return NGX_CONF_ERROR; |
277 } | |
278 | |
563 | 279 cln->handler = ngx_ssl_cleanup_ctx; |
280 cln->data = &conf->ssl; | |
281 | |
282 if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate, | |
283 &conf->certificate_key) | |
284 != NGX_OK) | |
547 | 285 { |
286 return NGX_CONF_ERROR; | |
287 } | |
539 | 288 |
289 if (conf->ciphers.len) { | |
547 | 290 if (SSL_CTX_set_cipher_list(conf->ssl.ctx, |
563 | 291 (const char *) conf->ciphers.data) |
292 == 0) | |
539 | 293 { |
294 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0, | |
295 "SSL_CTX_set_cipher_list(\"%V\") failed", | |
296 &conf->ciphers); | |
297 } | |
298 } | |
299 | |
563 | 300 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE |
301 | |
302 if (conf->prefer_server_ciphers) { | |
303 SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); | |
304 } | |
305 | |
306 #endif | |
307 | |
547 | 308 if (ngx_ssl_generate_rsa512_key(&conf->ssl) != NGX_OK) { |
539 | 309 return NGX_CONF_ERROR; |
310 } | |
311 | |
2044 | 312 if (ngx_ssl_dhparam(cf, &conf->ssl, &conf->dhparam) != NGX_OK) { |
313 return NGX_CONF_ERROR; | |
314 } | |
315 | |
976 | 316 ngx_conf_merge_value(conf->builtin_session_cache, |
2032 | 317 prev->builtin_session_cache, NGX_SSL_NONE_SCACHE); |
976 | 318 |
319 if (conf->shm_zone == NULL) { | |
320 conf->shm_zone = prev->shm_zone; | |
321 } | |
539 | 322 |
1136 | 323 if (ngx_ssl_session_cache(&conf->ssl, &ngx_mail_ssl_sess_id_ctx, |
976 | 324 conf->builtin_session_cache, |
325 conf->shm_zone, conf->session_timeout) | |
326 != NGX_OK) | |
327 { | |
328 return NGX_CONF_ERROR; | |
329 } | |
573 | 330 |
539 | 331 return NGX_CONF_OK; |
332 } | |
563 | 333 |
577 | 334 |
976 | 335 static char * |
2224 | 336 ngx_mail_ssl_enable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
337 { | |
338 ngx_mail_ssl_conf_t *scf = conf; | |
339 | |
340 char *rv; | |
341 | |
342 rv = ngx_conf_set_flag_slot(cf, cmd, conf); | |
343 | |
344 if (rv != NGX_CONF_OK) { | |
345 return rv; | |
346 } | |
347 | |
348 if (scf->enable && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) { | |
349 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
350 "\"starttls\" directive conflicts with \"ssl on\""); | |
351 return NGX_CONF_ERROR; | |
352 } | |
353 | |
354 scf->file = cf->conf_file->file.name.data; | |
355 scf->line = cf->conf_file->line; | |
356 | |
357 return NGX_CONF_OK; | |
358 } | |
359 | |
360 | |
361 static char * | |
362 ngx_mail_ssl_starttls(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
363 { | |
364 ngx_mail_ssl_conf_t *scf = conf; | |
365 | |
366 char *rv; | |
367 | |
368 rv = ngx_conf_set_enum_slot(cf, cmd, conf); | |
369 | |
370 if (rv != NGX_CONF_OK) { | |
371 return rv; | |
372 } | |
373 | |
374 if (scf->enable == 1 && (ngx_int_t) scf->starttls > NGX_MAIL_STARTTLS_OFF) { | |
375 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
376 "\"ssl\" directive conflicts with \"starttls\""); | |
377 return NGX_CONF_ERROR; | |
378 } | |
379 | |
380 scf->file = cf->conf_file->file.name.data; | |
381 scf->line = cf->conf_file->line; | |
382 | |
383 return NGX_CONF_OK; | |
384 } | |
385 | |
386 | |
387 static char * | |
1136 | 388 ngx_mail_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
976 | 389 { |
1136 | 390 ngx_mail_ssl_conf_t *scf = conf; |
976 | 391 |
392 size_t len; | |
393 ngx_str_t *value, name, size; | |
394 ngx_int_t n; | |
395 ngx_uint_t i, j; | |
396 | |
397 value = cf->args->elts; | |
398 | |
399 for (i = 1; i < cf->args->nelts; i++) { | |
400 | |
1778 | 401 if (ngx_strcmp(value[i].data, "off") == 0) { |
402 scf->builtin_session_cache = NGX_SSL_NO_SCACHE; | |
403 continue; | |
404 } | |
405 | |
2032 | 406 if (ngx_strcmp(value[i].data, "none") == 0) { |
407 scf->builtin_session_cache = NGX_SSL_NONE_SCACHE; | |
408 continue; | |
409 } | |
410 | |
976 | 411 if (ngx_strcmp(value[i].data, "builtin") == 0) { |
412 scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE; | |
413 continue; | |
414 } | |
415 | |
416 if (value[i].len > sizeof("builtin:") - 1 | |
417 && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1) | |
418 == 0) | |
419 { | |
420 n = ngx_atoi(value[i].data + sizeof("builtin:") - 1, | |
421 value[i].len - (sizeof("builtin:") - 1)); | |
422 | |
423 if (n == NGX_ERROR) { | |
424 goto invalid; | |
425 } | |
426 | |
427 scf->builtin_session_cache = n; | |
428 | |
429 continue; | |
430 } | |
431 | |
432 if (value[i].len > sizeof("shared:") - 1 | |
433 && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1) | |
434 == 0) | |
435 { | |
436 len = 0; | |
437 | |
438 for (j = sizeof("shared:") - 1; j < value[i].len; j++) { | |
439 if (value[i].data[j] == ':') { | |
440 break; | |
441 } | |
442 | |
443 len++; | |
444 } | |
445 | |
446 if (len == 0) { | |
447 goto invalid; | |
448 } | |
449 | |
450 name.len = len; | |
451 name.data = value[i].data + sizeof("shared:") - 1; | |
452 | |
453 size.len = value[i].len - j - 1; | |
454 size.data = name.data + len + 1; | |
455 | |
456 n = ngx_parse_size(&size); | |
457 | |
458 if (n == NGX_ERROR) { | |
459 goto invalid; | |
460 } | |
461 | |
462 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
463 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
464 "session cache \"%V\" is too small", | |
465 &value[i]); | |
466 | |
467 return NGX_CONF_ERROR; | |
468 } | |
469 | |
470 scf->shm_zone = ngx_shared_memory_add(cf, &name, n, | |
1136 | 471 &ngx_mail_ssl_module); |
976 | 472 if (scf->shm_zone == NULL) { |
473 return NGX_CONF_ERROR; | |
474 } | |
475 | |
476 continue; | |
477 } | |
478 | |
479 goto invalid; | |
480 } | |
481 | |
482 if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) { | |
483 scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE; | |
484 } | |
485 | |
486 return NGX_CONF_OK; | |
487 | |
488 invalid: | |
489 | |
490 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
491 "invalid session cache \"%V\"", &value[i]); | |
492 | |
493 return NGX_CONF_ERROR; | |
494 } | |
495 | |
496 | |
563 | 497 #if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE) |
577 | 498 |
563 | 499 static char * |
1136 | 500 ngx_mail_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
577 | 501 { |
563 | 502 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
503 "\"%V\" directive is available only in %s,", | |
504 &cmd->name, cmd->post); | |
505 | |
506 return NGX_CONF_ERROR; | |
507 } | |
508 | |
509 #endif |