43695e221d9e9b818af727dc924c1918cea5e45e
[monkeysphere.git] / src / share / ma / update_users
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere authentication update-users subcommand
5 #
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 update_users() {
15
16 local returnCode=0
17 local unames
18 local uname
19 local authorizedKeysDir
20 local tmpAuthorizedKeys
21 local authorizedUserIDs
22
23 if [ "$1" ] ; then
24     # get users from command line
25     unames="$@"
26 else         
27     # or just look at all users if none specified
28     unames=$(list_users)
29 fi
30
31 # set gnupg home
32 GNUPGHOME="$GNUPGHOME_SPHERE"
33
34 # the authorized_keys directory
35 authorizedKeysDir="${SYSDATADIR}/authorized_keys"
36
37 # check to see if the gpg trust database has been initialized
38 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
39     failure "GNUPG trust database uninitialized.  Please see MONKEYSPHERE-SERVER(8)."
40 fi
41
42 # make sure the authorized_keys directory exists
43 mkdir -p "${authorizedKeysDir}"
44
45 # loop over users
46 for uname in $unames ; do
47     # check all specified users exist
48     if ! id "$uname" >/dev/null ; then
49         log error "----- unknown user '$uname' -----"
50         continue
51     fi
52
53     log verbose "----- user: $uname -----"
54
55     # make temporary directory
56     TMPLOC=$(mktemp -d ${MATMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
57
58     # trap to delete temporary directory on exit
59     trap "rm -rf $TMPLOC" EXIT
60
61      # create temporary authorized_keys file
62     tmpAuthorizedKeys="${TMPLOC}/authorized_keys"
63     touch "$tmpAuthorizedKeys"
64
65     # set restrictive permissions on the temporary files
66     # FIXME: is there a better way to do this?
67     chmod 0700 "$TMPLOC"
68     chmod 0600 "$tmpAuthorizedKeys"
69     chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
70
71     # process authorized_user_ids file
72     log debug "checking for authorized_user_ids..."
73     # translating ssh-style path variables
74     authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
75     if [ -s "$authorizedUserIDs" ] ; then
76         log debug "authorized_user_ids file found."
77         # check permissions on the authorized_user_ids file path
78         if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
79
80             # process authorized_user_ids file, as monkeysphere user
81             su_monkeysphere_user \
82                 ". ${SYSSHAREDIR}/common; STRICT_MODES='$STRICT_MODES' process_authorized_user_ids -" \
83                 < "$authorizedUserIDs" \
84                 > "$tmpAuthorizedKeys"
85
86         else
87             log debug "not processing authorized_user_ids."
88         fi
89     else
90         log debug "empty or absent authorized_user_ids file."
91     fi
92
93     # add user-controlled authorized_keys file if specified translate
94     # ssh-style path variables
95     rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
96     if [ "$rawAuthorizedKeys" != 'none' ] ; then
97         log debug "checking for raw authorized_keys..."
98         if [ -s "$rawAuthorizedKeys" ] ; then
99             # check permissions on the authorized_keys file path
100             if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
101                 log verbose "adding raw authorized_keys file... "
102                 cat "$rawAuthorizedKeys" >> "$tmpAuthorizedKeys"
103             else
104                 log debug "not adding raw authorized_keys file."                
105             fi
106         else
107             log debug "empty or absent authorized_keys file."
108         fi
109     fi
110
111     # move the new authorized_keys file into place
112     if [ -s "$tmpAuthorizedKeys" ] ; then
113         # openssh appears to check the contents of the authorized_keys
114         # file as the user in question, so the file must be readable
115         # by that user at least.
116
117         # but in general, we don't want the user tampering with this
118         # file directly, so we'll adopt this approach: Own the file by
119         # the monkeysphere-server invoker (usually root, but should be
120         # the same uid that sshd is launched as); change the group of
121         # the file so that members of the user's group can read it.
122
123         if [ "$OUTPUT_STDOUT" ] ; then
124             log debug "outputting keys to stdout..."
125             cat "$tmpAuthorizedKeys"
126         else
127             log debug "moving new file to ${authorizedKeysDir}/${uname}..."
128             # FIXME: is there a better way to do this?
129             chown $(whoami) "$tmpAuthorizedKeys" && \
130                 chgrp $(id -g "$uname") "$tmpAuthorizedKeys" && \
131                 chmod g+r "$tmpAuthorizedKeys" && \
132                 mv -f "$tmpAuthorizedKeys" "${authorizedKeysDir}/${uname}" || \
133                 {
134                 log error "Failed to install authorized_keys for '$uname'!"
135                 rm -f "${authorizedKeysDir}/${uname}"
136                 # indicate that there has been a failure:
137                 returnCode=1
138             }
139         fi
140     else
141         rm -f "${authorizedKeysDir}/${uname}"
142     fi
143
144     # unset the trap
145     trap - EXIT
146
147     # destroy temporary directory
148     rm -rf "$TMPLOC"
149 done
150
151 return $returnCode
152 }