3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
17 ngx_uint_t deny; /* unsigned deny:1; */
18 } ngx_http_access_rule_t;
22 ngx_array_t *rules; /* array of ngx_http_access_rule_t */
23 } ngx_http_access_loc_conf_t;
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,
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_conf_t *cf);
35 static ngx_command_t ngx_http_access_commands[] = {
37 { ngx_string("allow"),
38 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
41 NGX_HTTP_LOC_CONF_OFFSET,
46 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
49 NGX_HTTP_LOC_CONF_OFFSET,
58 static ngx_http_module_t ngx_http_access_module_ctx = {
59 NULL, /* preconfiguration */
60 ngx_http_access_init, /* postconfiguration */
62 NULL, /* create main configuration */
63 NULL, /* init main configuration */
65 NULL, /* create server configuration */
66 NULL, /* merge server configuration */
68 ngx_http_access_create_loc_conf, /* create location configuration */
69 ngx_http_access_merge_loc_conf /* merge location configuration */
73 ngx_module_t ngx_http_access_module = {
75 &ngx_http_access_module_ctx, /* module context */
76 ngx_http_access_commands, /* module directives */
77 NGX_HTTP_MODULE, /* module type */
78 NULL, /* init master */
79 NULL, /* init module */
80 NULL, /* init process */
81 NULL, /* init thread */
82 NULL, /* exit thread */
83 NULL, /* exit process */
84 NULL, /* exit master */
90 ngx_http_access_handler(ngx_http_request_t *r)
93 struct sockaddr_in *sin;
94 ngx_http_access_rule_t *rule;
95 ngx_http_core_loc_conf_t *clcf;
96 ngx_http_access_loc_conf_t *alcf;
98 alcf = ngx_http_get_module_loc_conf(r, ngx_http_access_module);
100 if (alcf->rules == NULL) {
106 sin = (struct sockaddr_in *) r->connection->sockaddr;
108 rule = alcf->rules->elts;
109 for (i = 0; i < alcf->rules->nelts; i++) {
111 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
112 "access: %08XD %08XD %08XD",
113 sin->sin_addr.s_addr, rule[i].mask, rule[i].addr);
115 if ((sin->sin_addr.s_addr & rule[i].mask) == rule[i].addr) {
117 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
119 if (clcf->satisfy == NGX_HTTP_SATISFY_ALL) {
120 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
121 "access forbidden by rule");
124 return NGX_HTTP_FORBIDDEN;
136 ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
138 ngx_http_access_loc_conf_t *alcf = conf;
142 ngx_inet_cidr_t in_cidr;
143 ngx_http_access_rule_t *rule;
145 if (alcf->rules == NULL) {
146 alcf->rules = ngx_array_create(cf->pool, 4,
147 sizeof(ngx_http_access_rule_t));
148 if (alcf->rules == NULL) {
149 return NGX_CONF_ERROR;
153 rule = ngx_array_push(alcf->rules);
155 return NGX_CONF_ERROR;
158 value = cf->args->elts;
160 rule->deny = (value[0].data[0] == 'd') ? 1 : 0;
162 if (value[1].len == 3 && ngx_strcmp(value[1].data, "all") == 0) {
169 rc = ngx_ptocidr(&value[1], &in_cidr);
171 if (rc == NGX_ERROR) {
172 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
174 return NGX_CONF_ERROR;
177 if (rc == NGX_DONE) {
178 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
179 "low address bits of %V are meaningless", &value[1]);
182 rule->mask = in_cidr.mask;
183 rule->addr = in_cidr.addr;
190 ngx_http_access_create_loc_conf(ngx_conf_t *cf)
192 ngx_http_access_loc_conf_t *conf;
194 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_access_loc_conf_t));
196 return NGX_CONF_ERROR;
204 ngx_http_access_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
206 ngx_http_access_loc_conf_t *prev = parent;
207 ngx_http_access_loc_conf_t *conf = child;
209 if (conf->rules == NULL) {
210 conf->rules = prev->rules;
218 ngx_http_access_init(ngx_conf_t *cf)
220 ngx_http_handler_pt *h;
221 ngx_http_core_main_conf_t *cmcf;
223 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
225 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
230 *h = ngx_http_access_handler;