extract.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <fcntl.h>
  32. #include <unistd.h>
  33. #include <ar.h>
  34. #include <stdbool.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/buffer.h>
  40. #include <dpkg/subproc.h>
  41. #include <dpkg/compress.h>
  42. #include <dpkg/ar.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. c1 = subproc_fork();
  50. if (!c1) {
  51. execlp("sh", "sh", "-c", buf, NULL);
  52. ohshite(_("unable to execute %s (%s)"),
  53. _("shell command to move files"), buf);
  54. }
  55. subproc_wait_check(c1, _("shell command to move files"), 0);
  56. }
  57. static void DPKG_ATTR_NORET
  58. read_fail(int rc, const char *filename, const char *what)
  59. {
  60. if (rc == 0)
  61. ohshit(_("unexpected end of file in %s in %.255s"),what,filename);
  62. else
  63. ohshite(_("error reading %s from file %.255s"), what, filename);
  64. }
  65. static ssize_t
  66. read_line(int fd, char *buf, size_t min_size, size_t max_size)
  67. {
  68. ssize_t line_size = 0;
  69. size_t n = min_size;
  70. while (line_size < (ssize_t)max_size) {
  71. ssize_t r;
  72. char *nl;
  73. r = read(fd, buf + line_size, n);
  74. if (r <= 0)
  75. return r;
  76. nl = strchr(buf + line_size, '\n');
  77. line_size += r;
  78. if (nl != NULL) {
  79. nl[1] = '\0';
  80. return line_size;
  81. }
  82. n = 1;
  83. }
  84. buf[line_size] = '\0';
  85. return line_size;
  86. }
  87. static size_t
  88. parseheaderlength(const char *inh, size_t len,
  89. const char *fn, const char *what)
  90. {
  91. char lintbuf[15];
  92. ssize_t r;
  93. char *endp;
  94. if (memchr(inh,0,len))
  95. ohshit(_("file `%.250s' is corrupt - %.250s length contains nulls"),fn,what);
  96. assert(sizeof(lintbuf) > len);
  97. memcpy(lintbuf,inh,len);
  98. lintbuf[len]= ' ';
  99. *strchr(lintbuf, ' ') = '\0';
  100. r = strtol(lintbuf, &endp, 10);
  101. if (r < 0)
  102. ohshit(_("file `%.250s' is corrupt - negative member length %zi"), fn, r);
  103. if (*endp)
  104. ohshit(_("file `%.250s' is corrupt - bad digit (code %d) in %s"),fn,*endp,what);
  105. return (size_t)r;
  106. }
  107. void extracthalf(const char *debar, const char *directory,
  108. const char *taroption, int admininfo) {
  109. char versionbuf[40];
  110. float versionnum;
  111. size_t ctrllennum, memberlen= 0;
  112. ssize_t r;
  113. int dummy;
  114. pid_t c1=0,c2,c3;
  115. int p1[2], p2[2];
  116. int arfd;
  117. struct stat stab;
  118. char nlc;
  119. int adminmember;
  120. bool oldformat, header_done;
  121. struct compressor *decompressor = &compressor_gzip;
  122. arfd = open(debar, O_RDONLY);
  123. if (arfd < 0)
  124. ohshite(_("failed to read archive `%.255s'"), debar);
  125. if (fstat(arfd, &stab))
  126. ohshite(_("failed to fstat archive"));
  127. r = read_line(arfd, versionbuf, strlen(DPKG_AR_MAGIC), sizeof(versionbuf));
  128. if (r < 0)
  129. read_fail(r, debar, _("archive magic version number"));
  130. if (!strcmp(versionbuf, DPKG_AR_MAGIC)) {
  131. oldformat = false;
  132. ctrllennum= 0;
  133. header_done = false;
  134. for (;;) {
  135. struct ar_hdr arh;
  136. r = read(arfd, &arh, sizeof(arh));
  137. if (r != sizeof(arh))
  138. read_fail(r, debar, _("archive member header"));
  139. dpkg_ar_normalize_name(&arh);
  140. if (memcmp(arh.ar_fmag,ARFMAG,sizeof(arh.ar_fmag)))
  141. ohshit(_("file `%.250s' is corrupt - bad magic at end of first header"),debar);
  142. memberlen= parseheaderlength(arh.ar_size,sizeof(arh.ar_size),
  143. debar, _("archive member size"));
  144. if (!header_done) {
  145. char *infobuf;
  146. char *cur;
  147. if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
  148. ohshit(_("file `%.250s' is not a debian binary archive (try dpkg-split?)"),debar);
  149. infobuf= m_malloc(memberlen+1);
  150. r = read(arfd, infobuf, memberlen + (memberlen & 1));
  151. if ((size_t)r != (memberlen + (memberlen & 1)))
  152. read_fail(r, debar, _("archive information header member"));
  153. infobuf[memberlen] = '\0';
  154. cur= strchr(infobuf,'\n');
  155. if (!cur) ohshit(_("archive has no newlines in header"));
  156. *cur = '\0';
  157. cur= strchr(infobuf,'.');
  158. if (!cur) ohshit(_("archive has no dot in version number"));
  159. *cur = '\0';
  160. if (strcmp(infobuf,"2"))
  161. ohshit(_("archive version %.250s not understood, get newer dpkg-deb"), infobuf);
  162. *cur= '.';
  163. strncpy(versionbuf,infobuf,sizeof(versionbuf));
  164. versionbuf[sizeof(versionbuf) - 1] = '\0';
  165. free(infobuf);
  166. header_done = true;
  167. } else if (arh.ar_name[0] == '_') {
  168. /* Members with `_' are noncritical, and if we don't understand them
  169. * we skip them.
  170. */
  171. fd_null_copy(arfd, memberlen + (memberlen & 1),
  172. _("skipped archive member data from %s"), debar);
  173. } else {
  174. if (strncmp(arh.ar_name, ADMINMEMBER, sizeof(arh.ar_name)) == 0)
  175. adminmember = 1;
  176. else {
  177. adminmember = -1;
  178. if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
  179. const char *extension = arh.ar_name + strlen(DATAMEMBER);
  180. adminmember= 0;
  181. decompressor = compressor_find_by_extension(extension);
  182. }
  183. if (adminmember == -1 || decompressor == NULL)
  184. ohshit(_("archive '%.250s' contains not understood data member %.*s, giving up"),
  185. debar, (int)sizeof(arh.ar_name), arh.ar_name);
  186. }
  187. if (adminmember == 1) {
  188. if (ctrllennum != 0)
  189. ohshit(_("archive '%.250s' contains two control members, giving up"),
  190. debar);
  191. ctrllennum= memberlen;
  192. }
  193. if (!adminmember != !admininfo) {
  194. fd_null_copy(arfd, memberlen + (memberlen & 1),
  195. _("skipped archive member data from %s"), debar);
  196. } else {
  197. break; /* Yes ! - found it. */
  198. }
  199. }
  200. }
  201. if (admininfo >= 2) {
  202. printf(_(" new debian package, version %s.\n"
  203. " size %ld bytes: control archive= %zi bytes.\n"),
  204. versionbuf, (long)stab.st_size, ctrllennum);
  205. m_output(stdout, _("<standard output>"));
  206. }
  207. } else if (!strncmp(versionbuf,"0.93",4) &&
  208. sscanf(versionbuf,"%f%c%d",&versionnum,&nlc,&dummy) == 2 &&
  209. nlc == '\n') {
  210. char ctrllenbuf[40];
  211. int l = 0;
  212. oldformat = true;
  213. l = strlen(versionbuf);
  214. if (l && versionbuf[l - 1] == '\n')
  215. versionbuf[l - 1] = '\0';
  216. r = read_line(arfd, ctrllenbuf, 1, sizeof(ctrllenbuf));
  217. if (r < 0)
  218. read_fail(r, debar, _("archive control member size"));
  219. if (sscanf(ctrllenbuf,"%zi%c%d",&ctrllennum,&nlc,&dummy) !=2 || nlc != '\n')
  220. ohshit(_("archive has malformatted control member size '%s'"), ctrllenbuf);
  221. if (admininfo) {
  222. memberlen = ctrllennum;
  223. } else {
  224. memberlen = stab.st_size - ctrllennum - strlen(ctrllenbuf) - l;
  225. fd_null_copy(arfd, ctrllennum,
  226. _("skipped archive control member data from %s"), debar);
  227. }
  228. if (admininfo >= 2) {
  229. printf(_(" old debian package, version %s.\n"
  230. " size %ld bytes: control archive= %zi, main archive= %ld.\n"),
  231. versionbuf, (long)stab.st_size, ctrllennum,
  232. (long) (stab.st_size - ctrllennum - strlen(ctrllenbuf) - l));
  233. m_output(stdout, _("<standard output>"));
  234. }
  235. } else {
  236. if (!strncmp(versionbuf,"!<arch>",7)) {
  237. fprintf(stderr,
  238. _("dpkg-deb: file looks like it might be an archive which has been\n"
  239. "dpkg-deb: corrupted by being downloaded in ASCII mode\n"));
  240. }
  241. ohshit(_("`%.255s' is not a debian format archive"),debar);
  242. }
  243. m_pipe(p1);
  244. c1 = subproc_fork();
  245. if (!c1) {
  246. close(p1[0]);
  247. fd_fd_copy(arfd, p1[1], memberlen, _("failed to write to pipe in copy"));
  248. if (close(p1[1]))
  249. ohshite(_("failed to close pipe in copy"));
  250. exit(0);
  251. }
  252. close(p1[1]);
  253. if (taroption) m_pipe(p2);
  254. c2 = subproc_fork();
  255. if (!c2) {
  256. m_dup2(p1[0], 0);
  257. if (admininfo) close(p1[0]);
  258. if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
  259. decompress_filter(decompressor, 0, 1, _("data"));
  260. }
  261. close(p1[0]);
  262. close(arfd);
  263. if (taroption) close(p2[1]);
  264. if (taroption && directory) {
  265. if (chdir(directory)) {
  266. if (errno == ENOENT) {
  267. if (mkdir(directory,0777)) ohshite(_("failed to create directory"));
  268. if (chdir(directory)) ohshite(_("failed to chdir to directory after creating it"));
  269. } else {
  270. ohshite(_("failed to chdir to directory"));
  271. }
  272. }
  273. }
  274. if (taroption) {
  275. c3 = subproc_fork();
  276. if (!c3) {
  277. char buffer[30+2];
  278. if (strlen(taroption) > 30)
  279. internerr("taroption is too long '%s'", taroption);
  280. strcpy(buffer, taroption);
  281. strcat(buffer, "f");
  282. m_dup2(p2[0],0);
  283. close(p2[0]);
  284. unsetenv("TAR_OPTIONS");
  285. execlp(TAR, "tar", buffer, "-", NULL);
  286. ohshite(_("unable to execute %s (%s)"), "tar", TAR);
  287. }
  288. close(p2[0]);
  289. subproc_wait_check(c3, "tar", 0);
  290. }
  291. subproc_wait_check(c2, _("<decompress>"), PROCPIPE);
  292. if (c1 != -1)
  293. subproc_wait_check(c1, _("paste"), 0);
  294. if (oldformat && admininfo) {
  295. if (versionnum == 0.931F) {
  296. movecontrolfiles(OLDOLDDEBDIR);
  297. } else if (versionnum == 0.932F || versionnum == 0.933F) {
  298. movecontrolfiles(OLDDEBDIR);
  299. }
  300. }
  301. }
  302. static void controlextractvextract(int admin,
  303. const char *taroptions,
  304. const char *const *argv) {
  305. const char *debar, *directory;
  306. if (!(debar= *argv++))
  307. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  308. if (!(directory= *argv++)) {
  309. if (admin) directory= EXTRACTCONTROLDIR;
  310. else ohshit(_("--%s needs a target directory.\n"
  311. "Perhaps you should be using dpkg --install ?"),cipaction->olong);
  312. } else if (*argv) {
  313. badusage(_("--%s takes at most two arguments (.deb and directory)"),cipaction->olong);
  314. }
  315. extracthalf(debar, directory, taroptions, admin);
  316. }
  317. void do_fsystarfile(const char *const *argv) {
  318. const char *debar;
  319. if (!(debar= *argv++))
  320. badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  321. if (*argv)
  322. badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
  323. extracthalf(debar, NULL, NULL, 0);
  324. }
  325. void do_control(const char *const *argv) { controlextractvextract(1, "x", argv); }
  326. void do_extract(const char *const *argv) { controlextractvextract(0, "xp", argv); }
  327. void do_vextract(const char *const *argv) { controlextractvextract(0, "xpv", argv); }