forked from reconquest/shadowd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_ssh.go
72 lines (58 loc) · 1.25 KB
/
handle_ssh.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/reconquest/hierr-go"
"golang.org/x/crypto/ssh"
)
func (server *Server) HandleSSH(
writer http.ResponseWriter, request *http.Request,
) {
token := strings.TrimPrefix(request.URL.Path, "/ssh/")
keys, err := server.backend.GetPublicKeys(token)
if err != nil {
if err == ErrNotFound {
writer.WriteHeader(http.StatusNotFound)
return
}
log.Println(err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
_, err = writer.Write([]byte(keys))
if err != nil {
log.Println(err)
}
}
func handleSSHKeyAppend(backend Backend, args map[string]interface{}) error {
var (
token = args["<token>"].(string)
truncate = args["--truncate"].(bool)
)
key, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return hierr.Errorf(
err, "can't read stdin",
)
}
key = bytes.TrimSpace(key)
_, comment, _, _, err := ssh.ParseAuthorizedKey(key)
if err != nil {
return hierr.Errorf(
err, "can't parse public ssh key",
)
}
err = backend.AddPublicKey(token, key, truncate)
if err != nil {
return hierr.Errorf(
err, "can't add public key for %s", token,
)
}
fmt.Println("Added new key with comment:", comment)
return nil
}