cvsdist 9bfe3cb
#!/bin/bash
cvsdist cba1243
#
cvsdist cba1243
# ldap	This shell script takes care of starting and stopping
d5ef856
#	ldap servers (slapd).
cvsdist cba1243
#
6b99432
# chkconfig: - 27 73
cvsdist cba1243
# description: LDAP stands for Lightweight Directory Access Protocol, used \
cvsdist cba1243
#              for implementing the industry standard directory services.
cvsdist cba1243
# processname: slapd
cvsdist cba1243
# config: /etc/openldap/slapd.conf
fenlason d8aedf8
# pidfile: /var/run/openldap/slapd.pid
cvsdist cba1243
1447738
### BEGIN INIT INFO
1447738
# Provides: ldap
1447738
# Required-Start: $network $local_fs
1447738
# Required-Stop: $network $local_fs 
1447738
# Should-Start: 
1447738
# Should-Stop: 
1447738
# Default-Start: 
1447738
# Default-Stop: 
1447738
# Short-Description: starts and stopd OpenLDAP server daemon
1447738
# Description: LDAP stands for Lightweight Directory Access Protocol, used
1447738
#              for implementing the industry standard directory services.
1447738
### END INIT INFO
1447738
cvsdist cba1243
# Source function library.
cvsdist cba1243
. /etc/init.d/functions
cvsdist cba1243
ba2e462
# Define default values of options allowed in /etc/sysconfig/ldap
ba2e462
SLAPD_LDAP="yes"
ba2e462
SLAPD_LDAPI="no"
ba2e462
SLAPD_LDAPS="no"
4c8f60b
SLAPD_SHUTDOWN_TIMEOUT=3
d5ef856
# OPTIONS, SLAPD_OPTIONS and KTB5_KTNAME are not defined
ba2e462
ba2e462
# Source an auxiliary options file if we have one
cvsdist 9bfe3cb
if [ -r /etc/sysconfig/ldap ] ; then
cvsdist 9bfe3cb
	. /etc/sysconfig/ldap
