-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
daemon.go
290 lines (237 loc) · 6.76 KB
/
daemon.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
package main
import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"encoding/pem"
"fmt"
"net"
"os"
"path"
"path/filepath"
"time"
log "github.com/sirupsen/logrus"
"github.com/tg123/sshpiper/cmd/sshpiperd/internal/plugin"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh"
)
type daemon struct {
config *plugin.GrpcPluginConfig
lis net.Listener
loginGraceTime time.Duration
recorddir string
recordfmt string
usernameAsRecorddir bool
filterHostkeysReqeust bool
}
func generateSshKey(keyfile string) error {
_, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return err
}
privateKeyPEM, err := ssh.MarshalPrivateKey(privateKey, "")
if err != nil {
return err
}
privateKeyBytes := pem.EncodeToMemory(privateKeyPEM)
return os.WriteFile(keyfile, privateKeyBytes, 0600)
}
func newDaemon(ctx *cli.Context) (*daemon, error) {
config := &plugin.GrpcPluginConfig{}
config.SetDefaults()
keybase64 := ctx.String("server-key-data")
if keybase64 != "" {
log.Infof("parsing host key in base64 params")
privateBytes, err := base64.StdEncoding.DecodeString(keybase64)
if err != nil {
return nil, err
}
private, err := ssh.ParsePrivateKey([]byte(privateBytes))
if err != nil {
return nil, err
}
config.AddHostKey(private)
} else {
keyfile := ctx.String("server-key")
privateKeyFiles, err := filepath.Glob(keyfile)
if err != nil {
return nil, err
}
generate := false
switch ctx.String("server-key-generate-mode") {
case "notexist":
generate = len(privateKeyFiles) == 0
case "always":
generate = true
case "disable":
default:
return nil, fmt.Errorf("unknown server-key-generate-mode %v", ctx.String("server-key-generate-mode"))
}
if generate {
log.Infof("generating host key %v", keyfile)
if err := generateSshKey(keyfile); err != nil {
return nil, err
}
privateKeyFiles = []string{keyfile}
}
if len(privateKeyFiles) == 0 {
return nil, fmt.Errorf("no server key found")
}
log.Infof("found host keys %v", privateKeyFiles)
for _, privateKey := range privateKeyFiles {
log.Infof("loading host key %v", privateKey)
privateBytes, err := os.ReadFile(privateKey)
if err != nil {
return nil, err
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
return nil, err
}
config.AddHostKey(private)
}
}
lis, err := net.Listen("tcp", net.JoinHostPort(ctx.String("address"), ctx.String("port")))
if err != nil {
return nil, fmt.Errorf("failed to listen for connection: %v", err)
}
bannertext := ctx.String("banner-text")
bannerfile := ctx.String("banner-file")
if bannertext != "" || bannerfile != "" {
config.BannerCallback = func(_ ssh.ConnMetadata, _ ssh.ChallengeContext) string {
if bannerfile != "" {
text, err := os.ReadFile(bannerfile)
if err != nil {
log.Warnf("cannot read banner file %v: %v", bannerfile, err)
} else {
return string(text)
}
}
return bannertext
}
}
return &daemon{
config: config,
lis: lis,
loginGraceTime: ctx.Duration("login-grace-time"),
}, nil
}
func (d *daemon) install(plugins ...*plugin.GrpcPlugin) error {
if len(plugins) == 0 {
return fmt.Errorf("no plugins found")
}
if len(plugins) == 1 {
return plugins[0].InstallPiperConfig(d.config)
}
m := plugin.ChainPlugins{}
for _, p := range plugins {
if err := m.Append(p); err != nil {
return err
}
}
return m.InstallPiperConfig(d.config)
}
func (d *daemon) run() error {
defer d.lis.Close()
log.Infof("sshpiperd is listening on: %v", d.lis.Addr().String())
for {
conn, err := d.lis.Accept()
if err != nil {
log.Debugf("failed to accept connection: %v", err)
continue
}
log.Debugf("connection accepted: %v", conn.RemoteAddr())
go func(c net.Conn) {
defer c.Close()
pipec := make(chan *ssh.PiperConn)
errorc := make(chan error)
go func() {
p, err := ssh.NewSSHPiperConn(c, &d.config.PiperConfig)
if err != nil {
errorc <- err
return
}
pipec <- p
}()
var p *ssh.PiperConn
select {
case p = <-pipec:
case err := <-errorc:
log.Debugf("connection from %v establishing failed reason: %v", c.RemoteAddr(), err)
if d.config.PipeCreateErrorCallback != nil {
d.config.PipeCreateErrorCallback(c, err)
}
return
case <-time.After(d.loginGraceTime):
log.Debugf("pipe establishing timeout, disconnected connection from %v", c.RemoteAddr())
if d.config.PipeCreateErrorCallback != nil {
d.config.PipeCreateErrorCallback(c, fmt.Errorf("pipe establishing timeout"))
}
return
}
defer p.Close()
log.Infof("ssh connection pipe created %v (username [%v]) -> %v (username [%v])", p.DownstreamConnMeta().RemoteAddr(), p.DownstreamConnMeta().User(), p.UpstreamConnMeta().RemoteAddr(), p.UpstreamConnMeta().User())
var uphook func([]byte) ([]byte, error)
var downhook func([]byte) ([]byte, error)
if d.recorddir != "" {
var recorddir string
if d.usernameAsRecorddir {
recorddir = path.Join(d.recorddir, p.DownstreamConnMeta().User())
} else {
uniqID := plugin.GetUniqueID(p.ChallengeContext())
recorddir = path.Join(d.recorddir, uniqID)
}
err = os.MkdirAll(recorddir, 0700)
if err != nil {
log.Errorf("cannot create screen recording dir %v: %v", recorddir, err)
return
}
if d.recordfmt == "asciicast" {
prefix := ""
if d.usernameAsRecorddir {
// add prefix to avoid conflict
prefix = fmt.Sprintf("%d-", time.Now().Unix())
}
recorder := newAsciicastLogger(recorddir, prefix)
defer recorder.Close()
uphook = recorder.uphook
downhook = recorder.downhook
} else if d.recordfmt == "typescript" {
recorder, err := newFilePtyLogger(recorddir)
if err != nil {
log.Errorf("cannot create screen recording logger: %v", err)
return
}
defer recorder.Close()
uphook = recorder.loggingTty
}
}
if d.filterHostkeysReqeust {
nextUpHook := uphook
uphook = func(b []byte) ([]byte, error) {
if b[0] == 80 {
var x struct {
RequestName string `sshtype:"80"`
}
_ = ssh.Unmarshal(b, &x)
if x.RequestName == "[email protected]" || x.RequestName == "[email protected]" {
return nil, nil
}
}
if nextUpHook != nil {
return nextUpHook(b)
}
return b, nil
}
}
if d.config.PipeStartCallback != nil {
d.config.PipeStartCallback(p.DownstreamConnMeta(), p.ChallengeContext())
}
err = p.WaitWithHook(uphook, downhook)
if d.config.PipeErrorCallback != nil {
d.config.PipeErrorCallback(p.DownstreamConnMeta(), p.ChallengeContext(), err)
}
log.Infof("connection from %v closed reason: %v", c.RemoteAddr(), err)
}(conn)
}
}