Clean up REQUIRED_KEY_CAPABILITY option passing to process_user_id.
[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 authorizedUserIDs
21
22 if [ "$1" ] ; then
23     # get users from command line
24     unames="$@"
25 else         
26     # or just look at all users if none specified
27     unames=$(list_users)
28 fi
29
30 # set gnupg home
31 GNUPGHOME="$GNUPGHOME_SPHERE"
32
33 # the authorized_keys directory
34 authorizedKeysDir="${SYSDATADIR}/authorized_keys"
35
36 # check to see if the gpg trust database has been initialized
37 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
38     failure "GNUPG trust database uninitialized.  Please see MONKEYSPHERE-SERVER(8)."
39 fi
40
41 # make sure the authorized_keys directory exists
42 mkdir -p "${authorizedKeysDir}"
43
44 # loop over users
45 for uname in $unames ; do
46     # check all specified users exist
47     if ! id "$uname" >/dev/null ; then
48         log error "----- unknown user '$uname' -----"
49         continue
50     fi
51
52     log verbose "----- user: $uname -----"
53
54     # make temporary directory
55     TMPLOC=$(mktemp -d ${MATMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
56
57     # trap to delete temporary directory on exit
58     trap "rm -rf $TMPLOC" EXIT
59
60     # create temporary authorized_user_ids file
61     TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
62     touch "$TMP_AUTHORIZED_USER_IDS"
63
64      # create temporary authorized_keys file
65     AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
66     touch "$AUTHORIZED_KEYS"
67
68     # set restrictive permissions on the temporary files
69     # FIXME: is there a better way to do this?
70     chmod 0700 "$TMPLOC"
71     chmod 0600 "$AUTHORIZED_KEYS"
72     chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
73     chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
74
75     # process authorized_user_ids file
76     log debug "checking for authorized_user_ids..."
77     # translating ssh-style path variables
78     authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
79     if [ -s "$authorizedUserIDs" ] ; then
80         log debug "authorized_user_ids file found."
81         # check permissions on the authorized_user_ids file path
82         if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
83             # copy user authorized_user_ids file to temporary
84             # location
85             cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
86
87             # export needed variables
88             export AUTHORIZED_KEYS
89             export TMP_AUTHORIZED_USER_IDS
90
91             # process authorized_user_ids file, as monkeysphere user
92             su_monkeysphere_user \
93                 ". ${SYSSHAREDIR}/common; STRICT_MODES='$STRICT_MODES' process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS" \
94                 || returnCode="$?"
95         else
96             log debug "not processing authorized_user_ids."
97         fi
98     else
99         log debug "empty or absent authorized_user_ids file."
100     fi
101
102     # add user-controlled authorized_keys file if specified translate
103     # ssh-style path variables
104     rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
105     if [ "$rawAuthorizedKeys" != 'none' ] ; then
106         log debug "checking for raw authorized_keys..."
107         if [ -s "$rawAuthorizedKeys" ] ; then
108             # check permissions on the authorized_keys file path
109             if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
110                 log verbose "adding raw authorized_keys file... "
111                 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
112             else
113                 log debug "not adding raw authorized_keys file."                
114             fi
115         else
116             log debug "empty or absent authorized_keys file."
117         fi
118     fi
119
120     # move the new authorized_keys file into place
121     if [ -s "$AUTHORIZED_KEYS" ] ; then
122         # openssh appears to check the contents of the authorized_keys
123         # file as the user in question, so the file must be readable
124         # by that user at least.
125
126         # but in general, we don't want the user tampering with this
127         # file directly, so we'll adopt this approach: Own the file by
128         # the monkeysphere-server invoker (usually root, but should be
129         # the same uid that sshd is launched as); change the group of
130         # the file so that members of the user's group can read it.
131
132         # FIXME: is there a better way to do this?
133         chown $(whoami) "$AUTHORIZED_KEYS" && \
134             chgrp $(id -g "$uname") "$AUTHORIZED_KEYS" && \
135             chmod g+r "$AUTHORIZED_KEYS" && \
136             mv -f "$AUTHORIZED_KEYS" "${authorizedKeysDir}/${uname}" || \
137             { 
138             log error "Failed to install authorized_keys for '$uname'!"
139             rm -f "${authorizedKeysDir}/${uname}"
140             # indicate that there has been a failure:
141             returnCode=1
142         }
143     else
144         rm -f "${authorizedKeysDir}/${uname}"
145     fi
146
147     # unset the trap
148     trap - EXIT
149
150     # destroy temporary directory
151     rm -rf "$TMPLOC"
152 done
153
154 return $returnCode
155 }