PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/main/fopen_wrappers.c

http://github.com/infusion/PHP
C | 832 lines | 620 code | 98 blank | 114 comment | 280 complexity | 50ec4c7653cc835fd67bce367b62e7c7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 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: fopen_wrappers.c 307563 2011-01-18 22:20:09Z pajoye $ */
  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(path, NULL 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. FILE *fp;
  320. #ifndef PHP_WIN32
  321. struct stat st;
  322. #endif
  323. char *path_info;
  324. char *filename = NULL;
  325. char *resolved_path = NULL;
  326. int length;
  327. path_info = SG(request_info).request_uri;
  328. #if HAVE_PWD_H
  329. if (PG(user_dir) && *PG(user_dir) && path_info && '/' == path_info[0] && '~' == path_info[1]) {
  330. char *s = strchr(path_info + 2, '/');
  331. if (s) { /* if there is no path name after the file, do not bother */
  332. char user[32]; /* to try open the directory */
  333. struct passwd *pw;
  334. #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
  335. struct passwd pwstruc;
  336. long pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
  337. char *pwbuf;
  338. if (pwbuflen < 1) {
  339. return FAILURE;
  340. }
  341. pwbuf = emalloc(pwbuflen);
  342. #endif
  343. length = s - (path_info + 2);
  344. if (length > (int)sizeof(user) - 1) {
  345. length = sizeof(user) - 1;
  346. }
  347. memcpy(user, path_info + 2, length);
  348. user[length] = '\0';
  349. #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
  350. if (getpwnam_r(user, &pwstruc, pwbuf, pwbuflen, &pw)) {
  351. efree(pwbuf);
  352. return FAILURE;
  353. }
  354. #else
  355. pw = getpwnam(user);
  356. #endif
  357. if (pw && pw->pw_dir) {
  358. spprintf(&filename, 0, "%s%c%s%c%s", pw->pw_dir, PHP_DIR_SEPARATOR, PG(user_dir), PHP_DIR_SEPARATOR, s + 1); /* Safe */
  359. } else {
  360. filename = SG(request_info).path_translated;
  361. }
  362. #if defined(ZTS) && defined(HAVE_GETPWNAM_R) && defined(_SC_GETPW_R_SIZE_MAX)
  363. efree(pwbuf);
  364. #endif
  365. }
  366. } else
  367. #endif
  368. if (PG(doc_root) && path_info && (length = strlen(PG(doc_root))) &&
  369. IS_ABSOLUTE_PATH(PG(doc_root), length)) {
  370. filename = emalloc(length + strlen(path_info) + 2);
  371. if (filename) {
  372. memcpy(filename, PG(doc_root), length);
  373. if (!IS_SLASH(filename[length - 1])) { /* length is never 0 */
  374. filename[length++] = PHP_DIR_SEPARATOR;
  375. }
  376. if (IS_SLASH(path_info[0])) {
  377. length--;
  378. }
  379. strcpy(filename + length, path_info);
  380. }
  381. } else {
  382. filename = SG(request_info).path_translated;
  383. }
  384. if (filename) {
  385. resolved_path = zend_resolve_path(filename, strlen(filename) TSRMLS_CC);
  386. }
  387. if (!resolved_path) {
  388. if (SG(request_info).path_translated != filename) {
  389. STR_FREE(filename);
  390. }
  391. /* we have to free SG(request_info).path_translated here because
  392. * php_destroy_request_info assumes that it will get
  393. * freed when the include_names hash is emptied, but
  394. * we're not adding it in this case */
  395. STR_FREE(SG(request_info).path_translated);
  396. SG(request_info).path_translated = NULL;
  397. return FAILURE;
  398. }
  399. fp = VCWD_FOPEN(resolved_path, "rb");
  400. #ifndef PHP_WIN32
  401. /* refuse to open anything that is not a regular file */
  402. if (fp && (0 > fstat(fileno(fp), &st) || !S_ISREG(st.st_mode))) {
  403. fclose(fp);
  404. fp = NULL;
  405. }
  406. #endif
  407. if (!fp) {
  408. if (SG(request_info).path_translated != filename) {
  409. STR_FREE(filename);
  410. }
  411. STR_FREE(SG(request_info).path_translated); /* for same reason as above */
  412. SG(request_info).path_translated = NULL;
  413. return FAILURE;
  414. }
  415. file_handle->opened_path = resolved_path;
  416. if (SG(request_info).path_translated != filename) {
  417. STR_FREE(SG(request_info).path_translated); /* for same reason as above */
  418. SG(request_info).path_translated = filename;
  419. }
  420. file_handle->filename = SG(request_info).path_translated;
  421. file_handle->free_filename = 0;
  422. file_handle->handle.fp = fp;
  423. file_handle->type = ZEND_HANDLE_FP;
  424. return SUCCESS;
  425. }
  426. /* }}} */
  427. /* {{{ php_resolve_path
  428. * Returns the realpath for given filename according to include path
  429. */
  430. PHPAPI char *php_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC)
  431. {
  432. char resolved_path[MAXPATHLEN];
  433. char trypath[MAXPATHLEN];
  434. const char *ptr, *end, *p;
  435. char *actual_path;
  436. php_stream_wrapper *wrapper;
  437. if (!filename) {
  438. return NULL;
  439. }
  440. if (strlen(filename) != filename_length) {
  441. return NULL;
  442. }
  443. /* Don't resolve paths which contain protocol (except of file://) */
  444. for (p = filename; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
  445. if ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/')) {
  446. wrapper = php_stream_locate_url_wrapper(filename, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
  447. if (wrapper == &php_plain_files_wrapper) {
  448. if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
  449. return estrdup(resolved_path);
  450. }
  451. }
  452. return NULL;
  453. }
  454. if ((*filename == '.' &&
  455. (IS_SLASH(filename[1]) ||
  456. ((filename[1] == '.') && IS_SLASH(filename[2])))) ||
  457. IS_ABSOLUTE_PATH(filename, filename_length) ||
  458. !path ||
  459. !*path) {
  460. if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) {
  461. return estrdup(resolved_path);
  462. } else {
  463. return NULL;
  464. }
  465. }
  466. ptr = path;
  467. while (ptr && *ptr) {
  468. /* Check for stream wrapper */
  469. int is_stream_wrapper = 0;
  470. for (p = ptr; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
  471. if ((*p == ':') && (p - ptr > 1) && (p[1] == '/') && (p[2] == '/')) {
  472. /* .:// or ..:// is not a stream wrapper */
  473. if (p[-1] != '.' || p[-2] != '.' || p - 2 != ptr) {
  474. p += 3;
  475. is_stream_wrapper = 1;
  476. }
  477. }
  478. end = strchr(p, DEFAULT_DIR_SEPARATOR);
  479. if (end) {
  480. if ((end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) {
  481. ptr = end + 1;
  482. continue;
  483. }
  484. memcpy(trypath, ptr, end-ptr);
  485. trypath[end-ptr] = '/';
  486. memcpy(trypath+(end-ptr)+1, filename, filename_length+1);
  487. ptr = end+1;
  488. } else {
  489. int len = strlen(ptr);
  490. if (len + 1 + filename_length + 1 >= MAXPATHLEN) {
  491. break;
  492. }
  493. memcpy(trypath, ptr, len);
  494. trypath[len] = '/';
  495. memcpy(trypath+len+1, filename, filename_length+1);
  496. ptr = NULL;
  497. }
  498. actual_path = trypath;
  499. if (is_stream_wrapper) {
  500. wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
  501. if (!wrapper) {
  502. continue;
  503. } else if (wrapper != &php_plain_files_wrapper) {
  504. if (wrapper->wops->url_stat) {
  505. php_stream_statbuf ssb;
  506. if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
  507. return estrdup(trypath);
  508. }
  509. }
  510. continue;
  511. }
  512. }
  513. if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
  514. return estrdup(resolved_path);
  515. }
  516. } /* end provided path */
  517. /* check in calling scripts' current working directory as a fall back case
  518. */
  519. if (zend_is_executing(TSRMLS_C)) {
  520. char *exec_fname = zend_get_executed_filename(TSRMLS_C);
  521. int exec_fname_length = strlen(exec_fname);
  522. while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
  523. if (exec_fname && exec_fname[0] != '[' &&
  524. exec_fname_length > 0 &&
  525. exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {
  526. memcpy(trypath, exec_fname, exec_fname_length + 1);
  527. memcpy(trypath+exec_fname_length + 1, filename, filename_length+1);
  528. actual_path = trypath;
  529. /* Check for stream wrapper */
  530. for (p = trypath; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
  531. if ((*p == ':') && (p - trypath > 1) && (p[1] == '/') && (p[2] == '/')) {
  532. wrapper = php_stream_locate_url_wrapper(trypath, &actual_path, STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
  533. if (!wrapper) {
  534. return NULL;
  535. } else if (wrapper != &php_plain_files_wrapper) {
  536. if (wrapper->wops->url_stat) {
  537. php_stream_statbuf ssb;
  538. if (SUCCESS == wrapper->wops->url_stat(wrapper, trypath, 0, &ssb, NULL TSRMLS_CC)) {
  539. return estrdup(trypath);
  540. }
  541. }
  542. return NULL;
  543. }
  544. }
  545. if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
  546. return estrdup(resolved_path);
  547. }
  548. }
  549. }
  550. return NULL;
  551. }
  552. /* }}} */
  553. /* {{{ php_fopen_with_path
  554. * Tries to open a file with a PATH-style list of directories.
  555. * If the filename starts with "." or "/", the path is ignored.
  556. */
  557. PHPAPI FILE *php_fopen_with_path(const char *filename, const char *mode, const char *path, char **opened_path TSRMLS_DC)
  558. {
  559. char *pathbuf, *ptr, *end;
  560. char *exec_fname;
  561. char trypath[MAXPATHLEN];
  562. struct stat sb;
  563. FILE *fp;
  564. int path_length;
  565. int filename_length;
  566. int exec_fname_length;
  567. if (opened_path) {
  568. *opened_path = NULL;
  569. }
  570. if (!filename) {
  571. return NULL;
  572. }
  573. filename_length = strlen(filename);
  574. /* Relative path open */
  575. if (*filename == '.') {
  576. return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC);
  577. }
  578. /* Absolute path open */
  579. if (IS_ABSOLUTE_PATH(filename, filename_length)) {
  580. return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC);
  581. }
  582. if (!path || (path && !*path)) {
  583. return php_fopen_and_set_opened_path(filename, mode, opened_path TSRMLS_CC);
  584. }
  585. /* check in provided path */
  586. /* append the calling scripts' current working directory
  587. * as a fall back case
  588. */
  589. if (zend_is_executing(TSRMLS_C)) {
  590. exec_fname = zend_get_executed_filename(TSRMLS_C);
  591. exec_fname_length = strlen(exec_fname);
  592. path_length = strlen(path);
  593. while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
  594. if ((exec_fname && exec_fname[0] == '[') || exec_fname_length <= 0) {
  595. /* [no active file] or no path */
  596. pathbuf = estrdup(path);
  597. } else {
  598. pathbuf = (char *) emalloc(exec_fname_length + path_length + 1 + 1);
  599. memcpy(pathbuf, path, path_length);
  600. pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
  601. memcpy(pathbuf + path_length + 1, exec_fname, exec_fname_length);
  602. pathbuf[path_length + exec_fname_length + 1] = '\0';
  603. }
  604. } else {
  605. pathbuf = estrdup(path);
  606. }
  607. ptr = pathbuf;
  608. while (ptr && *ptr) {
  609. end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  610. if (end != NULL) {
  611. *end = '\0';
  612. end++;
  613. }
  614. if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
  615. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
  616. }
  617. fp = php_fopen_and_set_opened_path(trypath, mode, opened_path TSRMLS_CC);
  618. if (fp) {
  619. efree(pathbuf);
  620. return fp;
  621. }
  622. ptr = end;
  623. } /* end provided path */
  624. efree(pathbuf);
  625. return NULL;
  626. }
  627. /* }}} */
  628. /* {{{ php_strip_url_passwd
  629. */
  630. PHPAPI char *php_strip_url_passwd(char *url)
  631. {
  632. register char *p, *url_start;
  633. if (url == NULL) {
  634. return "";
  635. }
  636. p = url;
  637. while (*p) {
  638. if (*p == ':' && *(p + 1) == '/' && *(p + 2) == '/') {
  639. /* found protocol */
  640. url_start = p = p + 3;
  641. while (*p) {
  642. if (*p == '@') {
  643. int i;
  644. for (i = 0; i < 3 && url_start < p; i++, url_start++) {
  645. *url_start = '.';
  646. }
  647. for (; *p; p++) {
  648. *url_start++ = *p;
  649. }
  650. *url_start=0;
  651. break;
  652. }
  653. p++;
  654. }
  655. return url;
  656. }
  657. p++;
  658. }
  659. return url;
  660. }
  661. /* }}} */
  662. /* {{{ expand_filepath
  663. */
  664. PHPAPI char *expand_filepath(const char *filepath, char *real_path TSRMLS_DC)
  665. {
  666. return expand_filepath_ex(filepath, real_path, NULL, 0 TSRMLS_CC);
  667. }
  668. /* }}} */
  669. /* {{{ expand_filepath_ex
  670. */
  671. PHPAPI char *expand_filepath_ex(const char *filepath, char *real_path, const char *relative_to, size_t relative_to_len TSRMLS_DC)
  672. {
  673. cwd_state new_state;
  674. char cwd[MAXPATHLEN];
  675. int copy_len;
  676. if (!filepath[0]) {
  677. return NULL;
  678. } else if (IS_ABSOLUTE_PATH(filepath, strlen(filepath))) {
  679. cwd[0] = '\0';
  680. } else {
  681. const char *iam = SG(request_info).path_translated;
  682. const char *result;
  683. if (relative_to) {
  684. if (relative_to_len > MAXPATHLEN-1U) {
  685. return NULL;
  686. }
  687. result = relative_to;
  688. memcpy(cwd, relative_to, relative_to_len+1U);
  689. } else {
  690. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  691. }
  692. if (!result && (iam != filepath)) {
  693. int fdtest = -1;
  694. fdtest = VCWD_OPEN(filepath, O_RDONLY);
  695. if (fdtest != -1) {
  696. /* return a relative file path if for any reason
  697. * we cannot cannot getcwd() and the requested,
  698. * relatively referenced file is accessible */
  699. copy_len = strlen(filepath) > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : strlen(filepath);
  700. real_path = estrndup(filepath, copy_len);
  701. close(fdtest);
  702. return real_path;
  703. } else {
  704. cwd[0] = '\0';
  705. }
  706. } else if (!result) {
  707. cwd[0] = '\0';
  708. }
  709. }
  710. new_state.cwd = strdup(cwd);
  711. new_state.cwd_length = strlen(cwd);
  712. if (virtual_file_ex(&new_state, filepath, NULL, CWD_FILEPATH)) {
  713. free(new_state.cwd);
  714. return NULL;
  715. }
  716. if (real_path) {
  717. copy_len = new_state.cwd_length > MAXPATHLEN - 1 ? MAXPATHLEN - 1 : new_state.cwd_length;
  718. memcpy(real_path, new_state.cwd, copy_len);
  719. real_path[copy_len] = '\0';
  720. } else {
  721. real_path = estrndup(new_state.cwd, new_state.cwd_length);
  722. }
  723. free(new_state.cwd);
  724. return real_path;
  725. }
  726. /* }}} */
  727. /*
  728. * Local variables:
  729. * tab-width: 4
  730. * c-basic-offset: 4
  731. * End:
  732. * vim600: sw=4 ts=4 fdm=marker
  733. * vim<600: sw=4 ts=4
  734. */