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