extract.c 12 KB

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