Query proccessing mechanism writed

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-04-28 16:14:23 +04:00
parent e4de44a9b4
commit 89920df709
4 changed files with 71 additions and 6 deletions

View File

@ -7,7 +7,7 @@ sub new {
my $class = shift; my $class = shift;
# Get home dir # Get home dir
my $home = $ENV->{HOME}; my $home = $ENV{HOME};
my $gpg = GPG->new($home); my $gpg = GPG->new($home);
@ -27,9 +27,38 @@ sub connect {
return $dbh; return $dbh;
} }
# Query proccessing mechanism
sub mdo { sub mdo {
my ( $self, $query, $type, $file ) = @_; my ( $self, $query ) = @_;
my $dbh = Database->connect(); 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 # Create config dirrectory and DB if not exist

View File

@ -5,6 +5,7 @@ use warnings;
use utf8; use utf8;
use Database; use Database;
use GPG;
use PasswordSave; use PasswordSave;
use PasswordShow; use PasswordShow;
@ -13,12 +14,14 @@ sub new {
my $class = shift; my $class = shift;
my $db = Database->new(); my $db = Database->new();
my $gpg = GPG->new( $ENV{HOME} );
my $p_save = PasswordSave->new(); my $p_save = PasswordSave->new();
my $p_show = PasswordShow->new(); my $p_show = PasswordShow->new();
my $self = { my $self = {
_db => $db, _db => $db,
_gpg => $gpg,
_p_save => $p_save, _p_save => $p_save,
_p_show => $p_show, _p_show => $p_show,
}; };
@ -27,10 +30,37 @@ sub new {
return $self; 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. # Check configuration. If it doesn't exist create it.
sub check_config { sub check_config {
my ($self) = @_; my ($self) = @_;
if ( -e $ENV->{HOME} . "/.PM/db.sqlite" ) { if ( -e $ENV{HOME} . "/.PM/db.sqlite" ) {
return 0; return 0;
} }
else { else {

View File

@ -2,3 +2,9 @@ PM
== ==
Simple password manager writed in perl. Simple password manager writed in perl.
TODO
====
* Store decrypted DB into RAM not in /tmp/
* Different keys selection

2
pm.pl
View File

@ -3,7 +3,7 @@
use Password; use Password;
use Getopt::Std; use Getopt::Std;
our $VERSION = '0.0.0b'; our $VERSION = '0.0.0c';
# Debug # Debug
use Data::Dumper; use Data::Dumper;