Skip to content

1

Script for creating users, groups, and adding them to Samba

#!/bin/bash

# ============================
#  FUNCTIONS
# ============================

repeat_until_no_empty() {
    local prompt="$1"
    local readed

    while true; do
        read -p "$prompt" readed
        if [[ -z "$readed" ]]; then
            echo "Input cannot be empty!" >&2
        else
            echo "$readed"
            return
        fi
    done
}

display_items() {
    local file="$1"
    local label="$2"

    read -p "Filter by name (y/n): " confirmation
    if [[ "$confirmation" =~ ^[yY]$ ]]; then
        name=$(repeat_until_no_empty "Enter filter: ")
        result=$(grep "^$name" "$file" | cut -d: -f1 | tr '\n' ' ' || true)
    else
        result=$(cut -d: -f1 "$file" | tr '\n' ' ' || true)
    fi

    [[ -n "${result// }" ]] && echo "$result" || echo "$label not found!"
}

display_groups() {
    display_items "/etc/group" "Groups"
}

display_users() {
    display_items "/etc/passwd" "Users"
}

add_group() {
    group_name=$(repeat_until_no_empty "Enter new group name: ")

    if getent group "$group_name" > /dev/null; then
        echo "Group '$group_name' already exists!"
        return
    fi

    if groupadd "$group_name" 2>/dev/null; then
        echo "Group '$group_name' has been created!"
    else
        echo "Failed to create group '$group_name'"
        return
    fi

    read -p "Do you want to create a group directory? (y/n): " create_dir
    if [[ "$create_dir" =~ ^[yY]$ ]]; then
        dir_name=$(repeat_until_no_empty "Directory name: ")

        path=$(repeat_until_no_empty "Path for this directory: ")

        full_path="$path/$dir_name"

        if mkdir -p "$full_path"; then
            echo "Directory created at: $full_path"
        else
            echo "Failed to create directory!"
            return
        fi

        read -p "Should it be shared in Samba? (y/n): " samba
        if [[ "$samba" =~ ^[yY]$ ]]; then
            chown -R root:"$group_name" "$full_path"
            chmod 770 "$full_path"
            setfacl -R -m d:g:"$group_name":rwx "$full_path"
            echo "Permissions and ACL configured for Samba"
        fi
    fi
}

add_user() {
    user_login=$(repeat_until_no_empty "Enter new user login: ")

    if cut -d: -f1 /etc/passwd | grep -Fx "$user_login" > /dev/null; then
        echo "User login '$user_login' already exists!"
        return
    fi

    if useradd "$user_login"; then
        echo "User '$user_login' created."
    else
        echo "Failed to create user '$user_login'."
        return
    fi

    read -p "Should it be sudo/wheel account? (y/n): " sudo_account
    if [[ $sudo_account =~ ^[yY]$ ]]; then
        if getent group sudo > /dev/null; then 
            admin_group="sudo" 
        elif getent group wheel > /dev/null; then 
            admin_group="wheel" 
        else 
            echo "No admin group found (sudo/wheel)!" 
            return 
        fi

        usermod -aG "$admin_group" "$user_login"
        echo "Added '$user_login' to '$admin_group' group."
    fi

    read -p "Want to set password? (y/n):" set_password
    if [[ $set_password =~ ^[yY]$ ]]; then

        password=$(repeat_until_no_empty "Write password: ")
        echo "$user_login:$password" | chpasswd
        echo "Password set for '$user_login'."
    fi

    read -p "Should add account to other group? (y/n): " other_account
    if [[ $other_account =~ ^[yY]$ ]]; then

        echo "Available groups:"
        cut -d: -f1 /etc/group | tr '\n' ' '
        echo

        read -p "Write group name: " group_name
        if getent group "$group_name" > /dev/null; then 
            usermod -aG "$group_name" "$user_login" 
            echo "Added '$user_login' to group '$group_name'." 
        else 
            echo "Group '$group_name' does not exist!" 
        fi
    fi
}


# ============================
#  MAIN MENU
# ============================
if [[ $EUID -ne 0 ]]; then
    echo "This script must be run with administrative privileges (sudo)!"
    exit 1
fi

while true; do
    cat <<EOF

# ============================
#  MAIN MENU
# ============================
    Available options:
    1) Display groups
    2) Display users
    3) Add new group
    4) Add new user
    0) Exit
EOF

    read -p "Choose option (default=0): " selected_option
    selected_option="${selected_option:-0}"

    case "$selected_option" in
        1) display_groups ;;
        2) display_users ;;
        3) add_group ;;
        4) add_user ;;
        0) exit 0 ;;
        *) echo "Unknown option" ;;
    esac
done