-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.go
202 lines (175 loc) · 4.49 KB
/
main.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
package main
import (
"bytes"
"encoding/base64"
"errors"
"flag"
"fmt"
"log"
"net"
"github.com/fsnotify/fsnotify"
"github.com/kennylevinsen/sshmux"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
)
type Host struct {
Address string `json:"address"`
Users []string `json:"users"`
NoAuth bool `json:"noAuth"`
}
type User struct {
PublicKey string `json:"publicKey"`
Name string `json:"name"`
}
var configFile = flag.String("config", "", "User-supplied configuration file to use")
func parseUsers() ([]*sshmux.User, error) {
var users []*sshmux.User
us := make([]User, 0)
err := viper.UnmarshalKey("users", &us)
if err != nil {
return nil, err
}
for _, u := range us {
encoded, err := base64.StdEncoding.DecodeString(u.PublicKey)
if err != nil {
return nil, errors.New("Could not decode key: " + u.Name)
}
pk, err := ssh.ParsePublicKey([]byte(encoded))
if err != nil {
return nil, errors.New(err.Error() + " for " + u.Name)
}
u := &sshmux.User{
PublicKey: pk,
Name: u.Name,
}
users = append(users, u)
}
return users, nil
}
func main() {
flag.Parse()
viper.SetDefault("address", ":22")
viper.SetDefault("hostkey", "hostkey")
viper.SetDefault("authkeys", "authkeys")
viper.SetConfigName("sshmuxd")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.sshmuxd")
viper.AddConfigPath("/etc/sshmuxd/")
viper.SetConfigFile(*configFile)
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Error parsing the config file: %s\n", err))
}
log.Printf("Config File used: %s", viper.ConfigFileUsed())
hosts := make([]Host, 0)
err = viper.UnmarshalKey("hosts", &hosts)
if err != nil {
panic(fmt.Errorf("Error parsing the config file hosts list: %s\n", err))
}
users, err := parseUsers()
if err != nil {
panic(fmt.Errorf("Error parsing the config file hosts list: %s\n", err))
}
hostSigner, err := ssh.ParsePrivateKey([]byte(viper.GetString("hostkey")))
if err != nil {
panic(err)
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Config file changed:", e.Name)
nh := make([]Host, 0)
err = viper.UnmarshalKey("hosts", &nh)
if err != nil {
log.Printf("Error parsing the config file hosts list: %s\n"+
"Keeping current host list", err)
} else {
hosts = nh
log.Printf("New hosts list: %+v\n", hosts)
}
if u, err := parseUsers(); err != nil {
log.Printf("Error parsing the config file users list: %s\n"+
"Keeping current users list", err)
} else {
users = u
}
h, err := ssh.ParsePrivateKey([]byte(viper.GetString("hostkey")))
if err != nil {
log.Printf("Error parsing the config file hostkey: %s\n"+
"Keeping current hostkey", err)
} else {
hostSigner = h
}
})
hasDefaults := false
for _, h := range hosts {
if h.NoAuth {
hasDefaults = true
break
}
}
// sshmux setup
auth := func(c ssh.ConnMetadata, key ssh.PublicKey) (*sshmux.User, error) {
t := key.Type()
k := key.Marshal()
for i := range users {
candidate := users[i].PublicKey
if t == candidate.Type() && bytes.Compare(k, candidate.Marshal()) == 0 {
return users[i], nil
}
}
if hasDefaults {
return nil, nil
}
log.Printf("%s: access denied (username: %s)", c.RemoteAddr(), c.User())
return nil, errors.New("access denied")
}
setup := func(session *sshmux.Session) error {
var username string
if session.User != nil {
username = session.User.Name
} else {
username = "unknown user"
}
log.Printf("%s: %s authorized (username: %s)", session.Conn.RemoteAddr(), username, session.Conn.User())
outer:
for _, h := range hosts {
if h.NoAuth {
session.Remotes = append(session.Remotes, &sshmux.Remote{
Names: []string{h.Address},
Address: h.Address,
})
continue outer
}
if session.User == nil {
continue
}
for _, u := range h.Users {
if u == session.User.Name {
session.Remotes = append(session.Remotes, &sshmux.Remote{
Names: []string{h.Address},
Address: h.Address,
})
continue outer
}
}
}
return nil
}
server := sshmux.New(hostSigner, auth, setup)
server.Selected = func(session *sshmux.Session, remote string) error {
var username string
if session.User != nil {
username = session.User.Name
} else {
username = "unknown user"
}
log.Printf("%s: %s connecting to %s", session.Conn.RemoteAddr(), username, remote)
return nil
}
// Set up listener
l, err := net.Listen("tcp", viper.GetString("address"))
if err != nil {
panic(err)
}
server.Serve(l)
}