(file) Return to snmpDeliverTrap_netsnmp.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / snmpIndicationHandler

Diff for /pegasus/src/Pegasus/Handler/snmpIndicationHandler/snmpDeliverTrap_netsnmp.cpp between version 1.18 and 1.29

version 1.18, 2008/08/14 17:30:42 version 1.29, 2013/01/21 05:00:43
Line 1 
Line 1 
 //%2006////////////////////////////////////////////////////////////////////////  //%LICENSE////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development  // Licensed to The Open Group (TOG) under one or more contributor license
 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.  // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;  // this work for additional information regarding copyright ownership.
 // IBM Corp.; EMC Corporation, The Open Group.  // Each contributor licenses this file to you under the OpenPegasus Open
 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;  // Source License; you may not use this file except in compliance with the
 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.  // License.
 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;  
 // EMC Corporation; VERITAS Software Corporation; The Open Group.  
 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;  
 // EMC Corporation; Symantec Corporation; The Open Group.  
 // //
 // Permission is hereby granted, free of charge, to any person obtaining a copy  // Permission is hereby granted, free of charge, to any person obtaining a
 // of this software and associated documentation files (the "Software"), to  // copy of this software and associated documentation files (the "Software"),
 // deal in the Software without restriction, including without limitation the  // to deal in the Software without restriction, including without limitation
 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or  // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 // sell copies of the Software, and to permit persons to whom the Software is  // and/or sell copies of the Software, and to permit persons to whom the
 // furnished to do so, subject to the following conditions:  // Software is furnished to do so, subject to the following conditions:
 // //
 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN  // The above copyright notice and this permission notice shall be included
 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED  // in all copies or substantial portions of the Software.
 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT  
 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR  
 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT  
 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN  
 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION  
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
 // //
 //==============================================================================  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   //
   //////////////////////////////////////////////////////////////////////////
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #include <Pegasus/Common/Packer.h>  #include <Pegasus/Repository/Packer.h>
 #include <Pegasus/Common/Tracer.h> #include <Pegasus/Common/Tracer.h>
 #include "snmpDeliverTrap_netsnmp.h" #include "snmpDeliverTrap_netsnmp.h"
  
Line 45 
Line 43 
     // loaded and loading them can cause some stderr;     // loaded and loading them can cause some stderr;
     // use environment variable MIBS to override the default MIB modules.     // use environment variable MIBS to override the default MIB modules.
     // If there is no MIBS environment variable, add it in.     // If there is no MIBS environment variable, add it in.
     char* envVar;  
     envVar = getenv("MIBS");  
  
     if (envVar == NULL)      setenv("MIBS", "", 0);
     {  
         putenv("MIBS=");  
     }  
  
     // Initialize the mib reader     // Initialize the mib reader
     netsnmp_set_mib_directory("");     netsnmp_set_mib_directory("");
Line 60 
Line 53 
     // Initializes the SNMP library     // Initializes the SNMP library
     init_snmp("snmpIndicationHandler");     init_snmp("snmpIndicationHandler");
  
       // don't load/save persistent file
   #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
       netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
           NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
   #endif
   
     // windows32 specific initialization (is a NOOP on unix)     // windows32 specific initialization (is a NOOP on unix)
     SOCK_STARTUP;     SOCK_STARTUP;
  
Line 87 
Line 86 
     const Uint32& portNumber,     const Uint32& portNumber,
     const Uint16& snmpVersion,     const Uint16& snmpVersion,
     const String& engineID,     const String& engineID,
       const Uint8& snmpSecLevel,
       const Uint8& snmpSecAuthProto,
       const Array<Uint8>& snmpSecAuthKey,
       const Uint8& snmpSecPrivProto,
       const Array<Uint8>& snmpSecPrivKey,
     const Array<String>& vbOids,     const Array<String>& vbOids,
     const Array<String>& vbTypes,     const Array<String>& vbTypes,
     const Array<String>& vbValues)     const Array<String>& vbValues)
