summaryrefslogtreecommitdiffstats
path: root/tools/gpgtar-list.c
blob: 396e837f4f339269b7fce34327d59082e9c007de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* gpgtar-list.c - List a TAR archive
 * Copyright (C) 2010 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "../common/i18n.h"
#include "gpgtar.h"
#include "../common/exectool.h"
#include "../common/ccparray.h"



static unsigned long long
parse_xoctal (const void *data, size_t length, const char *filename)
{
  const unsigned char *p = data;
  unsigned long long value;

  if (!length)
    value = 0;
  else if ( (*p & 0x80))
    {
      /* Binary format.  */
      value = (*p++ & 0x7f);
      while (--length)
        {
          value <<= 8;
          value |= *p++;
        }
    }
  else
    {
      /* Octal format  */
      value = 0;
      /* Skip leading spaces and zeroes.  */
      for (; length && (*p == ' ' || *p == '0'); length--, p++)
        ;
      for (; length && *p; length--, p++)
        {
          if (*p >= '0' && *p <= '7')
            {
              value <<= 3;
              value += (*p - '0');
            }
          else
            {
              log_error ("%s: invalid octal number encountered - assuming 0\n",
                         filename);
              value = 0;
              break;
            }
        }
    }
  return value;
}


static tar_header_t
parse_header (const void *record, const char *filename, tarinfo_t info)
{
  const struct ustar_raw_header *raw = record;
  size_t n, namelen, prefixlen;
  tar_header_t header;
  int use_prefix;
  int anyerror = 0;

  info->headerblock = info->nblocks - 1;

  use_prefix = (!memcmp (raw->magic, "ustar", 5)
                && (raw->magic[5] == ' ' || !raw->magic[5]));


  for (namelen=0; namelen < sizeof raw->name && raw->name[namelen]; namelen++)
    ;
  if (namelen == sizeof raw->name)
    {
      log_info ("%s: warning: name not terminated by a nul\n", filename);
      anyerror = 1;
    }
  for (n=namelen+1; n < sizeof raw->name; n++)
    if (raw->name[n])
      {
        log_info ("%s: warning: garbage after name\n", filename);
        anyerror = 1;
        break;
      }

  if (use_prefix && raw->prefix[0])
    {
      for (prefixlen=0; (prefixlen < sizeof raw->prefix
                         && raw->prefix[prefixlen]); prefixlen++)
        ;
      if (prefixlen == sizeof raw->prefix)
        log_info ("%s: warning: prefix not terminated by a nul (block %llu)\n",
                  filename, info->headerblock);
      for (n=prefixlen+1; n < sizeof raw->prefix; n++)
        if (raw->prefix[n])
          {
            log_info ("%s: warning: garbage after prefix\n", filename);
            anyerror = 1;
            break;
          }
    }
  else
    prefixlen = 0;

  header = xtrycalloc (1, sizeof *header + prefixlen + 1 + namelen);
  if (!header)
    {
      log_error ("%s: error allocating header: %s\n",
                 filename, gpg_strerror (gpg_error_from_syserror ()));
      return NULL;
    }
  if (prefixlen)
    {
      n = prefixlen;
      memcpy (header->name, raw->prefix, n);
      if (raw->prefix[n-1] != '/')
        header->name[n++] = '/';
    }
  else
    n = 0;
  memcpy (header->name+n, raw->name, namelen);
  header->name[n+namelen] = 0;

  header->mode  = parse_xoctal (raw->mode, sizeof raw->mode, filename);
  header->uid   = parse_xoctal (raw->uid, sizeof raw->uid, filename);
  header->gid   = parse_xoctal (raw->gid, sizeof raw->gid, filename);
  header->size  = parse_xoctal (raw->size, sizeof raw->size, filename);
  header->mtime = parse_xoctal (raw->mtime, sizeof raw->mtime, filename);
  /* checksum = */
  switch (raw->typeflag[0])
    {
    case '0': header->typeflag = TF_REGULAR; break;
    case '1': header->typeflag = TF_HARDLINK; break;
    case '2': header->typeflag = TF_SYMLINK; break;
    case '3': header->typeflag = TF_CHARDEV; break;
    case '4': header->typeflag = TF_BLOCKDEV; break;
    case '5': header->typeflag = TF_DIRECTORY; break;
    case '6': header->typeflag = TF_FIFO; break;
    case '7': header->typeflag = TF_RESERVED; break;
    default:  header->typeflag = TF_UNKNOWN; break;
    }

  /* Compute the number of data records following this header.  */
  if (header->typeflag == TF_REGULAR || header->typeflag == TF_UNKNOWN)
    header->nrecords = (header->size + RECORDSIZE-1)/RECORDSIZE;
  else
    header->nrecords = 0;

  if (anyerror)
    {
      log_info ("%s: header block %llu is corrupt"
                " (size=%llu type=%d nrec=%llu)\n",
                filename, info->headerblock,
                header->size, header->typeflag, header->nrecords);
      /* log_printhex (record, RECORDSIZE, " "); */
    }

  return header;
}



/* Read the next block, assuming it is a tar header.  Returns a header
   object on success in R_HEADER, or an error.  If the stream is
   consumed, R_HEADER is set to NULL.  In case of an error an error
   message has been printed.  */
