save-entropy.sh revision 144889
1119026Sume#!/bin/sh 266776Skris# 355163Sshin# Copyright (c) 2001-2005 Douglas Barton, DougB@FreeBSD.org 455163Sshin# All rights reserved. 555163Sshin# 662632Skris# Redistribution and use in source and binary forms, with or without 755163Sshin# modification, are permitted provided that the following conditions 855163Sshin# are met: 955163Sshin# 1. Redistributions of source code must retain the above copyright 1055163Sshin# notice, this list of conditions and the following disclaimer. 1155163Sshin# 2. Redistributions in binary form must reproduce the above copyright 1255163Sshin# notice, this list of conditions and the following disclaimer in the 1355163Sshin# documentation and/or other materials provided with the distribution. 1455163Sshin# 1555163Sshin# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1655163Sshin# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1755163Sshin# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1862632Skris# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1955163Sshin# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2055163Sshin# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2155163Sshin# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2255163Sshin# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2355163Sshin# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2455163Sshin# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2555163Sshin# SUCH DAMAGE. 2655163Sshin# 2755163Sshin# $FreeBSD: head/libexec/save-entropy/save-entropy.sh 144889 2005-04-11 02:07:33Z dougb $ 2855163Sshin 2955163Sshin# This script is called by cron to store bits of randomness which are 3055163Sshin# then used to seed /dev/random on boot. 3155163Sshin 3255163Sshin# Originally developed by Doug Barton, DougB@FreeBSD.org 3355163Sshin 3455163SshinPATH=/bin:/usr/bin 3555163Sshin 3662632Skris# If there is a global system configuration file, suck it in. 37118909Sume# 3855163Sshinif [ -r /etc/defaults/rc.conf ]; then 3962632Skris . /etc/defaults/rc.conf 4055163Sshin source_rc_confs 4155163Sshinelif [ -r /etc/rc.conf ]; then 4255163Sshin . /etc/rc.conf 4355163Sshinfi 4455163Sshin 4555163Sshincase ${entropy_dir} in 4655163Sshin[Nn][Oo]) 4755163Sshin exit 0 4855163Sshin ;; 4955163Sshin*) 5055163Sshin entropy_dir=${entropy_dir:-/var/db/entropy} 5155163Sshin ;; 5255163Sshinesac 5355163Sshin 5466776Skrisentropy_save_sz=${entropy_save_sz:-2048} 55118916Sumeentropy_save_num=${entropy_save_num:-8} 56118916Sume 57118916Sumeif [ ! -d "${entropy_dir}" ]; then 58118664Sume umask 077 5955163Sshin mkdir "${entropy_dir}" || { 6055163Sshin logger -is -t "$0" The entropy directory "${entropy_dir}" does not \ 6155163Sshinexist, and cannot be created. Therefore no entropy can be saved. ; 6255163Sshin exit 1;} 63118664Sume /usr/sbin/chown operator:operator "${entropy_dir}" 64118664Sume chmod 0700 "${entropy_dir}" 65124525Sumefi 66118664Sume 6766776Skrisumask 377 6866776Skris 69118664Sumefor file_num in `jot ${entropy_save_num} ${entropy_save_num} 1`; do 70118661Sume if [ -e "${entropy_dir}/saved-entropy.${file_num}" ]; then 7155163Sshin if [ -f "${entropy_dir}/saved-entropy.${file_num}" ]; then 72147150Ssuz new_num=$(($file_num + 1)) 7362632Skris if [ "${new_num}" -gt "${entropy_save_num}" ]; then 7462632Skris rm -f "${entropy_dir}/saved-entropy.${file_num}" 7562632Skris else 7655163Sshin mv "${entropy_dir}/saved-entropy.${file_num}" \ 77118664Sume "${entropy_dir}/saved-entropy.${new_num}" 78118910Sume fi 79118664Sume else 80118664Sume logger -is -t "$0" \ 81118664Sume"${entropy_dir}/saved-entropy.${file_num} is not a regular file, and therefore \ 8255163Sshinit will not be rotated. Entropy file harvesting is aborted." 83173412Skevlo exit 1 8455163Sshin fi 8555163Sshin fi 8655163Sshindone 87124526Sume 8855163Sshindd if=/dev/random of="${entropy_dir}/saved-entropy.1" \ 8962632Skris bs="$entropy_save_sz" count=1 2> /dev/null 90124526Sume 91124526Sumeexit 0 9255163Sshin 93124526Sume