Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (69 sloc) 2.22 KB
#! /bin/bash
### BEGIN INIT INFO
# Provides: randomsanity
# Required-Start: $network $named $syslog urandom
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop:
# Description: Sanity check /dev/urandom at startup
### END INIT INFO
# Author: Gavin Andresen <gavinandresen@gmail.com>
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Sanity check /dev/urandom using the randomsanity.org REST service"
# Read configuration variable file if it is present
[ -r /etc/default/randomsanity ] && . /etc/default/randomsanity
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
. /lib/lsb/init-functions
if [ -z "${BASEURL+x}" ] # Default if not set in config file:
then
BASEURL=https://rest.randomsanity.org/v1/q
fi
checkbytes() {
log_begin_msg "Sanity checking /dev/urandom..."
URL="$BASEURL/$1"
# RESPONSE is an array of response lines from the server
IFS=$'\n' RESPONSE=( $(wget --quiet --tries=1 --output-document=- --timeout=5 --max-redirect=1 --save-headers "$URL" ) )
WGET_RESULT=$?
if [ $WGET_RESULT -ne 0 ]
then
log_end_msg $WGET_RESULT
log_warning_msg "wget $URL failed: (${WGET_RESULT})"
return $WGET_RESULT
fi
last=$(( ${#RESPONSE[@]}-1 )) # Index of last line
case "${RESPONSE[$last]}" in
true)
log_end_msg 0
;;
false)
log_end_msg 1
log_failure_msg "random sanity check failure"
;;
*)
log_end_msg 1
log_warning_msg "Unexpected result from server: ${RESPONSE[$last]}"
return 1
;;
esac
return 0
}
case "$1" in
start)
hexbytes=$(head --bytes=64 /dev/urandom | od -v -t x1 -An | tr -d '\n ')
checkbytes "$hexbytes"
;;
stop)
;;
test)
# Force a failure by using /dev/zero as the source of 'randomness':
hexbytes=$(head --bytes=64 /dev/zero | od -v -t x1 -An | tr -d '\n ')
checkbytes "$hexbytes"
;;
*)
echo "Usage: $SCRIPTNAME {start|test}" >&2
exit 3
;;
esac
: