3 # monkeysphere: MonkeySphere client tool
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # They are Copyright 2008, and are all released under the GPL, version 3
11 ########################################################################
14 SHARE=${MONKEYSPHERE_SHARE:-"/usr/share/monkeysphere"}
16 . "${SHARE}/common" || exit 1
18 # date in UTF format if needed
19 DATE=$(date -u '+%FT%T')
21 # unset some environment variables that could screw things up
27 ########################################################################
29 ########################################################################
33 usage: $PGRM <subcommand> [args]
34 MonkeySphere client tool.
37 update-known_hosts (k) [HOST]... update known_hosts file
38 update-authorized_keys (a) update authorized_keys file
39 gen-subkey (g) KEYID generate an 'a' capable subkey
40 -l|--length BITS key length in bits (2048)
41 -e|--expire EXPIRE date to expire
47 # generate a subkey with the 'a' usage flags set
48 # FIXME: this needs some tweaking to clean it up
56 gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
57 "$keyID" 2> /dev/null)
59 # fail if there only "tru" lines are output from gpg, which
60 # indicates the key was not found.
61 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
62 failure "Key ID '$keyID' not found."
65 # fail if multiple pub lines are returned, which means the id given
67 if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
68 failure "Key ID '$keyID' is not unique."
71 # prompt if an authentication subkey already exists
72 if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
73 echo "An authentication subkey already exists for key '$keyID'."
74 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
75 if [ "${OK/y/Y}" != 'Y' ] ; then
82 KEY_LENGTH=${KEY_LENGTH:-}
84 # prompt about key expiration if not specified
85 if [ -z "$KEY_EXPIRE" ] ; then
87 Please specify how long the key should be valid.
88 0 = key does not expire
89 <n> = key expires in n days
90 <n>w = key expires in n weeks
91 <n>m = key expires in n months
92 <n>y = key expires in n years
94 while [ -z "$KEY_EXPIRE" ] ; do
95 read -p "Key is valid for? (0) " KEY_EXPIRE
96 if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
101 elif ! test_gpg_expire "$KEY_EXPIRE" ; then
102 failure "invalid key expiration value '$KEY_EXPIRE'."
105 # generate the list of commands that will be passed to edit-key
106 editCommands=$(cat <<EOF
119 log "generating subkey..."
120 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
124 ########################################################################
126 ########################################################################
128 # unset variables that should be defined only in config file
130 unset CHECK_KEYSERVER
132 unset HASH_KNOWN_HOSTS
133 unset AUTHORIZED_KEYS
136 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
138 # set monkeysphere home directory
139 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
140 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
143 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
145 # set empty config variables with ones from the environment, or from
146 # config file, or with defaults
147 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
148 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
149 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
150 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
151 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
152 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
154 # other variables not in config file
155 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
156 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
157 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
159 # export GNUPGHOME and make sure gpg home exists with proper
162 mkdir -p -m 0700 "$GNUPGHOME"
166 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
169 # unset option variables
173 # get options for key generation and add-certifier functions
174 TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
176 if [ $? != 0 ] ; then
181 # Note the quotes around `$TEMP': they are essential!
204 'update-known_hosts'|'update-known-hosts'|'k')
207 # if hosts are specified on the command line, process just
210 update_known_hosts "$@"
213 # otherwise, if no hosts are specified, process every host
214 # in the user's known_hosts file
216 if [ ! -s "$KNOWN_HOSTS" ] ; then
217 failure "known_hosts file '$KNOWN_HOSTS' is empty or does not exist."
225 'update-authorized_keys'|'update-authorized-keys'|'a')
226 MODE='authorized_keys'
228 # fail if the authorized_user_ids file is empty
229 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
230 failure "$AUTHORIZED_USER_IDS is empty or does not exist."
233 # process authorized_user_ids file
234 process_authorized_user_ids "$AUTHORIZED_USER_IDS"
240 if [ -z "$keyID" ] ; then
241 failure "You must specify the key ID of your primary key."
251 failure "Unknown command: '$COMMAND'
252 Type '$PGRM help' for usage."