forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal.go
1302 lines (1141 loc) · 40 KB
/
marshal.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012-2014 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
package gosnmp
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/des"
"encoding/asn1"
"encoding/binary"
"fmt"
"sync/atomic"
"time"
)
//
// Remaining globals and definitions located here.
// See http://www.rane.com/note161.html for a succint description of the SNMP
// protocol.
//
// SnmpVersion 1, 2c and 3 implemented
type SnmpVersion uint8
// SnmpVersion 1, 2c and 3 implemented
const (
Version1 SnmpVersion = 0x0
Version2c SnmpVersion = 0x1
Version3 SnmpVersion = 0x3
)
// SnmpV3MsgFlags contains various message flags to describe Authentication, Privacy, and whether a report PDU must be sent.
type SnmpV3MsgFlags uint8
// Possible values of SnmpV3MsgFlags
const (
NoAuthNoPriv SnmpV3MsgFlags = 0x0 // No authentication, and no privacy
AuthNoPriv SnmpV3MsgFlags = 0x1 // Authentication and no privacy
AuthPriv SnmpV3MsgFlags = 0x3 // Authentication and privacy
Reportable SnmpV3MsgFlags = 0x4 // Report PDU must be sent.
)
// SnmpV3SecurityModel describes the security model used by a SnmpV3 connection
type SnmpV3SecurityModel uint8
// UserSecurityModel is the only SnmpV3SecurityModel currently implemented.
const (
UserSecurityModel SnmpV3SecurityModel = 3
)
// SnmpV3AuthProtocol describes the authentication protocol in use by an authenticated SnmpV3 connection.
type SnmpV3AuthProtocol uint8
// NoAuth, MD5, and SHA are implemented
const (
NoAuth SnmpV3AuthProtocol = 1
MD5 SnmpV3AuthProtocol = 2
SHA SnmpV3AuthProtocol = 3
)
// SnmpV3PrivProtocol is the privacy protocol in use by an private SnmpV3 connection.
type SnmpV3PrivProtocol uint8
// NoPriv, DES implemented, AES planned
const (
NoPriv SnmpV3PrivProtocol = 1
DES SnmpV3PrivProtocol = 2
AES SnmpV3PrivProtocol = 3
)
// SnmpV3SecurityParameters is a generic interface type to contain various implementations of SnmpV3SecurityParameters
type SnmpV3SecurityParameters interface {
Copy() SnmpV3SecurityParameters
}
// UsmSecurityParameters is an implementation of SnmpV3SecurityParameters for the UserSecurityModel
type UsmSecurityParameters struct {
AuthoritativeEngineID string
AuthoritativeEngineBoots uint32
AuthoritativeEngineTime uint32
UserName string
AuthenticationParameters string
PrivacyParameters []byte
AuthenticationProtocol SnmpV3AuthProtocol
PrivacyProtocol SnmpV3PrivProtocol
AuthenticationPassphrase string
PrivacyPassphrase string
localDESSalt uint32
localAESSalt uint64
}
// Copy method for UsmSecurityParameters used to copy a SnmpV3SecurityParameters without knowing it's implementation
func (sp *UsmSecurityParameters) Copy() SnmpV3SecurityParameters {
return &UsmSecurityParameters{AuthoritativeEngineID: sp.AuthoritativeEngineID,
AuthoritativeEngineBoots: sp.AuthoritativeEngineBoots,
AuthoritativeEngineTime: sp.AuthoritativeEngineTime,
UserName: sp.UserName,
AuthenticationParameters: sp.AuthenticationParameters,
PrivacyParameters: sp.PrivacyParameters,
AuthenticationProtocol: sp.AuthenticationProtocol,
PrivacyProtocol: sp.PrivacyProtocol,
AuthenticationPassphrase: sp.AuthenticationPassphrase,
PrivacyPassphrase: sp.PrivacyPassphrase,
localDESSalt: sp.localDESSalt,
localAESSalt: sp.localAESSalt,
}
}
// SnmpPacket struct represents the entire SNMP Message or Sequence at the
// application layer.
type SnmpPacket struct {
Version SnmpVersion
MsgFlags SnmpV3MsgFlags
SecurityModel SnmpV3SecurityModel
SecurityParameters SnmpV3SecurityParameters
ContextEngineID string
ContextName string
Community string
PDUType PDUType
MsgID uint32
RequestID uint32
Error SNMPError
ErrorIndex uint8
NonRepeaters uint8
MaxRepetitions uint8
Variables []SnmpPDU
}
// VarBind struct represents an SNMP Varbind.
type VarBind struct {
Name asn1.ObjectIdentifier
Value asn1.RawValue
}
// PDUType describes which SNMP Protocol Data Unit is being sent.
type PDUType byte
// The currently supported PDUType's
const (
Sequence PDUType = 0x30
GetRequest PDUType = 0xa0
GetNextRequest PDUType = 0xa1
GetResponse PDUType = 0xa2
SetRequest PDUType = 0xa3
Trap PDUType = 0xa4
GetBulkRequest PDUType = 0xa5
InformRequest PDUType = 0xa6
SNMPV2Trap PDUType = 0xa7
Report PDUType = 0xa8
)
const rxBufSize = 65535 // max size of IPv4 & IPv6 packet
// Logger is an interface used for debugging. Both Print and
// Printf have the same interfaces as Package Log in the std library. The
// Logger interface is small to give you flexibility in how you do
// your debugging.
//
// For verbose logging to stdout:
//
// gosnmp_logger = log.New(os.Stdout, "", 0)
type Logger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
}
// send/receive one snmp request
func (x *GoSNMP) sendOneRequest(pdus []SnmpPDU, packetOut *SnmpPacket) (result *SnmpPacket, err error) {
finalDeadline := time.Now().Add(x.Timeout)
allReqIDs := make([]uint32, 0, x.Retries+1)
allMsgIDs := make([]uint32, 0, x.Retries+1)
for retries := 0; ; retries++ {
if retries > 0 {
if x.loggingEnabled {
x.Logger.Printf("Retry number %d. Last error was: %v", retries, err)
}
if time.Now().After(finalDeadline) {
err = fmt.Errorf("Request timeout (after %d retries)", retries-1)
break
}
if retries > x.Retries {
// Report last error
break
}
}
err = nil
reqDeadline := time.Now().Add(x.Timeout / time.Duration(x.Retries+1))
x.Conn.SetDeadline(reqDeadline)
// Request ID is an atomic counter (started at a random value)
reqID := atomic.AddUint32(&(x.requestID), 1) // TODO: fix overflows
allReqIDs = append(allReqIDs, reqID)
var msgID uint32
if x.Version == Version3 {
msgID = atomic.AddUint32(&(x.msgID), 1) // TODO: fix overflows
allMsgIDs = append(allMsgIDs, msgID)
// http://tools.ietf.org/html/rfc2574#section-8.1.1.1
// localDESSalt needs to be incremented on every packet.
if x.MsgFlags&AuthPriv > AuthNoPriv && x.SecurityModel == UserSecurityModel {
baseSecParams, ok := x.SecurityParameters.(*UsmSecurityParameters)
if !ok || baseSecParams == nil {
err = fmt.Errorf("&GoSNMP.SecurityModel indicates the User Security Model, but &GoSNMP.SecurityParameters is not of type &UsmSecurityParameters")
break
}
var newPktLocalAESSalt uint64
var newPktLocalDESSalt uint32
switch baseSecParams.PrivacyProtocol {
case AES:
newPktLocalAESSalt = atomic.AddUint64(&(baseSecParams.localAESSalt), 1)
case DES:
newPktLocalDESSalt = atomic.AddUint32(&(baseSecParams.localDESSalt), 1)
}
if packetOut.Version == Version3 && packetOut.SecurityModel == UserSecurityModel && packetOut.MsgFlags&AuthPriv > AuthNoPriv {
pktSecParams, ok := packetOut.SecurityParameters.(*UsmSecurityParameters)
if !ok || baseSecParams == nil {
err = fmt.Errorf("packetOut.SecurityModel indicates the User Security Model, but packetOut.SecurityParameters is not of type &UsmSecurityParameters")
break
}
switch pktSecParams.PrivacyProtocol {
case AES:
var salt = make([]byte, 8)
binary.BigEndian.PutUint64(salt, newPktLocalAESSalt)
pktSecParams.PrivacyParameters = salt
default:
var salt = make([]byte, 8)
binary.BigEndian.PutUint32(salt, pktSecParams.AuthoritativeEngineBoots)
binary.BigEndian.PutUint32(salt[4:], newPktLocalDESSalt)
pktSecParams.PrivacyParameters = salt
}
}
}
}
var outBuf []byte
outBuf, err = packetOut.marshalMsg(pdus, packetOut.PDUType, msgID, reqID)
if err != nil {
// Don't retry - not going to get any better!
err = fmt.Errorf("marshal: %v", err)
break
}
_, err = x.Conn.Write(outBuf)
if err != nil {
continue
}
for {
// Receive response and try receiving again on any decoding error.
// Let the deadline abort us if we don't receive a valid response.
var resp []byte
resp, err = x.receive()
if err != nil {
// receive error. retrying won't help. abort
break
}
result = new(SnmpPacket)
result.MsgFlags = packetOut.MsgFlags
if packetOut.SecurityParameters != nil {
result.SecurityParameters = packetOut.SecurityParameters.Copy()
}
err = x.unmarshal(resp, result)
if err != nil {
err = fmt.Errorf("Unable to decode packet: %s", err.Error())
continue
}
if result == nil || len(result.Variables) < 1 {
err = fmt.Errorf("Unable to decode packet: nil")
continue
}
validID := false
for _, id := range allReqIDs {
if id == result.RequestID {
validID = true
}
}
if result.RequestID == 0 {
validID = true
}
if !validID {
err = fmt.Errorf("Out of order response")
continue
}
break
}
if err != nil {
continue
}
// Success!
return result, nil
}
// Return last error
return nil, err
}
// generic "sender" that negotiate any version of snmp request
func (x *GoSNMP) send(pdus []SnmpPDU, packetOut *SnmpPacket) (result *SnmpPacket, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("recover: %v", e)
}
}()
if x.Conn == nil {
return nil, fmt.Errorf("&GoSNMP.Conn is missing. Provide a connection or use Connect()")
}
if x.Retries < 0 {
x.Retries = 0
}
// http://tools.ietf.org/html/rfc2574#section-2.2.3
// This code does not check if the last message received was more than 150 seconds ago
// The snmpds that this code was tested on emit an 'out of time window' error with the new
// time and this code will retransmit when that is received.
if packetOut.Version == Version3 {
if packetOut.SecurityModel == UserSecurityModel {
secParams, ok := packetOut.SecurityParameters.(*UsmSecurityParameters)
if !ok || secParams == nil {
return nil, fmt.Errorf("packetOut.SecurityModel indicates the User Security Model, but packetOut.SecurityParameters is not of type &UsmSecurityParameters")
}
if secParams.AuthoritativeEngineID == "" {
// send blank packet to discover authoriative engine ID/boots/time
blankPacket := &SnmpPacket{
Version: Version3,
MsgFlags: Reportable | NoAuthNoPriv,
SecurityModel: UserSecurityModel,
SecurityParameters: &UsmSecurityParameters{},
PDUType: GetRequest,
}
var emptyPdus []SnmpPDU
result, err := x.sendOneRequest(emptyPdus, blankPacket)
if err != nil {
return nil, err
}
// store the authoritative engine parameters
newSecParams, ok := result.SecurityParameters.(*UsmSecurityParameters)
if ok && newSecParams != nil {
secParams.AuthoritativeEngineID = newSecParams.AuthoritativeEngineID
secParams.AuthoritativeEngineBoots = newSecParams.AuthoritativeEngineBoots
secParams.AuthoritativeEngineTime = newSecParams.AuthoritativeEngineTime
// it seems common to use the authoritative engine id as the default
// context engine id when it is not specified
if packetOut.ContextEngineID == "" {
packetOut.ContextEngineID = newSecParams.AuthoritativeEngineID
}
// store for base connection as well
if x.ContextEngineID == "" {
x.ContextEngineID = newSecParams.AuthoritativeEngineID
}
}
}
}
}
// perform request
result, err = x.sendOneRequest(pdus, packetOut)
if err != nil {
return result, err
}
if result.Version == Version3 && result.SecurityModel == UserSecurityModel {
secParams, ok := result.SecurityParameters.(*UsmSecurityParameters)
if !ok || secParams == nil {
return nil, fmt.Errorf("result.SecurityModel indicates the User Security Model, but result.SecurityParameters is not of type &UsmSecurityParameters")
}
if x.Version == Version3 && x.SecurityModel == UserSecurityModel {
connSecParams, ok := x.SecurityParameters.(*UsmSecurityParameters)
if !ok || connSecParams != nil {
connSecParams.AuthoritativeEngineID = secParams.AuthoritativeEngineID
connSecParams.AuthoritativeEngineBoots = secParams.AuthoritativeEngineBoots
connSecParams.AuthoritativeEngineTime = secParams.AuthoritativeEngineTime
}
if x.ContextEngineID == "" {
x.ContextEngineID = secParams.AuthoritativeEngineID
}
}
if len(result.Variables) == 1 && result.Variables[0].Name == ".1.3.6.1.6.3.15.1.1.2.0" {
// out of time window -- but since we just renegotiated the authoritative engine parameters,
// just resubmit the packet with updated parameters
pktSecParams, ok := packetOut.SecurityParameters.(*UsmSecurityParameters)
if !ok || pktSecParams == nil {
return nil, fmt.Errorf("packetOut.SecurityModel indicates the User Security Model, but packetOut.SecurityParameters is not of type &UsmSecurityParameters")
}
pktSecParams.AuthoritativeEngineID = secParams.AuthoritativeEngineID
pktSecParams.AuthoritativeEngineBoots = secParams.AuthoritativeEngineBoots
pktSecParams.AuthoritativeEngineTime = secParams.AuthoritativeEngineTime
if packetOut.ContextEngineID == "" {
packetOut.ContextEngineID = secParams.AuthoritativeEngineID
}
return x.sendOneRequest(pdus, packetOut)
}
}
return result, err
}
// -- Marshalling Logic --------------------------------------------------------
// marshal an SNMP message
func (packet *SnmpPacket) marshalMsg(pdus []SnmpPDU,
pdutype PDUType, msgid uint32, requestid uint32) ([]byte, error) {
var authParamStart uint32
buf := new(bytes.Buffer)
// version
buf.Write([]byte{2, 1, byte(packet.Version)})
if packet.Version != Version3 {
// community
buf.Write([]byte{4, uint8(len(packet.Community))})
buf.WriteString(packet.Community)
// pdu
pdu, err := packet.marshalPDU(pdus, requestid)
if err != nil {
return nil, err
}
buf.Write(pdu)
} else {
header, err := packet.marshalSnmpV3Header(msgid)
if err != nil {
return nil, err
}
buf.Write([]byte{byte(Sequence), byte(len(header))})
buf.Write(header)
var securityParameters []byte
if packet.SecurityModel == UserSecurityModel {
securityParameters, authParamStart, err = packet.marshalSnmpV3UsmSecurityParameters()
if err != nil {
return nil, err
}
}
buf.Write([]byte{byte(OctetString)})
secParamLen, err := marshalLength(len(securityParameters))
if err != nil {
return nil, err
}
buf.Write(secParamLen)
authParamStart += uint32(buf.Len())
buf.Write(securityParameters)
scopedPdu, err := packet.marshalSnmpV3ScopedPDU(pdus, requestid)
if err != nil {
return nil, err
}
buf.Write(scopedPdu)
}
// build up resulting msg - sequence, length then the tail (buf)
msg := new(bytes.Buffer)
msg.WriteByte(byte(Sequence))
bufLengthBytes, err2 := marshalLength(buf.Len())
if err2 != nil {
return nil, err2
}
msg.Write(bufLengthBytes)
authParamStart += uint32(msg.Len())
buf.WriteTo(msg) // reverse logic - want to do msg.Write(buf)
authenticatedMessage, err := packet.authenticate(msg.Bytes(), authParamStart)
if err != nil {
return nil, err
}
return authenticatedMessage, nil
}
// marshal a snmp version 3 packet header
func (packet *SnmpPacket) marshalSnmpV3Header(msgid uint32) ([]byte, error) {
buf := new(bytes.Buffer)
// msg id
buf.Write([]byte{byte(Integer), 4})
err := binary.Write(buf, binary.BigEndian, msgid)
if err != nil {
return nil, err
}
// maximum response msg size
maxmsgsize := marshalUvarInt(rxBufSize)
buf.Write([]byte{byte(Integer), byte(len(maxmsgsize))})
buf.Write(maxmsgsize)
// msg flags
buf.Write([]byte{byte(OctetString), 1, byte(packet.MsgFlags)})
// msg security model
buf.Write([]byte{byte(Integer), 1, byte(packet.SecurityModel)})
return buf.Bytes(), nil
}
// marshal a snmp version 3 security parameters field for the User Security Model
func (packet *SnmpPacket) marshalSnmpV3UsmSecurityParameters() ([]byte, uint32, error) {
var buf bytes.Buffer
var authParamStart uint32
secParams, ok := packet.SecurityParameters.(*UsmSecurityParameters)
if !ok || secParams == nil {
return nil, 0, fmt.Errorf("packet.SecurityParameters is not of type &UsmSecurityParameters")
}
// msgAuthoritativeEngineID
buf.Write([]byte{byte(OctetString), byte(len(secParams.AuthoritativeEngineID))})
buf.WriteString(secParams.AuthoritativeEngineID)
// msgAuthoritativeEngineBoots
msgAuthoritativeEngineBoots := marshalUvarInt(secParams.AuthoritativeEngineBoots)
buf.Write([]byte{byte(Integer), byte(len(msgAuthoritativeEngineBoots))})
buf.Write(msgAuthoritativeEngineBoots)
// msgAuthoritativeEngineTime
msgAuthoritativeEngineTime := marshalUvarInt(secParams.AuthoritativeEngineTime)
buf.Write([]byte{byte(Integer), byte(len(msgAuthoritativeEngineTime))})
buf.Write(msgAuthoritativeEngineTime)
// msgUserName
buf.Write([]byte{byte(OctetString), byte(len(secParams.UserName))})
buf.WriteString(secParams.UserName)
authParamStart = uint32(buf.Len() + 2) // +2 indicates PDUType + Length
// msgAuthenticationParameters
if packet.MsgFlags&AuthNoPriv > 0 {
buf.Write([]byte{byte(OctetString), 12,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0})
} else {
buf.Write([]byte{byte(OctetString), 0})
}
// msgPrivacyParameters
if packet.MsgFlags&AuthPriv > AuthNoPriv {
privlen, err := marshalLength(len(secParams.PrivacyParameters))
if err != nil {
return nil, 0, err
}
buf.Write([]byte{byte(OctetString)})
buf.Write(privlen)
buf.Write(secParams.PrivacyParameters)
} else {
buf.Write([]byte{byte(OctetString), 0})
}
// wrap security parameters in a sequence
paramLen, err := marshalLength(buf.Len())
if err != nil {
return nil, 0, err
}
tmpseq := append([]byte{byte(Sequence)}, paramLen...)
authParamStart += uint32(len(tmpseq))
tmpseq = append(tmpseq, buf.Bytes()...)
return tmpseq, authParamStart, nil
}
// marshal and encrypt (if necessary) a snmp version 3 Scoped PDU
func (packet *SnmpPacket) marshalSnmpV3ScopedPDU(pdus []SnmpPDU, requestid uint32) ([]byte, error) {
var b []byte
scopedPdu, err := packet.prepareSnmpV3ScopedPDU(pdus, requestid)
if err != nil {
return nil, err
}
pduLen, err := marshalLength(len(scopedPdu))
if err != nil {
return nil, err
}
b = append([]byte{byte(Sequence)}, pduLen...)
scopedPdu = append(b, scopedPdu...)
if packet.MsgFlags&AuthPriv > AuthNoPriv && packet.SecurityModel == UserSecurityModel {
secParams, ok := packet.SecurityParameters.(*UsmSecurityParameters)
if !ok || secParams == nil {
return nil, fmt.Errorf("packet.SecurityModel indicates the User Security Model, but packet.SecurityParameters is not of type &UsmSecurityParameters")
}
var privkey = genlocalkey(secParams.AuthenticationProtocol,
secParams.PrivacyPassphrase,
secParams.AuthoritativeEngineID)
switch secParams.PrivacyProtocol {
case AES:
var iv [16]byte
binary.BigEndian.PutUint32(iv[:], secParams.AuthoritativeEngineBoots)
binary.BigEndian.PutUint32(iv[4:], secParams.AuthoritativeEngineTime)
copy(iv[8:], secParams.PrivacyParameters)
block, err := aes.NewCipher(privkey[:16])
if err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv[:])
ciphertext := make([]byte, len(scopedPdu))
stream.XORKeyStream(ciphertext, scopedPdu)
pduLen, err := marshalLength(len(ciphertext))
if err != nil {
return nil, err
}
b = append([]byte{byte(OctetString)}, pduLen...)
scopedPdu = append(b, ciphertext...)
default:
preiv := privkey[8:]
var iv [8]byte
for i := 0; i < len(iv); i++ {
iv[i] = preiv[i] ^ secParams.PrivacyParameters[i]
}
block, err := des.NewCipher(privkey[:8])
if err != nil {
return nil, err
}
mode := cipher.NewCBCEncrypter(block, iv[:])
pad := make([]byte, des.BlockSize-len(scopedPdu)%des.BlockSize)
scopedPdu = append(scopedPdu, pad...)
ciphertext := make([]byte, len(scopedPdu))
mode.CryptBlocks(ciphertext, scopedPdu)
pduLen, err := marshalLength(len(ciphertext))
if err != nil {
return nil, err
}
b = append([]byte{byte(OctetString)}, pduLen...)
scopedPdu = append(b, ciphertext...)
}
}
return scopedPdu, nil
}
// prepare the plain text of a snmp version 3 Scoped PDU
func (packet *SnmpPacket) prepareSnmpV3ScopedPDU(pdus []SnmpPDU, requestid uint32) ([]byte, error) {
var buf bytes.Buffer
//ContextEngineID
idlen, err := marshalLength(len(packet.ContextEngineID))
if err != nil {
return nil, err
}
buf.Write(append([]byte{byte(OctetString)}, idlen...))
buf.WriteString(packet.ContextEngineID)
//ContextName
namelen, err := marshalLength(len(packet.ContextName))
if err != nil {
return nil, err
}
buf.Write(append([]byte{byte(OctetString)}, namelen...))
buf.WriteString(packet.ContextName)
data, err := packet.marshalPDU(pdus, requestid)
if err != nil {
return nil, err
}
buf.Write(data)
return buf.Bytes(), nil
}
// marshal a PDU
func (packet *SnmpPacket) marshalPDU(pdus []SnmpPDU, requestid uint32) ([]byte, error) {
buf := new(bytes.Buffer)
// requestid
buf.Write([]byte{2, 4})
err := binary.Write(buf, binary.BigEndian, requestid)
if err != nil {
return nil, err
}
if packet.PDUType == GetBulkRequest {
// non repeaters
buf.Write([]byte{2, 1, packet.NonRepeaters})
// max repetitions
buf.Write([]byte{2, 1, packet.MaxRepetitions})
} else { // get and getnext have same packet format
// error
buf.Write([]byte{2, 1, 0})
// error index
buf.Write([]byte{2, 1, 0})
}
// varbind list
vbl, err := packet.marshalVBL(pdus)
if err != nil {
return nil, err
}
buf.Write(vbl)
// build up resulting pdu - request type, length, then the tail (buf)
pdu := new(bytes.Buffer)
pdu.WriteByte(byte(packet.PDUType))
bufLengthBytes, err2 := marshalLength(buf.Len())
if err2 != nil {
return nil, err2
}
pdu.Write(bufLengthBytes)
buf.WriteTo(pdu) // reverse logic - want to do pdu.Write(buf)
return pdu.Bytes(), nil
}
// marshal a varbind list
func (packet *SnmpPacket) marshalVBL(pdus []SnmpPDU) ([]byte, error) {
vblBuf := new(bytes.Buffer)
for _, pdu := range pdus {
vb, err := marshalVarbind(&pdu)
if err != nil {
return nil, err
}
vblBuf.Write(vb)
}
vblBytes := vblBuf.Bytes()
vblLengthBytes, err := marshalLength(len(vblBytes))
if err != nil {
return nil, err
}
// FIX does bytes.Buffer give better performance than byte slices?
result := []byte{byte(Sequence)}
result = append(result, vblLengthBytes...)
result = append(result, vblBytes...)
return result, nil
}
// marshal a varbind
func marshalVarbind(pdu *SnmpPDU) ([]byte, error) {
oid, err := marshalOID(pdu.Name)
if err != nil {
return nil, err
}
pduBuf := new(bytes.Buffer)
tmpBuf := new(bytes.Buffer)
// Marshal the PDU type into the appropriate BER
switch pdu.Type {
case Null:
pduBuf.Write([]byte{byte(Sequence), byte(len(oid) + 4)})
pduBuf.Write([]byte{byte(ObjectIdentifier), byte(len(oid))})
pduBuf.Write(oid)
pduBuf.Write([]byte{Null, 0x00})
case Integer:
// TODO tests currently only cover positive integers
// Oid
tmpBuf.Write([]byte{byte(ObjectIdentifier), byte(len(oid))})
tmpBuf.Write(oid)
// Integer
var intBytes []byte
switch value := pdu.Value.(type) {
case byte:
intBytes = []byte{byte(pdu.Value.(int))}
case int:
intBytes = marshalInt16(value)
default:
return nil, fmt.Errorf("Unable to marshal PDU Integer; not byte or int.")
}
tmpBuf.Write([]byte{byte(Integer), byte(len(intBytes))})
tmpBuf.Write(intBytes)
// Sequence, length of oid + integer, then oid/integer data
pduBuf.WriteByte(byte(Sequence))
pduBuf.WriteByte(byte(len(oid) + len(intBytes) + 4))
pduBuf.Write(tmpBuf.Bytes())
case OctetString:
//Oid
tmpBuf.Write([]byte{byte(ObjectIdentifier), byte(len(oid))})
tmpBuf.Write(oid)
//OctetString
var octetStringBytes []byte
switch value := pdu.Value.(type) {
case []byte:
octetStringBytes = value
case string:
octetStringBytes = []byte(value)
default:
return nil, fmt.Errorf("Unable to marshal PDU OctetString; not []byte or String.")
}
tmpBuf.Write([]byte{byte(OctetString), byte(len(octetStringBytes))})
tmpBuf.Write(octetStringBytes)
// Sequence, length of oid + octetstring, then oid/octetstring data
pduBuf.WriteByte(byte(Sequence))
pduBuf.WriteByte(byte(len(oid) + len(octetStringBytes) + 4))
pduBuf.Write(tmpBuf.Bytes())
default:
return nil, fmt.Errorf("Unable to marshal PDU: unknown BER type %d", pdu.Type)
}
return pduBuf.Bytes(), nil
}
// -- Unmarshalling Logic ------------------------------------------------------
func (x *GoSNMP) unmarshal(packet []byte, response *SnmpPacket) error {
if response == nil {
return fmt.Errorf("Cannot unmarshal response into nil packet reference")
}
var OrigMsgFlags = response.MsgFlags
var OrigAuthEngineID string
secParameters, ok := response.SecurityParameters.(*UsmSecurityParameters)
if ok && secParameters != nil {
OrigAuthEngineID = secParameters.AuthoritativeEngineID
}
response.Variables = make([]SnmpPDU, 0, 5)
// Start parsing the packet
cursor := 0
// First bytes should be 0x30
if PDUType(packet[0]) != Sequence {
return fmt.Errorf("Invalid packet header\n")
}
length, cursor := parseLength(packet)
if len(packet) != length {
return fmt.Errorf("Error verifying packet sanity: Got %d Expected: %d\n", len(packet), length)
}
if x.loggingEnabled {
x.Logger.Printf("Packet sanity verified, we got all the bytes (%d)", length)
}
// Parse SNMP Version
rawVersion, count, err := x.parseRawField(packet[cursor:], "version")
if err != nil {
return fmt.Errorf("Error parsing SNMP packet version: %s", err.Error())
}
cursor += count
if version, ok := rawVersion.(int); ok {
response.Version = SnmpVersion(version)
if x.loggingEnabled {
x.Logger.Printf("Parsed version %d", version)
}
}
if response.Version != Version3 {
// Parse community
rawCommunity, count, err := x.parseRawField(packet[cursor:], "community")
if err != nil {
return fmt.Errorf("Error parsing community string: %s", err.Error())
}
cursor += count
if community, ok := rawCommunity.(string); ok {
response.Community = community
if x.loggingEnabled {
x.Logger.Printf("Parsed community %s", community)
}
}
} else {
if PDUType(packet[cursor]) != Sequence {
return fmt.Errorf("Invalid SNMPV3 Header\n")
}
_, cursorTmp := parseLength(packet[cursor:])
cursor += cursorTmp
rawMsgID, count, err := x.parseRawField(packet[cursor:], "msgID")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 message ID: %s", err.Error())
}
cursor += count
if MsgID, ok := rawMsgID.(int); ok {
response.MsgID = uint32(MsgID)
if x.loggingEnabled {
x.Logger.Printf("Parsed message ID %d", MsgID)
}
}
// discard msg max size
_, count, err = x.parseRawField(packet[cursor:], "maxMsgSize")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 maxMsgSize: %s", err.Error())
}
cursor += count
// discard msg max size
rawMsgFlags, count, err := x.parseRawField(packet[cursor:], "msgFlags")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 msgFlags: %s", err.Error())
}
cursor += count
if MsgFlags, ok := rawMsgFlags.(string); ok {
response.MsgFlags = SnmpV3MsgFlags(MsgFlags[0])
if x.loggingEnabled {
x.Logger.Printf("parsed msg flags %s", MsgFlags)
}
}
rawSecModel, count, err := x.parseRawField(packet[cursor:], "msgSecurityModel")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 msgSecModel: %s", err.Error())
}
cursor += count
if SecModel, ok := rawSecModel.(int); ok {
response.SecurityModel = SnmpV3SecurityModel(SecModel)
if x.loggingEnabled {
x.Logger.Printf("Parsed security model %d", SecModel)
}
}
if PDUType(packet[cursor]) != OctetString {
return fmt.Errorf("Invalid SNMPV3 Security Parameters\n")
}
_, cursorTmp = parseLength(packet[cursor:])
cursor += cursorTmp
if response.SecurityModel == UserSecurityModel {
secParameters, ok := response.SecurityParameters.(*UsmSecurityParameters)
if !ok || secParameters == nil {
return fmt.Errorf("&GoSNMP.SecurityModel indicates the User Security Model, but &GoSNMP.SecurityParameters is not of type &UsmSecurityParameters")
}
if PDUType(packet[cursor]) != Sequence {
return fmt.Errorf("Error parsing SNMPV3 User Security Model parameters\n")
}
_, cursorTmp = parseLength(packet[cursor:])
cursor += cursorTmp
rawMsgAuthoritativeEngineID, count, err := x.parseRawField(packet[cursor:], "msgAuthoritativeEngineID")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 User Security Model msgAuthoritativeEngineID: %s", err.Error())
}
cursor += count
if AuthoritativeEngineID, ok := rawMsgAuthoritativeEngineID.(string); ok {
secParameters.AuthoritativeEngineID = AuthoritativeEngineID
if x.loggingEnabled {
x.Logger.Printf("Parsed authoritativeEngineID %s", AuthoritativeEngineID)
}
}
rawMsgAuthoritativeEngineBoots, count, err := x.parseRawField(packet[cursor:], "msgAuthoritativeEngineBoots")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 User Security Model msgAuthoritativeEngineBoots: %s", err.Error())
}
cursor += count
if AuthoritativeEngineBoots, ok := rawMsgAuthoritativeEngineBoots.(int); ok {
secParameters.AuthoritativeEngineBoots = uint32(AuthoritativeEngineBoots)
if x.loggingEnabled {
x.Logger.Printf("Parsed authoritativeEngineBoots %d", AuthoritativeEngineBoots)
}
}
rawMsgAuthoritativeEngineTime, count, err := x.parseRawField(packet[cursor:], "msgAuthoritativeEngineTime")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 User Security Model msgAuthoritativeEngineTime: %s", err.Error())
}
cursor += count
if AuthoritativeEngineTime, ok := rawMsgAuthoritativeEngineTime.(int); ok {
secParameters.AuthoritativeEngineTime = uint32(AuthoritativeEngineTime)
if x.loggingEnabled {
x.Logger.Printf("Parsed authoritativeEngineTime %d", AuthoritativeEngineTime)
}
}
rawMsgUserName, count, err := x.parseRawField(packet[cursor:], "msgUserName")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 User Security Model msgUserName: %s", err.Error())
}
cursor += count
if msgUserName, ok := rawMsgUserName.(string); ok {
secParameters.UserName = msgUserName
if x.loggingEnabled {
x.Logger.Printf("Parsed userName %s", msgUserName)
}
}
rawMsgAuthParameters, count, err := x.parseRawField(packet[cursor:], "msgAuthenticationParameters")
if err != nil {
return fmt.Errorf("Error parsing SNMPV3 User Security Model msgAuthenticationParameters: %s", err.Error())
}