xref: /PHP-8.0/sapi/phpdbg/phpdbg_rinit_hook.c (revision 8ddaf13e)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Bob Weinand <bwoebi@php.net>                                |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "phpdbg_rinit_hook.h"
18 #include "php_ini.h"
19 #include <errno.h>
20 
21 ZEND_DECLARE_MODULE_GLOBALS(phpdbg_webhelper)
22 
PHP_INI_BEGIN()23 PHP_INI_BEGIN()
24 	STD_PHP_INI_ENTRY("phpdbg.auth", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, auth, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
25 	STD_PHP_INI_ENTRY("phpdbg.path", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, path, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
26 PHP_INI_END()
27 
28 static inline void php_phpdbg_webhelper_globals_ctor(zend_phpdbg_webhelper_globals *pg) /* {{{ */
29 {
30 } /* }}} */
31 
PHP_MINIT_FUNCTION(phpdbg_webhelper)32 static PHP_MINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
33 {
34 	if (!strcmp(sapi_module.name, PHPDBG_NAME)) {
35 		return SUCCESS;
36 	}
37 
38 	ZEND_INIT_MODULE_GLOBALS(phpdbg_webhelper, php_phpdbg_webhelper_globals_ctor, NULL);
39 	REGISTER_INI_ENTRIES();
40 
41 	return SUCCESS;
42 } /* }}} */
43 
PHP_RINIT_FUNCTION(phpdbg_webhelper)44 static PHP_RINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
45 {
46 	zval cookies = PG(http_globals)[TRACK_VARS_COOKIE];
47 	zval *auth;
48 
49 	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))) {
50 		return SUCCESS;
51 	}
52 
53 #ifndef _WIN32
54 	{
55 		struct sockaddr_un sock;
56 		int s = socket(AF_UNIX, SOCK_STREAM, 0);
57 		size_t len = strlen(PHPDBG_WG(path)) + sizeof(sock.sun_family);
58 		char buf[(1 << 8) + 1];
59 		ssize_t buflen;
60 		sock.sun_family = AF_UNIX;
61 		strcpy(sock.sun_path, PHPDBG_WG(path));
62 
63 		if (connect(s, (struct sockaddr *)&sock, len) == -1) {
64 			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));
65 		}
66 
67 		char *msg = NULL;
68 		size_t msglen = 0;
69 		phpdbg_webdata_compress(&msg, &msglen);
70 
71 		buf[0] = (msglen >>  0) & 0xff;
72 		buf[1] = (msglen >>  8) & 0xff;
73 		buf[2] = (msglen >> 16) & 0xff;
74 		buf[3] = (msglen >> 24) & 0xff;
75 		send(s, buf, 4, 0);
76 		send(s, msg, msglen, 0);
77 
78 		while ((buflen = recv(s, buf, sizeof(buf) - 1, 0)) > 0) {
79 			php_write(buf, buflen);
80 		}
81 
82 		close(s);
83 
84 		php_output_flush_all();
85 		zend_bailout();
86 	}
87 #endif
88 
89 	return SUCCESS;
90 } /* }}} */
91 
92 zend_module_entry phpdbg_webhelper_module_entry = {
93 	STANDARD_MODULE_HEADER,
94 	"phpdbg_webhelper",
95 	NULL,
96 	PHP_MINIT(phpdbg_webhelper),
97 	NULL,
98 	PHP_RINIT(phpdbg_webhelper),
99 	NULL,
100 	NULL,
101 	PHPDBG_VERSION,
102 	STANDARD_MODULE_PROPERTIES
103 };
104 
105 #ifdef COMPILE_DL_PHPDBG_WEBHELPER
106 ZEND_GET_MODULE(phpdbg_webhelper)
107 #endif
108