#!/bin/sh
### BEGIN INIT INFO
# Provides:          slapd
# Required-Start:    $remote_fs $network $syslog
# Required-Stop:     $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OpenLDAP standalone server (Lightweight Directory Access Protocol)
### END INIT INFO

# Specify path variable
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Kill me on all errors
set -e

# Set the paths to slapd as a variable so that someone who really
# wants to can override the path in /etc/default/slapd.
SLAPD=/usr/sbin/slapd

# Stop processing if slapd is not there
[ -x $SLAPD ] || exit 0

# debconf may have this file descriptor open and it makes things work a bit
# more reliably if we redirect it as a matter of course.  db_stop will take
# care of this, but this won't hurt.
exec 3>/dev/null

# Source the init script configuration
if [ -f "/etc/default/slapd" ]; then
	. /etc/default/slapd
fi

# Load the default location of the slapd config file
if [ -z "$SLAPD_CONF" ]; then
	SLAPD_CONF="/etc/ldap/slapd.d/"
fi

# Stop processing if the config file is not there
if [ ! -r "$SLAPD_CONF" ]; then
  cat <<EOF >&2
No configuration directory was found for slapd at $SLAPD_CONF.
If you have moved the slapd configuration directory please modify
/etc/default/slapd to reflect this.  If you chose to not
configure slapd during installation then you need to do so
prior to attempting to start slapd.
EOF
  exit 0 # Should this be 1?
fi

# Find out the name of slapd's pid file
if [ -z "$SLAPD_PIDFILE" ]; then
	# If using old one-file configuration scheme
	if [ -f "$SLAPD_CONF" ] ; then
		SLAPD_PIDFILE=`sed -ne 's/^pidfile[[:space:]]\+\(.\+\)/\1/p' \
			"$SLAPD_CONF"`
	# Else, if using new directory configuration scheme
	elif [ -d "$SLAPD_CONF" ] ; then
		SLAPD_PIDFILE=`sed -ne \
			's/^olcPidFile:[[:space:]]\+\(.\+\)[[:space:]]*/\1/p' \
			"$SLAPD_CONF"/'cn=config.ldif'`
	fi
fi

# Set the correct config option according to the configuration backend.
if [ -d "$SLAPD_CONF" ] ; then
	SLAPD_OPTIONS="-F $SLAPD_CONF $SLAPD_OPTIONS"
else
	SLAPD_OPTIONS="-f $SLAPD_CONF $SLAPD_OPTIONS"
fi

# XXX: Breaks upgrading if there is no pidfile (invoke-rc.d stop will fail)
# -- Torsten
if [ -z "$SLAPD_PIDFILE" ]; then
	cat <<EOF >&2
The pidfile for slapd is neither specified in "$SLAPD_CONF" nor
in /etc/default/slapd. Consequently, slapd will not be started.
EOF
	exit 1
fi

# Make sure the pidfile directory exists with correct permissions
piddir=`dirname "$SLAPD_PIDFILE"`
if [ ! -d "$piddir" ]; then
	mkdir -p "$piddir"
	[ -z "$SLAPD_USER" ] || chown -R "$SLAPD_USER" "$piddir"
	[ -z "$SLAPD_GROUP" ] || chgrp -R "$SLAPD_GROUP" "$piddir"
fi

# Pass the user and group to run under to slapd
if [ "$SLAPD_USER" ]; then
	SLAPD_OPTIONS="-u $SLAPD_USER $SLAPD_OPTIONS"
fi

if [ "$SLAPD_GROUP" ]; then
	SLAPD_OPTIONS="-g $SLAPD_GROUP $SLAPD_OPTIONS"
fi

# Check whether we were configured to not start the services.
check_for_no_start() {
	if [ -n "$SLAPD_NO_START" ]; then
		echo 'Not starting slapd: SLAPD_NO_START set in /etc/default/slapd' >&2
		exit 0
	fi
	if [ -n "$SLAPD_SENTINEL_FILE" ] && [ -e "$SLAPD_SENTINEL_FILE" ]; then
		echo "Not starting slapd: $SLAPD_SENTINEL_FILE exists" >&2
		exit 0
	fi
}

