diff --git a/Database.pm b/Database.pm index 7a68d7f..ef1f99e 100644 --- a/Database.pm +++ b/Database.pm @@ -7,7 +7,7 @@ sub new { my $class = shift; # Get home dir - my $home = $ENV->{HOME}; + my $home = $ENV{HOME}; my $gpg = GPG->new($home); @@ -27,9 +27,38 @@ sub connect { return $dbh; } +# Query proccessing mechanism sub mdo { - my ( $self, $query, $type, $file ) = @_; - my $dbh = Database->connect(); + my ( $self, $query ) = @_; + my $db_file = $query->{file}; + my $q = $query->{query}; + my $name = $query->{name}; + my $type = $query->{type}; + + my $dbh = Database->connect($db_file); + + # Select + if ( $type eq 'select' ) { + my $sth = $dbh->prepare($q); + $sth->execute(); + my ( $name, $resource, $password ) = $sth->fetchrow_array(); + + my $q_hash = { + name => $name, + resource => $resource, + password => $password, + }; + return $q_hash; + } + elsif ( $type eq 'do' ) { + $dbh->do($q); + return 0; + } + else { + print STDERR "Something went wrong! $!\n"; + return 1; + } + return 1; } # Create config dirrectory and DB if not exist diff --git a/Password.pm b/Password.pm index 3a550d1..0c6bc98 100644 --- a/Password.pm +++ b/Password.pm @@ -5,6 +5,7 @@ use warnings; use utf8; use Database; +use GPG; use PasswordSave; use PasswordShow; @@ -12,13 +13,15 @@ use PasswordShow; sub new { my $class = shift; - my $db = Database->new(); + my $db = Database->new(); + my $gpg = GPG->new( $ENV{HOME} ); my $p_save = PasswordSave->new(); my $p_show = PasswordShow->new(); my $self = { _db => $db, + _gpg => $gpg, _p_save => $p_save, _p_show => $p_show, }; @@ -27,10 +30,37 @@ sub new { return $self; } +sub show { + my ( $self, $name ) = @_; + my $db_class = $self->{_db}; + my $gpg = $self->{_gpg}; + + # Decrypt db + my $dec_db_file = $gpg->decrypt_db(); + + # Query + my $query_string + = "select name, resource, password from passwords where name='$name'"; + + my $mdo_q = { + file => $dec_db_file, + query => $query_string, + name => $name, + type => 'select', + }; + my $q_hash = $db_class->mdo($mdo_q); + + # Remove unencrypted file + @rm_cmd = ( "rm", "$dec_db_file" ); + system(@rm_cmd) == 0 or die "Cannot remove unencrypted database! $!\n"; + + return $q_hash; +} + # Check configuration. If it doesn't exist create it. sub check_config { my ($self) = @_; - if ( -e $ENV->{HOME} . "/.PM/db.sqlite" ) { + if ( -e $ENV{HOME} . "/.PM/db.sqlite" ) { return 0; } else { diff --git a/README.md b/README.md index bb8166c..869d98f 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,9 @@ PM == Simple password manager writed in perl. + +TODO +==== + +* Store decrypted DB into RAM not in /tmp/ +* Different keys selection \ No newline at end of file diff --git a/pm.pl b/pm.pl index b2f564c..671168d 100755 --- a/pm.pl +++ b/pm.pl @@ -3,7 +3,7 @@ use Password; use Getopt::Std; -our $VERSION = '0.0.0b'; +our $VERSION = '0.0.0c'; # Debug use Data::Dumper;