From c978fcd613f3738537f385d3618631230527eea9 Mon Sep 17 00:00:00 2001 From: missaak Date: Tue, 17 Mar 2026 20:59:51 +0100 Subject: [PATCH] Fix: preserve user-configured ContextName and ContextEngineId in UpdateDiscoveryValues UpdateDiscoveryValues() unconditionally overwrites _contextName and _contextEngineId with values from the incoming packet's ScopedPdu. When processing SNMPv3 Report PDUs (e.g. usmStatsNotInTimeWindows), these fields in the Report are typically empty, which destroys any user-configured context values. This causes SNMPv3 communication to fail with snmpUnavailableContexts on devices that require a non-empty context name for VACM access control. Per RFC 3414 (USM) Section 3.2 and RFC 3412 Section 7.2, the contextName is a locally-configured value that identifies the MIB view for the request. Report PDUs are administrative responses for engine synchronization and should not alter the manager's context configuration. The fix preserves user-set ContextName and ContextEngineId by only updating them from the packet when they have not been explicitly configured (length == 0). --- SecureAgentParameters.cs | 6 ++++-- UTarget.cs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/SecureAgentParameters.cs b/SecureAgentParameters.cs index 33e7ac5..1cb9f88 100644 --- a/SecureAgentParameters.cs +++ b/SecureAgentParameters.cs @@ -537,8 +537,10 @@ public void UpdateDiscoveryValues(SnmpPacket packet) _engineTime.Value = pkt.USM.EngineTime; _engineBoots.Value = pkt.USM.EngineBoots; UpdateTimeStamp(); - _contextEngineId.Set(pkt.ScopedPdu.ContextEngineId); - _contextName.Set(pkt.ScopedPdu.ContextName); + if (_contextEngineId.Length == 0) + _contextEngineId.Set(pkt.ScopedPdu.ContextEngineId); + if (_contextName.Length == 0) + _contextName.Set(pkt.ScopedPdu.ContextName); } else throw new SnmpInvalidVersionException("Invalid SNMP version."); diff --git a/UTarget.cs b/UTarget.cs index b23bc85..0fc2589 100644 --- a/UTarget.cs +++ b/UTarget.cs @@ -317,8 +317,10 @@ public void UpdateDiscoveryValues(SnmpPacket packet) _engineTime.Value = pkt.USM.EngineTime; _engineBoots.Value = pkt.USM.EngineBoots; UpdateTimeStamp(); - _contextEngineId.Set(pkt.ScopedPdu.ContextEngineId); - _contextName.Set(pkt.ScopedPdu.ContextName); + if (_contextEngineId.Length == 0) + _contextEngineId.Set(pkt.ScopedPdu.ContextEngineId); + if (_contextName.Length == 0) + _contextName.Set(pkt.ScopedPdu.ContextName); } else throw new SnmpInvalidVersionException("Invalid SNMP version.");