extract.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 <stdbool.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/i18n.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/buffer.h>
  39. #include <dpkg/subproc.h>
  40. #include <dpkg/compress.h>
  41. #include <dpkg/ar.h>
  42. #include <dpkg/myopt.h>
  43. #include "dpkg-deb.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. c1 = subproc_fork();
  49. if (!c1) {
  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 DPKG_ATTR_NORET
  56. readfail(FILE *a, const char *filename, const char *what)
  57. {
  58. if (ferror(a)) {
  59. ohshite(_("error reading %s from file %.255s"), what, filename);
  60. } else {
  61. ohshit(_("unexpected end of file in %s in %.255s"),what,filename);
  62. }
  63. }
  64. static size_t
  65. parseheaderlength(const char *inh, size_t len,
  66. const char *fn, const char *what)
  67. {
  68. char lintbuf[15];
  69. ssize_t r;
  70. char *endp;
  71. if (memchr(inh,0,len))
  72. ohshit(_("file `%.250s' is corrupt - %.250s length contains nulls"),fn,what);
  73. assert(sizeof(lintbuf) > len);
  74. memcpy(lintbuf,inh,len);
  75. lintbuf[len]= ' ';
  76. *strchr(lintbuf, ' ') = '\0';
  77. r = strtol(lintbuf, &endp, 10);
  78. if (r < 0)
  79. ohshit(_("file `%.250s' is corrupt - negative member length %zi"), fn, r);
  80. if (*endp)
  81. ohshit(_("file `%.250s' is corrupt - bad digit (code %d) in %s"),fn,*endp,what);
  82. return (size_t)r;
  83. }
  84. static void
  85. safe_fflush(FILE *f)
  86. {
  87. #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ > 0)
  88. /* XXX: Glibc 2.1 and some versions of Linux want to make fflush()
  89. * move the current fpos. Remove this code some time. */
  90. fpos_t fpos;
  91. if (fgetpos(f, &fpos))
  92. ohshit(_("failed getting the current file position"));
  93. fflush(f);
  94. if (fsetpos(f, &fpos))
  95. ohshit(_("failed setting the current file position"));
  96. #else
  97. fflush(f);
  98. #endif
  99. }
  100. void extracthalf(const char *debar, const char *directory,
  101. const char *taroption, int admininfo) {
  102. char versionbuf[40];
  103. float versionnum;
  104. size_t ctrllennum, memberlen= 0;
  105. int dummy;
  106. pid_t c1=0,c2,c3;
  107. int p1[2], p2[2];
  108. FILE *ar;
  109. struct stat stab;
  110. char nlc;
  111. int adminmember;
  112. bool oldformat, header_done;
  113. struct compressor *decompressor = &compressor_gzip;
  114. ar= fopen(debar,"r"); if (!ar) ohshite(_("failed to read archive `%.255s'"),debar);
  115. if (fstat(fileno(ar),&stab)) ohshite(_("failed to fstat archive"));
  116. if (!fgets(versionbuf,sizeof(versionbuf),ar)) readfail(ar,debar,_("version number"));
  117. if (!strcmp(versionbuf, DPKG_AR_MAGIC)) {
  118. oldformat = false;
  119. ctrllennum= 0;
  120. header_done = false;
  121. for (;;) {
  122. struct ar_hdr arh;
  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. char *infobuf;
  132. char *cur;
  133. if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  134. ohshit(_("file `%.250s' is not a debian binary archive (try dpkg-split?)"),debar);
  135. infobuf= m_malloc(memberlen+1);
  136. if (fread(infobuf,1, memberlen + (memberlen&1), ar) != memberlen + (memberlen&1))
  137. readfail(ar,debar,_("header info member"));
  138. infobuf[memberlen] = '\0';
  139. cur= strchr(infobuf,'\n');
  140. if (!cur) ohshit(_("archive has no newlines in header"));
  141. *cur = '\0';
  142. cur= strchr(infobuf,'.');
  143. if (!cur) ohshit(_("archive has no dot in version number"));
  144. *cur = '\0';
  145. if (strcmp(infobuf,"2"))
  146. ohshit(_("archive version %.250s not understood, get newer dpkg-deb"), infobuf);
  147. *cur= '.';
  148. strncpy(versionbuf,infobuf,sizeof(versionbuf));
  149. versionbuf[sizeof(versionbuf) - 1] = '\0';
  150. free(infobuf);
  151. header_done = true;
  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. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  159. adminmember = 1;
  160. else {
  161. adminmember = -1;
  162. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  163. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  164. adminmember= 0;
  165. decompressor = compressor_find_by_extension(extension);
  166. }
  167. if (adminmember == -1 || decompressor == NULL)
  168. ohshit(_("file `%.250s' contains ununderstood data member %.*s, giving up"),
  169. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  170. }
  171. if (adminmember == 1) {
  172. if (ctrllennum != 0)
  173. ohshit(_("file `%.250s' contains two control members, giving up"), debar);
  174. ctrllennum= memberlen;
  175. }
  176. if (!adminmember != !admininfo) {
  177. stream_null_copy(ar, memberlen + (memberlen&1),_("skipped member data from %s"), debar);
  178. } else {
  179. break; /* Yes ! - found it. */
  180. }
  181. }
  182. }
  183. if (admininfo >= 2) {
  184. printf(_(" new debian package, version %s.\n"
  185. " size %ld bytes: control archive= %zi bytes.\n"),
  186. versionbuf, (long)stab.st_size, ctrllennum);
  187. m_output(stdout, _("<standard output>"));
  188. }
  189. } else if (!strncmp(versionbuf,"0.93",4) &&
  190. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  191. nlc == '\n') {
  192. char ctrllenbuf[40];
  193. int l = 0;
  194. oldformat = true;
  195. l = strlen(versionbuf);
  196. if (l && versionbuf[l - 1] == '\n')
  197. versionbuf[l - 1] = '\0';
  198. if (!fgets(ctrllenbuf,sizeof(ctrllenbuf),ar))
  199. readfail(ar, debar, _("control information length"));
  200. if (sscanf(ctrllenbuf,"%zi%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  201. ohshit(_("archive has malformatted control length `%s'"), ctrllenbuf);
  202. if (admininfo) {
  203. memberlen = ctrllennum;
  204. } else {
  205. memberlen = stab.st_size - ctrllennum - strlen(ctrllenbuf) - l;
  206. stream_null_copy(ar, ctrllennum, _("skipped control area from %s"), debar);
  207. }
  208. if (admininfo >= 2) {
  209. printf(_(" old debian package, version %s.\n"
  210. " size %ld bytes: control archive= %zi, main archive= %ld.\n"),
  211. versionbuf, (long)stab.st_size, ctrllennum,
  212. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  213. m_output(stdout, _("<standard output>"));
  214. }
  215. } else {
  216. if (!strncmp(versionbuf,"!<arch>",7)) {
  217. 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. }
  221. ohshit(_("`%.255s' is not a debian format archive"),debar);
  222. }
  223. safe_fflush(ar);
  224. m_pipe(p1);
  225. c1 = subproc_fork();
  226. if (!c1) {
  227. close(p1[0]);
  228. stream_fd_copy(ar, p1[1], memberlen, _("failed to write to pipe in copy"));
  229. if (close(p1[1]))
  230. ohshite(_("failed to close pipe in copy"));
  231. exit(0);
  232. }
  233. close(p1[1]);
  234. if (taroption) m_pipe(p2);
  235. c2 = subproc_fork();
  236. if (!c2) {
  237. m_dup2(p1[0], 0);
  238. if (admininfo) close(p1[0]);
  239. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  240. decompress_filter(decompressor, 0, 1, _("data"));
  241. }
  242. close(p1[0]);
  243. fclose(ar);
  244. if (taroption) close(p2[1]);
  245. if (taroption && directory) {
  246. if (chdir(directory)) {
  247. if (errno == ENOENT) {
  248. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  249. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  250. } else {
  251. ohshite(_("failed to chdir to directory"));
  252. }
  253. }
  254. }
  255. if (taroption) {
  256. c3 = subproc_fork();
  257. if (!c3) {
  258. char buffer[30+2];
  259. if (strlen(taroption) > 30)
  260. internerr("taroption is too long '%s'", taroption);
  261. strcpy(buffer, taroption);
  262. strcat(buffer, "f");
  263. m_dup2(p2[0],0);
  264. close(p2[0]);
  265. unsetenv("TAR_OPTIONS");
  266. execlp(TAR, "tar", buffer, "-", NULL);
  267. ohshite(_("failed to exec tar"));
  268. }
  269. close(p2[0]);
  270. subproc_wait_check(c3, "tar", 0);
  271. }
  272. subproc_wait_check(c2, _("<decompress>"), PROCPIPE);
  273. if (c1 != -1)
  274. subproc_wait_check(c1, _("paste"), 0);
  275. if (oldformat && admininfo) {
  276. if (versionnum == 0.931F) {
  277. movecontrolfiles(OLDOLDDEBDIR);
  278. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  279. movecontrolfiles(OLDDEBDIR);
  280. }
  281. }
  282. }
  283. static void controlextractvextract(int admin,
  284. const char *taroptions,
  285. const char *const *argv) {
  286. const char *debar, *directory;
  287. if (!(debar= *argv++))
  288. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  289. if (!(directory= *argv++)) {
  290. if (admin) directory= EXTRACTCONTROLDIR;
  291. else ohshit(_("--%s needs a target directory.\n"
  292. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  293. } else if (*argv) {
  294. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  295. }
  296. extracthalf(debar, directory, taroptions, admin);
  297. }
  298. void do_fsystarfile(const char *const *argv) {
  299. const char *debar;
  300. if (!(debar= *argv++))
  301. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  302. if (*argv)
  303. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  304. extracthalf(debar, NULL, NULL, 0);
  305. }
  306. void do_control(const char *const *argv) { controlextractvextract(1, "x", argv); }
  307. void do_extract(const char *const *argv) { controlextractvextract(0, "xp", argv); }
  308. void do_vextract(const char *const *argv) { controlextractvextract(0, "xpv", argv); }