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