#!/bin/sh -e
#
# Test if the LDAP server is working.
# $Id: ldap-client,v 1.1 2004/02/29 11:17:08 runesk-guest Exp $

RESULT=0

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
