#!/usr/local/groundwork/perl/bin/perl -w # Version 1.0 # H Kriel for GroundWork Open Source # perl program to extract a list of value pairs from the 'mysql.user' table # present them to the operator with opportunity to enter a new password # which is then written to the user table # the root password for mysql acesss is required and will be requested from the operator and not seen in history use DBI; my $rootpass; sub getUsers { my $dbh = DBI->connect('dbi:mysql:mysql', 'root', $rootpass); my $sql = "select User, Host, Password from mysql.user;;"; my $sth = $dbh->prepare($sql); my $setsql = ''; my $sthsql = ''; my ($username, $hostname, $passhash); $sth->execute(); $sth->bind_col(1, \$username); $sth->bind_col(2, \$hostname); $sth->bind_col(3, \$passhash); while ($sth->fetch) { # retrieve one row if ($username eq "root") { print "WARNING root password\n"; } print "$username, $hostname\n"; $newpass = Ask_About ('new password?', $passhash); if ($newpass eq $passhash) { print "no change requested\n"; } else { print "setting password for $username on $hostname to $newpass\n"; $setsql = "set password for '".$username."'\@'".$hostname."' = password('".$newpass."');;"; $sthsql = $dbh->prepare($setsql); if ( ! $sthsql->execute() ) { print "Error resetting password $dbh->errstr\n"; exit; } } } } sub Ask_About(@) { my ($lbl,$valu) = @_; print $lbl." <".$valu."> ? " ; my $inp = ; chomp $inp ; $valu = $inp unless ($inp eq "") ; #print $lbl.": ".$valu."\n\n" ; return $valu ; } print "Utility to reset user passwords in MySQL. Only non root passwords will be eligible\n"; $rootpass = Ask_About ('root pass?',''); getUsers; exit;