sshd.nix: Create ~/.ssh/authorized_keys with the right ownership
This commit is contained in:
parent
7fca8ceaf8
commit
7e77dae458
|
@ -103,25 +103,34 @@ let
|
||||||
local authKeyFiles="$3"
|
local authKeyFiles="$3"
|
||||||
local preserveExisting="$4"
|
local preserveExisting="$4"
|
||||||
|
|
||||||
eval authfile=~$userName/.ssh/authorized_keys
|
eval homeDir=~$userName
|
||||||
mkdir -p "$(dirname $authfile)"
|
if ! [ -d "$homeDir" ]; then
|
||||||
touch "$authfile"
|
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
|
if [ "$preserveExisting" == false ]; then
|
||||||
rm -f "$authfile"
|
rm -f "$authKeysFile"
|
||||||
echo "${marker2}" > "$authfile"
|
echo "${marker2}" > "$authKeysFile"
|
||||||
else
|
else
|
||||||
sed -i '/${marker1}/ d' "$authfile"
|
sed -i '/${marker1}/ d' "$authKeysFile"
|
||||||
fi
|
fi
|
||||||
IFS=,
|
IFS=,
|
||||||
for f in $authKeys; do
|
for f in $authKeys; do
|
||||||
echo "$f ${marker1}" >> "$authfile"
|
echo "$f ${marker1}" >> "$authKeysFile"
|
||||||
done
|
done
|
||||||
unset IFS
|
unset IFS
|
||||||
for f in $authKeyFiles; do
|
for f in $authKeyFiles; do
|
||||||
if [ -f "$f" ]; then
|
if [ -f "$f" ]; then
|
||||||
echo "$(cat "$f") ${marker1}" >> "$authfile"
|
echo "$(cat "$f") ${marker1}" >> "$authKeysFile"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
chown "$userName" "$authKeysFile"
|
||||||
}
|
}
|
||||||
|
|
||||||
${userLoop}
|
${userLoop}
|
||||||
|
|
Loading…
Reference in New Issue