vhost-stack/bin/vhost-user-del.sh
2024-09-18 12:35:55 -07:00

97 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# load include file
source $(dirname $0)/vhost.sh
help()
{
thisfilename=$(basename -- "$0")
echo "Remove user from this server."
echo ""
echo "usage: $thisfilename -u <username> [-h]"
echo ""
echo " -h Print this help."
echo " -u <username> System username to remove from server."
exit
}
vhost:getoptions "$@"
# check for username
if [ -z "$username" ]; then
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 " /usr/jails/$username/srv/www/" /etc/mtab; then
echo user \"$username\" has one or more jailed vhosts mounted
exit 1
fi
# check for virtualhost jail mount(s) in fstab.jails
if /bin/grep -q " /usr/jails/$username/srv/www/" /etc/fstab.jails; then
echo user \"$username\" has one or more jailed vhost mounts
exit 1
fi
# checks complete, start removing stuff
# check for php-fpm pool confs
vhost::set-phpVersionArray
for phpVersion in "${phpVersionArray[@]}"
do
if [[ -f "/etc/php/$phpVersion/fpm/pool.d/$username.conf" ]]; then
rm /etc/php/$phpVersion/fpm/pool.d/$username.conf
fi
done
# if users home dir is mounted in a jail, unmount it
if grep -q " /usr/jails/$username/home/$username " /etc/mtab; then
umount /usr/jails/$username/home/$username
fi
# 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
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
# remove trailing empty line from file, if it exists
sed -i '${/^$/d;}' /etc/jailkit/jk_socketd.ini
fi