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-05-06 00:40:26 +04:00
|
|
|
# Debug
|
|
|
|
use Data::Dumper;
|
|
|
|
|
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 $self = {
|
2014-04-29 15:45:31 +04:00
|
|
|
_db => $db,
|
|
|
|
_gpg => $gpg,
|
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 {
|
2014-05-06 15:18:34 +04:00
|
|
|
my ( $self, $name, $username ) = @_;
|
2014-04-28 16:14:23 +04:00
|
|
|
my $db_class = $self->{_db};
|
|
|
|
my $gpg = $self->{_gpg};
|
|
|
|
|
|
|
|
# Decrypt db
|
|
|
|
my $dec_db_file = $gpg->decrypt_db();
|
|
|
|
|
|
|
|
# Query
|
2014-05-06 15:18:34 +04:00
|
|
|
my $query_string;
|
|
|
|
if ( defined($username) ) {
|
2014-05-12 13:46:23 +04:00
|
|
|
$query_string = "select id, name, resource, password from passwords
|
2014-05-06 15:18:34 +04:00
|
|
|
where name='$name' and username='$username'";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$query_string
|
2014-05-12 13:46:23 +04:00
|
|
|
= "select id, name, resource, password from passwords where name='$name'";
|
2014-05-06 15:18:34 +04:00
|
|
|
}
|
2014-04-28 16:14:23 +04:00
|
|
|
|
|
|
|
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-05-12 11:07:30 +04:00
|
|
|
# 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();
|
2014-05-12 17:16:10 +04:00
|
|
|
my $q = "delete from passwords where id=$id";
|
|
|
|
my $mdo_q = {
|
2014-05-12 11:07:30 +04:00
|
|
|
file => $dec_db_file,
|
|
|
|
query => $q,
|
|
|
|
type => 'do',
|
|
|
|
};
|
|
|
|
|
|
|
|
$db_class->mdo($mdo_q);
|
|
|
|
$gpg->encrypt_db($dec_db_file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-03 18:38:40 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-29 11:37:41 +04:00
|
|
|
# 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};
|
2014-05-13 17:57:14 +04:00
|
|
|
|
2014-05-12 17:16:10 +04:00
|
|
|
# Comment check
|
2014-05-13 17:57:14 +04:00
|
|
|
my $comment = '';
|
2014-05-12 17:16:10 +04:00
|
|
|
if ( defined( $store->{comment} ) ) {
|
|
|
|
$comment = $store->{comment};
|
|
|
|
}
|
2014-05-06 15:18:34 +04:00
|
|
|
|
|
|
|
# Username check
|
|
|
|
my $username = '';
|
2014-05-12 11:07:30 +04:00
|
|
|
if ( defined( $store->{username} ) ) {
|
|
|
|
$username = $store->{username};
|
2014-05-06 15:18:34 +04:00
|
|
|
}
|
2014-04-29 11:37:41 +04:00
|
|
|
|
|
|
|
# Decrypt database
|
|
|
|
my $dec_db_file = $gpg->decrypt_db();
|
2014-05-13 17:57:14 +04:00
|
|
|
my $q
|
|
|
|
= "insert into passwords(name, resource, password, username, comment)
|
2014-05-12 17:16:10 +04:00
|
|
|
values('$name', '$resource', '$password', '$username', '$comment')";
|
2014-04-29 11:37:41 +04:00
|
|
|
my $mdo_q = {
|
|
|
|
file => $dec_db_file,
|
|
|
|
name => $name,
|
2014-04-29 15:45:31 +04:00
|
|
|
query => $q,
|
2014-04-29 11:37:41 +04:00
|
|
|
type => 'do',
|
|
|
|
};
|
|
|
|
|
|
|
|
$db_class->mdo($mdo_q);
|
|
|
|
$gpg->encrypt_db($dec_db_file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate password
|
|
|
|
sub generate {
|
2014-09-03 18:51:28 +04:00
|
|
|
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, '!', '@', '$', '(', ')' );
|
2014-04-29 11:37:41 +04:00
|
|
|
my $string;
|
|
|
|
$string .= $chars[ rand @chars ] for 1 .. 16;
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
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-05-06 12:18:44 +04:00
|
|
|
return 1;
|
2014-04-28 12:17:17 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
my $db = $self->{_db};
|
|
|
|
$db->create_base();
|
2014-04-29 15:45:31 +04:00
|
|
|
|
2014-05-06 00:40:26 +04:00
|
|
|
print "Done!\n";
|
2014-04-29 15:45:31 +04:00
|
|
|
return 0;
|
2014-04-28 12:17:17 +04:00
|
|
|
}
|
2014-04-29 15:45:31 +04:00
|
|
|
return 1;
|
2014-04-28 12:04:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 12:17:17 +04:00
|
|
|
1;
|