PM/Database.pm

208 lines
4.8 KiB
Perl
Raw Permalink Normal View History

2014-04-27 23:11:52 +04:00
package Database;
use DBI;
2014-04-28 12:04:17 +04:00
use GPG;
2015-04-10 12:50:56 +03:00
use Term::ANSIColor;
2014-04-27 23:11:52 +04:00
2014-05-06 00:40:26 +04:00
use Password;
2014-04-27 23:11:52 +04:00
sub new {
my $class = shift;
2014-04-28 12:17:17 +04:00
# Get home dir
2014-04-28 16:14:23 +04:00
my $home = $ENV{HOME};
2014-04-28 00:04:16 +04:00
2014-04-28 14:07:01 +04:00
my $gpg = GPG->new($home);
2014-04-28 12:17:17 +04:00
my $self = {
_home => $home,
_gpg => $gpg,
2014-04-27 23:11:52 +04:00
};
bless $self, $class;
return $self;
}
2014-04-27 23:35:58 +04:00
2014-04-28 00:04:16 +04:00
sub connect {
2014-04-28 14:07:01 +04:00
my ( $self, $db_file ) = @_;
2014-04-28 12:17:17 +04:00
my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "" );
2014-04-28 00:04:16 +04:00
return $dbh;
}
2016-05-09 12:01:40 +03:00
sub print_table {
my ( $sth ) = @_;
@max = (0, 0, 0, 0, 0, 0);
@rows;
$sum = 2;
@colors = ("white", "magenta", "bold magenta", "blue", "green", "yellow");
while ( my @row = $sth->fetchrow_array() )
{
push(@rows, \@row);
}
@labels = ("ID", "NAME", "GROUP", "RESOURCE", "USERNAME", "COMMENT");
@rows=reverse(@rows);
push(@rows, \@labels);
foreach my $row(@rows) {
for $i (0 .. 5) {
$str = $row -> [$i];
$l=length($str);
if ($l > $max[$i]) {
$max[$i] = $l;
}
}
}
foreach my $num(@max) {
$sum += ($num + 3);
}
while (my $row = pop(@rows)) {
print "-" x $sum . "\n";
for $i (0 .. 5) {
$l=$max[$i];
$string=$row -> [$i];
$strl=$l-length($string);
$color=$colors[$i];
printf "| %s ", colored($string, $color).(' ' x $strl);
}
print " |\n";
}
print "-" x $sum . "\n";
}
2014-04-28 16:14:23 +04:00
# Query proccessing mechanism
2014-04-28 00:04:16 +04:00
sub mdo {
2014-04-28 16:14:23 +04:00
my ( $self, $query ) = @_;
my $db_file = $query->{file};
my $q = $query->{query};
my $name = $query->{name};
my $type = $query->{type};
2015-04-10 12:50:56 +03:00
my $g = $query->{group};
2014-04-28 16:14:23 +04:00
my $dbh = Database->connect($db_file);
# Select
if ( $type eq 'select' ) {
2014-05-06 15:01:07 +04:00
# Bad hack
if ( $name eq 'all' ) {
2014-05-12 17:16:10 +04:00
my $q
2015-04-10 12:01:28 +03:00
= 'select id, name, `group`, resource, username, comment from passwords';
2014-05-06 15:01:07 +04:00
my $sth = $dbh->prepare($q);
my $rv = $sth->execute();
2014-05-07 15:30:18 +04:00
2016-05-09 12:01:40 +03:00
print_table ($sth);
2014-05-12 15:25:22 +04:00
2014-05-06 16:04:18 +04:00
# Remove unencrypted file
my @rm_cmd = ( "rm", "-f", "$db_file" );
2014-05-12 15:25:22 +04:00
system(@rm_cmd) == 0
or die "Cannot remove unencrypted database! $!\n";
2014-05-06 15:01:07 +04:00
exit 0;
}
2015-04-10 12:50:56 +03:00
# Show group
if ($g) {
my $sth = $dbh->prepare($q);
my $rv = $sth->execute();
2016-05-09 12:01:40 +03:00
print_table($sth);
2015-04-10 12:50:56 +03:00
# Remove unencrypted file
my @rm_cmd = ( "rm", "-f", "$db_file" );
system(@rm_cmd) == 0
or die "Cannot remove unencrypted database! $!\n";
exit 0;
}
2014-04-28 16:14:23 +04:00
my $sth = $dbh->prepare($q);
$sth->execute();
2014-05-06 15:01:07 +04:00
2016-05-13 12:46:48 +03:00
my ( $id, $name, $resource, $password, $username ) = $sth->fetchrow_array();
2014-04-28 16:14:23 +04:00
my $q_hash = {
2014-05-12 11:07:30 +04:00
id => $id,
2014-04-28 16:14:23 +04:00
name => $name,
resource => $resource,
password => $password,
2016-05-13 12:46:48 +03:00
username => $username
2014-04-28 16:14:23 +04:00
};
return $q_hash;
}
elsif ( $type eq 'do' ) {
2014-05-06 00:40:26 +04:00
$dbh->do("$q") or die "$!\n";
2014-04-28 16:14:23 +04:00
return 0;
}
else {
print STDERR "Something went wrong! $!\n";
return 1;
}
return 1;
2014-04-28 00:04:16 +04:00
}
2014-04-28 14:07:01 +04:00
# Create config dirrectory and DB if not exist
2014-04-28 00:04:16 +04:00
sub create_base {
2014-04-28 12:17:17 +04:00
my ($self) = @_;
my $home = $self->{_home};
my $pm_dir = $home . "/.PM/";
2014-04-28 14:07:01 +04:00
my $gpg = $self->{_gpg};
2014-04-28 12:17:17 +04:00
# Check dir
2014-05-06 12:23:56 +04:00
if ( !( -d $pm_dir ) or !( -e $pm_dir . "db.sqlite" ) ) {
# Remove old configuration dirrectory
print "Remove old dirrectory...\n";
my @rm_old_cmd = ( 'rm', '-rf', $pm_dir );
system(@rm_old_cmd) == 0 or die "Cannot remove $pm_dir: $!\n";
2014-04-28 12:17:17 +04:00
# Create dirrectory
2014-05-06 00:40:26 +04:00
print "Creating configuration dirrectory...\n";
2014-04-28 12:17:17 +04:00
my @mkdir_cmd = ( "mkdir", "$pm_dir" );
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
2014-05-06 12:23:56 +04:00
my $pass = Password->new();
my $string = $pass->generate();
2014-05-06 00:40:26 +04:00
my $first_sqlite = "/tmp/$string";
2014-04-28 14:07:01 +04:00
2014-04-28 12:17:17 +04:00
# Create DB file
2014-04-28 14:07:01 +04:00
my @createdb_cmd = ( "touch", "$first_sqlite" );
2014-04-28 12:17:17 +04:00
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
2014-05-06 00:40:26 +04:00
print "Creating database...\n";
2014-05-06 12:23:56 +04:00
2014-04-28 14:07:01 +04:00
# Create table.
my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" );
2014-04-28 12:17:17 +04:00
print "Create database schema\n";
my $q_table
2015-04-10 12:01:28 +03:00
= "CREATE TABLE passwords(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR(32) NOT NULL,
username VARCHAR(32) NOT NULL,
resource TEXT NOT NULL,
password VARCHAR(32) NOT NULL,
comment TEXT NOT NULL,
'group' VARCHAR(32) NOT NULL
)";
2014-04-28 12:17:17 +04:00
$dbh->do($q_table);
2014-05-06 00:40:26 +04:00
print "Encrypt database...\n";
2014-05-06 12:23:56 +04:00
2014-04-28 12:17:17 +04:00
# Encrypt db
2014-04-28 14:07:01 +04:00
$gpg->encrypt_db($first_sqlite);
2014-04-28 12:17:17 +04:00
return 0;
}
else {
print "Dirrectory is exist!\n";
2014-05-06 12:18:44 +04:00
return 1;
2014-04-28 12:17:17 +04:00
}
2014-05-06 12:18:44 +04:00
return 1;
2014-04-28 00:04:16 +04:00
}
2014-04-28 12:17:17 +04:00
1;