Merge branch 'testing'

This commit is contained in:
Denis Zheleztsov 2015-07-10 12:56:13 +03:00
commit 4098a71b94
3 changed files with 126 additions and 32 deletions

View File

@ -2,6 +2,7 @@ package Database;
use DBI; use DBI;
use GPG; use GPG;
use Term::ANSIColor;
use Password; use Password;
@ -36,6 +37,7 @@ sub mdo {
my $q = $query->{query}; my $q = $query->{query};
my $name = $query->{name}; my $name = $query->{name};
my $type = $query->{type}; my $type = $query->{type};
my $g = $query->{group};
my $dbh = Database->connect($db_file); my $dbh = Database->connect($db_file);
@ -45,28 +47,62 @@ sub mdo {
# Bad hack # Bad hack
if ( $name eq 'all' ) { if ( $name eq 'all' ) {
my $q my $q
= 'select id, name, resource, username, comment from passwords'; = 'select id, name, `group`, resource, username, comment from passwords';
my $sth = $dbh->prepare($q); my $sth = $dbh->prepare($q);
my $rv = $sth->execute(); my $rv = $sth->execute();
use Term::ANSIColor;
printf "%-11s %-11s %-11s %-11s %-11s\n", printf "%-11s %-11s %-11s %-11s %-11s\n",
colored( "ID", 'white' ), colored( "ID", 'white' ),
colored( "NAME", 'magenta' ), colored( "NAME", 'magenta' ),
colored( "GROUP", 'bold magenta' ),
colored( "RESOURCE", 'blue' ), colored( "RESOURCE", 'blue' ),
colored( "USERNAME", 'green' ), colored( "USERNAME", 'green' ),
colored( "COMMENT", 'yellow' ); colored( "COMMENT", 'yellow' );
print "=================================\n"; print "=========================================\n";
while ( my ( $id, $name, $resource, $username, $comment ) while ( my ( $id, $name, $group, $resource, $username, $comment )
= $sth->fetchrow_array() ) = $sth->fetchrow_array() )
{ {
if ( !defined($comment) ) { if ( !defined($comment) ) {
$comment = ''; $comment = '';
} }
printf "%-11s %-11s %-11s %-11s %-11s\n", printf "%-11s %-11s %-11s %-11s %-11s %-11s\n",
colored( $id, 'white' ), colored( $id, 'white' ),
colored( $name, 'magenta' ), 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;
}
# 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( $resource, 'blue' ),
colored( $username, 'green' ), colored( $username, 'green' ),
colored( $comment, 'yellow' ); colored( $comment, 'yellow' );
@ -137,8 +173,15 @@ sub create_base {
my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" ); my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" );
print "Create database schema\n"; print "Create database schema\n";
my $q_table my $q_table
= "create table passwords(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(32), username VARCHAR(32), = "CREATE TABLE passwords(
resource TEXT, password TEXT, comment TEXT)"; `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL,
resource TEXT NOT NULL,
password VARCHAR(32) NOT NULL,
comment TEXT NOT NULL,
'group' VARCHAR(32) NOT NULL
)";
$dbh->do($q_table); $dbh->do($q_table);
print "Encrypt database...\n"; print "Encrypt database...\n";

View File

@ -7,6 +7,9 @@ use utf8;
use Database; use Database;
use GPG; use GPG;
use Digest::MD5;
use MIME::Base64;
# Debug # Debug
use Data::Dumper; use Data::Dumper;
@ -26,7 +29,7 @@ sub new {
} }
sub show { sub show {
my ( $self, $name, $username ) = @_; my ( $self, $name, $username, $g ) = @_;
my $db_class = $self->{_db}; my $db_class = $self->{_db};
my $gpg = $self->{_gpg}; my $gpg = $self->{_gpg};
@ -35,21 +38,34 @@ sub show {
# Query # Query
my $query_string; my $query_string;
if ( defined($username) ) { if ( defined($username) and !($g)) {
$query_string = "select id, name, resource, password from passwords $query_string = "select id, name, resource, password from passwords
where name='$name' and username='$username'"; 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 { else {
$query_string $query_string
= "select id, name, resource, password from passwords where name='$name'"; = "select id, name, resource, password from passwords where name='$name'";
} }
my $mdo_q = { my $mdo_q;
$mdo_q = {
file => $dec_db_file, file => $dec_db_file,
query => $query_string, query => $query_string,
name => $name, name => $name,
type => 'select', 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); my $q_hash = $db_class->mdo($mdo_q);
# Remove unencrypted file # Remove unencrypted file
@ -103,6 +119,7 @@ sub save {
my $name = $store->{name}; my $name = $store->{name};
my $resource = $store->{resource}; my $resource = $store->{resource};
my $password = $store->{password}; my $password = $store->{password};
my $group = $store->{group};
# Comment check # Comment check
my $comment = ''; my $comment = '';
@ -119,8 +136,8 @@ sub save {
# Decrypt database # Decrypt database
my $dec_db_file = $gpg->decrypt_db(); my $dec_db_file = $gpg->decrypt_db();
my $q my $q
= "insert into passwords(name, resource, password, username, comment) = "insert into passwords(name, resource, password, username, comment, 'group')
values('$name', '$resource', '$password', '$username', '$comment')"; values('$name', '$resource', '$password', '$username', '$comment', '$group')";
my $mdo_q = { my $mdo_q = {
file => $dec_db_file, file => $dec_db_file,
name => $name, name => $name,
@ -138,15 +155,39 @@ sub save {
sub generate { sub generate {
my $value; my $value;
open my $rnd, "<", "/dev/random"; # Defaults
read $rnd, $value, 32; my $length = 16;
my $c = unpack ("H*", $value);
my @chars = split(//,$c); my $digest;
push @chars, $_ for ( '!', '@', '(', ')','A'..'Z' ); for (1..32) {
open my $rnd, "<", "/dev/urandom";
read $rnd, $value, 1000;
my $c = unpack( "H*", $value );
close $rnd;
# MORE ENTROPY
my $ctx = Digest::MD5->new();
$ctx->add($c);
# MORE
my $encoded = encode_base64( $ctx->hexdigest() );
$encoded =~ s/=//g;
$encoded =~ s/\n//g;
$digest .= $encoded;
}
my @chars = split( //, $digest );
my @r_special = ( '!', '@', '(', ')', '#', '$', '%', '^', '&' );
for (1..10) {
foreach my $special (@r_special) {
$chars[ rand(@chars) ] = $special ];
}
}
my $string; my $string;
$string .= $chars[ rand @chars ] for 1 .. 16; $string .= $chars[ rand @chars ] for 1 .. $length;
return $string; return $string;
} }

20
pm.pl
View File

@ -10,16 +10,16 @@ use Usage;
# Debug # Debug
use Data::Dumper; use Data::Dumper;
our $VERSION = '0.0.1-beta1'; our $VERSION = '0.0.1';
my $usage = Usage->new(); my $usage = Usage->new();
sub init() { 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(); getopts("$opt_string") or $usage->show();
our ( our (
$opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h, $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: " print "Simple password manager writed in Perl.\nVersion: "
@ -61,6 +61,13 @@ if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) {
print "$get_pass\n" if $ans eq "Yes"; print "$get_pass\n" if $ans eq "Yes";
print "Cancel\n" if $ans ne "Yes"; print "Cancel\n" if $ans ne "Yes";
} }
exit 0;
}
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) ) { elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {
@ -90,7 +97,7 @@ elsif ( defined($opt_w)
and !defined($opt_p) ) and !defined($opt_p) )
{ {
# Generate password and store it into DB # 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(); $opt_p = $pass->generate();
@ -100,6 +107,7 @@ elsif ( defined($opt_w)
password => $opt_p, password => $opt_p,
username => $opt_u, username => $opt_u,
comment => $opt_c, comment => $opt_c,
group => $opt_g,
}; };
$pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n"; $pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n";
@ -112,7 +120,7 @@ elsif ( defined($opt_w)
and defined($opt_p) ) and defined($opt_p) )
{ {
# Store new password into DB # Store new password into DB
print "$opt_w, $opt_n, $opt_l, $opt_p\n"; $opt_g = '' if !($opt_g);
my $store_h = { my $store_h = {
name => $opt_n, name => $opt_n,
@ -121,11 +129,13 @@ elsif ( defined($opt_w)
gen => 0, gen => 0,
username => $opt_u, username => $opt_u,
comment => $opt_c, comment => $opt_c,
group => $opt_g,
}; };
$pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n"; $pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n";
print colored( "Password was stored into DB!\n", 'green' ); print colored( "Password was stored into DB!\n", 'green' );
} }
# Export # Export
elsif ( defined($opt_x) ) { elsif ( defined($opt_x) ) {
$pass->export($opt_x); $pass->export($opt_x);