170922Sdougb#!/bin/sh
270922Sdougb#
3240090Sdougb# 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: releng/10.3/libexec/save-entropy/save-entropy.sh 269220 2014-07-29 06:00:16Z delphij $
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
32240090Sdougb# 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
45269220Sdelphij[ $(/sbin/sysctl -n security.jail.jailed) = 0 ] || exit 0
46269220Sdelphij
4770922Sdougbcase ${entropy_dir} in
4870922Sdougb[Nn][Oo])
4970922Sdougb	exit 0
5070922Sdougb	;;
5170922Sdougb*)
5271014Sdougb	entropy_dir=${entropy_dir:-/var/db/entropy}
5370922Sdougb	;;
5470922Sdougbesac
5570922Sdougb
5670922Sdougbentropy_save_sz=${entropy_save_sz:-2048}
5770922Sdougbentropy_save_num=${entropy_save_num:-8}
5870922Sdougb
5970922Sdougbif [ ! -d "${entropy_dir}" ]; then
60240090Sdougb	install -d -o operator -g operator -m 0700 "${entropy_dir}" || {
61240090Sdougb		logger -is -t "$0" The entropy directory "${entropy_dir}" does \
62240090Sdougb		    not exist, and cannot be created. Therefore no entropy can \
63240090Sdougb		    be saved.; exit 1; }
6470922Sdougbfi
6570922Sdougb
66240090Sdougbcd "${entropy_dir}" || {
67240090Sdougb	logger -is -t "$0" Cannot cd to the entropy directory: "${entropy_dir}". \
68240090Sdougb	    Entropy file rotation is aborted.; exit 1; }
69240090Sdougb
70240090Sdougbfor f in saved-entropy.*; do
71240090Sdougb	case "${f}" in saved-entropy.\*) continue ;; esac	# No files match
72240090Sdougb	[ ${f#saved-entropy\.} -ge ${entropy_save_num} ] && unlink ${f}
73240090Sdougbdone
74240090Sdougb
7570922Sdougbumask 377
7670922Sdougb
77240090Sdougbn=$(( ${entropy_save_num} - 1 ))
78240090Sdougbwhile [ ${n} -ge 1 ]; do
79240090Sdougb	if [ -f "saved-entropy.${n}" ]; then
80240090Sdougb		mv "saved-entropy.${n}" "saved-entropy.$(( ${n} + 1 ))"
81240090Sdougb	elif [ -e "saved-entropy.${n}" -o -L "saved-entropy.${n}" ]; then
82240090Sdougb		logger -is -t "$0" \
83240090Sdougb	"${entropy_dir}/saved-entropy.${n}" is not a regular file, and so \
84240090Sdougb	    it will not be rotated. Entropy file rotation is aborted.
85240090Sdougb		exit 1
8670922Sdougb	fi
87240090Sdougb	n=$(( ${n} - 1 ))
8870922Sdougbdone
8970922Sdougb
90240090Sdougbdd if=/dev/random of=saved-entropy.1 bs=${entropy_save_sz} count=1 2>/dev/null
9170922Sdougb
9270922Sdougbexit 0
93