Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 148 additions & 19 deletions server/sql/create_tables.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
# NicTool v2.00-rc1 Copyright 2001 Damon Edwards, Abe Shelton & Greg Schueler
# NicTool v2.01+ Copyright 2004-2008 The Network People, Inc.
#
# NicTool is free software; you can redistribute it and/or modify it under
# the terms of the Affero General Public License as published by Affero,
# NicTool is free software; you can redistribute it and/or modify it under
# the terms of the Affero General Public License as published by Affero,
# Inc.; either version 1 of the License, or any later version.
#
# NicTool is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# NicTool is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Affero GPL for details.
#
# You should have received a copy of the Affero General Public License
Expand All @@ -20,8 +20,22 @@
use Crypt::KeyDerivation;
use DBI;
use English;
use Getopt::Long qw/HelpMessage/;

$|++;
my $test_run = $ARGV[0] eq '-test';

GetOptions(
'test' => \my $test_run,
'environment' => \my $environment,
'db-hostname=s' => \my $db_hostname,
'db-root-password=s' => \my $db_root_password,
'nictool-db-name=s' => \my $nictool_db_name,
'nictool-db-user=s' => \my $nictool_db_user,
'nictool-db-user-password=s' => \my $nictool_db_user_password,
'nictool-root-email=s' => \my $nictool_root_email,
'nictool-root-password=s' => \my $nictool_root_password,
'help' => sub { HelpMessage(0) },
) or HelpMessage(1);

my ($dbh, $db_host) = get_dbh();

Expand All @@ -30,22 +44,76 @@
NicTool DSN (database connection settings)
#########################################################################
";
my $db = answer("the NicTool database name", 'nictool');

my $db = undef;

if ($environment) {
$nictool_db_name = undef;
die "NICTOOL_DB_NAME not set!!!\n" unless $ENV{NICTOOL_DB_NAME};
$db = $ENV{NICTOOL_DB_NAME};
} elsif ($nictool_db_name) {
$db = $nictool_db_name;
} else {
$db = answer("the NicTool database name", 'nictool');
}

die "Sorry\n" if $db =~/^mysql$/i;

my $db_user = answer("the NicTool database user", 'nictool');
my $db_pass = get_password("the DB user $db_user");
my $db_user = undef;

if ($environment) {
$nictool_db_user = undef;
die "NICTOOL_DB_USER not set!!!\n" unless $ENV{NICTOOL_DB_USER};
$db_user = $ENV{NICTOOL_DB_USER};
} elsif ($nictool_db_user) {
$db_user = $nictool_db_user;
} else {
$db_user = answer("the NicTool database user", 'nictool');
}

my $db_pass = undef;

if ($environment) {
$nictool_db_user_password = undef;
die "NICTOOL_DB_USER_PASSWORD not set!!!\n" unless $ENV{NICTOOL_DB_USER_PASSWORD};
$db_pass = $ENV{NICTOOL_DB_USER_PASSWORD};
} elsif ($nictool_db_user_password) {
$db_pass = $nictool_db_user_password;
} else {
$db_pass = get_password("the DB user $db_user");
}

