#!/usr/bin/perl

# fixup script for a few things that can get corrupted with In-Place-Upgrade
# generally if the suffix and domain are not the default ones
# things get messed up in slapd.conf
# we also correct for plaintext passwords, either x1234567890x or secret.

BEGIN {
	$newdir = "/opt/msg/bin";
	unshift (@INC, "$newdir");
}

use	msglib;

$slapconf = "$msgldapdir/slapd.conf";
$slapinit = "$msginitdir/ldap";
$imapconf = "/etc/imapd.conf";

$rootdn = "";
$rootpw = "";
$saverootpw;

# put our password into slapd.conf, save the old pwd,
# get the bind addr, start ldap
sub
slapd_in
{
	my ($ndx, $line, @arr);

	open(FD, "$slapconf") || die "Unable to read $slapconf";
	@slapfile = <FD>;
	close(FD);

	for ($ndx = 0; $ndx < scalar(@slapfile); $ndx++) {
		$line = $slapfile[$ndx];
		if ($line =~ /^rootdn[ \t]/) {
			@arr = split("[ \t]+", $line, 2);
			$rootdn = $arr[1];
			chomp($rootdn);
			$rootdn =~ s/"//g;
		}
		if ($line =~ /^rootpw[ \t]/) {
			$saverootpw = $line;
			$slapfile[$ndx] = "rootpw\t\t$rootpw\n";
		}
	}

	open(FD, ">$slapconf") || die "Unable to write $slapconf";
	foreach $line (@slapfile) {
		print FD $line;
	}
	close(FD);

	system("$slapinit stop > /dev/null 2>&1");
	sleep(2);
	system("killall -15 slapd >/dev/null 2>&1");
	sleep(2);
	system("killall -9 slapd >/dev/null 2>&1");
	sleep(2);
	unlink("/var/run/slapd.pid") if ( -e "/var/run/slapd.pid" );
	unlink("/var/run/slapd/slapd.pid") if ( -e "/var/run/slapd/slapd.pid" );
	system("$slapinit start > /dev/null 2>&1");
	sleep(2);
}

# reverse of slapd_in, stop slapd, remove ourself from slapd.conf.
sub
slapd_out
{
	my ($ndx, $line, @arr);

	open(FD, "$slapconf") || die "Unable to read $slapconf";
	@slapfile = <FD>;
	close(FD);

	for ($ndx = 0; $ndx < scalar(@slapfile); $ndx++) {
		$line = $slapfile[$ndx];
		if ($line =~ /^rootpw[ \t]/) {
			$slapfile[$ndx] = $saverootpw;
			last;
		}
	}

	open(FD, ">$slapconf") || die "Unable to write $slapconf";
	foreach $line (@slapfile) {
		print FD $line;
	}
	close(FD);

	system("$slapinit stop > /dev/null 2>&1");
	sleep(2);
	system("$slapinit start > /dev/null 2>&1");
	sleep(2);
}

# coerce the admin password in ldap to match the one in msg.conf.
sub
update_admin
{
	my ($result, $mesg, $userentry, $userdn, %attrpairs, $hashref);

	$ldap = Net::LDAP->new("localhost") || die "Unable to create LDAP object";
	$result = $ldap->bind("$rootdn", password => "$rootpw", version => 3);
	if ($result->code) {
		print getLDAPerrorString("bind", $result) . "\n";
		return;
	}

	$mesg = $ldap->search(
		base => "$ldapsuffix",
		scope => "sub",
		filter => "(uid=admin)",
		attrs => 'userPassword' );

	$userentry = $mesg->entry(0);
	if (!$userentry) {
		$ldap->unbind;
		print(STDERR "admin entry not found\n");
		return;
	}

	$userdn = $userentry->dn;

	print("Fixing admin password in ldap database\n");
	%attrpairs = ();
	$attrpairs{"userPassword"} = "admin";
	$hashref = \%attrpairs;
	$mesg = $ldap->modify($userdn, replace => $hashref);
	if ($mesg->code) {
		$ldap->unbind;
		print getLDAPerrorString("modify", $mesg) . "\n";
		return;
	}

	$ldap->unbind;
}

# MAIN Main main

parse_conf noauth;

$rootpw = mkpwd();

slapd_in();

update_admin();

slapd_out();
