forked from slayercat/GoSNMPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agentcontrol.go
344 lines (300 loc) · 9.89 KB
/
agentcontrol.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package GoSNMPServer
import (
"crypto/rand"
"encoding/binary"
"encoding/hex"
"io"
"log"
"reflect"
"time"
"github.com/gosnmp/gosnmp"
"github.com/pkg/errors"
)
type EnabledVersion uint8
const (
SNMPV1 EnabledVersion = 1 << iota
SNMPV2c
SNMPV3
)
type FuncGetAuthoritativeEngineTime func() uint32
// MasterAgent identifies software which runs on managed devices
//
// One server (port) could ONLY have one MasterAgent
type MasterAgent struct {
SecurityConfig SecurityConfig
SubAgents []*SubAgent
Logger *log.Logger
AllowedVersion EnabledVersion
CreateTime time.Time
priv struct {
communityToSubAgent map[string]*SubAgent
defaultSubAgent *SubAgent
}
}
type SecurityConfig struct {
NoSecurity bool
// AuthoritativeEngineID is SNMPV3 AuthoritativeEngineID
AuthoritativeEngineID SNMPEngineID
// AuthoritativeEngineBoots is SNMPV3 AuthoritativeEngineBoots
AuthoritativeEngineBoots uint32
// OnGetAuthoritativeEngineTime will be called to get SNMPV3 AuthoritativeEngineTime
// if sets to nil, the sys boottime will be used
OnGetAuthoritativeEngineTime FuncGetAuthoritativeEngineTime
Users []gosnmp.UsmSecurityParameters
}
func (v *SecurityConfig) FindForUser(name string) *gosnmp.UsmSecurityParameters {
if v.Users == nil {
return nil
}
for item := range v.Users {
if v.Users[item].UserName == name {
return &v.Users[item]
}
}
return nil
}
type SNMPEngineID struct {
// See https://tools.ietf.org/html/rfc3411#section-5
// SnmpEngineID ::= TEXTUAL-CONVENTION
// SYNTAX OCTET STRING (SIZE(5..32))
PEN uint32
EngineIDData string
}
func (t *SNMPEngineID) Marshal() []byte {
// msgAuthoritativeEngineID: 80004fb8054445534b544f502d4a3732533245343ab63bc8
// 1... .... = Engine ID Conformance: RFC3411 (SNMPv3)
// Engine Enterprise ID: pysnmp (20408) - used if no PEN specified
// Engine ID Format: Octets, administratively assigned (5)
// Engine ID Data: 4445534b544f502d4a3732533245343ab63bc8
engineIdPrefix := make([]byte, 4)
// Use the PEN for the first 4 octets as per RFC3411
pen := t.PEN
// Set the first bit to 1 to indicate RFC3411 method
pen |= 1 << 31
// Convert this uint32 to []byte via the binary package
binary.BigEndian.PutUint32(engineIdPrefix, pen)
// Append the engine ID format info to the slice, in this
// case we will use Octets, administratively assigned (5) per
// RFC 3411
engineIdPrefix = append(engineIdPrefix, 0x05)
tm := make([]byte, hex.EncodedLen(len(engineIdPrefix)))
hex.Encode(tm, engineIdPrefix)
// Append the remaining engine ID data
toAppend := []byte(t.EngineIDData)
maxDefineallowed := 32 - 5
if len(toAppend) > maxDefineallowed { //Max 32 bytes
toAppend = toAppend[:maxDefineallowed]
}
tm = append(tm, toAppend...)
return tm
}
func (t *MasterAgent) syncAndCheck() error {
if len(t.SubAgents) == 0 {
return errors.WithStack(errors.Errorf("MasterAgent shell have at least one SubAgents"))
}
if t.SecurityConfig.NoSecurity && len(t.SubAgents) != 1 {
return errors.WithStack(errors.Errorf("NoSecurity MasterAgent shell have one one SubAgent"))
}
if t.Logger == nil {
//Set New NIL Logger
t.Logger = log.New(io.Discard, "", 0)
}
if t.CreateTime.IsZero() {
t.CreateTime = time.Now()
}
if t.SecurityConfig.OnGetAuthoritativeEngineTime == nil {
t.SecurityConfig.OnGetAuthoritativeEngineTime = func() uint32 {
return uint32(time.Since(t.CreateTime).Seconds())
}
}
if t.SecurityConfig.AuthoritativeEngineID.EngineIDData == "" {
t.SecurityConfig.AuthoritativeEngineID = DefaultAuthoritativeEngineID()
}
return nil
}
func (t *MasterAgent) ReadyForWork() error {
if err := t.syncAndCheck(); err != nil {
return err
}
return t.SyncConfig()
}
func (t *MasterAgent) getUserNameFromRequest(request *gosnmp.SnmpPacket) string {
var username string
if val, ok := request.SecurityParameters.(*gosnmp.UsmSecurityParameters); !ok {
panic(errors.WithMessagef(ErrUnsupportedPacketData, "GoSNMP .Unknown Type:%v", reflect.TypeOf(request.SecurityParameters)))
} else {
username = val.UserName
}
return username
}
func (t *MasterAgent) ResponseForBuffer(i []byte) ([]byte, error) {
// Decode
vhandle := gosnmp.GoSNMP{}
vhandle.Logger = gosnmp.NewLogger(t.Logger)
mb, _ := t.getUsmSecurityParametersFromUser("")
vhandle.SecurityParameters = mb
request, decodeError := vhandle.SnmpDecodePacket(i)
if (request.Version == gosnmp.Version1) && (t.AllowedVersion&SNMPV1 != 0) {
return t.marshalPkt(t.ResponseForPkt(request))
} else if (request.Version == gosnmp.Version2c) && (t.AllowedVersion&SNMPV2c != 0) {
return t.marshalPkt(t.ResponseForPkt(request))
} else if (request.Version == gosnmp.Version3) && (t.AllowedVersion&SNMPV3 != 0) {
// check for initial - discover response / non Privacy Items
if decodeError == nil && len(request.Variables) == 0 {
val, err := t.ResponseForPkt(request)
if val == nil {
return t.marshalPkt(request, err)
} else {
return t.marshalPkt(val, err)
}
}
//v3 might want for Privacy
if request.SecurityParameters == nil {
return nil, errors.WithMessagef(ErrUnsupportedPacketData, "GoSNMP Returns %v", decodeError)
}
username := t.getUserNameFromRequest(request)
usm, err := t.getUsmSecurityParametersFromUser(username)
if err != nil {
return nil, err
}
if decodeError != nil {
t.Logger.Printf("v3 decode [will fail with non password] meet %v\n", err)
vhandle.SecurityParameters = &gosnmp.UsmSecurityParameters{
UserName: usm.UserName,
AuthenticationProtocol: usm.AuthenticationProtocol,
PrivacyProtocol: usm.PrivacyProtocol,
AuthenticationPassphrase: usm.AuthenticationPassphrase,
PrivacyPassphrase: usm.PrivacyPassphrase,
Logger: vhandle.Logger,
}
request, err = vhandle.SnmpDecodePacket(i)
if err != nil {
return nil, errors.WithMessagef(ErrUnsupportedPacketData, "GoSNMP Returns %v", err)
}
}
val, err := t.ResponseForPkt(request)
if val == nil {
request.SecurityParameters = vhandle.SecurityParameters
return t.marshalPkt(request, err)
} else {
securityParamters := usm
GenKeys(securityParamters)
GenSalt(securityParamters)
val.SecurityParameters = securityParamters
return t.marshalPkt(val, err)
}
} else {
return nil, errors.WithStack(ErrUnsupportedProtoVersion)
}
}
func (t *MasterAgent) marshalPkt(pkt *gosnmp.SnmpPacket, err error) ([]byte, error) {
// when err. marshal error pkt
if pkt == nil {
pkt = &gosnmp.SnmpPacket{}
}
if err != nil {
t.Logger.Printf("Will marshal: %v\n", err)
errFill := t.fillErrorPkt(err, pkt)
if errFill != nil {
return nil, err
}
return pkt.MarshalMsg()
}
out, err := pkt.MarshalMsg()
return out, err
}
func (t *MasterAgent) getUsmSecurityParametersFromUser(username string) (*gosnmp.UsmSecurityParameters, error) {
if username == "" {
return &gosnmp.UsmSecurityParameters{
Logger: gosnmp.NewLogger(t.Logger),
AuthoritativeEngineID: string(t.SecurityConfig.AuthoritativeEngineID.Marshal()),
AuthoritativeEngineBoots: t.SecurityConfig.AuthoritativeEngineBoots,
AuthoritativeEngineTime: t.SecurityConfig.OnGetAuthoritativeEngineTime(),
}, nil
}
if val := t.SecurityConfig.FindForUser(username); val != nil {
fval := val.Copy().(*gosnmp.UsmSecurityParameters)
fval.Logger = gosnmp.NewLogger(t.Logger)
fval.AuthoritativeEngineID = string(t.SecurityConfig.AuthoritativeEngineID.Marshal())
fval.AuthoritativeEngineBoots = t.SecurityConfig.AuthoritativeEngineBoots
fval.AuthoritativeEngineTime = t.SecurityConfig.OnGetAuthoritativeEngineTime()
return fval, nil
} else {
return nil, errors.WithStack(ErrNoPermission)
}
}
func (t *MasterAgent) fillErrorPkt(err error, io *gosnmp.SnmpPacket) error {
io.PDUType = gosnmp.GetResponse
if errors.Is(err, ErrNoSNMPInstance) {
io.Error = gosnmp.NoAccess
} else if errors.Is(err, ErrUnsupportedOperation) {
io.Error = gosnmp.ResourceUnavailable
} else if errors.Is(err, ErrNoPermission) {
io.Error = gosnmp.AuthorizationError
} else if errors.Is(err, ErrUnsupportedPacketData) {
io.Error = gosnmp.BadValue
} else {
io.Error = gosnmp.GenErr
}
io.ErrorIndex = 0
return nil
}
func (t *MasterAgent) ResponseForPkt(i *gosnmp.SnmpPacket) (*gosnmp.SnmpPacket, error) {
// Find for which SubAgent
community := getPktContextOrCommunity(i)
subAgent := t.findForSubAgent(community)
if subAgent == nil {
return i, errors.WithStack(ErrNoSNMPInstance)
}
return subAgent.Serve(i)
}
func (t *MasterAgent) SyncConfig() error {
t.priv.defaultSubAgent = nil
t.priv.communityToSubAgent = make(map[string]*SubAgent)
for id, current := range t.SubAgents {
t.SubAgents[id].Logger = t.Logger
t.SubAgents[id].master = t
if err := t.SubAgents[id].SyncConfig(); err != nil {
return err
}
if len(current.CommunityIDs) == 0 || t.SecurityConfig.NoSecurity {
if t.priv.defaultSubAgent != nil {
return errors.Errorf("SyncConfig: Config Error: duplicate default agent")
}
t.priv.defaultSubAgent = current
continue
}
for _, val := range current.CommunityIDs {
if _, exists := t.priv.communityToSubAgent[val]; exists {
return errors.Errorf("SyncConfig: Config Error: duplicate value:%s", val)
}
t.Logger.Printf("communityToSubAgent: val=%v, current=%p\n", val, current)
t.priv.communityToSubAgent[val] = current
}
}
return nil
}
func (t *MasterAgent) findForSubAgent(community string) *SubAgent {
if val, ok := t.priv.communityToSubAgent[community]; ok {
return val
} else {
return t.priv.defaultSubAgent
}
}
// NOTE: Using random data here really is *NOT* the proper way to do this
// as per RFC3411 the same SNMP Engine should always return the same SNMP
// engine ID
func RandomEngineIdData(length int) string {
bytes := make([]byte, length)
if _, err := rand.Read(bytes); err != nil {
// This really shouldn't error
panic(err)
}
return hex.EncodeToString(bytes)
}
func DefaultAuthoritativeEngineID() SNMPEngineID {
return SNMPEngineID{
PEN: 20408,
EngineIDData: RandomEngineIdData(16),
}
}