Commits

elie  committed 3d8dd2a

fix to processIncomingMessage() to take SecurityModel into account
when lookup up SecurityName by CommunityName. This allows mixed SNMPv1/v2c
communication with the same target

  • Participants
  • Parent commits d2041dd

Comments (0)

Files changed (2)

   moved to module scope to become accessible by wrapper routines
   (v1.py/v2c.py). This is used for setting strictly typed default values
   to corresponding SNMP data structures.
+- Fix to rfc2576:processIncomingMessage() to take SecurityModel into account
+  when lookup up SecurityName by CommunityName. This allows mixed SNMPv1/v2c
+  communication with the same target.
 - Fix to v1.PDUAPI.setDefaults() method that used to set wrongly typed
   time-stamp component.
 - Fix to IPv6 address handling to prevent system from crashing whilst

File pysnmp/proto/secmod/rfc2576.py

         wholeMsg = encoder.encode(msg)
         return ( communityName, wholeMsg )
 
+    def _verifySecurityModel(self, snmpEngine, securityName):
+        ( snmpTargetParamsSecurityModel,
+          snmpTargetParamsSecurityName ) = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols(
+          'SNMP-TARGET-MIB',
+          'snmpTargetParamsSecurityModel',
+          'snmpTargetParamsSecurityName'
+          )
+
+        nextMibNode = snmpTargetParamsSecurityName
+
+        while 1:
+            try:
+                nextMibNode = snmpTargetParamsSecurityName.getNextNode(
+                                  nextMibNode.name
+                              )
+            except NoSuchInstanceError:
+                return False
+
+            if nextMibNode.syntax != securityName:
+                continue
+
+            instId = nextMibNode.name[len(snmpTargetParamsSecurityName.name):]
+
+            # make sure given securityName has valid securityModel
+            mibNode = snmpTargetParamsSecurityModel.getNode(
+                snmpTargetParamsSecurityModel.name + instId
+                )
+            if mibNode.syntax == self.securityModelID:
+                return True
+ 
     def processIncomingMsg(
         self,
         snmpEngine,
                             break
                     else:
                         continue
-            break
-        
+
+            securityName = snmpCommunitySecurityName.getNode(
+                snmpCommunitySecurityName.name + instId
+            )
+            if self._verifySecurityModel(snmpEngine, securityName.syntax):
+                break
+
         communityName = snmpCommunityName.getNode(
             snmpCommunityName.name + instId
             )
-        securityName = snmpCommunitySecurityName.getNode(
-            snmpCommunitySecurityName.name + instId
-            )
         contextEngineId = snmpCommunityContextEngineId.getNode(
             snmpCommunityContextEngineId.name + instId
             )
             )
         snmpEngineID, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineID')
 
-        debug.logger & debug.flagSM and debug.logger('processIncomingMsg: looked up securityName %r contextEngineId %r contextName %r by communityName %r' % (securityName.syntax, contextEngineId.syntax, contextName.syntax, communityName.syntax))
+        debug.logger & debug.flagSM and debug.logger('processIncomingMsg: looked up securityName %r securityModel %r contextEngineId %r contextName %r by communityName %r' % (securityName.syntax, self.securityModelID, contextEngineId.syntax, contextName.syntax, communityName.syntax))
 
         stateReference = self._cache.push(
             communityName=communityName.syntax