File: getcgi.c

package info (click to toggle)
icinga 1.14.2%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,264 kB
  • sloc: ansic: 108,564; sql: 9,656; sh: 4,945; perl: 3,439; makefile: 1,213; php: 581; xml: 104
file content (320 lines) | stat: -rw-r--r-- 9,306 bytes parent folder | download | duplicates (2)
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
/*****************************************************************************
 *
 * GETCGI.C -  Icinga CGI Input Routines
 *
 * Copyright (c) 1999-2009 Ethan Galstad (egalstad@nagios.org)
 * Copyright (c) 2009-2015 Icinga Development Team (http://www.icinga.org)
 *
 * License:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 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 "../include/config.h"
#include "../include/getcgi.h"
#include "../include/cgiutils.h"
#include <stdio.h>
#include <stdlib.h>


#undef PARANOID_CGI_INPUT


/* Remove potentially harmful characters from CGI input that we don't need or want */
void sanitize_cgi_input(char **cgivars) {
	char *strptr;
	int x, y, i;
	int keep;

	/* don't strip for now... */
	return;

	for (strptr = cgivars[i=0]; strptr != NULL; strptr = cgivars[++i]) {

		for (x = 0, y = 0; strptr[x] != '\x0'; x++) {

			keep = 1;

			/* remove potentially nasty characters */
			if (strptr[x] == ';' || strptr[x] == '|' || strptr[x] == '&' || strptr[x] == '<' || strptr[x] == '>')
				keep = 0;
#ifdef PARANOID_CGI_INPUT
			else if (strptr[x] == '/' || strptr[x] == '\\')
				keep = 0;
#endif
			if (keep == 1)
				strptr[y++] = strptr[x];
		}

		strptr[y] = '\x0';
	}

	return;
}


/* convert encoded hex string (2 characters representing an 8-bit number) to its ASCII char equivalent */
unsigned char hex_to_char(char *input) {
	unsigned char outchar = '\x0';
	unsigned int outint;
	char tempbuf[3];

	/* NULL or empty string */
	if (input == NULL)
		return '\x0';
	if (input[0] == '\x0')
		return '\x0';

	tempbuf[0] = input[0];
	tempbuf[1] = input[1];
	tempbuf[2] = '\x0';

	sscanf(tempbuf, "%X", &outint);

	/* only convert "normal" ASCII characters - we don't want the rest.  Normally you would
	   convert all characters (i.e. for allowing users to post binary files), but since we
	   aren't doing this, stay on the cautious side of things and reject outsiders... */
#ifdef PARANOID_CGI_INPUT
	if (outint < 32 || outint > 126)
		outint = 0;
#endif

	outchar = (unsigned char)outint;

	return outchar;
}



/* unescape hex characters and plus in CGI input */
void unescape_cgi_input(char *input) {
	int x, y;
	int len;

	if (input == NULL)
		return;

	len = strlen(input);
	for (x = 0, y = 0; x < len; x++, y++) {

		if (input[x] == '\x0') {
			break;

		// RB 2013-04-07
		// only allow hex conversion if '%' is follow by a valid character
		} else if (input[x] == '%' && (
			// 0 - 9
			(input[x+1] >= 48 && input[x+1] <= 57) ||
			// A - F
			(input[x+1] >= 65 && input[x+1] <= 70) ||
			// a - f
			(input[x+1] >= 97 && input[x+1] <= 102))
			) {

			input[y] = hex_to_char(&input[x+1]);
			x += 2;

		// RB 2011-09-08
		// convert plus as well that it can bu used in service and host names
		} else if (input[x] == '+') {
			input[y] = ' ';
		} else
			input[y] = input[x];
	}
	input[y] = '\x0';

	return;
}

