7a43fca0ac19f456f95c6daa4c746feb05878c7b
[monkeysphere.git] / rhesus / rhesus
1 #!/bin/sh -e
2
3 # rhesus: monkeysphere authorized_keys/known_hosts generating script
4 #
5 # Written by
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 #
8 # Copyright 2008, released under the GPL, version 3 or later
9
10 PGRM=$(basename $0)
11
12 ########################################################################
13 # FUNCTIONS
14 ########################################################################
15
16 usage() {
17 cat <<EOF
18 usage: $PGRM k|known_hosts [userid...]
19        $PGRM a|authorized_keys [userid...]
20 Monkeysphere update of known_hosts or authorized_keys file.
21 If userids are specified, only specified userids will be processed
22 (userids must be included in the appropriate auth_*_ids file).
23 EOF
24 }
25
26 failure() {
27     echo "$1" >&2
28     exit ${2:-'1'}
29 }
30
31 log() {
32     echo -n "ms: "
33     echo "$@"
34 }
35
36 # cut out all comments(#) and blank lines from standard input
37 meat() {
38     grep -v -e "^[[:space:]]*#" -e '^$'
39 }
40
41 # cut a specified line from standard input
42 cutline() {
43     head --line="$1" | tail -1
44 }
45
46 # retrieve all keys with given user id from keyserver
47 # FIXME: need to figure out how to retrieve all matching keys
48 # (not just first 5)
49 gpg_fetch_keys() {
50     local id
51     id="$1"
52     echo 1,2,3,4,5 | \
53         gpg --quiet --batch --command-fd 0 --with-colons \
54         --keyserver "$KEYSERVER" \
55         --search ="$id" >/dev/null 2>&1
56 }
57
58 # convert escaped characters from gpg output back into original
59 # character
60 # FIXME: undo all escape character translation in with-colons gpg output
61 unescape() {
62     echo "$1" | sed 's/\\x3a/:/'
63 }
64
65 # stand in until we get dkg's gpg2ssh program
66 gpg2ssh_tmp() {
67     local mode
68     local keyID
69
70     mode="$1"
71     keyID="$2"
72     userID="$3"
73
74     if [ "$mode" = 'authorized_keys' -o "$mode" = 'a' ] ; then
75         gpgkey2ssh "$keyID" | sed -e "s/COMMENT/$userID/"
76     elif [ "$mode" = 'known_hosts' -o "$mode" = 'k' ] ; then
77         echo -n "$userID "; gpgkey2ssh "$keyID" | sed -e 's/ COMMENT//'
78     fi
79 }
80
81 # userid and key policy checking
82 # the following checks policy on the returned keys
83 # - checks that full key has appropriate valididy (u|f)
84 # - checks key has appropriate capability (E|A)
85 # - checks that particular desired user id has appropriate validity
86 # see /usr/share/doc/gnupg/DETAILS.gz
87 # FIXME: add some more status output
88 # expects global variable: "mode"
89 process_user_id() {
90     local userID
91     local cacheDir
92     local keyOK
93     local keyCapability
94     local keyFingerprint
95     local userIDHash
96
97     userID="$1"
98     cacheDir="$2"
99
100     # fetch all keys from keyserver
101     # if none found, break
102     if ! gpg_fetch_keys "$userID" ; then
103         echo "    no keys found."
104         return
105     fi
106
107     # some crazy piping here that takes the output of gpg and
108     # pipes it into a "while read" loop that reads each line
109     # of standard input one-by-one.
110     gpg --fixed-list-mode --list-key --with-colons \
111         --with-fingerprint ="$userID" 2> /dev/null | \
112     cut -d : -f 1,2,5,10,12 | \
113     while IFS=: read -r type validity keyid uidfpr capability ; do
114         # process based on record type
115         case $type in
116             'pub')
117                 # new key, wipe the slate
118                 keyOK=
119                 keyCapability=
120                 keyFingerprint=
121                 # check primary key validity
122                 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
123                     continue
124                 fi
125                 # check capability is not Disabled...
126                 if echo "$capability" | grep -q 'D' ; then
127                     continue
128                 fi
129                 # check capability is Encryption and Authentication
130                 # FIXME: make more flexible capability specification
131                 # (ie. in conf file)
132                 if echo "$capability" | grep -q -v 'E' ; then
133                     if echo "$capability" | grep -q -v 'A' ; then
134                         continue
135                     fi
136                 fi
137                 keyCapability="$capability"
138                 keyOK=true
139                 keyID="$keyid"
140                 ;;
141             'fpr')
142                 # if key ok, get fingerprint
143                 if [ "$keyOK" ] ; then
144                     keyFingerprint="$uidfpr"
145                 fi
146                 ;;
147             'uid')
148                 # check key ok and we have key fingerprint
149                 if [ -z "$keyOK" -o  -z "$keyFingerprint" ] ; then
150                     continue
151                 fi
152                 # check key validity
153                 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
154                     continue
155                 fi
156                 # check the uid matches
157                 if [ "$(unescape "$uidfpr")" != "$userID" ] ; then
158                     continue
159                 fi
160                 # convert the key
161                 # FIXME: needs to apply extra options if specified
162                 echo -n "    valid key found; generating ssh key(s)... "
163                 userIDHash=$(echo "$userID" | sha1sum | awk '{ print $1 }')
164                 # export the key with gpg2ssh
165                 #gpg --export "$keyFingerprint" | gpg2ssh "$mode" > "$cacheDir"/"$userIDHash"."$keyFingerprint"
166                 # stand in until we get dkg's gpg2ssh program
167                 gpg2ssh_tmp "$mode" "$keyID" "$userID" > "$cacheDir"/"$userIDHash"."$keyFingerprint"
168                 if [ "$?" = 0 ] ; then
169                     echo "done."
170                 else
171                     echo "error."
172                 fi
173                 ;;
174         esac
175     done
176 }
177
178 # process the auth_*_ids file
179 # go through line-by-line, extracting and processing each user id
180 # expects global variable: "mode"
181 process_auth_file() {
182     local authIDsFile
183     local cacheDir
184     local nLines
185     local line
186     local userID
187
188     authIDsFile="$1"
189     cacheDir="$2"
190
191     # find number of user ids in auth_user_ids file
192     nLines=$(meat <"$authIDsFile" | wc -l)
193
194     # clean out keys file and remake keys directory
195     rm -rf "$cacheDir"
196     mkdir -p "$cacheDir"
197
198     # loop through all user ids
199     for line in $(seq 1 $nLines) ; do
200         # get user id
201         # FIXME: needs to handle extra options if necessary
202         userID=$(meat <"$authIDsFile" | cutline "$line" )
203
204         # process the user id and extract keys
205         log "processing user id: '$userID'"
206         process_user_id "$userID" "$cacheDir"
207     done
208 }
209
210 ########################################################################
211 # MAIN
212 ########################################################################
213
214 if [ -z "$1" ] ; then
215     usage
216     exit 1
217 fi
218
219 # check mode
220 mode="$1"
221 shift 1
222
223 # check user
224 if ! id -u "$USER" > /dev/null 2>&1 ; then
225     failure "invalid user '$USER'."
226 fi
227
228 # set user home directory
229 HOME=$(getent passwd "$USER" | cut -d: -f6)
230
231 # set ms home directory
232 MS_HOME=${MS_HOME:-"$HOME"/.config/monkeysphere}
233
234 # load configuration file
235 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere.conf}
236 [ -e "$MS_CONF" ] && . "$MS_CONF"
237
238 # set config variable defaults
239 STAGING_AREA=${STAGING_AREA:-"$MS_HOME"}
240 AUTH_HOST_FILE=${AUTH_HOST_FILE:-"$MS_HOME"/auth_host_ids}
241 AUTH_USER_FILE=${AUTH_USER_FILE:-"$MS_HOME"/auth_user_ids}
242 GNUPGHOME=${GNUPGHOME:-"$HOME"/.gnupg}
243 KEYSERVER=${KEYSERVER:-subkeys.pgp.net}
244
245 USER_KNOW_HOSTS="$HOME"/.ssh/known_hosts
246 USER_AUTHORIZED_KEYS="$HOME"/.ssh/authorized_keys
247
248 # export USER and GNUPGHOME variables, since they are used by gpg
249 export USER
250 export GNUPGHOME
251
252 # stagging locations
253 hostKeysCacheDir="$STAGING_AREA"/host_keys
254 userKeysCacheDir="$STAGING_AREA"/user_keys
255 msKnownHosts="$STAGING_AREA"/known_hosts
256 msAuthorizedKeys="$STAGING_AREA"/authorized_keys
257
258 # set mode variables
259 if [ "$mode" = 'known_hosts' -o "$mode" = 'k' ] ; then
260     fileType=known_hosts
261     authFileType=auth_host_ids
262     authIDsFile="$AUTH_HOST_FILE"
263     outFile="$msKnownHosts"
264     cacheDir="$hostKeysCacheDir"
265     userFile="$USER_KNOWN_HOSTS"
266 elif [ "$mode" = 'authorized_keys' -o "$mode" = 'a' ] ; then
267     fileType=authorized_keys
268     authFileType=auth_user_ids
269     authIDsFile="$AUTH_USER_FILE"
270     outFile="$msAuthorizedKeys"
271     cacheDir="$userKeysCacheDir"
272     userFile="$USER_AUTHORIZED_KEYS"
273 else
274     failure "unknown command '$mode'."
275 fi
276
277 # check auth ids file
278 if [ ! -s "$authIDsFile" ] ; then
279     echo "'$authFileType' file is empty or does not exist."
280     exit
281 fi
282
283 log "user '$USER': monkeysphere $fileType generation"
284
285 # make sure gpg home exists with proper permissions
286 mkdir -p -m 0700 "$GNUPGHOME"
287
288 # if users are specified on the command line, process just
289 # those users
290 if [ "$1" ] ; then
291     # process userids given on the command line
292     for userID ; do
293         if ! grep -q "$userID" "$authIDsFile" ; then
294             log "userid '$userID' not in $authFileType file."
295             continue
296         fi
297         log "processing user id: '$userID'"
298         process_user_id "$userID" "$cacheDir"
299     done
300 # otherwise if no users are specified, process the entire
301 # auth_*_ids file
302 else
303     # process the auth file
304     process_auth_file "$authIDsFile" "$cacheDir"
305 fi
306
307 # write output key file
308 log "writing ms $fileType file... "
309 > "$outFile"
310 if [ "$(ls "$cacheDir")" ] ; then
311     log -n "adding gpg keys... "
312     cat "$cacheDir"/* > "$outFile"
313     echo "done."
314 else
315     log "no gpg keys to add."
316 fi
317 if [ -s "$userFile" ] ; then
318     log -n "adding user $fileType file... "
319     cat "$userFile" >> "$outFile"
320     echo "done."
321 fi
322 log "ms $fileType file generated:"
323 log "$outFile"