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