kentpeacock / rpms / openssh

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