mhonek / rpms / openldap

Forked from rpms/openldap 3 years ago
Clone
cvsdist 9bfe3cb
#!/bin/bash
cvsdist cba1243
#
cvsdist cba1243
# ldap	This shell script takes care of starting and stopping
cvsdist cba1243
#	ldap servers (slapd and slurpd).
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
cvsdist cba1243
# Source function library.
cvsdist cba1243
. /etc/init.d/functions
cvsdist cba1243
cvsdist cba1243
# Source networking configuration and check that networking is up.
cvsdist cba1243
if [ -r /etc/sysconfig/network ] ; then
cvsdist cba1243
	. /etc/sysconfig/network
233b801
	[ ${NETWORKING} = "no" ] && exit 1
cvsdist cba1243
fi
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"
ba2e462
# OPTIONS, SLAPD_OPTIONS, SLURPD_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 cba1243
slurpd=/usr/sbin/slurpd
cvsdist 19aca62
slaptest=/usr/sbin/slaptest
233b801
[ -x ${slapd} ] || exit 1
233b801
[ -x ${slurpd} ] || exit 1
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=
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.
704e3ce
	slaptestflags=
704e3ce
	for dbdir in `LANG=C egrep '^directory[[:space:]]+[[:print:]]+$' /etc/openldap/slapd.conf | 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
704e3ce
					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.
a2323f6
	tlsconfigs=`LANG=C egrep '^(TLSCACertificateFile|TLSCertificateFile|TLSCertificateKeyFile)[[:space:]]' /etc/openldap/slapd.conf | 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=$?
3dbb0a0
	# print warning if slaptest passed but reports some problems
3dbb0a0
	if test $slaptestexit == 0  -a  -n "$slaptestout" ; then
3dbb0a0
		echo -n $"Checking configuration files for $prog: " ; warning ; echo
3dbb0a0
		echo "$slaptestout"
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
e466757
			dirs=`LANG=C egrep '^directory[[:space:]]+[[:print:]]+$' /etc/openldap/slapd.conf | 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
e466757
		exit 1
e466757
	fi
a2323f6
}
a2323f6
a2323f6
function start() {
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: "
9e63293
	daemon --check=$prog ${slapd} -h "\"$harg\"" -u ${user} $OPTIONS $SLAPD_OPTIONS 
0a5bb3a
	RETVAL=$?
cvsdist cba1243
	echo
cvsdist cba1243
	if [ $RETVAL -eq 0 ]; then
0a5bb3a
	    if grep -q "^replogfile" /etc/openldap/slapd.conf; then
cvsdist 97bfc35
		prog=`basename ${slurpd}`
7d73bcb
		i=1;
7d73bcb
		for replogfile in `grep  "^replogfile" /etc/openldap/slapd.conf`
7d73bcb
		do
7d73bcb
		    if [ "$replogfile" != "replogfile" ]
7d73bcb
		    then
7d73bcb
			echo -n $"Starting $prog: "
ba2e462
			daemon ${slurpd} -r $replogfile -n $i $SLURPD_OPTIONS
7d73bcb
			# make the return value nozero if any of the slurpd failed
7d73bcb
			RET=$?
7d73bcb
			if [ $RET -ne 0 ] ; then
7d73bcb
			    RETVAL=$RET
7d73bcb
			fi
7d73bcb
			i=$[i+1]
7d73bcb
			echo
7d73bcb
		    fi
7d73bcb
		done
0a5bb3a
	    fi
cvsdist cba1243
	fi
cvsdist cba1243
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ldap
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: "
cvsdist cba1243
	killproc ${slapd}
cvsdist cba1243
	RETVAL=$?
cvsdist 9bfe3cb
	echo
cvsdist cba1243
	if [ $RETVAL -eq 0 ]; then
cvsdist cba1243
	    if grep -q "^replogfile" /etc/openldap/slapd.conf; then
cvsdist 7a16c4a
		prog=`basename ${slurpd}`
cvsdist 7a16c4a
		echo -n $"Stopping $prog: "
cvsdist cba1243
		killproc ${slurpd}
cvsdist cba1243
		RETVAL=$?
cvsdist 9bfe3cb
		echo
cvsdist cba1243
	    fi
cvsdist cba1243
	fi
0a5bb3a
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ldap /var/run/slapd.args
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)
0a5bb3a
	status ${slapd}
233b801
	RETVAL=$?
0a5bb3a
	if grep -q "^replogfile" /etc/openldap/slapd.conf ; then
0a5bb3a
	    status ${slurpd}
233b801
	    RET=$?
233b801
	    if [ $RET -ne 0 ] ; then
233b801
		RETVAL=$RET;
233b801
	    fi
cvsdist cba1243
	fi
cvsdist cba1243
	;;
cvsdist cba1243
    restart)
cvsdist cba1243
	stop
cvsdist cba1243
	start
233b801
	RETVAL=$?
cvsdist cba1243
	;;
cvsdist cba1243
    condrestart)
0a5bb3a
	if [ -f /var/lock/subsys/ldap ] ; then
0a5bb3a
	    stop
0a5bb3a
	    start
233b801
	    RETVAL=$?
0a5bb3a
	fi
cvsdist cba1243
	;;
cvsdist cba1243
    *)
cvsdist 9bfe3cb
	echo $"Usage: $0 {start|stop|restart|status|condrestart}"
cvsdist cba1243
	RETVAL=1
cvsdist cba1243
esac
cvsdist cba1243
cvsdist cba1243
exit $RETVAL