comparison src/http/modules/ngx_http_access_module.c @ 50:72eb30262aac NGINX_0_1_25

nginx 0.1.25 *) Bugfix: nginx did run on Linux parisc. *) Feature: nginx now does not start under FreeBSD if the sysctl kern.ipc.somaxconn value is too big. *) Bugfix: if a request was internally redirected by the ngx_http_index_module module to the ngx_http_proxy_module or ngx_http_fastcgi_module modules, then the index file was not closed after request completion. *) Feature: the "proxy_pass" can be used in location with regular expression. *) Feature: the ngx_http_rewrite_filter_module module supports the condition like "if ($HTTP_USER_AGENT ~ MSIE)". *) Bugfix: nginx started too slow if the large number of addresses and text values were used in the "geo" directive. *) Change: a variable name must be declared as "$name" in the "geo" directive. The previous variant without "$" is still supported, but will be removed soon. *) Feature: the "%{VARIABLE}v" logging parameter. *) Feature: the "set $name value" directive. *) Bugfix: gcc 4.0 compatibility. *) Feature: the --with-openssl-opt=OPTIONS autoconfiguration directive.
author Igor Sysoev <http://sysoev.ru>
date Sat, 19 Mar 2005 00:00:00 +0300
parents
children b55cbf18157e
comparison
equal deleted inserted replaced
49:93dabbc9efb9 50:72eb30262aac
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
12 /* AF_INET only */
13
14 typedef struct {
15 in_addr_t mask;
16 in_addr_t addr;
17 unsigned deny;
18 } ngx_http_access_rule_t;
19
20
21 typedef struct {
22 ngx_array_t *rules; /* array of ngx_http_access_rule_t */
23 } ngx_http_access_loc_conf_t;
24
25
26 static ngx_int_t ngx_http_access_handler(ngx_http_request_t *r);
27 static char *ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd,
28 void *conf);
29 static void *ngx_http_access_create_loc_conf(ngx_conf_t *cf);
30 static char *ngx_http_access_merge_loc_conf(ngx_conf_t *cf,
31 void *parent, void *child);
32 static ngx_int_t ngx_http_access_init(ngx_cycle_t *cycle);
33
34
35 static ngx_command_t ngx_http_access_commands[] = {
36
37 { ngx_string("allow"),
38 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
39 ngx_http_access_rule,
40 NGX_HTTP_LOC_CONF_OFFSET,
41 0,
42 NULL },
43
44 { ngx_string("deny"),
45 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
46 ngx_http_access_rule,
47 NGX_HTTP_LOC_CONF_OFFSET,
48 0,
49 NULL },
50
51 ngx_null_command
52 };
53
54
55
56 ngx_http_module_t ngx_http_access_module_ctx = {
57 NULL, /* pre conf */
58
59 NULL, /* create main configuration */
60 NULL, /* init main configuration */
61
62 NULL, /* create server configuration */
63 NULL, /* merge server configuration */
64
65 ngx_http_access_create_loc_conf, /* create location configuration */
66 ngx_http_access_merge_loc_conf /* merge location configuration */
67 };
68
69
70 ngx_module_t ngx_http_access_module = {
71 NGX_MODULE,
72 &ngx_http_access_module_ctx, /* module context */
73 ngx_http_access_commands, /* module directives */
74 NGX_HTTP_MODULE, /* module type */
75 ngx_http_access_init, /* init module */
76 NULL /* init process */
77 };
78
79
80 static ngx_int_t
81 ngx_http_access_handler(ngx_http_request_t *r)
82 {
83 ngx_uint_t i;
84 struct sockaddr_in *sin;
85 ngx_http_access_rule_t *rule;
86 ngx_http_access_loc_conf_t *alcf;
87
88 alcf = ngx_http_get_module_loc_conf(r, ngx_http_access_module);
89
90 if (alcf->rules == NULL) {
91 return NGX_OK;
92 }
93
94 /* AF_INET only */
95
96 sin = (struct sockaddr_in *) r->connection->sockaddr;
97
98 rule = alcf->rules->elts;
99 for (i = 0; i < alcf->rules->nelts; i++) {
100
101 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
102 "%08XD %08XD %08XD",
103 sin->sin_addr.s_addr, rule[i].mask, rule[i].addr);
104
105 if ((sin->sin_addr.s_addr & rule[i].mask) == rule[i].addr) {
106 if (rule[i].deny) {
107 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
108 "access forbidden by rule");
109
110 return NGX_HTTP_FORBIDDEN;
111 }
112
113 return NGX_OK;
114 }
115 }
116
117 return NGX_OK;
118 }
119
120
121 static char *
122 ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
123 {
124 ngx_http_access_loc_conf_t *alcf = conf;
125
126 ngx_str_t *value;
127 ngx_inet_cidr_t in_cidr;
128 ngx_http_access_rule_t *rule;
129
130 if (alcf->rules == NULL) {
131 alcf->rules = ngx_array_create(cf->pool, 4,
132 sizeof(ngx_http_access_rule_t));
133 if (alcf->rules == NULL) {
134 return NGX_CONF_ERROR;
135 }
136 }
137
138 rule = ngx_array_push(alcf->rules);
139 if (rule == NULL) {
140 return NGX_CONF_ERROR;
141 }
142
143 value = cf->args->elts;
144
145 rule->deny = (value[0].data[0] == 'd') ? 1 : 0;
146
147 if (value[1].len == 3 && ngx_strcmp(value[1].data, "all") == 0) {
148 rule->mask = 0;
149 rule->addr = 0;
150
151 return NGX_CONF_OK;
152 }
153
154 rule->addr = inet_addr((char *) value[1].data);
155
156 if (rule->addr != INADDR_NONE) {
157 rule->mask = 0xffffffff;
158
159 return NGX_CONF_OK;
160 }
161
162 if (ngx_ptocidr(&value[1], &in_cidr) == NGX_ERROR) {
163 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
164 &value[1]);
165 return NGX_CONF_ERROR;
166 }
167
168 rule->mask = in_cidr.mask;
169 rule->addr = in_cidr.addr;
170
171 return NGX_CONF_OK;
172 }
173
174
175 static void *
176 ngx_http_access_create_loc_conf(ngx_conf_t *cf)
177 {
178 ngx_http_access_loc_conf_t *conf;
179
180 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_access_loc_conf_t));
181 if (conf == NULL) {
182 return NGX_CONF_ERROR;
183 }
184
185 return conf;
186 }
187
188
189 static char *
190 ngx_http_access_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
191 {
192 ngx_http_access_loc_conf_t *prev = parent;
193 ngx_http_access_loc_conf_t *conf = child;
194
195 if (conf->rules == NULL) {
196 conf->rules = prev->rules;
197 }
198
199 return NGX_CONF_OK;
200 }
201
202
203 static ngx_int_t
204 ngx_http_access_init(ngx_cycle_t *cycle)
205 {
206 ngx_http_handler_pt *h;
207 ngx_http_core_main_conf_t *cmcf;
208
209 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
210
211 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
212 if (h == NULL) {
213 return NGX_ERROR;
214 }
215
216 *h = ngx_http_access_handler;
217
218 return NGX_OK;
219 }