jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone
f09e2e
#!/bin/bash
f09e2e
f09e2e
# Create the host keys for the OpenSSH server.
f09e2e
#
f09e2e
# The creation is controlled by the $AUTOCREATE_SERVER_KEYS environment
f09e2e
# variable.
017ff1
AUTOCREATE_SERVER_KEYS="RSA ECDSA ED25519"
f09e2e
f09e2e
# source function library
f09e2e
. /etc/rc.d/init.d/functions
f09e2e
f09e2e
# Some functions to make the below more readable
f09e2e
KEYGEN=/usr/bin/ssh-keygen
f09e2e
RSA1_KEY=/etc/ssh/ssh_host_key
f09e2e
RSA_KEY=/etc/ssh/ssh_host_rsa_key
f09e2e
DSA_KEY=/etc/ssh/ssh_host_dsa_key
f09e2e
ECDSA_KEY=/etc/ssh/ssh_host_ecdsa_key
017ff1
ED25519_KEY=/etc/ssh/ssh_host_ed25519_key
f09e2e
f09e2e
# pull in sysconfig settings
f09e2e
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
f09e2e
f09e2e
fips_enabled() {
f09e2e
	if [ -r /proc/sys/crypto/fips_enabled ]; then
f09e2e
		cat /proc/sys/crypto/fips_enabled
f09e2e
	else
f09e2e
		echo 0
f09e2e
	fi
f09e2e
}
f09e2e
f09e2e
do_rsa1_keygen() {
f09e2e
	if [ ! -s $RSA1_KEY -a `fips_enabled` -eq 0 ]; then
f09e2e
		echo -n $"Generating SSH1 RSA host key: "
f09e2e
		rm -f $RSA1_KEY
f09e2e
		if test ! -f $RSA1_KEY && $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
f09e2e
			chgrp ssh_keys $RSA1_KEY
f09e2e
			chmod 640 $RSA1_KEY
f09e2e
			chmod 644 $RSA1_KEY.pub
f09e2e
			if [ -x /sbin/restorecon ]; then
017ff1
			    /sbin/restorecon $RSA1_KEY{,.pub}
f09e2e
			fi
f09e2e
			success $"RSA1 key generation"
f09e2e
			echo
f09e2e
		else
f09e2e
			failure $"RSA1 key generation"
f09e2e
			echo
f09e2e
			exit 1
f09e2e
		fi
f09e2e
	fi
f09e2e
}
f09e2e
f09e2e
do_rsa_keygen() {
f09e2e
	if [ ! -s $RSA_KEY ]; then
f09e2e
		echo -n $"Generating SSH2 RSA host key: "
f09e2e
		rm -f $RSA_KEY
f09e2e
		if test ! -f $RSA_KEY && $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
f09e2e
			chgrp ssh_keys $RSA_KEY
f09e2e
			chmod 640 $RSA_KEY
f09e2e
			chmod 644 $RSA_KEY.pub
f09e2e
			if [ -x /sbin/restorecon ]; then
017ff1
			    /sbin/restorecon $RSA_KEY{,.pub}
f09e2e
			fi
f09e2e
			success $"RSA key generation"
f09e2e
			echo
f09e2e
		else
f09e2e
			failure $"RSA key generation"
f09e2e
			echo
f09e2e
			exit 1
f09e2e
		fi
f09e2e
	fi
f09e2e
}
f09e2e
f09e2e
do_dsa_keygen() {
017ff1
	if [ ! -s $DSA_KEY -a `fips_enabled` -eq 0 ]; then
f09e2e
		echo -n $"Generating SSH2 DSA host key: "
f09e2e
		rm -f $DSA_KEY
f09e2e
		if test ! -f $DSA_KEY && $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
f09e2e
			chgrp ssh_keys $DSA_KEY
f09e2e
			chmod 640 $DSA_KEY
f09e2e
			chmod 644 $DSA_KEY.pub
f09e2e
			if [ -x /sbin/restorecon ]; then
017ff1
			    /sbin/restorecon $DSA_KEY{,.pub}
f09e2e
			fi
f09e2e
			success $"DSA key generation"
f09e2e
			echo
f09e2e
		else
f09e2e
			failure $"DSA key generation"
f09e2e
			echo
f09e2e
			exit 1
f09e2e
		fi
f09e2e
	fi
f09e2e
}
f09e2e
f09e2e
do_ecdsa_keygen() {
f09e2e
	if [ ! -s $ECDSA_KEY ]; then
f09e2e
		echo -n $"Generating SSH2 ECDSA host key: "
f09e2e
		rm -f $ECDSA_KEY
f09e2e
		if test ! -f $ECDSA_KEY && $KEYGEN -q -t ecdsa -f $ECDSA_KEY -C '' -N '' >&/dev/null; then
f09e2e
			chgrp ssh_keys $ECDSA_KEY
770374
			chmod 640 $ECDSA_KEY
f09e2e
			chmod 644 $ECDSA_KEY.pub
f09e2e
			if [ -x /sbin/restorecon ]; then
017ff1
			    /sbin/restorecon $ECDSA_KEY{,.pub}
f09e2e
			fi
f09e2e
			success $"ECDSA key generation"
f09e2e
			echo
f09e2e
		else
f09e2e
			failure $"ECDSA key generation"
f09e2e
			echo
f09e2e
			exit 1
f09e2e
		fi
f09e2e
	fi
f09e2e
}
f09e2e
017ff1
do_ed25519_keygen() {
017ff1
	if [ ! -s $ED25519_KEY -a `fips_enabled` -eq 0 ]; then
017ff1
		echo -n $"Generating SSH2 ED25519 host key: "
017ff1
		rm -f $ED25519_KEY
017ff1
		if test ! -f $ED25519_KEY && $KEYGEN -q -t ed25519 -f $ED25519_KEY -C '' -N '' >&/dev/null; then
017ff1
			chgrp ssh_keys $ED25519_KEY
017ff1
			chmod 640 $ED25519_KEY
017ff1
			chmod 644 $ED25519_KEY.pub
017ff1
			if [ -x /sbin/restorecon ]; then
017ff1
			    /sbin/restorecon $ED25519_KEY{,.pub}
017ff1
			fi
017ff1
			success $"ED25519 key generation"
017ff1
			echo
017ff1
		else
017ff1
			failure $"ED25519 key generation"
017ff1
			echo
017ff1
			exit 1
770374
		fi
f09e2e
	fi
017ff1
}
017ff1
017ff1
if [ "x${AUTOCREATE_SERVER_KEYS}" == "xNO" ]; then
017ff1
	exit 0
f09e2e
fi
017ff1
017ff1
# legacy options
017ff1
case $AUTOCREATE_SERVER_KEYS in
017ff1
	NODSA) AUTOCREATE_SERVER_KEYS="RSA ECDSA ED25519";;
017ff1
	RSAONLY) AUTOCREATE_SERVER_KEYS="RSA";;
017ff1
	YES) AUTOCREATE_SERVER_KEYS="DSA RSA ECDSA ED25519";;
017ff1
esac
017ff1
017ff1
for KEY in $AUTOCREATE_SERVER_KEYS; do
017ff1
	case $KEY in
017ff1
		DSA) do_dsa_keygen;;
017ff1
		RSA) do_rsa_keygen;;
017ff1
		ECDSA) do_ecdsa_keygen;;
017ff1
		ED25519) do_ed25519_keygen;;
017ff1
	esac
017ff1
done