From a25d98fdeb03ccf9b33e5607fd0d43546481bae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Barbosa?= Date: Sun, 7 Sep 2025 01:25:34 +0100 Subject: [PATCH] Enhance user prompts in setup script for existing user handling during additional user creation. Update SSH key setup process to reflect selected users, improving clarity in user feedback and logging. Streamline checks for user existence and adjust logging for SSH key management. --- setup.sh | 55 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/setup.sh b/setup.sh index bb50722..948d53b 100755 --- a/setup.sh +++ b/setup.sh @@ -90,8 +90,16 @@ ask_additional_user() { echo -e "${RED}Username cannot be the same as sysadmin user. Please choose a different name.${NC}" continue elif id "$username_input" &>/dev/null; then - echo -e "${RED}User '$username_input' already exists. Please choose a different name.${NC}" - continue + echo -e "${YELLOW}User '$username_input' already exists.${NC}" + read -p "Continue with existing user '$username_input'? (y/N): " continue_existing + if [[ "$continue_existing" =~ ^[Yy]$ ]]; then + ADDITIONAL_USER="$username_input" + log "Will use existing user: $ADDITIONAL_USER" + break + else + echo "Please choose a different name." + continue + fi else ADDITIONAL_USER="$username_input" log "Will create additional user: $ADDITIONAL_USER" @@ -138,28 +146,37 @@ ask_firewall_preferences() { # Ask user about SSH key setup for created users ask_ssh_key_setup() { - # Check if any users were created - local users_created=() + # Check if any users were created or selected + local users_for_ssh=() if [[ "$SYSADMIN_USER_CREATED" == "yes" ]]; then - users_created+=("$SYSADMIN_USER") + users_for_ssh+=("$SYSADMIN_USER") fi - if [[ "$ADDITIONAL_USER_CREATED" == "yes" ]]; then - users_created+=("$ADDITIONAL_USER") + if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then + users_for_ssh+=("$ADDITIONAL_USER") fi - # Only prompt if users were actually created - if [[ ${#users_created[@]} -eq 0 ]]; then + # Only prompt if users were created or selected + if [[ ${#users_for_ssh[@]} -eq 0 ]]; then return fi echo "" echo -e "${BLUE}=== SSH Key Setup ===${NC}" - echo "Users created during setup: ${users_created[*]}" + if [[ "$SYSADMIN_USER_CREATED" == "yes" ]]; then + echo "Users created during setup: $SYSADMIN_USER" + fi + if [[ "$CREATE_ADDITIONAL_USER" == "yes" && -n "$ADDITIONAL_USER" ]]; then + if [[ "$ADDITIONAL_USER_CREATED" == "yes" ]]; then + echo "Additional user created: $ADDITIONAL_USER" + else + echo "Additional user selected: $ADDITIONAL_USER (existing)" + fi + fi echo "You can add your SSH public key for easier login." echo "" - read -p "Add your SSH public key to created users? (Y/n): " add_key_choice + read -p "Add your SSH public key to selected users? (Y/n): " add_key_choice if [[ "$add_key_choice" =~ ^[Nn]$ ]]; then log "SSH key setup skipped" @@ -169,9 +186,9 @@ ask_ssh_key_setup() { local user_public_key="" # Special case for user "sergio" - offer pre-defined key - if [[ " ${users_created[*]} " =~ " sergio " ]]; then + if [[ " ${users_for_ssh[*]} " =~ " sergio " ]]; then echo "" - echo "Detected user 'sergio' was created." + echo "Detected user 'sergio' in selected users." echo "Use pre-configured SSH key for sergio? (Y/n)" echo "Key: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINBYyuGSa2wswiiObp2qj30MoiNRyFdBIBciFSbtrkZ8 mbpm1" echo "" @@ -205,15 +222,15 @@ ask_ssh_key_setup() { done fi - # Add key to all created users and generate SSH keys for them - for username in "${users_created[@]}"; do + # Add key to all selected users and generate SSH keys for them + for username in "${users_for_ssh[@]}"; do setup_ssh_key_for_user "$username" "$user_public_key" generate_ssh_key_for_user "$username" done echo "" - log "SSH key added to: ${users_created[*]}" - log "SSH keys generated for: ${users_created[*]}" + log "SSH key added to: ${users_for_ssh[*]}" + log "SSH keys generated for: ${users_for_ssh[*]}" } setup_ssh_key_for_user() { @@ -327,7 +344,7 @@ create_additional_user() { log "Creating additional user: $ADDITIONAL_USER" if id "$ADDITIONAL_USER" &>/dev/null; then - warn "User $ADDITIONAL_USER already exists, skipping creation" + log "Using existing user: $ADDITIONAL_USER" ADDITIONAL_USER_CREATED="no" else # Create user with home directory @@ -340,7 +357,7 @@ create_additional_user() { passwd "$ADDITIONAL_USER" fi - # Add additional user to sudo group + # Add additional user to sudo group (existing or newly created) log "Adding $ADDITIONAL_USER to sudo group..." usermod -aG sudo "$ADDITIONAL_USER" fi