2018-11-09 23:45:01 +01:00
|
|
|
|
#!/usr/bin/env bash
|
2015-08-25 16:54:13 +02:00
|
|
|
|
|
|
|
|
|
############################################################
|
|
|
|
|
# #
|
|
|
|
|
# EvoAdmin : gestion des comptes FTP virtuels de ProFTPd #
|
|
|
|
|
# #
|
|
|
|
|
# Copyright (c) 2009 Evolix - Tous droits reserves #
|
|
|
|
|
# #
|
|
|
|
|
# @author Sebastien Palma <spalma@evolix.fr> #
|
|
|
|
|
# @version 1.0 #
|
|
|
|
|
# #
|
|
|
|
|
############################################################
|
|
|
|
|
|
|
|
|
|
# vim: expandtab softtabstop=4 tabstop=4 shiftwidth=4 showtabline=2
|
|
|
|
|
|
2018-11-09 23:45:01 +01:00
|
|
|
|
set -o errexit
|
|
|
|
|
set -o pipefail
|
|
|
|
|
set -o nounset
|
|
|
|
|
#set -x
|
|
|
|
|
|
2015-08-25 16:54:13 +02:00
|
|
|
|
VPASSWD_PATH="/etc/proftpd/vpasswd"
|
|
|
|
|
FTPLOG_PATH="/var/log/evolix-ftp.log"
|
|
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
|
|
|
|
|
|
cat <<EOT >&2
|
|
|
|
|
|
|
|
|
|
Usage: $0 [ACTION UID [PARAMS]]
|
|
|
|
|
|
|
|
|
|
Manage ProFTPd virtual accounts.
|
|
|
|
|
|
|
|
|
|
-a ACTION
|
|
|
|
|
l (list), a (add), m (modify), d (delete) ProFTPd virtual account(s)
|
|
|
|
|
|
|
|
|
|
-u UID
|
|
|
|
|
UID of the real account the virtual FTP accounts will be related to
|
|
|
|
|
|
|
|
|
|
Available PARAMS are :
|
|
|
|
|
|
|
|
|
|
-n ACCOUNTNAME
|
|
|
|
|
Name of the ProFTPd virtual account you want to add/edit/delete.
|
|
|
|
|
Mandatory in all actions.
|
|
|
|
|
|
|
|
|
|
-f FOLDER
|
|
|
|
|
Directory that the virtual account will have as home directory.
|
|
|
|
|
Mandatory for add and modify action.
|
|
|
|
|
|
|
|
|
|
-p PASSWD
|
|
|
|
|
Password for the virtual account.
|
|
|
|
|
Mandatory for add and modify action.
|
|
|
|
|
|
|
|
|
|
EOT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_msg() {
|
2018-11-07 23:25:12 +01:00
|
|
|
|
curdate="$(date +"%Y/%m/%d %H:%M:%S")"
|
|
|
|
|
echo "$curdate $1" >> "$FTPLOG_PATH"
|
2015-08-25 16:54:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get_user_login_by_UID() {
|
|
|
|
|
uid=$1
|
2021-12-23 15:51:55 +01:00
|
|
|
|
grep "$uid" /etc/passwd | awk -F : '{if ($3==$uid) print $1}'
|
2015-08-25 16:54:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list_accounts_by_UID() {
|
2021-12-23 15:51:55 +01:00
|
|
|
|
uid=$1
|
|
|
|
|
|
|
|
|
|
# Remove # and empty lines
|
|
|
|
|
ftp_users=$(grep -v -E '^[[:blank:]]*(#.*)*$' "$VPASSWD_PATH")
|
|
|
|
|
|
|
|
|
|
for line in $ftp_users; do
|
|
|
|
|
line_uid="$(echo "$line" | cut -d":" -f3)"
|
|
|
|
|
|
|
|
|
|
if [[ ! "$uid" ]] || [[ "$line_uid" == "$uid" ]]; then
|
|
|
|
|
username=$(get_user_login_by_UID "$line_uid")
|
|
|
|
|
account=$(echo "$line" | cut -d":" -f1)
|
|
|
|
|
path=$(echo "$line" | cut -d":" -f6)
|
|
|
|
|
size="inconnue"
|
|
|
|
|
# Check output of daily "du" cron job
|
|
|
|
|
# (set by ansible-roles/packweb-apache/tasks/main.yml)
|
|
|
|
|
if [ -r "$path/.size" ]; then
|
|
|
|
|
size=$(cat "$path/.size")
|
|
|
|
|
fi
|
|
|
|
|
#modif="$(cat $path/.lastmodified)"
|
|
|
|
|
echo "$username:$account:$path:$size${modif:+:$modif}"
|
|
|
|
|
fi
|
|
|
|
|
done
|
2015-08-25 16:54:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add_account() {
|
|
|
|
|
user_id=$1
|
|
|
|
|
account_name=$2
|
|
|
|
|
path=$3
|
|
|
|
|
passwd=$4
|
|
|
|
|
|
|
|
|
|
cmd="{if (\$3==$user_id) print \$4}"
|
2018-11-09 23:41:32 +01:00
|
|
|
|
user_gid="$(awk -F : "$cmd" /etc/passwd)"
|
2015-08-25 16:54:13 +02:00
|
|
|
|
|
2018-11-09 23:41:32 +01:00
|
|
|
|
# Si le répertoire de travail du compte FTP n'existe pas, on le crée
|
|
|
|
|
if [[ ! -d "$path" ]]; then
|
|
|
|
|
mkdir -p "$path"
|
|
|
|
|
chown "$user_id":"$user_gid" "$path"
|
2015-08-25 16:54:13 +02:00
|
|
|
|
# fix by tmartin : s/655/755/
|
2018-11-09 23:41:32 +01:00
|
|
|
|
chmod 755 "$path"
|
2015-08-25 16:54:13 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2018-11-09 23:41:32 +01:00
|
|
|
|
echo "$passwd" | ftpasswd --passwd --file=$VPASSWD_PATH --name="$account_name" --uid="$user_id" --gid="$user_gid" --home="$path" --shell=/bin/false --stdin
|
2015-08-25 16:54:13 +02:00
|
|
|
|
log_msg "Creation du compte $account_name (uid=$user_id, gid=$user_gid, home=$path)"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
edit_password() {
|
|
|
|
|
account_name=$1
|
|
|
|
|
passwd=$2
|
|
|
|
|
|
2018-11-09 23:42:47 +01:00
|
|
|
|
echo "$passwd" | ftpasswd --passwd --file="$VPASSWD_PATH" --name="$account_name" --uid=9999 --gid=9999 --home=/dev/null --shell=/dev/null --change-password --stdin
|
2015-08-25 16:54:13 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete_account() {
|
|
|
|
|
|
|
|
|
|
account_name=$1
|
|
|
|
|
|
2018-11-09 23:48:35 +01:00
|
|
|
|
ftpasswd --passwd --file=$VPASSWD_PATH --name="$account_name" --uid=9999 --gid=9999 --home=/dev/null --shell=/dev/null --delete-user
|
2015-08-25 16:54:13 +02:00
|
|
|
|
log_msg "Suppression du compte $account_name"
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 23:45:01 +01:00
|
|
|
|
while getopts ha:u:n:f:p: opt; do
|
2015-08-25 16:54:13 +02:00
|
|
|
|
case "$opt" in
|
|
|
|
|
a)
|
2017-08-20 04:31:00 +02:00
|
|
|
|
in_action=$OPTARG
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
u)
|
2017-08-20 04:31:00 +02:00
|
|
|
|
in_userid=$OPTARG
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
n)
|
2017-08-20 04:31:00 +02:00
|
|
|
|
in_accountname=$OPTARG
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
f)
|
2017-08-20 04:31:00 +02:00
|
|
|
|
in_workpath=$OPTARG
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
p)
|
2017-08-20 04:31:00 +02:00
|
|
|
|
in_password=$OPTARG
|
|
|
|
|
;;
|
2018-11-09 23:45:01 +01:00
|
|
|
|
h)
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
2018-11-09 23:45:01 +01:00
|
|
|
|
case "${in_action-}" in
|
2015-08-25 16:54:13 +02:00
|
|
|
|
l)
|
2018-11-09 23:45:01 +01:00
|
|
|
|
echo -e "$(list_accounts_by_UID "${in_userid-}")"
|
2017-08-20 04:31:00 +02:00
|
|
|
|
exit 1
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
a)
|
2018-11-09 23:45:01 +01:00
|
|
|
|
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
|
2017-08-20 04:31:00 +02:00
|
|
|
|
exit 1
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
m)
|
2018-11-09 23:45:01 +01:00
|
|
|
|
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
|
2017-08-20 04:31:00 +02:00
|
|
|
|
exit 1;
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
d)
|
2018-11-09 23:45:01 +01:00
|
|
|
|
if [[ -z "${in_accountname-}" ]]; then
|
|
|
|
|
echo "Account name not specified"
|
|
|
|
|
else
|
|
|
|
|
echo -e -n \
|
|
|
|
|
"$(delete_account "$in_accountname")"
|
|
|
|
|
fi
|
2017-08-20 04:31:00 +02:00
|
|
|
|
exit 1;
|
|
|
|
|
;;
|
2018-11-09 23:45:01 +01:00
|
|
|
|
*)
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
;;
|
2015-08-25 16:54:13 +02:00
|
|
|
|
esac
|