xref: /freebsd/libexec/save-entropy/save-entropy.sh (revision 8fcb9ca99b8d4fdf82b52933c622f8a21c28ca17)
127a803d6SDoug Barton#!/bin/sh
227a803d6SDoug Barton#
381f72adfSDoug Barton# Copyright (c) 2001-2006,2012 Douglas Barton, dougb@FreeBSD.org
427a803d6SDoug Barton# All rights reserved.
527a803d6SDoug Barton#
627a803d6SDoug Barton# Redistribution and use in source and binary forms, with or without
727a803d6SDoug Barton# modification, are permitted provided that the following conditions
827a803d6SDoug Barton# are met:
927a803d6SDoug Barton# 1. Redistributions of source code must retain the above copyright
1027a803d6SDoug Barton#    notice, this list of conditions and the following disclaimer.
1127a803d6SDoug Barton# 2. Redistributions in binary form must reproduce the above copyright
1227a803d6SDoug Barton#    notice, this list of conditions and the following disclaimer in the
1327a803d6SDoug Barton#    documentation and/or other materials provided with the distribution.
1427a803d6SDoug Barton#
1527a803d6SDoug Barton# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1627a803d6SDoug Barton# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1727a803d6SDoug Barton# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1827a803d6SDoug Barton# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1927a803d6SDoug Barton# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2027a803d6SDoug Barton# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2127a803d6SDoug Barton# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2227a803d6SDoug Barton# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2327a803d6SDoug Barton# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2427a803d6SDoug Barton# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2527a803d6SDoug Barton# SUCH DAMAGE.
2627a803d6SDoug Barton#
2727a803d6SDoug Barton# $FreeBSD$
2827a803d6SDoug Barton
2927a803d6SDoug Barton# This script is called by cron to store bits of randomness which are
3027a803d6SDoug Barton# then used to seed /dev/random on boot.
3127a803d6SDoug Barton
3281f72adfSDoug Barton# Originally developed by Doug Barton, dougb@FreeBSD.org
3310fe5181SDoug Barton
3427a803d6SDoug BartonPATH=/bin:/usr/bin
3527a803d6SDoug Barton
3627a803d6SDoug Barton# If there is a global system configuration file, suck it in.
3727a803d6SDoug Barton#
3827a803d6SDoug Bartonif [ -r /etc/defaults/rc.conf ]; then
3927a803d6SDoug Barton	. /etc/defaults/rc.conf
40e8ea7f02SDoug Barton	source_rc_confs 2>/dev/null
4127a803d6SDoug Bartonelif [ -r /etc/rc.conf ]; then
42e8ea7f02SDoug Barton	. /etc/rc.conf 2>/dev/null
4327a803d6SDoug Bartonfi
4427a803d6SDoug Barton
45*8fcb9ca9SXin LI[ $(/sbin/sysctl -n security.jail.jailed) = 0 ] || exit 0
46*8fcb9ca9SXin LI
4727a803d6SDoug Bartoncase ${entropy_dir} in
4827a803d6SDoug Barton[Nn][Oo])
4927a803d6SDoug Barton	exit 0
5027a803d6SDoug Barton	;;
5127a803d6SDoug Barton*)
5210fe5181SDoug Barton	entropy_dir=${entropy_dir:-/var/db/entropy}
5327a803d6SDoug Barton	;;
5427a803d6SDoug Bartonesac
5527a803d6SDoug Barton
5627a803d6SDoug Bartonentropy_save_sz=${entropy_save_sz:-2048}
5727a803d6SDoug Bartonentropy_save_num=${entropy_save_num:-8}
5827a803d6SDoug Barton
5927a803d6SDoug Bartonif [ ! -d "${entropy_dir}" ]; then
6081f72adfSDoug Barton	install -d -o operator -g operator -m 0700 "${entropy_dir}" || {
6181f72adfSDoug Barton		logger -is -t "$0" The entropy directory "${entropy_dir}" does \
6281f72adfSDoug Barton		    not exist, and cannot be created. Therefore no entropy can \
6381f72adfSDoug Barton		    be saved.; exit 1; }
6427a803d6SDoug Bartonfi
6527a803d6SDoug Barton
6681f72adfSDoug Bartoncd "${entropy_dir}" || {
6781f72adfSDoug Barton	logger -is -t "$0" Cannot cd to the entropy directory: "${entropy_dir}". \
6881f72adfSDoug Barton	    Entropy file rotation is aborted.; exit 1; }
6981f72adfSDoug Barton
7081f72adfSDoug Bartonfor f in saved-entropy.*; do
7181f72adfSDoug Barton	case "${f}" in saved-entropy.\*) continue ;; esac	# No files match
7281f72adfSDoug Barton	[ ${f#saved-entropy\.} -ge ${entropy_save_num} ] && unlink ${f}
7381f72adfSDoug Bartondone
7481f72adfSDoug Barton
7527a803d6SDoug Bartonumask 377
7627a803d6SDoug Barton
7781f72adfSDoug Bartonn=$(( ${entropy_save_num} - 1 ))
7881f72adfSDoug Bartonwhile [ ${n} -ge 1 ]; do
7981f72adfSDoug Barton	if [ -f "saved-entropy.${n}" ]; then
8081f72adfSDoug Barton		mv "saved-entropy.${n}" "saved-entropy.$(( ${n} + 1 ))"
8181f72adfSDoug Barton	elif [ -e "saved-entropy.${n}" -o -L "saved-entropy.${n}" ]; then
8210fe5181SDoug Barton		logger -is -t "$0" \
8381f72adfSDoug Barton	"${entropy_dir}/saved-entropy.${n}" is not a regular file, and so \
8481f72adfSDoug Barton	    it will not be rotated. Entropy file rotation is aborted.
8510fe5181SDoug Barton		exit 1
8610fe5181SDoug Barton	fi
8781f72adfSDoug Barton	n=$(( ${n} - 1 ))
8827a803d6SDoug Bartondone
8927a803d6SDoug Barton
9081f72adfSDoug Bartondd if=/dev/random of=saved-entropy.1 bs=${entropy_save_sz} count=1 2>/dev/null
9127a803d6SDoug Barton
9227a803d6SDoug Bartonexit 0
93