Home | History | Annotate | Line # | Download | only in rc.d
random_seed revision 1.3
      1  1.1   tls #!/bin/sh
      2  1.1   tls #
      3  1.3   apb # $NetBSD: random_seed,v 1.3 2012/11/10 15:10:22 apb Exp $
      4  1.1   tls #
      5  1.1   tls 
      6  1.1   tls # PROVIDE: random_seed
      7  1.1   tls # REQUIRE: mountcritlocal
      8  1.1   tls # BEFORE: securelevel
      9  1.3   apb # BEFORE: bootconf
     10  1.1   tls # KEYWORD: shutdown
     11  1.3   apb #
     12  1.3   apb # The "BEFORE: securelevel" is a real dependency, in that
     13  1.3   apb # this script won't work if run after the securelevel is changed.
     14  1.3   apb #
     15  1.3   apb # The "BEFORE: bootconf" is intended to cause this to
     16  1.3   apb # be the first script to runs after mountcritlocal.
     17  1.1   tls 
     18  1.1   tls $_rc_subr_loaded . /etc/rc.subr
     19  1.1   tls 
     20  1.1   tls name="random_seed"
     21  1.1   tls rcvar=$name
     22  1.1   tls start_cmd="random_load"
     23  1.1   tls stop_cmd="random_save"
     24  1.1   tls 
     25  1.1   tls random_file=${random_file:-/var/db/entropy-file}
     26  1.1   tls 
     27  1.1   tls fs_safe()
     28  1.1   tls {
     29  1.1   tls 	#
     30  1.1   tls 	# Enforce that the file's on a local filesystem.
     31  1.1   tls 	# Include only the types we can actually write.
     32  1.1   tls 	#
     33  1.1   tls 	fstype=$(df -G $1 | awk '$2 == "fstype" {print $1}')
     34  1.1   tls 	case $fstype in
     35  1.1   tls 	    ffs)
     36  1.1   tls 		return 0
     37  1.1   tls 		;;
     38  1.1   tls 	    lfs)
     39  1.1   tls 		return 0
     40  1.1   tls 		;;
     41  1.1   tls 	    ext2fs)
     42  1.1   tls 		return 0;
     43  1.1   tls 		;;
     44  1.2  hans 	    msdos)
     45  1.1   tls 		return 0;
     46  1.1   tls 		;;
     47  1.1   tls 	    v7fs)
     48  1.1   tls 		return 0;
     49  1.1   tls 		;;
     50  1.1   tls 	 esac
     51  1.1   tls 	 return 1
     52  1.1   tls }
     53  1.1   tls 
     54  1.1   tls random_load()
     55  1.1   tls {
     56  1.1   tls 	if [ -f $random_file ]; then
     57  1.1   tls 
     58  1.1   tls 		if ! fs_safe $(dirname ${random_file}); then
     59  1.1   tls 			return 1
     60  1.1   tls 		fi
     61  1.1   tls 
     62  1.1   tls 		eval $(stat -s ${random_file})
     63  1.1   tls 
     64  1.1   tls 		# The file must be owned by root,
     65  1.1   tls 		if [ "$st_uid" != "0" ]; then
     66  1.1   tls 			return 1
     67  1.1   tls 		fi
     68  1.1   tls 		# and root read/write only.
     69  1.1   tls 		if [ "$(echo $st_mode | tail -c4)" != "600" ]; then
     70  1.1   tls 			return 1
     71  1.1   tls 		fi
     72  1.1   tls 
     73  1.1   tls 		if rndctl -L ${random_file}; then
     74  1.1   tls 			echo "Loaded entropy from disk."
     75  1.1   tls 		fi
     76  1.1   tls 		
     77  1.1   tls 	fi
     78  1.1   tls }
     79  1.1   tls 
     80  1.1   tls random_save()
     81  1.1   tls {
     82  1.1   tls 	oum=$(umask)
     83  1.1   tls 	umask 077
     84  1.1   tls 
     85  1.1   tls 	rm -Pf ${random_file}
     86  1.1   tls 
     87  1.1   tls 	if ! fs_safe $(dirname ${random_file}); then
     88  1.1   tls 		return 1
     89  1.1   tls 	fi
     90  1.1   tls 
     91  1.1   tls 	if rndctl -S ${random_file}; then
     92  1.1   tls 		echo "Saved entropy to disk."
     93  1.1   tls 	fi
     94  1.1   tls }
     95  1.1   tls 
     96  1.1   tls 
     97  1.1   tls load_rc_config $name
     98  1.1   tls run_rc_command "$1"
     99