#!/usr/bin/perl
#
# Tool to set a new admin password in LDAP.  Written by Torstein
# Svendsen.
#
# If the LDAP admin password is lost, use rootdn and rootpw in
# /etc/ldap/slapd.conf to get an override password able to set a new
# password.
#
# Check out ldappasswd from ldap-utils to see if it can replace this
# program.

use strict;
use warnings;
use Net::LDAP;

use vars qw($dn $password $md4password $ldaphost);

$ldaphost    = 'ldap';
$dn          = 'cn=admin,ou=ldap-access,dc=skole,dc=skolelinux,dc=no';
$password    = 'secret'; # Current passord in clear text
$md5password = '$1$ubzu8/wx$KuUO3OtpjbXu74d67vqyI/'; # new password hash

my $ldap = new Net::LDAP($ldaphost);

my $mesg = $ldap->bind(
                       'dn' => $dn,
                       'password' => $password
                       );
            
die("Wrong password!") unless ( 0 == $mesg->code );

my $md5 = '{crypt}'. $md5password;

$ldap->modify(
              $dn,
              'replace' => {
                  'userPassword' => $md5,
              }
              );
