From 6aff364cbb7cb1d1e3b91df9f760cd867cc62a5b Mon Sep 17 00:00:00 2001 From: "Difrex(Denis Zheleztsov)" Date: Mon, 12 May 2014 11:07:30 +0400 Subject: [PATCH] Remove password is working --- Database.pm | 15 ++++++++------- Password.pm | 26 ++++++++++++++++++++++++-- pm.pl | 19 ++++++++++++++----- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Database.pm b/Database.pm index 3e64cf2..8cea90e 100644 --- a/Database.pm +++ b/Database.pm @@ -44,19 +44,19 @@ sub mdo { # Bad hack if ( $name eq 'all' ) { - my $q = 'select name, resource, username from passwords'; + my $q = 'select id, name, resource, username from passwords'; my $sth = $dbh->prepare($q); my $rv = $sth->execute(); - printf "%-11s %-11s %-11s\n", "NAME", "RESOURCE", "USERNAME"; - while ( my ( $name, $resource, $username ) + printf "%-11s %-11s %-11s %-11s\n", "ID", "NAME", "RESOURCE", "USERNAME"; + while ( my ( $id, $name, $resource, $username ) = $sth->fetchrow_array() ) { - printf "%-11s %-11s %-11s\n", - $name, $resource, "\t$username"; + printf "%-11s %-11s %-11s %-11s\n", + $id, $name, $resource, $username; } # Remove unencrypted file my @rm_cmd = ( "rm", "-f", "$db_file" ); @@ -67,9 +67,10 @@ sub mdo { my $sth = $dbh->prepare($q); $sth->execute(); - my ( $name, $resource, $password ) = $sth->fetchrow_array(); + my ( $id, $name, $resource, $password ) = $sth->fetchrow_array(); my $q_hash = { + id => $id, name => $name, resource => $resource, password => $password, @@ -122,7 +123,7 @@ sub create_base { print "Create database schema\n"; my $q_table = "create table passwords(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(32), username VARCHAR(32), - resource TEXT, password TEXT)"; + resource TEXT, password TEXT, comment TEXT)"; $dbh->do($q_table); print "Encrypt database...\n"; diff --git a/Password.pm b/Password.pm index cf12f7b..9fef37b 100644 --- a/Password.pm +++ b/Password.pm @@ -59,6 +59,28 @@ sub show { 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; +} + # Decrypt base and store new password sub save { my ( $self, $store ) = @_; @@ -72,8 +94,8 @@ sub save { # Username check my $username = ''; - if (defined($store->{username})) { - $username = $store->{username} + if ( defined( $store->{username} ) ) { + $username = $store->{username}; } if ( $generate == 1 ) { diff --git a/pm.pl b/pm.pl index 85f0d3b..9624b8a 100755 --- a/pm.pl +++ b/pm.pl @@ -22,6 +22,7 @@ Simple password manager writed in Perl. if key not selected PM generate secure password and copy it to xclipboard -r remove password + -i password ID -o open link -h show this help screen and exit -v show version info and exit @@ -48,11 +49,11 @@ EOF } sub init() { - my $opt_string = 'swn:l:p:rhvou:'; + my $opt_string = 'swn:l:p:rhvou:i:'; getopts("$opt_string") or usage(); our ( - $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, - $opt_p, $opt_h, $opt_v, $opt_o, $opt_u + $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, + $opt_h, $opt_v, $opt_o, $opt_u, $opt_i, ); print "Simple password manager writed in Perl.\nVersion: " @@ -76,10 +77,11 @@ my $copy = ClipPass->new(); # It's really ugly code. Sorry :( if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) { - my $get_h = $pass->show($opt_n, $opt_u); + my $get_h = $pass->show( $opt_n, $opt_u ); my $get_pass = $get_h->{password}; $copy->copy($get_pass); + # TEST use Term::ANSIColor; print color 'green'; @@ -92,7 +94,7 @@ if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) { } elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) { - my $get_h = $pass->show($opt_n, $opt_u); + my $get_h = $pass->show( $opt_n, $opt_u ); $copy->copy( $get_h->{password} ); # Open resource. @@ -101,6 +103,13 @@ elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) { print "Password copied to clipboard. Trying to open uri.\n"; } +# Remove string from db +elsif ( defined($opt_r) and defined($opt_i) ) { + + my $store_h = { id => $opt_i, }; + + $pass->remove($store_h) == 0 or die "Oops! 105: pm.pl. $!\n"; +} elsif ( defined($opt_w) and defined($opt_n) and defined($opt_l)