forked from kennylevinsen/sshmux
/
forward.go
233 lines (197 loc) · 4.82 KB
/
forward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package sshmux
import (
"fmt"
"io"
"net"
"sync"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
func proxy(reqs1, reqs2 <-chan *ssh.Request, channel1, channel2 ssh.Channel) {
var closer sync.Once
closeFunc := func() {
channel1.Close()
channel2.Close()
}
defer closer.Do(closeFunc)
closerChan := make(chan bool, 1)
go func() {
io.Copy(channel1, channel2)
closerChan <- true
}()
go func() {
io.Copy(channel2, channel1)
closerChan <- true
}()
for {
select {
case req := <-reqs1:
if req == nil {
return
}
b, err := channel2.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
return
}
req.Reply(b, nil)
case req := <-reqs2:
if req == nil {
return
}
b, err := channel1.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
return
}
req.Reply(b, nil)
case <-closerChan:
return
}
}
}
type channelOpenDirectMsg struct {
RAddr string
RPort uint32
LAddr string
LPort uint32
}
// ChannelForward establishes a secure channel forward (ssh -W) to the server
// requested by the user, assuming it is a permitted host.
func (s *Server) ChannelForward(session *Session, newChannel ssh.NewChannel) {
var msg channelOpenDirectMsg
ssh.Unmarshal(newChannel.ExtraData(), &msg)
address := fmt.Sprintf("%s:%d", msg.RAddr, msg.RPort)
permitted := false
for _, remote := range session.Remotes {
if remote == address {
permitted = true
break
}
}
if !permitted {
newChannel.Reject(ssh.Prohibited, "remote host access denied for user")
return
}
conn, err := net.Dial("tcp", address)
if err != nil {
newChannel.Reject(ssh.ConnectionFailed, fmt.Sprintf("error: %v", err))
return
}
channel, reqs, err := newChannel.Accept()
go ssh.DiscardRequests(reqs)
var closer sync.Once
closeFunc := func() {
channel.Close()
conn.Close()
}
go func() {
io.Copy(channel, conn)
closer.Do(closeFunc)
}()
go func() {
io.Copy(conn, channel)
closer.Do(closeFunc)
}()
}
type rw struct {
io.Reader
io.Writer
}
// SessionForward performs a regular forward, providing the user with an
// interactive remote host selection if necessary. This forwarding type
// requires agent forwarding in order to work.
func (s *Server) SessionForward(session *Session, newChannel ssh.NewChannel, chans <-chan ssh.NewChannel) {
// Okay, we're handling this as a regular session
sesschan, sessReqs, err := newChannel.Accept()
if err != nil {
return
}
stderr := sesschan.Stderr()
remote := ""
switch len(session.Remotes) {
case 0:
sesschan.Close()
return
case 1:
remote = session.Remotes[0]
default:
comm := rw{Reader: sesschan, Writer: stderr}
if s.Interactive == nil {
remote, err = DefaultInteractive(comm, session)
} else {
remote, err = s.Interactive(comm, session)
}
if err != nil {
sesschan.Close()
return
}
}
fmt.Fprintf(stderr, "Connecting to %s\r\n", remote)
// Set up the agent
agentChan, agentReqs, err := session.Conn.OpenChannel("auth-agent@openssh.com", nil)
if err != nil {
fmt.Fprintf(stderr, "\r\n====== sshmux ======\r\n")
fmt.Fprintf(stderr, "sshmux requires either agent forwarding or secure channel forwarding.\r\n")
fmt.Fprintf(stderr, "Either enable agent forwarding (-A), or use a ssh -W proxy command.\r\n")
fmt.Fprintf(stderr, "For more info, see the sshmux wiki.\r\n")
sesschan.Close()
return
}
defer agentChan.Close()
go ssh.DiscardRequests(agentReqs)
// Set up the client
ag := agent.NewClient(agentChan)
clientConfig := &ssh.ClientConfig{
User: session.Conn.User(),
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(ag.Signers),
},
}
client, err := ssh.Dial("tcp", remote, clientConfig)
if err != nil {
fmt.Fprintf(stderr, "Connect failed: %v\r\n", err)
sesschan.Close()
return
}
// Handle all incoming channel requests
go func() {
for newChannel = range chans {
if newChannel == nil {
return
}
channel2, reqs2, err := client.OpenChannel(newChannel.ChannelType(), newChannel.ExtraData())
if err != nil {
x, ok := err.(*ssh.OpenChannelError)
if ok {
newChannel.Reject(x.Reason, x.Message)
} else {
newChannel.Reject(ssh.Prohibited, "remote server denied channel request")
}
continue
}
channel, reqs, err := newChannel.Accept()
if err != nil {
channel2.Close()
continue
}
go proxy(reqs, reqs2, channel, channel2)
}
}()
// Forward the session channel
channel2, reqs2, err := client.OpenChannel("session", []byte{})
if err != nil {
fmt.Fprintf(stderr, "Remote session setup failed: %v\r\n", err)
sesschan.Close()
return
}
// Proxy the channel and its requests
maskedReqs := make(chan *ssh.Request, 1)
go func() {
for req := range sessReqs {
if req.Type == "auth-agent-req@openssh.com" {
continue
}
maskedReqs <- req
}
}()
proxy(maskedReqs, reqs2, sesschan, channel2)
}