X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=src%2Fmonkeysphere;h=a25fd6a64257a780fc6628842f119218a1f198ee;hb=5fadec09dcd44c4dcad657a0f3d96878b592b77b;hp=f279d86395a249b5169df2fd9d26fa30b1aceb90;hpb=4793624c65673268128fb0146cd9bd1b3cfeb6c4;p=monkeysphere.git diff --git a/src/monkeysphere b/src/monkeysphere index f279d86..a25fd6a 100755 --- a/src/monkeysphere +++ b/src/monkeysphere @@ -1,20 +1,28 @@ -#!/bin/sh +#!/bin/bash + +# monkeysphere: MonkeySphere client tool +# +# The monkeysphere scripts are written by: +# Jameson Rollins +# +# They are Copyright 2008, and are all released under the GPL, version 3 +# or later. ######################################################################## PGRM=$(basename $0) -SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"} -export SHAREDIR -. "${SHAREDIR}/common" - -GLOBAL_CONFIG=${GLOBAL_CONFIG:-"${ETC}"/monkeysphere.conf} -[ -r "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG" +SHARE=${MONKEYSPHERE_SHARE:-"/usr/share/monkeysphere"} +export SHARE +. "${SHARE}/common" || exit 1 # date in UTF format if needed DATE=$(date -u '+%FT%T') # unset some environment variables that could screw things up -GREP_OPTIONS= +unset GREP_OPTIONS + +# default return code +RETURN=0 ######################################################################## # FUNCTIONS @@ -22,125 +30,217 @@ GREP_OPTIONS= usage() { cat < [args] -Monkeysphere client tool. +usage: $PGRM [options] [args] +MonkeySphere client tool. subcommands: - update-known-hosts (k) [HOST]... update known_hosts file - update-authorized-keys (a) update authorized_keys file - update-userid (u) [USERID]... add/update userid to - authorized_user_ids - help (h,?) this help + update-known_hosts (k) [HOST]... update known_hosts file + update-authorized_keys (a) update authorized_keys file + gen-subkey (g) KEYID generate an 'a' capable subkey + -l|--length BITS key length in bits (2048) + -e|--expire EXPIRE date to expire + help (h,?) this help EOF } +# generate a subkey with the 'a' usage flags set +# FIXME: this needs some tweaking to clean it up +gen_subkey(){ + local keyID + local gpgOut + local userID + + keyID="$1" + + gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \ + "$keyID" 2> /dev/null) + + # fail if there only "tru" lines are output from gpg, which + # indicates the key was not found. + if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then + failure "Key ID '$keyID' not found." + fi + + # fail if multiple pub lines are returned, which means the id given + # is not unique + if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then + failure "Key ID '$keyID' is not unique." + fi + + # prompt if an authentication subkey already exists + if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then + echo "An authentication subkey already exists for key '$keyID'." + read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N} + if [ "${OK/y/Y}" != 'Y' ] ; then + failure "aborting." + fi + fi + + # set subkey defaults + KEY_TYPE="RSA" + KEY_LENGTH=${KEY_LENGTH:-} + KEY_USAGE="auth" + # prompt about key expiration if not specified + if [ -z "$KEY_EXPIRE" ] ; then + cat < = key expires in n days + w = key expires in n weeks + m = key expires in n months + y = key expires in n years +EOF + while [ -z "$KEY_EXPIRE" ] ; do + read -p "Key is valid for? (0) " KEY_EXPIRE + if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then + echo "invalid value" + unset KEY_EXPIRE + fi + done + elif ! test_gpg_expire "$KEY_EXPIRE" ; then + failure "invalid key expiration value '$KEY_EXPIRE'." + fi + + # generate the list of commands that will be passed to edit-key + editCommands=$(cat < "$msAuthorizedKeys" - echo "done." - else - log "no gpg keys to add." - fi - if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" ] ; then - userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"} - if [ -s "$userAuthorizedKeys" ] ; then - log -n "adding user authorized_keys file... " - cat "$userAuthorizedKeys" >> "$msAuthorizedKeys" - echo "done." - fi - fi - log "monkeysphere authorized_keys file generated:" - log "$msAuthorizedKeys" + # process authorized_user_ids file + process_authorized_user_ids "$AUTHORIZED_USER_IDS" + RETURN="$?" ;; - 'update-userid'|'u') - if [ -z "$1" ] ; then - failure "you must specify at least one userid." + 'gen-subkey'|'g') + keyID="$1" + if [ -z "$keyID" ] ; then + failure "You must specify the key ID of your primary key." fi - for userID ; do - if ! grep -q "^${userID}\$" "$AUTHORIZED_USER_IDS" ; then - log "userid '$userID' not in authorized_user_ids file." - continue - fi - log "processing user id: '$userID'" - process_user_id "$userID" "$userKeysCacheDir" > /dev/null - done + gen_subkey "$keyID" ;; 'help'|'h'|'?') @@ -149,6 +249,8 @@ case $COMMAND in *) failure "Unknown command: '$COMMAND' -Type 'cereal-admin help' for usage." +Type '$PGRM help' for usage." ;; esac + +exit "$RETURN"