#!/bin/sh
#
# slapd	   This shell script takes care of starting and stopping
#          OpenLDAP server (slapd).
#
# chkconfig: 345 39 61
#
# description:	LDAP stands for Lightweight Directory Access Protocol, used
#               for implementing the industry standard directory services.
#
# processname: slapd
# config: /etc/openldap/
# pidfile: /var/run/slapd.pid
#
# Version 3.3.2 for OpenLDAP 2.2.22
# (modify by vserge For ALT Linux Team Sisyphus)
#
# From version of this script 3.1.0 SLURPD removed to different script
#

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

. cert-sh-functions

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network

# Source an auxiliary options file if we have one, and pick up OPTIONS,
# SLAPD_OPTIONS.
SourceIfNotEmpty /etc/sysconfig/ldap

if [ -f /etc/openldap/ldap.keytab ] ; then
	export KRB5_KTNAME=/etc/openldap/ldap.keytab
fi

DAEMON=/usr/sbin/slapd
DAEMONTEST=/usr/sbin/slaptest
ROOT=/var/lib/ldap
CONFIG=/etc/openldap/slapd.conf
LOCKFILE=/var/lock/subsys/slapd
PIDFILE=$ROOT/var/run/slapd.pid
RETVAL=0

do_config_sanity_check()
{
	action $"Checking slapd configuration" $DAEMONTEST -u || exit
}

adjust()
{
	ssl_generate "slapd"
	action $"Adjusting environment for slapd:" /etc/chroot.d/ldap.all || exit
}

start()
{
	is_yes "$NETWORKING" || return 0
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user ldap -- $DAEMON -u ldap -r $ROOT -h "$SLAPDURLLIST" "$SLAPD_OPTIONS"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user ldap $DAEMON
	RETVAL=$?
	return $RETVAL
}

restart()
{
        do_config_sanity_check
        stop
		sleep 3
        start
}
			

# See how we were called.
case "$1" in
	start)
		adjust
		start
		;;
	stop)
		stop
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user ldap $DAEMON
		RETVAL=$?
		;;
	restart)
		adjust
		restart
		;;
	check)
		do_config_sanity_check
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			adjust
			restart
		fi
		;;
	*)
		msg_usage "${0##*/} {start|stop|status|check|restart|condstop|condrestart}"
		RETVAL=1
esac

exit $RETVAL

