Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_referer_module.c @ 1316:f5bb5521d02c
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 17 Jul 2007 10:15:07 +0000 |
parents | 8ef04207c84f |
children | 92bc89c5ff0f |
rev | line source |
---|---|
577 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
595 | 12 #define NGX_HTTP_REFERER_NO_URI_PART ((void *) 4) |
13 | |
577 | 14 typedef struct { |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
15 ngx_hash_combined_t hash; |
577 | 16 |
593 | 17 ngx_flag_t no_referer; |
18 ngx_flag_t blocked_referer; | |
577 | 19 |
593 | 20 ngx_hash_keys_arrays_t *keys; |
577 | 21 } ngx_http_referer_conf_t; |
22 | |
23 | |
24 static void * ngx_http_referer_create_conf(ngx_conf_t *cf); | |
25 static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, | |
26 void *child); | |
27 static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, | |
28 void *conf); | |
593 | 29 static char *ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys, |
595 | 30 ngx_str_t *value, ngx_str_t *uri); |
593 | 31 static int ngx_libc_cdecl ngx_http_cmp_referer_wildcards(const void *one, |
32 const void *two); | |
577 | 33 |
34 | |
35 static ngx_command_t ngx_http_referer_commands[] = { | |
36 | |
37 { ngx_string("valid_referers"), | |
38 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, | |
39 ngx_http_valid_referers, | |
40 NGX_HTTP_LOC_CONF_OFFSET, | |
41 0, | |
42 NULL }, | |
43 | |
44 ngx_null_command | |
45 }; | |
46 | |
47 | |
48 static ngx_http_module_t ngx_http_referer_module_ctx = { | |
49 NULL, /* preconfiguration */ | |
50 NULL, /* postconfiguration */ | |
51 | |
52 NULL, /* create main configuration */ | |
53 NULL, /* init main configuration */ | |
54 | |
55 NULL, /* create server configuration */ | |
56 NULL, /* merge server configuration */ | |
57 | |
58 ngx_http_referer_create_conf, /* create location configuration */ | |
59 ngx_http_referer_merge_conf /* merge location configuration */ | |
60 }; | |
61 | |
62 | |
63 ngx_module_t ngx_http_referer_module = { | |
64 NGX_MODULE_V1, | |
65 &ngx_http_referer_module_ctx, /* module context */ | |
66 ngx_http_referer_commands, /* module directives */ | |
67 NGX_HTTP_MODULE, /* module type */ | |
68 NULL, /* init master */ | |
69 NULL, /* init module */ | |
70 NULL, /* init process */ | |
71 NULL, /* init thread */ | |
72 NULL, /* exit thread */ | |
73 NULL, /* exit process */ | |
74 NULL, /* exit master */ | |
75 NGX_MODULE_V1_PADDING | |
76 }; | |
77 | |
78 | |
79 static ngx_int_t | |
80 ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, | |
81 uintptr_t data) | |
82 { | |
595 | 83 u_char *p, *ref, *last; |
593 | 84 size_t len; |
595 | 85 ngx_str_t *uri; |
86 ngx_uint_t i, key; | |
593 | 87 ngx_http_referer_conf_t *rlcf; |
595 | 88 u_char buf[256]; |
577 | 89 |
593 | 90 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_referer_module); |
577 | 91 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
92 if (rlcf->hash.hash.buckets == NULL |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
93 && rlcf->hash.wc_head == NULL |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
94 && rlcf->hash.wc_tail == NULL) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
95 { |
595 | 96 goto valid; |
577 | 97 } |
98 | |
99 if (r->headers_in.referer == NULL) { | |
593 | 100 if (rlcf->no_referer) { |
595 | 101 goto valid; |
102 } | |
577 | 103 |
595 | 104 goto invalid; |
577 | 105 } |
106 | |
107 len = r->headers_in.referer->value.len; | |
108 ref = r->headers_in.referer->value.data; | |
109 | |
110 if (len < sizeof("http://i.ru") - 1 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
637
diff
changeset
|
111 || (ngx_strncasecmp(ref, (u_char *) "http://", 7) != 0)) |
577 | 112 { |
593 | 113 if (rlcf->blocked_referer) { |
595 | 114 goto valid; |
115 } | |
577 | 116 |
595 | 117 goto invalid; |
577 | 118 } |
119 | |
595 | 120 last = ref + len; |
577 | 121 ref += 7; |
595 | 122 i = 0; |
123 key = 0; | |
577 | 124 |
595 | 125 for (p = ref; p < last; p++) { |
593 | 126 if (*p == '/' || *p == ':') { |
127 break; | |
577 | 128 } |
595 | 129 |
130 buf[i] = ngx_tolower(*p); | |
131 key = ngx_hash(key, buf[i++]); | |
132 | |
133 if (i == 256) { | |
134 goto invalid; | |
135 } | |
593 | 136 } |
577 | 137 |
593 | 138 len = p - ref; |
577 | 139 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
140 uri = ngx_hash_find_combined(&rlcf->hash, key, buf, len); |
577 | 141 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
142 if (uri) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
143 goto uri; |
577 | 144 } |
145 | |
595 | 146 invalid: |
147 | |
577 | 148 *v = ngx_http_variable_true_value; |
149 | |
150 return NGX_OK; | |
595 | 151 |
152 uri: | |
153 | |
154 for ( /* void */ ; p < last; p++) { | |
155 if (*p == '/') { | |
156 break; | |
157 } | |
158 } | |
159 | |
160 len = last - p; | |
161 | |
162 if (uri == NGX_HTTP_REFERER_NO_URI_PART) { | |
163 goto valid; | |
164 } | |
165 | |
166 if (len < uri->len || ngx_strncmp(uri->data, p, uri->len) != 0) { | |
167 goto invalid; | |
168 } | |
169 | |
170 valid: | |
171 | |
172 *v = ngx_http_variable_null_value; | |
173 | |
174 return NGX_OK; | |
577 | 175 } |
176 | |
177 | |
178 static void * | |
179 ngx_http_referer_create_conf(ngx_conf_t *cf) | |
180 { | |
181 ngx_http_referer_conf_t *conf; | |
182 | |
593 | 183 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_referer_conf_t)); |
577 | 184 if (conf == NULL) { |
185 return NGX_CONF_ERROR; | |
186 } | |
187 | |
188 conf->no_referer = NGX_CONF_UNSET; | |
189 conf->blocked_referer = NGX_CONF_UNSET; | |
190 | |
191 return conf; | |
192 } | |
193 | |
194 | |
195 static char * | |
196 ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
197 { | |
198 ngx_http_referer_conf_t *prev = parent; | |
199 ngx_http_referer_conf_t *conf = child; | |
200 | |
593 | 201 ngx_hash_init_t hash; |
202 | |
203 if (conf->keys == NULL) { | |
204 conf->hash = prev->hash; | |
205 | |
577 | 206 ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0); |
207 ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0); | |
593 | 208 |
209 return NGX_CONF_OK; | |
210 } | |
211 | |
603 | 212 if ((conf->no_referer == 1 || conf->blocked_referer == 1) |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
213 && conf->keys->keys.nelts == 0 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
214 && conf->keys->dns_wc_head.nelts == 0 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
215 && conf->keys->dns_wc_tail.nelts == 0) |
603 | 216 { |
217 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
218 "the \"none\" or \"blocked\" referers are specified " | |
219 "in the \"valid_referers\" directive " | |
220 "without any valid referer"); | |
221 return NGX_CONF_ERROR; | |
222 } | |
223 | |
593 | 224 hash.key = ngx_hash_key_lc; |
225 hash.max_size = 2048; /* TODO: referer_hash_max_size; */ | |
226 hash.bucket_size = 64; /* TODO: referer_hash_bucket_size; */ | |
227 hash.name = "referers_hash"; | |
228 hash.pool = cf->pool; | |
229 | |
230 if (conf->keys->keys.nelts) { | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
231 hash.hash = &conf->hash.hash; |
593 | 232 hash.temp_pool = NULL; |
233 | |
234 if (ngx_hash_init(&hash, conf->keys->keys.elts, conf->keys->keys.nelts) | |
235 != NGX_OK) | |
236 { | |
237 return NGX_CONF_ERROR; | |
238 } | |
239 } | |
240 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
241 if (conf->keys->dns_wc_head.nelts) { |
593 | 242 |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
243 ngx_qsort(conf->keys->dns_wc_head.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
244 (size_t) conf->keys->dns_wc_head.nelts, |
593 | 245 sizeof(ngx_hash_key_t), |
246 ngx_http_cmp_referer_wildcards); | |
247 | |
248 hash.hash = NULL; | |
249 hash.temp_pool = cf->temp_pool; | |
250 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
251 if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_head.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
252 conf->keys->dns_wc_head.nelts) |
593 | 253 != NGX_OK) |
254 { | |
255 return NGX_CONF_ERROR; | |
256 } | |
257 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
258 conf->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
259 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
260 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
261 if (conf->keys->dns_wc_tail.nelts) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
262 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
263 ngx_qsort(conf->keys->dns_wc_tail.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
264 (size_t) conf->keys->dns_wc_tail.nelts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
265 sizeof(ngx_hash_key_t), |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
266 ngx_http_cmp_referer_wildcards); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
267 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
268 hash.hash = NULL; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
269 hash.temp_pool = cf->temp_pool; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
270 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
271 if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_tail.elts, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
272 conf->keys->dns_wc_tail.nelts) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
273 != NGX_OK) |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
274 { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
275 return NGX_CONF_ERROR; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
276 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
277 |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
278 conf->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash; |
577 | 279 } |
280 | |
281 if (conf->no_referer == NGX_CONF_UNSET) { | |
282 conf->no_referer = 0; | |
283 } | |
284 | |
285 if (conf->blocked_referer == NGX_CONF_UNSET) { | |
286 conf->blocked_referer = 0; | |
287 } | |
288 | |
611 | 289 conf->keys = NULL; |
290 | |
577 | 291 return NGX_CONF_OK; |
292 } | |
293 | |
294 | |
295 static char * | |
296 ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
297 { | |
593 | 298 ngx_http_referer_conf_t *rlcf = conf; |
577 | 299 |
593 | 300 u_char *p; |
595 | 301 ngx_str_t *value, uri, name; |
593 | 302 ngx_uint_t i, n; |
577 | 303 ngx_http_variable_t *var; |
304 ngx_http_server_name_t *sn; | |
305 ngx_http_core_srv_conf_t *cscf; | |
306 | |
307 name.len = sizeof("invalid_referer") - 1; | |
308 name.data = (u_char *) "invalid_referer"; | |
309 | |
583 | 310 var = ngx_http_add_variable(cf, &name, |
311 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH); | |
577 | 312 if (var == NULL) { |
313 return NGX_CONF_ERROR; | |
314 } | |
315 | |
637 | 316 var->get_handler = ngx_http_referer_variable; |
577 | 317 |
593 | 318 if (rlcf->keys == NULL) { |
319 rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t)); | |
320 if (rlcf->keys == NULL) { | |
321 return NGX_CONF_ERROR; | |
322 } | |
577 | 323 |
593 | 324 rlcf->keys->pool = cf->pool; |
325 rlcf->keys->temp_pool = cf->pool; | |
326 | |
327 if (ngx_hash_keys_array_init(rlcf->keys, NGX_HASH_SMALL) != NGX_OK) { | |
577 | 328 return NGX_CONF_ERROR; |
329 } | |
330 } | |
331 | |
332 value = cf->args->elts; | |
333 | |
334 for (i = 1; i < cf->args->nelts; i++) { | |
335 if (value[i].len == 0) { | |
336 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
337 "invalid referer \"%V\"", &value[i]); | |
338 return NGX_CONF_ERROR; | |
339 } | |
340 | |
341 if (ngx_strcmp(value[i].data, "none") == 0) { | |
593 | 342 rlcf->no_referer = 1; |
577 | 343 continue; |
344 } | |
345 | |
346 if (ngx_strcmp(value[i].data, "blocked") == 0) { | |
593 | 347 rlcf->blocked_referer = 1; |
577 | 348 continue; |
349 } | |
350 | |
595 | 351 uri.len = 0; |
613 | 352 uri.data = NULL; |
595 | 353 |
577 | 354 if (ngx_strcmp(value[i].data, "server_names") == 0) { |
593 | 355 |
356 cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module); | |
577 | 357 |
593 | 358 sn = cscf->server_names.elts; |
359 for (n = 0; n < cscf->server_names.nelts; n++) { | |
595 | 360 if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name, &uri) |
361 != NGX_OK) | |
362 { | |
593 | 363 return NGX_CONF_ERROR; |
364 } | |
365 } | |
577 | 366 |
367 continue; | |
368 } | |
369 | |
1242 | 370 p = (u_char *) ngx_strchr(value[i].data, '/'); |
577 | 371 |
593 | 372 if (p) { |
595 | 373 uri.len = (value[i].data + value[i].len) - p; |
374 uri.data = p; | |
593 | 375 value[i].len = p - value[i].data; |
577 | 376 } |
377 | |
595 | 378 if (ngx_http_add_referer(cf, rlcf->keys, &value[i], &uri) != NGX_OK) { |
577 | 379 return NGX_CONF_ERROR; |
380 } | |
381 } | |
382 | |
383 return NGX_CONF_OK; | |
384 } | |
593 | 385 |
386 | |
387 static char * | |
388 ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys, | |
595 | 389 ngx_str_t *value, ngx_str_t *uri) |
593 | 390 { |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
391 ngx_int_t rc; |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
392 ngx_str_t *u; |
593 | 393 |
595 | 394 if (uri->len == 0) { |
395 u = NGX_HTTP_REFERER_NO_URI_PART; | |
396 | |
397 } else { | |
398 u = ngx_palloc(cf->pool, sizeof(ngx_str_t)); | |
399 if (u == NULL) { | |
400 return NGX_CONF_ERROR; | |
401 } | |
402 | |
403 *u = *uri; | |
404 } | |
405 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
406 rc = ngx_hash_add_key(keys, value, u, NGX_HASH_WILDCARD_KEY); |
593 | 407 |
408 if (rc == NGX_OK) { | |
409 return NGX_CONF_OK; | |
410 } | |
411 | |
1253
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
412 if (rc == NGX_DECLINED) { |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
413 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
414 "invalid hostname or wildcard \"%V\"", value); |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
415 } |
8ef04207c84f
the "www.example.*" wildcard hash support
Igor Sysoev <igor@sysoev.ru>
parents:
1242
diff
changeset
|
416 |
593 | 417 if (rc == NGX_BUSY) { |
418 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
419 "conflicting parameter \"%V\"", value); | |
420 } | |
421 | |
422 return NGX_CONF_ERROR; | |
423 } | |
424 | |
425 | |
426 static int ngx_libc_cdecl | |
427 ngx_http_cmp_referer_wildcards(const void *one, const void *two) | |
428 { | |
429 ngx_hash_key_t *first, *second; | |
430 | |
431 first = (ngx_hash_key_t *) one; | |
432 second = (ngx_hash_key_t *) two; | |
433 | |
434 return ngx_strcmp(first->key.data, second->key.data); | |
435 } |