/
conn.go
281 lines (241 loc) · 6.27 KB
/
conn.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
// Copyright (c) 2018-2021, AT&T Intellectual Property. All rights reserved.
//
// Copyright (c) 2014-2015,2017 by Brocade Communications Systems, Inc.
// All rights reserved.
//
// SPDX-License-Identifier: LGPL-2.1-only
package server
import (
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/user"
"reflect"
"strconv"
"sync"
"syscall"
"github.com/danos/config/auth"
"github.com/danos/config/schema"
"github.com/danos/configd"
"github.com/danos/configd/rpc"
"github.com/danos/mgmterror"
"github.com/danos/utils/audit"
"github.com/danos/utils/os/group"
"github.com/danos/utils/tty"
)
type any interface{}
func newResponse(result any, err error, id int) *rpc.Response {
var resp rpc.Response
if err != nil {
switch val := err.(type) {
case mgmterror.MgmtErrorList:
resp = rpc.Response{MgmtErrList: val, Id: id}
case mgmterror.MgmtErrorRef:
var mel mgmterror.MgmtErrorList
mel.MgmtErrorListAppend(err)
resp = rpc.Response{MgmtErrList: mel, Id: id}
default:
resp = rpc.Response{Error: err.Error(), Id: id}
}
} else {
resp = rpc.Response{Result: result, Id: id}
}
return &resp
}
type SrvConn struct {
*net.UnixConn
srv *Srv
uid uint32
pid int
cred *syscall.Ucred
enc *json.Encoder
dec *json.Decoder
sending *sync.Mutex
}
type LoginPidError struct {
pid int32
}
func (e *LoginPidError) Error() string {
return fmt.Sprintf("Login User Id is not set for PID %d", e.pid)
}
func newLoginPidError(pid int32) error {
return &LoginPidError{pid: pid}
}
func IsLoginPidError(err error) bool {
_, ok := err.(*LoginPidError)
return ok
}
//Send an rpc response with appropriate data or an error
func (conn *SrvConn) sendResponse(resp *rpc.Response) error {
conn.sending.Lock()
err := conn.enc.Encode(&resp)
conn.sending.Unlock()
return err
}
//Receive an rpc request and do some preprocessing.
func (conn *SrvConn) readRequest() (*rpc.Request, error) {
var req = new(rpc.Request)
err := conn.dec.Decode(req)
if err != nil {
return nil, err
}
return req, nil
}
// Get User ID for connecting process
func getLoginUid(pid int32) (uint32, error) {
u, e := audit.GetPidLoginuid(pid)
if e != nil {
return 0, e
}
// The special value of -1 is used when login ID is not set. This
// is the case for daemons and boot processes. Since we're using
// unsigned numbers we take the bitwise complement of 0.
if u == ^uint32(0) {
return 0, newLoginPidError(pid)
}
return u, nil
}
//Grab the credentials off of the unix socet using SO_PEERCRED and store them int the SrvConn
func (conn *SrvConn) getCreds() (*syscall.Ucred, error) {
uf, err := conn.File()
if err != nil {
return nil, err
}
cred, err := syscall.GetsockoptUcred(
int(uf.Fd()),
syscall.SOL_SOCKET,
syscall.SO_PEERCRED)
if err != nil {
conn.srv.LogError(err)
return nil, err
}
uf.Close()
cred.Uid, err = getLoginUid(cred.Pid)
return cred, err
}
// Handle is the main loop for a connection. It receives the requests, authorizes
// the request, calls the request method and returns the response to the client.
func (conn *SrvConn) Handle(compMgr schema.ComponentManager) {
var err error
conn.cred, err = conn.getCreds()
if err != nil {
if !IsLoginPidError(err) {
if !os.IsNotExist(err) {
conn.srv.LogError(err)
}
conn.Close()
return
}
}
disp := &Disp{
smgr: conn.srv.smgr,
cmgr: conn.srv.cmgr,
ms: conn.srv.ms,
msFull: conn.srv.msFull,
ctx: &configd.Context{
Configd: conn.cred.Uid == conn.srv.uid,
Uid: conn.cred.Uid,
Pid: conn.cred.Pid,
Groups: make([]string, 0),
Superuser: conn.cred.Uid == 0,
Config: conn.srv.Config,
Elog: conn.srv.Elog,
Dlog: conn.srv.Dlog,
Wlog: conn.srv.Wlog,
CompMgr: compMgr,
},
}
//Group lookup is expensive, do it once per connection.
//groups are not needed for commit spawned processes
//if the uid is the same as configd auth allows it implicitly
//don't include groups for these users
if conn.cred.Uid != conn.srv.uid {
groups, err := group.LookupUid(strconv.Itoa(int(disp.ctx.Uid)))
conn.srv.LogError(err)
haveSuperGroup := conn.srv.Config.SuperGroup != ""
for _, gr := range groups {
disp.ctx.Groups = append(disp.ctx.Groups, gr.Name)
if haveSuperGroup && gr.Name == conn.srv.Config.SuperGroup {
disp.ctx.Superuser = true
}
}
}
ttyName, err := tty.TtyNameForPid(int(conn.cred.Pid))
if err != nil && !os.IsNotExist(err) {
conn.srv.LogError(err)
}
authEnv := &auth.AuthEnv{Tty: ttyName}
disp.ctx.Auth = auth.NewAuthForUser(conn.srv.authGlobal, disp.ctx.Uid, disp.ctx.Groups, authEnv)
u, err := user.LookupId(strconv.Itoa(int(disp.ctx.Uid)))
if err != nil {
conn.srv.LogError(err)
conn.Close()
return
}
disp.ctx.User = u.Username
disp.ctx.UserHome = u.HomeDir
//Unlock all sessions this connection may have locked on return
defer conn.srv.smgr.UnlockAllPid(disp.ctx)
for {
req, err := conn.readRequest()
if err != nil {
if err != io.EOF {
conn.srv.LogError(err)
}
break
}
result, err := conn.Call(disp, req.Method, req.Args)
err = conn.sendResponse(newResponse(result, err, req.Id))
if err != nil {
break
}
}
if err = disp.sessionTermination(); err != nil {
conn.srv.LogError(err)
}
conn.Close()
return
}
func (conn *SrvConn) Call(
disp *Disp,
method string,
args []interface{},
) (any, error) {
m, ok := conn.srv.m[method]
if !ok {
return nil, &rpc.MethErr{Name: method}
}
if !disp.ctx.Auth.AuthorizeFn(disp.ctx.Uid, disp.ctx.Groups, method) {
return nil, mgmterror.NewAccessDeniedApplicationError()
}
typ := m.Func.Type()
//Number of args are equal?
if len(args) != typ.NumIn()-1 {
return nil, &rpc.ArgNErr{Method: method, Len: len(args), Elen: typ.NumIn() - 1}
}
//validate arguments
//prepending the first argument *Disp
vals := make([]reflect.Value, len(args)+1)
vals[0] = reflect.ValueOf(disp)
for i, v := range args {
t1 := reflect.TypeOf(v)
t2 := typ.In(i + 1)
if t1 != t2 {
if !t1.ConvertibleTo(t2) {
return nil, &rpc.ArgErr{Method: method, Farg: v, Typ: t1.Name(), Etyp: t2.Name()}
}
vals[i+1] = reflect.ValueOf(v).Convert(t2)
} else {
vals[i+1] = reflect.ValueOf(v)
}
}
//call the function
rets := m.Func.Call(vals)
err, ok := rets[1].Interface().(error)
if ok {
return rets[0].Interface(), err
}
return rets[0].Interface(), nil
}