PM/pm.pl

139 lines
3.2 KiB
Perl
Raw Normal View History

2014-04-27 03:46:32 +04:00
#!/usr/bin/perl
use Password;
use Getopt::Std;
2014-04-29 15:45:31 +04:00
use ClipPass;
2014-04-27 03:46:32 +04:00
2014-04-28 11:29:38 +04:00
# Debug
use Data::Dumper;
2014-05-06 15:18:34 +04:00
our $VERSION = '0.0.1-beta1';
2014-04-29 15:45:31 +04:00
2014-04-27 03:46:32 +04:00
sub usage() {
2014-04-28 12:17:17 +04:00
print STDERR << "EOF";
2014-04-27 03:46:32 +04:00
Simple password manager writed in Perl.
2014-04-28 16:43:00 +04:00
-s show password
-n [Name of resource] name of resource
-w store new password
-l [Link] link to resource
2014-05-06 11:57:48 +04:00
-u username
2014-04-28 16:43:00 +04:00
-p [Password] password
if key not selected PM generate secure password
and copy it to xclipboard
-r remove password
-o open link
-h show this help screen and exit
-v show version info and exit
2014-04-27 03:46:32 +04:00
2014-04-28 11:29:38 +04:00
Examples:
2014-05-06 15:18:34 +04:00
Show password for resource:
\tpm.pl -s -n LOR
\tPassword copied to xclipboard.\n\t\tURI is http://linux.org.ru/
2014-04-28 11:29:38 +04:00
2014-05-06 15:18:34 +04:00
Store new password:
\tpm.pl -w -n PRON -l http://superpronsite.com/ -p my_secret_password
\tPassword for resource PRON is stored into DB!
2014-04-28 11:29:38 +04:00
2014-05-06 15:18:34 +04:00
Copy password and open link:
\tpm.pl -s -n LOR -o
\tPassword copied to clipboard. Trying to open uri.
2014-04-28 11:29:38 +04:00
2014-04-27 03:46:32 +04:00
EOF
2014-04-28 12:17:17 +04:00
exit 1;
2014-04-27 03:46:32 +04:00
}
sub init() {
2014-05-06 15:18:34 +04:00
my $opt_string = 'swn:l:p:rhvou:';
2014-04-28 12:17:17 +04:00
getopts("$opt_string") or usage();
2014-04-29 15:45:31 +04:00
our (
$opt_s, $opt_w, $opt_n, $opt_r, $opt_l,
2014-05-06 11:57:48 +04:00
$opt_p, $opt_h, $opt_v, $opt_o, $opt_u
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;
usage 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-06 15:18:34 +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};
2014-05-06 00:40:26 +04:00
$copy->copy($get_pass);
2014-05-08 00:21:22 +04:00
# TEST
use Term::ANSIColor;
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';
2014-04-29 15:45:31 +04:00
}
elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {
2014-05-06 15:18:34 +04:00
my $get_h = $pass->show($opt_n, $opt_u);
$copy->copy( $get_h->{password} );
# 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";
print "Password copied to clipboard. Trying to open uri.\n";
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
print "$opt_w, $opt_n, $opt_l, $opt_p\n";
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,
gen => 1,
username => $opt_u,
2014-05-05 23:51:31 +04:00
};
$pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n";
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
print "$opt_w, $opt_n, $opt_l, $opt_p\n";
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-05 23:51:31 +04:00
};
$pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n";
2014-04-29 15:45:31 +04:00
}
else {
2014-05-06 12:18:44 +04:00
usage;
2014-04-29 15:45:31 +04:00
}