-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssharing.go
333 lines (242 loc) · 6.75 KB
/
ssharing.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
package main
import (
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"encoding/json"
"code.google.com/p/go.crypto/bcrypt"
"code.google.com/p/go.crypto/ssh"
)
const (
DEFAULT_SSH_KEY_LOCATION = "keys/id_rsa"
USERS_DIR = "users/"
DEFAULT_SSH_PORT = ":2222"
DEFAULT_HTTP_PORT = ":8080"
CONF_FILE = "ssharing.conf"
CONF_SSH_KEY_LOCATION = "ssh_key_location"
CONF_USERS_DIR = "users_dir_location"
CONF_SSH_PORT = "ssh_port"
CONF_HTTP_PORT = "http_port"
CONF_ENABLE_TLS = "enable_tls"
CONF_TLS_CERT_LOCATION = "tls_cert_location"
CONF_TLS_KEY_LOCATION = "tls_key_location"
)
type Upload struct {
path string
writer chan<- http.ResponseWriter
complete <-chan bool
}
type Configuration struct {
SshKeyLocation string
UsersDir string
SshPort string
HttpPort string
EnableTls bool
TlsCertLocation string
TlsKeyLocation string
}
var uploadMap map[string]Upload
var config Configuration
func loadConfiguration() Configuration {
if _, err := os.Stat(CONF_FILE); err == nil {
jsonConf, err := ioutil.ReadFile(CONF_FILE)
if err != nil {
log.Fatal("Can't read configuration file", err)
}
conf := Configuration{}
err = json.Unmarshal(jsonConf, &conf)
if err != nil {
log.Fatal("Failed to parse configuration file", err)
}
return conf
}
conf := Configuration{SshKeyLocation: DEFAULT_SSH_KEY_LOCATION, UsersDir: USERS_DIR, SshPort: DEFAULT_SSH_PORT, HttpPort: DEFAULT_HTTP_PORT, EnableTls: false}
jsonConf, _ := json.Marshal(&conf)
err := ioutil.WriteFile(CONF_FILE, jsonConf, 0644)
if err != nil {
log.Panic("Failed to create configuration file", err)
}
return conf
}
func newUser(name string, pass []byte) {
hash, err := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost)
if err != nil {
log.Panic("Failed to generate password hash", err)
}
userData := map[string]string{
"name": name,
"hash": string(hash),
}
jsonUserData, _ := json.Marshal(userData)
err = ioutil.WriteFile(config.UsersDir+name, jsonUserData, 0644)
if err != nil {
log.Panic("Failed to write user data file", err)
}
}
func findUser(name string) map[string]string {
userFilePath := config.UsersDir + name
if _, err := os.Stat(userFilePath); err == nil {
jsonUserData, err := ioutil.ReadFile(userFilePath)
if err != nil {
log.Panic("Failed to retrieve user information", err)
}
var userData map[string]string
err = json.Unmarshal(jsonUserData, &userData)
if err != nil {
log.Panic("Failed to unmarshal user data file", err)
}
return userData
}
return nil
}
func passwordCallback(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
userData := findUser(c.User())
if userData == nil {
newUser(c.User(), pass)
return nil, nil
}
err := bcrypt.CompareHashAndPassword([]byte(userData["hash"]), pass)
if err != nil {
return nil, err
}
return nil, nil
}
func handleScpFileTransfer(channel ssh.Channel, destDir string) error {
// send zero byte to tell scp on the other side we're ready to proceed...
channel.Write([]byte{0})
readBuf := make([]byte, 256)
go func() {
defer channel.Close()
bytesRead, err := channel.Read(readBuf)
if err != nil {
log.Panic("Error reading data", err)
}
cmdLine := strings.Trim(string(readBuf[:bytesRead]), "\n ")
cmdParts := strings.Split(cmdLine, " ")
code := string([]rune(cmdParts[0])[0])
// handle only single file transfer - scp command starting with "C"
if code == "C" {
// for now ignoring file mode
// extract byte length of transferred file
fileLength, err := strconv.Atoi(cmdParts[1])
if err != nil {
log.Panic("Can't parse scp command", err)
}
// file name of transferred file
fileName := cmdParts[2]
path := destDir + "/" + fileName
writer := make(chan http.ResponseWriter)
complete := make(chan bool)
uploadMap[path] = Upload{path, writer, complete}
w := <-writer
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(fileLength))
channel.Write([]byte{0})
// copy file content to to http response writer
_, err = io.CopyN(w, channel, int64(fileLength))
if err != nil {
// just log error for now
log.Println(err)
}
// respond with zero byte to confirm transfer success
channel.Write([]byte{0})
// tell http request handler that we're finished
complete <- true
}
}()
return nil
}
func listenSsh(addr string, config *ssh.ServerConfig) {
listener, err := net.Listen("tcp", addr)
if err != nil {
log.Panic("Failed to start listening", err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error establishing connection", err)
continue
}
go handleSshConnection(conn, config)
}
}
func handleSshConnection(conn net.Conn, config *ssh.ServerConfig) {
serverCon, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
log.Println("Handshake failed", err)
return
}
go ssh.DiscardRequests(reqs)
for newChannel := range chans {
channel, requests, err := newChannel.Accept()
if err != nil {
log.Println("Error accepting request", err)
return
}
go func(in <-chan *ssh.Request) {
for req := range in {
switch req.Type {
case "shell":
req.Reply(true, nil)
channel.Close()
case "exec":
req.Reply(true, nil)
go handleScpFileTransfer(channel, serverCon.User())
}
}
}(requests)
}
}
func listenHttp(addr string, tls bool) {
http.HandleFunc("/", handleHttpRequest)
var err error
if tls {
err = http.ListenAndServeTLS(addr, config.TlsCertLocation, config.TlsKeyLocation, nil)
} else {
err = http.ListenAndServe(addr, nil)
}
if err != nil {
log.Panic(err)
}
}
func handleHttpRequest(writer http.ResponseWriter, request *http.Request) {
path := strings.Split(strings.Trim(request.URL.Path, "/"), "/")
if len(path) == 2 {
filePath := path[0] + "/" + path[1]
if upload, ok := uploadMap[filePath]; ok {
upload.writer <- writer
<-upload.complete
delete(uploadMap, filePath)
} else {
http.NotFound(writer, request)
}
} else {
http.NotFound(writer, request)
}
}
func main() {
config = loadConfiguration()
// prepare directory layout
os.Mkdir(config.UsersDir, 0744)
sshServerConfig := &ssh.ServerConfig{
PasswordCallback: passwordCallback,
}
privateBytes, err := ioutil.ReadFile(config.SshKeyLocation)
if err != nil {
log.Fatal("Failed to load private key", err)
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
log.Fatal("Failed to parse private key", err)
}
sshServerConfig.AddHostKey(private)
uploadMap = make(map[string]Upload)
go listenSsh(config.SshPort, sshServerConfig)
// start http server
listenHttp(config.HttpPort, config.EnableTls)
}