html_request *getcgivars(void) {
	char *request_method;
	char *content_type;
	char *content_length_string;
	int content_length;
	char *cgiinput;
	char *nvpair;
	char *eqpos;
	char *temp_pair;
	html_request *new_html_request_list = NULL;
	html_request *new_request_item = NULL;
	html_request *last_request_item = NULL;

	/* initialize char variable(s) */
	cgiinput = "";

	/* depending on the request method, read all CGI input into cgiinput */

	request_method = getenv("REQUEST_METHOD");
	if (request_method == NULL)
		request_method = "";

	if (!strcmp(request_method, "GET") || !strcmp(request_method, "HEAD")) {

		/* check for NULL query string environment variable - 04/28/00 (Ludo Bosmans) */
		if (getenv("QUERY_STRING") == NULL) {
			cgiinput = (char *)malloc(1);
			if (cgiinput != NULL) {
				cgiinput[0] = '\x0';
			}
		} else {
			cgiinput = strdup(getenv("QUERY_STRING"));
			if (cgiinput == NULL) {
				printf("getcgivars(): Could not allocate memory for CGI input.\n");
				exit(1);
			}
		}
	}

	else if (!strcmp(request_method, "POST") || !strcmp(request_method, "PUT")) {

		/* if CONTENT_TYPE variable is not specified, RFC-2068 says we should assume it is "application/octet-string" */
		/* mobile (WAP) stations generate CONTENT_TYPE with charset, we we should only check first 33 chars */

		content_type = getenv("CONTENT_TYPE");
		if (content_type == NULL)
			content_type = "";

		if (strlen(content_type) && strncasecmp(content_type, "application/x-www-form-urlencoded", 33)) {
			printf("getcgivars(): Unsupported Content-Type.\n");
			exit(1);
		}

		content_length_string = getenv("CONTENT_LENGTH");
		if (content_length_string == NULL)
			content_length_string = "0";

		if (!(content_length = atoi(content_length_string))) {
			printf("getcgivars(): No Content-Length was sent with the POST request.\n") ;
			exit(1);
		}
		/* suspicious content length */
		if ((content_length < 0) || (content_length >= INT_MAX - 1)) {
			printf("getcgivars(): Suspicious Content-Length was sent with the POST request.\n");
			exit(1);
		}

		if (!(cgiinput = (char *)malloc(content_length + 1))) {
			printf("getcgivars(): Could not allocate memory for CGI input.\n");
			exit(1);
		}
		if (!fread(cgiinput, content_length, 1, stdin)) {
			printf("getcgivars(): Could not read input from STDIN.\n");
			exit(1);
		}
		cgiinput[content_length] = '\0';

	} else {

		printf("getcgivars(): Unsupported REQUEST_METHOD -> '%s'\n", request_method);
		printf("\n");
		printf("I'm guessing you're trying to execute the CGI from a command line.\n");
		printf("In order to do that, you need to set the REQUEST_METHOD environment\n");
		printf("variable to either \"GET\", \"HEAD\", or \"POST\".  When using the\n");
		printf("GET and HEAD methods, arguments can be passed to the CGI\n");
		printf("by setting the \"QUERY_STRING\" environment variable.  If you're\n");
		printf("using the POST method, data is read from standard input.  Also of\n");
		printf("note: if you've enabled authentication in the CGIs, you must set the\n");
		printf("\"REMOTE_USER\" environment variable to be the name of the user you're\n");
		printf("\"authenticated\" as.\n");
		printf("\n");

		exit(1);
	}

	nvpair = my_strtok(cgiinput, "&");
	while (nvpair) {

		temp_pair = strdup(nvpair);
		if(temp_pair == NULL) {
			printf("getcgivars(): Could not allocate memory for name-value pair element %s.\n", nvpair);
			exit(1);
		}

		/* allocating new memory */
		new_request_item = (html_request *)malloc(sizeof(html_request));
		if(new_request_item == NULL) {
			printf("getcgivars(): Could not allocate memory for new html_request element.\n");
			my_free(temp_pair);
			exit(1);
		}

		new_request_item->option = NULL;
		new_request_item->value = NULL;
		new_request_item->is_valid = FALSE;
		new_request_item->next = NULL;

		/* get value */
		if ((eqpos = strchr(temp_pair, '=')) != NULL) {
			*eqpos = '\0';
			new_request_item->value = strdup(eqpos + 1);
			if(new_request_item->value == NULL) {
				printf("getcgivars(): Could not allocate memory for cgi param value: %s=%s.\n", temp_pair,eqpos + 1);
				exit(1);
			}
			unescape_cgi_input(new_request_item->value);
			/* do some basic length checking */
			if (strlen(new_request_item->value) >= MAX_INPUT_BUFFER - 1) {
				printf("getcgivars(): length of cgi param value exceeds MAX_INPUT_BUFFER: %d.\n", MAX_INPUT_BUFFER);
				exit(1);
			}
		}

		/* get option name
		   just reuse the temp_pair pointer without allocating new memory
		*/
		new_request_item->option = temp_pair;
		unescape_cgi_input(new_request_item->option);
		if (strlen(new_request_item->option) >= MAX_INPUT_BUFFER - 1) {
			printf("getcgivars(): length of cgi param option exceeds MAX_INPUT_BUFFER: %d.\n", MAX_INPUT_BUFFER);
			exit(1);
		}

		if (new_html_request_list == NULL) {
			new_html_request_list = new_request_item;
			new_html_request_list->next = NULL;
			last_request_item = new_html_request_list;
		} else {
			last_request_item->next = new_request_item;
			last_request_item = new_request_item;
			last_request_item->next = NULL;
		}

		nvpair = my_strtok(NULL, "&");
	}

	/* free allocated memory */
	free(cgiinput);

	/* return the list of name-value strings */
	return new_html_request_list;
}

/* free() memory allocated to storing the CGI request data */
void free_html_request(html_request *html_request_list) {
	html_request *this_html_request = NULL;
	html_request *next_html_request = NULL;

	/* free memory for html request list */
	for (this_html_request = html_request_list; this_html_request != NULL; this_html_request = next_html_request) {
		next_html_request = this_html_request->next;
		my_free(this_html_request->option);
		my_free(this_html_request->value);
		my_free(this_html_request);
	}

	html_request_list = NULL;

	return;
}