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,
19 local authorizedKeysDir
20 local tmpAuthorizedKeys
21 local authorizedUserIDs
24 # get users from command line
27 # or just look at all users if none specified
32 GNUPGHOME="$GNUPGHOME_SPHERE"
34 # the authorized_keys directory
35 authorizedKeysDir="${SYSDATADIR}/authorized_keys"
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)."
42 # make sure the authorized_keys directory exists
43 mkdir -p "${authorizedKeysDir}"
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' -----"
53 log verbose "----- user: $uname -----"
55 # make temporary directory
56 TMPLOC=$(mktemp -d ${MATMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
58 # trap to delete temporary directory on exit
59 trap "rm -rf $TMPLOC" EXIT
61 # create temporary authorized_keys file
62 tmpAuthorizedKeys="${TMPLOC}/authorized_keys"
63 touch "$tmpAuthorizedKeys"
65 # set restrictive permissions on the temporary files
66 # FIXME: is there a better way to do this?
68 chmod 0600 "$tmpAuthorizedKeys"
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 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
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"
87 log debug "not processing authorized_user_ids."
90 log debug "empty or absent authorized_user_ids file."
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"
104 log debug "not adding raw authorized_keys file."
107 log debug "empty or absent authorized_keys file."
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.
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.
123 if [ "$OUTPUT_STDOUT" ] ; then
124 log debug "outputting keys to stdout..."
125 cat "$tmpAuthorizedKeys"
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}" || \
134 log error "Failed to install authorized_keys for '$uname'!"
135 rm -f "${authorizedKeysDir}/${uname}"
136 # indicate that there has been a failure:
141 rm -f "${authorizedKeysDir}/${uname}"
147 # destroy temporary directory