#!/bin/sh -e
#
# Test if the LDAP server is working.
# $Id: ldap-server 5101 2005-12-04 16:05:09Z pere $

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

# Only Main-Server should use LDAP
if echo "$PROFILE" | grep -q Main-Server ; then
    :
else
    exit 0
fi

if [ -f /etc/ldap/slapd.conf ] ; then
    :
else
    echo "error: $0: /etc/ldap/slapd.conf is missing.  Is slapd installed?"
    exit 1
fi

if pidof slapd > /dev/null ; then
    echo "success: $0: slapd is running."
else
    echo "error: $0: slapd is not running."
    exit 1
fi

RESULT=0

for port in ldap ldaps ; do
    proto=tcp

    if netstat -a 2>&1 | grep ":$port " | grep -q "^$proto" ; then
        echo "success: $0: slapd server is listening on $port/$proto."
    else
        echo "error: $0: slapd server is not listening on $port/$proto."
        RESULT=1
    fi
done

if [ -x /usr/sbin/slapcat ] ; then
    slapcat | sed "s%^%info: $0: slapcat: %"
else
    echo "error: $0: Unable to find /usr/sbin/slapcat"
    RESULT=1
fi

if [ -f /etc/ldap/ssl/slapd.pem ] ; then
    openssl verify  /etc/ldap/ssl/slapd.pem | 
	  sed "s%^%info: $0: slapd.pem: %"
else
    echo "error: Missing /etc/ldap/ssl/slapd.pem"
    RESULT=1
fi

exit $RESULT
