comparison src/http/modules/ngx_http_access_handler.c @ 0:f0b350454894 NGINX_0_1_0

nginx 0.1.0 *) The first public version.
author Igor Sysoev <http://sysoev.ru>
date Mon, 04 Oct 2004 00:00:00 +0400
parents
children 46833bd150cb
comparison
equal deleted inserted replaced
-1:000000000000 0:f0b350454894
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 ngx_http_access_handler(ngx_http_request_t *r)
81 {
82 ngx_uint_t i;
83 struct sockaddr_in *addr_in;
84 ngx_http_access_rule_t *rule;
85 ngx_http_access_loc_conf_t *alcf;
86
87 alcf = ngx_http_get_module_loc_conf(r, ngx_http_access_module);
88
89 if (alcf->rules == NULL) {
90 return NGX_OK;
91 }
92
93 /* AF_INET only */
94
95 addr_in = (struct sockaddr_in *) r->connection->sockaddr;
96
97 rule = alcf->rules->elts;
98 for (i = 0; i < alcf->rules->nelts; i++) {
99
100 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "%08X %08X %08X",
101 addr_in->sin_addr.s_addr, rule[i].mask, rule[i].addr);
102
103 if ((addr_in->sin_addr.s_addr & rule[i].mask) == rule[i].addr) {
104 if (rule[i].deny) {
105 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
106 "access forbidden by rule");
107
108 return NGX_HTTP_FORBIDDEN;
109 }
110
111 return NGX_OK;
112 }
113 }
114
115 return NGX_OK;
116 }
117
118
119 static char *ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd,
120 void *conf)
121 {
122 ngx_http_access_loc_conf_t *alcf = conf;
123
124 ngx_str_t *value;
125 ngx_inet_cidr_t in_cidr;
126 ngx_http_access_rule_t *rule;
127
128 if (alcf->rules == NULL) {
129 alcf->rules = ngx_create_array(cf->pool, 5,
130 sizeof(ngx_http_access_rule_t));
131 if (alcf->rules == NULL) {
132 return NGX_CONF_ERROR;
133 }
134 }
135
136 if (!(rule = ngx_push_array(alcf->rules))) {
137 return NGX_CONF_ERROR;
138 }
139
140 value = cf->args->elts;
141
142 rule->deny = (value[0].data[0] == 'd') ? 1 : 0;
143
144 if (value[1].len == 3 && ngx_strcmp(value[1].data, "all") == 0) {
145 rule->mask = 0;
146 rule->addr = 0;
147
148 return NGX_CONF_OK;
149 }
150
151 rule->addr = inet_addr((char *) value[1].data);
152
153 if (rule->addr != INADDR_NONE) {
154 rule->mask = 0xffffffff;
155
156 return NGX_CONF_OK;
157 }
158
159 if (ngx_ptocidr(&value[1], &in_cidr) == NGX_ERROR) {
160 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid paramter \"%s\"",
161 value[1].data);
162 return NGX_CONF_ERROR;
163 }
164
165 rule->mask = in_cidr.mask;
166 rule->addr = in_cidr.addr;
167
168 return NGX_CONF_OK;
169 }
170
171
172 static void *ngx_http_access_create_loc_conf(ngx_conf_t *cf)
173 {
174 ngx_http_access_loc_conf_t *conf;
175
176 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_access_loc_conf_t)))) {
177 return NGX_CONF_ERROR;
178 }
179
180 return conf;
181 }
182
183
184 static char *ngx_http_access_merge_loc_conf(ngx_conf_t *cf,
185 void *parent, void *child)
186 {
187 ngx_http_access_loc_conf_t *prev = parent;
188 ngx_http_access_loc_conf_t *conf = child;
189
190 if (conf->rules == NULL) {
191 conf->rules = prev->rules;
192 }
193
194 return NGX_CONF_OK;
195 }
196
197
198 static ngx_int_t ngx_http_access_init(ngx_cycle_t *cycle)
199 {
200 ngx_http_handler_pt *h;
201 ngx_http_core_main_conf_t *cmcf;
202
203 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
204
205 h = ngx_push_array(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
206 if (h == NULL) {
207 return NGX_ERROR;
208 }
209
210 *h = ngx_http_access_handler;
211
212 return NGX_OK;
213 }