2014-04-28 14:07:01 +04:00
|
|
|
# GPG layer for encrypt/decrypt passwords database
|
2014-04-28 11:51:50 +04:00
|
|
|
package GPG;
|
|
|
|
|
2014-05-06 00:40:26 +04:00
|
|
|
# Debug
|
|
|
|
use Data::Dumper;
|
2014-04-28 11:51:50 +04:00
|
|
|
|
|
|
|
sub new {
|
2014-04-28 14:07:01 +04:00
|
|
|
my $class = shift;
|
|
|
|
|
2014-05-06 00:40:26 +04:00
|
|
|
my $home = $ENV{HOME};
|
2014-04-28 14:07:01 +04:00
|
|
|
my $db = $home . "/.PM/db.sqlite";
|
|
|
|
|
2014-05-10 20:52:30 +04:00
|
|
|
my $self = {
|
|
|
|
_db => $db,
|
|
|
|
_home => $home,
|
|
|
|
};
|
2014-04-28 11:51:50 +04:00
|
|
|
|
|
|
|
bless $self, $class;
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2014-04-28 14:07:01 +04:00
|
|
|
# Encrypt sqlite database with default key
|
|
|
|
# and save it in config dir
|
|
|
|
sub encrypt_db {
|
|
|
|
my ( $self, $file ) = @_;
|
|
|
|
my $db = $self->{_db};
|
|
|
|
|
2014-04-28 16:13:19 +04:00
|
|
|
# Remove old database
|
|
|
|
@rm_db = ( "rm", "-f", "$db" );
|
|
|
|
system(@rm_db) == 0 or die "Cannot remove old database: $!\n";
|
|
|
|
|
2014-05-10 20:52:30 +04:00
|
|
|
# Keys selection.
|
|
|
|
my @enc_cmd;
|
|
|
|
my $recipient;
|
|
|
|
if ( -e $self->{_home} . "/.PM/.key" ) {
|
|
|
|
open my $key_f, "<" , $self->{_home} . "/.PM/.key"
|
|
|
|
or die "Cannot open file: $!\n";
|
|
|
|
while ( <$key_f> ) {
|
|
|
|
$recipient = $_;
|
|
|
|
}
|
|
|
|
@enc_cmd = (
|
|
|
|
"gpg", "--output", "$db",
|
|
|
|
"-a", "--recipient", "$recipient",
|
|
|
|
"--encrypt", "$file",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# gpg --output test.gpg --encrypt test -a --default-recipient-self
|
|
|
|
@enc_cmd = (
|
|
|
|
"gpg", "--output", "$db",
|
|
|
|
"-a", "--default-recipient-self",
|
|
|
|
"--encrypt", "$file",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-28 16:13:19 +04:00
|
|
|
system(@enc_cmd) == 0
|
|
|
|
or die "Cannot encrypt!\nDecrypted file: $file\nTraceback: $!\n";
|
2014-04-28 14:07:01 +04:00
|
|
|
|
|
|
|
# Remove unencrypted file
|
2014-04-28 16:13:19 +04:00
|
|
|
@rm_cmd = ( "rm", "-f", "$file" );
|
2014-04-28 14:07:01 +04:00
|
|
|
system(@rm_cmd) == 0 or die "Cannot remove file $file: $!\n";
|
2014-04-28 16:13:19 +04:00
|
|
|
|
|
|
|
return 0;
|
2014-04-28 14:07:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Decrypt database, save it in new place
|
|
|
|
# and return path to file
|
|
|
|
sub decrypt_db {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $db = $self->{_db};
|
|
|
|
|
2014-05-06 00:40:26 +04:00
|
|
|
my $gpg = '/usr/bin/gpg';
|
|
|
|
|
2014-04-28 14:07:01 +04:00
|
|
|
# Generate random file name
|
|
|
|
my @chars = ( "A" .. "Z", "a" .. "z" );
|
|
|
|
my $string;
|
|
|
|
$string .= $chars[ rand @chars ] for 1 .. 10;
|
|
|
|
my $file = '/tmp/' . 'pm.' . $string;
|
|
|
|
|
|
|
|
# gpg --output /tmp/decryptfile --decrypt $db
|
2014-05-06 00:40:26 +04:00
|
|
|
@dec_cmd = ( "$gpg", "--output", "$file", "--decrypt", "$db" );
|
|
|
|
system(@dec_cmd) == 0 or die "Cannot decrypt $db: $!\n";
|
2014-04-28 14:07:01 +04:00
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|