From 40dbee86f3043aff8a8c2055521e270318e33068 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Tue, 24 Nov 2015 18:39:30 +0100 Subject: tools: Add encryption and decryption support to gpgtar. * tools/Makefile.am: Amend CFLAGS and LDADD. * tools/gpgtar-create.c (gpgtar_create): Add encrypt flag and encrypt stream if requested. * tools/gpgtar-extract.c (gpgtar_extract): Likewise for decryption. * tools/gpgtar-list.c (gpgtar_list): Likewise. * tools/gpgtar.c (main): Initialize npth and assuan. Parse recipient and local user, and note which flags are currently ignored. Adapt calls to gpgtar_list and friends. (tar_and_encrypt): Drop stub function and prototype. (decrypt_and_untar): Likewise. (decrypt_and_list): Likewise. * tools/gpgtar.h (gpgtar_{create,extract,list}): Add encryption or decryption argument. Signed-off-by: Justus Winter --- tools/gpgtar-create.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'tools/gpgtar-create.c') diff --git a/tools/gpgtar-create.c b/tools/gpgtar-create.c index fad6d57ae..59b88bfb9 100644 --- a/tools/gpgtar-create.c +++ b/tools/gpgtar-create.c @@ -36,6 +36,7 @@ #include #include "i18n.h" +#include "../common/call-gpg.h" #include "../common/sysutils.h" #include "gpgtar.h" @@ -740,13 +741,14 @@ write_eof_mark (estream_t stream) INPATTERN is NULL take the pattern as null terminated strings from stdin. */ void -gpgtar_create (char **inpattern) +gpgtar_create (char **inpattern, int encrypt) { gpg_error_t err = 0; struct scanctrl_s scanctrl_buffer; scanctrl_t scanctrl = &scanctrl_buffer; tar_header_t hdr, *start_tail; estream_t outstream = NULL; + estream_t cipher_stream = NULL; int eof_seen = 0; if (!inpattern) @@ -863,6 +865,17 @@ gpgtar_create (char **inpattern) if (outstream == es_stdout) es_set_binary (es_stdout); + if (encrypt) + { + cipher_stream = outstream; + outstream = es_fopenmem (0, "rwb"); + if (! outstream) + { + err = gpg_error_from_syserror (); + goto leave; + } + } + for (hdr = scanctrl->flist; hdr; hdr = hdr->next) { err = write_file (outstream, hdr); @@ -870,6 +883,22 @@ gpgtar_create (char **inpattern) goto leave; } err = write_eof_mark (outstream); + if (err) + goto leave; + + if (encrypt) + { + err = es_fseek (outstream, 0, SEEK_SET); + if (err) + goto leave; + + err = gpg_encrypt_stream (NULL, NULL, + outstream, + opt.recipients, + cipher_stream); + if (err) + goto leave; + } leave: if (!err) @@ -879,6 +908,11 @@ gpgtar_create (char **inpattern) else err = es_fflush (outstream); outstream = NULL; + if (cipher_stream != es_stdout) + err = es_fclose (cipher_stream); + else + err = es_fflush (cipher_stream); + cipher_stream = NULL; } if (err) { @@ -886,6 +920,8 @@ gpgtar_create (char **inpattern) es_fname_get (outstream), gpg_strerror (err)); if (outstream && outstream != es_stdout) es_fclose (outstream); + if (cipher_stream && cipher_stream != es_stdout) + es_fclose (cipher_stream); if (opt.outfile) gnupg_remove (opt.outfile); } -- cgit v1.2.3