#!/bin/ksh
#
# OSSTech OpenLDAP slapd service
#
# chkconfig: - 27 73
# chkconfig.solaris: 2 70 40
# chkconfig.aix: 2 270 40
# chkconfig.debian: 2345 19 80
# description: LDAP stands for Lightweight Directory Access Protocol, used \
#              for implementing the industry standard directory services.
#
### BEGIN INIT INFO
# Provides:		osstech-ldap
# Required-Start:	$network $local_fs osstech-svscan
# Required-Stop:	$network $local_fs osstech-svscan
# Should-Start:		$syslog
# Should-Stop:		$syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
### END INIT INFO

. /opt/osstech/etc/sv/svinit || exit 1

sv_name="slapd"
slapd_conf_default="/opt/osstech/etc/openldap/slapd.conf"
slapd_user_default="ldap"
slaptest_cmd="/opt/osstech/sbin/slaptest"

function sv_configtest {
  typeset sv_dir=$(sv_dir "$1"); shift

  typeset slapd_conf=$(sv_env "$sv_dir" CONFIGFILE || echo "$slapd_conf_default")
  typeset slapd_user=$(sv_env "$sv_dir" USER || echo "$slapd_user_default")

  typeset db_dir= db_file= slaptest_opts=
  for db_dir in `awk '$1=="directory" {print $2}' "$slapd_conf"`; do
    for db_file in `find $db_dir ! -user "$slapd_user" \( -name "*.dbb" -o -name "*.gdbm" -o -name "*.bdb" -o -name "__db.*" -o -name "log.*" -o -name alock \)`; do
      echo_n "$db_file is not owned by \"$slapd_user\" "; warning; echo
    done
    if [ ! -s $db_dir/id2entry.* ]; then
      slaptest_opts=-u
    fi
  done

  typeset tls_file=
  for tls_file in `awk '$1~/^TLS[A-Za-z]*File$/ {print $2}' "$slapd_conf"`; do
    if ! setuidgid "$slapd_user" test -r $tls_file; then
      echo_n "$tls_file is not readable by \"$slapd_user\" "; warning; echo
    fi
  done

  typeset slaptest_out= slaptest_ret=
  slaptest_out=$(setuidgid "$slapd_user" "$slaptest_cmd" $slaptest_opts 2>&1)
  slaptest_ret=$?
  if [ $slaptest_ret -eq 0 ]; then
    if echo "$slaptest_out" |grep -v "config file testing succeeded" >/dev/null; then
      echo_n "Checking configuration files for ${sv_dir##*/}: "; warning; echo
      echo "$slaptest_out"
    fi
  fi
  if [ $slaptest_ret -ne 0 ]; then
    echo_n "Checking configuration files for ${sv_dir##*/}: "; failure; echo
    echo "$slaptest_out"
    return 6
  fi

  return 0
}

sv_init "$sv_name" ${1+"$@"}

