File: //scripts.20110531.215904.25158/mysqlconnectioncheck
#!/usr/bin/perl
# cpanel - mysqlconnectioncheck Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use warnings;
use Cpanel::MysqlUtils ();
# Prevent loop
exit if ( $ENV{'MYSQLCCHK'} && $ENV{'MYSQLCCHK'} eq '1' );
$ENV{'MYSQLCCHK'} = 1;
my $need_passreset = '';
my $mysql = Cpanel::MysqlUtils::find_mysql();
my $dbpassword = Cpanel::MysqlUtils::getmydbpass('root'); # read from /root/.my.cnf
my $dbhost = Cpanel::MysqlUtils::getmydbhost();
my $myout;
$ENV{'USER'} = 'root';
if ($dbpassword) {
if ( !check_mysql_connection() ) {
$need_passreset = 'Access Denied';
}
}
else {
require IPC::Open3;
my $newpass = get_mysql_password();
print "No MySQL(R) root password set!\n";
print "Attempting to set the MySQL root user's password.\n";
my $pid = IPC::Open3::open3( \*MYSQL, ">&STDERR", ">&STDERR", '/scripts/mysqlpasswd', '--multistdin' );
print MYSQL 'root' . "\n" . $newpass . "\n\n";
close(MYSQL);
waitpid( $pid, 0 );
$dbpassword = Cpanel::MysqlUtils::getmydbpass('root'); # read from /root/.my.cnf
my $conection_ok = check_mysql_connection();
if ($conection_ok) {
print "Password Set!\n";
exit;
}
else {
$need_passreset = 'Access Denied';
print "Password could not be set.\n";
}
}
if ($need_passreset) {
if ( $dbhost && $dbhost ne 'localhost' && $dbhost ne '127.0.0.1' ) {
require Cpanel::Notify;
require Cpanel::Hostname;
my $hostname = Cpanel::Hostname::gethostname();
Cpanel::Notify::notification(
'application' => 'mysqlconnectioncheck',
'status' => $need_passreset,
'priority' => 1,
'interval' => 1,
'subject' => qq{[mysqlconnectioncheck] $need_passreset on $hostname},
'message' => qq{cPanel was unable to automatically reset the password for mysql server on $dbhost from the $hostname server.\n\n }
. ( $need_passreset =~ /no pass/i ? qq{ There is currently no password set in /root/.my.cnf.\n\n} : '' )
. qq{Please reset the password on $dbhost, or if you already know the root password for the mysql srever, please update the password in /root/.my.cnf on $hostname. }
. ( $myout ? qq{\n\n\nThe error from mysql was: $myout.} : '' )
);
exit 1;
}
my $newpass = get_mysql_password();
delete $ENV{'GATEWAY_INTERFACE'}; # important for getting the html free output
$ENV{'REQUEST_METHOD'} = 'GET';
$ENV{'QUERY_STRING'} = 'password=' . $newpass; # URI encoding not needed since generate_password will not generate any chars that need to be encoded
$ENV{'REMOTE_USER'} = 'root';
print 'Attempting to reset mysql password...because..' . $need_passreset . '...';
system( ( -x '/usr/local/cpanel/whostmgr/bin/whostmgr.pl' ? '/usr/local/cpanel/whostmgr/bin/whostmgr.pl' : '/usr/local/cpanel/whostmgr/bin/whostmgr' ), '--resetmysqlroot' );
print "Done\n";
}
sub get_mysql_password {
require Cpanel::PasswdStrength;
my $lengthcounter = 8;
if ( $dbpassword && $dbpassword =~ m/['"]/ ) {
undef $dbpassword;
}
my $newpass = ( $dbpassword || Cpanel::PasswdStrength::generate_password($lengthcounter) ); # keep the old password if we can
while ( $newpass =~ /['"]/ || !Cpanel::PasswdStrength::check_password_strength( 'pw' => $newpass, 'app' => 'mysql' ) ) {
$newpass = Cpanel::PasswdStrength::generate_password($lengthcounter);
next if ( $newpass =~ /['"]/ );
$lengthcounter++;
last if $lengthcounter > 128; # This will protect us from in infinite loop
}
return $newpass;
}
sub check_mysql_connection {
require Cpanel::PwCache;
my $status = 1;
$SIG{'PIPE'} = 'IGNORE';
$ENV{'HOME'} = ( Cpanel::PwCache::getpwnam('root') )[7]; # should be /root for .my.cnf
require IPC::Open3;
open( WNULL, '>', '/dev/null' );
my $pid = IPC::Open3::open3( \*MYSQL, ">&WNULL", \*MERR, $mysql );
print MYSQL "exit\n";
close(MYSQL);
while (<MERR>) {
$myout .= $_;
chomp;
if (m/Access\s+Denied/i) {
$status = 0;
last;
}
}
close(WNULL);
waitpid( $pid, 0 );
return $status;
}