50
|
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 = {
|
58
|
57 NULL, /* preconfiguration */
|
|
58 NULL, /* postconfiguration */
|
50
|
59
|
|
60 NULL, /* create main configuration */
|
|
61 NULL, /* init main configuration */
|
|
62
|
|
63 NULL, /* create server configuration */
|
|
64 NULL, /* merge server configuration */
|
|
65
|
|
66 ngx_http_access_create_loc_conf, /* create location configuration */
|
|
67 ngx_http_access_merge_loc_conf /* merge location configuration */
|
|
68 };
|
|
69
|
|
70
|
|
71 ngx_module_t ngx_http_access_module = {
|
58
|
72 NGX_MODULE_V1,
|
50
|
73 &ngx_http_access_module_ctx, /* module context */
|
|
74 ngx_http_access_commands, /* module directives */
|
|
75 NGX_HTTP_MODULE, /* module type */
|
|
76 ngx_http_access_init, /* init module */
|
|
77 NULL /* init process */
|
|
78 };
|
|
79
|
|
80
|
|
81 static ngx_int_t
|
|
82 ngx_http_access_handler(ngx_http_request_t *r)
|
|
83 {
|
|
84 ngx_uint_t i;
|
|
85 struct sockaddr_in *sin;
|
|
86 ngx_http_access_rule_t *rule;
|
|
87 ngx_http_access_loc_conf_t *alcf;
|
|
88
|
|
89 alcf = ngx_http_get_module_loc_conf(r, ngx_http_access_module);
|
|
90
|
|
91 if (alcf->rules == NULL) {
|
|
92 return NGX_OK;
|
|
93 }
|
|
94
|
|
95 /* AF_INET only */
|
|
96
|
|
97 sin = (struct sockaddr_in *) r->connection->sockaddr;
|
|
98
|
|
99 rule = alcf->rules->elts;
|
|
100 for (i = 0; i < alcf->rules->nelts; i++) {
|
|
101
|
|
102 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
103 "%08XD %08XD %08XD",
|
|
104 sin->sin_addr.s_addr, rule[i].mask, rule[i].addr);
|
|
105
|
|
106 if ((sin->sin_addr.s_addr & rule[i].mask) == rule[i].addr) {
|
|
107 if (rule[i].deny) {
|
|
108 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
109 "access forbidden by rule");
|
|
110
|
|
111 return NGX_HTTP_FORBIDDEN;
|
|
112 }
|
|
113
|
|
114 return NGX_OK;
|
|
115 }
|
|
116 }
|
|
117
|
|
118 return NGX_OK;
|
|
119 }
|
|
120
|
|
121
|
|
122 static char *
|
|
123 ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
124 {
|
|
125 ngx_http_access_loc_conf_t *alcf = conf;
|
|
126
|
|
127 ngx_str_t *value;
|
|
128 ngx_inet_cidr_t in_cidr;
|
|
129 ngx_http_access_rule_t *rule;
|
|
130
|
|
131 if (alcf->rules == NULL) {
|
|
132 alcf->rules = ngx_array_create(cf->pool, 4,
|
|
133 sizeof(ngx_http_access_rule_t));
|
|
134 if (alcf->rules == NULL) {
|
|
135 return NGX_CONF_ERROR;
|
|
136 }
|
|
137 }
|
|
138
|
|
139 rule = ngx_array_push(alcf->rules);
|
|
140 if (rule == NULL) {
|
|
141 return NGX_CONF_ERROR;
|
|
142 }
|
|
143
|
|
144 value = cf->args->elts;
|
|
145
|
|
146 rule->deny = (value[0].data[0] == 'd') ? 1 : 0;
|
|
147
|
|
148 if (value[1].len == 3 && ngx_strcmp(value[1].data, "all") == 0) {
|
|
149 rule->mask = 0;
|
|
150 rule->addr = 0;
|
|
151
|
|
152 return NGX_CONF_OK;
|
|
153 }
|
|
154
|
|
155 rule->addr = inet_addr((char *) value[1].data);
|
|
156
|
|
157 if (rule->addr != INADDR_NONE) {
|
|
158 rule->mask = 0xffffffff;
|
|
159
|
|
160 return NGX_CONF_OK;
|
|
161 }
|
|
162
|
|
163 if (ngx_ptocidr(&value[1], &in_cidr) == NGX_ERROR) {
|
|
164 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
|
|
165 &value[1]);
|
|
166 return NGX_CONF_ERROR;
|
|
167 }
|
|
168
|
|
169 rule->mask = in_cidr.mask;
|
|
170 rule->addr = in_cidr.addr;
|
|
171
|
|
172 return NGX_CONF_OK;
|
|
173 }
|
|
174
|
|
175
|
|
176 static void *
|
|
177 ngx_http_access_create_loc_conf(ngx_conf_t *cf)
|
|
178 {
|
|
179 ngx_http_access_loc_conf_t *conf;
|
|
180
|
|
181 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_access_loc_conf_t));
|
|
182 if (conf == NULL) {
|
|
183 return NGX_CONF_ERROR;
|
|
184 }
|
|
185
|
|
186 return conf;
|
|
187 }
|
|
188
|
|
189
|
|
190 static char *
|
|
191 ngx_http_access_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
192 {
|
|
193 ngx_http_access_loc_conf_t *prev = parent;
|
|
194 ngx_http_access_loc_conf_t *conf = child;
|
|
195
|
|
196 if (conf->rules == NULL) {
|
|
197 conf->rules = prev->rules;
|
|
198 }
|
|
199
|
|
200 return NGX_CONF_OK;
|
|
201 }
|
|
202
|
|
203
|
|
204 static ngx_int_t
|
|
205 ngx_http_access_init(ngx_cycle_t *cycle)
|
|
206 {
|
|
207 ngx_http_handler_pt *h;
|
|
208 ngx_http_core_main_conf_t *cmcf;
|
|
209
|
|
210 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
211
|
|
212 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
|
|
213 if (h == NULL) {
|
|
214 return NGX_ERROR;
|
|
215 }
|
|
216
|
|
217 *h = ngx_http_access_handler;
|
|
218
|
|
219 return NGX_OK;
|
|
220 }
|