forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
630 lines (512 loc) · 14.8 KB
/
term.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
/*
Copyright 2015 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 srv
import (
"io"
"os"
"os/exec"
"os/user"
"strconv"
"sync"
"syscall"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/services"
rsession "github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/sshutils"
"github.com/docker/docker/pkg/term"
"github.com/gravitational/trace"
"github.com/kr/pty"
log "github.com/sirupsen/logrus"
)
// LookupUser is used to mock the value returned by user.Lookup(string).
type LookupUser func(string) (*user.User, error)
// LookupGroup is used to mock the value returned by user.LookupGroup(string).
type LookupGroup func(string) (*user.Group, error)
// Terminal defines an interface of handy functions for managing a (local or
// remote) PTY, such as resizing windows, executing commands with a PTY, and
// cleaning up.
type Terminal interface {
// AddParty adds another participant to this terminal. We will keep the
// Terminal open until all participants have left.
AddParty(delta int)
// Run will run the terminal.
Run() error
// Wait will block until the terminal is complete.
Wait() (*ExecResult, error)
// Continue will resume execution of the process after it completes its
// pre-processing routine (placed in a cgroup).
Continue()
// Kill will force kill the terminal.
Kill() error
// PTY returns the PTY backing the terminal.
PTY() io.ReadWriter
// TTY returns the TTY backing the terminal.
TTY() *os.File
// PID returns the PID of the Teleport process that was re-execed.
PID() int
// Close will free resources associated with the terminal.
Close() error
// GetWinSize returns the window size of the terminal.
GetWinSize() (*term.Winsize, error)
// SetWinSize sets the window size of the terminal.
SetWinSize(params rsession.TerminalParams) error
// GetTerminalParams is a fast call to get cached terminal parameters
// and avoid extra system call.
GetTerminalParams() rsession.TerminalParams
// SetTerminalModes sets the terminal modes from "pty-req"
SetTerminalModes(ssh.TerminalModes)
// GetTermType gets the terminal type set in "pty-req"
GetTermType() string
// SetTermType sets the terminal type from "pty-req"
SetTermType(string)
}
// NewTerminal returns a new terminal. Terminal can be local or remote
// depending on cluster configuration.
func NewTerminal(ctx *ServerContext) (Terminal, error) {
// It doesn't matter what mode the cluster is in, if this is a Teleport node
// return a local terminal.
if ctx.srv.Component() == teleport.ComponentNode {
return newLocalTerminal(ctx)
}
// If this is not a Teleport node, find out what mode the cluster is in and
// return the correct terminal.
if ctx.ClusterConfig.GetSessionRecording() == services.RecordAtProxy {
return newRemoteTerminal(ctx)
}
return newLocalTerminal(ctx)
}
// terminal is a local PTY created by Teleport nodes.
type terminal struct {
wg sync.WaitGroup
mu sync.Mutex
log *log.Entry
cmd *exec.Cmd
ctx *ServerContext
pty *os.File
tty *os.File
pid int
termType string
params rsession.TerminalParams
}
// NewLocalTerminal creates and returns a local PTY.
func newLocalTerminal(ctx *ServerContext) (*terminal, error) {
var err error
t := &terminal{
log: log.WithFields(log.Fields{
trace.Component: teleport.ComponentLocalTerm,
}),
ctx: ctx,
}
// Open PTY and corresponding TTY.
t.pty, t.tty, err = pty.Open()
if err != nil {
log.Warnf("Could not start PTY %v", err)
return nil, err
}
// Set the TTY owner. Failure is not fatal, for example Teleport is running
// on a read-only filesystem, but logging is useful for diagnostic purposes.
err = t.setOwner()
if err != nil {
log.Debugf("Unable to set TTY owner: %v.\n", err)
}
return t, nil
}
// AddParty adds another participant to this terminal. We will keep the
// Terminal open until all participants have left.
func (t *terminal) AddParty(delta int) {
t.wg.Add(delta)
}
// Run will run the terminal.
func (t *terminal) Run() error {
var err error
defer t.closeTTY()
// Create the command that will actually execute.
t.cmd, err = ConfigureCommand(t.ctx)
if err != nil {
return trace.Wrap(err)
}
// Pass PTY and TTY to child as well since a terminal is attached.
t.cmd.ExtraFiles = append(t.cmd.ExtraFiles, t.pty)
t.cmd.ExtraFiles = append(t.cmd.ExtraFiles, t.tty)
// Start the process.
err = t.cmd.Start()
if err != nil {
return trace.Wrap(err)
}
// Save off the PID of the Teleport process under which the shell is executing.
t.pid = t.cmd.Process.Pid
return nil
}
// Wait will block until the terminal is complete.
func (t *terminal) Wait() (*ExecResult, error) {
err := t.cmd.Wait()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
status := exitErr.Sys().(syscall.WaitStatus)
return &ExecResult{Code: status.ExitStatus(), Command: t.cmd.Path}, nil
}
return nil, err
}
status, ok := t.cmd.ProcessState.Sys().(syscall.WaitStatus)
if !ok {
return nil, trace.Errorf("unknown exit status: %T(%v)", t.cmd.ProcessState.Sys(), t.cmd.ProcessState.Sys())
}
return &ExecResult{
Code: status.ExitStatus(),
Command: t.cmd.Path,
}, nil
}
// Continue will resume execution of the process after it completes its
// pre-processing routine (placed in a cgroup).
func (t *terminal) Continue() {
t.ctx.contw.Close()
}
// Kill will force kill the terminal.
func (t *terminal) Kill() error {
if t.cmd.Process != nil {
if err := t.cmd.Process.Kill(); err != nil {
if err.Error() != "os: process already finished" {
return trace.Wrap(err)
}
}
}
return nil
}
// PTY returns the PTY backing the terminal.
func (t *terminal) PTY() io.ReadWriter {
return t.pty
}
// TTY returns the TTY backing the terminal.
func (t *terminal) TTY() *os.File {
return t.tty
}
// PID returns the PID of the Teleport process that was re-execed.
func (t *terminal) PID() int {
return t.pid
}
// Close will free resources associated with the terminal.
func (t *terminal) Close() error {
var err error
// note, pty is closed in the copying goroutine,
// not here to avoid data races
if t.tty != nil {
if e := t.tty.Close(); e != nil {
err = e
}
}
go t.closePTY()
return trace.Wrap(err)
}
func (t *terminal) closeTTY() {
if err := t.tty.Close(); err != nil {
t.log.Warnf("Failed to close TTY: %v", err)
}
t.tty = nil
}
func (t *terminal) closePTY() {
t.mu.Lock()
defer t.mu.Unlock()
defer t.log.Debugf("Closed PTY")
// wait until all copying is over (all participants have left)
t.wg.Wait()
t.pty.Close()
t.pty = nil
}
// GetWinSize returns the window size of the terminal.
func (t *terminal) GetWinSize() (*term.Winsize, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.pty == nil {
return nil, trace.NotFound("no pty")
}
ws, err := term.GetWinsize(t.pty.Fd())
if err != nil {
return nil, trace.Wrap(err)
}
return ws, nil
}
// SetWinSize sets the window size of the terminal.
func (t *terminal) SetWinSize(params rsession.TerminalParams) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.pty == nil {
return trace.NotFound("no pty")
}
if err := term.SetWinsize(t.pty.Fd(), params.Winsize()); err != nil {
return trace.Wrap(err)
}
t.params = params
return nil
}
// GetTerminalParams is a fast call to get cached terminal parameters
// and avoid extra system call.
func (t *terminal) GetTerminalParams() rsession.TerminalParams {
t.mu.Lock()
defer t.mu.Unlock()
return t.params
}
// GetTermType gets the terminal type set in "pty-req"
func (t *terminal) GetTermType() string {
return t.termType
}
// SetTermType sets the terminal type from "req-pty" request.
func (t *terminal) SetTermType(term string) {
if term == "" {
term = defaultTerm
}
t.termType = term
}
func (t *terminal) SetTerminalModes(termModes ssh.TerminalModes) {}
// getOwner determines the uid, gid, and mode of the TTY similar to OpenSSH:
// https://github.com/openssh/openssh-portable/blob/ddc0f38/sshpty.c#L164-L215
func getOwner(login string, lookupUser LookupUser, lookupGroup LookupGroup) (int, int, os.FileMode, error) {
var err error
var uid int
var gid int
var mode os.FileMode
// Lookup the Unix login for the UID and fallback GID.
u, err := lookupUser(login)
if err != nil {
return 0, 0, 0, trace.Wrap(err)
}
uid, err = strconv.Atoi(u.Uid)
if err != nil {
return 0, 0, 0, trace.Wrap(err)
}
// If the tty group exists, use that as the gid of the TTY and set mode to
// be u+rw. Otherwise use the group of the user with mode u+rw g+w.
group, err := lookupGroup("tty")
if err != nil {
gid, err = strconv.Atoi(u.Gid)
if err != nil {
return 0, 0, 0, trace.Wrap(err)
}
mode = 0620
} else {
gid, err = strconv.Atoi(group.Gid)
if err != nil {
return 0, 0, 0, trace.Wrap(err)
}
mode = 0600
}
return uid, gid, mode, nil
}
// setOwner changes the owner and mode of the TTY.
func (t *terminal) setOwner() error {
uid, gid, mode, err := getOwner(t.ctx.Identity.Login, user.Lookup, user.LookupGroup)
if err != nil {
return trace.Wrap(err)
}
err = os.Chown(t.tty.Name(), uid, gid)
if err != nil {
return trace.Wrap(err)
}
err = os.Chmod(t.tty.Name(), mode)
if err != nil {
return trace.Wrap(err)
}
log.Debugf("Set permissions on %v to %v:%v with mode %v.", t.tty.Name(), uid, gid, mode)
return nil
}
type remoteTerminal struct {
wg sync.WaitGroup
mu sync.Mutex
log *log.Entry
ctx *ServerContext
session *ssh.Session
params rsession.TerminalParams
termModes ssh.TerminalModes
ptyBuffer *ptyBuffer
termType string
}
func newRemoteTerminal(ctx *ServerContext) (*remoteTerminal, error) {
if ctx.RemoteSession == nil {
return nil, trace.BadParameter("remote session required")
}
t := &remoteTerminal{
log: log.WithFields(log.Fields{
trace.Component: teleport.ComponentRemoteTerm,
}),
ctx: ctx,
session: ctx.RemoteSession,
ptyBuffer: &ptyBuffer{},
}
return t, nil
}
func (t *remoteTerminal) AddParty(delta int) {
t.wg.Add(delta)
}
type ptyBuffer struct {
r io.Reader
w io.Writer
}
func (b *ptyBuffer) Read(p []byte) (n int, err error) {
return b.r.Read(p)
}
func (b *ptyBuffer) Write(p []byte) (n int, err error) {
return b.w.Write(p)
}
func (t *remoteTerminal) Run() error {
// prepare the remote remote session by setting environment variables
t.prepareRemoteSession(t.session, t.ctx)
// combine stdout and stderr
stdout, err := t.session.StdoutPipe()
if err != nil {
return trace.Wrap(err)
}
t.session.Stderr = t.session.Stdout
stdin, err := t.session.StdinPipe()
if err != nil {
return trace.Wrap(err)
}
// create a pty buffer that stdin and stdout are hooked up to
t.ptyBuffer = &ptyBuffer{
r: stdout,
w: stdin,
}
// if a specific term type was not requested, then pick the default one and request a pty
if t.termType == "" {
t.termType = defaultTerm
}
if err := t.session.RequestPty(t.termType, t.params.H, t.params.W, t.termModes); err != nil {
return trace.Wrap(err)
}
// we want to run a "exec" command within a pty
if t.ctx.ExecRequest.GetCommand() != "" {
t.log.Debugf("Running exec request within a PTY")
if err := t.session.Start(t.ctx.ExecRequest.GetCommand()); err != nil {
return trace.Wrap(err)
}
return nil
}
// we want an interactive shell
t.log.Debugf("Requesting an interactive terminal of type %v", t.termType)
if err := t.session.Shell(); err != nil {
return trace.Wrap(err)
}
return nil
}
func (t *remoteTerminal) Wait() (*ExecResult, error) {
err := t.session.Wait()
if err != nil {
if exitErr, ok := err.(*ssh.ExitError); ok {
return &ExecResult{
Code: exitErr.ExitStatus(),
Command: t.ctx.ExecRequest.GetCommand(),
}, err
}
return &ExecResult{
Code: teleport.RemoteCommandFailure,
Command: t.ctx.ExecRequest.GetCommand(),
}, err
}
return &ExecResult{
Code: teleport.RemoteCommandSuccess,
Command: t.ctx.ExecRequest.GetCommand(),
}, nil
}
// Continue does nothing for remote command execution.
func (r *remoteTerminal) Continue() {}
func (t *remoteTerminal) Kill() error {
err := t.session.Signal(ssh.SIGKILL)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func (t *remoteTerminal) PTY() io.ReadWriter {
return t.ptyBuffer
}
func (t *remoteTerminal) TTY() *os.File {
return nil
}
// PID returns the PID of the Teleport process that was re-execed. Always
// returns 0 for remote terminals.
func (t *remoteTerminal) PID() int {
return 0
}
func (t *remoteTerminal) Close() error {
// this closes the underlying stdin,stdout,stderr which is what ptyBuffer is
// hooked to directly
err := t.session.Close()
if err != nil {
return trace.Wrap(err)
}
t.log.Debugf("Closed remote terminal and underlying SSH session")
return nil
}
func (t *remoteTerminal) GetWinSize() (*term.Winsize, error) {
t.mu.Lock()
defer t.mu.Unlock()
return t.params.Winsize(), nil
}
func (t *remoteTerminal) SetWinSize(params rsession.TerminalParams) error {
t.mu.Lock()
defer t.mu.Unlock()
err := t.windowChange(params.W, params.H)
if err != nil {
return trace.Wrap(err)
}
t.params = params
return nil
}
func (t *remoteTerminal) GetTerminalParams() rsession.TerminalParams {
t.mu.Lock()
defer t.mu.Unlock()
return t.params
}
// GetTermType gets the terminal type set in "pty-req"
func (t *remoteTerminal) GetTermType() string {
return t.termType
}
func (t *remoteTerminal) SetTermType(term string) {
if term == "" {
term = defaultTerm
}
t.termType = term
}
func (t *remoteTerminal) SetTerminalModes(termModes ssh.TerminalModes) {
t.termModes = termModes
}
func (t *remoteTerminal) windowChange(w int, h int) error {
type windowChangeRequest struct {
W uint32
H uint32
Wpx uint32
Hpx uint32
}
req := windowChangeRequest{
W: uint32(w),
H: uint32(h),
Wpx: uint32(w * 8),
Hpx: uint32(h * 8),
}
_, err := t.session.SendRequest(sshutils.WindowChangeRequest, false, ssh.Marshal(&req))
return err
}
// prepareRemoteSession prepares the more session for execution.
func (t *remoteTerminal) prepareRemoteSession(session *ssh.Session, ctx *ServerContext) {
envs := map[string]string{
teleport.SSHTeleportUser: ctx.Identity.TeleportUser,
teleport.SSHSessionWebproxyAddr: ctx.ProxyPublicAddress(),
teleport.SSHTeleportHostUUID: ctx.srv.ID(),
teleport.SSHTeleportClusterName: ctx.ClusterName,
teleport.SSHSessionID: string(ctx.session.id),
}
for k, v := range envs {
if err := session.Setenv(k, v); err != nil {
t.log.Debugf("Unable to set environment variable: %v: %v", k, v)
}
}
}