extract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <unistd.h>
  32. #include <ar.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/buffer.h>
  38. #include <dpkg/subproc.h>
  39. #include <dpkg/compress.h>
  40. #include <dpkg/ar.h>
  41. #include <dpkg/myopt.h>
  42. #include "dpkg-deb.h"
  43. static void movecontrolfiles(const char *thing) {
  44. char buf[200];
  45. pid_t c1;
  46. sprintf(buf, "mv %s/* . && rmdir %s", thing, thing);
  47. c1 = subproc_fork();
  48. if (!c1) {
  49. execlp("sh", "sh", "-c", buf, NULL);
  50. ohshite(_("failed to exec sh -c mv foo/* &c"));
  51. }
  52. subproc_wait_check(c1, "sh -c mv foo/* &c", 0);
  53. }
  54. static void DPKG_ATTR_NORET
  55. readfail(FILE *a, const char *filename, const char *what)
  56. {
  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. struct compressor *decompressor = &compressor_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. dpkg_ar_normalize_name(&arh);
  127. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  128. ohshit(_("file `%.250s' is corrupt - bad magic at end of first header"),debar);
  129. memberlen= parseheaderlength(arh.ar_size,sizeof(arh.ar_size),
  130. debar, _("member length"));
  131. if (!header_done) {
  132. if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  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. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  157. adminmember = 1;
  158. else {
  159. adminmember = -1;
  160. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  161. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  162. adminmember= 0;
  163. decompressor = compressor_find_by_extension(extension);
  164. }
  165. if (adminmember == -1 || decompressor == NULL)
  166. ohshit(_("file `%.250s' contains ununderstood data member %.*s, giving up"),
  167. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  168. }
  169. if (adminmember == 1) {
  170. if (ctrllennum != 0)
  171. ohshit(_("file `%.250s' contains two control members, giving up"), debar);
  172. ctrllennum= memberlen;
  173. }
  174. if (!adminmember != !admininfo) {
  175. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  176. } else {
  177. break; /* Yes ! - found it. */
  178. }
  179. }
  180. }
  181. if (admininfo >= 2) {
  182. printf(_(" new debian package, version %s.\n"
  183. " size %ld bytes: control archive= %zi bytes.\n"),
  184. versionbuf, (long)stab.st_size, ctrllennum);
  185. m_output(stdout, _("<standard output>"));
  186. }
  187. } else if (!strncmp(versionbuf,"0.93",4) &&
  188. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  189. nlc == '\n') {
  190. oldformat= 1;
  191. l = strlen(versionbuf);
  192. if (l && versionbuf[l - 1] == '\n')
  193. versionbuf[l - 1] = '\0';
  194. if (!fgets(ctrllenbuf,sizeof(ctrllenbuf),ar))
  195. readfail(ar, debar, _("control information length"));
  196. if (sscanf(ctrllenbuf,"%zi%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  197. ohshit(_("archive has malformatted control length `%s'"), ctrllenbuf);
  198. if (admininfo >= 2) {
  199. printf(_(" old debian package, version %s.\n"
  200. " size %ld bytes: control archive= %zi, main archive= %ld.\n"),
  201. versionbuf, (long)stab.st_size, ctrllennum,
  202. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  203. m_output(stdout, _("<standard output>"));
  204. }
  205. ctrlarea = m_malloc(ctrllennum);
  206. errno=0; if (fread(ctrlarea,1,ctrllennum,ar) != ctrllennum)
  207. readfail(ar, debar, _("control area"));
  208. } else {
  209. if (!strncmp(versionbuf,"!<arch>",7)) {
  210. fprintf(stderr,
  211. _("dpkg-deb: file looks like it might be an archive which has been\n"
  212. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"));
  213. }
  214. ohshit(_("`%.255s' is not a debian format archive"),debar);
  215. }
  216. safe_fflush(ar);
  217. if (oldformat) {
  218. if (admininfo) {
  219. m_pipe(p1);
  220. c1 = subproc_fork();
  221. if (!c1) {
  222. close(p1[0]);
  223. pi = fdopen(p1[1], "w");
  224. if (!pi)
  225. ohshite(_("failed to open pipe descriptor `1' in paste"));
  226. errno=0; if (fwrite(ctrlarea,1,ctrllennum,pi) != ctrllennum)
  227. ohshit(_("failed to write to gzip -dc"));
  228. if (fclose(pi)) ohshit(_("failed to close gzip -dc"));
  229. exit(0);
  230. }
  231. close(p1[1]);
  232. readfromfd= p1[0];
  233. } else {
  234. if (lseek(fileno(ar),l+strlen(ctrllenbuf)+ctrllennum,SEEK_SET) == -1)
  235. ohshite(_("failed to syscall lseek to files archive portion"));
  236. c1= -1;
  237. readfromfd= fileno(ar);
  238. }
  239. } else {
  240. m_pipe(p1);
  241. c1 = subproc_fork();
  242. if (!c1) {
  243. close(p1[0]);
  244. stream_fd_copy(ar, p1[1], memberlen, _("failed to write to pipe in copy"));
  245. if (close(p1[1]) == EOF) ohshite(_("failed to close pipe in copy"));
  246. exit(0);
  247. }
  248. close(p1[1]);
  249. readfromfd= p1[0];
  250. }
  251. if (taroption) m_pipe(p2);
  252. c2 = subproc_fork();
  253. if (!c2) {
  254. m_dup2(readfromfd,0);
  255. if (admininfo) close(p1[0]);
  256. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  257. decompress_filter(decompressor, 0, 1, _("data"));
  258. }
  259. if (readfromfd != fileno(ar)) close(readfromfd);
  260. if (taroption) close(p2[1]);
  261. if (taroption && directory) {
  262. if (chdir(directory)) {
  263. if (errno == ENOENT) {
  264. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  265. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  266. } else {
  267. ohshite(_("failed to chdir to directory"));
  268. }
  269. }
  270. }
  271. if (taroption) {
  272. c3 = subproc_fork();
  273. if (!c3) {
  274. char buffer[30+2];
  275. if (strlen(taroption) > 30)
  276. internerr("taroption is too long '%s'", taroption);
  277. strcpy(buffer, taroption);
  278. strcat(buffer, "f");
  279. m_dup2(p2[0],0);
  280. close(p2[0]);
  281. unsetenv("TAR_OPTIONS");
  282. execlp(TAR, "tar", buffer, "-", NULL);
  283. ohshite(_("failed to exec tar"));
  284. }
  285. close(p2[0]);
  286. subproc_wait_check(c3, "tar", 0);
  287. }
  288. subproc_wait_check(c2, _("<decompress>"), PROCPIPE);
  289. if (c1 != -1)
  290. subproc_wait_check(c1, _("paste"), 0);
  291. if (oldformat && admininfo) {
  292. if (versionnum == 0.931F) {
  293. movecontrolfiles(OLDOLDDEBDIR);
  294. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  295. movecontrolfiles(OLDDEBDIR);
  296. }
  297. }
  298. }
  299. static void controlextractvextract(int admin,
  300. const char *taroptions,
  301. const char *const *argv) {
  302. const char *debar, *directory;
  303. if (!(debar= *argv++))
  304. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  305. if (!(directory= *argv++)) {
  306. if (admin) directory= EXTRACTCONTROLDIR;
  307. else ohshit(_("--%s needs a target directory.\n"
  308. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  309. } else if (*argv) {
  310. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  311. }
  312. extracthalf(debar, directory, taroptions, admin);
  313. }
  314. void do_fsystarfile(const char *const *argv) {
  315. const char *debar;
  316. if (!(debar= *argv++))
  317. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  318. if (*argv)
  319. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  320. extracthalf(debar, NULL, NULL, 0);
  321. }
  322. void do_control(const char *const *argv) { controlextractvextract(1, "x", argv); }
  323. void do_extract(const char *const *argv) { controlextractvextract(0, "xp", argv); }
  324. void do_vextract(const char *const *argv) { controlextractvextract(0, "xpv", argv); }