cvsdist 9bfe3cb
fi
cvsdist cba1243
cvsdist cba1243
slapd=/usr/sbin/slapd
cvsdist 19aca62
slaptest=/usr/sbin/slaptest
1447738
lockfile=/var/lock/subsys/ldap
1447738
configfile=/etc/openldap/slapd.conf
001a81b
pidfile=/var/run/openldap/slapd.pid
cvsdist cba1243
cvsdist cba1243
RETVAL=0
cvsdist cba1243
a2323f6
#
a2323f6
# Pass commands given in $2 and later to "test" run as user given in $1.
a2323f6
#
a2323f6
function testasuser() {
a2323f6
    local user= cmd=
a2323f6
    user="$1"
a2323f6
    shift
a2323f6
    cmd="$@"
a2323f6
    if test x"$user" != x ; then
0a5bb3a
	if test x"$cmd" != x ; then
0a5bb3a
	    /sbin/runuser -f -m -s /bin/sh -c "test $cmd" -- "$user"
0a5bb3a
	else
0a5bb3a
	    false
0a5bb3a
	fi
a2323f6
    else
0a5bb3a
	false
a2323f6
    fi
a2323f6
}
a2323f6
a2323f6
#
a2323f6
# Check for read-access errors for the user given in $1 for a service named $2.
a2323f6
# If $3 is specified, the command is run if "klist" can't be found.
a2323f6
#
a2323f6
function checkkeytab() {
a2323f6
    local user= service= klist= default=
a2323f6
    user="$1"
a2323f6
    service="$2"
a2323f6
    default="${3:-false}"
a2323f6
    if test -x /usr/kerberos/bin/klist ; then
0a5bb3a
	klist=/usr/kerberos/bin/klist
a2323f6
    elif test -x /usr/bin/klist ; then
0a5bb3a
	klist=/usr/bin/klist
a2323f6
    fi
a2323f6
    KRB5_KTNAME="${KRB5_KTNAME:-/etc/krb5.keytab}"
b8c3dc4
    export KRB5_KTNAME
a2323f6
    if test -s "$KRB5_KTNAME" ; then
0a5bb3a
	if test x"$klist" != x ; then
4898dbd
	    if LANG=C $klist -k "$KRB5_KTNAME" | tail -n 4 | awk '{print $2}' | grep -q ^"$service"/ ; then
0a5bb3a
		if ! testasuser "$user" -r ${KRB5_KTNAME:-/etc/krb5.keytab} ; then
0a5bb3a
		    true
0a5bb3a
		else
0a5bb3a
		    false
0a5bb3a
		fi
0a5bb3a
	    else
0a5bb3a
		false
0a5bb3a
	    fi
0a5bb3a
	else
0a5bb3a
	    $default
0a5bb3a
	fi
a2323f6
    else
0a5bb3a
	false
a2323f6
    fi
a2323f6
}
a2323f6
a2323f6
function configtest() {
a2323f6
	local user= ldapuid= dbdir= file=
1447738
	[ -f $configfile ] || exit 6
0a5bb3a
	# Check for simple-but-common errors.
cvsdist f388181
	user=ldap
3ed0fcc
	prog=`basename ${slapd}`
cvsdist f388181
	ldapuid=`id -u $user`
cvsdist f388181
	# Unaccessible database files.
d5ef856
	slaptestflags=""
1447738
	for dbdir in `LANG=C egrep '^directory[[:space:]]+[[:print:]]+$' $configfile | sed s,^directory,,` ; do
e97219f
		for file in `find ${dbdir}/ -not -uid $ldapuid -and \( -name "*.dbb" -or -name "*.gdbm" -or -name "*.bdb" -or -name "__db.*" -or -name "log.*" -or -name alock \)` ; do
cvsdist f388181
			echo -n $"$file is not owned by \"$user\"" ; warning ; echo
cvsdist f388181
		done
704e3ce
		if ! test -s ${dbdir}/id2entry.dbb ; then
0a5bb3a
			if ! test -s ${dbdir}/id2entry.gdbm ; then
0a5bb3a
				if ! test -s ${dbdir}/id2entry.bdb ; then
d5ef856
					slaptestflags="-u"
704e3ce
				fi
704e3ce
			fi
704e3ce
		fi
cvsdist f388181
	done
a2323f6
	# Unaccessible keytab with an "ldap" key.
a2323f6
	if checkkeytab $user ldap ; then
a2323f6
		file=${KRB5_KTNAME:-/etc/krb5.keytab}
a2323f6
		echo -n $"$file is not readable by \"$user\"" ; warning ; echo
cvsdist 19aca62
	fi
a2323f6
	# Unaccessible TLS configuration files.
1447738
	tlsconfigs=`LANG=C egrep '^(TLSCACertificateFile|TLSCertificateFile|TLSCertificateKeyFile)[[:space:]]' $configfile | awk '{print $2}'`
a2323f6
	for file in $tlsconfigs ; do
a2323f6
		if ! testasuser $user -r $file ; then
a2323f6
			echo -n $"$file is not readable by \"$user\"" ; warning ; echo
a2323f6
		fi
a2323f6
	done
a2323f6
	# Check the configuration file.
3dbb0a0
	slaptestout=`/sbin/runuser -m -s "$slaptest" -- "$user" $slaptestflags 2>&1`
3dbb0a0
	slaptestexit=$?
808537b
#	slaptestout=`echo $slaptestout 2>/dev/null | grep -v "config file testing succeeded"`
3dbb0a0
	# print warning if slaptest passed but reports some problems
808537b
	if test $slaptestexit == 0 ; then
808537b
		if echo "$slaptestout" | grep -v "config file testing succeeded" >/dev/null ; then
808537b
			echo -n $"Checking configuration files for $prog: " ; warning ; echo
808537b
			echo "$slaptestout"
808537b
		fi
3dbb0a0
	fi
3dbb0a0
	# report error if configuration file is wrong
3dbb0a0
	if test $slaptestexit != 0 ; then
3dbb0a0
		echo -n $"Checking configuration files for $prog: " ; failure ; echo
3dbb0a0
		echo "$slaptestout"
fenlason 282cf62
		if /sbin/runuser -m -s "$slaptest" -- "$user" "-u" > /dev/null 2> /dev/null ; then
1447738
			dirs=`LANG=C egrep '^directory[[:space:]]+[[:print:]]+$' $configfile | awk '{print $2}'`
e466757
			for directory in $dirs ; do
e466757
				if test -r $directory/__db.001 ; then
e466757
					echo -n $"stale lock files may be present in $directory" ; warning ; echo
e466757
				fi
e466757
			done
e466757
		fi
1447738
		exit 6
e466757
	fi
a2323f6
}
a2323f6
a2323f6
function start() {
1447738
	[ -x $slapd ] || exit 5
a2323f6
	configtest
0a5bb3a
	# Define a couple of local variables which we'll need. Maybe.
a2323f6
	user=ldap
cvsdist 7a16c4a
	prog=`basename ${slapd}`
ba2e462
	harg=""
05dc6ea
	if test x$SLAPD_LDAP = xyes ; then
ba2e462
	    harg="ldap:///"
ba2e462
	fi
ba2e462
	if test x$SLAPD_LDAPS = xyes ; then
0a5bb3a
	    harg="$harg ldaps:///"
0a5bb3a
	fi
0a5bb3a
	if test x$SLAPD_LDAPI = xyes ; then
0a5bb3a
	    harg="$harg ldapi:///"
cvsdist 9bfe3cb
	fi
0a5bb3a
	# Start daemons.
0a5bb3a
	echo -n $"Starting $prog: "
001a81b
	daemon --pidfile=$pidfile --check=$prog ${slapd} -h "\"$harg\"" -u ${user} $OPTIONS $SLAPD_OPTIONS 
0a5bb3a
	RETVAL=$?
1447738
	[ $RETVAL -eq 0 ] && touch $lockfile
808537b
	echo
cvsdist cba1243
	return $RETVAL
cvsdist cba1243
}
cvsdist cba1243
cvsdist cba1243
function stop() {
0a5bb3a
	# Stop daemons.
cvsdist 7a16c4a
	prog=`basename ${slapd}`
cvsdist 7a16c4a
	echo -n $"Stopping $prog: "
4c8f60b
	killproc -p $pidfile -d $SLAPD_SHUTDOWN_TIMEOUT ${slapd}
cvsdist cba1243
	RETVAL=$?
1447738
	[ $RETVAL -eq 0 ] && rm -f $lockfile /var/run/slapd.args
808537b
	echo
cvsdist cba1243
	return $RETVAL
cvsdist cba1243
}
cvsdist cba1243
cvsdist cba1243
# See how we were called.
cvsdist cba1243
case "$1" in
a2323f6
    configtest)
0a5bb3a
	configtest
0a5bb3a
	;;
cvsdist cba1243
    start)
0a5bb3a
	start
233b801
	RETVAL=$?
0a5bb3a
	;;
cvsdist cba1243
    stop)
0a5bb3a
	stop
233b801
	RETVAL=$?
0a5bb3a
	;;
cvsdist cba1243
    status)
001a81b
	status -p $pidfile ${slapd}
233b801
	RETVAL=$?
cvsdist cba1243
	;;
1447738
    restart|force-reload)
cvsdist cba1243
	stop
cvsdist cba1243
	start
233b801
	RETVAL=$?
cvsdist cba1243
	;;
1447738
    condrestart|try-restart)
0a5bb3a
	if [ -f /var/lock/subsys/ldap ] ; then
0a5bb3a
	    stop
0a5bb3a
	    start
233b801
	    RETVAL=$?
0a5bb3a
	fi
cvsdist cba1243
	;;
cvsdist cba1243
    *)
1447738
	echo $"Usage: $0 {start|stop|restart|status|condrestart|configtest}"
1447738
	RETVAL=2
cvsdist cba1243
esac
cvsdist cba1243
cvsdist cba1243
exit $RETVAL