#!/bin/sh -e
#
# Test if the LDAP server is working.
# $Id: ldap-client 5107 2005-12-05 20:51:01Z pere $

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

# Only networked profiles use LDAP
if echo "$PROFILE" | egrep -q 'Main-Server|Workstation|Thin-Client-Server' ; then
    :
else
    exit 0
fi

RESULT=0

# Test if LDAP server is reachable
if ping -c 3 ldap > /dev/null 2>&1 ; then
    echo "success: $0: Host 'ldap' is pingable."
else
    echo "error: $0: Host 'ldap' is not pingable."
    RESULT=1
fi

for file in libnss-ldap.conf pam_ldap.conf ; do
    if [ -f /etc/$file ] ; then
	grep -v '^#' /etc/$file | grep -v '^$' | sort |
	    sed "s/^/info: $file: /"
    else
	RESULT=1
	echo "error: $0: $file is missing."
    fi
done

if [ -f /etc/ldap/ldap.conf ] ; then
    if grep -q "^HOST ldap" /etc/ldap/ldap.conf ; then
        :
    else
        echo "error: $0: ldap/ldap.conf misses definition of HOST ldap"
        RESULT=1
    fi
else
    RESULT=1
    echo "error: $0: ldap/ldap.conf is missing."
fi

# test netgroups
if ldap2netgroup | grep -q tjener ; then
    echo "success: $0: ldap2netgroup found 'tjener'"
else
    echo "error: $0: ldap2netgroup unable to find 'tjener'."
    RESULT=1
fi

if netgroup all-hosts | grep -q tjener ; then
    echo "success: $0: netgroup found 'tjener'"
else
    echo "error: $0: netgroup unable to find 'tjener'."
    RESULT=1
fi

if [ -x /usr/bin/ldapsearch ] ; then
    LDAP_MOUNTS="$(
        ldapsearch -LLL -h ldap -b ou=Automount,dc=skole,dc=skolelinux,dc=no \
                   -x '(objectClass=automount)' |\
            grep "^cn:" | while read attr val; do
                echo "$val"
          done
        )"
    echo info: $0: Mountpoints found in ldap: $LDAP_MOUNTS
    for WANT_MOUNT in /skole tjener home0 ; do
        if ! echo $LDAP_MOUNTS | grep -q $WANT_MOUNT ; then
            echo "error: $0: Missing $WANT_MOUNT mount point in ldap"
            RESULT=1
        fi
    done
else
    echo "error: $0:Missing /usr/bin/ldapsearch "
    RESULT=1
fi

exit $RESULT
