From 3a42c521f6fecbfa6e6e456198f76d600669b08a Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 5 Apr 2023 11:21:26 +0900 Subject: [PATCH] Add central ssh authorized_keys file detect, fixes for home folder create * new detect for central authorized keys folder 1) must have %u set in the AuthorizedKeysFile list 2) folder must exists (will not be created, if not exists abort) If above is set, it will create a username file with the ssh key in there and lock it down as r--/user and +i attrib else uses old .ssh folder form * fix for user add with different home base folder add this as option for the useradd command --- bin/create_user.sh | 70 +++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/bin/create_user.sh b/bin/create_user.sh index 61b0282..7fd28cf 100755 --- a/bin/create_user.sh +++ b/bin/create_user.sh @@ -94,6 +94,15 @@ ssh_keytype=''; # sshallow or sshforward ssh_group=''; ssh_forward_ok=0; +# detect ssh authorized_keys setting +SSH_CENTRAL_AUTHORIZED_FILE_FOLDER=''; +SSH_AUTHORIZED_FILE=''; +for cf in $(grep "^AuthorizedKeysFile" /etc/ssh/sshd_config | grep "%u"); do + if [ ! -z $(echo "${cf}" | grep "%u") ]; then + SSH_CENTRAL_AUTHORIZED_FILE_FOLDER=$(echo "${cf}" | sed -e 's/%u//'); + fi; +done; + # check if ssh key folder exists if [ ! -d "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}" ]; then mkdir "${ROOT_FOLDER}${SSH_KEYGEN_FOLDER}"; @@ -234,11 +243,17 @@ 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} -m ${username}; + useradd -c `date +"%F"` -s /bin/bash -g ${group}${sub_group_opt} -d "${HOME_FOLDER}" -m ${username}; else - echo "$> useradd -s /bin/bash -g ${group}${sub_group_opt} -m ${username}"; + echo "$> useradd -c `date +"%F"` -s /bin/bash -g ${group}${sub_group_opt} -d "${HOME_FOLDER}" -m ${username}"; fi; fi; + # set the auth file + if [ -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then + SSH_AUTHORIZED_FILE="${HOME_FOLDER}${username}/.ssh/authorized_keys"; + else + SSH_AUTHORIZED_FILE="${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}${username}"; + fi; skip_ssh=0; # if public pem already exists skip creation if [ ! -f "${ssh_keyfile_check_pub}" ]; then @@ -262,7 +277,7 @@ 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})" ${HOME_FOLDER}${username}/.ssh/authorized_keys); + found=$(grep "$(cat ${ssh_keyfile_check_pub})" ${SSH_AUTHORIZED_FILE}); if [ ! -z "${found}" ]; then skip_ssh=1; # override previously set with stored one @@ -282,28 +297,45 @@ while read i; do create_output_file="${ROOT_FOLDER}${output_file}.TEST"; fi; 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 - echo " > Create .ssh folder"; - if [ ${TEST} -eq 0 ]; then - mkdir ${HOME_FOLDER}${username}/.ssh/; - else - echo "$> mkdir ${HOME_FOLDER}${username}/.ssh/"; + if [ -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then + echo " > Create .ssh folder"; + if [ ${TEST} -eq 0 ]; then + mkdir ${HOME_FOLDER}${username}/.ssh/; + else + echo "$> mkdir ${HOME_FOLDER}${username}/.ssh/"; + fi; fi; - echo " > Add public into authorized_keys"; + echo " > Add public into authorized_keys file"; if [ ${TEST} -eq 0 ]; then - cat "${ssh_keyfile_pub}" > ${HOME_FOLDER}${username}/.ssh/authorized_keys; + cat "${ssh_keyfile_pub}" > ${SSH_AUTHORIZED_FILE}; else - echo "$> cat ${ssh_keyfile_pub} > ${HOME_FOLDER}${username}/.ssh/authorized_keys"; + echo "$> cat ${ssh_keyfile_pub} > ${SSH_AUTHORIZED_FILE}"; fi; - echo " > Secure folder .ssh and authorized_keys file"; - if [ ${TEST} -eq 0 ]; then - chown -R ${username}:${group} ${HOME_FOLDER}${username}/.ssh/; - chmod 700 ${HOME_FOLDER}${username}/.ssh/; - chmod 600 ${HOME_FOLDER}${username}/.ssh/authorized_keys; + if [ -z "${SSH_CENTRAL_AUTHORIZED_FILE_FOLDER}" ]; then + echo " > Secure home directory folder .ssh and authorized_keys file"; + if [ ${TEST} -eq 0 ]; then + chown -R ${username}:${group} ${HOME_FOLDER}${username}/.ssh/; + chmod 700 ${HOME_FOLDER}${username}/.ssh/; + chmod 600 ${SSH_AUTHORIZED_FILE}; + else + echo "$> chown -R ${username}:${group} ${HOME_FOLDER}${username}/.ssh/"; + echo "$> chmod 700 ${HOME_FOLDER}${username}/.ssh/"; + echo "$> chmod 600 ${SSH_AUTHORIZED_FILE}"; + fi; else - echo "$> chown -R ${username}:${group} ${HOME_FOLDER}${username}/.ssh/"; - echo "$> chmod 700 ${HOME_FOLDER}${username}/.ssh/"; - echo "$> chmod 600 ${HOME_FOLDER}${username}/.ssh/authorized_keys"; + echo " > Secure central authorized_keys file"; + if [ ${TEST} -eq 0 ]; then + chown ${username}:${group} ${SSH_AUTHORIZED_FILE}; + chmod 400 ${SSH_AUTHORIZED_FILE}; + # set +i so user can't change file + chattr +i ${SSH_AUTHORIZED_FILE}; + else + echo "$> chown ${username}:${group} ${SSH_AUTHORIZED_FILE}"; + echo "$> chmod 400 ${SSH_AUTHORIZED_FILE}"; + echo "$> chattr +i ${SSH_AUTHORIZED_FILE}"; + fi; fi; fi; done;