tar.c 43 KB

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