correct return codes for monkeysphere subkey-to-ssh-agent
[monkeysphere.git] / src / share / m / subkey_to_ssh_agent
index 818f4f70c3ae65a86ed58c169a02c31b89884d5e..4ce14f898709a4002de77b075bcde9b27f746457 100644 (file)
 
 # try to add all authentication subkeys to the agent
 
+# FIXME: what if you only want to add one authentication subkey to the
+# agent?
+
 subkey_to_ssh_agent() {
-    local sshaddresponse
+    local sshaddresponse=0
     local secretkeys
     local authsubkeys
     local workingdir
-    local keysuccess
+    local keysuccess=0
     local subkey
     local publine
     local kname
@@ -38,8 +41,7 @@ For more details, see:
 
     # and if it looks like it's running, but we can't actually talk to
     # it, bail out:
-    ssh-add -l >/dev/null
-    sshaddresponse="$?"
+    ssh-add -l >/dev/null || sshaddresponse="$?"
     if [ "$sshaddresponse" = "2" ]; then
        failure "Could not connect to ssh-agent"
     fi
@@ -69,7 +71,6 @@ You might want to 'monkeysphere gen-subkey'"
     trap "rm -rf $workingdir" EXIT
     umask 077
     mkfifo "$workingdir/passphrase"
-    keysuccess=1
 
     # FIXME: we're currently allowing any other options to get passed
     # through to ssh-add.  should we limit it to known ones?  For
@@ -89,7 +90,7 @@ You might want to 'monkeysphere gen-subkey'"
        if [ "$1" = '-d' ]; then
            # we're removing the subkey:
            gpg_user --export "0x${subkey}!" | openpgp2ssh "$subkey" > "$workingdir/$kname"
-           (cd "$workingdir" && ssh-add -d "$kname")
+           (cd "$workingdir" && ssh-add -d "$kname") || keysuccess="$?"
        else
            # we're adding the subkey:
            mkfifo "$workingdir/$kname"
@@ -99,9 +100,8 @@ You might want to 'monkeysphere gen-subkey'"
            (cd "$workingdir" && DISPLAY=nosuchdisplay SSH_ASKPASS=/bin/false ssh-add "$@" "$kname" </dev/null )&
 
            passphrase_prompt "Enter passphrase for key $kname: " "$workingdir/passphrase"
-           wait %2
+           wait %2 || keysuccess="$?"
        fi
-       keysuccess="$?"
 
        rm -f "$workingdir/$kname"
     done
@@ -110,7 +110,7 @@ You might want to 'monkeysphere gen-subkey'"
     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"
+    # failure code of the last authentication subkey which fails.
+    # what if more than one authentication subkey fails?
+    return "$keysuccess"
 }