Skip to content

Commit 2fe4448

Browse files
Add logging handler for USM security errors.
Add no-auth and no-priv protocol objects. Add the OIDs for all the auth and priv protocol objects. ZEN-35146
1 parent a3d527c commit 2fe4448

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

Diff for: pynetsnmp/CONSTANTS.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
NULL = 0
12
USM_LENGTH_OID_TRANSFORM = 10
2-
NULL = None
33
MAX_CALLBACK_IDS = 2
44
MAX_CALLBACK_SUBIDS = 16
55
SNMP_CALLBACK_LIBRARY = 0
@@ -306,7 +306,8 @@
306306
NETSNMP_CALLBACK_OP_SEND_FAILED = 3
307307
NETSNMP_CALLBACK_OP_CONNECT = 4
308308
NETSNMP_CALLBACK_OP_DISCONNECT = 5
309-
snmp_init_statistics = ()
309+
NETSNMP_CALLBACK_OP_RESEND = 6
310+
NETSNMP_CALLBACK_OP_SEC_ERROR = 7
310311
STAT_SNMPUNKNOWNSECURITYMODELS = 0
311312
STAT_SNMPINVALIDMSGS = 1
312313
STAT_SNMPUNKNOWNPDUHANDLERS = 2
@@ -377,7 +378,6 @@
377378
MAX_STATS = NETSNMP_STAT_MAX_STATS
378379
COMMUNITY_MAX_LEN = 256
379380
SPRINT_MAX_LEN = 2560
380-
NULL = 0
381381
TRUE = 1
382382
FALSE = 0
383383
READ = 1

Diff for: pynetsnmp/netsnmp.py

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
MAX_OID_LEN,
6060
NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE,
6161
NETSNMP_CALLBACK_OP_TIMED_OUT,
62+
NETSNMP_CALLBACK_OP_SEC_ERROR,
6263
NETSNMP_DS_LIB_APPTYPE,
6364
NETSNMP_DS_LIBRARY_ID,
6465
NETSNMP_LOGHANDLER_CALLBACK,
@@ -658,6 +659,13 @@ def _callback(operation, sp, reqid, pdu, magic):
658659
sess.callback(pdu.contents)
659660
elif operation == NETSNMP_CALLBACK_OP_TIMED_OUT:
660661
sess.timeout(reqid)
662+
elif operation == NETSNMP_CALLBACK_OP_SEC_ERROR:
663+
_getLogger("callback").error(
664+
"peer has rejected security credentials "
665+
"peername=%s security-name=%s",
666+
sp.contents.peername,
667+
sp.contents.securityName,
668+
)
661669
else:
662670
_getLogger("callback").error("Unknown operation: %d", operation)
663671
except Exception as ex:

