First buggy but worked version :)

This commit is contained in:
Difrex 2014-05-06 00:40:26 +04:00
parent ea307156d8
commit 992d7268ef
5 changed files with 35 additions and 19 deletions

View File

@ -5,15 +5,14 @@ use Clipboard;
sub new { sub new {
my $class = shift; my $class = shift;
my $self = { _password => shift, }; my $self = {};
bless $self, $class; bless $self, $class;
return $self; return $self;
} }
sub copy { sub copy {
my ($self) = @_; my ( $self, $password ) = @_;
my $password = $self->{_password};
if ( 'Clipboard::Xclip' eq $Clipboard::driver ) { if ( 'Clipboard::Xclip' eq $Clipboard::driver ) {
no warnings 'redefine'; no warnings 'redefine';

View File

@ -3,6 +3,8 @@ package Database;
use DBI; use DBI;
use GPG; use GPG;
use Password;
sub new { sub new {
my $class = shift; my $class = shift;
@ -51,7 +53,7 @@ sub mdo {
return $q_hash; return $q_hash;
} }
elsif ( $type eq 'do' ) { elsif ( $type eq 'do' ) {
$dbh->do($q) or die "$!\n"; $dbh->do("$q") or die "$!\n";
return 0; return 0;
} }
else { else {
@ -72,15 +74,19 @@ sub create_base {
if ( !( -d $pm_dir ) ) { if ( !( -d $pm_dir ) ) {
# Create dirrectory # Create dirrectory
print "Creating configuration dirrectory...\n";
my @mkdir_cmd = ( "mkdir", "$pm_dir" ); my @mkdir_cmd = ( "mkdir", "$pm_dir" );
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n"; 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 # Create DB file
my @createdb_cmd = ( "touch", "$first_sqlite" ); my @createdb_cmd = ( "touch", "$first_sqlite" );
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n"; system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
print "Creating database...\n";
# Create table. # Create table.
my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" ); my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" );
print "Create database schema\n"; print "Create database schema\n";
@ -88,6 +94,7 @@ sub create_base {
= "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)"; = "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
$dbh->do($q_table); $dbh->do($q_table);
print "Encrypt database...\n";
# Encrypt db # Encrypt db
$gpg->encrypt_db($first_sqlite); $gpg->encrypt_db($first_sqlite);

19
GPG.pm
View File

@ -1,12 +1,13 @@
# GPG layer for encrypt/decrypt passwords database # GPG layer for encrypt/decrypt passwords database
package GPG; package GPG;
our $gpg = '/usr/bin/gpg'; # Debug
use Data::Dumper;
sub new { sub new {
my $class = shift; my $class = shift;
my $home = shift; my $home = $ENV{HOME};
my $db = $home . "/.PM/db.sqlite"; my $db = $home . "/.PM/db.sqlite";
my $self = { _db => $db, }; my $self = { _db => $db, };
@ -25,13 +26,13 @@ sub encrypt_db {
@rm_db = ( "rm", "-f", "$db" ); @rm_db = ( "rm", "-f", "$db" );
system(@rm_db) == 0 or die "Cannot remove old database: $!\n"; 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 = ( @enc_cmd = (
"$gpg", "--output", "gpg", "--output", "$db",
"$db", "--encrypt",
"-a", "--default-recipient-self", "-a", "--default-recipient-self",
"$file" "--encrypt", "$file",
); );
system(@enc_cmd) == 0 system(@enc_cmd) == 0
or die "Cannot encrypt!\nDecrypted file: $file\nTraceback: $!\n"; or die "Cannot encrypt!\nDecrypted file: $file\nTraceback: $!\n";
@ -48,6 +49,8 @@ sub decrypt_db {
my ($self) = @_; my ($self) = @_;
my $db = $self->{_db}; my $db = $self->{_db};
my $gpg = '/usr/bin/gpg';
# Generate random file name # Generate random file name
my @chars = ( "A" .. "Z", "a" .. "z" ); my @chars = ( "A" .. "Z", "a" .. "z" );
my $string; my $string;
@ -55,8 +58,8 @@ sub decrypt_db {
my $file = '/tmp/' . 'pm.' . $string; my $file = '/tmp/' . 'pm.' . $string;
# gpg --output /tmp/decryptfile --decrypt $db # gpg --output /tmp/decryptfile --decrypt $db
@dec_cmd = ( "$gpg", "--decrypt", "$db", "--output", "$file" ); @dec_cmd = ( "$gpg", "--output", "$file", "--decrypt", "$db" );
system(@sys_dec_cmd) == 0 or die "Cannot decrypt $db: $!\n"; system(@dec_cmd) == 0 or die "Cannot decrypt $db: $!\n";
return $file; return $file;
} }

View File

@ -7,6 +7,9 @@ use utf8;
use Database; use Database;
use GPG; use GPG;
# Debug
use Data::Dumper;
sub new { sub new {
my $class = shift; my $class = shift;
@ -67,7 +70,8 @@ sub save {
# Decrypt database # Decrypt database
my $dec_db_file = $gpg->decrypt_db(); my $dec_db_file = $gpg->decrypt_db();
my $q 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 = { my $mdo_q = {
file => $dec_db_file, file => $dec_db_file,
name => $name, name => $name,
@ -100,6 +104,7 @@ sub check_config {
my $db = $self->{_db}; my $db = $self->{_db};
$db->create_base(); $db->create_base();
print "Done!\n";
return 0; return 0;
} }
return 1; return 1;

10
pm.pl
View File

@ -7,7 +7,7 @@ use ClipPass;
# Debug # Debug
use Data::Dumper; use Data::Dumper;
our $VERSION = '0.0.1c'; our $VERSION = '0.0.1-alpha';
sub usage() { sub usage() {
print STDERR << "EOF"; print STDERR << "EOF";
@ -61,7 +61,7 @@ sub init() {
my $pass = Password->new(); my $pass = Password->new();
# Don't use it's before GPG and Database # 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 # Parse cmd line
init(); init();
@ -73,9 +73,11 @@ my $copy = ClipPass->new();
if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) { if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) {
my $get_h = $pass->show($opt_n); 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) ) { elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {