-
Notifications
You must be signed in to change notification settings - Fork 0
/
ksh-cgi.c
508 lines (388 loc) · 9.85 KB
/
ksh-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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <linux/limits.h>
#include <stdarg.h>
#include "common.h"
#include "messageheader.h"
// extern
int tmpfilescount = 0;
char percentdecode(const char *e)
{
char tmp[3];
char *endptr;
char c;
if (e[0] == '\0' || e[1] == '\0')
exitwithstatus(400, "cannot decode percent encoded string");
tmp[0] = e[0];
tmp[1] = e[1];
tmp[2] = '\0';
c = strtol(tmp, &endptr, 16);
if (errno == ERANGE || tmp != (endptr - 2))
exitwithstatus(400, "cannot decode percent encoded string");
return c;
}
int isallowed(char a)
{
if ((a >= 'a' && a <= 'z')
|| (a >= 'A' && a <= 'Z')
|| (a >= '0' && a <= '9')
|| a == '-' || a == '_'
|| a == '.' || a == '~'
|| a == '*')
return 1;
return 0;
}
void valdecode(char *s)
{
const char *p;
for (p = s; *p != '\0'; ++p) {
if (isallowed(*p))
*(s++) = *p;
else if (*p == '+')
*(s++) = ' ';
else if (*p == '%') {
*(s++) = percentdecode(p + 1);
p += 2;
}
else
exitwithstatus(400, "unexpected symbol in url encoded string");
}
*(s++) = '\0';
}
int urldecode(char *s, char **attr, char **value)
{
static char *t = NULL;
if (s != NULL)
t = s;
if (t == NULL || *t == '\0')
return (-1);
s = t;
while (*t != '&' && *t != '\0')
++t;
if (*t == '&')
*(t++) = '\0';
*attr = s;
s = strchr(s, '=');
*(s++) = '\0';
*value = s;
valdecode(*attr);
valdecode(*value);
return 0;
}
void fprint(FILE *f, const char *s)
{
size_t len;
len = strlen(s);
if (fwrite(s, 1, len, f) < len)
exitwithstatus(500, "");
}
void fprintnquoted(FILE *f, const char *s, size_t sz)
{
const char *b, *e;
b = s;
while ((e = memchr(b, '\'', b + sz - b)) != NULL) {
if (fwrite(b, 1, e - b, f) < e - b)
exitwithstatus(500, "");
fprintf(f, "'\"'\"'");
b = e + 1;
}
if (fwrite(b, 1, s + sz - b, f) < s + sz - b)
exitwithstatus(500, "");
}
void urlencodedforms(char *forms, FILE *fifofile)
{
char *attr, *val;
fprint(fifofile, "typeset -A formdata;\n\n");
while (urldecode(forms, &attr, &val) >= 0) {
fprint(fifofile, "formdata[");
fprint(fifofile, attr);
fprint(fifofile, "]='");
fprintnquoted(fifofile, val, strlen(val));
fprint(fifofile, "';\n");
forms = NULL;
}
}
struct partheader {
char *disptype;
char *formname;
char *filename;
char *conttype;
};
void multipartheader(struct partheader *hdr)
{
char *field;
hdr->disptype = hdr->formname = hdr->filename = hdr->conttype = NULL;
while ((field = mh_getfield()) != NULL) {
char *body;
if ((body = mh_fieldbody(field)) == NULL)
exitwithstatus(400, "cannot read body of multipart data");
if (strcasecmp(field, "Content-Disposition") == 0) {
char *name, *val;
hdr->disptype = mh_getdisptype(body);
while ((name = mh_getparam(&val)) != NULL) {
if (strcasecmp(name, "name") == 0)
hdr->formname = val;
else if (strcasecmp(name, "filename") == 0)
hdr->filename = val;
}
}
else if (strcasecmp(field, "Content-Type") == 0)
hdr->conttype = mh_getcontenttype(body);
}
}
void multipartdata(FILE *fifofile, char *boundary, size_t contlen)
{
char *s;
size_t ssz;
char *bnd;
size_t bndlen;
int filen;
int r;
fprint(fifofile, "typeset -A formdata;\ntypeset -A filename;\n\n");
bnd = xrealloc(NULL, strlen("--\r\n") + strlen(boundary) + 1);
sprintf(bnd, "--%s", boundary);
bndlen = strlen(bnd);
s = NULL;
do {
if ((r = getline(&s, &ssz, stdin)) < 0)
exitwithstatus(400, "boundary not found while reading multipart data");
} while (strncmp(s, bnd, bndlen) != 0);
sprintf(bnd, "\r\n--%s", boundary);
bndlen = strlen(bnd);
filen = 0;
while (1) {
struct partheader hdr;
char e[2];
multipartheader(&hdr);
if (hdr.formname == NULL || hdr.disptype == NULL)
exitwithstatus(400, "cannot read header of multipart data block");
// TODO can i rely on Content-Type field existance
// when checking source of data?
if (hdr.conttype == NULL) {
char cc;
char *bp;
//TODO can be ' in formname?
fprint(fifofile, "formdata[");
fprint(fifofile, hdr.formname);
fprint(fifofile, "]='");
bp = bnd;
while (*bp != '\0') {
int c;
if ((c = getc(stdin)) == EOF)
exitwithstatus(400, "unexpected EOF while reading multipart data");
else if (c == *bp)
++bp;
else {
cc = c;
fprintnquoted(fifofile, bnd, bp - bnd);
bp = bnd;
if (c == *bp) {
++bp;
continue;
}
fprintnquoted(fifofile, &cc, 1);
}
}
fprint(fifofile, "';\n");
}
else {
FILE *tmpfile;
int tmpfilefd;
char *bp;
if (tmpfilescount >= MAX_TEMP_FILES)
exitwithstatus(500, "");
sprintf(tmpfiles[tmpfilescount], "/tmp/ksh-cgi_%d%d",
(int) getpid(), filen++);
if ((tmpfilefd = open(tmpfiles[tmpfilescount],
O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU)) < 0)
exitwithstatus(500, "");
if ((tmpfile = fdopen(tmpfilefd, "w")) == NULL)
exitwithstatus(500, "");
bp = bnd;
while (*bp != '\0') {
int c;
if ((c = getc(stdin)) == EOF)
exitwithstatus(400, "unexpected EOF while reading multipart data");
else if (c == *bp)
++bp;
else {
fwrite(bnd, bp - bnd, 1, tmpfile);
bp = bnd;
if (c == *bp) {
++bp;
continue;
}
putc(c, tmpfile);
}
}
fclose(tmpfile);
fprint(fifofile, "formdata[");
fprint(fifofile, hdr.formname);
fprint(fifofile, "]='");
fprintnquoted(fifofile, tmpfiles[tmpfilescount],
strlen(tmpfiles[tmpfilescount]));
fprint(fifofile, "';\n");
if (hdr.filename != NULL) {
fprint(fifofile, "filename[");
fprint(fifofile, hdr.formname);
fprint(fifofile, "]='");
fprintnquoted(fifofile, hdr.filename,
strlen(hdr.filename));
fprint(fifofile, "';\n");
}
++tmpfilescount;
}
if (hdr.disptype != NULL)
free(hdr.disptype);
if (hdr.conttype != NULL)
free(hdr.conttype);
e[0]=getc(stdin);
while ((e[1] = getc(stdin)) != EOF
&& strncmp(e, "\r\n", 2) != 0
&& strncmp(e, "--", 2) != 0)
e[0] = e[1];
if (e[1] == EOF)
exitwithstatus(400, "unexpected EOF while reading multipart data");
if (strncmp(e, "--", 2) == 0)
break;
}
free(bnd);
}
void postmethod(FILE *fifofile)
{
char *type;
char *endptr;
size_t contlen;
if (getenv("CONTENT_TYPE") == NULL)
exitwithstatus(400, "CONTENT_TYPE field is empty in post request");
contlen = strtol(getenv("CONTENT_LENGTH"), &endptr, 10);
if (errno == ERANGE || getenv("CONTENT_LEGTH") == endptr)
exitwithstatus(400, "CONTENT_LENGTH field is out of range in post request");
type = mh_getcontenttype(getenv("CONTENT_TYPE"));
if (strcasecmp(type, "application/x-www-form-urlencoded") == 0) {
char *body, *p;
int r;
if ((body = xrealloc(NULL, contlen + 1)) == NULL)
exitwithstatus(500, "");
p = body;
while ((r = read(0, p, contlen)) != 0) {
if (r < 0)
exitwithstatus(500, "");
p += r;
}
*p = '\0';
urlencodedforms(body, fifofile);
free(body);
} else if (strcasecmp(type, "multipart/form-data") == 0) {
char *name, *val;
char *boundary;
while ((name = mh_getparam(&val)) != NULL)
if (strcasecmp(name, "boundary") == 0)
boundary = val;
multipartdata(fifofile, boundary, contlen);
}
else
exitwithstatus(501, "");
free(type);
}
int outputproc()
{
char *s;
size_t ssz;
char buf[4096];
int r;
ssz = 0;
if (getline(&s, &ssz, stdin) < 0)
exitwithstatus(500, "");
if (strcmp(s, "<!DOCTYPE html>") == 0)
if (printf("%s\r\n\r\n", "Content-Type: text/html; charset=utf-8") < 0)
exitwithstatus(500, "");
if (printf("%s", s) < 0)
exitwithstatus(500, "");
// transmit other ksh output data
while ((r = fread(buf, 1, sizeof(buf), stdin)) != 0) {
if (r < 0)
return 1;
fwrite(buf, 1, r, stdout);
}
return 0;
}
int main(int argc, char *argv[], char *envp[])
{
FILE *fifofile;
FILE *scriptfile;
int cpid;
int oprocpid;
char buf[4096];
int p[2];
int status;
int r;
// making fifo for translating script
// with initilized form values to ksh
if (sprintf(fifopath, "/tmp/ksh-cgi_%d.fifo", (int) getpid()) < 0)
exitwithstatus(500, "");
if (mkfifo(fifopath, S_IRWXU) < 0)
exitwithstatus(500, "cannot create fifo");
// ksh output is transmited through pipe to separate
// process that parsing output.
if (pipe(p) < 0)
exitwithstatus(500, "cannot open pipe");
// run ksh
if ((cpid = fork()) == 0) {
close(STDOUT_FILENO);
dup(p[1]);
close(p[0]);
close(p[1]);
argv[0] = "/bin/ksh";
argv[1] = fifopath;
execve("/bin/ksh", argv, envp);
return 1;
}
// run output parsing process
if ((oprocpid = fork()) == 0) {
close(STDIN_FILENO);
dup(p[0]);
close(p[0]);
close(p[1]);
return outputproc();
}
close(p[0]);
close(p[1]);
// write code to initilize form values before main script
// for file upload form also create temporary file
if ((fifofile = fopen(fifopath, "w")) < 0)
exitwithstatus(500, "cannot create fifo");
if (strcmp(getenv("REQUEST_METHOD"), "GET") == 0) {
// RFC 3875 says that this enviroment variable MUST be set,
// lighttpd doesn't care. Maybe there are exits another RFC.
if (getenv("QUERY_STRING") != NULL)
urlencodedforms(getenv("QUERY_STRING"), fifofile);
}
else if (strcmp(getenv("REQUEST_METHOD"), "POST") == 0)
postmethod(fifofile);
// write main script
if ((scriptfile = fopen(argv[1], "r")) < 0)
exitwithstatus(500, "cannot open script file");
while ((r = fread(buf, 1, sizeof(buf), scriptfile)) != 0) {
if (r < 0)
exitwithstatus(500, "error while reading script file");
fwrite(buf, 1, r, fifofile);
}
fclose(fifofile);
// wait for ksh process and output processing to end
wait(&status);
if (status != 0)
exitwithstatus(500, "script ended with error");
wait(&status);
if (status != 0)
exitwithstatus(500, "script ended with error");
cleanup();
return 0;
}