extract.c 14 KB

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