Path: blob/main/libexec/save-entropy/save-entropy.sh
34821 views
#!/bin/sh1#2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2001-2006,2012 Douglas Barton, [email protected]5# All rights reserved.6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions9# are met:10# 1. Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# 2. Redistributions in binary form must reproduce the above copyright13# notice, this list of conditions and the following disclaimer in the14# documentation and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26# SUCH DAMAGE.27#2829# This script is called by cron to store bits of randomness which are30# then used to seed /dev/random on boot.3132# Originally developed by Doug Barton, [email protected]3334PATH=/bin:/usr/bin3536# If there is a global system configuration file, suck it in.37#38if [ -r /etc/defaults/rc.conf ]; then39. /etc/defaults/rc.conf40source_rc_confs 2>/dev/null41elif [ -r /etc/rc.conf ]; then42. /etc/rc.conf 2>/dev/null43fi4445[ $(/sbin/sysctl -n security.jail.jailed) = 0 ] || exit 04647case ${entropy_dir} in48[Nn][Oo])49exit 050;;51*)52entropy_dir=${entropy_dir:-/var/db/entropy}53;;54esac5556entropy_save_sz=${entropy_save_sz:-4096}57entropy_save_num=${entropy_save_num:-8}5859if [ ! -d "${entropy_dir}" ]; then60install -d -o operator -g operator -m 0700 "${entropy_dir}" || {61logger -is -t "$0" The entropy directory "${entropy_dir}" does \62not exist, and cannot be created. Therefore no entropy can \63be saved.; exit 1; }64fi6566cd "${entropy_dir}" || {67logger -is -t "$0" Cannot cd to the entropy directory: "${entropy_dir}". \68Entropy file rotation is aborted.; exit 1; }6970for f in saved-entropy.*; do71case "${f}" in saved-entropy.\*) continue ;; esac # No files match72[ ${f#saved-entropy\.} -gt ${entropy_save_num} ] && unlink ${f}73done7475umask 1777677# Scan slots [1..$entropy_save_num), picking an empty slot or the oldest78# existing file if no empty slot was available.79#80# 1. Find out the first regular file or empty slot (and its serial number)81#82n=183while [ ${n} -le ${entropy_save_num} ]; do84save_file="saved-entropy.${n}"85if [ ! -e "${save_file}" -o -f "${save_file}" ]; then86break87else88logger -is -t "$0" \89"${save_file}" is not a regular file, skipped.90fi91n=$(( ${n} + 1 ))92done93#94# 2. Start from (serial number + 1), and check if the slot is empty95# or is an older regular file, update save_file pointer in either96# case, and break early if we found an empty slot.97#98if [ -f ${save_file} ]; then99n=$(( ${n} + 1 ))100while [ ${n} -le ${entropy_save_num} ]; do101next_file=saved-entropy.${n}102if [ -f "${next_file}" ]; then103[ "${next_file}" -ot "${save_file}" ] && \104save_file="${next_file}"105elif [ ! -e "${next_file}" ]; then106save_file="${next_file}"107break108else109logger -is -t "$0" \110"${next_file}" is not a regular file, skipped.111fi112n=$(( ${n} + 1 ))113done114fi115#116# 3. Check if the pointer we have in hand is really a regular file or117# an empty slot, and bail out as that means there is no available slot.118#119if [ -e "${save_file}" -a ! -f "${save_file}" ]; then120logger -is -t "$0" \121No available slot in "${entropy_dir}", save entropy is aborted.122exit 1123fi124125# Save entropy to the selected slot.126chmod 600 "${save_file}" 2>/dev/null || :127dd if=/dev/random of="${save_file}" bs=${entropy_save_sz} count=1 2>/dev/null128chflags nodump "${save_file}" 2>/dev/null || :129fsync "${save_file}" "."130131exit 0132133134