3 # rhesus: monkeysphere authorized_keys/known_hosts generating script
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # Copyright 2008, released under the GPL, version 3 or later
12 ########################################################################
14 ########################################################################
18 usage: $CMD -k|--known_hosts
19 $CMD -a|--authorized_keys
33 # cut out all comments(#) and blank lines from standard input
35 grep -v -e "^[[:space:]]*#" -e '^$'
38 # cut a specified line from standard input
40 head --line="$1" | tail -1
43 # retrieve all keys with given user id from keyserver
44 # FIXME: need to figure out how to retrieve all matching keys
49 gpg --quiet --batch --command-fd 0 --with-colons \
50 --keyserver "$KEYSERVER" \
51 --search ="$id" >/dev/null 2>&1
54 # convert escaped characters from gpg output back into original
56 # FIXME: undo all escape character translation in with-colons gpg output
58 echo "$1" | sed 's/\\x3a/:/'
61 # stand in until we get dkg's gpg2ssh program
70 if [ "$mode" = '--authorized_keys' -o "$mode" = '-a' ] ; then
71 gpgkey2ssh "$keyID" | sed -e "s/COMMENT/$userID/"
72 elif [ "$mode" = '--known_hosts' -o "$mode" = '-k' ] ; then
73 echo -n "$userID "; gpgkey2ssh "$keyID" | sed -e 's/ COMMENT//'
77 # userid and key policy checking
78 # the following checks policy on the returned keys
79 # - checks that full key has appropriate valididy (u|f)
80 # - checks key has appropriate capability (E|A)
81 # - checks that particular desired user id has appropriate validity
82 # see /usr/share/doc/gnupg/DETAILS.gz
83 # FIXME: add some more status output
84 # expects global variable: "mode"
96 # fetch all keys from keyserver
97 # if none found, break
98 if ! gpg_fetch_keys "$userID" ; then
99 echo " no keys found."
103 # some crazy piping here that takes the output of gpg and
104 # pipes it into a "while read" loop that reads each line
105 # of standard input one-by-one.
106 gpg --fixed-list-mode --list-key --with-colons \
107 --with-fingerprint ="$userID" 2> /dev/null | \
108 cut -d : -f 1,2,5,10,12 | \
109 while IFS=: read -r type validity keyid uidfpr capability ; do
110 # process based on record type
113 # new key, wipe the slate
117 # check primary key validity
118 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
121 # check capability is not Disabled...
122 if echo "$capability" | grep -q 'D' ; then
125 # check capability is Encryption and Authentication
126 # FIXME: make more flexible capability specification
128 if echo "$capability" | grep -q -v 'E' ; then
129 if echo "$capability" | grep -q -v 'A' ; then
133 keyCapability="$capability"
138 # if key ok, get fingerprint
139 if [ "$keyOK" ] ; then
140 keyFingerprint="$uidfpr"
144 # check key ok and we have key fingerprint
145 if [ -z "$keyOK" -o -z "$keyFingerprint" ] ; then
149 if [ "$validity" != 'u' -a "$validity" != 'f' ] ; then
152 # check the uid matches
153 if [ "$(unescape "$uidfpr")" != "$userID" ] ; then
157 # FIXME: needs to apply extra options if specified
158 echo -n " valid key found; generating ssh key(s)... "
159 userIDHash=$(echo "$userID" | sha1sum | awk '{ print $1 }')
160 # export the key with gpg2ssh
161 #gpg --export "$keyFingerprint" | gpg2ssh "$mode" > "$cacheDir"/"$userIDHash"."$keyFingerprint"
162 # stand in until we get dkg's gpg2ssh program
163 gpg2ssh_tmp "$mode" "$keyID" "$userID" > "$cacheDir"/"$userIDHash"."$keyFingerprint"
164 if [ "$?" = 0 ] ; then
174 # process the auth_*_ids file
175 # go through line-by-line, extracting and processing each user id
176 # expects global variable: "mode"
177 process_auth_file() {
187 # find number of user ids in auth_user_ids file
188 nLines=$(meat <"$authIDsFile" | wc -l)
190 # make sure gpg home exists with proper permissions
191 mkdir -p -m 0700 "$GNUPGHOME"
193 # clean out keys file and remake keys directory
197 # loop through all user ids
198 for line in $(seq 1 $nLines) ; do
200 # FIXME: needs to handle extra options if necessary
201 userID=$(meat <"$authIDsFile" | cutline "$line" )
203 # process the user id and extract keys
204 log "processing user id: '$userID'"
205 process_user_id "$userID" "$cacheDir"
210 ########################################################################
212 ########################################################################
214 if [ -z "$1" ] ; then
224 if ! id -u "$USER" > /dev/null 2>&1 ; then
225 failure "invalid user '$USER'."
228 # set user home directory
229 HOME=$(getent passwd "$USER" | cut -d: -f6)
231 # get ms home directory
232 MS_HOME=${MS_HOME:-"$HOME"/.config/monkeysphere}
234 # load configuration file
235 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere.conf}
236 [ -e "$MS_CONF" ] && . "$MS_CONF"
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}
245 USER_KNOW_HOSTS="$HOME"/.ssh/known_hosts
246 USER_AUTHORIZED_KEYS="$HOME"/.ssh/authorized_keys
248 # export USER and GNUPGHOME variables, since they are used by gpg
253 hostKeysCacheDir="$STAGING_AREA"/host_keys
254 userKeysCacheDir="$STAGING_AREA"/user_keys
255 msKnownHosts="$STAGING_AREA"/known_hosts
256 msAuthorizedKeys="$STAGING_AREA"/authorized_keys
259 if [ "$mode" = '--known_hosts' -o "$mode" = '-k' ] ; then
261 authIDsFile="$AUTH_HOST_FILE"
262 outFile="$msKnownHosts"
263 cacheDir="$hostKeysCacheDir"
264 userFile="$USER_KNOWN_HOSTS"
265 elif [ "$mode" = '--authorized_keys' -o "$mode" = '-a' ] ; then
266 fileType=authorized_keys
267 authIDsFile="$AUTH_USER_FILE"
268 outFile="$msAuthorizedKeys"
269 cacheDir="$userKeysCacheDir"
270 userFile="$USER_AUTHORIZED_KEYS"
272 failure "unknown command '$mode'."
275 # check auth ids file
276 if [ ! -s "$authIDsFile" ] ; then
277 echo $(basename "$authIDsFile") "file is empty or does not exist."
281 log "user '$USER': monkeysphere $fileType generation..."
283 # process the auth file
284 process_auth_file "$authIDsFile" "$cacheDir"
286 # write output key file
287 log "writing ms $fileType file... "
289 if [ "$(ls "$cacheDir")" ] ; then
290 log -n "adding gpg keys... "
291 cat "$cacheDir"/* > "$outFile"
294 log "no gpg keys to add."
296 if [ -s "$userFile" ] ; then
297 log -n "adding user $fileType file... "
298 cat "$userFile" >> "$outFile"
301 log "ms $fileType file generated:"