-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshd.go
351 lines (308 loc) · 9.17 KB
/
sshd.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
// Package sshd implements an SSH server.
//
// See https://tools.ietf.org/html/rfc4254
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"strings"
"github.com/smothiki/mybuilder/git"
"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"
multiplePush string = "Another git push is ongoing"
)
var errBuildAppPerm = errors.New("user has no permission to build the app")
var errDirPerm = errors.New("Cannot change directory in file name.")
var errDirCreatePerm = errors.New("Empty repo name.")
// AuthKey authenticates based on a public key.
func AuthKey(key ssh.PublicKey) (*ssh.Permissions, error) {
fmt.Println("Starting ssh authentication")
perm := &ssh.Permissions{
Extensions: map[string]string{
"user": "smothiki",
"fingerprint": "nothing",
"apps": "apps",
},
}
return perm, nil
}
// Configure creates a new SSH configuration object.
//
// Config sets a PublicKeyCallback handler that forwards public key auth
// requests to the route named "pubkeyAuth".
//
// This assumes certain details about our environment, like the location of the
// host keys. It also provides only key-based authentication.
// ConfigureServerSshConfig
//
// Returns:
// An *ssh.ServerConfig
func Configure() (*ssh.ServerConfig, error) {
cfg := &ssh.ServerConfig{
// PublicKeyCallback: func(m ssh.ConnMetadata, k ssh.PublicKey) (*ssh.Permissions, error) {
// return nil, nil
// },
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
// Should use constant-time compare (or better, salt+hash) in a production setting.
return nil, nil
},
}
path := "key"
// for _, t := range hostKeyTypes {
// path := fmt.Sprintf(pathTpl, t)
key, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("Failed to read key %s (skipping): %s\n", path, err)
return nil, err
}
hk, err := ssh.ParsePrivateKey(key)
if err != nil {
fmt.Printf("Failed to parse host key %s (skipping): %s\n", path, err)
return nil, err
}
fmt.Printf("Parsed host key %s.\n", path)
cfg.AddHostKey(hk)
cfg.Config.Ciphers = []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "[email protected]"}
return cfg, nil
}
// Serve starts a native SSH server.
func Serve(
cfg *ssh.ServerConfig,
gitHomeDir string,
addr, receivetype string) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
srv := &server{
gitHome: gitHomeDir,
receivetype: receivetype,
}
fmt.Printf("Listening on %s\n", addr)
srv.listen(listener, cfg)
return nil
}
// server is the struct that encapsulates the SSH server.
type server struct {
gitHome string
receivetype string
}
// 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) error {
fmt.Println("Accepting new connections.")
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
fmt.Printf("Error during Accept: %s\n", err)
// We shut down the listener if Accept errors
return err
}
go 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()
fmt.Println("Accepted connection.")
sshConn, chans, reqs, err := ssh.NewServerConn(conn, conf)
if err != nil {
// Handshake failure.
fmt.Printf("Failed handshake: %s\n", err)
return
}
// Discard global requests. We're only concerned with channels.
go ssh.DiscardRequests(reqs)
condata := sshConnection(conn)
// Now we handle the channels.
for incoming := range chans {
fmt.Printf("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)
}
go s.answer(channel, req, condata, sshConn)
}
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 %s %s %s", 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, condata string, sshconn *ssh.ServerConn) error {
defer channel.Close()
// Answer all the requests on this connection.
for req := range requests {
ok := false
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)
switch parts[0] {
case "ping":
err := Ping(channel, req)
if err != nil {
fmt.Printf("Error pinging: %s\n", err)
}
return err
case "git-receive-pack", "git-upload-pack":
if len(parts) < 2 {
fmt.Println("Expected two-part command.")
req.Reply(ok, nil)
break
}
repoName, err := cleanRepoName(parts[1])
if err != nil {
fmt.Printf("Illegal repo name: %s.\n", err)
channel.Stderr().Write([]byte("No repo given"))
return err
}
ferr := s.runReceive(req, sshconn, channel, repoName, parts, condata)
err = ferr()
if pktErr := gitPktLine(channel, fmt.Sprintf("ERR %v\n", multiplePush)); pktErr != nil {
fmt.Printf("Failed to write to channel: %s\n", err)
}
var xs uint32
sendExitStatus(xs, channel)
return nil
default:
fmt.Printf("Illegal command is '%s'\n", clean)
req.Reply(false, nil)
return nil
}
if err := sendExitStatus(0, channel); err != nil {
fmt.Printf("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.
fmt.Printf("Received request of type %s\n", req.Type)
req.Reply(false, nil)
}
}
return nil
}
func (s *server) runReceive(
req *ssh.Request,
sshConn *ssh.ServerConn,
channel ssh.Channel,
repoName string,
parts []string,
connData string,
) func() error {
return func() error {
req.Reply(true, nil) // We processed. Yay.
// if !strings.Contains(sshConn.Permissions.Extensions["apps"], repoName) {
// return errBuildAppPerm
// }
repo := repoName + ".git"
recvErr := git.Receive(
repo,
parts[0],
s.gitHome,
channel,
"nothing",
"smothiki",
connData,
s.receivetype,
)
return recvErr
}
}
// 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)
// TODO: Minimal escaping of values in command. There is probably a better
// way of doing this.
r := strings.NewReplacer("$", "", "`", "'")
return r.Replace(e.Value)
}
// 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(channel ssh.Channel, req *ssh.Request) error {
fmt.Println("PING")
if _, err := channel.Write([]byte("pong")); err != nil {
fmt.Printf("Failed to write to channel: %s\n", err)
}
sendExitStatus(0, channel)
req.Reply(true, nil)
return nil
}
// cleanRepoName cleans a repository name for a git-sh operation.
func cleanRepoName(name string) (string, error) {
if len(name) == 0 {
return name, errDirCreatePerm
}
if strings.Contains(name, "..") {
return "", errDirPerm
}
name = strings.Replace(name, "'", "", -1)
return strings.TrimPrefix(strings.TrimSuffix(name, ".git"), "/"), nil
}
// gitPktLine writes a line following the pkt-line git protocol. See https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt for the protocol and https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt for its usage.
func gitPktLine(w io.Writer, s string) error {
_, err := fmt.Fprintf(w, "%04x%s", len(s)+4, s)
return err
}