/
ssh_exec.go
282 lines (231 loc) · 6.15 KB
/
ssh_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
package sshdebug
import (
"context"
"errors"
"fmt"
"io"
"log"
"log/slog"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
sshd "github.com/costinm/ssh-mesh"
"golang.org/x/crypto/ssh"
)
// Based on okteto code: https://raw.githubusercontent.com/okteto/remote/main/pkg/ssh/ssh.go
// Removed deps on logger, integrated with ugate.
// Handles PTY/noPTY shell sessions and sftp.
// gliderlabs: current version doesn't work with certs. config() method requires a PublicKeyHandler, which
// doesn't have a reference to the conn ( because gliderlabs decided to invent it's 'better' interface ).
// In general the interface and abstractions are too complex and not needed.
var (
//idleTimeout = 60 * time.Second
// ErrEOF is the error when the terminal exits
ErrEOF = errors.New("EOF")
)
// sessionHandler handles the "session" channel.
// Based on build flags, it may handle sftp, PTY channels and exec.
// TODO: If "exec" is called, it may invoke http handlers and handle
// internal console/logs
func SessionHandler(ctx context.Context, sconn *sshd.SSHSMux, newChannel ssh.NewChannel) {
conn := sconn.ServerConn
isOwner := conn.Permissions.Extensions["role"] == "admin"
env := []*sshd.KV{}
if !isOwner {
sshd.SessionHandler(ctx, sconn, newChannel)
return
}
ch, reqs, _ := newChannel.Accept()
ssht := sconn.SSHServer
sess := &ptySession{
Channel: ch,
conn: conn,
}
// Requests are actively used.
// Extension: shell/exec can be called multiple times on a channel.
// Standard clients won't do this - no harm to skip an extra call.
for req := range reqs {
switch req.Type {
// shell has no args, should run a default shell.
// It is usually sent after requesting a pty.
// exec may be a command (/..) or may need to be evaluated.
// as a shell.
case "shell", "exec":
// This is normally the last command in a channel.
// Env and pty are called first.
//
var payload = struct{ Value string }{}
ssh.Unmarshal(req.Payload, &payload)
sess.rawCmd = payload.Value
req.Reply(true, nil)
go execHandler(ssht, conn, sess, env, payload.Value)
case "subsystem":
subsystemHandler(req, conn, ch)
case "env":
var kv sshd.KV
// Typical: LANG
ssh.Unmarshal(req.Payload, &kv)
env = append(env, &kv)
if req.WantReply {
req.Reply(true, nil)
}
sess.env = append(sess.env, fmt.Sprintf("%s=%s", kv.Key, kv.Value))
default:
// Typical pty-req, only for shell ( no params)
sess.handleRequest(req)
}
}
}
func exit(sess ssh.Channel, code int) error {
status := struct{ Status uint32 }{uint32(code)}
_, err := sess.SendRequest("exit-status", false, ssh.Marshal(&status))
if err != nil {
return err
}
return sess.Close()
}
func getExitStatusFromError(err error) int {
if err == nil {
return 0
}
exitErr, ok := err.(*exec.ExitError)
if !ok {
return 1
}
waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
if !ok {
if exitErr.Success() {
return 0
}
return 1
}
return waitStatus.ExitStatus()
}
func sendErrAndExit(s ssh.Channel, err error) {
msg := strings.TrimPrefix(err.Error(), "exec: ")
if _, err := s.Stderr().Write([]byte(msg)); err != nil {
log.Println("failed to write error back to session", err)
}
if err := exit(s, getExitStatusFromError(err)); err != nil {
log.Println(err, "pty session failed to exit")
}
}
func handleNoTTY(cmd *exec.Cmd, s ssh.Channel) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
if err = cmd.Start(); err != nil {
log.Println(err, "couldn't start nopty command '%s'", cmd.String())
return err
}
go func() {
defer stdin.Close()
if _, err := io.Copy(stdin, s); err != nil {
log.Println(err, "failed to write session to stdin.")
}
}()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
if _, err := io.Copy(s, stdout); err != nil {
log.Println(err, "failed to write stdout to session.")
}
}()
wg.Add(1)
go func() {
defer wg.Done()
if _, err := io.Copy(s.Stderr(), stderr); err != nil {
log.Println(err, "failed to write stderr to session.")
}
}()
wg.Wait()
if err := cmd.Wait(); err != nil {
log.Println(err, "command failed while waiting")
return err
}
return nil
}
func execHandlerInternal(ssht *sshd.SSHMesh, conn *ssh.ServerConn,
ch ssh.Channel, env []*sshd.KV, rawCmd string) {
t0 := time.Now()
defer func() {
slog.Info("sshd_exec_close", "dur", time.Since(t0),
"cmd", rawCmd)
}()
ch.Write([]byte("Dummy session"))
d := make([]byte, 1024)
for {
_, err := ch.Read(d)
if err != nil {
break
}
}
exit(ch, 0)
return
}
// Handle exec and shell commands.
// "raw" is the string command - like a URL, but with space separators.
//
// "admin" ( owner key or user ID in a client cert) can run shell session
// or run any command.
//
// Regular users have access to a restricted set of internal commands
// When the server is a real dropbear/sshd, this is handled with the
// native ssh permission system.
func execHandler(ssht *sshd.SSHMesh, conn *ssh.ServerConn, s *ptySession, env []*sshd.KV, raw string) {
t0 := time.Now()
defer func() {
s.Close()
slog.Info("sshd_exec_close", "dur", time.Since(t0),
"cmd", s.rawCmd)
}()
cmd := buildCmd(ssht, env, raw)
if s.pty != nil {
ok, err := s.handlePTY(cmd)
if ok {
if err != nil {
sendErrAndExit(s, err)
return
}
exit(s, 0)
return
}
}
if err := handleNoTTY(cmd, s); err != nil {
sendErrAndExit(s, err)
return
}
exit(s, 0)
}
func buildCmd(ssht *sshd.SSHMesh, env []*sshd.KV, rawCmd string) *exec.Cmd {
// TODO: differentiate between admin and user - run as UID if not admin.
// ( assuming the command is run as regular user)
var cmd *exec.Cmd
sh := "/bin/bash"
if len(rawCmd) == 0 {
cmd = exec.Command(sh)
} else {
// Running the exec in a shell is nice - no need for full path, etc.
// However with distroless there is no shell.
args := []string{"-c", rawCmd}
cmd = exec.Command(sh, args...)
}
cmd.Env = append(cmd.Env, os.Environ()...)
for _, k := range env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k.Key, k.Value))
}
return cmd
}