#!/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);
}

# add ACLs at this point
sub
update_slapconf
{
	my ($ndx, $line, $new, $newsuffix, @arr, $tmp);

	$ndx = index($ldapsuffix, ",");
	$newsuffix = substr($ldapsuffix, $ndx + 1, 999);

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

	for ($ndx = 0; $ndx < scalar(@slapfile); $ndx++) {
		$line = $slapfile[$ndx];
		# fix suffix
		if ($line =~ /^suffix/) {
			$new = "suffix\t\t\"$newsuffix\"\n";
			if ($new ne $line) {
				print("Fixing suffix in $slapconf\n");
			}
			$slapfile[$ndx] = $new;
		}
		# fix rootdn
		if ($line =~ /^rootdn/) {
			$new = "rootdn\t\t\"cn=Manager,$newsuffix\"\n";
			if ($new ne $line) {
				print("Fixing rootdn in $slapconf\n");
			}
			$slapfile[$ndx] = $new;
		}
		# for password
		if ($line =~ /^rootpw/) {
			@arr = split("[ \t]+", $line, 2);
			$tmp = $arr[1];
			$tmp =~ s/"//g;
			# if x[digits]x or secret then fix it.

			$found = 0;
			if ($tmp =~ m/^x[0-9]*x$/) {
				$found = 1;
			}
			if ($tmp =~ m/^secret$/) {
				$found = 1;
			}
			if ($found == 0) {
				next;
			}

			$new = mkpwd();
			$new = `slappasswd -s $new`;
			chomp($new);
			$new = "rootpw\t\t$new\n";
			if ($new ne $line) {
				print("Fixing rootpw in $slapconf\n");
			}
			$slapfile[$ndx] = $new;
		}
	}

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

# coerce the msguser password in ldap to match the one in msg.conf.
# the msg.conf one is assumed to be correct.
sub
update_msguser
{
	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=msguser)",
		attrs => 'userPassword' );

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

	$userdn = $userentry->dn;
	$oldpassword = $userentry->get_value("userPassword");
	if ($oldpassword eq $msgpw) {
		return;
	}

	print("Fixing msguser password in ldap database\n");
	%attrpairs = ();
	$attrpairs{"userPassword"} = "$msgpw";
	$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();

update_slapconf();

slapd_in();

update_msguser();

slapd_out();
