0335e31448f9b3dbc409bf71af406f62ff5cc27f
[monkeysphere.git] / src / subcommands / 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 if [ "$1" ] ; then
17     # get users from command line
18     unames="$@"
19 else         
20     # or just look at all users if none specified
21     unames=$(getent passwd | cut -d: -f1)
22 fi
23
24 RETCODE=0
25
26 # set mode
27 MODE="authorized_keys"
28
29 # set gnupg home
30 GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
31
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)."
35 fi
36
37 # make sure the authorized_keys directory exists
38 mkdir -p "${SYSDATADIR}/authorized_keys"
39
40 # loop over users
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' -----"
45         continue
46     fi
47
48     log verbose "----- user: $uname -----"
49
50     # make temporary directory
51     TMPLOC=$(mktemp -d ${MSTMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
52
53     # trap to delete temporary directory on exit
54     trap "rm -rf $TMPLOC" EXIT
55
56     # create temporary authorized_user_ids file
57     TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
58     touch "$TMP_AUTHORIZED_USER_IDS"
59
60      # create temporary authorized_keys file
61     AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
62     touch "$AUTHORIZED_KEYS"
63
64     # set restrictive permissions on the temporary files
65     # FIXME: is there a better way to do this?
66     chmod 0700 "$TMPLOC"
67     chmod 0600 "$AUTHORIZED_KEYS"
68     chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
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         # 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
79             # location
80             cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
81
82             # export needed variables
83             export AUTHORIZED_KEYS
84             export TMP_AUTHORIZED_USER_IDS
85
86             # process authorized_user_ids file, as monkeysphere user
87             su_monkeysphere_user \
88                 ". ${SYSSHAREDIR}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
89             RETURN="$?"
90         else
91             log debug "not processing authorized_user_ids."
92         fi
93     else
94         log debug "empty or absent authorized_user_ids file."
95     fi
96
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"
107             else
108                 log debug "not adding raw authorized_keys file."                
109             fi
110         else
111             log debug "empty or absent authorized_keys file."
112         fi
113     fi
114
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.
120
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.
126
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}" || \
132             { 
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:
136             RETURN=1
137         }
138     else
139         rm -f "${SYSDATADIR}/authorized_keys/${uname}"
140     fi
141
142     # unset the trap
143     trap - EXIT
144
145     # destroy temporary directory
146     rm -rf "$TMPLOC"
147 done
148
149 }