-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60-ssh-keys
More file actions
executable file
·42 lines (34 loc) · 939 Bytes
/
Copy path60-ssh-keys
File metadata and controls
executable file
·42 lines (34 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh
# vim: set ts=4 sw=4:
set -eu
update_ssh_keys() {
local user="$1"
local keys="$2"
local homedir="$(getent passwd "$user" | cut -d: -f6)"
if [ -z "$homedir" ]; then
echo "ERROR: No home directory found for user $user" >&2
return 1
fi
local auth_file="${homedir:-/root}/.ssh/authorized_keys"
local group="$(id -gn "$user")"
if ! [ -f "$auth_file" ]; then
install -m700 -o "$user" -g "$group" -d "${auth_file%/*}"
install -m600 -o "$user" -g "$group" /dev/null "$auth_file"
fi
printf '%s\n' "$keys" | while read -r key; do
if ! grep -Fq "$key" "$auth_file"; then
echo "Adding SSH key to $auth_file" >&2
printf '%s\n' "$key" >> "$auth_file"
fi
done
}
if [ "${ROOT_SSH_KEYS:-}" ]; then
update_ssh_keys 'root' "$ROOT_SSH_KEYS"
fi
if [ "${ADMIN_SSH_KEYS:-}" ]; then
if ! id "${ADMIN_USER:-}" >/dev/null 2>&1; then
ADMIN_USER='root'
fi
update_ssh_keys "$ADMIN_USER" "$ADMIN_SSH_KEYS"
fi
exit 0