/
conn.go
228 lines (196 loc) · 4.35 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
// Copyright (c) 2019,2021 AT&T Intellectual Property.
// All rights reserved.
//
// Copyright (c) 2015-2017 by Brocade Communications Systems, Inc.
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only
package ifmgrd
import (
"encoding/json"
"fmt"
"io"
"net"
"os"
"reflect"
"strconv"
"sync"
"syscall"
client "github.com/danos/configd/client"
"github.com/danos/utils/audit"
"github.com/danos/utils/os/group"
)
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
}
func newResponse(result interface{}, err error, id int) *Response {
var resp Response
if err != nil {
resp = Response{Error: err.Error(), Id: id}
} else {
resp = Response{Result: result, Id: id}
}
return &resp
}
// Get User ID for connecting process
func getLoginUid(pid int32) (uint32, error) {
u, e := audit.GetPidLoginuid(pid)
if e != nil {
fmt.Printf("Error getting Login User Id: %s\n", e.Error())
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
}
type SrvConn struct {
*net.UnixConn
cred *syscall.Ucred
srv *Srv
enc *json.Encoder
dec *json.Decoder
sending *sync.Mutex
}
//Send an rpc response with appropriate data or an error
func (conn *SrvConn) sendResponse(resp *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() (*Request, error) {
var req = new(Request)
err := conn.dec.Decode(req)
if err != nil {
return nil, err
}
return req, nil
}
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, _ = getLoginUid(cred.Pid)
return cred, nil
}
// Handle is the main loop for a connection.
// It receives the requests, calls the request method
//and returns the response to the client.
func (conn *SrvConn) Handle() {
var secrets bool
cred, err := conn.getCreds()
if err != nil {
if !IsLoginPidError(err) {
fmt.Fprintln(os.Stderr, err)
}
} else {
groups, err := group.LookupUid(strconv.Itoa(int(cred.Uid)))
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
for _, gr := range groups {
if gr.Name == "secrets" {
secrets = true
}
}
}
}
client, err := client.Dial("unix", conn.srv.Config.ConfigdSocket, "RUNNING")
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
defer client.Close()
disp := &Disp{
client: client,
secrets: secrets,
}
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
}
}
conn.Close()
return
}
func (conn *SrvConn) Call(
disp *Disp,
method string,
args []interface{},
) (interface{}, error) {
m, ok := conn.srv.m[method]
if !ok {
return nil, &MethErr{Name: method}
}
typ := m.Func.Type()
//Number of args are equal?
if len(args) != typ.NumIn()-1 {
return nil, &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, &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
} else {
return rets[0].Interface(), nil
}
}