extract.c 14 KB

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