extract.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/myopt.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 arfd;
  100. struct stat stab;
  101. char nlc;
  102. int adminmember;
  103. bool oldformat, header_done;
  104. struct compressor *decompressor = &compressor_gzip;
  105. arfd = open(debar, O_RDONLY);
  106. if (arfd < 0)
  107. ohshite(_("failed to read archive `%.255s'"), debar);
  108. if (fstat(arfd, &stab))
  109. ohshite(_("failed to fstat archive"));
  110. r = read_line(arfd, versionbuf, strlen(DPKG_AR_MAGIC), sizeof(versionbuf));
  111. if (r < 0)
  112. read_fail(r, debar, _("archive magic version number"));
  113. if (!strcmp(versionbuf, DPKG_AR_MAGIC)) {
  114. oldformat = false;
  115. ctrllennum= 0;
  116. header_done = false;
  117. for (;;) {
  118. struct ar_hdr arh;
  119. r = fd_read(arfd, &arh, sizeof(arh));
  120. if (r != sizeof(arh))
  121. read_fail(r, debar, _("archive member header"));
  122. dpkg_ar_normalize_name(&arh);
  123. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  124. ohshit(_("file '%.250s' is corrupt - bad archive header magic"), debar);
  125. memberlen = dpkg_ar_member_get_size(debar, &arh);
  126. if (!header_done) {
  127. char *infobuf;
  128. char *cur;
  129. if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  130. ohshit(_("file `%.250s' is not a debian binary archive (try dpkg-split?)"),debar);
  131. infobuf= m_malloc(memberlen+1);
  132. r = fd_read(arfd, infobuf, memberlen + (memberlen & 1));
  133. if (r != (memberlen + (memberlen & 1)))
  134. read_fail(r, debar, _("archive information header member"));
  135. infobuf[memberlen] = '\0';
  136. cur= strchr(infobuf,'\n');
  137. if (!cur) ohshit(_("archive has no newlines in header"));
  138. *cur = '\0';
  139. cur= strchr(infobuf,'.');
  140. if (!cur) ohshit(_("archive has no dot in version number"));
  141. *cur = '\0';
  142. if (strcmp(infobuf,"2"))
  143. ohshit(_("archive version %.250s not understood, get newer dpkg-deb"), infobuf);
  144. *cur= '.';
  145. strncpy(versionbuf,infobuf,sizeof(versionbuf));
  146. versionbuf[sizeof(versionbuf) - 1] = '\0';
  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. fd_null_copy(arfd, memberlen + (memberlen & 1),
  153. _("skipped archive member data from %s"), debar);
  154. } else {
  155. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  156. adminmember = 1;
  157. else {
  158. adminmember = -1;
  159. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  160. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  161. adminmember= 0;
  162. decompressor = compressor_find_by_extension(extension);
  163. }
  164. if (adminmember == -1 || decompressor == NULL)
  165. ohshit(_("archive '%.250s' contains not understood data member %.*s, giving up"),
  166. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  167. }
  168. if (adminmember == 1) {
  169. if (ctrllennum != 0)
  170. ohshit(_("archive '%.250s' contains two control members, giving up"),
  171. debar);
  172. ctrllennum= memberlen;
  173. }
  174. if (!adminmember != !admininfo) {
  175. fd_null_copy(arfd, memberlen + (memberlen & 1),
  176. _("skipped archive member data from %s"), debar);
  177. } else {
  178. /* Yes! - found it. */
  179. break;
  180. }
  181. }
  182. }
  183. if (admininfo >= 2) {
  184. printf(_(" new debian package, version %s.\n"
  185. " size %jd bytes: control archive= %jd bytes.\n"),
  186. versionbuf, (intmax_t)stab.st_size, (intmax_t)ctrllennum);
  187. m_output(stdout, _("<standard output>"));
  188. }
  189. } else if (!strncmp(versionbuf,"0.93",4) &&
  190. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  191. nlc == '\n') {
  192. char ctrllenbuf[40];
  193. int l = 0;
  194. oldformat = true;
  195. l = strlen(versionbuf);
  196. if (l && versionbuf[l - 1] == '\n')
  197. versionbuf[l - 1] = '\0';
  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_null_copy(arfd, ctrllennum,
  209. _("skipped archive control member data from %s"), debar);
  210. }
  211. if (admininfo >= 2) {
  212. printf(_(" old debian package, version %s.\n"
  213. " size %jd bytes: control archive= %jd, main archive= %jd.\n"),
  214. versionbuf, (intmax_t)stab.st_size, (intmax_t)ctrllennum,
  215. (intmax_t)(stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  216. m_output(stdout, _("<standard output>"));
  217. }
  218. } else {
  219. if (!strncmp(versionbuf,"!<arch>",7)) {
  220. fprintf(stderr,
  221. _("dpkg-deb: file looks like it might be an archive which has been\n"
  222. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"));
  223. }
  224. ohshit(_("`%.255s' is not a debian format archive"),debar);
  225. }
  226. m_pipe(p1);
  227. c1 = subproc_fork();
  228. if (!c1) {
  229. close(p1[0]);
  230. fd_fd_copy(arfd, p1[1], memberlen, _("failed to write to pipe in copy"));
  231. if (close(p1[1]))
  232. ohshite(_("failed to close pipe in copy"));
  233. exit(0);
  234. }
  235. close(p1[1]);
  236. if (taroption) m_pipe(p2);
  237. c2 = subproc_fork();
  238. if (!c2) {
  239. m_dup2(p1[0], 0);
  240. if (admininfo) close(p1[0]);
  241. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  242. decompress_filter(decompressor, 0, 1, _("data"));
  243. }
  244. close(p1[0]);
  245. close(arfd);
  246. if (taroption) close(p2[1]);
  247. if (taroption && dir) {
  248. if (chdir(dir)) {
  249. if (errno == ENOENT) {
  250. if (mkdir(dir, 0777))
  251. ohshite(_("failed to create directory"));
  252. if (chdir(dir))
  253. ohshite(_("failed to chdir to directory after creating it"));
  254. } else {
  255. ohshite(_("failed to chdir to directory"));
  256. }
  257. }
  258. }
  259. if (taroption) {
  260. c3 = subproc_fork();
  261. if (!c3) {
  262. char buffer[30+2];
  263. if (strlen(taroption) > 30)
  264. internerr("taroption is too long '%s'", taroption);
  265. strcpy(buffer, taroption);
  266. strcat(buffer, "f");
  267. m_dup2(p2[0],0);
  268. close(p2[0]);
  269. unsetenv("TAR_OPTIONS");
  270. execlp(TAR, "tar", buffer, "-", NULL);
  271. ohshite(_("unable to execute %s (%s)"), "tar", TAR);
  272. }
  273. close(p2[0]);
  274. subproc_wait_check(c3, "tar", 0);
  275. }
  276. subproc_wait_check(c2, _("<decompress>"), PROCPIPE);
  277. if (c1 != -1)
  278. subproc_wait_check(c1, _("paste"), 0);
  279. if (oldformat && admininfo) {
  280. if (versionnum == 0.931F) {
  281. movecontrolfiles(OLDOLDDEBDIR);
  282. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  283. movecontrolfiles(OLDDEBDIR);
  284. }
  285. }
  286. }
  287. static int
  288. controlextractvextract(int admin, const char *taroptions,
  289. const char *const *argv)
  290. {
  291. const char *debar, *dir;
  292. if (!(debar= *argv++))
  293. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  294. dir = *argv++;
  295. if (!dir) {
  296. if (admin)
  297. dir = EXTRACTCONTROLDIR;
  298. else ohshit(_("--%s needs a target directory.\n"
  299. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  300. } else if (*argv) {
  301. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  302. }
  303. extracthalf(debar, dir, taroptions, admin);
  304. return 0;
  305. }
  306. int
  307. do_fsystarfile(const char *const *argv)
  308. {
  309. const char *debar;
  310. if (!(debar= *argv++))
  311. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  312. if (*argv)
  313. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  314. extracthalf(debar, NULL, NULL, 0);
  315. return 0;
  316. }
  317. int
  318. do_control(const char *const *argv)
  319. {
  320. return controlextractvextract(1, "x", argv);
  321. }
  322. int
  323. do_extract(const char *const *argv)
  324. {
  325. return controlextractvextract(0, "xp", argv);
  326. }
  327. int
  328. do_vextract(const char *const *argv)
  329. {
  330. return controlextractvextract(0, "xpv", argv);
  331. }