#!/bin/sh -efu

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

. shell-config

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

SLAPD_CONF="/etc/openldap/slapd-generated.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" | sed -e 's/"//g'
}

get_sid()
{
	local sid="$(net getlocalsid 2>/dev/null)"
	[ "$?" -eq 0 ] && SID=${sid##*:}
}

ldap_config() {
	[ "$#" -ge 1 ] && local SLAPD_CONF="$1"
	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
