random_seed revision 1.12
11.1Stls#!/bin/sh
21.1Stls#
31.12Sriastrad# $NetBSD: random_seed,v 1.12 2020/05/07 20:00:38 riastradh Exp $
41.1Stls#
51.1Stls
61.1Stls# PROVIDE: random_seed
71.1Stls# REQUIRE: mountcritlocal
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.6Schristos		message "Not present"
611.6Schristos		return
621.6Schristos	fi
631.1Stls
641.11Sriastrad	if ! fs_safe "${random_file}"; then
651.12Sriastrad		message "Unsafe file system"
661.10Sriastrad		flags=-i
671.6Schristos	fi
681.1Stls
691.6Schristos	set -- $(ls -ldn "${random_file}")
701.6Schristos	st_mode="$1" # should be "-rw-------"
711.6Schristos	st_uid="$3"  # should be "0" for root
721.6Schristos
731.6Schristos	# The file must be owned by root,
741.6Schristos	if [ "$st_uid" != "0" ]; then
751.6Schristos		message "Bad owner ${st_uid}"
761.10Sriastrad		flags=-i
771.6Schristos	fi
781.6Schristos	# and root read/write only.
791.6Schristos	if [ "$st_mode" != "-rw-------" ]; then
801.6Schristos		message "Bad mode ${st_mode}"
811.10Sriastrad		flags=-i
821.6Schristos	fi
831.1Stls
841.10Sriastrad	if rndctl $flags -L "${random_file}"; then
851.6Schristos		echo "Loaded entropy from ${random_file}."
861.1Stls	fi
871.1Stls}
881.1Stls
891.1Stlsrandom_save()
901.1Stls{
911.6Schristos	oum="$(umask)"
921.1Stls	umask 077
931.1Stls
941.4Sapb	if rndctl -S "${random_file}"; then
951.6Schristos		echo "Saved entropy to ${random_file}."
961.1Stls	fi
971.6Schristos	umask "${oum}"
981.1Stls}
991.1Stls
1001.1Stls
1011.6Schristosload_rc_config "${name}"
1021.1Stlsrun_rc_command "$1"
103