File: //scripts.20110531.215904.25158/mysqlpasswd
#!/usr/bin/perl
# cpanel - mysqlpasswd 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 Cpanel::AccessIds::SetUids ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::MysqlUtils ();
use Cpanel::MysqlUtils::MyCnf ();
use Cpanel::SafeRun ();
use Cpanel::SafeRun::Full ();
if ( $> != 0 ) {
die "Setting MySQL passwords is only possible using the root account\n";
}
my $mysqlbin;
if ( -x "/usr/local/bin/mysql" ) {
$mysqlbin = '/usr/local/bin/mysql';
}
if ( -x "/usr/bin/mysql" ) {
$mysqlbin = '/usr/bin/mysql';
}
if ( !$mysqlbin ) {
die "Unable to locate a suitable mysql client binary!\n";
}
alarm(15);
my ( $user, $pass, $userhost );
if (grep(/--multistdin/,@ARGV)) {
$user = <STDIN>;
chomp($user);
$pass = <STDIN>;
chomp($pass);
$userhost = <STDIN>;
chomp($userhost);
} elsif ( !@ARGV ) {
chomp( my $up = <STDIN> );
my @UP = split( / /, $up );
$user = $UP[0];
$pass = $UP[1];
$userhost = $UP[2];
}
else {
$user = $ARGV[0];
$pass = $ARGV[1];
$userhost = $ARGV[2];
}
alarm(0);
if ( !$user ) { print STDERR "$0: user is blank\n"; exit 1; }
if ( !$pass ) { print STDERR "$0: pass is blank\n"; exit 1; }
my $safepass = Cpanel::MysqlUtils::safesqlstring($pass);
my $cpuser = $user;
if ($user ne 'root') {
my %DBOWNERS;
Cpanel::Config::LoadConfig::loadConfig( '/etc/dbowners', \%DBOWNERS, ': ' );
%DBOWNERS = reverse %DBOWNERS;
$cpuser = $DBOWNERS{$user};
}
open( my $mysql_fh, '|-' ) || exec( $mysqlbin, '-u', 'root', 'mysql' );
if ( !$userhost ) {
print $mysql_fh "UPDATE user SET Password=PASSWORD('$safepass') WHERE user='$user';\n";
}
else {
print $mysql_fh "UPDATE user SET Password=PASSWORD('$safepass') WHERE user='$user' AND host='$userhost';\n";
}
print $mysql_fh "FLUSH PRIVILEGES;\n";
close($mysql_fh);
my ( $uid, $gid, $homedir ) = ( getpwnam($cpuser) )[ 2, 3, 7 ];
if ( $user eq $cpuser ) {
my $host = Cpanel::MysqlUtils::getmydbhost($cpuser);
my @remote_hosts = split( /\n/, Cpanel::SafeRun::saferun( '/usr/local/cpanel/bin/mysqladmin', $uid, 'LISTHOSTS' ) );
if ( !$host || $host eq 'localhost' || grep { $_ eq $host } @remote_hosts ) {
if ($homedir) {
my $cpconf_ref = Cpanel::Config::LoadCpConf::loadcpconf();
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
}
else {
if ( $uid > 0 ) {
Cpanel::AccessIds::SetUids::setuids($cpuser);
}
# Write ~/.my.cnf
if ($host) {
Cpanel::MysqlUtils::MyCnf::update_mycnf(
user => $cpuser,
items => [
{
user => $cpuser,
pass => $pass,
host => $host,
}
],
);
}
else {
Cpanel::MysqlUtils::MyCnf::update_mycnf(
user => $cpuser,
items => [
{
user => $cpuser,
pass => $pass,
}
],
);
}
# Check for relocated root. Why, oh why change root's homedir???
if ( $user eq 'root' ) {
if ( !-e '/root' ) {
mkdir '/root', 0700;
}
if ( $homedir ne '/root' && -d '/root' ) {
warn "Detected relocated /root directory ($homedir)\n";
system 'cp', '-f', $homedir . '/.my.cnf', '/root/.my.cnf';
}
}
exit;
}
}
}
}
queue_dbstoregrants($cpuser);
sub queue_dbstoregrants {
my $cpuser = shift;
Cpanel::SafeRun::Full::run(
'program' => '/usr/local/cpanel/bin/servers_queue',
'args' => [ '--plugin=MysqlTasks', 'queue', "dbstoregrants $cpuser" ],
);
}