xref: /PHP-7.2/sapi/phpdbg/phpdbg_rinit_hook.c (revision 7a7ec01a)
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    | 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 		int len = strlen(PHPDBG_WG(path)) + sizeof(sock.sun_family);
60 		char buf[(1 << 8) + 1];
61 		int 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 		char msglen[5] = {0};
71 		phpdbg_webdata_compress(&msg, (int *)msglen);
72 
73 		send(s, msglen, 4, 0);
74 		send(s, msg, *(int *) msglen, 0);
75 
76 		while ((buflen = recv(s, buf, sizeof(buf) - 1, 0)) > 0) {
77 			php_write(buf, buflen);
78 		}
79 
80 		close(s);
81 
82 		php_output_flush_all();
83 		zend_bailout();
84 	}
85 #endif
86 
87 	return SUCCESS;
88 } /* }}} */
89 
90 zend_module_entry phpdbg_webhelper_module_entry = {
91 	STANDARD_MODULE_HEADER,
92 	"phpdbg_webhelper",
93 	NULL,
94 	PHP_MINIT(phpdbg_webhelper),
95 	NULL,
96 	PHP_RINIT(phpdbg_webhelper),
97 	NULL,
98 	NULL,
99 	PHPDBG_VERSION,
100 	STANDARD_MODULE_PROPERTIES
101 };
102 
103 #ifdef COMPILE_DL_PHPDBG_WEBHELPER
104 ZEND_GET_MODULE(phpdbg_webhelper)
105 #endif
106