2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
4 # Monkeysphere authentication update-users subcommand
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>
11 # They are Copyright 2008-2009, and are all released under the GPL,
17 # get users from command line
20 # or just look at all users if none specified
21 unames=$(getent passwd | cut -d: -f1)
27 MODE="authorized_keys"
30 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
32 # check to see if the gpg trust database has been initialized
33 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
34 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
37 # make sure the authorized_keys directory exists
38 mkdir -p "${SYSDATADIR}/authorized_keys"
41 for uname in $unames ; do
42 # check all specified users exist
43 if ! id "$uname" >/dev/null ; then
44 log error "----- unknown user '$uname' -----"
48 log verbose "----- user: $uname -----"
50 # make temporary directory
51 TMPLOC=$(mktemp -d ${MSTMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
53 # trap to delete temporary directory on exit
54 trap "rm -rf $TMPLOC" EXIT
56 # create temporary authorized_user_ids file
57 TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
58 touch "$TMP_AUTHORIZED_USER_IDS"
60 # create temporary authorized_keys file
61 AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
62 touch "$AUTHORIZED_KEYS"
64 # set restrictive permissions on the temporary files
65 # FIXME: is there a better way to do this?
67 chmod 0600 "$AUTHORIZED_KEYS"
68 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
69 chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
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 # check permissions on the authorized_user_ids file path
77 if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
78 # copy user authorized_user_ids file to temporary
80 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
82 # export needed variables
83 export AUTHORIZED_KEYS
84 export TMP_AUTHORIZED_USER_IDS
86 # process authorized_user_ids file, as monkeysphere user
87 su_monkeysphere_user \
88 ". ${SYSSHAREDIR}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
91 log debug "not processing authorized_user_ids."
94 log debug "empty or absent authorized_user_ids file."
97 # add user-controlled authorized_keys file if specified translate
98 # ssh-style path variables
99 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
100 if [ "$rawAuthorizedKeys" != 'none' ] ; then
101 log debug "checking for raw authorized_keys..."
102 if [ -s "$rawAuthorizedKeys" ] ; then
103 # check permissions on the authorized_keys file path
104 if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
105 log verbose "adding raw authorized_keys file... "
106 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
108 log debug "not adding raw authorized_keys file."
111 log debug "empty or absent authorized_keys file."
115 # move the new authorized_keys file into place
116 if [ -s "$AUTHORIZED_KEYS" ] ; then
117 # openssh appears to check the contents of the authorized_keys
118 # file as the user in question, so the file must be readable
119 # by that user at least.
121 # but in general, we don't want the user tampering with this
122 # file directly, so we'll adopt this approach: Own the file by
123 # the monkeysphere-server invoker (usually root, but should be
124 # the same uid that sshd is launched as); change the group of
125 # the file so that members of the user's group can read it.
127 # FIXME: is there a better way to do this?
128 chown $(whoami) "$AUTHORIZED_KEYS" && \
129 chgrp $(id -g "$uname") "$AUTHORIZED_KEYS" && \
130 chmod g+r "$AUTHORIZED_KEYS" && \
131 mv -f "$AUTHORIZED_KEYS" "${SYSDATADIR}/authorized_keys/${uname}" || \
133 log error "Failed to install authorized_keys for '$uname'!"
134 rm -f "${SYSDATADIR}/authorized_keys/${uname}"
135 # indicate that there has been a failure:
139 rm -f "${SYSDATADIR}/authorized_keys/${uname}"
145 # destroy temporary directory