#!/bin/sh

alterator_api_version=1

### 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"`,"
}


### high level API

## main ftp settings

read_settings()
{
    ! 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 "$?"
}

write_settings()
{
    [ -z "$in_anon_state" ] ||
	shell_config_set "$MAIN_CONFIG" "anonymous_enable" "$(echo "$in_anon_state"|from_scm)"

    [ -z "$in_anon_mkdir" ] ||
	shell_config_set "$MAIN_CONFIG" "anon_mkdir_write_enable" "$(echo "$in_anon_mkdir"|from_scm)"

    [ -z "$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

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

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

    [ -z "$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/chkconfig xinetd on
	/sbin/service xinetd start >&2 #chkconfig can stop xinetd
    else
	/sbin/chkconfig vsftpd off
	/sbin/service xinetd reload >&2 #chkconfig can stop xinetd
    fi
}

## users

enable_user()
{
    local IFS=';'

    [ -z "$in_user" ] ||
	for i in $in_user;do
	    shell_config_set "$(USER_CONFIG "$i")" "write_enable" "YES"
	done
}

disable_user()
{
    local IFS=';'

    [ -z "$in_user" ] ||
	for i in $in_user;do
	    shell_config_set "$(USER_CONFIG "$i")" "write_enable" "NO"
	done
}

add_user()
{
    [ -n "$in_new_user" ] || return 0

    local path="$(USER_CONFIG "$in_new_user")"
    if [ -f "$path" ];then
	write_error "`_ "Same user already exists"`"
    else
	shell_config_set "$path" "write_enable" "YES"
    fi
}

delete_user()
{
    local IFS=';'

    [ -z "$in_user" ] ||
	for i in $in_user;do
	    rm -f -- "$(USER_CONFIG "$i")"
	done
}

list_user()
{
    find "$USER_CONFIG_DIR" -mindepth 1 -maxdepth 1 -type f |
	while read name;do
	    local name=${name##*/}
	    write_table_item \
		name "$name" \
		user_state "$(read_param "$(USER_CONFIG "$name")" "write_enable"|to_string)" #"
	done
}

list_avail_user()
{
    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"
}

list_action()
{
    write_enum_item "enable_user" "`_ "enable write access"`"
    write_enum_item "disable_user" "`_ "disable write access"`"
    write_enum_item "delete_user" "`_ "remove from list"`"
}

alterator_export_proc enable_user
alterator_export_proc disable_user
alterator_export_proc add_user
alterator_export_proc delete_user
alterator_export_proc list_user
alterator_export_proc list_avail_user
alterator_export_proc list_action

alterator_export_proc read_settings
alterator_export_proc write_settings

message_loop
