extract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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-2012 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 <http://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, const char *taroption,
  92. int admininfo)
  93. {
  94. const char *err;
  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;
  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. err = deb_version_parse(&version, infobuf);
  141. if (err)
  142. ohshit(_("archive has invalid format version: %s"), err);
  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. fd_skip(arfd, memberlen + (memberlen & 1),
  152. _("skipped archive member data from %s"), debar);
  153. } else {
  154. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  155. adminmember = 1;
  156. else {
  157. adminmember = -1;
  158. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  159. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  160. adminmember= 0;
  161. decompressor = compressor_find_by_extension(extension);
  162. }
  163. if (adminmember == -1 || decompressor == compressor_type_unknown)
  164. ohshit(_("archive '%.250s' contains not understood data member %.*s, giving up"),
  165. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  166. }
  167. if (adminmember == 1) {
  168. if (ctrllennum != 0)
  169. ohshit(_("archive '%.250s' contains two control members, giving up"),
  170. debar);
  171. ctrllennum= memberlen;
  172. }
  173. if (!adminmember != !admininfo) {
  174. fd_skip(arfd, memberlen + (memberlen & 1),
  175. _("skipped archive member data from %s"), debar);
  176. } else {
  177. /* Yes! - found it. */
  178. break;
  179. }
  180. }
  181. }
  182. if (admininfo >= 2) {
  183. printf(_(" new debian package, version %d.%d.\n"
  184. " size %jd bytes: control archive=%jd bytes.\n"),
  185. version.major, version.minor,
  186. (intmax_t)stab.st_size, (intmax_t)ctrllennum);
  187. m_output(stdout, _("<standard output>"));
  188. }
  189. } else if (strncmp(versionbuf, "0.93", 4) == 0) {
  190. char ctrllenbuf[40];
  191. int l = 0;
  192. l = strlen(versionbuf);
  193. if (strchr(versionbuf, '\n') == NULL)
  194. ohshit(_("archive has no newlines in header"));
  195. err = deb_version_parse(&version, versionbuf);
  196. if (err)
  197. ohshit(_("archive has invalid format version: %s"), err);
  198. r = read_line(arfd, ctrllenbuf, 1, sizeof(ctrllenbuf));
  199. if (r < 0)
  200. read_fail(r, debar, _("archive control member size"));
  201. if (sscanf(ctrllenbuf, "%jd%c%d", &ctrllennum, &nlc, &dummy) != 2 ||
  202. nlc != '\n')
  203. ohshit(_("archive has malformatted control member size '%s'"), ctrllenbuf);
  204. if (admininfo) {
  205. memberlen = ctrllennum;
  206. } else {
  207. memberlen = stab.st_size - ctrllennum - strlen(ctrllenbuf) - l;
  208. fd_skip(arfd, ctrllennum,
  209. _("skipped archive control member data from %s"), debar);
  210. }
  211. if (admininfo >= 2) {
  212. printf(_(" old debian package, version %d.%d.\n"
  213. " size %jd bytes: control archive=%jd, main archive=%jd.\n"),
  214. version.major, version.minor,
  215. (intmax_t)stab.st_size, (intmax_t)ctrllennum,
  216. (intmax_t)(stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  217. m_output(stdout, _("<standard output>"));
  218. }
  219. } else {
  220. if (strncmp(versionbuf, "!<arch>", 7) == 0) {
  221. fprintf(stderr,
  222. _("dpkg-deb: file looks like it might be an archive which has been\n"
  223. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"));
  224. }
  225. ohshit(_("`%.255s' is not a debian format archive"),debar);
  226. }
  227. m_pipe(p1);
  228. c1 = subproc_fork();
  229. if (!c1) {
  230. close(p1[0]);
  231. fd_fd_copy(arfd, p1[1], memberlen, _("failed to write to pipe in copy"));
  232. if (close(p1[1]))
  233. ohshite(_("failed to close pipe in copy"));
  234. exit(0);
  235. }
  236. close(p1[1]);
  237. if (taroption) {
  238. m_pipe(p2);
  239. p2_out = p2[1];
  240. } else {
  241. p2_out = 1;
  242. }
  243. c2 = subproc_fork();
  244. if (!c2) {
  245. if (taroption)
  246. close(p2[0]);
  247. decompress_filter(decompressor, p1[0], p2_out, _("data"));
  248. exit(0);
  249. }
  250. close(p1[0]);
  251. close(arfd);
  252. if (taroption) close(p2[1]);
  253. if (taroption) {
  254. c3 = subproc_fork();
  255. if (!c3) {
  256. char buffer[30+2];
  257. if (strlen(taroption) > 30)
  258. internerr("taroption is too long '%s'", taroption);
  259. strcpy(buffer, taroption);
  260. strcat(buffer, "f");
  261. m_dup2(p2[0],0);
  262. close(p2[0]);
  263. unsetenv("TAR_OPTIONS");
  264. if (dir) {
  265. if (chdir(dir)) {
  266. if (errno != ENOENT)
  267. ohshite(_("failed to chdir to directory"));
  268. if (mkdir(dir, 0777))
  269. ohshite(_("failed to create directory"));
  270. if (chdir(dir))
  271. ohshite(_("failed to chdir to directory after creating it"));
  272. }
  273. }
  274. execlp(TAR, "tar", buffer, "-", "--warning=no-timestamp", NULL);
  275. ohshite(_("unable to execute %s (%s)"), "tar", TAR);
  276. }
  277. close(p2[0]);
  278. subproc_wait_check(c3, "tar", 0);
  279. }
  280. subproc_wait_check(c2, _("<decompress>"), PROCPIPE);
  281. if (c1 != -1)
  282. subproc_wait_check(c1, _("paste"), 0);
  283. if (version.major == 0 && admininfo) {
  284. /* Handle the version as a float to preserve the behaviour of old code,
  285. * because even if the format is defined to be padded by 0's that might
  286. * not have been always true for really ancient versions... */
  287. while (version.minor && (version.minor % 10) == 0)
  288. version.minor /= 10;
  289. if (version.minor == 931)
  290. movecontrolfiles(OLDOLDDEBDIR);
  291. else if (version.minor == 932 || version.minor == 933)
  292. movecontrolfiles(OLDDEBDIR);
  293. }
  294. }
  295. static int
  296. controlextractvextract(int admin, const char *taroptions,
  297. const char *const *argv)
  298. {
  299. const char *debar, *dir;
  300. if (!(debar= *argv++))
  301. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  302. dir = *argv++;
  303. if (!dir) {
  304. if (admin)
  305. dir = EXTRACTCONTROLDIR;
  306. else ohshit(_("--%s needs a target directory.\n"
  307. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  308. } else if (*argv) {
  309. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  310. }
  311. extracthalf(debar, dir, taroptions, admin);
  312. return 0;
  313. }
  314. int
  315. do_fsystarfile(const char *const *argv)
  316. {
  317. const char *debar;
  318. if (!(debar= *argv++))
  319. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  320. if (*argv)
  321. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  322. extracthalf(debar, NULL, NULL, 0);
  323. return 0;
  324. }
  325. int
  326. do_control(const char *const *argv)
  327. {
  328. return controlextractvextract(1, "x", argv);
  329. }
  330. int
  331. do_extract(const char *const *argv)
  332. {
  333. if (opt_verbose)
  334. return controlextractvextract(0, "xpv", argv);
  335. else
  336. return controlextractvextract(0, "xp", argv);
  337. }
  338. int
  339. do_vextract(const char *const *argv)
  340. {
  341. /* XXX: Backward compatibility. */
  342. opt_verbose = 1;
  343. return do_extract(argv);
  344. }
  345. int
  346. do_raw_extract(const char *const *argv)
  347. {
  348. const char *debar, *dir;
  349. char *control_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. badusage(_("--%s needs a target directory.\n"
  356. "Perhaps you should be using dpkg --install ?"),
  357. cipaction->olong);
  358. else if (*argv)
  359. badusage(_("--%s takes at most two arguments (.deb and directory)"),
  360. cipaction->olong);
  361. m_asprintf(&control_dir, "%s/%s", dir, EXTRACTCONTROLDIR);
  362. if (opt_verbose)
  363. extracthalf(debar, dir, "xpv", 0);
  364. else
  365. extracthalf(debar, dir, "xp", 0);
  366. extracthalf(debar, control_dir, "x", 1);
  367. free(control_dir);
  368. return 0;
  369. }