-
Notifications
You must be signed in to change notification settings - Fork 1
/
cgi.c
358 lines (289 loc) · 7.46 KB
/
cgi.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
/*
* vQadmin Virtual Administration Interface
* Copyright (C) 2000-2002 Inter7 Internet Technologies, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "global.h"
struct var_t {
char *name,
*data;
struct var_t *next;
};
struct env_t {
char *name,
*data;
struct env_t *next;
};
extern char **environ;
extern char vqa_user[],
vqa_group[];
unsigned int content_length = 0;
char *gdata = NULL;
struct var_t *varlist = NULL;
struct env_t *envlist = NULL;
/*
Determine what user is trying to do based on variables supplied
*/
void cgi_nav(void)
{
char *r = NULL;
r = cgi_is_var("nav");
if (r) {
if (!(strcasecmp(r, "add_domain"))) add_domain();
else if (!(strcasecmp(r, "del_domain"))) del_domain();
else if (!(strcasecmp(r, "view_domain"))) view_domain();
else if (!(strcasecmp(r, "add_user"))) add_user();
else if (!(strcasecmp(r, "del_user"))) del_user();
else if (!(strcasecmp(r, "view_user"))) view_user();
else if (!(strcasecmp(r, "mod_user"))) mod_user();
else if (!(strcasecmp(r, "show_users"))) show_users();
else if (!(strcasecmp(r, "show_controls"))) show_controls();
else if (!(strcasecmp(r, "mod_domain"))) mod_domain();
else if (!(strcasecmp(r, "list_domains"))) list_domains();
else if (!(strcasecmp(r, "add_alias_domain"))) add_alias_domain();
else if (!(strcasecmp(r, "display_file"))) display_file();
else if (!(strcasecmp(r, "modify_file"))) modify_file();
else if (!(strcasecmp(r, "delete_file"))) delete_file();
}
t_main(1);
}
/*
Parse out information in stdin, if any
gdata contains parsed out data, which is pointed to by
the var_t linked list.
*/
void cgi_var(void)
{
struct var_t *v = NULL, *vt = NULL;
char *h = NULL, *t = NULL, *n = NULL, *d = NULL;
cgi_parse_hex();
varlist = (struct var_t *)malloc(sizeof(struct var_t));
varlist->next = NULL;
vt = varlist;
for (n = NULL, d = NULL, h = t = gdata;;) {
if ((*h == '=') && (n == NULL)) {
*h++ = '\0';
n = t;
t = h;
} else if (((*h == '&') && (n != NULL) && (d == NULL)) ||
((!(*h)) && (n != NULL) && (d == NULL))) {
if (*h) *h++ = '\0';
else h = NULL;
d = t;
t = h;
v = (struct var_t *)malloc(sizeof(struct var_t));
if (v == NULL) global_error("Out of memory", 1, 0);
v->name = n;
v->data = d;
v->next = NULL;
vt->next = v;
vt = v;
n = d = NULL;
if (h == NULL) break;
} else {
h++;
}
}
}
/*
Convert all hex codes in content to ascii
*/
void cgi_parse_hex(void)
{
unsigned int len = 0;
unsigned char r = 0;
char *p = NULL;
len = content_length;
for (p = gdata; *p; p++) {
len--;
if (*p == '%') {
if ((*(p + 1)) && (*(p + 2))) {
r = hex2asc(*(p + 1), *(p + 2));
*p = r;
memmove((char *)(p + 1), (char *)(p + 3), (len - 2));
*(p + len - 1) = '\0';
}
}
}
}
/*
Fetch HTTP environment variables
*/
void cgi_env(void)
{
int i = 0;
struct env_t *et = NULL, *e = NULL;
char *t = NULL, *n = NULL, *v = NULL;
int elen = 0, nlen = 0, vlen = 0;
envlist = (struct env_t *)malloc(sizeof(struct env_t));
if (envlist == NULL) global_error("Out of memory", 1, 0);
envlist->next = NULL;
et = envlist;
for (i = 0; environ[i]; i++) {
for (t = environ[i]; *t != '='; t++);
elen = strlen(environ[i]);
nlen = (elen - strlen(t));
vlen = (elen - nlen);
e = (struct env_t *)malloc(sizeof(struct env_t));
if (e == NULL) global_error("Out of memory", 1, 0);
n = (char *)malloc(nlen + 1);
if (n == NULL) global_error("Out of memory", 1, 0);
v = (char *)malloc(vlen + 1);
if (v == NULL) global_error("Out of memory", 1, 0);
e->name = n;
e->data = v;
e->next = NULL;
memset(n, 0, (nlen + 1));
memset(v, 0, (vlen + 1));
memcpy(n, environ[i], nlen);
memcpy(v, (++t), vlen);
et->next = e;
et = e;
}
}
void cgi_init(void)
{
int val = 0;
char *env = NULL;
int ret;
cgi_env();
env = cgi_is_env("REMOTE_USER");
if (!env) {
global_error("Username unknown", 0, 1);
t_page(T_AUTH_FAILED, 1);
}
memcpy((char *)vqa_user, (char *)env, MAX_GLOBAL_LENGTH);
acl_init();
env = cgi_is_env("REQUEST_METHOD");
if (env == NULL) global_error("Unknown request method", 1, 0);
if (strcasecmp(env, "POST")==0) {
env = cgi_is_env("CONTENT_LENGTH");
if (env == NULL) global_error("Unknown content", 1, 0);
val = atoi(env);
if (val < 1) global_error("Invalid content length", 1, 0);
if (val > MAX_CONTENT_LENGTH) val = (MAX_CONTENT_LENGTH - 1);
content_length = val;
gdata = malloc(content_length + 1);
if (gdata == NULL) global_error("Out of memory", 1, 0);
memset((unsigned char *)gdata, 0, (content_length + 1));
ret = read(0, (unsigned char *)gdata, content_length);
if (ret != content_length) global_error("Invalid content length", 1, 0);
cgi_var();
} else if (strcasecmp(env, "GET") == 0 ) {
env = getenv("QUERY_STRING");
if (env == NULL) global_error("No Query String", 1, 0);
content_length = strlen(env);
gdata = env;
cgi_var();
/*t_main(1);*/
} else {
global_error("Unsupported request method", 1, 0);
t_main(1);
}
}
char *cgi_is_env(char *name)
{
struct env_t *e = NULL;
for (e = envlist; e->next; e = e->next) {
if (!(strcmp(e->next->name, name))) return e->next->data;
}
return NULL;
}
char *cgi_is_var(char *name)
{
struct var_t *v = NULL;
for (v = varlist; v->next; v = v->next) {
if (!(strcmp(v->next->name, name))) return v->next->data;
}
return NULL;
}
char matoh(char x)
{
char ret = 0;
switch(x) {
case '0':
ret = 0;
break;
case '1':
ret = 1;
break;
case '2':
ret = 2;
break;
case '3':
ret = 3;
break;
case '4':
ret = 4;
break;
case '5':
ret = 5;
break;
case '6':
ret = 6;
break;
case '7':
ret = 7;
break;
case '8':
ret = 8;
break;
case '9':
ret = 9;
break;
case 'A':
ret = 10;
break;
case 'B':
ret = 11;
break;
case 'C':
ret = 12;
break;
case 'D':
ret = 13;
break;
case 'E':
ret = 14;
break;
case 'F':
ret = 15;
break;
default:
ret = -1;
break;
}
return ret;
}
unsigned char hex2asc(char s, char f)
{
char ret = 0;
unsigned char val1 = 0, val2 = 0, val3 = 0;
ret = matoh(f);
if (ret == -1) return 0;
val1 = ret;
ret = matoh(s);
if (ret == -1) return 0;
val2 = ret;
val3 = val1 + (val2 * 15) + val2;
return val3;
}