summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_rinit_hook.c
blob: c62b272a16e24ae9c3182105892d88a9cd4357ac (plain)
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
/*
   +----------------------------------------------------------------------+
   | PHP Version 7                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2017 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Bob Weinand <bwoebi@php.net>                                |
   +----------------------------------------------------------------------+
*/

#include "phpdbg_rinit_hook.h"
#include "php_ini.h"
#include <errno.h>

ZEND_DECLARE_MODULE_GLOBALS(phpdbg_webhelper);

PHP_INI_BEGIN()
	STD_PHP_INI_ENTRY("phpdbg.auth", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, auth, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
	STD_PHP_INI_ENTRY("phpdbg.path", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, path, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
PHP_INI_END()

static inline void php_phpdbg_webhelper_globals_ctor(zend_phpdbg_webhelper_globals *pg) /* {{{ */
{
} /* }}} */

static PHP_MINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
{
	if (!strcmp(sapi_module.name, PHPDBG_NAME)) {
		return SUCCESS;
	}

	ZEND_INIT_MODULE_GLOBALS(phpdbg_webhelper, php_phpdbg_webhelper_globals_ctor, NULL);
	REGISTER_INI_ENTRIES();

	return SUCCESS;
} /* }}} */

static PHP_RINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
{
	zval cookies = PG(http_globals)[TRACK_VARS_COOKIE];
	zval *auth;

	if (Z_TYPE(cookies) == IS_ARRAY || (auth = zend_hash_str_find(Z_ARRVAL(cookies), PHPDBG_NAME "_AUTH_COOKIE", sizeof(PHPDBG_NAME "_AUTH_COOKIE"))) || Z_STRLEN_P(auth) != strlen(PHPDBG_WG(auth)) || strcmp(Z_STRVAL_P(auth), PHPDBG_WG(auth))) {
		return SUCCESS;
	}

#ifndef _WIN32
	{
		struct sockaddr_un sock;
		int s = socket(AF_UNIX, SOCK_STREAM, 0);
		size_t len = strlen(PHPDBG_WG(path)) + sizeof(sock.sun_family);
		char buf[(1 << 8) + 1];
		ssize_t buflen;
		sock.sun_family = AF_UNIX;
		strcpy(sock.sun_path, PHPDBG_WG(path));

		if (connect(s, (struct sockaddr *)&sock, len) == -1) {
			zend_error(E_ERROR, "Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting. Reason: %s", PHPDBG_WG(path), strerror(errno));
		}

		char *msg = NULL;
		size_t msglen = 0;
		phpdbg_webdata_compress(&msg, &msglen);

		buf[0] = (msglen >>  0) & 0xff;
		buf[1] = (msglen >>  8) & 0xff;
		buf[2] = (msglen >> 16) & 0xff;
		buf[3] = (msglen >> 24) & 0xff;
		send(s, buf, 4, 0);
		send(s, msg, msglen, 0);

		while ((buflen = recv(s, buf, sizeof(buf) - 1, 0)) > 0) {
			php_write(buf, buflen);
		}

		close(s);

		php_output_flush_all();
		zend_bailout();
	}
#endif

	return SUCCESS;
} /* }}} */

zend_module_entry phpdbg_webhelper_module_entry = {
	STANDARD_MODULE_HEADER,
	"phpdbg_webhelper",
	NULL,
	PHP_MINIT(phpdbg_webhelper),
	NULL,
	PHP_RINIT(phpdbg_webhelper),
	NULL,
	NULL,
	PHPDBG_VERSION,
	STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_PHPDBG_WEBHELPER
ZEND_GET_MODULE(phpdbg_webhelper)
#endif