extract.c 12 KB

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