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 authentication subkey
43 --length (-l) BITS key length in bits (2048)
44 --expire (-e) EXPIRE date to expire
45 subkey-to-ssh-agent (s) store authentication subkey in ssh-agent
51 # generate a subkey with the 'a' usage flags set
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 # find all secret keys
94 keyID=$(gpg --with-colons --list-secret-keys | grep ^sec | cut -f5 -d:)
95 # if multiple sec keys exist, fail
96 if (( $(echo "$keyID" | wc -l) > 1 )) ; then
97 echo "Multiple secret keys found:"
99 failure "Please specify which primary key to use."
104 if [ -z "$keyID" ] ; then
105 failure "You have no secret key available. You should create an OpenPGP
106 key before joining the monkeysphere. You can do this with:
110 # get key output, and fail if not found
111 gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
114 # fail if multiple sec lines are returned, which means the id
115 # given is not unique
116 if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
117 failure "Key ID '$keyID' is not unique."
120 # prompt if an authentication subkey already exists
121 if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
122 echo "An authentication subkey already exists for key '$keyID'."
123 read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
124 if [ "${OK/y/Y}" != 'Y' ] ; then
129 # set subkey defaults
130 # prompt about key expiration if not specified
131 if [ -z "$keyExpire" ] ; then
133 Please specify how long the key should be valid.
134 0 = key does not expire
135 <n> = key expires in n days
136 <n>w = key expires in n weeks
137 <n>m = key expires in n months
138 <n>y = key expires in n years
140 while [ -z "$keyExpire" ] ; do
141 read -p "Key is valid for? (0) " keyExpire
142 if ! test_gpg_expire ${keyExpire:=0} ; then
147 elif ! test_gpg_expire "$keyExpire" ; then
148 failure "invalid key expiration value '$keyExpire'."
151 # generate the list of commands that will be passed to edit-key
152 editCommands=$(cat <<EOF
165 log "generating subkey..."
167 (umask 077 && mkfifo "$fifoDir/pass")
168 echo "$editCommands" | gpg --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
170 passphrase_prompt "Please enter your passphrase for $keyID: " "$fifoDir/pass"
177 function subkey_to_ssh_agent() {
178 # try to add all authentication subkeys to the agent:
186 # get list of secret keys (to work around https://bugs.g10code.com/gnupg/issue945):
187 secretkeys=$(gpg --list-secret-keys --with-colons --fixed-list-mode --fingerprint | grep '^fpr:' | cut -f10 -d: | awk '{ print "0x" $1 "!" }')
189 authsubkeys=$(gpg --list-secret-keys --with-colons --fixed-list-mode --fingerprint --fingerprint $secretkeys | cut -f1,5,10,12 -d: | grep -A1 '^ssb:[^:]*::[^:]*a[^:]*$' | grep '^fpr::' | cut -f3 -d: | sort -u)
191 workingdir=$(mktemp -d)
193 mkfifo "$workingdir/passphrase"
195 # FIXME: we're currently allowing any other options to get passed
196 # through to ssh-add. should we limit it to known ones? For
197 # example: -d or -c and/or -t <lifetime>
199 # FIXME: how do we know if we succeeded or failed? ssh-add gives
200 # weird return values under setsid, and if there are more than one
202 for subkey in $authsubkeys; do
203 kname="MonkeySphere Key $subkey"
205 if [ "$1" = '-d' ]; then
206 # we're removing the subkey:
207 gpg --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
208 (cd "$workingdir" && ssh-add -d "$kname")
210 # we're adding the subkey:
211 mkfifo "$workingdir/$kname"
212 gpg --quiet --passphrase-fd 3 3<"$workingdir/passphrase" \
213 --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
214 --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
215 (cd "$workingdir" && unset -v DISPLAY && unset -v SSH_ASKPASS && /usr/bin/setsid ssh-add "$@" "$kname" </dev/null )&
217 passphrase_prompt "Enter passphrase for MonkeySphere Key $subkey: " "$workingdir/passphrase"
220 rm -f "$workingdir/$kname"
226 ########################################################################
228 ########################################################################
230 # unset variables that should be defined only in config file
232 unset CHECK_KEYSERVER
234 unset HASH_KNOWN_HOSTS
235 unset AUTHORIZED_KEYS
238 [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
240 # set monkeysphere home directory
241 MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
242 mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
245 [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
247 # set empty config variables with ones from the environment, or from
248 # config file, or with defaults
249 GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
250 KEYSERVER=${MONKEYSPHERE_KEYSERVER:="$KEYSERVER"}
251 # if keyserver not specified in env or monkeysphere.conf,
253 if [ -z "$KEYSERVER" ] ; then
254 if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
255 KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
258 # if it's still not specified, use the default
259 KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
260 CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
261 KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
262 HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
263 AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
265 # other variables not in config file
266 AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
267 REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
268 REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
270 # export GNUPGHOME and make sure gpg home exists with proper
273 mkdir -p -m 0700 "$GNUPGHOME"
277 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
281 'update-known_hosts'|'update-known-hosts'|'k')
284 # check permissions on the known_hosts file path
285 if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
286 failure "Improper permissions on known_hosts file path."
289 # if hosts are specified on the command line, process just
292 update_known_hosts "$@"
295 # otherwise, if no hosts are specified, process every host
296 # in the user's known_hosts file
298 # exit if the known_hosts file does not exist
299 if [ ! -e "$KNOWN_HOSTS" ] ; then
300 log "known_hosts file '$KNOWN_HOSTS' does not exist."
309 'update-authorized_keys'|'update-authorized-keys'|'a')
310 MODE='authorized_keys'
312 # check permissions on the authorized_user_ids file path
313 if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
314 failure "Improper permissions on authorized_user_ids file path."
317 # check permissions on the authorized_keys file path
318 if ! check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" ; then
319 failure "Improper permissions on authorized_keys file path."
322 # exit if the authorized_user_ids file is empty
323 if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
324 log "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
328 # process authorized_user_ids file
329 process_authorized_user_ids "$AUTHORIZED_USER_IDS"
337 'subkey-to-ssh-agent'|'s')
338 subkey_to_ssh_agent "$@"
341 '--help'|'help'|'-h'|'h'|'?')
346 failure "Unknown command: '$COMMAND'
347 Type '$PGRM help' for usage."