xref: /php-src/Zend/zend_virtual_cwd.h (revision 5d3fab93)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Andi Gutmans <andi@php.net>                                 |
14    |          Sascha Schumann <sascha@schumann.cx>                        |
15    |          Pierre Joye <pierre@php.net>                                |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef VIRTUAL_CWD_H
20 #define VIRTUAL_CWD_H
21 
22 #include "TSRM.h"
23 
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <ctype.h>
27 
28 #ifdef HAVE_UTIME_H
29 #include <utime.h>
30 #endif
31 
32 #include <stdarg.h>
33 #include <limits.h>
34 
35 #ifdef HAVE_SYS_PARAM_H
36 # include <sys/param.h>
37 #endif
38 
39 #ifndef MAXPATHLEN
40 # ifdef _WIN32
41 #  include "win32/ioutil.h"
42 #  define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
43 # elif PATH_MAX
44 #  define MAXPATHLEN PATH_MAX
45 # elif defined(MAX_PATH)
46 #  define MAXPATHLEN MAX_PATH
47 # else
48 #  define MAXPATHLEN 256
49 # endif
50 #endif
51 
52 #ifdef ZTS
53 #define VIRTUAL_DIR
54 #endif
55 
56 #ifndef ZEND_WIN32
57 #include <unistd.h>
58 #else
59 #include <direct.h>
60 #endif
61 
62 #if defined(__osf__) || defined(_AIX)
63 #include <errno.h>
64 #endif
65 
66 #include "zend_stream.h"
67 
68 #ifdef ZEND_WIN32
69 #include "win32/readdir.h"
70 #include <sys/utime.h>
71 #include "win32/ioutil.h"
72 /* mode_t isn't defined on Windows */
73 typedef unsigned short mode_t;
74 
75 #define DEFAULT_SLASH '\\'
76 #define DEFAULT_DIR_SEPARATOR	';'
77 #define IS_SLASH(c)	((c) == '/' || (c) == '\\')
78 #define IS_SLASH_P(c)	(*(c) == '/' || \
79         (*(c) == '\\' && !IsDBCSLeadByte(*(c-1))))
80 
81 /* COPY_WHEN_ABSOLUTE is 2 under Win32 because by chance both regular absolute paths
82    in the file system and UNC paths need copying of two characters */
83 #define COPY_WHEN_ABSOLUTE(path) 2
84 #define IS_UNC_PATH(path, len) \
85 	(len >= 2 && IS_SLASH(path[0]) && IS_SLASH(path[1]))
86 #define IS_ABSOLUTE_PATH(path, len) \
87 	(len >= 2 && (/* is local */isalpha(path[0]) && path[1] == ':' || /* is UNC */IS_SLASH(path[0]) && IS_SLASH(path[1])))
88 
89 #else
90 #ifdef HAVE_DIRENT_H
91 #include <dirent.h>
92 
93 #ifndef DT_UNKNOWN
94 # define DT_UNKNOWN 0
95 #endif
96 #ifndef DT_DIR
97 # define DT_DIR 4
98 #endif
99 #ifndef DT_REG
100 # define DT_REG 8
101 #endif
102 #endif
103 
104 #define DEFAULT_SLASH '/'
105 
106 #ifdef __riscos__
107 #define DEFAULT_DIR_SEPARATOR  ';'
108 #else
109 #define DEFAULT_DIR_SEPARATOR  ':'
110 #endif
111 
112 #define IS_SLASH(c)	((c) == '/')
113 #define IS_SLASH_P(c)	(*(c) == '/')
114 
115 #endif
116 
117 
118 #ifndef COPY_WHEN_ABSOLUTE
119 #define COPY_WHEN_ABSOLUTE(path) 0
120 #endif
121 
122 #ifndef IS_ABSOLUTE_PATH
123 #define IS_ABSOLUTE_PATH(path, len) \
124 	(IS_SLASH(path[0]))
125 #endif
126 
127 #ifdef TSRM_EXPORTS
128 #define CWD_EXPORTS
129 #endif
130 
131 #ifdef ZEND_WIN32
132 #	ifdef CWD_EXPORTS
133 #		define CWD_API __declspec(dllexport)
134 #	else
135 #		define CWD_API __declspec(dllimport)
136 #	endif
137 #elif defined(__GNUC__) && __GNUC__ >= 4
138 #	define CWD_API __attribute__ ((visibility("default")))
139 #else
140 #	define CWD_API
141 #endif
142 
143 #ifdef ZEND_WIN32
144 # define php_sys_stat_ex php_win32_ioutil_stat_ex
145 # define php_sys_stat php_win32_ioutil_stat
146 # define php_sys_lstat php_win32_ioutil_lstat
147 # define php_sys_fstat php_win32_ioutil_fstat
148 # define php_sys_readlink php_win32_ioutil_readlink
149 # define php_sys_symlink php_win32_ioutil_symlink
150 # define php_sys_link php_win32_ioutil_link
151 #else
152 # define php_sys_stat stat
153 # define php_sys_lstat lstat
154 # define php_sys_fstat fstat
155 # ifdef HAVE_SYMLINK
156 # define php_sys_readlink(link, target, target_len) readlink(link, target, target_len)
157 # define php_sys_symlink symlink
158 # define php_sys_link link
159 # endif
160 #endif
161 
162 typedef struct _cwd_state {
163 	char *cwd;
164 	size_t cwd_length;
165 } cwd_state;
166 
167 typedef int (*verify_path_func)(const cwd_state *);
168 
169 CWD_API void virtual_cwd_startup(void);
170 CWD_API void virtual_cwd_shutdown(void);
171 CWD_API int virtual_cwd_activate(void);
172 CWD_API int virtual_cwd_deactivate(void);
173 CWD_API char *virtual_getcwd_ex(size_t *length);
174 CWD_API char *virtual_getcwd(char *buf, size_t size);
175 CWD_API zend_result virtual_chdir(const char *path);
176 CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path));
177 CWD_API int virtual_filepath(const char *path, char **filepath);
178 CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path);
179 CWD_API char *virtual_realpath(const char *path, char *real_path);
180 CWD_API FILE *virtual_fopen(const char *path, const char *mode);
181 CWD_API int virtual_open(const char *path, int flags, ...);
182 CWD_API int virtual_creat(const char *path, mode_t mode);
183 CWD_API int virtual_rename(const char *oldname, const char *newname);
184 CWD_API int virtual_stat(const char *path, zend_stat_t *buf);
185 CWD_API int virtual_lstat(const char *path, zend_stat_t *buf);
186 CWD_API int virtual_unlink(const char *path);
187 CWD_API int virtual_mkdir(const char *pathname, mode_t mode);
188 CWD_API int virtual_rmdir(const char *pathname);
189 CWD_API DIR *virtual_opendir(const char *pathname);
190 CWD_API FILE *virtual_popen(const char *command, const char *type);
191 CWD_API int virtual_access(const char *pathname, int mode);
192 
193 #ifdef HAVE_UTIME
194 CWD_API int virtual_utime(const char *filename, struct utimbuf *buf);
195 #endif
196 CWD_API int virtual_chmod(const char *filename, mode_t mode);
197 #if !defined(ZEND_WIN32)
198 CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link);
199 #endif
200 
201 /* One of the following constants must be used as the last argument
202    in virtual_file_ex() call. */
203 
204 // TODO Make this into an enum
205 #define CWD_EXPAND   0 /* expand "." and ".." but don't resolve symlinks     */
206 #define CWD_FILEPATH 1 /* resolve symlinks if file is exist otherwise expand */
207 #define CWD_REALPATH 2 /* call realpath(), resolve symlinks. File must exist */
208 
209 CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath);
210 
211 CWD_API char *tsrm_realpath(const char *path, char *real_path);
212 
213 #define REALPATH_CACHE_TTL  (2*60) /* 2 minutes */
214 #define REALPATH_CACHE_SIZE 0      /* disabled while php.ini isn't loaded */
215 
216 typedef struct _realpath_cache_bucket {
217 	zend_ulong                    key;
218 	char                          *path;
219 	char                          *realpath;
220 	struct _realpath_cache_bucket *next;
221 	time_t                         expires;
222 	uint16_t                       path_len;
223 	uint16_t                       realpath_len;
224 	uint8_t                        is_dir:1;
225 #ifdef ZEND_WIN32
226 	uint8_t                        is_rvalid:1;
227 	uint8_t                        is_readable:1;
228 	uint8_t                        is_wvalid:1;
229 	uint8_t                        is_writable:1;
230 #endif
231 } realpath_cache_bucket;
232 
233 typedef struct _virtual_cwd_globals {
234 	cwd_state cwd;
235 	zend_long                   realpath_cache_size;
236 	zend_long                   realpath_cache_size_limit;
237 	zend_long                   realpath_cache_ttl;
238 	realpath_cache_bucket *realpath_cache[1024];
239 } virtual_cwd_globals;
240 
241 #ifdef ZTS
242 extern ts_rsrc_id cwd_globals_id;
243 extern size_t cwd_globals_offset;
244 # define CWDG(v) ZEND_TSRMG_FAST(cwd_globals_offset, virtual_cwd_globals *, v)
245 #else
246 extern virtual_cwd_globals cwd_globals;
247 # define CWDG(v) (cwd_globals.v)
248 #endif
249 
250 CWD_API void realpath_cache_clean(void);
251 CWD_API void realpath_cache_del(const char *path, size_t path_len);
252 CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, size_t path_len, time_t t);
253 CWD_API zend_long realpath_cache_size(void);
254 CWD_API zend_long realpath_cache_max_buckets(void);
255 CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void);
256 
257 #ifdef CWD_EXPORTS
258 extern void virtual_cwd_main_cwd_init(uint8_t);
259 #endif
260 
261 /* The actual macros to be used in programs using TSRM
262  * If the program defines VIRTUAL_DIR it will use the
263  * virtual_* functions
264  */
265 
266 #ifdef VIRTUAL_DIR
267 
268 #define VCWD_GETCWD(buff, size) virtual_getcwd(buff, size)
269 #define VCWD_FOPEN(path, mode) virtual_fopen(path, mode)
270 /* Because open() has two modes, we have to macros to replace it */
271 #define VCWD_OPEN(path, flags) virtual_open(path, flags)
272 #define VCWD_OPEN_MODE(path, flags, mode) virtual_open(path, flags, mode)
273 #define VCWD_CREAT(path, mode) virtual_creat(path, mode)
274 #define VCWD_CHDIR(path) virtual_chdir(path)
275 #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, (int (*)(const char *)) virtual_chdir)
276 #define VCWD_GETWD(buf)
277 #define VCWD_REALPATH(path, real_path) virtual_realpath(path, real_path)
278 #define VCWD_RENAME(oldname, newname) virtual_rename(oldname, newname)
279 #define VCWD_STAT(path, buff) virtual_stat(path, buff)
280 # define VCWD_LSTAT(path, buff) virtual_lstat(path, buff)
281 #define VCWD_UNLINK(path) virtual_unlink(path)
282 #define VCWD_MKDIR(pathname, mode) virtual_mkdir(pathname, mode)
283 #define VCWD_RMDIR(pathname) virtual_rmdir(pathname)
284 #define VCWD_OPENDIR(pathname) virtual_opendir(pathname)
285 #define VCWD_POPEN(command, type) virtual_popen(command, type)
286 #define VCWD_ACCESS(pathname, mode) virtual_access(pathname, mode)
287 #ifdef HAVE_UTIME
288 #define VCWD_UTIME(path, time) virtual_utime(path, time)
289 #endif
290 #define VCWD_CHMOD(path, mode) virtual_chmod(path, mode)
291 #if !defined(ZEND_WIN32)
292 #define VCWD_CHOWN(path, owner, group) virtual_chown(path, owner, group, 0)
293 #ifdef HAVE_LCHOWN
294 #define VCWD_LCHOWN(path, owner, group) virtual_chown(path, owner, group, 1)
295 #endif
296 #endif
297 
298 #else
299 
300 #define VCWD_CREAT(path, mode) creat(path, mode)
301 /* rename on windows will fail if newname already exists.
302    MoveFileEx has to be used */
303 #if defined(ZEND_WIN32)
304 #define VCWD_FOPEN(path, mode)  php_win32_ioutil_fopen(path, mode)
305 #define VCWD_OPEN(path, flags) php_win32_ioutil_open(path, flags)
306 #define VCWD_OPEN_MODE(path, flags, mode) php_win32_ioutil_open(path, flags, mode)
307 # define VCWD_RENAME(oldname, newname) php_win32_ioutil_rename(oldname, newname)
308 #define VCWD_MKDIR(pathname, mode) php_win32_ioutil_mkdir(pathname, mode)
309 #define VCWD_RMDIR(pathname) php_win32_ioutil_rmdir(pathname)
310 #define VCWD_UNLINK(path) php_win32_ioutil_unlink(path)
311 #define VCWD_CHDIR(path) php_win32_ioutil_chdir(path)
312 #define VCWD_ACCESS(pathname, mode) tsrm_win32_access(pathname, mode)
313 #define VCWD_GETCWD(buff, size) php_win32_ioutil_getcwd(buff, size)
314 #define VCWD_CHMOD(path, mode) php_win32_ioutil_chmod(path, mode)
315 #else
316 #define VCWD_FOPEN(path, mode)  fopen(path, mode)
317 #define VCWD_OPEN(path, flags) open(path, flags)
318 #define VCWD_OPEN_MODE(path, flags, mode)	open(path, flags, mode)
319 # define VCWD_RENAME(oldname, newname) rename(oldname, newname)
320 #define VCWD_MKDIR(pathname, mode) mkdir(pathname, mode)
321 #define VCWD_RMDIR(pathname) rmdir(pathname)
322 #define VCWD_UNLINK(path) unlink(path)
323 #define VCWD_CHDIR(path) chdir(path)
324 #define VCWD_ACCESS(pathname, mode) access(pathname, mode)
325 #define VCWD_GETCWD(buff, size) getcwd(buff, size)
326 #define VCWD_CHMOD(path, mode) chmod(path, mode)
327 #endif
328 
329 #define VCWD_CHDIR_FILE(path) virtual_chdir_file(path, chdir)
330 #define VCWD_GETWD(buf) getwd(buf)
331 #define VCWD_STAT(path, buff) php_sys_stat(path, buff)
332 #define VCWD_LSTAT(path, buff) lstat(path, buff)
333 #define VCWD_OPENDIR(pathname) opendir(pathname)
334 #define VCWD_POPEN(command, type) popen(command, type)
335 
336 #define VCWD_REALPATH(path, real_path) tsrm_realpath(path, real_path)
337 
338 #ifdef HAVE_UTIME
339 # ifdef ZEND_WIN32
340 #  define VCWD_UTIME(path, time) win32_utime(path, time)
341 # else
342 #  define VCWD_UTIME(path, time) utime(path, time)
343 # endif
344 #endif
345 
346 #if !defined(ZEND_WIN32)
347 #define VCWD_CHOWN(path, owner, group) chown(path, owner, group)
348 #ifdef HAVE_LCHOWN
349 #define VCWD_LCHOWN(path, owner, group) lchown(path, owner, group)
350 #endif
351 #endif
352 
353 #endif
354 
355 /* Global stat declarations */
356 #ifndef _S_IFDIR
357 #define _S_IFDIR S_IFDIR
358 #endif
359 
360 #ifndef _S_IFREG
361 #define _S_IFREG S_IFREG
362 #endif
363 
364 #ifndef S_IFLNK
365 #define _IFLNK  0120000	/* symbolic link */
366 #define S_IFLNK _IFLNK
367 #endif
368 
369 #ifndef S_ISDIR
370 #define S_ISDIR(mode)	(((mode)&S_IFMT) == S_IFDIR)
371 #endif
372 
373 #ifndef S_ISREG
374 #define S_ISREG(mode)	(((mode)&S_IFMT) == S_IFREG)
375 #endif
376 
377 #ifndef S_ISLNK
378 #define S_ISLNK(mode)	(((mode)&S_IFMT) == S_IFLNK)
379 #endif
380 
381 #ifndef S_IXROOT
382 #define S_IXROOT ( S_IXUSR | S_IXGRP | S_IXOTH )
383 #endif
384 
385 /* XXX should be _S_IFIFO? */
386 #ifndef S_IFIFO
387 #define	_IFIFO  0010000	/* fifo */
388 #define S_IFIFO	_IFIFO
389 #endif
390 
391 #ifndef S_IFBLK
392 #define	_IFBLK  0060000	/* block special */
393 #define S_IFBLK	_IFBLK
394 #endif
395 
396 #endif /* VIRTUAL_CWD_H */
397