e4ff3b
# Author: Jan Vcelak <jvcelak@redhat.com>
e4ff3b
e4ff3b
SLAPD_USER=
e4ff3b
SLAPD_CONFIG_FILE=
e4ff3b
SLAPD_CONFIG_DIR=
e4ff3b
SLAPD_CONFIG_CUSTOM=
e4ff3b
SLAPD_GLOBAL_OPTIONS=
e4ff3b
SLAPD_SYSCONFIG_FILE=
e4ff3b
e4ff3b
function default_config()
e4ff3b
{
e4ff3b
	SLAPD_USER=ldap
e4ff3b
	SLAPD_CONFIG_FILE=/etc/openldap/slapd.conf
e4ff3b
	SLAPD_CONFIG_DIR=/etc/openldap/slapd.d
e4ff3b
	SLAPD_CONFIG_CUSTOM=
e4ff3b
	SLAPD_GLOBAL_OPTIONS=
e4ff3b
	SLAPD_SYSCONFIG_FILE=/etc/sysconfig/slapd
e4ff3b
}
e4ff3b
e4ff3b
function parse_config_options()
e4ff3b
{
e4ff3b
	user=
e4ff3b
	config_file=
e4ff3b
	config_dir=
e4ff3b
	while getopts :u:f:F: opt; do
e4ff3b
		case "$opt" in
e4ff3b
		u)
e4ff3b
			user="$OPTARG"
e4ff3b
			;;
e4ff3b
		f)
e4ff3b
			config_file="$OPTARG"
e4ff3b
			;;
e4ff3b
		F)
e4ff3b
			config_dir="$OPTARG"
e4ff3b
			;;
e4ff3b
		esac
e4ff3b
	done
e4ff3b
e4ff3b
	if [ -n "$user" ]; then
e4ff3b
		SLAPD_USER="$user"
e4ff3b
	fi
e4ff3b
e4ff3b
	if [ -n "$config_dir" ]; then
e4ff3b
		SLAPD_CONFIG_DIR="$config_dir"
e4ff3b
		SLAPD_CONFIG_FILE=
e4ff3b
		SLAPD_CONFIG_CUSTOM=1
e4ff3b
		SLAPD_GLOBAL_OPTIONS="-F '$config_dir'"
e4ff3b
	elif [ -n "$config_file" ]; then
e4ff3b
		SLAPD_CONFIG_DIR=
e4ff3b
		SLAPD_CONFIG_FILE="$config_file"
e4ff3b
		SLAPD_CONFIG_CUSTOM=1
e4ff3b
		SLAPD_GLOBAL_OPTIONS="-f '$config_file'"
e4ff3b
	fi
e4ff3b
}
e4ff3b
e4ff3b
function uses_new_config()
e4ff3b
{
e4ff3b
	[ -n "$SLAPD_CONFIG_DIR" ]
e4ff3b
	return $?
e4ff3b
}
e4ff3b
e4ff3b
function run_as_ldap()
e4ff3b
{
e4ff3b
	/sbin/runuser --shell /bin/sh --session-command "$1" "$SLAPD_USER"
e4ff3b
	return $?
e4ff3b
}
e4ff3b
e4ff3b
function ldif_unbreak()
e4ff3b
{
e4ff3b
	sed ':a;N;s/\n //;ta;P;D'
e4ff3b
}
e4ff3b
e4ff3b
function ldif_value()
e4ff3b
{
e4ff3b
	sed 's/^[^:]*: //'
e4ff3b
}
e4ff3b
e4ff3b
function databases_new()
e4ff3b
{
e4ff3b
	slapcat $SLAPD_GLOBAL_OPTIONS -c \
e4ff3b
	-H 'ldap:///cn=config???(|(objectClass=olcBdbConfig)(objectClass=olcHdbConfig))' 2>/dev/null | \
e4ff3b
		ldif_unbreak | \
e4ff3b
		grep '^olcDbDirectory: ' | \
e4ff3b
		ldif_value
e4ff3b
}
e4ff3b
e4ff3b
function certificates_new()
e4ff3b
{
e4ff3b
	slapcat $SLAPD_GLOBAL_OPTIONS -c -H 'ldap:///cn=config???(cn=config)' 2>/dev/null | \
e4ff3b
		ldif_unbreak | \
e4ff3b
		grep '^olcTLS\(CACertificateFile\|CACertificatePath\|CertificateFile\|CertificateKeyFile\): ' | \
e4ff3b
		ldif_value
e4ff3b
}
e4ff3b
e4ff3b
function certificates()
e4ff3b
{
976c7e
	uses_new_config && certificates_new
e4ff3b
}
e4ff3b
e4ff3b
function databases()
e4ff3b
{
976c7e
	uses_new_config && databases_new
e4ff3b
}
e4ff3b
e4ff3b
e4ff3b
function error()
e4ff3b
{
e4ff3b
	format="$1\n"; shift
e4ff3b
	printf "$format" $@ >&2
e4ff3b
}
e4ff3b
e4ff3b
function load_sysconfig()
e4ff3b
{
e4ff3b
	[ -r "$SLAPD_SYSCONFIG_FILE" ] || return
e4ff3b
e4ff3b
	. "$SLAPD_SYSCONFIG_FILE"
e4ff3b
	[ -n "$SLAPD_OPTIONS" ] && parse_config_options $SLAPD_OPTIONS
e4ff3b
}
e4ff3b
e4ff3b
default_config