#!/bin/sh

po_domain="alterator-vsftpd"

#variables

MAIN_CONFIG_DIR=/etc/vsftpd
USER_CONFIG_DIR=/etc/vsftpd/user_conf

MAIN_CONFIG=$MAIN_CONFIG_DIR/conf

UPLOAD_DIR=/var/ftp/incoming

USER_CONFIG()
{
	echo $USER_CONFIG_DIR/$1
}

set -f

. alterator-sh-functions
. shell-config

#helper functions

#get default param value
#arg0: param-name
default_param()
{
	if [ "$1" = "anonymous_enable" ];then
		echo "YES"
	else	
		echo "NO"
	fi
}


#read some parameter from config
#arg0:config-name
#arg1:param-name
read_param()
{
	[ -f "$1" ] || return 1
	local retval="$(shell_config_get "$1" "$2")"
	[ -n "$retval" ] || retval="$(default_param "$2")"
	echo "$retval"
}

from_scm()
{
	sed -r 's,#t,YES,;s,#f,NO,i'
}

to_string()
{
	sed -r "s,YES,`_ "yes"`,;s,NO,`_ "no"`,;s,DEFAULT,`_ "default"`,"
}

#read_param "a1" "par" || read_param "a2" "par"

on_message()
{
	case "$in_action" in
		read)
		    echo '('
		    ! LANG=C /sbin/chkconfig vsftpd --list|grep -qs 'on$'
		    write_bool_param service_state "$?"

		    write_bool_param anon_state "$(read_param "$MAIN_CONFIG" anonymous_enable)"
		    write_bool_param anon_mkdir "$(read_param "$MAIN_CONFIG" anon_mkdir_write_enable)"
		    write_bool_param anon_upload "$(read_param "$MAIN_CONFIG" anon_upload_enable)"

		    ! [ -d "$UPLOAD_DIR" ]
		    write_bool_param anon_upload_dir "$?"

		    write_bool_param anon_other "$(read_param "$MAIN_CONFIG" anon_other_write_enable)"
		    write_bool_param write_state "$(read_param "$MAIN_CONFIG" write_enable)"
		    write_bool_param local_state "$(read_param "$MAIN_CONFIG" local_enable)"

		    read_param "$MAIN_CONFIG" user_config_dir|grep -qs 'NO'
		    write_bool_param local_detailed "$?"
		    echo ')'
		    ;;
		list)
		    echo '('
		    if [  "$in__objects" = "users" ]; then
			find "$USER_CONFIG_DIR" -mindepth 1 -maxdepth 1 -type f |
			    while read name;do
				local name=${name##*/}
				printf '("%s" user_state "%s")\n' \
			    	    "$name" \
			    	    "$(read_param "$(USER_CONFIG "$name")" "write_enable"|to_string)" #"
			    done
		    elif [ "$in__objects" = "avail_users" ] ;then
			local UID_MIN="$(grep -s ^UID_MIN /etc/login.defs |awk '{print $2;exit}')"
			[ -n "$UID_MIN" ] || UID_MIN=500

			local installed="$(mktemp -t installed.XXXXXX)"
			find "$USER_CONFIG_DIR" -mindepth 1 -maxdepth 1 -type f -printf '%f\n' | sort >"$installed"

			getent passwd |
			    awk -F: -v "uid_min=$UID_MIN" '$3>=uid_min && $1!="root" && $7!="/dev/null"{print $1}'|
			    sort |
			    comm -23 - "$installed" |
			    write_enum
			rm -f "$installed"
		    else
			write_enum_item "enable" "`_ "enable write access"`"
			write_enum_item "disable" "`_ "disable write access"`"
			write_enum_item "delete" "`_ "remove from list"`"
		    fi
		    echo ')'
		    ;;
		write)
    		    [ -n "$in_anon_state" ] &&
		    	shell_config_set "$MAIN_CONFIG" "anonymous_enable" "$(echo "$in_anon_state"|from_scm)"
		    [ -n "$in_anon_mkdir" ] &&
			shell_config_set "$MAIN_CONFIG" "anon_mkdir_write_enable" "$(echo "$in_anon_mkdir"|from_scm)"

		    [ -n "$in_anon_upload" ] &&
			shell_config_set "$MAIN_CONFIG" "anon_upload_enable" "$(echo "$in_anon_upload"|from_scm)"

		    if test_bool "$in_anon_upload_dir"; then
			mkdir -p "$UPLOAD_DIR" >&2
			chgrp vsftpd "$UPLOAD_DIR" >&2
			chmod 02775 "$UPLOAD_DIR"
		    else
			rmdir "$UPLOAD_DIR" >/dev/null 2>/dev/null
		    fi

		    [ -n "$in_anon_other" ] &&
			    shell_config_set "$MAIN_CONFIG" "anon_other_write_enable" "$(echo "$in_anon_other"|from_scm)"

		    [ -n "$in_write_state" ] &&
			    shell_config_set "$MAIN_CONFIG" "write_enable" "$(echo "$in_write_state"|from_scm)"

		    [ -n "$in_local_state" ] &&
			    shell_config_set "$MAIN_CONFIG" "local_enable" "$(echo "$in_local_state"|from_scm)"

		    if test_bool "$in_local_detailed";then
			    shell_config_set "$MAIN_CONFIG" "user_config_dir" "$USER_CONFIG_DIR"
		    else
			    shell_config_del "$MAIN_CONFIG" "user_config_dir"
		    fi

		    if test_bool "$in_service_state";then
		        /sbin/chkconfig vsftpd on
		        /sbin/service xinetd start >&2 #chkconfig can stop xinetd
		    else
		        /sbin/chkconfig vsftpd off
		    fi
		    /sbin/service xinetd reload >&2 #chkconfig can stop xinetd
		    write_nop
		    ;;
		delete)
		    [ "$in__objects" == "/" ] || rm -f "$(USER_CONFIG "${in__objects##*/}")"
		    write_nop
		    ;;
		enable)
		    shell_config_set "$(USER_CONFIG "${in__objects##*/}")" "write_enable" "YES"
		    write_nop
		    ;;
		disable)
		    shell_config_set "$(USER_CONFIG "${in__objects##*/}")" "write_enable" "NO"
		    write_nop
		    ;;
		new)
		    local path="$(USER_CONFIG "$in_user")"
		    if [ -f "$path" ];then
			write_error "`_ "Same user already exists"`"
		    else
		        shell_config_set "$path" "write_enable" "YES"
			write_nop
		    fi
		    ;;
		*)
		    echo '#f'
		    ;;
	esac
}

message_loop
