#!/bin/sh

# Short script one can run under xinetd as a "random number daemon"
# that makes a random binary stream accessible a uint at a time to
# a inetd socket.  Security and so on are your own responsibility:
# you will probably need to punch a hole in your firewall for the
# selected port, make the script belong to nobody/guest, run it in
# a private chroot -- or simply run it inside a cluster that is already
# so insecure that nobody cares.

# Obviously if performance is important to you, there are lots of ways
# to write a much better daemon in actual c that uses the GSL to provide
# a relatively FAST stream of random numbers without the overhead of a
# shell script and dd, but it is anticipated that this socket will only
# be used to get a handful of uints on a cluster node.

IFS=
TRUE=1
while [ $TRUE != 0 ]
do

 # This blocks until a line is input with LF on stdin
 read
 # This is a line that will read a single uint from /dev/urandom
 # and write it to stdout.  However we cannot check it because
 # bash doesn't handle raw binary at all, let alone well.
 dd if=/dev/urandom bs=1c count=4 2>/dev/null

 # So to test it, we have to convert it on the fly into something
 # we can see.  uuencode will do this.  Uncomment this if you want
 # the daemon to return an ascii string that you can read that
 # gives you a reason to think that the bytes dd is otherwise spitting
 # out are indeed random.  Or use perl or some other language that can
 # hand binary data!
 # for i in 1 2 3 4
 # do
 #   RAND=`dd if=/dev/urandom bs=1 count=1 2>/dev/null | uuencode stdout | \
 #      sed -e '/^!/!d' -e 's/^!//' -e 's/\`\`$//g' `
 #   echo -n "|$RAND"
 # done
 # echo "|"

done