static gpg_error_t
read_header (estream_t stream, tarinfo_t info, tar_header_t *r_header)
{
  gpg_error_t err;
  char record[RECORDSIZE];
  int i;

  err = read_record (stream, record);
  if (err)
    return err;
  info->nblocks++;

  for (i=0; i < RECORDSIZE && !record[i]; i++)
    ;
  if (i == RECORDSIZE)
    {
      /* All zero header - check whether it is the first part of an
         end of archive mark.  */
      err = read_record (stream, record);
      if (err)
        return err;
      info->nblocks++;

      for (i=0; i < RECORDSIZE && !record[i]; i++)
        ;
      if (i != RECORDSIZE)
        log_info ("%s: warning: skipping empty header\n",
                  es_fname_get (stream));
      else
        {
          /* End of archive - FIXME: we might want to check for garbage.  */
          *r_header = NULL;
          return 0;
        }
    }

  *r_header = parse_header (record, es_fname_get (stream), info);
  return *r_header ? 0 : gpg_error_from_syserror ();
}


/* Skip the data records according to HEADER.  Prints an error message
   on error and return -1. */
static int
skip_data (estream_t stream, tarinfo_t info, tar_header_t header)
{
  char record[RECORDSIZE];
  unsigned long long n;

  for (n=0; n < header->nrecords; n++)
    {
      if (read_record (stream, record))
        return -1;
      info->nblocks++;
    }

  return 0;
}



static void
print_header (tar_header_t header, estream_t out)
{
  unsigned long mask;
  char modestr[10+1];
  int i;

  *modestr = '?';
  switch (header->typeflag)
    {
    case TF_REGULAR:  *modestr = '-'; break;
    case TF_HARDLINK: *modestr = 'h'; break;
    case TF_SYMLINK:  *modestr = 'l'; break;
    case TF_CHARDEV:  *modestr = 'c'; break;
    case TF_BLOCKDEV: *modestr = 'b'; break;
    case TF_DIRECTORY:*modestr = 'd'; break;
    case TF_FIFO:     *modestr = 'f'; break;
    case TF_RESERVED: *modestr = '='; break;
    case TF_UNKNOWN:  break;
    case TF_NOTSUP:   break;
    }
  for (mask = 0400, i = 0; i < 9; i++, mask >>= 1)
    modestr[1+i] = (header->mode & mask)? "rwxrwxrwx"[i]:'-';
  if ((header->typeflag & 04000))
    modestr[3] = modestr[3] == 'x'? 's':'S';
  if ((header->typeflag & 02000))
    modestr[6] = modestr[6] == 'x'? 's':'S';
  if ((header->typeflag & 01000))
    modestr[9] = modestr[9] == 'x'? 't':'T';
  modestr[10] = 0;

  es_fprintf (out, "%s %lu %lu/%lu %12llu %s %s\n",
              modestr, header->nlink, header->uid, header->gid, header->size,
              isotimestamp (header->mtime), header->name);
}



/* List the tarball FILENAME or, if FILENAME is NULL, the tarball read
   from stdin.  */
gpg_error_t
gpgtar_list (const char *filename, int decrypt)
{
  gpg_error_t err;
  estream_t stream;
  estream_t cipher_stream = NULL;
  tar_header_t header = NULL;
  struct tarinfo_s tarinfo_buffer;
  tarinfo_t tarinfo = &tarinfo_buffer;

  memset (&tarinfo_buffer, 0, sizeof tarinfo_buffer);

  if (filename)
    {
      if (!strcmp (filename, "-"))
        stream = es_stdin;
      else
        stream = es_fopen (filename, "rb");
      if (!stream)
        {
          err = gpg_error_from_syserror ();
          log_error ("error opening '%s': %s\n", filename, gpg_strerror (err));
          return err;
        }
    }
  else
    stream = es_stdin;

  if (stream == es_stdin)
    es_set_binary (es_stdin);

  if (decrypt)
    {
      strlist_t arg;
      ccparray_t ccp;
      const char **argv;

      cipher_stream = stream;
      stream = es_fopenmem (0, "rwb");
      if (! stream)
        {
          err = gpg_error_from_syserror ();
          goto leave;
        }

      ccparray_init (&ccp, 0);

      ccparray_put (&ccp, "--decrypt");
      for (arg = opt.gpg_arguments; arg; arg = arg->next)
        ccparray_put (&ccp, arg->d);

      ccparray_put (&ccp, NULL);
      argv = ccparray_get (&ccp, NULL);
      if (!argv)
        {
          err = gpg_error_from_syserror ();
          goto leave;
        }

      err = gnupg_exec_tool_stream (opt.gpg_program, argv,
                                    cipher_stream, NULL, stream, NULL, NULL);
      xfree (argv);
      if (err)
        goto leave;

      err = es_fseek (stream, 0, SEEK_SET);
      if (err)
        goto leave;
    }

  for (;;)
    {
      err = read_header (stream, tarinfo, &header);
      if (err || header == NULL)
        goto leave;

      print_header (header, es_stdout);

      if (skip_data (stream, tarinfo, header))
        goto leave;
      xfree (header);
      header = NULL;
    }


 leave:
  xfree (header);
  if (stream != es_stdin)
    es_fclose (stream);
  if (stream != cipher_stream)
    es_fclose (cipher_stream);
  return err;
}

gpg_error_t
gpgtar_read_header (estream_t stream, tarinfo_t info, tar_header_t *r_header)
{
  return read_header (stream, info, r_header);
}

void
gpgtar_print_header (tar_header_t header, estream_t out)
{
  if (header && out)
    print_header (header, out);
}