zend_virtual_cwd.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  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. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <limits.h>
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <fcntl.h>
  26. #include <time.h>
  27. #include "zend.h"
  28. #include "zend_virtual_cwd.h"
  29. #ifdef ZEND_WIN32
  30. #include <io.h>
  31. #include "tsrm_win32.h"
  32. # ifndef IO_REPARSE_TAG_SYMLINK
  33. # define IO_REPARSE_TAG_SYMLINK 0xA000000C
  34. # endif
  35. # ifndef IO_REPARSE_TAG_DEDUP
  36. # define IO_REPARSE_TAG_DEDUP 0x80000013
  37. # endif
  38. # ifndef IO_REPARSE_TAG_CLOUD
  39. # define IO_REPARSE_TAG_CLOUD (0x9000001AL)
  40. # endif
  41. /* IO_REPARSE_TAG_CLOUD_1 through IO_REPARSE_TAG_CLOUD_F have values of 0x9000101AL
  42. to 0x9000F01AL, they can be checked against the mask. */
  43. #ifndef IO_REPARSE_TAG_CLOUD_MASK
  44. #define IO_REPARSE_TAG_CLOUD_MASK (0x0000F000L)
  45. #endif
  46. #ifndef IO_REPARSE_TAG_ONEDRIVE
  47. #define IO_REPARSE_TAG_ONEDRIVE (0x80000021L)
  48. #endif
  49. # ifndef IO_REPARSE_TAG_ACTIVISION_HSM
  50. # define IO_REPARSE_TAG_ACTIVISION_HSM (0x00000047L)
  51. # endif
  52. # ifndef IO_REPARSE_TAG_PROJFS
  53. # define IO_REPARSE_TAG_PROJFS (0x9000001CL)
  54. # endif
  55. # ifndef VOLUME_NAME_NT
  56. # define VOLUME_NAME_NT 0x2
  57. # endif
  58. # ifndef VOLUME_NAME_DOS
  59. # define VOLUME_NAME_DOS 0x0
  60. # endif
  61. # include <winioctl.h>
  62. # include <winnt.h>
  63. #endif
  64. #define VIRTUAL_CWD_DEBUG 0
  65. #include "TSRM.h"
  66. /* Only need mutex for popen() in Windows because it doesn't chdir() on UNIX */
  67. #if defined(ZEND_WIN32) && defined(ZTS)
  68. MUTEX_T cwd_mutex;
  69. #endif
  70. #ifdef ZTS
  71. ts_rsrc_id cwd_globals_id;
  72. size_t cwd_globals_offset;
  73. #else
  74. virtual_cwd_globals cwd_globals;
  75. #endif
  76. static cwd_state main_cwd_state; /* True global */
  77. #ifndef ZEND_WIN32
  78. #include <unistd.h>
  79. #else
  80. #include <direct.h>
  81. #include "zend_globals.h"
  82. #include "zend_globals_macros.h"
  83. #endif
  84. #define CWD_STATE_COPY(d, s) \
  85. (d)->cwd_length = (s)->cwd_length; \
  86. (d)->cwd = (char *) emalloc((s)->cwd_length+1); \
  87. memcpy((d)->cwd, (s)->cwd, (s)->cwd_length+1);
  88. #define CWD_STATE_FREE(s) \
  89. efree((s)->cwd); \
  90. (s)->cwd_length = 0;
  91. #ifdef ZEND_WIN32
  92. # define CWD_STATE_FREE_ERR(state) do { \
  93. DWORD last_error = GetLastError(); \
  94. CWD_STATE_FREE(state); \
  95. SetLastError(last_error); \
  96. } while (0)
  97. #else
  98. # define CWD_STATE_FREE_ERR(state) CWD_STATE_FREE(state)
  99. #endif
  100. static int php_is_dir_ok(const cwd_state *state) /* {{{ */
  101. {
  102. zend_stat_t buf;
  103. if (php_sys_stat(state->cwd, &buf) == 0 && S_ISDIR(buf.st_mode))
  104. return (0);
  105. return (1);
  106. }
  107. /* }}} */
  108. static int php_is_file_ok(const cwd_state *state) /* {{{ */
  109. {
  110. zend_stat_t buf;
  111. if (php_sys_stat(state->cwd, &buf) == 0 && S_ISREG(buf.st_mode))
  112. return (0);
  113. return (1);
  114. }
  115. /* }}} */
  116. static void cwd_globals_ctor(virtual_cwd_globals *cwd_g) /* {{{ */
  117. {
  118. CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state);
  119. cwd_g->realpath_cache_size = 0;
  120. cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE;
  121. cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL;
  122. memset(cwd_g->realpath_cache, 0, sizeof(cwd_g->realpath_cache));
  123. }
  124. /* }}} */
  125. static void realpath_cache_clean_helper(uint32_t max_entries, realpath_cache_bucket **cache, zend_long *cache_size)
  126. {
  127. uint32_t i;
  128. for (i = 0; i < max_entries; i++) {
  129. realpath_cache_bucket *p = cache[i];
  130. while (p != NULL) {
  131. realpath_cache_bucket *r = p;
  132. p = p->next;
  133. free(r);
  134. }
  135. cache[i] = NULL;
  136. }
  137. *cache_size = 0;
  138. }
  139. static void cwd_globals_dtor(virtual_cwd_globals *cwd_g) /* {{{ */
  140. {
  141. realpath_cache_clean_helper(sizeof(cwd_g->realpath_cache)/sizeof(cwd_g->realpath_cache[0]), cwd_g->realpath_cache, &cwd_g->realpath_cache_size);
  142. }
  143. /* }}} */
  144. void virtual_cwd_main_cwd_init(uint8_t reinit) /* {{{ */
  145. {
  146. char cwd[MAXPATHLEN];
  147. char *result;
  148. if (reinit) {
  149. free(main_cwd_state.cwd);
  150. }
  151. #ifdef ZEND_WIN32
  152. ZeroMemory(&cwd, sizeof(cwd));
  153. result = php_win32_ioutil_getcwd(cwd, sizeof(cwd));
  154. #else
  155. result = getcwd(cwd, sizeof(cwd));
  156. #endif
  157. if (!result) {
  158. cwd[0] = '\0';
  159. }
  160. main_cwd_state.cwd_length = strlen(cwd);
  161. #ifdef ZEND_WIN32
  162. if (main_cwd_state.cwd_length >= 2 && cwd[1] == ':') {
  163. cwd[0] = toupper(cwd[0]);
  164. }
  165. #endif
  166. main_cwd_state.cwd = strdup(cwd);
  167. }
  168. /* }}} */
  169. CWD_API void virtual_cwd_startup(void) /* {{{ */
  170. {
  171. virtual_cwd_main_cwd_init(0);
  172. #ifdef ZTS
  173. ts_allocate_fast_id(&cwd_globals_id, &cwd_globals_offset, sizeof(virtual_cwd_globals), (ts_allocate_ctor) cwd_globals_ctor, (ts_allocate_dtor) cwd_globals_dtor);
  174. #else
  175. cwd_globals_ctor(&cwd_globals);
  176. #endif
  177. #if (defined(ZEND_WIN32)) && defined(ZTS)
  178. cwd_mutex = tsrm_mutex_alloc();
  179. #endif
  180. }
  181. /* }}} */
  182. CWD_API void virtual_cwd_shutdown(void) /* {{{ */
  183. {
  184. #ifndef ZTS
  185. cwd_globals_dtor(&cwd_globals);
  186. #endif
  187. #if (defined(ZEND_WIN32)) && defined(ZTS)
  188. tsrm_mutex_free(cwd_mutex);
  189. #endif
  190. free(main_cwd_state.cwd); /* Don't use CWD_STATE_FREE because the non global states will probably use emalloc()/efree() */
  191. }
  192. /* }}} */
  193. CWD_API int virtual_cwd_activate(void) /* {{{ */
  194. {
  195. if (CWDG(cwd).cwd == NULL) {
  196. CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state);
  197. }
  198. return 0;
  199. }
  200. /* }}} */
  201. CWD_API int virtual_cwd_deactivate(void) /* {{{ */
  202. {
  203. if (CWDG(cwd).cwd != NULL) {
  204. CWD_STATE_FREE(&CWDG(cwd));
  205. CWDG(cwd).cwd = NULL;
  206. }
  207. return 0;
  208. }
  209. /* }}} */
  210. CWD_API char *virtual_getcwd_ex(size_t *length) /* {{{ */
  211. {
  212. cwd_state *state;
  213. state = &CWDG(cwd);
  214. if (state->cwd_length == 0) {
  215. char *retval;
  216. *length = 1;
  217. retval = (char *) emalloc(2);
  218. retval[0] = DEFAULT_SLASH;
  219. retval[1] = '\0';
  220. return retval;
  221. }
  222. #ifdef ZEND_WIN32
  223. /* If we have something like C: */
  224. if (state->cwd_length == 2 && state->cwd[state->cwd_length-1] == ':') {
  225. char *retval;
  226. *length = state->cwd_length+1;
  227. retval = (char *) emalloc(*length+1);
  228. memcpy(retval, state->cwd, *length);
  229. retval[0] = toupper(retval[0]);
  230. retval[*length-1] = DEFAULT_SLASH;
  231. retval[*length] = '\0';
  232. return retval;
  233. }
  234. #endif
  235. if (!state->cwd) {
  236. *length = 0;
  237. return NULL;
  238. }
  239. *length = state->cwd_length;
  240. return estrdup(state->cwd);
  241. }
  242. /* }}} */
  243. /* Same semantics as UNIX getcwd() */
  244. CWD_API char *virtual_getcwd(char *buf, size_t size) /* {{{ */
  245. {
  246. size_t length;
  247. char *cwd;
  248. cwd = virtual_getcwd_ex(&length);
  249. if (buf == NULL) {
  250. return cwd;
  251. }
  252. if (length > size-1) {
  253. efree(cwd);
  254. errno = ERANGE; /* Is this OK? */
  255. return NULL;
  256. }
  257. if (!cwd) {
  258. return NULL;
  259. }
  260. memcpy(buf, cwd, length+1);
  261. efree(cwd);
  262. return buf;
  263. }
  264. /* }}} */
  265. #ifdef ZEND_WIN32
  266. static inline zend_ulong realpath_cache_key(const char *path, size_t path_len) /* {{{ */
  267. {
  268. zend_ulong h;
  269. size_t bucket_key_len;
  270. const char *bucket_key_start = tsrm_win32_get_path_sid_key(path, path_len, &bucket_key_len);
  271. const char *bucket_key = bucket_key_start;
  272. const char *e;
  273. if (!bucket_key) {
  274. return 0;
  275. }
  276. e = bucket_key + bucket_key_len;
  277. for (h = Z_UL(2166136261); bucket_key < e;) {
  278. h *= Z_UL(16777619);
  279. h ^= *bucket_key++;
  280. }
  281. if (bucket_key_start != path) {
  282. HeapFree(GetProcessHeap(), 0, (LPVOID)bucket_key_start);
  283. }
  284. return h;
  285. }
  286. /* }}} */
  287. #else
  288. static inline zend_ulong realpath_cache_key(const char *path, size_t path_len) /* {{{ */
  289. {
  290. zend_ulong h;
  291. const char *e = path + path_len;
  292. for (h = Z_UL(2166136261); path < e;) {
  293. h *= Z_UL(16777619);
  294. h ^= *path++;
  295. }
  296. return h;
  297. }
  298. /* }}} */
  299. #endif /* defined(ZEND_WIN32) */
  300. CWD_API void realpath_cache_clean(void) /* {{{ */
  301. {
  302. realpath_cache_clean_helper(sizeof(CWDG(realpath_cache))/sizeof(CWDG(realpath_cache)[0]), CWDG(realpath_cache), &CWDG(realpath_cache_size));
  303. }
  304. /* }}} */
  305. CWD_API void realpath_cache_del(const char *path, size_t path_len) /* {{{ */
  306. {
  307. zend_ulong key = realpath_cache_key(path, path_len);
  308. zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  309. realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
  310. while (*bucket != NULL) {
  311. if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
  312. memcmp(path, (*bucket)->path, path_len) == 0) {
  313. realpath_cache_bucket *r = *bucket;
  314. *bucket = (*bucket)->next;
  315. /* if the pointers match then only subtract the length of the path */
  316. if(r->path == r->realpath) {
  317. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
  318. } else {
  319. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
  320. }
  321. free(r);
  322. return;
  323. } else {
  324. bucket = &(*bucket)->next;
  325. }
  326. }
  327. }
  328. /* }}} */
  329. static inline void realpath_cache_add(const char *path, size_t path_len, const char *realpath, size_t realpath_len, int is_dir, time_t t) /* {{{ */
  330. {
  331. zend_long size = sizeof(realpath_cache_bucket) + path_len + 1;
  332. int same = 1;
  333. if (realpath_len != path_len ||
  334. memcmp(path, realpath, path_len) != 0) {
  335. size += realpath_len + 1;
  336. same = 0;
  337. }
  338. if (CWDG(realpath_cache_size) + size <= CWDG(realpath_cache_size_limit)) {
  339. realpath_cache_bucket *bucket = malloc(size);
  340. zend_ulong n;
  341. if (bucket == NULL) {
  342. return;
  343. }
  344. bucket->key = realpath_cache_key(path, path_len);
  345. bucket->path = (char*)bucket + sizeof(realpath_cache_bucket);
  346. memcpy(bucket->path, path, path_len+1);
  347. bucket->path_len = path_len;
  348. if (same) {
  349. bucket->realpath = bucket->path;
  350. } else {
  351. bucket->realpath = bucket->path + (path_len + 1);
  352. memcpy(bucket->realpath, realpath, realpath_len+1);
  353. }
  354. bucket->realpath_len = realpath_len;
  355. bucket->is_dir = is_dir > 0;
  356. #ifdef ZEND_WIN32
  357. bucket->is_rvalid = 0;
  358. bucket->is_readable = 0;
  359. bucket->is_wvalid = 0;
  360. bucket->is_writable = 0;
  361. #endif
  362. bucket->expires = t + CWDG(realpath_cache_ttl);
  363. n = bucket->key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  364. bucket->next = CWDG(realpath_cache)[n];
  365. CWDG(realpath_cache)[n] = bucket;
  366. CWDG(realpath_cache_size) += size;
  367. }
  368. }
  369. /* }}} */
  370. static inline realpath_cache_bucket* realpath_cache_find(const char *path, size_t path_len, time_t t) /* {{{ */
  371. {
  372. zend_ulong key = realpath_cache_key(path, path_len);
  373. zend_ulong n = key % (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  374. realpath_cache_bucket **bucket = &CWDG(realpath_cache)[n];
  375. while (*bucket != NULL) {
  376. if (CWDG(realpath_cache_ttl) && (*bucket)->expires < t) {
  377. realpath_cache_bucket *r = *bucket;
  378. *bucket = (*bucket)->next;
  379. /* if the pointers match then only subtract the length of the path */
  380. if(r->path == r->realpath) {
  381. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1;
  382. } else {
  383. CWDG(realpath_cache_size) -= sizeof(realpath_cache_bucket) + r->path_len + 1 + r->realpath_len + 1;
  384. }
  385. free(r);
  386. } else if (key == (*bucket)->key && path_len == (*bucket)->path_len &&
  387. memcmp(path, (*bucket)->path, path_len) == 0) {
  388. return *bucket;
  389. } else {
  390. bucket = &(*bucket)->next;
  391. }
  392. }
  393. return NULL;
  394. }
  395. /* }}} */
  396. CWD_API realpath_cache_bucket* realpath_cache_lookup(const char *path, size_t path_len, time_t t) /* {{{ */
  397. {
  398. return realpath_cache_find(path, path_len, t);
  399. }
  400. /* }}} */
  401. CWD_API zend_long realpath_cache_size(void)
  402. {
  403. return CWDG(realpath_cache_size);
  404. }
  405. CWD_API zend_long realpath_cache_max_buckets(void)
  406. {
  407. return (sizeof(CWDG(realpath_cache)) / sizeof(CWDG(realpath_cache)[0]));
  408. }
  409. CWD_API realpath_cache_bucket** realpath_cache_get_buckets(void)
  410. {
  411. return CWDG(realpath_cache);
  412. }
  413. #undef LINK_MAX
  414. #define LINK_MAX 32
  415. static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, time_t *t, int use_realpath, bool is_dir, int *link_is_dir) /* {{{ */
  416. {
  417. size_t i, j;
  418. int directory = 0, save;
  419. #ifdef ZEND_WIN32
  420. WIN32_FIND_DATAW dataw;
  421. HANDLE hFind = INVALID_HANDLE_VALUE;
  422. ALLOCA_FLAG(use_heap_large)
  423. wchar_t *pathw = NULL;
  424. int may_retry_reparse_point;
  425. #define FREE_PATHW() \
  426. do { free(pathw); } while(0);
  427. #else
  428. zend_stat_t st;
  429. #endif
  430. realpath_cache_bucket *bucket;
  431. char *tmp;
  432. ALLOCA_FLAG(use_heap)
  433. while (1) {
  434. if (len <= start) {
  435. if (link_is_dir) {
  436. *link_is_dir = 1;
  437. }
  438. return start;
  439. }
  440. i = len;
  441. while (i > start && !IS_SLASH(path[i-1])) {
  442. i--;
  443. }
  444. assert(i < MAXPATHLEN);
  445. if (i == len ||
  446. (i + 1 == len && path[i] == '.')) {
  447. /* remove double slashes and '.' */
  448. len = EXPECTED(i > 0) ? i - 1 : 0;
  449. is_dir = 1;
  450. continue;
  451. } else if (i + 2 == len && path[i] == '.' && path[i+1] == '.') {
  452. /* remove '..' and previous directory */
  453. is_dir = 1;
  454. if (link_is_dir) {
  455. *link_is_dir = 1;
  456. }
  457. if (i <= start + 1) {
  458. return start ? start : len;
  459. }
  460. j = tsrm_realpath_r(path, start, i-1, ll, t, use_realpath, 1, NULL);
  461. if (j > start && j != (size_t)-1) {
  462. j--;
  463. assert(i < MAXPATHLEN);
  464. while (j > start && !IS_SLASH(path[j])) {
  465. j--;
  466. }
  467. assert(i < MAXPATHLEN);
  468. if (!start) {
  469. /* leading '..' must not be removed in case of relative path */
  470. if (j == 0 && path[0] == '.' && path[1] == '.' &&
  471. IS_SLASH(path[2])) {
  472. path[3] = '.';
  473. path[4] = '.';
  474. path[5] = DEFAULT_SLASH;
  475. j = 5;
  476. } else if (j > 0 &&
  477. path[j+1] == '.' && path[j+2] == '.' &&
  478. IS_SLASH(path[j+3])) {
  479. j += 4;
  480. path[j++] = '.';
  481. path[j++] = '.';
  482. path[j] = DEFAULT_SLASH;
  483. }
  484. }
  485. } else if (!start && !j) {
  486. /* leading '..' must not be removed in case of relative path */
  487. path[0] = '.';
  488. path[1] = '.';
  489. path[2] = DEFAULT_SLASH;
  490. j = 2;
  491. }
  492. return j;
  493. }
  494. path[len] = 0;
  495. save = (use_realpath != CWD_EXPAND);
  496. if (start && save && CWDG(realpath_cache_size_limit)) {
  497. /* cache lookup for absolute path */
  498. if (!*t) {
  499. *t = time(0);
  500. }
  501. if ((bucket = realpath_cache_find(path, len, *t)) != NULL) {
  502. if (is_dir && !bucket->is_dir) {
  503. /* not a directory */
  504. return (size_t)-1;
  505. } else {
  506. if (link_is_dir) {
  507. *link_is_dir = bucket->is_dir;
  508. }
  509. memcpy(path, bucket->realpath, bucket->realpath_len + 1);
  510. return bucket->realpath_len;
  511. }
  512. }
  513. }
  514. #ifdef ZEND_WIN32
  515. retry_reparse_point:
  516. may_retry_reparse_point = 0;
  517. if (save) {
  518. pathw = php_win32_ioutil_any_to_w(path);
  519. if (!pathw) {
  520. return (size_t)-1;
  521. }
  522. hFind = FindFirstFileExW(pathw, FindExInfoBasic, &dataw, FindExSearchNameMatch, NULL, 0);
  523. if (INVALID_HANDLE_VALUE == hFind) {
  524. if (use_realpath == CWD_REALPATH) {
  525. /* file not found */
  526. FREE_PATHW()
  527. return (size_t)-1;
  528. }
  529. /* continue resolution anyway but don't save result in the cache */
  530. save = 0;
  531. } else {
  532. FindClose(hFind);
  533. }
  534. }
  535. tmp = do_alloca(len+1, use_heap);
  536. memcpy(tmp, path, len+1);
  537. retry_reparse_tag_cloud:
  538. if(save &&
  539. !(IS_UNC_PATH(path, len) && len >= 3 && path[2] != '?') &&
  540. (dataw.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
  541. ) {
  542. /* File is a reparse point. Get the target */
  543. HANDLE hLink = NULL;
  544. PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER * pbuffer;
  545. DWORD retlength = 0;
  546. size_t bufindex = 0;
  547. uint8_t isabsolute = 0;
  548. wchar_t * reparsetarget;
  549. BOOL isVolume = FALSE;
  550. #if VIRTUAL_CWD_DEBUG
  551. char *printname = NULL;
  552. #endif
  553. char *substitutename = NULL;
  554. size_t substitutename_len;
  555. size_t substitutename_off = 0;
  556. wchar_t tmpsubstname[MAXPATHLEN];
  557. if(++(*ll) > LINK_MAX) {
  558. free_alloca(tmp, use_heap);
  559. FREE_PATHW()
  560. return (size_t)-1;
  561. }
  562. hLink = CreateFileW(pathw,
  563. 0,
  564. PHP_WIN32_IOUTIL_DEFAULT_SHARE_MODE,
  565. NULL,
  566. OPEN_EXISTING,
  567. FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
  568. NULL);
  569. if(hLink == INVALID_HANDLE_VALUE) {
  570. free_alloca(tmp, use_heap);
  571. FREE_PATHW()
  572. return (size_t)-1;
  573. }
  574. pbuffer = (PHP_WIN32_IOUTIL_REPARSE_DATA_BUFFER *)do_alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE, use_heap_large);
  575. if (pbuffer == NULL) {
  576. CloseHandle(hLink);
  577. free_alloca(tmp, use_heap);
  578. FREE_PATHW()
  579. return (size_t)-1;
  580. }
  581. if(!DeviceIoControl(hLink, FSCTL_GET_REPARSE_POINT, NULL, 0, pbuffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &retlength, NULL)) {
  582. BY_HANDLE_FILE_INFORMATION fileInformation;
  583. free_alloca(pbuffer, use_heap_large);
  584. if ((dataw.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
  585. (dataw.dwReserved0 & ~IO_REPARSE_TAG_CLOUD_MASK) == IO_REPARSE_TAG_CLOUD &&
  586. EG(windows_version_info).dwMajorVersion >= 10 &&
  587. EG(windows_version_info).dwMinorVersion == 0 &&
  588. EG(windows_version_info).dwBuildNumber >= 18362 &&
  589. GetFileInformationByHandle(hLink, &fileInformation) &&
  590. !(fileInformation.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
  591. dataw.dwFileAttributes = fileInformation.dwFileAttributes;
  592. CloseHandle(hLink);
  593. (*ll)--;
  594. goto retry_reparse_tag_cloud;
  595. }
  596. free_alloca(tmp, use_heap);
  597. CloseHandle(hLink);
  598. FREE_PATHW()
  599. return (size_t)-1;
  600. }
  601. CloseHandle(hLink);
  602. if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
  603. may_retry_reparse_point = 1;
  604. reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
  605. isabsolute = pbuffer->SymbolicLinkReparseBuffer.Flags == 0;
  606. #if VIRTUAL_CWD_DEBUG
  607. printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
  608. if (!printname) {
  609. free_alloca(pbuffer, use_heap_large);
  610. free_alloca(tmp, use_heap);
  611. FREE_PATHW()
  612. return (size_t)-1;
  613. }
  614. #endif
  615. substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
  616. if (substitutename_len >= MAXPATHLEN) {
  617. free_alloca(pbuffer, use_heap_large);
  618. free_alloca(tmp, use_heap);
  619. FREE_PATHW()
  620. return (size_t)-1;
  621. }
  622. memmove(tmpsubstname, reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR), pbuffer->MountPointReparseBuffer.SubstituteNameLength);
  623. tmpsubstname[substitutename_len] = L'\0';
  624. substitutename = php_win32_cp_conv_w_to_any(tmpsubstname, substitutename_len, &substitutename_len);
  625. if (!substitutename || substitutename_len >= MAXPATHLEN) {
  626. free_alloca(pbuffer, use_heap_large);
  627. free_alloca(tmp, use_heap);
  628. free(substitutename);
  629. #if VIRTUAL_CWD_DEBUG
  630. free(printname);
  631. #endif
  632. FREE_PATHW()
  633. return (size_t)-1;
  634. }
  635. }
  636. else if(pbuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  637. isabsolute = 1;
  638. reparsetarget = pbuffer->MountPointReparseBuffer.ReparseTarget;
  639. #if VIRTUAL_CWD_DEBUG
  640. printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
  641. if (!printname) {
  642. free_alloca(pbuffer, use_heap_large);
  643. free_alloca(tmp, use_heap);
  644. FREE_PATHW()
  645. return (size_t)-1;
  646. }
  647. #endif
  648. substitutename_len = pbuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR);
  649. if (substitutename_len >= MAXPATHLEN) {
  650. free_alloca(pbuffer, use_heap_large);
  651. free_alloca(tmp, use_heap);
  652. FREE_PATHW()
  653. return (size_t)-1;
  654. }
  655. memmove(tmpsubstname, reparsetarget + pbuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR), pbuffer->MountPointReparseBuffer.SubstituteNameLength);
  656. tmpsubstname[substitutename_len] = L'\0';
  657. substitutename = php_win32_cp_conv_w_to_any(tmpsubstname, substitutename_len, &substitutename_len);
  658. if (!substitutename || substitutename_len >= MAXPATHLEN) {
  659. free_alloca(pbuffer, use_heap_large);
  660. free_alloca(tmp, use_heap);
  661. free(substitutename);
  662. #if VIRTUAL_CWD_DEBUG
  663. free(printname);
  664. #endif
  665. FREE_PATHW()
  666. return (size_t)-1;
  667. }
  668. }
  669. else if (pbuffer->ReparseTag == IO_REPARSE_TAG_DEDUP ||
  670. /* Starting with 1709. */
  671. (pbuffer->ReparseTag & ~IO_REPARSE_TAG_CLOUD_MASK) == IO_REPARSE_TAG_CLOUD ||
  672. IO_REPARSE_TAG_ONEDRIVE == pbuffer->ReparseTag ||
  673. IO_REPARSE_TAG_ACTIVISION_HSM == pbuffer->ReparseTag ||
  674. IO_REPARSE_TAG_PROJFS == pbuffer->ReparseTag) {
  675. isabsolute = 1;
  676. substitutename = malloc((len + 1) * sizeof(char));
  677. if (!substitutename) {
  678. free_alloca(pbuffer, use_heap_large);
  679. free_alloca(tmp, use_heap);
  680. FREE_PATHW()
  681. return (size_t)-1;
  682. }
  683. memcpy(substitutename, path, len + 1);
  684. substitutename_len = len;
  685. } else {
  686. /* XXX this might be not the end, restart handling with REPARSE_GUID_DATA_BUFFER should be implemented. */
  687. free_alloca(pbuffer, use_heap_large);
  688. free_alloca(tmp, use_heap);
  689. FREE_PATHW()
  690. return (size_t)-1;
  691. }
  692. if(isabsolute && substitutename_len > 4) {
  693. /* Do not resolve volumes (for now). A mounted point can
  694. target a volume without a drive, it is not certain that
  695. all IO functions we use in php and its deps support
  696. path with volume GUID instead of the DOS way, like:
  697. d:\test\mnt\foo
  698. \\?\Volume{62d1c3f8-83b9-11de-b108-806e6f6e6963}\foo
  699. */
  700. if (strncmp(substitutename, "\\??\\Volume{",11) == 0
  701. || strncmp(substitutename, "\\\\?\\Volume{",11) == 0
  702. || strncmp(substitutename, "\\??\\UNC\\", 8) == 0
  703. ) {
  704. isVolume = TRUE;
  705. substitutename_off = 0;
  706. } else
  707. /* do not use the \??\ and \\?\ prefix*/
  708. if (strncmp(substitutename, "\\??\\", 4) == 0
  709. || strncmp(substitutename, "\\\\?\\", 4) == 0) {
  710. substitutename_off = 4;
  711. }
  712. }
  713. if (!isVolume) {
  714. char * tmp2 = substitutename + substitutename_off;
  715. for (bufindex = 0; bufindex + substitutename_off < substitutename_len; bufindex++) {
  716. *(path + bufindex) = *(tmp2 + bufindex);
  717. }
  718. *(path + bufindex) = 0;
  719. j = bufindex;
  720. } else {
  721. j = len;
  722. }
  723. #if VIRTUAL_CWD_DEBUG
  724. fprintf(stderr, "reparse: print: %s ", printname);
  725. fprintf(stderr, "sub: %s ", substitutename);
  726. fprintf(stderr, "resolved: %s ", path);
  727. free(printname);
  728. #endif
  729. free_alloca(pbuffer, use_heap_large);
  730. free(substitutename);
  731. if (may_retry_reparse_point) {
  732. DWORD attrs;
  733. FREE_PATHW()
  734. pathw = php_win32_ioutil_any_to_w(path);
  735. if (!pathw) {
  736. return (size_t)-1;
  737. }
  738. attrs = GetFileAttributesW(pathw);
  739. if (!isVolume && attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_REPARSE_POINT)) {
  740. free_alloca(tmp, use_heap);
  741. FREE_PATHW()
  742. goto retry_reparse_point;
  743. }
  744. }
  745. if(isabsolute == 1) {
  746. if (!((j == 3) && (path[1] == ':') && (path[2] == '\\'))) {
  747. /* use_realpath is 0 in the call below coz path is absolute*/
  748. j = tsrm_realpath_r(path, 0, j, ll, t, 0, is_dir, &directory);
  749. if(j == (size_t)-1) {
  750. free_alloca(tmp, use_heap);
  751. FREE_PATHW()
  752. return (size_t)-1;
  753. }
  754. }
  755. }
  756. else {
  757. if(i + j >= MAXPATHLEN - 1) {
  758. free_alloca(tmp, use_heap);
  759. FREE_PATHW()
  760. return (size_t)-1;
  761. }
  762. memmove(path+i, path, j+1);
  763. memcpy(path, tmp, i-1);
  764. path[i-1] = DEFAULT_SLASH;
  765. j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
  766. if(j == (size_t)-1) {
  767. free_alloca(tmp, use_heap);
  768. FREE_PATHW()
  769. return (size_t)-1;
  770. }
  771. }
  772. directory = (dataw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  773. if(link_is_dir) {
  774. *link_is_dir = directory;
  775. }
  776. }
  777. else {
  778. if (save) {
  779. directory = (dataw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  780. if (is_dir && !directory) {
  781. /* not a directory */
  782. free_alloca(tmp, use_heap);
  783. FREE_PATHW()
  784. return (size_t)-1;
  785. }
  786. }
  787. #else
  788. if (save && php_sys_lstat(path, &st) < 0) {
  789. if (use_realpath == CWD_REALPATH) {
  790. /* file not found */
  791. return (size_t)-1;
  792. }
  793. /* continue resolution anyway but don't save result in the cache */
  794. save = 0;
  795. }
  796. tmp = do_alloca(len+1, use_heap);
  797. memcpy(tmp, path, len+1);
  798. if (save && S_ISLNK(st.st_mode)) {
  799. if (++(*ll) > LINK_MAX || (j = (size_t)php_sys_readlink(tmp, path, MAXPATHLEN)) == (size_t)-1) {
  800. /* too many links or broken symlinks */
  801. free_alloca(tmp, use_heap);
  802. return (size_t)-1;
  803. }
  804. path[j] = 0;
  805. if (IS_ABSOLUTE_PATH(path, j)) {
  806. j = tsrm_realpath_r(path, 1, j, ll, t, use_realpath, is_dir, &directory);
  807. if (j == (size_t)-1) {
  808. free_alloca(tmp, use_heap);
  809. return (size_t)-1;
  810. }
  811. } else {
  812. if (i + j >= MAXPATHLEN-1) {
  813. free_alloca(tmp, use_heap);
  814. return (size_t)-1; /* buffer overflow */
  815. }
  816. memmove(path+i, path, j+1);
  817. memcpy(path, tmp, i-1);
  818. path[i-1] = DEFAULT_SLASH;
  819. j = tsrm_realpath_r(path, start, i + j, ll, t, use_realpath, is_dir, &directory);
  820. if (j == (size_t)-1) {
  821. free_alloca(tmp, use_heap);
  822. return (size_t)-1;
  823. }
  824. }
  825. if (link_is_dir) {
  826. *link_is_dir = directory;
  827. }
  828. } else {
  829. if (save) {
  830. directory = S_ISDIR(st.st_mode);
  831. if (link_is_dir) {
  832. *link_is_dir = directory;
  833. }
  834. if (is_dir && !directory) {
  835. /* not a directory */
  836. free_alloca(tmp, use_heap);
  837. return (size_t)-1;
  838. }
  839. }
  840. #endif
  841. if (i <= start + 1) {
  842. j = start;
  843. } else {
  844. /* some leading directories may be inaccessible */
  845. j = tsrm_realpath_r(path, start, i-1, ll, t, save ? CWD_FILEPATH : use_realpath, 1, NULL);
  846. if (j > start && j != (size_t)-1) {
  847. path[j++] = DEFAULT_SLASH;
  848. }
  849. }
  850. #ifdef ZEND_WIN32
  851. if (j == (size_t)-1 || j + len >= MAXPATHLEN - 1 + i) {
  852. free_alloca(tmp, use_heap);
  853. FREE_PATHW()
  854. return (size_t)-1;
  855. }
  856. if (save) {
  857. size_t sz;
  858. char *tmp_path = php_win32_ioutil_conv_w_to_any(dataw.cFileName, PHP_WIN32_CP_IGNORE_LEN, &sz);
  859. if (!tmp_path) {
  860. free_alloca(tmp, use_heap);
  861. FREE_PATHW()
  862. return (size_t)-1;
  863. }
  864. i = sz;
  865. memcpy(path+j, tmp_path, i+1);
  866. free(tmp_path);
  867. j += i;
  868. } else {
  869. /* use the original file or directory name as it wasn't found */
  870. memcpy(path+j, tmp+i, len-i+1);
  871. j += (len-i);
  872. }
  873. }
  874. #else
  875. if (j == (size_t)-1 || j + len >= MAXPATHLEN - 1 + i) {
  876. free_alloca(tmp, use_heap);
  877. return (size_t)-1;
  878. }
  879. memcpy(path+j, tmp+i, len-i+1);
  880. j += (len-i);
  881. }
  882. #endif
  883. if (save && start && CWDG(realpath_cache_size_limit)) {
  884. /* save absolute path in the cache */
  885. realpath_cache_add(tmp, len, path, j, directory, *t);
  886. }
  887. free_alloca(tmp, use_heap);
  888. #ifdef ZEND_WIN32
  889. FREE_PATHW()
  890. #undef FREE_PATHW
  891. #endif
  892. return j;
  893. }
  894. }
  895. /* }}} */
  896. /* Resolve path relatively to state and put the real path into state */
  897. /* returns 0 for ok, 1 for error, -1 if (path_length >= MAXPATHLEN-1) */
  898. CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path, int use_realpath) /* {{{ */
  899. {
  900. size_t path_length = strlen(path);
  901. char resolved_path[MAXPATHLEN] = {0};
  902. size_t start = 1;
  903. int ll = 0;
  904. time_t t;
  905. int ret;
  906. bool add_slash;
  907. void *tmp;
  908. if (!path_length || path_length >= MAXPATHLEN-1) {
  909. #ifdef ZEND_WIN32
  910. SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_PARAMETER);
  911. #else
  912. errno = EINVAL;
  913. #endif
  914. return 1;
  915. }
  916. #if VIRTUAL_CWD_DEBUG
  917. fprintf(stderr,"cwd = %s path = %s\n", state->cwd, path);
  918. #endif
  919. /* cwd_length can be 0 when getcwd() fails.
  920. * This can happen under solaris when a dir does not have read permissions
  921. * but *does* have execute permissions */
  922. if (!IS_ABSOLUTE_PATH(path, path_length)) {
  923. if (state->cwd_length == 0) {
  924. /* resolve relative path */
  925. start = 0;
  926. memcpy(resolved_path , path, path_length + 1);
  927. } else {
  928. size_t state_cwd_length = state->cwd_length;
  929. #ifdef ZEND_WIN32
  930. if (IS_SLASH(path[0])) {
  931. if (state->cwd[1] == ':') {
  932. /* Copy only the drive name */
  933. state_cwd_length = 2;
  934. } else if (IS_UNC_PATH(state->cwd, state->cwd_length)) {
  935. /* Copy only the share name */
  936. state_cwd_length = 2;
  937. while (IS_SLASH(state->cwd[state_cwd_length])) {
  938. state_cwd_length++;
  939. }
  940. while (state->cwd[state_cwd_length] &&
  941. !IS_SLASH(state->cwd[state_cwd_length])) {
  942. state_cwd_length++;
  943. }
  944. while (IS_SLASH(state->cwd[state_cwd_length])) {
  945. state_cwd_length++;
  946. }
  947. while (state->cwd[state_cwd_length] &&
  948. !IS_SLASH(state->cwd[state_cwd_length])) {
  949. state_cwd_length++;
  950. }
  951. }
  952. }
  953. #endif
  954. if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
  955. #ifdef ZEND_WIN32
  956. SET_ERRNO_FROM_WIN32_CODE(ERROR_BUFFER_OVERFLOW);
  957. #else
  958. errno = ENAMETOOLONG;
  959. #endif
  960. return 1;
  961. }
  962. memcpy(resolved_path, state->cwd, state_cwd_length);
  963. if (resolved_path[state_cwd_length-1] == DEFAULT_SLASH) {
  964. memcpy(resolved_path + state_cwd_length, path, path_length + 1);
  965. path_length += state_cwd_length;
  966. } else {
  967. resolved_path[state_cwd_length] = DEFAULT_SLASH;
  968. memcpy(resolved_path + state_cwd_length + 1, path, path_length + 1);
  969. path_length += state_cwd_length + 1;
  970. }
  971. }
  972. } else {
  973. #ifdef ZEND_WIN32
  974. if (path_length > 2 && path[1] == ':' && !IS_SLASH(path[2])) {
  975. resolved_path[0] = path[0];
  976. resolved_path[1] = ':';
  977. resolved_path[2] = DEFAULT_SLASH;
  978. memcpy(resolved_path + 3, path + 2, path_length - 1);
  979. path_length++;
  980. } else
  981. #endif
  982. memcpy(resolved_path, path, path_length + 1);
  983. }
  984. #ifdef ZEND_WIN32
  985. if (memchr(resolved_path, '*', path_length) ||
  986. memchr(resolved_path, '?', path_length)) {
  987. SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_NAME);
  988. return 1;
  989. }
  990. #endif
  991. #ifdef ZEND_WIN32
  992. if (IS_UNC_PATH(resolved_path, path_length)) {
  993. /* skip UNC name */
  994. resolved_path[0] = DEFAULT_SLASH;
  995. resolved_path[1] = DEFAULT_SLASH;
  996. start = 2;
  997. while (!IS_SLASH(resolved_path[start])) {
  998. if (resolved_path[start] == 0) {
  999. goto verify;
  1000. }
  1001. resolved_path[start] = toupper(resolved_path[start]);
  1002. start++;
  1003. }
  1004. resolved_path[start++] = DEFAULT_SLASH;
  1005. while (!IS_SLASH(resolved_path[start])) {
  1006. if (resolved_path[start] == 0) {
  1007. goto verify;
  1008. }
  1009. resolved_path[start] = toupper(resolved_path[start]);
  1010. start++;
  1011. }
  1012. resolved_path[start++] = DEFAULT_SLASH;
  1013. } else if (IS_ABSOLUTE_PATH(resolved_path, path_length)) {
  1014. /* skip DRIVE name */
  1015. resolved_path[0] = toupper(resolved_path[0]);
  1016. resolved_path[2] = DEFAULT_SLASH;
  1017. start = 3;
  1018. }
  1019. #endif
  1020. add_slash = (use_realpath != CWD_REALPATH) && path_length > 0 && IS_SLASH(resolved_path[path_length-1]);
  1021. t = CWDG(realpath_cache_ttl) ? 0 : -1;
  1022. path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
  1023. if (path_length == (size_t)-1) {
  1024. errno = ENOENT;
  1025. return 1;
  1026. }
  1027. if (!start && !path_length) {
  1028. resolved_path[path_length++] = '.';
  1029. }
  1030. if (add_slash && path_length && !IS_SLASH(resolved_path[path_length-1])) {
  1031. if (path_length >= MAXPATHLEN-1) {
  1032. return -1;
  1033. }
  1034. resolved_path[path_length++] = DEFAULT_SLASH;
  1035. }
  1036. resolved_path[path_length] = 0;
  1037. #ifdef ZEND_WIN32
  1038. verify:
  1039. #endif
  1040. if (verify_path) {
  1041. cwd_state old_state;
  1042. CWD_STATE_COPY(&old_state, state);
  1043. state->cwd_length = path_length;
  1044. tmp = erealloc(state->cwd, state->cwd_length+1);
  1045. state->cwd = (char *) tmp;
  1046. memcpy(state->cwd, resolved_path, state->cwd_length+1);
  1047. if (verify_path(state)) {
  1048. CWD_STATE_FREE(state);
  1049. *state = old_state;
  1050. ret = 1;
  1051. } else {
  1052. CWD_STATE_FREE(&old_state);
  1053. ret = 0;
  1054. }
  1055. } else {
  1056. state->cwd_length = path_length;
  1057. tmp = erealloc(state->cwd, state->cwd_length+1);
  1058. state->cwd = (char *) tmp;
  1059. memcpy(state->cwd, resolved_path, state->cwd_length+1);
  1060. ret = 0;
  1061. }
  1062. #if VIRTUAL_CWD_DEBUG
  1063. fprintf (stderr, "virtual_file_ex() = %s\n",state->cwd);
  1064. #endif
  1065. return (ret);
  1066. }
  1067. /* }}} */
  1068. CWD_API zend_result virtual_chdir(const char *path) /* {{{ */
  1069. {
  1070. return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH) ? FAILURE : SUCCESS;
  1071. }
  1072. /* }}} */
  1073. /* returns 0 for ok, 1 for empty string, -1 on error */
  1074. CWD_API int virtual_chdir_file(const char *path, int (*p_chdir)(const char *path)) /* {{{ */
  1075. {
  1076. size_t length = strlen(path);
  1077. char *temp;
  1078. int retval;
  1079. ALLOCA_FLAG(use_heap)
  1080. if (length == 0) {
  1081. return 1; /* Can't cd to empty string */
  1082. }
  1083. while(--length < SIZE_MAX && !IS_SLASH(path[length])) {
  1084. }
  1085. if (length == SIZE_MAX) {
  1086. /* No directory only file name */
  1087. errno = ENOENT;
  1088. return -1;
  1089. }
  1090. if (length == COPY_WHEN_ABSOLUTE(path) && IS_ABSOLUTE_PATH(path, length+1)) { /* Also use trailing slash if this is absolute */
  1091. length++;
  1092. }
  1093. temp = (char *) do_alloca(length+1, use_heap);
  1094. memcpy(temp, path, length);
  1095. temp[length] = 0;
  1096. #if VIRTUAL_CWD_DEBUG
  1097. fprintf (stderr, "Changing directory to %s\n", temp);
  1098. #endif
  1099. retval = p_chdir(temp);
  1100. free_alloca(temp, use_heap);
  1101. return retval;
  1102. }
  1103. /* }}} */
  1104. CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */
  1105. {
  1106. cwd_state new_state;
  1107. char *retval;
  1108. char cwd[MAXPATHLEN];
  1109. /* realpath("") returns CWD */
  1110. if (!*path) {
  1111. new_state.cwd = (char*)emalloc(1);
  1112. new_state.cwd[0] = '\0';
  1113. new_state.cwd_length = 0;
  1114. if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
  1115. path = cwd;
  1116. }
  1117. } else if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
  1118. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1119. } else {
  1120. new_state.cwd = (char*)emalloc(1);
  1121. new_state.cwd[0] = '\0';
  1122. new_state.cwd_length = 0;
  1123. }
  1124. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)==0) {
  1125. size_t len = new_state.cwd_length>MAXPATHLEN-1?MAXPATHLEN-1:new_state.cwd_length;
  1126. memcpy(real_path, new_state.cwd, len);
  1127. real_path[len] = '\0';
  1128. retval = real_path;
  1129. } else {
  1130. retval = NULL;
  1131. }
  1132. CWD_STATE_FREE(&new_state);
  1133. return retval;
  1134. }
  1135. /* }}} */
  1136. /* returns 0 for ok, 1 for error, -1 if (path_length >= MAXPATHLEN-1) */
  1137. CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_func verify_path) /* {{{ */
  1138. {
  1139. cwd_state new_state;
  1140. int retval;
  1141. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1142. retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH);
  1143. *filepath = new_state.cwd;
  1144. return retval;
  1145. }
  1146. /* }}} */
  1147. /* returns 0 for ok, 1 for error, -1 if (path_length >= MAXPATHLEN-1) */
  1148. CWD_API int virtual_filepath(const char *path, char **filepath) /* {{{ */
  1149. {
  1150. return virtual_filepath_ex(path, filepath, php_is_file_ok);
  1151. }
  1152. /* }}} */
  1153. CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */
  1154. {
  1155. cwd_state new_state;
  1156. FILE *f;
  1157. if (path[0] == '\0') { /* Fail to open empty path */
  1158. return NULL;
  1159. }
  1160. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1161. if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
  1162. CWD_STATE_FREE_ERR(&new_state);
  1163. return NULL;
  1164. }
  1165. #ifdef ZEND_WIN32
  1166. f = php_win32_ioutil_fopen(new_state.cwd, mode);
  1167. #else
  1168. f = fopen(new_state.cwd, mode);
  1169. #endif
  1170. CWD_STATE_FREE_ERR(&new_state);
  1171. return f;
  1172. }
  1173. /* }}} */
  1174. CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */
  1175. {
  1176. cwd_state new_state;
  1177. int ret;
  1178. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1179. if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
  1180. CWD_STATE_FREE_ERR(&new_state);
  1181. return -1;
  1182. }
  1183. #if defined(ZEND_WIN32)
  1184. ret = tsrm_win32_access(new_state.cwd, mode);
  1185. #else
  1186. ret = access(new_state.cwd, mode);
  1187. #endif
  1188. CWD_STATE_FREE_ERR(&new_state);
  1189. return ret;
  1190. }
  1191. /* }}} */
  1192. #if HAVE_UTIME
  1193. CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */
  1194. {
  1195. cwd_state new_state;
  1196. int ret;
  1197. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1198. if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
  1199. CWD_STATE_FREE_ERR(&new_state);
  1200. return -1;
  1201. }
  1202. #ifdef ZEND_WIN32
  1203. ret = win32_utime(new_state.cwd, buf);
  1204. #else
  1205. ret = utime(new_state.cwd, buf);
  1206. #endif
  1207. CWD_STATE_FREE_ERR(&new_state);
  1208. return ret;
  1209. }
  1210. /* }}} */
  1211. #endif
  1212. CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */
  1213. {
  1214. cwd_state new_state;
  1215. int ret;
  1216. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1217. if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
  1218. CWD_STATE_FREE_ERR(&new_state);
  1219. return -1;
  1220. }
  1221. #ifdef ZEND_WIN32
  1222. {
  1223. mode_t _tmp = mode;
  1224. mode = 0;
  1225. if (_tmp & _S_IREAD) {
  1226. mode |= _S_IREAD;
  1227. }
  1228. if (_tmp & _S_IWRITE) {
  1229. mode |= _S_IWRITE;
  1230. }
  1231. ret = php_win32_ioutil_chmod(new_state.cwd, mode);
  1232. }
  1233. #else
  1234. ret = chmod(new_state.cwd, mode);
  1235. #endif
  1236. CWD_STATE_FREE_ERR(&new_state);
  1237. return ret;
  1238. }
  1239. /* }}} */
  1240. #if !defined(ZEND_WIN32)
  1241. CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int link) /* {{{ */
  1242. {
  1243. cwd_state new_state;
  1244. int ret;
  1245. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1246. if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) {
  1247. CWD_STATE_FREE_ERR(&new_state);
  1248. return -1;
  1249. }
  1250. if (link) {
  1251. #if HAVE_LCHOWN
  1252. ret = lchown(new_state.cwd, owner, group);
  1253. #else
  1254. ret = -1;
  1255. #endif
  1256. } else {
  1257. ret = chown(new_state.cwd, owner, group);
  1258. }
  1259. CWD_STATE_FREE_ERR(&new_state);
  1260. return ret;
  1261. }
  1262. /* }}} */
  1263. #endif
  1264. CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */
  1265. {
  1266. cwd_state new_state;
  1267. int f;
  1268. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1269. if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
  1270. CWD_STATE_FREE_ERR(&new_state);
  1271. return -1;
  1272. }
  1273. if (flags & O_CREAT) {
  1274. mode_t mode;
  1275. va_list arg;
  1276. va_start(arg, flags);
  1277. mode = (mode_t) va_arg(arg, int);
  1278. va_end(arg);
  1279. #ifdef ZEND_WIN32
  1280. f = php_win32_ioutil_open(new_state.cwd, flags, mode);
  1281. #else
  1282. f = open(new_state.cwd, flags, mode);
  1283. #endif
  1284. } else {
  1285. #ifdef ZEND_WIN32
  1286. f = php_win32_ioutil_open(new_state.cwd, flags);
  1287. #else
  1288. f = open(new_state.cwd, flags);
  1289. #endif
  1290. }
  1291. CWD_STATE_FREE_ERR(&new_state);
  1292. return f;
  1293. }
  1294. /* }}} */
  1295. CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */
  1296. {
  1297. cwd_state new_state;
  1298. int f;
  1299. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1300. if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) {
  1301. CWD_STATE_FREE_ERR(&new_state);
  1302. return -1;
  1303. }
  1304. f = creat(new_state.cwd, mode);
  1305. CWD_STATE_FREE_ERR(&new_state);
  1306. return f;
  1307. }
  1308. /* }}} */
  1309. CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */
  1310. {
  1311. cwd_state old_state;
  1312. cwd_state new_state;
  1313. int retval;
  1314. CWD_STATE_COPY(&old_state, &CWDG(cwd));
  1315. if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) {
  1316. CWD_STATE_FREE_ERR(&old_state);
  1317. return -1;
  1318. }
  1319. oldname = old_state.cwd;
  1320. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1321. if (virtual_file_ex(&new_state, newname, NULL, CWD_EXPAND)) {
  1322. CWD_STATE_FREE_ERR(&old_state);
  1323. CWD_STATE_FREE_ERR(&new_state);
  1324. return -1;
  1325. }
  1326. newname = new_state.cwd;
  1327. /* rename on windows will fail if newname already exists.
  1328. MoveFileEx has to be used */
  1329. #ifdef ZEND_WIN32
  1330. /* MoveFileEx returns 0 on failure, other way 'round for this function */
  1331. retval = php_win32_ioutil_rename(oldname, newname);
  1332. #else
  1333. retval = rename(oldname, newname);
  1334. #endif
  1335. CWD_STATE_FREE_ERR(&old_state);
  1336. CWD_STATE_FREE_ERR(&new_state);
  1337. return retval;
  1338. }
  1339. /* }}} */
  1340. CWD_API int virtual_stat(const char *path, zend_stat_t *buf) /* {{{ */
  1341. {
  1342. cwd_state new_state;
  1343. int retval;
  1344. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1345. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
  1346. CWD_STATE_FREE_ERR(&new_state);
  1347. return -1;
  1348. }
  1349. retval = php_sys_stat(new_state.cwd, buf);
  1350. CWD_STATE_FREE_ERR(&new_state);
  1351. return retval;
  1352. }
  1353. /* }}} */
  1354. CWD_API int virtual_lstat(const char *path, zend_stat_t *buf) /* {{{ */
  1355. {
  1356. cwd_state new_state;
  1357. int retval;
  1358. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1359. if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
  1360. CWD_STATE_FREE_ERR(&new_state);
  1361. return -1;
  1362. }
  1363. retval = php_sys_lstat(new_state.cwd, buf);
  1364. CWD_STATE_FREE_ERR(&new_state);
  1365. return retval;
  1366. }
  1367. /* }}} */
  1368. CWD_API int virtual_unlink(const char *path) /* {{{ */
  1369. {
  1370. cwd_state new_state;
  1371. int retval;
  1372. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1373. if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) {
  1374. CWD_STATE_FREE_ERR(&new_state);
  1375. return -1;
  1376. }
  1377. #ifdef ZEND_WIN32
  1378. retval = php_win32_ioutil_unlink(new_state.cwd);
  1379. #else
  1380. retval = unlink(new_state.cwd);
  1381. #endif
  1382. CWD_STATE_FREE_ERR(&new_state);
  1383. return retval;
  1384. }
  1385. /* }}} */
  1386. CWD_API int virtual_mkdir(const char *pathname, mode_t mode) /* {{{ */
  1387. {
  1388. cwd_state new_state;
  1389. int retval;
  1390. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1391. if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) {
  1392. CWD_STATE_FREE_ERR(&new_state);
  1393. return -1;
  1394. }
  1395. #ifdef ZEND_WIN32
  1396. retval = php_win32_ioutil_mkdir(new_state.cwd, mode);
  1397. #else
  1398. retval = mkdir(new_state.cwd, mode);
  1399. #endif
  1400. CWD_STATE_FREE_ERR(&new_state);
  1401. return retval;
  1402. }
  1403. /* }}} */
  1404. CWD_API int virtual_rmdir(const char *pathname) /* {{{ */
  1405. {
  1406. cwd_state new_state;
  1407. int retval;
  1408. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1409. if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) {
  1410. CWD_STATE_FREE_ERR(&new_state);
  1411. return -1;
  1412. }
  1413. #ifdef ZEND_WIN32
  1414. retval = php_win32_ioutil_rmdir(new_state.cwd);
  1415. #else
  1416. retval = rmdir(new_state.cwd);
  1417. #endif
  1418. CWD_STATE_FREE_ERR(&new_state);
  1419. return retval;
  1420. }
  1421. /* }}} */
  1422. #ifdef ZEND_WIN32
  1423. DIR *opendir(const char *name);
  1424. #endif
  1425. CWD_API DIR *virtual_opendir(const char *pathname) /* {{{ */
  1426. {
  1427. cwd_state new_state;
  1428. DIR *retval;
  1429. CWD_STATE_COPY(&new_state, &CWDG(cwd));
  1430. if (virtual_file_ex(&new_state, pathname, NULL, CWD_REALPATH)) {
  1431. CWD_STATE_FREE_ERR(&new_state);
  1432. return NULL;
  1433. }
  1434. retval = opendir(new_state.cwd);
  1435. CWD_STATE_FREE_ERR(&new_state);
  1436. return retval;
  1437. }
  1438. /* }}} */
  1439. #ifdef ZEND_WIN32
  1440. CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
  1441. {
  1442. return popen_ex(command, type, CWDG(cwd).cwd, NULL);
  1443. }
  1444. /* }}} */
  1445. #else /* Unix */
  1446. CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */
  1447. {
  1448. size_t command_length;
  1449. int dir_length, extra = 0;
  1450. char *command_line;
  1451. char *ptr, *dir;
  1452. FILE *retval;
  1453. command_length = strlen(command);
  1454. dir_length = CWDG(cwd).cwd_length;
  1455. dir = CWDG(cwd).cwd;
  1456. while (dir_length > 0) {
  1457. if (*dir == '\'') extra+=3;
  1458. dir++;
  1459. dir_length--;
  1460. }
  1461. dir_length = CWDG(cwd).cwd_length;
  1462. dir = CWDG(cwd).cwd;
  1463. ptr = command_line = (char *) emalloc(command_length + sizeof("cd '' ; ") + dir_length + extra+1+1);
  1464. memcpy(ptr, "cd ", sizeof("cd ")-1);
  1465. ptr += sizeof("cd ")-1;
  1466. if (CWDG(cwd).cwd_length == 0) {
  1467. *ptr++ = DEFAULT_SLASH;
  1468. } else {
  1469. *ptr++ = '\'';
  1470. while (dir_length > 0) {
  1471. switch (*dir) {
  1472. case '\'':
  1473. *ptr++ = '\'';
  1474. *ptr++ = '\\';
  1475. *ptr++ = '\'';
  1476. ZEND_FALLTHROUGH;
  1477. default:
  1478. *ptr++ = *dir;
  1479. }
  1480. dir++;
  1481. dir_length--;
  1482. }
  1483. *ptr++ = '\'';
  1484. }
  1485. *ptr++ = ' ';
  1486. *ptr++ = ';';
  1487. *ptr++ = ' ';
  1488. memcpy(ptr, command, command_length+1);
  1489. retval = popen(command_line, type);
  1490. efree(command_line);
  1491. return retval;
  1492. }
  1493. /* }}} */
  1494. #endif
  1495. CWD_API char *tsrm_realpath(const char *path, char *real_path) /* {{{ */
  1496. {
  1497. cwd_state new_state;
  1498. char cwd[MAXPATHLEN];
  1499. /* realpath("") returns CWD */
  1500. if (!*path) {
  1501. new_state.cwd = (char*)emalloc(1);
  1502. new_state.cwd[0] = '\0';
  1503. new_state.cwd_length = 0;
  1504. if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
  1505. path = cwd;
  1506. }
  1507. } else if (!IS_ABSOLUTE_PATH(path, strlen(path)) &&
  1508. VCWD_GETCWD(cwd, MAXPATHLEN)) {
  1509. new_state.cwd = estrdup(cwd);
  1510. new_state.cwd_length = strlen(cwd);
  1511. } else {
  1512. new_state.cwd = (char*)emalloc(1);
  1513. new_state.cwd[0] = '\0';
  1514. new_state.cwd_length = 0;
  1515. }
  1516. if (virtual_file_ex(&new_state, path, NULL, CWD_REALPATH)) {
  1517. efree(new_state.cwd);
  1518. return NULL;
  1519. }
  1520. if (real_path) {
  1521. size_t copy_len = new_state.cwd_length>MAXPATHLEN-1 ? MAXPATHLEN-1 : new_state.cwd_length;
  1522. memcpy(real_path, new_state.cwd, copy_len);
  1523. real_path[copy_len] = '\0';
  1524. efree(new_state.cwd);
  1525. return real_path;
  1526. } else {
  1527. return new_state.cwd;
  1528. }
  1529. }
  1530. /* }}} */