From 43ef147de64ca33b2921f9b59bf739af71b6a97e Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 23 May 2023 09:08:14 +0900 Subject: [PATCH] Fixes in create user script with central SSH location and dynamic home Missing username in create folder path for adding new user check if pub key exists in central location ran even if central file was missing. Fixed check for .ssh or central place to use. File check before trying to remove chattr "i" flag, can't do that if the file does not exists. --- bin/create_user.sh | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/bin/create_user.sh b/bin/create_user.sh index 78ef382..c12b940 100755 --- a/bin/create_user.sh +++ b/bin/create_user.sh @@ -247,9 +247,9 @@ while read i; do echo "++ Create '${username}:${group}(${sub_group})'"; if [ ${TEST} -eq 0 ]; then # comment is user create time - useradd -c `date +"%F"` -s /bin/bash -g ${group}${sub_group_opt} -d "${HOME_FOLDER}" -m ${username}; + useradd -c `date +"%F"` -s /bin/bash -g ${group}${sub_group_opt} -d "${HOME_FOLDER}${username}" -m ${username}; else - echo "$> useradd -c `date +"%F"` -s /bin/bash -g ${group}${sub_group_opt} -d "${HOME_FOLDER}" -m ${username}"; + echo "$> useradd -c `date +"%F"` -s /bin/bash -g ${group}${sub_group_opt} -d "${HOME_FOLDER}${username}" -m ${username}"; fi; fi; # set the auth file @@ -281,7 +281,10 @@ while read i; do echo "$> ssh-keygen -t ${ssh_keytype} -f ${ssh_keyfile} -C ${hostname}: ${username}@${group} -a 100 -N ${password}"; fi; else - found=$(grep "$(cat ${ssh_keyfile_check_pub})" ${SSH_AUTHORIZED_FILE}); + found=''; + if [ -f "${SSH_AUTHORIZED_FILE}" ]; then + found=$(grep "$(cat ${ssh_keyfile_check_pub})" ${SSH_AUTHORIZED_FILE}); + fi; if [ ! -z "${found}" ]; then skip_ssh=1; echo "-- Skip SSH Key creation: ${ssh_keygen_id}.pub"; @@ -303,7 +306,7 @@ while read i; do echo $(date +"%F %T")";"${host}";"${_hostname}";"${username}";"${password}";"${ssh_allow_type} >> ${create_output_file}; # create folder only if we do not have central # create the SSH foler and authorized access file with correct permissions - if [ ! -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then + if [ -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then echo " > Create .ssh folder"; if [ ${TEST} -eq 0 ]; then mkdir ${HOME_FOLDER}${username}/.ssh/; @@ -314,12 +317,18 @@ while read i; do # add echo " > Add public into authorized_keys file"; if [ ${TEST} -eq 0 ]; then - if [ ! -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then + if + [ ! -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ] && + [ -f "${SSH_AUTHORIZED_FILE}" ]; + then chattr -i ${SSH_AUTHORIZED_FILE}; fi; cat "${ssh_keyfile_pub}" > ${SSH_AUTHORIZED_FILE}; else - if [ ! -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then + if + [ ! -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ] && + [ -f "${SSH_AUTHORIZED_FILE}" ]; + then echo "$> chattr -i ${SSH_AUTHORIZED_FILE}"; fi; echo "$> cat ${ssh_keyfile_pub} > ${SSH_AUTHORIZED_FILE}";