extract.c 12 KB

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