-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
422 lines (378 loc) · 11.6 KB
/
exec.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
/*
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 (
"bufio"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
log "github.com/Sirupsen/logrus"
"github.com/kardianos/osext"
)
const (
defaultPath = "/bin:/usr/bin:/usr/local/bin:/sbin"
defaultEnvPath = "PATH=" + defaultPath
defaultTerm = "xterm"
defaultLoginDefsPath = "/etc/login.defs"
)
// execResult is used internally to send the result of a command execution from
// a goroutine to SSH request handler and back to the calling client
type execResult struct {
command string
// returned exec code
code int
}
type execReq struct {
Command string
}
// execResponse prepares the response to a 'exec' SSH request, i.e. executing
// a command after making an SSH connection and delivering the result back.
type execResponse struct {
cmdName string
cmd *exec.Cmd
ctx *ctx
}
// parseExecRequest parses SSH exec request
func parseExecRequest(req *ssh.Request, ctx *ctx) (*execResponse, error) {
var e execReq
if err := ssh.Unmarshal(req.Payload, &e); err != nil {
return nil, trace.BadParameter("failed to parse exec request, error: %v", err)
}
// split up command by space to grab the first word
args := strings.Split(e.Command, " ")
if len(args) > 0 {
_, f := filepath.Split(args[0])
// is this scp request?
if f == "scp" {
// for 'scp' requests, we'll launch ourselves with scp parameters:
teleportBin, err := osext.Executable()
if err != nil {
return nil, trace.Wrap(err)
}
e.Command = fmt.Sprintf("%s scp --remote-addr=%s --local-addr=%s %v",
teleportBin,
ctx.conn.RemoteAddr().String(),
ctx.conn.LocalAddr().String(),
strings.Join(args[1:], " "))
}
}
ctx.exec = &execResponse{
ctx: ctx,
cmdName: e.Command,
}
return ctx.exec, nil
}
func (e *execResponse) String() string {
return fmt.Sprintf("Exec(cmd=%v)", e.cmdName)
}
// prepInteractiveCommand configures exec.Cmd object for launching an interactive command
// (or a shell)
func prepInteractiveCommand(ctx *ctx) (*exec.Cmd, error) {
var (
err error
runShell bool
)
// determine shell for the given OS user:
if ctx.exec.cmdName == "" {
runShell = true
ctx.exec.cmdName, err = utils.GetLoginShell(ctx.login)
if err != nil {
log.Error(err)
return nil, trace.Wrap(err)
}
// in test mode short-circuit to /bin/sh
if ctx.isTestStub {
ctx.exec.cmdName = "/bin/sh"
}
}
c, err := prepareCommand(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
// this configures shell to run in 'login' mode. from openssh source:
// "If we have no command, execute the shell. In this case, the shell
// name to be passed in argv[0] is preceded by '-' to indicate that
// this is a login shell."
// https://github.com/openssh/openssh-portable/blob/master/session.c
if runShell {
c.Args = []string{"-" + filepath.Base(ctx.exec.cmdName)}
}
return c, nil
}
// prepareCommand configures exec.Cmd for executing a given command within an SSH
// session.
//
// 'cmd' is the string passed as parameter to 'ssh' command, like "ls -l /"
//
// If 'cmd' does not have any spaces in it, it gets executed directly, otherwise
// it is passed to user's shell for interpretation
func prepareCommand(ctx *ctx) (*exec.Cmd, error) {
osUserName := ctx.login
// configure UID & GID of the requested OS user:
osUser, err := user.Lookup(osUserName)
if err != nil {
return nil, trace.Wrap(err)
}
uid, err := strconv.Atoi(osUser.Uid)
if err != nil {
return nil, trace.Wrap(err)
}
gid, err := strconv.Atoi(osUser.Gid)
if err != nil {
return nil, trace.Wrap(err)
}
// get user's shell:
shell, err := utils.GetLoginShell(ctx.login)
if err != nil {
log.Warn(err)
}
if ctx.isTestStub {
shell = "/bin/sh"
}
// try and get the public address from the first available proxy. if public_address
// is not set, fall back to the hostname of the first proxy we get back.
proxyHost := "<proxyhost>:3080"
if ctx.srv != nil {
proxies, err := ctx.srv.authService.GetProxies()
if err != nil {
log.Errorf("Unexpected response from authService.GetProxies(): %v", err)
}
if len(proxies) > 0 {
proxyHost = proxies[0].GetPublicAddr()
if proxyHost == "" {
proxyHost = fmt.Sprintf("%v:%v", proxies[0].GetHostname(), defaults.HTTPListenPort)
log.Debugf("public_address not set for proxy, returning proxyHost: %q", proxyHost)
}
}
}
// by default, execute command using user's shell like openssh does:
// https://github.com/openssh/openssh-portable/blob/master/session.c
c := exec.Command(shell, "-c", ctx.exec.cmdName)
clusterName, err := ctx.srv.authService.GetDomainName()
if err != nil {
return nil, trace.Wrap(err)
}
c.Env = []string{
"LANG=en_US.UTF-8",
getDefaultEnvPath(osUser.Uid, defaultLoginDefsPath),
"HOME=" + osUser.HomeDir,
"USER=" + osUserName,
"SHELL=" + shell,
teleport.SSHTeleportUser + "=" + ctx.teleportUser,
teleport.SSHSessionWebproxyAddr + "=" + proxyHost,
teleport.SSHTeleportHostUUID + "=" + ctx.srv.ID(),
teleport.SSHTeleportClusterName + "=" + clusterName,
}
c.Dir = osUser.HomeDir
c.SysProcAttr = &syscall.SysProcAttr{}
if _, found := ctx.env["TERM"]; !found {
c.Env = append(c.Env, "TERM="+defaultTerm)
}
// execute the command under requested user's UID:GID
me, err := user.Current()
if err != nil {
return nil, trace.Wrap(err)
}
if me.Uid != osUser.Uid || me.Gid != osUser.Gid {
userGroups, err := osUser.GroupIds()
if err != nil {
return nil, trace.Wrap(err)
}
groups := make([]uint32, 0)
for _, sgid := range userGroups {
igid, err := strconv.Atoi(sgid)
if err != nil {
log.Warnf("Cannot interpret user group: '%v'", sgid)
} else {
groups = append(groups, uint32(igid))
}
}
if len(groups) == 0 {
groups = append(groups, uint32(gid))
}
c.SysProcAttr.Credential = &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
Groups: groups,
}
c.SysProcAttr.Setsid = true
}
// apply environment variables passed from the client
for n, v := range ctx.env {
c.Env = append(c.Env, fmt.Sprintf("%s=%s", n, v))
}
// apply SSH_xx environment variables
remoteHost, remotePort, err := net.SplitHostPort(ctx.conn.RemoteAddr().String())
if err != nil {
log.Warn(err)
} else {
localHost, localPort, err := net.SplitHostPort(ctx.conn.LocalAddr().String())
if err != nil {
log.Warn(err)
} else {
c.Env = append(c.Env,
fmt.Sprintf("SSH_CLIENT=%s %s %s", remoteHost, remotePort, localPort),
fmt.Sprintf("SSH_CONNECTION=%s %s %s %s", remoteHost, remotePort, localHost, localPort))
}
}
if ctx.session != nil {
if ctx.session.term != nil {
c.Env = append(c.Env, fmt.Sprintf("SSH_TTY=%s", ctx.session.term.tty.Name()))
}
if ctx.session.id != "" {
c.Env = append(c.Env, fmt.Sprintf("%s=%s", teleport.SSHSessionID, ctx.session.id))
}
}
// if the server allows reading in of ~/.tsh/environment read it in
// and pass environment variables along to new session
if ctx.srv.PermitUserEnvironment() {
filename := filepath.Join(osUser.HomeDir, ".tsh", "environment")
userEnvs, err := utils.ReadEnvironmentFile(filename)
if err != nil {
return nil, trace.Wrap(err)
}
c.Env = append(c.Env, userEnvs...)
}
return c, nil
}
// start launches the given command returns (nil, nil) if successful. execResult is only used
// to communicate an error while launching
func (e *execResponse) start(ch ssh.Channel) (*execResult, error) {
var err error
e.cmd, err = prepareCommand(e.ctx)
if err != nil {
return nil, trace.Wrap(err)
}
e.cmd.Stderr = ch.Stderr()
e.cmd.Stdout = ch
inputWriter, err := e.cmd.StdinPipe()
if err != nil {
return nil, trace.Wrap(err)
}
go func() {
io.Copy(inputWriter, ch)
inputWriter.Close()
}()
if err := e.cmd.Start(); err != nil {
e.ctx.Warningf("%v start failure err: %v", e, err)
return e.collectStatus(e.cmd, trace.ConvertSystemError(err))
}
e.ctx.Infof("%v started", e)
return nil, nil
}
func (e *execResponse) wait() (*execResult, error) {
if e.cmd.Process == nil {
e.ctx.Errorf("no process")
}
err := e.cmd.Wait()
return e.collectStatus(e.cmd, err)
}
func (e *execResponse) collectStatus(cmd *exec.Cmd, err error) (*execResult, error) {
status, err := collectStatus(e.cmd, err)
// report the result of this exec event to the audit logger
auditLog := e.ctx.srv.alog
if auditLog == nil {
return status, err
}
fields := events.EventFields{
events.ExecEventCommand: strings.Join(cmd.Args, " "),
events.EventUser: e.ctx.teleportUser,
events.EventLogin: e.ctx.login,
events.LocalAddr: e.ctx.conn.LocalAddr().String(),
events.RemoteAddr: e.ctx.conn.RemoteAddr().String(),
events.EventNamespace: e.ctx.srv.getNamespace(),
}
if err != nil {
fields[events.ExecEventError] = err.Error()
if status != nil {
fields[events.ExecEventCode] = strconv.Itoa(status.code)
}
}
auditLog.EmitAuditEvent(events.ExecEvent, fields)
return status, err
}
func collectStatus(cmd *exec.Cmd, err error) (*execResult, error) {
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
status := exitErr.Sys().(syscall.WaitStatus)
return &execResult{code: status.ExitStatus(), command: cmd.Path}, nil
}
return nil, err
}
status, ok := cmd.ProcessState.Sys().(syscall.WaitStatus)
if !ok {
return nil, fmt.Errorf("unknown exit status: %T(%v)", cmd.ProcessState.Sys(), cmd.ProcessState.Sys())
}
return &execResult{code: status.ExitStatus(), command: cmd.Path}, nil
}
// getDefaultEnvPath returns the default value of PATH environment variable for
// new logins (prior to shell) based on login.defs. Returns a strings which
// looks like "PATH=/usr/bin:/bin"
func getDefaultEnvPath(uid string, loginDefsPath string) string {
envPath := defaultEnvPath
envSuPath := defaultEnvPath
// open file, if it doesn't exist return a default path and move on
f, err := os.Open(loginDefsPath)
if err != nil {
log.Warn("Unable to open %q: %v: returning default path: %q", loginDefsPath, err, defaultEnvPath)
return defaultEnvPath
}
defer f.Close()
// read path to login.defs file /etc/login.defs line by line:
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// skip comments and empty lines:
if line == "" || line[0] == '#' {
continue
}
// look for a line that starts with ENV_SUPATH or ENV_PATH
fields := strings.Fields(line)
if len(fields) > 1 {
if fields[0] == "ENV_PATH" {
envPath = fields[1]
}
if fields[0] == "ENV_SUPATH" {
envSuPath = fields[1]
}
}
}
// if any error occurs while reading the file, return the default value
err = scanner.Err()
if err != nil {
log.Warnf("Unable to read %q: %v: returning default path: %q", loginDefsPath, err, defaultEnvPath)
return defaultEnvPath
}
// if requesting path for uid 0 and no ENV_SUPATH is given, fallback to
// ENV_PATH first, then the default path.
if uid == "0" {
if envSuPath == defaultEnvPath {
return envPath
}
return envSuPath
}
return envPath
}