forked from gliderlabs/ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent forwarding support (gliderlabs#31)
* agent: added agent forwarding support with an example * context: encode session id to hex string * agent: ensure conn doesn't change in closure as loop iterates * tests: use HostKeyCallback in ClientConfig * README: noting examples in _example * agent: documented exported names, added constants for temp file creation Signed-off-by: Jeff Lindsay <[email protected]>
- Loading branch information
Showing
8 changed files
with
169 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os/exec" | ||
|
||
"github.com/gliderlabs/ssh" | ||
) | ||
|
||
func main() { | ||
ssh.Handle(func(s ssh.Session) { | ||
cmd := exec.Command("ssh-add", "-l") | ||
if ssh.AgentRequested(s) { | ||
l, err := ssh.NewAgentListener() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer l.Close() | ||
go ssh.ForwardAgentConnections(l, s) | ||
cmd.Env = append(s.Environ(), fmt.Sprintf("%s=%s", "SSH_AUTH_SOCK", l.Addr().String())) | ||
} else { | ||
cmd.Env = s.Environ() | ||
} | ||
cmd.Stdout = s | ||
cmd.Stderr = s.Stderr() | ||
if err := cmd.Run(); err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
}) | ||
|
||
log.Println("starting ssh server on port 2222...") | ||
log.Fatal(ssh.ListenAndServe(":2222", nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ssh | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"path" | ||
"sync" | ||
|
||
gossh "golang.org/x/crypto/ssh" | ||
) | ||
|
||
const ( | ||
agentRequestType = "[email protected]" | ||
agentChannelType = "[email protected]" | ||
|
||
agentTempDir = "auth-agent" | ||
agentListenFile = "listener.sock" | ||
) | ||
|
||
// contextKeyAgentRequest is an internal context key for storing if the | ||
// client requested agent forwarding | ||
var contextKeyAgentRequest = &contextKey{"auth-agent-req"} | ||
|
||
func setAgentRequested(sess *session) { | ||
sess.ctx.SetValue(contextKeyAgentRequest, true) | ||
} | ||
|
||
// AgentRequested returns true if the client requested agent forwarding. | ||
func AgentRequested(sess Session) bool { | ||
return sess.Context().Value(contextKeyAgentRequest) == true | ||
} | ||
|
||
// NewAgentListener sets up a temporary Unix socket that can be communicated | ||
// to the session environment and used for forwarding connections. | ||
func NewAgentListener() (net.Listener, error) { | ||
dir, err := ioutil.TempDir("", agentTempDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
l, err := net.Listen("unix", path.Join(dir, agentListenFile)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return l, nil | ||
} | ||
|
||
// ForwardAgentConnections takes connections from a listener to proxy into the | ||
// session on the OpenSSH channel for agent connections. It blocks and services | ||
// connections until the listener stop accepting. | ||
func ForwardAgentConnections(l net.Listener, s Session) { | ||
sshConn := s.Context().Value(ContextKeyConn).(gossh.Conn) | ||
for { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
return | ||
} | ||
go func(conn net.Conn) { | ||
defer conn.Close() | ||
channel, reqs, err := sshConn.OpenChannel(agentChannelType, nil) | ||
if err != nil { | ||
return | ||
} | ||
defer channel.Close() | ||
go gossh.DiscardRequests(reqs) | ||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
go func() { | ||
io.Copy(conn, channel) | ||
conn.(*net.UnixConn).CloseWrite() | ||
wg.Done() | ||
}() | ||
go func() { | ||
io.Copy(channel, conn) | ||
channel.CloseWrite() | ||
wg.Done() | ||
}() | ||
wg.Wait() | ||
}(conn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters