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

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