util.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar php single-file executable PHP extension |
  4. | utility functions |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.01 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | https://www.php.net/license/3_01.txt |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Authors: Gregory Beaver <cellog@php.net> |
  17. | Marcus Boerger <helly@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "phar_internal.h"
  21. #include "ext/hash/php_hash_sha.h"
  22. #ifdef PHAR_HAVE_OPENSSL
  23. /* OpenSSL includes */
  24. #include <openssl/evp.h>
  25. #include <openssl/x509.h>
  26. #include <openssl/x509v3.h>
  27. #include <openssl/crypto.h>
  28. #include <openssl/pem.h>
  29. #include <openssl/err.h>
  30. #include <openssl/conf.h>
  31. #include <openssl/rand.h>
  32. #include <openssl/ssl.h>
  33. #include <openssl/pkcs12.h>
  34. #else
  35. static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t end, char *key, size_t key_len, char **signature, size_t *signature_len, php_uint32 sig_type);
  36. #endif
  37. /* for links to relative location, prepend cwd of the entry */
  38. static char *phar_get_link_location(phar_entry_info *entry) /* {{{ */
  39. {
  40. char *p, *ret = NULL;
  41. if (!entry->link) {
  42. return NULL;
  43. }
  44. if (entry->link[0] == '/') {
  45. return estrdup(entry->link + 1);
  46. }
  47. p = strrchr(entry->filename, '/');
  48. if (p) {
  49. *p = '\0';
  50. spprintf(&ret, 0, "%s/%s", entry->filename, entry->link);
  51. return ret;
  52. }
  53. return entry->link;
  54. }
  55. /* }}} */
  56. phar_entry_info *phar_get_link_source(phar_entry_info *entry) /* {{{ */
  57. {
  58. phar_entry_info *link_entry;
  59. char *link;
  60. if (!entry->link) {
  61. return entry;
  62. }
  63. link = phar_get_link_location(entry);
  64. if (NULL != (link_entry = zend_hash_str_find_ptr(&(entry->phar->manifest), entry->link, strlen(entry->link))) ||
  65. NULL != (link_entry = zend_hash_str_find_ptr(&(entry->phar->manifest), link, strlen(link)))) {
  66. if (link != entry->link) {
  67. efree(link);
  68. }
  69. return phar_get_link_source(link_entry);
  70. } else {
  71. if (link != entry->link) {
  72. efree(link);
  73. }
  74. return NULL;
  75. }
  76. }
  77. /* }}} */
  78. /* retrieve a phar_entry_info's current file pointer for reading contents */
  79. php_stream *phar_get_efp(phar_entry_info *entry, int follow_links) /* {{{ */
  80. {
  81. if (follow_links && entry->link) {
  82. phar_entry_info *link_entry = phar_get_link_source(entry);
  83. if (link_entry && link_entry != entry) {
  84. return phar_get_efp(link_entry, 1);
  85. }
  86. }
  87. if (phar_get_fp_type(entry) == PHAR_FP) {
  88. if (!phar_get_entrypfp(entry)) {
  89. /* re-open just in time for cases where our refcount reached 0 on the phar archive */
  90. phar_open_archive_fp(entry->phar);
  91. }
  92. return phar_get_entrypfp(entry);
  93. } else if (phar_get_fp_type(entry) == PHAR_UFP) {
  94. return phar_get_entrypufp(entry);
  95. } else if (entry->fp_type == PHAR_MOD) {
  96. return entry->fp;
  97. } else {
  98. /* temporary manifest entry */
  99. if (!entry->fp) {
  100. entry->fp = php_stream_open_wrapper(entry->tmp, "rb", STREAM_MUST_SEEK|0, NULL);
  101. }
  102. return entry->fp;
  103. }
  104. }
  105. /* }}} */
  106. int phar_seek_efp(phar_entry_info *entry, zend_off_t offset, int whence, zend_off_t position, int follow_links) /* {{{ */
  107. {
  108. php_stream *fp = phar_get_efp(entry, follow_links);
  109. zend_off_t temp, eoffset;
  110. if (!fp) {
  111. return -1;
  112. }
  113. if (follow_links) {
  114. phar_entry_info *t;
  115. t = phar_get_link_source(entry);
  116. if (t) {
  117. entry = t;
  118. }
  119. }
  120. if (entry->is_dir) {
  121. return 0;
  122. }
  123. eoffset = phar_get_fp_offset(entry);
  124. switch (whence) {
  125. case SEEK_END:
  126. temp = eoffset + entry->uncompressed_filesize + offset;
  127. break;
  128. case SEEK_CUR:
  129. temp = eoffset + position + offset;
  130. break;
  131. case SEEK_SET:
  132. temp = eoffset + offset;
  133. break;
  134. default:
  135. temp = 0;
  136. }
  137. if (temp > eoffset + (zend_off_t) entry->uncompressed_filesize) {
  138. return -1;
  139. }
  140. if (temp < eoffset) {
  141. return -1;
  142. }
  143. return php_stream_seek(fp, temp, SEEK_SET);
  144. }
  145. /* }}} */
  146. /* mount an absolute path or uri to a path internal to the phar archive */
  147. int phar_mount_entry(phar_archive_data *phar, char *filename, size_t filename_len, char *path, size_t path_len) /* {{{ */
  148. {
  149. phar_entry_info entry = {0};
  150. php_stream_statbuf ssb;
  151. int is_phar;
  152. const char *err;
  153. if (phar_path_check(&path, &path_len, &err) > pcr_is_ok) {
  154. return FAILURE;
  155. }
  156. if (path_len >= sizeof(".phar")-1 && !memcmp(path, ".phar", sizeof(".phar")-1)) {
  157. /* no creating magic phar files by mounting them */
  158. return FAILURE;
  159. }
  160. is_phar = (filename_len > 7 && !memcmp(filename, "phar://", 7));
  161. entry.phar = phar;
  162. entry.filename = estrndup(path, path_len);
  163. #ifdef PHP_WIN32
  164. phar_unixify_path_separators(entry.filename, path_len);
  165. #endif
  166. entry.filename_len = path_len;
  167. if (is_phar) {
  168. entry.tmp = estrndup(filename, filename_len);
  169. } else {
  170. entry.tmp = expand_filepath(filename, NULL);
  171. if (!entry.tmp) {
  172. entry.tmp = estrndup(filename, filename_len);
  173. }
  174. }
  175. filename = entry.tmp;
  176. /* only check openbasedir for files, not for phar streams */
  177. if (!is_phar && php_check_open_basedir(filename)) {
  178. efree(entry.tmp);
  179. efree(entry.filename);
  180. return FAILURE;
  181. }
  182. entry.is_mounted = 1;
  183. entry.is_crc_checked = 1;
  184. entry.fp_type = PHAR_TMP;
  185. if (SUCCESS != php_stream_stat_path(filename, &ssb)) {
  186. efree(entry.tmp);
  187. efree(entry.filename);
  188. return FAILURE;
  189. }
  190. if (ssb.sb.st_mode & S_IFDIR) {
  191. entry.is_dir = 1;
  192. if (NULL == zend_hash_str_add_ptr(&phar->mounted_dirs, entry.filename, path_len, entry.filename)) {
  193. /* directory already mounted */
  194. efree(entry.tmp);
  195. efree(entry.filename);
  196. return FAILURE;
  197. }
  198. } else {
  199. entry.is_dir = 0;
  200. entry.uncompressed_filesize = entry.compressed_filesize = ssb.sb.st_size;
  201. }
  202. entry.flags = ssb.sb.st_mode;
  203. if (NULL != zend_hash_str_add_mem(&phar->manifest, entry.filename, path_len, (void*)&entry, sizeof(phar_entry_info))) {
  204. return SUCCESS;
  205. }
  206. efree(entry.tmp);
  207. efree(entry.filename);
  208. return FAILURE;
  209. }
  210. /* }}} */
  211. zend_string *phar_find_in_include_path(char *filename, size_t filename_len, phar_archive_data **pphar) /* {{{ */
  212. {
  213. zend_string *ret;
  214. char *path, *fname, *arch, *entry, *test;
  215. size_t arch_len, entry_len, fname_len;
  216. phar_archive_data *phar;
  217. if (pphar) {
  218. *pphar = NULL;
  219. } else {
  220. pphar = &phar;
  221. }
  222. if (!zend_is_executing() || !PHAR_G(cwd)) {
  223. return NULL;
  224. }
  225. fname = (char*)zend_get_executed_filename();
  226. fname_len = strlen(fname);
  227. if (PHAR_G(last_phar) && !memcmp(fname, "phar://", 7) && fname_len - 7 >= PHAR_G(last_phar_name_len) && !memcmp(fname + 7, PHAR_G(last_phar_name), PHAR_G(last_phar_name_len))) {
  228. arch = estrndup(PHAR_G(last_phar_name), PHAR_G(last_phar_name_len));
  229. arch_len = PHAR_G(last_phar_name_len);
  230. phar = PHAR_G(last_phar);
  231. goto splitted;
  232. }
  233. if (fname_len < 7 || memcmp(fname, "phar://", 7) || SUCCESS != phar_split_fname(fname, strlen(fname), &arch, &arch_len, &entry, &entry_len, 1, 0)) {
  234. return NULL;
  235. }
  236. efree(entry);
  237. if (*filename == '.') {
  238. size_t try_len;
  239. if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
  240. efree(arch);
  241. return NULL;
  242. }
  243. splitted:
  244. if (pphar) {
  245. *pphar = phar;
  246. }
  247. try_len = filename_len;
  248. test = phar_fix_filepath(estrndup(filename, filename_len), &try_len, 1);
  249. if (*test == '/') {
  250. if (zend_hash_str_exists(&(phar->manifest), test + 1, try_len - 1)) {
  251. ret = strpprintf(0, "phar://%s%s", arch, test);
  252. efree(arch);
  253. efree(test);
  254. return ret;
  255. }
  256. } else {
  257. if (zend_hash_str_exists(&(phar->manifest), test, try_len)) {
  258. ret = strpprintf(0, "phar://%s/%s", arch, test);
  259. efree(arch);
  260. efree(test);
  261. return ret;
  262. }
  263. }
  264. efree(test);
  265. }
  266. spprintf(&path, MAXPATHLEN + 1 + strlen(PG(include_path)), "phar://%s/%s%c%s", arch, PHAR_G(cwd), DEFAULT_DIR_SEPARATOR, PG(include_path));
  267. efree(arch);
  268. ret = php_resolve_path(filename, filename_len, path);
  269. efree(path);
  270. if (ret && ZSTR_LEN(ret) > 8 && !strncmp(ZSTR_VAL(ret), "phar://", 7)) {
  271. /* found phar:// */
  272. if (SUCCESS != phar_split_fname(ZSTR_VAL(ret), ZSTR_LEN(ret), &arch, &arch_len, &entry, &entry_len, 1, 0)) {
  273. return ret;
  274. }
  275. *pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), arch, arch_len);
  276. if (!*pphar && PHAR_G(manifest_cached)) {
  277. *pphar = zend_hash_str_find_ptr(&cached_phars, arch, arch_len);
  278. }
  279. efree(arch);
  280. efree(entry);
  281. }
  282. return ret;
  283. }
  284. /* }}} */
  285. /**
  286. * Retrieve a copy of the file information on a single file within a phar, or null.
  287. * This also transfers the open file pointer, if any, to the entry.
  288. *
  289. * If the file does not already exist, this will fail. Pre-existing files can be
  290. * appended, truncated, or read. For read, if the entry is marked unmodified, it is
  291. * assumed that the file pointer, if present, is opened for reading
  292. */
  293. int phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security) /* {{{ */
  294. {
  295. phar_archive_data *phar;
  296. phar_entry_info *entry;
  297. int for_write = mode[0] != 'r' || mode[1] == '+';
  298. int for_append = mode[0] == 'a';
  299. int for_create = mode[0] != 'r';
  300. int for_trunc = mode[0] == 'w';
  301. if (!ret) {
  302. return FAILURE;
  303. }
  304. *ret = NULL;
  305. if (error) {
  306. *error = NULL;
  307. }
  308. if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, error)) {
  309. return FAILURE;
  310. }
  311. if (for_write && PHAR_G(readonly) && !phar->is_data) {
  312. if (error) {
  313. spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, disabled by ini setting", path, fname);
  314. }
  315. return FAILURE;
  316. }
  317. if (!path_len) {
  318. if (error) {
  319. spprintf(error, 4096, "phar error: file \"\" in phar \"%s\" cannot be empty", fname);
  320. }
  321. return FAILURE;
  322. }
  323. really_get_entry:
  324. if (allow_dir) {
  325. if ((entry = phar_get_entry_info_dir(phar, path, path_len, allow_dir, for_create && !PHAR_G(readonly) && !phar->is_data ? NULL : error, security)) == NULL) {
  326. if (for_create && (!PHAR_G(readonly) || phar->is_data)) {
  327. return SUCCESS;
  328. }
  329. return FAILURE;
  330. }
  331. } else {
  332. if ((entry = phar_get_entry_info(phar, path, path_len, for_create && !PHAR_G(readonly) && !phar->is_data ? NULL : error, security)) == NULL) {
  333. if (for_create && (!PHAR_G(readonly) || phar->is_data)) {
  334. return SUCCESS;
  335. }
  336. return FAILURE;
  337. }
  338. }
  339. if (for_write && phar->is_persistent) {
  340. if (FAILURE == phar_copy_on_write(&phar)) {
  341. if (error) {
  342. spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, could not make cached phar writeable", path, fname);
  343. }
  344. return FAILURE;
  345. } else {
  346. goto really_get_entry;
  347. }
  348. }
  349. if (entry->is_modified && !for_write) {
  350. if (error) {
  351. spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for reading, writable file pointers are open", path, fname);
  352. }
  353. return FAILURE;
  354. }
  355. if (entry->fp_refcount && for_write) {
  356. if (error) {
  357. spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, readable file pointers are open", path, fname);
  358. }
  359. return FAILURE;
  360. }
  361. if (entry->is_deleted) {
  362. if (!for_create) {
  363. return FAILURE;
  364. }
  365. entry->is_deleted = 0;
  366. }
  367. if (entry->is_dir) {
  368. *ret = (phar_entry_data *) emalloc(sizeof(phar_entry_data));
  369. (*ret)->position = 0;
  370. (*ret)->fp = NULL;
  371. (*ret)->phar = phar;
  372. (*ret)->for_write = for_write;
  373. (*ret)->internal_file = entry;
  374. (*ret)->is_zip = entry->is_zip;
  375. (*ret)->is_tar = entry->is_tar;
  376. if (!phar->is_persistent) {
  377. ++(entry->phar->refcount);
  378. ++(entry->fp_refcount);
  379. }
  380. return SUCCESS;
  381. }
  382. if (entry->fp_type == PHAR_MOD) {
  383. if (for_trunc) {
  384. if (FAILURE == phar_create_writeable_entry(phar, entry, error)) {
  385. return FAILURE;
  386. }
  387. } else if (for_append) {
  388. phar_seek_efp(entry, 0, SEEK_END, 0, 0);
  389. }
  390. } else {
  391. if (for_write) {
  392. if (entry->link) {
  393. efree(entry->link);
  394. entry->link = NULL;
  395. entry->tar_type = (entry->is_tar ? TAR_FILE : '\0');
  396. }
  397. if (for_trunc) {
  398. if (FAILURE == phar_create_writeable_entry(phar, entry, error)) {
  399. return FAILURE;
  400. }
  401. } else {
  402. if (FAILURE == phar_separate_entry_fp(entry, error)) {
  403. return FAILURE;
  404. }
  405. }
  406. } else {
  407. if (FAILURE == phar_open_entry_fp(entry, error, 1)) {
  408. return FAILURE;
  409. }
  410. }
  411. }
  412. *ret = (phar_entry_data *) emalloc(sizeof(phar_entry_data));
  413. (*ret)->position = 0;
  414. (*ret)->phar = phar;
  415. (*ret)->for_write = for_write;
  416. (*ret)->internal_file = entry;
  417. (*ret)->is_zip = entry->is_zip;
  418. (*ret)->is_tar = entry->is_tar;
  419. (*ret)->fp = phar_get_efp(entry, 1);
  420. if (entry->link) {
  421. phar_entry_info *link = phar_get_link_source(entry);
  422. if(!link) {
  423. efree(*ret);
  424. return FAILURE;
  425. }
  426. (*ret)->zero = phar_get_fp_offset(link);
  427. } else {
  428. (*ret)->zero = phar_get_fp_offset(entry);
  429. }
  430. if (!phar->is_persistent) {
  431. ++(entry->fp_refcount);
  432. ++(entry->phar->refcount);
  433. }
  434. return SUCCESS;
  435. }
  436. /* }}} */
  437. /**
  438. * Create a new dummy file slot within a writeable phar for a newly created file
  439. */
  440. phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security) /* {{{ */
  441. {
  442. phar_archive_data *phar;
  443. phar_entry_info *entry, etemp;
  444. phar_entry_data *ret;
  445. const char *pcr_error;
  446. char is_dir;
  447. #ifdef PHP_WIN32
  448. phar_unixify_path_separators(path, path_len);
  449. #endif
  450. is_dir = (path_len && path[path_len - 1] == '/') ? 1 : 0;
  451. if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, error)) {
  452. return NULL;
  453. }
  454. if (FAILURE == phar_get_entry_data(&ret, fname, fname_len, path, path_len, mode, allow_dir, error, security)) {
  455. return NULL;
  456. } else if (ret) {
  457. return ret;
  458. }
  459. if (phar_path_check(&path, &path_len, &pcr_error) > pcr_is_ok) {
  460. if (error) {
  461. spprintf(error, 0, "phar error: invalid path \"%s\" contains %s", path, pcr_error);
  462. }
  463. return NULL;
  464. }
  465. if (phar->is_persistent && FAILURE == phar_copy_on_write(&phar)) {
  466. if (error) {
  467. spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be created, could not make cached phar writeable", path, fname);
  468. }
  469. return NULL;
  470. }
  471. /* create a new phar data holder */
  472. ret = (phar_entry_data *) emalloc(sizeof(phar_entry_data));
  473. /* create an entry, this is a new file */
  474. memset(&etemp, 0, sizeof(phar_entry_info));
  475. etemp.filename_len = path_len;
  476. etemp.fp_type = PHAR_MOD;
  477. etemp.fp = php_stream_fopen_tmpfile();
  478. if (!etemp.fp) {
  479. if (error) {
  480. spprintf(error, 0, "phar error: unable to create temporary file");
  481. }
  482. efree(ret);
  483. return NULL;
  484. }
  485. etemp.fp_refcount = 1;
  486. if (allow_dir == 2) {
  487. etemp.is_dir = 1;
  488. etemp.flags = etemp.old_flags = PHAR_ENT_PERM_DEF_DIR;
  489. } else {
  490. etemp.flags = etemp.old_flags = PHAR_ENT_PERM_DEF_FILE;
  491. }
  492. if (is_dir && path_len) {
  493. etemp.filename_len--; /* strip trailing / */
  494. path_len--;
  495. }
  496. phar_add_virtual_dirs(phar, path, path_len);
  497. etemp.is_modified = 1;
  498. etemp.timestamp = time(0);
  499. etemp.is_crc_checked = 1;
  500. etemp.phar = phar;
  501. etemp.filename = estrndup(path, path_len);
  502. etemp.is_zip = phar->is_zip;
  503. if (phar->is_tar) {
  504. etemp.is_tar = phar->is_tar;
  505. etemp.tar_type = etemp.is_dir ? TAR_DIR : TAR_FILE;
  506. }
  507. if (NULL == (entry = zend_hash_str_add_mem(&phar->manifest, etemp.filename, path_len, (void*)&etemp, sizeof(phar_entry_info)))) {
  508. php_stream_close(etemp.fp);
  509. if (error) {
  510. spprintf(error, 0, "phar error: unable to add new entry \"%s\" to phar \"%s\"", etemp.filename, phar->fname);
  511. }
  512. efree(ret);
  513. efree(etemp.filename);
  514. return NULL;
  515. }
  516. if (!entry) {
  517. php_stream_close(etemp.fp);
  518. efree(etemp.filename);
  519. efree(ret);
  520. return NULL;
  521. }
  522. ++(phar->refcount);
  523. ret->phar = phar;
  524. ret->fp = entry->fp;
  525. ret->position = ret->zero = 0;
  526. ret->for_write = 1;
  527. ret->is_zip = entry->is_zip;
  528. ret->is_tar = entry->is_tar;
  529. ret->internal_file = entry;
  530. return ret;
  531. }
  532. /* }}} */
  533. /* initialize a phar_archive_data's read-only fp for existing phar data */
  534. int phar_open_archive_fp(phar_archive_data *phar) /* {{{ */
  535. {
  536. if (phar_get_pharfp(phar)) {
  537. return SUCCESS;
  538. }
  539. if (php_check_open_basedir(phar->fname)) {
  540. return FAILURE;
  541. }
  542. phar_set_pharfp(phar, php_stream_open_wrapper(phar->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, NULL));
  543. if (!phar_get_pharfp(phar)) {
  544. return FAILURE;
  545. }
  546. return SUCCESS;
  547. }
  548. /* }}} */
  549. /* copy file data from an existing to a new phar_entry_info that is not in the manifest */
  550. int phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error) /* {{{ */
  551. {
  552. phar_entry_info *link;
  553. if (FAILURE == phar_open_entry_fp(source, error, 1)) {
  554. return FAILURE;
  555. }
  556. if (dest->link) {
  557. efree(dest->link);
  558. dest->link = NULL;
  559. dest->tar_type = (dest->is_tar ? TAR_FILE : '\0');
  560. }
  561. dest->fp_type = PHAR_MOD;
  562. dest->offset = 0;
  563. dest->is_modified = 1;
  564. dest->fp = php_stream_fopen_tmpfile();
  565. if (dest->fp == NULL) {
  566. spprintf(error, 0, "phar error: unable to create temporary file");
  567. return EOF;
  568. }
  569. phar_seek_efp(source, 0, SEEK_SET, 0, 1);
  570. link = phar_get_link_source(source);
  571. if (!link) {
  572. link = source;
  573. }
  574. if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), dest->fp, link->uncompressed_filesize, NULL)) {
  575. php_stream_close(dest->fp);
  576. dest->fp_type = PHAR_FP;
  577. if (error) {
  578. spprintf(error, 4096, "phar error: unable to copy contents of file \"%s\" to \"%s\" in phar archive \"%s\"", source->filename, dest->filename, source->phar->fname);
  579. }
  580. return FAILURE;
  581. }
  582. return SUCCESS;
  583. }
  584. /* }}} */
  585. /* open and decompress a compressed phar entry
  586. */
  587. int phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links) /* {{{ */
  588. {
  589. php_stream_filter *filter;
  590. phar_archive_data *phar = entry->phar;
  591. char *filtername;
  592. zend_off_t loc;
  593. php_stream *ufp;
  594. phar_entry_data dummy;
  595. if (follow_links && entry->link) {
  596. phar_entry_info *link_entry = phar_get_link_source(entry);
  597. if (link_entry && link_entry != entry) {
  598. return phar_open_entry_fp(link_entry, error, 1);
  599. }
  600. }
  601. if (entry->is_modified) {
  602. return SUCCESS;
  603. }
  604. if (entry->fp_type == PHAR_TMP) {
  605. if (!entry->fp) {
  606. entry->fp = php_stream_open_wrapper(entry->tmp, "rb", STREAM_MUST_SEEK|0, NULL);
  607. }
  608. return SUCCESS;
  609. }
  610. if (entry->fp_type != PHAR_FP) {
  611. /* either newly created or already modified */
  612. return SUCCESS;
  613. }
  614. if (!phar_get_pharfp(phar)) {
  615. if (FAILURE == phar_open_archive_fp(phar)) {
  616. spprintf(error, 4096, "phar error: Cannot open phar archive \"%s\" for reading", phar->fname);
  617. return FAILURE;
  618. }
  619. }
  620. if ((entry->old_flags && !(entry->old_flags & PHAR_ENT_COMPRESSION_MASK)) || !(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
  621. dummy.internal_file = entry;
  622. dummy.phar = phar;
  623. dummy.zero = entry->offset;
  624. dummy.fp = phar_get_pharfp(phar);
  625. if (FAILURE == phar_postprocess_file(&dummy, entry->crc32, error, 1)) {
  626. return FAILURE;
  627. }
  628. return SUCCESS;
  629. }
  630. if (!phar_get_entrypufp(entry)) {
  631. phar_set_entrypufp(entry, php_stream_fopen_tmpfile());
  632. if (!phar_get_entrypufp(entry)) {
  633. spprintf(error, 4096, "phar error: Cannot open temporary file for decompressing phar archive \"%s\" file \"%s\"", phar->fname, entry->filename);
  634. return FAILURE;
  635. }
  636. }
  637. dummy.internal_file = entry;
  638. dummy.phar = phar;
  639. dummy.zero = entry->offset;
  640. dummy.fp = phar_get_pharfp(phar);
  641. if (FAILURE == phar_postprocess_file(&dummy, entry->crc32, error, 1)) {
  642. return FAILURE;
  643. }
  644. ufp = phar_get_entrypufp(entry);
  645. if ((filtername = phar_decompress_filter(entry, 0)) != NULL) {
  646. filter = php_stream_filter_create(filtername, NULL, 0);
  647. } else {
  648. filter = NULL;
  649. }
  650. if (!filter) {
  651. spprintf(error, 4096, "phar error: unable to read phar \"%s\" (cannot create %s filter while decompressing file \"%s\")", phar->fname, phar_decompress_filter(entry, 1), entry->filename);
  652. return FAILURE;
  653. }
  654. /* now we can safely use proper decompression */
  655. /* save the new offset location within ufp */
  656. php_stream_seek(ufp, 0, SEEK_END);
  657. loc = php_stream_tell(ufp);
  658. php_stream_filter_append(&ufp->writefilters, filter);
  659. php_stream_seek(phar_get_entrypfp(entry), phar_get_fp_offset(entry), SEEK_SET);
  660. if (entry->uncompressed_filesize) {
  661. if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_entrypfp(entry), ufp, entry->compressed_filesize, NULL)) {
  662. spprintf(error, 4096, "phar error: internal corruption of phar \"%s\" (actual filesize mismatch on file \"%s\")", phar->fname, entry->filename);
  663. php_stream_filter_remove(filter, 1);
  664. return FAILURE;
  665. }
  666. }
  667. php_stream_filter_flush(filter, 1);
  668. php_stream_flush(ufp);
  669. php_stream_filter_remove(filter, 1);
  670. if (php_stream_tell(ufp) - loc != (zend_off_t) entry->uncompressed_filesize) {
  671. spprintf(error, 4096, "phar error: internal corruption of phar \"%s\" (actual filesize mismatch on file \"%s\")", phar->fname, entry->filename);
  672. return FAILURE;
  673. }
  674. entry->old_flags = entry->flags;
  675. /* this is now the new location of the file contents within this fp */
  676. phar_set_fp_type(entry, PHAR_UFP, loc);
  677. dummy.zero = entry->offset;
  678. dummy.fp = ufp;
  679. if (FAILURE == phar_postprocess_file(&dummy, entry->crc32, error, 0)) {
  680. return FAILURE;
  681. }
  682. return SUCCESS;
  683. }
  684. /* }}} */
  685. int phar_create_writeable_entry(phar_archive_data *phar, phar_entry_info *entry, char **error) /* {{{ */
  686. {
  687. if (entry->fp_type == PHAR_MOD) {
  688. /* already newly created, truncate */
  689. php_stream_truncate_set_size(entry->fp, 0);
  690. entry->old_flags = entry->flags;
  691. entry->is_modified = 1;
  692. phar->is_modified = 1;
  693. /* reset file size */
  694. entry->uncompressed_filesize = 0;
  695. entry->compressed_filesize = 0;
  696. entry->crc32 = 0;
  697. entry->flags = PHAR_ENT_PERM_DEF_FILE;
  698. entry->fp_type = PHAR_MOD;
  699. entry->offset = 0;
  700. return SUCCESS;
  701. }
  702. if (error) {
  703. *error = NULL;
  704. }
  705. /* open a new temp file for writing */
  706. if (entry->link) {
  707. efree(entry->link);
  708. entry->link = NULL;
  709. entry->tar_type = (entry->is_tar ? TAR_FILE : '\0');
  710. }
  711. entry->fp = php_stream_fopen_tmpfile();
  712. if (!entry->fp) {
  713. if (error) {
  714. spprintf(error, 0, "phar error: unable to create temporary file");
  715. }
  716. return FAILURE;
  717. }
  718. entry->old_flags = entry->flags;
  719. entry->is_modified = 1;
  720. phar->is_modified = 1;
  721. /* reset file size */
  722. entry->uncompressed_filesize = 0;
  723. entry->compressed_filesize = 0;
  724. entry->crc32 = 0;
  725. entry->flags = PHAR_ENT_PERM_DEF_FILE;
  726. entry->fp_type = PHAR_MOD;
  727. entry->offset = 0;
  728. return SUCCESS;
  729. }
  730. /* }}} */
  731. int phar_separate_entry_fp(phar_entry_info *entry, char **error) /* {{{ */
  732. {
  733. php_stream *fp;
  734. phar_entry_info *link;
  735. if (FAILURE == phar_open_entry_fp(entry, error, 1)) {
  736. return FAILURE;
  737. }
  738. if (entry->fp_type == PHAR_MOD) {
  739. return SUCCESS;
  740. }
  741. fp = php_stream_fopen_tmpfile();
  742. if (fp == NULL) {
  743. spprintf(error, 0, "phar error: unable to create temporary file");
  744. return FAILURE;
  745. }
  746. phar_seek_efp(entry, 0, SEEK_SET, 0, 1);
  747. link = phar_get_link_source(entry);
  748. if (!link) {
  749. link = entry;
  750. }
  751. if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), fp, link->uncompressed_filesize, NULL)) {
  752. if (error) {
  753. spprintf(error, 4096, "phar error: cannot separate entry file \"%s\" contents in phar archive \"%s\" for write access", entry->filename, entry->phar->fname);
  754. }
  755. return FAILURE;
  756. }
  757. if (entry->link) {
  758. efree(entry->link);
  759. entry->link = NULL;
  760. entry->tar_type = (entry->is_tar ? TAR_FILE : '\0');
  761. }
  762. entry->offset = 0;
  763. entry->fp = fp;
  764. entry->fp_type = PHAR_MOD;
  765. entry->is_modified = 1;
  766. return SUCCESS;
  767. }
  768. /* }}} */
  769. /**
  770. * helper function to open an internal file's fp just-in-time
  771. */
  772. phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error) /* {{{ */
  773. {
  774. if (error) {
  775. *error = NULL;
  776. }
  777. /* seek to start of internal file and read it */
  778. if (FAILURE == phar_open_entry_fp(entry, error, 1)) {
  779. return NULL;
  780. }
  781. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1)) {
  782. spprintf(error, 4096, "phar error: cannot seek to start of file \"%s\" in phar \"%s\"", entry->filename, phar->fname);
  783. return NULL;
  784. }
  785. return entry;
  786. }
  787. /* }}} */
  788. PHP_PHAR_API int phar_resolve_alias(char *alias, size_t alias_len, char **filename, size_t *filename_len) /* {{{ */ {
  789. phar_archive_data *fd_ptr;
  790. if (HT_IS_INITIALIZED(&PHAR_G(phar_alias_map))
  791. && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
  792. *filename = fd_ptr->fname;
  793. *filename_len = fd_ptr->fname_len;
  794. return SUCCESS;
  795. }
  796. return FAILURE;
  797. }
  798. /* }}} */
  799. int phar_free_alias(phar_archive_data *phar, char *alias, size_t alias_len) /* {{{ */
  800. {
  801. if (phar->refcount || phar->is_persistent) {
  802. return FAILURE;
  803. }
  804. /* this archive has no open references, so emit a notice and remove it */
  805. if (zend_hash_str_del(&(PHAR_G(phar_fname_map)), phar->fname, phar->fname_len) != SUCCESS) {
  806. return FAILURE;
  807. }
  808. /* invalidate phar cache */
  809. PHAR_G(last_phar) = NULL;
  810. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  811. return SUCCESS;
  812. }
  813. /* }}} */
  814. /**
  815. * Looks up a phar archive in the filename map, connecting it to the alias
  816. * (if any) or returns null
  817. */
  818. int phar_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, char *alias, size_t alias_len, char **error) /* {{{ */
  819. {
  820. phar_archive_data *fd, *fd_ptr;
  821. char *my_realpath, *save;
  822. size_t save_len;
  823. phar_request_initialize();
  824. if (error) {
  825. *error = NULL;
  826. }
  827. *archive = NULL;
  828. if (PHAR_G(last_phar) && fname_len == PHAR_G(last_phar_name_len) && !memcmp(fname, PHAR_G(last_phar_name), fname_len)) {
  829. *archive = PHAR_G(last_phar);
  830. if (alias && alias_len) {
  831. if (!PHAR_G(last_phar)->is_temporary_alias && (alias_len != PHAR_G(last_phar)->alias_len || memcmp(PHAR_G(last_phar)->alias, alias, alias_len))) {
  832. if (error) {
  833. spprintf(error, 0, "alias \"%s\" is already used for archive \"%s\" cannot be overloaded with \"%s\"", alias, PHAR_G(last_phar)->fname, fname);
  834. }
  835. *archive = NULL;
  836. return FAILURE;
  837. }
  838. if (PHAR_G(last_phar)->alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), PHAR_G(last_phar)->alias, PHAR_G(last_phar)->alias_len))) {
  839. zend_hash_str_del(&(PHAR_G(phar_alias_map)), PHAR_G(last_phar)->alias, PHAR_G(last_phar)->alias_len);
  840. }
  841. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, *archive);
  842. PHAR_G(last_alias) = alias;
  843. PHAR_G(last_alias_len) = alias_len;
  844. }
  845. return SUCCESS;
  846. }
  847. if (alias && alias_len && PHAR_G(last_phar) && alias_len == PHAR_G(last_alias_len) && !memcmp(alias, PHAR_G(last_alias), alias_len)) {
  848. fd = PHAR_G(last_phar);
  849. fd_ptr = fd;
  850. goto alias_success;
  851. }
  852. if (alias && alias_len) {
  853. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
  854. alias_success:
  855. if (fname && (fname_len != fd_ptr->fname_len || strncmp(fname, fd_ptr->fname, fname_len))) {
  856. if (error) {
  857. spprintf(error, 0, "alias \"%s\" is already used for archive \"%s\" cannot be overloaded with \"%s\"", alias, fd_ptr->fname, fname);
  858. }
  859. if (SUCCESS == phar_free_alias(fd_ptr, alias, alias_len)) {
  860. if (error) {
  861. efree(*error);
  862. *error = NULL;
  863. }
  864. }
  865. return FAILURE;
  866. }
  867. *archive = fd_ptr;
  868. fd = fd_ptr;
  869. PHAR_G(last_phar) = fd;
  870. PHAR_G(last_phar_name) = fd->fname;
  871. PHAR_G(last_phar_name_len) = fd->fname_len;
  872. PHAR_G(last_alias) = alias;
  873. PHAR_G(last_alias_len) = alias_len;
  874. return SUCCESS;
  875. }
  876. if (PHAR_G(manifest_cached) && NULL != (fd_ptr = zend_hash_str_find_ptr(&cached_alias, alias, alias_len))) {
  877. goto alias_success;
  878. }
  879. }
  880. my_realpath = NULL;
  881. save = fname;
  882. save_len = fname_len;
  883. if (fname && fname_len) {
  884. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len))) {
  885. *archive = fd_ptr;
  886. fd = fd_ptr;
  887. if (alias && alias_len) {
  888. if (!fd->is_temporary_alias && (alias_len != fd->alias_len || memcmp(fd->alias, alias, alias_len))) {
  889. if (error) {
  890. spprintf(error, 0, "alias \"%s\" is already used for archive \"%s\" cannot be overloaded with \"%s\"", alias, fd_ptr->fname, fname);
  891. }
  892. return FAILURE;
  893. }
  894. if (fd->alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), fd->alias, fd->alias_len))) {
  895. zend_hash_str_del(&(PHAR_G(phar_alias_map)), fd->alias, fd->alias_len);
  896. }
  897. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, fd);
  898. }
  899. PHAR_G(last_phar) = fd;
  900. PHAR_G(last_phar_name) = fd->fname;
  901. PHAR_G(last_phar_name_len) = fd->fname_len;
  902. PHAR_G(last_alias) = fd->alias;
  903. PHAR_G(last_alias_len) = fd->alias_len;
  904. return SUCCESS;
  905. }
  906. if (PHAR_G(manifest_cached) && NULL != (fd_ptr = zend_hash_str_find_ptr(&cached_phars, fname, fname_len))) {
  907. *archive = fd_ptr;
  908. fd = fd_ptr;
  909. /* this could be problematic - alias should never be different from manifest alias
  910. for cached phars */
  911. if (!fd->is_temporary_alias && alias && alias_len) {
  912. if (alias_len != fd->alias_len || memcmp(fd->alias, alias, alias_len)) {
  913. if (error) {
  914. spprintf(error, 0, "alias \"%s\" is already used for archive \"%s\" cannot be overloaded with \"%s\"", alias, fd_ptr->fname, fname);
  915. }
  916. return FAILURE;
  917. }
  918. }
  919. PHAR_G(last_phar) = fd;
  920. PHAR_G(last_phar_name) = fd->fname;
  921. PHAR_G(last_phar_name_len) = fd->fname_len;
  922. PHAR_G(last_alias) = fd->alias;
  923. PHAR_G(last_alias_len) = fd->alias_len;
  924. return SUCCESS;
  925. }
  926. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), save, save_len))) {
  927. fd = *archive = fd_ptr;
  928. PHAR_G(last_phar) = fd;
  929. PHAR_G(last_phar_name) = fd->fname;
  930. PHAR_G(last_phar_name_len) = fd->fname_len;
  931. PHAR_G(last_alias) = fd->alias;
  932. PHAR_G(last_alias_len) = fd->alias_len;
  933. return SUCCESS;
  934. }
  935. if (PHAR_G(manifest_cached) && NULL != (fd_ptr = zend_hash_str_find_ptr(&cached_alias, save, save_len))) {
  936. fd = *archive = fd_ptr;
  937. PHAR_G(last_phar) = fd;
  938. PHAR_G(last_phar_name) = fd->fname;
  939. PHAR_G(last_phar_name_len) = fd->fname_len;
  940. PHAR_G(last_alias) = fd->alias;
  941. PHAR_G(last_alias_len) = fd->alias_len;
  942. return SUCCESS;
  943. }
  944. /* not found, try converting \ to / */
  945. my_realpath = expand_filepath(fname, my_realpath);
  946. if (my_realpath) {
  947. fname_len = strlen(my_realpath);
  948. fname = my_realpath;
  949. } else {
  950. return FAILURE;
  951. }
  952. #ifdef PHP_WIN32
  953. phar_unixify_path_separators(fname, fname_len);
  954. #endif
  955. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len))) {
  956. realpath_success:
  957. *archive = fd_ptr;
  958. fd = fd_ptr;
  959. if (alias && alias_len) {
  960. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, fd);
  961. }
  962. efree(my_realpath);
  963. PHAR_G(last_phar) = fd;
  964. PHAR_G(last_phar_name) = fd->fname;
  965. PHAR_G(last_phar_name_len) = fd->fname_len;
  966. PHAR_G(last_alias) = fd->alias;
  967. PHAR_G(last_alias_len) = fd->alias_len;
  968. return SUCCESS;
  969. }
  970. if (PHAR_G(manifest_cached) && NULL != (fd_ptr = zend_hash_str_find_ptr(&cached_phars, fname, fname_len))) {
  971. goto realpath_success;
  972. }
  973. efree(my_realpath);
  974. }
  975. return FAILURE;
  976. }
  977. /* }}} */
  978. /**
  979. * Determine which stream compression filter (if any) we need to read this file
  980. */
  981. char * phar_compress_filter(phar_entry_info * entry, int return_unknown) /* {{{ */
  982. {
  983. switch (entry->flags & PHAR_ENT_COMPRESSION_MASK) {
  984. case PHAR_ENT_COMPRESSED_GZ:
  985. return "zlib.deflate";
  986. case PHAR_ENT_COMPRESSED_BZ2:
  987. return "bzip2.compress";
  988. default:
  989. return return_unknown ? "unknown" : NULL;
  990. }
  991. }
  992. /* }}} */
  993. /**
  994. * Determine which stream decompression filter (if any) we need to read this file
  995. */
  996. char * phar_decompress_filter(phar_entry_info * entry, int return_unknown) /* {{{ */
  997. {
  998. uint32_t flags;
  999. if (entry->is_modified) {
  1000. flags = entry->old_flags;
  1001. } else {
  1002. flags = entry->flags;
  1003. }
  1004. switch (flags & PHAR_ENT_COMPRESSION_MASK) {
  1005. case PHAR_ENT_COMPRESSED_GZ:
  1006. return "zlib.inflate";
  1007. case PHAR_ENT_COMPRESSED_BZ2:
  1008. return "bzip2.decompress";
  1009. default:
  1010. return return_unknown ? "unknown" : NULL;
  1011. }
  1012. }
  1013. /* }}} */
  1014. /**
  1015. * retrieve information on a file contained within a phar, or null if it ain't there
  1016. */
  1017. phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security) /* {{{ */
  1018. {
  1019. return phar_get_entry_info_dir(phar, path, path_len, 0, error, security);
  1020. }
  1021. /* }}} */
  1022. /**
  1023. * retrieve information on a file or directory contained within a phar, or null if none found
  1024. * allow_dir is 0 for none, 1 for both empty directories in the phar and temp directories, and 2 for only
  1025. * valid pre-existing empty directory entries
  1026. */
  1027. phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security) /* {{{ */
  1028. {
  1029. const char *pcr_error;
  1030. phar_entry_info *entry;
  1031. int is_dir;
  1032. #ifdef PHP_WIN32
  1033. phar_unixify_path_separators(path, path_len);
  1034. #endif
  1035. is_dir = (path_len && (path[path_len - 1] == '/')) ? 1 : 0;
  1036. if (error) {
  1037. *error = NULL;
  1038. }
  1039. if (security && path_len >= sizeof(".phar")-1 && !memcmp(path, ".phar", sizeof(".phar")-1)) {
  1040. if (error) {
  1041. spprintf(error, 4096, "phar error: cannot directly access magic \".phar\" directory or files within it");
  1042. }
  1043. return NULL;
  1044. }
  1045. if (!path_len && !dir) {
  1046. if (error) {
  1047. spprintf(error, 4096, "phar error: invalid path \"%s\" must not be empty", path);
  1048. }
  1049. return NULL;
  1050. }
  1051. if (phar_path_check(&path, &path_len, &pcr_error) > pcr_is_ok) {
  1052. if (error) {
  1053. spprintf(error, 4096, "phar error: invalid path \"%s\" contains %s", path, pcr_error);
  1054. }
  1055. return NULL;
  1056. }
  1057. if (!HT_IS_INITIALIZED(&phar->manifest)) {
  1058. return NULL;
  1059. }
  1060. if (is_dir) {
  1061. if (!path_len || path_len == 1) {
  1062. return NULL;
  1063. }
  1064. path_len--;
  1065. }
  1066. if (NULL != (entry = zend_hash_str_find_ptr(&phar->manifest, path, path_len))) {
  1067. if (entry->is_deleted) {
  1068. /* entry is deleted, but has not been flushed to disk yet */
  1069. return NULL;
  1070. }
  1071. if (entry->is_dir && !dir) {
  1072. if (error) {
  1073. spprintf(error, 4096, "phar error: path \"%s\" is a directory", path);
  1074. }
  1075. return NULL;
  1076. }
  1077. if (!entry->is_dir && dir == 2) {
  1078. /* user requested a directory, we must return one */
  1079. if (error) {
  1080. spprintf(error, 4096, "phar error: path \"%s\" exists and is a not a directory", path);
  1081. }
  1082. return NULL;
  1083. }
  1084. return entry;
  1085. }
  1086. if (dir) {
  1087. if (zend_hash_str_exists(&phar->virtual_dirs, path, path_len)) {
  1088. /* a file or directory exists in a sub-directory of this path */
  1089. entry = (phar_entry_info *) ecalloc(1, sizeof(phar_entry_info));
  1090. /* this next line tells PharFileInfo->__destruct() to efree the filename */
  1091. entry->is_temp_dir = entry->is_dir = 1;
  1092. entry->filename = (char *) estrndup(path, path_len + 1);
  1093. entry->filename_len = path_len;
  1094. entry->phar = phar;
  1095. return entry;
  1096. }
  1097. }
  1098. if (HT_IS_INITIALIZED(&phar->mounted_dirs) && zend_hash_num_elements(&phar->mounted_dirs)) {
  1099. zend_string *str_key;
  1100. ZEND_HASH_FOREACH_STR_KEY(&phar->mounted_dirs, str_key) {
  1101. if (ZSTR_LEN(str_key) >= path_len || strncmp(ZSTR_VAL(str_key), path, ZSTR_LEN(str_key))) {
  1102. continue;
  1103. } else {
  1104. char *test;
  1105. size_t test_len;
  1106. php_stream_statbuf ssb;
  1107. if (NULL == (entry = zend_hash_find_ptr(&phar->manifest, str_key))) {
  1108. if (error) {
  1109. spprintf(error, 4096, "phar internal error: mounted path \"%s\" could not be retrieved from manifest", ZSTR_VAL(str_key));
  1110. }
  1111. return NULL;
  1112. }
  1113. if (!entry->tmp || !entry->is_mounted) {
  1114. if (error) {
  1115. spprintf(error, 4096, "phar internal error: mounted path \"%s\" is not properly initialized as a mounted path", ZSTR_VAL(str_key));
  1116. }
  1117. return NULL;
  1118. }
  1119. test_len = spprintf(&test, MAXPATHLEN, "%s%s", entry->tmp, path + ZSTR_LEN(str_key));
  1120. if (SUCCESS != php_stream_stat_path(test, &ssb)) {
  1121. efree(test);
  1122. return NULL;
  1123. }
  1124. if ((ssb.sb.st_mode & S_IFDIR) && !dir) {
  1125. efree(test);
  1126. if (error) {
  1127. spprintf(error, 4096, "phar error: path \"%s\" is a directory", path);
  1128. }
  1129. return NULL;
  1130. }
  1131. if ((ssb.sb.st_mode & S_IFDIR) == 0 && dir) {
  1132. efree(test);
  1133. /* user requested a directory, we must return one */
  1134. if (error) {
  1135. spprintf(error, 4096, "phar error: path \"%s\" exists and is a not a directory", path);
  1136. }
  1137. return NULL;
  1138. }
  1139. /* mount the file just in time */
  1140. if (SUCCESS != phar_mount_entry(phar, test, test_len, path, path_len)) {
  1141. efree(test);
  1142. if (error) {
  1143. spprintf(error, 4096, "phar error: path \"%s\" exists as file \"%s\" and could not be mounted", path, test);
  1144. }
  1145. return NULL;
  1146. }
  1147. efree(test);
  1148. if (NULL == (entry = zend_hash_str_find_ptr(&phar->manifest, path, path_len))) {
  1149. if (error) {
  1150. spprintf(error, 4096, "phar error: path \"%s\" exists as file \"%s\" and could not be retrieved after being mounted", path, test);
  1151. }
  1152. return NULL;
  1153. }
  1154. return entry;
  1155. }
  1156. } ZEND_HASH_FOREACH_END();
  1157. }
  1158. return NULL;
  1159. }
  1160. /* }}} */
  1161. static const char hexChars[] = "0123456789ABCDEF";
  1162. static int phar_hex_str(const char *digest, size_t digest_len, char **signature) /* {{{ */
  1163. {
  1164. int pos = -1;
  1165. size_t len = 0;
  1166. *signature = (char*)safe_pemalloc(digest_len, 2, 1, PHAR_G(persist));
  1167. for (; len < digest_len; ++len) {
  1168. (*signature)[++pos] = hexChars[((const unsigned char *)digest)[len] >> 4];
  1169. (*signature)[++pos] = hexChars[((const unsigned char *)digest)[len] & 0x0F];
  1170. }
  1171. (*signature)[++pos] = '\0';
  1172. return pos;
  1173. }
  1174. /* }}} */
  1175. #ifndef PHAR_HAVE_OPENSSL
  1176. static int phar_call_openssl_signverify(int is_sign, php_stream *fp, zend_off_t end, char *key, size_t key_len, char **signature, size_t *signature_len, php_uint32 sig_type) /* {{{ */
  1177. {
  1178. zend_fcall_info fci;
  1179. zend_fcall_info_cache fcc;
  1180. zval retval, zp[4], openssl;
  1181. zend_string *str;
  1182. ZVAL_STRINGL(&openssl, is_sign ? "openssl_sign" : "openssl_verify", is_sign ? sizeof("openssl_sign")-1 : sizeof("openssl_verify")-1);
  1183. if (*signature_len) {
  1184. ZVAL_STRINGL(&zp[1], *signature, *signature_len);
  1185. } else {
  1186. ZVAL_EMPTY_STRING(&zp[1]);
  1187. }
  1188. ZVAL_STRINGL(&zp[2], key, key_len);
  1189. php_stream_rewind(fp);
  1190. str = php_stream_copy_to_mem(fp, (size_t) end, 0);
  1191. if (str) {
  1192. ZVAL_STR(&zp[0], str);
  1193. } else {
  1194. ZVAL_EMPTY_STRING(&zp[0]);
  1195. }
  1196. if (sig_type == PHAR_SIG_OPENSSL_SHA512) {
  1197. ZVAL_LONG(&zp[3], 9); /* value from openssl.c #define OPENSSL_ALGO_SHA512 9 */
  1198. } else if (sig_type == PHAR_SIG_OPENSSL_SHA256) {
  1199. ZVAL_LONG(&zp[3], 7); /* value from openssl.c #define OPENSSL_ALGO_SHA256 7 */
  1200. } else {
  1201. /* don't rely on default value which may change in the future */
  1202. ZVAL_LONG(&zp[3], 1); /* value from openssl.c #define OPENSSL_ALGO_SHA1 1 */
  1203. }
  1204. if ((size_t)end != Z_STRLEN(zp[0])) {
  1205. zval_ptr_dtor_str(&zp[0]);
  1206. zval_ptr_dtor_str(&zp[1]);
  1207. zval_ptr_dtor_str(&zp[2]);
  1208. zval_ptr_dtor_str(&openssl);
  1209. return FAILURE;
  1210. }
  1211. if (FAILURE == zend_fcall_info_init(&openssl, 0, &fci, &fcc, NULL, NULL)) {
  1212. zval_ptr_dtor_str(&zp[0]);
  1213. zval_ptr_dtor_str(&zp[1]);
  1214. zval_ptr_dtor_str(&zp[2]);
  1215. zval_ptr_dtor_str(&openssl);
  1216. return FAILURE;
  1217. }
  1218. fci.param_count = 4;
  1219. fci.params = zp;
  1220. Z_ADDREF(zp[0]);
  1221. if (is_sign) {
  1222. ZVAL_NEW_REF(&zp[1], &zp[1]);
  1223. } else {
  1224. Z_ADDREF(zp[1]);
  1225. }
  1226. Z_ADDREF(zp[2]);
  1227. fci.retval = &retval;
  1228. if (FAILURE == zend_call_function(&fci, &fcc)) {
  1229. zval_ptr_dtor_str(&zp[0]);
  1230. zval_ptr_dtor(&zp[1]);
  1231. zval_ptr_dtor_str(&zp[2]);
  1232. zval_ptr_dtor_str(&openssl);
  1233. return FAILURE;
  1234. }
  1235. zval_ptr_dtor_str(&openssl);
  1236. Z_DELREF(zp[0]);
  1237. if (is_sign) {
  1238. ZVAL_UNREF(&zp[1]);
  1239. } else {
  1240. Z_DELREF(zp[1]);
  1241. }
  1242. Z_DELREF(zp[2]);
  1243. zval_ptr_dtor_str(&zp[0]);
  1244. zval_ptr_dtor_str(&zp[2]);
  1245. switch (Z_TYPE(retval)) {
  1246. default:
  1247. case IS_LONG:
  1248. zval_ptr_dtor(&zp[1]);
  1249. if (1 == Z_LVAL(retval)) {
  1250. return SUCCESS;
  1251. }
  1252. return FAILURE;
  1253. case IS_TRUE:
  1254. *signature = estrndup(Z_STRVAL(zp[1]), Z_STRLEN(zp[1]));
  1255. *signature_len = Z_STRLEN(zp[1]);
  1256. zval_ptr_dtor(&zp[1]);
  1257. return SUCCESS;
  1258. case IS_FALSE:
  1259. zval_ptr_dtor(&zp[1]);
  1260. return FAILURE;
  1261. }
  1262. }
  1263. /* }}} */
  1264. #endif /* #ifndef PHAR_HAVE_OPENSSL */
  1265. int phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type, char *sig, size_t sig_len, char *fname, char **signature, size_t *signature_len, char **error) /* {{{ */
  1266. {
  1267. size_t read_size, len;
  1268. zend_off_t read_len;
  1269. unsigned char buf[1024];
  1270. php_stream_rewind(fp);
  1271. switch (sig_type) {
  1272. case PHAR_SIG_OPENSSL_SHA512:
  1273. case PHAR_SIG_OPENSSL_SHA256:
  1274. case PHAR_SIG_OPENSSL: {
  1275. #ifdef PHAR_HAVE_OPENSSL
  1276. BIO *in;
  1277. EVP_PKEY *key;
  1278. const EVP_MD *mdtype;
  1279. EVP_MD_CTX *md_ctx;
  1280. if (sig_type == PHAR_SIG_OPENSSL_SHA512) {
  1281. mdtype = EVP_sha512();
  1282. } else if (sig_type == PHAR_SIG_OPENSSL_SHA256) {
  1283. mdtype = EVP_sha256();
  1284. } else {
  1285. mdtype = EVP_sha1();
  1286. }
  1287. #else
  1288. size_t tempsig;
  1289. #endif
  1290. zend_string *pubkey = NULL;
  1291. char *pfile;
  1292. php_stream *pfp;
  1293. #ifndef PHAR_HAVE_OPENSSL
  1294. if (!zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
  1295. if (error) {
  1296. spprintf(error, 0, "openssl not loaded");
  1297. }
  1298. return FAILURE;
  1299. }
  1300. #endif
  1301. /* use __FILE__ . '.pubkey' for public key file */
  1302. spprintf(&pfile, 0, "%s.pubkey", fname);
  1303. pfp = php_stream_open_wrapper(pfile, "rb", 0, NULL);
  1304. efree(pfile);
  1305. if (!pfp || !(pubkey = php_stream_copy_to_mem(pfp, PHP_STREAM_COPY_ALL, 0)) || !ZSTR_LEN(pubkey)) {
  1306. if (pfp) {
  1307. php_stream_close(pfp);
  1308. }
  1309. if (error) {
  1310. spprintf(error, 0, "openssl public key could not be read");
  1311. }
  1312. return FAILURE;
  1313. }
  1314. php_stream_close(pfp);
  1315. #ifndef PHAR_HAVE_OPENSSL
  1316. tempsig = sig_len;
  1317. if (FAILURE == phar_call_openssl_signverify(0, fp, end_of_phar, pubkey ? ZSTR_VAL(pubkey) : NULL, pubkey ? ZSTR_LEN(pubkey) : 0, &sig, &tempsig, sig_type)) {
  1318. if (pubkey) {
  1319. zend_string_release_ex(pubkey, 0);
  1320. }
  1321. if (error) {
  1322. spprintf(error, 0, "openssl signature could not be verified");
  1323. }
  1324. return FAILURE;
  1325. }
  1326. if (pubkey) {
  1327. zend_string_release_ex(pubkey, 0);
  1328. }
  1329. sig_len = tempsig;
  1330. #else
  1331. in = BIO_new_mem_buf(pubkey ? ZSTR_VAL(pubkey) : NULL, pubkey ? ZSTR_LEN(pubkey) : 0);
  1332. if (NULL == in) {
  1333. zend_string_release_ex(pubkey, 0);
  1334. if (error) {
  1335. spprintf(error, 0, "openssl signature could not be processed");
  1336. }
  1337. return FAILURE;
  1338. }
  1339. key = PEM_read_bio_PUBKEY(in, NULL, NULL, NULL);
  1340. BIO_free(in);
  1341. zend_string_release_ex(pubkey, 0);
  1342. if (NULL == key) {
  1343. if (error) {
  1344. spprintf(error, 0, "openssl signature could not be processed");
  1345. }
  1346. return FAILURE;
  1347. }
  1348. md_ctx = EVP_MD_CTX_create();
  1349. EVP_VerifyInit(md_ctx, mdtype);
  1350. read_len = end_of_phar;
  1351. if ((size_t)read_len > sizeof(buf)) {
  1352. read_size = sizeof(buf);
  1353. } else {
  1354. read_size = (size_t)read_len;
  1355. }
  1356. php_stream_seek(fp, 0, SEEK_SET);
  1357. while (read_size && (len = php_stream_read(fp, (char*)buf, read_size)) > 0) {
  1358. EVP_VerifyUpdate (md_ctx, buf, len);
  1359. read_len -= (zend_off_t)len;
  1360. if (read_len < read_size) {
  1361. read_size = (size_t)read_len;
  1362. }
  1363. }
  1364. if (EVP_VerifyFinal(md_ctx, (unsigned char *)sig, sig_len, key) != 1) {
  1365. /* 1: signature verified, 0: signature does not match, -1: failed signature operation */
  1366. EVP_PKEY_free(key);
  1367. EVP_MD_CTX_destroy(md_ctx);
  1368. if (error) {
  1369. spprintf(error, 0, "broken openssl signature");
  1370. }
  1371. return FAILURE;
  1372. }
  1373. EVP_PKEY_free(key);
  1374. EVP_MD_CTX_destroy(md_ctx);
  1375. #endif
  1376. *signature_len = phar_hex_str((const char*)sig, sig_len, signature);
  1377. }
  1378. break;
  1379. case PHAR_SIG_SHA512: {
  1380. unsigned char digest[64];
  1381. PHP_SHA512_CTX context;
  1382. if (sig_len < sizeof(digest)) {
  1383. if (error) {
  1384. spprintf(error, 0, "broken signature");
  1385. }
  1386. return FAILURE;
  1387. }
  1388. PHP_SHA512Init(&context);
  1389. read_len = end_of_phar;
  1390. if ((size_t)read_len > sizeof(buf)) {
  1391. read_size = sizeof(buf);
  1392. } else {
  1393. read_size = (size_t)read_len;
  1394. }
  1395. while ((len = php_stream_read(fp, (char*)buf, read_size)) > 0) {
  1396. PHP_SHA512Update(&context, buf, len);
  1397. read_len -= (zend_off_t)len;
  1398. if ((size_t)read_len < read_size) {
  1399. read_size = (size_t)read_len;
  1400. }
  1401. }
  1402. PHP_SHA512Final(digest, &context);
  1403. if (memcmp(digest, sig, sizeof(digest))) {
  1404. if (error) {
  1405. spprintf(error, 0, "broken signature");
  1406. }
  1407. return FAILURE;
  1408. }
  1409. *signature_len = phar_hex_str((const char*)digest, sizeof(digest), signature);
  1410. break;
  1411. }
  1412. case PHAR_SIG_SHA256: {
  1413. unsigned char digest[32];
  1414. PHP_SHA256_CTX context;
  1415. if (sig_len < sizeof(digest)) {
  1416. if (error) {
  1417. spprintf(error, 0, "broken signature");
  1418. }
  1419. return FAILURE;
  1420. }
  1421. PHP_SHA256Init(&context);
  1422. read_len = end_of_phar;
  1423. if ((size_t)read_len > sizeof(buf)) {
  1424. read_size = sizeof(buf);
  1425. } else {
  1426. read_size = (size_t)read_len;
  1427. }
  1428. while ((len = php_stream_read(fp, (char*)buf, read_size)) > 0) {
  1429. PHP_SHA256Update(&context, buf, len);
  1430. read_len -= (zend_off_t)len;
  1431. if ((size_t)read_len < read_size) {
  1432. read_size = (size_t)read_len;
  1433. }
  1434. }
  1435. PHP_SHA256Final(digest, &context);
  1436. if (memcmp(digest, sig, sizeof(digest))) {
  1437. if (error) {
  1438. spprintf(error, 0, "broken signature");
  1439. }
  1440. return FAILURE;
  1441. }
  1442. *signature_len = phar_hex_str((const char*)digest, sizeof(digest), signature);
  1443. break;
  1444. }
  1445. case PHAR_SIG_SHA1: {
  1446. unsigned char digest[20];
  1447. PHP_SHA1_CTX context;
  1448. if (sig_len < sizeof(digest)) {
  1449. if (error) {
  1450. spprintf(error, 0, "broken signature");
  1451. }
  1452. return FAILURE;
  1453. }
  1454. PHP_SHA1Init(&context);
  1455. read_len = end_of_phar;
  1456. if ((size_t)read_len > sizeof(buf)) {
  1457. read_size = sizeof(buf);
  1458. } else {
  1459. read_size = (size_t)read_len;
  1460. }
  1461. while ((len = php_stream_read(fp, (char*)buf, read_size)) > 0) {
  1462. PHP_SHA1Update(&context, buf, len);
  1463. read_len -= (zend_off_t)len;
  1464. if ((size_t)read_len < read_size) {
  1465. read_size = (size_t)read_len;
  1466. }
  1467. }
  1468. PHP_SHA1Final(digest, &context);
  1469. if (memcmp(digest, sig, sizeof(digest))) {
  1470. if (error) {
  1471. spprintf(error, 0, "broken signature");
  1472. }
  1473. return FAILURE;
  1474. }
  1475. *signature_len = phar_hex_str((const char*)digest, sizeof(digest), signature);
  1476. break;
  1477. }
  1478. case PHAR_SIG_MD5: {
  1479. unsigned char digest[16];
  1480. PHP_MD5_CTX context;
  1481. if (sig_len < sizeof(digest)) {
  1482. if (error) {
  1483. spprintf(error, 0, "broken signature");
  1484. }
  1485. return FAILURE;
  1486. }
  1487. PHP_MD5Init(&context);
  1488. read_len = end_of_phar;
  1489. if ((size_t)read_len > sizeof(buf)) {
  1490. read_size = sizeof(buf);
  1491. } else {
  1492. read_size = (size_t)read_len;
  1493. }
  1494. while ((len = php_stream_read(fp, (char*)buf, read_size)) > 0) {
  1495. PHP_MD5Update(&context, buf, len);
  1496. read_len -= (zend_off_t)len;
  1497. if ((size_t)read_len < read_size) {
  1498. read_size = (size_t)read_len;
  1499. }
  1500. }
  1501. PHP_MD5Final(digest, &context);
  1502. if (memcmp(digest, sig, sizeof(digest))) {
  1503. if (error) {
  1504. spprintf(error, 0, "broken signature");
  1505. }
  1506. return FAILURE;
  1507. }
  1508. *signature_len = phar_hex_str((const char*)digest, sizeof(digest), signature);
  1509. break;
  1510. }
  1511. default:
  1512. if (error) {
  1513. spprintf(error, 0, "broken or unsupported signature");
  1514. }
  1515. return FAILURE;
  1516. }
  1517. return SUCCESS;
  1518. }
  1519. /* }}} */
  1520. int phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error) /* {{{ */
  1521. {
  1522. unsigned char buf[1024];
  1523. size_t sig_len;
  1524. php_stream_rewind(fp);
  1525. if (phar->signature) {
  1526. efree(phar->signature);
  1527. phar->signature = NULL;
  1528. }
  1529. switch(phar->sig_flags) {
  1530. case PHAR_SIG_SHA512: {
  1531. unsigned char digest[64];
  1532. PHP_SHA512_CTX context;
  1533. PHP_SHA512Init(&context);
  1534. while ((sig_len = php_stream_read(fp, (char*)buf, sizeof(buf))) > 0) {
  1535. PHP_SHA512Update(&context, buf, sig_len);
  1536. }
  1537. PHP_SHA512Final(digest, &context);
  1538. *signature = estrndup((char *) digest, 64);
  1539. *signature_length = 64;
  1540. break;
  1541. }
  1542. default:
  1543. phar->sig_flags = PHAR_SIG_SHA256;
  1544. ZEND_FALLTHROUGH;
  1545. case PHAR_SIG_SHA256: {
  1546. unsigned char digest[32];
  1547. PHP_SHA256_CTX context;
  1548. PHP_SHA256Init(&context);
  1549. while ((sig_len = php_stream_read(fp, (char*)buf, sizeof(buf))) > 0) {
  1550. PHP_SHA256Update(&context, buf, sig_len);
  1551. }
  1552. PHP_SHA256Final(digest, &context);
  1553. *signature = estrndup((char *) digest, 32);
  1554. *signature_length = 32;
  1555. break;
  1556. }
  1557. case PHAR_SIG_OPENSSL_SHA512:
  1558. case PHAR_SIG_OPENSSL_SHA256:
  1559. case PHAR_SIG_OPENSSL: {
  1560. unsigned char *sigbuf;
  1561. #ifdef PHAR_HAVE_OPENSSL
  1562. unsigned int siglen;
  1563. BIO *in;
  1564. EVP_PKEY *key;
  1565. EVP_MD_CTX *md_ctx;
  1566. const EVP_MD *mdtype;
  1567. if (phar->sig_flags == PHAR_SIG_OPENSSL_SHA512) {
  1568. mdtype = EVP_sha512();
  1569. } else if (phar->sig_flags == PHAR_SIG_OPENSSL_SHA256) {
  1570. mdtype = EVP_sha256();
  1571. } else {
  1572. mdtype = EVP_sha1();
  1573. }
  1574. in = BIO_new_mem_buf(PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len));
  1575. if (in == NULL) {
  1576. if (error) {
  1577. spprintf(error, 0, "unable to write to phar \"%s\" with requested openssl signature", phar->fname);
  1578. }
  1579. return FAILURE;
  1580. }
  1581. key = PEM_read_bio_PrivateKey(in, NULL,NULL, "");
  1582. BIO_free(in);
  1583. if (!key) {
  1584. if (error) {
  1585. spprintf(error, 0, "unable to process private key");
  1586. }
  1587. return FAILURE;
  1588. }
  1589. md_ctx = EVP_MD_CTX_create();
  1590. siglen = EVP_PKEY_size(key);
  1591. sigbuf = emalloc(siglen + 1);
  1592. if (!EVP_SignInit(md_ctx, mdtype)) {
  1593. EVP_PKEY_free(key);
  1594. efree(sigbuf);
  1595. if (error) {
  1596. spprintf(error, 0, "unable to initialize openssl signature for phar \"%s\"", phar->fname);
  1597. }
  1598. return FAILURE;
  1599. }
  1600. while ((sig_len = php_stream_read(fp, (char*)buf, sizeof(buf))) > 0) {
  1601. if (!EVP_SignUpdate(md_ctx, buf, sig_len)) {
  1602. EVP_PKEY_free(key);
  1603. efree(sigbuf);
  1604. if (error) {
  1605. spprintf(error, 0, "unable to update the openssl signature for phar \"%s\"", phar->fname);
  1606. }
  1607. return FAILURE;
  1608. }
  1609. }
  1610. if (!EVP_SignFinal (md_ctx, sigbuf, &siglen, key)) {
  1611. EVP_PKEY_free(key);
  1612. efree(sigbuf);
  1613. if (error) {
  1614. spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname);
  1615. }
  1616. return FAILURE;
  1617. }
  1618. sigbuf[siglen] = '\0';
  1619. EVP_PKEY_free(key);
  1620. EVP_MD_CTX_destroy(md_ctx);
  1621. #else
  1622. size_t siglen;
  1623. sigbuf = NULL;
  1624. siglen = 0;
  1625. php_stream_seek(fp, 0, SEEK_END);
  1626. if (FAILURE == phar_call_openssl_signverify(1, fp, php_stream_tell(fp), PHAR_G(openssl_privatekey), PHAR_G(openssl_privatekey_len), (char **)&sigbuf, &siglen, phar->sig_flags)) {
  1627. if (error) {
  1628. spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname);
  1629. }
  1630. return FAILURE;
  1631. }
  1632. #endif
  1633. *signature = (char *) sigbuf;
  1634. *signature_length = siglen;
  1635. }
  1636. break;
  1637. case PHAR_SIG_SHA1: {
  1638. unsigned char digest[20];
  1639. PHP_SHA1_CTX context;
  1640. PHP_SHA1Init(&context);
  1641. while ((sig_len = php_stream_read(fp, (char*)buf, sizeof(buf))) > 0) {
  1642. PHP_SHA1Update(&context, buf, sig_len);
  1643. }
  1644. PHP_SHA1Final(digest, &context);
  1645. *signature = estrndup((char *) digest, 20);
  1646. *signature_length = 20;
  1647. break;
  1648. }
  1649. case PHAR_SIG_MD5: {
  1650. unsigned char digest[16];
  1651. PHP_MD5_CTX context;
  1652. PHP_MD5Init(&context);
  1653. while ((sig_len = php_stream_read(fp, (char*)buf, sizeof(buf))) > 0) {
  1654. PHP_MD5Update(&context, buf, sig_len);
  1655. }
  1656. PHP_MD5Final(digest, &context);
  1657. *signature = estrndup((char *) digest, 16);
  1658. *signature_length = 16;
  1659. break;
  1660. }
  1661. }
  1662. phar->sig_len = phar_hex_str((const char *)*signature, *signature_length, &phar->signature);
  1663. return SUCCESS;
  1664. }
  1665. /* }}} */
  1666. void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, size_t filename_len) /* {{{ */
  1667. {
  1668. const char *s;
  1669. zend_string *str;
  1670. zval *ret;
  1671. while ((s = zend_memrchr(filename, '/', filename_len))) {
  1672. filename_len = s - filename;
  1673. if (!filename_len) {
  1674. break;
  1675. }
  1676. if (GC_FLAGS(&phar->virtual_dirs) & GC_PERSISTENT) {
  1677. str = zend_string_init_interned(filename, filename_len, 1);
  1678. } else {
  1679. str = zend_string_init(filename, filename_len, 0);
  1680. }
  1681. ret = zend_hash_add_empty_element(&phar->virtual_dirs, str);
  1682. zend_string_release(str);
  1683. if (ret == NULL) {
  1684. break;
  1685. }
  1686. }
  1687. }
  1688. /* }}} */
  1689. static int phar_update_cached_entry(zval *data, void *argument) /* {{{ */
  1690. {
  1691. phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(data);
  1692. entry->phar = (phar_archive_data *)argument;
  1693. if (entry->link) {
  1694. entry->link = estrdup(entry->link);
  1695. }
  1696. if (entry->tmp) {
  1697. entry->tmp = estrdup(entry->tmp);
  1698. }
  1699. entry->filename = estrndup(entry->filename, entry->filename_len);
  1700. entry->is_persistent = 0;
  1701. /* Replace metadata with non-persistent clones of the metadata. */
  1702. phar_metadata_tracker_clone(&entry->metadata_tracker);
  1703. return ZEND_HASH_APPLY_KEEP;
  1704. }
  1705. /* }}} */
  1706. static void phar_manifest_copy_ctor(zval *zv) /* {{{ */
  1707. {
  1708. phar_entry_info *info = emalloc(sizeof(phar_entry_info));
  1709. memcpy(info, Z_PTR_P(zv), sizeof(phar_entry_info));
  1710. Z_PTR_P(zv) = info;
  1711. }
  1712. /* }}} */
  1713. static void phar_copy_cached_phar(phar_archive_data **pphar) /* {{{ */
  1714. {
  1715. phar_archive_data *phar;
  1716. HashTable newmanifest;
  1717. char *fname;
  1718. phar_archive_object *objphar;
  1719. phar = (phar_archive_data *) emalloc(sizeof(phar_archive_data));
  1720. *phar = **pphar;
  1721. phar->is_persistent = 0;
  1722. fname = phar->fname;
  1723. phar->fname = estrndup(phar->fname, phar->fname_len);
  1724. phar->ext = phar->fname + (phar->ext - fname);
  1725. if (phar->alias) {
  1726. phar->alias = estrndup(phar->alias, phar->alias_len);
  1727. }
  1728. if (phar->signature) {
  1729. phar->signature = estrdup(phar->signature);
  1730. }
  1731. phar_metadata_tracker_clone(&phar->metadata_tracker);
  1732. zend_hash_init(&newmanifest, sizeof(phar_entry_info),
  1733. zend_get_hash_value, destroy_phar_manifest_entry, 0);
  1734. zend_hash_copy(&newmanifest, &(*pphar)->manifest, phar_manifest_copy_ctor);
  1735. zend_hash_apply_with_argument(&newmanifest, phar_update_cached_entry, (void *)phar);
  1736. phar->manifest = newmanifest;
  1737. zend_hash_init(&phar->mounted_dirs, sizeof(char *),
  1738. zend_get_hash_value, NULL, 0);
  1739. zend_hash_init(&phar->virtual_dirs, sizeof(char *),
  1740. zend_get_hash_value, NULL, 0);
  1741. zend_hash_copy(&phar->virtual_dirs, &(*pphar)->virtual_dirs, NULL);
  1742. *pphar = phar;
  1743. /* now, scan the list of persistent Phar objects referencing this phar and update the pointers */
  1744. ZEND_HASH_FOREACH_PTR(&PHAR_G(phar_persist_map), objphar) {
  1745. if (objphar->archive->fname_len == phar->fname_len && !memcmp(objphar->archive->fname, phar->fname, phar->fname_len)) {
  1746. objphar->archive = phar;
  1747. }
  1748. } ZEND_HASH_FOREACH_END();
  1749. }
  1750. /* }}} */
  1751. int phar_copy_on_write(phar_archive_data **pphar) /* {{{ */
  1752. {
  1753. zval zv, *pzv;
  1754. phar_archive_data *newpphar;
  1755. ZVAL_PTR(&zv, *pphar);
  1756. if (NULL == (pzv = zend_hash_str_add(&(PHAR_G(phar_fname_map)), (*pphar)->fname, (*pphar)->fname_len, &zv))) {
  1757. return FAILURE;
  1758. }
  1759. phar_copy_cached_phar((phar_archive_data **)&Z_PTR_P(pzv));
  1760. newpphar = Z_PTR_P(pzv);
  1761. /* invalidate phar cache */
  1762. PHAR_G(last_phar) = NULL;
  1763. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  1764. if (newpphar->alias_len && NULL == zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), newpphar->alias, newpphar->alias_len, newpphar)) {
  1765. zend_hash_str_del(&(PHAR_G(phar_fname_map)), (*pphar)->fname, (*pphar)->fname_len);
  1766. return FAILURE;
  1767. }
  1768. *pphar = newpphar;
  1769. return SUCCESS;
  1770. }
  1771. /* }}} */