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