forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgiutil.c
494 lines (423 loc) · 14.8 KB
/
cgiutil.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: cgiRequestObj and CGI parameter parsing.
* Author: Steve Lime and the MapServer team.
*
* Notes: Portions derived from NCSA HTTPd Server's example CGI programs (util.c).
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mapserver.h"
#include "cgiutil.h"
#include "cpl_conv.h"
#define LF 10
#define CR 13
int readPostBody( cgiRequestObj *request, char **data )
{
size_t data_max, data_len;
int chunk_size;
(void)request;
msIO_needBinaryStdin();
/* -------------------------------------------------------------------- */
/* If the length is provided, read in one gulp. */
/* -------------------------------------------------------------------- */
if( getenv("CONTENT_LENGTH") != NULL ) {
data_max = (size_t) atoi(getenv("CONTENT_LENGTH"));
/* Test for suspicious CONTENT_LENGTH (negative value or SIZE_MAX) */
if( data_max >= SIZE_MAX ) {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("Suspicious Content-Length.\n");
msSetError(MS_WEBERR, "Suspicious Content-Length.", "readPostBody()");
return MS_FAILURE;
}
*data = (char *) malloc(data_max+1);
if( *data == NULL ) {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("malloc() failed, Content-Length: %u unreasonably large?\n", (unsigned int)data_max );
msSetError(MS_WEBERR, "malloc() failed, Content-Length: %u unreasonably large?", "readPostBody()", (unsigned int)data_max);
return MS_FAILURE;
}
if( (int) msIO_fread(*data, 1, data_max, stdin) < (int) data_max ) {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("POST body is short\n");
msSetError(MS_WEBERR, "POST body is short.", "readPostBody()");
return MS_FAILURE;
}
(*data)[data_max] = '\0';
return MS_SUCCESS;
}
/* -------------------------------------------------------------------- */
/* Otherwise read in chunks to the end. */
/* -------------------------------------------------------------------- */
#define DATA_ALLOC_SIZE 10000
data_max = DATA_ALLOC_SIZE;
data_len = 0;
*data = (char *) msSmallMalloc(data_max+1);
(*data)[data_max] = '\0';
while( (chunk_size = msIO_fread( *data + data_len, 1, data_max-data_len, stdin )) > 0 ) {
data_len += chunk_size;
if( data_len == data_max ) {
/* Realloc buffer, making sure we check for possible size_t overflow */
if ( data_max > SIZE_MAX - (DATA_ALLOC_SIZE+1) ) {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("Possible size_t overflow, cannot reallocate input buffer, POST body too large?\n" );
msSetError(MS_WEBERR, "Possible size_t overflow, cannot reallocate input buffer, POST body too large?", "readPostBody()");
return MS_FAILURE;
}
data_max = data_max + DATA_ALLOC_SIZE;
*data = (char *) msSmallRealloc(*data, data_max+1);
}
}
(*data)[data_len] = '\0';
return MS_SUCCESS;
}
static char* msGetEnv(const char *name, void* thread_context)
{
(void)thread_context;
return getenv(name);
}
int loadParams(cgiRequestObj *request,
char* (*getenv2)(const char*, void* thread_context),
char *raw_post_data,
ms_uint32 raw_post_data_length,
void* thread_context)
{
register int x,m=0;
char *s, *queryString = NULL, *httpCookie = NULL;
int debuglevel;
int maxParams = MS_DEFAULT_CGI_PARAMS;
if (getenv2==NULL)
getenv2 = &msGetEnv;
if(getenv2("REQUEST_METHOD", thread_context)==NULL) {
msIO_printf("This script can only be used to decode form results and \n");
msIO_printf("should be initiated as a CGI process via a httpd server.\n");
msIO_printf("For other options please try using the --help switch.\n");
return -1;
}
debuglevel = (int)msGetGlobalDebugLevel();
if(strcmp(getenv2("REQUEST_METHOD", thread_context),"POST") == 0 && CPLGetConfigOption("MS_NO_POST", NULL) == NULL) { /* we've got a post from a form */
char *post_data;
int data_len;
request->type = MS_POST_REQUEST;
if (request->contenttype == NULL){
s = getenv2("CONTENT_TYPE", thread_context);
if (s != NULL){
request->contenttype = msStrdup(s);
}
else {
/* we've to set default Content-Type which is
* application/octet-stream according to
* W3 RFC 2626 section 7.2.1 */
request->contenttype = msStrdup("application/octet-stream");
}
}
if (raw_post_data) {
post_data = msStrdup(raw_post_data);
data_len = raw_post_data_length;
} else {
if(MS_SUCCESS != readPostBody( request, &post_data ))
return -1;
data_len = strlen(post_data);
}
/* if the content_type is application/x-www-form-urlencoded,
we have to parse it like the QUERY_STRING variable */
if(strncmp(request->contenttype, "application/x-www-form-urlencoded", strlen("application/x-www-form-urlencoded")) == 0) {
while( data_len > 0 && isspace(post_data[data_len-1]) )
post_data[--data_len] = '\0';
while( post_data[0] ) {
if(m >= maxParams) {
maxParams *= 2;
request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams);
request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams);
}
request->ParamValues[m] = makeword(post_data,'&');
plustospace(request->ParamValues[m]);
unescape_url(request->ParamValues[m]);
request->ParamNames[m] = makeword(request->ParamValues[m],'=');
m++;
}
free( post_data );
} else
request->postrequest = post_data;
/* check the QUERY_STRING even in the post request since it can contain
information. Eg a wfs request with */
s = getenv2("QUERY_STRING", thread_context);
if(s) {
if (debuglevel >= MS_DEBUGLEVEL_DEBUG)
msDebug("loadParams() QUERY_STRING: %s\n", s);
queryString = msStrdup(s);
for(x=0; queryString[0] != '\0'; x++) {
if(m >= maxParams) {
maxParams *= 2;
request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams);
request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams);
}
request->ParamValues[m] = makeword(queryString,'&');
plustospace(request->ParamValues[m]);
unescape_url(request->ParamValues[m]);
request->ParamNames[m] = makeword(request->ParamValues[m],'=');
m++;
}
}
} else {
if(strcmp(getenv2("REQUEST_METHOD", thread_context),"GET") == 0) { /* we've got a get request */
request->type = MS_GET_REQUEST;
s = getenv2("QUERY_STRING", thread_context);
if(s == NULL) {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("No query information to decode. QUERY_STRING not set.\n");
msSetError(MS_WEBERR, "No query information to decode. QUERY_STRING not set.", "loadParams()");
return -1;
}
if (debuglevel >= MS_DEBUGLEVEL_DEBUG)
msDebug("loadParams() QUERY_STRING: %s\n", s);
if(strlen(s)==0) {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("No query information to decode. QUERY_STRING is set, but empty.\n");
msSetError(MS_WEBERR, "No query information to decode. QUERY_STRING is set, but empty.", "loadParams()");
return -1;
}
/* don't modify the string returned by getenv2 */
queryString = msStrdup(s);
for(x=0; queryString[0] != '\0'; x++) {
if(m >= maxParams) {
maxParams *= 2;
request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams);
request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams);
}
request->ParamValues[m] = makeword(queryString,'&');
plustospace(request->ParamValues[m]);
unescape_url(request->ParamValues[m]);
request->ParamNames[m] = makeword(request->ParamValues[m],'=');
m++;
}
} else {
// msIO_setHeader("Content-Type","text/html");
// msIO_sendHeaders();
// msIO_printf("This script should be referenced with a METHOD of GET or METHOD of POST.\n");
msSetError(MS_WEBERR, "This script should be referenced with a METHOD of GET or METHOD of POST.", "loadParams()");
return -1;
}
}
/* check for any available cookies */
s = getenv2("HTTP_COOKIE", thread_context);
if(s != NULL) {
httpCookie = msStrdup(s);
request->httpcookiedata = msStrdup(s);
for(x=0; httpCookie[0] != '\0'; x++) {
if(m >= maxParams) {
maxParams *= 2;
request->ParamNames = (char **) msSmallRealloc(request->ParamNames,sizeof(char *) * maxParams);
request->ParamValues = (char **) msSmallRealloc(request->ParamValues,sizeof(char *) * maxParams);
}
request->ParamValues[m] = makeword(httpCookie,';');
plustospace(request->ParamValues[m]);
unescape_url(request->ParamValues[m]);
request->ParamNames[m] = makeword_skip(request->ParamValues[m],'=',' ');
m++;
}
}
if (queryString)
free(queryString);
if (httpCookie)
free(httpCookie);
return(m);
}
void getword(char *word, char *line, char stop)
{
int x = 0,y;
for(x=0; ((line[x]) && (line[x] != stop)); x++)
word[x] = line[x];
word[x] = '\0';
if(line[x]) ++x;
y=0;
while((line[y++] = line[x++]));
}
char *makeword_skip(char *line, char stop, char skip)
{
int x = 0,y,offset=0;
char *word = (char *) msSmallMalloc(sizeof(char) * (strlen(line) + 1));
for(x=0; ((line[x]) && (line[x] == skip)); x++);
offset = x;
for(x=offset; ((line[x]) && (line[x] != stop)); x++)
word[x-offset] = line[x];
word[x-offset] = '\0';
if(line[x]) ++x;
y=0;
while((line[y++] = line[x++]));
return word;
}
char *makeword(char *line, char stop)
{
int x = 0,y;
char *word = (char *) msSmallMalloc(sizeof(char) * (strlen(line) + 1));
for(x=0; ((line[x]) && (line[x] != stop)); x++)
word[x] = line[x];
word[x] = '\0';
if(line[x]) ++x;
y=0;
while((line[y++] = line[x++]));
return word;
}
char *fmakeword(FILE *f, char stop, int *cl)
{
int wsize;
char *word;
int ll;
wsize = 102400;
ll=0;
word = (char *) msSmallMalloc(sizeof(char) * (wsize + 1));
while(1) {
word[ll] = (char)fgetc(f);
if(ll==wsize) {
word[ll+1] = '\0';
wsize+=102400;
word = (char *)msSmallRealloc(word,sizeof(char)*(wsize+1));
}
--(*cl);
if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
if(word[ll] != stop) ll++;
word[ll] = '\0';
word = (char *) msSmallRealloc(word, ll+1);
return word;
}
++ll;
}
}
char x2c(char *what)
{
register char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
void unescape_url(char *url)
{
register int x,y;
for(x=0,y=0; url[y]; ++x,++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
void plustospace(char *str)
{
register int x;
for(x=0; str[x]; x++) if(str[x] == '+') str[x] = ' ';
}
int rind(char *s, char c)
{
register int x;
for(x=strlen(s) - 1; x != -1; x--)
if(s[x] == c) return x;
return -1;
}
void send_fd(FILE *f, FILE *fd)
{
int c;
while (1) {
c = fgetc(f);
if(c == EOF)
return;
fputc((char)c,fd);
}
}
int ind(char *s, char c)
{
register int x;
for(x=0; s[x]; x++)
if(s[x] == c) return x;
return -1;
}
/*
** patched version according to CERT advisory...
*/
void escape_shell_cmd(char *cmd)
{
register int x,y,l;
l=strlen(cmd);
for(x=0; cmd[x]; x++) {
if(ind("&;`'\"|*?~<>^()[]{}$\\\n",cmd[x]) != -1) {
for(y=l+1; y>x; y--)
cmd[y] = cmd[y-1];
l++; /* length has been increased */
cmd[x] = '\\';
x++; /* skip the character */
}
}
}
/*
** Allocate a new request holder structure
*/
cgiRequestObj *msAllocCgiObj()
{
cgiRequestObj *request = (cgiRequestObj *)malloc(sizeof(cgiRequestObj));
if(!request)
return NULL;
request->ParamNames = (char **) msSmallMalloc(MS_DEFAULT_CGI_PARAMS*sizeof(char*));
request->ParamValues = (char **) msSmallMalloc(MS_DEFAULT_CGI_PARAMS*sizeof(char*));
request->NumParams = 0;
request->type = MS_GET_REQUEST;
request->contenttype = NULL;
request->postrequest = NULL;
request->httpcookiedata = NULL;
request->path_info = NULL;
request->api_path = NULL;
request->api_path_length = 0;
return request;
}
void msFreeCgiObj(cgiRequestObj *request)
{
msFreeCharArray(request->ParamNames, request->NumParams);
msFreeCharArray(request->ParamValues, request->NumParams);
request->ParamNames = NULL;
request->ParamValues = NULL;
request->NumParams = 0;
request->type = -1;
msFree(request->contenttype);
msFree(request->postrequest);
msFree(request->httpcookiedata);
request->contenttype = NULL;
request->postrequest = NULL;
request->httpcookiedata = NULL;
if(request->api_path) {
msFreeCharArray(request->api_path, request->api_path_length);
request->api_path = NULL;
request->api_path_length = 0;
}
msFree(request);
}