--- sbin/ldapusersetup | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) --- a/sbin/ldapusersetup +++ b/sbin/ldapusersetup @@ -44,6 +44,29 @@ _SHELL="" ### Helper functions ### +# Gets input from user and validates it. +# Will only return if input meets validation +# criteria otherwise will just sit there. +# +# Input : input string ($1), valid output options ($2) +# Output: the validated input +# Note : the validation list must be an array +LdapUserInput () { +declare -a optionAry=("${!2}") +while true; do + read -p "$1" _output + # convert to lower case + _output2=${_output,,} + # check if output is a valid option + if [[ "${optionAry[@]}" =~ "$_output2" ]]; then + break + else + echo "Invalid input \"$_output\". Allowed options: ${optionAry[@]}" >&2 + fi +done + echo "$_output2" +} + # Delete an ldap user if it exists # and exit with error # Input : username ($1), exit msg ($2) @@ -67,10 +90,12 @@ LdapAddUser() { LdapAddLoginShell () { if [ -z "$2" ]; then # Ask the user for the login shell - echo "Select Login Shell option # [2]: + shellInput="Select Login Shell option # [2]: 1) Bash -2) Lshell" - read opn +2) Lshell +" + options=( 1, 2 ) + opn=`LdapUserInput "$shellInput" options[@]` case $opn in 1) _SHELL="/bin/sh";; 2) _SHELL="$_DEFAULTLSHELL";; @@ -139,7 +164,6 @@ LdapUpdateShadowWarning () { echo "Updating password expiry to $_newWarning days" } - # Since this setup script is meant to be a # wrapper on top of existing ldap scripts, # it share invoke those... we could have achieved @@ -170,10 +194,9 @@ if [ "$#" -eq 0 ]; then # prompt for sudo permissions if [ "$_SHELL" != "$_DEFAULTLSHELL" ]; then # Should sudo be activated for this user - echo -n "Add $_username to sudoer list? (yes/NO): " - read CONFIRM - CONFIRM=${CONFIRM,,} - + shellInput="Add $_username to sudoer list? (yes/NO): " + options=( "yes", "no" ) + CONFIRM=`LdapUserInput "$shellInput" options[@]` if is_yes $CONFIRM then LdapAddSudo "$_username" @@ -181,9 +204,9 @@ if [ "$#" -eq 0 ]; then fi # Add to secondary user group - echo -n "Add $_username to secondary user group? (yes/NO): " - read CONFIRM - CONFIRM=${CONFIRM,,} + shellInput="Add $_username to secondary user group? (yes/NO): " + options=( "yes", "no" ) + CONFIRM=`LdapUserInput "$shellInput" options[@]` if is_yes $CONFIRM then echo -n "Secondary group to add user to? [$_DEFAULTGRP2]: "