diff --git a/GPG.pm b/GPG.pm index 82cf004..88ecc5f 100644 --- a/GPG.pm +++ b/GPG.pm @@ -90,4 +90,23 @@ sub decrypt_db { return $file; } +sub export { + my ($self, $file) = @_; + + use Term::ANSIColor; + print "Password for " . colored("export\n", 'yellow'); + + # gpg --symmetric filename + my @enc_cmd = ('gpg', '--symmetric', "$file"); + system(@enc_cmd) == 0 or die "Cannot encrypt $file: $!\n"; + + # Remove unencrypted file + my @rm_cmd = ('rm', '-f', "$file"); + system(@rm_cmd) == 0 or die "Cannot remove file $file: $!\n"; + + my $export_file = $file . '.gpg'; + + return $export_file; +} + 1; diff --git a/Password.pm b/Password.pm index 985796c..3d80b42 100644 --- a/Password.pm +++ b/Password.pm @@ -81,6 +81,19 @@ sub remove { return 0; } +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; +} + # Decrypt base and store new password sub save { my ( $self, $store ) = @_; @@ -90,9 +103,9 @@ sub save { my $name = $store->{name}; my $resource = $store->{resource}; my $password = $store->{password}; - + # Comment check - my $comment = ''; + my $comment = ''; if ( defined( $store->{comment} ) ) { $comment = $store->{comment}; } @@ -105,7 +118,8 @@ sub save { # Decrypt database my $dec_db_file = $gpg->decrypt_db(); - my $q = "insert into passwords(name, resource, password, username, comment) + my $q + = "insert into passwords(name, resource, password, username, comment) values('$name', '$resource', '$password', '$username', '$comment')"; my $mdo_q = { file => $dec_db_file, @@ -122,9 +136,17 @@ sub save { # Generate password sub generate { - my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, '!', '@', '$', '(', ')' ); + 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' ); + my $string; - $string .= $chars[ rand @chars ] for 1 .. 16; + $string .= $chars[ rand @chars ] for 1 .. 16; return $string; } diff --git a/Usage.pm b/Usage.pm index f5efd61..130b968 100644 --- a/Usage.pm +++ b/Usage.pm @@ -24,6 +24,7 @@ Simple password manager writed in Perl. -r remove password -i password ID -o open link + -x [filename] export -h show this help screen and exit -v show version info and exit diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..2280c74 --- /dev/null +++ b/install.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +PWD=`pwd` +BIN_PATH=${HOME}/.local/bin + +mkdir -p $BIN_PATH + +cat > $BIN_PATH/pm << EOF +#!/bin/bash + +cd $PWD +./pm.pl "\$@" +cd - >/dev/null 2>/dev/null +EOF + +chmod +x $BIN_PATH/pm + +echo "Please add $BIN_PATH to your \$PATH variable" diff --git a/pm.pl b/pm.pl index 68cfa67..de62e33 100755 --- a/pm.pl +++ b/pm.pl @@ -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:'; + my $opt_string = 'swn:l:p:rhvou:i:c:x:'; 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_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, ); print "Simple password manager writed in Perl.\nVersion: " @@ -46,15 +46,21 @@ if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) { my $get_h = $pass->show( $opt_n, $opt_u ); my $get_pass = $get_h->{password}; - $copy->copy($get_pass); + if ( defined( $ENV{'DISPLAY'} ) ) { + $copy->copy($get_pass); - print color 'green'; - print "Password copied to xclipboard."; - print color 'reset'; - print "\nURI is "; - print color 'bold blue'; - print $get_h->{resource} . "\n"; - print color 'reset'; + print colored("Password copied to xclipboard.", 'green'); + print "\nURI is "; + print colored($get_h->{resource} . "\n", 'bold blue'); + } + else { + print colored("Warning! Password will show to terminal!", 'red'); + print " Yes/No: "; + my $ans = ; + chomp($ans); + print "$get_pass\n" if $ans eq "Yes"; + print "Cancel\n" if $ans ne "Yes"; + } } elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) { @@ -65,13 +71,9 @@ elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) { my @open_cmd = ( 'xdg-open', $get_h->{resource} ); system(@open_cmd) == 0 or die "Cannot open URI: $!\n"; - print color 'bold green'; - print "Password copied to clipboard.\n"; - print color 'reset'; + print colored("Password copied to clipboard.\n", 'bold green'); print "Trying to open "; - print color 'bold blue'; - print $get_h->{resource} . "\n"; - print color 'reset'; + print colored($get_h->{resource} . "\n", 'bold blue'); } # Remove string from db @@ -80,9 +82,7 @@ elsif ( defined($opt_r) and defined($opt_i) ) { my $store_h = { id => $opt_i, }; $pass->remove($store_h) == 0 or die "Oops! 111: pm.pl. $!\n"; - print color 'bold red'; - print "Password was removed!\n"; - print color 'reset'; + print colored("Password was removed!\n", 'bold red'); } elsif ( defined($opt_w) and defined($opt_n) @@ -104,9 +104,7 @@ elsif ( defined($opt_w) $pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n"; $copy->copy($opt_p); - print color 'green'; - print "Password was stored into DB!\n"; - print color 'reset'; + print colored("Password was stored into DB!\n", 'green'); } elsif ( defined($opt_w) and defined($opt_n) @@ -126,9 +124,12 @@ elsif ( defined($opt_w) }; $pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n"; - print color 'green'; - print "Password was stored into DB!\n"; - print color 'reset'; + print colored("Password was stored into DB!\n", 'green'); +} +# Export +elsif ( defined($opt_x) ) { + $pass->export($opt_x); + print colored("Dabase stored in $opt_x\n", 'green'); } else { $usage->show();