From 0d8ae8d8489f6d378688d2ce5773381bd2f97085 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin Date: Fri, 13 Nov 2015 19:04:37 +0100 Subject: [PATCH] Contextualize SSH user with $USERNAME variable Make sure to retrieve ${HOME} location from getent. --- base/etc/one-context.d/02-ssh_public_key | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/base/etc/one-context.d/02-ssh_public_key b/base/etc/one-context.d/02-ssh_public_key index e84015c..d992eb2 100755 --- a/base/etc/one-context.d/02-ssh_public_key +++ b/base/etc/one-context.d/02-ssh_public_key @@ -16,7 +16,24 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -AUTH_DIR="/root/.ssh" +[ -z "${SSH_PUBLIC_KEY}${EC2_PUBLIC_KEY}" ] && exit 0 + +if [ -z "${USERNAME}" ] +then + USERNAME=root +fi + +# Get user $HOME directory +USER_HOME=$(getent passwd "${USERNAME}" | awk -F':' '{print $6}') + +if [ -n "${USER_HOME}" ] +then + AUTH_DIR="${USER_HOME}/.ssh" +else + # Fallback on root + AUTH_DIR="/root/.ssh" +fi + AUTH_FILE="$AUTH_DIR/authorized_keys" function add_keys { @@ -41,4 +58,5 @@ if [ -n "$EC2_PUBLIC_KEY" ]; then echo "$EC2_PUBLIC_KEY" | add_keys fi +chown "${USERNAME}": ${AUTH_DIR} ${AUTH_FILE} chmod 600 $AUTH_FILE