From 7e77dae45866813c9a1c061e6bc7c506d25df6ca Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 13 Jul 2012 11:48:47 -0400 Subject: [PATCH] sshd.nix: Create ~/.ssh/authorized_keys with the right ownership --- modules/services/networking/ssh/sshd.nix | 25 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/modules/services/networking/ssh/sshd.nix b/modules/services/networking/ssh/sshd.nix index 53efc08cfb89..7f1cc24793ec 100644 --- a/modules/services/networking/ssh/sshd.nix +++ b/modules/services/networking/ssh/sshd.nix @@ -103,25 +103,34 @@ let local authKeyFiles="$3" local preserveExisting="$4" - eval authfile=~$userName/.ssh/authorized_keys - mkdir -p "$(dirname $authfile)" - touch "$authfile" + eval homeDir=~$userName + if ! [ -d "$homeDir" ]; then + echo "User $userName does not exist" + return + fi + if ! [ -d "$homeDir/.ssh" ]; then + mkdir -v -m 700 "$homeDir/.ssh" + chown "$userName":users "$homeDir/.ssh" + fi + local authKeysFile="$homeDir/.ssh/authorized_keys" + touch "$authKeysFile" if [ "$preserveExisting" == false ]; then - rm -f "$authfile" - echo "${marker2}" > "$authfile" + rm -f "$authKeysFile" + echo "${marker2}" > "$authKeysFile" else - sed -i '/${marker1}/ d' "$authfile" + sed -i '/${marker1}/ d' "$authKeysFile" fi IFS=, for f in $authKeys; do - echo "$f ${marker1}" >> "$authfile" + echo "$f ${marker1}" >> "$authKeysFile" done unset IFS for f in $authKeyFiles; do if [ -f "$f" ]; then - echo "$(cat "$f") ${marker1}" >> "$authfile" + echo "$(cat "$f") ${marker1}" >> "$authKeysFile" fi done + chown "$userName" "$authKeysFile" } ${userLoop}