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