#!/bin/bash
# Configure the ldap service for the appliance
# Copyright (C) 2004 Networks Associates Technology Inc. All Rights Reserved
#
CONFIGOPT=${1}
if [ -f $NETAWSS/.profile.vars ]; then
 . $NETAWSS/.profile.vars
else
 . /var/NAIENV/.profile.vars
fi
PATH=$wsPATH:$PATH
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

option=$1

create_jail()
{
    JAIL=/var/ldap.jail
    [[ -e /var/lock/subsys/ldap ]] && /etc/init.d/ldap stop

    # Create the jail
    if [[ $JAIL/dev -ef /var/dev ]]; then
	umount $JAIL/dev || return
    fi
    /bin/rm -rf ${JAIL}
    mkdir ${JAIL}

    # Create the files in the jail
    /bin/cp --recursive --parents /etc/openldap/* ${JAIL}
    /bin/grep ldap /etc/passwd > ${JAIL}/etc/passwd
    /bin/grep ldap /etc/group > ${JAIL}/etc/group
    /bin/chown --recursive ldap:ldap ${JAIL}/*
    mkdir ${JAIL}/dev && mount --bind /var/dev ${JAIL}/dev

    # request restart
    [[ -f $WS_RESTART_LIST ]] && echo ldap >>"$WS_RESTART_LIST"
}

create_sysconfig_ldap()
{
	echo "OPTIONS=\"-r ${JAIL} -u ldap -g ldap\"" > /etc/sysconfig/ldap
}

start()
{
    # check ldap is active
    /sbin/chkconfig --level 3 ldap || return 0	# nothing to do

    # Create the jail
    create_jail

    # Create the configuration options to use the jail
    create_sysconfig_ldap
}

case $option in
    (start|restart|reload) start ;;
    (stop|status) : ;;
    (*) echo $"Usage: $0 {start|stop|restart|reload|status}"; exit 1 ;;
esac

exit 0
