PM/Password.pm

74 lines
1.3 KiB
Perl
Raw Normal View History

2014-04-27 03:46:32 +04:00
package Password;
use strict;
use warnings;
use utf8;
2014-04-27 23:11:52 +04:00
use Database;
2014-04-28 16:14:23 +04:00
use GPG;
2014-04-27 03:46:32 +04:00
2014-04-28 12:04:17 +04:00
use PasswordSave;
use PasswordShow;
2014-04-27 03:46:32 +04:00
sub new {
2014-04-28 12:17:17 +04:00
my $class = shift;
2014-04-27 03:46:32 +04:00
2014-04-28 16:14:23 +04:00
my $db = Database->new();
my $gpg = GPG->new( $ENV{HOME} );
2014-04-28 12:04:17 +04:00
2014-04-28 12:17:17 +04:00
my $p_save = PasswordSave->new();
my $p_show = PasswordShow->new();
2014-04-28 12:04:17 +04:00
2014-04-28 12:17:17 +04:00
my $self = {
_db => $db,
2014-04-28 16:14:23 +04:00
_gpg => $gpg,
2014-04-28 12:17:17 +04:00
_p_save => $p_save,
_p_show => $p_show,
2014-04-27 03:46:32 +04:00
};
2014-04-28 12:17:17 +04:00
2014-04-27 03:46:32 +04:00
bless $self, $class;
return $self;
}
2014-04-28 16:14:23 +04:00
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
2014-04-28 16:43:00 +04:00
my @rm_cmd = ( "rm", "-f", "$dec_db_file" );
2014-04-28 16:14:23 +04:00
system(@rm_cmd) == 0 or die "Cannot remove unencrypted database! $!\n";
return $q_hash;
}
2014-04-28 12:04:17 +04:00
# Check configuration. If it doesn't exist create it.
sub check_config {
2014-04-28 12:17:17 +04:00
my ($self) = @_;
2014-04-28 16:14:23 +04:00
if ( -e $ENV{HOME} . "/.PM/db.sqlite" ) {
2014-04-28 12:17:17 +04:00
return 0;
}
else {
my $db = $self->{_db};
$db->create_base();
}
return 0;
2014-04-28 12:04:17 +04:00
}
2014-04-28 12:17:17 +04:00
1;