dl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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: Brian Schaffner <brian@tool.net> |
  16. | Shane Caraveo <shane@caraveo.com> |
  17. | Zeev Suraski <zeev@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "php.h"
  21. #include "dl.h"
  22. #include "php_globals.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "SAPI.h"
  26. #if defined(HAVE_LIBDL)
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #ifdef HAVE_STRING_H
  30. #include <string.h>
  31. #else
  32. #include <strings.h>
  33. #endif
  34. #ifdef PHP_WIN32
  35. #include "win32/param.h"
  36. #include "win32/winutil.h"
  37. #define GET_DL_ERROR() php_win_err()
  38. #else
  39. #include <sys/param.h>
  40. #define GET_DL_ERROR() DL_ERROR()
  41. #endif
  42. #endif /* defined(HAVE_LIBDL) */
  43. /* {{{ proto int dl(string extension_filename)
  44. Load a PHP extension at runtime */
  45. PHPAPI PHP_FUNCTION(dl)
  46. {
  47. char *filename;
  48. size_t filename_len;
  49. ZEND_PARSE_PARAMETERS_START(1, 1)
  50. Z_PARAM_STRING(filename, filename_len)
  51. ZEND_PARSE_PARAMETERS_END();
  52. if (!PG(enable_dl)) {
  53. php_error_docref(NULL, E_WARNING, "Dynamically loaded extensions aren't enabled");
  54. RETURN_FALSE;
  55. }
  56. if (filename_len >= MAXPATHLEN) {
  57. php_error_docref(NULL, E_WARNING, "File name exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  58. RETURN_FALSE;
  59. }
  60. php_dl(filename, MODULE_TEMPORARY, return_value, 0);
  61. if (Z_TYPE_P(return_value) == IS_TRUE) {
  62. EG(full_tables_cleanup) = 1;
  63. }
  64. }
  65. /* }}} */
  66. #if defined(HAVE_LIBDL)
  67. /* {{{ php_load_shlib
  68. */
  69. PHPAPI void *php_load_shlib(char *path, char **errp)
  70. {
  71. void *handle;
  72. char *err;
  73. handle = DL_LOAD(path);
  74. if (!handle) {
  75. err = GET_DL_ERROR();
  76. #ifdef PHP_WIN32
  77. if (err && (*err)) {
  78. size_t i = strlen(err);
  79. (*errp)=estrdup(err);
  80. LocalFree(err);
  81. while (i > 0 && isspace((*errp)[i-1])) { (*errp)[i-1] = '\0'; i--; }
  82. } else {
  83. (*errp) = estrdup("<No message>");
  84. }
  85. #else
  86. (*errp) = estrdup(err);
  87. GET_DL_ERROR(); /* free the buffer storing the error */
  88. #endif
  89. }
  90. return handle;
  91. }
  92. /* }}} */
  93. /* {{{ php_load_extension
  94. */
  95. PHPAPI int php_load_extension(char *filename, int type, int start_now)
  96. {
  97. void *handle;
  98. char *libpath;
  99. zend_module_entry *module_entry;
  100. zend_module_entry *(*get_module)(void);
  101. int error_type, slash_suffix = 0;
  102. char *extension_dir;
  103. char *err1, *err2;
  104. if (type == MODULE_PERSISTENT) {
  105. extension_dir = INI_STR("extension_dir");
  106. } else {
  107. extension_dir = PG(extension_dir);
  108. }
  109. if (type == MODULE_TEMPORARY) {
  110. error_type = E_WARNING;
  111. } else {
  112. error_type = E_CORE_WARNING;
  113. }
  114. /* Check if passed filename contains directory separators */
  115. if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
  116. /* Passing modules with full path is not supported for dynamically loaded extensions */
  117. if (type == MODULE_TEMPORARY) {
  118. php_error_docref(NULL, E_WARNING, "Temporary module name should contain only filename");
  119. return FAILURE;
  120. }
  121. libpath = estrdup(filename);
  122. } else if (extension_dir && extension_dir[0]) {
  123. slash_suffix = IS_SLASH(extension_dir[strlen(extension_dir)-1]);
  124. /* Try as filename first */
  125. if (slash_suffix) {
  126. spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
  127. } else {
  128. spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
  129. }
  130. } else {
  131. return FAILURE; /* Not full path given or extension_dir is not set */
  132. }
  133. handle = php_load_shlib(libpath, &err1);
  134. if (!handle) {
  135. /* Now, consider 'filename' as extension name and build file name */
  136. char *orig_libpath = libpath;
  137. if (slash_suffix) {
  138. spprintf(&libpath, 0, "%s" PHP_SHLIB_EXT_PREFIX "%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
  139. } else {
  140. spprintf(&libpath, 0, "%s%c" PHP_SHLIB_EXT_PREFIX "%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
  141. }
  142. handle = php_load_shlib(libpath, &err2);
  143. if (!handle) {
  144. php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' (tried: %s (%s), %s (%s))",
  145. filename, orig_libpath, err1, libpath, err2);
  146. efree(orig_libpath);
  147. efree(err1);
  148. efree(libpath);
  149. efree(err2);
  150. return FAILURE;
  151. }
  152. efree(orig_libpath);
  153. efree(err1);
  154. }
  155. efree(libpath);
  156. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
  157. /* Some OS prepend _ to symbol names while their dynamic linker
  158. * does not do that automatically. Thus we check manually for
  159. * _get_module. */
  160. if (!get_module) {
  161. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
  162. }
  163. if (!get_module) {
  164. if (DL_FETCH_SYMBOL(handle, "zend_extension_entry") || DL_FETCH_SYMBOL(handle, "_zend_extension_entry")) {
  165. DL_UNLOAD(handle);
  166. php_error_docref(NULL, error_type, "Invalid library (appears to be a Zend Extension, try loading using zend_extension=%s from php.ini)", filename);
  167. return FAILURE;
  168. }
  169. DL_UNLOAD(handle);
  170. php_error_docref(NULL, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
  171. return FAILURE;
  172. }
  173. module_entry = get_module();
  174. if (module_entry->zend_api != ZEND_MODULE_API_NO) {
  175. php_error_docref(NULL, error_type,
  176. "%s: Unable to initialize module\n"
  177. "Module compiled with module API=%d\n"
  178. "PHP compiled with module API=%d\n"
  179. "These options need to match\n",
  180. module_entry->name, module_entry->zend_api, ZEND_MODULE_API_NO);
  181. DL_UNLOAD(handle);
  182. return FAILURE;
  183. }
  184. if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
  185. php_error_docref(NULL, error_type,
  186. "%s: Unable to initialize module\n"
  187. "Module compiled with build ID=%s\n"
  188. "PHP compiled with build ID=%s\n"
  189. "These options need to match\n",
  190. module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
  191. DL_UNLOAD(handle);
  192. return FAILURE;
  193. }
  194. module_entry->type = type;
  195. module_entry->module_number = zend_next_free_module();
  196. module_entry->handle = handle;
  197. if ((module_entry = zend_register_module_ex(module_entry)) == NULL) {
  198. DL_UNLOAD(handle);
  199. return FAILURE;
  200. }
  201. if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry) == FAILURE) {
  202. DL_UNLOAD(handle);
  203. return FAILURE;
  204. }
  205. if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
  206. if (module_entry->request_startup_func(type, module_entry->module_number) == FAILURE) {
  207. php_error_docref(NULL, error_type, "Unable to initialize module '%s'", module_entry->name);
  208. DL_UNLOAD(handle);
  209. return FAILURE;
  210. }
  211. }
  212. return SUCCESS;
  213. }
  214. /* }}} */
  215. /* {{{ php_dl
  216. */
  217. PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now)
  218. {
  219. /* Load extension */
  220. if (php_load_extension(file, type, start_now) == FAILURE) {
  221. RETVAL_FALSE;
  222. } else {
  223. RETVAL_TRUE;
  224. }
  225. }
  226. /* }}} */
  227. PHP_MINFO_FUNCTION(dl)
  228. {
  229. php_info_print_table_row(2, "Dynamic Library Support", "enabled");
  230. }
  231. #else
  232. PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now)
  233. {
  234. php_error_docref(NULL, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", file);
  235. RETVAL_FALSE;
  236. }
  237. PHP_MINFO_FUNCTION(dl)
  238. {
  239. PUTS("Dynamic Library support not available<br />.\n");
  240. }
  241. #endif
  242. /*
  243. * Local variables:
  244. * tab-width: 4
  245. * c-basic-offset: 4
  246. * End:
  247. * vim600: sw=4 ts=4 fdm=marker
  248. * vim<600: sw=4 ts=4
  249. */