]> arthur.barton.de Git - ax-zsh.git/blobdiff - default_plugins/ssh/ssh.zshrc
ssh: Try to save and restore SSH environment
[ax-zsh.git] / default_plugins / ssh / ssh.zshrc
index c63985b514dc1dfc6f697be02ecd160e3f42d5fc..ce2a877d086258ae168b3b93e1e6994989357c51 100644 (file)
@@ -4,18 +4,44 @@
 # Make sure that "ssh(1)" is installed
 (( $+commands[ssh] )) || return
 
-ssh_logname_prompt() {
+_ax_ssh_prompt() {
        [[ -n "$SSH_CLIENT" ]] || return 1
-       echo "$LOGNAME"
        return 0
 }
 
-ax_logname_prompt_functions=($ax_logname_prompt_functions ssh_logname_prompt)
+ax_logname_prompt_functions=($ax_logname_prompt_functions _ax_ssh_prompt)
+ax_hostname_prompt_functions=($ax_hostname_prompt_functions _ax_ssh_prompt)
 
-ssh_hostname_prompt() {
-       [[ -n "$SSH_CLIENT" ]] || return 1
-       echo "$SHORT_HOST"
-       return 0
-}
+# Validate SSH_AUTH_SOCK: Inside of screen(1) sessions for example, the socket
+# file becomes invalid when the session has been disconnected.
+[[ ! -r "$SSH_AUTH_SOCK" ]] && unset SSH_AUTH_SOCK
+
+# Save SSH environment when available:
+if [[ -n "$SSH_AUTH_SOCK" ]]; then
+       # Save current environment when no state exists or is invalid.
+       if [[ -r "$XDG_RUNTIME_DIR/ssh-env.sh" ]]; then
+               (
+                       source "$XDG_RUNTIME_DIR/ssh-env.sh"
+                       if [[ -z "$SSH_AUTH_SOCK" || ! -r "$SSH_AUTH_SOCK" ]]; then
+                               # Content is invalid, remove state file!
+                               rm -f "$XDG_RUNTIME_DIR/ssh-env.sh"
+                       fi
+               )
+       fi
+       if [[ ! -r "$XDG_RUNTIME_DIR/ssh-env.sh" ]]; then
+               # No state file exists, create a new one:
+               echo "SSH_AUTH_SOCK=\"$SSH_AUTH_SOCK\"" >"$XDG_RUNTIME_DIR/ssh-env.sh"
+               echo "export SSH_AUTH_SOCK" >>"$XDG_RUNTIME_DIR/ssh-env.sh"
+       fi
+fi
 
-ax_hostname_prompt_functions=($ax_hostname_prompt_functions ssh_hostname_prompt)
+# Restore SSH environment when not set but available:
+if [[ -z "$SSH_AUTH_SOCK" && -r "$XDG_RUNTIME_DIR/ssh-env.sh" ]]; then
+       # Try to recover known good environment ...
+       source "$XDG_RUNTIME_DIR/ssh-env.sh"
+       if [[ ! -r "$SSH_AUTH_SOCK" ]]; then
+               # Clean up!
+               unset SSH_AUTH_SOCK
+               rm -f "$XDG_RUNTIME_DIR/ssh-env.sh"
+       fi
+fi