extract.c 12 KB

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