Groups support. Dirty hack

This commit is contained in:
Denis Zheleztsov 2015-04-10 12:50:56 +03:00
parent de8a7421e4
commit 0e92985b5e
3 changed files with 74 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package Database;
use DBI;
use GPG;
use Term::ANSIColor;
use Password;
@ -36,6 +37,7 @@ sub mdo {
my $q = $query->{query};
my $name = $query->{name};
my $type = $query->{type};
my $g = $query->{group};
my $dbh = Database->connect($db_file);
@ -50,7 +52,6 @@ sub mdo {
my $sth = $dbh->prepare($q);
my $rv = $sth->execute();
use Term::ANSIColor;
printf "%-11s %-11s %-11s %-11s %-11s\n",
colored( "ID", 'white' ),
colored( "NAME", 'magenta' ),
@ -81,6 +82,39 @@ sub mdo {
exit 0;
}
# Show group
if ($g) {
my $sth = $dbh->prepare($q);
my $rv = $sth->execute();
printf "%-11s %-11s %-11s %-11s %-11s\n",
colored( "ID", 'white' ),
colored( "NAME", 'magenta' ),
colored( "GROUP", 'bold magenta' ),
colored( "RESOURCE", 'blue' ),
colored( "USERNAME", 'green' ),
colored( "COMMENT", 'yellow' );
print "=========================================\n";
while ( my ( $id, $name, $group, $resource, $username, $comment )
= $sth->fetchrow_array() )
{
printf "%-11s %-11s %-11s %-11s %-11s %-11s\n",
colored( $id, 'white' ),
colored( $name, 'magenta' ),
colored( $group, 'bold magenta' ),
colored( $resource, 'blue' ),
colored( $username, 'green' ),
colored( $comment, 'yellow' );
}
# Remove unencrypted file
my @rm_cmd = ( "rm", "-f", "$db_file" );
system(@rm_cmd) == 0
or die "Cannot remove unencrypted database! $!\n";
exit 0;
}
my $sth = $dbh->prepare($q);
$sth->execute();

View File

@ -26,7 +26,7 @@ sub new {
}
sub show {
my ( $self, $name, $username ) = @_;
my ( $self, $name, $username, $g ) = @_;
my $db_class = $self->{_db};
my $gpg = $self->{_gpg};
@ -35,21 +35,34 @@ sub show {
# Query
my $query_string;
if ( defined($username) ) {
if ( defined($username) and !($g)) {
$query_string = "select id, name, resource, password from passwords
where name='$name' and username='$username'";
}
# Fasthack
elsif ( defined($g)) {
$query_string = "select id, name, `group`, resource, username, comment from passwords where `group`='$g'";
}
else {
$query_string
= "select id, name, resource, password from passwords where name='$name'";
}
my $mdo_q = {
my $mdo_q;
$mdo_q = {
file => $dec_db_file,
query => $query_string,
name => $name,
type => 'select',
};
$mdo_q = {
file => $dec_db_file,
query => $query_string,
name => $name,
type => 'select',
group => $g,
} if $g;
my $q_hash = $db_class->mdo($mdo_q);
# Remove unencrypted file
@ -103,6 +116,7 @@ sub save {
my $name = $store->{name};
my $resource = $store->{resource};
my $password = $store->{password};
my $group = $store->{group};
# Comment check
my $comment = '';
@ -119,8 +133,8 @@ sub save {
# 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')";
= "insert into passwords(name, resource, password, username, comment, 'group')
values('$name', '$resource', '$password', '$username', '$comment', '$group')";
my $mdo_q = {
file => $dec_db_file,
name => $name,
@ -137,16 +151,17 @@ sub save {
# Generate password
sub generate {
my $value;
open my $rnd, "<", "/dev/random";
read $rnd, $value, 32;
my $c = unpack ("H*", $value);
my @chars = split(//,$c);
push @chars, $_ for ( '!', '@', '(', ')','A'..'Z' );
open my $rnd, "<", "/dev/random";
read $rnd, $value, 32;
my $c = unpack( "H*", $value );
close $rnd;
my @chars = split( //, $c );
push @chars, $_ for ( '!', '@', '(', ')', 'A' .. 'Z' );
my $string;
$string .= $chars[ rand @chars ] for 1 .. 16;
$string .= $chars[ rand @chars ] for 1 .. 16;
return $string;
}

15
pm.pl
View File

@ -15,11 +15,11 @@ our $VERSION = '0.0.1-beta1';
my $usage = Usage->new();
sub init() {
my $opt_string = 'swn:l:p:rhvou:i:c:x:';
my $opt_string = 'swn:l:p:rhvou:i:c:x:g:';
getopts("$opt_string") or $usage->show();
our (
$opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h,
$opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_x,
$opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_x, $opt_g,
);
print "Simple password manager writed in Perl.\nVersion: "
@ -62,6 +62,11 @@ if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) {
print "Cancel\n" if $ans ne "Yes";
}
}
if ( defined($opt_s) and defined($opt_g) ) {
$pass->show( $opt_n, '', $opt_g );
}
elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {
my $get_h = $pass->show( $opt_n, $opt_u );
@ -90,7 +95,7 @@ elsif ( defined($opt_w)
and !defined($opt_p) )
{
# Generate password and store it into DB
print "$opt_w, $opt_n, $opt_l, $opt_p\n";
$opt_g = '' if !($opt_g);
$opt_p = $pass->generate();
@ -100,6 +105,7 @@ elsif ( defined($opt_w)
password => $opt_p,
username => $opt_u,
comment => $opt_c,
group => $opt_g,
};
$pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n";
@ -112,7 +118,7 @@ elsif ( defined($opt_w)
and defined($opt_p) )
{
# Store new password into DB
print "$opt_w, $opt_n, $opt_l, $opt_p\n";
$opt_g = '' if !($opt_g);
my $store_h = {
name => $opt_n,
@ -121,6 +127,7 @@ elsif ( defined($opt_w)
gen => 0,
username => $opt_u,
comment => $opt_c,
group => $opt_g,
};
$pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n";