Line 100 
Line 104 
     struct snmp_pdu* snmpPdu;     struct snmp_pdu* snmpPdu;
  
     // Creates a SNMP session     // Creates a SNMP session
     _createSession(targetHost, targetHostFormat, portNumber, securityName,      _createSession(
                    sessionHandle, sessionPtr);          targetHost,
           targetHostFormat,
           portNumber,
           securityName,
           snmpVersion,
           engineID,
           snmpSecLevel,
           snmpSecAuthProto,
           snmpSecAuthKey,
           snmpSecPrivProto,
           snmpSecPrivKey,
           sessionHandle,
           sessionPtr);
  
     try     try
     {     {
Line 150 
Line 166 
  
         free(errStr);         free(errStr);
  
           snmp_free_pdu(snmpPdu);
   
         _destroySession(sessionHandle);         _destroySession(sessionHandle);
  
         PEG_METHOD_EXIT();         PEG_METHOD_EXIT();
Line 169 
Line 187 
     Uint16 targetHostFormat,     Uint16 targetHostFormat,
     Uint32 portNumber,     Uint32 portNumber,
     const String& securityName,     const String& securityName,
       Uint16 snmpVersion,
       const String& engineID,
       const Uint8& snmpSecLevel,
       const Uint8& snmpSecAuthProto,
       const Array<Uint8>& snmpSecAuthKey,
       const Uint8& snmpSecPrivProto,
       const Array<Uint8>& snmpSecPrivKey,
     void*& sessionHandle,     void*& sessionHandle,
     snmp_session*& sessionPtr)     snmp_session*& sessionPtr)
 { {
Line 189 
Line 214 
  
         // peername has format: targetHost:portNumber         // peername has format: targetHost:portNumber
         snmpSession.peername =         snmpSession.peername =
             (char*)malloc((size_t)(strlen(targetHostCStr) + 1 + 32));              (char*)calloc(1,strlen(targetHostCStr) + 1 + 32);
  
         if (targetHostFormat == _IPV6_ADDRESS)         if (targetHostFormat == _IPV6_ADDRESS)
         {         {
Line 218 
Line 243 
  
         free(errStr);         free(errStr);
  
           free(snmpSession.peername);
   
         PEG_METHOD_EXIT();         PEG_METHOD_EXIT();
  
         throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,         throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
Line 239 
Line 266 
  
             free(errStr);             free(errStr);
  
               free(snmpSession.peername);
   
             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(             throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
                 _MSG_GET_SESSION_POINTER_FAILED_KEY,                 _MSG_GET_SESSION_POINTER_FAILED_KEY,
                 exceptionStr));                 exceptionStr));
         }         }
  
           free(snmpSession.peername);
   
           switch (snmpVersion)
           {
               case _SNMPv1_TRAP:
               {
                   sessionPtr->version = SNMP_VERSION_1;
                   _addCommunity(sessionPtr,securityName);
                  break;
   
               }
               case _SNMPv2C_TRAP:
               {
                   sessionPtr->version = SNMP_VERSION_2c;
                   _addCommunity(sessionPtr,securityName);
                   break;
               }
   #ifdef PEGASUS_ENABLE_NET_SNMPV3
               case _SNMPv3_TRAP:
               {
                   sessionPtr->version = SNMP_VERSION_3;
                   CString securityNameCStr = securityName.getCString();
                   size_t securityNameLen = strlen(securityNameCStr);
                   SNMP_FREE(sessionPtr->securityName);
                   sessionPtr->securityName = (char *)calloc(1,securityNameLen+1);
                   sessionPtr->securityNameLen = securityNameLen;
                   memcpy(sessionPtr->securityName, (const char*)securityNameCStr,
                       securityNameLen);
   
                   CString engineIdCStr = engineID.getCString();
                   size_t engineIdHexLen = strlen(engineIdCStr);
                   size_t engineIdBinLen = 0;
                   u_char *engineIdBin = (u_char *)calloc(1,engineIdHexLen);
                   free(sessionPtr->securityEngineID);
                   if(!snmp_hex_to_binary(&engineIdBin, &engineIdHexLen,
                       &engineIdBinLen, 1,engineIdCStr))
                   {
                       PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2,
                           "Snmp Indication Handler failed to generate binary"
                               " engine ID for sending the SNMPv3 trap.");
                       throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED,
                           MessageLoaderParms(
                               "Handler.snmpIndicationHandler."
                                   "snmpIndicationHandler."
                               "FAILED_TO_DELIVER_TRAP",
                           "Failed to deliver trap."));
                   }
                   sessionPtr->securityEngineIDLen = engineIdBinLen;
                   sessionPtr->securityEngineID = engineIdBin;
   
                   switch(snmpSecLevel)
                   {
                       case 1:
                           sessionPtr->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
                           break;
                       case 2:
                           sessionPtr->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
                           break;
                       case 3:
                           sessionPtr->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
                           break;
                       default:
                           //use the dedault in the net-snmp conf file.
                           break;
                   }
   
                   SNMP_FREE(sessionPtr->securityAuthProto);
                   if(snmpSecAuthProto == 1) // MD5
                   {
                       sessionPtr->securityAuthProto = snmp_duplicate_objid(
                           usmHMACMD5AuthProtocol,
                           USM_AUTH_PROTO_MD5_LEN);
                       sessionPtr->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
                   }
                   else if(snmpSecAuthProto == 2)// SHA
                   {
                       sessionPtr->securityAuthProto = snmp_duplicate_objid(
                           usmHMACSHA1AuthProtocol,
                           USM_AUTH_PROTO_SHA_LEN);
                       sessionPtr->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
                   }
                   // use the default in net-snmp conf files.
   
                   if(snmpSecAuthKey.size() > 0)
                   {
                       for(Uint32 i=0;i<snmpSecAuthKey.size();i++)
                       {
                           sessionPtr->securityAuthKey[i] = snmpSecAuthKey[i];
                       }
                       sessionPtr->securityAuthKeyLen = snmpSecAuthKey.size();
                   }
   
                   SNMP_FREE(sessionPtr->securityPrivProto);
                   //Privacy
                   if(snmpSecPrivProto == 1) //DES
                   {
                       sessionPtr->securityPrivProto = snmp_duplicate_objid(
                           usmDESPrivProtocol,
                           USM_PRIV_PROTO_DES_LEN);
                       sessionPtr->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
                   }
                   else if(snmpSecPrivProto == 2) // AES
                   {
                       sessionPtr->securityPrivProto = snmp_duplicate_objid(
                           usmAESPrivProtocol,
                           USM_PRIV_PROTO_AES_LEN);
                       sessionPtr->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
                   }
                   // use the defaults in net-snmp conf files
   
                   // Privacy Key
                   if(snmpSecPrivKey.size() > 0)
                   {
                       for(Uint32 j=0;j<snmpSecPrivKey.size();j++)
                       {
                           sessionPtr->securityPrivKey[j] = snmpSecPrivKey[j];
                       }
                       sessionPtr->securityPrivKeyLen = snmpSecPrivKey.size();
                   }
                   break;
               }
   #endif // ifdef PEGASUS_ENABLE_NET_SNMPV3
               default:
               {
                   PEG_METHOD_EXIT();
                   throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED,
                       MessageLoaderParms(
                           _MSG_VERSION_NOT_SUPPORTED_KEY,
                           _MSG_VERSION_NOT_SUPPORTED));
               }
           }
   
       }
       catch (...)
       {
           _destroySession(sessionHandle);
   
           PEG_METHOD_EXIT();
           throw;
       }
   
       PEG_METHOD_EXIT();
   }
   
   void snmpDeliverTrap_netsnmp::_addCommunity(
       struct snmp_session*& sessionPtr,
       const String& securityName)
   {
       PEG_METHOD_ENTER(TRC_IND_HANDLER,
           "snmpDeliverTrap_netsnmp::_addCommunity");
   
         // Community Name, default is public         // Community Name, default is public
         String communityName;         String communityName;
         if (securityName.size() == 0)         if (securityName.size() == 0)
Line 255 
Line 435 
             communityName = securityName;             communityName = securityName;
         }         }
  
         free(snmpSession.peername);  
   
         free(sessionPtr->community);         free(sessionPtr->community);
  
         CString communityNameCStr = communityName.getCString();         CString communityNameCStr = communityName.getCString();
         size_t communityNameLen = strlen(communityNameCStr);         size_t communityNameLen = strlen(communityNameCStr);
  
         sessionPtr->community = (u_char*)malloc(communityNameLen);      sessionPtr->community = (u_char*)calloc(1,communityNameLen+1);
  
         memcpy(sessionPtr->community, (const char*)communityNameCStr,         memcpy(sessionPtr->community, (const char*)communityNameCStr,
                communityNameLen);                communityNameLen);
         sessionPtr->community_len = communityNameLen;  
     }  
     catch (...)  
     {  
         _destroySession(sessionHandle);  
   
         PEG_METHOD_EXIT();  
         throw;  
     }  
  
       sessionPtr->community_len = communityNameLen;
     PEG_METHOD_EXIT();     PEG_METHOD_EXIT();
 } }
  
