summaryrefslogtreecommitdiff
path: root/deleteuser.sh
blob: d4a3841b6c1234c888f3c5b80ffac7bffa41b051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash

# Check if username is provided
if [ -z "$1" ]; then
    echo "Usage: $0 <username>"
    exit 1
fi

USERNAME="$1"
JAIL_DIR="/home/publicaccess"
USER_HOME="$JAIL_DIR/home/$USERNAME"
PASSWD_FILE="$JAIL_DIR/etc/passwd"

# Check if the user exists on the system
if ! id "$USERNAME" >/dev/null 2>&1; then
    echo "User '$USERNAME' does not exist on the host system."
else
    echo "Removing user '$USERNAME' from the system..."
    sudo userdel -r "$USERNAME"
fi

# Remove user from jail passwd file if present
if [ -f "$PASSWD_FILE" ]; then
    sudo sed -i "/^$USERNAME:/d" "$PASSWD_FILE"
fi

# Delete the user's jailed home directory
if [ -d "$USER_HOME" ]; then
    echo "Deleting jailed home directory: $USER_HOME"
    sudo rm -rf "$USER_HOME"
fi

# Remove from group file if applicable
GROUP_FILE="$JAIL_DIR/etc/group"
if [ -f "$GROUP_FILE" ]; then
    sudo sed -i "/^$USERNAME:/d" "$GROUP_FILE"
fi

echo "User '$USERNAME' deleted from system and jail (if present)."
exit 0