extract.c 11 KB

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