random_seed revision 1.15
11.1Stls#!/bin/sh 21.1Stls# 31.15Smartin# $NetBSD: random_seed,v 1.15 2020/09/08 12:52:18 martin Exp $ 41.1Stls# 51.1Stls 61.1Stls# PROVIDE: random_seed 71.15Smartin# REQUIRE: CRITLOCALMOUNTED 81.1Stls# BEFORE: securelevel 91.3Sapb# BEFORE: bootconf 101.1Stls# KEYWORD: shutdown 111.3Sapb# 121.3Sapb# The "BEFORE: securelevel" is a real dependency, in that 131.3Sapb# this script won't work if run after the securelevel is changed. 141.3Sapb# 151.3Sapb# The "BEFORE: bootconf" is intended to cause this to 161.4Sapb# be the first script that runs after mountcritlocal. 171.1Stls 181.1Stls$_rc_subr_loaded . /etc/rc.subr 191.1Stls 201.1Stlsname="random_seed" 211.1Stlsrcvar=$name 221.1Stlsstart_cmd="random_load" 231.1Stlsstop_cmd="random_save" 241.1Stls 251.6Schristosrandom_file="${random_file:-/var/db/entropy-file}" 261.6Schristos 271.6Schristosmessage() 281.6Schristos{ 291.6Schristos echo "${name}: ${random_file}: $@" 1>&2 301.6Schristos} 311.6Schristos 321.1Stlsfs_safe() 331.1Stls{ 341.10Sriastrad # Consider the root file system safe always. 351.10Sriastrad df -P "$1" | (while read dev total used avail cap mountpoint; do 361.10Sriastrad case $mountpoint in 371.10Sriastrad 'Mounted on') continue;; 381.10Sriastrad /) exit 0;; 391.10Sriastrad *) exit 1;; 401.10Sriastrad esac 411.10Sriastrad done) && return 0 421.10Sriastrad 431.10Sriastrad # Otherwise, consider local file systems safe and non-local 441.10Sriastrad # file systems unsafe. 451.10Sriastrad case $(df -l "$1") in 461.10Sriastrad *Warning:*) 471.10Sriastrad return 1 481.1Stls ;; 491.6Schristos *) 501.10Sriastrad return 0 511.1Stls ;; 521.6Schristos esac 531.1Stls} 541.1Stls 551.1Stlsrandom_load() 561.1Stls{ 571.10Sriastrad local flags= 581.10Sriastrad 591.6Schristos if [ ! -f "${random_file}" ]; then 601.13Sriastrad message "Not present; creating" 611.13Sriastrad random_save 621.6Schristos return 631.6Schristos fi 641.1Stls 651.11Sriastrad if ! fs_safe "${random_file}"; then 661.12Sriastrad message "Unsafe file system" 671.10Sriastrad flags=-i 681.6Schristos fi 691.1Stls 701.6Schristos set -- $(ls -ldn "${random_file}") 711.6Schristos st_mode="$1" # should be "-rw-------" 721.6Schristos st_uid="$3" # should be "0" for root 731.6Schristos 741.6Schristos # The file must be owned by root, 751.6Schristos if [ "$st_uid" != "0" ]; then 761.6Schristos message "Bad owner ${st_uid}" 771.10Sriastrad flags=-i 781.6Schristos fi 791.6Schristos # and root read/write only. 801.6Schristos if [ "$st_mode" != "-rw-------" ]; then 811.6Schristos message "Bad mode ${st_mode}" 821.10Sriastrad flags=-i 831.6Schristos fi 841.1Stls 851.10Sriastrad if rndctl $flags -L "${random_file}"; then 861.6Schristos echo "Loaded entropy from ${random_file}." 871.1Stls fi 881.1Stls} 891.1Stls 901.1Stlsrandom_save() 911.1Stls{ 921.6Schristos oum="$(umask)" 931.1Stls umask 077 941.1Stls 951.4Sapb if rndctl -S "${random_file}"; then 961.6Schristos echo "Saved entropy to ${random_file}." 971.1Stls fi 981.6Schristos umask "${oum}" 991.1Stls} 1001.1Stls 1011.1Stls 1021.6Schristosload_rc_config "${name}" 1031.1Stlsrun_rc_command "$1" 104