-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
343 lines (297 loc) · 9.46 KB
/
server.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
/*Package sshd implements an SSH server.
See https://tools.ietf.org/html/rfc4254
This was copied over (and effectively forked from) cookoo-ssh. Mainly this
differs from the cookoo-ssh version in that this does not act like a
stand-alone SSH server.
*/
package sshd
import (
"encoding/binary"
"fmt"
"net"
"strings"
"sync"
"text/template"
"github.com/Masterminds/cookoo"
"github.com/Masterminds/cookoo/log"
"github.com/Masterminds/cookoo/safely"
"golang.org/x/crypto/ssh"
)
const (
// HostKeys is the context key for Host Keys list.
HostKeys string = "ssh.HostKeys"
// Address is the context key for SSH address.
Address string = "ssh.Address"
// ServerConfig is the context key for ServerConfig object.
ServerConfig string = "ssh.ServerConfig"
)
// PrereceiveHookTmpl is a pre-receive hook.
const PrereceiveHookTpl = `#!/bin/bash
strip_remote_prefix() {
stdbuf -i0 -o0 -e0 sed "s/^/"$'\e[1G'"/"
}
echo "pre-receive hook START"
set -eo pipefail; while read oldrev newrev refname; do
[[ $refname = "refs/heads/master" ]] && git archive $newrev | {{.Receiver}} "$RECEIVE_REPO" "$newrev" | strip_remote_prefix
done
echo "pre-receive hook END"
`
// Serve starts a native SSH server.
//
// The general design of the server is that it acts as a main server for
// a Cookoo app. It assumes that certain things have been configured for it,
// like an ssh.ServerConfig. Once it runs, it will block until the main
// process terminates. If you want to stop it prior to that, you can grab
// the closer ("sshd.Closer") out of the context and send it a signal.
//
// Currently, the service is not generic. It only runs git hooks.
//
// This expects the following Context variables.
// - ssh.Hostkeys ([]ssh.Signer): Host key, as an unparsed byte slice.
// - ssh.Address (string): Address/port
// - ssh.ServerConfig (*ssh.ServerConfig): The server config to use.
//
// This puts the following variables into the context:
// - ssh.Closer (chan interface{}): Send a message to this to shutdown the server.
func Serve(reg *cookoo.Registry, router *cookoo.Router, c cookoo.Context) cookoo.Interrupt {
hostkeys := c.Get(HostKeys, []ssh.Signer{}).([]ssh.Signer)
addr := c.Get(Address, "0.0.0.0:2223").(string)
cfg := c.Get(ServerConfig, &ssh.ServerConfig{}).(*ssh.ServerConfig)
for _, hk := range hostkeys {
cfg.AddHostKey(hk)
log.Infof(c, "Added hostkey.")
}
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
srv := &server{
c: c,
gitHome: "/home/git",
}
closer := make(chan interface{}, 1)
c.Put("sshd.Closer", closer)
log.Infof(c, "Listening on %s", addr)
srv.listen(listener, cfg, closer)
return nil
}
// server is the struct that encapsulates the SSH server.
type server struct {
c cookoo.Context
gitHome string
hookTpl *template.Template
createLock sync.Mutex
}
// listen handles accepting and managing connections. However, since closer
// is len(1), it will not block the sender.
func (s *server) listen(l net.Listener, conf *ssh.ServerConfig, closer chan interface{}) error {
cxt := s.c
log.Info(cxt, "Accepting new connections.")
defer l.Close()
// FIXME: Since Accept blocks, closer may not be checked often enough.
for {
log.Info(cxt, "Checking closer.")
if len(closer) > 0 {
<-closer
log.Info(cxt, "Shutting down SSHD listener.")
return nil
}
conn, err := l.Accept()
if err != nil {
log.Warnf(cxt, "Error during Accept: %s", err)
// We shouldn't kill the listener because of an error.
return err
}
safely.GoDo(cxt, func() {
s.handleConn(conn, conf)
})
}
}
// handleConn handles an individual client connection.
//
// It manages the connection, but passes channels on to `answer()`.
func (s *server) handleConn(conn net.Conn, conf *ssh.ServerConfig) {
defer conn.Close()
log.Info(s.c, "Accepted connection.")
_, chans, reqs, err := ssh.NewServerConn(conn, conf)
if err != nil {
// Handshake failure.
log.Errf(s.c, "Failed handshake: %s (%v)", err, conn)
return
}
// Discard global requests. We're only concerned with channels.
safely.GoDo(s.c, func() { ssh.DiscardRequests(reqs) })
condata := sshConnection(conn)
// Now we handle the channels.
for incoming := range chans {
log.Infof(s.c, "Channel type: %s\n", incoming.ChannelType())
if incoming.ChannelType() != "session" {
incoming.Reject(ssh.UnknownChannelType, "Unknown channel type")
}
channel, req, err := incoming.Accept()
if err != nil {
// Should close request and move on.
panic(err)
}
safely.GoDo(s.c, func() { s.answer(channel, req, condata) })
}
conn.Close()
}
// sshConnection generates the SSH_CONNECTION environment variable.
//
// This is untested on UNIX sockets.
func sshConnection(conn net.Conn) string {
remote := conn.RemoteAddr().String()
local := conn.LocalAddr().String()
rhost, rport, _ := net.SplitHostPort(remote)
lhost, lport, _ := net.SplitHostPort(local)
return fmt.Sprintf("%s %d %s %d", rhost, rport, lhost, lport)
}
func sendExitStatus(status uint32, channel ssh.Channel) error {
exit := struct{ Status uint32 }{uint32(0)}
_, err := channel.SendRequest("exit-status", false, ssh.Marshal(exit))
return err
}
// answer handles answering requests and channel requests
//
// Currently, an exec must be either "ping", "git-receive-pack" or
// "git-upload-pack". Anything else will result in a failure response. Right
// now, we leave the channel open on failure because it is unclear what the
// correct behavior for a failed exec is.
//
// Support for setting environment variables via `env` has been disabled.
func (s *server) answer(channel ssh.Channel, requests <-chan *ssh.Request, sshConn string) error {
defer channel.Close()
// Answer all the requests on this connection.
for req := range requests {
ok := false
// I think that ideally what we want to do here is pass this on to
// the Cookoo router and let it handle each Type on its own.
switch req.Type {
case "env":
o := &EnvVar{}
ssh.Unmarshal(req.Payload, o)
fmt.Printf("Key='%s', Value='%s'\n", o.Name, o.Value)
req.Reply(true, nil)
case "exec":
clean := cleanExec(req.Payload)
parts := strings.SplitN(clean, " ", 2)
router := s.c.Get("cookoo.Router", nil).(*cookoo.Router)
// XXX: Should we unset the context value 'cookoo.Router'?
// We need a shallow copy of the context to avoid race conditions.
cxt := s.c.Copy()
cxt.Put("SSH_CONNECTION", sshConn)
// Only allow commands that we know about.
switch parts[0] {
case "ping":
cxt.Put("channel", channel)
cxt.Put("request", req)
sshPing := cxt.Get("route.sshd.sshPing", "sshPing").(string)
err := router.HandleRequest(sshPing, cxt, true)
if err != nil {
log.Warnf(s.c, "Error pinging: %s", err)
}
return err
case "git-receive-pack", "git-upload-pack":
if len(parts) < 2 {
log.Warn(s.c, "Expected two-part command.\n")
req.Reply(ok, nil)
break
}
req.Reply(true, nil) // We processed. Yay.
cxt.Put("channel", channel)
cxt.Put("request", req)
cxt.Put("operation", parts[0])
cxt.Put("repository", parts[1])
sshGitReceive := cxt.Get("route.sshd.sshGitReceive", "sshGitReceive").(string)
err := router.HandleRequest(sshGitReceive, cxt, true)
var xs uint32
if err != nil {
log.Errf(s.c, "Failed git receive: %v", err)
xs = 1
}
sendExitStatus(xs, channel)
return nil
default:
log.Warnf(s.c, "Illegal command is '%s'\n", clean)
req.Reply(false, nil)
return nil
}
if err := sendExitStatus(0, channel); err != nil {
log.Errf(s.c, "Failed to write exit status: %s", err)
}
return nil
default:
// We simply ignore all of the other cases and leave the
// channel open to take additional requests.
log.Infof(s.c, "Received request of type %s\n", req.Type)
req.Reply(false, nil)
}
}
return nil
}
// ExecCmd is an SSH exec request
type ExecCmd struct {
Value string
}
// EnvVar is an SSH env request
type EnvVar struct {
Name string
Value string
}
// GenericMessage describes a simple string message, which is common in SSH.
type GenericMessage struct {
Value string
}
// cleanExec cleans the exec string.
func cleanExec(pay []byte) string {
e := &ExecCmd{}
ssh.Unmarshal(pay, e)
// XXX: Minimal escaping of values in command. There is probably a better
// way of doing this.
r := strings.NewReplacer("$", "", "`", "'")
return r.Replace(e.Value)
}
// parseString parses an encoded string according to the indicated length.
// From ssh.Unmarshal.
func parseString(in []byte) (out, rest []byte, ok bool) {
if len(in) < 4 {
return
}
length := binary.BigEndian.Uint32(in)
if uint32(len(in)) < 4+length {
return
}
out = in[4 : 4+length]
rest = in[4+length:]
ok = true
return
}
// parseEnv parses the key/value pairs in env requests.
func parseEnv(pay []byte) ([]byte, []byte) {
l := pay[3]
key := pay[4 : 4+l]
offset := l + 8
l = pay[7+l] // 4 for the offset, l for the key, 3 for the next three bytes.
val := pay[offset : l+offset]
return key, val
}
// Ping handles a simple test SSH exec.
//
// Returns the string PONG and exit status 0.
//
// Params:
// - channel (ssh.Channel): The channel to respond on.
// - request (*ssh.Request): The request.
//
func Ping(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
channel := p.Get("channel", nil).(ssh.Channel)
req := p.Get("request", nil).(*ssh.Request)
log.Info(c, "PING\n")
if _, err := channel.Write([]byte("pong")); err != nil {
log.Errf(c, "Failed to write to channel: %s", err)
}
sendExitStatus(0, channel)
req.Reply(true, nil)
return nil, nil
}