#!/bin/sh
# Store as /opt/ltsp/$arch/usr/share/ltsp/get-ldap-ltsp-config
#
# Fetch LTSP client settings from LDAP based on MAC address
#
# Uses ethernet address as stored in the dhcpHost objectclass using
# the dhcpHWAddress attribute or as stored in the ieee802Device
# objectclass with the macAddress attribute.
#
# This module is written to be schema agnostic, and only depend on the
# existence of attribute names.
#
# The LTSP configuration variables are saved directly using a
# ltspConfig attribute.  To set the SERVER variable, set a ltspConfig
# attribute to 'SERVER=value'.
#
# Some LDAP schema should be created with all the relevant
# configuration settings.  Something like this should work:
#
# attributetype ( some-OID NAME 'ltspConfig'
#    DESC 'LTSP config setting'
#    SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
#
# objectclass ( some-OID NAME 'ltspClientConfigAux'
#    DESC 'LTSP client configuration attributes'
#    SUP top
#    AUXILIARY
#    MAY ( ltspConfig ))
#
# objectclass ( some-OID NAME 'ltspClientConfig'
#    DESC 'LTSP client configuration attributes'
#    SUP top
#    STRUCTURAL
#    MUST ( cn )
#    MAY ( ltspConfig ))
#
# Example LDAP object:
#
# dn: cn=ltspConfigDefault,ou=somewhere
# objectclass: device
# objectclass: ltspClientConfigAux
# cn=ltspConfigDefault
# ltspConfig: SERVER=ltspserver.somewhere
# ltspConfig: SOUND=N
#
# dn: cn=hostname,ou=somewhere
# objectclass: ieee802Device
# objectclass: domainRelatedObject
# objectclass: ltspClientConfigAux
# cn=hostname
# macAddress: 00:01:02:03:04:05
# associatedDomain: hostname.somewhere
# ltspConfig: SOUND=N

#
# GOSA also have a LDAP approach for the tftp content (PXE arguments),
# searching for
#
# filter => "(&(macAddress=$mac)(objectClass=gotoTerminal))",
# attrs => [ 'gotoTerminalPath', 'gotoBootKernel',
#            'gotoKernelParameters', 'gotoLdapServer', 'cn' ] );
#
# See the fts-ltsp-ldap package for this.  The gotoTerminal object
# class is auxiliary, allowing it to be combined with other
# objectclasses.

echo "Fetching ltsp config from ldap"

#LDAP_HOST=tjener.intern
#BASE_DN=dc=skole,dc=skolelinux,dc=no
cachefile=/var/cache/ltsp/ltsp_config_edu
envfile=/var/cache/ltsp/ltsp_config_env
PATH=/bin:/usr/bin:/usr/sbin

setup_from_ldap() {
    filter="(&(ltspConfig=*)$1)"
    config="$(ldapsearch -h "$LDAP_HOST" -b "$BASE_DN" -x "$filter" ltspConfig | \
	    awk '/^ltspConfig: [^=]*=[^;]*$/ { print $2 }')"
    if [ "$config" ] ; then
	if eval "$config" ; then
            echo "$config" >> $cachefile
	else
	    logger -t ltsp-ldap "got invalid LTSP config from LDAP: '$config'"
	fi
	foundinldap=true
    fi
}

lookup_mac_addrs() {
    PATH=/sbin:$PATH LANG=C ifconfig 2>/dev/null | grep -i hwaddr | awk '{print $5}' | sort -u
}

# Only check LDAP when the result can be cached
if touch $cachefile && touch $envfile; then
    if [ -z "$LDAP_HOST" ] ; then
	LDAP_HOST=$(debian-edu-ldapserver || :)
    fi
    if [ "$LDAP_HOST" ] && ping -W2 -c2 "$LDAP_HOST" > /dev/null 2>&1 ; then
        if [ -z "$BASE_DN" ] ; then
            BASE_DN=$(debian-edu-ldapserver -s "$LDAP_HOST" -b || :)
        fi

        if [ "$BASE_DN" ] ; then
            # First set default values if found
            setup_from_ldap '(cn=ltspConfigDefault)'

            # Next, look up the host MAC address(es).
            foundinldap=false
            if [ -e /proc/net/dev ] ; then
                for MAC in $(lookup_mac_addrs) ; do
                    filter="(|(dhcpHWAddress=ethernet $MAC)(macAddress=$MAC))"
                    setup_from_ldap "$filter"
                done
            fi
            # If the HW MAC address was not found, look for the host name
            # instead.
            if [ false = "$foundinldap" ] ; then
                fqdn=$(hostname -f)
                # No use checking if it isn't set up yet
                if [ "(none)" != "$fqdn" ] ; then
                    setup_from_ldap "(associatedDomain=$fqdn)"
                fi
            fi
        fi
    fi
fi
echo "Fetching ltsp config from ldap done"
