/usr/share/pyshared/pysnmp/proto/mpmod/rfc2576.py is in python-pysnmp4 4.2.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # SNMP v1 & v2c message processing models implementation
import sys
from pyasn1.codec.ber import decoder
from pyasn1.type import univ
from pyasn1.compat.octets import null
from pyasn1.error import PyAsn1Error
from pysnmp.proto.mpmod.base import AbstractMessageProcessingModel
from pysnmp.proto import rfc3411, errind, error
from pysnmp.proto.api import v1, v2c
from pysnmp import debug
# Since I have not found a detailed reference to v1MP/v2cMP
# inner workings, the following has been patterned from v3MP. Most
# references here goes to RFC3412.
class SnmpV1MessageProcessingModel(AbstractMessageProcessingModel):
messageProcessingModelID = univ.Integer(0) # SNMPv1
snmpMsgSpec = v1.Message
# rfc3412: 7.1
def prepareOutgoingMessage(
self,
snmpEngine,
transportDomain,
transportAddress,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
contextEngineId,
contextName,
pduVersion,
pdu,
expectResponse,
sendPduHandle
):
snmpEngineID, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineID')
snmpEngineID = snmpEngineID.syntax
# rfc3412: 7.1.1b
if pdu.tagSet in rfc3411.confirmedClassPDUs:
pdu.setComponentByPosition(1)
msgID = pdu.getComponentByPosition(0)
# rfc3412: 7.1.4
# Since there's no SNMP engine identification in v1/2c,
# set destination contextEngineId to ours
if not contextEngineId:
contextEngineId = snmpEngineID
# rfc3412: 7.1.5
if not contextName:
contextName = null
debug.logger & debug.flagMP and debug.logger('prepareOutgoingMessage: using contextEngineId %r contextName %r' % (contextEngineId, contextName))
# rfc3412: 7.1.6
scopedPDU = ( contextEngineId, contextName, pdu )
msg = self._snmpMsgSpec
msg.setComponentByPosition(0, self.messageProcessingModelID)
msg.setComponentByPosition(2)
msg.getComponentByPosition(2).setComponentByType(
pdu.tagSet, pdu, verifyConstraints=False
)
# rfc3412: 7.1.7
globalData = ( msg, )
k = int(securityModel)
if k in snmpEngine.securityModels:
smHandler = snmpEngine.securityModels[k]
else:
raise error.StatusInformation(
errorIndication = errind.unsupportedSecurityModel
)
# rfc3412: 7.1.9.a & rfc2576: 5.2.1 --> no-op
snmpEngineMaxMessageSize, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineMaxMessageSize')
# rfc3412: 7.1.9.b
( securityParameters,
wholeMsg ) = smHandler.generateRequestMsg(
snmpEngine,
self.messageProcessingModelID,
globalData,
snmpEngineMaxMessageSize.syntax,
securityModel,
snmpEngineID,
securityName,
securityLevel,
scopedPDU
)
# rfc3412: 7.1.9.c
if pdu.tagSet in rfc3411.confirmedClassPDUs:
# XXX rfc bug? why stateReference should be created?
self._cache.pushByMsgId(
int(msgID),
sendPduHandle=sendPduHandle,
msgID=msgID,
snmpEngineID=snmpEngineID,
securityModel=securityModel,
securityName=securityName,
securityLevel=securityLevel,
contextEngineId=contextEngineId,
contextName=contextName,
transportDomain=transportDomain,
transportAddress=transportAddress
)
return ( transportDomain, transportAddress, wholeMsg )
# rfc3412: 7.1
def prepareResponseMessage(
self,
snmpEngine,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
contextEngineId,
contextName,
pduVersion,
pdu,
maxSizeResponseScopedPDU,
stateReference,
statusInformation
):
snmpEngineID, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineID')
snmpEngineID = snmpEngineID.syntax
# rfc3412: 7.1.2.b
if stateReference is None:
raise error.StatusInformation(
errorIndication = errind.nonReportable
)
cachedParams = self._cache.popByStateRef(stateReference)
msgID = cachedParams['msgID']
contextEngineId = cachedParams['contextEngineId']
contextName = cachedParams['contextName']
securityModel = cachedParams['securityModel']
securityName = cachedParams['securityName']
securityLevel = cachedParams['securityLevel']
securityStateReference = cachedParams['securityStateReference']
maxMessageSize = cachedParams['msgMaxSize']
transportDomain = cachedParams['transportDomain']
transportAddress = cachedParams['transportAddress']
debug.logger & debug.flagMP and debug.logger('prepareResponseMessage: cache read msgID %s transportDomain %s transportAddress %s by stateReference %s' % (msgID, transportDomain, transportAddress, stateReference))
# rfc3412: 7.1.3
if statusInformation:
# rfc3412: 7.1.3a (N/A)
# rfc3412: 7.1.3b (always discard)
raise error.StatusInformation(
errorIndication = errind.nonReportable
)
# rfc3412: 7.1.4
# Since there's no SNMP engine identification in v1/2c,
# set destination contextEngineId to ours
if not contextEngineId:
contextEngineId = snmpEngineID
# rfc3412: 7.1.5
if not contextName:
contextName = null
# rfc3412: 7.1.6
scopedPDU = ( contextEngineId, contextName, pdu )
debug.logger & debug.flagMP and debug.logger('prepareResponseMessage: using contextEngineId %r contextName %r' % (contextEngineId, contextName))
msg = self._snmpMsgSpec
msg.setComponentByPosition(0, messageProcessingModel)
msg.setComponentByPosition(2)
msg.getComponentByPosition(2).setComponentByType(
pdu.tagSet, pdu, verifyConstraints=False
)
# att: msgId not set back to PDU as it's up to responder app
# rfc3412: 7.1.7
globalData = ( msg, )
k = int(securityModel)
if k in snmpEngine.securityModels:
smHandler = snmpEngine.securityModels[k]
else:
raise error.StatusInformation(
errorIndication = errind.unsupportedSecurityModel
)
securityEngineId = snmpEngineID
# rfc3412: 7.1.8.a
( securityParameters,
wholeMsg ) = smHandler.generateResponseMsg(
snmpEngine,
self.messageProcessingModelID,
globalData,
maxMessageSize,
securityModel,
snmpEngineID,
securityName,
securityLevel,
scopedPDU,
securityStateReference
)
return ( transportDomain, transportAddress, wholeMsg )
# rfc3412: 7.2.1
def prepareDataElements(
self,
snmpEngine,
transportDomain,
transportAddress,
wholeMsg
):
# rfc3412: 7.2.2
try:
msg, restOfwholeMsg = decoder.decode(
wholeMsg, asn1Spec=self._snmpMsgSpec
)
except PyAsn1Error:
debug.logger & debug.flagMP and debug.logger('prepareDataElements: %s' % (sys.exc_info()[1],))
snmpInASNParseErrs, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB', 'snmpInASNParseErrs')
snmpInASNParseErrs.syntax = snmpInASNParseErrs.syntax + 1
raise error.StatusInformation(
errorIndication = errind.parseError
)
debug.logger & debug.flagMP and debug.logger('prepareDataElements: %s' % (msg.prettyPrint(),))
# rfc3412: 7.2.3
msgVersion = messageProcessingModel = msg.getComponentByPosition(0)
# rfc2576: 5.2.1
snmpEngineMaxMessageSize, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineMaxMessageSize')
securityParameters = (
msg.getComponentByPosition(1),
# transportDomain identifies local enpoint
(transportDomain, transportAddress)
)
messageProcessingModel = int(msg.getComponentByPosition(0))
securityModel = messageProcessingModel + 1
securityLevel = 1
# rfc3412: 7.2.4 -- 7.2.5 -> noop
k = int(securityModel)
if k in snmpEngine.securityModels:
smHandler = snmpEngine.securityModels[k]
else:
raise error.StatusInformation(
errorIndication = errind.unsupportedSecurityModel
)
# rfc3412: 7.2.6
( securityEngineID,
securityName,
scopedPDU,
maxSizeResponseScopedPDU,
securityStateReference ) = smHandler.processIncomingMsg(
snmpEngine,
messageProcessingModel,
snmpEngineMaxMessageSize.syntax,
securityParameters,
securityModel,
securityLevel,
wholeMsg,
msg
)
debug.logger & debug.flagMP and debug.logger('prepareDataElements: SM returned securityEngineID %r securityName %r' % (securityEngineID, securityName))
# rfc3412: 7.2.6a --> noop
# rfc3412: 7.2.7
contextEngineId, contextName, pdu = scopedPDU
# rfc2576: 5.2.1
pduVersion = msgVersion
pduType = pdu.tagSet
# rfc3412: 7.2.8, 7.2.9 -> noop
# rfc3412: 7.2.10
if pduType in rfc3411.responseClassPDUs:
# (wild hack: use PDU reqID at MsgID)
msgID = pdu.getComponentByPosition(0)
# 7.2.10a
try:
cachedReqParams = self._cache.popByMsgId(int(msgID))
except error.ProtocolError:
smHandler.releaseStateInformation(securityStateReference)
raise error.StatusInformation(
errorIndication = errind.dataMismatch
)
# 7.2.10b
sendPduHandle = cachedReqParams['sendPduHandle']
else:
sendPduHandle = None
# no error by default
statusInformation = None
# rfc3412: 7.2.11 -> noop
# rfc3412: 7.2.12
if pduType in rfc3411.responseClassPDUs:
# rfc3412: 7.2.12a -> noop
# rfc3412: 7.2.12b
if securityModel != cachedReqParams['securityModel'] or \
securityName != cachedReqParams['securityName'] or \
securityLevel != cachedReqParams['securityLevel'] or \
contextEngineId != cachedReqParams['contextEngineId'] or \
contextName != cachedReqParams['contextName']:
smHandler.releaseStateInformation(securityStateReference)
raise error.StatusInformation(
errorIndication = errind.dataMismatch
)
stateReference = None
# rfc3412: 7.2.12c
smHandler.releaseStateInformation(securityStateReference)
# rfc3412: 7.2.12d
return ( messageProcessingModel,
securityModel,
securityName,
securityLevel,
contextEngineId,
contextName,
pduVersion,
pdu,
pduType,
sendPduHandle,
maxSizeResponseScopedPDU,
statusInformation,
stateReference )
# rfc3412: 7.2.13
if pduType in rfc3411.confirmedClassPDUs:
# (wild hack: use PDU reqID at MsgID)
msgID = pdu.getComponentByPosition(0)
# rfc3412: 7.2.13a
snmpEngineID, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMP-FRAMEWORK-MIB', 'snmpEngineID')
if securityEngineID != snmpEngineID.syntax:
smHandler.releaseStateInformation(securityStateReference)
raise error.StatusInformation(
errorIndication = errind.engineIDMismatch
)
# rfc3412: 7.2.13b
stateReference = self._cache.newStateReference()
self._cache.pushByStateRef(
stateReference,
msgVersion=messageProcessingModel,
msgID=msgID,
contextEngineId=contextEngineId,
contextName=contextName,
securityModel=securityModel,
securityName=securityName,
securityLevel=securityLevel,
securityStateReference=securityStateReference,
msgMaxSize=snmpEngineMaxMessageSize.syntax,
maxSizeResponseScopedPDU=maxSizeResponseScopedPDU,
transportDomain=transportDomain,
transportAddress=transportAddress
)
debug.logger & debug.flagMP and debug.logger('prepareDataElements: cached by new stateReference %s' % stateReference)
# rfc3412: 7.2.13c
return ( messageProcessingModel,
securityModel,
securityName,
securityLevel,
contextEngineId,
contextName,
pduVersion,
pdu,
pduType,
sendPduHandle,
maxSizeResponseScopedPDU,
statusInformation,
stateReference )
# rfc3412: 7.2.14
if pduType in rfc3411.unconfirmedClassPDUs:
# Pass new stateReference to let app browse request details
stateReference = self._cache.newStateReference()
# This is not specified explicitly in RFC
smHandler.releaseStateInformation(securityStateReference)
return ( messageProcessingModel,
securityModel,
securityName,
securityLevel,
contextEngineId,
contextName,
pduVersion,
pdu,
pduType,
sendPduHandle,
maxSizeResponseScopedPDU,
statusInformation,
stateReference )
smHandler.releaseStateInformation(securityStateReference)
raise error.StatusInformation(
errorIndication = errind.unsupportedPDUtype
)
class SnmpV2cMessageProcessingModel(SnmpV1MessageProcessingModel):
messageProcessingModelID = univ.Integer(1) # SNMPv2c
snmpMsgSpec = v2c.Message
|