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 # set the file creation mask to be only owner rw
30 ########################################################################
32 ########################################################################
36 usage: $PGRM <subcommand> [options] [args]
37 MonkeySphere client tool.
40 update-known_hosts (k) [HOST]... update known_hosts file
41 update-authorized_keys (a) update authorized_keys file
42 gen-subkey (g) KEYID generate an 'a' capable subkey
43 -l|--length BITS key length in bits (2048)
44 -e|--expire EXPIRE date to expire
50 # generate a subkey with the 'a' usage flags set
51 # FIXME: this needs some tweaking to clean it up
59 gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
60 "$keyID" 2> /dev/null)
62 # fail if there only "tru" lines are output from gpg, which
63 # indicates the key was not found.
64 if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
65 failure "Key ID '$keyID' not found."
68 # fail if multiple pub lines are returned, which means the id given
70 if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
71 failure "Key ID '$keyID' is not unique."
74 # prompt if an authentication subkey already exists
75 if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
76 echo "An authentication subkey already exists for key '$keyID'."
77 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
78 if [ "${OK/y/Y}" != 'Y' ] ; then
85 KEY_LENGTH=${KEY_LENGTH:-}
87 # prompt about key expiration if not specified
88 if [ -z "$KEY_EXPIRE" ] ; then
90 Please specify how long the key should be valid.
91 0 = key does not expire
92 <n> = key expires in n days
93 <n>w = key expires in n weeks
94 <n>m = key expires in n months
95 <n>y = key expires in n years
97 while [ -z "$KEY_EXPIRE" ] ; do
98 read -p "Key is valid for? (0) " KEY_EXPIRE
99 if ! test_gpg_expire ${KEY_EXPIRE:=0} ; then
104 elif ! test_gpg_expire "$KEY_EXPIRE" ; then
105 failure "invalid key expiration value '$KEY_EXPIRE'."
108 # generate the list of commands that will be passed to edit-key
109 editCommands=$(cat <<EOF
122 log "generating subkey..."
123 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
127 ########################################################################
129 ########################################################################
131 # unset variables that should be defined only in config file
133 unset CHECK_KEYSERVER
135 unset HASH_KNOWN_HOSTS
136 unset AUTHORIZED_KEYS
139 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
141 # set monkeysphere home directory
142 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
143 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
146 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
148 # set empty config variables with ones from the environment, or from
149 # config file, or with defaults
150 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
151 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
152 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
153 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
154 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
155 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
157 # other variables not in config file
158 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
159 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
160 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
162 # export GNUPGHOME and make sure gpg home exists with proper
165 mkdir -p -m 0700 "$GNUPGHOME"
169 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
172 # unset option variables
176 # get options for key generation and add-certifier functions
177 TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
179 if [ $? != 0 ] ; then
184 # Note the quotes around `$TEMP': they are essential!
207 'update-known_hosts'|'update-known-hosts'|'k')
210 if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
211 failure "Improper permissions on known_hosts file."
214 # if hosts are specified on the command line, process just
217 update_known_hosts "$@"
220 # otherwise, if no hosts are specified, process every host
221 # in the user's known_hosts file
223 if [ ! -s "$KNOWN_HOSTS" ] ; then
224 failure "known_hosts file '$KNOWN_HOSTS' is empty or does not exist."
232 'update-authorized_keys'|'update-authorized-keys'|'a')
233 MODE='authorized_keys'
235 # fail if the authorized_user_ids file is empty
236 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
237 failure "authorized_user_ids file '$AUTHORIZED_USER_IDS' is empty or does not exist."
240 if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
241 failure "Improper permissions on authorized_user_ids file."
244 # process authorized_user_ids file
245 process_authorized_user_ids "$AUTHORIZED_USER_IDS"
251 if [ -z "$keyID" ] ; then
252 failure "You must specify the key ID of your primary key."
262 failure "Unknown command: '$COMMAND'
263 Type '$PGRM help' for usage."