From c35bb34608b3dc0f15296ab5ffddf2d8a594a65a Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Tue, 9 Sep 2014 02:15:33 +0200 Subject: [PATCH 1/5] Add webhelper extension and support for loading extensions New phpdbg commands: dl [path] - loads module / Zend extension wait - waits for incoming connection from a phpdbg_webhelper module Webhelper module is a UNIX domain socket to which a SAPI with the phpdbg_webhelper module loaded will write to the socket information about its whole environment. phpdbg can then run the request locally [TODO: write the request back to the sender] --- config.m4 | 20 +- phpdbg.c | 8 +- phpdbg.h | 25 +- phpdbg_cmd.c | 3 - phpdbg_prompt.c | 250 +++++++++++++++++++- phpdbg_prompt.h | 4 +- phpdbg_rinit_hook.c | 88 +++++++ phpdbg_rinit_hook.h | 43 ++++ phpdbg_webdata_transfer.h | 480 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 901 insertions(+), 20 deletions(-) create mode 100644 phpdbg_rinit_hook.c create mode 100644 phpdbg_rinit_hook.h create mode 100644 phpdbg_webdata_transfer.h diff --git a/config.m4 b/config.m4 index d78a439af02..06c7ee098a4 100644 --- a/config.m4 +++ b/config.m4 @@ -3,12 +3,15 @@ dnl $Id$ dnl PHP_ARG_ENABLE(phpdbg, for phpdbg support, -[ --enable-phpdbg Build phpdbg], no, no) +[ --enable-phpdbg Build phpdbg], no, no) + +PHP_ARG_ENABLE(phpdbg-webhelper, for phpdbg web SAPI support, +[ --enable-phpdbg-webhelper Build phpdbg web SAPI support], yes, yes) PHP_ARG_ENABLE(phpdbg-debug, for phpdbg debug build, -[ --enable-phpdbg-debug Build phpdbg in debug mode], no, no) +[ --enable-phpdbg-debug Build phpdbg in debug mode], no, no) -if test "$PHP_PHPDBG" != "no"; then +if test "$BUILD_PHPDBG" == "" && test "$PHP_PHPDBG" != "no"; then AC_HEADER_TIOCGWINSZ AC_DEFINE(HAVE_PHPDBG, 1, [ ]) @@ -18,6 +21,17 @@ if test "$PHP_PHPDBG" != "no"; then AC_DEFINE(PHPDBG_DEBUG, 0, [ ]) fi + if test "$PHP_PHPDBG_WEBHELPER" != "no"; then + if ! test -d ext/phpdbg_webhelper; then + ln -s ../sapi/phpdbg ext/phpdbg_webhelper + fi + if test "$PHP_JSON" != "no"; then + PHP_NEW_EXTENSION(phpdbg_webhelper, phpdbg_rinit_hook.c, $ext_shared) + else + AC_MSG_ERROR(Webhelper extension of phpdbg needs json enabled) + fi + fi + PHP_PHPDBG_CFLAGS="-D_GNU_SOURCE" PHP_PHPDBG_FILES="phpdbg.c phpdbg_parser.c phpdbg_lexer.c phpdbg_prompt.c phpdbg_help.c phpdbg_break.c phpdbg_print.c phpdbg_bp.c phpdbg_opcode.c phpdbg_list.c phpdbg_utils.c phpdbg_info.c phpdbg_cmd.c phpdbg_set.c phpdbg_frame.c phpdbg_watch.c phpdbg_btree.c" diff --git a/phpdbg.c b/phpdbg.c index 1fbd18a423a..15da7196b50 100644 --- a/phpdbg.c +++ b/phpdbg.c @@ -43,6 +43,10 @@ ZEND_DECLARE_MODULE_GLOBALS(phpdbg); +PHP_INI_BEGIN() + STD_PHP_INI_ENTRY("phpdbg.path", "", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateString, socket_path, zend_phpdbg_globals, phpdbg_globals) +PHP_INI_END() + static zend_bool phpdbg_booted = 0; #if PHP_VERSION_ID >= 50500 @@ -77,6 +81,8 @@ static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) /* {{{ */ static PHP_MINIT_FUNCTION(phpdbg) /* {{{ */ { ZEND_INIT_MODULE_GLOBALS(phpdbg, php_phpdbg_globals_ctor, NULL); + REGISTER_INI_ENTRIES(); + #if PHP_VERSION_ID >= 50500 zend_execute_old = zend_execute_ex; zend_execute_ex = phpdbg_execute_ex; @@ -86,7 +92,7 @@ static PHP_MINIT_FUNCTION(phpdbg) /* {{{ */ #endif REGISTER_STRINGL_CONSTANT("PHPDBG_VERSION", PHPDBG_VERSION, sizeof(PHPDBG_VERSION)-1, CONST_CS|CONST_PERSISTENT); - + REGISTER_LONG_CONSTANT("PHPDBG_FILE", FILE_PARAM, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PHPDBG_METHOD", METHOD_PARAM, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PHPDBG_LINENO", NUMERIC_PARAM, CONST_CS|CONST_PERSISTENT); diff --git a/phpdbg.h b/phpdbg.h index 2fa2d5093a8..cf47cfc1e4e 100644 --- a/phpdbg.h +++ b/phpdbg.h @@ -64,7 +64,7 @@ # include "TSRM.h" #endif -#ifdef HAVE_LIBREADLINE +#ifdef LIBREADLINE # include # include #endif @@ -72,6 +72,16 @@ # include #endif +/* {{{ strings */ +#define PHPDBG_NAME "phpdbg" +#define PHPDBG_AUTHORS "Felipe Pena, Joe Watkins and Bob Weinand" /* Ordered by last name */ +#define PHPDBG_URL "http://phpdbg.com" +#define PHPDBG_ISSUES "http://github.com/krakjoe/phpdbg/issues" +#define PHPDBG_VERSION "0.4.0" +#define PHPDBG_INIT_FILENAME ".phpdbginit" +/* }}} */ + +#ifndef PHPDBG_WEBHELPER_H #include "phpdbg_lexer.h" #include "phpdbg_cmd.h" #include "phpdbg_utils.h" @@ -158,15 +168,6 @@ int phpdbg_do_parse(phpdbg_param_t *stack, char *input TSRMLS_DC); # define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET|PHPDBG_IS_BP_ENABLED) #endif /* }}} */ -/* {{{ strings */ -#define PHPDBG_NAME "phpdbg" -#define PHPDBG_AUTHORS "Felipe Pena, Joe Watkins and Bob Weinand" /* Ordered by last name */ -#define PHPDBG_URL "http://phpdbg.com" -#define PHPDBG_ISSUES "http://github.com/krakjoe/phpdbg/issues" -#define PHPDBG_VERSION "0.4.0" -#define PHPDBG_INIT_FILENAME ".phpdbginit" -/* }}} */ - /* {{{ output descriptors */ #define PHPDBG_STDIN 0 #define PHPDBG_STDOUT 1 @@ -210,6 +211,8 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg) char *buffer; /* buffer */ zend_ulong flags; /* phpdbg flags */ + + char *socket_path; /* phpdbg.path ini setting */ ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */ /* the beginning (= the important part) of the _zend_mm_heap struct defined in Zend/zend_alloc.c @@ -227,4 +230,6 @@ struct _zend_mm_heap { zend_mm_storage *storage; }; +#endif + #endif /* PHPDBG_H */ diff --git a/phpdbg_cmd.c b/phpdbg_cmd.c index a45513bee6b..38b73b6a149 100644 --- a/phpdbg_cmd.c +++ b/phpdbg_cmd.c @@ -792,9 +792,7 @@ PHPDBG_API int phpdbg_stack_execute(phpdbg_param_t *stack, char **why TSRMLS_DC) PHPDBG_API char* phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */ { char *cmd = NULL; -#if !defined(HAVE_LIBREADLINE) && !defined(HAVE_LIBEDIT) char buf[PHPDBG_MAX_CMD]; -#endif char *buffer = NULL; if (!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING)) { @@ -832,7 +830,6 @@ readline: /* note: EOF makes readline write prompt again in local console mode */ readline: if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE)) { - char buf[PHPDBG_MAX_CMD]; if (fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) { cmd = buf; } else goto disconnect; diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index d91ef3f3f5c..2940d0a1560 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -23,6 +23,9 @@ #include "zend.h" #include "zend_compile.h" #include "phpdbg.h" + +ZEND_EXTERN_MODULE_GLOBALS(phpdbg); + #include "phpdbg_help.h" #include "phpdbg_print.h" #include "phpdbg_info.h" @@ -37,6 +40,21 @@ #include "phpdbg_frame.h" #include "phpdbg_lexer.h" #include "phpdbg_parser.h" +#include "phpdbg_webdata_transfer.h" + +#ifdef HAVE_LIBDL +#ifdef PHP_WIN32 +#include "win32/param.h" +#include "win32/winutil.h" +#define GET_DL_ERROR() php_win_err() +#elif defined(NETWARE) +#include +#define GET_DL_ERROR() dlerror() +#else +#include +#define GET_DL_ERROR() DL_ERROR() +#endif +#endif /* {{{ command declarations */ const phpdbg_command_t phpdbg_prompt_commands[] = { @@ -61,14 +79,14 @@ const phpdbg_command_t phpdbg_prompt_commands[] = { PHPDBG_COMMAND_D(register,"register a function", 'R', NULL, "s"), PHPDBG_COMMAND_D(source, "execute a phpdbginit", '<', NULL, "s"), PHPDBG_COMMAND_D(export, "export breaks to a .phpdbginit script", '>', NULL, "s"), + PHPDBG_COMMAND_D(dl, "load module or zend extension", 0, NULL, "|s"), PHPDBG_COMMAND_D(sh, "shell a command", 0, NULL, "i"), PHPDBG_COMMAND_D(quit, "exit phpdbg", 'q', NULL, 0), + PHPDBG_COMMAND_D(wait, "wait for other process", 'W', NULL, 0), PHPDBG_COMMAND_D(watch, "set watchpoint", 'w', phpdbg_watch_commands, "|ss"), PHPDBG_END_COMMAND }; /* }}} */ -ZEND_EXTERN_MODULE_GLOBALS(phpdbg); - static inline int phpdbg_call_register(phpdbg_param_t *stack TSRMLS_DC) /* {{{ */ { phpdbg_param_t *name = NULL; @@ -847,6 +865,190 @@ PHPDBG_COMMAND(sh) /* {{{ */ return SUCCESS; } /* }}} */ +static int add_extension_info(zend_module_entry *module TSRMLS_DC) { + phpdbg_write("%s\n", module->name); + return 0; +} + +static int add_zendext_info(zend_extension *ext TSRMLS_DC) { + phpdbg_write("%s\n", ext->name); + return 0; +} + +PHPDBG_API const char *phpdbg_load_module_or_extension(char **path, char **str TSRMLS_DC) { + DL_HANDLE handle; + char *extension_dir; + + extension_dir = INI_STR("extension_dir"); + + if (strchr(*path, '/') != NULL || strchr(*path, DEFAULT_SLASH) != NULL) { + /* path is fine */ + } else if (extension_dir && extension_dir[0]) { + char *libpath; + int extension_dir_len = strlen(extension_dir); + if (IS_SLASH(extension_dir[extension_dir_len-1])) { + spprintf(&libpath, 0, "%s%s", extension_dir, *path); /* SAFE */ + } else { + spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, *path); /* SAFE */ + } + efree(*path); + *path = libpath; + } else { + *str = estrdup("Not a full path given or extension_dir ini setting is not set"); + + return NULL; + } + + handle = DL_LOAD(*path); + + if (!handle) { +#if PHP_WIN32 + char *err = GET_DL_ERROR(); + if (err && (*err != "")) { + *str = estrdup(err); + LocalFree(err); + } else { + *str = estrdup("Unknown reason"); + } +#else + *str = estrdup(GET_DL_ERROR()); + GET_DL_ERROR(); /* free the buffer storing the error */ +#endif + return NULL; + } + +#if ZEND_EXTENSIONS_SUPPORT + do { + zend_extension *new_extension; + zend_extension_version_info *extension_version_info; + + extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info"); + if (!extension_version_info) { + extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info"); + } + new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry"); + if (!new_extension) { + new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry"); + } + if (!extension_version_info || !new_extension) { + break; + } + if (extension_version_info->zend_extension_api_no != ZEND_EXTENSION_API_NO &&(!new_extension->api_no_check || new_extension->api_no_check(ZEND_EXTENSION_API_NO) != SUCCESS)) { + asprintf(str, "%s requires Zend Engine API version %d, which does not match the installed Zend Engine API version %d", new_extension->name, extension_version_info->zend_extension_api_no, ZEND_EXTENSION_API_NO); + + goto quit; + } else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) && (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) { + asprintf(str, "%s was built with configuration %s, whereas running engine is %s", new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID); + + goto quit; + } + + *str = new_extension->name; + + zend_register_extension(new_extension, handle); + + if (new_extension->startup) { + if (new_extension->startup(new_extension) != SUCCESS) { + asprintf(str, "Unable to startup Zend extension %s", new_extension->name); + + goto quit; + } + zend_append_version_info(new_extension); + } + + return "Zend extension"; + } while (0); +#endif + + do { + zend_module_entry *module_entry; + zend_module_entry *(*get_module)(void); + + get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module"); + if (!get_module) { + get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module"); + } + + if (!get_module) { + break; + } + + module_entry = get_module(); + + if (strcmp(ZEND_EXTENSION_BUILD_ID, module_entry->build_id)) { + asprintf(str, "%s was built with configuration %s, whereas running engine is %s", module_entry->name, module_entry->build_id, ZEND_EXTENSION_BUILD_ID); + + goto quit; + } + + module_entry->type = MODULE_PERSISTENT; + module_entry->module_number = zend_next_free_module(); + module_entry->handle = handle; + + if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) { + asprintf(str, "Unable to register module %s", module_entry->name); + + goto quit; + } + + if (zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) { + asprintf(str, "Unable to startup module %s", module_entry->name); + + goto quit; + } + + if (module_entry->request_startup_func) { + if (module_entry->request_startup_func(MODULE_PERSISTENT, module_entry->module_number TSRMLS_CC) == FAILURE) { + asprintf(str, "Unable to initialize module %s", module_entry->name); + + goto quit; + } + } + + return "module"; + } while (0); + + *str = estrdup("This shared object is nor a Zend extension nor a module"); + +quit: + DL_UNLOAD(handle); + return NULL; +} + +PHPDBG_COMMAND(dl) /* {{{ */ +{ + const char *type; + char *name, *path; + + if (!param || param->type == EMPTY_PARAM) { + phpdbg_notice("Zend extensions"); + zend_llist_apply(&zend_extensions, (llist_apply_func_t) add_zendext_info TSRMLS_CC); + phpdbg_writeln(""); + phpdbg_notice("Modules"); + zend_hash_apply(&module_registry, (apply_func_t) add_extension_info TSRMLS_CC); + } else switch (param->type) { + case STR_PARAM: +#ifdef HAVE_LIBDL + path = estrndup(param->str, param->len); + + if ((type = phpdbg_load_module_or_extension(&path, &name TSRMLS_CC)) == NULL) { + phpdbg_error("Could not load %s, not found or invalid zend extension / module: %s", path, name); + efree(name); + } else { + phpdbg_notice("Successfully loaded the %s %s at path %s", type, name, path); + } + efree(path); +#else + phpdbg_error("Cannot dynamically load %.*s - dynamic modules are not supported", (int) param->len, param->str); +#endif + break; + + phpdbg_default_switch_case(); + } + + return SUCCESS; +} /* }}} */ + PHPDBG_COMMAND(source) /* {{{ */ { struct stat sb; @@ -991,6 +1193,50 @@ PHPDBG_COMMAND(watch) /* {{{ */ return SUCCESS; } /* }}} */ +PHPDBG_COMMAND(wait) /* {{{ */ +{ + struct sockaddr_un local, remote; + int rlen, len, sr, sl = socket(AF_UNIX, SOCK_STREAM, 0); + unlink(PHPDBG_G(socket_path)); + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, PHPDBG_G(socket_path)); + len = strlen(local.sun_path) + sizeof(local.sun_family); + if (bind(sl, (struct sockaddr *)&local, len) == -1) { + phpdbg_error("Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting", PHPDBG_G(socket_path)); + return FAILURE; + } + + chmod(PHPDBG_G(socket_path), 0666); + + listen(sl, 2); + + rlen = sizeof(remote); + sr = accept(sl, (struct sockaddr *) &remote, (socklen_t *) &rlen); + + char msglen[5]; + int recvd = 4; + + do { + recvd -= recv(sr, &(msglen[4 - recvd]), recvd, 0); + } while (recvd > 0); + + recvd = *(size_t *) msglen; + char *data = emalloc(recvd); + + do { + recvd -= recv(sr, &(data[(*(int *) msglen) - recvd]), recvd, 0); + } while (recvd > 0); + + phpdbg_webdata_decompress(data, *(int *) msglen TSRMLS_CC); + + efree(data); + + phpdbg_notice("Successfully imported request data, stopped before executing"); + + return SUCCESS; +} /* }}} */ + int phpdbg_interactive(TSRMLS_D) /* {{{ */ { int ret = SUCCESS; diff --git a/phpdbg_prompt.h b/phpdbg_prompt.h index ef648aabeb0..9c8c452669f 100644 --- a/phpdbg_prompt.h +++ b/phpdbg_prompt.h @@ -47,12 +47,14 @@ PHPDBG_COMMAND(clean); PHPDBG_COMMAND(clear); PHPDBG_COMMAND(help); PHPDBG_COMMAND(sh); +PHPDBG_COMMAND(dl); PHPDBG_COMMAND(set); PHPDBG_COMMAND(source); PHPDBG_COMMAND(export); PHPDBG_COMMAND(register); PHPDBG_COMMAND(quit); -PHPDBG_COMMAND(watch); /* }}} */ +PHPDBG_COMMAND(watch); +PHPDBG_COMMAND(wait); /* }}} */ /* {{{ prompt commands */ extern const phpdbg_command_t phpdbg_prompt_commands[]; /* }}} */ diff --git a/phpdbg_rinit_hook.c b/phpdbg_rinit_hook.c new file mode 100644 index 00000000000..d68fd9ac9af --- /dev/null +++ b/phpdbg_rinit_hook.c @@ -0,0 +1,88 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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 | + +----------------------------------------------------------------------+ +*/ + +#include "phpdbg_rinit_hook.h" +#include "php_ini.h" +#include + +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 (!cookies || zend_hash_find(Z_ARRVAL_P(cookies), PHPDBG_NAME "_AUTH_COOKIE", sizeof(PHPDBG_NAME "_AUTH_COOKIE"), (void **) &auth) == FAILURE || Z_STRLEN_PP(auth) != strlen(PHPDBG_WG(auth)) || strcmp(Z_STRVAL_PP(auth), PHPDBG_WG(auth))) { + return SUCCESS; + } + +#ifndef _WIN32 + struct sockaddr_un sock; + int s = socket(AF_UNIX, SOCK_STREAM, 0); + int len = strlen(PHPDBG_WG(path)) + sizeof(sock.sun_family); + 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; + char msglen[5] = {0}; + phpdbg_webdata_compress(&msg, (int *)msglen TSRMLS_CC); + + send(s, msglen, 4, 0); + send(s, msg, *(int *) msglen, 0); +#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 +}; diff --git a/phpdbg_rinit_hook.h b/phpdbg_rinit_hook.h new file mode 100644 index 00000000000..1c6e92971e8 --- /dev/null +++ b/phpdbg_rinit_hook.h @@ -0,0 +1,43 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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: Felipe Pena | + | Authors: Joe Watkins | + | Authors: Bob Weinand | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHPDBG_WEBHELPER_H +#define PHPDBG_WEBHELPER_H + +#define phpdbg_notice(...) + +#include "phpdbg_webdata_transfer.h" + +extern zend_module_entry phpdbg_webhelper_module_entry; +#define phpext_phpdbg_webhelper_ptr &phpdbg_webhelper_module_entry + +#ifdef ZTS +# define PHPDBG_WG(v) TSRMG(phpdbg_webhelper_globals_id, zend_phpdbg_webhelper_globals *, v) +#else +# define PHPDBG_WG(v) (phpdbg_webhelper_globals.v) +#endif + +/* {{{ structs */ +ZEND_BEGIN_MODULE_GLOBALS(phpdbg_webhelper) + char *auth; + char *path; +ZEND_END_MODULE_GLOBALS(phpdbg_webhelper) /* }}} */ + +#endif /* PHPDBG_WEBHELPER_H */ diff --git a/phpdbg_webdata_transfer.h b/phpdbg_webdata_transfer.h new file mode 100644 index 00000000000..4bcfb0c8e2e --- /dev/null +++ b/phpdbg_webdata_transfer.h @@ -0,0 +1,480 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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 | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHPDBG_WEBDATA_TRANSFER_H +#define PHPDBG_WEBDATA_TRANSFER_H + +/* {{{ remote console headers */ +#ifndef _WIN32 +# include +# include +# include +# include +#endif /* }}} */ + +#include "zend.h" +#include "phpdbg.h" +#include "ext/json/php_json.h" +#include "ext/standard/basic_functions.h" + +static inline void phpdbg_webdata_compress(char **msg, int *len TSRMLS_DC) { + smart_str buf = {0}; + zval array; + HashTable *ht; + /* I really need to change that to an array of zvals... */ + zval zv1, *zvp1 = &zv1; + zval zv2, *zvp2 = &zv2; + zval zv3, *zvp3 = &zv3; + zval zv4, *zvp4 = &zv4; + zval zv5, *zvp5 = &zv5; + zval zv6, *zvp6 = &zv6; + zval zv7, *zvp7 = &zv7; + zval zv8, *zvp8 = &zv8; + + array_init(&array); + ht = Z_ARRVAL(array); + + /* fetch superglobals */ + { + zend_is_auto_global(ZEND_STRL("GLOBALS") TSRMLS_CC); + /* might be JIT */ + zend_is_auto_global(ZEND_STRL("_ENV") TSRMLS_CC); + zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC); + zend_is_auto_global(ZEND_STRL("_REQUEST") TSRMLS_CC); + array_init(&zv1); + zend_hash_copy(Z_ARRVAL(zv1), &EG(symbol_table), NULL, (void *) NULL, sizeof(zval *)); + Z_ARRVAL(zv1)->pDestructor = NULL; /* we're operating on a copy! Don't double free zvals */ + zend_hash_del(Z_ARRVAL(zv1), "GLOBALS", sizeof("GLOBALS")); /* do not use the reference to itself in json */ + zend_hash_add(ht, "GLOBALS", sizeof("GLOBALS"), &zvp1, sizeof(zval *), NULL); + } + + /* save php://input */ + { + php_stream *stream; + int len; + char *contents; + + stream = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); + if ((len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0)) > 0) { + ZVAL_STRINGL(&zv2, contents, len, 0); + } else { + ZVAL_EMPTY_STRING(&zv2); + } + zend_hash_add(ht, "input", sizeof("input"), &zvp2, sizeof(zval *), NULL); + } + + /* change sapi name */ + { + if (sapi_module.name) { + ZVAL_STRING(&zv6, sapi_module.name, 1); + } else { + Z_TYPE(zv6) = IS_NULL; + } + zend_hash_add(ht, "sapi_name", sizeof("sapi_name"), &zvp6, sizeof(zval *), NULL); + } + + /* handle modules / extensions */ + { + HashPosition position; + zend_module_entry *module; + zend_extension *extension; + zend_llist_position pos; + + array_init(&zv7); + for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); + zend_hash_get_current_data_ex(&module_registry, (void**) &module, &position) == SUCCESS; + zend_hash_move_forward_ex(&module_registry, &position)) { + zval **value = emalloc(sizeof(zval *)); + ALLOC_ZVAL(*value); + ZVAL_STRING(*value, module->name, 1); + zend_hash_next_index_insert(Z_ARRVAL(zv7), value, sizeof(zval *), NULL); + } + zend_hash_add(ht, "modules", sizeof("modules"), &zvp7, sizeof(zval *), NULL); + + array_init(&zv8); + extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); + while (extension) { + zval **value = emalloc(sizeof(zval *)); + ALLOC_ZVAL(*value); + ZVAL_STRING(*value, extension->name, 1); + zend_hash_next_index_insert(Z_ARRVAL(zv8), value, sizeof(zval *), NULL); + extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); + } + zend_hash_add(ht, "extensions", sizeof("extensions"), &zvp8, sizeof(zval *), NULL); + } + + /* switch cwd */ + { + char *ret = NULL; + char path[MAXPATHLEN]; + +#if HAVE_GETCWD + ret = VCWD_GETCWD(path, MAXPATHLEN); +#elif HAVE_GETWD + ret = VCWD_GETWD(path); +#endif + if (ret) { + ZVAL_STRING(&zv5, path, 1); + zend_hash_add(ht, "cwd", sizeof("cwd"), &zvp5, sizeof(zval *), NULL); + } + } + + /* get system ini entries */ + { + HashPosition position; + zend_ini_entry *ini_entry; + + array_init(&zv3); + for (zend_hash_internal_pointer_reset_ex(EG(ini_directives), &position); + zend_hash_get_current_data_ex(EG(ini_directives), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(EG(ini_directives), &position)) { + zval **value = emalloc(sizeof(zval *)); + if (ini_entry->modified) { + if (!ini_entry->orig_value) { + efree(value); + continue; + } + ALLOC_ZVAL(*value); + ZVAL_STRINGL(*value, ini_entry->orig_value, ini_entry->orig_value_length, 1); + } else { + if (!ini_entry->value) { + efree(value); + continue; + } + ALLOC_ZVAL(*value); + ZVAL_STRINGL(*value, ini_entry->value, ini_entry->value_length, 1); + } + zend_hash_add(Z_ARRVAL(zv3), ini_entry->name, ini_entry->name_length, value, sizeof(zval *), NULL); + } + zend_hash_add(ht, "systemini", sizeof("systemini"), &zvp3, sizeof(zval *), NULL); + } + + /* get perdir ini entries */ + if (EG(modified_ini_directives)) { + HashPosition position; + zend_ini_entry *ini_entry; + + array_init(&zv4); + for (zend_hash_internal_pointer_reset_ex(EG(modified_ini_directives), &position); + zend_hash_get_current_data_ex(EG(modified_ini_directives), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(EG(modified_ini_directives), &position)) { + zval **value = emalloc(sizeof(zval *)); + if (!ini_entry->value) { + efree(value); + continue; + } + ALLOC_ZVAL(*value); + ZVAL_STRINGL(*value, ini_entry->value, ini_entry->value_length, 1); + zend_hash_add(Z_ARRVAL(zv4), ini_entry->name, ini_entry->name_length, value, sizeof(zval *), NULL); + } + zend_hash_add(ht, "userini", sizeof("userini"), &zvp4, sizeof(zval *), NULL); + } + + /* encode data */ + php_json_encode(&buf, &array, 0 TSRMLS_CC); + *msg = buf.c; + *len = buf.len; + zval_dtor(&array); +} + +static void phpdbg_rebuild_http_globals_array(int type, const char *name TSRMLS_DC) { + zval **zvpp; + if (PG(http_globals)[type]) { + zval_dtor(PG(http_globals)[type]); + } + if (zend_hash_find(&EG(symbol_table), name, strlen(name) + 1, (void **) &zvpp) == SUCCESS) { + Z_SET_REFCOUNT_PP(zvpp, 2); + PG(http_globals)[type] = *zvpp; + } +} + +/* +static int phpdbg_remove_rearm_autoglobals(zend_auto_global *auto_global TSRMLS_DC) { +// zend_hash_del(&EG(symbol_table), auto_global->name, auto_global->name_len + 1); + + return 1; +}*/ + +typedef struct { + HashTable *ht[2]; + HashPosition pos[2]; +} phpdbg_intersect_ptr; + +static void phpdbg_array_intersect_init(phpdbg_intersect_ptr *info, HashTable *ht1, HashTable *ht2 TSRMLS_DC) { + info->ht[0] = ht1; + info->ht[1] = ht2; + + zend_hash_sort(info->ht[0], zend_qsort, (compare_func_t) string_compare_function, 0 TSRMLS_CC); + zend_hash_sort(info->ht[1], zend_qsort, (compare_func_t) string_compare_function, 0 TSRMLS_CC); + + zend_hash_internal_pointer_reset_ex(info->ht[0], &info->pos[0]); + zend_hash_internal_pointer_reset_ex(info->ht[1], &info->pos[1]); +} + +/* -1 => first array, 0 => both arrays equal, 1 => second array */ +static int phpdbg_array_intersect(phpdbg_intersect_ptr *info, zval ***ptr) { + int ret; + zval **zvpp[2]; + int invalid = !info->ht[0] + !info->ht[1]; + + if (invalid > 0) { + invalid = !!info->ht[0]; + + if (zend_hash_get_current_data_ex(info->ht[invalid], (void **) ptr, &info->pos[invalid]) == FAILURE) { + *ptr = NULL; + return 0; + } + + zend_hash_move_forward_ex(info->ht[invalid], &info->pos[invalid]); + + return invalid ? -1 : 1; + } + + if (zend_hash_get_current_data_ex(info->ht[0], (void **) &zvpp[0], &info->pos[0]) == FAILURE) { + info->ht[0] = NULL; + return phpdbg_array_intersect(info, ptr); + } + if (zend_hash_get_current_data_ex(info->ht[1], (void **) &zvpp[1], &info->pos[1]) == FAILURE) { + info->ht[1] = NULL; + return phpdbg_array_intersect(info, ptr); + } + + ret = zend_binary_zval_strcmp(*zvpp[0], *zvpp[1]); + + if (ret <= 0) { + *ptr = zvpp[0]; + zend_hash_move_forward_ex(info->ht[0], &info->pos[0]); + } + if (ret >= 0) { + *ptr = zvpp[1]; + zend_hash_move_forward_ex(info->ht[1], &info->pos[1]); + } + + return ret; +} + +static inline void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { + zval *free_zv = NULL; + zval zv, **zvpp; + HashTable *ht; + php_json_decode(&zv, msg, len, 1, 1000 /* enough */ TSRMLS_CC); + ht = Z_ARRVAL(zv); + + /* Reapply symbol table */ + if (zend_hash_find(ht, "GLOBALS", sizeof("GLOBALS"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + zend_hash_clean(&EG(symbol_table)); + EG(symbol_table) = *Z_ARRVAL_PP(zvpp); + + /* Rebuild cookies, env vars etc. from GLOBALS (PG(http_globals)) */ + phpdbg_rebuild_http_globals_array(TRACK_VARS_POST, "_POST" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_GET, "_GET" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_COOKIE, "_COOKIE" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_SERVER, "_SERVER" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_ENV, "_ENV" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_FILES, "_FILES" TSRMLS_CC); + + Z_ADDREF_PP(zvpp); + free_zv = *zvpp; + } + + if (zend_hash_find(ht, "input", sizeof("input"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { + if (SG(request_info).request_body) { + php_stream_close(SG(request_info).request_body); + } + SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); + php_stream_truncate_set_size(SG(request_info).request_body, 0); + php_stream_write(SG(request_info).request_body, Z_STRVAL_PP(zvpp), Z_STRLEN_PP(zvpp)); + } + + if (zend_hash_find(ht, "cwd", sizeof("cwd"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { + if (VCWD_CHDIR(Z_STRVAL_PP(zvpp)) == SUCCESS) { + if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentStatFile), strlen(BG(CurrentStatFile)))) { + efree(BG(CurrentStatFile)); + BG(CurrentStatFile) = NULL; + } + if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentLStatFile), strlen(BG(CurrentLStatFile)))) { + efree(BG(CurrentLStatFile)); + BG(CurrentLStatFile) = NULL; + } + } + } + + if (zend_hash_find(ht, "sapi_name", sizeof("sapi_name"), (void **) &zvpp) == SUCCESS && (Z_TYPE_PP(zvpp) == IS_STRING || Z_TYPE_PP(zvpp) == IS_NULL)) { + if (sapi_module.name) { + efree(sapi_module.name); + } + if (Z_TYPE_PP(zvpp) == IS_STRING) { + sapi_module.name = estrndup(Z_STRVAL_PP(zvpp), Z_STRLEN_PP(zvpp)); + } else { + sapi_module.name = NULL; + } + } + + if (zend_hash_find(ht, "modules", sizeof("modules"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + phpdbg_intersect_ptr pos; + zval **module; + zend_module_entry *mod; + HashTable zv_registry; + + /* intersect modules, unregister mpdules loaded "too much", announce not yet registered modules (phpdbg_notice) */ + + zend_hash_init(&zv_registry, zend_hash_num_elements(&zv_registry), 0, ZVAL_PTR_DTOR, 0); + for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); + zend_hash_get_current_data_ex(&module_registry, (void **) &mod, &position) == SUCCESS; + zend_hash_move_forward_ex(&module_registry, &position)) { + zval **value = emalloc(sizeof(zval *)); + ALLOC_ZVAL(*value); + ZVAL_STRING(*value, mod->name, 1); + zend_hash_next_index_insert(&zv_registry, value, sizeof(zval *), NULL); + } + + phpdbg_array_intersect_init(&pos, &zv_registry, Z_ARRVAL_PP(zvpp) TSRMLS_CC); + do { + int mode = phpdbg_array_intersect(&pos, &module); + if (mode < 0) { + // loaded module, but not needed + zend_hash_del(&module_registry, Z_STRVAL_PP(module), Z_STRLEN_PP(module) + 1); + } else if (mode > 0) { + // not loaded module + phpdbg_notice("The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module)); + } + } while (module); + + zend_hash_clean(&zv_registry); + } + + if (zend_hash_find(ht, "extensions", sizeof("extensions"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + zend_extension *extension; + zend_llist_position pos; + HashPosition hpos; + zval **name, key; + + extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); + while (extension) { + extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); + + /* php_serach_array() body should be in some ZEND_API function */ + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { + if (Z_TYPE_PP(name) == IS_STRING && !zend_binary_strcmp(extension->name, strlen(extension->name), Z_STRVAL_PP(name), Z_STRLEN_PP(name))) { + break; + } + } + + if (zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &zvpp, &hpos) == FAILURE) { + /* sigh, breaking the encapsulation, there aren't any functions manipulating the llist at the place of the zend_llist_position */ + zend_llist_element *elm = pos; + if (elm->prev) { + elm->prev->next = elm->next; + } else { + zend_extensions.head = elm->next; + } + if (elm->next) { + elm->next->prev = elm->prev; + } else { + zend_extensions.tail = elm->prev; + } +#if ZEND_EXTENSIONS_SUPPORT + if (extension->shutdown) { + extension->shutdown(extension); + } +#endif + if (zend_extensions.dtor) { + zend_extensions.dtor(elm->data); + } + pefree(elm, zend_extensions.persistent); + zend_extensions.count--; + } else { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &hpos); + if (Z_TYPE(key) == IS_LONG) { + zend_hash_index_del(Z_ARRVAL_PP(zvpp), Z_LVAL(key)); + } + } + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { + phpdbg_notice("The (zend) extension %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/extension.so", Z_STRLEN_PP(name), Z_STRVAL_PP(name)); + } + } + } + + zend_ini_deactivate(TSRMLS_C); + + if (zend_hash_find(ht, "systemini", sizeof("systemini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + zval **ini_entry; + zend_ini_entry *original_ini; + zval key; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); + if (Z_TYPE(key) == IS_STRING) { + if (Z_TYPE_PP(ini_entry) == IS_STRING) { + if (zend_hash_find(EG(ini_directives), Z_STRVAL(key), Z_STRLEN(key) + 1, (void **) &original_ini) == SUCCESS) { + if (!original_ini->on_modify || original_ini->on_modify(original_ini, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), original_ini->mh_arg1, original_ini->mh_arg2, original_ini->mh_arg3, ZEND_INI_STAGE_ACTIVATE TSRMLS_CC) == SUCCESS) { + original_ini->value = Z_STRVAL_PP(ini_entry); + original_ini->value_length = Z_STRLEN_PP(ini_entry); + continue; /* don't free the string */ + } + } + } + efree(Z_STRVAL(key)); + } + } + } + + if (zend_hash_find(ht, "userini", sizeof("userini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + zval **ini_entry; + zval key; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); + if (Z_TYPE(key) == IS_STRING) { + if (Z_TYPE_PP(ini_entry) == IS_STRING) { + zend_alter_ini_entry_ex(Z_STRVAL(key), Z_STRLEN(key) + 1, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), ZEND_INI_PERDIR, ZEND_INI_STAGE_HTACCESS, 1 TSRMLS_CC); + } + efree(Z_STRVAL(key)); + } + } + } + + zval_dtor(&zv); + if (free_zv) { + /* separate freeing to not dtor the symtable too, just the container zval... */ + efree(free_zv); + } + + /* Remove and readd autoglobals */ +/* php_hash_environment(TSRMLS_C); + zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_remove_rearm_autoglobals TSRMLS_CC); + php_startup_auto_globals(TSRMLS_C); + zend_activate_auto_globals(TSRMLS_C); +*/ + /* Reapply raw input */ + /* ??? */ +} + +#endif /* PHPDBG_WEBDATA_TRANSFER_H */ From 554b30389e854270eca46359a72b9d5c177c3dc0 Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Wed, 10 Sep 2014 15:53:40 +0200 Subject: [PATCH 2/5] Fix segfaults, reorder files and set filename to exec --- config.m4 | 4 +- phpdbg.c | 12 +- phpdbg.h | 11 +- phpdbg_prompt.c | 50 +---- phpdbg_rinit_hook.h | 2 - phpdbg_wait.c | 392 ++++++++++++++++++++++++++++++++ phpdbg_wait.h | 29 +++ phpdbg_webdata_transfer.c | 180 +++++++++++++++ phpdbg_webdata_transfer.h | 455 +------------------------------------- 9 files changed, 625 insertions(+), 510 deletions(-) create mode 100644 phpdbg_wait.c create mode 100644 phpdbg_wait.h create mode 100644 phpdbg_webdata_transfer.c diff --git a/config.m4 b/config.m4 index 06c7ee098a4..a589c320184 100644 --- a/config.m4 +++ b/config.m4 @@ -26,14 +26,14 @@ if test "$BUILD_PHPDBG" == "" && test "$PHP_PHPDBG" != "no"; then ln -s ../sapi/phpdbg ext/phpdbg_webhelper fi if test "$PHP_JSON" != "no"; then - PHP_NEW_EXTENSION(phpdbg_webhelper, phpdbg_rinit_hook.c, $ext_shared) + PHP_NEW_EXTENSION(phpdbg_webhelper, phpdbg_rinit_hook.c phpdbg_webdata_transfer.c, $ext_shared) else AC_MSG_ERROR(Webhelper extension of phpdbg needs json enabled) fi fi PHP_PHPDBG_CFLAGS="-D_GNU_SOURCE" - PHP_PHPDBG_FILES="phpdbg.c phpdbg_parser.c phpdbg_lexer.c phpdbg_prompt.c phpdbg_help.c phpdbg_break.c phpdbg_print.c phpdbg_bp.c phpdbg_opcode.c phpdbg_list.c phpdbg_utils.c phpdbg_info.c phpdbg_cmd.c phpdbg_set.c phpdbg_frame.c phpdbg_watch.c phpdbg_btree.c" + PHP_PHPDBG_FILES="phpdbg.c phpdbg_parser.c phpdbg_lexer.c phpdbg_prompt.c phpdbg_help.c phpdbg_break.c phpdbg_print.c phpdbg_bp.c phpdbg_opcode.c phpdbg_list.c phpdbg_utils.c phpdbg_info.c phpdbg_cmd.c phpdbg_set.c phpdbg_frame.c phpdbg_watch.c phpdbg_btree.c phpdbg_wait.c" if test "$PHP_READLINE" != "no" -o "$PHP_LIBEDIT" != "no"; then PHPDBG_EXTRA_LIBS="$PHP_READLINE_LIBS" diff --git a/phpdbg.c b/phpdbg.c index 15da7196b50..e4868879fdb 100644 --- a/phpdbg.c +++ b/phpdbg.c @@ -76,6 +76,8 @@ static inline void php_phpdbg_globals_ctor(zend_phpdbg_globals *pg) /* {{{ */ pg->io[PHPDBG_STDOUT] = NULL; pg->io[PHPDBG_STDERR] = NULL; pg->frame.num = 0; + + pg->sapi_name_ptr = NULL; } /* }}} */ static PHP_MINIT_FUNCTION(phpdbg) /* {{{ */ @@ -192,7 +194,7 @@ static PHP_RSHUTDOWN_FUNCTION(phpdbg) /* {{{ */ efree(PHPDBG_G(buffer)); PHPDBG_G(buffer) = NULL; } - + if (PHPDBG_G(exec)) { efree(PHPDBG_G(exec)); PHPDBG_G(exec) = NULL; @@ -1283,6 +1285,8 @@ phpdbg_main: sigaction(SIGBUS, &signal_struct, &PHPDBG_G(old_sigsegv_signal)); #endif + PHPDBG_G(sapi_name_ptr) = sapi_name; + if (php_request_startup(TSRMLS_C) == SUCCESS) { int i; @@ -1516,10 +1520,10 @@ phpdbg_out: } #endif - if (sapi_name) { - free(sapi_name); + if (PHPDBG_G(sapi_name_ptr)) { + free(PHPDBG_G(sapi_name_ptr)); } - + #ifdef _WIN32 free(bp_tmp_file); #else diff --git a/phpdbg.h b/phpdbg.h index cf47cfc1e4e..7ee0b44afd8 100644 --- a/phpdbg.h +++ b/phpdbg.h @@ -72,6 +72,14 @@ # include #endif +/* {{{ remote console headers */ +#ifndef _WIN32 +# include +# include +# include +# include +#endif /* }}} */ + /* {{{ strings */ #define PHPDBG_NAME "phpdbg" #define PHPDBG_AUTHORS "Felipe Pena, Joe Watkins and Bob Weinand" /* Ordered by last name */ @@ -81,7 +89,7 @@ #define PHPDBG_INIT_FILENAME ".phpdbginit" /* }}} */ -#ifndef PHPDBG_WEBHELPER_H +#if !defined(PHPDBG_WEBDATA_TRANSFER_H) && !defined(PHPDBG_WEBHELPER_H) #include "phpdbg_lexer.h" #include "phpdbg_cmd.h" #include "phpdbg_utils.h" @@ -213,6 +221,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg) zend_ulong flags; /* phpdbg flags */ char *socket_path; /* phpdbg.path ini setting */ + char *sapi_name_ptr; /* store sapi name to free it if necessary to not leak memory */ ZEND_END_MODULE_GLOBALS(phpdbg) /* }}} */ /* the beginning (= the important part) of the _zend_mm_heap struct defined in Zend/zend_alloc.c diff --git a/phpdbg_prompt.c b/phpdbg_prompt.c index 2940d0a1560..e38a172c468 100644 --- a/phpdbg_prompt.c +++ b/phpdbg_prompt.c @@ -24,8 +24,6 @@ #include "zend_compile.h" #include "phpdbg.h" -ZEND_EXTERN_MODULE_GLOBALS(phpdbg); - #include "phpdbg_help.h" #include "phpdbg_print.h" #include "phpdbg_info.h" @@ -40,7 +38,9 @@ ZEND_EXTERN_MODULE_GLOBALS(phpdbg); #include "phpdbg_frame.h" #include "phpdbg_lexer.h" #include "phpdbg_parser.h" -#include "phpdbg_webdata_transfer.h" +#include "phpdbg_wait.h" + +ZEND_EXTERN_MODULE_GLOBALS(phpdbg); #ifdef HAVE_LIBDL #ifdef PHP_WIN32 @@ -1193,50 +1193,6 @@ PHPDBG_COMMAND(watch) /* {{{ */ return SUCCESS; } /* }}} */ -PHPDBG_COMMAND(wait) /* {{{ */ -{ - struct sockaddr_un local, remote; - int rlen, len, sr, sl = socket(AF_UNIX, SOCK_STREAM, 0); - unlink(PHPDBG_G(socket_path)); - - local.sun_family = AF_UNIX; - strcpy(local.sun_path, PHPDBG_G(socket_path)); - len = strlen(local.sun_path) + sizeof(local.sun_family); - if (bind(sl, (struct sockaddr *)&local, len) == -1) { - phpdbg_error("Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting", PHPDBG_G(socket_path)); - return FAILURE; - } - - chmod(PHPDBG_G(socket_path), 0666); - - listen(sl, 2); - - rlen = sizeof(remote); - sr = accept(sl, (struct sockaddr *) &remote, (socklen_t *) &rlen); - - char msglen[5]; - int recvd = 4; - - do { - recvd -= recv(sr, &(msglen[4 - recvd]), recvd, 0); - } while (recvd > 0); - - recvd = *(size_t *) msglen; - char *data = emalloc(recvd); - - do { - recvd -= recv(sr, &(data[(*(int *) msglen) - recvd]), recvd, 0); - } while (recvd > 0); - - phpdbg_webdata_decompress(data, *(int *) msglen TSRMLS_CC); - - efree(data); - - phpdbg_notice("Successfully imported request data, stopped before executing"); - - return SUCCESS; -} /* }}} */ - int phpdbg_interactive(TSRMLS_D) /* {{{ */ { int ret = SUCCESS; diff --git a/phpdbg_rinit_hook.h b/phpdbg_rinit_hook.h index 1c6e92971e8..d28be10f8c4 100644 --- a/phpdbg_rinit_hook.h +++ b/phpdbg_rinit_hook.h @@ -21,8 +21,6 @@ #ifndef PHPDBG_WEBHELPER_H #define PHPDBG_WEBHELPER_H -#define phpdbg_notice(...) - #include "phpdbg_webdata_transfer.h" extern zend_module_entry phpdbg_webhelper_module_entry; diff --git a/phpdbg_wait.c b/phpdbg_wait.c new file mode 100644 index 00000000000..d369dedf106 --- /dev/null +++ b/phpdbg_wait.c @@ -0,0 +1,392 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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 | + +----------------------------------------------------------------------+ +*/ + +#include "phpdbg_wait.h" +#include "phpdbg_prompt.h" +#include "ext/json/php_json.h" +#include "ext/standard/basic_functions.h" + +ZEND_EXTERN_MODULE_GLOBALS(phpdbg); + +static void phpdbg_rebuild_http_globals_array(int type, const char *name TSRMLS_DC) { + zval **zvpp; + if (PG(http_globals)[type]) { + zval_dtor(PG(http_globals)[type]); + } + if (zend_hash_find(&EG(symbol_table), name, strlen(name) + 1, (void **) &zvpp) == SUCCESS) { + Z_SET_REFCOUNT_PP(zvpp, 2); + PG(http_globals)[type] = *zvpp; + } +} + +/* +static int phpdbg_remove_rearm_autoglobals(zend_auto_global *auto_global TSRMLS_DC) { +// zend_hash_del(&EG(symbol_table), auto_global->name, auto_global->name_len + 1); + + return 1; +}*/ + +typedef struct { + HashTable *ht[2]; + HashPosition pos[2]; +} phpdbg_intersect_ptr; + +static int phpdbg_array_data_compare(const void *a, const void *b TSRMLS_DC) { + Bucket *f, *s; + zval result; + zval *first, *second; + + f = *((Bucket **) a); + s = *((Bucket **) b); + + first = *((zval **) f->pData); + second = *((zval **) s->pData); + + if (string_compare_function(&result, first, second TSRMLS_CC) == FAILURE) { + return 0; + } + + if (Z_LVAL(result) < 0) { + return -1; + } else if (Z_LVAL(result) > 0) { + return 1; + } + + return 0; +} + +static void phpdbg_array_intersect_init(phpdbg_intersect_ptr *info, HashTable *ht1, HashTable *ht2 TSRMLS_DC) { + info->ht[0] = ht1; + info->ht[1] = ht2; + + zend_hash_sort(info->ht[0], zend_qsort, (compare_func_t) phpdbg_array_data_compare, 0 TSRMLS_CC); + zend_hash_sort(info->ht[1], zend_qsort, (compare_func_t) phpdbg_array_data_compare, 0 TSRMLS_CC); + + zend_hash_internal_pointer_reset_ex(info->ht[0], &info->pos[0]); + zend_hash_internal_pointer_reset_ex(info->ht[1], &info->pos[1]); +} + +/* -1 => first array, 0 => both arrays equal, 1 => second array */ +static int phpdbg_array_intersect(phpdbg_intersect_ptr *info, zval ***ptr) { + int ret; + zval **zvpp[2]; + int invalid = !info->ht[0] + !info->ht[1]; + + if (invalid > 0) { + invalid = !info->ht[0]; + + if (zend_hash_get_current_data_ex(info->ht[invalid], (void **) ptr, &info->pos[invalid]) == FAILURE) { + *ptr = NULL; + return 0; + } + + zend_hash_move_forward_ex(info->ht[invalid], &info->pos[invalid]); + + return invalid ? -1 : 1; + } + + if (zend_hash_get_current_data_ex(info->ht[0], (void **) &zvpp[0], &info->pos[0]) == FAILURE) { + info->ht[0] = NULL; + return phpdbg_array_intersect(info, ptr); + } + if (zend_hash_get_current_data_ex(info->ht[1], (void **) &zvpp[1], &info->pos[1]) == FAILURE) { + info->ht[1] = NULL; + return phpdbg_array_intersect(info, ptr); + } + + ret = zend_binary_zval_strcmp(*zvpp[0], *zvpp[1]); + + if (ret <= 0) { + *ptr = zvpp[0]; + zend_hash_move_forward_ex(info->ht[0], &info->pos[0]); + } + if (ret >= 0) { + *ptr = zvpp[1]; + zend_hash_move_forward_ex(info->ht[1], &info->pos[1]); + } + + return ret; +} + +void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { + zval *free_zv = NULL; + zval zv, **zvpp; + HashTable *ht; + php_json_decode(&zv, msg, len, 1, 1000 /* enough */ TSRMLS_CC); + ht = Z_ARRVAL(zv); + + /* Reapply symbol table */ + if (zend_hash_find(ht, "GLOBALS", sizeof("GLOBALS"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + zend_hash_clean(&EG(symbol_table)); + EG(symbol_table) = *Z_ARRVAL_PP(zvpp); + + { + zval **srv; + if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &srv) == SUCCESS && Z_TYPE_PP(srv) == IS_ARRAY) { + zval **script; + if (zend_hash_find(Z_ARRVAL_PP(srv), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &script) == SUCCESS && Z_TYPE_PP(script) == IS_STRING) { + phpdbg_param_t param; + param.str = Z_STRVAL_PP(script); + PHPDBG_COMMAND_HANDLER(exec)(¶m TSRMLS_CC); + } + } + } + + /* Rebuild cookies, env vars etc. from GLOBALS (PG(http_globals)) */ + phpdbg_rebuild_http_globals_array(TRACK_VARS_POST, "_POST" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_GET, "_GET" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_COOKIE, "_COOKIE" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_SERVER, "_SERVER" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_ENV, "_ENV" TSRMLS_CC); + phpdbg_rebuild_http_globals_array(TRACK_VARS_FILES, "_FILES" TSRMLS_CC); + + Z_ADDREF_PP(zvpp); + free_zv = *zvpp; + } + + if (zend_hash_find(ht, "input", sizeof("input"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { + if (SG(request_info).request_body) { + php_stream_close(SG(request_info).request_body); + } + SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); + php_stream_truncate_set_size(SG(request_info).request_body, 0); + php_stream_write(SG(request_info).request_body, Z_STRVAL_PP(zvpp), Z_STRLEN_PP(zvpp)); + } + + if (zend_hash_find(ht, "cwd", sizeof("cwd"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { + if (VCWD_CHDIR(Z_STRVAL_PP(zvpp)) == SUCCESS) { + if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentStatFile), strlen(BG(CurrentStatFile)))) { + efree(BG(CurrentStatFile)); + BG(CurrentStatFile) = NULL; + } + if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentLStatFile), strlen(BG(CurrentLStatFile)))) { + efree(BG(CurrentLStatFile)); + BG(CurrentLStatFile) = NULL; + } + } + } + + if (zend_hash_find(ht, "sapi_name", sizeof("sapi_name"), (void **) &zvpp) == SUCCESS && (Z_TYPE_PP(zvpp) == IS_STRING || Z_TYPE_PP(zvpp) == IS_NULL)) { + if (PHPDBG_G(sapi_name_ptr)) { + free(PHPDBG_G(sapi_name_ptr)); + } + if (Z_TYPE_PP(zvpp) == IS_STRING) { + PHPDBG_G(sapi_name_ptr) = sapi_module.name = strdup(Z_STRVAL_PP(zvpp)); + } else { + PHPDBG_G(sapi_name_ptr) = sapi_module.name = NULL; + } + } + + if (zend_hash_find(ht, "modules", sizeof("modules"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + phpdbg_intersect_ptr pos; + zval **module; + zend_module_entry *mod; + HashTable zv_registry; + + /* intersect modules, unregister modules loaded "too much", announce not yet registered modules (phpdbg_notice) */ + + zend_hash_init(&zv_registry, zend_hash_num_elements(&module_registry), 0, ZVAL_PTR_DTOR, 0); + for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); + zend_hash_get_current_data_ex(&module_registry, (void **) &mod, &position) == SUCCESS; + zend_hash_move_forward_ex(&module_registry, &position)) { + if (mod->name && (!sapi_module.name || strcmp(sapi_module.name, mod->name))) { + zval **value = emalloc(sizeof(zval *)); + MAKE_STD_ZVAL(*value); + ZVAL_STRING(*value, mod->name, 1); + zend_hash_next_index_insert(&zv_registry, value, sizeof(zval *), NULL); + } + } + + phpdbg_array_intersect_init(&pos, &zv_registry, Z_ARRVAL_PP(zvpp) TSRMLS_CC); + do { + int mode = phpdbg_array_intersect(&pos, &module); + if (mode < 0) { + // loaded module, but not needed + zend_hash_del(&module_registry, Z_STRVAL_PP(module), Z_STRLEN_PP(module) + 1); + } else if (mode > 0) { + // not loaded module + phpdbg_notice("The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module)); + } + } while (module); + + zend_hash_clean(&zv_registry); + } + + if (zend_hash_find(ht, "extensions", sizeof("extensions"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + zend_extension *extension; + zend_llist_position pos; + HashPosition hpos; + zval **name, key; + + extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); + while (extension) { + extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); + + /* php_serach_array() body should be in some ZEND_API function */ + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { + if (Z_TYPE_PP(name) == IS_STRING && !zend_binary_strcmp(extension->name, strlen(extension->name), Z_STRVAL_PP(name), Z_STRLEN_PP(name))) { + break; + } + } + + if (zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &zvpp, &hpos) == FAILURE) { + /* sigh, breaking the encapsulation, there aren't any functions manipulating the llist at the place of the zend_llist_position */ + zend_llist_element *elm = pos; + if (elm->prev) { + elm->prev->next = elm->next; + } else { + zend_extensions.head = elm->next; + } + if (elm->next) { + elm->next->prev = elm->prev; + } else { + zend_extensions.tail = elm->prev; + } +#if ZEND_EXTENSIONS_SUPPORT + if (extension->shutdown) { + extension->shutdown(extension); + } +#endif + if (zend_extensions.dtor) { + zend_extensions.dtor(elm->data); + } + pefree(elm, zend_extensions.persistent); + zend_extensions.count--; + } else { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &hpos); + if (Z_TYPE(key) == IS_LONG) { + zend_hash_index_del(Z_ARRVAL_PP(zvpp), Z_LVAL(key)); + } + } + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { + phpdbg_notice("The (zend) extension %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/extension.so", Z_STRLEN_PP(name), Z_STRVAL_PP(name)); + } + } + } + + zend_ini_deactivate(TSRMLS_C); + + if (zend_hash_find(ht, "systemini", sizeof("systemini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + zval **ini_entry; + zend_ini_entry *original_ini; + zval key; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); + if (Z_TYPE(key) == IS_STRING) { + if (Z_TYPE_PP(ini_entry) == IS_STRING) { + if (zend_hash_find(EG(ini_directives), Z_STRVAL(key), Z_STRLEN(key) + 1, (void **) &original_ini) == SUCCESS) { + if (!original_ini->on_modify || original_ini->on_modify(original_ini, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), original_ini->mh_arg1, original_ini->mh_arg2, original_ini->mh_arg3, ZEND_INI_STAGE_ACTIVATE TSRMLS_CC) == SUCCESS) { + original_ini->value = Z_STRVAL_PP(ini_entry); + original_ini->value_length = Z_STRLEN_PP(ini_entry); + continue; /* don't free the string */ + } + } + } + efree(Z_STRVAL(key)); + } + } + } + + if (zend_hash_find(ht, "userini", sizeof("userini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { + HashPosition position; + zval **ini_entry; + zval key; + + for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); + zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { + zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); + if (Z_TYPE(key) == IS_STRING) { + if (Z_TYPE_PP(ini_entry) == IS_STRING) { + zend_alter_ini_entry_ex(Z_STRVAL(key), Z_STRLEN(key) + 1, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), ZEND_INI_PERDIR, ZEND_INI_STAGE_HTACCESS, 1 TSRMLS_CC); + } + efree(Z_STRVAL(key)); + } + } + } + + zval_dtor(&zv); + if (free_zv) { + /* separate freeing to not dtor the symtable too, just the container zval... */ + efree(free_zv); + } + + /* Remove and readd autoglobals */ +/* php_hash_environment(TSRMLS_C); + zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_remove_rearm_autoglobals TSRMLS_CC); + php_startup_auto_globals(TSRMLS_C); + zend_activate_auto_globals(TSRMLS_C); +*/ + /* Reapply raw input */ + /* ??? */ +} + +PHPDBG_COMMAND(wait) /* {{{ */ +{ + struct sockaddr_un local, remote; + int rlen, len, sr, sl = socket(AF_UNIX, SOCK_STREAM, 0); + unlink(PHPDBG_G(socket_path)); + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, PHPDBG_G(socket_path)); + len = strlen(local.sun_path) + sizeof(local.sun_family); + if (bind(sl, (struct sockaddr *)&local, len) == -1) { + phpdbg_error("Unable to connect to UNIX domain socket at %s defined by phpdbg.path ini setting", PHPDBG_G(socket_path)); + return FAILURE; + } + + chmod(PHPDBG_G(socket_path), 0666); + + listen(sl, 2); + + rlen = sizeof(remote); + sr = accept(sl, (struct sockaddr *) &remote, (socklen_t *) &rlen); + + char msglen[5]; + int recvd = 4; + + do { + recvd -= recv(sr, &(msglen[4 - recvd]), recvd, 0); + } while (recvd > 0); + + recvd = *(size_t *) msglen; + char *data = emalloc(recvd); + + do { + recvd -= recv(sr, &(data[(*(int *) msglen) - recvd]), recvd, 0); + } while (recvd > 0); + + phpdbg_webdata_decompress(data, *(int *) msglen TSRMLS_CC); + + efree(data); + + phpdbg_notice("Successfully imported request data, stopped before executing"); + + return SUCCESS; +} /* }}} */ + diff --git a/phpdbg_wait.h b/phpdbg_wait.h new file mode 100644 index 00000000000..7cf95919ccb --- /dev/null +++ b/phpdbg_wait.h @@ -0,0 +1,29 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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 | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHPDBG_WAIT_H +#define PHPDBG_WAIT_H + +#include "zend.h" +#include "phpdbg.h" + +PHPDBG_COMMAND(wait); + +void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC); + +#endif /* PHPDBG_WAIT_H */ diff --git a/phpdbg_webdata_transfer.c b/phpdbg_webdata_transfer.c new file mode 100644 index 00000000000..49782e59e2d --- /dev/null +++ b/phpdbg_webdata_transfer.c @@ -0,0 +1,180 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 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 | + +----------------------------------------------------------------------+ +*/ + +#include "phpdbg_webdata_transfer.h" +#include "ext/json/php_json.h" + +PHPDBG_API void phpdbg_webdata_compress(char **msg, int *len TSRMLS_DC) { + smart_str buf = {0}; + zval array; + HashTable *ht; + /* I really need to change that to an array of zvals... */ + zval zv1, *zvp1 = &zv1; + zval zv2, *zvp2 = &zv2; + zval zv3, *zvp3 = &zv3; + zval zv4, *zvp4 = &zv4; + zval zv5, *zvp5 = &zv5; + zval zv6, *zvp6 = &zv6; + zval zv7, *zvp7 = &zv7; + zval zv8, *zvp8 = &zv8; + + array_init(&array); + ht = Z_ARRVAL(array); + + /* fetch superglobals */ + { + zend_is_auto_global(ZEND_STRL("GLOBALS") TSRMLS_CC); + /* might be JIT */ + zend_is_auto_global(ZEND_STRL("_ENV") TSRMLS_CC); + zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC); + zend_is_auto_global(ZEND_STRL("_REQUEST") TSRMLS_CC); + array_init(&zv1); + zend_hash_copy(Z_ARRVAL(zv1), &EG(symbol_table), NULL, (void *) NULL, sizeof(zval *)); + Z_ARRVAL(zv1)->pDestructor = NULL; /* we're operating on a copy! Don't double free zvals */ + zend_hash_del(Z_ARRVAL(zv1), "GLOBALS", sizeof("GLOBALS")); /* do not use the reference to itself in json */ + zend_hash_add(ht, "GLOBALS", sizeof("GLOBALS"), &zvp1, sizeof(zval *), NULL); + } + + /* save php://input */ + { + php_stream *stream; + int len; + char *contents; + + stream = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); + if ((len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0)) > 0) { + ZVAL_STRINGL(&zv2, contents, len, 0); + } else { + ZVAL_EMPTY_STRING(&zv2); + } + zend_hash_add(ht, "input", sizeof("input"), &zvp2, sizeof(zval *), NULL); + } + + /* change sapi name */ + { + if (sapi_module.name) { + ZVAL_STRING(&zv6, sapi_module.name, 1); + } else { + Z_TYPE(zv6) = IS_NULL; + } + zend_hash_add(ht, "sapi_name", sizeof("sapi_name"), &zvp6, sizeof(zval *), NULL); + } + + /* handle modules / extensions */ + { + HashPosition position; + zend_module_entry *module; + zend_extension *extension; + zend_llist_position pos; + + array_init(&zv7); + for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); + zend_hash_get_current_data_ex(&module_registry, (void**) &module, &position) == SUCCESS; + zend_hash_move_forward_ex(&module_registry, &position)) { + zval **value = emalloc(sizeof(zval *)); + ALLOC_ZVAL(*value); + ZVAL_STRING(*value, module->name, 1); + zend_hash_next_index_insert(Z_ARRVAL(zv7), value, sizeof(zval *), NULL); + } + zend_hash_add(ht, "modules", sizeof("modules"), &zvp7, sizeof(zval *), NULL); + + array_init(&zv8); + extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); + while (extension) { + zval **value = emalloc(sizeof(zval *)); + ALLOC_ZVAL(*value); + ZVAL_STRING(*value, extension->name, 1); + zend_hash_next_index_insert(Z_ARRVAL(zv8), value, sizeof(zval *), NULL); + extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); + } + zend_hash_add(ht, "extensions", sizeof("extensions"), &zvp8, sizeof(zval *), NULL); + } + + /* switch cwd */ + { + char *ret = NULL; + char path[MAXPATHLEN]; + +#if HAVE_GETCWD + ret = VCWD_GETCWD(path, MAXPATHLEN); +#elif HAVE_GETWD + ret = VCWD_GETWD(path); +#endif + if (ret) { + ZVAL_STRING(&zv5, path, 1); + zend_hash_add(ht, "cwd", sizeof("cwd"), &zvp5, sizeof(zval *), NULL); + } + } + + /* get system ini entries */ + { + HashPosition position; + zend_ini_entry *ini_entry; + + array_init(&zv3); + for (zend_hash_internal_pointer_reset_ex(EG(ini_directives), &position); + zend_hash_get_current_data_ex(EG(ini_directives), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(EG(ini_directives), &position)) { + zval **value = emalloc(sizeof(zval *)); + if (ini_entry->modified) { + if (!ini_entry->orig_value) { + efree(value); + continue; + } + ALLOC_ZVAL(*value); + ZVAL_STRINGL(*value, ini_entry->orig_value, ini_entry->orig_value_length, 1); + } else { + if (!ini_entry->value) { + efree(value); + continue; + } + ALLOC_ZVAL(*value); + ZVAL_STRINGL(*value, ini_entry->value, ini_entry->value_length, 1); + } + zend_hash_add(Z_ARRVAL(zv3), ini_entry->name, ini_entry->name_length, value, sizeof(zval *), NULL); + } + zend_hash_add(ht, "systemini", sizeof("systemini"), &zvp3, sizeof(zval *), NULL); + } + + /* get perdir ini entries */ + if (EG(modified_ini_directives)) { + HashPosition position; + zend_ini_entry *ini_entry; + + array_init(&zv4); + for (zend_hash_internal_pointer_reset_ex(EG(modified_ini_directives), &position); + zend_hash_get_current_data_ex(EG(modified_ini_directives), (void**) &ini_entry, &position) == SUCCESS; + zend_hash_move_forward_ex(EG(modified_ini_directives), &position)) { + zval **value = emalloc(sizeof(zval *)); + if (!ini_entry->value) { + efree(value); + continue; + } + ALLOC_ZVAL(*value); + ZVAL_STRINGL(*value, ini_entry->value, ini_entry->value_length, 1); + zend_hash_add(Z_ARRVAL(zv4), ini_entry->name, ini_entry->name_length, value, sizeof(zval *), NULL); + } + zend_hash_add(ht, "userini", sizeof("userini"), &zvp4, sizeof(zval *), NULL); + } + + /* encode data */ + php_json_encode(&buf, &array, 0 TSRMLS_CC); + *msg = buf.c; + *len = buf.len; + zval_dtor(&array); +} diff --git a/phpdbg_webdata_transfer.h b/phpdbg_webdata_transfer.h index 4bcfb0c8e2e..d70175ad99d 100644 --- a/phpdbg_webdata_transfer.h +++ b/phpdbg_webdata_transfer.h @@ -19,462 +19,9 @@ #ifndef PHPDBG_WEBDATA_TRANSFER_H #define PHPDBG_WEBDATA_TRANSFER_H -/* {{{ remote console headers */ -#ifndef _WIN32 -# include -# include -# include -# include -#endif /* }}} */ - #include "zend.h" #include "phpdbg.h" -#include "ext/json/php_json.h" -#include "ext/standard/basic_functions.h" -static inline void phpdbg_webdata_compress(char **msg, int *len TSRMLS_DC) { - smart_str buf = {0}; - zval array; - HashTable *ht; - /* I really need to change that to an array of zvals... */ - zval zv1, *zvp1 = &zv1; - zval zv2, *zvp2 = &zv2; - zval zv3, *zvp3 = &zv3; - zval zv4, *zvp4 = &zv4; - zval zv5, *zvp5 = &zv5; - zval zv6, *zvp6 = &zv6; - zval zv7, *zvp7 = &zv7; - zval zv8, *zvp8 = &zv8; - - array_init(&array); - ht = Z_ARRVAL(array); - - /* fetch superglobals */ - { - zend_is_auto_global(ZEND_STRL("GLOBALS") TSRMLS_CC); - /* might be JIT */ - zend_is_auto_global(ZEND_STRL("_ENV") TSRMLS_CC); - zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC); - zend_is_auto_global(ZEND_STRL("_REQUEST") TSRMLS_CC); - array_init(&zv1); - zend_hash_copy(Z_ARRVAL(zv1), &EG(symbol_table), NULL, (void *) NULL, sizeof(zval *)); - Z_ARRVAL(zv1)->pDestructor = NULL; /* we're operating on a copy! Don't double free zvals */ - zend_hash_del(Z_ARRVAL(zv1), "GLOBALS", sizeof("GLOBALS")); /* do not use the reference to itself in json */ - zend_hash_add(ht, "GLOBALS", sizeof("GLOBALS"), &zvp1, sizeof(zval *), NULL); - } - - /* save php://input */ - { - php_stream *stream; - int len; - char *contents; - - stream = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); - if ((len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0)) > 0) { - ZVAL_STRINGL(&zv2, contents, len, 0); - } else { - ZVAL_EMPTY_STRING(&zv2); - } - zend_hash_add(ht, "input", sizeof("input"), &zvp2, sizeof(zval *), NULL); - } - - /* change sapi name */ - { - if (sapi_module.name) { - ZVAL_STRING(&zv6, sapi_module.name, 1); - } else { - Z_TYPE(zv6) = IS_NULL; - } - zend_hash_add(ht, "sapi_name", sizeof("sapi_name"), &zvp6, sizeof(zval *), NULL); - } - - /* handle modules / extensions */ - { - HashPosition position; - zend_module_entry *module; - zend_extension *extension; - zend_llist_position pos; - - array_init(&zv7); - for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); - zend_hash_get_current_data_ex(&module_registry, (void**) &module, &position) == SUCCESS; - zend_hash_move_forward_ex(&module_registry, &position)) { - zval **value = emalloc(sizeof(zval *)); - ALLOC_ZVAL(*value); - ZVAL_STRING(*value, module->name, 1); - zend_hash_next_index_insert(Z_ARRVAL(zv7), value, sizeof(zval *), NULL); - } - zend_hash_add(ht, "modules", sizeof("modules"), &zvp7, sizeof(zval *), NULL); - - array_init(&zv8); - extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); - while (extension) { - zval **value = emalloc(sizeof(zval *)); - ALLOC_ZVAL(*value); - ZVAL_STRING(*value, extension->name, 1); - zend_hash_next_index_insert(Z_ARRVAL(zv8), value, sizeof(zval *), NULL); - extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); - } - zend_hash_add(ht, "extensions", sizeof("extensions"), &zvp8, sizeof(zval *), NULL); - } - - /* switch cwd */ - { - char *ret = NULL; - char path[MAXPATHLEN]; - -#if HAVE_GETCWD - ret = VCWD_GETCWD(path, MAXPATHLEN); -#elif HAVE_GETWD - ret = VCWD_GETWD(path); -#endif - if (ret) { - ZVAL_STRING(&zv5, path, 1); - zend_hash_add(ht, "cwd", sizeof("cwd"), &zvp5, sizeof(zval *), NULL); - } - } - - /* get system ini entries */ - { - HashPosition position; - zend_ini_entry *ini_entry; - - array_init(&zv3); - for (zend_hash_internal_pointer_reset_ex(EG(ini_directives), &position); - zend_hash_get_current_data_ex(EG(ini_directives), (void**) &ini_entry, &position) == SUCCESS; - zend_hash_move_forward_ex(EG(ini_directives), &position)) { - zval **value = emalloc(sizeof(zval *)); - if (ini_entry->modified) { - if (!ini_entry->orig_value) { - efree(value); - continue; - } - ALLOC_ZVAL(*value); - ZVAL_STRINGL(*value, ini_entry->orig_value, ini_entry->orig_value_length, 1); - } else { - if (!ini_entry->value) { - efree(value); - continue; - } - ALLOC_ZVAL(*value); - ZVAL_STRINGL(*value, ini_entry->value, ini_entry->value_length, 1); - } - zend_hash_add(Z_ARRVAL(zv3), ini_entry->name, ini_entry->name_length, value, sizeof(zval *), NULL); - } - zend_hash_add(ht, "systemini", sizeof("systemini"), &zvp3, sizeof(zval *), NULL); - } - - /* get perdir ini entries */ - if (EG(modified_ini_directives)) { - HashPosition position; - zend_ini_entry *ini_entry; - - array_init(&zv4); - for (zend_hash_internal_pointer_reset_ex(EG(modified_ini_directives), &position); - zend_hash_get_current_data_ex(EG(modified_ini_directives), (void**) &ini_entry, &position) == SUCCESS; - zend_hash_move_forward_ex(EG(modified_ini_directives), &position)) { - zval **value = emalloc(sizeof(zval *)); - if (!ini_entry->value) { - efree(value); - continue; - } - ALLOC_ZVAL(*value); - ZVAL_STRINGL(*value, ini_entry->value, ini_entry->value_length, 1); - zend_hash_add(Z_ARRVAL(zv4), ini_entry->name, ini_entry->name_length, value, sizeof(zval *), NULL); - } - zend_hash_add(ht, "userini", sizeof("userini"), &zvp4, sizeof(zval *), NULL); - } - - /* encode data */ - php_json_encode(&buf, &array, 0 TSRMLS_CC); - *msg = buf.c; - *len = buf.len; - zval_dtor(&array); -} - -static void phpdbg_rebuild_http_globals_array(int type, const char *name TSRMLS_DC) { - zval **zvpp; - if (PG(http_globals)[type]) { - zval_dtor(PG(http_globals)[type]); - } - if (zend_hash_find(&EG(symbol_table), name, strlen(name) + 1, (void **) &zvpp) == SUCCESS) { - Z_SET_REFCOUNT_PP(zvpp, 2); - PG(http_globals)[type] = *zvpp; - } -} - -/* -static int phpdbg_remove_rearm_autoglobals(zend_auto_global *auto_global TSRMLS_DC) { -// zend_hash_del(&EG(symbol_table), auto_global->name, auto_global->name_len + 1); - - return 1; -}*/ - -typedef struct { - HashTable *ht[2]; - HashPosition pos[2]; -} phpdbg_intersect_ptr; - -static void phpdbg_array_intersect_init(phpdbg_intersect_ptr *info, HashTable *ht1, HashTable *ht2 TSRMLS_DC) { - info->ht[0] = ht1; - info->ht[1] = ht2; - - zend_hash_sort(info->ht[0], zend_qsort, (compare_func_t) string_compare_function, 0 TSRMLS_CC); - zend_hash_sort(info->ht[1], zend_qsort, (compare_func_t) string_compare_function, 0 TSRMLS_CC); - - zend_hash_internal_pointer_reset_ex(info->ht[0], &info->pos[0]); - zend_hash_internal_pointer_reset_ex(info->ht[1], &info->pos[1]); -} - -/* -1 => first array, 0 => both arrays equal, 1 => second array */ -static int phpdbg_array_intersect(phpdbg_intersect_ptr *info, zval ***ptr) { - int ret; - zval **zvpp[2]; - int invalid = !info->ht[0] + !info->ht[1]; - - if (invalid > 0) { - invalid = !!info->ht[0]; - - if (zend_hash_get_current_data_ex(info->ht[invalid], (void **) ptr, &info->pos[invalid]) == FAILURE) { - *ptr = NULL; - return 0; - } - - zend_hash_move_forward_ex(info->ht[invalid], &info->pos[invalid]); - - return invalid ? -1 : 1; - } - - if (zend_hash_get_current_data_ex(info->ht[0], (void **) &zvpp[0], &info->pos[0]) == FAILURE) { - info->ht[0] = NULL; - return phpdbg_array_intersect(info, ptr); - } - if (zend_hash_get_current_data_ex(info->ht[1], (void **) &zvpp[1], &info->pos[1]) == FAILURE) { - info->ht[1] = NULL; - return phpdbg_array_intersect(info, ptr); - } - - ret = zend_binary_zval_strcmp(*zvpp[0], *zvpp[1]); - - if (ret <= 0) { - *ptr = zvpp[0]; - zend_hash_move_forward_ex(info->ht[0], &info->pos[0]); - } - if (ret >= 0) { - *ptr = zvpp[1]; - zend_hash_move_forward_ex(info->ht[1], &info->pos[1]); - } - - return ret; -} - -static inline void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { - zval *free_zv = NULL; - zval zv, **zvpp; - HashTable *ht; - php_json_decode(&zv, msg, len, 1, 1000 /* enough */ TSRMLS_CC); - ht = Z_ARRVAL(zv); - - /* Reapply symbol table */ - if (zend_hash_find(ht, "GLOBALS", sizeof("GLOBALS"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { - zend_hash_clean(&EG(symbol_table)); - EG(symbol_table) = *Z_ARRVAL_PP(zvpp); - - /* Rebuild cookies, env vars etc. from GLOBALS (PG(http_globals)) */ - phpdbg_rebuild_http_globals_array(TRACK_VARS_POST, "_POST" TSRMLS_CC); - phpdbg_rebuild_http_globals_array(TRACK_VARS_GET, "_GET" TSRMLS_CC); - phpdbg_rebuild_http_globals_array(TRACK_VARS_COOKIE, "_COOKIE" TSRMLS_CC); - phpdbg_rebuild_http_globals_array(TRACK_VARS_SERVER, "_SERVER" TSRMLS_CC); - phpdbg_rebuild_http_globals_array(TRACK_VARS_ENV, "_ENV" TSRMLS_CC); - phpdbg_rebuild_http_globals_array(TRACK_VARS_FILES, "_FILES" TSRMLS_CC); - - Z_ADDREF_PP(zvpp); - free_zv = *zvpp; - } - - if (zend_hash_find(ht, "input", sizeof("input"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { - if (SG(request_info).request_body) { - php_stream_close(SG(request_info).request_body); - } - SG(request_info).request_body = php_stream_temp_create_ex(TEMP_STREAM_DEFAULT, SAPI_POST_BLOCK_SIZE, PG(upload_tmp_dir)); - php_stream_truncate_set_size(SG(request_info).request_body, 0); - php_stream_write(SG(request_info).request_body, Z_STRVAL_PP(zvpp), Z_STRLEN_PP(zvpp)); - } - - if (zend_hash_find(ht, "cwd", sizeof("cwd"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_STRING) { - if (VCWD_CHDIR(Z_STRVAL_PP(zvpp)) == SUCCESS) { - if (BG(CurrentStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentStatFile), strlen(BG(CurrentStatFile)))) { - efree(BG(CurrentStatFile)); - BG(CurrentStatFile) = NULL; - } - if (BG(CurrentLStatFile) && !IS_ABSOLUTE_PATH(BG(CurrentLStatFile), strlen(BG(CurrentLStatFile)))) { - efree(BG(CurrentLStatFile)); - BG(CurrentLStatFile) = NULL; - } - } - } - - if (zend_hash_find(ht, "sapi_name", sizeof("sapi_name"), (void **) &zvpp) == SUCCESS && (Z_TYPE_PP(zvpp) == IS_STRING || Z_TYPE_PP(zvpp) == IS_NULL)) { - if (sapi_module.name) { - efree(sapi_module.name); - } - if (Z_TYPE_PP(zvpp) == IS_STRING) { - sapi_module.name = estrndup(Z_STRVAL_PP(zvpp), Z_STRLEN_PP(zvpp)); - } else { - sapi_module.name = NULL; - } - } - - if (zend_hash_find(ht, "modules", sizeof("modules"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { - HashPosition position; - phpdbg_intersect_ptr pos; - zval **module; - zend_module_entry *mod; - HashTable zv_registry; - - /* intersect modules, unregister mpdules loaded "too much", announce not yet registered modules (phpdbg_notice) */ - - zend_hash_init(&zv_registry, zend_hash_num_elements(&zv_registry), 0, ZVAL_PTR_DTOR, 0); - for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); - zend_hash_get_current_data_ex(&module_registry, (void **) &mod, &position) == SUCCESS; - zend_hash_move_forward_ex(&module_registry, &position)) { - zval **value = emalloc(sizeof(zval *)); - ALLOC_ZVAL(*value); - ZVAL_STRING(*value, mod->name, 1); - zend_hash_next_index_insert(&zv_registry, value, sizeof(zval *), NULL); - } - - phpdbg_array_intersect_init(&pos, &zv_registry, Z_ARRVAL_PP(zvpp) TSRMLS_CC); - do { - int mode = phpdbg_array_intersect(&pos, &module); - if (mode < 0) { - // loaded module, but not needed - zend_hash_del(&module_registry, Z_STRVAL_PP(module), Z_STRLEN_PP(module) + 1); - } else if (mode > 0) { - // not loaded module - phpdbg_notice("The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module)); - } - } while (module); - - zend_hash_clean(&zv_registry); - } - - if (zend_hash_find(ht, "extensions", sizeof("extensions"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { - zend_extension *extension; - zend_llist_position pos; - HashPosition hpos; - zval **name, key; - - extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos); - while (extension) { - extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos); - - /* php_serach_array() body should be in some ZEND_API function */ - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); - zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { - if (Z_TYPE_PP(name) == IS_STRING && !zend_binary_strcmp(extension->name, strlen(extension->name), Z_STRVAL_PP(name), Z_STRLEN_PP(name))) { - break; - } - } - - if (zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &zvpp, &hpos) == FAILURE) { - /* sigh, breaking the encapsulation, there aren't any functions manipulating the llist at the place of the zend_llist_position */ - zend_llist_element *elm = pos; - if (elm->prev) { - elm->prev->next = elm->next; - } else { - zend_extensions.head = elm->next; - } - if (elm->next) { - elm->next->prev = elm->prev; - } else { - zend_extensions.tail = elm->prev; - } -#if ZEND_EXTENSIONS_SUPPORT - if (extension->shutdown) { - extension->shutdown(extension); - } -#endif - if (zend_extensions.dtor) { - zend_extensions.dtor(elm->data); - } - pefree(elm, zend_extensions.persistent); - zend_extensions.count--; - } else { - zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &hpos); - if (Z_TYPE(key) == IS_LONG) { - zend_hash_index_del(Z_ARRVAL_PP(zvpp), Z_LVAL(key)); - } - } - - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &hpos); - zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void **) &name, &hpos) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &hpos)) { - phpdbg_notice("The (zend) extension %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/extension.so", Z_STRLEN_PP(name), Z_STRVAL_PP(name)); - } - } - } - - zend_ini_deactivate(TSRMLS_C); - - if (zend_hash_find(ht, "systemini", sizeof("systemini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { - HashPosition position; - zval **ini_entry; - zend_ini_entry *original_ini; - zval key; - - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); - zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { - zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); - if (Z_TYPE(key) == IS_STRING) { - if (Z_TYPE_PP(ini_entry) == IS_STRING) { - if (zend_hash_find(EG(ini_directives), Z_STRVAL(key), Z_STRLEN(key) + 1, (void **) &original_ini) == SUCCESS) { - if (!original_ini->on_modify || original_ini->on_modify(original_ini, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), original_ini->mh_arg1, original_ini->mh_arg2, original_ini->mh_arg3, ZEND_INI_STAGE_ACTIVATE TSRMLS_CC) == SUCCESS) { - original_ini->value = Z_STRVAL_PP(ini_entry); - original_ini->value_length = Z_STRLEN_PP(ini_entry); - continue; /* don't free the string */ - } - } - } - efree(Z_STRVAL(key)); - } - } - } - - if (zend_hash_find(ht, "userini", sizeof("userini"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { - HashPosition position; - zval **ini_entry; - zval key; - - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(zvpp), &position); - zend_hash_get_current_data_ex(Z_ARRVAL_PP(zvpp), (void**) &ini_entry, &position) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_PP(zvpp), &position)) { - zend_hash_get_current_key_zval_ex(Z_ARRVAL_PP(zvpp), &key, &position); - if (Z_TYPE(key) == IS_STRING) { - if (Z_TYPE_PP(ini_entry) == IS_STRING) { - zend_alter_ini_entry_ex(Z_STRVAL(key), Z_STRLEN(key) + 1, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), ZEND_INI_PERDIR, ZEND_INI_STAGE_HTACCESS, 1 TSRMLS_CC); - } - efree(Z_STRVAL(key)); - } - } - } - - zval_dtor(&zv); - if (free_zv) { - /* separate freeing to not dtor the symtable too, just the container zval... */ - efree(free_zv); - } - - /* Remove and readd autoglobals */ -/* php_hash_environment(TSRMLS_C); - zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_remove_rearm_autoglobals TSRMLS_CC); - php_startup_auto_globals(TSRMLS_C); - zend_activate_auto_globals(TSRMLS_C); -*/ - /* Reapply raw input */ - /* ??? */ -} +PHPDBG_API void phpdbg_webdata_compress(char **msg, int *len TSRMLS_DC); #endif /* PHPDBG_WEBDATA_TRANSFER_H */ From cccc75eef9dda2d73c385cc22e2e06b6926111ea Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Wed, 10 Sep 2014 17:01:08 +0200 Subject: [PATCH 3/5] Do not unload phpdbg module in any case --- phpdbg_wait.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/phpdbg_wait.c b/phpdbg_wait.c index d369dedf106..f01be306672 100644 --- a/phpdbg_wait.c +++ b/phpdbg_wait.c @@ -18,10 +18,11 @@ #include "phpdbg_wait.h" #include "phpdbg_prompt.h" -#include "ext/json/php_json.h" +#include "ext/json/JSON_parser.h" #include "ext/standard/basic_functions.h" ZEND_EXTERN_MODULE_GLOBALS(phpdbg); +ZEND_EXTERN_MODULE_GLOBALS(json); static void phpdbg_rebuild_http_globals_array(int type, const char *name TSRMLS_DC) { zval **zvpp; @@ -97,7 +98,7 @@ static int phpdbg_array_intersect(phpdbg_intersect_ptr *info, zval ***ptr) { zend_hash_move_forward_ex(info->ht[invalid], &info->pos[invalid]); - return invalid ? -1 : 1; + return invalid ? 1 : -1; } if (zend_hash_get_current_data_ex(info->ht[0], (void **) &zvpp[0], &info->pos[0]) == FAILURE) { @@ -128,6 +129,12 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { zval zv, **zvpp; HashTable *ht; php_json_decode(&zv, msg, len, 1, 1000 /* enough */ TSRMLS_CC); + + if (JSON_G(error_code) != PHP_JSON_ERROR_NONE) { + phpdbg_error("Malformed JSON was sent to this socket, arborting"); + return; + } + ht = Z_ARRVAL(zv); /* Reapply symbol table */ @@ -205,7 +212,7 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { for (zend_hash_internal_pointer_reset_ex(&module_registry, &position); zend_hash_get_current_data_ex(&module_registry, (void **) &mod, &position) == SUCCESS; zend_hash_move_forward_ex(&module_registry, &position)) { - if (mod->name && (!sapi_module.name || strcmp(sapi_module.name, mod->name))) { + if (mod->name) { zval **value = emalloc(sizeof(zval *)); MAKE_STD_ZVAL(*value); ZVAL_STRING(*value, mod->name, 1); @@ -218,10 +225,14 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { int mode = phpdbg_array_intersect(&pos, &module); if (mode < 0) { // loaded module, but not needed - zend_hash_del(&module_registry, Z_STRVAL_PP(module), Z_STRLEN_PP(module) + 1); + if (strcmp(PHPDBG_NAME, Z_STRVAL_PP(module))) { + zend_hash_del(&module_registry, Z_STRVAL_PP(module), Z_STRLEN_PP(module) + 1); + } } else if (mode > 0) { // not loaded module - phpdbg_notice("The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module)); + if (!sapi_module.name || strcmp(sapi_module.name, Z_STRVAL_PP(module))) { + phpdbg_notice("The module %.*s isn't present in " PHPDBG_NAME ", you still can load via dl /path/to/module/%.*s.so", Z_STRLEN_PP(module), Z_STRVAL_PP(module), Z_STRLEN_PP(module), Z_STRVAL_PP(module)); + } } } while (module); From f48fc67c86cebb4526cfa14c84530dd31ab2ff3f Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Sun, 5 Oct 2014 00:16:19 +0200 Subject: [PATCH 4/5] Fix (system) ini entry transfer --- phpdbg_wait.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phpdbg_wait.c b/phpdbg_wait.c index 286be365259..4b06700dc0d 100644 --- a/phpdbg_wait.c +++ b/phpdbg_wait.c @@ -312,9 +312,12 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { if (Z_TYPE_PP(ini_entry) == IS_STRING) { if (zend_hash_find(EG(ini_directives), Z_STRVAL(key), Z_STRLEN(key) + 1, (void **) &original_ini) == SUCCESS) { if (!original_ini->on_modify || original_ini->on_modify(original_ini, Z_STRVAL_PP(ini_entry), Z_STRLEN_PP(ini_entry), original_ini->mh_arg1, original_ini->mh_arg2, original_ini->mh_arg3, ZEND_INI_STAGE_ACTIVATE TSRMLS_CC) == SUCCESS) { + if (original_ini->modified && original_ini->orig_value != original_ini->value) { + efree(original_ini->value); + } original_ini->value = Z_STRVAL_PP(ini_entry); original_ini->value_length = Z_STRLEN_PP(ini_entry); - continue; /* don't free the string */ + Z_TYPE_PP(ini_entry) = IS_NULL; /* don't free the value */ } } } From c55dbad54672e0c8c8979f87f4a6250f7d11dc34 Mon Sep 17 00:00:00 2001 From: Bob Weinand Date: Sun, 5 Oct 2014 02:27:51 +0200 Subject: [PATCH 5/5] Disable automatic resetting of imported auto_globals --- phpdbg_wait.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/phpdbg_wait.c b/phpdbg_wait.c index 4b06700dc0d..e2bded08750 100644 --- a/phpdbg_wait.c +++ b/phpdbg_wait.c @@ -35,12 +35,14 @@ static void phpdbg_rebuild_http_globals_array(int type, const char *name TSRMLS_ } } -/* -static int phpdbg_remove_rearm_autoglobals(zend_auto_global *auto_global TSRMLS_DC) { -// zend_hash_del(&EG(symbol_table), auto_global->name, auto_global->name_len + 1); - return 1; -}*/ +static int phpdbg_dearm_autoglobals(zend_auto_global *auto_global TSRMLS_DC) { + if (auto_global->name_len != sizeof("GLOBALS") - 1 || memcmp(auto_global->name, "GLOBALS", sizeof("GLOBALS") - 1)) { + auto_global->armed = 0; + } + + return ZEND_HASH_APPLY_KEEP; +} typedef struct { HashTable *ht[2]; @@ -139,12 +141,9 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { /* Reapply symbol table */ if (zend_hash_find(ht, "GLOBALS", sizeof("GLOBALS"), (void **) &zvpp) == SUCCESS && Z_TYPE_PP(zvpp) == IS_ARRAY) { - zend_hash_clean(&EG(symbol_table)); - EG(symbol_table) = *Z_ARRVAL_PP(zvpp); - { zval **srv; - if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &srv) == SUCCESS && Z_TYPE_PP(srv) == IS_ARRAY) { + if (zend_hash_find(Z_ARRVAL_PP(zvpp), "_SERVER", sizeof("_SERVER"), (void **) &srv) == SUCCESS && Z_TYPE_PP(srv) == IS_ARRAY) { zval **script; if (zend_hash_find(Z_ARRVAL_PP(srv), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &script) == SUCCESS && Z_TYPE_PP(script) == IS_STRING) { phpdbg_param_t param; @@ -154,6 +153,12 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { } } + PG(auto_globals_jit) = 0; + zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_dearm_autoglobals TSRMLS_CC); + + zend_hash_clean(&EG(symbol_table)); + EG(symbol_table) = *Z_ARRVAL_PP(zvpp); + /* Rebuild cookies, env vars etc. from GLOBALS (PG(http_globals)) */ phpdbg_rebuild_http_globals_array(TRACK_VARS_POST, "_POST" TSRMLS_CC); phpdbg_rebuild_http_globals_array(TRACK_VARS_GET, "_GET" TSRMLS_CC); @@ -350,12 +355,6 @@ void phpdbg_webdata_decompress(char *msg, int len TSRMLS_DC) { efree(free_zv); } - /* Remove and readd autoglobals */ -/* php_hash_environment(TSRMLS_C); - zend_hash_apply(CG(auto_globals), (apply_func_t) phpdbg_remove_rearm_autoglobals TSRMLS_CC); - php_startup_auto_globals(TSRMLS_C); - zend_activate_auto_globals(TSRMLS_C); -*/ /* Reapply raw input */ /* ??? */ }