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 {
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';

View File

@ -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);

19
GPG.pm
View File

@ -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",
"gpg", "--output", "$db",
"-a", "--default-recipient-self",
"$file"
"--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;
}

View File

@ -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;

10
pm.pl
View File

@ -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) ) {