#!/bin/sh

. /usr/share/alterator/build/backend3.sh

. shell-config

SLAPD_CONF="/etc/openldap/slapd.conf"
SLAPD_HDB_PREFIX="/etc/openldap/slapd-hdb"
SLAPD_ROOT="/var/lib/ldap"
LDAP_SYSCONFIG="/etc/sysconfig/ldap"
LDAP_CONF="/etc/openldap/ldap.conf"

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-openldap" "$1"
}

ldap_read_sysconfig()
{	local url
	url="$(shell_config_get "$LDAP_SYSCONFIG" SLAPDURLLIST)"
	[ "${url/ldap:\/\//}" != "$url" ] && echo ' port_ldap "#t"'
	[ "${url/ldaps:\/\//}" != "$url" ] && echo ' port_ldaps "#t"'
	[ "${url/\/\/localhost\/}" != "$url" ] && echo ' localhost "#t"'
}

# inits a LDAP base with People and Group containers
# uses a base suffix as the 1st arg, and slapd config as the 2nd
# slapd daemon shouldn't currently use this updated base
ldap_init_base()
{
	local base config
	[ "$#" -eq 2 ] || return
	base="$1"
	config="$2"

	cat << EOF | slapadd -b "$base" -f "$config"
dn: $base
objectClass: organization
objectClass: dcObject
$(printf %s\\n "$base" | sed -r 's/^dc=([^[:space:],]*).*$/dc: \1/')
$(printf %s\\n "$base" | sed -r 's/^.*dc=([^[:space:],]*)/o: \1/')

dn: ou=People,$base
objectClass: organizationalUnit
ou: People

dn: ou=Group,$base
objectClass: organizationalUnit
ou: Group
EOF
}

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			printf ' ldap_suffix (label "%s")' "`_ "Base DN"`"
			printf ' ldap_rootdn (label "%s")' "`_ "Root DN"`"
			printf ' passwd1  (equal passwd2 label "%s")' "`_ "Root password"`"
			printf ' name (label "%s")' "`_ "Domain"`"
			printf ' localhost (label "%s")' "`_ "Accept local connections only"`"
			printf ' port_ldap (label "%s")' "`_ "Enable LDAP port"`"
			printf ' port_ldaps (label "%s")' "`_ "Enable LDAPS (TLS) port"`"
			echo ')'
			;;
		list)
			echo '('
			shell_config_get "$SLAPD_CONF" include ' ' | \
			    grep "${SLAPD_HDB_PREFIX}" | \
			    sed -e "s,^${SLAPD_HDB_PREFIX}-\(.*\)\.conf$,(\"\1\"),g"
			echo ')'
			;;
		read)
			echo '('
			if [ "$in__objects" != "/" ]; then
			    suffix="$(shell_config_get "${SLAPD_HDB_PREFIX}-${in__objects#*/}.conf" suffix ' ')"
			    rootdn="$(shell_config_get "${SLAPD_HDB_PREFIX}-${in__objects#*/}.conf" rootdn ' ')"
			    printf 'name "%s" ldap_suffix "%s" ldap_rootdn "%s"' \
				"${in__objects#*/}" \
				"$(string_quote_remove $suffix)" \
				"$(string_quote_remove $rootdn)"
			else
				ldap_read_sysconfig
			fi
			echo ')'
			;;
		write)
			if [ "$in__objects" != "/" ]; then
				shell_config_set "${SLAPD_HDB_PREFIX}-${in__objects#*/}.conf" suffix "$(string_quote_remove $in_ldap_suffix)" ' ' ' '
				shell_config_set "${SLAPD_HDB_PREFIX}-${in__objects#*/}.conf" rootdn "$(string_quote_remove $in_ldap_rootdn)" ' ' ' '
				[ -n "$in_passwd1" ] && \
				    shell_config_set "${SLAPD_HDB_PREFIX}-${in__objects#*/}.conf" rootpw "$(string_quote_remove $in_passwd1)" ' ' ' '
			else
				host=
				url=
				if [ "$in_localhost" = "#t" ]; then
					host="localhost"
				fi
				if [ "$in_port_ldap" = "#t" ]; then
					url="$url ldap://$host/"
				fi
				if [ "$in_port_ldaps" = "#t" ]; then
					url="$url ldaps://$host/"
				fi
				shell_config_set "$LDAP_SYSCONFIG" SLAPDURLLIST "\"'$url'\""
				shell_config_set "$LDAP_CONF" URI "$url" ' ' ' '
			fi
			service slapd condrestart &> /dev/null
			echo '()'
			;;
		new)
			if grep -qs "^include[[:space:]]*${SLAPD_HDB_PREFIX}-${in_name}\.conf$" "$SLAPD_CONF"; then
			    printf '(error "%s")' "`_ "Such domain is already specified"`"
			else
			    # don't show config anybody
			    UMASK="$(umask)"
			    umask 0077
			    TMPFILE="$(mktemp -t slapd.conf.XXXXXX)"
			    cat "$SLAPD_CONF" > "$TMPFILE"
			    # append new base to the temporary config
			    echo "include ${SLAPD_HDB_PREFIX}-$in_name.conf" >> "$TMPFILE"
			    # make base config from template
			    install -o ldap -g ldap "${SLAPD_HDB_PREFIX}-sample.cnf" "${SLAPD_HDB_PREFIX}-$in_name.conf"
			    dc="${in_name//\./,dc=}"
			    # update base config
			    shell_config_set "${SLAPD_HDB_PREFIX}-$in_name.conf" suffix "dc=$dc" ' ' ' '
			    shell_config_set "${SLAPD_HDB_PREFIX}-$in_name.conf" rootdn "cn=admin,dc=$dc" ' ' ' '
			    shell_config_set "${SLAPD_HDB_PREFIX}-$in_name.conf" directory "$SLAPD_ROOT/bases/$in_name" ' ' ' '
			    # create base dir and copy DB_CONFIG there
			    mkdir "$SLAPD_ROOT/bases/$in_name"
			    cp -f "$SLAPD_ROOT/bases/DB_CONFIG" "$SLAPD_ROOT/bases/$in_name/DB_CONFIG"

			    # init base with People and Group containers
			    ldap_init_base "dc=$dc" "$TMPFILE"

			    # fix ownership of newly created files
			    chown ldap:ldap "$SLAPD_ROOT/bases/$in_name"/*
			    # get config back
			    cat "$TMPFILE" > "$SLAPD_CONF"
			    rm -f "$TMPFILE"
			    # and restart service
			    service slapd condrestart &> /dev/null
			    # get back umask
			    umask "$UMASK"
			    echo "()"
			fi
			;;
			
		delete)
			local config="${SLAPD_HDB_PREFIX}-${in__objects#*/}.conf"
			local directory="$(shell_config_get "$config" directory ' ')"
			[ -n "$directory" ] && rm -fR "$directory"
			rm -fR "$SLAPD_ROOT/dblogs/${in__objects#*/}"
			rm -f "$config"
			sed -e "/^include[[:space:]]*$(quote_sed_regexp $config)$/ {d}" -i "$SLAPD_CONF"
			service slapd condrestart &> /dev/null
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}


message_loop

