extract.c 12 KB

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