Line data Source code
1 : #ifndef DDEBUG
2 : #define DDEBUG 0
3 : #endif
4 : #include "ddebug.h"
5 :
6 :
7 : #include "ngx_http_set_misc_module.h"
8 : #include <ndk.h>
9 : #include "ngx_http_set_base32.h"
10 : #include "ngx_http_set_default_value.h"
11 : #include "ngx_http_set_hashed_upstream.h"
12 : #include "ngx_http_set_unescape_uri.h"
13 : #include "ngx_http_set_quote_sql.h"
14 : #include "ngx_http_set_quote_json.h"
15 : #include "ngx_http_set_escape_uri.h"
16 : #include "ngx_http_set_local_today.h"
17 : #include "ngx_http_set_hash.h"
18 : #include "ngx_http_set_hex.h"
19 : #include "ngx_http_set_base64.h"
20 : #if NGX_OPENSSL
21 : #include "ngx_http_set_hmac.h"
22 : #endif
23 : #include "ngx_http_set_random.h"
24 : #include "ngx_http_set_secure_random.h"
25 : #include "ngx_http_set_rotate.h"
26 :
27 :
28 : #define NGX_UNESCAPE_URI_COMPONENT 0
29 : #define BASE32_ALPHABET_LEN 32
30 :
31 :
32 : static void *ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf);
33 : static char *ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent,
34 : void *child);
35 : static char *ngx_http_set_misc_base32_alphabet(ngx_conf_t *cf,
36 : ngx_command_t *cmd, void *conf);
37 :
38 :
39 : static ngx_conf_deprecated_t ngx_conf_deprecated_set_misc_base32_padding = {
40 : ngx_conf_deprecated, "set_misc_base32_padding", "set_base32_padding"
41 : };
42 :
43 :
44 : static ndk_set_var_t ngx_http_set_misc_set_encode_base64_filter = {
45 : NDK_SET_VAR_VALUE,
46 : (void *) ngx_http_set_misc_set_encode_base64,
47 : 1,
48 : NULL
49 : };
50 :
51 : static ndk_set_var_t ngx_http_set_misc_set_decode_base64_filter = {
52 : NDK_SET_VAR_VALUE,
53 : (void *) ngx_http_set_misc_set_decode_base64,
54 : 1,
55 : NULL
56 : };
57 :
58 :
59 : static ndk_set_var_t ngx_http_set_misc_set_decode_hex_filter = {
60 : NDK_SET_VAR_VALUE,
61 : (void *) ngx_http_set_misc_set_decode_hex,
62 : 1,
63 : NULL
64 : };
65 :
66 :
67 : static ndk_set_var_t ngx_http_set_misc_set_encode_hex_filter = {
68 : NDK_SET_VAR_VALUE,
69 : (void *) ngx_http_set_misc_set_encode_hex,
70 : 1,
71 : NULL
72 : };
73 :
74 :
75 : #if NGX_OPENSSL
76 : static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = {
77 : NDK_SET_VAR_MULTI_VALUE,
78 : (void *) ngx_http_set_misc_set_hmac_sha1,
79 : 2,
80 : NULL
81 : };
82 : #endif
83 :
84 :
85 : #ifndef NGX_HTTP_SET_HASH
86 : static ndk_set_var_t ngx_http_set_misc_set_md5_filter = {
87 : NDK_SET_VAR_VALUE,
88 : (void *) ngx_http_set_misc_set_md5,
89 : 1,
90 : NULL
91 : };
92 :
93 :
94 : #if NGX_HAVE_SHA1
95 : static ndk_set_var_t ngx_http_set_misc_set_sha1_filter = {
96 : NDK_SET_VAR_VALUE,
97 : (void *) ngx_http_set_misc_set_sha1,
98 : 1,
99 : NULL
100 : };
101 : #endif
102 : #endif
103 :
104 :
105 : static ndk_set_var_t ngx_http_set_misc_unescape_uri_filter = {
106 : NDK_SET_VAR_VALUE,
107 : (void *) ngx_http_set_misc_unescape_uri,
108 : 1,
109 : NULL
110 : };
111 :
112 :
113 : static ndk_set_var_t ngx_http_set_misc_escape_uri_filter = {
114 : NDK_SET_VAR_VALUE,
115 : (void *) ngx_http_set_misc_escape_uri,
116 : 1,
117 : NULL
118 : };
119 :
120 :
121 : static ndk_set_var_t ngx_http_set_misc_decode_base32_filter = {
122 : NDK_SET_VAR_VALUE,
123 : (void *) ngx_http_set_misc_decode_base32,
124 : 1,
125 : NULL
126 : };
127 :
128 :
129 : static ndk_set_var_t ngx_http_set_misc_quote_sql_str_filter = {
130 : NDK_SET_VAR_VALUE,
131 : (void *) ngx_http_set_misc_quote_sql_str,
132 : 1,
133 : NULL
134 : };
135 :
136 :
137 : static ndk_set_var_t ngx_http_set_misc_quote_pgsql_str_filter = {
138 : NDK_SET_VAR_VALUE,
139 : (void *) ngx_http_set_misc_quote_pgsql_str,
140 : 1,
141 : NULL
142 : };
143 :
144 :
145 : static ndk_set_var_t ngx_http_set_misc_quote_json_str_filter = {
146 : NDK_SET_VAR_VALUE,
147 : (void *) ngx_http_set_misc_quote_json_str,
148 : 1,
149 : NULL
150 : };
151 :
152 :
153 : static ndk_set_var_t ngx_http_set_misc_encode_base32_filter = {
154 : NDK_SET_VAR_VALUE,
155 : (void *) ngx_http_set_misc_encode_base32,
156 : 1,
157 : NULL
158 : };
159 :
160 :
161 : static ndk_set_var_t ngx_http_set_misc_local_today_filter = {
162 : NDK_SET_VAR_VALUE,
163 : (void *) ngx_http_set_local_today,
164 : 0,
165 : NULL
166 : };
167 :
168 :
169 : static ndk_set_var_t ngx_http_set_misc_formatted_gmt_time_filter = {
170 : NDK_SET_VAR_VALUE,
171 : (void *) ngx_http_set_formatted_gmt_time,
172 : 2,
173 : NULL
174 : };
175 :
176 :
177 : static ndk_set_var_t ngx_http_set_misc_formatted_local_time_filter = {
178 : NDK_SET_VAR_VALUE,
179 : (void *) ngx_http_set_formatted_local_time,
180 : 2,
181 : NULL
182 : };
183 :
184 :
185 : static ndk_set_var_t ngx_http_set_misc_set_random_filter = {
186 : NDK_SET_VAR_MULTI_VALUE,
187 : (void *) ngx_http_set_misc_set_random,
188 : 2,
189 : NULL
190 : };
191 :
192 :
193 : static ndk_set_var_t ngx_http_set_misc_set_secure_random_alphanum_filter = {
194 : NDK_SET_VAR_VALUE,
195 : (void *) ngx_http_set_misc_set_secure_random_alphanum,
196 : 1,
197 : NULL
198 : };
199 :
200 :
201 : static ndk_set_var_t ngx_http_set_misc_set_secure_random_lcalpha_filter = {
202 : NDK_SET_VAR_VALUE,
203 : (void *) ngx_http_set_misc_set_secure_random_lcalpha,
204 : 1,
205 : NULL
206 : };
207 :
208 :
209 : static ngx_command_t ngx_http_set_misc_commands[] = {
210 : { ngx_string ("set_encode_base64"),
211 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
212 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
213 : ndk_set_var_value,
214 : 0,
215 : 0,
216 : &ngx_http_set_misc_set_encode_base64_filter
217 : },
218 : { ngx_string ("set_decode_base64"),
219 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
220 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
221 : ndk_set_var_value,
222 : 0,
223 : 0,
224 : &ngx_http_set_misc_set_decode_base64_filter
225 : },
226 : { ngx_string ("set_decode_hex"),
227 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
228 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
229 : ndk_set_var_value,
230 : 0,
231 : 0,
232 : &ngx_http_set_misc_set_decode_hex_filter
233 : },
234 : { ngx_string ("set_encode_hex"),
235 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
236 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
237 : ndk_set_var_value,
238 : 0,
239 : 0,
240 : &ngx_http_set_misc_set_encode_hex_filter
241 : },
242 : #if NGX_OPENSSL
243 : { ngx_string ("set_hmac_sha1"),
244 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
245 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
246 : ndk_set_var_multi_value,
247 : 0,
248 : 0,
249 : &ngx_http_set_misc_set_hmac_sha1_filter
250 : },
251 : #endif
252 : #ifndef NGX_HTTP_SET_HASH
253 : { ngx_string ("set_md5"),
254 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
255 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
256 : ndk_set_var_value,
257 : 0,
258 : 0,
259 : &ngx_http_set_misc_set_md5_filter
260 : },
261 : #if NGX_HAVE_SHA1
262 : {
263 : ngx_string ("set_sha1"),
264 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
265 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
266 : ndk_set_var_value,
267 : 0,
268 : 0,
269 : &ngx_http_set_misc_set_sha1_filter
270 : },
271 : #endif
272 : #endif
273 : {
274 : ngx_string ("set_unescape_uri"),
275 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
276 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
277 : ndk_set_var_value,
278 : 0,
279 : 0,
280 : &ngx_http_set_misc_unescape_uri_filter
281 : },
282 : {
283 : ngx_string ("set_escape_uri"),
284 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
285 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
286 : ndk_set_var_value,
287 : 0,
288 : 0,
289 : &ngx_http_set_misc_escape_uri_filter
290 : },
291 : {
292 : ngx_string ("set_quote_sql_str"),
293 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
294 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
295 : ndk_set_var_value,
296 : 0,
297 : 0,
298 : &ngx_http_set_misc_quote_sql_str_filter
299 : },
300 : {
301 : ngx_string ("set_quote_pgsql_str"),
302 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
303 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
304 : ndk_set_var_value,
305 : 0,
306 : 0,
307 : &ngx_http_set_misc_quote_pgsql_str_filter
308 : },
309 : {
310 : ngx_string ("set_quote_json_str"),
311 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
312 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
313 : ndk_set_var_value,
314 : 0,
315 : 0,
316 : &ngx_http_set_misc_quote_json_str_filter
317 : },
318 : {
319 : ngx_string ("set_if_empty"),
320 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
321 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
322 : ngx_http_set_if_empty,
323 : 0,
324 : 0,
325 : NULL
326 : },
327 : {
328 : ngx_string("set_hashed_upstream"),
329 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
330 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
331 : ngx_http_set_hashed_upstream,
332 : 0,
333 : 0,
334 : NULL
335 : },
336 : {
337 : /* this is now deprecated; use set_base32_padding instead */
338 : ngx_string("set_misc_base32_padding"),
339 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
340 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
341 : ngx_conf_set_flag_slot,
342 : NGX_HTTP_LOC_CONF_OFFSET,
343 : offsetof(ngx_http_set_misc_loc_conf_t, base32_padding),
344 : &ngx_conf_deprecated_set_misc_base32_padding,
345 : },
346 : {
347 : ngx_string("set_base32_padding"),
348 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
349 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
350 : ngx_conf_set_flag_slot,
351 : NGX_HTTP_LOC_CONF_OFFSET,
352 : offsetof(ngx_http_set_misc_loc_conf_t, base32_padding),
353 : NULL
354 : },
355 : {
356 : ngx_string("set_base32_alphabet"),
357 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
358 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
359 : ngx_http_set_misc_base32_alphabet,
360 : NGX_HTTP_LOC_CONF_OFFSET,
361 : offsetof(ngx_http_set_misc_loc_conf_t, base32_alphabet),
362 : NULL
363 : },
364 : {
365 : ngx_string("set_encode_base32"),
366 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
367 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
368 : ndk_set_var_value,
369 : 0,
370 : 0,
371 : &ngx_http_set_misc_encode_base32_filter
372 : },
373 : {
374 : ngx_string("set_decode_base32"),
375 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
376 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
377 : ndk_set_var_value,
378 : 0,
379 : 0,
380 : &ngx_http_set_misc_decode_base32_filter
381 : },
382 : {
383 : ngx_string("set_local_today"),
384 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
385 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
386 : ndk_set_var_value,
387 : 0,
388 : 0,
389 : &ngx_http_set_misc_local_today_filter
390 : },
391 : {
392 : ngx_string("set_formatted_gmt_time"),
393 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
394 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
395 : ndk_set_var_value,
396 : 0,
397 : 0,
398 : &ngx_http_set_misc_formatted_gmt_time_filter
399 : },
400 : {
401 : ngx_string("set_formatted_local_time"),
402 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
403 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2,
404 : ndk_set_var_value,
405 : 0,
406 : 0,
407 : &ngx_http_set_misc_formatted_local_time_filter
408 : },
409 : { ngx_string ("set_random"),
410 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
411 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
412 : ndk_set_var_multi_value,
413 : 0,
414 : 0,
415 : &ngx_http_set_misc_set_random_filter
416 : },
417 : { ngx_string ("set_secure_random_alphanum"),
418 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
419 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
420 : ndk_set_var_value,
421 : 0,
422 : 0,
423 : &ngx_http_set_misc_set_secure_random_alphanum_filter
424 : },
425 : { ngx_string ("set_secure_random_lcalpha"),
426 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
427 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12,
428 : ndk_set_var_value,
429 : 0,
430 : 0,
431 : &ngx_http_set_misc_set_secure_random_lcalpha_filter
432 : },
433 : { ngx_string ("set_rotate"),
434 : NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
435 : |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
436 : ngx_http_set_rotate,
437 : 0,
438 : 0,
439 : NULL
440 : },
441 :
442 : ngx_null_command
443 : };
444 :
445 :
446 : static ngx_http_module_t ngx_http_set_misc_module_ctx = {
447 : NULL, /* preconfiguration */
448 : NULL, /* postconfiguration */
449 :
450 : NULL, /* create main configuration */
451 : NULL, /* init main configuration */
452 :
453 : NULL, /* create server configuration */
454 : NULL, /* merge server configuration */
455 :
456 : ngx_http_set_misc_create_loc_conf, /* create location configuration */
457 : ngx_http_set_misc_merge_loc_conf /* merge location configuration */
458 : };
459 :
460 :
461 : ngx_module_t ngx_http_set_misc_module = {
462 : NGX_MODULE_V1,
463 : &ngx_http_set_misc_module_ctx, /* module context */
464 : ngx_http_set_misc_commands, /* module directives */
465 : NGX_HTTP_MODULE, /* module type */
466 : NULL, /* init master */
467 : NULL, /* init module */
468 : NULL, /* init process */
469 : NULL, /* init thread */
470 : NULL, /* exit thread */
471 : NULL, /* exit process */
472 : NULL, /* exit master */
473 : NGX_MODULE_V1_PADDING
474 : };
475 :
476 :
477 : void *
478 108 : ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf)
479 : {
480 : ngx_http_set_misc_loc_conf_t *conf;
481 :
482 108 : conf = ngx_palloc(cf->pool, sizeof(ngx_http_set_misc_loc_conf_t));
483 108 : if (conf == NULL) {
484 0 : return NULL;
485 : }
486 :
487 108 : conf->base32_padding = NGX_CONF_UNSET;
488 108 : conf->base32_alphabet.data = NULL;
489 108 : conf->base32_alphabet.len = 0;
490 108 : conf->current = NGX_CONF_UNSET;
491 :
492 108 : return conf;
493 : }
494 :
495 :
496 : char *
497 90 : ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
498 : {
499 : ngx_uint_t i;
500 :
501 90 : ngx_http_set_misc_loc_conf_t *prev = parent;
502 90 : ngx_http_set_misc_loc_conf_t *conf = child;
503 :
504 90 : ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1);
505 :
506 90 : ngx_conf_merge_str_value(conf->base32_alphabet, prev->base32_alphabet,
507 : "0123456789abcdefghijklmnopqrstuv");
508 :
509 90 : ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET);
510 :
511 2970 : for (i = 0; i < BASE32_ALPHABET_LEN; i++) {
512 2880 : conf->basis32[conf->base32_alphabet.data[i]] = (u_char) i;
513 : }
514 :
515 90 : return NGX_CONF_OK;
516 : }
517 :
518 :
519 : static char *
520 0 : ngx_http_set_misc_base32_alphabet(ngx_conf_t *cf, ngx_command_t *cmd,
521 : void *conf)
522 : {
523 : ngx_str_t *value;
524 :
525 0 : value = cf->args->elts;
526 :
527 0 : if (value[1].len != BASE32_ALPHABET_LEN) {
528 0 : ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
529 : "\"set_base32_alphabet\" directive takes an "
530 : "alphabet of %uz bytes but %d expected",
531 0 : value[1].len, BASE32_ALPHABET_LEN);
532 0 : return NGX_CONF_ERROR;
533 : }
534 :
535 0 : return ngx_conf_set_str_slot(cf, cmd, conf);
536 : }
|