Edit File: suspendmysqlusers
#!/usr/local/cpanel/3rdparty/bin/perl # cpanel - suspendmysqlusers Copyright 2011 cPanel, Inc. # All rights Reserved. # copyright@cpanel.net http://cpanel.net # This code is subject to the cPanel license. Unauthorized copying is prohibited package scripts::suspendmysqlusers; use strict; use warnings; use Cpanel::DB::Map::Reader (); use Cpanel::DB::Utils (); use Cpanel::MysqlUtils (); use Cpanel::Logger (); use Cpanel::MysqlUtils::Connect (); if ( !caller() ) { my $user = shift @ARGV; if ( !$user ) { print "USAGE: $0 <user>\n"; exit 1; } suspend($user); } sub suspend { my ($user) = @_; my $logger = Cpanel::Logger->new(); my $map = Cpanel::DB::Map::Reader->new( 'cpuser' => $user, engine => 'mysql' ); my @user_list = $map->get_dbusers_plus_cpses(); # Case48428 - Need to include the parent user associated with the database and not just virtual users # when suspending accounts in MySQL. # Also, the DB map datastore might have a different owner name associated with the user # account which the databases are tied to. my @db_owner = ( $user, Cpanel::DB::Utils::username_to_dbowner($user), ); push @user_list, @db_owner; my $user_list = join ',', map { Cpanel::MysqlUtils::quote($_) } @user_list; Cpanel::MysqlUtils::Connect::connect(); my $result = Cpanel::MysqlUtils::sqlcmd("SELECT User, Host, Password from mysql.user WHERE user IN ($user_list)") || ''; my @lines = split /\n/, $result; my %result; foreach my $line (@lines) { my ( $user, $host, $pass ) = split /\s+/, $line, 3; if ( !$pass ) { $logger->info("MySQL user $user\@$host has a blank password!"); $result{$user}{$host} = '*' x 41; } else { $result{$user}{$host} = $pass; } } foreach my $user ( keys %result ) { foreach my $host ( keys %{ $result{$user} } ) { if ( $result{$user}{$host} =~ m/^\*/ ) { $result{$user}{$host} =~ s/^\*//; $result{$user}{$host} = reverse $result{$user}{$host}; $result{$user}{$host} = '-' . $result{$user}{$host}; } else { # Case 76857 $result{$user}{$host} =~ s/^([0-9a-f]{16})$/ '!' . reverse($1) /e; } } } my @sql_cmds; foreach my $user ( keys %result ) { foreach my $host ( keys %{ $result{$user} } ) { my $pass = $result{$user}{$host}; my $dbuser = Cpanel::MysqlUtils::quote($user); my $dbhost = Cpanel::MysqlUtils::quote($host); $pass = Cpanel::MysqlUtils::quote($pass); push @sql_cmds, "UPDATE mysql.user SET Password=$pass WHERE user=$dbuser AND host=$dbhost"; } } Cpanel::MysqlUtils::sqlcmd( \@sql_cmds ) if @sql_cmds; # This must be done in the foreground or pkgacct will break. Cpanel::MysqlUtils::sqlcmd('FLUSH PRIVILEGES'); return 1; } 1;