PM/GPG.pm

126 lines
3.1 KiB
Perl
Raw Normal View History

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-12 11:25:39 +04:00
my $self = {
_db => $db,
2014-05-10 20:52:30 +04:00
_home => $home,
2014-05-12 11:25:39 +04:00
};
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" ) {
2014-05-12 11:25:39 +04:00
open my $key_f, "<", $self->{_home} . "/.PM/.key"
2014-05-10 20:52:30 +04:00
or die "Cannot open file: $!\n";
2014-05-12 11:25:39 +04:00
while (<$key_f>) {
2014-05-10 20:52:30 +04:00
$recipient = $_;
}
@enc_cmd = (
2014-05-12 11:25:39 +04:00
"gpg", "--output", "$db", "-a",
"--recipient", "$recipient", "--encrypt", "$file",
2014-05-10 20:52:30 +04:00
);
}
else {
# gpg --output test.gpg --encrypt test -a --default-recipient-self
@enc_cmd = (
2014-05-12 11:25:39 +04:00
"gpg", "--output", "$db", "-a", "--default-recipient-self",
2014-05-10 20:52:30 +04:00
"--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
2014-05-12 11:25:39 +04:00
# Change file permissions
@chmod_cmd = ( "chmod", 600, $db );
system(@chmod_cmd) == 0 or die "Cannot chmod $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;
2016-05-01 16:27:28 +03:00
my $file = '/dev/shm/' . 'pm.' . $string;
2014-04-28 14:07:01 +04:00
# 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
2014-05-12 11:25:39 +04:00
# Change file permissions
@chmod_cmd = ( "chmod", 600, $file );
system(@chmod_cmd) == 0 or die "Cannot chmod $file: $!\n";
2014-04-28 14:07:01 +04:00
return $file;
}
2014-05-13 17:57:14 +04:00
sub export {
my ($self, $file) = @_;
2014-05-13 17:57:14 +04:00
use Term::ANSIColor;
print "Password for " . colored("export\n", 'yellow');
2014-05-13 17:57:14 +04:00
# gpg --symmetric filename
my @enc_cmd = ('gpg', '--symmetric', "$file");
2014-05-13 17:57:14 +04:00
system(@enc_cmd) == 0 or die "Cannot encrypt $file: $!\n";
# Remove unencrypted file
my @rm_cmd = ('rm', '-f', "$file");
2014-05-13 17:57:14 +04:00
system(@rm_cmd) == 0 or die "Cannot remove file $file: $!\n";
my $export_file = $file . '.gpg';
return $export_file;
}
2016-05-01 16:27:28 +03:00
sub import_db {
my ($self, $file) = @_;
my @chars = ( "A" .. "Z", "a" .. "z" );
my $string;
$string .= $chars[ rand @chars ] for 1 .. 10;
my $tmpfile = '/dev/shm/' . 'pm.' . $string;
system("gpg --output $tmpfile --decrypt $file") == 0 or die "Cannot decrypt $file: $!\n";
my $encrypted = $self->encrypt_db($tmpfile);
return $encrypted;
}
1;