PM/Database.pm

202 lines
5.8 KiB
Perl
Raw 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;
}
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
2014-05-12 17:16:10 +04:00
printf "%-11s %-11s %-11s %-11s %-11s\n",
2014-05-12 15:25:22 +04:00
colored( "ID", 'white' ),
colored( "NAME", 'magenta' ),
2015-04-10 12:01:28 +03:00
colored( "GROUP", 'bold magenta' ),
2014-05-12 15:25:22 +04:00
colored( "RESOURCE", 'blue' ),
2014-05-12 17:16:10 +04:00
colored( "USERNAME", 'green' ),
colored( "COMMENT", 'yellow' );
2015-04-10 12:01:28 +03:00
print "=========================================\n";
while ( my ( $id, $name, $group, $resource, $username, $comment )
2014-05-06 15:01:07 +04:00
= $sth->fetchrow_array() )
{
2014-05-12 17:16:10 +04:00
if ( !defined($comment) ) {
$comment = '';
}
2015-04-10 12:01:28 +03:00
printf "%-11s %-11s %-11s %-11s %-11s %-11s\n",
2014-05-12 15:25:22 +04:00
colored( $id, 'white' ),
colored( $name, 'magenta' ),
2015-04-10 12:01:28 +03:00
colored( $group, 'bold magenta' ),
2014-05-12 15:25:22 +04:00
colored( $resource, 'blue' ),
2014-05-12 17:16:10 +04:00
colored( $username, 'green' ),
colored( $comment, 'yellow' );
2014-05-06 15:01:07 +04:00
}
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();
printf "%-11s %-11s %-11s %-11s %-11s\n",
colored( "ID", 'white' ),
colored( "NAME", 'magenta' ),
colored( "GROUP", 'bold magenta' ),
colored( "RESOURCE", 'blue' ),
colored( "USERNAME", 'green' ),
colored( "COMMENT", 'yellow' );
print "=========================================\n";
while ( my ( $id, $name, $group, $resource, $username, $comment )
= $sth->fetchrow_array() )
{
printf "%-11s %-11s %-11s %-11s %-11s %-11s\n",
colored( $id, 'white' ),
colored( $name, 'magenta' ),
colored( $group, 'bold magenta' ),
colored( $resource, 'blue' ),
colored( $username, 'green' ),
colored( $comment, 'yellow' );
}
# 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
2014-05-12 11:07:30 +04:00
my ( $id, $name, $resource, $password ) = $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,
};
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;