martin: server/kolabd/kolabd kolabpasswd,NONE,1.1
cvs at intevation.de
cvs at intevation.de
Sun Dec 12 21:36:29 CET 2004
Author: martin
Update of /kolabrepository/server/kolabd/kolabd
In directory doto:/tmp/cvs-serv1296
Added Files:
kolabpasswd
Log Message:
Martin K.: Added new command kolabpasswd which allows to change the manager password
--- NEW FILE: kolabpasswd ---
#!/kolab/bin/perl -w
use Term::ReadKey;
use IO::File;
use File::Temp;
use Net::LDAP;
use Kolab::Util;
my $kolab_prefix = (getpwnam('kolab'))[7] || die "Error: could not determine the kolab directory prefix (e.g. /kolab)";
# Hash a password
sub hashPassword {
my $pw = shift;
my $hashcmd = $kolab_prefix."/sbin/slappasswd -s '".$pw."'";
(my $hashpw = `$hashcmd`) or die $@;
chomp($hashpw);
return $hashpw;
}
# open old kolab master config file
my $kolabconfname = $kolab_prefix."/etc/kolab/kolab.conf";
# read old config data
my %config = readConfig($kolabconfname);
my $kolabconf = IO::File->new($kolab_prefix.'/etc/kolab/kolab.conf','r')
|| die "kolabpasswd: Fatal Error: could not open kolab config at $kolabconfname";
print "Changing password for manager";
# open ldap connection and verify old manager password
my $ldap = Net::LDAP->new( $config{'ldap_uri'})
|| die "\nkolabpasswd: Fatal Error: could not connect to LDAP Server";
do {
print "\nOld Password: ";
ReadMode 'noecho';
my $old_password = ReadLine 0; chomp $old_password;
$mesg = $ldap->bind( $config{'bind_dn'}, password => $old_password ) || die "\nkolabpasswd: Failed to bind to LDAP server";
} while ( $mesg->code );
# read in new manager password
print "\nNew Password: ";
ReadMode 'noecho';
my $new_password = ReadLine 0; chomp $new_password;
print "\nRe-enter New Password: ";
my $new_password2 = ReadLine 0; chomp $new_password2;
print "\n";
ReadMode 'normal';
($new_password eq $new_password2) || die "Sorry, passwords do not match.\n";
# create temporary config file
my $tmp = new File::Temp( TEMPLATE => 'tempXXXXX', DIR => $kolab_prefix.'/etc/kolab', UNLINK => 0, SUFFIX => '.conf')
|| die "Error: could not create temporary file under ".$kolab_prefix."/etc/kolab";
$tmpfilename = $tmp->filename;
# copy and replace old config to temporary file
foreach ($kolabconf->getlines()) {
if (/^(bind_pw\s:\s).*$/) {
print $tmp $1.$new_password."\n";
} else {
if (/^(bind_pw_hash\s:\s).*$/) {
my $bind_pw_hash = hashPassword($new_password);
print $tmp $1.$bind_pw_hash."\n";
} else {
print $tmp $_;
}
}
}
undef $tmp;
undef $kolabconf;
# open ldap connection and update manager password
$ldap = Net::LDAP->new( $config{'ldap_uri'})
|| die "Error: could not connect LDAP Server";
$ldap->bind( $config{'bind_dn'}, password => $config{'bind_pw'} )
|| die "Error: Failed to bind as manager to LDAP Server";
$ldap->modify($config{'bind_dn'}, replace => {'userPassword' => $new_password } )
|| die "Error: could not update LDAP with new manager password";
$ldap->unbind;
undef $ldap;
# move temporary file to kolab master config
rename($tmpfilename,$kolabconfname) || die "Error: could not install new $kolabconfname";
print "Password changed successfully, please be patient...\n";
# trigger kolabd to run update
system($kolab_prefix."/sbin/kolabconf > /dev/null 2>&1");
exit 0;
More information about the commits
mailing list