-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_accesskey_module.c
305 lines (240 loc) · 8.99 KB
/
ngx_http_accesskey_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* nginx (c) Igor Sysoev
* this module (C) Mykola Grechukh <gns@altlinux.org>
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <openssl/md5.h>
#define MD5Init MD5_Init
#define MD5Update MD5_Update
#define MD5Final MD5_Final
#include <openssl/sha.h>
#define NGX_ACCESSKEY_MD5 1
#define NGX_ACCESSKEY_SHA1 2
typedef struct {
ngx_flag_t enable;
ngx_str_t arg;
ngx_uint_t hashmethod;
ngx_str_t signature;
ngx_array_t *signature_lengths;
ngx_array_t *signature_values;
} ngx_http_accesskey_loc_conf_t;
static ngx_int_t ngx_http_accesskey_handler(ngx_http_request_t *r);
static char *ngx_http_accesskey_signature(ngx_conf_t *cf, void *post, void *data);
static char *ngx_http_accesskey_hashmethod(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_accesskey_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_accesskey_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_accesskey_init(ngx_conf_t *cf);
static ngx_conf_post_handler_pt ngx_http_accesskey_signature_p =
ngx_http_accesskey_signature;
static ngx_command_t ngx_http_accesskey_commands[] = {
{ ngx_string("accesskey"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_accesskey_loc_conf_t, enable),
NULL },
{ ngx_string("accesskey_hashmethod"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_accesskey_hashmethod,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("accesskey_signature"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_accesskey_loc_conf_t, signature),
&ngx_http_accesskey_signature_p },
{ ngx_string("accesskey_arg"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_accesskey_loc_conf_t, arg),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_accesskey_module_ctx = {
NULL, /* preconfiguration */
ngx_http_accesskey_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_accesskey_create_loc_conf, /* create location configuration */
ngx_http_accesskey_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_accesskey_module = {
NGX_MODULE_V1,
&ngx_http_accesskey_module_ctx, /* module context */
ngx_http_accesskey_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_accesskey_handler(ngx_http_request_t *r)
{
ngx_uint_t i;
ngx_uint_t hashlength,bhashlength;
ngx_http_accesskey_loc_conf_t *alcf;
alcf = ngx_http_get_module_loc_conf(r, ngx_http_accesskey_module);
if (!alcf->enable) {
return NGX_OK;
}
if (!alcf->signature_lengths || !alcf->signature_values) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"accesskey enabled, but signature not configured!");
return NGX_HTTP_FORBIDDEN;
}
switch(alcf->hashmethod) {
case NGX_ACCESSKEY_SHA1:
bhashlength=20; break;
case NGX_ACCESSKEY_MD5:
bhashlength=16; break;
default:
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"accesskey: hash not supported");
return NGX_HTTP_FORBIDDEN;
}
hashlength=bhashlength*2;
ngx_str_t args = r->args;
ngx_str_t look = alcf->arg;
ngx_uint_t j=0,k=0,l=0;
for (i = 0; i <= args.len; i++) {
if ( ( i == args.len) || (args.data[i] == '&') ) {
if (j > 1) { k = j; l = i; }
j = 0;
} else if ( (j == 0) && (i<args.len-look.len) ) {
if ( (ngx_strncmp(args.data+i, look.data, look.len) == 0)
&& (args.data[i+look.len] == '=') ) {
j=i+look.len+1;
i=j-1;
} else j=1;
}
}
if (l-k!=hashlength) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"accesskey: length %d of \"%V\" argument is not equal %d",
l-k, &look, hashlength);
return NGX_HTTP_FORBIDDEN;
}
ngx_str_t val;
if (ngx_http_script_run(r, &val, alcf->signature_lengths->elts, 0, alcf->signature_values->elts) == NULL) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"accesskey: evaluation failed");
return NGX_ERROR;
}
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"accesskey: evaluated value of signature: \"%V\"", &val);
u_char hashb[64], hasht[128];
MD5_CTX md5;
SHA_CTX sha;
switch(alcf->hashmethod) {
case NGX_ACCESSKEY_MD5:
MD5Init(&md5);
MD5Update(&md5,val.data,val.len);
MD5Final(hashb, &md5);
break;
case NGX_ACCESSKEY_SHA1:
SHA1_Init(&sha);
SHA1_Update(&sha,val.data,val.len);
SHA1_Final(hashb,&sha);
break;
};
static u_char hex[] = "0123456789abcdef";
u_char *text = hasht;
for (i = 0; i < bhashlength; i++) {
*text++ = hex[hashb[i] >> 4];
*text++ = hex[hashb[i] & 0xf];
}
*text = '\0';
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"accesskey: hash value \"%s\"", hasht);
if (ngx_strncmp(hasht,args.data+k,hashlength)!=0)
return NGX_HTTP_FORBIDDEN;
return NGX_OK;
}
static char *
ngx_http_accesskey_compile_signature(ngx_conf_t *cf, ngx_http_accesskey_loc_conf_t *alcf)
{
ngx_http_script_compile_t sc;
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
sc.cf = cf;
sc.source = &alcf->signature;
sc.lengths = &alcf->signature_lengths;
sc.values = &alcf->signature_values;
sc.variables = ngx_http_script_variables_count(&alcf->signature);;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_http_accesskey_signature(ngx_conf_t *cf, void *post, void *data)
{
ngx_http_accesskey_loc_conf_t *alcf =
ngx_http_conf_get_module_loc_conf(cf, ngx_http_accesskey_module);
return ngx_http_accesskey_compile_signature(cf, alcf);
}
static char *
ngx_http_accesskey_hashmethod(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *d = cf->args->elts;
ngx_http_accesskey_loc_conf_t *alcf = conf;
if ( (d[1].len == 3 ) && (ngx_strncmp(d[1].data,"md5",3) == 0) ) {
alcf->hashmethod = NGX_ACCESSKEY_MD5;
} else if ( (d[1].len == 4) && (ngx_strncmp(d[1].data,"sha1",4) == 0) ){
alcf->hashmethod = NGX_ACCESSKEY_SHA1;
} else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"accesskey_hashmethod should be md5 or sha1, not \"%V\"", d+1);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static void *
ngx_http_accesskey_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_accesskey_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_accesskey_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_accesskey_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_accesskey_loc_conf_t *prev = parent;
ngx_http_accesskey_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_uint_value(conf->hashmethod, prev->hashmethod, NGX_ACCESSKEY_MD5);
ngx_conf_merge_str_value(conf->arg, prev->arg, "key");
ngx_conf_merge_str_value(conf->signature,prev->signature,"$remote_addr");
return ngx_http_accesskey_compile_signature(cf, conf);
}
static ngx_int_t
ngx_http_accesskey_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_accesskey_handler;
return NGX_OK;
}