#!/bin/bash
#
# netinfod/netinfo SSL key generation script
# Mostly ripped from the sshd init script. Sorry... :-)
#

# Some functions to make the below more readable
CFGDIR=/etc/netinfo
KEYGEN=/usr/bin/ssh-keygen
RSA1_KEY=${CFGDIR}/netinfo_host_key
RSA_KEY=${CFGDIR}/netinfo_host_rsa_key
DSA_KEY=${CFGDIR}/netinfo_host_dsa_key

do_rsa1_keygen() {
	if [ ! -s $RSA1_KEY ]; then
		echo -n $"Generating SSH1 RSA host key: "
		if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
			chmod 600 $RSA1_KEY
			chmod 644 $RSA1_KEY.pub
			success $"RSA1 key generation"
			echo
		else
			failure $"RSA1 key generation"
			echo
			exit 1
		fi
	fi
}

do_rsa_keygen() {
	if [ ! -s $RSA_KEY ]; then
		echo -n $"Generating SSH2 RSA host key: "
		if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
			chmod 600 $RSA_KEY
			chmod 644 $RSA_KEY.pub
			success $"RSA key generation"
			echo
		else
			failure $"RSA key generation"
			echo
			exit 1
		fi
	fi
}

do_dsa_keygen() {
	if [ ! -s $DSA_KEY ]; then
		echo -n $"Generating SSH2 DSA host key: "
		if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
			chmod 600 $DSA_KEY
			chmod 644 $DSA_KEY.pub
			success $"DSA key generation"
			echo
		else
			failure $"DSA key generation"
			echo
			exit 1
		fi
	fi
}

# Create keys
if [ ! -f $RSA1_KEY ] ; then
	do_rsa1_keygen
else
	echo "netinfo_host_key already exists! Delete it if you really want"
	echo "to create a new host key."
fi

if [ ! -f $RSA_KEY ] ; then
	do_rsa_keygen
else
	echo "netinfo_host_rsa_key already exists! Delete it if you really want"
	echo "to create a new host key."
fi

if [ ! -f $DSA_KEY ] ; then
	do_dsa_keygen
else
	echo "netinfo_host_dsa_key already exists! Delete it if you really want"
	echo "to create a new host key."
fi



