initial support for export

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-05-13 17:57:14 +04:00
parent c8b9949bdf
commit 52c4bd5e0a
4 changed files with 45 additions and 6 deletions

19
GPG.pm
View File

@ -90,4 +90,23 @@ sub decrypt_db {
return $file; 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; 1;

View File

@ -81,6 +81,19 @@ sub remove {
return 0; 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 # Decrypt base and store new password
sub save { sub save {
my ( $self, $store ) = @_; my ( $self, $store ) = @_;
@ -90,9 +103,9 @@ sub save {
my $name = $store->{name}; my $name = $store->{name};
my $resource = $store->{resource}; my $resource = $store->{resource};
my $password = $store->{password}; my $password = $store->{password};
# Comment check # Comment check
my $comment = ''; my $comment = '';
if ( defined( $store->{comment} ) ) { if ( defined( $store->{comment} ) ) {
$comment = $store->{comment}; $comment = $store->{comment};
} }
@ -105,7 +118,8 @@ sub save {
# Decrypt database # Decrypt database
my $dec_db_file = $gpg->decrypt_db(); 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')"; values('$name', '$resource', '$password', '$username', '$comment')";
my $mdo_q = { my $mdo_q = {
file => $dec_db_file, file => $dec_db_file,

View File

@ -24,6 +24,7 @@ Simple password manager writed in Perl.
-r remove password -r remove password
-i password ID -i password ID
-o open link -o open link
-x [filename] export
-h show this help screen and exit -h show this help screen and exit
-v show version info and exit -v show version info and exit

11
pm.pl
View File

@ -15,11 +15,11 @@ our $VERSION = '0.0.1-beta1';
my $usage = Usage->new(); my $usage = Usage->new();
sub init() { 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(); getopts("$opt_string") or $usage->show();
our ( our (
$opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h,
$opt_h, $opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_x,
); );
print "Simple password manager writed in Perl.\nVersion: " print "Simple password manager writed in Perl.\nVersion: "
@ -130,6 +130,11 @@ elsif ( defined($opt_w)
print "Password was stored into DB!\n"; print "Password was stored into DB!\n";
print color 'reset'; print color 'reset';
} }
# Export
elsif ( defined($opt_x) ) {
$pass->export($opt_x);
print colored("Dabase stored in $opt_x\n", 'green');
}
else { else {
$usage->show(); $usage->show();
} }