adf540
#!/bin/sh
adf540
# Author: Jan Vcelak <jvcelak@redhat.com>
adf540
adf540
. /usr/libexec/openldap/functions
adf540
adf540
function help()
adf540
{
adf540
	error "usage: %s [-f config-file] [-F config-dir]\n" "`basename $0`"
adf540
	exit 2
adf540
}
adf540
adf540
load_sysconfig
adf540
adf540
while getopts :f:F: opt; do
adf540
	case "$opt" in
adf540
	f)
adf540
		SLAPD_CONFIG_FILE="$OPTARG"
adf540
		;;
adf540
	F)
adf540
		SLAPD_CONFIG_DIR="$OPTARG"
adf540
		;;
adf540
	*)
adf540
		help
adf540
		;;
adf540
	esac
adf540
done
adf540
shift $((OPTIND-1))
adf540
[ -n "$1" ] && help
adf540
adf540
# check source, target
adf540
adf540
if [ ! -f "$SLAPD_CONFIG_FILE" ]; then
adf540
	error "Source configuration file '%s' not found." "$SLAPD_CONFIG_FILE"
adf540
	exit 1
adf540
fi
adf540
adf540
if grep -iq '^dn: cn=config$' "$SLAPD_CONFIG_FILE"; then
adf540
	SLAPD_CONFIG_FILE_FORMAT=ldif
adf540
else
adf540
	SLAPD_CONFIG_FILE_FORMAT=conf
adf540
fi
adf540
adf540
if [ -d "$SLAPD_CONFIG_DIR" ]; then
adf540
	if [ `find "$SLAPD_CONFIG_DIR" -maxdepth 0 -empty | wc -l` -eq 0 ]; then
adf540
		error "Target configuration directory '%s' is not empty." "$SLAPD_CONFIG_DIR"
adf540
		exit 1
adf540
	fi
adf540
fi
adf540
adf540
# perform the conversion
adf540
adf540
tmp_convert=`mktemp --tmpdir=/var/run/openldap`
adf540
adf540
if [ `id -u` -eq 0 ]; then
adf540
	install -d --owner $SLAPD_USER --group `id -g $SLAPD_USER` --mode 0750 "$SLAPD_CONFIG_DIR" &>>$tmp_convert
adf540
	if [ $SLAPD_CONFIG_FILE_FORMAT = ldif ]; then
adf540
		run_as_ldap "/usr/sbin/slapadd -F \"$SLAPD_CONFIG_DIR\" -n 0 -l \"$SLAPD_CONFIG_FILE\"" &>>$tmp_convert
adf540
	else
adf540
		run_as_ldap "/usr/sbin/slaptest -f \"$SLAPD_CONFIG_FILE\" -F \"$SLAPD_CONFIG_DIR\"" &>>$tmp_convert
adf540
	fi
adf540
	retcode=$?
adf540
else
adf540
	error "You are not root! Permission will not be set."
adf540
	install -d --mode 0750 "$SLAPD_CONFIG_DIR" &>>$tmp_convert
adf540
	if [ $SLAPD_CONFIG_FILE_FORMAT = ldif ]; then
adf540
		/usr/sbin/slapadd -F "$SLAPD_CONFIG_DIR" -n 0 -l "$SLAPD_CONFIG_FILE" &>>$tmp_convert
adf540
	else
adf540
		/usr/sbin/slaptest -f "$SLAPD_CONFIG_FILE" -F "$SLAPD_CONFIG_DIR" &>>$tmp_convert
adf540
	fi
adf540
	retcode=$?
adf540
fi
adf540
adf540
if [ $retcode -ne 0 ]; then
adf540
	error "Configuration conversion failed:"
adf540
	cat $tmp_convert >&2
adf540
fi
adf540
adf540
rm $tmp_convert
adf540
exit $retcode