1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/crypto/ssh"
16 // RequestAgentForwarding sets up agent forwarding for the session.
17 // ForwardToAgent or ForwardToRemote should be called to route
18 // the authentication requests.
19 func RequestAgentForwarding(session *ssh.Session) error {
20 ok, err := session.SendRequest("auth-agent-req@openssh.com", true, nil)
25 return errors.New("forwarding request denied")
30 // ForwardToAgent routes authentication requests to the given keyring.
31 func ForwardToAgent(client *ssh.Client, keyring Agent) error {
32 channels := client.HandleChannelOpen(channelType)
34 return errors.New("agent: already have handler for " + channelType)
38 for ch := range channels {
39 channel, reqs, err := ch.Accept()
43 go ssh.DiscardRequests(reqs)
45 ServeAgent(keyring, channel)
53 const channelType = "auth-agent@openssh.com"
55 // ForwardToRemote routes authentication requests to the ssh-agent
56 // process serving on the given unix socket.
57 func ForwardToRemote(client *ssh.Client, addr string) error {
58 channels := client.HandleChannelOpen(channelType)
60 return errors.New("agent: already have handler for " + channelType)
62 conn, err := net.Dial("unix", addr)
69 for ch := range channels {
70 channel, reqs, err := ch.Accept()
74 go ssh.DiscardRequests(reqs)
75 go forwardUnixSocket(channel, addr)
81 func forwardUnixSocket(channel ssh.Channel, addr string) {
82 conn, err := net.Dial("unix", addr)
90 io.Copy(conn, channel)
91 conn.(*net.UnixConn).CloseWrite()
95 io.Copy(channel, conn)