1 /* gpgtar-extract.c - Extract from a TAR archive
2 * Copyright (C) 2010 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
25 #include <sys/types.h>
31 #include "../common/exectool.h"
32 #include "../common/sysutils.h"
33 #include "../common/ccparray.h"
38 extract_regular (estream_t stream, const char *dirname,
42 char record[RECORDSIZE];
43 size_t n, nbytes, nwritten;
45 estream_t outfp = NULL;
47 fname = strconcat (dirname, "/", hdr->name, NULL);
50 err = gpg_error_from_syserror ();
51 log_error ("error creating filename: %s\n", gpg_strerror (err));
58 outfp = es_fopenmem (0, "wb");
60 outfp = es_fopen (fname, "wb");
63 err = gpg_error_from_syserror ();
64 log_error ("error creating '%s': %s\n", fname, gpg_strerror (err));
68 for (n=0; n < hdr->nrecords;)
70 err = read_record (stream, record);
74 if (n < hdr->nrecords || (hdr->size && !(hdr->size % RECORDSIZE)))
77 nbytes = (hdr->size % RECORDSIZE);
79 nwritten = es_fwrite (record, 1, nbytes, outfp);
80 if (nwritten != nbytes)
82 err = gpg_error_from_syserror ();
83 log_error ("error writing '%s': %s\n", fname, gpg_strerror (err));
87 /* Fixme: Set permissions etc. */
90 if (!err && opt.verbose)
91 log_info ("extracted '%s'\n", fname);
93 if (err && fname && outfp)
95 if (gnupg_remove (fname))
96 log_error ("error removing incomplete file '%s': %s\n",
97 fname, gpg_strerror (gpg_error_from_syserror ()));
105 extract_directory (const char *dirname, tar_header_t hdr)
111 prefixlen = strlen (dirname) + 1;
112 fname = strconcat (dirname, "/", hdr->name, NULL);
115 err = gpg_error_from_syserror ();
116 log_error ("error creating filename: %s\n", gpg_strerror (err));
122 if (fname[strlen (fname)-1] == '/')
123 fname[strlen (fname)-1] = 0;
125 if (! opt.dry_run && gnupg_mkdir (fname, "-rwx------"))
127 err = gpg_error_from_syserror ();
128 if (gpg_err_code (err) == GPG_ERR_EEXIST)
130 /* Ignore existing directories while extracting. */
134 if (gpg_err_code (err) == GPG_ERR_ENOENT)
136 /* Try to create the directory with parents but keep the
137 original error code in case of a failure. */
141 for (p = fname+prefixlen; (p = strchr (p, '/')); p++)
144 rc = gnupg_mkdir (fname, "-rwx------");
149 if (!rc && !gnupg_mkdir (fname, "-rwx------"))
153 log_error ("error creating directory '%s': %s\n",
154 fname, gpg_strerror (err));
158 if (!err && opt.verbose)
159 log_info ("created '%s/'\n", fname);
166 extract (estream_t stream, const char *dirname, tar_header_t hdr)
171 n = strlen (hdr->name);
172 #ifdef HAVE_DOSISH_SYSTEM
173 if (strchr (hdr->name, '\\'))
175 log_error ("filename '%s' contains a backslash - "
176 "can't extract on this system\n", hdr->name);
177 return gpg_error (GPG_ERR_INV_NAME);
179 #endif /*HAVE_DOSISH_SYSTEM*/
182 || strstr (hdr->name, "//")
183 || strstr (hdr->name, "/../")
184 || !strncmp (hdr->name, "../", 3)
185 || (n >= 3 && !strcmp (hdr->name+n-3, "/.." )))
187 log_error ("filename '%s' as suspicious parts - not extracting\n",
189 return gpg_error (GPG_ERR_INV_NAME);
192 if (hdr->typeflag == TF_REGULAR || hdr->typeflag == TF_UNKNOWN)
193 err = extract_regular (stream, dirname, hdr);
194 else if (hdr->typeflag == TF_DIRECTORY)
195 err = extract_directory (dirname, hdr);
198 char record[RECORDSIZE];
200 log_info ("unsupported file type %d for '%s' - skipped\n",
201 (int)hdr->typeflag, hdr->name);
202 for (err = 0, n=0; !err && n < hdr->nrecords; n++)
203 err = read_record (stream, record);
209 /* Create a new directory to be used for extracting the tarball.
210 Returns the name of the directory which must be freed by the
211 caller. In case of an error a diagnostic is printed and NULL
214 create_directory (const char *dirprefix)
217 char *prefix_buffer = NULL;
218 char *dirname = NULL;
222 /* Remove common suffixes. */
223 n = strlen (dirprefix);
224 if (n > 4 && (!compare_filenames (dirprefix + n - 4, EXTSEP_S GPGEXT_GPG)
225 || !compare_filenames (dirprefix + n - 4, EXTSEP_S "pgp")
226 || !compare_filenames (dirprefix + n - 4, EXTSEP_S "asc")
227 || !compare_filenames (dirprefix + n - 4, EXTSEP_S "pem")
228 || !compare_filenames (dirprefix + n - 4, EXTSEP_S "p7m")
229 || !compare_filenames (dirprefix + n - 4, EXTSEP_S "p7e")))
231 prefix_buffer = xtrystrdup (dirprefix);
234 err = gpg_error_from_syserror ();
237 prefix_buffer[n-4] = 0;
238 dirprefix = prefix_buffer;
243 for (idx=1; idx < 5000; idx++)
246 dirname = xtryasprintf ("%s_%d_", dirprefix, idx);
249 err = gpg_error_from_syserror ();
252 if (!gnupg_mkdir (dirname, "-rwx------"))
253 goto leave; /* Ready. */
254 if (errno != EEXIST && errno != ENOTDIR)
256 err = gpg_error_from_syserror ();
260 err = gpg_error (GPG_ERR_LIMIT_REACHED);
265 log_error ("error creating an extract directory: %s\n",
270 xfree (prefix_buffer);
277 gpgtar_extract (const char *filename, int decrypt)
281 estream_t cipher_stream = NULL;
282 tar_header_t header = NULL;
283 const char *dirprefix = NULL;
284 char *dirname = NULL;
288 if (!strcmp (filename, "-"))
291 stream = es_fopen (filename, "rb");
294 err = gpg_error_from_syserror ();
295 log_error ("error opening '%s': %s\n", filename, gpg_strerror (err));
302 if (stream == es_stdin)
303 es_set_binary (es_stdin);
311 cipher_stream = stream;
312 stream = es_fopenmem (0, "rwb");
315 err = gpg_error_from_syserror ();
319 ccparray_init (&ccp, 0);
321 ccparray_put (&ccp, "--decrypt");
322 for (arg = opt.gpg_arguments; arg; arg = arg->next)
323 ccparray_put (&ccp, arg->d);
325 ccparray_put (&ccp, NULL);
326 argv = ccparray_get (&ccp, NULL);
329 err = gpg_error_from_syserror ();
333 err = gnupg_exec_tool_stream (opt.gpg_program, argv,
334 cipher_stream, NULL, stream, NULL, NULL);
339 err = es_fseek (stream, 0, SEEK_SET);
345 dirname = xtrystrdup (opt.directory);
350 dirprefix = strrchr (filename, '/');
354 dirprefix = filename;
356 else if (opt.filename)
358 dirprefix = strrchr (opt.filename, '/');
362 dirprefix = opt.filename;
365 if (!dirprefix || !*dirprefix)
366 dirprefix = "GPGARCH";
368 dirname = create_directory (dirprefix);
371 err = gpg_error (GPG_ERR_GENERAL);
377 log_info ("extracting to '%s/'\n", dirname);
381 err = gpgtar_read_header (stream, &header);
382 if (err || header == NULL)
385 err = extract (stream, dirname, header);
396 if (stream != es_stdin)
398 if (stream != cipher_stream)
399 es_fclose (cipher_stream);