extract.c 12 KB

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