/usr/lib/python3/dist-packages/pysnmp/proto/proxy/rfc2576.py is in python3-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 | # PDU v1/v2c two-way proxy
from pysnmp.proto import rfc1905, rfc3411, error
from pysnmp.proto.api import v1, v2c
from pysnmp import debug
# 2.1.1
__v1ToV2ValueMap = {
v1.Integer.tagSet: v2c.Integer32(),
v1.OctetString.tagSet: v2c.OctetString(),
v1.Null.tagSet: v2c.Null(),
v1.ObjectIdentifier.tagSet: v2c.ObjectIdentifier(),
v1.IpAddress.tagSet: v2c.IpAddress(),
v1.Counter.tagSet: v2c.Counter32(),
v1.Gauge.tagSet: v2c.Gauge32(),
v1.TimeTicks.tagSet: v2c.TimeTicks(),
v1.Opaque.tagSet: v2c.Opaque()
}
__v2ToV1ValueMap = { # XXX do not re-create same-type items?
v2c.Integer32.tagSet: v1.Integer(),
v2c.OctetString.tagSet: v1.OctetString(),
v2c.Null.tagSet: v1.Null(),
v2c.ObjectIdentifier.tagSet: v1.ObjectIdentifier(),
v2c.IpAddress.tagSet: v1.IpAddress(),
v2c.Counter32.tagSet: v1.Counter(),
v2c.Gauge32.tagSet: v1.Gauge(),
v2c.TimeTicks.tagSet: v1.TimeTicks(),
v2c.Opaque.tagSet: v1.Opaque()
}
# PDU map
__v1ToV2PduMap = {
v1.GetRequestPDU.tagSet: v2c.GetRequestPDU(),
v1.GetNextRequestPDU.tagSet: v2c.GetNextRequestPDU(),
v1.SetRequestPDU.tagSet: v2c.SetRequestPDU(),
v1.GetResponsePDU.tagSet: v2c.ResponsePDU(),
v1.TrapPDU.tagSet: v2c.SNMPv2TrapPDU()
}
__v2ToV1PduMap = {
v2c.GetRequestPDU.tagSet: v1.GetRequestPDU(),
v2c.GetNextRequestPDU.tagSet: v1.GetNextRequestPDU(),
v2c.SetRequestPDU.tagSet: v1.SetRequestPDU(),
v2c.ResponsePDU.tagSet: v1.GetResponsePDU(),
v2c.SNMPv2TrapPDU.tagSet: v1.TrapPDU(),
v2c.GetBulkRequestPDU.tagSet: v1.GetNextRequestPDU() # 4.1.1
}
# Trap map
__v1ToV2TrapMap = {
0: (1,3,6,1,6,3,1,1,5,1),
1: (1,3,6,1,6,3,1,1,5,2),
2: (1,3,6,1,6,3,1,1,5,3),
3: (1,3,6,1,6,3,1,1,5,4),
4: (1,3,6,1,6,3,1,1,5,5),
5: (1,3,6,1,6,3,1,1,5,6)
}
__v2ToV1TrapMap = {
(1,3,6,1,6,3,1,1,5,1): 0,
(1,3,6,1,6,3,1,1,5,2): 1,
(1,3,6,1,6,3,1,1,5,3): 2,
(1,3,6,1,6,3,1,1,5,4): 3,
(1,3,6,1,6,3,1,1,5,5): 4,
(1,3,6,1,6,3,1,1,5,6): 5
}
# 4.3
__v2ToV1ErrorMap = {
0: 0,
1: 1,
5: 5,
10: 3,
9: 3,
7: 3,
8: 3,
12: 3,
6: 2,
17: 2,
11: 2,
18: 2,
13: 5,
14: 5,
15: 5,
16: 2
}
__zeroInt = v1.Integer(0)
def v1ToV2(v1Pdu, origV2Pdu=None):
pduType = v1Pdu.tagSet
v2Pdu = __v1ToV2PduMap[pduType].clone()
debug.logger & debug.flagPrx and debug.logger('v1ToV2: v1Pdu %s' % v1Pdu.prettyPrint())
v2VarBinds = []
# 3.1
if pduType in rfc3411.notificationClassPDUs:
# 3.1.1
sysUpTime = v1.apiTrapPDU.getTimeStamp(v1Pdu)
# 3.1.2
genericTrap = v1.apiTrapPDU.getGenericTrap(v1Pdu)
if genericTrap == 6:
snmpTrapOIDParam = v1.apiTrapPDU.getEnterprise(v1Pdu) + (0,) + \
(int(v1.apiTrapPDU.getSpecificTrap(v1Pdu)),)
# 3.1.3
else:
snmpTrapOIDParam = v2c.ObjectIdentifier(
__v1ToV2TrapMap[genericTrap]
)
# 3.1.4 (XXX snmpTrapCommunity.0 is missing here)
v2VarBinds.append((v2c.apiTrapPDU.sysUpTime, sysUpTime))
v2VarBinds.append((v2c.apiTrapPDU.snmpTrapOID, snmpTrapOIDParam))
v2VarBinds.append(
(v2c.apiTrapPDU.snmpTrapAddress, v1.apiTrapPDU.getAgentAddr(v1Pdu))
)
v2VarBinds.append((v2c.apiTrapPDU.snmpTrapCommunity, v2c.OctetString("")))
v2VarBinds.append((v2c.apiTrapPDU.snmpTrapEnterprise,
v1.apiTrapPDU.getEnterprise(v1Pdu)))
varBinds = v1.apiTrapPDU.getVarBinds(v1Pdu)
else:
varBinds = v1.apiPDU.getVarBinds(v1Pdu)
# Translate Var-Binds
for oid, v1Val in varBinds:
# 2.1.1.11
if v1Val.tagSet == v1.NetworkAddress.tagSet:
v1Val = v1Val.getComponent()
v2VarBinds.append(
(oid, __v1ToV2ValueMap[v1Val.tagSet].clone(v1Val))
)
if pduType in rfc3411.responseClassPDUs:
# 4.1.2.2.1&2
errorStatus = int(v1.apiPDU.getErrorStatus(v1Pdu))
errorIndex = int(v1.apiPDU.getErrorIndex(v1Pdu, muteErrors=True))
if errorStatus == 2: # noSuchName
if origV2Pdu.tagSet == v2c.GetNextRequestPDU.tagSet:
v2VarBinds = [
(o, rfc1905.endOfMibView) for o, v in v2VarBinds
]
else:
v2VarBinds = [
(o, rfc1905.noSuchObject) for o, v in v2VarBinds
]
# one-to-one mapping
v2c.apiPDU.setErrorStatus(v2Pdu, errorStatus)
v2c.apiPDU.setErrorIndex(v2Pdu, errorIndex)
# 4.1.2.1 --> no-op
if pduType not in rfc3411.notificationClassPDUs:
v2c.apiPDU.setRequestID(v2Pdu, int(v1.apiPDU.getRequestID(v1Pdu)))
v2c.apiPDU.setVarBinds(v2Pdu, v2VarBinds)
debug.logger & debug.flagPrx and debug.logger('v1ToV2: v2Pdu %s' % v2Pdu.prettyPrint())
return v2Pdu
def v2ToV1(v2Pdu, origV1Pdu=None):
debug.logger & debug.flagPrx and debug.logger('v2ToV1: v2Pdu %s' % v2Pdu.prettyPrint())
pduType = v2Pdu.tagSet
if pduType in __v2ToV1PduMap:
v1Pdu = __v2ToV1PduMap[pduType].clone()
else:
raise error.ProtocolError('Unsupported PDU type')
v2VarBinds = v2c.apiPDU.getVarBinds(v2Pdu)
v1VarBinds = []
# 3.2
if pduType in rfc3411.notificationClassPDUs:
# 3.2.1
(snmpTrapOID, snmpTrapOIDParam) = v2VarBinds[1]
if snmpTrapOID != v2c.apiTrapPDU.snmpTrapOID:
raise error.ProtocolError('Second OID not snmpTrapOID')
if snmpTrapOIDParam in __v2ToV1TrapMap:
for oid, val in v2VarBinds:
if oid == v2c.apiTrapPDU.snmpTrapEnterprise:
v1.apiTrapPDU.setEnterprise(v1Pdu, val)
break
else:
# snmpTraps
v1.apiTrapPDU.setEnterprise(v1Pdu, (1, 3, 6, 1, 6, 3, 1, 1, 5))
else:
if snmpTrapOIDParam[-2] == 0:
v1.apiTrapPDU.setEnterprise(v1Pdu, snmpTrapOIDParam[:-2])
else:
v1.apiTrapPDU.setEnterprise(v1Pdu, snmpTrapOIDParam[:-1])
# 3.2.2
for oid, val in v2VarBinds:
# snmpTrapAddress
if oid == v2c.apiTrapPDU.snmpTrapAddress:
v1.apiTrapPDU.setAgentAddr(v1Pdu, val)
break
else:
v1.apiTrapPDU.setAgentAddr(v1Pdu, v1.apiTrapPDU.agentAddress)
# 3.2.3
if snmpTrapOIDParam in __v2ToV1TrapMap:
v1.apiTrapPDU.setGenericTrap(v1Pdu, __v2ToV1TrapMap[snmpTrapOIDParam])
else:
v1.apiTrapPDU.setGenericTrap(v1Pdu, 6)
# 3.2.4
if snmpTrapOIDParam in __v2ToV1TrapMap:
v1.apiTrapPDU.setSpecificTrap(v1Pdu, __zeroInt)
else:
v1.apiTrapPDU.setSpecificTrap(v1Pdu, snmpTrapOIDParam[-1])
# 3.2.5
v1.apiTrapPDU.setTimeStamp(v1Pdu, v2VarBinds[0][1])
__v2VarBinds = []
for oid, val in v2VarBinds[2:]:
if oid in __v2ToV1TrapMap or \
oid in (v2c.apiTrapPDU.sysUpTime,
v2c.apiTrapPDU.snmpTrapAddress,
v2c.apiTrapPDU.snmpTrapEnterprise):
continue
__v2VarBinds.append((oid, val))
v2VarBinds = __v2VarBinds
# 3.2.6 --> done below
else:
v1.apiPDU.setErrorStatus(v1Pdu, __zeroInt)
v1.apiPDU.setErrorIndex(v1Pdu, __zeroInt)
if pduType in rfc3411.responseClassPDUs:
idx = len(v2VarBinds)-1
while idx >= 0:
# 4.1.2.1
oid, val = v2VarBinds[idx]
if v2c.Counter64.tagSet == val.tagSet:
if origV1Pdu.tagSet == v1.GetRequestPDU.tagSet:
v1.apiPDU.setErrorStatus(v1Pdu, 2)
v1.apiPDU.setErrorIndex(v1Pdu, idx+1)
break
elif origV1Pdu.tagSet == v1.GetNextRequestPDU.tagSet:
raise error.StatusInformation(idx=idx, pdu=v2Pdu)
else:
raise error.ProtocolError('Counter64 on the way')
# 4.1.2.2.1&2
if val.tagSet in (v2c.NoSuchObject.tagSet,
v2c.NoSuchInstance.tagSet,
v2c.EndOfMibView.tagSet):
v1.apiPDU.setErrorStatus(v1Pdu, 2)
v1.apiPDU.setErrorIndex(v1Pdu, idx+1)
idx = idx - 1
# 4.1.2.3.1
v2ErrorStatus = v2c.apiPDU.getErrorStatus(v2Pdu)
if v2ErrorStatus:
v1.apiPDU.setErrorStatus(
v1Pdu, __v2ToV1ErrorMap[v2ErrorStatus]
)
v1.apiPDU.setErrorIndex(v1Pdu, v2c.apiPDU.getErrorIndex(v2Pdu, muteErrors=True))
# Translate Var-Binds
if pduType in rfc3411.responseClassPDUs and \
v1.apiPDU.getErrorStatus(v1Pdu):
v1VarBinds = v1.apiPDU.getVarBinds(origV1Pdu)
else:
for oid, v2Val in v2VarBinds:
v1VarBinds.append(
(oid, __v2ToV1ValueMap[v2Val.tagSet].clone(v2Val))
)
if pduType in rfc3411.notificationClassPDUs:
v1.apiTrapPDU.setVarBinds(v1Pdu, v1VarBinds)
else:
v1.apiPDU.setVarBinds(v1Pdu, v1VarBinds)
v1.apiPDU.setRequestID(
v1Pdu, v2c.apiPDU.getRequestID(v2Pdu)
)
debug.logger & debug.flagPrx and debug.logger('v2ToV1: v1Pdu %s' % v1Pdu.prettyPrint())
return v1Pdu
|