Line 309 
Line 479 
     {     {
         case _SNMPv1_TRAP:         case _SNMPv1_TRAP:
         {         {
   
             sessionPtr->version = SNMP_VERSION_1;  
   
             // Create the PDU             // Create the PDU
             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP);
  
Line 350 
Line 517 
             break;             break;
         }         }
         case _SNMPv2C_TRAP:         case _SNMPv2C_TRAP:
           case _SNMPv3_TRAP:
         {         {
             sessionPtr->version = SNMP_VERSION_2c;  
   
             // Create the PDU             // Create the PDU
             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP2);             snmpPdu = snmp_pdu_create(SNMP_MSG_TRAP2);
  
Line 455 
Line 621 
     CString trapOidCStr = trapOid.getCString();     CString trapOidCStr = trapOid.getCString();
  
     char* trapOidCopy = strdup(trapOidCStr);     char* trapOidCopy = strdup(trapOidCStr);
     char* numericEntOid = (char*) malloc(strlen(trapOidCStr));      char* numericEntOid = (char*) malloc(strlen(trapOidCStr)+1);
  
     try     try
     {     {
Line 471 
Line 637 
             oidSubIdentifiers.append(p);             oidSubIdentifiers.append(p);
         }         }
  
         long genTrap = 0;  
         long specTrap = 0;  
   
         enterpriseOidLength = MAX_OID_LEN;         enterpriseOidLength = MAX_OID_LEN;
  
         if (Contains(standard_traps, trapOid))         if (Contains(standard_traps, trapOid))
Line 555 
Line 718 
  
         }         }
  
           SNMP_FREE(snmpPdu->enterprise);
         snmpPdu->enterprise = (oid*) malloc(enterpriseOidLength * sizeof(oid));         snmpPdu->enterprise = (oid*) malloc(enterpriseOidLength * sizeof(oid));
         memcpy(snmpPdu->enterprise, enterpriseOid,         memcpy(snmpPdu->enterprise, enterpriseOid,
             enterpriseOidLength * sizeof(oid));             enterpriseOidLength * sizeof(oid));


Legend:
Removed from v.1.18  
changed lines
  Added in v.1.29

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2