root/honeyclient/tags/exp/UP2-jpuchalski-active_content/bin/install_honeyclient_db.pl

Revision 601, 4.8 kB (checked in by kindlund, 2 years ago)

Merged DB branch back into trunk.

  • Property svn:executable set to *
  • Property svn:keywords set to Id "$file"
Line 
1 #!/usr/bin/perl -Ilib -w
2
3 # $Id$
4
5 use strict;
6 use warnings;
7 use Carp ();
8
9 use ExtUtils::MakeMaker qw(prompt);
10 use Net::IP qw(ip_is_ipv4);
11 use DBI;
12 use DBI::Const::GetInfoType;
13 use HoneyClient::Util::Config qw(getVar);
14
15 print "************************************************\n" .
16       "*** HoneyClient Database Installation Script ***\n" .
17       "************************************************\n" .
18       "\n" .
19       "This script will install and configure the\n" .
20       "HoneyClient database onto an existing MySQL server.\n" .
21       "\n" .
22       "Before running this script, you need to edit your\n" .
23       "etc/honeyclient.xml global configuration file and\n" .
24       "make sure that the <HoneyClient/><DB/> section\n" .
25       "contains valid database connection information.\n" .
26       "\n";
27
28 # Retrieve values from the global configuration file.
29 my $host = getVar(name => "host", namespace => "HoneyClient::DB");
30 my $user = getVar(name => "user", namespace => "HoneyClient::DB");
31 my $pass = getVar(name => "pass", namespace => "HoneyClient::DB");
32 my $database_name = getVar(name => "dbname", namespace => "HoneyClient::DB");
33 my ($question, $root_password);
34
35 print "The following database configuration was found:\n\n";
36
37 my $buf = sprintf("%s %s\t= '%s'", " " x 4,  "host", $host) . "\n" .
38           sprintf("%s %s\t= '%s'", " " x 4,  "user", $user) . "\n" .
39           sprintf("%s %s\t= '%s'", " " x 4,  "pass", $pass) . "\n" .
40           sprintf("%s %s\t= '%s'", " " x 4,  "dbname", $database_name) . "\n";
41
42 print $buf . "\n";
43
44 $question = prompt("Is this correct?", "yes");
45 print "\n";
46
47 if ($question !~ /^y.*/i) {
48     print "Please edit the etc/honeyclient.xml file\n" .
49           "accordingly re-run this script.\n";
50     exit;
51 }
52
53 # Get the root password.
54 system("stty -echo");
55 $root_password = prompt("Please enter your database 'root' password:");
56 system("stty echo");
57 print "\n";
58 print "\n";
59
60 my $sql = undef;
61 my $dsn = "DBI:mysql:database=mysql;host=" . $host;
62
63 eval {
64     # Connect and Create Database
65     my $dbh = DBI->connect($dsn, 'root', $root_password, {'RaiseError' => 1});
66     if ($dbh eq '') {
67         Carp::croak("Installation Failed: " . $DBI::errstr);
68     }
69
70     my $database_system_name = $dbh->get_info($GetInfoType{SQL_DBMS_NAME});
71     my $database_system_version = $dbh->get_info($GetInfoType{SQL_DBMS_VER});
72
73     # Extract the major version number.
74     $database_system_version =~ s/^(\d.*?)\..*/$1/;
75
76     if (($database_system_name !~ /^mysql/i) or
77         ($database_system_version < 5)) {
78
79         print "Your database does not appear to be running MySQL v5.0\n" .
80               "or greater.  This code will only work properly on databases\n" .
81               "with this type and version.\n";
82
83         $dbh->disconnect() if $dbh;
84         exit;
85     }
86
87     # Create the database.
88     print "* Creating database name '" . $database_name . "'.\n\n";
89     $sql = "CREATE DATABASE " . $database_name;
90     print "Issuing SQL Command:\n" . $sql . "\n";
91     proceed();
92     $dbh->do($sql);
93
94     # Get the IP address of the host system where the Manager will be
95     # installed to.
96     my $manager_address = "127.0.0.1";
97     $question = prompt("Will the database and the HoneyClient::Manager\n" .
98                        "run on the same host system?", "yes");
99     print "\n";
100
101     if ($question !~ /^y.*/i) {
102         my $ip = "x";
103         my $is_valid = 0;
104         while (!$is_valid) {
105             $manager_address = prompt("Enter the IP address of the host system\n" .
106                                       "that the HoneyClient::Manager will run\n" .
107                                       "from (wildcard is %):\n", "172.16.164.%");
108
109             $ip = $manager_address;
110             $ip =~ s/%/0/g;
111             $is_valid = ip_is_ipv4($ip);
112
113             if (!$is_valid) {
114                 print "\n";
115                 print "* Error: Address is not valid! Try again.\n";
116             }
117             print "\n";
118         }
119     }
120
121     # Create a user account to access and manage the database.
122     print "* Creating database user '". $user . "'.\n\n";
123     $sql = "GRANT ALL PRIVILEGES ON " . $database_name .".* TO '" . $user . "\'@\'" .
124            $manager_address . "' IDENTIFIED BY '" . $pass . "'";
125     print "Issuing SQL Command:\n" . $sql . "\n";
126     proceed();
127     $dbh->do($sql);
128
129     # Flush privileges, in order to get MySQL to re-read the GRANT table.
130     print "* Flushing privileges, in order to activate the newly added user.\n\n";
131     $sql = "FLUSH PRIVILEGES";
132     print "Issuing SQL Command:\n" . $sql . "\n";
133     proceed();
134     $dbh->do($sql);
135        
136     $dbh->disconnect() if $dbh;
137 };
138 if ($@) {
139     Carp::croak("Installation Failed: " . $@);
140 }
141
142 sub proceed {
143     print "\n";
144     my $question = prompt("Proceed?", "yes");
145     print "\n";
146     if ($question !~ /^y.*/i) {
147         print "Aborting Installation.\n";
148         exit;
149     }
150 }
151
152 print "Database and user installed successfully.\n";
Note: See TracBrowser for help on using the browser.