forked from RandomSanityProject/randomsanity_debian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomsanity
80 lines (69 loc) · 2.22 KB
/
randomsanity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /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 <[email protected]>
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
: