vishalmishra434 / rpms / openssh

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