+ if [ "$1" = '-d' ]; then
+ # we're removing the subkey:
+ gpg --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
+ (cd "$workingdir" && ssh-add -d "$kname")
+ else
+ # we're adding the subkey:
+ mkfifo "$workingdir/$kname"
+ gpg --quiet --passphrase-fd 3 3<"$workingdir/passphrase" \
+ --export-options export-reset-subkey-passwd,export-minimal,no-export-attributes \
+ --export-secret-subkeys "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname" &
+ (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
+
+ passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
+ wait %2
+ fi
+ keysuccess="$?"
+
+ rm -f "$workingdir/$kname"
+ done
+
+ rm -rf "$workingdir"
+
+ # FIXME: sort out the return values: we're just returning the
+ # success or failure of the final authentication subkey in this
+ # case. What if earlier ones failed?
+ exit "$keysuccess"
+}
+
+########################################################################
+# MAIN
+########################################################################
+
+# unset variables that should be defined only in config file
+unset KEYSERVER
+unset CHECK_KEYSERVER
+unset KNOWN_HOSTS
+unset HASH_KNOWN_HOSTS
+unset AUTHORIZED_KEYS
+
+# load global config
+[ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] && . "${SYSCONFIGDIR}/monkeysphere.conf"
+
+# set monkeysphere home directory
+MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
+mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
+
+# load local config
+[ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
+
+# set empty config variables with ones from the environment, or from
+# config file, or with defaults
+LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
+GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
+KEYSERVER=${MONKEYSPHERE_KEYSERVER:="$KEYSERVER"}
+# if keyserver not specified in env or monkeysphere.conf,
+# look in gpg.conf
+if [ -z "$KEYSERVER" ] ; then
+ if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
+ KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
+ fi
+fi
+# if it's still not specified, use the default
+KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
+CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
+KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
+HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
+AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
+
+# other variables not in config file
+AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
+REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
+REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
+
+# export GNUPGHOME and make sure gpg home exists with proper
+# permissions
+export GNUPGHOME