util.c 54 KB

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