Mercurial > hg > nginx
comparison src/http/modules/ngx_http_secure_link_module.c @ 3756:7224d008faaf
new ngx_http_secure_link_module with secure_link, secure_link_md5, and
secure_link_expires
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 02 Sep 2010 14:37:16 +0000 |
parents | c7d57b539248 |
children | 38f74d11e5bd |
comparison
equal
deleted
inserted
replaced
3755:76e3a93821b1 | 3756:7224d008faaf |
---|---|
9 #include <ngx_http.h> | 9 #include <ngx_http.h> |
10 #include <ngx_md5.h> | 10 #include <ngx_md5.h> |
11 | 11 |
12 | 12 |
13 typedef struct { | 13 typedef struct { |
14 ngx_str_t secret; | 14 ngx_http_complex_value_t *variable; |
15 ngx_http_complex_value_t *md5; | |
16 ngx_str_t secret; | |
17 ngx_flag_t expires; | |
15 } ngx_http_secure_link_conf_t; | 18 } ngx_http_secure_link_conf_t; |
16 | 19 |
17 | 20 |
21 typedef struct { | |
22 ngx_str_t expires; | |
23 } ngx_http_secure_link_ctx_t; | |
24 | |
25 | |
26 static ngx_int_t ngx_http_secure_link_old_variable(ngx_http_request_t *r, | |
27 ngx_http_secure_link_conf_t *conf, ngx_http_variable_value_t *v, | |
28 uintptr_t data); | |
29 static ngx_int_t ngx_http_secure_link_expires_variable(ngx_http_request_t *r, | |
30 ngx_http_variable_value_t *v, uintptr_t data); | |
18 static void *ngx_http_secure_link_create_conf(ngx_conf_t *cf); | 31 static void *ngx_http_secure_link_create_conf(ngx_conf_t *cf); |
19 static char *ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, | 32 static char *ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, |
20 void *child); | 33 void *child); |
21 static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf); | 34 static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf); |
22 | 35 |
23 | 36 |
24 static ngx_command_t ngx_http_secure_link_commands[] = { | 37 static ngx_command_t ngx_http_secure_link_commands[] = { |
38 | |
39 { ngx_string("secure_link"), | |
40 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
41 ngx_http_set_comlex_value_slot, | |
42 NGX_HTTP_LOC_CONF_OFFSET, | |
43 offsetof(ngx_http_secure_link_conf_t, variable), | |
44 NULL }, | |
45 | |
46 { ngx_string("secure_link_md5"), | |
47 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
48 ngx_http_set_comlex_value_slot, | |
49 NGX_HTTP_LOC_CONF_OFFSET, | |
50 offsetof(ngx_http_secure_link_conf_t, md5), | |
51 NULL }, | |
52 | |
53 { ngx_string("secure_link_expires"), | |
54 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
55 ngx_conf_set_flag_slot, | |
56 NGX_HTTP_LOC_CONF_OFFSET, | |
57 offsetof(ngx_http_secure_link_conf_t, expires), | |
58 NULL }, | |
25 | 59 |
26 { ngx_string("secure_link_secret"), | 60 { ngx_string("secure_link_secret"), |
27 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | 61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
28 ngx_conf_set_str_slot, | 62 ngx_conf_set_str_slot, |
29 NGX_HTTP_LOC_CONF_OFFSET, | 63 NGX_HTTP_LOC_CONF_OFFSET, |
63 NULL, /* exit master */ | 97 NULL, /* exit master */ |
64 NGX_MODULE_V1_PADDING | 98 NGX_MODULE_V1_PADDING |
65 }; | 99 }; |
66 | 100 |
67 | 101 |
68 static ngx_str_t ngx_http_secure_link = ngx_string("secure_link"); | 102 static ngx_str_t ngx_http_secure_link_name = ngx_string("secure_link"); |
103 static ngx_str_t ngx_http_secure_link_expires_name = | |
104 ngx_string("secure_link_expires"); | |
69 | 105 |
70 | 106 |
71 static ngx_int_t | 107 static ngx_int_t |
72 ngx_http_secure_link_variable(ngx_http_request_t *r, | 108 ngx_http_secure_link_variable(ngx_http_request_t *r, |
73 ngx_http_variable_value_t *v, uintptr_t data) | 109 ngx_http_variable_value_t *v, uintptr_t data) |
74 { | 110 { |
75 u_char *p, *start, *end, *last; | 111 u_char *p, *last; |
76 size_t len; | 112 ngx_str_t val, hash; |
77 ngx_int_t n; | 113 time_t expires; |
78 ngx_uint_t i; | 114 ngx_md5_t md5; |
79 ngx_md5_t md5; | 115 ngx_http_secure_link_ctx_t *ctx; |
80 ngx_http_secure_link_conf_t *conf; | 116 ngx_http_secure_link_conf_t *conf; |
81 u_char hash[16]; | 117 u_char hash_buf[16], md5_buf[16]; |
82 | 118 |
83 conf = ngx_http_get_module_loc_conf(r, ngx_http_secure_link_module); | 119 conf = ngx_http_get_module_loc_conf(r, ngx_http_secure_link_module); |
84 | 120 |
85 if (conf->secret.len == 0) { | 121 if (conf->secret.len) { |
86 goto not_found; | 122 return ngx_http_secure_link_old_variable(r, conf, v, data); |
87 } | 123 } |
124 | |
125 if (conf->variable == NULL || conf->md5 == NULL) { | |
126 goto not_found; | |
127 } | |
128 | |
129 if (ngx_http_complex_value(r, conf->variable, &val) != NGX_OK) { | |
130 return NGX_ERROR; | |
131 } | |
132 | |
133 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
134 "secure link: \"%V\"", &val); | |
135 | |
136 last = val.data + val.len; | |
137 | |
138 p = ngx_strlchr(val.data, last, ','); | |
139 expires = 0; | |
140 | |
141 if (p) { | |
142 val.len = p++ - val.data; | |
143 | |
144 if (conf->expires) { | |
145 expires = ngx_atotm(p, last - p); | |
146 if (expires <= 0) { | |
147 goto not_found; | |
148 } | |
149 | |
150 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_secure_link_ctx_t)); | |
151 if (ctx == NULL) { | |
152 return NGX_ERROR; | |
153 } | |
154 | |
155 ngx_http_set_ctx(r, ctx, ngx_http_secure_link_module); | |
156 | |
157 ctx->expires.len = last - p; | |
158 ctx->expires.data = p; | |
159 } | |
160 } | |
161 | |
162 if (val.len > 24) { | |
163 goto not_found; | |
164 } | |
165 | |
166 hash.len = 16; | |
167 hash.data = hash_buf; | |
168 | |
169 if (ngx_decode_base64url(&hash, &val) != NGX_OK) { | |
170 goto not_found; | |
171 } | |
172 | |
173 if (hash.len != 16) { | |
174 goto not_found; | |
175 } | |
176 | |
177 if (ngx_http_complex_value(r, conf->md5, &val) != NGX_OK) { | |
178 return NGX_ERROR; | |
179 } | |
180 | |
181 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
182 "secure link md5: \"%V\"", &val); | |
183 | |
184 ngx_md5_init(&md5); | |
185 ngx_md5_update(&md5, val.data, val.len); | |
186 ngx_md5_final(md5_buf, &md5); | |
187 | |
188 if (ngx_memcmp(hash_buf, md5_buf, 16) != 0) { | |
189 goto not_found; | |
190 } | |
191 | |
192 v->data = (u_char *) ((expires && expires < ngx_time()) ? "0" : "1"); | |
193 v->len = 1; | |
194 v->valid = 1; | |
195 v->no_cacheable = 0; | |
196 v->not_found = 0; | |
197 | |
198 return NGX_OK; | |
199 | |
200 not_found: | |
201 | |
202 v->not_found = 1; | |
203 | |
204 return NGX_OK; | |
205 } | |
206 | |
207 | |
208 static ngx_int_t | |
209 ngx_http_secure_link_old_variable(ngx_http_request_t *r, | |
210 ngx_http_secure_link_conf_t *conf, ngx_http_variable_value_t *v, | |
211 uintptr_t data) | |
212 { | |
213 u_char *p, *start, *end, *last; | |
214 size_t len; | |
215 ngx_int_t n; | |
216 ngx_uint_t i; | |
217 ngx_md5_t md5; | |
218 u_char hash[16]; | |
88 | 219 |
89 p = &r->unparsed_uri.data[1]; | 220 p = &r->unparsed_uri.data[1]; |
90 last = r->unparsed_uri.data + r->unparsed_uri.len; | 221 last = r->unparsed_uri.data + r->unparsed_uri.len; |
91 | 222 |
92 while (p < last) { | 223 while (p < last) { |
143 | 274 |
144 return NGX_OK; | 275 return NGX_OK; |
145 } | 276 } |
146 | 277 |
147 | 278 |
279 static ngx_int_t | |
280 ngx_http_secure_link_expires_variable(ngx_http_request_t *r, | |
281 ngx_http_variable_value_t *v, uintptr_t data) | |
282 { | |
283 ngx_http_secure_link_ctx_t *ctx; | |
284 | |
285 ctx = ngx_http_get_module_ctx(r, ngx_http_secure_link_module); | |
286 | |
287 if (ctx) { | |
288 v->len = ctx->expires.len; | |
289 v->valid = 1; | |
290 v->no_cacheable = 0; | |
291 v->not_found = 0; | |
292 v->data = ctx->expires.data; | |
293 | |
294 } else { | |
295 v->not_found = 1; | |
296 } | |
297 | |
298 return NGX_OK; | |
299 } | |
300 | |
301 | |
148 static void * | 302 static void * |
149 ngx_http_secure_link_create_conf(ngx_conf_t *cf) | 303 ngx_http_secure_link_create_conf(ngx_conf_t *cf) |
150 { | 304 { |
151 ngx_http_secure_link_conf_t *conf; | 305 ngx_http_secure_link_conf_t *conf; |
152 | 306 |
156 } | 310 } |
157 | 311 |
158 /* | 312 /* |
159 * set by ngx_pcalloc(): | 313 * set by ngx_pcalloc(): |
160 * | 314 * |
161 * conf->secret = { 0, NULL } | 315 * conf->variable = NULL; |
316 * conf->md5 = NULL; | |
317 * conf->secret = { 0, NULL }; | |
162 */ | 318 */ |
319 | |
320 conf->expires = NGX_CONF_UNSET; | |
163 | 321 |
164 return conf; | 322 return conf; |
165 } | 323 } |
166 | 324 |
167 | 325 |
171 ngx_http_secure_link_conf_t *prev = parent; | 329 ngx_http_secure_link_conf_t *prev = parent; |
172 ngx_http_secure_link_conf_t *conf = child; | 330 ngx_http_secure_link_conf_t *conf = child; |
173 | 331 |
174 ngx_conf_merge_str_value(conf->secret, prev->secret, ""); | 332 ngx_conf_merge_str_value(conf->secret, prev->secret, ""); |
175 | 333 |
334 if (conf->variable == NULL) { | |
335 conf->variable = prev->variable; | |
336 } | |
337 | |
338 if (conf->md5 == NULL) { | |
339 conf->md5 = prev->md5; | |
340 } | |
341 | |
342 ngx_conf_merge_value(conf->expires, prev->expires, 0); | |
343 | |
176 return NGX_CONF_OK; | 344 return NGX_CONF_OK; |
177 } | 345 } |
178 | 346 |
179 | 347 |
180 static ngx_int_t | 348 static ngx_int_t |
181 ngx_http_secure_link_add_variables(ngx_conf_t *cf) | 349 ngx_http_secure_link_add_variables(ngx_conf_t *cf) |
182 { | 350 { |
183 ngx_http_variable_t *var; | 351 ngx_http_variable_t *var; |
184 | 352 |
185 var = ngx_http_add_variable(cf, &ngx_http_secure_link, NGX_HTTP_VAR_NOHASH); | 353 var = ngx_http_add_variable(cf, &ngx_http_secure_link_name, 0); |
186 if (var == NULL) { | 354 if (var == NULL) { |
187 return NGX_ERROR; | 355 return NGX_ERROR; |
188 } | 356 } |
189 | 357 |
190 var->get_handler = ngx_http_secure_link_variable; | 358 var->get_handler = ngx_http_secure_link_variable; |
191 | 359 |
192 return NGX_OK; | 360 var = ngx_http_add_variable(cf, &ngx_http_secure_link_expires_name, 0); |
193 } | 361 if (var == NULL) { |
362 return NGX_ERROR; | |
363 } | |
364 | |
365 var->get_handler = ngx_http_secure_link_expires_variable; | |
366 | |
367 return NGX_OK; | |
368 } |