163 lines
3.4 KiB
Perl
163 lines
3.4 KiB
Perl
package Password;
|
|
|
|
use strict;
|
|
use warnings;
|
|
use utf8;
|
|
|
|
use Database;
|
|
use GPG;
|
|
|
|
# Debug
|
|
use Data::Dumper;
|
|
|
|
sub new {
|
|
my $class = shift;
|
|
|
|
my $db = Database->new();
|
|
my $gpg = GPG->new( $ENV{HOME} );
|
|
|
|
my $self = {
|
|
_db => $db,
|
|
_gpg => $gpg,
|
|
};
|
|
|
|
bless $self, $class;
|
|
return $self;
|
|
}
|
|
|
|
sub show {
|
|
my ( $self, $name, $username ) = @_;
|
|
my $db_class = $self->{_db};
|
|
my $gpg = $self->{_gpg};
|
|
|
|
# Decrypt db
|
|
my $dec_db_file = $gpg->decrypt_db();
|
|
|
|
# Query
|
|
my $query_string;
|
|
if ( defined($username) ) {
|
|
$query_string = "select id, name, resource, password from passwords
|
|
where name='$name' and username='$username'";
|
|
}
|
|
else {
|
|
$query_string
|
|
= "select id, 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
|
|
my @rm_cmd = ( "rm", "-f", "$dec_db_file" );
|
|
system(@rm_cmd) == 0 or die "Cannot remove unencrypted database! $!\n";
|
|
|
|
return $q_hash;
|
|
}
|
|
|
|
# Remove password
|
|
sub remove {
|
|
my ( $self, $store ) = @_;
|
|
my $db_class = $self->{_db};
|
|
my $gpg = $self->{_gpg};
|
|
my $id = $store->{id};
|
|
|
|
# Decrypt database
|
|
my $dec_db_file = $gpg->decrypt_db();
|
|
my $q = "delete from passwords where id=$id";
|
|
my $mdo_q = {
|
|
file => $dec_db_file,
|
|
query => $q,
|
|
type => 'do',
|
|
};
|
|
|
|
$db_class->mdo($mdo_q);
|
|
$gpg->encrypt_db($dec_db_file);
|
|
|
|
return 0;
|
|
}
|
|
|
|
sub export {
|
|
my ( $self, $filename ) = @_;
|
|
my $gpg = $self->{_gpg};
|
|
|
|
my $dec_db_file = $gpg->decrypt_db();
|
|
my $export_enc = $gpg->export($dec_db_file);
|
|
|
|
my @mv_cmd = ( 'mv', "$export_enc", "$filename" );
|
|
system(@mv_cmd) == 0 or die "Cannot move $export_enc to $filename: $!\n";
|
|
|
|
return 0;
|
|
}
|
|
|
|
# Decrypt base and store new password
|
|
sub save {
|
|
my ( $self, $store ) = @_;
|
|
my $db_class = $self->{_db};
|
|
my $gpg = $self->{_gpg};
|
|
|
|
my $name = $store->{name};
|
|
my $resource = $store->{resource};
|
|
my $password = $store->{password};
|
|
|
|
# Comment check
|
|
my $comment = '';
|
|
if ( defined( $store->{comment} ) ) {
|
|
$comment = $store->{comment};
|
|
}
|
|
|
|
# Username check
|
|
my $username = '';
|
|
if ( defined( $store->{username} ) ) {
|
|
$username = $store->{username};
|
|
}
|
|
|
|
# Decrypt database
|
|
my $dec_db_file = $gpg->decrypt_db();
|
|
my $q
|
|
= "insert into passwords(name, resource, password, username, comment)
|
|
values('$name', '$resource', '$password', '$username', '$comment')";
|
|
my $mdo_q = {
|
|
file => $dec_db_file,
|
|
name => $name,
|
|
query => $q,
|
|
type => 'do',
|
|
};
|
|
|
|
$db_class->mdo($mdo_q);
|
|
$gpg->encrypt_db($dec_db_file);
|
|
|
|
return 0;
|
|
}
|
|
|
|
# Generate password
|
|
sub generate {
|
|
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, '!', '@', '$', '(', ')' );
|
|
my $string;
|
|
$string .= $chars[ rand @chars ] for 1 .. 16;
|
|
|
|
return $string;
|
|
}
|
|
|
|
# Check configuration. If it doesn't exist create it.
|
|
sub check_config {
|
|
my ($self) = @_;
|
|
if ( -e $ENV{HOME} . "/.PM/db.sqlite" ) {
|
|
return 1;
|
|
}
|
|
else {
|
|
my $db = $self->{_db};
|
|
$db->create_base();
|
|
|
|
print "Done!\n";
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
1;
|