Rev 4 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/bin/bash## slapd This shell script takes care of starting and stopping# ldap servers (slapd).## chkconfig: - 27 73# description: LDAP stands for Lightweight Directory Access Protocol, used \# for implementing the industry standard directory services.# processname: slapd# config: /etc/openldap/slapd.conf# pidfile: /var/run/slapd.pid### BEGIN INIT INFO# Provides: slapd# Required-Start: $network $local_fs# Required-Stop: $network $local_fs# Should-Start:# Should-Stop:# Default-Start:# Default-Stop:# Short-Description: starts and stopd OpenLDAP server daemon# Description: LDAP stands for Lightweight Directory Access Protocol, used# for implementing the industry standard directory services.### END INIT INFO# Source function library.. /etc/init.d/functions# Define default values of options allowed in /etc/sysconfig/ldapSLAPD_LDAP="yes"SLAPD_LDAPI="no"SLAPD_LDAPS="no"SLAPD_URLS=""SLAPD_SHUTDOWN_TIMEOUT=3# OPTIONS, SLAPD_OPTIONS and KTB5_KTNAME are not defined# Source an auxiliary options file if we have oneif [ -r /etc/sysconfig/ldap ] ; then. /etc/sysconfig/ldapfislapd=/usr/sbin/slapdslaptest=/usr/sbin/slaptestlockfile=/var/lock/subsys/slapdconfigdir=/etc/openldap/slapd.d/configfile=/etc/openldap/slapd.confpidfile=/var/run/slapd.pidslapd_pidfile=/var/run/openldap/slapd.pidRETVAL=0## Pass commands given in $2 and later to "test" run as user given in $1.#function testasuser() {local user= cmd=user="$1"shiftcmd="$@"if test x"$user" != x ; thenif test x"$cmd" != x ; then/sbin/runuser -f -m -s /bin/sh -c "test $cmd" -- "$user"elsefalsefielsefalsefi}## Check for read-access errors for the user given in $1 for a service named $2.# If $3 is specified, the command is run if "klist" can't be found.#function checkkeytab() {local user= service= klist= default=user="$1"service="$2"default="${3:-false}"if test -x /usr/kerberos/bin/klist ; thenklist=/usr/kerberos/bin/klistelif test -x /usr/bin/klist ; thenklist=/usr/bin/klistfiKRB5_KTNAME="${KRB5_KTNAME:-/etc/krb5.keytab}"export KRB5_KTNAMEif test -s "$KRB5_KTNAME" ; thenif test x"$klist" != x ; thenif LANG=C $klist -k "$KRB5_KTNAME" | tail -n 4 | awk '{print $2}' | grep -q ^"$service"/ ; thenif ! testasuser "$user" -r ${KRB5_KTNAME:-/etc/krb5.keytab} ; thentrueelsefalsefielsefalsefielse$defaultfielsefalsefi}function configtest() {local user= ldapuid= dbdir= file=# Check for simple-but-common errors.user=ldapprog=`basename ${slapd}`ldapuid=`id -u $user`# Unaccessible database files.dbdirs=""if [ -d $configdir ]; thenfor configfile in `ls -1 $configdir/cn\=config/olcDatabase*.ldif`; dodbdirs=$dbdirs""`LANG=C egrep '^olcDbDirectory[[:space:]]*:[[:space:]]+[[:print:]]+$' $configfile | sed 's,^olcDbDirectory: ,,'`doneelif [ -f $configfile ]; thendbdirs=`LANG=C egrep '^directory[[:space:]]+' $configfile | sed 's,^directory[[:space:]]*,,' | tr -d \"`elseexit 6fifor dbdir in $dbdirs; doif [ ! -d $dbdir ]; thenexit 6fifor 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 \)` ; doecho -n $"$file is not owned by \"$user\"" ; warning ; echodoneif test -f "${dbdir}/DB_CONFIG"; thenif ! testasuser $user -r "${dbdir}/DB_CONFIG"; thenfile=DB_CONFIGecho -n $"$file is not readable by \"$user\"" ; warning ; echofifidone# Unaccessible keytab with an "ldap" key.if checkkeytab $user ldap ; thenfile=${KRB5_KTNAME:-/etc/krb5.keytab}echo -n $"$file is not readable by \"$user\"" ; warning ; echofi# Check the configuration file.slaptestout=`/sbin/runuser -m -s "$slaptest" -- "$user" "-u" 2>&1`slaptestexit=$?# slaptestout=`echo $slaptestout 2>/dev/null | grep -v "config file testing succeeded"`# print warning if slaptest passed but reports some problemsif test $slaptestexit == 0 ; thenif echo "$slaptestout" | grep -v "config file testing succeeded" >/dev/null ; thenecho -n $"Checking configuration files for $prog: " ; warning ; echoecho "$slaptestout"fifi# report error if configuration file is wrongif test $slaptestexit != 0 ; thenecho -n $"Checking configuration files for $prog: " ; failure ; echoecho "$slaptestout"if /sbin/runuser -m -s "$slaptest" -- "$user" "-u" > /dev/null 2> /dev/null ; then#dirs=`LANG=C egrep '^directory[[:space:]]+[[:print:]]+$' $configfile | awk '{print $2}'`for directory in $dbdirs ; doif test -r $directory/__db.001 ; thenecho -n $"stale lock files may be present in $directory" ; warning ; echofidonefiexit 6fi}function start() {[ -x $slapd ] || exit 5[ `id -u` -eq 0 ] || exit 4configtest# Define a couple of local variables which we'll need. Maybe.user=ldapprog=`basename ${slapd}`harg="$SLAPD_URLS"if test x$SLAPD_LDAP = xyes ; thenharg="$harg ldap:///"fiif test x$SLAPD_LDAPS = xyes ; thenharg="$harg ldaps:///"fiif test x$SLAPD_LDAPI = xyes ; thenharg="$harg ldapi:///"fi# System resources limit.if [ -n "$SLAPD_ULIMIT_SETTINGS" ]; thenulimit="ulimit $SLAPD_ULIMIT_SETTINGS &>/dev/null;"elseulimit=""fi# Release reserverd port[ -x /sbin/portrelease ] && /sbin/portrelease slapd &>/dev/null || :# Start daemons.echo -n $"Starting $prog: "daemon --pidfile=$pidfile --check=$prog $ulimit ${slapd} -h "\"$harg\"" -u ${user} $OPTIONS $SLAPD_OPTIONSRETVAL=$?if [ $RETVAL -eq 0 ]; thentouch $lockfileln $slapd_pidfile $pidfilefiechoreturn $RETVAL}function stop() {# Stop daemons.prog=`basename ${slapd}`[ `id -u` -eq 0 ] || exit 4echo -n $"Stopping $prog: "# This will remove pid and args files from /var/run/openldapkillproc -p $slapd_pidfile -d $SLAPD_SHUTDOWN_TIMEOUT ${slapd}RETVAL=$?# Now we want to remove lock file and hardlink of pid file[ $RETVAL -eq 0 ] && rm -f $pidfile $lockfileechoreturn $RETVAL}# See how we were called.case "$1" inconfigtest)configtest;;start)startRETVAL=$?;;stop)stopRETVAL=$?;;status)status -p $pidfile ${slapd}RETVAL=$?;;restart|force-reload)stopstartRETVAL=$?;;condrestart|try-restart)status -p $pidfile ${slapd} > /dev/null 2>&1 || exit 0stopstart;;usage)echo $"Usage: $0 {start|stop|restart|force-reload|status|condrestart|try-restart|configtest|usage}"RETVAL=0;;*)echo $"Usage: $0 {start|stop|restart|force-reload|status|condrestart|try-restart|configtest|usage}"RETVAL=2esacexit $RETVAL