/
handler_secdownload.c
252 lines (202 loc) · 6.33 KB
/
handler_secdownload.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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2010 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "handler_secdownload.h"
#include "connection-protected.h"
#include "util.h"
#define ENTRIES "handler,secdownload"
ret_t
cherokee_handler_secdownload_props_free (cherokee_handler_secdownload_props_t *props)
{
if (props->props_file != NULL) {
cherokee_handler_file_props_free (props->props_file);
props->props_file = NULL;
}
cherokee_buffer_mrproper (&props->secret);
return cherokee_handler_props_free_base (HANDLER_PROPS(props));
}
ret_t
cherokee_handler_secdownload_configure (cherokee_config_node_t *conf,
cherokee_server_t *srv,
cherokee_module_props_t **_props)
{
ret_t ret;
cherokee_handler_secdownload_props_t *props;
if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, handler_secdownload_props);
cherokee_handler_props_init_base (HANDLER_PROPS(n),
MODULE_PROPS_FREE(cherokee_handler_secdownload_props_free));
cherokee_buffer_init (&n->secret);
n->timeout = 60;
*_props = MODULE_PROPS(n);
}
props = PROP_SECDOWN(*_props);
/* Parse 'file' parameters
*/
props->props_file = NULL;
ret = cherokee_handler_file_configure (conf, srv, (cherokee_module_props_t **)&props->props_file);
if ((ret != ret_ok) && (ret != ret_deny))
return ret;
/* Properties
*/
ret = cherokee_config_node_copy (conf, "secret", &props->secret);
if (ret != ret_ok) {
LOG_CRITICAL_S (CHEROKEE_ERROR_HANDLER_SECDOWN_SECRET);
return ret_error;
}
cherokee_config_node_read_int (conf, "timeout", (int*)&props->timeout);
return ret_ok;
}
static int
check_hex (char *p, int len)
{
int i;
for (i=0; i < len; i++) {
if (!((p[i] >= '0' && p[i] <= '9') ||
(p[i] >= 'a' && p[i] <= 'f') ||
(p[i] >= 'A' && p[i] <= 'F')))
{
return 1;
}
}
return 0;
}
static int
get_time (char *p)
{
int i;
time_t time = 0;
for (i=0; i<8; i++) {
time = (time << 4) + hex2dec_tab[(int)p[i]];
}
return time;
}
ret_t
cherokee_handler_secdownload_new (cherokee_handler_t **hdl,
void *cnt,
cherokee_module_props_t *props)
{
int re;
ret_t ret;
char *p;
cuint_t path_len;
time_t time_url;
char *time_s;
cherokee_buffer_t md5 = CHEROKEE_BUF_INIT;
cherokee_connection_t *conn = CONN(cnt);
TRACE(ENTRIES, "Analyzing request '%s'\n", conn->request.buf);
/* Sanity check
*/
if (conn->request.len <= 1 + 32 + 2) {
TRACE(ENTRIES, "Malformed URL. Too short: len=%d.\n", conn->request.len);
conn->error_code = http_not_found;
return ret_error;
}
if (conn->request.buf[0] != '/') {
TRACE(ENTRIES, "Malformed URL: %s\n", "Not slash (1)");
conn->error_code = http_not_found;
return ret_error;
}
p = conn->request.buf + 1;
if (check_hex (p, 32)) {
TRACE(ENTRIES, "Malformed URL: %s\n", "No MD5");
conn->error_code = http_not_found;
return ret_error;
}
p += 32;
if (*p != '/') {
TRACE(ENTRIES, "Malformed URL: %s\n", "Not slash (2)");
conn->error_code = http_not_found;
return ret_error;
}
p += 1;
time_s = p;
if (check_hex (p, 8)) {
TRACE(ENTRIES, "Malformed URL: %s\n", "No MD5 (2)");
conn->error_code = http_not_found;
return ret_error;
}
p += 8;
/* Check the time
*/
time_url = get_time (time_s);
if ((cherokee_bogonow_now - time_url) > (int)PROP_SECDOWN(props)->timeout) {
TRACE(ENTRIES, "Time out: %d (now=%d)\n", time_url, cherokee_bogonow_now);
conn->error_code = http_gone;
return ret_error;
}
/* Check the MD5
* [secret][path][hex(time)]
*/
path_len = (conn->request.buf + conn->request.len) - p;
cherokee_buffer_add_buffer (&md5, &PROP_SECDOWN(props)->secret);
cherokee_buffer_add (&md5, p, path_len);
cherokee_buffer_add (&md5, time_s, 8);
cherokee_buffer_encode_md5_digest (&md5);
re = strncasecmp (md5.buf, &conn->request.buf[1], 32);
if (re != 0) {
#ifdef TRACE_ENABLED
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_add_str (&tmp, "secret='");
cherokee_buffer_add_buffer (&tmp, &PROP_SECDOWN(props)->secret);
cherokee_buffer_add_str (&tmp, "', path='");
cherokee_buffer_add (&tmp, p, path_len);
cherokee_buffer_add_str (&tmp, "', time='");
cherokee_buffer_add (&tmp, time_s, 8);
cherokee_buffer_add_str (&tmp, "'");
TRACE(ENTRIES, "MD5 (%s) didn't match (%s)\n", md5.buf, tmp.buf);
cherokee_buffer_mrproper(&tmp);
#endif
cherokee_buffer_mrproper(&md5);
conn->error_code = http_access_denied;
return ret_error;
}
cherokee_buffer_mrproper (&md5);
/* At this point the request has been validated
*/
if (cherokee_buffer_is_empty (&conn->request_original)) {
cherokee_buffer_add_buffer (&conn->request_original, &conn->request);
cherokee_buffer_add_buffer (&conn->query_string_original, &conn->query_string);
}
cherokee_buffer_clean (&conn->request);
cherokee_buffer_add (&conn->request, p, path_len);
/* Instance the File handler
*/
ret = cherokee_handler_file_new (hdl, cnt, MODULE_PROPS(PROP_SECDOWN(props)->props_file));
if (ret != ret_ok)
return ret_ok;
return ret_ok;
}
/* Library init function
*/
static cherokee_boolean_t _secdownload_is_init = false;
void
PLUGIN_INIT_NAME(secdownload) (cherokee_plugin_loader_t *loader)
{
if (_secdownload_is_init) return;
_secdownload_is_init = true;
/* Load the dependences
*/
cherokee_plugin_loader_load (loader, "file");
}
PLUGIN_INFO_HANDLER_EASY_INIT (secdownload, http_all_methods);