Diff for: pynetsnmp/twistedsnmp.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,7 @@ def create(
201201
except AttributeError:
202202
ip = address
203203
return cls(
204-
ip,
205-
port=port,
206-
security=security,
207-
timeout=timeout,
208-
tries=retries,
204+
ip, port=port, security=security, timeout=timeout, tries=retries
209205
)
210206

211207
def __init__(
@@ -214,7 +210,7 @@ def __init__(
214210
port=161,
215211
community="public",
216212
snmpVersion="1",
217-
protocol=None,
213+
protocol=None, # no longer used
218214
allowCache=False, # no longer used
219215
timeout=1.5,
220216
tries=3,
@@ -390,15 +386,14 @@ def open(self):
390386
if self.session is not None:
391387
self.session.close()
392388
self.session = None
389+
updateReactor()
393390

394391
if self._security:
395392
agent = asAgent(self.ip, self.port)
396393
cmdlineargs = self._security.getArguments() + (
397394
("-t", str(self.timeout), "-r", str(self.tries), agent)
398395
)
399-
self.session = netsnmp.Session(
400-
cmdLineArgs=cmdlineargs
401-
)
396+
self.session = netsnmp.Session(cmdLineArgs=cmdlineargs)
402397
else:
403398
self.session = netsnmp.Session(
404399
version=netsnmp.SNMP_VERSION_MAP.get(

Diff for: pynetsnmp/usm.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
from __future__ import absolute_import
22

3+
34
class _Protocol(object):
45
""" """
56

6-
__slots__ = ("name",)
7+
__slots__ = ("name", "oid")
78

8-
def __init__(self, name):
9+
def __init__(self, name, oid):
910
self.name = name
11+
self.oid = oid
1012

1113
def __eq__(self, other):
1214
if not isinstance(other, type(self)):
1315
return NotImplemented
14-
return self.name == other.name
16+
return self.name == other.name and self.oid == other.oid
1517

1618
def __str__(self):
1719
return self.name
1820

1921
def __repr__(self):
20-
return "<{0.__module__}.{0.__name__} {1}>".format(
21-
self.__class__, self.name
22+
return "<{0.__module__}.{0.__name__} {1} {2}>".format(
23+
self.__class__, self.name, ".".join(str(v) for v in self.oid)
2224
)
2325

2426

@@ -53,12 +55,13 @@ def __repr__(self):
5355
)
5456

5557

56-
AUTH_MD5 = _Protocol("MD5")
57-
AUTH_SHA = _Protocol("SHA")
58-
AUTH_SHA_224 = _Protocol("SHA-224")
59-
AUTH_SHA_256 = _Protocol("SHA-256")
60-
AUTH_SHA_384 = _Protocol("SHA-384")
61-
AUTH_SHA_512 = _Protocol("SHA-512")
58+
AUTH_NOAUTH = _Protocol("NOAUTH", (1, 3, 6, 1, 6, 3, 10, 1, 1, 1))
59+
AUTH_MD5 = _Protocol("MD5", (1, 3, 6, 1, 6, 3, 10, 1, 1, 2))
60+
AUTH_SHA = _Protocol("SHA", (1, 3, 6, 1, 6, 3, 10, 1, 1, 3))
61+
AUTH_SHA_224 = _Protocol("SHA-224", (1, 3, 6, 1, 6, 3, 10, 1, 1, 4))
62+
AUTH_SHA_256 = _Protocol("SHA-256", (1, 3, 6, 1, 6, 3, 10, 1, 1, 5))
63+
AUTH_SHA_384 = _Protocol("SHA-384", (1, 3, 6, 1, 6, 3, 10, 1, 1, 6))
64+
AUTH_SHA_512 = _Protocol("SHA-512", (1, 3, 6, 1, 6, 3, 10, 1, 1, 7))
6265

6366
auth_protocols = _Protocols(
6467
(
@@ -72,10 +75,11 @@ def __repr__(self):
7275
"authentication",
7376
)
7477

75-
PRIV_DES = _Protocol("DES")
76-
PRIV_AES = _Protocol("AES")
77-
PRIV_AES_192 = _Protocol("AES-192")
78-
PRIV_AES_256 = _Protocol("AES-256")
78+
PRIV_NOPRIV = _Protocol("NOPRIV", (1, 3, 6, 1, 6, 3, 10, 1, 2, 1))
79+
PRIV_DES = _Protocol("DES", (1, 3, 6, 1, 6, 3, 10, 1, 2, 2))
80+
PRIV_AES = _Protocol("AES", (1, 3, 6, 1, 6, 3, 10, 1, 2, 4))
81+
PRIV_AES_192 = _Protocol("AES-192", (1, 3, 6, 1, 4, 1, 14832, 1, 3))
82+
PRIV_AES_256 = _Protocol("AES-256", (1, 3, 6, 1, 4, 1, 14832, 1, 4))
7983

8084
priv_protocols = _Protocols(
8185
(PRIV_DES, PRIV_AES, PRIV_AES_192, PRIV_AES_256), "privacy"
@@ -85,13 +89,15 @@ def __repr__(self):
8589
del _Protocols
8690

8791
__all__ = (
92+
"AUTH_NOAUTH",
8893
"AUTH_MD5",
8994
"AUTH_SHA",
9095
"AUTH_SHA_224",
9196
"AUTH_SHA_256",
9297
"AUTH_SHA_384",
9398
"AUTH_SHA_512",
9499
"auth_protocols",
100+
"PRIV_NOPRIV",
95101
"PRIV_DES",
96102
"PRIV_AES",
97103
"PRIV_AES_192",

0 commit comments

Comments
 (0)