-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.go
317 lines (276 loc) · 8.13 KB
/
server.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package sshclient
import (
"encoding/binary"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"unsafe"
"github.com/creack/pty"
"golang.org/x/crypto/ssh"
)
// Logger accomodates testing.T as a logger
type Logger interface {
Log(...interface{})
Logf(string, ...interface{})
}
// ExecHandler abstracts handling of ssh "exec"
type ExecHandler interface {
Exec(string) (int, error)
SetChannel(ssh.Channel)
}
// ServerOptions control the ssh server behavior
type ServerOptions struct {
Hostname string
Username string
Password string
KeyFile string
KeyBytes []byte
Port *int
Logger Logger
Exec ExecHandler
}
// MockHandler allows faking expected behavior
type MockHandler struct {
RC int
Stdout string
Stderr string
ch ssh.Channel
}
// SetChannel makes this an ExecHandler
func (m *MockHandler) SetChannel(ch ssh.Channel) {
m.ch = ch
}
// Exec makes this an ExecHandler
func (m *MockHandler) Exec(_ string) (int, error) {
fmt.Fprint(m.ch, m.Stdout)
fmt.Fprint(m.ch.Stderr(), m.Stderr)
return m.RC, nil
}
// EchoHandler is the default dummy handler
type EchoHandler struct {
ch ssh.Channel
}
// SetChannel makes this an ExecHandler
func (m *EchoHandler) SetChannel(ch ssh.Channel) {
m.ch = ch
}
// Exec makes this an ExecHandler
func (m *EchoHandler) Exec(cmd string) (int, error) {
fmt.Fprintf(m.ch, "command is: %q", cmd)
return 0, nil
}
// BashHandler runs a command in bash
type BashHandler struct {
ch ssh.Channel
}
// SetChannel makes this an ExecHandler
func (m *BashHandler) SetChannel(ch ssh.Channel) {
m.ch = ch
}
// Exec makes this an ExecHandler
func (m *BashHandler) Exec(cmd string) (int, error) {
basher := exec.Command("bash", "--noprofile", "--norc", "-c", cmd)
basher.Stdout = m.ch
basher.Stderr = m.ch.Stderr()
_, err := pty.Start(basher)
if err != nil {
return 0, fmt.Errorf("could not start pty: %w", err)
}
status, err := basher.Process.Wait()
if err != nil {
return basher.ProcessState.ExitCode(), fmt.Errorf("bash wait error: %w", err)
}
return status.ExitCode(), nil
}
type nonlLogger struct{}
// Log makes this a Logger
func (n nonlLogger) Log(_ ...interface{}) {}
// Logf makes this a Logger
func (n nonlLogger) Logf(_ string, _ ...interface{}) {}
// Server is a fake ssh server for unit testing
func Server(options *ServerOptions) (func(), error) {
if options.Exec == nil {
options.Exec = &EchoHandler{}
}
if options.Logger == nil {
options.Logger = nonlLogger{}
}
if options.Hostname == "" {
options.Hostname = "localhost"
}
config := &ssh.ServerConfig{}
if options.Password != "" {
//Define a function to run when a client attempts a password login
config.PasswordCallback = func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
// Should use constant-time compare (or better, salt+hash) in a production setting.
if c.User() == options.Username && string(pass) == options.Password {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
}
// You may also explicitly allow anonymous client authentication, though anon bash
// sessions may not be a wise idea
// NoClientAuth: true,
}
// You can generate a keypair with 'ssh-keygen -t rsa'
if options.KeyFile != "" {
if strings.HasPrefix(options.KeyFile, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("can't find home dir to find `~`: %w", err)
}
options.KeyFile = filepath.Join(home, options.KeyFile[2:])
}
privateBytes, err := ioutil.ReadFile(options.KeyFile)
if err != nil {
return nil, fmt.Errorf("failed to load private key (%s): %v", options.KeyFile, err)
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
config.AddHostKey(private)
}
if len(options.KeyBytes) > 0 {
private, err := ssh.ParsePrivateKey(options.KeyBytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
config.AddHostKey(private)
}
// to ensure we can start, by default we'll expect no port to be specified
// to avoid port conflicts, so we bind to :0 and report back the port chosen
var listenPort int
if options.Port == nil {
options.Port = &listenPort
}
addr := fmt.Sprintf("%s:%d", options.Hostname, listenPort)
listener, err := net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to listen on %s: %w", addr, err)
}
*(options.Port) = listener.Addr().(*net.TCPAddr).Port
addr = fmt.Sprintf("%s:%d", options.Hostname, *(options.Port))
listening := true
go func() {
options.Logger.Logf("Listening on %s...\n", addr)
for {
tcpConn, err := listener.Accept()
if err != nil {
if !listening {
break
}
options.Logger.Logf("Failed to accept incoming connection (%s)", err)
continue
}
// Before use, a handshake must be performed on the incoming net.Conn.
sshConn, chans, reqs, err := ssh.NewServerConn(tcpConn, config)
if err != nil {
options.Logger.Logf("Failed to handshake (%s)", err)
continue
}
options.Logger.Logf("New SSH connection from %s (%s)", sshConn.RemoteAddr(), sshConn.ClientVersion())
// Discard all global out-of-band Requests
go ssh.DiscardRequests(reqs)
// Accept all channels
go handleChannels(chans, options.Exec, options.Logger)
}
}()
close := func() {
options.Logger.Logf("closing listener")
listening = false
listener.Close()
}
return close, nil
}
func handleChannels(chans <-chan ssh.NewChannel, hndlr ExecHandler, logger Logger) {
// Service the incoming Channel channel in go routine
for newChannel := range chans {
go handleChannel(newChannel, hndlr, logger)
}
}
func handleChannel(newChannel ssh.NewChannel, hndlr ExecHandler, logger Logger) {
// Since we're handling a shell, we expect a
// channel type of "session". The also describes
// "x11", "direct-tcpip" and "forwarded-tcpip"
// channel types.
if t := newChannel.ChannelType(); t != "session" {
newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t))
return
}
// At this point, we have the opportunity to reject the client's
// request for another logical connection
connection, requests, err := newChannel.Accept()
if err != nil {
logger.Logf("Could not accept channel (%s)", err)
return
}
hndlr.SetChannel(connection)
// Sessions have out-of-band requests such as "shell", "pty-req" and "env"
go func() {
for req := range requests {
actionOk := true
switch req.Type {
case "shell":
// only accept the default shell,
// (i.e. no command in the Payload)
actionOk = len(req.Payload) == 0
/*
case "pty-req":
termLen := req.Payload[3]
w, h := parseDims(req.Payload[termLen+4:])
SetWinsize(bashf.Fd(), w, h)
// Responding true (OK) here will let the client
// know we have a pty ready for input
req.Reply(true, nil)
case "window-change":
w, h := parseDims(req.Payload)
SetWinsize(bashf.Fd(), w, h)
*/
case "exec":
cmd := string(req.Payload[4:])
rc, err := hndlr.Exec(cmd)
if err != nil {
logger.Logf("handler exec error: %v\n", err)
actionOk = false
}
logger.Logf("exec rc: %d\n", rc)
_, err = connection.SendRequest("exit-status", false, []byte{0, 0, 0, byte(rc)})
if err != nil {
logger.Logf("SendRequest error: %+v", err)
}
req.Reply(actionOk, nil)
connection.Close()
default:
logger.Logf("unhandled request type: %s\n", req.Type)
}
if req.WantReply {
req.Reply(actionOk, nil)
}
}
logger.Log("end of session requests")
}()
}
// parseDims extracts terminal dimensions (width x height) from the provided buffer.
func parseDims(b []byte) (uint32, uint32) {
w := binary.BigEndian.Uint32(b)
h := binary.BigEndian.Uint32(b[4:])
return w, h
}
// Winsize stores the Height and Width of a terminal.
type Winsize struct {
Height uint16
Width uint16
x uint16 // unused
y uint16 // unused
}
// SetWinsize sets the size of the given pty.
func SetWinsize(fd uintptr, w, h uint32) {
ws := &Winsize{Width: uint16(w), Height: uint16(h)}
syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
}