Made ftpadmin more usable from the commandline

Adds more portable bash invocation.

Adds more severe bash evaluation.

Added h flag and made improper use print the usage function.

Added checks that make sure the parameters are okay.

Proper variable quoting, tests and $() use.
This commit is contained in:
Patrick Marchand 2018-11-09 17:45:01 -05:00
parent 2ab5a60906
commit 58642ec0ac

View file

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
############################################################ ############################################################
# # # #
@ -13,6 +13,11 @@
# vim: expandtab softtabstop=4 tabstop=4 shiftwidth=4 showtabline=2 # vim: expandtab softtabstop=4 tabstop=4 shiftwidth=4 showtabline=2
set -o errexit
set -o pipefail
set -o nounset
#set -x
VPASSWD_PATH="/etc/proftpd/vpasswd" VPASSWD_PATH="/etc/proftpd/vpasswd"
FTPLOG_PATH="/var/log/evolix-ftp.log" FTPLOG_PATH="/var/log/evolix-ftp.log"
@ -118,8 +123,7 @@ delete_account() {
log_msg "Suppression du compte $account_name" log_msg "Suppression du compte $account_name"
} }
while getopts ha:u:n:f:p: opt; do
while getopts a:u:n:f:p: opt; do
case "$opt" in case "$opt" in
a) a)
in_action=$OPTARG in_action=$OPTARG
@ -136,26 +140,67 @@ while getopts a:u:n:f:p: opt; do
p) p)
in_password=$OPTARG in_password=$OPTARG
;; ;;
h)
usage
exit 1
;;
*)
usage
exit 1
;;
esac esac
done done
case "$in_action" in case "${in_action-}" in
l) l)
account_list=`list_accounts_by_UID $in_userid` echo -e "$(list_accounts_by_UID "${in_userid-}")"
echo -e -n $account_list
exit 1 exit 1
;; ;;
a) a)
echo -e -n `add_account $in_userid $in_accountname $in_workpath $in_password` if [[ -z "${in_userid-}" ]]; then
echo "User ID not specified"
elif [[ $in_userid = *[!0-9]* ]]; then
echo "User ID must be a non negative integer"
elif [[ -z "${in_accountname-}" ]]; then
echo "Account name not specified"
elif [[ -z "${in_workpath-}" ]]; then
echo "A directory was not specified"
elif [[ -z "${in_password-}" ]]; then
echo "A password was not specified"
else
echo -e -n \
"$(add_account \
"$in_userid" \
"$in_accountname" \
"$in_workpath" \
"$in_password")"
fi
exit 1 exit 1
;; ;;
m) m)
echo -e -n `edit_password $in_accountname $in_password` if [[ -z "${in_accountname-}" ]]; then
echo "Account name not specified"
elif [[ -z "${in_password-}" ]]; then
echo "A password was not specified"
else
echo -e -n \
"$(edit_password \
"$in_accountname" \
"$in_password")"
fi
exit 1; exit 1;
;; ;;
d) d)
echo -e -n `delete_account $in_accountname` if [[ -z "${in_accountname-}" ]]; then
echo "Account name not specified"
else
echo -e -n \
"$(delete_account "$in_accountname")"
fi
exit 1; exit 1;
;; ;;
*)
usage
exit 1
;;
esac esac