extract.c 14 KB

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