#!/bin/sh

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


#turn off auto expansion
set -f

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

. shell-config

UID_MIN=$(grep '^UID_MIN' /etc/login.defs 2>/dev/null|sed -r 's,UID_MIN[[:space:]]+,,')
[ -z "$UID_MIN" ] && UID_MIN=500

FSTAB="/etc/fstab"
SPOOL_QUOTA="/usr/share/alterator-users-ldap/spool_quota.awk"
REPQUOTA="/usr/sbin/repquota"
SETQUOTA="/usr/sbin/setquota"

SLAPD_CONF="/etc/openldap/slapd.conf"
SLAPD_HDB_PREFIX="/etc/openldap/slapd-hdb"

list_users()
{
    local db="$1"
    local IFS=:
    
    ldap-getent-passwd "${SLAPD_HDB_PREFIX}-$db.conf" |
    while read name password uid gid gecos home shell; do
        [ "$uid" -ge "$UID_MIN" ] || continue
	[ "$shell" == "/sbin/nologin" ] || grep -qs "^$shell$" /etc/shells || continue
        printf '("%s" gecos "%s" home "%s" shell "%s")' "$name" "$gecos" "$home" "$shell"
    done
}


read_user()
{
    local db="$1"
    local user="$2"
    local SPOOL=$("$SPOOL_QUOTA" -r "$FSTAB")
    local mboxsize=$("$REPQUOTA" -u "$SPOOL" | grep "^$1[[:space:]]" | awk '{print $4}')
    local IFS=:

    ldap-getent-passwd "${SLAPD_HDB_PREFIX}-$db.conf" "$user"|
	(read name password uid gid gecos home shell;
	 printf ' gecos "%s" home "%s" shell "%s" mboxsize "%s"' "$gecos" "$home" "$shell" "$((mboxsize/1024))")
}

set_spool_quota()
{
    local SPOOL=$("$SPOOL_QUOTA" -r "$FSTAB")
    # hard limit = soft limit * 1.1
    "$SETQUOTA" "$1" "$(($2*1024))" "$(($2*1126))" 0 0 "$SPOOL"
}

error_message()
{
	echo "(error \"$1\")"
}

user_error_message()
{
	case "$1" in
		1) error_message "`_ "can't update password file"`" ;;  #'
	        2) error_message "`_ "invalid command syntax"`" ;; 
       		3) error_message "`_ "invalid argument to option"`" ;; 
		4) error_message "`_ "uid already in use"`" ;; 
	        6) error_message "`_ "specified user doesn't exist"`" ;; #'
		8) error_message "`_ "user currently logged in"`" ;;
		9) error_message "`_ "username already in use"`" ;;
		10) error_message "`_  "can't update group file"`" ;; #'
		12) error_message "`_ "can't create or remove home directory"`" ;;#'
		13) error_message "`_ "can't create mail spool"`" ;; #'
		*) error_message "retcode=$1" ;;
	esac
}


on_message()
{
	case "$in_action" in
		#object manipulations
		constraints)
			echo '('
			if [ "$in__objects" = "/" ]; then
			    printf 'name (label "%s")' "`_ "Domain"`"
			else
			    printf 'name (label "%s")' "`_ "Name"`"
			fi
			printf 'shell (label "%s")' "`_ "Shell"`"
			printf 'home (label "%s")' "`_ "Home directory"`"
			printf 'passwd1 (equal passwd2 label "%s")' "`_ "Password"`"
			printf 'gecos (label "%s")' "`_ "Gecos"`"
			echo ')'
			;;
		list)
		    echo '('
			case "${in__objects##*/}" in
				domains)
					shell_config_get "$SLAPD_CONF" include ' ' |
					    grep "${SLAPD_HDB_PREFIX}" |
			    		    sed -e "s,^${SLAPD_HDB_PREFIX}-\(.*\)\.conf$,(\"\1\"),g"
					;;
				shells)
				    	sed 's,.*,("&"),' /etc/shells
					printf '("/sbin/nologin")'
					;;
				*)
					list_users "${in__objects##*/}"
					;;
			esac
		    echo ')'
		    ;;
		read)
			echo '('
			user="${in__objects##*/}"
			domain="${in__objects%/$user}"
			read_user "${domain##*/}" "$user"
			echo ')'
			;;
		write)
			domain="${in__objects%/$in_name}"
			local retcode=0
			(   [ -n "$in_gecos" ] && echo "cn: $in_gecos"
			    [ -n "$in_home" ] && echo "homeDirectory: $in_home"
			    [ -n "$in_shell" ] && echo "loginShell: $in_shell" ) |
				cl-usermod "${SLAPD_HDB_PREFIX}-${domain##*/}.conf" "$in_name" &> /dev/null
			retcode=$?
			if [ "$retcode" -eq 0 -a -n "$in_passwd1" ];then
			    echo "userPassword: $in_passwd1" |
				cl-usermod "${SLAPD_HDB_PREFIX}-${domain##*/}.conf" "$in_name" &> /dev/null
			    retcode=$?
			fi

#			test -n "$in_mboxsize" && set_spool_quota "$in_name" "$in_mboxsize" &> /dev/null

			if [ "$retcode" -ne 0 ]; then
				user_error_message "$retcode"
			else
				echo '()'
			fi
			;;
		new)
			domain="${in__objects%/$in_name}"
			local retcode=0
			cl-useradd "${SLAPD_HDB_PREFIX}-${domain##*/}.conf" "$in_name" &> /dev/null
			retcode=$?
			if [ "$retcode" -eq 0 ]; then
			    (	[ -n "$in_gecos" ] && echo "cn: $in_gecos"
				[ -n "$in_home" ] && echo "homeDirectory: $in_home"
				[ -n "$in_shell" ] && echo "loginShell: $in_shell" ) |
				    cl-usermod "${SLAPD_HDB_PREFIX}-${domain##*/}.conf" "$in_name" &> /dev/null
			    retcode=$?
			fi

			if [ "$retcode" -eq 0 -a -n "$in_passwd1" ];then
			    echo "userPassword: $in_passwd1" |
			        cl-usermod "${SLAPD_HDB_PREFIX}-${domain##*/}.conf" "$in_name" &> /dev/null
			    retcode=$?
			fi
#			test -n "$in_mboxsize" && set_spool_quota "$in__objects" "$in_mboxsize" &> /dev/null

			if [ "$retcode" -ne 0 ]; then
				user_error_message "$retcode"
			else
				echo '()'
			fi
			;;
		delete)
			domain="${in__objects%/$in_name}"
			local retcode=0
			cl-userdel "${SLAPD_HDB_PREFIX}-${domain##*/}.conf" "$in_name" &> /dev/null
			retcode=$?
			if [ "$retcode" -ne 0 ]; then
				user_error_message "$retcode"
			else
				echo '()'
			fi
			;;
		*)
			echo '#f'
			;;
	esac
}


message_loop
