From 25dd4469a0bcf101556e445048445a1cf888637d Mon Sep 17 00:00:00 2001 From: Marvin Roman Date: Sat, 20 Nov 2021 23:58:34 -0800 Subject: [PATCH 1/4] validate username before using in the script (cherry picked from commit 145a0add3451945096005b3833197c429a1b8b98) --- 1-setup.sh | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/1-setup.sh b/1-setup.sh index ada72c7..725b8fe 100755 --- a/1-setup.sh +++ b/1-setup.sh @@ -84,17 +84,46 @@ fi echo -e "\nDone!\n" if ! source install.conf; then - read -p "Please enter username:" username -echo "username=$username" >> ${HOME}/$SCRIPTHOME/install.conf + # Loop through user input until the user gives a valid username + while true + do + read -p "Please enter username:" username + # username regex per response here https://unix.stackexchange.com/questions/157426/what-is-the-regex-to-validate-linux-users + # lowercase the username to test regex + if [[ "${username,,}" =~ ^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$ ]] + then + break + fi + echo "Incorrect username." + done +# save the username in lowercase form to install.conf +echo "username=${username,,}" >> ${HOME}/${SCRIPTHOME}/install.conf fi if [ $(whoami) = "root" ]; then useradd -m -G wheel,libvirt -s /bin/bash $username passwd $username - cp -R /root/$SCRIPTHOME /home/$username/ - chown -R $username: /home/$username/$SCRIPTHOME - read -p "Please name your machine:" nameofmachine - echo $nameofmachine > /etc/hostname + cp -R ${HOME}/${SCRIPTHOME} /home/${username}/ + chown -R ${username}: /home/${username}/${SCRIPTHOME} + + # Loop through user input until the user gives a valid hostname, but allow the user to force save + while true + do + read -p "Please name your machine:" nameofmachine + # hostname regex (!!couldn't find spec for computer name!!) + if [[ "${nameofmachine,,}" =~ ^[a-z][a-z0-9_-\.]{0,62}[a-z0-9]$ ]] + then + break + fi + # if validation fails allow the user to force saving of the hostname + read -p "Username doesn't seem correct. Do you still want to save it? (y/n)" force + if [[ "${force,,}" = "y" ]] + then + break + fi + done + + echo ${nameofmachine,,} > /etc/hostname else echo "You are already a user proceed with aur installs" fi From 3963aa1476d4f4c454fff2a1ec8ba6b64bd68938 Mon Sep 17 00:00:00 2001 From: Marvin Roman Date: Sun, 21 Nov 2021 00:02:18 -0800 Subject: [PATCH 2/4] remove saving hostname in lowercase --- 1-setup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/1-setup.sh b/1-setup.sh index 725b8fe..4f49f28 100755 --- a/1-setup.sh +++ b/1-setup.sh @@ -96,7 +96,7 @@ if ! source install.conf; then fi echo "Incorrect username." done -# save the username in lowercase form to install.conf +# convert name to lowercase before saving to install.conf echo "username=${username,,}" >> ${HOME}/${SCRIPTHOME}/install.conf fi if [ $(whoami) = "root" ]; @@ -123,7 +123,7 @@ then fi done - echo ${nameofmachine,,} > /etc/hostname + echo ${nameofmachine} > /etc/hostname else echo "You are already a user proceed with aur installs" fi From c42bdf3ce4d192df98cbc57c442b8392186ff3f2 Mon Sep 17 00:00:00 2001 From: Marvin Roman Date: Mon, 22 Nov 2021 12:31:25 -0800 Subject: [PATCH 3/4] fixed computer name regex --- 1-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-setup.sh b/1-setup.sh index 4f49f28..0d88d8c 100755 --- a/1-setup.sh +++ b/1-setup.sh @@ -111,7 +111,7 @@ then do read -p "Please name your machine:" nameofmachine # hostname regex (!!couldn't find spec for computer name!!) - if [[ "${nameofmachine,,}" =~ ^[a-z][a-z0-9_-\.]{0,62}[a-z0-9]$ ]] + if [[ "${nameofmachine,,}" =~ ^[a-z][a-z0-9-_.]{0,62}[a-z0-9]$ ]] then break fi From 53605936669a100352bf140f73f6634cc3be1d86 Mon Sep 17 00:00:00 2001 From: Marvin Roman Date: Mon, 22 Nov 2021 13:57:13 -0800 Subject: [PATCH 4/4] tested fix for computer name regex --- 1-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-setup.sh b/1-setup.sh index 0d88d8c..422da96 100755 --- a/1-setup.sh +++ b/1-setup.sh @@ -111,7 +111,7 @@ then do read -p "Please name your machine:" nameofmachine # hostname regex (!!couldn't find spec for computer name!!) - if [[ "${nameofmachine,,}" =~ ^[a-z][a-z0-9-_.]{0,62}[a-z0-9]$ ]] + if [[ "${nameofmachine,,}" =~ ^[a-z][a-z0-9_.-]{0,62}[a-z0-9]$ ]] then break fi