#!/bin/sh -efux

if [ -z "${__included_ldap_config-}" ]; then
__included_ldap_config=1

. shell-config
. shell-error

rdelim='[[:space:]]\+'
wdelim=' '

SLAPD_CONF=
PAMLDAP_CONF="/etc/pam_ldap.conf"
PROXYUSER_FILE="/etc/openldap/proxyuser.pass"
SID="S-1-0-0"

# mail spool dir
SPOOL="/var/spool/mail"

read_config()
{
	shell_config_get "$1" "$2" "$rdelim" | tr -d '"'
}

get_default_conf()
{
    local base="$(read_config "$PAMLDAP_CONF" base)"

    [ -z "$base" ] && message "base dn not set in '$PAMLDAP_CONF'" && exit 1

    /usr/sbin/ldap-dn find "$base"
}

get_sid()
{
     local sid="$(net getlocalsid | cut -f2 -d':'| sed -e "s/[[:space:]]//g" 2>/dev/null)"
     [ -n "$sid" ] && SID="$sid"
}

ldap_config() {

    [ "$#" -gt 1 ] && SLAPD_CONF="$1"
    [ -z "$SLAPD_CONF" ] && SLAPD_CONF="$(get_default_conf)"
    
	base=$(read_config "$SLAPD_CONF"  suffix)
	rootdn=$(read_config "$SLAPD_CONF" rootdn)
	rootpw=$(read_config "$SLAPD_CONF" rootpw)

	binddn=$(read_config "$PAMLDAP_CONF" binddn)
	bindpw=$(read_config "$PAMLDAP_CONF" bindpw)

	[ -n "$rootpw" ] && rootpw="-w $rootpw" || rootpw="-W"
	[ -n "$binddn" ] && binddn="-D $binddn"
	[ -n "$bindpw" ] && bindpw="-w $bindpw" 
	export base rootdn rootpw binddn bindpw
}

local_getent_group()
{
	local group="$1"
	if [ -n "$group" ]; then
		grep -v "^#" /etc/group | grep "^$group:"
	else
		grep -v "^#" /etc/group
	fi
}

local_getent_passwd()
{
	local user="$1"
	if [ -n "$user" ]; then
		grep -v "^#" /etc/passwd | grep "^$user:"
	else
		grep -v "^#" /etc/passwd
	fi
}
fi #__included_ldap_config
