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