-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshserver.go
827 lines (713 loc) · 24.2 KB
/
sshserver.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
Copyright 2017 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package forward
import (
"context"
"fmt"
"io"
"net"
"sync"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/pam"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/srv"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/proxy"
"github.com/gravitational/trace"
"github.com/pborman/uuid"
log "github.com/sirupsen/logrus"
)
// Server is a forwarding server. Server is used to create a single in-memory
// SSH server that will forward connections to a remote server. It's used along
// with the recording proxy to allow Teleport to record sessions with OpenSSH
// nodes at the proxy level.
//
// To create a forwarding server and serve a single SSH connection on it:
//
// serverConfig := forward.ServerConfig{
// ...
// }
// remoteServer, err := forward.New(serverConfig)
// if err != nil {
// return nil, trace.Wrap(err)
// }
// go remoteServer.Serve()
//
// conn, err := remoteServer.Dial()
// if err != nil {
// return nil, trace.Wrap(err)
// }
type Server struct {
log *log.Entry
id string
// targetConn is the TCP connection to the remote host.
targetConn net.Conn
// clientConn is the client half of the pipe used to connect the client
// and server.
clientConn net.Conn
// serverConn is the server half of the pipe used to connect the client and
// server.
serverConn net.Conn
// sconn is an authenticated SSH connection from the server perspective.
sconn *ssh.ServerConn
// remoteClient exposes an API to SSH functionality like shells, port
// forwarding, subsystems.
remoteClient *ssh.Client
// identityContext holds identity information about the user that has
// authenticated on sconn (like system login, Teleport username, roles).
identityContext srv.IdentityContext
// userAgent is the SSH user agent that was forwarded to the proxy.
userAgent agent.Agent
// hostCertificate is the SSH host certificate this in-memory server presents
// to the client.
hostCertificate ssh.Signer
// authHandlers are common authorization and authentication handlers shared
// by the regular and forwarding server.
authHandlers *srv.AuthHandlers
// termHandlers are common terminal handlers shared by the regular and
// forwarding server.
termHandlers *srv.TermHandlers
// ciphers is a list of ciphers that the server supports. If omitted,
// the defaults will be used.
ciphers []string
// kexAlgorithms is a list of key exchange (KEX) algorithms that the
// server supports. If omitted, the defaults will be used.
kexAlgorithms []string
// macAlgorithms is a list of message authentication codes (MAC) that
// the server supports. If omitted the defaults will be used.
macAlgorithms []string
authClient auth.ClientI
auditLog events.IAuditLog
authService auth.AccessPoint
sessionRegistry *srv.SessionRegistry
sessionServer session.Service
dataDir string
}
// ServerConfig is the configuration needed to create an instance of a Server.
type ServerConfig struct {
AuthClient auth.ClientI
UserAgent agent.Agent
TargetConn net.Conn
SrcAddr net.Addr
DstAddr net.Addr
HostCertificate ssh.Signer
// Ciphers is a list of ciphers that the server supports. If omitted,
// the defaults will be used.
Ciphers []string
// KEXAlgorithms is a list of key exchange (KEX) algorithms that the
// server supports. If omitted, the defaults will be used.
KEXAlgorithms []string
// MACAlgorithms is a list of message authentication codes (MAC) that
// the server supports. If omitted the defaults will be used.
MACAlgorithms []string
// DataDir is a local data directory used for local server storage
DataDir string
}
// CheckDefaults makes sure all required parameters are passed in.
func (s *ServerConfig) CheckDefaults() error {
if s.AuthClient == nil {
return trace.BadParameter("auth client required")
}
if s.DataDir == "" {
return trace.BadParameter("missing parameter DataDir")
}
if s.UserAgent == nil {
return trace.BadParameter("user agent required to connect to remote host")
}
if s.TargetConn == nil {
return trace.BadParameter("connection to target connection required")
}
if s.SrcAddr == nil {
return trace.BadParameter("source address required to identify client")
}
if s.DstAddr == nil {
return trace.BadParameter("destination address required to connect to remote host")
}
if s.HostCertificate == nil {
return trace.BadParameter("host certificate required to act on behalf of remote host")
}
return nil
}
// New creates a new unstarted Server.
func New(c ServerConfig) (*Server, error) {
// check and make sure we everything we need to build a forwarding node
err := c.CheckDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
// build a pipe connection to hook up the client and the server. we save both
// here and will pass them along to the context when we create it so they
// can be closed by the context.
serverConn, clientConn := utils.DualPipeNetConn(c.SrcAddr, c.DstAddr)
if err != nil {
return nil, trace.Wrap(err)
}
s := &Server{
log: log.WithFields(log.Fields{
trace.Component: teleport.ComponentForwardingNode,
trace.ComponentFields: map[string]string{
"src-addr": c.SrcAddr.String(),
"dst-addr": c.DstAddr.String(),
},
}),
id: uuid.New(),
targetConn: c.TargetConn,
serverConn: serverConn,
clientConn: clientConn,
userAgent: c.UserAgent,
hostCertificate: c.HostCertificate,
authClient: c.AuthClient,
auditLog: c.AuthClient,
authService: c.AuthClient,
sessionServer: c.AuthClient,
dataDir: c.DataDir,
}
s.sessionRegistry, err = srv.NewSessionRegistry(s)
if err != nil {
return nil, trace.Wrap(err)
}
// common auth handlers
s.authHandlers = &srv.AuthHandlers{
Entry: log.WithFields(log.Fields{
trace.Component: teleport.ComponentForwardingNode,
trace.ComponentFields: log.Fields{},
}),
Server: nil,
Component: teleport.ComponentForwardingNode,
AuditLog: c.AuthClient,
AccessPoint: c.AuthClient,
}
// common term handlers
s.termHandlers = &srv.TermHandlers{
SessionRegistry: s.sessionRegistry,
}
return s, nil
}
// GetDataDir returns server local storage
func (s *Server) GetDataDir() string {
return s.dataDir
}
// ID returns the ID of the proxy that creates the in-memory forwarding server.
func (s *Server) ID() string {
return s.id
}
// GetNamespace returns the namespace the forwarding server resides in.
func (s *Server) GetNamespace() string {
return defaults.Namespace
}
// AdvertiseAddr is the address of the remote host this forwarding server is
// connected to.
func (s *Server) AdvertiseAddr() string {
return s.clientConn.RemoteAddr().String()
}
// Component is the type of node this server is.
func (s *Server) Component() string {
return teleport.ComponentForwardingNode
}
// EmitAuditEvent sends an event to the Audit Log.
func (s *Server) EmitAuditEvent(eventType string, fields events.EventFields) {
auditLog := s.GetAuditLog()
if auditLog != nil {
if err := auditLog.EmitAuditEvent(eventType, fields); err != nil {
s.log.Error(err)
}
} else {
s.log.Warn("SSH server has no audit log")
}
}
// PermitUserEnvironment is always false because it's up the the remote host
// to decide if the user environment will be read or not.
func (s *Server) PermitUserEnvironment() bool {
return false
}
// GetAuditLog returns the Audit Log for this cluster.
func (s *Server) GetAuditLog() events.IAuditLog {
return s.auditLog
}
// GetAccessPoint returns an auth.AccessPoint for this cluster.
func (s *Server) GetAccessPoint() auth.AccessPoint {
return s.authService
}
// GetSessionServer returns a session server.
func (s *Server) GetSessionServer() session.Service {
return s.sessionServer
}
// GetPAM returns the PAM configuration for a server. Because the forwarding
// server runs in-memory, it does not support PAM.
func (s *Server) GetPAM() (*pam.Config, error) {
return nil, trace.BadParameter("PAM not supported by forwarding server")
}
// Dial returns the client connection created by pipeAddrConn.
func (s *Server) Dial() (net.Conn, error) {
return s.clientConn, nil
}
func (s *Server) Serve() {
config := &ssh.ServerConfig{
PublicKeyCallback: s.authHandlers.UserKeyAuth,
}
config.AddHostKey(s.hostCertificate)
sconn, chans, reqs, err := ssh.NewServerConn(s.serverConn, config)
if err != nil {
s.targetConn.Close()
s.clientConn.Close()
s.serverConn.Close()
s.log.Errorf("Unable to create server connection: %v.", err)
return
}
s.sconn = sconn
// Take connection and extract identity information for the user from it.
s.identityContext, err = s.authHandlers.CreateIdentityContext(sconn)
if err != nil {
s.targetConn.Close()
s.clientConn.Close()
s.serverConn.Close()
s.log.Errorf("Unable to create server connection: %v.", err)
return
}
// Connect and authenticate to the remote node.
s.log.Debugf("Creating remote connection to %v@%v", sconn.User(), s.clientConn.RemoteAddr().String())
s.remoteClient, err = s.newRemoteClient(sconn.User())
if err != nil {
// Reject the connection with an error so the client doesn't hang then
// close the connection.
s.rejectChannel(chans, err)
sconn.Close()
s.targetConn.Close()
s.clientConn.Close()
s.serverConn.Close()
s.log.Errorf("Unable to create remote connection: %v", err)
return
}
// Create a cancelable context and pass it to a keep alive loop. The keep
// alive loop will keep pinging the remote server and after it has missed a
// certain number of keep alive requests it will cancel the context which
// will close any listening goroutines.
heartbeatContext, cancel := context.WithCancel(context.Background())
go s.keepAliveLoop(cancel)
go s.handleConnection(heartbeatContext, chans, reqs)
}
// Close will close all underlying connections that the forwarding server holds.
func (s *Server) Close() error {
conns := []io.Closer{
s.sconn,
s.clientConn,
s.serverConn,
s.targetConn,
s.remoteClient,
}
var errs []error
for _, c := range conns {
if c == nil {
continue
}
err := c.Close()
if err != nil {
errs = append(errs, err)
}
}
return trace.NewAggregate(errs...)
}
// newRemoteSession will create and return a *ssh.Client and *ssh.Session
// with a remote host.
func (s *Server) newRemoteClient(systemLogin string) (*ssh.Client, error) {
// the proxy will use the agent that has been forwarded to it as the auth
// method when connecting to the remote host
if s.userAgent == nil {
return nil, trace.AccessDenied("agent must be forwarded to proxy")
}
authMethod := ssh.PublicKeysCallback(s.userAgent.Signers)
clientConfig := &ssh.ClientConfig{
User: systemLogin,
Auth: []ssh.AuthMethod{
authMethod,
},
HostKeyCallback: s.authHandlers.HostKeyAuth,
Timeout: defaults.DefaultDialTimeout,
}
if len(s.ciphers) > 0 {
clientConfig.Ciphers = s.ciphers
}
if len(s.kexAlgorithms) > 0 {
clientConfig.KeyExchanges = s.kexAlgorithms
}
if len(s.macAlgorithms) > 0 {
clientConfig.MACs = s.macAlgorithms
}
dstAddr := s.targetConn.RemoteAddr().String()
client, err := proxy.NewClientConnWithDeadline(s.targetConn, dstAddr, clientConfig)
if err != nil {
return nil, trace.Wrap(err)
}
return client, nil
}
func (s *Server) handleConnection(heartbeatContext context.Context, chans <-chan ssh.NewChannel, reqs <-chan *ssh.Request) {
defer s.log.Debugf("Closing forwarding server connected to %v and releasing resources.", s.sconn.LocalAddr())
defer s.Close()
for {
select {
// Global out-of-band requests.
case newRequest := <-reqs:
if newRequest == nil {
return
}
go s.handleGlobalRequest(newRequest)
// Channel requests.
case newChannel := <-chans:
if newChannel == nil {
return
}
go s.handleChannel(newChannel)
// If the heartbeats failed, close everything and cleanup.
case <-heartbeatContext.Done():
return
}
}
}
func (s *Server) keepAliveLoop(cancel context.CancelFunc) {
var missed int
// tick at 1/3 of the idle timeout duration
keepAliveTick := time.NewTicker(defaults.DefaultIdleConnectionDuration / 3)
defer keepAliveTick.Stop()
for {
select {
case <-keepAliveTick.C:
// send a keep alive to the target node and the client to ensure both are alive.
proxyToNodeOk := s.sendKeepAliveWithTimeout(s.remoteClient, defaults.ReadHeadersTimeout)
proxyToClientOk := s.sendKeepAliveWithTimeout(s.sconn, defaults.ReadHeadersTimeout)
if proxyToNodeOk && proxyToClientOk {
missed = 0
continue
}
// if we miss 3 in a row the connections dead, call cancel and cleanup
missed += 1
if missed == 3 {
s.log.Infof("Missed %v keep alive messages, closing connection", missed)
cancel()
return
}
}
}
}
func (s *Server) rejectChannel(chans <-chan ssh.NewChannel, err error) {
for newChannel := range chans {
err := newChannel.Reject(ssh.ConnectionFailed, err.Error())
if err != nil {
s.log.Errorf("Unable to reject and close connection.")
}
return
}
}
func (s *Server) handleGlobalRequest(req *ssh.Request) {
ok, payload, err := s.remoteClient.SendRequest(req.Type, req.WantReply, req.Payload)
if err != nil {
s.log.Warnf("Failed to forward global request %v: %v", req.Type, err)
return
}
if req.WantReply {
err = req.Reply(ok, payload)
if err != nil {
s.log.Warnf("Failed to reply to global request: %v: %v", req.Type, err)
}
}
}
func (s *Server) handleChannel(nch ssh.NewChannel) {
channelType := nch.ChannelType()
switch channelType {
// A client requested the terminal size to be sent along with every
// session message (Teleport-specific SSH channel for web-based terminals).
case "x-teleport-request-resize-events":
ch, _, _ := nch.Accept()
go s.handleTerminalResize(ch)
// Channels of type "session" handle requests that are invovled in running
// commands on a server.
case "session":
ch, requests, err := nch.Accept()
if err != nil {
s.log.Warnf("Unable to accept channel: %v", err)
nch.Reject(ssh.ConnectionFailed, fmt.Sprintf("unable to accept channel: %v", err))
return
}
go s.handleSessionRequests(ch, requests)
// Channels of type "direct-tcpip" handles request for port forwarding.
case "direct-tcpip":
req, err := sshutils.ParseDirectTCPIPReq(nch.ExtraData())
if err != nil {
s.log.Errorf("Failed to parse request data: %v, err: %v", string(nch.ExtraData()), err)
nch.Reject(ssh.UnknownChannelType, "failed to parse direct-tcpip request")
return
}
ch, _, err := nch.Accept()
if err != nil {
s.log.Warnf("Unable to accept channel: %v", err)
nch.Reject(ssh.ConnectionFailed, fmt.Sprintf("unable to accept channel: %v", err))
return
}
go s.handleDirectTCPIPRequest(ch, req)
default:
nch.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %v", channelType))
}
}
// handleDirectTCPIPRequest handles port forwarding requests.
func (s *Server) handleDirectTCPIPRequest(ch ssh.Channel, req *sshutils.DirectTCPIPReq) {
srcAddr := fmt.Sprintf("%v:%d", req.Orig, req.OrigPort)
dstAddr := fmt.Sprintf("%v:%d", req.Host, req.Port)
// Create context for this channel. This context will be closed when
// forwarding is complete.
ctx := srv.NewServerContext(s, s.sconn, s.identityContext)
ctx.RemoteClient = s.remoteClient
defer ctx.Close()
// Check if the role allows port forwarding for this user.
err := s.authHandlers.CheckPortForward(dstAddr, ctx)
if err != nil {
ch.Stderr().Write([]byte(err.Error()))
return
}
s.log.Debugf("Opening direct-tcpip channel from %v to %v in context %v.", srcAddr, dstAddr, ctx.ID())
defer s.log.Debugf("Completing direct-tcpip request from %v to %v in context %v.", srcAddr, dstAddr, ctx.ID())
// Create "direct-tcpip" channel from the remote host to the target host.
conn, err := s.remoteClient.Dial("tcp", dstAddr)
if err != nil {
ctx.Infof("Failed to connect to: %v: %v", dstAddr, err)
return
}
defer conn.Close()
// Emit a port forwarding audit event.
s.EmitAuditEvent(events.PortForwardEvent, events.EventFields{
events.PortForwardAddr: dstAddr,
events.PortForwardSuccess: true,
events.EventLogin: s.identityContext.Login,
events.LocalAddr: s.sconn.LocalAddr().String(),
events.RemoteAddr: s.sconn.RemoteAddr().String(),
})
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(ch, conn)
ch.Close()
}()
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(conn, ch)
conn.Close()
}()
wg.Wait()
}
// handleTerminalResize is called by the web proxy via its SSH connection.
// when a web browser connects to the web API, the web proxy asks us,
// by creating this new SSH channel, to start injecting the terminal size
// into every SSH write back to it.
//
// This is the only way to make web-based terminal UI not break apart
// when window changes its size.
func (s *Server) handleTerminalResize(channel ssh.Channel) {
err := s.sessionRegistry.PushTermSizeToParty(s.sconn, channel)
if err != nil {
s.log.Warnf("Unable to push terminal size to party: %v", err)
}
}
// handleSessionRequests handles out of band session requests once the session
// channel has been created this function's loop handles all the "exec",
// "subsystem" and "shell" requests.
func (s *Server) handleSessionRequests(ch ssh.Channel, in <-chan *ssh.Request) {
// Create context for this channel. This context will be closed when the
// session request is complete.
ctx := srv.NewServerContext(s, s.sconn, s.identityContext)
ctx.RemoteClient = s.remoteClient
ctx.AddCloser(ch)
defer ctx.Close()
// Create a "session" channel on the remote host.
remoteSession, err := s.remoteClient.NewSession()
if err != nil {
ch.Stderr().Write([]byte(err.Error()))
return
}
ctx.RemoteSession = remoteSession
s.log.Debugf("Opening session request to %v in context %v.", s.sconn.RemoteAddr(), ctx.ID())
defer s.log.Debugf("Closing session request to %v in context %v.", s.sconn.RemoteAddr(), ctx.ID())
for {
// Update the context with the session ID.
err := ctx.CreateOrJoinSession(s.sessionRegistry)
if err != nil {
errorMessage := fmt.Sprintf("unable to update context: %v", err)
ctx.Errorf("%v", errorMessage)
// Write the error to channel and close it.
ch.Stderr().Write([]byte(errorMessage))
_, err := ch.SendRequest("exit-status", false, ssh.Marshal(struct{ C uint32 }{C: teleport.RemoteCommandFailure}))
if err != nil {
ctx.Errorf("Failed to send exit status %v", errorMessage)
}
return
}
select {
case result := <-ctx.SubsystemResultCh:
// Subsystem has finished executing, close the channel and session.
ctx.Debugf("Subsystem execution result: %v", result.Err)
return
case req := <-in:
if req == nil {
// The client has closed or dropped the connection.
ctx.Debugf("Client %v disconnected", s.sconn.RemoteAddr())
return
}
if err := s.dispatch(ch, req, ctx); err != nil {
s.replyError(ch, req, err)
return
}
if req.WantReply {
req.Reply(true, nil)
}
case result := <-ctx.ExecResultCh:
ctx.Debugf("Exec request (%q) complete: %v", result.Command, result.Code)
// The exec process has finished and delivered the execution result, send
// the result back to the client, and close the session and channel.
_, err := ch.SendRequest("exit-status", false, ssh.Marshal(struct{ C uint32 }{C: uint32(result.Code)}))
if err != nil {
ctx.Infof("Failed to send exit status for %v: %v", result.Command, err)
}
return
}
}
}
func (s *Server) dispatch(ch ssh.Channel, req *ssh.Request, ctx *srv.ServerContext) error {
ctx.Debugf("Handling request %v, want reply %v.", req.Type, req.WantReply)
switch req.Type {
case sshutils.ExecRequest:
return s.termHandlers.HandleExec(ch, req, ctx)
case sshutils.PTYRequest:
return s.termHandlers.HandlePTYReq(ch, req, ctx)
case sshutils.ShellRequest:
return s.termHandlers.HandleShell(ch, req, ctx)
case sshutils.WindowChangeRequest:
return s.termHandlers.HandleWinChange(ch, req, ctx)
case sshutils.EnvRequest:
return s.handleEnv(ch, req, ctx)
case sshutils.SubsystemRequest:
return s.handleSubsystem(ch, req, ctx)
case sshutils.AgentForwardRequest:
// to maintain interoperability with OpenSSH, agent forwarding requests
// should never fail, all errors should be logged and we should continue
// processing requests.
err := s.handleAgentForward(ch, req, ctx)
if err != nil {
s.log.Debug(err)
}
return nil
default:
return trace.BadParameter(
"%v doesn't support request type '%v'", s.Component(), req.Type)
}
}
func (s *Server) handleAgentForward(ch ssh.Channel, req *ssh.Request, ctx *srv.ServerContext) error {
// Check if the user's RBAC role allows agent forwarding.
err := s.authHandlers.CheckAgentForward(ctx)
if err != nil {
return trace.Wrap(err)
}
// Route authentication requests to the agent that was forwarded to the proxy.
err = agent.ForwardToAgent(ctx.RemoteClient, s.userAgent)
if err != nil {
return trace.Wrap(err)
}
// Make an "auth-agent-req@openssh.com" request on the remote host.
err = agent.RequestAgentForwarding(ctx.RemoteSession)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func (s *Server) handleSubsystem(ch ssh.Channel, req *ssh.Request, ctx *srv.ServerContext) error {
subsystem, err := parseSubsystemRequest(req, ctx)
if err != nil {
return trace.Wrap(err)
}
// start the requested subsystem, if it fails to start return result right away
err = subsystem.Start(ch)
if err != nil {
ctx.SendSubsystemResult(srv.SubsystemResult{
Name: subsystem.subsytemName,
Err: trace.Wrap(err),
})
return trace.Wrap(err)
}
// wait for the subsystem to finish and return that result
go func() {
err := subsystem.Wait()
ctx.SendSubsystemResult(srv.SubsystemResult{
Name: subsystem.subsytemName,
Err: trace.Wrap(err),
})
}()
return nil
}
func (s *Server) handleEnv(ch ssh.Channel, req *ssh.Request, ctx *srv.ServerContext) error {
var e sshutils.EnvReqParams
if err := ssh.Unmarshal(req.Payload, &e); err != nil {
ctx.Error(err)
return trace.Wrap(err, "failed to parse env request")
}
err := ctx.RemoteSession.Setenv(e.Name, e.Value)
if err != nil {
s.log.Debugf("Unable to set environment variable: %v: %v", e.Name, e.Value)
}
return nil
}
// RequestSender is an interface that impliments SendRequest. It is used so
// server and client connections can be passed to functions to send requests.
type RequestSender interface {
// SendRequest is used to send a out-of-band request.
SendRequest(name string, wantReply bool, payload []byte) (bool, []byte, error)
}
// sendKeepAliveWithTimeout sends a keepalive@openssh.com message to the remote
// client. A manual timeout is needed here because SendRequest will wait for a
// response forever.
func (s *Server) sendKeepAliveWithTimeout(conn RequestSender, timeout time.Duration) bool {
errorCh := make(chan error, 1)
go func() {
_, _, err := conn.SendRequest(teleport.KeepAliveReqType, true, nil)
errorCh <- err
}()
select {
case err := <-errorCh:
if err != nil {
return false
}
return true
case <-time.After(timeout):
return false
}
}
func (s *Server) replyError(ch ssh.Channel, req *ssh.Request, err error) {
s.log.Error(err)
message := []byte(utils.UserMessageFromError(err))
ch.Stderr().Write(message)
if req.WantReply {
req.Reply(false, message)
}
}
func parseSubsystemRequest(req *ssh.Request, ctx *srv.ServerContext) (*remoteSubsystem, error) {
var r sshutils.SubsystemReq
err := ssh.Unmarshal(req.Payload, &r)
if err != nil {
return nil, trace.BadParameter("failed to parse subsystem request: %v", err)
}
return parseRemoteSubsystem(context.Background(), r.Name, ctx), nil
}