3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
15 ngx_uint_t deny; /* unsigned deny:1; */
16 } ngx_http_access_rule_t;
20 ngx_array_t *rules; /* array of ngx_http_access_rule_t */
21 } ngx_http_access_loc_conf_t;
24 static ngx_int_t ngx_http_access_handler(ngx_http_request_t *r);
25 static char *ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd,
27 static void *ngx_http_access_create_loc_conf(ngx_conf_t *cf);
28 static char *ngx_http_access_merge_loc_conf(ngx_conf_t *cf,
29 void *parent, void *child);
30 static ngx_int_t ngx_http_access_init(ngx_conf_t *cf);
33 static ngx_command_t ngx_http_access_commands[] = {
35 { ngx_string("allow"),
36 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
39 NGX_HTTP_LOC_CONF_OFFSET,
44 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
47 NGX_HTTP_LOC_CONF_OFFSET,
56 static ngx_http_module_t ngx_http_access_module_ctx = {
57 NULL, /* preconfiguration */
58 ngx_http_access_init, /* postconfiguration */
60 NULL, /* create main configuration */
61 NULL, /* init main configuration */
63 NULL, /* create server configuration */
64 NULL, /* merge server configuration */
66 ngx_http_access_create_loc_conf, /* create location configuration */
67 ngx_http_access_merge_loc_conf /* merge location configuration */
71 ngx_module_t ngx_http_access_module = {
73 &ngx_http_access_module_ctx, /* module context */
74 ngx_http_access_commands, /* module directives */
75 NGX_HTTP_MODULE, /* module type */
76 NULL, /* init master */
77 NULL, /* init module */
78 NULL, /* init process */
79 NULL, /* init thread */
80 NULL, /* exit thread */
81 NULL, /* exit process */
82 NULL, /* exit master */
88 ngx_http_access_handler(ngx_http_request_t *r)
91 struct sockaddr_in *sin;
92 ngx_http_access_rule_t *rule;
93 ngx_http_core_loc_conf_t *clcf;
94 ngx_http_access_loc_conf_t *alcf;
96 alcf = ngx_http_get_module_loc_conf(r, ngx_http_access_module);
98 if (alcf->rules == NULL) {
104 if (r->connection->sockaddr->sa_family != AF_INET) {
108 sin = (struct sockaddr_in *) r->connection->sockaddr;
110 rule = alcf->rules->elts;
111 for (i = 0; i < alcf->rules->nelts; i++) {
113 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
114 "access: %08XD %08XD %08XD",
115 sin->sin_addr.s_addr, rule[i].mask, rule[i].addr);
117 if ((sin->sin_addr.s_addr & rule[i].mask) == rule[i].addr) {
119 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
121 if (clcf->satisfy == NGX_HTTP_SATISFY_ALL) {
122 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
123 "access forbidden by rule");
126 return NGX_HTTP_FORBIDDEN;
138 ngx_http_access_rule(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
140 ngx_http_access_loc_conf_t *alcf = conf;
144 ngx_inet_cidr_t in_cidr;
145 ngx_http_access_rule_t *rule;
147 if (alcf->rules == NULL) {
148 alcf->rules = ngx_array_create(cf->pool, 4,
149 sizeof(ngx_http_access_rule_t));
150 if (alcf->rules == NULL) {
151 return NGX_CONF_ERROR;
155 rule = ngx_array_push(alcf->rules);
157 return NGX_CONF_ERROR;
160 value = cf->args->elts;
162 rule->deny = (value[0].data[0] == 'd') ? 1 : 0;
164 if (value[1].len == 3 && ngx_strcmp(value[1].data, "all") == 0) {
171 rc = ngx_ptocidr(&value[1], &in_cidr);
173 if (rc == NGX_ERROR) {
174 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
176 return NGX_CONF_ERROR;
179 if (rc == NGX_DONE) {
180 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
181 "low address bits of %V are meaningless", &value[1]);
184 rule->mask = in_cidr.mask;
185 rule->addr = in_cidr.addr;
192 ngx_http_access_create_loc_conf(ngx_conf_t *cf)
194 ngx_http_access_loc_conf_t *conf;
196 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_access_loc_conf_t));
198 return NGX_CONF_ERROR;
206 ngx_http_access_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
208 ngx_http_access_loc_conf_t *prev = parent;
209 ngx_http_access_loc_conf_t *conf = child;
211 if (conf->rules == NULL) {
212 conf->rules = prev->rules;
220 ngx_http_access_init(ngx_conf_t *cf)
222 ngx_http_handler_pt *h;
223 ngx_http_core_main_conf_t *cmcf;
225 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
227 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
232 *h = ngx_http_access_handler;