126
|
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
|
144
|
12 #define NGX_HTTP_REFERER_NO_URI_PART ((void *) 4)
|
|
13
|
126
|
14 typedef struct {
|
312
|
15 ngx_hash_combined_t hash;
|
126
|
16
|
142
|
17 ngx_flag_t no_referer;
|
|
18 ngx_flag_t blocked_referer;
|
126
|
19
|
142
|
20 ngx_hash_keys_arrays_t *keys;
|
126
|
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);
|
142
|
29 static char *ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys,
|
144
|
30 ngx_str_t *value, ngx_str_t *uri);
|
142
|
31 static int ngx_libc_cdecl ngx_http_cmp_referer_wildcards(const void *one,
|
|
32 const void *two);
|
126
|
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 {
|
144
|
83 u_char *p, *ref, *last;
|
142
|
84 size_t len;
|
144
|
85 ngx_str_t *uri;
|
|
86 ngx_uint_t i, key;
|
142
|
87 ngx_http_referer_conf_t *rlcf;
|
144
|
88 u_char buf[256];
|
126
|
89
|
142
|
90 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_referer_module);
|
126
|
91
|
312
|
92 if (rlcf->hash.hash.buckets == NULL
|
|
93 && rlcf->hash.wc_head == NULL
|
|
94 && rlcf->hash.wc_tail == NULL)
|
|
95 {
|
144
|
96 goto valid;
|
126
|
97 }
|
|
98
|
|
99 if (r->headers_in.referer == NULL) {
|
142
|
100 if (rlcf->no_referer) {
|
144
|
101 goto valid;
|
|
102 }
|
126
|
103
|
144
|
104 goto invalid;
|
126
|
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
|
286
|
111 || (ngx_strncasecmp(ref, (u_char *) "http://", 7) != 0))
|
126
|
112 {
|
142
|
113 if (rlcf->blocked_referer) {
|
144
|
114 goto valid;
|
|
115 }
|
126
|
116
|
144
|
117 goto invalid;
|
126
|
118 }
|
|
119
|
144
|
120 last = ref + len;
|
126
|
121 ref += 7;
|
144
|
122 i = 0;
|
|
123 key = 0;
|
126
|
124
|
144
|
125 for (p = ref; p < last; p++) {
|
142
|
126 if (*p == '/' || *p == ':') {
|
|
127 break;
|
126
|
128 }
|
144
|
129
|
|
130 buf[i] = ngx_tolower(*p);
|
|
131 key = ngx_hash(key, buf[i++]);
|
|
132
|
|
133 if (i == 256) {
|
|
134 goto invalid;
|
|
135 }
|
142
|
136 }
|
126
|
137
|
142
|
138 len = p - ref;
|
126
|
139
|
312
|
140 uri = ngx_hash_find_combined(&rlcf->hash, key, buf, len);
|
126
|
141
|
312
|
142 if (uri) {
|
|
143 goto uri;
|
126
|
144 }
|
|
145
|
144
|
146 invalid:
|
|
147
|
126
|
148 *v = ngx_http_variable_true_value;
|
|
149
|
|
150 return NGX_OK;
|
144
|
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;
|
126
|
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
|
142
|
183 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_referer_conf_t));
|
126
|
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
|
142
|
201 ngx_hash_init_t hash;
|
|
202
|
|
203 if (conf->keys == NULL) {
|
|
204 conf->hash = prev->hash;
|
|
205
|
126
|
206 ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0);
|
|
207 ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0);
|
142
|
208
|
|
209 return NGX_CONF_OK;
|
|
210 }
|
|
211
|
152
|
212 if ((conf->no_referer == 1 || conf->blocked_referer == 1)
|
312
|
213 && conf->keys->keys.nelts == 0
|
|
214 && conf->keys->dns_wc_head.nelts == 0
|
|
215 && conf->keys->dns_wc_tail.nelts == 0)
|
152
|
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
|
142
|
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) {
|
312
|
231 hash.hash = &conf->hash.hash;
|
142
|
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
|
312
|
241 if (conf->keys->dns_wc_head.nelts) {
|
142
|
242
|
312
|
243 ngx_qsort(conf->keys->dns_wc_head.elts,
|
|
244 (size_t) conf->keys->dns_wc_head.nelts,
|
142
|
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
|
312
|
251 if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_head.elts,
|
|
252 conf->keys->dns_wc_head.nelts)
|
142
|
253 != NGX_OK)
|
|
254 {
|
|
255 return NGX_CONF_ERROR;
|
|
256 }
|
|
257
|
312
|
258 conf->hash.wc_head = (ngx_hash_wildcard_t *) hash.hash;
|
|
259 }
|
|
260
|
|
261 if (conf->keys->dns_wc_tail.nelts) {
|
|
262
|
|
263 ngx_qsort(conf->keys->dns_wc_tail.elts,
|
|
264 (size_t) conf->keys->dns_wc_tail.nelts,
|
|
265 sizeof(ngx_hash_key_t),
|
|
266 ngx_http_cmp_referer_wildcards);
|
|
267
|
|
268 hash.hash = NULL;
|
|
269 hash.temp_pool = cf->temp_pool;
|
|
270
|
|
271 if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wc_tail.elts,
|
|
272 conf->keys->dns_wc_tail.nelts)
|
|
273 != NGX_OK)
|
|
274 {
|
|
275 return NGX_CONF_ERROR;
|
|
276 }
|
|
277
|
|
278 conf->hash.wc_tail = (ngx_hash_wildcard_t *) hash.hash;
|
126
|
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
|
160
|
289 conf->keys = NULL;
|
|
290
|
126
|
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 {
|
142
|
298 ngx_http_referer_conf_t *rlcf = conf;
|
126
|
299
|
142
|
300 u_char *p;
|
144
|
301 ngx_str_t *value, uri, name;
|
142
|
302 ngx_uint_t i, n;
|
126
|
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
|
132
|
310 var = ngx_http_add_variable(cf, &name,
|
|
311 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH);
|
126
|
312 if (var == NULL) {
|
|
313 return NGX_CONF_ERROR;
|
|
314 }
|
|
315
|
186
|
316 var->get_handler = ngx_http_referer_variable;
|
126
|
317
|
142
|
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 }
|
126
|
323
|
142
|
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) {
|
126
|
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) {
|
142
|
342 rlcf->no_referer = 1;
|
126
|
343 continue;
|
|
344 }
|
|
345
|
|
346 if (ngx_strcmp(value[i].data, "blocked") == 0) {
|
142
|
347 rlcf->blocked_referer = 1;
|
126
|
348 continue;
|
|
349 }
|
|
350
|
144
|
351 uri.len = 0;
|
162
|
352 uri.data = NULL;
|
144
|
353
|
126
|
354 if (ngx_strcmp(value[i].data, "server_names") == 0) {
|
142
|
355
|
|
356 cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
|
126
|
357
|
142
|
358 sn = cscf->server_names.elts;
|
|
359 for (n = 0; n < cscf->server_names.nelts; n++) {
|
144
|
360 if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name, &uri)
|
|
361 != NGX_OK)
|
|
362 {
|
142
|
363 return NGX_CONF_ERROR;
|
|
364 }
|
|
365 }
|
126
|
366
|
|
367 continue;
|
|
368 }
|
|
369
|
310
|
370 p = (u_char *) ngx_strchr(value[i].data, '/');
|
126
|
371
|
142
|
372 if (p) {
|
144
|
373 uri.len = (value[i].data + value[i].len) - p;
|
|
374 uri.data = p;
|
142
|
375 value[i].len = p - value[i].data;
|
126
|
376 }
|
|
377
|
144
|
378 if (ngx_http_add_referer(cf, rlcf->keys, &value[i], &uri) != NGX_OK) {
|
126
|
379 return NGX_CONF_ERROR;
|
|
380 }
|
|
381 }
|
|
382
|
|
383 return NGX_CONF_OK;
|
|
384 }
|
142
|
385
|
|
386
|
|
387 static char *
|
|
388 ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys,
|
144
|
389 ngx_str_t *value, ngx_str_t *uri)
|
142
|
390 {
|
312
|
391 ngx_int_t rc;
|
|
392 ngx_str_t *u;
|
142
|
393
|
144
|
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
|
312
|
406 rc = ngx_hash_add_key(keys, value, u, NGX_HASH_WILDCARD_KEY);
|
142
|
407
|
|
408 if (rc == NGX_OK) {
|
|
409 return NGX_CONF_OK;
|
|
410 }
|
|
411
|
312
|
412 if (rc == NGX_DECLINED) {
|
|
413 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
414 "invalid hostname or wildcard \"%V\"", value);
|
|
415 }
|
|
416
|
142
|
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 }
|