Browse code

Add support for agent forwarding

gwjo authored on 05/06/2011 at 01:07:55
Showing 1 changed files
... ...
@@ -1,23 +1,51 @@
1
-# Based on code from Joseph M. Reagle
2
-# http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
1
+#
2
+# INSTRUCTIONS
3
+#
4
+#   To enabled agent forwarding support add the following to
5
+#   your .zshrc file:
6
+#
7
+#     zstyle :omz:plugins:ssh-agent agent-forwarding on
8
+#
9
+#
10
+# CREDITS
11
+#
12
+#   Based on code from Joseph M. Reagle
13
+#   http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
14
+#
15
+#   Agent forwarding support based on ideas from
16
+#   Florent Thoumie and Jonas Pfenniger
17
+#
3 18
 
4
-local SSH_ENV=$HOME/.ssh/environment-$HOST
19
+local _plugin__ssh_env=$HOME/.ssh/environment-$HOST
20
+local _plugin__forwarding
5 21
 
6
-function start_agent {
7
-  /usr/bin/env ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
8
-  chmod 600 ${SSH_ENV}
9
-  . ${SSH_ENV} > /dev/null
22
+function _plugin__start_agent()
23
+{
24
+  /usr/bin/env ssh-agent | sed 's/^echo/#echo/' > ${_plugin__ssh_env}
25
+  chmod 600 ${_plugin__ssh_env}
26
+  . ${_plugin__ssh_env} > /dev/null
10 27
   /usr/bin/ssh-add;
11 28
 }
12 29
 
13
-# Source SSH settings, if applicable
30
+# test if agent-forwarding is enabled
31
+zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
32
+if [[ ${_plugin__forwarding} == "yes" && -z $SSH_AGENT_PID && -n "$SSH_AUTH_SOCK" ]]; then
33
+  # No PID but a AUTH_SOCK means agent forwarding is enabled
34
+  # Add a nifty symlink for screen/tmux 
35
+  [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
14 36
 
15
-if [ -f "${SSH_ENV}" ]; then
16
-  . ${SSH_ENV} > /dev/null
37
+elif [ -f "${_plugin__ssh_env}" ]; then
38
+  # Source SSH settings, if applicable
39
+  . ${_plugin__ssh_env} > /dev/null
17 40
   ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
18
-    start_agent;
41
+    _plugin__start_agent;
19 42
   }
20 43
 else
21
-  start_agent;
44
+  _plugin__start_agent;
22 45
 fi
23 46
 
47
+# tidy up after ourselves
48
+unfunction _plugin__start_agent
49
+unset _plugin__forwarding
50
+unset _plugin__ssh_env
51
+