fopen_wrappers.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  16. | Jim Winstead <jimw@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. /* {{{ includes
  21. */
  22. #include "php.h"
  23. #include "php_globals.h"
  24. #include "SAPI.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #ifdef PHP_WIN32
  32. #define O_RDONLY _O_RDONLY
  33. #include "win32/param.h"
  34. #else
  35. #include <sys/param.h>
  36. #endif
  37. #include "ext/standard/head.h"
  38. #include "ext/standard/php_standard.h"
  39. #include "zend_compile.h"
  40. #include "php_network.h"
  41. #if HAVE_PWD_H
  42. #include <pwd.h>
  43. #endif
  44. #include <sys/types.h>
  45. #if HAVE_SYS_SOCKET_H
  46. #include <sys/socket.h>
  47. #endif
  48. #ifndef S_ISREG
  49. #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
  50. #endif
  51. #ifdef PHP_WIN32
  52. #include <winsock2.h>
  53. #elif defined(NETWARE) && defined(USE_WINSOCK)
  54. #include <novsock2.h>
  55. #else
  56. #include <netinet/in.h>
  57. #include <netdb.h>
  58. #if HAVE_ARPA_INET_H
  59. #include <arpa/inet.h>
  60. #endif
  61. #endif
  62. #if defined(PHP_WIN32) || defined(__riscos__) || defined(NETWARE)
  63. #undef AF_UNIX
  64. #endif
  65. #if defined(AF_UNIX)
  66. #include <sys/un.h>
  67. #endif
  68. /* }}} */
  69. /* {{{ OnUpdateBaseDir
  70. Allows any change to open_basedir setting in during Startup and Shutdown events,
  71. or a tightening during activation/runtime/deactivation */
  72. PHPAPI ZEND_INI_MH(OnUpdateBaseDir)
  73. {
  74. char **p, *pathbuf, *ptr, *end;
  75. #ifndef ZTS
  76. char *base = (char *) mh_arg2;
  77. #else
  78. char *base = (char *) ts_resource(*((int *) mh_arg2));
  79. #endif
  80. p = (char **) (base + (size_t) mh_arg1);
  81. if (stage == PHP_INI_STAGE_STARTUP || stage == PHP_INI_STAGE_SHUTDOWN || stage == PHP_INI_STAGE_ACTIVATE || stage == PHP_INI_STAGE_DEACTIVATE) {
  82. /* We're in a PHP_INI_SYSTEM context, no restrictions */
  83. *p = new_value;
  84. return SUCCESS;
  85. }
  86. /* Otherwise we're in runtime */
  87. if (!*p || !**p) {
  88. /* open_basedir not set yet, go ahead and give it a value */
  89. *p = new_value;
  90. return SUCCESS;
  91. }
  92. /* Shortcut: When we have a open_basedir and someone tries to unset, we know it'll fail */
  93. if (!new_value || !*new_value) {
  94. return FAILURE;
  95. }
  96. /* Is the proposed open_basedir at least as restrictive as the current setting? */
  97. ptr = pathbuf = estrdup(new_value);
  98. while (ptr && *ptr) {
  99. end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  100. if (end != NULL) {
  101. *end = '\0';
  102. end++;
  103. }
  104. if (php_check_open_basedir_ex(ptr, 0 TSRMLS_CC) != 0) {
  105. /* At least one portion of this open_basedir is less restrictive than the prior one, FAIL */
  106. efree(pathbuf);
  107. return FAILURE;
  108. }
  109. ptr = end;
  110. }
  111. efree(pathbuf);
  112. /* Everything checks out, set it */
  113. *p = new_value;
  114. return SUCCESS;
  115. }
  116. /* }}} */
  117. /* {{{ php_check_specific_open_basedir
  118. When open_basedir is not NULL, check if the given filename is located in
  119. open_basedir. Returns -1 if error or not in the open_basedir, else 0.
  120. When open_basedir is NULL, always return 0.
  121. */
  122. PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path TSRMLS_DC)
  123. {
  124. char resolved_name[MAXPATHLEN];
  125. char resolved_basedir[MAXPATHLEN];
  126. char local_open_basedir[MAXPATHLEN];
  127. char path_tmp[MAXPATHLEN];
  128. char *path_file;
  129. int resolved_basedir_len;
  130. int resolved_name_len;
  131. int path_len;
  132. int nesting_level = 0;
  133. /* Special case basedir==".": Use script-directory */
  134. if (strcmp(basedir, ".") || !VCWD_GETCWD(local_open_basedir, MAXPATHLEN)) {
  135. /* Else use the unmodified path */
  136. strlcpy(local_open_basedir, basedir, sizeof(local_open_basedir));
  137. }
  138. path_len = strlen(path);
  139. if (path_len > (MAXPATHLEN - 1)) {
  140. /* empty and too long paths are invalid */
  141. return -1;
  142. }
  143. /* normalize and expand path */
  144. if (expand_filepath(path, resolved_name TSRMLS_CC) == NULL) {
  145. return -1;
  146. }
  147. path_len = strlen(resolved_name);
  148. memcpy(path_tmp, resolved_name, path_len + 1); /* safe */
  149. while (VCWD_REALPATH(path_tmp, resolved_name) == NULL) {
  150. #if defined(PHP_WIN32) || defined(HAVE_SYMLINK)
  151. #if defined(PHP_WIN32)
  152. if (EG(windows_version_info).dwMajorVersion > 5) {
  153. #endif
  154. if (nesting_level == 0) {
  155. int ret;
  156. char buf[MAXPATHLEN];
  157. ret = php_sys_readlink(path_tmp, buf, MAXPATHLEN - 1);
  158. if (ret < 0) {
  159. /* not a broken symlink, move along.. */
  160. } else {
  161. /* put the real path into the path buffer */
  162. memcpy(path_tmp, buf, ret);
  163. path_tmp[ret] = '\0';
  164. }
  165. }
  166. #if defined(PHP_WIN32)
  167. }
  168. #endif
  169. #endif
  170. #if defined(PHP_WIN32) || defined(NETWARE)
  171. path_file = strrchr(path_tmp, DEFAULT_SLASH);
  172. if (!path_file) {
  173. path_file = strrchr(path_tmp, '/');
  174. }
  175. #else
  176. path_file = strrchr(path_tmp, DEFAULT_SLASH);
  177. #endif
  178. if (!path_file) {
  179. /* none of the path components exist. definitely not in open_basedir.. */
  180. return -1;
  181. } else {
  182. path_len = path_file - path_tmp + 1;
  183. #if defined(PHP_WIN32) || defined(NETWARE)
  184. if (path_len > 1 && path_tmp[path_len - 2] == ':') {
  185. if (path_len != 3) {
  186. return -1;
  187. }
  188. /* this is c:\ */
  189. path_tmp[path_len] = '\0';
  190. } else {
  191. path_tmp[path_len - 1] = '\0';
  192. }
  193. #else
  194. path_tmp[path_len - 1] = '\0';
  195. #endif
  196. }
  197. nesting_level++;
  198. }
  199. /* Resolve open_basedir to resolved_basedir */
  200. if (expand_filepath(local_open_basedir, resolved_basedir TSRMLS_CC) != NULL) {
  201. /* Handler for basedirs that end with a / */
  202. resolved_basedir_len = strlen(resolved_basedir);
  203. #if defined(PHP_WIN32) || defined(NETWARE)
  204. if (basedir[strlen(basedir) - 1] == PHP_DIR_SEPARATOR || basedir[strlen(basedir) - 1] == '/') {
  205. #else
  206. if (basedir[strlen(basedir) - 1] == PHP_DIR_SEPARATOR) {
  207. #endif
  208. if (resolved_basedir[resolved_basedir_len - 1] != PHP_DIR_SEPARATOR) {
  209. resolved_basedir[resolved_basedir_len] = PHP_DIR_SEPARATOR;
  210. resolved_basedir[++resolved_basedir_len] = '\0';
  211. }
  212. } else {
  213. resolved_basedir[resolved_basedir_len++] = PHP_DIR_SEPARATOR;
  214. resolved_basedir[resolved_basedir_len] = '\0';
  215. }
  216. resolved_name_len = strlen(resolved_name);
  217. if (path_tmp[path_len - 1] == PHP_DIR_SEPARATOR) {
  218. if (resolved_name[resolved_name_len - 1] != PHP_DIR_SEPARATOR) {
  219. resolved_name[resolved_name_len] = PHP_DIR_SEPARATOR;
  220. resolved_name[++resolved_name_len] = '\0';
  221. }
  222. }
  223. /* Check the path */
  224. #if defined(PHP_WIN32) || defined(NETWARE)
  225. if (strncasecmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) {
  226. #else
  227. if (strncmp(resolved_basedir, resolved_name, resolved_basedir_len) == 0) {
  228. #endif
  229. if (resolved_name_len > resolved_basedir_len &&
  230. resolved_name[resolved_basedir_len - 1] != PHP_DIR_SEPARATOR) {
  231. return -1;
  232. } else {
  233. /* File is in the right directory */
  234. return 0;
  235. }
  236. } else {
  237. /* /openbasedir/ and /openbasedir are the same directory */
  238. if (resolved_basedir_len == (resolved_name_len + 1) && resolved_basedir[resolved_basedir_len - 1] == PHP_DIR_SEPARATOR) {
  239. #if defined(PHP_WIN32) || defined(NETWARE)
  240. if (strncasecmp(resolved_basedir, resolved_name, resolved_name_len) == 0) {
  241. #else
  242. if (strncmp(resolved_basedir, resolved_name, resolved_name_len) == 0) {
  243. #endif
  244. return 0;
  245. }
  246. }
  247. return -1;
  248. }
  249. } else {
  250. /* Unable to resolve the real path, return -1 */
  251. return -1;
  252. }
  253. }
  254. /* }}} */
  255. PHPAPI int php_check_open_basedir(const char *path TSRMLS_DC)
  256. {
  257. return php_check_open_basedir_ex(path, 1 TSRMLS_CC);
  258. }
  259. /* {{{ php_check_open_basedir
  260. */
  261. PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
  262. {
  263. /* Only check when open_basedir is available */
  264. if (PG(open_basedir) && *PG(open_basedir)) {
  265. char *pathbuf;
  266. char *ptr;
  267. char *end;
  268. /* Check if the path is too long so we can give a more useful error
  269. * message. */
  270. if (strlen(path) > (MAXPATHLEN - 1)) {
  271. php_error_docref(NULL TSRMLS_CC, E_WARNING, "File name is longer than the maximum allowed path length on this platform (%d): %s", MAXPATHLEN, path);
  272. errno = EINVAL;
  273. return -1;
  274. }
  275. pathbuf = estrdup(PG(open_basedir));
  276. ptr = pathbuf;
  277. while (ptr && *ptr) {
  278. end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  279. if (end != NULL) {
  280. *end = '\0';
  281. end++;
  282. }
  283. if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
  284. efree(pathbuf);
  285. return 0;
  286. }
  287. ptr = end;
  288. }
  289. if (warn) {
  290. php_error_docref(NULL TSRMLS_CC, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir));
  291. }
  292. efree(pathbuf);
  293. errno = EPERM; /* we deny permission to open it */
  294. return -1;
  295. }
  296. /* Nothing to check... */
  297. return 0;
  298. }
  299. /* }}} */
  300. /* {{{ php_fopen_and_set_opened_path
  301. */
  302. static FILE *php_fopen_and_set_opened_path(const char *path, const char *mode, char **opened_path TSRMLS_DC)
  303. {
  304. FILE *fp;
  305. if (php_check_open_basedir((char *)path TSRMLS_CC)) {
  306. return NULL;
  307. }
  308. fp = VCWD_FOPEN(path, mode);
  309. if (fp && opened_path) {
  310. *opened_path = expand_filepath_with_mode(path, NULL, NULL, 0, CWD_EXPAND TSRMLS_CC);
  311. }
  312. return fp;
  313. }
  314. /* }}} */
  315. /* {{{ php_fopen_primary_script
  316. */
  317. PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC)
  318. {
  319. char *path_info;
  320. char *filename = NULL;
  321. char *resolved_path = NULL;
  322. int length;
  323. zend_bool orig_display_errors;
  324. path_info = SG(request_info).request_uri;
  325. #if HAVE_PWD_H
  326. if (PG(user_dir) && *PG(user_dir) && path_info && '/' == path_info[0] && '~' == path_info[1]) {
  327. char *s = strchr(path_info + 2, '/');
  328. if (s) { /* if there is no path name after the file, do not bother */
  329. char user[32]; /* to try open the directory */
  330. struct passwd *pw;
  331. #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
  332. struct passwd pwstruc;
  333. long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  334. char *pwbuf;
  335. if (pwbuflen < 1) {
  336. return FAILURE;
  337. }
  338. pwbuf = emalloc(pwbuflen);
  339. #endif
  340. length = s - (path_info + 2);
  341. if (length > (int)sizeof(user) - 1) {
  342. length = sizeof(user) - 1;
  343. }
  344. memcpy(user, path_info + 2, length);
  345. user[length] = '\0';
  346. #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
  347. if (getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw)) {
  348. efree(pwbuf);
  349. return FAILURE;
  350. }
  351. #else
  352. pw = getpwnam(user);
  353. #endif
  354. if (pw && pw->pw_dir) {
  355. spprintf(&filename, 0, "%s%c%s%c%s", pw->pw_dir, PHP_DIR_SEPARATOR, PG(user_dir), PHP_DIR_SEPARATOR, s + 1); /* Safe */
  356. } else {
  357. filename = SG(request_info).path_translated;
  358. }
  359. #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
  360. efree(pwbuf);
  361. #endif
  362. }
  363. } else
  364. #endif
  365. if (PG(doc_root) && path_info && (length = strlen(PG(doc_root))) &&
  366. IS_ABSOLUTE_PATH(PG(doc_root), length)) {
  367. int path_len = strlen(path_info);
  368. filename = emalloc(length + path_len + 2);
  369. if (filename) {
  370. memcpy(filename, PG(doc_root), length);
  371. if (!IS_SLASH(filename[length - 1])) { /* length is never 0 */
  372. filename[length++] = PHP_DIR_SEPARATOR;
  373. }
  374. if (IS_SLASH(path_info[0])) {
  375. length--;
  376. }
  377. strncpy(filename + length, path_info, path_len + 1);
  378. }
  379. } else {
  380. filename = SG(request_info).path_translated;
  381. }
  382. if (filename) {
  383. resolved_path = zend_resolve_path(filename, strlen(filename) TSRMLS_CC);
  384. }
  385. if (!resolved_path) {
  386. if (SG(request_info).path_translated != filename) {
  387. STR_FREE(filename);
  388. }
  389. /* we have to free SG(request_info).path_translated here because
  390. * php_destroy_request_info assumes that it will get
  391. * freed when the include_names hash is emptied, but
  392. * we're not adding it in this case */
  393. STR_FREE(SG(request_info).path_translated);
  394. SG(request_info).path_translated = NULL;
  395. return FAILURE;
  396. }
  397. efree(resolved_path);
  398. orig_display_errors = PG(display_errors);
  399. PG(display_errors) = 0;
  400. if (zend_stream_open(filename, file_handle TSRMLS_CC) == FAILURE) {
  401. PG(display_errors) = orig_display_errors;
  402. if (SG(request_info).path_translated != filename) {
  403. STR_FREE(filename);
  404. }
  405. STR_FREE(SG(request_info).path_translated); /* for same reason as above */
  406. SG(request_info).path_translated = NULL;
  407. return FAILURE;
  408. }
  409. PG(display_errors) = orig_display_errors;
  410. if (SG(request_info).path_translated != filename) {
  411. STR_FREE(SG(request_info).path_translated); /* for same reason as above */
  412. SG(request_info).path_translated = filename;
  413. }
  414. return SUCCESS;
  415. }
  416. /* }}} */
  417. /* {{{ php_resolve_path
  418. * Returns the realpath for given filename according to include path
  419. */
  420. PHPAPI char *php_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC)
  421. {
  422. char resolved_path[MAXPATHLEN];
  423. char trypath[MAXPATHLEN];
  424. const char *ptr, *end, *p;
  425. const char *actual_path;
  426. php_stream_wrapper *wrapper;
  427. if (!filename || CHECK_NULL_PATH(filename, filename_length)) {
  428. return NULL;
  429. }
  430. /* Don't resolve paths which contain protocol (except of file://) */
  431. for (p = filename; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
  432. if ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/')) {
  433. wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
  434. if (wrapper == &php_plain_files_wrapper) {
  435. if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
  436. return estrdup(resolved_path);
  437. }
  438. }
  439. return NULL;
  440. }
  441. if ((*filename == '.' &&
  442. (IS_SLASH(filename[1]) ||
  443. ((filename[1] == '.') && IS_SLASH(filename[2])))) ||
  444. IS_ABSOLUTE_PATH(filename, filename_length) ||
  445. !path ||
  446. !*path) {
  447. if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) {
  448. return estrdup(resolved_path);
  449. } else {
  450. return NULL;
  451. }
  452. }
  453. ptr = path;
  454. while (ptr && *ptr) {
  455. /* Check for stream wrapper */
  456. int is_stream_wrapper = 0;
  457. for (p = ptr; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
  458. if ((*p == ':') && (p - ptr > 1) && (p[1] == '/') && (p[2] == '/')) {
  459. /* .:// or ..:// is not a stream wrapper */
  460. if (p[-1] != '.' || p[-2] != '.' || p - 2 != ptr) {
  461. p += 3;
  462. is_stream_wrapper = 1;
  463. }
  464. }
  465. end = strchr(p, DEFAULT_DIR_SEPARATOR);
  466. if (end) {
  467. if (filename_length > (MAXPATHLEN - 2) || (end-ptr) > MAXPATHLEN || (end-ptr) + 1 + (size_t)filename_length + 1 >= MAXPATHLEN) {
  468. ptr = end + 1;
  469. continue;
  470. }
  471. memcpy(trypath, ptr, end-ptr);
  472. trypath[end-ptr] = '/';
  473. memcpy(trypath+(end-ptr)+1, filename, filename_length+1);
  474. ptr = end+1;
  475. } else {
  476. size_t len = strlen(ptr);
  477. if (filename_length > (MAXPATHLEN - 2) || len > MAXPATHLEN || (size_t)len + 1 + (size_t)filename_length + 1 >= MAXPATHLEN) {
  478. break;
  479. }
  480. memcpy(trypath, ptr, len);
  481. trypath[len] = '/';
  482. memcpy(trypath+len+1, filename, filename_length+1);
  483. ptr = NULL;
  484. }
  485. actual_path = trypath;
  486. if (is_stream_wrapper) {
  487. wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
  488. if (!wrapper) {
  489. continue;
  490. } else if (wrapper != &php_plain_files_wrapper) {
  491. if (wrapper->wops->url_stat) {
  492. php_stream_statbuf ssb;
  493. if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
  494. return estrdup(trypath);
  495. }
  496. }
  497. continue;
  498. }
  499. }
  500. if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
  501. return estrdup(resolved_path);
  502. }
  503. } /* end provided path */
  504. /* check in calling scripts' current working directory as a fall back case
  505. */
  506. if (zend_is_executing(TSRMLS_C)) {
  507. const char *exec_fname = zend_get_executed_filename(TSRMLS_C);
  508. int exec_fname_length = strlen(exec_fname);
  509. while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
  510. if (exec_fname && exec_fname[0] != '[' &&
  511. exec_fname_length > 0 &&
  512. filename_length < (MAXPATHLEN - 2) &&
  513. exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {
  514. memcpy(trypath, exec_fname, exec_fname_length + 1);
  515. memcpy(trypath+exec_fname_length + 1, filename, filename_length+1);
  516. actual_path = trypath;
  517. /* Check for stream wrapper */
  518. for (p = trypath; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
  519. if ((*p == ':') && (p - trypath > 1) && (p[1] == '/') && (p[2] == '/')) {
  520. wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
  521. if (!wrapper) {
  522. return NULL;
  523. } else if (wrapper != &php_plain_files_wrapper) {
  524. if (wrapper->wops->url_stat) {
  525. php_stream_statbuf ssb;
  526. if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
  527. return estrdup(trypath);
  528. }
  529. }
  530. return NULL;
  531. }
  532. }
  533. if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
  534. return estrdup(resolved_path);
  535. }
  536. }
  537. }
  538. return NULL;
  539. }
  540. /* }}} */
  541. /* {{{ php_fopen_with_path
  542. * Tries to open a file with a PATH-style list of directories.
  543. * If the filename starts with "." or "/", the path is ignored.
  544. */
  545. PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path TSRMLS_DC)
  546. {
  547. char *pathbuf, *ptr, *end;
  548. const char *exec_fname;
  549. char trypath[MAXPATHLEN];
  550. FILE *fp;
  551. int path_length;
  552. int filename_length;
  553. int exec_fname_length;
  554. if (opened_path) {
  555. *opened_path = NULL;
  556. }
  557. if (!filename) {
  558. return NULL;
  559. }
  560. filename_length = strlen(filename);
  561. /* Relative path open */
  562. if ((*filename == '.')
  563. /* Absolute path open */
  564. || IS_ABSOLUTE_PATH(filename, filename_length)
  565. || (!path || (path && !*path))
  566. ) {
  567. return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC);
  568. }
  569. /* check in provided path */
  570. /* append the calling scripts' current working directory
  571. * as a fall back case
  572. */
  573. if (zend_is_executing(TSRMLS_C)) {
  574. exec_fname = zend_get_executed_filename(TSRMLS_C);
  575. exec_fname_length = strlen(exec_fname);
  576. path_length = strlen(path);
  577. while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
  578. if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) {
  579. /* [no active file] or no path */
  580. pathbuf = estrdup(path);
  581. } else {
  582. pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1);
  583. memcpy(pathbuf, path, path_length);
  584. pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
  585. memcpy(pathbuf + path_length + 1, exec_fname, exec_fname_length);
  586. pathbuf[path_length + exec_fname_length + 1] = '\0';
  587. }
  588. } else {
  589. pathbuf = estrdup(path);
  590. }
  591. ptr = pathbuf;
  592. while (ptr && *ptr) {
  593. end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  594. if (end != NULL) {
  595. *end = '\0';
  596. end++;
  597. }
  598. if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
  599. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
  600. }
  601. fp = php_fopen_and_set_opened_path(trypath, mode, opened_path TSRMLS_CC);
  602. if (fp) {
  603. efree(pathbuf);
  604. return fp;
  605. }
  606. ptr = end;
  607. } /* end provided path */
  608. efree(pathbuf);
  609. return NULL;
  610. }
  611. /* }}} */
  612. /* {{{ php_strip_url_passwd
  613. */
  614. PHPAPI char *php_strip_url_passwd(char *url)
  615. {
  616. register char *p, *url_start;
  617. if (url == NULL) {
  618. return "";
  619. }
  620. p = url;
  621. while (*p) {
  622. if (*p == ':' && *(p + 1) == '/' && *(p + 2) == '/') {
  623. /* found protocol */
  624. url_start = p = p + 3;
  625. while (*p) {
  626. if (*p == '@') {
  627. int i;
  628. for (i = 0; i < 3 && url_start < p; i++, url_start++) {
  629. *url_start = '.';
  630. }
  631. for (; *p; p++) {
  632. *url_start++ = *p;
  633. }
  634. *url_start=0;
  635. break;
  636. }
  637. p++;
  638. }
  639. return url;
  640. }
  641. p++;
  642. }
  643. return url;
  644. }
  645. /* }}} */
  646. /* {{{ expand_filepath
  647. */
  648. PHPAPI char *expand_filepath(const char *filepath, char *real_path TSRMLS_DC)
  649. {
  650. return expand_filepath_ex(filepath, real_path, NULL, 0 TSRMLS_CC);
  651. }
  652. /* }}} */
  653. /* {{{ expand_filepath_ex
  654. */
  655. PHPAPI char *expand_filepath_ex(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len TSRMLS_DC)
  656. {
  657. return expand_filepath_with_mode(filepath, real_path, relative_to, relative_to_len, CWD_FILEPATH TSRMLS_CC);
  658. }
  659. /* }}} */
  660. /* {{{ expand_filepath_use_realpath
  661. */
  662. PHPAPI char *expand_filepath_with_mode(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len, int realpath_mode TSRMLS_DC)
  663. {
  664. cwd_state new_state;
  665. char cwd[MAXPATHLEN];
  666. int copy_len;
  667. if (!filepath[0]) {
  668. return NULL;
  669. } else if (IS_ABSOLUTE_PATH(filepath, strlen(filepath))) {
  670. cwd[0] = '\0';
  671. } else {
  672. const char *iam = SG(request_info).path_translated;
  673. const char *result;
  674. if (relative_to) {
  675. if (relative_to_len > MAXPATHLEN-1U) {
  676. return NULL;
  677. }
  678. result = relative_to;
  679. memcpy(cwd, relative_to, relative_to_len+1U);
  680. } else {
  681. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  682. }
  683. if (!result && (iam != filepath)) {
  684. int fdtest = -1;
  685. fdtest = VCWD_OPEN(filepath, O_RDONLY);
  686. if (fdtest != -1) {
  687. /* return a relative file path if for any reason
  688. * we cannot cannot getcwd() and the requested,
  689. * relatively referenced file is accessible */
  690. copy_len = strlen(filepath) > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : strlen(filepath);
  691. if (real_path) {
  692. memcpy(real_path, filepath, copy_len);
  693. real_path[copy_len] = '\0';
  694. } else {
  695. real_path = estrndup(filepath, copy_len);
  696. }
  697. close(fdtest);
  698. return real_path;
  699. } else {
  700. cwd[0] = '\0';
  701. }
  702. } else if (!result) {
  703. cwd[0] = '\0';
  704. }
  705. }
  706. new_state.cwd = estrdup(cwd);
  707. new_state.cwd_length = strlen(cwd);
  708. if (virtual_file_ex(&new_state, filepath, NULL, realpath_mode TSRMLS_CC)) {
  709. efree(new_state.cwd);
  710. return NULL;
  711. }
  712. if (real_path) {
  713. copy_len = new_state.cwd_length > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : new_state.cwd_length;
  714. memcpy(real_path, new_state.cwd, copy_len);
  715. real_path[copy_len] = '\0';
  716. } else {
  717. real_path = estrndup(new_state.cwd, new_state.cwd_length);
  718. }
  719. efree(new_state.cwd);
  720. return real_path;
  721. }
  722. /* }}} */
  723. /*
  724. * Local variables:
  725. * tab-width: 4
  726. * c-basic-offset: 4
  727. * End:
  728. * vim600: sw=4 ts=4 fdm=marker
  729. * vim<600: sw=4 ts=4
  730. */