170922Sdougb#!/bin/sh
270922Sdougb#
3241205Sdougb# Copyright (c) 2001-2006,2012 Douglas Barton, dougb@FreeBSD.org
470922Sdougb# All rights reserved.
570922Sdougb#
670922Sdougb# Redistribution and use in source and binary forms, with or without
770922Sdougb# modification, are permitted provided that the following conditions
870922Sdougb# are met:
970922Sdougb# 1. Redistributions of source code must retain the above copyright
1070922Sdougb#    notice, this list of conditions and the following disclaimer.
1170922Sdougb# 2. Redistributions in binary form must reproduce the above copyright
1270922Sdougb#    notice, this list of conditions and the following disclaimer in the
1370922Sdougb#    documentation and/or other materials provided with the distribution.
1470922Sdougb#
1570922Sdougb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1670922Sdougb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1770922Sdougb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1870922Sdougb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1970922Sdougb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2070922Sdougb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2170922Sdougb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2270922Sdougb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2370922Sdougb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2470922Sdougb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2570922Sdougb# SUCH DAMAGE.
2670922Sdougb#
2770922Sdougb# $FreeBSD$
2870922Sdougb
2970922Sdougb# This script is called by cron to store bits of randomness which are
3070922Sdougb# then used to seed /dev/random on boot.
3170922Sdougb
32241205Sdougb# Originally developed by Doug Barton, dougb@FreeBSD.org
3371014Sdougb
3470922SdougbPATH=/bin:/usr/bin
3570922Sdougb
3670922Sdougb# If there is a global system configuration file, suck it in.
3770922Sdougb#
3870922Sdougbif [ -r /etc/defaults/rc.conf ]; then
3970922Sdougb	. /etc/defaults/rc.conf
40161683Sdougb	source_rc_confs 2>/dev/null
4170922Sdougbelif [ -r /etc/rc.conf ]; then
42161683Sdougb	. /etc/rc.conf 2>/dev/null
4370922Sdougbfi
4470922Sdougb
4570922Sdougbcase ${entropy_dir} in
4670922Sdougb[Nn][Oo])
4770922Sdougb	exit 0
4870922Sdougb	;;
4970922Sdougb*)
5071014Sdougb	entropy_dir=${entropy_dir:-/var/db/entropy}
5170922Sdougb	;;
5270922Sdougbesac
5370922Sdougb
5470922Sdougbentropy_save_sz=${entropy_save_sz:-2048}
5570922Sdougbentropy_save_num=${entropy_save_num:-8}
5670922Sdougb
5770922Sdougbif [ ! -d "${entropy_dir}" ]; then
58241205Sdougb	install -d -o operator -g operator -m 0700 "${entropy_dir}" || {
59241205Sdougb		logger -is -t "$0" The entropy directory "${entropy_dir}" does \
60241205Sdougb		    not exist, and cannot be created. Therefore no entropy can \
61241205Sdougb		    be saved.; exit 1; }
6270922Sdougbfi
6370922Sdougb
64241205Sdougbcd "${entropy_dir}" || {
65241205Sdougb	logger -is -t "$0" Cannot cd to the entropy directory: "${entropy_dir}". \
66241205Sdougb	    Entropy file rotation is aborted.; exit 1; }
67241205Sdougb
68241205Sdougbfor f in saved-entropy.*; do
69241205Sdougb	case "${f}" in saved-entropy.\*) continue ;; esac	# No files match
70241205Sdougb	[ ${f#saved-entropy\.} -ge ${entropy_save_num} ] && unlink ${f}
71241205Sdougbdone
72241205Sdougb
7370922Sdougbumask 377
7470922Sdougb
75241205Sdougbn=$(( ${entropy_save_num} - 1 ))
76241205Sdougbwhile [ ${n} -ge 1 ]; do
77241205Sdougb	if [ -f "saved-entropy.${n}" ]; then
78241205Sdougb		mv "saved-entropy.${n}" "saved-entropy.$(( ${n} + 1 ))"
79241205Sdougb	elif [ -e "saved-entropy.${n}" -o -L "saved-entropy.${n}" ]; then
80241205Sdougb		logger -is -t "$0" \
81241205Sdougb	"${entropy_dir}/saved-entropy.${n}" is not a regular file, and so \
82241205Sdougb	    it will not be rotated. Entropy file rotation is aborted.
83241205Sdougb		exit 1
8470922Sdougb	fi
85241205Sdougb	n=$(( ${n} - 1 ))
8670922Sdougbdone
8770922Sdougb
88241205Sdougbdd if=/dev/random of=saved-entropy.1 bs=${entropy_save_sz} count=1 2>/dev/null
8970922Sdougb
9070922Sdougbexit 0
91