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 # set default key parameter values
64 TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
70 # Note the quotes around `$TEMP': they are essential!
93 if [ -z "$keyID" ] ; then
94 failure "You must specify the key ID of your primary key."
97 # get key output, and fail if not found
98 gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
101 # fail if multiple sec lines are returned, which means the id
102 # given is not unique
103 if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
104 failure "Key ID '$keyID' is not unique."
107 # prompt if an authentication subkey already exists
108 if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
109 echo "An authentication subkey already exists for key '$keyID'."
110 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
111 if [ "${OK/y/Y}" != 'Y' ] ; then
116 # set subkey defaults
117 # prompt about key expiration if not specified
118 if [ -z "$keyExpire" ] ; then
120 Please specify how long the key should be valid.
121 0 = key does not expire
122 <n> = key expires in n days
123 <n>w = key expires in n weeks
124 <n>m = key expires in n months
125 <n>y = key expires in n years
127 while [ -z "$keyExpire" ] ; do
128 read -p "Key is valid for? (0) " keyExpire
129 if ! test_gpg_expire ${keyExpire:=0} ; then
134 elif ! test_gpg_expire "$keyExpire" ; then
135 failure "invalid key expiration value '$keyExpire'."
138 # generate the list of commands that will be passed to edit-key
139 editCommands=$(cat <<EOF
152 log "generating subkey..."
153 echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
157 ########################################################################
159 ########################################################################
161 # unset variables that should be defined only in config file
163 unset CHECK_KEYSERVER
165 unset HASH_KNOWN_HOSTS
166 unset AUTHORIZED_KEYS
169 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
171 # set monkeysphere home directory
172 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
173 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
176 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
178 # set empty config variables with ones from the environment, or from
179 # config file, or with defaults
180 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
181 KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
182 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
183 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
184 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
185 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
187 # other variables not in config file
188 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
189 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
190 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
192 # export GNUPGHOME and make sure gpg home exists with proper
195 mkdir -p -m 0700 "$GNUPGHOME"
199 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
203 'update-known_hosts'|'update-known-hosts'|'k')
206 if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
207 failure "Improper permissions on known_hosts file."
210 # if hosts are specified on the command line, process just
213 update_known_hosts "$@"
216 # otherwise, if no hosts are specified, process every host
217 # in the user's known_hosts file
219 if [ ! -s "$KNOWN_HOSTS" ] ; then
220 failure "known_hosts file '$KNOWN_HOSTS' is empty or does not exist."
228 'update-authorized_keys'|'update-authorized-keys'|'a')
229 MODE='authorized_keys'
231 # fail if the authorized_user_ids file is empty
232 if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
233 failure "authorized_user_ids file '$AUTHORIZED_USER_IDS' is empty or does not exist."
236 if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
237 failure "Improper permissions on authorized_user_ids file."
240 # process authorized_user_ids file
241 process_authorized_user_ids "$AUTHORIZED_USER_IDS"
254 failure "Unknown command: '$COMMAND'
255 Type '$PGRM help' for usage."