# Tell the user that something went wrong and give some hints for
# resolving the problem.
report_failure() {
	if [ -n "$reason" ]; then
		echo " - failed: "
		echo "$reason"
	else
		echo " - failed."
		cat <<EOF
The operation failed but no output was produced. For hints on what went
wrong please refer to the system's logfiles (e.g. /var/log/syslog) or
try running the daemon in Debug mode like via "slapd -d 16383" (warning:
this will create copious output).
EOF

		if [ -n "$SLAPD_OPTIONS" -o \
		     -n "$SLAPD_SERVICES" ]; then
			cat << EOF

Below, you can find the command line options used by this script to 
run slapd. Do not forget to specify those options if you
want to look to debugging output:
EOF
	                if [ -z "$SLAPD_SERVICES" ]; then
				if [ -n "$SLAPD_OPTIONS" ]; then
					echo "  slapd $SLAPD_OPTIONS"
				fi
                	else
                        	echo "  slapd -h '$SLAPD_SERVICES' $SLAPD_OPTIONS"
                	fi
		fi
	fi
}

# Start the slapd daemon and capture the error message if any to 
# $reason.
start_slapd() {
	echo -n " slapd"
	if [ -z "$SLAPD_SERVICES" ]; then
		reason="`start-stop-daemon --start --quiet --oknodo \
			--pidfile "$SLAPD_PIDFILE" \
			--exec $SLAPD -- $SLAPD_OPTIONS 2>&1`"
	else
		reason="`start-stop-daemon --start --quiet --oknodo \
			--pidfile "$SLAPD_PIDFILE" \
			--exec $SLAPD -- -h "$SLAPD_SERVICES" $SLAPD_OPTIONS 2>&1`"
	fi

	# Backward compatibility with OpenLDAP 2.1 client libraries.
	if [ ! -h /var/run/ldapi ] && [ ! -e /var/run/ldapi ] ; then
		ln -s slapd/ldapi /var/run/ldapi
	fi
}

# Stop the slapd daemon and capture the error message (if any) to
# $reason.
stop_slapd() {
	echo -n " slapd"
	reason="`start-stop-daemon --stop --quiet --oknodo --retry TERM/10 \
		--pidfile "$SLAPD_PIDFILE" \
		--exec $SLAPD 2>&1`"
}

# Start the OpenLDAP daemons
start_ldap() {
	echo -n "Starting OpenLDAP:"
	trap 'report_failure' 0
	start_slapd
	trap "-" 0
	echo .
}

# Stop the OpenLDAP daemons
stop_ldap() {
	echo -n "Stopping OpenLDAP:"
	trap 'report_failure' 0
	stop_slapd
	trap "-" 0
	echo .
}

# Check OpenLDAP status
status_slapd() {
	# No PID
	if [ ! -f $SLAPD_PIDFILE ]; then
		echo "OpenLDAP is NOT running"
		exit 3
	fi

	# Bogus PID
	for pid in $(cat $SLAPD_PIDFILE) ; do
		if ! ps --noheaders p "$pid" | grep $SLAPD > /dev/null ; then
			echo "OpenLDAP is NOT running, bogus pidfile found: '$SLAPD_PIDFILE'"
			exit 1
		fi
	done

	# Running PID
	echo "OpenLDAP is running"
	exit 0
}

case "$1" in
  start)
	check_for_no_start
  	start_ldap ;;
  stop)
  	stop_ldap ;;
  restart|force-reload)
	check_for_no_start
  	stop_ldap
	start_ldap
	;;
  status)
	status_slapd ;;
  *)
  	echo "Usage: $0 {start|stop|restart|force-reload|status}"
	exit 1
	;;
esac
