blob: 5463b1a4cb18337bb15f554856b8f256ae22a102 [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#! /bin/sh
2
Brad Bishopd7bf8c12018-02-25 22:55:05 -05003generate_key() {
4 local FILE=$1
5 local TYPE=$2
6 local DIR="$(dirname "$FILE")"
7
8 mkdir -p "$DIR"
9 ssh-keygen -q -f "${FILE}.tmp" -N '' -t $TYPE
10
11 # Atomically rename file public key
12 mv -f "${FILE}.tmp.pub" "${FILE}.pub"
13
14 # This sync does double duty: Ensuring that the data in the temporary
15 # private key file is on disk before the rename, and ensuring that the
16 # public key rename is completed before the private key rename, since we
17 # switch on the existence of the private key to trigger key generation.
18 # This does mean it is possible for the public key to exist, but be garbage
19 # but this is OK because in that case the private key won't exist and the
20 # keys will be regenerated.
21 #
22 # In the event that sync understands arguments that limit what it tries to
23 # fsync(), we provided them. If it does not, it will simply call sync()
24 # which is just as well
25 sync "${FILE}.pub" "$DIR" "${FILE}.tmp"
26
27 mv "${FILE}.tmp" "$FILE"
28
29 # sync to ensure the atomic rename is committed
30 sync "$DIR"
31}
32
Brad Bishop6e60e8b2018-02-01 10:27:11 -050033# /etc/default/ssh may set SYSCONFDIR and SSHD_OPTS
34if test -f /etc/default/ssh; then
35 . /etc/default/ssh
36fi
37
38[ -z "$SYSCONFDIR" ] && SYSCONFDIR=/etc/ssh
39mkdir -p $SYSCONFDIR
40
41# parse sshd options
42set -- ${SSHD_OPTS} --
43sshd_config=/etc/ssh/sshd_config
44while true ; do
45 case "$1" in
46 -f*) if [ "$1" = "-f" ] ; then
47 sshd_config="$2"
48 shift
49 else
50 sshd_config="${1#-f}"
51 fi
52 shift
53 ;;
54 --) shift; break;;
55 *) shift;;
56 esac
57done
58
59# parse location of keys
60HOST_KEY_RSA=$(grep ^HostKey "${sshd_config}" | grep _rsa_ | tail -1 | awk ' { print $2 } ')
61[ -z "${HOST_KEY_RSA}" ] && HOST_KEY_RSA=$(grep HostKey "${sshd_config}" | grep _rsa_ | tail -1 | awk ' { print $2 } ')
62[ -z "${HOST_KEY_RSA}" ] && HOST_KEY_RSA=$SYSCONFDIR/ssh_host_rsa_key
63HOST_KEY_DSA=$(grep ^HostKey "${sshd_config}" | grep _dsa_ | tail -1 | awk ' { print $2 } ')
64[ -z "${HOST_KEY_DSA}" ] && HOST_KEY_DSA=$(grep HostKey "${sshd_config}" | grep _dsa_ | tail -1 | awk ' { print $2 } ')
65[ -z "${HOST_KEY_DSA}" ] && HOST_KEY_DSA=$SYSCONFDIR/ssh_host_dsa_key
66HOST_KEY_ECDSA=$(grep ^HostKey "${sshd_config}" | grep _ecdsa_ | tail -1 | awk ' { print $2 } ')
67[ -z "${HOST_KEY_ECDSA}" ] && HOST_KEY_ECDSA=$(grep HostKey "${sshd_config}" | grep _ecdsa_ | tail -1 | awk ' { print $2 } ')
68[ -z "${HOST_KEY_ECDSA}" ] && HOST_KEY_ECDSA=$SYSCONFDIR/ssh_host_ecdsa_key
69HOST_KEY_ED25519=$(grep ^HostKey "${sshd_config}" | grep _ed25519_ | tail -1 | awk ' { print $2 } ')
70[ -z "${HOST_KEY_ED25519}" ] && HOST_KEY_ED25519=$(grep HostKey "${sshd_config}" | grep _ed25519_ | tail -1 | awk ' { print $2 } ')
71[ -z "${HOST_KEY_ED25519}" ] && HOST_KEY_ED25519=$SYSCONFDIR/ssh_host_ed25519_key
72
73# create keys if necessary
74if [ ! -f $HOST_KEY_RSA ]; then
75 echo " generating ssh RSA key..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 generate_key $HOST_KEY_RSA rsa
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077fi
78if [ ! -f $HOST_KEY_ECDSA ]; then
79 echo " generating ssh ECDSA key..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050080 generate_key $HOST_KEY_ECDSA ecdsa
Brad Bishop6e60e8b2018-02-01 10:27:11 -050081fi
82if [ ! -f $HOST_KEY_DSA ]; then
83 echo " generating ssh DSA key..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050084 generate_key $HOST_KEY_DSA dsa
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085fi
86if [ ! -f $HOST_KEY_ED25519 ]; then
87 echo " generating ssh ED25519 key..."
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 generate_key $HOST_KEY_ED25519 ed25519
Brad Bishop6e60e8b2018-02-01 10:27:11 -050089fi
90