xref: /freebsd/libexec/save-entropy/save-entropy.sh (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
127a803d6SDoug Barton#!/bin/sh
227a803d6SDoug Barton#
3*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
4e6209940SPedro F. Giffuni#
581f72adfSDoug Barton# Copyright (c) 2001-2006,2012 Douglas Barton, dougb@FreeBSD.org
627a803d6SDoug Barton# All rights reserved.
727a803d6SDoug Barton#
827a803d6SDoug Barton# Redistribution and use in source and binary forms, with or without
927a803d6SDoug Barton# modification, are permitted provided that the following conditions
1027a803d6SDoug Barton# are met:
1127a803d6SDoug Barton# 1. Redistributions of source code must retain the above copyright
1227a803d6SDoug Barton#    notice, this list of conditions and the following disclaimer.
1327a803d6SDoug Barton# 2. Redistributions in binary form must reproduce the above copyright
1427a803d6SDoug Barton#    notice, this list of conditions and the following disclaimer in the
1527a803d6SDoug Barton#    documentation and/or other materials provided with the distribution.
1627a803d6SDoug Barton#
1727a803d6SDoug Barton# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1827a803d6SDoug Barton# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1927a803d6SDoug Barton# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2027a803d6SDoug Barton# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2127a803d6SDoug Barton# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2227a803d6SDoug Barton# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2327a803d6SDoug Barton# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2427a803d6SDoug Barton# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2527a803d6SDoug Barton# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2627a803d6SDoug Barton# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727a803d6SDoug Barton# SUCH DAMAGE.
2827a803d6SDoug Barton#
2927a803d6SDoug Barton# $FreeBSD$
3027a803d6SDoug Barton
3127a803d6SDoug Barton# This script is called by cron to store bits of randomness which are
3227a803d6SDoug Barton# then used to seed /dev/random on boot.
3327a803d6SDoug Barton
3481f72adfSDoug Barton# Originally developed by Doug Barton, dougb@FreeBSD.org
3510fe5181SDoug Barton
3627a803d6SDoug BartonPATH=/bin:/usr/bin
3727a803d6SDoug Barton
3827a803d6SDoug Barton# If there is a global system configuration file, suck it in.
3927a803d6SDoug Barton#
4027a803d6SDoug Bartonif [ -r /etc/defaults/rc.conf ]; then
4127a803d6SDoug Barton	. /etc/defaults/rc.conf
42e8ea7f02SDoug Barton	source_rc_confs 2>/dev/null
4327a803d6SDoug Bartonelif [ -r /etc/rc.conf ]; then
44e8ea7f02SDoug Barton	. /etc/rc.conf 2>/dev/null
4527a803d6SDoug Bartonfi
4627a803d6SDoug Barton
478fcb9ca9SXin LI[ $(/sbin/sysctl -n security.jail.jailed) = 0 ] || exit 0
488fcb9ca9SXin LI
4927a803d6SDoug Bartoncase ${entropy_dir} in
5027a803d6SDoug Barton[Nn][Oo])
5127a803d6SDoug Barton	exit 0
5227a803d6SDoug Barton	;;
5327a803d6SDoug Barton*)
5410fe5181SDoug Barton	entropy_dir=${entropy_dir:-/var/db/entropy}
5527a803d6SDoug Barton	;;
5627a803d6SDoug Bartonesac
5727a803d6SDoug Barton
5810cb2424SMark Murrayentropy_save_sz=${entropy_save_sz:-4096}
5927a803d6SDoug Bartonentropy_save_num=${entropy_save_num:-8}
6027a803d6SDoug Barton
6127a803d6SDoug Bartonif [ ! -d "${entropy_dir}" ]; then
6281f72adfSDoug Barton	install -d -o operator -g operator -m 0700 "${entropy_dir}" || {
6381f72adfSDoug Barton		logger -is -t "$0" The entropy directory "${entropy_dir}" does \
6481f72adfSDoug Barton		    not exist, and cannot be created. Therefore no entropy can \
6581f72adfSDoug Barton		    be saved.; exit 1; }
6627a803d6SDoug Bartonfi
6727a803d6SDoug Barton
6881f72adfSDoug Bartoncd "${entropy_dir}" || {
6981f72adfSDoug Barton	logger -is -t "$0" Cannot cd to the entropy directory: "${entropy_dir}". \
7081f72adfSDoug Barton	    Entropy file rotation is aborted.; exit 1; }
7181f72adfSDoug Barton
7281f72adfSDoug Bartonfor f in saved-entropy.*; do
7381f72adfSDoug Barton	case "${f}" in saved-entropy.\*) continue ;; esac	# No files match
7446413cedSXin LI	[ ${f#saved-entropy\.} -gt ${entropy_save_num} ] && unlink ${f}
7581f72adfSDoug Bartondone
7681f72adfSDoug Barton
7746413cedSXin LIumask 177
7827a803d6SDoug Barton
7946413cedSXin LI# Scan slots [1..$entropy_save_num), picking an empty slot or the oldest
8046413cedSXin LI# existing file if no empty slot was available.
8146413cedSXin LI#
8246413cedSXin LI# 1. Find out the first regular file or empty slot (and its serial number)
8346413cedSXin LI#
8446413cedSXin LIn=1
8546413cedSXin LIwhile [ ${n} -le ${entropy_save_num} ]; do
8646413cedSXin LI	save_file="saved-entropy.${n}"
8746413cedSXin LI	if [ ! -e "${save_file}" -o -f "${save_file}" ]; then
8846413cedSXin LI		break
8946413cedSXin LI	else
9010fe5181SDoug Barton		logger -is -t "$0" \
9146413cedSXin LI		    "${save_file}" is not a regular file, skipped.
9246413cedSXin LI	fi
9346413cedSXin LI	n=$(( ${n} + 1 ))
9446413cedSXin LIdone
9546413cedSXin LI#
9646413cedSXin LI# 2. Start from (serial number + 1), and check if the slot is empty
9746413cedSXin LI#    or is an older regular file, update save_file pointer in either
9846413cedSXin LI#    case, and break early if we found an empty slot.
9946413cedSXin LI#
10046413cedSXin LIif [ -f ${save_file} ]; then
10146413cedSXin LI	n=$(( ${n} + 1 ))
10246413cedSXin LI	while [ ${n} -le ${entropy_save_num} ]; do
10346413cedSXin LI		next_file=saved-entropy.${n}
10446413cedSXin LI		if [ -f "${next_file}" ]; then
10546413cedSXin LI			[ "${next_file}" -ot "${save_file}" ] && \
10646413cedSXin LI			    save_file="${next_file}"
10746413cedSXin LI		elif [ ! -e "${next_file}" ]; then
10846413cedSXin LI			save_file="${next_file}"
10946413cedSXin LI			break
11046413cedSXin LI		else
11146413cedSXin LI			logger -is -t "$0" \
11246413cedSXin LI			    "${next_file}" is not a regular file, skipped.
11346413cedSXin LI		fi
11446413cedSXin LI		n=$(( ${n} + 1 ))
11546413cedSXin LI	done
11646413cedSXin LIfi
11746413cedSXin LI#
11846413cedSXin LI# 3. Check if the pointer we have in hand is really a regular file or
11946413cedSXin LI#    an empty slot, and bail out as that means there is no available slot.
12046413cedSXin LI#
12146413cedSXin LIif [ -e "${save_file}" -a ! -f "${save_file}" ]; then
12246413cedSXin LI	logger -is -t "$0" \
12346413cedSXin LI		No available slot in "${entropy_dir}", save entropy is aborted.
12410fe5181SDoug Barton	exit 1
12510fe5181SDoug Bartonfi
12627a803d6SDoug Barton
12746413cedSXin LI# Save entropy to the selected slot.
12846413cedSXin LIchmod 600 "${save_file}" 2>/dev/null || :
12946413cedSXin LIdd if=/dev/random of="${save_file}" bs=${entropy_save_sz} count=1 2>/dev/null
13046413cedSXin LIchflags nodump "${save_file}" 2>/dev/null || :
13146413cedSXin LIfsync "${save_file}" "."
13227a803d6SDoug Barton
13327a803d6SDoug Bartonexit 0
134