aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xicevault68
1 files changed, 29 insertions, 39 deletions
diff --git a/icevault b/icevault
index d454a18..943f1bf 100755
--- a/icevault
+++ b/icevault
@@ -265,41 +265,54 @@ sub getIdentityFile($) {
}
# Decrypt the given identity file and return the YAML-parsed form.
-sub loadIdentityFile($) {
- my $filename = shift;
+open my $NULL, '<', '/dev/null';
+sub loadIdentityFile($;$) {
+ my ($filename, $fh) = @_;
myprintf \*STDERR, "Decrypting identity file C<%s>", $filename if $CONFIG{debug};
- require 'YAML/Tiny.pm'; # XXX use Tiny::YAML instead?
- my $pid = open my $fh, '-|', $CONFIG{gpg}, qw/-o - --decrypt --/, $filename
- or error "Can't fork: %s", $!;
- my $str = do { local $/ = undef; <$fh> };
+ require 'IPC/Open2.pm';
+ my $pid = IPC::Open2::open2( (defined wantarray ? $fh : ">&".$fh->fileno)
+ , "<&".fileno($NULL)
+ , $CONFIG{gpg}, qw/-o - --decrypt --/, $filename)
+ or error "Can't fork: %s", $!;
+ my $str = do { local $/ = undef; <$fh> } if defined wantarray;
waitpid $pid, 0;
error "C<%s> exited with value %d", $CONFIG{gpg}, ($? >> 8) if $? and $? != -1;
close $fh;
+ return unless defined wantarray;
+
# the cleartext's charset is always UTF8
- return YAML::Tiny::Load(decode_utf8 $str);
+ require 'YAML/Tiny.pm'; # XXX use Tiny::YAML instead?
+ return YAML::Tiny::Load(decode_utf8 $str) if defined wantarray;
}
# Dump and encrypt a form into the given filename.
sub saveIdentityFile($$) {
my ($form, $filename) = @_;
- $form->{fields} = [ grep defined, @{$form->{fields}} ]; # remove undefined fields
myprintf \*STDERR, "Saving identity file C<%s>", $filename if $CONFIG{debug};
+
require 'File/Copy.pm';
require 'File/Path.pm';
require 'File/Temp.pm';
require 'IPC/Open2.pm';
- require 'YAML/Tiny.pm'; # XXX use Tiny::YAML instead?
+ require 'YAML/Tiny.pm' if ref $form; # XXX use Tiny::YAML instead?
# don't encrypt directly into the destination file so we don't
# end up with a messed up file if something goes wrong
my $outfh = File::Temp->new(SUFFIX => '.gpg', UNLINK => 0, TMPDIR => 1);
- my $pid = IPC::Open2::open2(">&".$outfh->fileno, my $infh, $CONFIG{gpg},
- qw/-o - --no-encrypt-to --recipient/, $CONFIG{keyid}, '--encrypt')
- or error "Can't fork: %s", $!;
- print $infh encode_utf8(YAML::Tiny::Dump($form)); # dump the form as UTF8
- close $infh;
+ my $pid = IPC::Open2::open2( ">&".$outfh->fileno
+ , (ref $form ? my $infh : "<&".fileno($NULL))
+ , $CONFIG{gpg}, qw/-o - --no-encrypt-to --recipient/, $CONFIG{keyid}
+ , '--encrypt', '--', (ref $form ? () : $form)
+ )
+ or error "Can't fork: %s", $!;
+
+ if (ref $form) {
+ $form->{fields} = [ grep defined, @{$form->{fields}} ]; # remove undefined fields
+ print $infh encode_utf8(YAML::Tiny::Dump($form)); # dump the form as UTF8
+ close $infh;
+ }
waitpid $pid, 0;
error "C<%s> exited with value %d", $CONFIG{gpg}, ($? >> 8) if $? and $? != -1;
$outfh->close;
@@ -736,16 +749,7 @@ elsif ($command eq 'edit') {
my $fh = File::Temp->new(SUFFIX => '.yaml', UNLINK => 0, TMPDIR => 1);
END { unlink $fh->filename if defined $fh; } # never leave cleartext lying around
- myprintf \*STDERR, "Decrypting identity file C<%s>", $filename if $CONFIG{debug};
-
- # XXX use loadIdentityFile
- open my $NULL, '<', '/dev/null';
- require 'IPC/Open2.pm';
- my $pid = IPC::Open2::open2(">&".$fh->fileno, "<&".fileno($NULL), $CONFIG{gpg}, qw/-o - --decrypt --/, $filename)
- or error "Can't fork: %s", $!;
- waitpid $pid, 0;
- error "C<%s> exited with value %d", $CONFIG{gpg}, ($? >> 8) if $? and $? != -1;
- $fh->close;
+ loadIdentityFile $filename, $fh;
my $h = sha256_file $fh->filename;
system $EDITOR, $fh->filename;
@@ -755,23 +759,9 @@ elsif ($command eq 'edit') {
print "No modification made\n";
}
else {
- # XXX use saveIdentityFile
- # don't encrypt directly into the destination file so we don't
- # end up with a messed up file if something goes wrong
myprintf "Saving user changes for identity C<%s>", $id;
- my $outfh = File::Temp->new(SUFFIX => '.gpg', UNLINK => 0, TMPDIR => 1);
- my $pid = open2(">&".$outfh->fileno, "<&".fileno($NULL),
- $CONFIG{gpg}, qw/-o - --no-encrypt-to --recipient/, $CONFIG{keyid},
- '--encrypt', '--', $fh->filename)
- or error "Can't fork: %s", $!;
- waitpid $pid, 0;
- error "C<%s> exited with value %d", $CONFIG{gpg}, ($? >> 8) if $? and $? != -1;
- $outfh->close;
-
- File::Copy::move($outfh->filename, $file) or error "Can't move C<%s>: %s", $outfh->filename, $!;
+ saveIdentityFile( $fh->filename, $filename);
}
-
- close $NULL;
}
elsif ($command eq 'clip') {