#!/bin/bash
#
# Idea and code due to Sean Reifschneider <jafo@tummy.com>
# Both somewhat mangled by alanr@unix.sh
#
#	RANDMAX is the upper bound (in 512-byte blocks) on how much
#	randomness to consume...
#
#	It may be the case that a single block is enough, since it's
#	supposedly quite random data, and all we need is a 32 byte
#	checksum string out of it...
#
RANDMAX=3 # upper bound (in blocks) on how much randomness to consume...
RDEV=/dev/urandom
AUTH=/etc/ha.d/authkeys
RAND=${RANDOM}

#	Make sure ${RANDOM} is supported
case $RAND in
  ?*)	;;
  *)	RAND=50;;
esac

#
#	Figure out how many blocks to checksum
#
CNT=`expr \( $RAND % $RANDMAX \) + 1`

if
  [ ! -c $RDEV ]
then
    echo "Random device $RDEV not supported on this OS"
    exit 1
fi


umask 077
touch $AUTH 2>/dev/null
chmod 600 $AUTH

cat <<-! >$AUTH
	# Key automatically generated by $0
	auth 1
	1 sha1 `dd if=$RDEV count=$CNT 2>/dev/null | md5sum | cut -c1-32`
!

