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