print "\n
#########################################################################
NicTool admin user (http://root\@$db_host/)
#########################################################################
";
my $nt_root_email;
while(!$nt_root_email){
$nt_root_email = answer("the NicTool 'root' users email address", $nt_root_email);
my $nt_root_email = undef;

if ($environment) {
$nictool_root_email = undef;
die "ROOT_USER_EMAIL not set!!!\n" unless $ENV{ROOT_USER_EMAIL};
$nt_root_email = $ENV{ROOT_USER_EMAIL};
} elsif ($nictool_root_email) {
$nt_root_email = $nictool_root_email;
} else {
while(!$nt_root_email){
$nt_root_email = answer("the NicTool 'root' users email address", $nt_root_email);
}
}

my $clear_pass = undef;

if ($environment) {
$nictool_root_password = undef;
die "ROOT_USER_PASSWORD not set!!!\n" unless $ENV{ROOT_USER_PASSWORD};
$clear_pass = $ENV{ROOT_USER_PASSWORD};
} elsif ($nictool_root_password) {
$clear_pass = $nictool_root_password;
} else {
$clear_pass = get_password("the NicTool user 'root'");
}
my $clear_pass = get_password("the NicTool user 'root'");

my $salt = _get_salt(16);
my $pass_hash = unpack("H*", Crypt::KeyDerivation::pbkdf2($clear_pass, $salt, 5000, 'SHA512'));

Expand Down Expand Up @@ -74,7 +142,16 @@
# Create database and initial privileges
$dbh->do("DROP DATABASE IF EXISTS $db");
$dbh->do("CREATE DATABASE $db");
$dbh->do("GRANT ALL PRIVILEGES ON $db.* TO $db_user\@$db_host IDENTIFIED BY '$db_pass'");

# remote sessions will never be recognized as 'db_user'@'db_hostname' as MySQL does
# a reverse lookup of the initiating host's IP address and uses that as the
# connection string, eg 'db_user'@'x.x.x.x'
if ($db_host eq 'localhost' || $db_host eq '127.0.0.1' || $db_host eq '::1') {
$dbh->do("GRANT ALL PRIVILEGES ON $db.* TO $db_user\@$db_host IDENTIFIED BY '$db_pass'");
} else {
$dbh->do("GRANT ALL PRIVILEGES ON $db.* TO $db_user\@'%' IDENTIFIED BY '$db_pass'");
}

$dbh->do("USE $db");

my @sql_files = get_sql_files();
Expand Down Expand Up @@ -112,11 +189,32 @@ sub get_dbh {
#########################################################################
Administrator DSN (database connection settings)
#########################################################################\n";
my $db_host = answer("database hostname", '127.0.0.1');

system "stty -echo";
my $db_root_pw = answer("mysql root password");
system "stty echo";
my $db_host = undef;

if ($environment) {
$db_hostname = undef;
die "DB_HOSTNAME not set!!!\n" unless $ENV{DB_HOSTNAME};
$db_host = $ENV{DB_HOSTNAME};
} elsif ($db_hostname) {
$db_host = $db_hostname;
} else {
$db_host = answer("database hostname", '127.0.0.1');
}

my $db_root_pw = undef;

if ($environment) {
$db_root_password = undef;
die "DB_ROOT_PASSWORD not set!!!\n" unless $ENV{DB_ROOT_PASSWORD};
$db_root_pw = $ENV{DB_ROOT_PASSWORD};
} elsif ($db_root_password) {
$db_root_pw = $db_root_password;
} else {
system "stty -echo";
$db_root_pw = answer("mysql root password");
system "stty echo";
}
print "\n";

return if $test_run;
Expand All @@ -131,7 +229,7 @@ sub get_dbh {
sub answer {

my ( $question, $default, $timeout) = @_;

# this sub is useless without a question.
unless ($question) {
die "question called incorrectly. RTFM. \n";
Expand All @@ -149,7 +247,7 @@ sub answer {
alarm $timeout;
$response = <STDIN>;
alarm 0;
};
};
if ($EVAL_ERROR) {
( $EVAL_ERROR eq "alarm\n" )
? print "timed out!\n"
Expand Down Expand Up @@ -218,3 +316,34 @@ sub _get_salt {
};
return $salt;
}

=head1 NAME

create_tables.pl - configure the NicTool database.

=head1 SYNOPSIS

--help Displays this message. If called without any arguments, this script
will simply run interactively.

--test Perform a test run.

--environment Use environment variables to set up the database. These
are as follows: DB_HOSTNAME, DB_ROOT_PASSWORD,
NICTOOL_DB_NAME, NICTOOL_DB_USER, NICTOOL_DB_USER_PASSWORD,
ROOT_USER_EMAIL, ROOT_USER_PASSWORD. If this flag is
present, the remaining arguments below are ignored.

--db-hostname The MySQL database hostname or IP address to connect to.
--db-root-password The MySQL root user password.
--nictool-db-name The name of the NicTool database. Defaults to 'nictool'.
--nictool-db-user The MySQL user of the NicTool database. Defaults to 'nictool'.
--nictool-db-user-password The password to use for the MySQL user of the NicTool database.
--nictool-root-email The e-mail address of the 'root' user of the NicTool web UI.
--nictool-root-password The password for the 'root' user of the NicTool web UI.

=head1 VERSION

2.33

=cut