opennetadmin / ona

OpenNetAdmin IP Address Management (IPAM) system
opennetadmin.com
GNU General Public License v2.0
142 stars 35 forks source link

Check for db and user before trying to create. #50

Open bstillman opened 11 years ago

bstillman commented 11 years ago

For security reasons, databases and user accounts should be created by a DBA, not via a script. Privileges to drop/create users and databases should not be given to application accounts. When the database and user are pre-created, two things happen during the install:

1) It reports the database was dropped and created, although this isn't true. The user doesn't have privilege to drop nor create. The install does continue though. Maybe a check could be added to see if the database already exists and report accordingly? If it exists, check for tables. No tables, it's done. If there are tables, put them in an array and loop through dropping them. The install continued, so I didn't worry about it.

2) It fails with "Failed to create system user...". The install fails with a fatal error. For this, I did the following as a temporary fix. I figured I'd share in case anyone else comes across the same thing.

In install/install.php, lines 494 through 507. Currently looks like:

          if ($status == 0) {
            // it is likely that this method here is mysql only?
            if(@$db->Execute("GRANT ALL ON {$database_name}.* TO '{$sys_login}'@'localhost' IDENTIFIED BY '{$sys_passwd}'")) {
                @$db->Execute("GRANT ALL ON {$database_name}.* TO '{$sys_login}'@'%' IDENTIFIED BY '{$sys_passwd}'");
                @$db->Execute("GRANT ALL ON {$database_name}.* TO '{$sys_login}'@'{$database_host}' IDENTIFIED BY '{$sys_passwd}'");
                @$db->Execute("FLUSH PRIVILEGES");
                $text .= "<img src=\"{$images}/silk/accept.png\" border=\"0\" /> Created system user '{$sys_login}'.<br>";
                printmsg("INFO => Created new DB user: {$sys_login}",0);
            }
            else {
                $status++;
                $text .= "<img src=\"{$images}/silk/exclamation.png\" border=\"0\" /> Failed to create system user '{$sys_login}'.<br><span style='font-size: xx-small;'>".$db->ErrorMsg()."</span><br>";
                printmsg("ERROR => There was an error creating DB user: ".$db->ErrorMsg(),0);
            }

            // add the default domain to the system
            // This is a manual add with hard coded values for timers.
            $xmldefdomain = <<<EOL

Change to:

          if ($status == 0) {
            // it is likely that this method here is mysql only?
            if ($db->NConnect( $database_host, $sys_login, $sys_passwd, $database_name)) {
            $text .= "<img src=\"{$images}/silk/accept.png\" border=\"0\" /> System user '{$sys_login}' already exists.<br>";
                printmsg("INFO => DB user exists: {$sys_login}",0);
            }
            elseif(@$db->Execute("GRANT ALL ON {$database_name}.* TO '{$sys_login}'@'localhost' IDENTIFIED BY '{$sys_passwd}'")) {
                @$db->Execute("GRANT ALL ON {$database_name}.* TO '{$sys_login}'@'%' IDENTIFIED BY '{$sys_passwd}'");
                @$db->Execute("GRANT ALL ON {$database_name}.* TO '{$sys_login}'@'{$database_host}' IDENTIFIED BY '{$sys_passwd}'");
                @$db->Execute("FLUSH PRIVILEGES");
                $text .= "<img src=\"{$images}/silk/accept.png\" border=\"0\" /> Created system user '{$sys_login}'.<br>";
                printmsg("INFO => Created new DB user: {$sys_login}",0);
            }
            else {
                $status++;
                $text .= "<img src=\"{$images}/silk/exclamation.png\" border=\"0\" /> Failed to create system user '{$sys_login}'.<br><span style='font-size: xx-small;'>".$db->ErrorMsg()."</span><br>";
                printmsg("ERROR => There was an error creating DB user: ".$db->ErrorMsg(),0);
            }

            // add the default domain to the system
            // This is a manual add with hard coded values for timers.
            $xmldefdomain = <<<EOL