3 # a set of useful functions for managing operations with passwords.
4 # a set of very simple operations, but the file needs to be protected from
5 # undesirable access. a good way to do that is to make the file owned by
6 # root, and for it to have permssions of "600" (full access by owner only),
7 # and to only try to read the password file when in sudo mode. the first
8 # two requirements are done automatically by the store_password function.
11 # provides a way to read a password out of a file. the filename is the first
12 # paramater and the variable to fill with the password is the second.
13 function load_password()
15 local passfile="$1"; shift
16 local varname="$1"; shift
17 if [ -z "$passfile" ]; then
18 echo 'The load_password function needs a filename to read the password from.'
21 if [ ! -f "$passfile" ]; then
22 # no file, which is not an error necessarily, but return a blank password
27 read passwd < "$passfile"
29 # return the password in the variable they provided.
30 eval $varname="$passwd"
31 #echo varname is: $varname
32 #echo new value of that variable is ${!varname}
35 # stores a password into a password file. the password file should be the
36 # first parameter and the password should be the second.
37 # this makes sure that only root can read the file.
38 function store_password()
40 local passfile="$1"; shift
41 local passwd="$1"; shift
42 if [ -z "$passfile" -o -z "$passwd" ]; then
44 The store_password function needs (1) the file to store the password into,
45 and (2) the password that should be stored.
50 echo "$passwd" > "$passfile"
51 test_or_die "writing password into the file $passfile"
53 chown root:root "$passfile"
54 test_or_die "chowning the password file to root ownership for: $passfile"
57 test_or_die "restricting permissions on password file for: $passfile"
60 # reads a password from the console, without echoing the letters when they
61 # are typed. the prompt to show the user is required as the first parameter,
62 # and the variable to fill with the result is the second parameter.
63 function read_password()
65 local prompt="$1"; shift
66 local varname="$1"; shift
67 #hmmm: complain if not enough parms.
69 # turn off echo but remember former setting.
76 # return the password in the variable they provided.
77 eval $varname="$the_passwd"
78 #echo varname is: $varname
79 #echo new value of that variable is ${!varname}