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