extract.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/wait.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <ctype.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/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_wait_check(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 = strchr(buf + line_size, '\n');
  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, const char *taroption,
  90. int admininfo)
  91. {
  92. char versionbuf[40];
  93. float versionnum;
  94. off_t ctrllennum, memberlen = 0;
  95. ssize_t r;
  96. int dummy;
  97. pid_t c1=0,c2,c3;
  98. int p1[2], p2[2];
  99. int p2_out;
  100. int arfd;
  101. struct stat stab;
  102. char nlc;
  103. int adminmember;
  104. bool oldformat, header_done;
  105. struct compressor *decompressor = &compressor_gzip;
  106. arfd = open(debar, O_RDONLY);
  107. if (arfd < 0)
  108. ohshite(_("failed to read archive `%.255s'"), debar);
  109. if (fstat(arfd, &stab))
  110. ohshite(_("failed to fstat archive"));
  111. r = read_line(arfd, versionbuf, strlen(DPKG_AR_MAGIC), sizeof(versionbuf));
  112. if (r < 0)
  113. read_fail(r, debar, _("archive magic version number"));
  114. if (!strcmp(versionbuf, DPKG_AR_MAGIC)) {
  115. oldformat = false;
  116. ctrllennum= 0;
  117. header_done = false;
  118. for (;;) {
  119. struct ar_hdr arh;
  120. r = fd_read(arfd, &arh, sizeof(arh));
  121. if (r != sizeof(arh))
  122. read_fail(r, debar, _("archive member header"));
  123. dpkg_ar_normalize_name(&arh);
  124. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  125. ohshit(_("file '%.250s' is corrupt - bad archive header magic"), debar);
  126. memberlen = dpkg_ar_member_get_size(debar, &arh);
  127. if (!header_done) {
  128. char *infobuf;
  129. char *cur;
  130. if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  131. ohshit(_("file `%.250s' is not a debian binary archive (try dpkg-split?)"),debar);
  132. infobuf= m_malloc(memberlen+1);
  133. r = fd_read(arfd, infobuf, memberlen + (memberlen & 1));
  134. if (r != (memberlen + (memberlen & 1)))
  135. read_fail(r, debar, _("archive information header member"));
  136. infobuf[memberlen] = '\0';
  137. cur= strchr(infobuf,'\n');
  138. if (!cur) ohshit(_("archive has no newlines in header"));
  139. *cur = '\0';
  140. cur= strchr(infobuf,'.');
  141. if (!cur) ohshit(_("archive has no dot in version number"));
  142. *cur = '\0';
  143. if (strcmp(infobuf,"2"))
  144. ohshit(_("archive version %.250s not understood, get newer dpkg-deb"), infobuf);
  145. *cur= '.';
  146. strncpy(versionbuf,infobuf,sizeof(versionbuf));
  147. versionbuf[sizeof(versionbuf) - 1] = '\0';
  148. free(infobuf);
  149. header_done = true;
  150. } else if (arh.ar_name[0] == '_') {
  151. /* Members with ‘_’ are noncritical, and if we don't understand
  152. * them we skip them. */
  153. fd_null_copy(arfd, memberlen + (memberlen & 1),
  154. _("skipped archive member data from %s"), debar);
  155. } else {
  156. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  157. adminmember = 1;
  158. else {
  159. adminmember = -1;
  160. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  161. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  162. adminmember= 0;
  163. decompressor = compressor_find_by_extension(extension);
  164. }
  165. if (adminmember == -1 || decompressor == NULL)
  166. ohshit(_("archive '%.250s' contains not understood data member %.*s, giving up"),
  167. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  168. }
  169. if (adminmember == 1) {
  170. if (ctrllennum != 0)
  171. ohshit(_("archive '%.250s' contains two control members, giving up"),
  172. debar);
  173. ctrllennum= memberlen;
  174. }
  175. if (!adminmember != !admininfo) {
  176. fd_null_copy(arfd, memberlen + (memberlen & 1),
  177. _("skipped archive member data from %s"), debar);
  178. } else {
  179. /* Yes! - found it. */
  180. break;
  181. }
  182. }
  183. }
  184. if (admininfo >= 2) {
  185. printf(_(" new debian package, version %s.\n"
  186. " size %jd bytes: control archive= %jd bytes.\n"),
  187. versionbuf, (intmax_t)stab.st_size, (intmax_t)ctrllennum);
  188. m_output(stdout, _("<standard output>"));
  189. }
  190. } else if (!strncmp(versionbuf,"0.93",4) &&
  191. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  192. nlc == '\n') {
  193. char ctrllenbuf[40];
  194. int l = 0;
  195. oldformat = true;
  196. l = strlen(versionbuf);
  197. if (l && versionbuf[l - 1] == '\n')
  198. versionbuf[l - 1] = '\0';
  199. r = read_line(arfd, ctrllenbuf, 1, sizeof(ctrllenbuf));
  200. if (r < 0)
  201. read_fail(r, debar, _("archive control member size"));
  202. if (sscanf(ctrllenbuf, "%jd%c%d", &ctrllennum, &nlc, &dummy) != 2 ||
  203. nlc != '\n')
  204. ohshit(_("archive has malformatted control member size '%s'"), ctrllenbuf);
  205. if (admininfo) {
  206. memberlen = ctrllennum;
  207. } else {
  208. memberlen = stab.st_size - ctrllennum - strlen(ctrllenbuf) - l;
  209. fd_null_copy(arfd, ctrllennum,
  210. _("skipped archive control member data from %s"), debar);
  211. }
  212. if (admininfo >= 2) {
  213. printf(_(" old debian package, version %s.\n"
  214. " size %jd bytes: control archive= %jd, main archive= %jd.\n"),
  215. versionbuf, (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)) {
  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, "-", 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 (oldformat && admininfo) {
  284. if (versionnum == 0.931F) {
  285. movecontrolfiles(OLDOLDDEBDIR);
  286. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  287. movecontrolfiles(OLDDEBDIR);
  288. }
  289. }
  290. }
  291. static int
  292. controlextractvextract(int admin, const char *taroptions,
  293. const char *const *argv)
  294. {
  295. const char *debar, *dir;
  296. if (!(debar= *argv++))
  297. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  298. dir = *argv++;
  299. if (!dir) {
  300. if (admin)
  301. dir = EXTRACTCONTROLDIR;
  302. else ohshit(_("--%s needs a target directory.\n"
  303. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  304. } else if (*argv) {
  305. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  306. }
  307. extracthalf(debar, dir, taroptions, admin);
  308. return 0;
  309. }
  310. int
  311. do_fsystarfile(const char *const *argv)
  312. {
  313. const char *debar;
  314. if (!(debar= *argv++))
  315. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  316. if (*argv)
  317. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  318. extracthalf(debar, NULL, NULL, 0);
  319. return 0;
  320. }
  321. int
  322. do_control(const char *const *argv)
  323. {
  324. return controlextractvextract(1, "x", argv);
  325. }
  326. int
  327. do_extract(const char *const *argv)
  328. {
  329. if (opt_verbose)
  330. return controlextractvextract(0, "xpv", argv);
  331. else
  332. return controlextractvextract(0, "xp", argv);
  333. }
  334. int
  335. do_vextract(const char *const *argv)
  336. {
  337. /* XXX: Backward compatibility. */
  338. opt_verbose = 1;
  339. return do_extract(argv);
  340. }
  341. int
  342. do_raw_extract(const char *const *argv)
  343. {
  344. const char *debar, *dir;
  345. char *control_dir;
  346. debar = *argv++;
  347. if (debar == NULL)
  348. badusage(_("--%s needs a .deb filename argument"), cipaction->olong);
  349. dir = *argv++;
  350. if (dir == NULL)
  351. badusage(_("--%s needs a target directory.\n"
  352. "Perhaps you should be using dpkg --install ?"),
  353. cipaction->olong);
  354. else if (*argv)
  355. badusage(_("--%s takes at most two arguments (.deb and directory)"),
  356. cipaction->olong);
  357. m_asprintf(&control_dir, "%s/%s", dir, EXTRACTCONTROLDIR);
  358. if (opt_verbose)
  359. extracthalf(debar, dir, "xpv", 0);
  360. else
  361. extracthalf(debar, dir, "xp", 0);
  362. extracthalf(debar, control_dir, "x", 1);
  363. free(control_dir);
  364. return 0;
  365. }