PM/pm.pl

163 lines
3.8 KiB
Perl
Raw Normal View History

2014-04-27 03:46:32 +04:00
#!/usr/bin/perl
use Getopt::Std;
2014-05-12 15:41:05 +04:00
use Term::ANSIColor;
use Password;
2014-04-29 15:45:31 +04:00
use ClipPass;
2014-05-12 15:41:05 +04:00
use Usage;
2014-04-27 03:46:32 +04:00
2014-04-28 11:29:38 +04:00
# Debug
use Data::Dumper;
2015-04-10 13:43:40 +03:00
our $VERSION = '0.0.1';
2014-04-29 15:45:31 +04:00
2014-05-12 15:41:05 +04:00
my $usage = Usage->new();
2014-04-27 03:46:32 +04:00
sub init() {
2016-05-01 16:27:28 +03:00
my $opt_string = 'swn:l:p:rhvou:i:c:x:g:b:e:';
2014-05-12 15:41:05 +04:00
getopts("$opt_string") or $usage->show();
2014-04-29 15:45:31 +04:00
our (
2014-05-13 17:57:14 +04:00
$opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h,
2015-04-10 12:50:56 +03:00
$opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_x, $opt_g,
2016-05-01 16:27:28 +03:00
$opt_b, $opt_e
2014-04-29 15:45:31 +04:00
);
2014-04-28 12:17:17 +04:00
print "Simple password manager writed in Perl.\nVersion: "
. $VERSION
. "\n" and exit 0
if $opt_v;
2014-05-12 15:41:05 +04:00
$usage->show() if $opt_h;
2014-04-27 03:46:32 +04:00
}
2014-04-28 12:04:17 +04:00
my $pass = Password->new();
2014-04-28 12:17:17 +04:00
2014-05-06 12:18:44 +04:00
if ( $pass->check_config() == 0 ) {
2014-05-06 15:18:34 +04:00
exit 0;
2014-05-06 12:18:44 +04:00
}
2014-04-29 15:45:31 +04:00
2014-05-06 15:01:07 +04:00
init();
2014-05-05 22:37:59 +04:00
2014-04-29 15:45:31 +04:00
my $copy = ClipPass->new();
# Command line arguments switch
# It's really ugly code. Sorry :(
2014-04-29 15:45:31 +04:00
if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) {
2014-05-12 11:07:30 +04:00
my $get_h = $pass->show( $opt_n, $opt_u );
2014-05-06 00:40:26 +04:00
my $get_pass = $get_h->{password};
2016-02-08 19:11:37 +03:00
print colored( "Password for $opt_n not found\n", 'red' ) and exit 1
if !($get_pass);
if ( defined( $ENV{'DISPLAY'} ) ) {
$copy->copy($get_pass);
2015-04-10 13:43:40 +03:00
print colored( "Password copied to xclipboard.", 'green' );
print "\nURI is ";
2015-04-10 13:43:40 +03:00
print colored( $get_h->{resource} . "\n", 'bold blue' );
}
else {
2015-04-10 13:43:40 +03:00
print colored( "Warning! Password will show to terminal!", 'red' );
print " Yes/No: ";
my $ans = <STDIN>;
chomp($ans);
print "$get_pass\n" if $ans eq "Yes";
print "Cancel\n" if $ans ne "Yes";
}
2015-04-10 13:43:40 +03:00
exit 0;
2014-04-29 15:45:31 +04:00
}
2015-04-10 12:50:56 +03:00
if ( defined($opt_s) and defined($opt_g) ) {
$pass->show( $opt_n, '', $opt_g );
2015-04-10 13:43:40 +03:00
2015-04-10 12:50:56 +03:00
}
2014-04-29 15:45:31 +04:00
elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {
2014-05-12 11:07:30 +04:00
my $get_h = $pass->show( $opt_n, $opt_u );
2016-02-08 19:11:37 +03:00
my $get_pass = $get_h->{password};
print colored( "Password for $opt_n not found\n", 'red' ) and exit 1
if !($get_pass);
$copy->copy( $get_pass );
# Open resource.
2014-05-06 15:18:34 +04:00
my @open_cmd = ( 'xdg-open', $get_h->{resource} );
system(@open_cmd) == 0 or die "Cannot open URI: $!\n";
2015-04-10 13:43:40 +03:00
print colored( "Password copied to clipboard.\n", 'bold green' );
2014-05-12 15:25:22 +04:00
print "Trying to open ";
2015-04-10 13:43:40 +03:00
print colored( $get_h->{resource} . "\n", 'bold blue' );
2014-04-29 15:45:31 +04:00
}
2014-05-12 17:16:10 +04:00
2014-05-12 11:07:30 +04:00
# Remove string from db
elsif ( defined($opt_r) and defined($opt_i) ) {
my $store_h = { id => $opt_i, };
2014-05-12 13:46:23 +04:00
$pass->remove($store_h) == 0 or die "Oops! 111: pm.pl. $!\n";
2015-04-10 13:43:40 +03:00
print colored( "Password was removed!\n", 'bold red' );
2014-05-12 11:07:30 +04:00
}
2014-04-29 15:45:31 +04:00
elsif ( defined($opt_w)
and defined($opt_n)
and defined($opt_l)
and !defined($opt_p) )
{
# Generate password and store it into DB
2015-04-10 12:50:56 +03:00
$opt_g = '' if !($opt_g);
2014-05-05 23:51:31 +04:00
2016-05-01 16:27:28 +03:00
if ( defined($opt_e) ) {
$pass_length=$opt_e;
}
$opt_p = $pass->generate($pass_length);
2014-05-12 15:41:05 +04:00
2014-05-05 23:51:31 +04:00
my $store_h = {
2014-05-06 15:18:34 +04:00
name => $opt_n,
resource => $opt_l,
2014-05-12 15:41:05 +04:00
password => $opt_p,
2014-05-06 15:18:34 +04:00
username => $opt_u,
2014-05-12 17:16:10 +04:00
comment => $opt_c,
2015-04-10 12:50:56 +03:00
group => $opt_g,
2014-05-05 23:51:31 +04:00
};
$pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n";
2014-05-12 17:16:10 +04:00
$copy->copy($opt_p);
2015-04-10 13:43:40 +03:00
print colored( "Password was stored into DB!\n", 'green' );
2014-04-29 15:45:31 +04:00
}
elsif ( defined($opt_w)
and defined($opt_n)
and defined($opt_l)
and defined($opt_p) )
{
# Store new password into DB
2015-04-10 12:50:56 +03:00
$opt_g = '' if !($opt_g);
2014-05-05 23:51:31 +04:00
my $store_h = {
2014-05-06 15:18:34 +04:00
name => $opt_n,
resource => $opt_l,
password => $opt_p,
gen => 0,
username => $opt_u,
2014-05-12 17:16:10 +04:00
comment => $opt_c,
2015-04-10 12:50:56 +03:00
group => $opt_g,
2014-05-05 23:51:31 +04:00
};
$pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n";
2015-04-10 13:43:40 +03:00
print colored( "Password was stored into DB!\n", 'green' );
2014-04-29 15:45:31 +04:00
}
2015-04-10 13:43:40 +03:00
2014-05-13 17:57:14 +04:00
# Export
elsif ( defined($opt_x) ) {
$pass->export($opt_x);
2015-04-10 13:43:40 +03:00
print colored( "Dabase stored in $opt_x\n", 'green' );
2014-05-13 17:57:14 +04:00
}
2016-05-01 16:27:28 +03:00
elsif ( defined($opt_b) ) {
$pass->import_db($opt_b);
print colored( "Database imported from $opt_b\n", 'green' );
}
2014-04-29 15:45:31 +04:00
else {
2014-05-12 15:41:05 +04:00
$usage->show();
2016-05-01 16:27:28 +03:00
}