vhost-stack/bin/vhost-user-del.sh

101 lines
2.6 KiB
Bash
Raw Normal View History

2021-04-04 13:28:22 -07:00
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
2021-04-04 14:15:16 -07:00
# load include file
source $(dirname $0)/vhost.sh
2021-04-04 13:28:22 -07:00
2021-09-16 16:21:35 -07:00
help()
{
thisfilename=$(basename -- "$0")
echo "Remove user from this server."
echo ""
2021-10-05 11:33:24 -07:00
echo "usage: $thisfilename -u <username> [-h]"
2021-09-16 16:21:35 -07:00
echo ""
echo " -h Print this help."
2021-10-05 11:33:24 -07:00
echo " -u <username> System username to remove from server."
2021-09-16 16:21:35 -07:00
exit
}
2021-10-05 11:33:24 -07:00
vhost:getoptions "$@"
# check for username
if [ -z "$username" ]; then
2021-04-04 13:28:22 -07:00
echo "username not set"
exit 1
fi
if ! /bin/grep -q "^$username:" /etc/passwd; then
echo user \"$username\" does not exist
exit 1
fi
vhost::set-virtualhostArray
for v in "${virtualhostArray[@]}"
do
if [ $(stat -c '%U' /srv/www/$v) = $username ]; then
echo "$username has one or more installed virtualhosts ($v)"
exit 1
fi
done
# check for jailed vhost mount
if /bin/grep -q "^/dev/sda /usr/jails/$username/srv/www/" /etc/mtab; then
echo user \"$username\" has one or more jailed vhosts mounted
exit 1
fi
2021-08-18 16:13:02 -07:00
# check for virtualhost jail mount(s) in fstab.jails
if /bin/grep -q " /usr/jails/$username/srv/www/" /etc/fstab.jails; then
2021-04-04 13:28:22 -07:00
echo user \"$username\" has one or more jailed vhost mounts
exit 1
fi
# checks complete, start removing stuff
# check for php-fpm pool conf
vhost::set-phpVersion
if [[ -f "/etc/php/$phpVersion/fpm/pool.d/$username.conf" ]]; then
rm /etc/php/$phpVersion/fpm/pool.d/$username.conf
# restart php$phpVersion-fpm if it's running
if systemctl is-active --quiet php$phpVersion-fpm ; then
if /usr/sbin/php-fpm$phpVersion -t >/dev/null 2>&1 ; then
systemctl reload php$phpVersion-fpm
else
echo "WARNING: php-fpm$phpVersion configuration test failed"
fi
fi
fi
# if users home dir is mounted in a jail, unmount it
if grep -q "^/dev/sda /usr/jails/$username/home/$username " /etc/mtab; then
umount /usr/jails/$username/home/$username
fi
2021-08-18 16:13:02 -07:00
# if user home dir mount in fstab.jails exists remove it
if grep -q "^/home/$username /usr/jails/$username/home/$username " /etc/fstab.jails; then
sed -i "\|/home/$username /usr/jails/$username/home/$username|d" /etc/fstab.jails
2021-04-04 13:28:22 -07:00
fi
# delete user
/usr/sbin/userdel $username
# delete users home dir if it still exists
if [[ -d "/home/$username" ]]; then
rm -r /home/$username
fi
# delete users jail dir if it exists
if [[ -d "/usr/jails/$username" ]]; then
rm -r /usr/jails/$username
fi
# remove jailkit socket if it exists
if grep -q "\[/usr/jails/$username/dev/log\]" /etc/jailkit/jk_socketd.ini; then
sed -i '/\/usr\/jails\/$username\/dev\/log/,+3 d' /etc/jailkit/jk_socketd.ini
killall jk_socketd
jk_socketd
fi