extract.c 14 KB

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