3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
15 } ngx_http_secure_link_conf_t;
18 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,
21 static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf);
24 static ngx_command_t ngx_http_secure_link_commands[] = {
26 { ngx_string("secure_link_secret"),
27 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
28 ngx_conf_set_str_slot,
29 NGX_HTTP_LOC_CONF_OFFSET,
30 offsetof(ngx_http_secure_link_conf_t, secret),
37 static ngx_http_module_t ngx_http_secure_link_module_ctx = {
38 ngx_http_secure_link_add_variables, /* preconfiguration */
39 NULL, /* postconfiguration */
41 NULL, /* create main configuration */
42 NULL, /* init main configuration */
44 NULL, /* create server configuration */
45 NULL, /* merge server configuration */
47 ngx_http_secure_link_create_conf, /* create location configuration */
48 ngx_http_secure_link_merge_conf /* merge location configuration */
52 ngx_module_t ngx_http_secure_link_module = {
54 &ngx_http_secure_link_module_ctx, /* module context */
55 ngx_http_secure_link_commands, /* module directives */
56 NGX_HTTP_MODULE, /* module type */
57 NULL, /* init master */
58 NULL, /* init module */
59 NULL, /* init process */
60 NULL, /* init thread */
61 NULL, /* exit thread */
62 NULL, /* exit process */
63 NULL, /* exit master */
68 static ngx_str_t ngx_http_secure_link = ngx_string("secure_link");
72 ngx_http_secure_link_variable(ngx_http_request_t *r,
73 ngx_http_variable_value_t *v, uintptr_t data)
75 u_char *p, *start, *end, *last;
80 ngx_http_secure_link_conf_t *conf;
83 conf = ngx_http_get_module_loc_conf(r, ngx_http_secure_link_module);
85 if (conf->secret.len == 0) {
89 p = &r->unparsed_uri.data[1];
90 last = r->unparsed_uri.data + r->unparsed_uri.len;
116 if (end - start != 32 || len == 0) {
121 ngx_md5_update(&md5, p, len);
122 ngx_md5_update(&md5, conf->secret.data, conf->secret.len);
123 ngx_md5_final(hash, &md5);
125 for (i = 0; i < 16; i++) {
126 n = ngx_hextoi(&start[2 * i], 2);
127 if (n == NGX_ERROR || n != hash[i]) {
149 ngx_http_secure_link_create_conf(ngx_conf_t *cf)
151 ngx_http_secure_link_conf_t *conf;
153 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_secure_link_conf_t));
155 return NGX_CONF_ERROR;
159 * set by ngx_pcalloc():
161 * conf->secret = { 0, NULL }
169 ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, void *child)
171 ngx_http_secure_link_conf_t *prev = parent;
172 ngx_http_secure_link_conf_t *conf = child;
174 ngx_conf_merge_str_value(conf->secret, prev->secret, "");
181 ngx_http_secure_link_add_variables(ngx_conf_t *cf)
183 ngx_http_variable_t *var;
185 var = ngx_http_add_variable(cf, &ngx_http_secure_link, NGX_HTTP_VAR_NOHASH);
190 var->get_handler = ngx_http_secure_link_variable;