PageRenderTime 81ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/phar/phar.c

http://github.com/infusion/PHP
C | 3739 lines | 2876 code | 521 blank | 342 comment | 960 complexity | e2d23dc05a55eb47f81518347ab21406 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar php single-file executable PHP extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2005-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: Gregory Beaver <cellog@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id: phar.c 307915 2011-02-01 14:01:00Z iliaa $ */
  20. #define PHAR_MAIN 1
  21. #include "phar_internal.h"
  22. #include "SAPI.h"
  23. #include "func_interceptors.h"
  24. static void destroy_phar_data(void *pDest);
  25. ZEND_DECLARE_MODULE_GLOBALS(phar)
  26. #if PHP_VERSION_ID >= 50300
  27. char *(*phar_save_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
  28. #endif
  29. /**
  30. * set's phar->is_writeable based on the current INI value
  31. */
  32. static int phar_set_writeable_bit(void *pDest, void *argument TSRMLS_DC) /* {{{ */
  33. {
  34. zend_bool keep = *(zend_bool *)argument;
  35. phar_archive_data *phar = *(phar_archive_data **)pDest;
  36. if (!phar->is_data) {
  37. phar->is_writeable = !keep;
  38. }
  39. return ZEND_HASH_APPLY_KEEP;
  40. }
  41. /* }}} */
  42. /* if the original value is 0 (disabled), then allow setting/unsetting at will. Otherwise only allow 1 (enabled), and error on disabling */
  43. ZEND_INI_MH(phar_ini_modify_handler) /* {{{ */
  44. {
  45. zend_bool old, ini;
  46. if (entry->name_length == 14) {
  47. old = PHAR_G(readonly_orig);
  48. } else {
  49. old = PHAR_G(require_hash_orig);
  50. }
  51. if (new_value_length == 2 && !strcasecmp("on", new_value)) {
  52. ini = (zend_bool) 1;
  53. }
  54. else if (new_value_length == 3 && !strcasecmp("yes", new_value)) {
  55. ini = (zend_bool) 1;
  56. }
  57. else if (new_value_length == 4 && !strcasecmp("true", new_value)) {
  58. ini = (zend_bool) 1;
  59. }
  60. else {
  61. ini = (zend_bool) atoi(new_value);
  62. }
  63. /* do not allow unsetting in runtime */
  64. if (stage == ZEND_INI_STAGE_STARTUP) {
  65. if (entry->name_length == 14) {
  66. PHAR_G(readonly_orig) = ini;
  67. } else {
  68. PHAR_G(require_hash_orig) = ini;
  69. }
  70. } else if (old && !ini) {
  71. return FAILURE;
  72. }
  73. if (entry->name_length == 14) {
  74. PHAR_G(readonly) = ini;
  75. if (PHAR_GLOBALS->request_init && PHAR_GLOBALS->phar_fname_map.arBuckets) {
  76. zend_hash_apply_with_argument(&(PHAR_GLOBALS->phar_fname_map), phar_set_writeable_bit, (void *)&ini TSRMLS_CC);
  77. }
  78. } else {
  79. PHAR_G(require_hash) = ini;
  80. }
  81. return SUCCESS;
  82. }
  83. /* }}}*/
  84. /* this global stores the global cached pre-parsed manifests */
  85. HashTable cached_phars;
  86. HashTable cached_alias;
  87. static void phar_split_cache_list(TSRMLS_D) /* {{{ */
  88. {
  89. char *tmp;
  90. char *key, *lasts, *end;
  91. char ds[2];
  92. phar_archive_data *phar;
  93. uint i = 0;
  94. if (!PHAR_GLOBALS->cache_list || !(PHAR_GLOBALS->cache_list[0])) {
  95. return;
  96. }
  97. ds[0] = DEFAULT_DIR_SEPARATOR;
  98. ds[1] = '\0';
  99. tmp = estrdup(PHAR_GLOBALS->cache_list);
  100. /* fake request startup */
  101. PHAR_GLOBALS->request_init = 1;
  102. if (zend_hash_init(&EG(regular_list), 0, NULL, NULL, 0) == SUCCESS) {
  103. EG(regular_list).nNextFreeElement=1; /* we don't want resource id 0 */
  104. }
  105. PHAR_G(has_bz2) = zend_hash_exists(&module_registry, "bz2", sizeof("bz2"));
  106. PHAR_G(has_zlib) = zend_hash_exists(&module_registry, "zlib", sizeof("zlib"));
  107. /* these two are dummies and will be destroyed later */
  108. zend_hash_init(&cached_phars, sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data, 1);
  109. zend_hash_init(&cached_alias, sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
  110. /* these two are real and will be copied over cached_phars/cached_alias later */
  111. zend_hash_init(&(PHAR_GLOBALS->phar_fname_map), sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data, 1);
  112. zend_hash_init(&(PHAR_GLOBALS->phar_alias_map), sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
  113. PHAR_GLOBALS->manifest_cached = 1;
  114. PHAR_GLOBALS->persist = 1;
  115. for (key = php_strtok_r(tmp, ds, &lasts);
  116. key;
  117. key = php_strtok_r(NULL, ds, &lasts)) {
  118. end = strchr(key, DEFAULT_DIR_SEPARATOR);
  119. if (end) {
  120. if (SUCCESS == phar_open_from_filename(key, end - key, NULL, 0, 0, &phar, NULL TSRMLS_CC)) {
  121. finish_up:
  122. phar->phar_pos = i++;
  123. php_stream_close(phar->fp);
  124. phar->fp = NULL;
  125. } else {
  126. finish_error:
  127. PHAR_GLOBALS->persist = 0;
  128. PHAR_GLOBALS->manifest_cached = 0;
  129. efree(tmp);
  130. zend_hash_destroy(&(PHAR_G(phar_fname_map)));
  131. PHAR_GLOBALS->phar_fname_map.arBuckets = 0;
  132. zend_hash_destroy(&(PHAR_G(phar_alias_map)));
  133. PHAR_GLOBALS->phar_alias_map.arBuckets = 0;
  134. zend_hash_destroy(&cached_phars);
  135. zend_hash_destroy(&cached_alias);
  136. zend_hash_graceful_reverse_destroy(&EG(regular_list));
  137. memset(&EG(regular_list), 0, sizeof(HashTable));
  138. /* free cached manifests */
  139. PHAR_GLOBALS->request_init = 0;
  140. return;
  141. }
  142. } else {
  143. if (SUCCESS == phar_open_from_filename(key, strlen(key), NULL, 0, 0, &phar, NULL TSRMLS_CC)) {
  144. goto finish_up;
  145. } else {
  146. goto finish_error;
  147. }
  148. }
  149. }
  150. PHAR_GLOBALS->persist = 0;
  151. PHAR_GLOBALS->request_init = 0;
  152. /* destroy dummy values from before */
  153. zend_hash_destroy(&cached_phars);
  154. zend_hash_destroy(&cached_alias);
  155. cached_phars = PHAR_GLOBALS->phar_fname_map;
  156. cached_alias = PHAR_GLOBALS->phar_alias_map;
  157. PHAR_GLOBALS->phar_fname_map.arBuckets = 0;
  158. PHAR_GLOBALS->phar_alias_map.arBuckets = 0;
  159. zend_hash_graceful_reverse_destroy(&EG(regular_list));
  160. memset(&EG(regular_list), 0, sizeof(HashTable));
  161. efree(tmp);
  162. }
  163. /* }}} */
  164. ZEND_INI_MH(phar_ini_cache_list) /* {{{ */
  165. {
  166. PHAR_G(cache_list) = new_value;
  167. if (stage == ZEND_INI_STAGE_STARTUP) {
  168. phar_split_cache_list(TSRMLS_C);
  169. }
  170. return SUCCESS;
  171. }
  172. /* }}} */
  173. PHP_INI_BEGIN()
  174. STD_PHP_INI_BOOLEAN( "phar.readonly", "1", PHP_INI_ALL, phar_ini_modify_handler, readonly, zend_phar_globals, phar_globals)
  175. STD_PHP_INI_BOOLEAN( "phar.require_hash", "1", PHP_INI_ALL, phar_ini_modify_handler, require_hash, zend_phar_globals, phar_globals)
  176. STD_PHP_INI_ENTRY("phar.cache_list", "", PHP_INI_SYSTEM, phar_ini_cache_list, cache_list, zend_phar_globals, phar_globals)
  177. PHP_INI_END()
  178. /**
  179. * When all uses of a phar have been concluded, this frees the manifest
  180. * and the phar slot
  181. */
  182. void phar_destroy_phar_data(phar_archive_data *phar TSRMLS_DC) /* {{{ */
  183. {
  184. if (phar->alias && phar->alias != phar->fname) {
  185. pefree(phar->alias, phar->is_persistent);
  186. phar->alias = NULL;
  187. }
  188. if (phar->fname) {
  189. pefree(phar->fname, phar->is_persistent);
  190. phar->fname = NULL;
  191. }
  192. if (phar->signature) {
  193. pefree(phar->signature, phar->is_persistent);
  194. phar->signature = NULL;
  195. }
  196. if (phar->manifest.arBuckets) {
  197. zend_hash_destroy(&phar->manifest);
  198. phar->manifest.arBuckets = NULL;
  199. }
  200. if (phar->mounted_dirs.arBuckets) {
  201. zend_hash_destroy(&phar->mounted_dirs);
  202. phar->mounted_dirs.arBuckets = NULL;
  203. }
  204. if (phar->virtual_dirs.arBuckets) {
  205. zend_hash_destroy(&phar->virtual_dirs);
  206. phar->virtual_dirs.arBuckets = NULL;
  207. }
  208. if (phar->metadata) {
  209. if (phar->is_persistent) {
  210. if (phar->metadata_len) {
  211. /* for zip comments that are strings */
  212. free(phar->metadata);
  213. } else {
  214. zval_internal_ptr_dtor(&phar->metadata);
  215. }
  216. } else {
  217. zval_ptr_dtor(&phar->metadata);
  218. }
  219. phar->metadata_len = 0;
  220. phar->metadata = 0;
  221. }
  222. if (phar->fp) {
  223. php_stream_close(phar->fp);
  224. phar->fp = 0;
  225. }
  226. if (phar->ufp) {
  227. php_stream_close(phar->ufp);
  228. phar->ufp = 0;
  229. }
  230. pefree(phar, phar->is_persistent);
  231. }
  232. /* }}}*/
  233. /**
  234. * Delete refcount and destruct if needed. On destruct return 1 else 0.
  235. */
  236. int phar_archive_delref(phar_archive_data *phar TSRMLS_DC) /* {{{ */
  237. {
  238. if (phar->is_persistent) {
  239. return 0;
  240. }
  241. if (--phar->refcount < 0) {
  242. if (PHAR_GLOBALS->request_done
  243. || zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), phar->fname, phar->fname_len) != SUCCESS) {
  244. phar_destroy_phar_data(phar TSRMLS_CC);
  245. }
  246. return 1;
  247. } else if (!phar->refcount) {
  248. /* invalidate phar cache */
  249. PHAR_G(last_phar) = NULL;
  250. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  251. if (phar->fp && !(phar->flags & PHAR_FILE_COMPRESSION_MASK)) {
  252. /* close open file handle - allows removal or rename of
  253. the file on windows, which has greedy locking
  254. only close if the archive was not already compressed. If it
  255. was compressed, then the fp does not refer to the original file */
  256. php_stream_close(phar->fp);
  257. phar->fp = NULL;
  258. }
  259. if (!zend_hash_num_elements(&phar->manifest)) {
  260. /* this is a new phar that has perhaps had an alias/metadata set, but has never
  261. been flushed */
  262. if (zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), phar->fname, phar->fname_len) != SUCCESS) {
  263. phar_destroy_phar_data(phar TSRMLS_CC);
  264. }
  265. return 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. /* }}}*/
  271. /**
  272. * Destroy phar's in shutdown, here we don't care about aliases
  273. */
  274. static void destroy_phar_data_only(void *pDest) /* {{{ */
  275. {
  276. phar_archive_data *phar_data = *(phar_archive_data **) pDest;
  277. TSRMLS_FETCH();
  278. if (EG(exception) || --phar_data->refcount < 0) {
  279. phar_destroy_phar_data(phar_data TSRMLS_CC);
  280. }
  281. }
  282. /* }}}*/
  283. /**
  284. * Delete aliases to phar's that got kicked out of the global table
  285. */
  286. static int phar_unalias_apply(void *pDest, void *argument TSRMLS_DC) /* {{{ */
  287. {
  288. return *(void**)pDest == argument ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_KEEP;
  289. }
  290. /* }}} */
  291. /**
  292. * Delete aliases to phar's that got kicked out of the global table
  293. */
  294. static int phar_tmpclose_apply(void *pDest TSRMLS_DC) /* {{{ */
  295. {
  296. phar_entry_info *entry = (phar_entry_info *) pDest;
  297. if (entry->fp_type != PHAR_TMP) {
  298. return ZEND_HASH_APPLY_KEEP;
  299. }
  300. if (entry->fp && !entry->fp_refcount) {
  301. php_stream_close(entry->fp);
  302. entry->fp = NULL;
  303. }
  304. return ZEND_HASH_APPLY_KEEP;
  305. }
  306. /* }}} */
  307. /**
  308. * Filename map destructor
  309. */
  310. static void destroy_phar_data(void *pDest) /* {{{ */
  311. {
  312. phar_archive_data *phar_data = *(phar_archive_data **) pDest;
  313. TSRMLS_FETCH();
  314. if (PHAR_GLOBALS->request_ends) {
  315. /* first, iterate over the manifest and close all PHAR_TMP entry fp handles,
  316. this prevents unnecessary unfreed stream resources */
  317. zend_hash_apply(&(phar_data->manifest), phar_tmpclose_apply TSRMLS_CC);
  318. destroy_phar_data_only(pDest);
  319. return;
  320. }
  321. zend_hash_apply_with_argument(&(PHAR_GLOBALS->phar_alias_map), phar_unalias_apply, phar_data TSRMLS_CC);
  322. if (--phar_data->refcount < 0) {
  323. phar_destroy_phar_data(phar_data TSRMLS_CC);
  324. }
  325. }
  326. /* }}}*/
  327. /**
  328. * destructor for the manifest hash, frees each file's entry
  329. */
  330. void destroy_phar_manifest_entry(void *pDest) /* {{{ */
  331. {
  332. phar_entry_info *entry = (phar_entry_info *)pDest;
  333. TSRMLS_FETCH();
  334. if (entry->cfp) {
  335. php_stream_close(entry->cfp);
  336. entry->cfp = 0;
  337. }
  338. if (entry->fp) {
  339. php_stream_close(entry->fp);
  340. entry->fp = 0;
  341. }
  342. if (entry->metadata) {
  343. if (entry->is_persistent) {
  344. if (entry->metadata_len) {
  345. /* for zip comments that are strings */
  346. free(entry->metadata);
  347. } else {
  348. zval_internal_ptr_dtor(&entry->metadata);
  349. }
  350. } else {
  351. zval_ptr_dtor(&entry->metadata);
  352. }
  353. entry->metadata_len = 0;
  354. entry->metadata = 0;
  355. }
  356. if (entry->metadata_str.c) {
  357. smart_str_free(&entry->metadata_str);
  358. entry->metadata_str.c = 0;
  359. }
  360. pefree(entry->filename, entry->is_persistent);
  361. if (entry->link) {
  362. pefree(entry->link, entry->is_persistent);
  363. entry->link = 0;
  364. }
  365. if (entry->tmp) {
  366. pefree(entry->tmp, entry->is_persistent);
  367. entry->tmp = 0;
  368. }
  369. }
  370. /* }}} */
  371. int phar_entry_delref(phar_entry_data *idata TSRMLS_DC) /* {{{ */
  372. {
  373. int ret = 0;
  374. if (idata->internal_file && !idata->internal_file->is_persistent) {
  375. if (--idata->internal_file->fp_refcount < 0) {
  376. idata->internal_file->fp_refcount = 0;
  377. }
  378. if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
  379. php_stream_close(idata->fp);
  380. }
  381. /* if phar_get_or_create_entry_data returns a sub-directory, we have to free it */
  382. if (idata->internal_file->is_temp_dir) {
  383. destroy_phar_manifest_entry((void *)idata->internal_file);
  384. efree(idata->internal_file);
  385. }
  386. }
  387. phar_archive_delref(idata->phar TSRMLS_CC);
  388. efree(idata);
  389. return ret;
  390. }
  391. /* }}} */
  392. /**
  393. * Removes an entry, either by actually removing it or by marking it.
  394. */
  395. void phar_entry_remove(phar_entry_data *idata, char **error TSRMLS_DC) /* {{{ */
  396. {
  397. phar_archive_data *phar;
  398. phar = idata->phar;
  399. if (idata->internal_file->fp_refcount < 2) {
  400. if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
  401. php_stream_close(idata->fp);
  402. }
  403. zend_hash_del(&idata->phar->manifest, idata->internal_file->filename, idata->internal_file->filename_len);
  404. idata->phar->refcount--;
  405. efree(idata);
  406. } else {
  407. idata->internal_file->is_deleted = 1;
  408. phar_entry_delref(idata TSRMLS_CC);
  409. }
  410. if (!phar->donotflush) {
  411. phar_flush(phar, 0, 0, 0, error TSRMLS_CC);
  412. }
  413. }
  414. /* }}} */
  415. #define MAPPHAR_ALLOC_FAIL(msg) \
  416. if (fp) {\
  417. php_stream_close(fp);\
  418. }\
  419. if (error) {\
  420. spprintf(error, 0, msg, fname);\
  421. }\
  422. return FAILURE;
  423. #define MAPPHAR_FAIL(msg) \
  424. efree(savebuf);\
  425. if (mydata) {\
  426. phar_destroy_phar_data(mydata TSRMLS_CC);\
  427. }\
  428. if (signature) {\
  429. pefree(signature, PHAR_G(persist));\
  430. }\
  431. MAPPHAR_ALLOC_FAIL(msg)
  432. #ifdef WORDS_BIGENDIAN
  433. # define PHAR_GET_32(buffer, var) \
  434. var = ((((unsigned char*)(buffer))[3]) << 24) \
  435. | ((((unsigned char*)(buffer))[2]) << 16) \
  436. | ((((unsigned char*)(buffer))[1]) << 8) \
  437. | (((unsigned char*)(buffer))[0]); \
  438. (buffer) += 4
  439. # define PHAR_GET_16(buffer, var) \
  440. var = ((((unsigned char*)(buffer))[1]) << 8) \
  441. | (((unsigned char*)(buffer))[0]); \
  442. (buffer) += 2
  443. #else
  444. # define PHAR_GET_32(buffer, var) \
  445. memcpy(&var, buffer, sizeof(var)); \
  446. buffer += 4
  447. # define PHAR_GET_16(buffer, var) \
  448. var = *(php_uint16*)(buffer); \
  449. buffer += 2
  450. #endif
  451. #define PHAR_ZIP_16(var) ((php_uint16)((((php_uint16)var[0]) & 0xff) | \
  452. (((php_uint16)var[1]) & 0xff) << 8))
  453. #define PHAR_ZIP_32(var) ((php_uint32)((((php_uint32)var[0]) & 0xff) | \
  454. (((php_uint32)var[1]) & 0xff) << 8 | \
  455. (((php_uint32)var[2]) & 0xff) << 16 | \
  456. (((php_uint32)var[3]) & 0xff) << 24))
  457. /**
  458. * Open an already loaded phar
  459. */
  460. int phar_open_parsed_phar(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  461. {
  462. phar_archive_data *phar;
  463. #ifdef PHP_WIN32
  464. char *unixfname;
  465. #endif
  466. if (error) {
  467. *error = NULL;
  468. }
  469. #ifdef PHP_WIN32
  470. unixfname = estrndup(fname, fname_len);
  471. phar_unixify_path_separators(unixfname, fname_len);
  472. if (SUCCESS == phar_get_archive(&phar, unixfname, fname_len, alias, alias_len, error TSRMLS_CC)
  473. && ((alias && fname_len == phar->fname_len
  474. && !strncmp(unixfname, phar->fname, fname_len)) || !alias)
  475. ) {
  476. phar_entry_info *stub;
  477. efree(unixfname);
  478. #else
  479. if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, alias_len, error TSRMLS_CC)
  480. && ((alias && fname_len == phar->fname_len
  481. && !strncmp(fname, phar->fname, fname_len)) || !alias)
  482. ) {
  483. phar_entry_info *stub;
  484. #endif
  485. /* logic above is as follows:
  486. If an explicit alias was requested, ensure the filename passed in
  487. matches the phar's filename.
  488. If no alias was passed in, then it can match either and be valid
  489. */
  490. if (!is_data) {
  491. /* prevent any ".phar" without a stub getting through */
  492. if (!phar->halt_offset && !phar->is_brandnew && (phar->is_tar || phar->is_zip)) {
  493. if (PHAR_G(readonly) && FAILURE == zend_hash_find(&(phar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1, (void **)&stub)) {
  494. if (error) {
  495. spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
  496. }
  497. return FAILURE;
  498. }
  499. }
  500. }
  501. if (pphar) {
  502. *pphar = phar;
  503. }
  504. return SUCCESS;
  505. } else {
  506. #ifdef PHP_WIN32
  507. efree(unixfname);
  508. #endif
  509. if (pphar) {
  510. *pphar = NULL;
  511. }
  512. if (phar && error && !(options & REPORT_ERRORS)) {
  513. efree(error);
  514. }
  515. return FAILURE;
  516. }
  517. }
  518. /* }}}*/
  519. /**
  520. * Parse out metadata from the manifest for a single file
  521. *
  522. * Meta-data is in this format:
  523. * [len32][data...]
  524. *
  525. * data is the serialized zval
  526. */
  527. int phar_parse_metadata(char **buffer, zval **metadata, int zip_metadata_len TSRMLS_DC) /* {{{ */
  528. {
  529. const unsigned char *p;
  530. php_uint32 buf_len;
  531. php_unserialize_data_t var_hash;
  532. if (!zip_metadata_len) {
  533. PHAR_GET_32(*buffer, buf_len);
  534. } else {
  535. buf_len = zip_metadata_len;
  536. }
  537. if (buf_len) {
  538. ALLOC_ZVAL(*metadata);
  539. INIT_ZVAL(**metadata);
  540. p = (const unsigned char*) *buffer;
  541. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  542. if (!php_var_unserialize(metadata, &p, p + buf_len, &var_hash TSRMLS_CC)) {
  543. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  544. zval_ptr_dtor(metadata);
  545. *metadata = NULL;
  546. return FAILURE;
  547. }
  548. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  549. if (PHAR_G(persist)) {
  550. /* lazy init metadata */
  551. zval_ptr_dtor(metadata);
  552. *metadata = (zval *) pemalloc(buf_len, 1);
  553. memcpy(*metadata, *buffer, buf_len);
  554. *buffer += buf_len;
  555. return SUCCESS;
  556. }
  557. } else {
  558. *metadata = NULL;
  559. }
  560. if (!zip_metadata_len) {
  561. *buffer += buf_len;
  562. }
  563. return SUCCESS;
  564. }
  565. /* }}}*/
  566. /**
  567. * Does not check for a previously opened phar in the cache.
  568. *
  569. * Parse a new one and add it to the cache, returning either SUCCESS or
  570. * FAILURE, and setting pphar to the pointer to the manifest entry
  571. *
  572. * This is used by phar_open_from_filename to process the manifest, but can be called
  573. * directly.
  574. */
  575. static int phar_parse_pharfile(php_stream *fp, char *fname, int fname_len, char *alias, int alias_len, long halt_offset, phar_archive_data** pphar, php_uint32 compression, char **error TSRMLS_DC) /* {{{ */
  576. {
  577. char b32[4], *buffer, *endbuffer, *savebuf;
  578. phar_archive_data *mydata = NULL;
  579. phar_entry_info entry;
  580. php_uint32 manifest_len, manifest_count, manifest_flags, manifest_index, tmp_len, sig_flags;
  581. php_uint16 manifest_ver;
  582. long offset;
  583. int register_alias, sig_len, temp_alias = 0;
  584. char *signature = NULL;
  585. if (pphar) {
  586. *pphar = NULL;
  587. }
  588. if (error) {
  589. *error = NULL;
  590. }
  591. /* check for ?>\n and increment accordingly */
  592. if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
  593. MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
  594. }
  595. buffer = b32;
  596. if (3 != php_stream_read(fp, buffer, 3)) {
  597. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  598. }
  599. if ((*buffer == ' ' || *buffer == '\n') && *(buffer + 1) == '?' && *(buffer + 2) == '>') {
  600. int nextchar;
  601. halt_offset += 3;
  602. if (EOF == (nextchar = php_stream_getc(fp))) {
  603. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  604. }
  605. if ((char) nextchar == '\r') {
  606. /* if we have an \r we require an \n as well */
  607. if (EOF == (nextchar = php_stream_getc(fp)) || (char)nextchar != '\n') {
  608. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  609. }
  610. ++halt_offset;
  611. }
  612. if ((char) nextchar == '\n') {
  613. ++halt_offset;
  614. }
  615. }
  616. /* make sure we are at the right location to read the manifest */
  617. if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
  618. MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
  619. }
  620. /* read in manifest */
  621. buffer = b32;
  622. if (4 != php_stream_read(fp, buffer, 4)) {
  623. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at manifest length)")
  624. }
  625. PHAR_GET_32(buffer, manifest_len);
  626. if (manifest_len > 1048576 * 100) {
  627. /* prevent serious memory issues by limiting manifest to at most 100 MB in length */
  628. MAPPHAR_ALLOC_FAIL("manifest cannot be larger than 100 MB in phar \"%s\"")
  629. }
  630. buffer = (char *)emalloc(manifest_len);
  631. savebuf = buffer;
  632. endbuffer = buffer + manifest_len;
  633. if (manifest_len < 10 || manifest_len != php_stream_read(fp, buffer, manifest_len)) {
  634. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
  635. }
  636. /* extract the number of entries */
  637. PHAR_GET_32(buffer, manifest_count);
  638. if (manifest_count == 0) {
  639. MAPPHAR_FAIL("in phar \"%s\", manifest claims to have zero entries. Phars must have at least 1 entry");
  640. }
  641. /* extract API version, lowest nibble currently unused */
  642. manifest_ver = (((unsigned char)buffer[0]) << 8)
  643. + ((unsigned char)buffer[1]);
  644. buffer += 2;
  645. if ((manifest_ver & PHAR_API_VER_MASK) < PHAR_API_MIN_READ) {
  646. efree(savebuf);
  647. php_stream_close(fp);
  648. if (error) {
  649. spprintf(error, 0, "phar \"%s\" is API version %1.u.%1.u.%1.u, and cannot be processed", fname, manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0x0F);
  650. }
  651. return FAILURE;
  652. }
  653. PHAR_GET_32(buffer, manifest_flags);
  654. manifest_flags &= ~PHAR_HDR_COMPRESSION_MASK;
  655. manifest_flags &= ~PHAR_FILE_COMPRESSION_MASK;
  656. /* remember whether this entire phar was compressed with gz/bzip2 */
  657. manifest_flags |= compression;
  658. /* The lowest nibble contains the phar wide flags. The compression flags can */
  659. /* be ignored on reading because it is being generated anyways. */
  660. if (manifest_flags & PHAR_HDR_SIGNATURE) {
  661. char sig_buf[8], *sig_ptr = sig_buf;
  662. off_t read_len;
  663. size_t end_of_phar;
  664. if (-1 == php_stream_seek(fp, -8, SEEK_END)
  665. || (read_len = php_stream_tell(fp)) < 20
  666. || 8 != php_stream_read(fp, sig_buf, 8)
  667. || memcmp(sig_buf+4, "GBMB", 4)) {
  668. efree(savebuf);
  669. php_stream_close(fp);
  670. if (error) {
  671. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  672. }
  673. return FAILURE;
  674. }
  675. PHAR_GET_32(sig_ptr, sig_flags);
  676. switch(sig_flags) {
  677. case PHAR_SIG_OPENSSL: {
  678. php_uint32 signature_len;
  679. char *sig;
  680. off_t whence;
  681. /* we store the signature followed by the signature length */
  682. if (-1 == php_stream_seek(fp, -12, SEEK_CUR)
  683. || 4 != php_stream_read(fp, sig_buf, 4)) {
  684. efree(savebuf);
  685. php_stream_close(fp);
  686. if (error) {
  687. spprintf(error, 0, "phar \"%s\" openssl signature length could not be read", fname);
  688. }
  689. return FAILURE;
  690. }
  691. sig_ptr = sig_buf;
  692. PHAR_GET_32(sig_ptr, signature_len);
  693. sig = (char *) emalloc(signature_len);
  694. whence = signature_len + 4;
  695. whence = -whence;
  696. if (-1 == php_stream_seek(fp, whence, SEEK_CUR)
  697. || !(end_of_phar = php_stream_tell(fp))
  698. || signature_len != php_stream_read(fp, sig, signature_len)) {
  699. efree(savebuf);
  700. efree(sig);
  701. php_stream_close(fp);
  702. if (error) {
  703. spprintf(error, 0, "phar \"%s\" openssl signature could not be read", fname);
  704. }
  705. return FAILURE;
  706. }
  707. if (FAILURE == phar_verify_signature(fp, end_of_phar, PHAR_SIG_OPENSSL, sig, signature_len, fname, &signature, &sig_len, error TSRMLS_CC)) {
  708. efree(savebuf);
  709. efree(sig);
  710. php_stream_close(fp);
  711. if (error) {
  712. char *save = *error;
  713. spprintf(error, 0, "phar \"%s\" openssl signature could not be verified: %s", fname, *error);
  714. efree(save);
  715. }
  716. return FAILURE;
  717. }
  718. efree(sig);
  719. }
  720. break;
  721. #if PHAR_HASH_OK
  722. case PHAR_SIG_SHA512: {
  723. unsigned char digest[64];
  724. php_stream_seek(fp, -(8 + 64), SEEK_END);
  725. read_len = php_stream_tell(fp);
  726. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  727. efree(savebuf);
  728. php_stream_close(fp);
  729. if (error) {
  730. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  731. }
  732. return FAILURE;
  733. }
  734. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA512, (char *)digest, 64, fname, &signature, &sig_len, error TSRMLS_CC)) {
  735. efree(savebuf);
  736. php_stream_close(fp);
  737. if (error) {
  738. char *save = *error;
  739. spprintf(error, 0, "phar \"%s\" SHA512 signature could not be verified: %s", fname, *error);
  740. efree(save);
  741. }
  742. return FAILURE;
  743. }
  744. break;
  745. }
  746. case PHAR_SIG_SHA256: {
  747. unsigned char digest[32];
  748. php_stream_seek(fp, -(8 + 32), SEEK_END);
  749. read_len = php_stream_tell(fp);
  750. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  751. efree(savebuf);
  752. php_stream_close(fp);
  753. if (error) {
  754. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  755. }
  756. return FAILURE;
  757. }
  758. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA256, (char *)digest, 32, fname, &signature, &sig_len, error TSRMLS_CC)) {
  759. efree(savebuf);
  760. php_stream_close(fp);
  761. if (error) {
  762. char *save = *error;
  763. spprintf(error, 0, "phar \"%s\" SHA256 signature could not be verified: %s", fname, *error);
  764. efree(save);
  765. }
  766. return FAILURE;
  767. }
  768. break;
  769. }
  770. #else
  771. case PHAR_SIG_SHA512:
  772. case PHAR_SIG_SHA256:
  773. efree(savebuf);
  774. php_stream_close(fp);
  775. if (error) {
  776. spprintf(error, 0, "phar \"%s\" has a unsupported signature", fname);
  777. }
  778. return FAILURE;
  779. #endif
  780. case PHAR_SIG_SHA1: {
  781. unsigned char digest[20];
  782. php_stream_seek(fp, -(8 + 20), SEEK_END);
  783. read_len = php_stream_tell(fp);
  784. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  785. efree(savebuf);
  786. php_stream_close(fp);
  787. if (error) {
  788. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  789. }
  790. return FAILURE;
  791. }
  792. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA1, (char *)digest, 20, fname, &signature, &sig_len, error TSRMLS_CC)) {
  793. efree(savebuf);
  794. php_stream_close(fp);
  795. if (error) {
  796. char *save = *error;
  797. spprintf(error, 0, "phar \"%s\" SHA1 signature could not be verified: %s", fname, *error);
  798. efree(save);
  799. }
  800. return FAILURE;
  801. }
  802. break;
  803. }
  804. case PHAR_SIG_MD5: {
  805. unsigned char digest[16];
  806. php_stream_seek(fp, -(8 + 16), SEEK_END);
  807. read_len = php_stream_tell(fp);
  808. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  809. efree(savebuf);
  810. php_stream_close(fp);
  811. if (error) {
  812. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  813. }
  814. return FAILURE;
  815. }
  816. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_MD5, (char *)digest, 16, fname, &signature, &sig_len, error TSRMLS_CC)) {
  817. efree(savebuf);
  818. php_stream_close(fp);
  819. if (error) {
  820. char *save = *error;
  821. spprintf(error, 0, "phar \"%s\" MD5 signature could not be verified: %s", fname, *error);
  822. efree(save);
  823. }
  824. return FAILURE;
  825. }
  826. break;
  827. }
  828. default:
  829. efree(savebuf);
  830. php_stream_close(fp);
  831. if (error) {
  832. spprintf(error, 0, "phar \"%s\" has a broken or unsupported signature", fname);
  833. }
  834. return FAILURE;
  835. }
  836. } else if (PHAR_G(require_hash)) {
  837. efree(savebuf);
  838. php_stream_close(fp);
  839. if (error) {
  840. spprintf(error, 0, "phar \"%s\" does not have a signature", fname);
  841. }
  842. return FAILURE;
  843. } else {
  844. sig_flags = 0;
  845. sig_len = 0;
  846. }
  847. /* extract alias */
  848. PHAR_GET_32(buffer, tmp_len);
  849. if (buffer + tmp_len > endbuffer) {
  850. MAPPHAR_FAIL("internal corruption of phar \"%s\" (buffer overrun)");
  851. }
  852. if (manifest_len < 10 + tmp_len) {
  853. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
  854. }
  855. /* tmp_len = 0 says alias length is 0, which means the alias is not stored in the phar */
  856. if (tmp_len) {
  857. /* if the alias is stored we enforce it (implicit overrides explicit) */
  858. if (alias && alias_len && (alias_len != (int)tmp_len || strncmp(alias, buffer, tmp_len)))
  859. {
  860. buffer[tmp_len] = '\0';
  861. php_stream_close(fp);
  862. if (signature) {
  863. efree(signature);
  864. }
  865. if (error) {
  866. spprintf(error, 0, "cannot load phar \"%s\" with implicit alias \"%s\" under different alias \"%s\"", fname, buffer, alias);
  867. }
  868. efree(savebuf);
  869. return FAILURE;
  870. }
  871. alias_len = tmp_len;
  872. alias = buffer;
  873. buffer += tmp_len;
  874. register_alias = 1;
  875. } else if (!alias_len || !alias) {
  876. /* if we neither have an explicit nor an implicit alias, we use the filename */
  877. alias = NULL;
  878. alias_len = 0;
  879. register_alias = 0;
  880. } else if (alias_len) {
  881. register_alias = 1;
  882. temp_alias = 1;
  883. }
  884. /* we have 5 32-bit items plus 1 byte at least */
  885. if (manifest_count > ((manifest_len - 10 - tmp_len) / (5 * 4 + 1))) {
  886. /* prevent serious memory issues */
  887. MAPPHAR_FAIL("internal corruption of phar \"%s\" (too many manifest entries for size of manifest)")
  888. }
  889. mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
  890. mydata->is_persistent = PHAR_G(persist);
  891. /* check whether we have meta data, zero check works regardless of byte order */
  892. if (mydata->is_persistent) {
  893. PHAR_GET_32(buffer, mydata->metadata_len);
  894. if (phar_parse_metadata(&buffer, &mydata->metadata, mydata->metadata_len TSRMLS_CC) == FAILURE) {
  895. MAPPHAR_FAIL("unable to read phar metadata in .phar file \"%s\"");
  896. }
  897. } else {
  898. if (phar_parse_metadata(&buffer, &mydata->metadata, 0 TSRMLS_CC) == FAILURE) {
  899. MAPPHAR_FAIL("unable to read phar metadata in .phar file \"%s\"");
  900. }
  901. }
  902. /* set up our manifest */
  903. zend_hash_init(&mydata->manifest, manifest_count,
  904. zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)mydata->is_persistent);
  905. zend_hash_init(&mydata->mounted_dirs, 5,
  906. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  907. zend_hash_init(&mydata->virtual_dirs, manifest_count * 2,
  908. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  909. mydata->fname = pestrndup(fname, fname_len, mydata->is_persistent);
  910. #ifdef PHP_WIN32
  911. phar_unixify_path_separators(mydata->fname, fname_len);
  912. #endif
  913. mydata->fname_len = fname_len;
  914. offset = halt_offset + manifest_len + 4;
  915. memset(&entry, 0, sizeof(phar_entry_info));
  916. entry.phar = mydata;
  917. entry.fp_type = PHAR_FP;
  918. entry.is_persistent = mydata->is_persistent;
  919. for (manifest_index = 0; manifest_index < manifest_count; ++manifest_index) {
  920. if (buffer + 4 > endbuffer) {
  921. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)")
  922. }
  923. PHAR_GET_32(buffer, entry.filename_len);
  924. if (entry.filename_len == 0) {
  925. MAPPHAR_FAIL("zero-length filename encountered in phar \"%s\"");
  926. }
  927. if (entry.is_persistent) {
  928. entry.manifest_pos = manifest_index;
  929. }
  930. if (buffer + entry.filename_len + 20 > endbuffer) {
  931. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)");
  932. }
  933. if ((manifest_ver & PHAR_API_VER_MASK) >= PHAR_API_MIN_DIR && buffer[entry.filename_len - 1] == '/') {
  934. entry.is_dir = 1;
  935. } else {
  936. entry.is_dir = 0;
  937. }
  938. phar_add_virtual_dirs(mydata, buffer, entry.filename_len TSRMLS_CC);
  939. entry.filename = pestrndup(buffer, entry.filename_len, entry.is_persistent);
  940. buffer += entry.filename_len;
  941. PHAR_GET_32(buffer, entry.uncompressed_filesize);
  942. PHAR_GET_32(buffer, entry.timestamp);
  943. if (offset == halt_offset + (int)manifest_len + 4) {
  944. mydata->min_timestamp = entry.timestamp;
  945. mydata->max_timestamp = entry.timestamp;
  946. } else {
  947. if (mydata->min_timestamp > entry.timestamp) {
  948. mydata->min_timestamp = entry.timestamp;
  949. } else if (mydata->max_timestamp < entry.timestamp) {
  950. mydata->max_timestamp = entry.timestamp;
  951. }
  952. }
  953. PHAR_GET_32(buffer, entry.compressed_filesize);
  954. PHAR_GET_32(buffer, entry.crc32);
  955. PHAR_GET_32(buffer, entry.flags);
  956. if (entry.is_dir) {
  957. entry.filename_len--;
  958. entry.flags |= PHAR_ENT_PERM_DEF_DIR;
  959. }
  960. if (entry.is_persistent) {
  961. PHAR_GET_32(buffer, entry.metadata_len);
  962. if (!entry.metadata_len) buffer -= 4;
  963. if (phar_parse_metadata(&buffer, &entry.metadata, entry.metadata_len TSRMLS_CC) == FAILURE) {
  964. pefree(entry.filename, entry.is_persistent);
  965. MAPPHAR_FAIL("unable to read file metadata in .phar file \"%s\"");
  966. }
  967. } else {
  968. if (phar_parse_metadata(&buffer, &entry.metadata, 0 TSRMLS_CC) == FAILURE) {
  969. pefree(entry.filename, entry.is_persistent);
  970. MAPPHAR_FAIL("unable to read file metadata in .phar file \"%s\"");
  971. }
  972. }
  973. entry.offset = entry.offset_abs = offset;
  974. offset += entry.compressed_filesize;
  975. switch (entry.flags & PHAR_ENT_COMPRESSION_MASK) {
  976. case PHAR_ENT_COMPRESSED_GZ:
  977. if (!PHAR_G(has_zlib)) {
  978. if (entry.metadata) {
  979. if (entry.is_persistent) {
  980. free(entry.metadata);
  981. } else {
  982. zval_ptr_dtor(&entry.metadata);
  983. }
  984. }
  985. pefree(entry.filename, entry.is_persistent);
  986. MAPPHAR_FAIL("zlib extension is required for gz compressed .phar file \"%s\"");
  987. }
  988. break;
  989. case PHAR_ENT_COMPRESSED_BZ2:
  990. if (!PHAR_G(has_bz2)) {
  991. if (entry.metadata) {
  992. if (entry.is_persistent) {
  993. free(entry.metadata);
  994. } else {
  995. zval_ptr_dtor(&entry.metadata);
  996. }
  997. }
  998. pefree(entry.filename, entry.is_persistent);
  999. MAPPHAR_FAIL("bz2 extension is required for bzip2 compressed .phar file \"%s\"");
  1000. }
  1001. break;
  1002. default:
  1003. if (entry.uncompressed_filesize != entry.compressed_filesize) {
  1004. if (entry.metadata) {
  1005. if (entry.is_persistent) {
  1006. free(entry.metadata);
  1007. } else {
  1008. zval_ptr_dtor(&entry.metadata);
  1009. }
  1010. }
  1011. pefree(entry.filename, entry.is_persistent);
  1012. MAPPHAR_FAIL("internal corruption of phar \"%s\" (compressed and uncompressed size does not match for uncompressed entry)");
  1013. }
  1014. break;
  1015. }
  1016. manifest_flags |= (entry.flags & PHAR_ENT_COMPRESSION_MASK);
  1017. /* if signature matched, no need to check CRC32 for each file */
  1018. entry.is_crc_checked = (manifest_flags & PHAR_HDR_SIGNATURE ? 1 : 0);
  1019. phar_set_inode(&entry TSRMLS_CC);
  1020. zend_hash_add(&mydata->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL);
  1021. }
  1022. snprintf(mydata->version, sizeof(mydata->version), "%u.%u.%u", manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0xF);
  1023. mydata->internal_file_start = halt_offset + manifest_len + 4;
  1024. mydata->halt_offset = halt_offset;
  1025. mydata->flags = manifest_flags;
  1026. endbuffer = strrchr(mydata->fname, '/');
  1027. if (endbuffer) {
  1028. mydata->ext = memchr(endbuffer, '.', (mydata->fname + fname_len) - endbuffer);
  1029. if (mydata->ext == endbuffer) {
  1030. mydata->ext = memchr(endbuffer + 1, '.', (mydata->fname + fname_len) - endbuffer - 1);
  1031. }
  1032. if (mydata->ext) {
  1033. mydata->ext_len = (mydata->fname + mydata->fname_len) - mydata->ext;
  1034. }
  1035. }
  1036. mydata->alias = alias ?
  1037. pestrndup(alias, alias_len, mydata->is_persistent) :
  1038. pestrndup(mydata->fname, fname_len, mydata->is_persistent);
  1039. mydata->alias_len = alias ? alias_len : fname_len;
  1040. mydata->sig_flags = sig_flags;
  1041. mydata->fp = fp;
  1042. mydata->sig_len = sig_len;
  1043. mydata->signature = signature;
  1044. phar_request_initialize(TSRMLS_C);
  1045. if (register_alias) {
  1046. phar_archive_data **fd_ptr;
  1047. mydata->is_temporary_alias = temp_alias;
  1048. if (!phar_validate_alias(mydata->alias, mydata->alias_len)) {
  1049. signature = NULL;
  1050. fp = NULL;
  1051. MAPPHAR_FAIL("Cannot open archive \"%s\", invalid alias");
  1052. }
  1053. if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void **)&fd_ptr)) {
  1054. if (SUCCESS != phar_free_alias(*fd_ptr, alias, alias_len TSRMLS_CC)) {
  1055. signature = NULL;
  1056. fp = NULL;
  1057. MAPPHAR_FAIL("Cannot open archive \"%s\", alias is already in use by existing archive");
  1058. }
  1059. }
  1060. zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  1061. } else {
  1062. mydata->is_temporary_alias = 1;
  1063. }
  1064. zend_hash_add(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  1065. efree(savebuf);
  1066. if (pphar) {
  1067. *pphar = mydata;
  1068. }
  1069. return SUCCESS;
  1070. }
  1071. /* }}} */
  1072. /**
  1073. * Create or open a phar for writing
  1074. */
  1075. int phar_open_or_create_filename(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  1076. {
  1077. const char *ext_str, *z;
  1078. char *my_error;
  1079. int ext_len;
  1080. phar_archive_data **test, *unused = NULL;
  1081. test = &unused;
  1082. if (error) {
  1083. *error = NULL;
  1084. }
  1085. /* first try to open an existing file */
  1086. if (phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 0, 1 TSRMLS_CC) == SUCCESS) {
  1087. goto check_file;
  1088. }
  1089. /* next try to create a new file */
  1090. if (FAILURE == phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 1, 1 TSRMLS_CC)) {
  1091. if (error) {
  1092. if (ext_len == -2) {
  1093. spprintf(error, 0, "Cannot create a phar archive from a URL like \"%s\". Phar objects can only be created from local files", fname);
  1094. } else {
  1095. spprintf(error, 0, "Cannot create phar '%s', file extension (or combination) not recognised or the directory does not exist", fname);
  1096. }
  1097. }
  1098. return FAILURE;
  1099. }
  1100. check_file:
  1101. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error TSRMLS_CC) == SUCCESS) {
  1102. if (pphar) {
  1103. *pphar = *test;
  1104. }
  1105. if ((*test)->is_data && !(*test)->is_tar && !(*test)->is_zip) {
  1106. if (error) {
  1107. spprintf(error, 0, "Cannot open '%s' as a PharData object. Use Phar::__construct() for executable archives", fname);
  1108. }
  1109. return FAILURE;
  1110. }
  1111. if (PHAR_G(readonly) && !(*test)->is_data && ((*test)->is_tar || (*test)->is_zip)) {
  1112. phar_entry_info *stub;
  1113. if (FAILURE == zend_hash_find(&((*test)->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1, (void **)&stub)) {
  1114. spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
  1115. return FAILURE;
  1116. }
  1117. }
  1118. if (!PHAR_G(readonly) || (*test)->is_data) {
  1119. (*test)->is_writeable = 1;
  1120. }
  1121. return SUCCESS;
  1122. } else if (my_error) {
  1123. if (error) {
  1124. *error = my_error;
  1125. } else {
  1126. efree(my_error);
  1127. }
  1128. return FAILURE;
  1129. }
  1130. if (ext_len > 3 && (z = memchr(ext_str, 'z', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ip", 2)) {
  1131. /* assume zip-based phar */
  1132. return phar_open_or_create_zip(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC);
  1133. }
  1134. if (ext_len > 3 && (z = memchr(ext_str, 't', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ar", 2)) {
  1135. /* assume tar-based phar */
  1136. return phar_open_or_create_tar(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC);
  1137. }
  1138. return phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC);
  1139. }
  1140. /* }}} */
  1141. int phar_create_or_parse_filename(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  1142. {
  1143. phar_archive_data *mydata;
  1144. php_stream *fp;
  1145. char *actual = NULL, *p;
  1146. if (!pphar) {
  1147. pphar = &mydata;
  1148. }
  1149. if (php_check_open_basedir(fname TSRMLS_CC)) {
  1150. return FAILURE;
  1151. }
  1152. /* first open readonly so it won't be created if not present */
  1153. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, &actual);
  1154. if (actual) {
  1155. fname = actual;
  1156. fname_len = strlen(actual);
  1157. }
  1158. if (fp) {
  1159. if (phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error TSRMLS_CC) == SUCCESS) {
  1160. if ((*pphar)->is_data || !PHAR_G(readonly)) {
  1161. (*pphar)->is_writeable = 1;
  1162. }
  1163. if (actual) {
  1164. efree(actual);
  1165. }
  1166. return SUCCESS;
  1167. } else {
  1168. /* file exists, but is either corrupt or not a phar archive */
  1169. if (actual) {
  1170. efree(actual);
  1171. }
  1172. return FAILURE;
  1173. }
  1174. }
  1175. if (actual) {
  1176. efree(actual);
  1177. }
  1178. if (PHAR_G(readonly) && !is_data) {
  1179. if (options & REPORT_ERRORS) {
  1180. if (error) {
  1181. spprintf(error, 0, "creating archive \"%s\" disabled by the php.ini setting phar.readonly", fname);
  1182. }
  1183. }
  1184. return FAILURE;
  1185. }
  1186. /* set up our manifest */
  1187. mydata = ecalloc(1, sizeof(phar_archive_data));
  1188. mydata->fname = expand_filepath(fname, NULL TSRMLS_CC);
  1189. fname_len = strlen(mydata->fname);
  1190. #ifdef PHP_WIN32
  1191. phar_unixify_path_separators(mydata->fname, fname_len);
  1192. #endif
  1193. p = strrchr(mydata->fname, '/');
  1194. if (p) {
  1195. mydata->ext = memchr(p, '.', (mydata->fname + fname_len) - p);
  1196. if (mydata->ext == p) {
  1197. mydata->ext = memchr(p + 1, '.', (mydata->fname + fname_len) - p - 1);
  1198. }
  1199. if (mydata->ext) {
  1200. mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
  1201. }
  1202. }
  1203. if (pphar) {
  1204. *pphar = mydata;
  1205. }
  1206. zend_hash_init(&mydata->manifest, sizeof(phar_entry_info),
  1207. zend_get_hash_value, destroy_phar_manifest_entry, 0);
  1208. zend_hash_init(&mydata->mounted_dirs, sizeof(char *),
  1209. zend_get_hash_value, NULL, 0);
  1210. zend_hash_init(&mydata->virtual_dirs, sizeof(char *),
  1211. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  1212. mydata->fname_len = fname_len;
  1213. snprintf(mydata->version, sizeof(mydata->version), "%s", PHP_PHAR_API_VERSION);
  1214. mydata->is_temporary_alias = alias ? 0 : 1;
  1215. mydata->internal_file_start = -1;
  1216. mydata->fp = NULL;
  1217. mydata->is_writeable = 1;
  1218. mydata->is_brandnew = 1;
  1219. phar_request_initialize(TSRMLS_C);
  1220. zend_hash_add(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  1221. if (is_data) {
  1222. alias = NULL;
  1223. alias_len = 0;
  1224. mydata->is_data = 1;
  1225. /* assume tar format, PharData can specify other */
  1226. mydata->is_tar = 1;
  1227. } else {
  1228. phar_archive_data **fd_ptr;
  1229. if (alias && SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void **)&fd_ptr)) {
  1230. if (SUCCESS != phar_free_alias(*fd_ptr, alias, alias_len TSRMLS_CC)) {
  1231. if (error) {
  1232. spprintf(error, 4096, "phar error: phar \"%s\" cannot set alias \"%s\", already in use by another phar archive", mydata->fname, alias);
  1233. }
  1234. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  1235. if (pphar) {
  1236. *pphar = NULL;
  1237. }
  1238. return FAILURE;
  1239. }
  1240. }
  1241. mydata->alias = alias ? estrndup(alias, alias_len) : estrndup(mydata->fname, fname_len);
  1242. mydata->alias_len = alias ? alias_len : fname_len;
  1243. }
  1244. if (alias_len && alias) {
  1245. if (FAILURE == zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void*)&mydata, sizeof(phar_archive_data*), NULL)) {
  1246. if (options & REPORT_ERRORS) {
  1247. if (error) {
  1248. spprintf(error, 0, "archive \"%s\" cannot be associated with alias \"%s\", already in use", fname, alias);
  1249. }
  1250. }
  1251. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  1252. if (pphar) {
  1253. *pphar = NULL;
  1254. }
  1255. return FAILURE;
  1256. }
  1257. }
  1258. return SUCCESS;
  1259. }
  1260. /* }}}*/
  1261. /**
  1262. * Return an already opened filename.
  1263. *
  1264. * Or scan a phar file for the required __HALT_COMPILER(); ?> token and verify
  1265. * that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
  1266. * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
  1267. */
  1268. int phar_open_from_filename(char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  1269. {
  1270. php_stream *fp;
  1271. char *actual;
  1272. int ret, is_data = 0;
  1273. if (error) {
  1274. *error = NULL;
  1275. }
  1276. if (!strstr(fname, ".phar")) {
  1277. is_data = 1;
  1278. }
  1279. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC) == SUCCESS) {
  1280. return SUCCESS;
  1281. } else if (error && *error) {
  1282. return FAILURE;
  1283. }
  1284. if (php_check_open_basedir(fname TSRMLS_CC)) {
  1285. return FAILURE;
  1286. }
  1287. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
  1288. if (!fp) {
  1289. if (options & REPORT_ERRORS) {
  1290. if (error) {
  1291. spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
  1292. }
  1293. }
  1294. if (actual) {
  1295. efree(actual);
  1296. }
  1297. return FAILURE;
  1298. }
  1299. if (actual) {
  1300. fname = actual;
  1301. fname_len = strlen(actual);
  1302. }
  1303. ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error TSRMLS_CC);
  1304. if (actual) {
  1305. efree(actual);
  1306. }
  1307. return ret;
  1308. }
  1309. /* }}}*/
  1310. static inline char *phar_strnstr(const char *buf, int buf_len, const char *search, int search_len) /* {{{ */
  1311. {
  1312. const char *c;
  1313. int so_far = 0;
  1314. if (buf_len < search_len) {
  1315. return NULL;
  1316. }
  1317. c = buf - 1;
  1318. do {
  1319. if (!(c = memchr(c + 1, search[0], buf_len - search_len - so_far))) {
  1320. return (char *) NULL;
  1321. }
  1322. so_far = c - buf;
  1323. if (so_far >= (buf_len - search_len)) {
  1324. return (char *) NULL;
  1325. }
  1326. if (!memcmp(c, search, search_len)) {
  1327. return (char *) c;
  1328. }
  1329. } while (1);
  1330. }
  1331. /* }}} */
  1332. /**
  1333. * Scan an open fp for the required __HALT_COMPILER(); ?> token and verify
  1334. * that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
  1335. * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
  1336. */
  1337. static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, int is_data, char **error TSRMLS_DC) /* {{{ */
  1338. {
  1339. const char token[] = "__HALT_COMPILER();";
  1340. const char zip_magic[] = "PK\x03\x04";
  1341. const char gz_magic[] = "\x1f\x8b\x08";
  1342. const char bz_magic[] = "BZh";
  1343. char *pos, buffer[1024 + sizeof(token)], test = '\0';
  1344. const long readsize = sizeof(buffer) - sizeof(token);
  1345. const long tokenlen = sizeof(token) - 1;
  1346. long halt_offset;
  1347. size_t got;
  1348. php_uint32 compression = PHAR_FILE_COMPRESSED_NONE;
  1349. if (error) {
  1350. *error = NULL;
  1351. }
  1352. if (-1 == php_stream_rewind(fp)) {
  1353. MAPPHAR_ALLOC_FAIL("cannot rewind phar \"%s\"")
  1354. }
  1355. buffer[sizeof(buffer)-1] = '\0';
  1356. memset(buffer, 32, sizeof(token));
  1357. halt_offset = 0;
  1358. /* Maybe it's better to compile the file instead of just searching, */
  1359. /* but we only want the offset. So we want a .re scanner to find it. */
  1360. while(!php_stream_eof(fp)) {
  1361. if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < (size_t) tokenlen) {
  1362. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
  1363. }
  1364. if (!test) {
  1365. test = '\1';
  1366. pos = buffer+tokenlen;
  1367. if (!memcmp(pos, gz_magic, 3)) {
  1368. char err = 0;
  1369. php_stream_filter *filter;
  1370. php_stream *temp;
  1371. /* to properly decompress, we have to tell zlib to look for a zlib or gzip header */
  1372. zval filterparams;
  1373. if (!PHAR_G(has_zlib)) {
  1374. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file, enable zlib extension in php.ini")
  1375. }
  1376. array_init(&filterparams);
  1377. /* this is defined in zlib's zconf.h */
  1378. #ifndef MAX_WBITS
  1379. #define MAX_WBITS 15
  1380. #endif
  1381. add_assoc_long(&filterparams, "window", MAX_WBITS + 32);
  1382. /* entire file is gzip-compressed, uncompress to temporary file */
  1383. if (!(temp = php_stream_fopen_tmpfile())) {
  1384. MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
  1385. }
  1386. php_stream_rewind(fp);
  1387. filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp) TSRMLS_CC);
  1388. if (!filter) {
  1389. err = 1;
  1390. add_assoc_long(&filterparams, "window", MAX_WBITS);
  1391. filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp) TSRMLS_CC);
  1392. zval_dtor(&filterparams);
  1393. if (!filter) {
  1394. php_stream_close(temp);
  1395. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
  1396. }
  1397. } else {
  1398. zval_dtor(&filterparams);
  1399. }
  1400. php_stream_filter_append(&temp->writefilters, filter);
  1401. if (SUCCESS != phar_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
  1402. if (err) {
  1403. php_stream_close(temp);
  1404. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
  1405. }
  1406. php_stream_close(temp);
  1407. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file")
  1408. }
  1409. php_stream_filter_flush(filter, 1);
  1410. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  1411. php_stream_close(fp);
  1412. fp = temp;
  1413. php_stream_rewind(fp);
  1414. compression = PHAR_FILE_COMPRESSED_GZ;
  1415. /* now, start over */
  1416. test = '\0';
  1417. continue;
  1418. } else if (!memcmp(pos, bz_magic, 3)) {
  1419. php_stream_filter *filter;
  1420. php_stream *temp;
  1421. if (!PHAR_G(has_bz2)) {
  1422. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file, enable bz2 extension in php.ini")
  1423. }
  1424. /* entire file is bzip-compressed, uncompress to temporary file */
  1425. if (!(temp = php_stream_fopen_tmpfile())) {
  1426. MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of bzipped phar archive \"%s\"")
  1427. }
  1428. php_stream_rewind(fp);
  1429. filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp) TSRMLS_CC);
  1430. if (!filter) {
  1431. php_stream_close(temp);
  1432. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\", filter creation failed")
  1433. }
  1434. php_stream_filter_append(&temp->writefilters, filter);
  1435. if (SUCCESS != phar_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
  1436. php_stream_close(temp);
  1437. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file")
  1438. }
  1439. php_stream_filter_flush(filter, 1);
  1440. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  1441. php_stream_close(fp);
  1442. fp = temp;
  1443. php_stream_rewind(fp);
  1444. compression = PHAR_FILE_COMPRESSED_BZ2;
  1445. /* now, start over */
  1446. test = '\0';
  1447. continue;
  1448. }
  1449. if (!memcmp(pos, zip_magic, 4)) {
  1450. php_stream_seek(fp, 0, SEEK_END);
  1451. return phar_parse_zipfile(fp, fname, fname_len, alias, alias_len, pphar, error TSRMLS_CC);
  1452. }
  1453. if (got > 512) {
  1454. if (phar_is_tar(pos, fname)) {
  1455. php_stream_rewind(fp);
  1456. return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error TSRMLS_CC);
  1457. }
  1458. }
  1459. }
  1460. if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {
  1461. halt_offset += (pos - buffer); /* no -tokenlen+tokenlen here */
  1462. return phar_parse_pharfile(fp, fname, fname_len, alias, alias_len, halt_offset, pphar, comp

Large files files are truncated, but you can click here to view the full file