Blame SOURCES/net-snmp-5.8-sec-counter.patch

23ac29
diff -urNp a/include/net-snmp/library/snmpusm.h b/include/net-snmp/library/snmpusm.h
23ac29
--- a/include/net-snmp/library/snmpusm.h	2020-03-16 09:54:29.883655600 +0100
23ac29
+++ b/include/net-snmp/library/snmpusm.h	2020-03-16 09:55:24.142944520 +0100
23ac29
@@ -43,6 +43,7 @@ extern          "C" {
23ac29
      * Structures.
23ac29
      */
23ac29
     struct usmStateReference {
23ac29
+        int             refcnt;
23ac29
         char           *usr_name;
23ac29
         size_t          usr_name_length;
23ac29
         u_char         *usr_engine_id;
23ac29
diff -urNp a/snmplib/snmp_client.c b/snmplib/snmp_client.c
23ac29
--- a/snmplib/snmp_client.c	2020-03-16 09:54:29.892655813 +0100
23ac29
+++ b/snmplib/snmp_client.c	2020-03-16 09:58:13.214021890 +0100
23ac29
@@ -402,27 +402,16 @@ _clone_pdu_header(netsnmp_pdu *pdu)
23ac29
         return NULL;
23ac29
     }
23ac29
 
23ac29
-    if (pdu->securityStateRef &&
23ac29
-        pdu->command == SNMP_MSG_TRAP2) {
23ac29
-
23ac29
-        ret = usm_clone_usmStateReference((struct usmStateReference *) pdu->securityStateRef,
23ac29
-                (struct usmStateReference **) &newpdu->securityStateRef );
23ac29
-
23ac29
-        if (ret)
23ac29
-        {
23ac29
+    sptr = find_sec_mod(newpdu->securityModel);
23ac29
+    if (sptr && sptr->pdu_clone) {
23ac29
+        /* call security model if it needs to know about this */
23ac29
+        ret = sptr->pdu_clone(pdu, newpdu);
23ac29
+        if (ret) {
23ac29
             snmp_free_pdu(newpdu);
23ac29
             return NULL;
23ac29
         }
23ac29
     }
23ac29
 
23ac29
-    if ((sptr = find_sec_mod(newpdu->securityModel)) != NULL &&
23ac29
-        sptr->pdu_clone != NULL) {
23ac29
-        /*
23ac29
-         * call security model if it needs to know about this 
23ac29
-         */
23ac29
-        (*sptr->pdu_clone) (pdu, newpdu);
23ac29
-    }
23ac29
-
23ac29
     return newpdu;
23ac29
 }
23ac29
 
23ac29
diff -urNp a/snmplib/snmpusm.c b/snmplib/snmpusm.c
23ac29
--- a/snmplib/snmpusm.c	2020-03-16 09:54:29.894655860 +0100
23ac29
+++ b/snmplib/snmpusm.c	2020-03-16 10:03:38.870027530 +0100
23ac29
@@ -285,43 +285,64 @@ free_enginetime_on_shutdown(int majorid,
23ac29
 struct usmStateReference *
23ac29
 usm_malloc_usmStateReference(void)
23ac29
 {
23ac29
-    struct usmStateReference *retval = (struct usmStateReference *)
23ac29
-        calloc(1, sizeof(struct usmStateReference));
23ac29
+    struct usmStateReference *retval;
23ac29
+
23ac29
+    retval = calloc(1, sizeof(struct usmStateReference));
23ac29
+    if (retval)
23ac29
+        retval->refcnt = 1;
23ac29
 
23ac29
     return retval;
23ac29
 }                               /* end usm_malloc_usmStateReference() */
23ac29
 
23ac29
+static int
23ac29
+usm_clone(netsnmp_pdu *pdu, netsnmp_pdu *new_pdu)
23ac29
+{
23ac29
+    struct usmStateReference *ref = pdu->securityStateRef;
23ac29
+    struct usmStateReference **new_ref =
23ac29
+        (struct usmStateReference **)&new_pdu->securityStateRef;
23ac29
+    int ret = 0;
23ac29
+
23ac29
+    if (!ref)
23ac29
+        return ret;
23ac29
+
23ac29
+    if (pdu->command == SNMP_MSG_TRAP2) {
23ac29
+        netsnmp_assert(pdu->securityModel == SNMP_DEFAULT_SECMODEL);
23ac29
+        ret = usm_clone_usmStateReference(ref, new_ref);
23ac29
+    } else {
23ac29
+        netsnmp_assert(ref == *new_ref);
23ac29
+        ref->refcnt++;
23ac29
+    }
23ac29
+
23ac29
+    return ret;
23ac29
+}
23ac29
+
23ac29
 
23ac29
 void
23ac29
 usm_free_usmStateReference(void *old)
23ac29
 {
23ac29
-    struct usmStateReference *old_ref = (struct usmStateReference *) old;
23ac29
+    struct usmStateReference *ref = old;
23ac29
 
23ac29
-    if (old_ref) {
23ac29
+    if (!ref)
23ac29
+        return;
23ac29
 
23ac29
-        if (old_ref->usr_name_length)
23ac29
-            SNMP_FREE(old_ref->usr_name);
23ac29
-        if (old_ref->usr_engine_id_length)
23ac29
-            SNMP_FREE(old_ref->usr_engine_id);
23ac29
-        if (old_ref->usr_auth_protocol_length)
23ac29
-            SNMP_FREE(old_ref->usr_auth_protocol);
23ac29
-        if (old_ref->usr_priv_protocol_length)
23ac29
-            SNMP_FREE(old_ref->usr_priv_protocol);
23ac29
-
23ac29
-        if (old_ref->usr_auth_key_length && old_ref->usr_auth_key) {
23ac29
-            SNMP_ZERO(old_ref->usr_auth_key, old_ref->usr_auth_key_length);
23ac29
-            SNMP_FREE(old_ref->usr_auth_key);
23ac29
-        }
23ac29
-        if (old_ref->usr_priv_key_length && old_ref->usr_priv_key) {
23ac29
-            SNMP_ZERO(old_ref->usr_priv_key, old_ref->usr_priv_key_length);
23ac29
-            SNMP_FREE(old_ref->usr_priv_key);
23ac29
-        }
23ac29
+    if (--ref->refcnt > 0)
23ac29
+        return;
23ac29
 
23ac29
-        SNMP_ZERO(old_ref, sizeof(*old_ref));
23ac29
-        SNMP_FREE(old_ref);
23ac29
+    SNMP_FREE(ref->usr_name);
23ac29
+    SNMP_FREE(ref->usr_engine_id);
23ac29
+    SNMP_FREE(ref->usr_auth_protocol);
23ac29
+    SNMP_FREE(ref->usr_priv_protocol);
23ac29
 
23ac29
+    if (ref->usr_auth_key_length && ref->usr_auth_key) {
23ac29
+        SNMP_ZERO(ref->usr_auth_key, ref->usr_auth_key_length);
23ac29
+        SNMP_FREE(ref->usr_auth_key);
23ac29
+    }
23ac29
+    if (ref->usr_priv_key_length && ref->usr_priv_key) {
23ac29
+        SNMP_ZERO(ref->usr_priv_key, ref->usr_priv_key_length);
23ac29
+        SNMP_FREE(ref->usr_priv_key);
23ac29
     }
23ac29
 
23ac29
+    SNMP_FREE(ref);
23ac29
 }                               /* end usm_free_usmStateReference() */
23ac29
 
23ac29
 struct usmUser *
23ac29
@@ -3316,6 +3337,7 @@ init_usm(void)
23ac29
     def->encode_reverse = usm_secmod_rgenerate_out_msg;
23ac29
     def->encode_forward = usm_secmod_generate_out_msg;
23ac29
     def->decode = usm_secmod_process_in_msg;
23ac29
+    def->pdu_clone = usm_clone;
23ac29
     def->pdu_free_state_ref = usm_free_usmStateReference;
23ac29
     def->session_setup = usm_session_init;
23ac29
     def->handle_report = usm_handle_report;