diff --git a/tools/gpgtar-extract.c b/tools/gpgtar-extract.c index 8613d193f..3da100c07 100644 --- a/tools/gpgtar-extract.c +++ b/tools/gpgtar-extract.c @@ -1,401 +1,411 @@ /* gpgtar-extract.c - Extract from 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 . */ #include #include #include #include #include #include #include #include #include #include "../common/i18n.h" #include "../common/exectool.h" #include "../common/sysutils.h" #include "../common/ccparray.h" #include "gpgtar.h" static gpg_error_t extract_regular (estream_t stream, const char *dirname, - tar_header_t hdr) + tarinfo_t info, tar_header_t hdr) { gpg_error_t err; char record[RECORDSIZE]; size_t n, nbytes, nwritten; char *fname; estream_t outfp = NULL; fname = strconcat (dirname, "/", hdr->name, NULL); if (!fname) { err = gpg_error_from_syserror (); log_error ("error creating filename: %s\n", gpg_strerror (err)); goto leave; } else err = 0; if (opt.dry_run) outfp = es_fopenmem (0, "wb"); else outfp = es_fopen (fname, "wb"); if (!outfp) { err = gpg_error_from_syserror (); log_error ("error creating '%s': %s\n", fname, gpg_strerror (err)); goto leave; } for (n=0; n < hdr->nrecords;) { err = read_record (stream, record); if (err) goto leave; + info->nblocks++; n++; if (n < hdr->nrecords || (hdr->size && !(hdr->size % RECORDSIZE))) nbytes = RECORDSIZE; else nbytes = (hdr->size % RECORDSIZE); nwritten = es_fwrite (record, 1, nbytes, outfp); if (nwritten != nbytes) { err = gpg_error_from_syserror (); log_error ("error writing '%s': %s\n", fname, gpg_strerror (err)); goto leave; } } /* Fixme: Set permissions etc. */ leave: if (!err && opt.verbose) log_info ("extracted '%s'\n", fname); es_fclose (outfp); if (err && fname && outfp) { if (gnupg_remove (fname)) log_error ("error removing incomplete file '%s': %s\n", fname, gpg_strerror (gpg_error_from_syserror ())); } xfree (fname); return err; } static gpg_error_t extract_directory (const char *dirname, tar_header_t hdr) { gpg_error_t err; char *fname; size_t prefixlen; prefixlen = strlen (dirname) + 1; fname = strconcat (dirname, "/", hdr->name, NULL); if (!fname) { err = gpg_error_from_syserror (); log_error ("error creating filename: %s\n", gpg_strerror (err)); goto leave; } else err = 0; if (fname[strlen (fname)-1] == '/') fname[strlen (fname)-1] = 0; if (! opt.dry_run && gnupg_mkdir (fname, "-rwx------")) { err = gpg_error_from_syserror (); if (gpg_err_code (err) == GPG_ERR_EEXIST) { /* Ignore existing directories while extracting. */ err = 0; } if (gpg_err_code (err) == GPG_ERR_ENOENT) { /* Try to create the directory with parents but keep the original error code in case of a failure. */ char *p; int rc = 0; for (p = fname+prefixlen; (p = strchr (p, '/')); p++) { *p = 0; rc = gnupg_mkdir (fname, "-rwx------"); *p = '/'; if (rc) break; } if (!rc && !gnupg_mkdir (fname, "-rwx------")) err = 0; } if (err) log_error ("error creating directory '%s': %s\n", fname, gpg_strerror (err)); } leave: if (!err && opt.verbose) log_info ("created '%s/'\n", fname); xfree (fname); return err; } static gpg_error_t -extract (estream_t stream, const char *dirname, tar_header_t hdr) +extract (estream_t stream, const char *dirname, tarinfo_t info, + tar_header_t hdr) { gpg_error_t err; size_t n; n = strlen (hdr->name); #ifdef HAVE_DOSISH_SYSTEM if (strchr (hdr->name, '\\')) { log_error ("filename '%s' contains a backslash - " "can't extract on this system\n", hdr->name); return gpg_error (GPG_ERR_INV_NAME); } #endif /*HAVE_DOSISH_SYSTEM*/ if (!n || strstr (hdr->name, "//") || strstr (hdr->name, "/../") || !strncmp (hdr->name, "../", 3) || (n >= 3 && !strcmp (hdr->name+n-3, "/.." ))) { log_error ("filename '%s' as suspicious parts - not extracting\n", hdr->name); return gpg_error (GPG_ERR_INV_NAME); } if (hdr->typeflag == TF_REGULAR || hdr->typeflag == TF_UNKNOWN) - err = extract_regular (stream, dirname, hdr); + err = extract_regular (stream, dirname, info, hdr); else if (hdr->typeflag == TF_DIRECTORY) err = extract_directory (dirname, hdr); else { char record[RECORDSIZE]; log_info ("unsupported file type %d for '%s' - skipped\n", (int)hdr->typeflag, hdr->name); for (err = 0, n=0; !err && n < hdr->nrecords; n++) - err = read_record (stream, record); + { + err = read_record (stream, record); + if (!err) + info->nblocks++; + } } return err; } /* Create a new directory to be used for extracting the tarball. Returns the name of the directory which must be freed by the caller. In case of an error a diagnostic is printed and NULL returned. */ static char * create_directory (const char *dirprefix) { gpg_error_t err = 0; char *prefix_buffer = NULL; char *dirname = NULL; size_t n; int idx; /* Remove common suffixes. */ n = strlen (dirprefix); if (n > 4 && (!compare_filenames (dirprefix + n - 4, EXTSEP_S GPGEXT_GPG) || !compare_filenames (dirprefix + n - 4, EXTSEP_S "pgp") || !compare_filenames (dirprefix + n - 4, EXTSEP_S "asc") || !compare_filenames (dirprefix + n - 4, EXTSEP_S "pem") || !compare_filenames (dirprefix + n - 4, EXTSEP_S "p7m") || !compare_filenames (dirprefix + n - 4, EXTSEP_S "p7e"))) { prefix_buffer = xtrystrdup (dirprefix); if (!prefix_buffer) { err = gpg_error_from_syserror (); goto leave; } prefix_buffer[n-4] = 0; dirprefix = prefix_buffer; } for (idx=1; idx < 5000; idx++) { xfree (dirname); dirname = xtryasprintf ("%s_%d_", dirprefix, idx); if (!dirname) { err = gpg_error_from_syserror (); goto leave; } if (!gnupg_mkdir (dirname, "-rwx------")) goto leave; /* Ready. */ if (errno != EEXIST && errno != ENOTDIR) { err = gpg_error_from_syserror (); goto leave; } } err = gpg_error (GPG_ERR_LIMIT_REACHED); leave: if (err) { log_error ("error creating an extract directory: %s\n", gpg_strerror (err)); xfree (dirname); dirname = NULL; } xfree (prefix_buffer); return dirname; } gpg_error_t gpgtar_extract (const char *filename, int decrypt) { gpg_error_t err; estream_t stream; estream_t cipher_stream = NULL; tar_header_t header = NULL; const char *dirprefix = NULL; char *dirname = 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; } if (opt.directory) dirname = xtrystrdup (opt.directory); else { if (opt.filename) { dirprefix = strrchr (opt.filename, '/'); if (dirprefix) dirprefix++; else dirprefix = opt.filename; } else if (filename) { dirprefix = strrchr (filename, '/'); if (dirprefix) dirprefix++; else dirprefix = filename; } if (!dirprefix || !*dirprefix) dirprefix = "GPGARCH"; dirname = create_directory (dirprefix); if (!dirname) { err = gpg_error (GPG_ERR_GENERAL); goto leave; } } if (opt.verbose) log_info ("extracting to '%s/'\n", dirname); for (;;) { - err = gpgtar_read_header (stream, &header); + err = gpgtar_read_header (stream, tarinfo, &header); if (err || header == NULL) goto leave; - err = extract (stream, dirname, header); + err = extract (stream, dirname, tarinfo, header); if (err) goto leave; xfree (header); header = NULL; } leave: xfree (header); xfree (dirname); if (stream != es_stdin) es_fclose (stream); if (stream != cipher_stream) es_fclose (cipher_stream); return err; } diff --git a/tools/gpgtar-list.c b/tools/gpgtar-list.c index 0e10be8a0..396e837f4 100644 --- a/tools/gpgtar-list.c +++ b/tools/gpgtar-list.c @@ -1,375 +1,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 . */ #include #include #include #include #include #include #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) +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 byte\n", filename); + { + 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 byte\n", - filename); + 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, assming it is a tar header. Returns a 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, tar_header_t *r_header) +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)); + *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, tar_header_t header) +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, &header); + err = read_header (stream, tarinfo, &header); if (err || header == NULL) goto leave; print_header (header, es_stdout); - if (skip_data (stream, header)) + 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, tar_header_t *r_header) +gpgtar_read_header (estream_t stream, tarinfo_t info, tar_header_t *r_header) { - return read_header (stream, 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); } diff --git a/tools/gpgtar.c b/tools/gpgtar.c index 2757ab011..77001dc91 100644 --- a/tools/gpgtar.c +++ b/tools/gpgtar.c @@ -1,597 +1,602 @@ /* gpgtar.c - A simple TAR implementation mainly useful for Windows. * 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 . */ /* GnuPG comes with a shell script gpg-zip which creates archive files in the same format as PGP Zip, which is actually a USTAR format. That is fine and works nicely on all Unices but for Windows we don't have a compatible shell and the supply of tar programs is limited. Given that we need just a few tar option and it is an open question how many Unix concepts are to be mapped to Windows, we might as well write our own little tar customized for use with gpg. So here we go. */ #include #include #include #include #include #include #include #include "../common/util.h" #include "../common/i18n.h" #include "../common/sysutils.h" #include "../common/openpgpdefs.h" #include "../common/init.h" #include "../common/strlist.h" #include "gpgtar.h" /* Constants to identify the commands and options. */ enum cmd_and_opt_values { aNull = 0, aCreate = 600, aExtract, aEncrypt = 'e', aDecrypt = 'd', aSign = 's', aList = 't', oSymmetric = 'c', oRecipient = 'r', oUser = 'u', oOutput = 'o', oDirectory = 'C', oQuiet = 'q', oVerbose = 'v', oFilesFrom = 'T', oNoVerbose = 500, aSignEncrypt, oGpgProgram, oSkipCrypto, oOpenPGP, oCMS, oSetFilename, oNull, /* Compatibility with gpg-zip. */ oGpgArgs, oTarArgs, /* Debugging. */ oDryRun, }; /* The list of commands and options. */ static ARGPARSE_OPTS opts[] = { ARGPARSE_group (300, N_("@Commands:\n ")), ARGPARSE_c (aCreate, "create", N_("create an archive")), ARGPARSE_c (aExtract, "extract", N_("extract an archive")), ARGPARSE_c (aEncrypt, "encrypt", N_("create an encrypted archive")), ARGPARSE_c (aDecrypt, "decrypt", N_("extract an encrypted archive")), ARGPARSE_c (aSign, "sign", N_("create a signed archive")), ARGPARSE_c (aList, "list-archive", N_("list an archive")), ARGPARSE_group (301, N_("@\nOptions:\n ")), ARGPARSE_s_n (oSymmetric, "symmetric", N_("use symmetric encryption")), ARGPARSE_s_s (oRecipient, "recipient", N_("|USER-ID|encrypt for USER-ID")), ARGPARSE_s_s (oUser, "local-user", N_("|USER-ID|use USER-ID to sign or decrypt")), ARGPARSE_s_s (oOutput, "output", N_("|FILE|write output to FILE")), ARGPARSE_s_n (oVerbose, "verbose", N_("verbose")), ARGPARSE_s_n (oQuiet, "quiet", N_("be somewhat more quiet")), ARGPARSE_s_s (oGpgProgram, "gpg", "@"), ARGPARSE_s_n (oSkipCrypto, "skip-crypto", N_("skip the crypto processing")), ARGPARSE_s_n (oDryRun, "dry-run", N_("do not make any changes")), ARGPARSE_s_s (oSetFilename, "set-filename", "@"), ARGPARSE_s_n (oOpenPGP, "openpgp", "@"), ARGPARSE_s_n (oCMS, "cms", "@"), ARGPARSE_group (302, N_("@\nTar options:\n ")), ARGPARSE_s_s (oDirectory, "directory", N_("|DIRECTORY|extract files into DIRECTORY")), ARGPARSE_s_s (oFilesFrom, "files-from", N_("|FILE|get names to create from FILE")), ARGPARSE_s_n (oNull, "null", N_("-T reads null-terminated names")), ARGPARSE_s_s (oGpgArgs, "gpg-args", "@"), ARGPARSE_s_s (oTarArgs, "tar-args", "@"), ARGPARSE_end () }; /* The list of commands and options for tar that we understand. */ static ARGPARSE_OPTS tar_opts[] = { ARGPARSE_s_s (oDirectory, "directory", N_("|DIRECTORY|extract files into DIRECTORY")), ARGPARSE_s_s (oFilesFrom, "files-from", N_("|FILE|get names to create from FILE")), ARGPARSE_s_n (oNull, "null", N_("-T reads null-terminated names")), ARGPARSE_end () }; +/* Global flags. */ +enum cmd_and_opt_values cmd = 0; +int skip_crypto = 0; +const char *files_from = NULL; +int null_names = 0; + + + /* Print usage information and provide strings for help. */ static const char * my_strusage( int level ) { const char *p; switch (level) { case 11: p = "@GPGTAR@ (@GNUPG@)"; break; case 13: p = VERSION; break; case 17: p = PRINTABLE_OS_NAME; break; case 19: p = _("Please report bugs to <@EMAIL@>.\n"); break; case 1: case 40: p = _("Usage: gpgtar [options] [files] [directories] (-h for help)"); break; case 41: p = _("Syntax: gpgtar [options] [files] [directories]\n" "Encrypt or sign files into an archive\n"); break; default: p = NULL; break; } return p; } static void set_cmd (enum cmd_and_opt_values *ret_cmd, enum cmd_and_opt_values new_cmd) { - enum cmd_and_opt_values cmd = *ret_cmd; - - if (!cmd || cmd == new_cmd) - cmd = new_cmd; - else if (cmd == aSign && new_cmd == aEncrypt) - cmd = aSignEncrypt; - else if (cmd == aEncrypt && new_cmd == aSign) - cmd = aSignEncrypt; + enum cmd_and_opt_values c = *ret_cmd; + + if (!c || c == new_cmd) + c = new_cmd; + else if (c == aSign && new_cmd == aEncrypt) + c = aSignEncrypt; + else if (c == aEncrypt && new_cmd == aSign) + c = aSignEncrypt; else { log_error (_("conflicting commands\n")); exit (2); } - *ret_cmd = cmd; + *ret_cmd = c; } + + /* Shell-like argument splitting. For compatibility with gpg-zip we accept arguments for GnuPG and tar given as a string argument to '--gpg-args' and '--tar-args'. gpg-zip was implemented as a Bourne Shell script, and therefore, we need to split the string the same way the shell would. */ static int shell_parse_stringlist (const char *str, strlist_t *r_list) { strlist_t list = NULL; const char *s = str; char quoted = 0; char arg[1024]; char *p = arg; #define addchar(c) \ do { if (p - arg + 2 < sizeof arg) *p++ = (c); else return 1; } while (0) #define addargument() \ do { \ if (p > arg) \ { \ *p = 0; \ append_to_strlist (&list, arg); \ p = arg; \ } \ } while (0) #define unquoted 0 #define singlequote '\'' #define doublequote '"' for (; *s; s++) { switch (quoted) { case unquoted: if (isspace (*s)) addargument (); else if (*s == singlequote || *s == doublequote) quoted = *s; else addchar (*s); break; case singlequote: if (*s == singlequote) quoted = unquoted; else addchar (*s); break; case doublequote: assert (s > str || !"cannot be quoted at first char"); if (*s == doublequote && *(s - 1) != '\\') quoted = unquoted; else addchar (*s); break; default: assert (! "reached"); } } /* Append the last argument. */ addargument (); #undef doublequote #undef singlequote #undef unquoted #undef addargument #undef addchar *r_list = list; return 0; } /* Like shell_parse_stringlist, but returns an argv vector instead of a strlist. */ static int shell_parse_argv (const char *s, int *r_argc, char ***r_argv) { int i; strlist_t list; if (shell_parse_stringlist (s, &list)) return 1; *r_argc = strlist_length (list); *r_argv = xtrycalloc (*r_argc, sizeof **r_argv); if (*r_argv == NULL) return 1; for (i = 0; list; i++) { gpgrt_annotate_leaked_object (list); (*r_argv)[i] = list->d; list = list->next; } gpgrt_annotate_leaked_object (*r_argv); return 0; } - -/* Global flags. */ -enum cmd_and_opt_values cmd = 0; -int skip_crypto = 0; -const char *files_from = NULL; -int null_names = 0; + /* Command line parsing. */ static void parse_arguments (ARGPARSE_ARGS *pargs, ARGPARSE_OPTS *popts) { int no_more_options = 0; while (!no_more_options && optfile_parse (NULL, NULL, NULL, pargs, popts)) { switch (pargs->r_opt) { case oOutput: opt.outfile = pargs->r.ret_str; break; case oDirectory: opt.directory = pargs->r.ret_str; break; case oSetFilename: opt.filename = pargs->r.ret_str; break; case oQuiet: opt.quiet = 1; break; case oVerbose: opt.verbose++; break; case oNoVerbose: opt.verbose = 0; break; case oFilesFrom: files_from = pargs->r.ret_str; break; case oNull: null_names = 1; break; case aList: case aDecrypt: case aEncrypt: case aSign: set_cmd (&cmd, pargs->r_opt); break; case aCreate: set_cmd (&cmd, aEncrypt); skip_crypto = 1; break; case aExtract: set_cmd (&cmd, aDecrypt); skip_crypto = 1; break; case oRecipient: add_to_strlist (&opt.recipients, pargs->r.ret_str); break; case oUser: opt.user = pargs->r.ret_str; break; case oSymmetric: set_cmd (&cmd, aEncrypt); opt.symmetric = 1; break; case oGpgProgram: opt.gpg_program = pargs->r.ret_str; break; case oSkipCrypto: skip_crypto = 1; break; case oOpenPGP: /* Dummy option for now. */ break; case oCMS: /* Dummy option for now. */ break; case oGpgArgs:; { strlist_t list; if (shell_parse_stringlist (pargs->r.ret_str, &list)) log_error ("failed to parse gpg arguments '%s'\n", pargs->r.ret_str); else { if (opt.gpg_arguments) strlist_last (opt.gpg_arguments)->next = list; else opt.gpg_arguments = list; } } break; case oTarArgs:; { int tar_argc; char **tar_argv; if (shell_parse_argv (pargs->r.ret_str, &tar_argc, &tar_argv)) log_error ("failed to parse tar arguments '%s'\n", pargs->r.ret_str); else { ARGPARSE_ARGS tar_args; tar_args.argc = &tar_argc; tar_args.argv = &tar_argv; tar_args.flags = ARGPARSE_FLAG_ARG0; parse_arguments (&tar_args, tar_opts); if (tar_args.err) log_error ("unsupported tar arguments '%s'\n", pargs->r.ret_str); pargs->err = tar_args.err; } } break; case oDryRun: opt.dry_run = 1; break; default: pargs->err = 2; break; } } } /* gpgtar main. */ int main (int argc, char **argv) { gpg_error_t err; const char *fname; ARGPARSE_ARGS pargs; assert (sizeof (struct ustar_raw_header) == 512); gnupg_reopen_std (GPGTAR_NAME); set_strusage (my_strusage); log_set_prefix (GPGTAR_NAME, GPGRT_LOG_WITH_PREFIX); /* Make sure that our subsystems are ready. */ i18n_init(); init_common_subsystems (&argc, &argv); /* Parse the command line. */ pargs.argc = &argc; pargs.argv = &argv; pargs.flags = ARGPARSE_FLAG_KEEP; parse_arguments (&pargs, opts); if ((files_from && !null_names) || (!files_from && null_names)) log_error ("--files-from and --null may only be used in conjunction\n"); if (files_from && strcmp (files_from, "-")) log_error ("--files-from only supports argument \"-\"\n"); if (log_get_errorcount (0)) exit (2); /* Print a warning if an argument looks like an option. */ if (!opt.quiet && !(pargs.flags & ARGPARSE_FLAG_STOP_SEEN)) { int i; for (i=0; i < argc; i++) if (argv[i][0] == '-' && argv[i][1] == '-') log_info (_("NOTE: '%s' is not considered an option\n"), argv[i]); } if (! opt.gpg_program) opt.gpg_program = gnupg_module_name (GNUPG_MODULE_NAME_GPG); if (opt.verbose > 1) opt.debug_level = 1024; switch (cmd) { case aList: if (argc > 1) usage (1); fname = argc ? *argv : NULL; if (opt.filename) log_info ("note: ignoring option --set-filename\n"); if (files_from) log_info ("note: ignoring option --files-from\n"); err = gpgtar_list (fname, !skip_crypto); if (err && log_get_errorcount (0) == 0) log_error ("listing archive failed: %s\n", gpg_strerror (err)); break; case aEncrypt: case aSign: case aSignEncrypt: if ((!argc && !null_names) || (argc && null_names)) usage (1); if (opt.filename) log_info ("note: ignoring option --set-filename\n"); err = gpgtar_create (null_names? NULL :argv, !skip_crypto && (cmd == aEncrypt || cmd == aSignEncrypt), cmd == aSign || cmd == aSignEncrypt); if (err && log_get_errorcount (0) == 0) log_error ("creating archive failed: %s\n", gpg_strerror (err)); break; case aDecrypt: if (argc != 1) usage (1); if (opt.outfile) log_info ("note: ignoring option --output\n"); if (files_from) log_info ("note: ignoring option --files-from\n"); fname = argc ? *argv : NULL; err = gpgtar_extract (fname, !skip_crypto); if (err && log_get_errorcount (0) == 0) log_error ("extracting archive failed: %s\n", gpg_strerror (err)); break; default: log_error (_("invalid command (there is no implicit command)\n")); break; } return log_get_errorcount (0)? 1:0; } /* Read the next record from STREAM. RECORD is a buffer provided by the caller and must be at leadt of size RECORDSIZE. The function return 0 on success and error code on failure; a diagnostic printed as well. Note that there is no need for an EOF indicator because a tarball has an explicit EOF record. */ gpg_error_t read_record (estream_t stream, void *record) { gpg_error_t err; size_t nread; nread = es_fread (record, 1, RECORDSIZE, stream); if (nread != RECORDSIZE) { err = gpg_error_from_syserror (); if (es_ferror (stream)) log_error ("error reading '%s': %s\n", es_fname_get (stream), gpg_strerror (err)); else log_error ("error reading '%s': premature EOF " "(size of last record: %zu)\n", es_fname_get (stream), nread); } else err = 0; return err; } /* Write the RECORD of size RECORDSIZE to STREAM. FILENAME is the name of the file used for diagnostics. */ gpg_error_t write_record (estream_t stream, const void *record) { gpg_error_t err; size_t nwritten; nwritten = es_fwrite (record, 1, RECORDSIZE, stream); if (nwritten != RECORDSIZE) { err = gpg_error_from_syserror (); log_error ("error writing '%s': %s\n", es_fname_get (stream), gpg_strerror (err)); } else err = 0; return err; } /* Return true if FP is an unarmored OpenPGP message. Note that this function reads a few bytes from FP but pushes them back. */ #if 0 static int openpgp_message_p (estream_t fp) { int ctb; ctb = es_getc (fp); if (ctb != EOF) { if (es_ungetc (ctb, fp)) log_fatal ("error ungetting first byte: %s\n", gpg_strerror (gpg_error_from_syserror ())); if ((ctb & 0x80)) { switch ((ctb & 0x40) ? (ctb & 0x3f) : ((ctb>>2)&0xf)) { case PKT_MARKER: case PKT_SYMKEY_ENC: case PKT_ONEPASS_SIG: case PKT_PUBKEY_ENC: case PKT_SIGNATURE: case PKT_COMMENT: case PKT_OLD_COMMENT: case PKT_PLAINTEXT: case PKT_COMPRESSED: case PKT_ENCRYPTED: return 1; /* Yes, this seems to be an OpenPGP message. */ default: break; } } } return 0; } #endif diff --git a/tools/gpgtar.h b/tools/gpgtar.h index 28d3d88b1..1a1b913d7 100644 --- a/tools/gpgtar.h +++ b/tools/gpgtar.h @@ -1,133 +1,143 @@ /* gpgtar.h - Global definitions for gpgtar * 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 . */ #ifndef GPGTAR_H #define GPGTAR_H #include "../common/util.h" #include "../common/strlist.h" /* We keep all global options in the structure OPT. */ struct { int verbose; unsigned int debug_level; int quiet; int dry_run; const char *gpg_program; strlist_t gpg_arguments; const char *outfile; strlist_t recipients; const char *user; int symmetric; const char *filename; const char *directory; } opt; +/* An info structure to avoid global variables. */ +struct tarinfo_s +{ + unsigned long long nblocks; /* Count of processed blocks. */ + unsigned long long headerblock; /* Number of current header block. */ +}; +typedef struct tarinfo_s *tarinfo_t; + + /* The size of a tar record. All IO is done in chunks of this size. Note that we don't care about blocking because this version of tar is not expected to be used directly on a tape drive in fact it is used in a pipeline with GPG and thus any blocking would be useless. */ -#define RECORDSIZE 512 +#define RECORDSIZE 512 /* Description of the USTAR header format. */ struct ustar_raw_header { char name[100]; char mode[8]; char uid[8]; char gid[8]; char size[12]; char mtime[12]; char checksum[8]; char typeflag[1]; char linkname[100]; char magic[6]; char version[2]; char uname[32]; - char gname[32]; - char devmajor[8]; + char gname[32]; + char devmajor[8]; char devminor[8]; - char prefix[155]; + char prefix[155]; char pad[12]; }; /* Filetypes as defined by USTAR. */ -typedef enum +typedef enum { TF_REGULAR, TF_HARDLINK, TF_SYMLINK, TF_CHARDEV, TF_BLOCKDEV, TF_DIRECTORY, TF_FIFO, TF_RESERVED, TF_UNKNOWN, /* Needs to be treated as regular file. */ TF_NOTSUP /* Not supported (used with --create). */ } typeflag_t; /* The internal representation of a TAR header. */ struct tar_header_s; typedef struct tar_header_s *tar_header_t; struct tar_header_s { - tar_header_t next; /* Used to build a linked list iof entries. */ + tar_header_t next; /* Used to build a linked list of entries. */ unsigned long mode; /* The file mode. */ unsigned long nlink; /* Number of hard links. */ unsigned long uid; /* The user id of the file. */ unsigned long gid; /* The group id of the file. */ unsigned long long size; /* The size of the file. */ unsigned long long mtime; /* Modification time since Epoch. Note that we don't use time_t here but a type which is more likely to be larger that 32 bit and thus allows tracking times beyond 2106. */ typeflag_t typeflag; /* The type of the file. */ - + unsigned long long nrecords; /* Number of data records. */ char name[1]; /* Filename (dynamically extended). */ }; /*-- gpgtar.c --*/ gpg_error_t read_record (estream_t stream, void *record); gpg_error_t write_record (estream_t stream, const void *record); /*-- gpgtar-create.c --*/ gpg_error_t gpgtar_create (char **inpattern, int encrypt, int sign); /*-- gpgtar-extract.c --*/ gpg_error_t gpgtar_extract (const char *filename, int decrypt); /*-- gpgtar-list.c --*/ gpg_error_t gpgtar_list (const char *filename, int decrypt); -gpg_error_t gpgtar_read_header (estream_t stream, tar_header_t *r_header); +gpg_error_t gpgtar_read_header (estream_t stream, tarinfo_t info, + tar_header_t *r_header); void gpgtar_print_header (tar_header_t header, estream_t out); #endif /*GPGTAR_H*/