From 52c4bd5e0a1cc2f0574f452d8f5bc36f633e2b59 Mon Sep 17 00:00:00 2001 From: "Difrex(Denis Zheleztsov)" Date: Tue, 13 May 2014 17:57:14 +0400 Subject: [PATCH] initial support for export --- GPG.pm | 19 +++++++++++++++++++ Password.pm | 20 +++++++++++++++++--- Usage.pm | 1 + pm.pl | 11 ++++++++--- 4 files changed, 45 insertions(+), 6 deletions(-) diff --git a/GPG.pm b/GPG.pm index 82cf004..88ecc5f 100644 --- a/GPG.pm +++ b/GPG.pm @@ -90,4 +90,23 @@ sub decrypt_db { return $file; } +sub export { + my ($self, $file) = @_; + + use Term::ANSIColor; + print "Password for " . colored("export\n", 'yellow'); + + # gpg --symmetric filename + my @enc_cmd = ('gpg', '--symmetric', "$file"); + system(@enc_cmd) == 0 or die "Cannot encrypt $file: $!\n"; + + # Remove unencrypted file + my @rm_cmd = ('rm', '-f', "$file"); + system(@rm_cmd) == 0 or die "Cannot remove file $file: $!\n"; + + my $export_file = $file . '.gpg'; + + return $export_file; +} + 1; diff --git a/Password.pm b/Password.pm index e2c12eb..102df30 100644 --- a/Password.pm +++ b/Password.pm @@ -81,6 +81,19 @@ sub remove { return 0; } +sub export { + my ( $self, $filename ) = @_; + my $gpg = $self->{_gpg}; + + my $dec_db_file = $gpg->decrypt_db(); + my $export_enc = $gpg->export($dec_db_file); + + my @mv_cmd = ( 'mv', "$export_enc", "$filename" ); + system(@mv_cmd) == 0 or die "Cannot move $export_enc to $filename: $!\n"; + + return 0; +} + # Decrypt base and store new password sub save { my ( $self, $store ) = @_; @@ -90,9 +103,9 @@ sub save { my $name = $store->{name}; my $resource = $store->{resource}; my $password = $store->{password}; - + # Comment check - my $comment = ''; + my $comment = ''; if ( defined( $store->{comment} ) ) { $comment = $store->{comment}; } @@ -105,7 +118,8 @@ sub save { # Decrypt database my $dec_db_file = $gpg->decrypt_db(); - my $q = "insert into passwords(name, resource, password, username, comment) + my $q + = "insert into passwords(name, resource, password, username, comment) values('$name', '$resource', '$password', '$username', '$comment')"; my $mdo_q = { file => $dec_db_file, diff --git a/Usage.pm b/Usage.pm index f5efd61..130b968 100644 --- a/Usage.pm +++ b/Usage.pm @@ -24,6 +24,7 @@ Simple password manager writed in Perl. -r remove password -i password ID -o open link + -x [filename] export -h show this help screen and exit -v show version info and exit diff --git a/pm.pl b/pm.pl index 68cfa67..952b865 100755 --- a/pm.pl +++ b/pm.pl @@ -15,11 +15,11 @@ our $VERSION = '0.0.1-beta1'; my $usage = Usage->new(); sub init() { - my $opt_string = 'swn:l:p:rhvou:i:c:'; + my $opt_string = 'swn:l:p:rhvou:i:c:x:'; getopts("$opt_string") or $usage->show(); our ( - $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, - $opt_h, $opt_v, $opt_o, $opt_u, $opt_i, $opt_c, + $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h, + $opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_x, ); print "Simple password manager writed in Perl.\nVersion: " @@ -130,6 +130,11 @@ elsif ( defined($opt_w) print "Password was stored into DB!\n"; print color 'reset'; } +# Export +elsif ( defined($opt_x) ) { + $pass->export($opt_x); + print colored("Dabase stored in $opt_x\n", 'green'); +} else { $usage->show(); }