extract.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 readfail(FILE *a, const char *filename, const char *what) DPKG_ATTR_NORET;
  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. void *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. struct compressor *compressor = &compressor_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. dpkg_ar_normalize_name(&arh);
  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 (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  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. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  156. adminmember = 1;
  157. else {
  158. adminmember = -1;
  159. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  160. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  161. adminmember= 0;
  162. compressor = compressor_find_by_extension(extension);
  163. }
  164. if (adminmember == -1 || compressor == NULL)
  165. ohshit(_("file `%.250s' contains ununderstood data member %.*s, giving up"),
  166. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  167. }
  168. if (adminmember == 1) {
  169. if (ctrllennum != 0)
  170. ohshit(_("file `%.250s' contains two control members, giving up"), debar);
  171. ctrllennum= memberlen;
  172. }
  173. if (!adminmember != !admininfo) {
  174. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  175. } else {
  176. break; /* Yes ! - found it. */
  177. }
  178. }
  179. }
  180. if (admininfo >= 2) {
  181. printf(_(" new debian package, version %s.\n"
  182. " size %ld bytes: control archive= %zi bytes.\n"),
  183. versionbuf, (long)stab.st_size, ctrllennum);
  184. m_output(stdout, _("<standard output>"));
  185. }
  186. } else if (!strncmp(versionbuf,"0.93",4) &&
  187. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  188. nlc == '\n') {
  189. oldformat= 1;
  190. l = strlen(versionbuf);
  191. if (l && versionbuf[l - 1] == '\n')
  192. versionbuf[l - 1] = '\0';
  193. if (!fgets(ctrllenbuf,sizeof(ctrllenbuf),ar))
  194. readfail(ar, debar, _("control information length"));
  195. if (sscanf(ctrllenbuf,"%zi%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  196. ohshit(_("archive has malformatted control length `%s'"), ctrllenbuf);
  197. if (admininfo >= 2) {
  198. printf(_(" old debian package, version %s.\n"
  199. " size %ld bytes: control archive= %zi, main archive= %ld.\n"),
  200. versionbuf, (long)stab.st_size, ctrllennum,
  201. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  202. m_output(stdout, _("<standard output>"));
  203. }
  204. ctrlarea = m_malloc(ctrllennum);
  205. errno=0; if (fread(ctrlarea,1,ctrllennum,ar) != ctrllennum)
  206. readfail(ar, debar, _("control area"));
  207. } else {
  208. if (!strncmp(versionbuf,"!<arch>",7)) {
  209. fprintf(stderr,
  210. _("dpkg-deb: file looks like it might be an archive which has been\n"
  211. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"));
  212. }
  213. ohshit(_("`%.255s' is not a debian format archive"),debar);
  214. }
  215. safe_fflush(ar);
  216. if (oldformat) {
  217. if (admininfo) {
  218. m_pipe(p1);
  219. c1 = subproc_fork();
  220. if (!c1) {
  221. close(p1[0]);
  222. pi = fdopen(p1[1], "w");
  223. if (!pi)
  224. ohshite(_("failed to open pipe descriptor `1' in paste"));
  225. errno=0; if (fwrite(ctrlarea,1,ctrllennum,pi) != ctrllennum)
  226. ohshit(_("failed to write to gzip -dc"));
  227. if (fclose(pi)) ohshit(_("failed to close gzip -dc"));
  228. exit(0);
  229. }
  230. close(p1[1]);
  231. readfromfd= p1[0];
  232. } else {
  233. if (lseek(fileno(ar),l+strlen(ctrllenbuf)+ctrllennum,SEEK_SET) == -1)
  234. ohshite(_("failed to syscall lseek to files archive portion"));
  235. c1= -1;
  236. readfromfd= fileno(ar);
  237. }
  238. } else {
  239. m_pipe(p1);
  240. c1 = subproc_fork();
  241. if (!c1) {
  242. close(p1[0]);
  243. stream_fd_copy(ar, p1[1], memberlen, _("failed to write to pipe in copy"));
  244. if (close(p1[1]) == EOF) ohshite(_("failed to close pipe in copy"));
  245. exit(0);
  246. }
  247. close(p1[1]);
  248. readfromfd= p1[0];
  249. }
  250. if (taroption) m_pipe(p2);
  251. c2 = subproc_fork();
  252. if (!c2) {
  253. m_dup2(readfromfd,0);
  254. if (admininfo) close(p1[0]);
  255. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  256. decompress_filter(compressor, 0, 1, _("data"));
  257. }
  258. if (readfromfd != fileno(ar)) close(readfromfd);
  259. if (taroption) close(p2[1]);
  260. if (taroption && directory) {
  261. if (chdir(directory)) {
  262. if (errno == ENOENT) {
  263. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  264. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  265. } else {
  266. ohshite(_("failed to chdir to directory"));
  267. }
  268. }
  269. }
  270. if (taroption) {
  271. c3 = subproc_fork();
  272. if (!c3) {
  273. char buffer[30+2];
  274. if (strlen(taroption) > 30)
  275. internerr("taroption is too long '%s'", taroption);
  276. strcpy(buffer, taroption);
  277. strcat(buffer, "f");
  278. m_dup2(p2[0],0);
  279. close(p2[0]);
  280. unsetenv("TAR_OPTIONS");
  281. execlp(TAR, "tar", buffer, "-", NULL);
  282. ohshite(_("failed to exec tar"));
  283. }
  284. close(p2[0]);
  285. subproc_wait_check(c3, "tar", 0);
  286. }
  287. subproc_wait_check(c2, _("<decompress>"), PROCPIPE);
  288. if (c1 != -1)
  289. subproc_wait_check(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); }