2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_hash_t hash;
|
|
|
|
ngx_hash_wildcard_t *dns_wildcards;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_flag_t no_referer;
|
|
|
|
ngx_flag_t blocked_referer;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_hash_keys_arrays_t *keys;
|
2005-11-15 21:30:52 +08:00
|
|
|
} ngx_http_referer_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
static void * ngx_http_referer_create_conf(ngx_conf_t *cf);
|
|
|
|
static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent,
|
|
|
|
void *child);
|
|
|
|
static char *ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
2005-12-27 01:07:48 +08:00
|
|
|
static char *ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys,
|
|
|
|
ngx_str_t *value);
|
|
|
|
static int ngx_libc_cdecl ngx_http_cmp_referer_wildcards(const void *one,
|
|
|
|
const void *two);
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_referer_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("valid_referers"),
|
|
|
|
NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
|
|
|
ngx_http_valid_referers,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_referer_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
NULL, /* postconfiguration */
|
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
ngx_http_referer_create_conf, /* create location configuration */
|
|
|
|
ngx_http_referer_merge_conf /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_referer_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_http_referer_module_ctx, /* module context */
|
|
|
|
ngx_http_referer_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
|
|
|
NULL, /* init master */
|
|
|
|
NULL, /* init module */
|
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
|
|
|
|
uintptr_t data)
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
u_char *p, *ref;
|
|
|
|
size_t len;
|
|
|
|
ngx_http_referer_conf_t *rlcf;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
rlcf = ngx_http_get_module_loc_conf(r, ngx_http_referer_module);
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->hash.buckets == NULL
|
|
|
|
&& rlcf->dns_wildcards == NULL
|
|
|
|
&& rlcf->dns_wildcards->hash.buckets == NULL)
|
|
|
|
{
|
2005-11-15 21:30:52 +08:00
|
|
|
*v = ngx_http_variable_null_value;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->headers_in.referer == NULL) {
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->no_referer) {
|
2005-11-15 21:30:52 +08:00
|
|
|
*v = ngx_http_variable_null_value;
|
|
|
|
return NGX_OK;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*v = ngx_http_variable_true_value;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = r->headers_in.referer->value.len;
|
|
|
|
ref = r->headers_in.referer->value.data;
|
|
|
|
|
|
|
|
if (len < sizeof("http://i.ru") - 1
|
|
|
|
|| (ngx_strncasecmp(ref, "http://", 7) != 0))
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->blocked_referer) {
|
2005-11-15 21:30:52 +08:00
|
|
|
*v = ngx_http_variable_null_value;
|
|
|
|
return NGX_OK;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*v = ngx_http_variable_true_value;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= 7;
|
|
|
|
ref += 7;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
for (p = ref; p < ref + len; p++) {
|
|
|
|
if (*p == '/' || *p == ':') {
|
|
|
|
break;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
2005-12-27 01:07:48 +08:00
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
len = p - ref;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->hash.buckets) {
|
|
|
|
if (ngx_hash_find(&rlcf->hash, ngx_hash_key_lc(ref, len), ref, len)) {
|
|
|
|
*v = ngx_http_variable_null_value;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->dns_wildcards && rlcf->dns_wildcards->hash.buckets) {
|
|
|
|
if (ngx_hash_find_wildcard(rlcf->dns_wildcards, ref, len)) {
|
|
|
|
*v = ngx_http_variable_null_value;
|
|
|
|
return NGX_OK;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = ngx_http_variable_true_value;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_http_referer_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_referer_conf_t *conf;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_referer_conf_t));
|
2005-11-15 21:30:52 +08:00
|
|
|
if (conf == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
conf->no_referer = NGX_CONF_UNSET;
|
|
|
|
conf->blocked_referer = NGX_CONF_UNSET;
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_http_referer_conf_t *prev = parent;
|
|
|
|
ngx_http_referer_conf_t *conf = child;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_hash_init_t hash;
|
|
|
|
|
|
|
|
if (conf->keys == NULL) {
|
|
|
|
conf->hash = prev->hash;
|
|
|
|
conf->dns_wildcards = prev->dns_wildcards;
|
|
|
|
|
2005-11-15 21:30:52 +08:00
|
|
|
ngx_conf_merge_value(conf->no_referer, prev->no_referer, 0);
|
|
|
|
ngx_conf_merge_value(conf->blocked_referer, prev->blocked_referer, 0);
|
2005-12-27 01:07:48 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash.key = ngx_hash_key_lc;
|
|
|
|
hash.max_size = 2048; /* TODO: referer_hash_max_size; */
|
|
|
|
hash.bucket_size = 64; /* TODO: referer_hash_bucket_size; */
|
|
|
|
hash.name = "referers_hash";
|
|
|
|
hash.pool = cf->pool;
|
|
|
|
|
|
|
|
if (conf->keys->keys.nelts) {
|
|
|
|
hash.hash = &conf->hash;
|
|
|
|
hash.temp_pool = NULL;
|
|
|
|
|
|
|
|
if (ngx_hash_init(&hash, conf->keys->keys.elts, conf->keys->keys.nelts)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf->keys->dns_wildcards.nelts) {
|
|
|
|
|
|
|
|
ngx_qsort(conf->keys->dns_wildcards.elts,
|
|
|
|
(size_t) conf->keys->dns_wildcards.nelts,
|
|
|
|
sizeof(ngx_hash_key_t),
|
|
|
|
ngx_http_cmp_referer_wildcards);
|
|
|
|
|
|
|
|
hash.hash = NULL;
|
|
|
|
hash.temp_pool = cf->temp_pool;
|
|
|
|
|
|
|
|
if (ngx_hash_wildcard_init(&hash, conf->keys->dns_wildcards.elts,
|
|
|
|
conf->keys->dns_wildcards.nelts)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
conf->dns_wildcards = (ngx_hash_wildcard_t *) hash.hash;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (conf->no_referer == NGX_CONF_UNSET) {
|
|
|
|
conf->no_referer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf->blocked_referer == NGX_CONF_UNSET) {
|
|
|
|
conf->blocked_referer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_http_referer_conf_t *rlcf = conf;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
u_char *p;
|
2005-11-15 21:30:52 +08:00
|
|
|
ngx_str_t *value, name;
|
2005-12-27 01:07:48 +08:00
|
|
|
ngx_uint_t i, n;
|
2005-11-15 21:30:52 +08:00
|
|
|
ngx_http_variable_t *var;
|
|
|
|
ngx_http_server_name_t *sn;
|
|
|
|
ngx_http_core_srv_conf_t *cscf;
|
|
|
|
|
|
|
|
name.len = sizeof("invalid_referer") - 1;
|
|
|
|
name.data = (u_char *) "invalid_referer";
|
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
var = ngx_http_add_variable(cf, &name,
|
|
|
|
NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH);
|
2005-11-15 21:30:52 +08:00
|
|
|
if (var == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
var->handler = ngx_http_referer_variable;
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rlcf->keys == NULL) {
|
|
|
|
rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t));
|
|
|
|
if (rlcf->keys == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rlcf->keys->pool = cf->pool;
|
|
|
|
rlcf->keys->temp_pool = cf->pool;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (ngx_hash_keys_array_init(rlcf->keys, NGX_HASH_SMALL) != NGX_OK) {
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
for (i = 1; i < cf->args->nelts; i++) {
|
|
|
|
if (value[i].len == 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid referer \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[i].data, "none") == 0) {
|
2005-12-27 01:07:48 +08:00
|
|
|
rlcf->no_referer = 1;
|
2005-11-15 21:30:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[i].data, "blocked") == 0) {
|
2005-12-27 01:07:48 +08:00
|
|
|
rlcf->blocked_referer = 1;
|
2005-11-15 21:30:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strcmp(value[i].data, "server_names") == 0) {
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
|
|
|
|
|
|
|
|
sn = cscf->server_names.elts;
|
|
|
|
for (n = 0; n < cscf->server_names.nelts; n++) {
|
|
|
|
if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
p = (u_char *) ngx_strstr(value[i].data, "/");
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (p) {
|
2005-11-15 21:30:52 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2005-12-27 01:07:48 +08:00
|
|
|
"URI part \"%s\" is deprecated, ignored", p);
|
|
|
|
|
|
|
|
value[i].len = p - value[i].data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_http_add_referer(cf, rlcf->keys, &value[i]) != NGX_OK) {
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-12-27 01:07:48 +08:00
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_add_referer(ngx_conf_t *cf, ngx_hash_keys_arrays_t *keys,
|
|
|
|
ngx_str_t *value)
|
|
|
|
{
|
|
|
|
u_char ch;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_uint_t flags;
|
|
|
|
|
|
|
|
ch = value->data[0];
|
|
|
|
|
|
|
|
if ((ch == '*' && (value->len < 3 || value->data[1] != '.'))
|
|
|
|
|| (ch == '.' && value->len < 2))
|
|
|
|
{
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid DNS wildcard \"%V\"", value);
|
|
|
|
|
|
|
|
return NGX_CONF_ERROR;
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
flags = (ch == '*' || ch == '.') ? NGX_HASH_WILDCARD_KEY : 0;
|
|
|
|
|
|
|
|
rc = ngx_hash_add_key(keys, value, (void *) 4, flags);
|
|
|
|
|
|
|
|
if (rc == NGX_OK) {
|
2005-11-15 21:30:52 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
if (rc == NGX_BUSY) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"conflicting parameter \"%V\"", value);
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2005-11-15 21:30:52 +08:00
|
|
|
|
|
|
|
|
2005-12-27 01:07:48 +08:00
|
|
|
static int ngx_libc_cdecl
|
|
|
|
ngx_http_cmp_referer_wildcards(const void *one, const void *two)
|
|
|
|
{
|
|
|
|
ngx_hash_key_t *first, *second;
|
|
|
|
|
|
|
|
first = (ngx_hash_key_t *) one;
|
|
|
|
second = (ngx_hash_key_t *) two;
|
|
|
|
|
|
|
|
return ngx_strcmp(first->key.data, second->key.data);
|
2005-11-15 21:30:52 +08:00
|
|
|
}
|