diff --git a/database_settings.sql b/database_settings.sql index 76c3601..cc2f12a 100644 --- a/database_settings.sql +++ b/database_settings.sql @@ -1,15 +1,13 @@ create table channels(id int primary key not null, server_id int not null, -- foreign key in servers table name text not null, - public int not null, - accessor int -- foreign key in accessors table + public int not null ); create table users(id int primary key not null, name text not null, password text not null, - privileges int not null, -- 0 - normal user, 1 - moderator, 2 - administrator - accessor int -- foreign key in accessors table + privileges int not null -- 0 - normal user, 1 - moderator, 2 - administrator ); create table servers(id int primary key not null, diff --git a/frontend.pm b/frontend.pm index f992519..464ccae 100644 --- a/frontend.pm +++ b/frontend.pm @@ -17,6 +17,7 @@ package frontend; use IO::Socket; +use Digest::SHA; use File::Spec; use Time::Piece; use DBI; @@ -450,11 +451,22 @@ sub sendResponse { sub httpServerWorker { my $db = DBI->connect("DBI:SQLite:dbname=$configuration::database", "", "", {RaiseError=>1}); + my $query = $db->prepare(qq(select id from users;)); + $query->execute(); + my @row = $query->fetchrow_array(); + if(scalar(@row)==0) { + # Create default user + my $password = Digest::SHA::sha256_hex("admin"); + $query = $db->prepare(qq(insert into users values(0, "admin", "$password", 2);)); + $query->execute(); + } + my $server = new IO::Socket::INET(LocalHost=>"localhost", LocalPort=>$configuration::httpServerPort, Proto=>"tcp", Listen=>1, Reuse=>1); if(!$server) { print("Failed to open HTTP server on port $configuration::httpServerPort\n"); return; } + while(1) { my $client = $server->accept(); my $buffer; diff --git a/frontend_routes.pm b/frontend_routes.pm index 8a493e4..00e6b39 100644 --- a/frontend_routes.pm +++ b/frontend_routes.pm @@ -91,8 +91,7 @@ sub handlePath { } my $username = $parameters{'username'}; - #my $hashedPassword = Digest::SHA::sha256_hex($parameters{"password"}); - my $hashedPassword = $parameters{"password"}; + my $hashedPassword = Digest::SHA::sha256_hex($parameters{"password"}); my $query = $aConnection->prepare(qq(select name, password from users where name=?;)); $query->execute($username); my @row = $query->fetchrow_array(); @@ -237,7 +236,8 @@ sub handlePath { $query->execute($session->{"username"}); my @row = $query->fetchrow_array(); my $password = $row[0]; - if($parameters{"currentPassword"} ne $password) { + my $hashedPassword = Digest::SHA::sha256_hex($password); + if($hashedPassword ne $password) { frontend::sendBadRequest($aClient, "Wrong password"); return 1; } @@ -247,7 +247,7 @@ sub handlePath { } $query = $aConnection->prepare(qq(update users set password=? where name=?;)); - $query->execute($parameters{"newPassword"}, $session->{"username"}); + $query->execute(Digest::SHA::sha256_hex($parameters{"newPassword"}), $session->{"username"}); frontend::redirect($aClient, "/password_changed.html"); return 1; diff --git a/prepare_database.sh b/prepare_database.sh old mode 100644 new mode 100755