extract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <dpkg/i18n.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <signal.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <errno.h>
  32. #include <unistd.h>
  33. #include <dirent.h>
  34. #include <limits.h>
  35. #include <ctype.h>
  36. #include <assert.h>
  37. #include <ar.h>
  38. #ifdef WITH_ZLIB
  39. #include <zlib.h>
  40. #endif
  41. #include <dpkg/dpkg.h>
  42. #include <dpkg/buffer.h>
  43. #include <dpkg/subproc.h>
  44. #include <dpkg/myopt.h>
  45. #include "dpkg-deb.h"
  46. static void movecontrolfiles(const char *thing) {
  47. char buf[200];
  48. pid_t c1;
  49. sprintf(buf, "mv %s/* . && rmdir %s", thing, thing);
  50. if (!(c1= m_fork())) {
  51. execlp("sh", "sh", "-c", buf, NULL);
  52. ohshite(_("failed to exec sh -c mv foo/* &c"));
  53. }
  54. waitsubproc(c1,"sh -c mv foo/* &c",0);
  55. }
  56. static void readfail(FILE *a, const char *filename, const char *what) DPKG_ATTR_NORET;
  57. static void readfail(FILE *a, const char *filename, const char *what) {
  58. if (ferror(a)) {
  59. ohshite(_("error reading %s from file %.255s"), what, filename);
  60. } else {
  61. ohshit(_("unexpected end of file in %s in %.255s"),what,filename);
  62. }
  63. }
  64. static size_t
  65. parseheaderlength(const char *inh, size_t len,
  66. const char *fn, const char *what)
  67. {
  68. char lintbuf[15];
  69. ssize_t r;
  70. char *endp;
  71. if (memchr(inh,0,len))
  72. ohshit(_("file `%.250s' is corrupt - %.250s length contains nulls"),fn,what);
  73. assert(sizeof(lintbuf) > len);
  74. memcpy(lintbuf,inh,len);
  75. lintbuf[len]= ' ';
  76. *strchr(lintbuf, ' ') = '\0';
  77. r = strtol(lintbuf, &endp, 10);
  78. if (r < 0)
  79. ohshit(_("file `%.250s' is corrupt - negative member length %zi"), fn, r);
  80. if (*endp)
  81. ohshit(_("file `%.250s' is corrupt - bad digit (code %d) in %s"),fn,*endp,what);
  82. return (size_t)r;
  83. }
  84. static void
  85. safe_fflush(FILE *f)
  86. {
  87. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  88. /* XXX: Glibc 2.1 and some versions of Linux want to make fflush()
  89. * move the current fpos. Remove this code some time. */
  90. fpos_t fpos;
  91. if (fgetpos(f, &fpos))
  92. ohshit(_("failed getting the current file position"));
  93. fflush(f);
  94. if (fsetpos(f, &fpos))
  95. ohshit(_("failed setting the current file position"));
  96. #else
  97. fflush(f);
  98. #endif
  99. }
  100. void extracthalf(const char *debar, const char *directory,
  101. const char *taroption, int admininfo) {
  102. char versionbuf[40];
  103. float versionnum;
  104. char ctrllenbuf[40], *infobuf;
  105. size_t ctrllennum, memberlen= 0;
  106. int dummy, l= 0;
  107. pid_t c1=0,c2,c3;
  108. void *ctrlarea = NULL;
  109. int p1[2], p2[2];
  110. FILE *ar, *pi;
  111. struct stat stab;
  112. char nlc;
  113. char *cur;
  114. struct ar_hdr arh;
  115. int readfromfd, oldformat= 0, header_done, adminmember;
  116. enum compress_type compress_type = compress_type_gzip;
  117. ar= fopen(debar,"r"); if (!ar) ohshite(_("failed to read archive `%.255s'"),debar);
  118. if (fstat(fileno(ar),&stab)) ohshite(_("failed to fstat archive"));
  119. if (!fgets(versionbuf,sizeof(versionbuf),ar)) readfail(ar,debar,_("version number"));
  120. if (!strcmp(versionbuf,"!<arch>\n")) {
  121. oldformat= 0;
  122. ctrllennum= 0;
  123. header_done= 0;
  124. for (;;) {
  125. if (fread(&arh,1,sizeof(arh),ar) != sizeof(arh))
  126. readfail(ar,debar,_("between members"));
  127. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  128. ohshit(_("file `%.250s' is corrupt - bad magic at end of first header"),debar);
  129. memberlen= parseheaderlength(arh.ar_size,sizeof(arh.ar_size),
  130. debar, _("member length"));
  131. if (!header_done) {
  132. if (memcmp(arh.ar_name,"debian-binary ",sizeof(arh.ar_name)) &&
  133. memcmp(arh.ar_name,"debian-binary/ ",sizeof(arh.ar_name)))
  134. ohshit(_("file `%.250s' is not a debian binary archive (try dpkg-split?)"),debar);
  135. infobuf= m_malloc(memberlen+1);
  136. if (fread(infobuf,1, memberlen + (memberlen&1), ar) != memberlen + (memberlen&1))
  137. readfail(ar,debar,_("header info member"));
  138. infobuf[memberlen] = '\0';
  139. cur= strchr(infobuf,'\n');
  140. if (!cur) ohshit(_("archive has no newlines in header"));
  141. *cur = '\0';
  142. cur= strchr(infobuf,'.');
  143. if (!cur) ohshit(_("archive has no dot in version number"));
  144. *cur = '\0';
  145. if (strcmp(infobuf,"2"))
  146. ohshit(_("archive version %.250s not understood, get newer dpkg-deb"), infobuf);
  147. *cur= '.';
  148. strncpy(versionbuf,infobuf,sizeof(versionbuf));
  149. versionbuf[sizeof(versionbuf) - 1] = '\0';
  150. header_done= 1;
  151. } else if (arh.ar_name[0] == '_') {
  152. /* Members with `_' are noncritical, and if we don't understand them
  153. * we skip them.
  154. */
  155. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  156. } else {
  157. adminmember=
  158. (!memcmp(arh.ar_name,ADMINMEMBER,sizeof(arh.ar_name)) ||
  159. !memcmp(arh.ar_name,ADMINMEMBER_COMPAT,sizeof(arh.ar_name))) ? 1 : -1;
  160. if (adminmember == -1) {
  161. if (!memcmp(arh.ar_name,DATAMEMBER_GZ,sizeof(arh.ar_name)) ||
  162. !memcmp(arh.ar_name,DATAMEMBER_COMPAT_GZ,sizeof(arh.ar_name))) {
  163. adminmember= 0;
  164. compress_type = compress_type_gzip;
  165. } else if (!memcmp(arh.ar_name,DATAMEMBER_BZ2,sizeof(arh.ar_name)) ||
  166. !memcmp(arh.ar_name,DATAMEMBER_COMPAT_BZ2,sizeof(arh.ar_name))) {
  167. adminmember= 0;
  168. compress_type = compress_type_bzip2;
  169. } else if (!memcmp(arh.ar_name, DATAMEMBER_LZMA, sizeof(arh.ar_name)) ||
  170. !memcmp(arh.ar_name, DATAMEMBER_COMPAT_LZMA, sizeof(arh.ar_name))) {
  171. adminmember = 0;
  172. compress_type = compress_type_lzma;
  173. } else if (!memcmp(arh.ar_name,DATAMEMBER_CAT,sizeof(arh.ar_name)) ||
  174. !memcmp(arh.ar_name,DATAMEMBER_COMPAT_CAT,sizeof(arh.ar_name))) {
  175. adminmember= 0;
  176. compress_type = compress_type_cat;
  177. } else {
  178. ohshit(_("file `%.250s' contains ununderstood data member %.*s, giving up"),
  179. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  180. }
  181. }
  182. if (adminmember == 1) {
  183. if (ctrllennum != 0)
  184. ohshit(_("file `%.250s' contains two control members, giving up"), debar);
  185. ctrllennum= memberlen;
  186. }
  187. if (!adminmember != !admininfo) {
  188. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  189. } else {
  190. break; /* Yes ! - found it. */
  191. }
  192. }
  193. }
  194. if (admininfo >= 2) {
  195. printf(_(" new debian package, version %s.\n"
  196. " size %ld bytes: control archive= %zi bytes.\n"),
  197. versionbuf, (long)stab.st_size, ctrllennum);
  198. m_output(stdout, _("<standard output>"));
  199. }
  200. } else if (!strncmp(versionbuf,"0.93",4) &&
  201. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  202. nlc == '\n') {
  203. oldformat= 1;
  204. l = strlen(versionbuf);
  205. if (l && versionbuf[l - 1] == '\n')
  206. versionbuf[l - 1] = '\0';
  207. if (!fgets(ctrllenbuf,sizeof(ctrllenbuf),ar))
  208. readfail(ar, debar, _("control information length"));
  209. if (sscanf(ctrllenbuf,"%zi%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  210. ohshit(_("archive has malformatted control length `%s'"), ctrllenbuf);
  211. if (admininfo >= 2) {
  212. printf(_(" old debian package, version %s.\n"
  213. " size %ld bytes: control archive= %zi, main archive= %ld.\n"),
  214. versionbuf, (long)stab.st_size, ctrllennum,
  215. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  216. m_output(stdout, _("<standard output>"));
  217. }
  218. ctrlarea = m_malloc(ctrllennum);
  219. errno=0; if (fread(ctrlarea,1,ctrllennum,ar) != ctrllennum)
  220. readfail(ar, debar, _("control area"));
  221. } else {
  222. if (!strncmp(versionbuf,"!<arch>",7)) {
  223. fprintf(stderr,
  224. _("dpkg-deb: file looks like it might be an archive which has been\n"
  225. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"));
  226. }
  227. ohshit(_("`%.255s' is not a debian format archive"),debar);
  228. }
  229. safe_fflush(ar);
  230. if (oldformat) {
  231. if (admininfo) {
  232. m_pipe(p1);
  233. if (!(c1= m_fork())) {
  234. close(p1[0]);
  235. pi = fdopen(p1[1], "w");
  236. if (!pi)
  237. ohshite(_("failed to open pipe descriptor `1' in paste"));
  238. errno=0; if (fwrite(ctrlarea,1,ctrllennum,pi) != ctrllennum)
  239. ohshit(_("failed to write to gzip -dc"));
  240. if (fclose(pi)) ohshit(_("failed to close gzip -dc"));
  241. exit(0);
  242. }
  243. close(p1[1]);
  244. readfromfd= p1[0];
  245. } else {
  246. if (lseek(fileno(ar),l+strlen(ctrllenbuf)+ctrllennum,SEEK_SET) == -1)
  247. ohshite(_("failed to syscall lseek to files archive portion"));
  248. c1= -1;
  249. readfromfd= fileno(ar);
  250. }
  251. } else {
  252. m_pipe(p1);
  253. if (!(c1= m_fork())) {
  254. close(p1[0]);
  255. stream_fd_copy(ar, p1[1], memberlen, _("failed to write to pipe in copy"));
  256. if (close(p1[1]) == EOF) ohshite(_("failed to close pipe in copy"));
  257. exit(0);
  258. }
  259. close(p1[1]);
  260. readfromfd= p1[0];
  261. }
  262. if (taroption) m_pipe(p2);
  263. if (!(c2= m_fork())) {
  264. m_dup2(readfromfd,0);
  265. if (admininfo) close(p1[0]);
  266. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  267. decompress_cat(compress_type, 0, 1, _("data"));
  268. }
  269. if (readfromfd != fileno(ar)) close(readfromfd);
  270. if (taroption) close(p2[1]);
  271. if (taroption && directory) {
  272. if (chdir(directory)) {
  273. if (errno == ENOENT) {
  274. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  275. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  276. } else {
  277. ohshite(_("failed to chdir to directory"));
  278. }
  279. }
  280. }
  281. if (taroption) {
  282. if (!(c3= m_fork())) {
  283. char buffer[30+2];
  284. if (strlen(taroption) > 30)
  285. internerr("taroption is too long '%s'", taroption);
  286. strcpy(buffer, taroption);
  287. strcat(buffer, "f");
  288. m_dup2(p2[0],0);
  289. close(p2[0]);
  290. unsetenv("TAR_OPTIONS");
  291. execlp(TAR, "tar", buffer, "-", NULL);
  292. ohshite(_("failed to exec tar"));
  293. }
  294. close(p2[0]);
  295. waitsubproc(c3,"tar",0);
  296. }
  297. waitsubproc(c2, _("<decompress>"), PROCPIPE);
  298. if (c1 != -1)
  299. waitsubproc(c1, _("paste"), 0);
  300. if (oldformat && admininfo) {
  301. if (versionnum == 0.931F) {
  302. movecontrolfiles(OLDOLDDEBDIR);
  303. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  304. movecontrolfiles(OLDDEBDIR);
  305. }
  306. }
  307. }
  308. static void controlextractvextract(int admin,
  309. const char *taroptions,
  310. const char *const *argv) {
  311. const char *debar, *directory;
  312. if (!(debar= *argv++))
  313. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  314. if (!(directory= *argv++)) {
  315. if (admin) directory= EXTRACTCONTROLDIR;
  316. else ohshit(_("--%s needs a target directory.\n"
  317. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  318. } else if (*argv) {
  319. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  320. }
  321. extracthalf(debar, directory, taroptions, admin);
  322. }
  323. void do_fsystarfile(const char *const *argv) {
  324. const char *debar;
  325. if (!(debar= *argv++))
  326. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  327. if (*argv)
  328. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  329. extracthalf(debar, NULL, NULL, 0);
  330. }
  331. void do_control(const char *const *argv) { controlextractvextract(1, "x", argv); }
  332. void do_extract(const char *const *argv) { controlextractvextract(0, "xp", argv); }
  333. void do_vextract(const char *const *argv) { controlextractvextract(0, "xpv", argv); }