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