PM/Database.pm

143 lines
3.4 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;
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};
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' ) {
my $q = 'select name, resource, username from passwords';
my $sth = $dbh->prepare($q);
my $rv = $sth->execute();
2014-05-07 15:30:18 +04:00
printf "%-11s %-11s %-11s\n", "NAME", "RESOURCE", "USERNAME";
2014-05-06 15:01:07 +04:00
while ( my ( $name, $resource, $username )
= $sth->fetchrow_array() )
{
2014-05-07 15:30:18 +04:00
printf "%-11s %-11s %-11s\n",
$name, $resource, "\t$username";
2014-05-06 15:01:07 +04:00
}
2014-05-06 16:04:18 +04:00
# Remove unencrypted file
my @rm_cmd = ( "rm", "-f", "$db_file" );
system(@rm_cmd) == 0 or die "Cannot remove unencrypted database! $!\n";
2014-05-06 15:01:07 +04:00
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-04-28 16:14:23 +04:00
my ( $name, $resource, $password ) = $sth->fetchrow_array();
my $q_hash = {
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
2014-05-12 10:33:04 +04:00
= "create table passwords(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(32), username VARCHAR(32),
2014-05-06 11:57:48 +04:00
resource TEXT, password TEXT)";
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;