chiark / gitweb /
f91d18ad8dcb97aeb3dc52189ad65fb82ac27f2a
[dpkg] / dpkg-deb / extract.c
1 /*
2  * dpkg-deb - construction and deconstruction of *.deb archives
3  * extract.c - extracting archives
4  *
5  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
7  *
8  * This is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23 #include <compat.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/wait.h>
28
29 #include <errno.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <dirent.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <ar.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40
41 #include <dpkg/i18n.h>
42 #include <dpkg/dpkg.h>
43 #include <dpkg/fdio.h>
44 #include <dpkg/buffer.h>
45 #include <dpkg/subproc.h>
46 #include <dpkg/command.h>
47 #include <dpkg/compress.h>
48 #include <dpkg/ar.h>
49 #include <dpkg/deb-version.h>
50 #include <dpkg/options.h>
51
52 #include "dpkg-deb.h"
53
54 static void movecontrolfiles(const char *thing) {
55   char buf[200];
56   pid_t pid;
57
58   sprintf(buf, "mv %s/* . && rmdir %s", thing, thing);
59   pid = subproc_fork();
60   if (pid == 0) {
61     command_shell(buf, _("shell command to move files"));
62   }
63   subproc_reap(pid, _("shell command to move files"), 0);
64 }
65
66 static void DPKG_ATTR_NORET
67 read_fail(int rc, const char *filename, const char *what)
68 {
69   if (rc >= 0)
70     ohshit(_("unexpected end of file in %s in %.255s"),what,filename);
71   else
72     ohshite(_("error reading %s from file %.255s"), what, filename);
73 }
74
75 static ssize_t
76 read_line(int fd, char *buf, size_t min_size, size_t max_size)
77 {
78   ssize_t line_size = 0;
79   size_t n = min_size;
80
81   while (line_size < (ssize_t)max_size) {
82     ssize_t r;
83     char *nl;
84
85     r = fd_read(fd, buf + line_size, n);
86     if (r <= 0)
87       return r;
88
89     nl = memchr(buf + line_size, '\n', r);
90     line_size += r;
91
92     if (nl != NULL) {
93       nl[1] = '\0';
94       return line_size;
95     }
96
97     n = 1;
98   }
99
100   buf[line_size] = '\0';
101   return line_size;
102 }
103
104 void
105 extracthalf(const char *debar, const char *dir,
106             enum dpkg_tar_options taroption, int admininfo)
107 {
108   struct dpkg_error err;
109   const char *errstr;
110   struct dpkg_ar *ar;
111   char versionbuf[40];
112   struct deb_version version;
113   off_t ctrllennum, memberlen = 0;
114   ssize_t r;
115   int dummy;
116   pid_t c1=0,c2,c3;
117   int p1[2], p2[2];
118   int p2_out;
119   char nlc;
120   int adminmember = -1;
121   bool header_done;
122   enum compressor_type decompressor = COMPRESSOR_TYPE_GZIP;
123
124   ar = dpkg_ar_open(debar);
125
126   r = read_line(ar->fd, versionbuf, strlen(DPKG_AR_MAGIC), sizeof(versionbuf) - 1);
127   if (r < 0)
128     read_fail(r, debar, _("archive magic version number"));
129
130   if (strcmp(versionbuf, DPKG_AR_MAGIC) == 0) {
131     ctrllennum= 0;
132     header_done = false;
133     for (;;) {
134       struct dpkg_ar_hdr arh;
135
136       r = fd_read(ar->fd, &arh, sizeof(arh));
137       if (r != sizeof(arh))
138         read_fail(r, debar, _("archive member header"));
139
140       dpkg_ar_normalize_name(&arh);
141
142       if (dpkg_ar_member_is_illegal(&arh))
143         ohshit(_("file '%.250s' is corrupt - bad archive header magic"), debar);
144       memberlen = dpkg_ar_member_get_size(ar, &arh);
145       if (!header_done) {
146         char *infobuf;
147
148         if (strncmp(arh.ar_name, DEBMAGIC, sizeof(arh.ar_name)) != 0)
149           ohshit(_("file '%.250s' is not a debian binary archive (try dpkg-split?)"),
150                  debar);
151         infobuf= m_malloc(memberlen+1);
152         r = fd_read(ar->fd, infobuf, memberlen + (memberlen & 1));
153         if (r != (memberlen + (memberlen & 1)))
154           read_fail(r, debar, _("archive information header member"));
155         infobuf[memberlen] = '\0';
156
157         if (strchr(infobuf, '\n') == NULL)
158           ohshit(_("archive has no newlines in header"));
159         errstr = deb_version_parse(&version, infobuf);
160         if (errstr)
161           ohshit(_("archive has invalid format version: %s"), errstr);
162         if (version.major != 2)
163           ohshit(_("archive is format version %d.%d; get a newer dpkg-deb"),
164                  version.major, version.minor);
165
166         free(infobuf);
167
168         header_done = true;
169       } else if (arh.ar_name[0] == '_') {
170         /* Members with ‘_’ are noncritical, and if we don't understand
171          * them we skip them. */
172         if (fd_skip(ar->fd, memberlen + (memberlen & 1), &err) < 0)
173           ohshit(_("cannot skip archive member from '%s': %s"), ar->name, err.str);
174       } else {
175         if (strncmp(arh.ar_name, ADMINMEMBER, strlen(ADMINMEMBER)) == 0) {
176           const char *extension = arh.ar_name + strlen(ADMINMEMBER);
177
178           adminmember = 1;
179           decompressor = compressor_find_by_extension(extension);
180           if (decompressor != COMPRESSOR_TYPE_NONE &&
181               decompressor != COMPRESSOR_TYPE_GZIP &&
182               decompressor != COMPRESSOR_TYPE_XZ)
183             ohshit(_("archive '%s' uses unknown compression for member '%.*s', "
184                      "giving up"),
185                    debar, (int)sizeof(arh.ar_name), arh.ar_name);
186         } else {
187           if (adminmember != 1)
188             ohshit(_("archive '%s' has premature member '%.*s' before '%s', "
189                      "giving up"),
190                    debar, (int)sizeof(arh.ar_name), arh.ar_name, ADMINMEMBER);
191
192           if (strncmp(arh.ar_name, DATAMEMBER, strlen(DATAMEMBER)) == 0) {
193             const char *extension = arh.ar_name + strlen(DATAMEMBER);
194
195             adminmember= 0;
196             decompressor = compressor_find_by_extension(extension);
197             if (decompressor == COMPRESSOR_TYPE_UNKNOWN)
198               ohshit(_("archive '%s' uses unknown compression for member '%.*s', "
199                        "giving up"),
200                      debar, (int)sizeof(arh.ar_name), arh.ar_name);
201           } else {
202             ohshit(_("archive '%s' has premature member '%.*s' before '%s', "
203                      "giving up"),
204                    debar, (int)sizeof(arh.ar_name), arh.ar_name, DATAMEMBER);
205           }
206         }
207         if (adminmember == 1) {
208           if (ctrllennum != 0)
209             ohshit(_("archive '%.250s' contains two control members, giving up"),
210                    debar);
211           ctrllennum= memberlen;
212         }
213         if (!adminmember != !admininfo) {
214           if (fd_skip(ar->fd, memberlen + (memberlen & 1), &err) < 0)
215             ohshit(_("cannot skip archive member from '%s': %s"), ar->name, err.str);
216         } else {
217           /* Yes! - found it. */
218           break;
219         }
220       }
221     }
222
223     if (admininfo >= 2) {
224       printf(_(" new debian package, version %d.%d.\n"
225                " size %jd bytes: control archive=%jd bytes.\n"),
226              version.major, version.minor,
227              (intmax_t)ar->size, (intmax_t)ctrllennum);
228       m_output(stdout, _("<standard output>"));
229     }
230   } else if (strncmp(versionbuf, "0.93", 4) == 0) {
231     char ctrllenbuf[40];
232     int l;
233
234     l = strlen(versionbuf);
235
236     if (strchr(versionbuf, '\n') == NULL)
237       ohshit(_("archive has no newlines in header"));
238     errstr = deb_version_parse(&version, versionbuf);
239     if (errstr)
240       ohshit(_("archive has invalid format version: %s"), errstr);
241
242     r = read_line(ar->fd, ctrllenbuf, 1, sizeof(ctrllenbuf) - 1);
243     if (r < 0)
244       read_fail(r, debar, _("archive control member size"));
245     if (sscanf(ctrllenbuf, "%jd%c%d", (intmax_t *)&ctrllennum, &nlc, &dummy) != 2 ||
246         nlc != '\n')
247       ohshit(_("archive has malformed control member size '%s'"), ctrllenbuf);
248
249     if (admininfo) {
250       memberlen = ctrllennum;
251     } else {
252       memberlen = ar->size - ctrllennum - strlen(ctrllenbuf) - l;
253       if (fd_skip(ar->fd, ctrllennum, &err) < 0)
254         ohshit(_("cannot skip archive control member from '%s': %s"), ar->name,
255                err.str);
256     }
257
258     if (admininfo >= 2) {
259       printf(_(" old debian package, version %d.%d.\n"
260                " size %jd bytes: control archive=%jd, main archive=%jd.\n"),
261              version.major, version.minor,
262              (intmax_t)ar->size, (intmax_t)ctrllennum,
263              (intmax_t)(ar->size - ctrllennum - strlen(ctrllenbuf) - l));
264       m_output(stdout, _("<standard output>"));
265     }
266   } else {
267     if (strncmp(versionbuf, "!<arch>", 7) == 0) {
268       notice(_("file looks like it might be an archive which has been\n"
269                " corrupted by being downloaded in ASCII mode"));
270     }
271
272     ohshit(_("'%.255s' is not a debian format archive"), debar);
273   }
274
275   m_pipe(p1);
276   c1 = subproc_fork();
277   if (!c1) {
278     close(p1[0]);
279     if (fd_fd_copy(ar->fd, p1[1], memberlen, &err) < 0)
280       ohshit(_("cannot copy archive member from '%s' to decompressor pipe: %s"),
281              ar->name, err.str);
282     if (close(p1[1]))
283       ohshite(_("cannot close decompressor pipe"));
284     exit(0);
285   }
286   close(p1[1]);
287
288   if (taroption) {
289     m_pipe(p2);
290     p2_out = p2[1];
291   } else {
292     p2_out = 1;
293   }
294
295   c2 = subproc_fork();
296   if (!c2) {
297     if (taroption)
298       close(p2[0]);
299     decompress_filter(decompressor, p1[0], p2_out,
300                       _("decompressing archive member"));
301     exit(0);
302   }
303   close(p1[0]);
304   dpkg_ar_close(ar);
305   if (taroption) close(p2[1]);
306
307   if (taroption) {
308     c3 = subproc_fork();
309     if (!c3) {
310       struct command cmd;
311
312       command_init(&cmd, TAR, "tar");
313       command_add_arg(&cmd, "tar");
314
315       if ((taroption & DPKG_TAR_LIST) && (taroption & DPKG_TAR_EXTRACT))
316         command_add_arg(&cmd, "-xv");
317       else if (taroption & DPKG_TAR_EXTRACT)
318         command_add_arg(&cmd, "-x");
319       else if (taroption & DPKG_TAR_LIST)
320         command_add_arg(&cmd, "-tv");
321       else
322         internerr("unknown or missing tar action '%d'", taroption);
323
324       if (taroption & DPKG_TAR_PERMS)
325         command_add_arg(&cmd, "-p");
326       if (taroption & DPKG_TAR_NOMTIME)
327         command_add_arg(&cmd, "-m");
328
329       command_add_arg(&cmd, "-f");
330       command_add_arg(&cmd, "-");
331       command_add_arg(&cmd, "--warning=no-timestamp");
332
333       m_dup2(p2[0],0);
334       close(p2[0]);
335
336       unsetenv("TAR_OPTIONS");
337
338       if (dir) {
339         if (mkdir(dir, 0777) != 0) {
340           if (errno != EEXIST)
341             ohshite(_("failed to create directory"));
342
343           if (taroption & DPKG_TAR_CREATE_DIR)
344             ohshite(_("unexpected pre-existing pathname %s"), dir);
345         }
346         if (chdir(dir) != 0)
347           ohshite(_("failed to chdir to directory"));
348       }
349
350       command_exec(&cmd);
351     }
352     close(p2[0]);
353     subproc_reap(c3, "tar", 0);
354   }
355
356   subproc_reap(c2, _("<decompress>"), SUBPROC_NOPIPE);
357   if (c1 != -1)
358     subproc_reap(c1, _("paste"), 0);
359   if (version.major == 0 && admininfo) {
360     /* Handle the version as a float to preserve the behaviour of old code,
361      * because even if the format is defined to be padded by 0's that might
362      * not have been always true for really ancient versions... */
363     while (version.minor && (version.minor % 10) == 0)
364       version.minor /= 10;
365
366     if (version.minor ==  931)
367       movecontrolfiles(OLDOLDDEBDIR);
368     else if (version.minor == 932 || version.minor == 933)
369       movecontrolfiles(OLDDEBDIR);
370   }
371 }
372
373 int
374 do_ctrltarfile(const char *const *argv)
375 {
376   const char *debar;
377
378   debar = *argv++;
379   if (debar == NULL)
380     badusage(_("--%s needs a .deb filename argument"), cipaction->olong);
381   if (*argv)
382     badusage(_("--%s takes only one argument (.deb filename)"),
383              cipaction->olong);
384
385   extracthalf(debar, NULL, DPKG_TAR_PASSTHROUGH, 1);
386
387   return 0;
388 }
389
390 int
391 do_fsystarfile(const char *const *argv)
392 {
393   const char *debar;
394
395   debar = *argv++;
396   if (debar == NULL)
397     badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
398   if (*argv)
399     badusage(_("--%s takes only one argument (.deb filename)"),cipaction->olong);
400   extracthalf(debar, NULL, DPKG_TAR_PASSTHROUGH, 0);
401
402   return 0;
403 }
404
405 int
406 do_control(const char *const *argv)
407 {
408   const char *debar, *dir;
409
410   debar = *argv++;
411   if (debar == NULL)
412     badusage(_("--%s needs a .deb filename argument"), cipaction->olong);
413
414   dir = *argv++;
415   if (dir == NULL)
416     dir = EXTRACTCONTROLDIR;
417   else if (*argv)
418     badusage(_("--%s takes at most two arguments (.deb and directory)"),
419              cipaction->olong);
420
421   extracthalf(debar, dir, DPKG_TAR_EXTRACT, 1);
422
423   return 0;
424 }
425
426 int
427 do_extract(const char *const *argv)
428 {
429   const char *debar, *dir;
430   enum dpkg_tar_options options = DPKG_TAR_EXTRACT | DPKG_TAR_PERMS;
431
432   if (opt_verbose)
433     options |= DPKG_TAR_LIST;
434
435   debar = *argv++;
436   if (debar == NULL)
437     badusage(_("--%s needs .deb filename and directory arguments"),
438              cipaction->olong);
439
440   dir = *argv++;
441   if (dir == NULL)
442     badusage(_("--%s needs a target directory.\n"
443                "Perhaps you should be using dpkg --install ?"),
444              cipaction->olong);
445   else if (*argv)
446     badusage(_("--%s takes at most two arguments (.deb and directory)"),
447              cipaction->olong);
448
449   extracthalf(debar, dir, options, 0);
450
451   return 0;
452 }
453
454 int
455 do_vextract(const char *const *argv)
456 {
457   /* XXX: Backward compatibility. */
458   opt_verbose = 1;
459   return do_extract(argv);
460 }
461
462 int
463 do_raw_extract(const char *const *argv)
464 {
465   enum dpkg_tar_options data_options;
466   const char *debar, *dir;
467   char *control_dir;
468
469   debar = *argv++;
470   if (debar == NULL)
471     badusage(_("--%s needs .deb filename and directory arguments"),
472              cipaction->olong);
473   else if (strcmp(debar, "-") == 0)
474     badusage(_("--%s does not support (yet) reading the .deb from standard input"),
475              cipaction->olong);
476
477   dir = *argv++;
478   if (dir == NULL)
479     badusage(_("--%s needs a target directory.\n"
480                "Perhaps you should be using dpkg --install ?"),
481              cipaction->olong);
482   else if (*argv)
483     badusage(_("--%s takes at most two arguments (.deb and directory)"),
484              cipaction->olong);
485
486   control_dir = str_fmt("%s/%s", dir, EXTRACTCONTROLDIR);
487
488   data_options = DPKG_TAR_EXTRACT | DPKG_TAR_PERMS;
489   if (opt_verbose)
490     data_options |= DPKG_TAR_LIST;
491
492   extracthalf(debar, dir, data_options, 0);
493   extracthalf(debar, control_dir, DPKG_TAR_EXTRACT | DPKG_TAR_CREATE_DIR, 1);
494
495   free(control_dir);
496
497   return 0;
498 }