From 9a7f06b3b452bee332d73ea9d940c4d867789ad2 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Fri, 29 Dec 2023 17:44:35 +0000 Subject: Add user account --- gohan.sh | 160 ++++++++++++++++------------------------------------------- packages.csv | 1 + 2 files changed, 45 insertions(+), 116 deletions(-) diff --git a/gohan.sh b/gohan.sh index 91f48b8..493fed7 100755 --- a/gohan.sh +++ b/gohan.sh @@ -6,143 +6,71 @@ ### OPTIONS AND VARIABLES ### -while getopts ":p:h" o; do case "${o}" in - h) printf "Optional arguments for custom use:\\n -p: Dependencies and programs csv (local file or url)\\n -h: Show this message\\n" && exit ;; - p) packages=${OPTARG} ;; - *) printf "Invalid option: -%s\\n" "$OPTARG" && exit ;; -esac done +### FUNCTIONS ### -[ -z "$packages" ] && packages="https://git.davidtsadler.com/gohan/plain/packages.csv" +read_password() { + PASSWORD="$( + # always read from the tty even when redirected: + exec < /dev/tty || exit # || exit only needed for bash -### FUNCTIONS ### + # save current tty settings: + tty_settings=$(stty -g) || exit -install_from_pacman() { - pacman --noconfirm --needed -S "$1" >/dev/null 2>&1 -} + # schedule restore of the settings on exit of that subshell + # or on receiving SIGINT or SIGTERM: + trap 'stty "$tty_settings"' EXIT INT TERM -error() { - clear - printf "ERROR:\\n%s\\n" "$1" - exit -} + # disable terminal local echo + stty -echo || exit + + # prompt on tty + printf "$1" > /dev/tty -welcome_msg() { - dialog --title "Welcome!" --msgbox "Welcome to my Arch Linux installation script!\\n\\nThis script will automatically install a fully-featured Linux desktop, which I use as my main machine.\\n\\n-David" 10 60 - dialog --colors --title "Important Note!" --yes-label "All ready!" --no-label "Return..." --yesno "Be sure the computer you are using has current pacman updates.\\n\\nIf it does not, the installation of some programs might fail." 8 70 + # read password as one line, record exit status + IFS= read -r password; ret=$? + + # display a newline to visually acknowledge the entered password + echo > /dev/tty + + # return the password for $REPLY + printf '%s\n' "$password" + exit "$ret" + )" } get_username_and_password() { - # Prompts user for new username and password. - name=$(dialog --inputbox "First, please enter a name for the user account." 10 60 3>&1 1>&2 2>&3 3>&1) || exit + printf "Enter a name for the user account: " + read name while ! echo "$name" | grep "^[a-z_][a-z0-9_-]*$" >/dev/null 2>&1; do - name=$(dialog --no-cancel --inputbox "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _." 10 60 3>&1 1>&2 2>&3 3>&1) + unset name + printf "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _.\n\n" + printf "Enter a name for the user account: " + read name done - pass1=$(dialog --no-cancel --passwordbox "Enter a password for that user." 10 60 3>&1 1>&2 2>&3 3>&1) - pass2=$(dialog --no-cancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1) + read_password "Enter a password for that user: " + pass1=$PASSWORD + read_password "Retype password: " + pass2=$PASSWORD while ! [ "$pass1" = "$pass2" ]; do unset pass2 - pass1=$(dialog --no-cancel --passwordbox "Passwords do not match.\\n\\nEnter password again." 10 60 3>&1 1>&2 2>&3 3>&1) - pass2=$(dialog --no-cancel --passwordbox "Retype password." 10 60 3>&1 1>&2 2>&3 3>&1) + echo "Passwords do not match." + read_password "Enter a password again: " + pass1=$PASSWORD + read_password "Retype password: " + pass2=$PASSWORD done } -user_check() { - ! (id -u "$name" >/dev/null) 2>&1 || dialog --colors --title "WARNING!" --yes-label "CONTINUE" --no-label "No wait..." --yesno "The user \`$name\` already exists on this system. GOHAN can install for a user already existing, but it will \\Zboverwrite\\Zn any conflicting settings/dotfiles on the user account.\\n\\nGOHAN will \\Zbnot\\Zn overwrite your user files, documents, videos, etc., so don't worry about that, but only click if you don't mind your settings being overwritten.\\n\\nNote also that GOHAN will change $name's password to the one you just gave." 14 70 -} - -preinstall_msg() { - dialog --title "Last chance!" --yes-label "Let's go!" --no-label "No, nevermind!" --yesno "Just press and the system will begin installation!" 13 60 || { clear; exit; } -} - add_user() { - # Adds user `$name` with password $pass1. - dialog --infobox "Adding user \"$name\"..." 4 50 + get_username_and_password + [ -d "/home/$name" ] && echo "Skipping adding of user as $name already exists" && return + useradd -m -s /bin/zsh "$name" >/dev/null 2>&1 || mkdir -p /home/"$name" && chown "$name":"$name" /home/"$name" usermod -aG wheel "$name" - [ ! -d "/home/$name/.local/src" ] && mkdir -p "/home/$name/.local/src" && chown -R "$name":"$name" /home/"$name/.local" - # Remove these files as they will be installed as part of the dotfiles. -# [ -f "/home/$name/.bash_history" ] && rm /home/$name/.bash_history -# [ -f "/home/$name/.bash_logout" ] && rm /home/$name/.bash_logout -# [ -f "/home/$name/.bash_profile" ] && rm /home/$name/.bash_profile -# [ -f "/home/$name/.bashrc" ] && rm /home/$name/.bashrc echo "$name:$pass1" | chpasswd unset pass1 pass2 } -install_packages() { - ([ -f "$packages" ] && cp "$packages" /tmp/packages.csv) || curl -Ls "$packages" > /tmp/packages.csv - total=$(wc -l < /tmp/packages.csv) - while IFS=, read -r tag program comment; do - n=$((n+1)) - echo "$comment" | grep "^\".*\"$" >/dev/null 2>&1 && comment="$(echo "$comment" | sed "s/\(^\"\|\"$\)//g")" - case "$tag" in - "A") install_from_aur "$program" "$comment" ;; - "G") install_from_github "$program" "$comment" ;; - *) install_package "$program" "$comment" ;; - esac - done < /tmp/packages.csv -} - -install_package() { - dialog --title "GOHAN Installation" --infobox "Installing \`$1\` ($n of $total). $1 $2" 5 70 - install_from_pacman "$1" -} - -install_from_aur() { - dialog --title "GOHAN Installation" --infobox "Installing \`$1\` ($n of $total) from the AUR. $1 $2" 5 70 - sudo -u "$name" yay -S --noconfirm "$1" >/dev/null 2>&1 -} - -install_from_github() { - progname="$(basename "$1" .git)" - repodir="/home/$name/.local/src" - dir="$repodir/$progname" - dialog --title "GOHAN Installation" --infobox "Installing \`$progname\` ($n of $total) via \`git\` and \`make\`. $(basename "$1") $2" 5 70 - sudo -u "$name" git clone --depth 1 "$1" "$dir" >/dev/null 2>&1 - cd "$dir" || exit - sudo -u "$name" make clean install >/dev/null 2>&1 -} - -configure_sudo() { - install_from_pacman sudo - [ -f /etc/sudoers ] && sed -i "s/# %wheel ALL=(ALL) NOPASSWD: ALL/%wheel ALL=(ALL) NOPASSWD: ALL/" /etc/sudoers -} - -install_yay() { - [ -f "/usr/bin/yay" ] || ( - dialog --infobox "Installing \"yay\", an AUR helper..." 4 50 - install_from_pacman base-devel - install_from_pacman git - # Use all cores for compilation. - sed -i "s/-j2/-j$(nproc)/;s/^#MAKEFLAGS/MAKEFLAGS/" /etc/makepkg.conf - cd /tmp || exit - rm -rf /tmp/yay* - git clone "https://aur.archlinux.org/yay.git" - cd yay - makepkg --noconfirm -si >/dev/null 2>&1 - cd /tmp - rm -rf yay - ); -} - ### THE ACTUAL SCRIPT ### -install_from_pacman dialog || error "Are you sure you're running this as the root user and have an internet connection?" - -welcome_msg || error "User exited." - -get_username_and_password || error "User exited." - -user_check || error "User exited." - -preinstall_msg || error "User exited." - -add_user || error "Error adding username and/or password." - -configure_sudo - -install_yay - -install_packages - -clear +add_user diff --git a/packages.csv b/packages.csv index 7af7b66..f347855 100644 --- a/packages.csv +++ b/packages.csv @@ -1,3 +1,4 @@ #Tag, Name, Description ,base-devel,contains tools to compile software ,git,is needed to install software from git repositories +,zsh,is a shell -- cgit v1.2.3-13-gbd6f