tar.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. /*
  2. +----------------------------------------------------------------------+
  3. | TAR archive support for Phar |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | https://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: Dmitry Stogov <dmitry@php.net> |
  16. | Gregory Beaver <cellog@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "phar_internal.h"
  20. static uint32_t phar_tar_number(char *buf, size_t len) /* {{{ */
  21. {
  22. uint32_t num = 0;
  23. size_t i = 0;
  24. while (i < len && buf[i] == ' ') {
  25. ++i;
  26. }
  27. while (i < len && buf[i] >= '0' && buf[i] <= '7') {
  28. num = num * 8 + (buf[i] - '0');
  29. ++i;
  30. }
  31. return num;
  32. }
  33. /* }}} */
  34. /* adapted from format_octal() in libarchive
  35. *
  36. * Copyright (c) 2003-2009 Tim Kientzle
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  49. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  50. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  51. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  52. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  54. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  55. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  56. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  57. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. */
  59. static int phar_tar_octal(char *buf, uint32_t val, int len) /* {{{ */
  60. {
  61. char *p = buf;
  62. int s = len;
  63. p += len; /* Start at the end and work backwards. */
  64. while (s-- > 0) {
  65. *--p = (char)('0' + (val & 7));
  66. val >>= 3;
  67. }
  68. if (val == 0)
  69. return SUCCESS;
  70. /* If it overflowed, fill field with max value. */
  71. while (len-- > 0)
  72. *p++ = '7';
  73. return FAILURE;
  74. }
  75. /* }}} */
  76. static uint32_t phar_tar_checksum(char *buf, size_t len) /* {{{ */
  77. {
  78. uint32_t sum = 0;
  79. char *end = buf + len;
  80. while (buf != end) {
  81. sum += (unsigned char)*buf;
  82. ++buf;
  83. }
  84. return sum;
  85. }
  86. /* }}} */
  87. int phar_is_tar(char *buf, char *fname) /* {{{ */
  88. {
  89. tar_header *header = (tar_header *) buf;
  90. uint32_t checksum = phar_tar_number(header->checksum, sizeof(header->checksum));
  91. uint32_t ret;
  92. char save[sizeof(header->checksum)], *bname;
  93. /* assume that the first filename in a tar won't begin with <?php */
  94. if (!strncmp(buf, "<?php", sizeof("<?php")-1)) {
  95. return 0;
  96. }
  97. memcpy(save, header->checksum, sizeof(header->checksum));
  98. memset(header->checksum, ' ', sizeof(header->checksum));
  99. ret = (checksum == phar_tar_checksum(buf, 512));
  100. memcpy(header->checksum, save, sizeof(header->checksum));
  101. if ((bname = strrchr(fname, PHP_DIR_SEPARATOR))) {
  102. fname = bname;
  103. }
  104. if (!ret && (bname = strstr(fname, ".tar")) && (bname[4] == '\0' || bname[4] == '.')) {
  105. /* probably a corrupted tar - so we will pretend it is one */
  106. return 1;
  107. }
  108. return ret;
  109. }
  110. /* }}} */
  111. int phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
  112. {
  113. phar_archive_data *phar;
  114. int ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error);
  115. if (FAILURE == ret) {
  116. return FAILURE;
  117. }
  118. if (pphar) {
  119. *pphar = phar;
  120. }
  121. phar->is_data = is_data;
  122. if (phar->is_tar) {
  123. return ret;
  124. }
  125. if (phar->is_brandnew) {
  126. phar->is_tar = 1;
  127. phar->is_zip = 0;
  128. phar->internal_file_start = 0;
  129. return SUCCESS;
  130. }
  131. /* we've reached here - the phar exists and is a regular phar */
  132. if (error) {
  133. spprintf(error, 4096, "phar tar error: \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a tar-based phar", fname);
  134. }
  135. return FAILURE;
  136. }
  137. /* }}} */
  138. static int phar_tar_process_metadata(phar_entry_info *entry, php_stream *fp) /* {{{ */
  139. {
  140. char *metadata;
  141. size_t save = php_stream_tell(fp), read;
  142. phar_entry_info *mentry;
  143. metadata = (char *) safe_emalloc(1, entry->uncompressed_filesize, 1);
  144. read = php_stream_read(fp, metadata, entry->uncompressed_filesize);
  145. if (read != entry->uncompressed_filesize) {
  146. efree(metadata);
  147. php_stream_seek(fp, save, SEEK_SET);
  148. return FAILURE;
  149. }
  150. phar_parse_metadata_lazy(metadata, &entry->metadata_tracker, entry->uncompressed_filesize, entry->is_persistent);
  151. if (entry->filename_len == sizeof(".phar/.metadata.bin")-1 && !memcmp(entry->filename, ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1)) {
  152. if (phar_metadata_tracker_has_data(&entry->phar->metadata_tracker, entry->phar->is_persistent)) {
  153. efree(metadata);
  154. return FAILURE;
  155. }
  156. entry->phar->metadata_tracker = entry->metadata_tracker;
  157. entry->metadata_tracker.str = NULL;
  158. ZVAL_UNDEF(&entry->metadata_tracker.val);
  159. } else if (entry->filename_len >= sizeof(".phar/.metadata/") + sizeof("/.metadata.bin") - 1 && NULL != (mentry = zend_hash_str_find_ptr(&(entry->phar->manifest), entry->filename + sizeof(".phar/.metadata/") - 1, entry->filename_len - (sizeof("/.metadata.bin") - 1 + sizeof(".phar/.metadata/") - 1)))) {
  160. if (phar_metadata_tracker_has_data(&mentry->metadata_tracker, mentry->is_persistent)) {
  161. efree(metadata);
  162. return FAILURE;
  163. }
  164. /* transfer this metadata to the entry it refers */
  165. mentry->metadata_tracker = entry->metadata_tracker;
  166. entry->metadata_tracker.str = NULL;
  167. ZVAL_UNDEF(&entry->metadata_tracker.val);
  168. }
  169. efree(metadata);
  170. php_stream_seek(fp, save, SEEK_SET);
  171. return SUCCESS;
  172. }
  173. /* }}} */
  174. #ifndef HAVE_STRNLEN
  175. static size_t strnlen(const char *s, size_t maxlen) {
  176. char *r = (char *)memchr(s, '\0', maxlen);
  177. return r ? r-s : maxlen;
  178. }
  179. #endif
  180. int phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, int is_data, uint32_t compression, char **error) /* {{{ */
  181. {
  182. char buf[512], *actual_alias = NULL, *p;
  183. phar_entry_info entry = {0};
  184. size_t pos = 0, read, totalsize;
  185. tar_header *hdr;
  186. uint32_t sum1, sum2, size, old;
  187. phar_archive_data *myphar, *actual;
  188. int last_was_longlink = 0;
  189. size_t linkname_len;
  190. if (error) {
  191. *error = NULL;
  192. }
  193. php_stream_seek(fp, 0, SEEK_END);
  194. totalsize = php_stream_tell(fp);
  195. php_stream_seek(fp, 0, SEEK_SET);
  196. read = php_stream_read(fp, buf, sizeof(buf));
  197. if (read != sizeof(buf)) {
  198. if (error) {
  199. spprintf(error, 4096, "phar error: \"%s\" is not a tar file or is truncated", fname);
  200. }
  201. php_stream_close(fp);
  202. return FAILURE;
  203. }
  204. hdr = (tar_header*)buf;
  205. old = (memcmp(hdr->magic, "ustar", sizeof("ustar")-1) != 0);
  206. myphar = (phar_archive_data *) pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
  207. myphar->is_persistent = PHAR_G(persist);
  208. /* estimate number of entries, can't be certain with tar files */
  209. zend_hash_init(&myphar->manifest, 2 + (totalsize >> 12),
  210. zend_get_hash_value, destroy_phar_manifest_entry, (bool)myphar->is_persistent);
  211. zend_hash_init(&myphar->mounted_dirs, 5,
  212. zend_get_hash_value, NULL, (bool)myphar->is_persistent);
  213. zend_hash_init(&myphar->virtual_dirs, 4 + (totalsize >> 11),
  214. zend_get_hash_value, NULL, (bool)myphar->is_persistent);
  215. myphar->is_tar = 1;
  216. /* remember whether this entire phar was compressed with gz/bzip2 */
  217. myphar->flags = compression;
  218. entry.is_tar = 1;
  219. entry.is_crc_checked = 1;
  220. entry.phar = myphar;
  221. pos += sizeof(buf);
  222. do {
  223. phar_entry_info *newentry;
  224. pos = php_stream_tell(fp);
  225. hdr = (tar_header*) buf;
  226. sum1 = phar_tar_number(hdr->checksum, sizeof(hdr->checksum));
  227. if (sum1 == 0 && phar_tar_checksum(buf, sizeof(buf)) == 0) {
  228. break;
  229. }
  230. memset(hdr->checksum, ' ', sizeof(hdr->checksum));
  231. sum2 = phar_tar_checksum(buf, old?sizeof(old_tar_header):sizeof(tar_header));
  232. if (old && sum2 != sum1) {
  233. uint32_t sum3 = phar_tar_checksum(buf, sizeof(tar_header));
  234. if (sum3 == sum1) {
  235. /* apparently a broken tar which is in ustar format w/o setting the ustar marker */
  236. sum2 = sum3;
  237. old = 0;
  238. }
  239. }
  240. size = entry.uncompressed_filesize = entry.compressed_filesize =
  241. phar_tar_number(hdr->size, sizeof(hdr->size));
  242. /* skip global/file headers (pax) */
  243. if (!old && (hdr->typeflag == TAR_GLOBAL_HDR || hdr->typeflag == TAR_FILE_HDR)) {
  244. size = (size+511)&~511;
  245. goto next;
  246. }
  247. if (((!old && hdr->prefix[0] == 0) || old) && strnlen(hdr->name, 100) == sizeof(".phar/signature.bin")-1 && !strncmp(hdr->name, ".phar/signature.bin", sizeof(".phar/signature.bin")-1)) {
  248. zend_off_t curloc;
  249. size_t sig_len;
  250. if (size > 511) {
  251. if (error) {
  252. spprintf(error, 4096, "phar error: tar-based phar \"%s\" has signature that is larger than 511 bytes, cannot process", fname);
  253. }
  254. bail:
  255. php_stream_close(fp);
  256. phar_destroy_phar_data(myphar);
  257. return FAILURE;
  258. }
  259. curloc = php_stream_tell(fp);
  260. read = php_stream_read(fp, buf, size);
  261. if (read != size || read <= 8) {
  262. if (error) {
  263. spprintf(error, 4096, "phar error: tar-based phar \"%s\" signature cannot be read", fname);
  264. }
  265. goto bail;
  266. }
  267. #ifdef WORDS_BIGENDIAN
  268. # define PHAR_GET_32(buffer) \
  269. (((((unsigned char*)(buffer))[3]) << 24) \
  270. | ((((unsigned char*)(buffer))[2]) << 16) \
  271. | ((((unsigned char*)(buffer))[1]) << 8) \
  272. | (((unsigned char*)(buffer))[0]))
  273. #else
  274. # define PHAR_GET_32(buffer) (uint32_t) *(buffer)
  275. #endif
  276. myphar->sig_flags = PHAR_GET_32(buf);
  277. if (FAILURE == phar_verify_signature(fp, php_stream_tell(fp) - size - 512, myphar->sig_flags, buf + 8, size - 8, fname, &myphar->signature, &sig_len, error)) {
  278. if (error) {
  279. char *save = *error;
  280. spprintf(error, 4096, "phar error: tar-based phar \"%s\" signature cannot be verified: %s", fname, save);
  281. efree(save);
  282. }
  283. goto bail;
  284. }
  285. myphar->sig_len = sig_len;
  286. php_stream_seek(fp, curloc + 512, SEEK_SET);
  287. /* signature checked out, let's ensure this is the last file in the phar */
  288. if (((hdr->typeflag == '\0') || (hdr->typeflag == TAR_FILE)) && size > 0) {
  289. /* this is not good enough - seek succeeds even on truncated tars */
  290. php_stream_seek(fp, 512, SEEK_CUR);
  291. if ((uint32_t)php_stream_tell(fp) > totalsize) {
  292. if (error) {
  293. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  294. }
  295. php_stream_close(fp);
  296. phar_destroy_phar_data(myphar);
  297. return FAILURE;
  298. }
  299. }
  300. read = php_stream_read(fp, buf, sizeof(buf));
  301. if (read != sizeof(buf)) {
  302. if (error) {
  303. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  304. }
  305. php_stream_close(fp);
  306. phar_destroy_phar_data(myphar);
  307. return FAILURE;
  308. }
  309. hdr = (tar_header*) buf;
  310. sum1 = phar_tar_number(hdr->checksum, sizeof(hdr->checksum));
  311. if (sum1 == 0 && phar_tar_checksum(buf, sizeof(buf)) == 0) {
  312. break;
  313. }
  314. if (error) {
  315. spprintf(error, 4096, "phar error: \"%s\" has entries after signature, invalid phar", fname);
  316. }
  317. goto bail;
  318. }
  319. if (!last_was_longlink && hdr->typeflag == 'L') {
  320. last_was_longlink = 1;
  321. /* support the ././@LongLink system for storing long filenames */
  322. entry.filename_len = entry.uncompressed_filesize;
  323. /* Check for overflow - bug 61065 */
  324. if (entry.filename_len == UINT_MAX || entry.filename_len == 0) {
  325. if (error) {
  326. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (invalid entry size)", fname);
  327. }
  328. php_stream_close(fp);
  329. phar_destroy_phar_data(myphar);
  330. return FAILURE;
  331. }
  332. entry.filename = pemalloc(entry.filename_len+1, myphar->is_persistent);
  333. read = php_stream_read(fp, entry.filename, entry.filename_len);
  334. if (read != entry.filename_len) {
  335. efree(entry.filename);
  336. if (error) {
  337. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  338. }
  339. php_stream_close(fp);
  340. phar_destroy_phar_data(myphar);
  341. return FAILURE;
  342. }
  343. entry.filename[entry.filename_len] = '\0';
  344. /* skip blank stuff */
  345. size = ((size+511)&~511) - size;
  346. /* this is not good enough - seek succeeds even on truncated tars */
  347. php_stream_seek(fp, size, SEEK_CUR);
  348. if ((uint32_t)php_stream_tell(fp) > totalsize) {
  349. efree(entry.filename);
  350. if (error) {
  351. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  352. }
  353. php_stream_close(fp);
  354. phar_destroy_phar_data(myphar);
  355. return FAILURE;
  356. }
  357. read = php_stream_read(fp, buf, sizeof(buf));
  358. if (read != sizeof(buf)) {
  359. efree(entry.filename);
  360. if (error) {
  361. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  362. }
  363. php_stream_close(fp);
  364. phar_destroy_phar_data(myphar);
  365. return FAILURE;
  366. }
  367. continue;
  368. } else if (!last_was_longlink && !old && hdr->prefix[0] != 0) {
  369. char name[256];
  370. int i, j;
  371. for (i = 0; i < 155; i++) {
  372. name[i] = hdr->prefix[i];
  373. if (name[i] == '\0') {
  374. break;
  375. }
  376. }
  377. name[i++] = '/';
  378. for (j = 0; j < 100; j++) {
  379. name[i+j] = hdr->name[j];
  380. if (name[i+j] == '\0') {
  381. break;
  382. }
  383. }
  384. entry.filename_len = i+j;
  385. if (name[entry.filename_len - 1] == '/') {
  386. /* some tar programs store directories with trailing slash */
  387. entry.filename_len--;
  388. }
  389. entry.filename = pestrndup(name, entry.filename_len, myphar->is_persistent);
  390. } else if (!last_was_longlink) {
  391. int i;
  392. /* calculate strlen, which can be no longer than 100 */
  393. for (i = 0; i < 100; i++) {
  394. if (hdr->name[i] == '\0') {
  395. break;
  396. }
  397. }
  398. entry.filename_len = i;
  399. entry.filename = pestrndup(hdr->name, i, myphar->is_persistent);
  400. if (i > 0 && entry.filename[entry.filename_len - 1] == '/') {
  401. /* some tar programs store directories with trailing slash */
  402. entry.filename[entry.filename_len - 1] = '\0';
  403. entry.filename_len--;
  404. }
  405. }
  406. last_was_longlink = 0;
  407. phar_add_virtual_dirs(myphar, entry.filename, entry.filename_len);
  408. if (sum1 != sum2) {
  409. if (error) {
  410. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (checksum mismatch of file \"%s\")", fname, entry.filename);
  411. }
  412. pefree(entry.filename, myphar->is_persistent);
  413. php_stream_close(fp);
  414. phar_destroy_phar_data(myphar);
  415. return FAILURE;
  416. }
  417. entry.tar_type = ((old & (hdr->typeflag == '\0')) ? TAR_FILE : hdr->typeflag);
  418. entry.offset = entry.offset_abs = pos; /* header_offset unused in tar */
  419. entry.fp_type = PHAR_FP;
  420. entry.flags = phar_tar_number(hdr->mode, sizeof(hdr->mode)) & PHAR_ENT_PERM_MASK;
  421. entry.timestamp = phar_tar_number(hdr->mtime, sizeof(hdr->mtime));
  422. entry.is_persistent = myphar->is_persistent;
  423. if (old && entry.tar_type == TAR_FILE && S_ISDIR(entry.flags)) {
  424. entry.tar_type = TAR_DIR;
  425. }
  426. if (entry.tar_type == TAR_DIR) {
  427. entry.is_dir = 1;
  428. } else {
  429. entry.is_dir = 0;
  430. }
  431. entry.link = NULL;
  432. /* link field is null-terminated unless it has 100 non-null chars.
  433. * Thus we can not use strlen. */
  434. linkname_len = strnlen(hdr->linkname, 100);
  435. if (entry.tar_type == TAR_LINK) {
  436. if (!zend_hash_str_exists(&myphar->manifest, hdr->linkname, linkname_len)) {
  437. if (error) {
  438. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file - hard link to non-existent file \"%.*s\"", fname, (int)linkname_len, hdr->linkname);
  439. }
  440. pefree(entry.filename, entry.is_persistent);
  441. php_stream_close(fp);
  442. phar_destroy_phar_data(myphar);
  443. return FAILURE;
  444. }
  445. entry.link = estrndup(hdr->linkname, linkname_len);
  446. } else if (entry.tar_type == TAR_SYMLINK) {
  447. entry.link = estrndup(hdr->linkname, linkname_len);
  448. }
  449. phar_set_inode(&entry);
  450. newentry = zend_hash_str_update_mem(&myphar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  451. ZEND_ASSERT(newentry != NULL);
  452. if (entry.is_persistent) {
  453. ++entry.manifest_pos;
  454. }
  455. if (entry.filename_len >= sizeof(".phar/.metadata")-1 && !memcmp(entry.filename, ".phar/.metadata", sizeof(".phar/.metadata")-1)) {
  456. if (FAILURE == phar_tar_process_metadata(newentry, fp)) {
  457. if (error) {
  458. spprintf(error, 4096, "phar error: tar-based phar \"%s\" has invalid metadata in magic file \"%s\"", fname, entry.filename);
  459. }
  460. php_stream_close(fp);
  461. phar_destroy_phar_data(myphar);
  462. return FAILURE;
  463. }
  464. }
  465. if (!actual_alias && entry.filename_len == sizeof(".phar/alias.txt")-1 && !strncmp(entry.filename, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
  466. /* found explicit alias */
  467. if (size > 511) {
  468. if (error) {
  469. spprintf(error, 4096, "phar error: tar-based phar \"%s\" has alias that is larger than 511 bytes, cannot process", fname);
  470. }
  471. php_stream_close(fp);
  472. phar_destroy_phar_data(myphar);
  473. return FAILURE;
  474. }
  475. read = php_stream_read(fp, buf, size);
  476. if (read == size) {
  477. buf[size] = '\0';
  478. if (!phar_validate_alias(buf, size)) {
  479. if (size > 50) {
  480. buf[50] = '.';
  481. buf[51] = '.';
  482. buf[52] = '.';
  483. buf[53] = '\0';
  484. }
  485. if (error) {
  486. spprintf(error, 4096, "phar error: invalid alias \"%s\" in tar-based phar \"%s\"", buf, fname);
  487. }
  488. php_stream_close(fp);
  489. phar_destroy_phar_data(myphar);
  490. return FAILURE;
  491. }
  492. actual_alias = pestrndup(buf, size, myphar->is_persistent);
  493. myphar->alias = actual_alias;
  494. myphar->alias_len = size;
  495. php_stream_seek(fp, pos, SEEK_SET);
  496. } else {
  497. if (error) {
  498. spprintf(error, 4096, "phar error: Unable to read alias from tar-based phar \"%s\"", fname);
  499. }
  500. php_stream_close(fp);
  501. phar_destroy_phar_data(myphar);
  502. return FAILURE;
  503. }
  504. }
  505. size = (size+511)&~511;
  506. if (((hdr->typeflag == '\0') || (hdr->typeflag == TAR_FILE)) && size > 0) {
  507. next:
  508. /* this is not good enough - seek succeeds even on truncated tars */
  509. php_stream_seek(fp, size, SEEK_CUR);
  510. if ((uint32_t)php_stream_tell(fp) > totalsize) {
  511. if (error) {
  512. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  513. }
  514. php_stream_close(fp);
  515. phar_destroy_phar_data(myphar);
  516. return FAILURE;
  517. }
  518. }
  519. read = php_stream_read(fp, buf, sizeof(buf));
  520. if (read != sizeof(buf)) {
  521. if (error) {
  522. spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
  523. }
  524. php_stream_close(fp);
  525. phar_destroy_phar_data(myphar);
  526. return FAILURE;
  527. }
  528. } while (!php_stream_eof(fp));
  529. if (zend_hash_str_exists(&(myphar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  530. myphar->is_data = 0;
  531. } else {
  532. myphar->is_data = 1;
  533. }
  534. /* ensure signature set */
  535. if (!myphar->is_data && PHAR_G(require_hash) && !myphar->signature) {
  536. php_stream_close(fp);
  537. phar_destroy_phar_data(myphar);
  538. if (error) {
  539. spprintf(error, 0, "tar-based phar \"%s\" does not have a signature", fname);
  540. }
  541. return FAILURE;
  542. }
  543. myphar->fname = pestrndup(fname, fname_len, myphar->is_persistent);
  544. #ifdef PHP_WIN32
  545. phar_unixify_path_separators(myphar->fname, fname_len);
  546. #endif
  547. myphar->fname_len = fname_len;
  548. myphar->fp = fp;
  549. p = strrchr(myphar->fname, '/');
  550. if (p) {
  551. myphar->ext = memchr(p, '.', (myphar->fname + fname_len) - p);
  552. if (myphar->ext == p) {
  553. myphar->ext = memchr(p + 1, '.', (myphar->fname + fname_len) - p - 1);
  554. }
  555. if (myphar->ext) {
  556. myphar->ext_len = (myphar->fname + fname_len) - myphar->ext;
  557. }
  558. }
  559. phar_request_initialize();
  560. if (NULL == (actual = zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), myphar->fname, fname_len, myphar))) {
  561. if (error) {
  562. spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\" to phar registry", fname);
  563. }
  564. php_stream_close(fp);
  565. phar_destroy_phar_data(myphar);
  566. return FAILURE;
  567. }
  568. myphar = actual;
  569. if (actual_alias) {
  570. phar_archive_data *fd_ptr;
  571. myphar->is_temporary_alias = 0;
  572. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), actual_alias, myphar->alias_len))) {
  573. if (SUCCESS != phar_free_alias(fd_ptr, actual_alias, myphar->alias_len)) {
  574. if (error) {
  575. spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\", alias is already in use", fname);
  576. }
  577. zend_hash_str_del(&(PHAR_G(phar_fname_map)), myphar->fname, fname_len);
  578. return FAILURE;
  579. }
  580. }
  581. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), actual_alias, myphar->alias_len, myphar);
  582. } else {
  583. phar_archive_data *fd_ptr;
  584. if (alias_len) {
  585. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
  586. if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
  587. if (error) {
  588. spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\", alias is already in use", fname);
  589. }
  590. zend_hash_str_del(&(PHAR_G(phar_fname_map)), myphar->fname, fname_len);
  591. return FAILURE;
  592. }
  593. }
  594. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, myphar);
  595. myphar->alias = pestrndup(alias, alias_len, myphar->is_persistent);
  596. myphar->alias_len = alias_len;
  597. } else {
  598. myphar->alias = pestrndup(myphar->fname, fname_len, myphar->is_persistent);
  599. myphar->alias_len = fname_len;
  600. }
  601. myphar->is_temporary_alias = 1;
  602. }
  603. if (pphar) {
  604. *pphar = myphar;
  605. }
  606. return SUCCESS;
  607. }
  608. /* }}} */
  609. struct _phar_pass_tar_info {
  610. php_stream *old;
  611. php_stream *new;
  612. int free_fp;
  613. int free_ufp;
  614. char **error;
  615. };
  616. static int phar_tar_writeheaders_int(phar_entry_info *entry, void *argument) /* {{{ */
  617. {
  618. tar_header header;
  619. size_t pos;
  620. struct _phar_pass_tar_info *fp = (struct _phar_pass_tar_info *)argument;
  621. char padding[512];
  622. if (entry->is_mounted) {
  623. return ZEND_HASH_APPLY_KEEP;
  624. }
  625. if (entry->is_deleted) {
  626. if (entry->fp_refcount <= 0) {
  627. return ZEND_HASH_APPLY_REMOVE;
  628. } else {
  629. /* we can't delete this in-memory until it is closed */
  630. return ZEND_HASH_APPLY_KEEP;
  631. }
  632. }
  633. phar_add_virtual_dirs(entry->phar, entry->filename, entry->filename_len);
  634. memset((char *) &header, 0, sizeof(header));
  635. if (entry->filename_len > 100) {
  636. char *boundary;
  637. if (entry->filename_len > 256) {
  638. if (fp->error) {
  639. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, filename \"%s\" is too long for tar file format", entry->phar->fname, entry->filename);
  640. }
  641. return ZEND_HASH_APPLY_STOP;
  642. }
  643. boundary = entry->filename + entry->filename_len - 101;
  644. while (*boundary && *boundary != '/') {
  645. ++boundary;
  646. }
  647. if (!*boundary || ((boundary - entry->filename) > 155)) {
  648. if (fp->error) {
  649. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, filename \"%s\" is too long for tar file format", entry->phar->fname, entry->filename);
  650. }
  651. return ZEND_HASH_APPLY_STOP;
  652. }
  653. memcpy(header.prefix, entry->filename, boundary - entry->filename);
  654. memcpy(header.name, boundary + 1, entry->filename_len - (boundary + 1 - entry->filename));
  655. } else {
  656. memcpy(header.name, entry->filename, entry->filename_len);
  657. }
  658. phar_tar_octal(header.mode, entry->flags & PHAR_ENT_PERM_MASK, sizeof(header.mode)-1);
  659. if (FAILURE == phar_tar_octal(header.size, entry->uncompressed_filesize, sizeof(header.size)-1)) {
  660. if (fp->error) {
  661. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, filename \"%s\" is too large for tar file format", entry->phar->fname, entry->filename);
  662. }
  663. return ZEND_HASH_APPLY_STOP;
  664. }
  665. if (FAILURE == phar_tar_octal(header.mtime, entry->timestamp, sizeof(header.mtime)-1)) {
  666. if (fp->error) {
  667. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, file modification time of file \"%s\" is too large for tar file format", entry->phar->fname, entry->filename);
  668. }
  669. return ZEND_HASH_APPLY_STOP;
  670. }
  671. /* calc checksum */
  672. header.typeflag = entry->tar_type;
  673. if (entry->link) {
  674. if (strlcpy(header.linkname, entry->link, sizeof(header.linkname)) >= sizeof(header.linkname)) {
  675. if (fp->error) {
  676. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, link \"%s\" is too long for format", entry->phar->fname, entry->link);
  677. }
  678. return ZEND_HASH_APPLY_STOP;
  679. }
  680. }
  681. memcpy(header.magic, "ustar", sizeof("ustar")-1);
  682. memcpy(header.version, "00", sizeof("00")-1);
  683. memcpy(header.checksum, " ", sizeof(" ")-1);
  684. entry->crc32 = phar_tar_checksum((char *)&header, sizeof(header));
  685. if (FAILURE == phar_tar_octal(header.checksum, entry->crc32, sizeof(header.checksum)-1)) {
  686. if (fp->error) {
  687. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, checksum of file \"%s\" is too large for tar file format", entry->phar->fname, entry->filename);
  688. }
  689. return ZEND_HASH_APPLY_STOP;
  690. }
  691. /* write header */
  692. entry->header_offset = php_stream_tell(fp->new);
  693. if (sizeof(header) != php_stream_write(fp->new, (char *) &header, sizeof(header))) {
  694. if (fp->error) {
  695. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, header for file \"%s\" could not be written", entry->phar->fname, entry->filename);
  696. }
  697. return ZEND_HASH_APPLY_STOP;
  698. }
  699. pos = php_stream_tell(fp->new); /* save start of file within tar */
  700. /* write contents */
  701. if (entry->uncompressed_filesize) {
  702. if (FAILURE == phar_open_entry_fp(entry, fp->error, 0)) {
  703. return ZEND_HASH_APPLY_STOP;
  704. }
  705. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
  706. if (fp->error) {
  707. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, contents of file \"%s\" could not be written, seek failed", entry->phar->fname, entry->filename);
  708. }
  709. return ZEND_HASH_APPLY_STOP;
  710. }
  711. if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), fp->new, entry->uncompressed_filesize, NULL)) {
  712. if (fp->error) {
  713. spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, contents of file \"%s\" could not be written", entry->phar->fname, entry->filename);
  714. }
  715. return ZEND_HASH_APPLY_STOP;
  716. }
  717. memset(padding, 0, 512);
  718. php_stream_write(fp->new, padding, ((entry->uncompressed_filesize +511)&~511) - entry->uncompressed_filesize);
  719. }
  720. if (!entry->is_modified && entry->fp_refcount) {
  721. /* open file pointers refer to this fp, do not free the stream */
  722. switch (entry->fp_type) {
  723. case PHAR_FP:
  724. fp->free_fp = 0;
  725. break;
  726. case PHAR_UFP:
  727. fp->free_ufp = 0;
  728. default:
  729. break;
  730. }
  731. }
  732. entry->is_modified = 0;
  733. if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp) {
  734. if (!entry->fp_refcount) {
  735. php_stream_close(entry->fp);
  736. }
  737. entry->fp = NULL;
  738. }
  739. entry->fp_type = PHAR_FP;
  740. /* note new location within tar */
  741. entry->offset = entry->offset_abs = pos;
  742. return ZEND_HASH_APPLY_KEEP;
  743. }
  744. /* }}} */
  745. static int phar_tar_writeheaders(zval *zv, void *argument) /* {{{ */
  746. {
  747. return phar_tar_writeheaders_int(Z_PTR_P(zv), argument);
  748. }
  749. /* }}} */
  750. int phar_tar_setmetadata(const phar_metadata_tracker *tracker, phar_entry_info *entry, char **error) /* {{{ */
  751. {
  752. /* Copy the metadata from tracker to the new entry being written out to temporary files */
  753. const zend_string *serialized_str;
  754. phar_metadata_tracker_copy(&entry->metadata_tracker, tracker, entry->is_persistent);
  755. phar_metadata_tracker_try_ensure_has_serialized_data(&entry->metadata_tracker, entry->is_persistent);
  756. serialized_str = entry->metadata_tracker.str;
  757. /* If there is no data, this will replace the metadata file (e.g. .phar/.metadata.bin) with an empty file */
  758. entry->uncompressed_filesize = entry->compressed_filesize = serialized_str ? ZSTR_LEN(serialized_str) : 0;
  759. if (entry->fp && entry->fp_type == PHAR_MOD) {
  760. php_stream_close(entry->fp);
  761. }
  762. entry->fp_type = PHAR_MOD;
  763. entry->is_modified = 1;
  764. entry->fp = php_stream_fopen_tmpfile();
  765. entry->offset = entry->offset_abs = 0;
  766. if (entry->fp == NULL) {
  767. spprintf(error, 0, "phar error: unable to create temporary file");
  768. return -1;
  769. }
  770. if (serialized_str && ZSTR_LEN(serialized_str) != php_stream_write(entry->fp, ZSTR_VAL(serialized_str), ZSTR_LEN(serialized_str))) {
  771. spprintf(error, 0, "phar tar error: unable to write metadata to magic metadata file \"%s\"", entry->filename);
  772. zend_hash_str_del(&(entry->phar->manifest), entry->filename, entry->filename_len);
  773. return ZEND_HASH_APPLY_STOP;
  774. }
  775. return ZEND_HASH_APPLY_KEEP;
  776. }
  777. /* }}} */
  778. static int phar_tar_setupmetadata(zval *zv, void *argument) /* {{{ */
  779. {
  780. int lookfor_len;
  781. struct _phar_pass_tar_info *i = (struct _phar_pass_tar_info *)argument;
  782. char *lookfor, **error = i->error;
  783. phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv), *metadata, newentry = {0};
  784. if (entry->filename_len >= sizeof(".phar/.metadata") && !memcmp(entry->filename, ".phar/.metadata", sizeof(".phar/.metadata")-1)) {
  785. if (entry->filename_len == sizeof(".phar/.metadata.bin")-1 && !memcmp(entry->filename, ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1)) {
  786. return phar_tar_setmetadata(&entry->phar->metadata_tracker, entry, error);
  787. }
  788. /* search for the file this metadata entry references */
  789. if (entry->filename_len >= sizeof(".phar/.metadata/") + sizeof("/.metadata.bin") - 1 && !zend_hash_str_exists(&(entry->phar->manifest), entry->filename + sizeof(".phar/.metadata/") - 1, entry->filename_len - (sizeof("/.metadata.bin") - 1 + sizeof(".phar/.metadata/") - 1))) {
  790. /* this is orphaned metadata, erase it */
  791. return ZEND_HASH_APPLY_REMOVE;
  792. }
  793. /* we can keep this entry, the file that refers to it exists */
  794. return ZEND_HASH_APPLY_KEEP;
  795. }
  796. if (!entry->is_modified) {
  797. return ZEND_HASH_APPLY_KEEP;
  798. }
  799. /* now we are dealing with regular files, so look for metadata */
  800. lookfor_len = spprintf(&lookfor, 0, ".phar/.metadata/%s/.metadata.bin", entry->filename);
  801. if (!phar_metadata_tracker_has_data(&entry->metadata_tracker, entry->is_persistent)) {
  802. zend_hash_str_del(&(entry->phar->manifest), lookfor, lookfor_len);
  803. efree(lookfor);
  804. return ZEND_HASH_APPLY_KEEP;
  805. }
  806. if (NULL != (metadata = zend_hash_str_find_ptr(&(entry->phar->manifest), lookfor, lookfor_len))) {
  807. int ret;
  808. ret = phar_tar_setmetadata(&entry->metadata_tracker, metadata, error);
  809. efree(lookfor);
  810. return ret;
  811. }
  812. newentry.filename = lookfor;
  813. newentry.filename_len = lookfor_len;
  814. newentry.phar = entry->phar;
  815. newentry.tar_type = TAR_FILE;
  816. newentry.is_tar = 1;
  817. if (NULL == (metadata = zend_hash_str_add_mem(&(entry->phar->manifest), lookfor, lookfor_len, (void *)&newentry, sizeof(phar_entry_info)))) {
  818. efree(lookfor);
  819. spprintf(error, 0, "phar tar error: unable to add magic metadata file to manifest for file \"%s\"", entry->filename);
  820. return ZEND_HASH_APPLY_STOP;
  821. }
  822. return phar_tar_setmetadata(&entry->metadata_tracker, metadata, error);
  823. }
  824. /* }}} */
  825. int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error) /* {{{ */
  826. {
  827. phar_entry_info entry = {0};
  828. static const char newstub[] = "<?php // tar-based phar archive stub file\n__HALT_COMPILER();";
  829. php_stream *oldfile, *newfile, *stubfile;
  830. int closeoldfile, free_user_stub;
  831. size_t signature_length;
  832. struct _phar_pass_tar_info pass;
  833. char *buf, *signature, *tmp, sigbuf[8];
  834. char halt_stub[] = "__HALT_COMPILER();";
  835. entry.flags = PHAR_ENT_PERM_DEF_FILE;
  836. entry.timestamp = time(NULL);
  837. entry.is_modified = 1;
  838. entry.is_crc_checked = 1;
  839. entry.is_tar = 1;
  840. entry.tar_type = '0';
  841. entry.phar = phar;
  842. entry.fp_type = PHAR_MOD;
  843. entry.fp = NULL;
  844. entry.filename = NULL;
  845. if (phar->is_persistent) {
  846. if (error) {
  847. spprintf(error, 0, "internal error: attempt to flush cached tar-based phar \"%s\"", phar->fname);
  848. }
  849. return EOF;
  850. }
  851. if (phar->is_data) {
  852. goto nostub;
  853. }
  854. /* set alias */
  855. if (!phar->is_temporary_alias && phar->alias_len) {
  856. entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1);
  857. entry.filename_len = sizeof(".phar/alias.txt")-1;
  858. entry.fp = php_stream_fopen_tmpfile();
  859. if (entry.fp == NULL) {
  860. efree(entry.filename);
  861. spprintf(error, 0, "phar error: unable to create temporary file");
  862. return -1;
  863. }
  864. if (phar->alias_len != php_stream_write(entry.fp, phar->alias, phar->alias_len)) {
  865. if (error) {
  866. spprintf(error, 0, "unable to set alias in tar-based phar \"%s\"", phar->fname);
  867. }
  868. php_stream_close(entry.fp);
  869. efree(entry.filename);
  870. return EOF;
  871. }
  872. entry.uncompressed_filesize = phar->alias_len;
  873. zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  874. /* At this point the entry is saved into the manifest. The manifest destroy
  875. routine will care about any resources to be freed. */
  876. } else {
  877. zend_hash_str_del(&phar->manifest, ".phar/alias.txt", sizeof(".phar/alias.txt")-1);
  878. }
  879. /* set stub */
  880. if (user_stub && !defaultstub) {
  881. char *pos;
  882. if (len < 0) {
  883. /* resource passed in */
  884. if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
  885. if (error) {
  886. spprintf(error, 0, "unable to access resource to copy stub to new tar-based phar \"%s\"", phar->fname);
  887. }
  888. return EOF;
  889. }
  890. if (len == -1) {
  891. len = PHP_STREAM_COPY_ALL;
  892. } else {
  893. len = -len;
  894. }
  895. user_stub = 0;
  896. // TODO: refactor to avoid reallocation ???
  897. //??? len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)
  898. {
  899. zend_string *str = php_stream_copy_to_mem(stubfile, len, 0);
  900. if (str) {
  901. len = ZSTR_LEN(str);
  902. user_stub = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  903. zend_string_release_ex(str, 0);
  904. } else {
  905. user_stub = NULL;
  906. len = 0;
  907. }
  908. }
  909. if (!len || !user_stub) {
  910. if (error) {
  911. spprintf(error, 0, "unable to read resource to copy stub to new tar-based phar \"%s\"", phar->fname);
  912. }
  913. return EOF;
  914. }
  915. free_user_stub = 1;
  916. } else {
  917. free_user_stub = 0;
  918. }
  919. tmp = estrndup(user_stub, len);
  920. if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
  921. efree(tmp);
  922. if (error) {
  923. spprintf(error, 0, "illegal stub for tar-based phar \"%s\"", phar->fname);
  924. }
  925. if (free_user_stub) {
  926. efree(user_stub);
  927. }
  928. return EOF;
  929. }
  930. pos = user_stub + (pos - tmp);
  931. efree(tmp);
  932. len = pos - user_stub + 18;
  933. entry.fp = php_stream_fopen_tmpfile();
  934. if (entry.fp == NULL) {
  935. spprintf(error, 0, "phar error: unable to create temporary file");
  936. return EOF;
  937. }
  938. entry.uncompressed_filesize = len + 5;
  939. if ((size_t)len != php_stream_write(entry.fp, user_stub, len)
  940. || 5 != php_stream_write(entry.fp, " ?>\r\n", 5)) {
  941. if (error) {
  942. spprintf(error, 0, "unable to create stub from string in new tar-based phar \"%s\"", phar->fname);
  943. }
  944. if (free_user_stub) {
  945. efree(user_stub);
  946. }
  947. php_stream_close(entry.fp);
  948. return EOF;
  949. }
  950. entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
  951. entry.filename_len = sizeof(".phar/stub.php")-1;
  952. zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  953. if (free_user_stub) {
  954. efree(user_stub);
  955. }
  956. } else {
  957. /* Either this is a brand new phar (add the stub), or the default stub is required (overwrite the stub) */
  958. entry.fp = php_stream_fopen_tmpfile();
  959. if (entry.fp == NULL) {
  960. spprintf(error, 0, "phar error: unable to create temporary file");
  961. return EOF;
  962. }
  963. if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
  964. php_stream_close(entry.fp);
  965. if (error) {
  966. spprintf(error, 0, "unable to %s stub in%star-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname);
  967. }
  968. return EOF;
  969. }
  970. entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
  971. entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
  972. entry.filename_len = sizeof(".phar/stub.php")-1;
  973. if (!defaultstub) {
  974. if (!zend_hash_str_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  975. if (NULL == zend_hash_str_add_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) {
  976. php_stream_close(entry.fp);
  977. efree(entry.filename);
  978. if (error) {
  979. spprintf(error, 0, "unable to create stub in tar-based phar \"%s\"", phar->fname);
  980. }
  981. return EOF;
  982. }
  983. } else {
  984. php_stream_close(entry.fp);
  985. efree(entry.filename);
  986. }
  987. } else {
  988. zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  989. }
  990. }
  991. nostub:
  992. if (phar->fp && !phar->is_brandnew) {
  993. oldfile = phar->fp;
  994. closeoldfile = 0;
  995. php_stream_rewind(oldfile);
  996. } else {
  997. oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
  998. closeoldfile = oldfile != NULL;
  999. }
  1000. newfile = php_stream_fopen_tmpfile();
  1001. if (!newfile) {
  1002. if (error) {
  1003. spprintf(error, 0, "unable to create temporary file");
  1004. }
  1005. if (closeoldfile) {
  1006. php_stream_close(oldfile);
  1007. }
  1008. return EOF;
  1009. }
  1010. pass.old = oldfile;
  1011. pass.new = newfile;
  1012. pass.error = error;
  1013. pass.free_fp = 1;
  1014. pass.free_ufp = 1;
  1015. if (phar_metadata_tracker_has_data(&phar->metadata_tracker, phar->is_persistent)) {
  1016. phar_entry_info *mentry;
  1017. if (NULL != (mentry = zend_hash_str_find_ptr(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1))) {
  1018. if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) {
  1019. if (closeoldfile) {
  1020. php_stream_close(oldfile);
  1021. }
  1022. return EOF;
  1023. }
  1024. } else {
  1025. phar_entry_info newentry = {0};
  1026. newentry.filename = estrndup(".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1);
  1027. newentry.filename_len = sizeof(".phar/.metadata.bin")-1;
  1028. newentry.phar = phar;
  1029. newentry.tar_type = TAR_FILE;
  1030. newentry.is_tar = 1;
  1031. if (NULL == (mentry = zend_hash_str_add_mem(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1, (void *)&newentry, sizeof(phar_entry_info)))) {
  1032. spprintf(error, 0, "phar tar error: unable to add magic metadata file to manifest for phar archive \"%s\"", phar->fname);
  1033. if (closeoldfile) {
  1034. php_stream_close(oldfile);
  1035. }
  1036. return EOF;
  1037. }
  1038. if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) {
  1039. zend_hash_str_del(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1);
  1040. if (closeoldfile) {
  1041. php_stream_close(oldfile);
  1042. }
  1043. return EOF;
  1044. }
  1045. }
  1046. }
  1047. zend_hash_apply_with_argument(&phar->manifest, phar_tar_setupmetadata, (void *) &pass);
  1048. if (error && *error) {
  1049. if (closeoldfile) {
  1050. php_stream_close(oldfile);
  1051. }
  1052. /* on error in the hash iterator above, error is set */
  1053. php_stream_close(newfile);
  1054. return EOF;
  1055. }
  1056. zend_hash_apply_with_argument(&phar->manifest, phar_tar_writeheaders, (void *) &pass);
  1057. /* add signature for executable tars or tars explicitly set with setSignatureAlgorithm */
  1058. if (!phar->is_data || phar->sig_flags) {
  1059. if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, error)) {
  1060. if (error) {
  1061. char *save = *error;
  1062. spprintf(error, 0, "phar error: unable to write signature to tar-based phar: %s", save);
  1063. efree(save);
  1064. }
  1065. if (closeoldfile) {
  1066. php_stream_close(oldfile);
  1067. }
  1068. php_stream_close(newfile);
  1069. return EOF;
  1070. }
  1071. entry.filename = ".phar/signature.bin";
  1072. entry.filename_len = sizeof(".phar/signature.bin")-1;
  1073. entry.fp = php_stream_fopen_tmpfile();
  1074. if (entry.fp == NULL) {
  1075. spprintf(error, 0, "phar error: unable to create temporary file");
  1076. return EOF;
  1077. }
  1078. #ifdef WORDS_BIGENDIAN
  1079. # define PHAR_SET_32(var, buffer) \
  1080. *(uint32_t *)(var) = (((((unsigned char*)&(buffer))[3]) << 24) \
  1081. | ((((unsigned char*)&(buffer))[2]) << 16) \
  1082. | ((((unsigned char*)&(buffer))[1]) << 8) \
  1083. | (((unsigned char*)&(buffer))[0]))
  1084. #else
  1085. # define PHAR_SET_32(var, buffer) *(uint32_t *)(var) = (uint32_t) (buffer)
  1086. #endif
  1087. PHAR_SET_32(sigbuf, phar->sig_flags);
  1088. PHAR_SET_32(sigbuf + 4, signature_length);
  1089. if (8 != php_stream_write(entry.fp, sigbuf, 8) || signature_length != php_stream_write(entry.fp, signature, signature_length)) {
  1090. efree(signature);
  1091. if (error) {
  1092. spprintf(error, 0, "phar error: unable to write signature to tar-based phar %s", phar->fname);
  1093. }
  1094. if (closeoldfile) {
  1095. php_stream_close(oldfile);
  1096. }
  1097. php_stream_close(newfile);
  1098. return EOF;
  1099. }
  1100. efree(signature);
  1101. entry.uncompressed_filesize = entry.compressed_filesize = signature_length + 8;
  1102. /* throw out return value and write the signature */
  1103. entry.filename_len = phar_tar_writeheaders_int(&entry, (void *)&pass);
  1104. if (error && *error) {
  1105. if (closeoldfile) {
  1106. php_stream_close(oldfile);
  1107. }
  1108. /* error is set by writeheaders */
  1109. php_stream_close(newfile);
  1110. return EOF;
  1111. }
  1112. } /* signature */
  1113. /* add final zero blocks */
  1114. buf = (char *) ecalloc(1024, 1);
  1115. php_stream_write(newfile, buf, 1024);
  1116. efree(buf);
  1117. if (closeoldfile) {
  1118. php_stream_close(oldfile);
  1119. }
  1120. /* on error in the hash iterator above, error is set */
  1121. if (error && *error) {
  1122. php_stream_close(newfile);
  1123. return EOF;
  1124. }
  1125. if (phar->fp && pass.free_fp) {
  1126. php_stream_close(phar->fp);
  1127. }
  1128. if (phar->ufp) {
  1129. if (pass.free_ufp) {
  1130. php_stream_close(phar->ufp);
  1131. }
  1132. phar->ufp = NULL;
  1133. }
  1134. phar->is_brandnew = 0;
  1135. php_stream_rewind(newfile);
  1136. if (phar->donotflush) {
  1137. /* deferred flush */
  1138. phar->fp = newfile;
  1139. } else {
  1140. phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
  1141. if (!phar->fp) {
  1142. phar->fp = newfile;
  1143. if (error) {
  1144. spprintf(error, 0, "unable to open new phar \"%s\" for writing", phar->fname);
  1145. }
  1146. return EOF;
  1147. }
  1148. if (phar->flags & PHAR_FILE_COMPRESSED_GZ) {
  1149. php_stream_filter *filter;
  1150. /* to properly compress, we have to tell zlib to add a zlib header */
  1151. zval filterparams;
  1152. array_init(&filterparams);
  1153. /* this is defined in zlib's zconf.h */
  1154. #ifndef MAX_WBITS
  1155. #define MAX_WBITS 15
  1156. #endif
  1157. add_assoc_long(&filterparams, "window", MAX_WBITS + 16);
  1158. filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp));
  1159. zend_array_destroy(Z_ARR(filterparams));
  1160. if (!filter) {
  1161. /* copy contents uncompressed rather than lose them */
  1162. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  1163. php_stream_close(newfile);
  1164. if (error) {
  1165. spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname);
  1166. }
  1167. return EOF;
  1168. }
  1169. php_stream_filter_append(&phar->fp->writefilters, filter);
  1170. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  1171. php_stream_filter_flush(filter, 1);
  1172. php_stream_filter_remove(filter, 1);
  1173. php_stream_close(phar->fp);
  1174. /* use the temp stream as our base */
  1175. phar->fp = newfile;
  1176. } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) {
  1177. php_stream_filter *filter;
  1178. filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp));
  1179. php_stream_filter_append(&phar->fp->writefilters, filter);
  1180. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  1181. php_stream_filter_flush(filter, 1);
  1182. php_stream_filter_remove(filter, 1);
  1183. php_stream_close(phar->fp);
  1184. /* use the temp stream as our base */
  1185. phar->fp = newfile;
  1186. } else {
  1187. php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  1188. /* we could also reopen the file in "rb" mode but there is no need for that */
  1189. php_stream_close(newfile);
  1190. }
  1191. }
  1192. return EOF;
  1193. }
  1194. /* }}} */