-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
301 lines (254 loc) · 6.92 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
package memguarded
import (
"crypto/rand"
"crypto/tls"
"crypto/x509"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
"golang.org/x/sys/unix"
"io"
"io/ioutil"
"net"
"os"
"os/user"
"reflect"
"strconv"
"syscall"
"time"
"unsafe"
)
type Server struct {
Timeout time.Duration
SocketPath string
StopOnAnyClientError bool
CertKey string
CertPem string
CAPem string
userUid uint32
commands map[string]func(net.Conn) error
stop chan struct{}
listener net.Listener
}
func (s *Server) Init(secretService *Service) error {
s.Timeout = 10 * time.Second
s.commands = make(map[string]func(net.Conn) error)
s.commands["set_secret"] = func(m net.Conn) error {
logs.Info("Set secret")
if err := secretService.FromReaderUntilNewLine(m); err != nil {
return err
}
return nil
}
s.commands["get_secret"] = func(m net.Conn) error {
logs.Info("Get secret")
err := secretService.Write(m)
if err != nil {
return err
}
return WriteBytes(m, []byte{'\n'})
}
uidStr, err := user.Current()
if err != nil {
return errs.WithE(err, "Failed to get current user uid")
}
uid, err := strconv.Atoi(uidStr.Uid)
if err != nil {
return errs.WithE(err, "Failed to convert current user uid to int")
}
s.userUid = uint32(uid)
return nil
}
func (s *Server) Start() error {
s.cleanupSocket()
s.stop = make(chan struct{}, 1)
cert, err := tls.LoadX509KeyPair(s.CertPem, s.CertKey)
if err != nil {
return errs.WithE(err, "Failed to load server key")
}
certpool := x509.NewCertPool()
pem, err := ioutil.ReadFile(s.CAPem)
if err != nil {
return errs.WithE(err, "Failed to read client CA certificate authority")
}
if !certpool.AppendCertsFromPEM(pem) {
return errs.WithE(err, "failed to parse client CA certificate authority")
}
config := tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: certpool,
Rand: rand.Reader,
}
listener, err := tls.Listen("unix", s.SocketPath, &config)
if err != nil {
return errs.WithEF(err, data.WithField("path", s.SocketPath), "Failed to listen on socket")
}
if err := os.Chmod(s.SocketPath, os.ModeSocket|0700); err != nil {
return errs.WithEF(err, data.WithField("socket", s.SocketPath), "Failed to set socket permissions")
}
s.listener = listener
defer s.cleanupSocket()
for {
conn, err := s.listener.Accept()
if err != nil {
select {
case <-s.stop:
return nil
default:
logs.WithE(err).Error("Failed to accept socket connection")
}
continue
}
socketStat, err := os.Stat(s.SocketPath)
if err != nil {
return errs.WithEF(err, data.WithField("socket", s.SocketPath), "Failed to get socket stats")
}
if socketStat.Mode() != os.ModeSocket|0700 {
return errs.WithF(data.WithField("socket", s.SocketPath).WithField("mode", socketStat.Mode()).WithField("xx", os.FileMode(0700)), "Socket mod changed")
}
//go s.handleConnection(conn)
s.handleConnection(conn) // we don't need a high throughput server
}
}
func (s *Server) Stop(e error) {
s.stop <- struct{}{}
if s.listener != nil {
_ = s.listener.Close()
}
}
/////////////////////
func (s *Server) cleanupSocket() {
_, err := os.Stat(s.SocketPath)
if os.IsNotExist(err) {
return
}
if err := syscall.Unlink(s.SocketPath); err != nil {
logs.WithEF(err, data.WithField("path", s.SocketPath)).Warn("Failed to unlink socket")
}
}
func (s *Server) handleConnection(conn net.Conn) {
err := s.handleConnectionE(conn)
if err != nil {
logs.WithE(err).Error("Client connection handle failed")
}
if err != nil && s.StopOnAnyClientError {
s.Stop(err)
}
}
func (s *Server) handleConnectionE(conn net.Conn) error {
defer conn.Close()
tlscon, ok := conn.(*tls.Conn)
if !ok {
return errs.With("Connection is not tls")
}
err := tlscon.Handshake()
if err != nil {
return errs.WithE(err, "TLS handshare failed")
}
state := tlscon.ConnectionState()
for _, v := range state.PeerCertificates {
key, err := x509.MarshalPKIXPublicKey(v.PublicKey)
if err != nil {
return errs.WithE(err, "failed to marshal public key")
}
logs.WithF(data.WithField("key", key)).Debug("Client public key")
}
creds, err := getConnectionCredentials(conn)
if err != nil {
return errs.WithE(err, "Failed to read client credentials")
}
if creds.Uid != s.userUid {
return errs.WithEF(err, data.WithField("uid", creds.Uid), "Unauthorized access")
}
for {
if err := conn.SetDeadline(time.Now().Add(s.Timeout)); err != nil {
return errs.WithE(err, "Failed to set deadline on socket connection")
}
command, err := readCommand(conn)
if err != nil {
if err == io.EOF {
return nil
}
return errs.WithE(err, "Failed to read command on socket")
}
commandFunc, ok := s.commands[command]
if !ok {
return errs.WithEF(err, data.WithField("command", command), "Unknown command on socket")
}
if err := commandFunc(conn); err != nil {
return errs.WithE(err, "Client command failed")
}
}
}
func readCommand(conn net.Conn) (string, error) {
command := ""
buffer := make([]byte, 1)
for {
n, err := conn.Read(buffer)
if err != nil {
return "", err
}
if n == 0 {
return "", err
}
if string(buffer) == " " || string(buffer) == "\n" {
return command, nil
}
command += string(buffer)
}
}
func WriteBytes(conn io.Writer, bytes []byte) error {
var total, written int
var err error
for total = 0; total < len(bytes); total += written {
written, err = conn.Write(bytes[total:])
if err != nil {
return err
}
}
return nil
}
func unixConnFromTLSConn(conn tls.Conn) (*net.UnixConn, error) {
value := reflect.ValueOf(conn)
field := value.FieldByName("conn")
// Fails with "reflect.Value.UnsafeAddr of unaddressable value" because rs isn't addressable:
// field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
valueCopy := reflect.New(value.Type()).Elem()
valueCopy.Set(value)
field = valueCopy.FieldByName("conn")
field = reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
i := field.Interface()
uc, ok := i.(*net.UnixConn)
if !ok {
return nil, errs.With("Failed to get unix connection from tls connection")
}
return uc, nil
}
func getConnectionCredentials(c net.Conn) (*unix.Ucred, error) {
var cred *unix.Ucred
tlscon, ok := c.(*tls.Conn)
if !ok {
return cred, errs.With("Connection is not tls")
}
uc, err := unixConnFromTLSConn(*tlscon)
if err != nil {
return nil, err
}
raw, err := uc.SyscallConn()
if err != nil {
return nil, errs.WithE(err, "Failed to open raw connection")
}
err2 := raw.Control(func(fd uintptr) {
cred, err = unix.GetsockoptUcred(int(fd),
unix.SOL_SOCKET,
unix.SO_PEERCRED)
})
if err != nil {
return nil, errs.WithE(err, "Failed to user credentials from socket control")
}
if err2 != nil {
return nil, errs.WithE(err2, "Failed to user credentials from socket control")
}
return cred, nil
}