extract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 %.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 compression_type compress_type = GZ;
  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= GZ;
  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= BZ2;
  149. } else if (!memcmp(arh.ar_name,DATAMEMBER_CAT,sizeof(arh.ar_name)) ||
  150. !memcmp(arh.ar_name,DATAMEMBER_COMPAT_CAT,sizeof(arh.ar_name))) {
  151. adminmember= 0;
  152. compress_type= CAT;
  153. } else {
  154. ohshit(_("file `%.250s' contains ununderstood data member %.*s, giving up"),
  155. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  156. }
  157. }
  158. if (adminmember == 1) {
  159. if (ctrllennum != 0)
  160. ohshit(_("file `%.250s' contains two control members, giving up"), debar);
  161. ctrllennum= memberlen;
  162. }
  163. if (!adminmember != !admininfo) {
  164. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  165. } else {
  166. break; /* Yes ! - found it. */
  167. }
  168. }
  169. }
  170. if (admininfo >= 2)
  171. if (printf(_(" new debian package, version %s.\n"
  172. " size %ld bytes: control archive= %zi bytes.\n"),
  173. versionbuf, (long)stab.st_size, ctrllennum) == EOF ||
  174. fflush(stdout)) werr("stdout");
  175. } else if (!strncmp(versionbuf,"0.93",4) &&
  176. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  177. nlc == '\n') {
  178. oldformat= 1;
  179. l= strlen(versionbuf); if (l && versionbuf[l-1]=='\n') versionbuf[l-1]=0;
  180. if (!fgets(ctrllenbuf,sizeof(ctrllenbuf),ar))
  181. readfail(ar,debar,_("ctrl information length"));
  182. if (sscanf(ctrllenbuf,"%zi%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  183. ohshit(_("archive has malformatted ctrl len `%s'"),ctrllenbuf);
  184. if (admininfo >= 2)
  185. if (printf(_(" old debian package, version %s.\n"
  186. " size %ld bytes: control archive= %zi, main archive= %ld.\n"),
  187. versionbuf, (long)stab.st_size, ctrllennum,
  188. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l)) == EOF ||
  189. fflush(stdout)) werr("stdout");
  190. ctrlarea= malloc(ctrllennum); if (!ctrlarea) ohshite("malloc ctrlarea failed");
  191. errno=0; if (fread(ctrlarea,1,ctrllennum,ar) != ctrllennum)
  192. readfail(ar,debar,_("ctrlarea"));
  193. } else {
  194. if (!strncmp(versionbuf,"!<arch>",7)) {
  195. if (fprintf(stderr,
  196. _("dpkg-deb: file looks like it might be an archive which has been\n"
  197. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"))
  198. == EOF) werr("stderr");
  199. }
  200. ohshit(_("`%.255s' is not a debian format archive"),debar);
  201. }
  202. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  203. if(fgetpos(ar, &fpos)) ohshit(_("fgetpos failed"));
  204. #endif
  205. fflush(ar);
  206. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  207. if(fsetpos(ar, &fpos)) ohshit(_("fsetpos failed"));
  208. #endif
  209. if (oldformat) {
  210. if (admininfo) {
  211. m_pipe(p1);
  212. if (!(c1= m_fork())) {
  213. close(p1[0]);
  214. if (!(pi= fdopen(p1[1],"w"))) ohshite(_("failed to fdopen p1 in paste"));
  215. errno=0; if (fwrite(ctrlarea,1,ctrllennum,pi) != ctrllennum)
  216. ohshit(_("failed to write to gzip -dc"));
  217. if (fclose(pi)) ohshit(_("failed to close gzip -dc"));
  218. exit(0);
  219. }
  220. close(p1[1]);
  221. readfromfd= p1[0];
  222. } else {
  223. if (lseek(fileno(ar),l+strlen(ctrllenbuf)+ctrllennum,SEEK_SET) == -1)
  224. ohshite(_("failed to syscall lseek to files archive portion"));
  225. c1= -1;
  226. readfromfd= fileno(ar);
  227. }
  228. } else {
  229. m_pipe(p1);
  230. if (!(c1= m_fork())) {
  231. close(p1[0]);
  232. stream_fd_copy(ar, p1[1], memberlen, _("failed to write to pipe in copy"));
  233. if (close(p1[1]) == EOF) ohshite(_("failed to close pipe in copy"));
  234. exit(0);
  235. }
  236. close(p1[1]);
  237. readfromfd= p1[0];
  238. }
  239. if (taroption) m_pipe(p2);
  240. if (!(c2= m_fork())) {
  241. m_dup2(readfromfd,0);
  242. if (admininfo) close(p1[0]);
  243. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  244. decompress_cat(compress_type, 0, 1, _("data"));
  245. }
  246. if (readfromfd != fileno(ar)) close(readfromfd);
  247. if (taroption) close(p2[1]);
  248. if (taroption && directory) {
  249. if (chdir(directory)) {
  250. if (errno == ENOENT) {
  251. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  252. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  253. } else {
  254. ohshite(_("failed to chdir to directory"));
  255. }
  256. }
  257. }
  258. if (taroption) {
  259. if (!(c3= m_fork())) {
  260. char buffer[30+2];
  261. if(strlen(taroption) > 30) internerr(taroption);
  262. strcpy(buffer, taroption);
  263. strcat(buffer, "f");
  264. m_dup2(p2[0],0);
  265. close(p2[0]);
  266. execlp(TAR,"tar",buffer,"-",(char*)0);
  267. ohshite(_("failed to exec tar"));
  268. }
  269. close(p2[0]);
  270. waitsubproc(c3,"tar",0);
  271. }
  272. waitsubproc(c2,"<decompress>",PROCPIPE);
  273. if (c1 != -1) waitsubproc(c1,"paste",0);
  274. if (oldformat && admininfo) {
  275. if (versionnum == 0.931F) {
  276. movecontrolfiles(OLDOLDDEBDIR);
  277. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  278. movecontrolfiles(OLDDEBDIR);
  279. }
  280. }
  281. }
  282. static void controlextractvextract(int admin,
  283. const char *taroptions,
  284. const char *const *argv) {
  285. const char *debar, *directory;
  286. if (!(debar= *argv++))
  287. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  288. if (!(directory= *argv++)) {
  289. if (admin) directory= EXTRACTCONTROLDIR;
  290. else ohshit(_("--%s needs a target directory.\n"
  291. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  292. } else if (*argv) {
  293. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  294. }
  295. extracthalf(debar, directory, taroptions, admin);
  296. }
  297. void do_fsystarfile(const char *const *argv) {
  298. const char *debar;
  299. if (!(debar= *argv++))
  300. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  301. if (*argv)
  302. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  303. extracthalf(debar,0,0,0);
  304. }
  305. void do_control(const char *const *argv) { controlextractvextract(1, "x", argv); }
  306. void do_extract(const char *const *argv) { controlextractvextract(0, "xp", argv); }
  307. void do_vextract(const char *const *argv) { controlextractvextract(0, "xpv", argv); }