extract.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * extract.c - extracting archives
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <string.h>
  29. #include <dirent.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. //#include <ar.h>
  33. #include <stdbool.h>
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/fdio.h>
  40. #include <dpkg/buffer.h>
  41. #include <dpkg/subproc.h>
  42. #include <dpkg/command.h>
  43. #include <dpkg/compress.h>
  44. #include <dpkg/ar.h>
  45. #include <dpkg/deb-version.h>
  46. #include <dpkg/options.h>
  47. #include "dpkg-deb.h"
  48. static void movecontrolfiles(const char *thing) {
  49. char buf[200];
  50. pid_t pid;
  51. sprintf(buf, "mv %s/* . && rmdir %s", thing, thing);
  52. pid = subproc_fork();
  53. if (pid == 0) {
  54. command_shell(buf, _("shell command to move files"));
  55. }
  56. subproc_reap(pid, _("shell command to move files"), 0);
  57. }
  58. static void DPKG_ATTR_NORET
  59. read_fail(int rc, const char *filename, const char *what)
  60. {
  61. if (rc >= 0)
  62. ohshit(_("unexpected end of file in %s in %.255s"),what,filename);
  63. else
  64. ohshite(_("error reading %s from file %.255s"), what, filename);
  65. }
  66. static ssize_t
  67. read_line(int fd, char *buf, size_t min_size, size_t max_size)
  68. {
  69. ssize_t line_size = 0;
  70. size_t n = min_size;
  71. while (line_size < (ssize_t)max_size) {
  72. ssize_t r;
  73. char *nl;
  74. r = fd_read(fd, buf + line_size, n);
  75. if (r <= 0)
  76. return r;
  77. nl = memchr(buf + line_size, '\n', r);
  78. line_size += r;
  79. if (nl != NULL) {
  80. nl[1] = '\0';
  81. return line_size;
  82. }
  83. n = 1;
  84. }
  85. buf[line_size] = '\0';
  86. return line_size;
  87. }
  88. void
  89. extracthalf(const char *debar, const char *dir,
  90. enum dpkg_tar_options taroption, int admininfo)
  91. {
  92. struct dpkg_error err;
  93. const char *errstr;
  94. struct dpkg_ar *ar;
  95. char versionbuf[40];
  96. struct deb_version version;
  97. off_t ctrllennum, memberlen = 0;
  98. ssize_t r;
  99. int dummy;
  100. pid_t c1=0,c2,c3;
  101. int p1[2], p2[2];
  102. int p2_out;
  103. char nlc;
  104. int adminmember = -1;
  105. bool header_done;
  106. enum compressor_type decompressor = COMPRESSOR_TYPE_GZIP;
  107. ar = dpkg_ar_open(debar);
  108. r = read_line(ar->fd, versionbuf, strlen(DPKG_AR_MAGIC), sizeof(versionbuf) - 1);
  109. if (r < 0)
  110. read_fail(r, debar, _("archive magic version number"));
  111. if (strcmp(versionbuf, DPKG_AR_MAGIC) == 0) {
  112. ctrllennum= 0;
  113. header_done = false;
  114. for (;;) {
  115. struct dpkg_ar_hdr arh;
  116. r = fd_read(ar->fd, &arh, sizeof(arh));
  117. if (r != sizeof(arh))
  118. read_fail(r, debar, _("archive member header"));
  119. dpkg_ar_normalize_name(&arh);
  120. if (dpkg_ar_member_is_illegal(&arh))
  121. ohshit(_("file '%.250s' is corrupt - bad archive header magic"), debar);
  122. memberlen = dpkg_ar_member_get_size(ar, &arh);
  123. if (!header_done) {
  124. char *infobuf;
  125. if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  126. ohshit(_("file '%.250s' is not a debian binary archive (try dpkg-split?)"),
  127. debar);
  128. infobuf= m_malloc(memberlen+1);
  129. r = fd_read(ar->fd, infobuf, memberlen + (memberlen & 1));
  130. if (r != (memberlen + (memberlen & 1)))
  131. read_fail(r, debar, _("archive information header member"));
  132. infobuf[memberlen] = '\0';
  133. if (strchr(infobuf, '\n') == NULL)
  134. ohshit(_("archive has no newlines in header"));
  135. errstr = deb_version_parse(&version, infobuf);
  136. if (errstr)
  137. ohshit(_("archive has invalid format version: %s"), errstr);
  138. if (version.major != 2)
  139. ohshit(_("archive is format version %d.%d; get a newer dpkg-deb"),
  140. version.major, version.minor);
  141. free(infobuf);
  142. header_done = true;
  143. } else if (arh.ar_name[0] == '_') {
  144. /* Members with ‘_’ are noncritical, and if we don't understand
  145. * them we skip them. */
  146. if (fd_skip(ar->fd, memberlen + (memberlen & 1), &err) < 0)
  147. ohshit(_("cannot skip archive member from '%s': %s"), ar->name, err.str);
  148. } else {
  149. if (strncmp(arh.ar_name, ADMINMEMBER, strlen(ADMINMEMBER)) == 0) {
  150. const char *extension = arh.ar_name + strlen(ADMINMEMBER);
  151. adminmember = 1;
  152. decompressor = compressor_find_by_extension(extension);
  153. if (decompressor != COMPRESSOR_TYPE_NONE &&
  154. decompressor != COMPRESSOR_TYPE_GZIP &&
  155. decompressor != COMPRESSOR_TYPE_XZ)
  156. ohshit(_("archive '%s' uses unknown compression for member '%.*s', "
  157. "giving up"),
  158. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  159. } else {
  160. if (adminmember != 1)
  161. ohshit(_("archive '%s' has premature member '%.*s' before '%s', "
  162. "giving up"),
  163. debar, (int)sizeof(arh.ar_name), arh.ar_name, ADMINMEMBER);
  164. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  165. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  166. adminmember= 0;
  167. decompressor = compressor_find_by_extension(extension);
  168. if (decompressor == COMPRESSOR_TYPE_UNKNOWN)
  169. ohshit(_("archive '%s' uses unknown compression for member '%.*s', "
  170. "giving up"),
  171. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  172. } else {
  173. ohshit(_("archive '%s' has premature member '%.*s' before '%s', "
  174. "giving up"),
  175. debar, (int)sizeof(arh.ar_name), arh.ar_name, DATAMEMBER);
  176. }
  177. }
  178. if (adminmember == 1) {
  179. if (ctrllennum != 0)
  180. ohshit(_("archive '%.250s' contains two control members, giving up"),
  181. debar);
  182. ctrllennum= memberlen;
  183. }
  184. if (!adminmember != !admininfo) {
  185. if (fd_skip(ar->fd, memberlen + (memberlen & 1), &err) < 0)
  186. ohshit(_("cannot skip archive member from '%s': %s"), ar->name, err.str);
  187. } else {
  188. /* Yes! - found it. */
  189. break;
  190. }
  191. }
  192. }
  193. if (admininfo >= 2) {
  194. printf(_(" new debian package, version %d.%d.\n"
  195. " size %jd bytes: control archive=%jd bytes.\n"),
  196. version.major, version.minor,
  197. (intmax_t)ar->size, (intmax_t)ctrllennum);
  198. m_output(stdout, _("<standard output>"));
  199. }
  200. } else if (strncmp(versionbuf, "0.93", 4) == 0) {
  201. char ctrllenbuf[40];
  202. int l;
  203. l = strlen(versionbuf);
  204. if (strchr(versionbuf, '\n') == NULL)
  205. ohshit(_("archive has no newlines in header"));
  206. errstr = deb_version_parse(&version, versionbuf);
  207. if (errstr)
  208. ohshit(_("archive has invalid format version: %s"), errstr);
  209. r = read_line(ar->fd, ctrllenbuf, 1, sizeof(ctrllenbuf) - 1);
  210. if (r < 0)
  211. read_fail(r, debar, _("archive control member size"));
  212. if (sscanf(ctrllenbuf, "%jd%c%d", (intmax_t *)&ctrllennum, &nlc, &dummy) != 2 ||
  213. nlc != '\n')
  214. ohshit(_("archive has malformatted control member size '%s'"), ctrllenbuf);
  215. if (admininfo) {
  216. memberlen = ctrllennum;
  217. } else {
  218. memberlen = ar->size - ctrllennum - strlen(ctrllenbuf) - l;
  219. if (fd_skip(ar->fd, ctrllennum, &err) < 0)
  220. ohshit(_("cannot skip archive control member from '%s': %s"), ar->name,
  221. err.str);
  222. }
  223. if (admininfo >= 2) {
  224. printf(_(" old debian package, version %d.%d.\n"
  225. " size %jd bytes: control archive=%jd, main archive=%jd.\n"),
  226. version.major, version.minor,
  227. (intmax_t)ar->size, (intmax_t)ctrllennum,
  228. (intmax_t)(ar->size - ctrllennum - strlen(ctrllenbuf) - l));
  229. m_output(stdout, _("<standard output>"));
  230. }
  231. } else {
  232. if (strncmp(versionbuf, "!<arch>", 7) == 0) {
  233. notice(_("file looks like it might be an archive which has been\n"
  234. " corrupted by being downloaded in ASCII mode"));
  235. }
  236. ohshit(_("'%.255s' is not a debian format archive"), debar);
  237. }
  238. m_pipe(p1);
  239. c1 = subproc_fork();
  240. if (!c1) {
  241. close(p1[0]);
  242. if (fd_fd_copy(ar->fd, p1[1], memberlen, &err) < 0)
  243. ohshit(_("cannot copy archive member from '%s' to decompressor pipe: %s"),
  244. ar->name, err.str);
  245. if (close(p1[1]))
  246. ohshite(_("cannot close decompressor pipe"));
  247. exit(0);
  248. }
  249. close(p1[1]);
  250. if (taroption) {
  251. m_pipe(p2);
  252. p2_out = p2[1];
  253. } else {
  254. p2_out = 1;
  255. }
  256. c2 = subproc_fork();
  257. if (!c2) {
  258. if (taroption)
  259. close(p2[0]);
  260. decompress_filter(decompressor, p1[0], p2_out,
  261. _("decompressing archive member"));
  262. exit(0);
  263. }
  264. close(p1[0]);
  265. dpkg_ar_close(ar);
  266. if (taroption) close(p2[1]);
  267. if (taroption) {
  268. c3 = subproc_fork();
  269. if (!c3) {
  270. struct command cmd;
  271. command_init(&cmd, TAR, "tar");
  272. command_add_arg(&cmd, "tar");
  273. if ((taroption & DPKG_TAR_LIST) && (taroption & DPKG_TAR_EXTRACT))
  274. command_add_arg(&cmd, "-xv");
  275. else if (taroption & DPKG_TAR_EXTRACT)
  276. command_add_arg(&cmd, "-x");
  277. else if (taroption & DPKG_TAR_LIST)
  278. command_add_arg(&cmd, "-tv");
  279. else
  280. internerr("unknown or missing tar action '%d'", taroption);
  281. if (taroption & DPKG_TAR_PERMS)
  282. command_add_arg(&cmd, "-p");
  283. if (taroption & DPKG_TAR_NOMTIME)
  284. command_add_arg(&cmd, "-m");
  285. command_add_arg(&cmd, "-f");
  286. command_add_arg(&cmd, "-");
  287. command_add_arg(&cmd, "--warning=no-timestamp");
  288. m_dup2(p2[0],0);
  289. close(p2[0]);
  290. unsetenv("TAR_OPTIONS");
  291. if (dir) {
  292. if (chdir(dir)) {
  293. if (errno != ENOENT)
  294. ohshite(_("failed to chdir to directory"));
  295. if (mkdir(dir, 0777))
  296. ohshite(_("failed to create directory"));
  297. if (chdir(dir))
  298. ohshite(_("failed to chdir to directory after creating it"));
  299. }
  300. }
  301. command_exec(&cmd);
  302. }
  303. close(p2[0]);
  304. subproc_reap(c3, "tar", 0);
  305. }
  306. subproc_reap(c2, _("<decompress>"), SUBPROC_NOPIPE);
  307. if (c1 != -1)
  308. subproc_reap(c1, _("paste"), 0);
  309. if (version.major == 0 && admininfo) {
  310. /* Handle the version as a float to preserve the behaviour of old code,
  311. * because even if the format is defined to be padded by 0's that might
  312. * not have been always true for really ancient versions... */
  313. while (version.minor && (version.minor % 10) == 0)
  314. version.minor /= 10;
  315. if (version.minor == 931)
  316. movecontrolfiles(OLDOLDDEBDIR);
  317. else if (version.minor == 932 || version.minor == 933)
  318. movecontrolfiles(OLDDEBDIR);
  319. }
  320. }
  321. int
  322. do_ctrltarfile(const char *const *argv)
  323. {
  324. const char *debar;
  325. debar = *argv++;
  326. if (debar == NULL)
  327. badusage(_("--%s needs a .deb filename argument"), cipaction->olong);
  328. if (*argv)
  329. badusage(_("--%s takes only one argument (.deb filename)"),
  330. cipaction->olong);
  331. extracthalf(debar, NULL, DPKG_TAR_PASSTHROUGH, 1);
  332. return 0;
  333. }
  334. int
  335. do_fsystarfile(const char *const *argv)
  336. {
  337. const char *debar;
  338. debar = *argv++;
  339. if (debar == NULL)
  340. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  341. if (*argv)
  342. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  343. extracthalf(debar, NULL, DPKG_TAR_PASSTHROUGH, 0);
  344. return 0;
  345. }
  346. int
  347. do_control(const char *const *argv)
  348. {
  349. const char *debar, *dir;
  350. debar = *argv++;
  351. if (debar == NULL)
  352. badusage(_("--%s needs a .deb filename argument"), cipaction->olong);
  353. dir = *argv++;
  354. if (dir == NULL)
  355. dir = EXTRACTCONTROLDIR;
  356. else if (*argv)
  357. badusage(_("--%s takes at most two arguments (.deb and directory)"),
  358. cipaction->olong);
  359. extracthalf(debar, dir, DPKG_TAR_EXTRACT, 1);
  360. return 0;
  361. }
  362. int
  363. do_extract(const char *const *argv)
  364. {
  365. const char *debar, *dir;
  366. enum dpkg_tar_options options = DPKG_TAR_EXTRACT | DPKG_TAR_PERMS;
  367. if (opt_verbose)
  368. options |= DPKG_TAR_LIST;
  369. debar = *argv++;
  370. if (debar == NULL)
  371. badusage(_("--%s needs .deb filename and directory arguments"),
  372. cipaction->olong);
  373. dir = *argv++;
  374. if (dir == NULL)
  375. badusage(_("--%s needs a target directory.\n"
  376. "Perhaps you should be using dpkg --install ?"),
  377. cipaction->olong);
  378. else if (*argv)
  379. badusage(_("--%s takes at most two arguments (.deb and directory)"),
  380. cipaction->olong);
  381. extracthalf(debar, dir, options, 0);
  382. return 0;
  383. }
  384. int
  385. do_vextract(const char *const *argv)
  386. {
  387. /* XXX: Backward compatibility. */
  388. opt_verbose = 1;
  389. return do_extract(argv);
  390. }
  391. int
  392. do_raw_extract(const char *const *argv)
  393. {
  394. enum dpkg_tar_options data_options;
  395. const char *debar, *dir;
  396. char *control_dir;
  397. debar = *argv++;
  398. if (debar == NULL)
  399. badusage(_("--%s needs .deb filename and directory arguments"),
  400. cipaction->olong);
  401. else if (strcmp(debar, "-") == 0)
  402. badusage(_("--%s does not support (yet) reading the .deb from standard input"),
  403. cipaction->olong);
  404. dir = *argv++;
  405. if (dir == NULL)
  406. badusage(_("--%s needs a target directory.\n"
  407. "Perhaps you should be using dpkg --install ?"),
  408. cipaction->olong);
  409. else if (*argv)
  410. badusage(_("--%s takes at most two arguments (.deb and directory)"),
  411. cipaction->olong);
  412. control_dir = str_fmt("%s/%s", dir, EXTRACTCONTROLDIR);
  413. data_options = DPKG_TAR_EXTRACT | DPKG_TAR_PERMS;
  414. if (opt_verbose)
  415. data_options |= DPKG_TAR_LIST;
  416. extracthalf(debar, dir, data_options, 0);
  417. extracthalf(debar, control_dir, DPKG_TAR_EXTRACT, 1);
  418. free(control_dir);
  419. return 0;
  420. }