1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Sascha Schumann <sascha@schumann.cx>                         |
16    +----------------------------------------------------------------------+
17  */
18 
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20 
21 #include "php.h"
22 #include "zend_smart_str.h"
23 #include "ext/standard/info.h"
24 #include "ext/standard/head.h"
25 #include "php_ini.h"
26 #include "SAPI.h"
27 
28 #define CORE_PRIVATE
29 #include "apr_strings.h"
30 #include "apr_time.h"
31 #include "ap_config.h"
32 #include "util_filter.h"
33 #include "httpd.h"
34 #include "http_config.h"
35 #include "http_request.h"
36 #include "http_core.h"
37 #include "http_protocol.h"
38 #include "http_log.h"
39 #include "http_main.h"
40 #include "util_script.h"
41 #include "http_core.h"
42 #include "ap_mpm.h"
43 #ifndef PHP_WIN32
44 #include "unixd.h"
45 #endif
46 
47 #include "php_apache.h"
48 
49 #ifdef ZTS
50 int php_apache2_info_id;
51 #else
52 php_apache2_info_struct php_apache2_info;
53 #endif
54 
55 #define SECTION(name)  PUTS("<h2>" name "</h2>\n")
56 
php_apache_lookup_uri(char * filename)57 static request_rec *php_apache_lookup_uri(char *filename)
58 {
59 	php_struct *ctx = SG(server_context);
60 
61 	if (!filename || !ctx || !ctx->r) {
62 		return NULL;
63 	}
64 
65 	return ap_sub_req_lookup_uri(filename, ctx->r, ctx->r->output_filters);
66 }
67 
68 /* {{{ proto bool virtual(string uri)
69  Perform an apache sub-request */
PHP_FUNCTION(virtual)70 PHP_FUNCTION(virtual)
71 {
72 	char *filename;
73 	size_t filename_len;
74 	request_rec *rr;
75 
76 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
77 		return;
78 	}
79 
80 	if (!(rr = php_apache_lookup_uri(filename))) {
81 		php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
82 		RETURN_FALSE;
83 	}
84 
85 	if (rr->status != HTTP_OK) {
86 		php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
87 		ap_destroy_sub_req(rr);
88 		RETURN_FALSE;
89 	}
90 
91 	/* Flush everything. */
92 	php_output_end_all();
93 	php_header();
94 
95 	/* Ensure that the ap_r* layer for the main request is flushed, to
96 	 * work around http://issues.apache.org/bugzilla/show_bug.cgi?id=17629 */
97 	ap_rflush(rr->main);
98 
99 	if (ap_run_sub_req(rr)) {
100 		php_error_docref(NULL, E_WARNING, "Unable to include '%s' - request execution failed", filename);
101 		ap_destroy_sub_req(rr);
102 		RETURN_FALSE;
103 	}
104 	ap_destroy_sub_req(rr);
105 	RETURN_TRUE;
106 }
107 /* }}} */
108 
109 #define ADD_LONG(name) \
110 		add_property_long(return_value, #name, rr->name)
111 #define ADD_TIME(name) \
112 		add_property_long(return_value, #name, apr_time_sec(rr->name));
113 #define ADD_STRING(name) \
114 		if (rr->name) add_property_string(return_value, #name, (char *) rr->name)
115 
PHP_FUNCTION(apache_lookup_uri)116 PHP_FUNCTION(apache_lookup_uri)
117 {
118 	request_rec *rr;
119 	char *filename;
120 	size_t filename_len;
121 
122 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
123 		return;
124 	}
125 
126 	if (!(rr = php_apache_lookup_uri(filename))) {
127 		php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
128 		RETURN_FALSE;
129 	}
130 
131 	if (rr->status == HTTP_OK) {
132 		object_init(return_value);
133 
134 		ADD_LONG(status);
135 		ADD_STRING(the_request);
136 		ADD_STRING(status_line);
137 		ADD_STRING(method);
138 		ADD_TIME(mtime);
139 		ADD_LONG(clength);
140 #if MODULE_MAGIC_NUMBER < 20020506
141 		ADD_STRING(boundary);
142 #endif
143 		ADD_STRING(range);
144 		ADD_LONG(chunked);
145 		ADD_STRING(content_type);
146 		ADD_STRING(handler);
147 		ADD_LONG(no_cache);
148 		ADD_LONG(no_local_copy);
149 		ADD_STRING(unparsed_uri);
150 		ADD_STRING(uri);
151 		ADD_STRING(filename);
152 		ADD_STRING(path_info);
153 		ADD_STRING(args);
154 		ADD_LONG(allowed);
155 		ADD_LONG(sent_bodyct);
156 		ADD_LONG(bytes_sent);
157 		ADD_LONG(mtime);
158 		ADD_TIME(request_time);
159 
160 		ap_destroy_sub_req(rr);
161 		return;
162 	}
163 
164 	php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
165 	ap_destroy_sub_req(rr);
166 	RETURN_FALSE;
167 }
168 
169 /* {{{ proto array getallheaders(void)
170    Fetch all HTTP request headers */
PHP_FUNCTION(apache_request_headers)171 PHP_FUNCTION(apache_request_headers)
172 {
173 	php_struct *ctx;
174 	const apr_array_header_t *arr;
175 	char *key, *val;
176 
177 	if (zend_parse_parameters_none() == FAILURE) {
178 		return;
179 	}
180 
181 	array_init(return_value);
182 
183 	ctx = SG(server_context);
184 	arr = apr_table_elts(ctx->r->headers_in);
185 
186 	APR_ARRAY_FOREACH_OPEN(arr, key, val)
187 		if (!val) val = "";
188 		add_assoc_string(return_value, key, val);
189 	APR_ARRAY_FOREACH_CLOSE()
190 }
191 /* }}} */
192 
193 /* {{{ proto array apache_response_headers(void)
194    Fetch all HTTP response headers */
PHP_FUNCTION(apache_response_headers)195 PHP_FUNCTION(apache_response_headers)
196 {
197 	php_struct *ctx;
198 	const apr_array_header_t *arr;
199 	char *key, *val;
200 
201 	if (zend_parse_parameters_none() == FAILURE) {
202 		return;
203 	}
204 
205 	array_init(return_value);
206 
207 	ctx = SG(server_context);
208 	arr = apr_table_elts(ctx->r->headers_out);
209 
210 	APR_ARRAY_FOREACH_OPEN(arr, key, val)
211 		if (!val) val = "";
212 		add_assoc_string(return_value, key, val);
213 	APR_ARRAY_FOREACH_CLOSE()
214 }
215 /* }}} */
216 
217 /* {{{ proto string apache_note(string note_name [, string note_value])
218    Get and set Apache request notes */
PHP_FUNCTION(apache_note)219 PHP_FUNCTION(apache_note)
220 {
221 	php_struct *ctx;
222 	char *note_name, *note_val = NULL;
223 	size_t note_name_len, note_val_len;
224 	char *old_note_val=NULL;
225 
226 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &note_name, &note_name_len, &note_val, &note_val_len) == FAILURE) {
227 		return;
228 	}
229 
230 	ctx = SG(server_context);
231 
232 	old_note_val = (char *) apr_table_get(ctx->r->notes, note_name);
233 
234 	if (note_val) {
235 		apr_table_set(ctx->r->notes, note_name, note_val);
236 	}
237 
238 	if (old_note_val) {
239 		RETURN_STRING(old_note_val);
240 	}
241 
242 	RETURN_FALSE;
243 }
244 /* }}} */
245 
246 
247 /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
248    Set an Apache subprocess_env variable */
249 /*
250  * XXX this doesn't look right. shouldn't it be the parent ?*/
PHP_FUNCTION(apache_setenv)251 PHP_FUNCTION(apache_setenv)
252 {
253 	php_struct *ctx;
254 	char *variable=NULL, *string_val=NULL;
255 	size_t variable_len, string_val_len;
256 	zend_bool walk_to_top = 0;
257 	int arg_count = ZEND_NUM_ARGS();
258 	request_rec *r;
259 
260 	if (zend_parse_parameters(arg_count, "ss|b", &variable, &variable_len, &string_val, &string_val_len, &walk_to_top) == FAILURE) {
261 		return;
262 	}
263 
264 	ctx = SG(server_context);
265 
266 	r = ctx->r;
267 	if (arg_count == 3) {
268 		if (walk_to_top) {
269 			while(r->prev) {
270 				r = r->prev;
271 			}
272 		}
273 	}
274 
275 	apr_table_set(r->subprocess_env, variable, string_val);
276 
277 	RETURN_TRUE;
278 }
279 /* }}} */
280 
281 /* {{{ proto bool apache_getenv(string variable [, bool walk_to_top])
282    Get an Apache subprocess_env variable */
283 /*
284  * XXX: shouldn't this be the parent not the 'prev'
285  */
PHP_FUNCTION(apache_getenv)286 PHP_FUNCTION(apache_getenv)
287 {
288 	php_struct *ctx;
289 	char *variable;
290 	size_t variable_len;
291 	zend_bool walk_to_top = 0;
292 	int arg_count = ZEND_NUM_ARGS();
293 	char *env_val=NULL;
294 	request_rec *r;
295 
296 	if (zend_parse_parameters(arg_count, "s|b", &variable, &variable_len, &walk_to_top) == FAILURE) {
297 		return;
298 	}
299 
300 	ctx = SG(server_context);
301 
302 	r = ctx->r;
303 	if (arg_count == 2) {
304 		if (walk_to_top) {
305 			while(r->prev) {
306 				r = r->prev;
307 			}
308 		}
309 	}
310 
311 	env_val = (char*) apr_table_get(r->subprocess_env, variable);
312 
313 	if (env_val != NULL) {
314 		RETURN_STRING(env_val);
315 	}
316 
317 	RETURN_FALSE;
318 }
319 /* }}} */
320 
php_apache_get_version()321 static char *php_apache_get_version()
322 {
323 #if MODULE_MAGIC_NUMBER_MAJOR >= 20060905
324 	return (char *) ap_get_server_banner();
325 #else
326 	return (char *) ap_get_server_version();
327 #endif
328 }
329 
330 /* {{{ proto string apache_get_version(void)
331    Fetch Apache version */
PHP_FUNCTION(apache_get_version)332 PHP_FUNCTION(apache_get_version)
333 {
334 	char *apv = php_apache_get_version();
335 
336 	if (apv && *apv) {
337 		RETURN_STRING(apv);
338 	} else {
339 		RETURN_FALSE;
340 	}
341 }
342 /* }}} */
343 
344 /* {{{ proto array apache_get_modules(void)
345    Get a list of loaded Apache modules */
PHP_FUNCTION(apache_get_modules)346 PHP_FUNCTION(apache_get_modules)
347 {
348 	int n;
349 	char *p;
350 
351 	array_init(return_value);
352 
353 	for (n = 0; ap_loaded_modules[n]; ++n) {
354 		char *s = (char *) ap_loaded_modules[n]->name;
355 		if ((p = strchr(s, '.'))) {
356 			add_next_index_stringl(return_value, s, (p - s));
357 		} else {
358 			add_next_index_string(return_value, s);
359 		}
360 	}
361 }
362 /* }}} */
363 
PHP_MINFO_FUNCTION(apache)364 PHP_MINFO_FUNCTION(apache)
365 {
366 	char *apv = php_apache_get_version();
367 	smart_str tmp1 = {0};
368 	char tmp[1024];
369 	int n, max_requests;
370 	char *p;
371 	server_rec *serv = ((php_struct *) SG(server_context))->r->server;
372 #ifndef PHP_WIN32
373 # if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
374 	AP_DECLARE_DATA extern unixd_config_rec ap_unixd_config;
375 # else
376 	AP_DECLARE_DATA extern unixd_config_rec unixd_config;
377 # endif
378 #endif
379 
380 	for (n = 0; ap_loaded_modules[n]; ++n) {
381 		char *s = (char *) ap_loaded_modules[n]->name;
382 		if ((p = strchr(s, '.'))) {
383 			smart_str_appendl(&tmp1, s, (p - s));
384 		} else {
385 			smart_str_appends(&tmp1, s);
386 		}
387 		smart_str_appendc(&tmp1, ' ');
388 	}
389 	if (tmp1.s) {
390 		if (tmp1.s->len > 0) {
391 			tmp1.s->val[tmp1.s->len - 1] = '\0';
392 		} else {
393 			tmp1.s->val[0] = '\0';
394 		}
395 	}
396 
397 	php_info_print_table_start();
398 	if (apv && *apv) {
399 		php_info_print_table_row(2, "Apache Version", apv);
400 	}
401 	snprintf(tmp, sizeof(tmp), "%d", MODULE_MAGIC_NUMBER);
402 	php_info_print_table_row(2, "Apache API Version", tmp);
403 
404 	if (serv->server_admin && *(serv->server_admin)) {
405 		php_info_print_table_row(2, "Server Administrator", serv->server_admin);
406 	}
407 
408 	snprintf(tmp, sizeof(tmp), "%s:%u", serv->server_hostname, serv->port);
409 	php_info_print_table_row(2, "Hostname:Port", tmp);
410 
411 #ifndef PHP_WIN32
412 #if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
413 	snprintf(tmp, sizeof(tmp), "%s(%d)/%d", ap_unixd_config.user_name, ap_unixd_config.user_id, ap_unixd_config.group_id);
414 #else
415 	snprintf(tmp, sizeof(tmp), "%s(%d)/%d", unixd_config.user_name, unixd_config.user_id, unixd_config.group_id);
416 #endif
417 	php_info_print_table_row(2, "User/Group", tmp);
418 #endif
419 
420 	ap_mpm_query(AP_MPMQ_MAX_REQUESTS_DAEMON, &max_requests);
421 	snprintf(tmp, sizeof(tmp), "Per Child: %d - Keep Alive: %s - Max Per Connection: %d", max_requests, (serv->keep_alive ? "on":"off"), serv->keep_alive_max);
422 	php_info_print_table_row(2, "Max Requests", tmp);
423 
424 	apr_snprintf(tmp, sizeof tmp,
425 				 "Connection: %" APR_TIME_T_FMT " - Keep-Alive: %" APR_TIME_T_FMT,
426 				 apr_time_sec(serv->timeout), apr_time_sec(serv->keep_alive_timeout));
427 	php_info_print_table_row(2, "Timeouts", tmp);
428 
429 	php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
430 	php_info_print_table_row(2, "Server Root", ap_server_root);
431 	php_info_print_table_row(2, "Loaded Modules", tmp1.s->val);
432 
433 	smart_str_free(&tmp1);
434 	php_info_print_table_end();
435 
436 	DISPLAY_INI_ENTRIES();
437 
438 	{
439 		const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
440 		char *key, *val;
441 
442 		SECTION("Apache Environment");
443 		php_info_print_table_start();
444 		php_info_print_table_header(2, "Variable", "Value");
445 		APR_ARRAY_FOREACH_OPEN(arr, key, val)
446 			if (!val) {
447 				val = "";
448 			}
449 			php_info_print_table_row(2, key, val);
450 		APR_ARRAY_FOREACH_CLOSE()
451 
452 		php_info_print_table_end();
453 
454 		SECTION("HTTP Headers Information");
455 		php_info_print_table_start();
456 		php_info_print_table_colspan_header(2, "HTTP Request Headers");
457 		php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
458 
459 		arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
460 		APR_ARRAY_FOREACH_OPEN(arr, key, val)
461 			if (!val) {
462 				val = "";
463 			}
464 		        php_info_print_table_row(2, key, val);
465 		APR_ARRAY_FOREACH_CLOSE()
466 
467 		php_info_print_table_colspan_header(2, "HTTP Response Headers");
468 		arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
469 		APR_ARRAY_FOREACH_OPEN(arr, key, val)
470 			if (!val) {
471 				val = "";
472 			}
473 		        php_info_print_table_row(2, key, val);
474 		APR_ARRAY_FOREACH_CLOSE()
475 
476 		php_info_print_table_end();
477 	}
478 }
479 
480 /* {{{ arginfo */
481 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_lookup_uri, 0, 0, 1)
482 	ZEND_ARG_INFO(0, filename)
483 ZEND_END_ARG_INFO()
484 
485 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_virtual, 0, 0, 1)
486 	ZEND_ARG_INFO(0, uri)
487 ZEND_END_ARG_INFO()
488 
489 ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_response_headers, 0)
490 ZEND_END_ARG_INFO()
491 
492 ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_getallheaders, 0)
493 ZEND_END_ARG_INFO()
494 
495 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_note, 0, 0, 1)
496 	ZEND_ARG_INFO(0, note_name)
497 	ZEND_ARG_INFO(0, note_value)
498 ZEND_END_ARG_INFO()
499 
500 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_setenv, 0, 0, 2)
501 	ZEND_ARG_INFO(0, variable)
502 	ZEND_ARG_INFO(0, value)
503 	ZEND_ARG_INFO(0, walk_to_top)
504 ZEND_END_ARG_INFO()
505 
506 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_getenv, 0, 0, 1)
507 	ZEND_ARG_INFO(0, variable)
508 	ZEND_ARG_INFO(0, walk_to_top)
509 ZEND_END_ARG_INFO()
510 
511 ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_get_version, 0)
512 ZEND_END_ARG_INFO()
513 
514 ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_get_modules, 0)
515 ZEND_END_ARG_INFO()
516 /* }}} */
517 
518 static const zend_function_entry apache_functions[] = {
519 	PHP_FE(apache_lookup_uri, 		arginfo_apache2handler_lookup_uri)
520 	PHP_FE(virtual, 				arginfo_apache2handler_virtual)
521 	PHP_FE(apache_request_headers, 	arginfo_apache2handler_getallheaders)
522 	PHP_FE(apache_response_headers, arginfo_apache2handler_response_headers)
523 	PHP_FE(apache_setenv, 		arginfo_apache2handler_setenv)
524 	PHP_FE(apache_getenv, 		arginfo_apache2handler_getenv)
525 	PHP_FE(apache_note, 		arginfo_apache2handler_note)
526 	PHP_FE(apache_get_version, 	arginfo_apache2handler_get_version)
527 	PHP_FE(apache_get_modules, 	arginfo_apache2handler_get_modules)
528 	PHP_FALIAS(getallheaders, 	apache_request_headers, arginfo_apache2handler_getallheaders)
529 	{NULL, NULL, NULL}
530 };
531 
532 PHP_INI_BEGIN()
533 	STD_PHP_INI_ENTRY("xbithack",		"0",	PHP_INI_ALL,	OnUpdateBool,	xbithack,	php_apache2_info_struct, php_apache2_info)
534 	STD_PHP_INI_ENTRY("engine",		"1",	PHP_INI_ALL,	OnUpdateBool,	engine, 	php_apache2_info_struct, php_apache2_info)
535 	STD_PHP_INI_ENTRY("last_modified",	"0",	PHP_INI_ALL,	OnUpdateBool,	last_modified,	php_apache2_info_struct, php_apache2_info)
PHP_INI_END()536 PHP_INI_END()
537 
538 static PHP_MINIT_FUNCTION(apache)
539 {
540 #ifdef ZTS
541 	ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
542 #endif
543 	REGISTER_INI_ENTRIES();
544 	return SUCCESS;
545 }
546 
PHP_MSHUTDOWN_FUNCTION(apache)547 static PHP_MSHUTDOWN_FUNCTION(apache)
548 {
549 	UNREGISTER_INI_ENTRIES();
550 	return SUCCESS;
551 }
552 
553 zend_module_entry php_apache_module = {
554 	STANDARD_MODULE_HEADER,
555 	"apache2handler",
556 	apache_functions,
557 	PHP_MINIT(apache),
558 	PHP_MSHUTDOWN(apache),
559 	NULL,
560 	NULL,
561 	PHP_MINFO(apache),
562 	NULL,
563 	STANDARD_MODULE_PROPERTIES
564 };
565 
566 /*
567  * Local variables:
568  * tab-width: 4
569  * c-basic-offset: 4
570  * End:
571  * vim600: sw=4 ts=4 fdm=marker
572  * vim<600: sw=4 ts=4
573  */
574