smb_utilities/functions/remove_smb_user.sh
2025-05-25 16:28:24 +00:00

68 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# Usage: ./remove_samba_user.sh <username>
# Check for root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
####################################################################
# Parse username
####################################################################
USERNAME="$1"
if [[ -z "$USERNAME" ]]; then
read -p "Enter username to remove: " USERNAME
fi
####################################################################
# Remove shares
# All the shares associated to the user are removed
####################################################################
bash $SMB_TOOLS_PATH/functions/list_smb_shares.sh $USERNAME | while IFS= read -r line; do
# Skip empty lines
[ -z "$line" ] && continue
bash $SMB_TOOLS_PATH/functions/remove_share.sh "$line" "$USERNAME"
done
# ####################################################################
# # Remove samba user
# ####################################################################
if pdbedit -L | grep -q "^$USERNAME:"; then
smbpasswd -x "$USERNAME"
echo "Samba user $USERNAME removed."
else
echo "Samba user $USERNAME does not exist."
fi
# ####################################################################
# # Remove system user
# ####################################################################
if id "$USERNAME" &>/dev/null; then
userdel -r "$USERNAME"
echo "System user $USERNAME removed (including home directory)."
else
echo "System user $USERNAME does not exist."
fi
# ####################################################################
# # Remove filebrowser
# ####################################################################
log_file="$SMB_TOOLS_PATH/filebrowser/removeusers.txt"
if [ -f "$log_file" ]; then
echo "$USERNAME" >> "$log_file"
fi
# Non-blocking docker restart
docker restart filebrowser </dev/null &>/dev/null &