From 992d7268ef889ad11f477a474da3590a234fdcad Mon Sep 17 00:00:00 2001 From: Difrex Date: Tue, 6 May 2014 00:40:26 +0400 Subject: [PATCH] First buggy but worked version :) --- ClipPass.pm | 5 ++--- Database.pm | 11 +++++++++-- GPG.pm | 21 ++++++++++++--------- Password.pm | 7 ++++++- pm.pl | 10 ++++++---- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/ClipPass.pm b/ClipPass.pm index cc14d01..83d1206 100644 --- a/ClipPass.pm +++ b/ClipPass.pm @@ -5,15 +5,14 @@ use Clipboard; sub new { my $class = shift; - my $self = { _password => shift, }; + my $self = {}; bless $self, $class; return $self; } sub copy { - my ($self) = @_; - my $password = $self->{_password}; + my ( $self, $password ) = @_; if ( 'Clipboard::Xclip' eq $Clipboard::driver ) { no warnings 'redefine'; diff --git a/Database.pm b/Database.pm index 72a2784..8744fe7 100644 --- a/Database.pm +++ b/Database.pm @@ -3,6 +3,8 @@ package Database; use DBI; use GPG; +use Password; + sub new { my $class = shift; @@ -51,7 +53,7 @@ sub mdo { return $q_hash; } elsif ( $type eq 'do' ) { - $dbh->do($q) or die "$!\n"; + $dbh->do("$q") or die "$!\n"; return 0; } else { @@ -72,15 +74,19 @@ sub create_base { if ( !( -d $pm_dir ) ) { # Create dirrectory + print "Creating configuration dirrectory...\n"; my @mkdir_cmd = ( "mkdir", "$pm_dir" ); system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n"; - my $first_sqlite = '/tmp/db.sqlite'; + my $pass = Password->new(); + my $string = $pass->generate(); + my $first_sqlite = "/tmp/$string"; # Create DB file my @createdb_cmd = ( "touch", "$first_sqlite" ); system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n"; + print "Creating database...\n"; # Create table. my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" ); print "Create database schema\n"; @@ -88,6 +94,7 @@ sub create_base { = "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)"; $dbh->do($q_table); + print "Encrypt database...\n"; # Encrypt db $gpg->encrypt_db($first_sqlite); diff --git a/GPG.pm b/GPG.pm index 9fd7b6e..890ce18 100644 --- a/GPG.pm +++ b/GPG.pm @@ -1,12 +1,13 @@ # GPG layer for encrypt/decrypt passwords database package GPG; -our $gpg = '/usr/bin/gpg'; +# Debug +use Data::Dumper; sub new { my $class = shift; - my $home = shift; + my $home = $ENV{HOME}; my $db = $home . "/.PM/db.sqlite"; my $self = { _db => $db, }; @@ -25,13 +26,13 @@ sub encrypt_db { @rm_db = ( "rm", "-f", "$db" ); system(@rm_db) == 0 or die "Cannot remove old database: $!\n"; - # gpg --output test.gpg --encrypt -a --default-recipient-self test + # gpg --output test.gpg --encrypt test -a --default-recipient-self @enc_cmd = ( - "$gpg", "--output", - "$db", "--encrypt", - "-a", "--default-recipient-self", - "$file" + "gpg", "--output", "$db", + "-a", "--default-recipient-self", + "--encrypt", "$file", ); + system(@enc_cmd) == 0 or die "Cannot encrypt!\nDecrypted file: $file\nTraceback: $!\n"; @@ -48,6 +49,8 @@ sub decrypt_db { my ($self) = @_; my $db = $self->{_db}; + my $gpg = '/usr/bin/gpg'; + # Generate random file name my @chars = ( "A" .. "Z", "a" .. "z" ); my $string; @@ -55,8 +58,8 @@ sub decrypt_db { my $file = '/tmp/' . 'pm.' . $string; # gpg --output /tmp/decryptfile --decrypt $db - @dec_cmd = ( "$gpg", "--decrypt", "$db", "--output", "$file" ); - system(@sys_dec_cmd) == 0 or die "Cannot decrypt $db: $!\n"; + @dec_cmd = ( "$gpg", "--output", "$file", "--decrypt", "$db" ); + system(@dec_cmd) == 0 or die "Cannot decrypt $db: $!\n"; return $file; } diff --git a/Password.pm b/Password.pm index b975f48..b4570f0 100644 --- a/Password.pm +++ b/Password.pm @@ -7,6 +7,9 @@ use utf8; use Database; use GPG; +# Debug +use Data::Dumper; + sub new { my $class = shift; @@ -67,7 +70,8 @@ sub save { # Decrypt database my $dec_db_file = $gpg->decrypt_db(); my $q - = "insert into passwords(name, resource, password) values($name, $resource, $password)"; + = "insert into passwords(name, resource, password) + values('$name', '$resource', '$password')"; my $mdo_q = { file => $dec_db_file, name => $name, @@ -100,6 +104,7 @@ sub check_config { my $db = $self->{_db}; $db->create_base(); + print "Done!\n"; return 0; } return 1; diff --git a/pm.pl b/pm.pl index 4c202d8..01574a3 100755 --- a/pm.pl +++ b/pm.pl @@ -7,7 +7,7 @@ use ClipPass; # Debug use Data::Dumper; -our $VERSION = '0.0.1c'; +our $VERSION = '0.0.1-alpha'; sub usage() { print STDERR << "EOF"; @@ -61,7 +61,7 @@ sub init() { my $pass = Password->new(); # Don't use it's before GPG and Database -# $pass->check_config() == 0 or die "$!\n"; +$pass->check_config() == 0 or die "$!\n"; # Parse cmd line init(); @@ -73,9 +73,11 @@ my $copy = ClipPass->new(); if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) { my $get_h = $pass->show($opt_n); - $copy->copy($get_h->{password}); + my $get_pass = $get_h->{password}; - print "Password copied to xclipboard.\nURI is " . $get_h->{resource}; + $copy->copy($get_pass); + + print "Password copied to xclipboard.\nURI is " . $get_h->{resource} . "\n"; } elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {