-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnmp_api.py
More file actions
1210 lines (1020 loc) · 39.1 KB
/
snmp_api.py
File metadata and controls
1210 lines (1020 loc) · 39.1 KB
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
import utime as time
import uhashlib as hashlib
from usr.snmp_type import *
from usr.snmp_utils import *
import usocket as socket
def udp_send(endpoint, packet, timeout, loop=None, retries=10):
while retries:
udp_socket = None
try:
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.settimeout(1)
# 发送数据(数据,(ip,端口))
udp_socket.sendto(packet, (endpoint.ip, endpoint.port))
ur = udp_socket.recvfrom(1024)
except Exception as e:
retries -= 1
else:
return ur[0]
finally:
if udp_socket:
udp_socket.close()
def get_request_id() -> int: # pragma: no cover
return int(time.mktime(time.localtime()))
def validate_response_id(request_id: int, response_id: int) -> None:
"""
Compare request and response IDs and raise an appropriate error.
Raises an appropriate error if the IDs differ. Otherwise returns
This helper method ensures we're always returning the same exception type
on invalid response IDs.
"""
if response_id != request_id:
raise InvalidResponseId(
"Invalid response ID {} for request id {}".format(response_id, request_id)
)
class ClientConfig(object):
def __init__(self, credentials, context, lcd, timeout=DEFAULT_TIMEOUT, retries=DEFAULT_RETRIES):
self.credentials = credentials
self.context = context
self.lcd = lcd
self.timeout = timeout
self.retries = retries
class Credentials(object):
community: str
def __init__(self, mpm: int):
self.mpm = mpm
def __repr__(self) -> str:
return "<{}.{}>".format(__name__, self.__class__.__name__)
class V1(Credentials):
def __init__(self, community):
super().__init__(0)
self.community = community
def __eq__(self, other):
return isinstance(other, V1) and other.community == self.community
class V2C(V1):
def __init__(self, community: str) -> None:
super().__init__(community)
self.mpm = 1
class V3(Credentials):
def __init__(self, username: str, auth=None, priv=None):
super().__init__(3)
self.username = username
self.auth = auth
self.priv = priv
def __eq__(self, other: object) -> bool:
return (
isinstance(other, V3)
and other.username == self.username
and other.auth == self.auth
and other.priv == self.priv
)
class Context(object):
def __init__(self, engine_id, name):
self.engine_id = engine_id
self.name = name
class SNMPv1SecurityModel(object):
def __init__(self, local_config):
self.local_config = {}
def generate_request_message(self, message, security_engine_id, credentials):
if not isinstance(credentials, V1):
raise SnmpError(
"Credentials for the SNMPv1 security model must be "
"V1 instances!"
)
packet = Sequence(
[Integer(0), OctetString(credentials.community), message]
)
return packet
class V2CEncodingResult(object):
def __init__(self, data, security_model=None):
self.data = data
self.security_model = security_model
class SNMPv2cSecurityModel(object):
"""
Implementation of the security model for community based SNMPv2 messages
"""
def generate_request_message(self, message, security_engine_id, credentials):
if not isinstance(credentials, V2C):
raise SnmpError(
"Credentials for the SNMPv2c security model must be "
"V2C instances!"
)
packet = Sequence(
[Integer(1), OctetString(credentials.community), message]
)
return packet
def process_incoming_message(self, message, credentials):
proto_version, community, pdu = message
if not isinstance(credentials, V2C):
raise SnmpError(
"Credentials for the SNMPv2c security model must be "
"V2C instances!"
)
if proto_version.pythonize() != 1:
raise SnmpError(
"Incoming SNMP message is not supported by the SNMPv2c "
"security model. Most likely the device is not talking "
"SNMPv2c but rather a different SNMP version."
)
if community.pythonize() != credentials.community.encode("ascii"):
raise SnmpError("Mismatching community in response mesasge!")
return pdu # type: ignore
class V1MPM(object):
def encode(self, request_id, credentials, engine_id, context_name, pdu):
pass
class V2CMPM(object):
def __init__(self, transport_handler, lcd):
self.transport_handler = transport_handler
self.lcd = lcd
self.disco = None
self.security_model = None
def encode(self, request_id, credentials, engine_id, context_name, pdu):
security_model_id = 2
if self.security_model is None:
self.security_model = SNMPv2cSecurityModel()
packet = self.security_model.generate_request_message(pdu, b"", credentials)
f = V2CEncodingResult(packet.to_bytes())
return f
def decode(self, whole_msg, credentials):
"""
Convert an SNMPv2c message into a PDU instance
"""
security_model_id = 2
if self.security_model is None:
self.security_model = SNMPv2cSecurityModel()
decoded, _ = decode(whole_msg, enforce_type=Sequence)
msg = self.security_model.process_incoming_message(decoded, credentials)
return msg
def is_confirmed(pdu: PDU) -> bool:
"""
Return True if the given PDU instance expects a response.
"""
# XXX TODO This might be doable cleaner with subclassing in puresnmp.pdu
return isinstance(pdu, GetRequest)
def localise_key(credentials: V3, engine_id: bytes) -> bytes:
if credentials.priv is None:
raise SnmpError(
"Attempting to derive a localised key from an empty "
"privacy object!"
)
if credentials.auth is None:
raise SnmpError(
"Attempting to derive a localised key from an empty " "auth object!"
)
if credentials.auth.method == "md5":
hasher = password_to_key(hashlib.md5, 16)
elif credentials.auth.method == "sha1":
hasher = password_to_key(hashlib.sha1, 20)
else:
raise SnmpError(
"Unknown authentication method: %r" % credentials.auth.method
)
output = hasher(credentials.priv.key, engine_id)
return output
class ScopedPDU(object):
def __init__(self, context_engine_id, context_name, data):
self.context_engine_id = context_engine_id
self.context_name = context_name
self.data = data
def to_bytes(self):
return self.as_snmp_type().to_bytes()
@staticmethod
def decode(data: bytes, slc=None) -> "ScopedPDU":
sequence, _ = decode(
data,
start_index=get_slice_start(slc),
enforce_type=Sequence,
strict=False,
)
engine_id = sequence[0]
cname = sequence[1]
pdu = sequence[2]
output = ScopedPDU(
context_engine_id=engine_id,
context_name=cname,
data=pdu,
)
return output
def as_snmp_type(self):
"""
Convert this message into an x.690 Sequence
"""
return Sequence(
[
self.context_engine_id,
self.context_name,
self.data,
]
)
class USMSecurityParameters(object):
def __init__(self, authoritative_engine_id, authoritative_engine_boots, authoritative_engine_time, user_name,
auth_params, priv_params):
self.authoritative_engine_id: bytes = authoritative_engine_id
self.authoritative_engine_boots: int = authoritative_engine_boots
self.authoritative_engine_time: int = authoritative_engine_time
self.user_name: bytes = user_name
self.auth_params: bytes = auth_params
self.priv_params: bytes = priv_params
@staticmethod
def decode(data: bytes) -> "USMSecurityParameters":
seq, _ = decode(data, enforce_type=Sequence)
return USMSecurityParameters.from_snmp_type(seq)
@staticmethod
def from_snmp_type(seq: Sequence) -> "USMSecurityParameters":
return USMSecurityParameters(
authoritative_engine_id=seq[0].pythonize(),
authoritative_engine_boots=seq[1].pythonize(),
authoritative_engine_time=seq[2].pythonize(),
user_name=seq[3].pythonize(),
auth_params=seq[4].pythonize(),
priv_params=seq[5].pythonize(),
)
def to_bytes(self) -> bytes:
return self.as_snmp_type().to_bytes()
def as_snmp_type(self) -> Sequence:
return Sequence(
[
OctetString(self.authoritative_engine_id),
Integer(self.authoritative_engine_boots),
Integer(self.authoritative_engine_time),
OctetString(self.user_name),
OctetString(self.auth_params),
OctetString(self.priv_params),
]
)
class Message(object):
def __init__(self, version, header, security_parameters, scoped_pdu):
#: The IANA version identifier
self.version = version
#: Additional information wrapping the old-style PDU
self.header = header
#: Additional data needed to authenticate & en/decrypt the message
self.security_parameters = security_parameters
#: The "old-style" PDU (either plain or encrypted)
self.scoped_pdu = scoped_pdu
def to_bytes(self) -> bytes:
if isinstance(self.scoped_pdu, ScopedPDU):
spdu = self.scoped_pdu.as_snmp_type()
else:
spdu = self.scoped_pdu
output = Sequence(
[
self.version,
self.header.as_snmp_type(),
OctetString(self.security_parameters),
spdu,
]
)
return output.to_bytes()
@classmethod
def from_sequence(cls, seq: Sequence):
version = seq[0]
header = seq[1]
security_parameters = seq[2].value
msg_id = header[0]
msg_max_size = header[1]
security_level = V3Flags.decode(header[2])
security_model_id = header[3].pythonize()
if security_level.priv:
payload = seq[3]
else:
scoped_pdu = seq[3]
engine_id = scoped_pdu[0]
context_name = scoped_pdu[1]
pdu = scoped_pdu[2]
payload = ScopedPDU(engine_id, context_name, pdu)
output = cls(
version,
HeaderData(
msg_id.pythonize(),
msg_max_size.pythonize(),
security_level,
security_model_id,
),
security_parameters,
payload,
)
return output
@staticmethod
def decode(data: bytes):
message, _ = decode(data, enforce_type=Sequence)
cls = (
EncryptedMessage
if isinstance(message[3], OctetString)
else PlainMessage
)
return cls.from_sequence(message) # type: ignore
class PlainMessage(Message):
pass
def apply_encryption(
message: PlainMessage,
credentials: V3,
security_name: bytes,
security_engine_id: bytes,
engine_boots: int,
engine_time: int,
):
if credentials.priv is not None and not credentials.priv.method:
raise UnsupportedSecurityLevel("Encryption method is missing")
if credentials.priv is None:
message.security_parameters = USMSecurityParameters(
security_engine_id,
engine_boots,
engine_time,
security_name,
b"",
b"",
).to_bytes()
return message
_m = credentials.priv.method
from usr.snmp_priv import AES_PRIV, DES_PRIV
if _m == AES_PRIV.IDENTIFIER:
priv_method = AES_PRIV()
else:
priv_method = DES_PRIV()
localised_key = localise_key(credentials, security_engine_id)
try:
encrypted, salt = priv_method.encrypt_data(
localised_key,
security_engine_id,
engine_boots,
engine_time,
message.scoped_pdu.to_bytes(),
)
scoped_pdu = OctetString(encrypted)
except Exception as exc:
raise EncryptionError("Unable to encrypt message ({})".format(exc))
message.scoped_pdu = scoped_pdu
message.security_parameters = USMSecurityParameters(
security_engine_id,
engine_boots,
engine_time,
security_name,
b"",
salt,
).to_bytes()
return message
class V3Flags(object):
def __init__(self, auth=False, priv=False, reportable=False):
self.auth = auth
self.priv = priv
self.reportable = reportable
@staticmethod
def decode(blob: OctetString) -> "V3Flags":
flags = int.from_bytes(blob.value, "big")
reportable = bool(flags & 0b100)
priv = bool(flags & 0b010)
auth = bool(flags & 0b001)
return V3Flags(auth, priv, reportable)
def to_bytes(self) -> bytes:
value = 0
value |= int(self.reportable) << 2
value |= int(self.priv) << 1
value |= int(self.auth)
return bytes([value])
def reset_digest(message: Message) -> Message:
secparams = USMSecurityParameters.decode(message.security_parameters)
secparams.auth_params = b"\x00" * 12
neutral = secparams
message.security_parameters = neutral.to_bytes()
output = message
return output
def apply_authentication(
unauthed_message,
credentials: V3,
security_engine_id: bytes,
):
if credentials.auth is not None and not credentials.auth.method:
raise UnsupportedSecurityLevel(
"Incomplete data for authentication. "
"Need both an auth-key and an auth-method!"
)
if credentials.auth is None:
return unauthed_message
from usr.snmp_auth import MD5_AUTH, SHA1_AUTH
_m = credentials.auth.method
if _m == MD5_AUTH.IDENTIFIER:
auth_method = MD5_AUTH()
else:
auth_method = SHA1_AUTH()
try:
without_digest = reset_digest(unauthed_message)
auth_result = auth_method.authenticate_outgoing_message(
credentials.auth.key,
without_digest.to_bytes(),
security_engine_id,
)
security_params = USMSecurityParameters.decode(unauthed_message.security_parameters)
security_params.auth_params = auth_result
authed_message = unauthed_message
authed_message.security_parameters = security_params.to_bytes()
return authed_message
except Exception as exc:
raise AuthenticationError(
"Unable to authenticat the message ({})".format(exc)
)
def verify_authentication(
message: Message, credentials: V3, security_params: USMSecurityParameters
) -> None:
if not message.header.flags.auth:
return
if not credentials.auth:
raise UnsupportedSecurityLevel(
"Message requires authentication but auth-method is missing!"
)
from usr.snmp_auth import MD5_AUTH, SHA1_AUTH
_m = credentials.auth.method
if _m == MD5_AUTH.IDENTIFIER:
auth_method = MD5_AUTH()
else:
auth_method = SHA1_AUTH()
without_digest = reset_digest(message)
is_authentic = auth_method.authenticate_incoming_message(
credentials.auth.key,
without_digest.to_bytes(),
security_params.auth_params,
security_params.authoritative_engine_id,
)
if not is_authentic:
raise AuthenticationError(
"Incoming message could not be authenticated!"
)
class HeaderData(object):
def __init__(self, message_id, message_max_size, flags, security_model):
self.message_id: int = message_id
self.message_max_size: int = message_max_size
self.flags: V3Flags = flags
self.security_model: int = security_model
def as_snmp_type(self) -> Sequence:
return Sequence(
[
Integer(self.message_id),
Integer(self.message_max_size),
OctetString(self.flags.to_bytes()),
Integer(self.security_model),
]
)
def to_bytes(self) -> bytes:
return self.as_snmp_type().to_bytes()
def validate_usm_message(message: PlainMessage) -> None:
"""
If the message contains known error-indicators, raise an appropriate
exception.
:raises SnmpError: If an error was found
"""
pdu = message.scoped_pdu.data.value
errors = {
ObjectIdentifier(
"1.3.6.1.6.3.15.1.1.1.0"
): "Unsupported security level",
ObjectIdentifier("1.3.6.1.6.3.15.1.1.2.0"): "Not in time window",
ObjectIdentifier("1.3.6.1.6.3.15.1.1.3.0"): "Unknown user-name",
ObjectIdentifier("1.3.6.1.6.3.15.1.1.4.0"): "Unknown engine-id",
ObjectIdentifier("1.3.6.1.6.3.15.1.1.5.0"): "Wrong message digest",
ObjectIdentifier("1.3.6.1.6.3.15.1.1.6.0"): "Unable to decrypt",
}
for varbind in pdu.varbinds:
if varbind.oid in errors:
msg = errors[varbind.oid]
raise SnmpError("Error response from remote device: {}".format(msg))
def decrypt_message(
message, credentials: V3
) -> PlainMessage:
"""
Decrypt a message using the given credentials
"""
if isinstance(message, PlainMessage):
return message
if not credentials.priv:
raise SnmpError("Attempting to decrypt a message without priv object")
_m = credentials.priv.method
from usr.snmp_priv import AES_PRIV, DES_PRIV
if _m == AES_PRIV.IDENTIFIER:
priv_method = AES_PRIV()
else:
priv_method = DES_PRIV()
key = credentials.priv.key
if not isinstance(message.scoped_pdu, OctetString):
raise SnmpError(
"Unexpectedly received unencrypted PDU with a security level "
"requesting encryption!"
)
security_parameters = USMSecurityParameters.decode(
message.security_parameters
)
localised_key = localise_key(
credentials, security_parameters.authoritative_engine_id
)
try:
decrypted = priv_method.decrypt_data(
localised_key,
security_parameters.authoritative_engine_id,
security_parameters.authoritative_engine_boots,
security_parameters.authoritative_engine_time,
security_parameters.priv_params,
message.scoped_pdu.value,
)
message.scoped_pdu = ScopedPDU.decode(decrypted)
except Exception as exc:
raise DecryptionError("Unable to decrypt message ({})".format(exc))
return message
class UserSecurityModel(object):
def __init__(self):
self.local_config = {}
def set_engine_timing(
self,
engine_id: bytes,
engine_boots: int,
engine_time: int,
) -> None:
engine_config = self.local_config.setdefault(engine_id, {})
engine_config["authoritative_engine_boots"] = engine_boots
engine_config["authoritative_engine_time"] = engine_time
def generate_request_message(self, message: PlainMessage, security_engine_id: bytes, credentials: Credentials):
if not isinstance(credentials, V3):
raise TypeError(
"Credentials must be a V3 instance for this scurity model!"
)
security_name = credentials.username.encode("ascii")
engine_config = self.local_config[security_engine_id]
engine_boots = engine_config["authoritative_engine_boots"]
engine_time = engine_config["authoritative_engine_time"]
encrypted_message = apply_encryption(
message,
credentials,
security_name,
security_engine_id,
engine_boots,
engine_time,
)
authed_message = apply_authentication(
encrypted_message, credentials, security_engine_id
)
return authed_message
def process_incoming_message(
self,
message,
credentials: Credentials,
) -> PlainMessage:
if not isinstance(credentials, V3):
raise SnmpError("Supplied credentials is not a V3 instance!")
security_params = USMSecurityParameters.decode(
message.security_parameters
)
security_name = security_params.user_name
if security_name != credentials.username.encode("ascii"):
# See https://tools.ietf.org/html/rfc3414#section-3.1
raise UnknownUser("Unknown user {}".format(security_name))
verify_authentication(message, credentials, security_params)
message = decrypt_message(message, credentials)
validate_usm_message(message)
return message
def send_discovery_message(self, transport_handler):
request_id = get_request_id()
security_params = USMSecurityParameters(
authoritative_engine_id=b"",
authoritative_engine_boots=0,
authoritative_engine_time=0,
user_name=b"",
auth_params=b"",
priv_params=b"",
)
discovery_message = Message(
Integer(3),
HeaderData(
request_id,
MESSAGE_MAX_SIZE,
V3Flags(False, False, True),
3,
),
security_params.to_bytes(),
ScopedPDU(
OctetString(),
OctetString(),
GetRequest(PDUContent(request_id, [])),
),
)
payload = discovery_message.to_bytes()
raw_response = transport_handler(payload)
response, _ = decode(raw_response, enforce_type=Sequence)
if isinstance(response, Null):
raise SnmpError("Unexpectedly got a NULL object")
response_msg = PlainMessage.from_sequence(response)
response_id = response_msg.header.message_id
validate_response_id(request_id, response_id)
security = USMSecurityParameters.decode(
response_msg.security_parameters
)
wrapped_vars = response_msg.scoped_pdu.data.value.varbinds
if not wrapped_vars:
raise SnmpError("Invalid discovery response (no varbinds returned)")
unknown_engine_id_var = wrapped_vars[0]
if not unknown_engine_id_var.value:
raise SnmpError("Discovery data did not contain valid data")
unknown_engine_ids = unknown_engine_id_var.value.pythonize()
out = DiscoData(
authoritative_engine_id=security.authoritative_engine_id,
authoritative_engine_boots=security.authoritative_engine_boots,
authoritative_engine_time=security.authoritative_engine_time,
unknown_engine_ids=unknown_engine_ids,
)
return out
class V3EncodingResult(object):
def __init__(self, data, security_model=None):
self.data = data
self.security_model = security_model
class V3MPM(object):
def __init__(self, transport_handler, lcd):
self.transport_handler = transport_handler
self.lcd = lcd
self.disco = None
self.security_model = None
def decode(
self,
whole_msg: bytes, # as received from the network
credentials: Credentials,
) -> PDU:
security_model_id = 3
if self.security_model is None:
self.security_model = UserSecurityModel()
message = Message.decode(whole_msg)
msg = self.security_model.process_incoming_message(message, credentials)
return msg.scoped_pdu.data
def encode(
self,
request_id: int,
credentials: Credentials,
engine_id: bytes,
context_name: bytes,
pdu: PDU,
) -> V3EncodingResult:
if not isinstance(credentials, V3):
raise TypeError("Credentials for SNMPv3 must be V3 instances!")
security_model_id = 3
if self.security_model is None:
self.security_model = UserSecurityModel()
# We need to determine some values from the remote host for security.
# These can be retrieved by sending a so called discovery message.
if not self.disco:
self.disco = self.security_model.send_discovery_message(
self.transport_handler
)
security_engine_id = self.disco.authoritative_engine_id
if engine_id == b"":
engine_id = security_engine_id
scoped_pdu = ScopedPDU(
OctetString(engine_id), OctetString(context_name), pdu
)
flags = V3Flags(
auth=credentials.auth is not None,
priv=credentials.priv is not None,
reportable=is_confirmed(pdu),
)
header = HeaderData(
request_id,
MESSAGE_MAX_SIZE,
flags,
security_model_id,
)
if self.disco is not None:
self.security_model.set_engine_timing(
self.disco.authoritative_engine_id,
self.disco.authoritative_engine_boots,
self.disco.authoritative_engine_time,
)
snmp_version = 3
msg = PlainMessage(Integer(snmp_version), header, b"", scoped_pdu)
output = self.security_model.generate_request_message(
msg,
security_engine_id,
credentials,
)
outgoing_message = output.to_bytes()
return V3EncodingResult(outgoing_message, self.security_model)
class PDUContent(object):
def __init__(self, request_id, varbinds, error_status=0, error_index=0):
self.request_id = request_id
self.varbinds = varbinds
self.error_status = error_status
self.error_index = error_index
def tablify(
varbinds,
num_base_nodes: int = 0,
base_oid: str = "",
_rowtype=None):
if _rowtype is None:
_rowtype = dict()
if isinstance(base_oid, str) and base_oid:
base_oid_parsed = ObjectIdentifier(base_oid)
# Each table has a sub-index for the table "entry" so the number of
# base-nodes needs to be incremented by 1
num_base_nodes = len(base_oid_parsed)
rows = {}
for oid, value in varbinds:
if num_base_nodes:
tail = oid.nodes[num_base_nodes:]
col_id_nodes, row_id_nodes = tail[0], tail[1:]
col_id = str(col_id_nodes)
row_id = ".".join([str(node) for node in row_id_nodes])
else:
col_id = str(oid.nodes[-2])
row_id = str(oid.nodes[-1])
tmp: TTableRow = { # type: ignore
"0": row_id,
}
row = rows.setdefault(row_id, tmp)
row[str(col_id)] = value
return list(rows.values())
class Client(object):
def __init__(self, ip, credentials, port=161, sender=udp_send, context_name=b"", engine_id=b""):
lcd = dict()
self.config = ClientConfig(
credentials=credentials,
context=Context(engine_id, context_name),
lcd=lcd,
)
self.endpoint = Endpoint(ip, port)
def handler(data):
return sender(self.endpoint, data, timeout=self.config.timeout, retries=self.config.retries)
if credentials.mpm == 1:
self.mpm = V2CMPM(handler, lcd)
else:
self.mpm = V3MPM(handler, lcd)
self.sender = sender
self.initialization()
def initialization(self):
if not X690Type.INIT:
X690Type.register(UnknownType)
X690Type.register(Boolean)
X690Type.register(Null)
X690Type.register(OctetString)
X690Type.register(Sequence)
X690Type.register(Integer)
X690Type.register(ObjectIdentifier)
X690Type.register(ObjectDescriptor)
X690Type.register(External)
X690Type.register(Real)
X690Type.register(Enumerated)
X690Type.register(EmbeddedPdv)
X690Type.register(Utf8String)
X690Type.register(RelativeOid)
X690Type.register(Set)
X690Type.register(NumericString)
X690Type.register(PrintableString)
X690Type.register(T61String)
X690Type.register(VideotexString)
X690Type.register(IA5String)
X690Type.register(UtcTime)
X690Type.register(GeneralizedTime)
X690Type.register(GraphicString)
X690Type.register(VisibleString)
X690Type.register(GeneralString)
X690Type.register(UniversalString)
X690Type.register(CharacterString)
X690Type.register(BmpString)
X690Type.register(EOC)
X690Type.register(BitString)
X690Type.register(Counter64)
X690Type.register(Counter)
X690Type.register(Gauge)
X690Type.register(TimeTicks)
X690Type.register(Opaque)
X690Type.register(NsapAddress)
X690Type.register(PDU)
X690Type.register(NoSuchObject)
X690Type.register(NoSuchInstance)
X690Type.register(EndOfMibView)
X690Type.register(GetRequest)
X690Type.register(GetResponse)
X690Type.register(GetNextRequest)
X690Type.register(SetRequest)
X690Type.register(BulkGetRequest)
X690Type.register(InformRequest)
X690Type.register(Trap)
X690Type.register(Report)
X690Type.register(IpAddress)
X690Type.INIT = True
if not ErrorResponse.INIT:
ErrorResponse.register(TooBig)
ErrorResponse.register(NoSuchOID)
ErrorResponse.register(BadValue)
ErrorResponse.register(ReadOnly)
ErrorResponse.register(GenErr)
ErrorResponse.register(NoAccess)
ErrorResponse.register(WrongType)
ErrorResponse.register(WrongLength)
ErrorResponse.register(WrongEncoding)
ErrorResponse.register(WrongValue)
ErrorResponse.register(NoCreation)
ErrorResponse.register(InconsistentValue)
ErrorResponse.register(ResourceUnavailable)
ErrorResponse.register(CommitFailed)
ErrorResponse.register(UndoFailed)
ErrorResponse.register(AuthorizationError)
ErrorResponse.register(NotWritable)
ErrorResponse.register(InconsistentName)
ErrorResponse.INIT = True
def reload(self):
X690Type.INIT = False
ErrorResponse.INIT = False
self.initialization()
def _send(self, pdu, request_id):
ret = self.mpm.encode(request_id, self.credentials, self.context.engine_id, self.context.name, pdu)
raw_resp = self.sender(self.endpoint, ret.data, timeout=self.config.timeout, retries=self.config.retries)
resp = self.mpm.decode(raw_resp, self.credentials)
validate_response_id(request_id, resp.value.request_id)
return resp
@property
def credentials(self) -> Credentials:
return self.config.credentials
@property
def context(self) -> Context:
return self.config.context
@property
def ip(self):
return self.endpoint.ip
@property
def port(self) -> int:
return self.endpoint.port
def get(self, oid):
oid = ObjectIdentifier(oid)
result = self.multiget([oid])
return result