From 0c60f421cbbdf673fada8fc6e795d069d1ae972e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20G=C3=BCntner?= Date: Wed, 27 Jul 2016 12:04:47 +0200 Subject: [PATCH 1/6] ssh-agent: Use /usr/bin/env to find ssh-add MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change is important when ssh-add is not inside /usr/bin e.g. on NixOS. Signed-off-by: Maximilian Güntner --- plugins/ssh-agent/ssh-agent.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh index b77b9ee78..24b6fd9d0 100644 --- a/plugins/ssh-agent/ssh-agent.plugin.zsh +++ b/plugins/ssh-agent/ssh-agent.plugin.zsh @@ -45,7 +45,7 @@ function _plugin__start_agent() zstyle -a :omz:plugins:ssh-agent identities identities echo starting ssh-agent... - /usr/bin/ssh-add $HOME/.ssh/${^identities} + /usr/bin/env ssh-add $HOME/.ssh/${^identities} } # Get the filename to store/lookup the environment from From 81e73e3d18ebf83d32e9c5e0edc2a26556066955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 21 Aug 2016 00:12:39 +0200 Subject: [PATCH 2/6] Delete useless `/usr/bin/env` in ssh-agent --- plugins/ssh-agent/ssh-agent.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh index 24b6fd9d0..6fdeb2725 100644 --- a/plugins/ssh-agent/ssh-agent.plugin.zsh +++ b/plugins/ssh-agent/ssh-agent.plugin.zsh @@ -37,7 +37,7 @@ function _plugin__start_agent() zstyle -s :omz:plugins:ssh-agent lifetime lifetime # start ssh-agent and setup environment - /usr/bin/env ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! ${_plugin__ssh_env} + ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! ${_plugin__ssh_env} chmod 600 ${_plugin__ssh_env} . ${_plugin__ssh_env} > /dev/null @@ -45,7 +45,7 @@ function _plugin__start_agent() zstyle -a :omz:plugins:ssh-agent identities identities echo starting ssh-agent... - /usr/bin/env ssh-add $HOME/.ssh/${^identities} + ssh-add $HOME/.ssh/${^identities} } # Get the filename to store/lookup the environment from From b60acddefe4cb18267e84b6cdd4314e82352be2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 21 Aug 2016 00:19:59 +0200 Subject: [PATCH 3/6] Extract comments into README for ssh-agent plugin --- plugins/ssh-agent/README.md | 38 ++++++++++++++++++++++++++ plugins/ssh-agent/ssh-agent.plugin.zsh | 30 -------------------- 2 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 plugins/ssh-agent/README.md diff --git a/plugins/ssh-agent/README.md b/plugins/ssh-agent/README.md new file mode 100644 index 000000000..00af42f01 --- /dev/null +++ b/plugins/ssh-agent/README.md @@ -0,0 +1,38 @@ +# ssh-agent plugin + +This plugin starts automatically `ssh-agent` to set up and load whichever +credentials you want for ssh connections. + +To enable it, add `ssh-agent` to your plugins: + +```zsh +plugins=(... ssh-agent) +``` + +## Instructions + +To enable **agent forwarding support** add the following to your zshrc file: + +```zsh +zstyle :omz:plugins:ssh-agent agent-forwarding on +``` + +To **load multiple identities** use the `identities` style, For example: + +```zsh +zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github +``` + +To **set the maximum lifetime of the identities**, use the `lifetime` style. +The lifetime may be specified in seconds or as described in sshd_config(5) +(see _TIME FORMATS_). If left unspecified, the default lifetime is forever. + +```zsh +zstyle :omz:plugins:ssh-agent lifetime 4h +``` + +## Credits + +Based on code from Joseph M. Reagle: http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html + +Agent-forwarding support based on ideas from Florent Thoumie and Jonas Pfenniger diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh index 6fdeb2725..81f27ca00 100644 --- a/plugins/ssh-agent/ssh-agent.plugin.zsh +++ b/plugins/ssh-agent/ssh-agent.plugin.zsh @@ -1,32 +1,3 @@ -# -# INSTRUCTIONS -# -# To enable agent forwarding support add the following to -# your .zshrc file: -# -# zstyle :omz:plugins:ssh-agent agent-forwarding on -# -# To load multiple identities use the identities style, For -# example: -# -# zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github -# -# To set the maximum lifetime of the identities, use the -# lifetime style. The lifetime may be specified in seconds -# or as described in sshd_config(5) (see TIME FORMATS) -# If left unspecified, the default lifetime is forever. -# -# zstyle :omz:plugins:ssh-agent lifetime 4h -# -# CREDITS -# -# Based on code from Joseph M. Reagle -# http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html -# -# Agent forwarding support based on ideas from -# Florent Thoumie and Jonas Pfenniger -# - local _plugin__ssh_env local _plugin__forwarding @@ -76,4 +47,3 @@ fi unfunction _plugin__start_agent unset _plugin__forwarding unset _plugin__ssh_env - From cb0833ac1360984c74f40ccabdec323bb6bbc31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 21 Aug 2016 01:08:34 +0200 Subject: [PATCH 4/6] Clean up formatting of ssh-agent plugin --- plugins/ssh-agent/ssh-agent.plugin.zsh | 63 ++++++++++++-------------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh index 81f27ca00..a427b80ae 100644 --- a/plugins/ssh-agent/ssh-agent.plugin.zsh +++ b/plugins/ssh-agent/ssh-agent.plugin.zsh @@ -1,49 +1,42 @@ -local _plugin__ssh_env -local _plugin__forwarding +typeset _agent_forwarding _ssh_env_cache -function _plugin__start_agent() -{ - local -a identities - local lifetime - zstyle -s :omz:plugins:ssh-agent lifetime lifetime +function _start_agent() { + local lifetime + local -a identities - # start ssh-agent and setup environment - ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! ${_plugin__ssh_env} - chmod 600 ${_plugin__ssh_env} - . ${_plugin__ssh_env} > /dev/null + # start ssh-agent and setup environment + zstyle -s :omz:plugins:ssh-agent lifetime lifetime - # load identies - zstyle -a :omz:plugins:ssh-agent identities identities - echo starting ssh-agent... + ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache + chmod 600 $_ssh_env_cache + . $_ssh_env_cache > /dev/null - ssh-add $HOME/.ssh/${^identities} + # load identies + zstyle -a :omz:plugins:ssh-agent identities identities + + echo starting ssh-agent... + ssh-add $HOME/.ssh/${^identities} } # Get the filename to store/lookup the environment from -if (( $+commands[scutil] )); then - # It's OS X! - _plugin__ssh_env="$HOME/.ssh/environment-$(scutil --get ComputerName)" -else - _plugin__ssh_env="$HOME/.ssh/environment-$HOST" -fi +_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST" # test if agent-forwarding is enabled -zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding -if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then - # Add a nifty symlink for screen/tmux if agent forwarding - [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen +zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding -elif [ -f "${_plugin__ssh_env}" ]; then - # Source SSH settings, if applicable - . ${_plugin__ssh_env} > /dev/null - ps x | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || { - _plugin__start_agent; - } +if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then + # Add a nifty symlink for screen/tmux if agent forwarding + [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen +elif [[ -f "$_ssh_env_cache" ]]; then + # Source SSH settings, if applicable + . $_ssh_env_cache > /dev/null + ps x | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null || { + _start_agent + } else - _plugin__start_agent; + _start_agent fi # tidy up after ourselves -unfunction _plugin__start_agent -unset _plugin__forwarding -unset _plugin__ssh_env +unset _agent_forwarding _ssh_env_cache +unfunction _start_agent From 142ad842d706e16317bcc8b44ef1b19d625d1fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 21 Aug 2016 02:21:47 +0200 Subject: [PATCH 5/6] Simplify PID check of current ssh-agent --- plugins/ssh-agent/ssh-agent.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh index a427b80ae..e6bc9c4e6 100644 --- a/plugins/ssh-agent/ssh-agent.plugin.zsh +++ b/plugins/ssh-agent/ssh-agent.plugin.zsh @@ -30,7 +30,7 @@ if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then elif [[ -f "$_ssh_env_cache" ]]; then # Source SSH settings, if applicable . $_ssh_env_cache > /dev/null - ps x | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null || { + ps -o cmd -p $SSH_AGENT_PID | grep -q ssh-agent || { _start_agent } else From 53c3567cc395127d6ba90b8a010e8f7a48a16713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 30 Sep 2014 11:33:42 +0200 Subject: [PATCH 6/6] Force ssh-agent output to use bourne-style syntax On systems where the shell cannot be changed because of a strict security policy, ssh-agent will use the syntax of whatever the default $SHELL is. For instance, if the default shell is tcsh, ssh-agent will use the c-shell style (setenv). This change forces ssh-agent to use bourne-style syntax since that has to be later interpreted by zsh. Consequently, the environment file will contain `export' statements from now on (instead of `setenv'). --- plugins/ssh-agent/ssh-agent.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh index e6bc9c4e6..a84aac58c 100644 --- a/plugins/ssh-agent/ssh-agent.plugin.zsh +++ b/plugins/ssh-agent/ssh-agent.plugin.zsh @@ -7,7 +7,7 @@ function _start_agent() { # start ssh-agent and setup environment zstyle -s :omz:plugins:ssh-agent lifetime lifetime - ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache + ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache chmod 600 $_ssh_env_cache . $_ssh_env_cache > /dev/null