3 # Monkeysphere authentication update-users subcommand
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 # Jamie McClelland <jm@mayfirst.org>
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
10 # They are Copyright 2008, and are all released under the GPL, version 3
16 # get users from command line
19 # or just look at all users if none specified
20 unames=$(getent passwd | cut -d: -f1)
26 MODE="authorized_keys"
29 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
31 # check to see if the gpg trust database has been initialized
32 if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
33 failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
36 # make sure the authorized_keys directory exists
37 mkdir -p "${SYSDATADIR}/authorized_keys"
40 for uname in $unames ; do
41 # check all specified users exist
42 if ! id "$uname" >/dev/null ; then
43 log error "----- unknown user '$uname' -----"
47 log verbose "----- user: $uname -----"
49 # make temporary directory
50 TMPLOC=$(mktemp -d ${MSTMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
52 # trap to delete temporary directory on exit
53 trap "rm -rf $TMPLOC" EXIT
55 # create temporary authorized_user_ids file
56 TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
57 touch "$TMP_AUTHORIZED_USER_IDS"
59 # create temporary authorized_keys file
60 AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
61 touch "$AUTHORIZED_KEYS"
63 # set restrictive permissions on the temporary files
64 # FIXME: is there a better way to do this?
66 chmod 0600 "$AUTHORIZED_KEYS"
67 chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
68 chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
70 # process authorized_user_ids file
71 log debug "checking for authorized_user_ids..."
72 # translating ssh-style path variables
73 authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
74 if [ -s "$authorizedUserIDs" ] ; then
75 # check permissions on the authorized_user_ids file path
76 if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
77 # copy user authorized_user_ids file to temporary
79 cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
81 # export needed variables
82 export AUTHORIZED_KEYS
83 export TMP_AUTHORIZED_USER_IDS
85 # process authorized_user_ids file, as monkeysphere user
86 su_monkeysphere_user \
87 ". ${SYSSHAREDIR}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
90 log debug "not processing authorized_user_ids."
93 log debug "empty or absent authorized_user_ids file."
96 # add user-controlled authorized_keys file if specified translate
97 # ssh-style path variables
98 rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
99 if [ "$rawAuthorizedKeys" != 'none' ] ; then
100 log debug "checking for raw authorized_keys..."
101 if [ -s "$rawAuthorizedKeys" ] ; then
102 # check permissions on the authorized_keys file path
103 if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
104 log verbose "adding raw authorized_keys file... "
105 cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
107 log debug "not adding raw authorized_keys file."
110 log debug "empty or absent authorized_keys file."
114 # move the new authorized_keys file into place
115 if [ -s "$AUTHORIZED_KEYS" ] ; then
116 # openssh appears to check the contents of the authorized_keys
117 # file as the user in question, so the file must be readable
118 # by that user at least.
120 # but in general, we don't want the user tampering with this
121 # file directly, so we'll adopt this approach: Own the file by
122 # the monkeysphere-server invoker (usually root, but should be
123 # the same uid that sshd is launched as); change the group of
124 # the file so that members of the user's group can read it.
126 # FIXME: is there a better way to do this?
127 chown $(whoami) "$AUTHORIZED_KEYS" && \
128 chgrp $(id -g "$uname") "$AUTHORIZED_KEYS" && \
129 chmod g+r "$AUTHORIZED_KEYS" && \
130 mv -f "$AUTHORIZED_KEYS" "${SYSDATADIR}/authorized_keys/${uname}" || \
132 log error "Failed to install authorized_keys for '$uname'!"
133 rm -f "${SYSDATADIR}/authorized_keys/${uname}"
134 # indicate that there has been a failure:
138 rm -f "${SYSDATADIR}/authorized_keys/${uname}"
144 # destroy temporary directory