int wpdu::load_vbs(snmp_pdu *raw_pdu, const Pdu& pdu) { int status = 0; // load up the payload // for all Vbs in list, add them to the pdu int vb_count; Vb tempvb; Oid tempoid; SmiLPOID smioid; SmiVALUE smival; vb_count = pdu.get_vb_count(); for (int z = 0; z < vb_count; z++) { pdu.get_vb( tempvb, z); tempvb.get_oid( tempoid); smioid = tempoid.oidval(); // what are we trying to convert here (vb oid part or value part) status = convert_vb_to_smival( tempvb, &smival ); if ( status != SNMP_CLASS_SUCCESS) return status; // add the var to the raw pdu cmu_snmp::add_var(raw_pdu, smioid->ptr, (int) smioid->len, &smival); free_smival_descriptor( &smival); } return status; }
int wpdu::convert_vb_to_smival( Vb &tempvb, SmiVALUE *smival ) { smival->syntax = tempvb.get_syntax(); switch ( smival->syntax ) { case sNMP_SYNTAX_NULL: break; // case sNMP_SYNTAX_INT32: case sNMP_SYNTAX_INT: { SnmpInt32 tmp; tempvb.get_value(tmp); smival->value.sNumber = tmp; } break; // case sNMP_SYNTAX_UINT32: case sNMP_SYNTAX_GAUGE32: case sNMP_SYNTAX_CNTR32: case sNMP_SYNTAX_TIMETICKS: { SnmpUInt32 tmp; tempvb.get_value(tmp); smival->value.uNumber = tmp; } break; // case Counter64 case sNMP_SYNTAX_CNTR64: { Counter64 c64; tempvb.get_value(c64); smival->value.hNumber.hipart = c64.high(); smival->value.hNumber.lopart = c64.low(); } break; // OID syntax case sNMP_SYNTAX_OID: { Oid tmpoid; tmpoid.oidval(); tempvb.get_value(tmpoid); SmiLPOID smi = tmpoid.oidval(); smival->value.oid.len = tmpoid.length(); ACE_NEW_RETURN(smival->value.oid.ptr, SmiUINT32 [smival->value.oid.len], 1); ACE_OS::memcpy(smival->value.oid.ptr, smi->ptr, smival->value.oid.len *sizeof(SmiUINT32)); } break; case sNMP_SYNTAX_BITS: case sNMP_SYNTAX_OCTETS: case sNMP_SYNTAX_IPADDR: { OctetStr os; tempvb.get_value(os); smival->value.string.ptr = 0; smival->value.string.len = os.length(); if ( smival->value.string.len > 0 ) { ACE_NEW_RETURN(smival->value.string.ptr, SmiBYTE [smival->value.string.len], 1); if ( smival->value.string.ptr ) { for (int i=0; i<(int) smival->value.string.len ; i++) smival->value.string.ptr[i] = os[i]; } else { smival->syntax = sNMP_SYNTAX_NULL; // invalidate the smival return SNMP_CLASS_RESOURCE_UNAVAIL; } } } break; default: ACE_DEBUG((LM_DEBUG, "wpdu::convert_vb_to_smival did not convert vb\n")); // ACE_ASSERT(0); } // switch return 0; }
// Send a report message. int v3MP::send_report(unsigned char* scopedPDU, int scopedPDULength, struct snmp_pdu *pdu, int errorCode, int sLevel, int sModel, OctetStr &sName, UdpAddress &destination, Snmp *snmp_session) { debugprintf(2, "v3MP::send_report: Sending report message."); unsigned char *data; int dataLength; int pdu_type = 0; unsigned char cEngineID[MAXLENGTH_ENGINEID+1]; unsigned char cName[MAXLENGTH_CONTEXT_NAME+1]; int cEngineIDLength = MAXLENGTH_ENGINEID+1; int cNameLength = MAXLENGTH_CONTEXT_NAME+1; debugprintf(2, "v3MP::send_report: securityLevel %d",sLevel); if (scopedPDULength != MAX_SNMP_PACKET) { // try to get scopedPDU and PDU data = asn1_parse_scoped_pdu(scopedPDU, &scopedPDULength, cEngineID, &cEngineIDLength, cName, &cNameLength); if (data == NULL) { debugprintf(1, "mp: Error while trying to parse scopedPDU!"); cEngineID[0] = '\0'; cEngineIDLength = 0; cName[0] = '\0'; cNameLength = 0; // Dont send encrypted report if decryption failed: //if (sLevel == SNMP_SECURITY_LEVEL_AUTH_PRIV) // sLevel = SNMP_SECURITY_LEVEL_AUTH_NOPRIV; } else { // data != NULL dataLength = scopedPDULength; // parse data of scopedPDU snmp_parse_data_pdu(pdu, data, dataLength); pdu_type = pdu->command; if (!data) { debugprintf(0, "mp: Error while trying to parse PDU!"); } } // end of: if (data == NULL) } // end if (scopedPDULength != MAX_SNMP_PACKET) else { // scopedPDULength == MAX_SNMP_PACKET cEngineID[0] = '\0'; cEngineIDLength = 0; cName[0] = '\0'; cNameLength = 0; pdu->reqid = 0; } clear_pdu(pdu); // Clear pdu and free all content debugprintf(4, "pdu->reqid = %ld",pdu->reqid); pdu->errstat = 0; pdu->errindex = 0; pdu->command = REPORT_MSG; Vb counterVb; Oid counterOid; SmiLPOID smioid; SmiVALUE smival; switch (errorCode) { case SNMPv3_MP_INVALID_MESSAGE: case SNMPv3_USM_PARSE_ERROR: { counterVb.set_oid(oidSnmpInvalidMsgs); counterVb.set_value(Counter32(get_stats_invalid_msgs())); break; } case SNMPv3_USM_NOT_IN_TIME_WINDOW: case SNMPv3_MP_NOT_IN_TIME_WINDOW: { counterVb.set_oid(oidUsmStatsNotInTimeWindows); counterVb.set_value(Counter32(usm->get_stats_not_in_time_windows())); break; } case SNMPv3_USM_DECRYPTION_ERROR: { counterVb.set_oid(oidUsmStatsDecryptionErrors); counterVb.set_value(Counter32(usm->get_stats_decryption_errors())); //sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; break; } case SNMPv3_USM_AUTHENTICATION_ERROR: case SNMPv3_USM_AUTHENTICATION_FAILURE: { counterVb.set_oid(oidUsmStatsWrongDigests); counterVb.set_value(Counter32(usm->get_stats_wrong_digests())); //sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; break; } case SNMPv3_USM_UNKNOWN_ENGINEID: case SNMPv3_MP_INVALID_ENGINEID: { //sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; counterVb.set_oid(oidUsmStatsUnknownEngineIDs); counterVb.set_value(Counter32(usm->get_stats_unknown_engine_ids())); break; } case SNMPv3_MP_UNSUPPORTED_SECURITY_MODEL: { counterVb.set_oid(oidSnmpUnknownSecurityModels); counterVb.set_value(Counter32(get_stats_unknown_security_models())); sModel = SNMP_SECURITY_MODEL_USM; sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; break; } case SNMPv3_USM_UNKNOWN_SECURITY_NAME: { counterVb.set_oid(oidUsmStatsUnknownUserNames); counterVb.set_value(Counter32(usm->get_stats_unknown_user_names())); sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; debugprintf(2, "Report: SecurityName: %s",sName.get_printable()); break; } case SNMPv3_USM_UNSUPPORTED_SECURITY_LEVEL: { counterVb.set_oid(oidUsmStatsUnsupportedSecLevels); counterVb.set_value(Counter32(usm->get_stats_unsupported_sec_levels())); sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; break; } default: { counterVb.set_oid(oidSnmpInvalidMsgs); counterVb.set_value(Counter32(get_stats_invalid_msgs())); sModel = SNMP_SECURITY_MODEL_USM; sLevel = SNMP_SECURITY_LEVEL_NOAUTH_NOPRIV; sName.set_data(0, 0); debugprintf(2, "ErrorCode was %i in snmp_parse", errorCode); } } // end switch counterVb.get_oid(counterOid); smioid = counterOid.oidval(); int status = convertVbToSmival(counterVb, &smival); if (status != SNMP_CLASS_SUCCESS) { return SNMPv3_MP_ERROR; } snmp_add_var(pdu, smioid->ptr, (int) smioid->len, &smival); freeSmivalDescriptor(&smival); Buffer<unsigned char> sendbuffer(MAX_SNMP_PACKET); int sendbufferlen= MAX_SNMP_PACKET; status = snmp_build( pdu, sendbuffer.get_ptr(), &sendbufferlen, own_engine_id_oct, sName, sModel, sLevel, OctetStr(cEngineID, cEngineIDLength), OctetStr(cName, cNameLength)); if (status != SNMPv3_MP_OK) { debugprintf(2, "v3MP::send_report: error serializing message (mpSnmpBuild returns: %i).", status); return SNMPv3_MP_ERROR; } SnmpSocket send_fd = INVALID_SOCKET; if (pdu_type == sNMP_PDU_INFORM) { debugprintf(4, "Received a snmpInform pdu."); if (snmp_session->get_eventListHolder()->notifyEventList()) send_fd = snmp_session->get_eventListHolder()->notifyEventList()->get_notify_fd(); } status = snmp_session->send_raw_data(sendbuffer.get_ptr(), (size_t)sendbufferlen,// pdu to send destination, // target address send_fd); // the fd to use if ( status != 0) { debugprintf(1, "v3MP::send_report: error sending message (%i)", status); return SNMPv3_MP_ERROR; } debugprintf(3, "v3MP::send_report: Report sent."); return SNMPv3_MP_OK; }
int wpdu::set_trap_info(snmp_pdu *raw_pdu, const Pdu& pdu) const { Oid enterprise; Oid trapid; // validate caller has set this correctly pdu.get_notify_id( trapid); if ( !trapid.valid() || trapid.length() < 2 ) { cmu_snmp::free_pdu( raw_pdu); return SNMP_CLASS_INVALID_NOTIFYID; } raw_pdu->specific_type=0; // TODO: object should emit numeric instead of this kind of mess... if ( trapid == coldStart) raw_pdu->trap_type = V1_COLD_START; // cold start else if ( trapid == warmStart) raw_pdu->trap_type = V1_WARM_START; // warm start else if( trapid == linkDown) raw_pdu->trap_type = V1_LINK_DOWN; // link down else if ( trapid == linkUp) raw_pdu->trap_type = V1_LINK_UP; // link up else if ( trapid == authenticationFailure ) raw_pdu->trap_type = V1_AUTH_FAILURE; // authentication failure else if ( trapid == egpNeighborLoss) raw_pdu->trap_type = V1_EGP_NEIGHBOR_LOSS; // egp neighbor loss else { raw_pdu->trap_type = V1_ENT_SPECIFIC; // enterprise specific // last oid subid is the specific value // if 2nd to last subid is "0", remove it // enterprise is always the notify oid prefix raw_pdu->specific_type = (int) trapid[(int) (trapid.length() - 1)]; trapid.trim(1); if ( trapid[(int)(trapid.length() - 1)] == 0 ) trapid.trim(1); enterprise = trapid; } if ( raw_pdu->trap_type != V1_ENT_SPECIFIC) pdu.get_notify_enterprise( enterprise); if ( enterprise.length() > 0) { // note!! To the contrary, enterprise OID val is // copied here and raw_pdu->enterprise is freed in free_pdu // as it should be (HDN) // these are hooks into an SNMP++ oid // and therefor the raw_pdu enterprise // should not free them. null them out!! SmiLPOID rawOid; rawOid = enterprise.oidval(); // HDN - enterprise is a local object, cannot simply assign pointer //raw_pdu->enterprise = rawOid->ptr; raw_pdu->enterprise_length = (int) rawOid->len; ACE_NEW_RETURN(raw_pdu->enterprise, oid[raw_pdu->enterprise_length],-1); ACE_OS::memcpy((char *)raw_pdu->enterprise,(char *)rawOid->ptr, raw_pdu->enterprise_length * sizeof(oid)); } TimeTicks timestamp; pdu.get_notify_timestamp( timestamp); raw_pdu->time = ( unsigned long) timestamp; // HDN - set agent addr using the local hostname if possible char localHostName[MAXHOSTNAMELEN]; Snmp::get_host_name(localHostName, MAXHOSTNAMELEN); if (ACE_OS::strlen(localHostName) > 0) { GenAddress addr(localHostName); OctetStr octet; addr.to_octet(octet); ACE_OS::memcpy(&(raw_pdu->agent_addr.sin_addr), octet.data(), octet.length()); } return 0; }
int SnmpMessage::load(const Pdu &cpdu, const OctetStr &community, const snmp_version version, const OctetStr* engine_id, const OctetStr* security_name, const int security_model) { int status; const Pdu *pdu = &cpdu; Pdu temppdu; // make sure pdu is valid if ( !pdu->valid()) return SNMP_CLASS_INVALID_PDU; // create a raw pdu snmp_pdu *raw_pdu; raw_pdu = snmp_pdu_create( (int) pdu->get_type()); Oid enterprise; // load it up raw_pdu->reqid = pdu->get_request_id(); #ifdef _SNMPv3 raw_pdu->msgid = pdu->get_message_id(); #endif raw_pdu->errstat= (unsigned long) pdu->get_error_status(); raw_pdu->errindex= (unsigned long) pdu->get_error_index(); // if its a V1 trap then load up other values // for v2, use normal pdu format if (raw_pdu->command == sNMP_PDU_V1TRAP) { // DON'T forget about the v1 trap agent address (changed by Frank Fock) GenAddress gen_addr; IpAddress ip_addr; int addr_set = FALSE; if (pdu->get_v1_trap_address(gen_addr)) { /* User did set the v1 trap address */ if ((gen_addr.get_type() != Address::type_ip) && (gen_addr.get_type() != Address::type_udp) ) { LOG_BEGIN(ERROR_LOG | 4); LOG("SNMPMessage: Bad v1 trap address type in pdu"); LOG(gen_addr.get_type()); LOG_END; snmp_free_pdu( raw_pdu); return SNMP_CLASS_INVALID_PDU; } ip_addr = gen_addr; if (!ip_addr.valid()) { LOG_BEGIN(ERROR_LOG | 1); LOG("SNMPMessage: Copied v1 trap address not valid"); LOG_END; snmp_free_pdu( raw_pdu); return SNMP_CLASS_RESOURCE_UNAVAIL; } addr_set = TRUE; } else { /* User did not set the v1 trap address */ char addrString[256]; if (gethostname(addrString, 255) == 0) { ip_addr = addrString; addr_set = TRUE; } } struct sockaddr_in agent_addr; // agent address socket struct // prepare the agent address memset(&agent_addr, 0, sizeof(agent_addr)); agent_addr.sin_family = AF_INET; if (addr_set) { agent_addr.sin_addr.s_addr = inet_addr(((IpAddress &)ip_addr).IpAddress::get_printable()); LOG_BEGIN(INFO_LOG | 7); LOG("SNMPMessage: Setting v1 trap address"); LOG(((IpAddress &)ip_addr).IpAddress::get_printable()); LOG_END; } raw_pdu->agent_addr = agent_addr; //-----[ compute generic trap value ]------------------------------- // determine the generic value // 0 - cold start // 1 - warm start // 2 - link down // 3 - link up // 4 - authentication failure // 5 - egpneighborloss // 6 - enterprise specific Oid trapid; pdu->get_notify_id( trapid); if ( !trapid.valid() || trapid.len() < 2 ) { snmp_free_pdu( raw_pdu); return SNMP_CLASS_INVALID_NOTIFYID; } raw_pdu->specific_type=0; if ( trapid == coldStart) raw_pdu->trap_type = 0; // cold start else if ( trapid == warmStart) raw_pdu->trap_type = 1; // warm start else if( trapid == linkDown) raw_pdu->trap_type = 2; // link down else if ( trapid == linkUp) raw_pdu->trap_type = 3; // link up else if ( trapid == authenticationFailure ) raw_pdu->trap_type = 4; // authentication failure else if ( trapid == egpNeighborLoss) raw_pdu->trap_type = 5; // egp neighbor loss else { raw_pdu->trap_type = 6; // enterprise specific // last oid subid is the specific value // if 2nd to last subid is "0", remove it // enterprise is always the notify oid prefix raw_pdu->specific_type = (int) trapid[(int) (trapid.len()-1)]; trapid.trim(1); if ( trapid[(int)(trapid.len()-1)] == 0 ) trapid.trim(1); enterprise = trapid; } if ( raw_pdu->trap_type !=6) pdu->get_notify_enterprise( enterprise); if ( enterprise.len() >0) { // note!! // these are hooks into an SNMP++ oid // and therefor the raw_pdu enterprise // should not free them. null them out!! SmiLPOID rawOid; rawOid = enterprise.oidval(); raw_pdu->enterprise = rawOid->ptr; raw_pdu->enterprise_length = (int) rawOid->len; } // timestamp TimeTicks timestamp; pdu->get_notify_timestamp( timestamp); raw_pdu->time = ( unsigned long) timestamp; } // if its a v2 trap then we need to make a few adjustments // vb #1 is the timestamp // vb #2 is the id, represented as an Oid if (( raw_pdu->command == sNMP_PDU_TRAP) || ( raw_pdu->command == sNMP_PDU_INFORM)) { Vb tempvb; temppdu = *pdu; temppdu.trim(temppdu.get_vb_count()); // vb #1 is the timestamp TimeTicks timestamp; tempvb.set_oid(SNMP_MSG_OID_SYSUPTIME); // sysuptime pdu->get_notify_timestamp( timestamp); tempvb.set_value ( timestamp); temppdu += tempvb; // vb #2 is the id Oid trapid; tempvb.set_oid(SNMP_MSG_OID_TRAPID); pdu->get_notify_id( trapid); tempvb.set_value( trapid); temppdu += tempvb; // append the remaining vbs for (int z=0; z<pdu->get_vb_count(); z++) { pdu->get_vb( tempvb,z); temppdu += tempvb; } pdu = &temppdu; // reassign the pdu to the temp one } // load up the payload // for all Vbs in list, add them to the pdu int vb_count; Vb tempvb; Oid tempoid; SmiLPOID smioid; SmiVALUE smival; vb_count = pdu->get_vb_count(); for (int z=0;z<vb_count;z++) { pdu->get_vb( tempvb,z); tempvb.get_oid( tempoid); smioid = tempoid.oidval(); // clear the value portion, in case its // not already been done so by the app writer // only do it in the case its a get,next or bulk if ((raw_pdu->command == sNMP_PDU_GET) || (raw_pdu->command == sNMP_PDU_GETNEXT) || (raw_pdu->command == sNMP_PDU_GETBULK)) tempvb.set_null(); status = convertVbToSmival( tempvb, &smival ); if ( status != SNMP_CLASS_SUCCESS) { snmp_free_pdu( raw_pdu); return status; } // add the vb to the raw pdu snmp_add_var( raw_pdu, smioid->ptr, (int) smioid->len, &smival); freeSmivalDescriptor( &smival); } // ASN1 encode the pdu #ifdef _SNMPv3 if (version == version3) { if ((!engine_id) || (!security_name)) { LOG_BEGIN(ERROR_LOG | 4); LOG("SNMPMessage: Need security name and engine id for v3 message"); LOG_END; // prevention of SNMP++ Enterprise Oid death if ( enterprise.len() >0) { raw_pdu->enterprise = 0; raw_pdu->enterprise_length=0; } snmp_free_pdu( raw_pdu); return SNMP_CLASS_INVALID_TARGET; } status = v3MP::I->snmp_build(raw_pdu, databuff, (int *)&bufflen, *engine_id, *security_name, security_model, pdu->get_security_level(), pdu->get_context_engine_id(), pdu->get_context_name()); if (status == SNMPv3_MP_OK) { if ((pdu->get_type() == sNMP_PDU_RESPONSE) && ((int)pdu->get_maxsize_scopedpdu() < pdu->get_asn1_length())) { LOG_BEGIN(ERROR_LOG | 1); LOG("SNMPMessage: *BUG*: Serialized response pdu is too big (len) (max)"); LOG(pdu->get_asn1_length()); LOG(pdu->get_maxsize_scopedpdu()); LOG_END; // prevention of SNMP++ Enterprise Oid death if ( enterprise.len() >0) { raw_pdu->enterprise = 0; raw_pdu->enterprise_length=0; } snmp_free_pdu( raw_pdu); return SNMP_ERROR_TOO_BIG; } } } else #endif status = snmp_build( raw_pdu, databuff, (int *) &bufflen, version, community.data(), (int) community.len()); LOG_BEGIN(DEBUG_LOG | 4); LOG("SNMPMessage: return value for build message"); LOG(status); LOG_END; if ((status != 0) #ifdef _SNMPv3 && ((version != version3) || (status != SNMPv3_MP_OK)) #endif ) { valid_flag = false; // prevention of SNMP++ Enterprise Oid death if ( enterprise.len() >0) { raw_pdu->enterprise = 0; raw_pdu->enterprise_length=0; } snmp_free_pdu( raw_pdu); #ifdef _SNMPv3 if (version == version3) return status; else #endif // NOTE: This is an assumption - in most cases during normal // operation the reason is a tooBig - another could be a // damaged variable binding. return SNMP_ERROR_TOO_BIG; } valid_flag = true; // prevention of SNMP++ Enterprise Oid death if ( enterprise.len() >0) { raw_pdu->enterprise = 0; raw_pdu->enterprise_length=0; } snmp_free_pdu( raw_pdu); return SNMP_CLASS_SUCCESS; }