forked from replay/ngx_http_secure_download
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngx_http_secure_download_module.c
400 lines (335 loc) · 10.8 KB
/
ngx_http_secure_download_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <ngx_core.h>
#include <ngx_string.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mhash.h>
#include <openssl/md5.h>
#include <ctype.h>
#define FOLDER_MODE 0
#define FILE_MODE 1
typedef struct {
const char *timestamp;
const char *md5;
const char *path;
int path_len;
int path_to_hash_len;
} ngx_http_secure_download_split_uri_t;
static ngx_int_t ngx_http_secure_download_split_uri (ngx_http_request_t*, ngx_http_secure_download_split_uri_t*);
static ngx_int_t ngx_http_secure_download_check_hash(ngx_http_request_t*, ngx_http_secure_download_split_uri_t*, ngx_str_t*);
static void * ngx_http_secure_download_create_loc_conf(ngx_conf_t*);
static char * ngx_http_secure_download_merge_loc_conf (ngx_conf_t*, void*, void*);
static ngx_int_t ngx_http_secure_download_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_secure_download_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
static char * ngx_conf_set_path_mode(ngx_conf_t*, ngx_command_t*, void*);
static char *ngx_http_secure_download_secret(ngx_conf_t *cf, void *post, void *data);
static ngx_conf_post_handler_pt ngx_http_secure_download_secret_p =
ngx_http_secure_download_secret;
typedef struct {
ngx_flag_t enable;
ngx_flag_t path_mode;
ngx_str_t secret;
ngx_array_t *secret_lengths;
ngx_array_t *secret_values;
} ngx_http_secure_download_loc_conf_t;
static ngx_command_t ngx_http_secure_download_commands[] = {
{
ngx_string("secure_download"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_secure_download_loc_conf_t, enable),
NULL
},
{
ngx_string("secure_download_path_mode"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_path_mode,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_secure_download_loc_conf_t, path_mode),
NULL
},
{
ngx_string("secure_download_secret"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_secure_download_loc_conf_t, secret),
&ngx_http_secure_download_secret_p
}
};
static ngx_http_module_t ngx_http_secure_download_module_ctx = {
ngx_http_secure_download_add_variables,
NULL,
NULL,
NULL,
NULL,
NULL,
ngx_http_secure_download_create_loc_conf,
ngx_http_secure_download_merge_loc_conf
};
ngx_module_t ngx_http_secure_download_module = {
NGX_MODULE_V1,
&ngx_http_secure_download_module_ctx,
ngx_http_secure_download_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_str_t ngx_http_secure_download = ngx_string("secure_download");
static char * ngx_conf_set_path_mode(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *d = cf->args->elts;
ngx_http_secure_download_loc_conf_t *sdlc = conf;
if ((d[1].len == 6) && (strncmp((char*)d[1].data, "folder", 6) == 0))
{
sdlc->path_mode = FOLDER_MODE;
}
else if((d[1].len == 4) && (strncmp((char*)d[1].data, "file", 4) == 0))
{
sdlc->path_mode = FILE_MODE;
}
else
{
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "secure_download_path_mode should be folder or file", 0);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static void * ngx_http_secure_download_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_secure_download_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_secure_download_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
conf->path_mode = NGX_CONF_UNSET;
conf->secret.data = NULL;
conf->secret.len = 0;
return conf;
}
static char * ngx_http_secure_download_merge_loc_conf (ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_secure_download_loc_conf_t *prev = parent;
ngx_http_secure_download_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enable, prev->enable, 0);
ngx_conf_merge_value(conf->path_mode, prev->path_mode, FOLDER_MODE);
ngx_conf_merge_str_value(conf->secret, prev->secret, "");
if (conf->enable == 1) {
if (conf->secret.len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"no secure_download_secret specified");
return NGX_CONF_ERROR;
}
}
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_secure_download_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) {
unsigned timestamp;
unsigned remaining_time = 0;
ngx_http_secure_download_loc_conf_t *sdc;
ngx_http_secure_download_split_uri_t sdsu;
ngx_str_t rel_path;
ngx_str_t secret;
int value = 0;
sdc = ngx_http_get_module_loc_conf(r, ngx_http_secure_download_module);
if (sdc->enable != 1)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"securedownload: module not enabled");
value = -3;
goto finish;
}
if (!sdc->secret_lengths || !sdc->secret_values) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"securedownload: module enabled, but secret key not configured!");
value = -3;
goto finish;
}
if (ngx_http_secure_download_split_uri(r, &sdsu) == NGX_ERROR)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "received an error from ngx_http_secure_download_split_uri", 0);
value = -3;
goto finish;
}
if (sscanf(sdsu.timestamp, "%08X", ×tamp) != 1)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "error in timestamp hex-dec conversion", 0);
value = -3;
goto finish;
}
remaining_time = timestamp - (unsigned) time(NULL);
if ((int)remaining_time <= 0)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "expired timestamp", 0);
value = -1;
goto finish;
}
if (ngx_http_script_run(r, &secret, sdc->secret_lengths->elts, 0, sdc->secret_values->elts) == NULL) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"securedownload: evaluation failed");
value = -3;
goto finish;
}
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"securedownload: evaluated value of secret: \"%V\"", &secret);
if (ngx_http_secure_download_check_hash(r, &sdsu, &secret) != NGX_OK)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "bad hash", 0);
value = -2;
goto finish;
}
rel_path.data = r->uri.data;
rel_path.len = sdsu.path_len;
finish:
v->not_found = 0;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
if (value == 0)
{
v->data = ngx_pcalloc(r->pool, sizeof(char) * 12);
if (v->data == NULL) {
return NGX_ERROR;
}
v->len = (int) sprintf((char *)v->data, "%i", remaining_time);
//printf("valid, %i\n", remaining_time);
} else {
v->data = ngx_pcalloc(r->pool, sizeof(char) * 2);
if (v->data == NULL) {
return NGX_ERROR;
}
v->len = (int) sprintf((char*)v->data, "%i", value);
//printf("problem %i\n", value);
}
return NGX_OK;
}
//////////////////////
static char *
ngx_http_secure_download_compile_secret(ngx_conf_t *cf, ngx_http_secure_download_loc_conf_t *sdc)
{
ngx_http_script_compile_t sc;
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
sc.cf = cf;
sc.source = &sdc->secret;
sc.lengths = &sdc->secret_lengths;
sc.values = &sdc->secret_values;
sc.variables = ngx_http_script_variables_count(&sdc->secret);
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_secure_download_secret(ngx_conf_t *cf, void *post, void *data)
{
ngx_http_secure_download_loc_conf_t *sdc =
ngx_http_conf_get_module_loc_conf(cf, ngx_http_secure_download_module);
return ngx_http_secure_download_compile_secret(cf, sdc);
}
////////////////////////
static ngx_int_t ngx_http_secure_download_check_hash(ngx_http_request_t *r, ngx_http_secure_download_split_uri_t *sdsu, ngx_str_t *secret)
{
int i;
unsigned char generated_hash[16];
char hash[33];
MHASH td;
char *hash_data, *str;
int data_len;
static const char xtoc[] = "0123456789abcdef";
/* rel_path_to_hash/secret/timestamp\0 */
data_len = sdsu->path_to_hash_len + secret->len + 10;
hash_data = malloc(data_len + 1);
if (hash_data == NULL)
{
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "error in allocating memory for string_to_hash.data", 0);
return NGX_ERROR;
}
str = hash_data;
memcpy(str, sdsu->path, sdsu->path_to_hash_len);
str += sdsu->path_to_hash_len;
*str++ = '/';
memcpy(str, secret->data, secret->len);
str += secret->len;
*str++ = '/';
memcpy(str, sdsu->timestamp, 8);
str[8] = 0;
td = mhash_init(MHASH_MD5);
if (td == MHASH_FAILED)
{
free(hash_data);
return NGX_ERROR;
}
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "hashing string \"%s\" with len %i", hash_data, data_len);
mhash(td, hash_data, data_len);
mhash_deinit(td, generated_hash);
free(hash_data);
for (i = 0; i < 16; ++i) {
hash[2 * i + 0] = xtoc[generated_hash[i] >> 4];
hash[2 * i + 1] = xtoc[generated_hash[i] & 0xf];
}
hash[32] = 0; //because %.32 doesn't work
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "computed hash: %32s", hash);
// ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "hash from uri: %.32s", sdsu->md5);
if(memcmp(hash, sdsu->md5, 32) != 0) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "hash mismatch", 0);
return NGX_ERROR;
}
return NGX_OK;
}
static ngx_int_t ngx_http_secure_download_split_uri(ngx_http_request_t *r, ngx_http_secure_download_split_uri_t *sdsu)
{
int md5_len = 0;
int tstamp_len = 0;
int len = r->uri.len;
const char *uri = (char*)r->uri.data;
ngx_http_secure_download_loc_conf_t *sdc = ngx_http_get_module_loc_conf(r, ngx_http_secure_download_module);
while(len && uri[--len] != '/')
++tstamp_len;
if(tstamp_len != 8) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "timestamp size mismatch: %d", tstamp_len);
return NGX_ERROR;
}
sdsu->timestamp = uri + len + 1;
while(len && uri[--len] != '/')
++md5_len;
if(md5_len != 32) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "md5 size mismatch: %d", md5_len);
return NGX_ERROR;
}
sdsu->md5 = uri + len + 1;
if(len == 0) {
ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "bad path", 0);
return NGX_ERROR;
}
sdsu->path = uri;
sdsu->path_len = len;
if(sdc->path_mode == FOLDER_MODE) {
while(len && uri[--len] != '/');
}
sdsu->path_to_hash_len = len;
return NGX_OK;
}
static ngx_int_t
ngx_http_secure_download_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var;
var = ngx_http_add_variable(cf, &ngx_http_secure_download, NGX_HTTP_VAR_NOHASH);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = ngx_http_secure_download_variable;
return NGX_OK;
}