xref: /PHP-7.4/sapi/phpdbg/phpdbg_rinit_hook.c (revision 0cf7de1c)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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    | Authors: Bob Weinand <bwoebi@php.net>                                |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "phpdbg_rinit_hook.h"
20 #include "php_ini.h"
21 #include <errno.h>
22 
23 ZEND_DECLARE_MODULE_GLOBALS(phpdbg_webhelper);
24 
25 PHP_INI_BEGIN()
26 	STD_PHP_INI_ENTRY("phpdbg.auth", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, auth, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
27 	STD_PHP_INI_ENTRY("phpdbg.path", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, path, zend_phpdbg_webhelper_globals, phpdbg_webhelper_globals)
PHP_INI_END()28 PHP_INI_END()
29 
30 static inline void php_phpdbg_webhelper_globals_ctor(zend_phpdbg_webhelper_globals *pg) /* {{{ */
31 {
32 } /* }}} */
33 
PHP_MINIT_FUNCTION(phpdbg_webhelper)34 static PHP_MINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
35 {
36 	if (!strcmp(sapi_module.name, PHPDBG_NAME)) {
37 		return SUCCESS;
38 	}
39 
40 	ZEND_INIT_MODULE_GLOBALS(phpdbg_webhelper, php_phpdbg_webhelper_globals_ctor, NULL);
41 	REGISTER_INI_ENTRIES();
42 
43 	return SUCCESS;
44 } /* }}} */
45 
PHP_RINIT_FUNCTION(phpdbg_webhelper)46 static PHP_RINIT_FUNCTION(phpdbg_webhelper) /* {{{ */
47 {
48 	zval cookies = PG(http_globals)[TRACK_VARS_COOKIE];
49 	zval *auth;
50 
51 	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))) {
52 		return SUCCESS;
53 	}
54 
55 #ifndef _WIN32
56 	{
57 		struct sockaddr_un sock;
58 		int s = socket(AF_UNIX, SOCK_STREAM, 0);
59 		size_t len = strlen(PHPDBG_WG(path)) + sizeof(sock.sun_family);
60 		char buf[(1 << 8) + 1];
61 		ssize_t buflen;
62 		sock.sun_family = AF_UNIX;
63 		strcpy(sock.sun_path, PHPDBG_WG(path));
64 
65 		if (connect(s, (struct sockaddr *)&sock, len) == -1) {
66 			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));
67 		}
68 
69 		char *msg = NULL;
70 		size_t msglen = 0;
71 		phpdbg_webdata_compress(&msg, &msglen);
72 
73 		buf[0] = (msglen >>  0) & 0xff;
74 		buf[1] = (msglen >>  8) & 0xff;
75 		buf[2] = (msglen >> 16) & 0xff;
76 		buf[3] = (msglen >> 24) & 0xff;
77 		send(s, buf, 4, 0);
78 		send(s, msg, msglen, 0);
79 
80 		while ((buflen = recv(s, buf, sizeof(buf) - 1, 0)) > 0) {
81 			php_write(buf, buflen);
82 		}
83 
84 		close(s);
85 
86 		php_output_flush_all();
87 		zend_bailout();
88 	}
89 #endif
90 
91 	return SUCCESS;
92 } /* }}} */
93 
94 zend_module_entry phpdbg_webhelper_module_entry = {
95 	STANDARD_MODULE_HEADER,
96 	"phpdbg_webhelper",
97 	NULL,
98 	PHP_MINIT(phpdbg_webhelper),
99 	NULL,
100 	PHP_RINIT(phpdbg_webhelper),
101 	NULL,
102 	NULL,
103 	PHPDBG_VERSION,
104 	STANDARD_MODULE_PROPERTIES
105 };
106 
107 #ifdef COMPILE_DL_PHPDBG_WEBHELPER
108 ZEND_GET_MODULE(phpdbg_webhelper)
109 #endif
110