summaryrefslogtreecommitdiffstats
path: root/projectroot/etc/rc.once.d/openssh
blob: 66cfa06df12ae23e4e878c12952d47b4cb542148 (plain)
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
43
44
45
46
47
48
49
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

get_hostkeys() {
	[ -f /etc/ssh/sshd_config ] || return
	sed -n 's/^HostKey[ \t][ \t]*\(.*\)/\1/p' /etc/ssh/sshd_config
}

host_keys_required() {
	hostkeys="$(get_hostkeys)"
	if [ "$hostkeys" ]; then
		echo "$hostkeys"
	else
		# No HostKey directives found, so we pick secure defaults
		echo /etc/ssh/ssh_host_ed25519_key
	fi
}

create_key() {
	keytype="$1"
	prettykeytype="$(echo $_type | tr a-z A-Z)"
	shift
	hostkeys="$1"
	shift

	file="/etc/ssh/ssh_host_${keytype}_key"

	if echo "$hostkeys" | grep -x -F "$file" >/dev/null; then
		echo "Create $prettykeytype key; this may take some time ..."
		rm -f $file &&
		ssh-keygen -q -f "$file" -N '' -t "$keytype" "$@" || return
		echo "Created $prettykeytype key."
	fi
}

create_keys() {
	hostkeys="$(host_keys_required)"

	create_key "dsa" "$hostkeys" &&
	create_key "ecdsa" "$hostkeys" &&
	create_key "ed25519" "$hostkeys" &&
	create_key "rsa" "$hostkeys" -b 4096
}

if ! create_keys; then
	echo "Generating SSH keys failed!"
	exit 1
fi