GPG module writed

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-04-28 14:07:01 +04:00
parent dcb70474e3
commit 58eea8d76c
2 changed files with 62 additions and 18 deletions

View File

@ -9,7 +9,7 @@ sub new {
# Get home dir # Get home dir
my $home = $ENV->{HOME}; my $home = $ENV->{HOME};
my $gpg = GPG->new(); my $gpg = GPG->new($home);
my $self = { my $self = {
_home => $home, _home => $home,
@ -21,24 +21,23 @@ sub new {
} }
sub connect { sub connect {
my ($self) = @_; my ( $self, $db_file ) = @_;
my $home = $self->{_home};
my $db_file = $home . "/.PM/db.sqlite";
my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "" ); my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "" );
return $dbh; return $dbh;
} }
sub mdo { sub mdo {
my ( $self, $query, $type ) = @_; my ( $self, $query, $type, $file ) = @_;
my $dbh = $self->{_dbh}; my $dbh = Database->connect();
} }
# Create config dirrectory and DB if not exist
sub create_base { sub create_base {
my ($self) = @_; my ($self) = @_;
my $home = $self->{_home}; my $home = $self->{_home};
my $pm_dir = $home . "/.PM/"; my $pm_dir = $home . "/.PM/";
my $gpg = $self->{_gpg};
# Check dir # Check dir
if ( !( -d $pm_dir ) ) { if ( !( -d $pm_dir ) ) {
@ -47,20 +46,21 @@ sub create_base {
my @mkdir_cmd = ( "mkdir", "$pm_dir" ); my @mkdir_cmd = ( "mkdir", "$pm_dir" );
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n"; system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
my $first_sqlite = '/tmp/db.sqlite';
# Create DB file # Create DB file
my @createdb_cmd = ( "touch", "$pm_dir/db.sqlite" ); my @createdb_cmd = ( "touch", "$first_sqlite" );
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n"; system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
# Create table. TODO: write this # Create table.
my $dbh my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" );
= DBI->connect( "dbi:SQLite:dbname=$pm_dir/db.sqlite", "", "" );
print "Create database schema\n"; print "Create database schema\n";
my $q_table my $q_table
= "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)"; = "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
$dbh->do($q_table); $dbh->do($q_table);
# Encrypt db # Encrypt db
# TODO: write this $gpg->encrypt_db($first_sqlite);
return 0; return 0;
} }

50
GPG.pm
View File

@ -1,15 +1,59 @@
# GPG layer for encrypt/decrypt passwords database
package GPG; package GPG;
# GPG abstraction layer for encrypt/decrypt password database our $gpg = '/usr/bin/gpg';
sub new { sub new {
my $class = shift; my $class = shift;
my $self = { my $home = shift;
}; my $db = $home . "/.PM/db.sqlite";
# Get default private key
my $self = { _db => $db, };
bless $self, $class; bless $self, $class;
return $self; return $self;
} }
# Encrypt sqlite database with default key
# and save it in config dir
sub encrypt_db {
my ( $self, $file ) = @_;
my $db = $self->{_db};
# gpg --output test.gpg --encrypt -a --default-recipient-self test
@enc_cmd = (
"$gpg", "--output",
"$db", "--encrypt",
"-a", "--default-recipient-self",
"$file"
);
system(@enc_cmd) == 0 or die "Cannot encrypt! $!\n";
# Remove unencrypted file
@rm_cmd = ( "rm", "$file" );
system(@rm_cmd) == 0 or die "Cannot remove file $file: $!\n";
}
# Decrypt database, save it in new place
# and return path to file
sub decrypt_db {
my ($self) = @_;
my $db = $self->{_db};
# 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
@dec_cmd = ( "$gpg", "--decrypt", "$db", "--output", "$file" );
system(@sys_dec_cmd) == 0 or die "Cannot decrypt $db: $!\n";
return $file;
}
1; 1;