mhonek / rpms / openldap

Forked from rpms/openldap 3 years ago
Clone
cvsdist 9bfe3cb
#!/bin/bash
cvsdist cba1243
#
Jan Zeleny 45f722d
# slapd	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
Jan Zeleny 5dec441
# pidfile: /var/run/slapd.pid
cvsdist cba1243
1447738
### BEGIN INIT INFO
Jan Zeleny 5dec441
# Provides: slapd
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"
Jan Zeleny 2f39763
SLAPD_URLS=""
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
Jan Zeleny 5dec441
lockfile=/var/lock/subsys/slapd
Jan Zeleny 45f722d
configdir=/etc/openldap/slapd.d/
Jan Zeleny 1507ff3
configfile=/etc/openldap/slapd.conf
Jan Zeleny 5dec441
pidfile=/var/run/slapd.pid
Jan Zeleny 5dec441
slapd_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=
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=""
Jan Zeleny 45f722d
	dbdirs=""
Jan Zeleny 1507ff3
    if [ -d $configdir ]; then
Jan Zeleny c1daa14
	    for configfile in `ls -1 $configdir/cn\=config/olcDatabase*.ldif`; do
Jan Zeleny 1507ff3
		    dbdirs=$dbdirs"
Jan Zeleny 1507ff3
		    "`LANG=C egrep '^olcDbDirectory[[:space:]]*:[[:space:]]+[[:print:]]+$' $configfile | sed 's,^olcDbDirectory: ,,'`
Jan Zeleny 1507ff3
	    done
Jan Zeleny 1507ff3
    elif [ -f $configfile ]; then
Jan Zeleny 1507ff3
		    dbdirs=`LANG=C egrep '^directory[[:space:]]+' $configfile | sed 's,^directory[[:space:]]*,,'`
Jan Zeleny 1507ff3
    else
Jan Zeleny 1507ff3
        exit 6
Jan Zeleny 1507ff3
    fi
Jan Zeleny 45f722d
	for dbdir in $dbdirs; do
Jan Zeleny 1507ff3
		if [ ! -d $dbdir ]; then
Jan Zeleny 1507ff3
			exit 6
Jan Zeleny 1507ff3
		fi
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.
Jan Zeleny 1507ff3
    if [ -d $configdir ]; then
Jan Zeleny c1daa14
		tlsconfigs=`LANG=C awk '/^olcTLS/ {
Jan Zeleny c1daa14
				while (/^olcTLS/) {
Jan Zeleny c1daa14
					if (/^olc(TLSCertificateKeyFile|TLSCertificateFile|TLSCACertificateFile)[ \t]*:[\t]*/) {
Jan Zeleny c1daa14
						do { printf gensub(/^ /,"",1); getline } while (/^ /)
Jan Zeleny c1daa14
						printf "\n" ;
Jan Zeleny c1daa14
					}
Jan Zeleny c1daa14
					else break; 
Jan Zeleny c1daa14
				}
Jan Zeleny c1daa14
			}' $configdir/cn\=config.ldif | awk '{print $2}'`
Jan Zeleny 1507ff3
    elif [ -f $configfile ]; then
Jan Zeleny 1507ff3
	    tlsconfigs=`LANG=C egrep '^(TLSCACertificateFile|TLSCertificateFile|TLSCertificateKeyFile)[[:space:]]+' $configfile | awk '{print $2}'`
Jan Zeleny 1507ff3
    fi
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
Jan Zeleny 45f722d
			#dirs=`LANG=C egrep '^directory[[:space:]]+[[:print:]]+$' $configfile | awk '{print $2}'`
Jan Zeleny 45f722d
			for directory in $dbdirs ; 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
Jan Zeleny 5dec441
	[ `id -u` -eq 0 ] || exit 4
a2323f6
	configtest
0a5bb3a
	# Define a couple of local variables which we'll need. Maybe.
a2323f6
	user=ldap
cvsdist 7a16c4a
	prog=`basename ${slapd}`
Jan Zeleny 2f39763
	harg="$SLAPD_URLS"
05dc6ea
	if test x$SLAPD_LDAP = xyes ; then
Jan Zeleny 2f39763
	    harg="$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=$?
Jan Zeleny 5dec441
	if [ $RETVAL -eq 0 ]; then
Jan Zeleny 5dec441
		touch $lockfile
Jan Zeleny 5dec441
		ln $slapd_pidfile $pidfile
Jan Zeleny 5dec441
	fi
808537b
	echo
cvsdist cba1243
	return $RETVAL
cvsdist cba1243
}
cvsdist cba1243
cvsdist cba1243
function stop() {
0a5bb3a
	# Stop daemons.
cvsdist 7a16c4a
	prog=`basename ${slapd}`
Jan Zeleny 5dec441
	[ `id -u` -eq 0 ] || exit 4
cvsdist 7a16c4a
	echo -n $"Stopping $prog: "
Jan Zeleny 5dec441
Jan Zeleny 5dec441
	# This will remove pid and args files from /var/run/openldap
Jan Zeleny 5dec441
	killproc -p $slapd_pidfile -d $SLAPD_SHUTDOWN_TIMEOUT ${slapd}
cvsdist cba1243
	RETVAL=$?
Jan Zeleny 5dec441
Jan Zeleny 5dec441
	# Now we want to remove lock file and hardlink of pid file
Jan Zeleny 5dec441
	[ $RETVAL -eq 0 ] && rm -f $pidfile $lockfile
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)
Jan Zeleny 16a73c1
	start
233b801
	RETVAL=$?
0a5bb3a
	;;
cvsdist cba1243
    stop)
Jan Zeleny 16a73c1
	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)
Jan Zeleny 1507ff3
	status -p $pidfile ${slapd} > /dev/null 2>&1 || exit 0
Jan Zeleny 5dec441
	stop
Jan Zeleny 5dec441
	start
Jan Zeleny 5dec441
	;;
Jan Zeleny 5dec441
    usage)
Jan Zeleny 5dec441
	echo $"Usage: $0 {start|stop|restart|status|condrestart|configtest|usage}"
Jan Zeleny 5dec441
	RETVAL=0
cvsdist cba1243
	;;
cvsdist cba1243
    *)
Jan Zeleny 5dec441
	echo $"Usage: $0 {start|stop|restart|status|condrestart|configtest|usage}"
Jan Zeleny 5dec441
	RETVAL=3
cvsdist cba1243
esac
cvsdist cba1243
cvsdist cba1243
exit $RETVAL