Esempio n. 1
0
void
cpqNicIfLogMapTable_cache_reload(int index)
{
    netsnmp_cache  *cpqNicIfLogMapTable_cache = NULL;
    netsnmp_container *iflogmap_container;
    netsnmp_iterator  *it;
    cpqNicIfLogMapTable_entry* entry = NULL;

    DEBUGMSGTL(("internal:cpqNicIfLogMapTable:_cache_reload", "triggered\n"));

    cpqNicIfLogMapTable_cache = netsnmp_cache_find_by_oid(cpqNicIfLogMapTable_oid,
                                            cpqNicIfLogMapTable_oid_len);

    if (cpqNicIfLogMapTable_cache != NULL) {
        iflogmap_container = cpqNicIfLogMapTable_cache->magic;
        it = CONTAINER_ITERATOR(iflogmap_container);

        entry = ITERATOR_FIRST( it );
        while (entry != NULL ) {
            if (entry->cpqNicIfLogMapIndex == index)
                break;
            entry = ITERATOR_NEXT( it );
        }
        ITERATOR_RELEASE( it );
        if (entry != NULL)
            cpqNicIfLogMap_reload_entry(entry);
    }
}
Esempio n. 2
0
int
swrun_count_processes_by_regex(pcre *regexp)
{
    netsnmp_swrun_entry *entry;
    netsnmp_iterator  *it;
    int i = 0;
    int found_ndx[30];
    int found;
    char fullCommand[64 + 128 + 128 + 3];

    netsnmp_cache *swrun_cache = netsnmp_swrun_cache();
    netsnmp_container *swrun_container = netsnmp_swrun_container();

    if ( !swrun_container || !regexp )
        return 0;    /* or -1 */

    it = CONTAINER_ITERATOR( swrun_container );
    while ((entry = (netsnmp_swrun_entry*)ITERATOR_NEXT( it )) != NULL) {
        // need to assemble full command back so regexps can get full picture
        sprintf(fullCommand, "%s %s", entry->hrSWRunPath, entry->hrSWRunParameters);
        found = pcre_exec(regexp, NULL, fullCommand, strlen(fullCommand), 0, 0, found_ndx, 30);
        if (found > 0) {
            i++;
        }
    }
    ITERATOR_RELEASE( it );

    return i;
}
Esempio n. 3
0
/*
 * log CSV version of trap.
 * dontcare param is there so this function can be passed directly
 * to CONTAINER_FOR_EACH.
 */
static void
_sql_log(sql_buf *sqlb, void* dontcare)
{
    netsnmp_iterator     *it;
    sql_vb_buf           *sqlvb;

    if ((NULL == sqlb) || sqlb->logged)
        return;

    /*
     * log trap info
     * nothing done to protect against data insertion attacks with
     * respect to bad data (commas, newlines, etc)
     */
    snmp_log(LOG_ERR,
             "trap:%d-%d-%d %d:%d:%d,%s,%d,%d,%d,%s,%s,%d,%d,%d,%s,%s,%s,%s\n",
             sqlb->time.year,sqlb->time.month,sqlb->time.day,
             sqlb->time.hour,sqlb->time.minute,sqlb->time.second,
             sqlb->user,
             sqlb->type, sqlb->version, sqlb->reqid, sqlb->oid,
             sqlb->transport, sqlb->security_model, sqlb->msgid,
             sqlb->security_level, sqlb->context,
             sqlb->context_engine, sqlb->security_name,
             sqlb->security_engine);

    sqlb->logged = 1; /* prevent multiple logging */

    it = CONTAINER_ITERATOR(sqlb->varbinds);
    if (NULL == it) {
        snmp_log(LOG_ERR,
                 "error creating iterator; incomplete trap logged\n");
        return;
    }

    /** log varbind info */
    for( sqlvb = ITERATOR_FIRST(it); sqlvb; sqlvb = ITERATOR_NEXT(it)) {
#ifdef NETSNMP_MYSQL_TRAP_VALUE_TEXT
        snmp_log(LOG_ERR,"varbind:%s,%s\n", sqlvb->oid, sqlvb->val);
#else
        char *hex;
        int len = binary_to_hex(sqlvb->val, sqlvb->val_len, &hex);
        if (hex) {
            snmp_log(LOG_ERR,"varbind:%d,%s,%s\n", sqlvb->oid, hex);
            free(hex);
        }
        else {
            snmp_log(LOG_ERR,"malloc failed for varbind hex value\n");
            snmp_log(LOG_ERR,"varbind:%s,\n", sqlvb->oid);
        }
#endif
    }
    ITERATOR_RELEASE(it);
   
}
static int
_cache_load(netsnmp_cache *cache, netsnmp_tdata *table)
{
    netsnmp_container *maps;
    netsnmp_iterator  *map_itr;
    netsnmp_cert_map  *map;
    netsnmp_tdata_row *row;
    certToTSN_entry   *entry;
    int                rc = 0;

    DEBUGMSGTL(("tlstmCertToTSNTable:cache:load", "called, %" NETSNMP_PRIz "d rows\n",
                CONTAINER_SIZE(table->container)));

    /** get current active maps */
    maps = netsnmp_cert_map_container();
    if (NULL == maps)
        return 0;
    DEBUGMSGTL(("tlstmCertToTSNTable:cache:load", "maps %" NETSNMP_PRIz "d rows\n",
                CONTAINER_SIZE(maps)));

    map_itr = CONTAINER_ITERATOR(maps);
    if (NULL == map_itr) {
        DEBUGMSGTL(("tlstmCertToTSNTable:cache:load",
                    "cant get map iterator\n"));
        return -1;
    }

    /*
     * insert rows for active maps into tbl container
     */
    map = ITERATOR_FIRST(map_itr);
    for( ; map; map = ITERATOR_NEXT(map_itr)) {

        row = _entry_from_map(map);
        if (NULL == row) {
            rc =-1;
            break;
        }
        entry = (certToTSN_entry*)row->data;
        entry->rowStatus = RS_ACTIVE;

        if (netsnmp_tdata_add_row(table, row) != SNMPERR_SUCCESS) {
            tlstmCertToTSNTable_removeEntry(NULL, row);
            rc = -1;
            break;
        }
    }
    ITERATOR_RELEASE(map_itr);

    DEBUGMSGTL(("tlstmCertToTSNTable:cache:load", "done, %" NETSNMP_PRIz "d rows\n",
                CONTAINER_SIZE(table->container)));

    return rc;
}
Esempio n. 5
0
/** create a new row in the table */
static void
register_foreach(const struct sysORTable* data, void* dummy)
{
    sysORTable_entry *entry;

    sysORLastChange = data->OR_uptime;

    entry = SNMP_MALLOC_TYPEDEF(sysORTable_entry);
    if (!entry) {
	snmp_log(LOG_ERR,
		 "could not allocate storage, sysORTable is inconsistent\n");
    } else {
	const oid firstNext = sysORNextIndex;
	netsnmp_iterator* it = CONTAINER_ITERATOR(table);

	do {
	    const sysORTable_entry* value;
	    const oid cur = sysORNextIndex;

	    if (sysORNextIndex == SNMP_MIN(MAX_SUBID, 2147483647UL))
		sysORNextIndex = 1;
	    else
		++sysORNextIndex;

	    for (value = (sysORTable_entry*)it->curr(it);
		 value && value->sysORIndex < cur;
		 value = (sysORTable_entry*)ITERATOR_NEXT(it)) {
	    }

	    if (value && value->sysORIndex == cur) {
		if (sysORNextIndex < cur)
		    it->reset(it);
	    } else {
		entry->sysORIndex = cur;
		break;
	    }
	} while (firstNext != sysORNextIndex);

	ITERATOR_RELEASE(it);

	if(firstNext == sysORNextIndex) {
            snmp_log(LOG_ERR, "Failed to locate a free index in sysORTable\n");
            free(entry);
	} else {
	    entry->data = data;
	    entry->oid_index.len = 1;
	    entry->oid_index.oids = &entry->sysORIndex;

	    CONTAINER_INSERT(table, entry);
	}
    }
}
Esempio n. 6
0
/* this is called after the connection on the server side by us to
   check other aspects about the connection and obtain the
   securityName from the remote certificate. */
int netsnmp_tlsbase_extract_security_name (SSL * ssl, _netsnmpTLSBaseData * tlsdata)
{
    netsnmp_container *chain_maps;

    netsnmp_cert_map *cert_map, *peer_cert;

    netsnmp_iterator *itr;

    int rc;

    netsnmp_assert_or_return (ssl != NULL, SNMPERR_GENERR);
    netsnmp_assert_or_return (tlsdata != NULL, SNMPERR_GENERR);

    if (NULL == (chain_maps = netsnmp_openssl_get_cert_chain (ssl)))
        return SNMPERR_GENERR;
    /*
     * map fingerprints to mapping entries
     */
    rc = netsnmp_cert_get_secname_maps (chain_maps);
    if ((-1 == rc) || (CONTAINER_SIZE (chain_maps) == 0))
    {
        netsnmp_cert_map_container_free (chain_maps);
        return SNMPERR_GENERR;
    }

    /*
     * change container to sorted (by clearing unsorted option), then
     * iterate over it until we find a map that returns a secname.
     */
    CONTAINER_SET_OPTIONS (chain_maps, 0, rc);
    itr = CONTAINER_ITERATOR (chain_maps);
    if (NULL == itr)
    {
        snmp_log (LOG_ERR, "could not get iterator for secname fingerprints\n");
        netsnmp_cert_map_container_free (chain_maps);
        return SNMPERR_GENERR;
    }
    peer_cert = cert_map = ITERATOR_FIRST (itr);
    for (; !tlsdata->securityName && cert_map; cert_map = ITERATOR_NEXT (itr))
        tlsdata->securityName = netsnmp_openssl_extract_secname (cert_map, peer_cert);
    ITERATOR_RELEASE (itr);

    netsnmp_cert_map_container_free (chain_maps);

    return (tlsdata->securityName ? SNMPERR_SUCCESS : SNMPERR_GENERR);
}
Esempio n. 7
0
void
_load_trusted_certs(SSL_CTX *the_ctx) {
    netsnmp_container *trusted_certs = NULL;
    netsnmp_iterator  *trusted_cert_iterator = NULL;
    char *fingerprint;

    trusted_certs = netsnmp_cert_get_trustlist();
    trusted_cert_iterator = CONTAINER_ITERATOR(trusted_certs);
    if (trusted_cert_iterator) {
        for (fingerprint = (char *) ITERATOR_FIRST(trusted_cert_iterator);
             fingerprint; fingerprint = ITERATOR_NEXT(trusted_cert_iterator)) {
            if (!_trust_this_cert(the_ctx, fingerprint))
                snmp_log(LOG_ERR, "failed to load trust cert: %s\n",
                         fingerprint);
        }
        ITERATOR_RELEASE(trusted_cert_iterator);
    }
}    
Esempio n. 8
0
/** remove a row from the table */
static int
unregister_cb(int major, int minor, void* serv, void* client)
{
    sysORTable_entry *value;
    netsnmp_iterator* it = CONTAINER_ITERATOR(table);

    DEBUGMSGTL(("mibII/sysORTable/unregister_cb",
                "unregister_cb(%d, %d, %p, %p)\n", major, minor, serv, client));
    sysORLastChange = ((struct sysORTable*)(serv))->OR_uptime;

    while ((value = (sysORTable_entry*)ITERATOR_NEXT(it)) && value->data != serv);
    ITERATOR_RELEASE(it);
    if(value) {
	CONTAINER_REMOVE(table, value);
	free(value);
    }
    return SNMP_ERR_NOERROR;
}
Esempio n. 9
0
int
swrun_count_processes_by_name( char *name )
{
    netsnmp_swrun_entry *entry;
    netsnmp_iterator  *it;
    int i = 0;

    netsnmp_cache_check_and_reload(swrun_cache);
    if ( !swrun_container || !name )
        return 0;    /* or -1 */

    it = CONTAINER_ITERATOR( swrun_container );
    while ((entry = (netsnmp_swrun_entry*)ITERATOR_NEXT( it )) != NULL) {
        if (0 == strcmp( entry->hrSWRunName, name ))
            i++;
    }
    ITERATOR_RELEASE( it );

    return i;
}
Esempio n. 10
0
/**
 * Setup an iterator for scanning the interfaces using the cached entry
 * from if-mib/ifTable.
 */
void
Interface_Scan_Init(void)
{
    netsnmp_container *cont = NULL;
    netsnmp_cache *cache    = NULL; 
    
    cache = netsnmp_cache_find_by_oid(ifTable_oid, ifTable_oid_size);
    if (NULL != cache) {
        netsnmp_cache_check_and_reload(cache);
        cont = (netsnmp_container*) cache->magic;
    }
    
    if (NULL != cont) {
        if (NULL != it)
            ITERATOR_RELEASE(it);
    
        it = CONTAINER_ITERATOR(cont);
    }
   
    if (NULL != it)
        row = (ifTable_rowreq_ctx*)ITERATOR_FIRST(it);
}
Esempio n. 11
0
void
Interface_Scan_Init(void)
{
    /*
     * ifTable container shouldn't change, so we shouldn' have to
     * re-fetch it every time.
     */
    if (NULL != c)
        netsnmp_access_interface_container_free(c, 0);

    c = netsnmp_access_interface_container_load(NULL, 0);
    
    if (NULL != c) {
        if (NULL != it)
            ITERATOR_RELEASE(it);
    
        it = CONTAINER_ITERATOR(c);
    }
   
    if (NULL != it)
        e = (netsnmp_interface_entry*)ITERATOR_FIRST(it);
}
static void
_cache_free(netsnmp_cache *cache, netsnmp_tdata *table)
{
    netsnmp_tdata_row *row;
    netsnmp_iterator   *tbl_itr;
    certToTSN_entry   *entry;

    DEBUGMSGTL(("tlstmCertToTSNTable:cache:free", "called, %" NETSNMP_PRIz "d rows\n",
                CONTAINER_SIZE(table->container)));

    /** insert rows for active maps into tbl container */
    tbl_itr = CONTAINER_ITERATOR(table->container);
    if (NULL == tbl_itr) {
        DEBUGMSGTL(("tlstmCertToTSNTable:cache:free",
                    "cant get map iterator\n"));
        return;
    }

    row = ITERATOR_FIRST(tbl_itr);
    for( ; row; row = ITERATOR_NEXT(tbl_itr)) {
        entry = row->data;

        /*
         * remove all active rows (they are in the maps container kept
         * by the library). Keep inactive ones for next time.
         */
        if (entry->rowStatus == RS_ACTIVE) {
            tlstmCertToTSNTable_removeEntry(NULL, row);
            ITERATOR_REMOVE(tbl_itr);
            continue;
        }
    }
    ITERATOR_RELEASE(tbl_itr);

    DEBUGMSGTL(("tlstmCertToTSNTable:cache:free", "done, %" NETSNMP_PRIz "d rows\n",
                CONTAINER_SIZE(table->container)));
}
static int
_save_maps(int majorID, int minorID, void *serverarg, void *clientarg)
{
    char            sep[] =
        "\n##############################################################";
    char            buf[] =
        "#\n" "# certificate secName mapping persistent data\n" "#";
    char           *type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
                                                 NETSNMP_DS_LIB_APPTYPE);
    netsnmp_container *maps = netsnmp_cert_map_container();
    netsnmp_tdata_row *row;
    netsnmp_iterator  *tbl_itr, *map_itr;
    netsnmp_cert_map  *map;
    certToTSN_entry   *entry;

    if ((NULL == maps) || ((CONTAINER_SIZE(maps) == 0) &&
                           (CONTAINER_SIZE(_table->container) == 0)))
        return SNMPERR_SUCCESS;

    read_config_store((char *) type, sep);
    read_config_store((char *) type, buf);

    /*
     * save active rows from maps
     */
    if (NULL != maps) {
        map_itr = CONTAINER_ITERATOR(maps);
        if (NULL == map_itr) {
            DEBUGMSGTL(("tlstmCertToTSNTable:save",
                        "cant get map iterator\n"));
            map = NULL;
        }
        else
            map = ITERATOR_FIRST(map_itr);

        for( ; map; map = ITERATOR_NEXT(map_itr)) {
            /** don't store config rows */
            if (map->flags & NSCM_FROM_CONFIG)
                continue;
            _save_map(map, RS_ACTIVE, type);
        }
    }
    ITERATOR_RELEASE(map_itr);

    /*
     * save inactive rows from mib
     */
    tbl_itr = CONTAINER_ITERATOR(_table->container);
    if (NULL == tbl_itr)
        DEBUGMSGTL(("tlstmCertToTSNTable:save", "cant get table iterator\n"));
    else {
        row = ITERATOR_FIRST(tbl_itr);
        for( ; row; row = ITERATOR_NEXT(tbl_itr)) {
            entry = row->data;

            /*
             * skip all active rows (should be in maps and thus saved
             * above) and volatile rows.
             */
            if ((entry->rowStatus == RS_ACTIVE) ||
                (entry->storageType != ST_NONVOLATILE))
                continue;

            _save_entry(entry, type);
        }
        ITERATOR_RELEASE(tbl_itr);
    }

    read_config_store((char *) type, sep);
    read_config_store((char *) type, "\n");

    /*
     * never fails 
     */
    return SNMPERR_SUCCESS;
}
/*
 * check for ipv6 addresses
 */
void
_arch_interface_has_ipv6(oid if_index, u_int *flags,
                         netsnmp_container *addr_container)
{
#ifdef NETSNMP_ENABLE_IPV6
    netsnmp_ipaddress_entry *addr_entry = NULL;
    netsnmp_iterator        *addr_it = NULL;
    u_int                    addr_container_flags = 0; /* must init to 0 */
#endif

    if (NULL == flags)
        return;

    *flags &= ~NETSNMP_INTERFACE_FLAGS_HAS_IPV6;

#ifdef NETSNMP_ENABLE_IPV6
    /*
     * get ipv6 addresses
     */
    if (NULL == addr_container) {
        /*
         * we only care about ipv6, if we need to allocate our own
         * temporary container. set the flags (which we also use later
         * to determine if we need to free the container).
         */
        addr_container_flags = NETSNMP_ACCESS_IPADDRESS_LOAD_IPV6_ONLY;
        addr_container =
            netsnmp_access_ipaddress_container_load(NULL,
                                                    addr_container_flags);
        if (NULL == addr_container) {
            DEBUGMSGTL(("access:ifcontainer",
                        "couldn't get ip addresses container\n"));
            return;
        }
    }
    else {
        /*
         * addr_container flags must be 0, so we don't release the
         * user's container.
         */
        netsnmp_assert(0 == addr_container_flags);
    }


    /*
     * get an ipaddress container iterator, and look for ipv6 addrs
     */   
    addr_it = CONTAINER_ITERATOR(addr_container);
    if (NULL == addr_it) {
        DEBUGMSGTL(("access:ifcontainer",
                    "couldn't get ip addresses iterator\n"));
        if (0!=addr_container_flags)
            netsnmp_access_ipaddress_container_free(addr_container, 0);
        return;
    }

    addr_entry = ITERATOR_FIRST(addr_it);
    for( ; addr_entry ; addr_entry = ITERATOR_NEXT(addr_it) ) {
        /*
         * skip non matching indexes and ipv4 addresses
         */
        if ((if_index != addr_entry->if_index) ||
            (4 == addr_entry->ia_address_len))
            continue;

        /*
         * found one! no need to keep looking, set the flag and bail
         */
        *flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV6;
        break;
    }

    /*
     * make mama proud and clean up after ourselves
     */
    ITERATOR_RELEASE(addr_it);
    if (0!=addr_container_flags)
        netsnmp_access_ipaddress_container_free(addr_container, 0);    
#endif
}
Esempio n. 15
0
int getPackage()
{
    int timelen;
    netsnmp_container *pkg_container = NULL;
    netsnmp_cache *pkg_cache = NULL;
    netsnmp_iterator  *it;
    cpqHoSwVerTable_entry *entry;
    int i;
    int pkgcount = 0;

    pkg_cache = netsnmp_cache_find_by_oid(cpqHoSwVerTable_oid, 
					  cpqHoSwVerTable_oid_len);
    if (pkg_cache == NULL)
        return  0;

    pkg_container = pkg_cache->magic;
    if (pkg_container == NULL)
        return 0;

    pkgcount = (oid)CONTAINER_SIZE(pkg_container);

    if ((packages = (pkg_entry**)malloc(pkgcount * sizeof(pkg_entry *)))
                    == NULL) {
        return 0;
    }

    it = CONTAINER_ITERATOR( pkg_container );
    entry = ITERATOR_FIRST( it );
    i = 0;
    while (entry != NULL) {

        if ((packages[i] = (pkg_entry*)malloc(sizeof(pkg_entry))) == NULL) {
            continue;
        }
        memset(packages[i], 0, sizeof(pkg_entry));

        if (entry->cpqHoSwVerName_len != 0 ) {
            packages[i]->sname = malloc(entry->cpqHoSwVerName_len + 1);
            memset(packages[i]->sname, 0, entry->cpqHoSwVerName_len + 1);
            strcpy(packages[i]->sname, entry->cpqHoSwVerName);

            packages[i]->name = malloc(entry->cpqHoSwVerName_len + 1);
            memset(packages[i]->name, 0, entry->cpqHoSwVerName_len + 1);
	    strcpy(packages[i]->name,  entry->cpqHoSwVerName);
        } else {
            packages[i]->sname = NULL;
            packages[i]->name = NULL;
        }

        if (entry->cpqHoSwVerVersion_len != 0 ) {
            packages[i]->version = malloc(entry->cpqHoSwVerVersion_len + 1);
            memset(packages[i]->version, 0, entry->cpqHoSwVerVersion_len + 1);
	    strcpy(packages[i]->version, entry->cpqHoSwVerVersion);
        } else
            packages[i]->version = NULL;

        if (entry->vendor_len != 0 ) {
            packages[i]->vendor = malloc(entry->vendor_len + 1);
            memset(packages[i]->vendor, 0, entry->vendor_len + 1);
	    strcpy(packages[i]->vendor, entry->vendor);
        } else
            packages[i]->vendor = NULL;

        if (entry->timestamp != 0) {
            packages[i]->timestamp = malloc(80);
            memset(packages[i]->timestamp, 0, 80);
            timelen = strftime(packages[i]->timestamp, 80,
                               "%Y-%m-%d %H:%M:%S %z",
                               gmtime(&entry->timestamp));
        } else
            packages[i]->timestamp = NULL;
                                 
        i++;
        entry = ITERATOR_NEXT( it );
    }
    ITERATOR_RELEASE( it );

    return pkgcount;
}
int
udpEndpointTable_container_load(netsnmp_container *container)
{
    udpEndpointTable_rowreq_ctx *rowreq_ctx;
    netsnmp_container *ep_c;
    netsnmp_iterator *ep_it;
    netsnmp_udp_endpoint_entry *ep;

    /*
     * temporary storage for index values
     */
    u_long	udpEndpointLocalAddressType;
    u_long	udpEndpointRemoteAddressType;

    DEBUGMSGTL(("verbose:udpEndpointTable:udpEndpointTable_container_load",
                "called\n"));

    /*
     * TODO:351:M: |-> Load/update data in the udpEndpointTable container.
     * loop over your udpEndpointTable data, allocate a rowreq context,
     * set the index(es) [and data, optionally] and insert into
     * the container.
     */
    ep_c = netsnmp_access_udp_endpoint_container_load(NULL, 0);
    if (NULL == ep_c)
        return MFD_RESOURCE_UNAVAILABLE;
    ep_it = CONTAINER_ITERATOR(ep_c);
    if (NULL == ep_c) {
        netsnmp_access_udp_endpoint_container_free(ep_c, 0);
        return MFD_RESOURCE_UNAVAILABLE;
    }
    for (ep = (netsnmp_udp_endpoint_entry*)ITERATOR_FIRST(ep_it); ep;
         ep = (netsnmp_udp_endpoint_entry*)ITERATOR_NEXT (ep_it)) {

        /*
         * TODO:352:M: |   |-> set indexes in new udpEndpointTable rowreq context.
         */
        rowreq_ctx = udpEndpointTable_allocate_rowreq_ctx();
        if (NULL == rowreq_ctx) {
            snmp_log(LOG_ERR, "memory allocation failed\n");
            return MFD_RESOURCE_UNAVAILABLE;
        }
        udpEndpointLocalAddressType = _address_type_from_len(ep->loc_addr_len);
	udpEndpointRemoteAddressType = _address_type_from_len(ep->rmt_addr_len);
        if (MFD_SUCCESS !=
            udpEndpointTable_indexes_set(rowreq_ctx,
                                         udpEndpointLocalAddressType,
                                         (char *) ep->loc_addr,
                                         ep->loc_addr_len,
                                         ep->loc_port,
                                         udpEndpointRemoteAddressType,
                                         (char *) ep->rmt_addr,
                                         ep->rmt_addr_len,
                                         ep->rmt_port,
                                         ep->instance,
                                         ep->pid)) {
            snmp_log(LOG_ERR,
                     "error setting index while loading "
                     "udpEndpointTable data.\n");
            udpEndpointTable_release_rowreq_ctx(rowreq_ctx);
            continue;
        }

        /*
         * TODO:352:r: |   |-> populate udpEndpointTable data context.
         * Populate data context here. (optionally, delay until row prep)
         */
        /*
         * non-TRANSIENT data: no need to copy. set pointer to data 
         */

        /*
         * insert into table container
         */
        CONTAINER_INSERT(container, rowreq_ctx);
    }

    ITERATOR_RELEASE(ep_it);

    netsnmp_access_udp_endpoint_container_free(ep_c, 0);


    DEBUGMSGT(("verbose:udpEndpointTable:udpEndpointTable_container_load",
               "inserted %d records\n", (int)CONTAINER_SIZE(container)));

    return MFD_SUCCESS;
}                               /* udpEndpointTable_container_load */
Esempio n. 17
0
/*
 * save a buffered trap to sql database
 */
static void
_sql_save(sql_buf *sqlb, void *dontcare)
{
    netsnmp_iterator     *it;
    sql_vb_buf           *sqlvb;
    u_long                trap_id;

    /*
     * don't even try if we don't have a database connection
     */
    if (0 == _sql.connected) {
        _sql_log(sqlb, NULL);
        return;
    }

    /*
     * the prepared statements are bound to the static buffer objects,
     * so copy the queued data to the static version.
     */
    _tbind[TBIND_HOST].buffer = sqlb->host;
    _tbind[TBIND_HOST].buffer_length = sqlb->host_len;

    _tbind[TBIND_OID].buffer = sqlb->oid;
    _tbind[TBIND_OID].buffer_length = sqlb->oid_len;

    _tbind[TBIND_REQID].buffer = (void *)&sqlb->reqid;
    _tbind[TBIND_VER].buffer = (void *)&sqlb->version;
    _tbind[TBIND_TYPE].buffer = (void *)&sqlb->type;
    _tbind[TBIND_SECURITY_MODEL].buffer = (void *)&sqlb->security_model;

    _tbind[TBIND_DATE].buffer = (void *)&sqlb->time;

    _tbind[TBIND_USER].buffer = sqlb->user;
    _tbind[TBIND_USER].buffer_length = sqlb->user_len;

    _tbind[TBIND_TRANSPORT].buffer = sqlb->transport;
    if (sqlb->transport)
        _tbind[TBIND_TRANSPORT].buffer_length = strlen(sqlb->transport);
    else
        _tbind[TBIND_TRANSPORT].buffer_length = 0;


    if ((SNMP_MP_MODEL_SNMPv3+1) == sqlb->version) {
        _no_v3 = 0;

        _tbind[TBIND_v3_MSGID].buffer = &sqlb->msgid;
        
        _tbind[TBIND_v3_SECURITY_LEVEL].buffer = &sqlb->security_level;
        
        _tbind[TBIND_v3_CONTEXT_NAME].buffer = sqlb->context;
        _tbind[TBIND_v3_CONTEXT_NAME].buffer_length = sqlb->context_len;

        _tbind[TBIND_v3_CONTEXT_ENGINE].buffer = sqlb->context_engine;
        _tbind[TBIND_v3_CONTEXT_ENGINE].buffer_length =
            sqlb->context_engine_len;

        _tbind[TBIND_v3_SECURITY_NAME].buffer = sqlb->security_name;
        _tbind[TBIND_v3_SECURITY_NAME].buffer_length = sqlb->security_name_len;

        _tbind[TBIND_v3_SECURITY_ENGINE].buffer = sqlb->security_engine;
        _tbind[TBIND_v3_SECURITY_ENGINE].buffer_length =
            sqlb->security_engine_len;
    }
    else {
        _no_v3 = 1;
    }

    if (mysql_stmt_bind_param(_sql.trap_stmt, _tbind) != 0) {
        netsnmp_sql_stmt_error(_sql.trap_stmt,
                               "Could not bind parameters for INSERT");
        _sql_log(sqlb, NULL);
        return;
    }

    /** execute the prepared statement */
    if (mysql_stmt_execute(_sql.trap_stmt) != 0) {
        netsnmp_sql_stmt_error(_sql.trap_stmt,
                               "Could not execute insert statement for trap");
        _sql_log(sqlb, NULL);
        return;
    }
    trap_id = mysql_insert_id(_sql.conn);

    /*
     * iterate over the varbinds, copy data and insert
     */
    it = CONTAINER_ITERATOR(sqlb->varbinds);
    if (NULL == it) {
        snmp_log(LOG_ERR,"Could not allocate iterator\n");
        _sql_log(sqlb, NULL);
        return;
    }

    for( sqlvb = ITERATOR_FIRST(it); sqlvb; sqlvb = ITERATOR_NEXT(it)) {

        _vbind[VBIND_ID].buffer = (void *)&trap_id;
        _vbind[VBIND_TYPE].buffer = (void *)&sqlvb->type;

        _vbind[VBIND_OID].buffer = sqlvb->oid;
        _vbind[VBIND_OID].buffer_length = sqlvb->oid_len;

        _vbind[VBIND_VAL].buffer = sqlvb->val;
        _vbind[VBIND_VAL].buffer_length = sqlvb->val_len;

        if (mysql_stmt_bind_param(_sql.vb_stmt, _vbind) != 0) {
            netsnmp_sql_stmt_error(_sql.vb_stmt,
                                   "Could not bind parameters for INSERT");
            _sql_log(sqlb, NULL);
            break;
        }

        if (mysql_stmt_execute(_sql.vb_stmt) != 0) {
            netsnmp_sql_stmt_error(_sql.vb_stmt,
                                   "Could not execute insert statement for varbind");
            _sql_log(sqlb, NULL);
            break;
        }
    }
    ITERATOR_RELEASE(it);
}
/**
 * load initial data
 *
 * TODO:350:M: Implement ipAddressPrefixTable data load
 * This function will also be called by the cache helper to load
 * the container again (after the container free function has been
 * called to free the previous contents).
 *
 * @param container container to which items should be inserted
 *
 * @retval MFD_SUCCESS              : success.
 * @retval MFD_RESOURCE_UNAVAILABLE : Can't access data source
 * @retval MFD_ERROR                : other error.
 *
 *  This function is called to load the index(es) (and data, optionally)
 *  for the every row in the data set.
 *
 * @remark
 *  While loading the data, the only important thing is the indexes.
 *  If access to your data is cheap/fast (e.g. you have a pointer to a
 *  structure in memory), it would make sense to update the data here.
 *  If, however, the accessing the data invovles more work (e.g. parsing
 *  some other existing data, or peforming calculations to derive the data),
 *  then you can limit yourself to setting the indexes and saving any
 *  information you will need later. Then use the saved information in
 *  ipAddressPrefixTable_row_prep() for populating data.
 *
 * @note
 *  If you need consistency between rows (like you want statistics
 *  for each row to be from the same time frame), you should set all
 *  data here.
 *
 */
int
ipAddressPrefixTable_container_load(netsnmp_container *container)
{
    ipAddressPrefixTable_rowreq_ctx *rowreq_ctx = NULL, *tmp_rowreq_ctx;
    ipAddressTable_rowreq_ctx *addr_rowreq_ctx;
    netsnmp_container *addr_container;
    netsnmp_iterator *addr_it;
    size_t          count = 0;
    u_char          tmp_pfx[NETSNMP_ACCESS_IPADDRESS_BUF_SIZE];

    DEBUGMSGTL(("verbose:ipAddressPrefixTable:ipAddressPrefixTable_container_load", "called\n"));

    addr_container = ipAddressTable_container_get();
    if (NULL == addr_container) {
        DEBUGMSGTL(("ipAddressPrefixTable:container_load",
                    "couldn't get ipAddress container\n"));
        return MFD_RESOURCE_UNAVAILABLE;
    }

    addr_it = CONTAINER_ITERATOR(addr_container);
    if (NULL == addr_container) {
        DEBUGMSGTL(("ipAddressPrefixTable:container_load",
                    "couldn't get ipAddress iterator\n"));
        return MFD_RESOURCE_UNAVAILABLE;
    }

    /*
     * TODO:351:M: |-> Load/update data in the ipAddressPrefixTable container.
     * loop over your ipAddressPrefixTable data, allocate a rowreq context,
     * set the index(es) [and data, optionally] and insert into
     * the container.
     */
    for (addr_rowreq_ctx = ITERATOR_FIRST(addr_it);
         addr_rowreq_ctx; addr_rowreq_ctx = ITERATOR_NEXT(addr_it)) {



        /*
         * TODO:352:M: |   |-> set indexes in new ipAddressPrefixTable rowreq context.
         * data context will be set from the param (unless NULL,
         *      in which case a new data context will be allocated)
         */
        if (NULL == rowreq_ctx) {
            rowreq_ctx = ipAddressPrefixTable_allocate_rowreq_ctx(NULL);
            if (NULL == rowreq_ctx) {
                snmp_log(LOG_ERR, "memory allocation failed\n");
                return MFD_RESOURCE_UNAVAILABLE;
            }
        }
        netsnmp_ipaddress_prefix_copy(tmp_pfx,
                                      addr_rowreq_ctx->tbl_idx.
                                      ipAddressAddr,
                                      addr_rowreq_ctx->data->
                                      ia_address_len,
                                      addr_rowreq_ctx->data->
                                      ia_prefix_len);
        if (MFD_SUCCESS !=
            ipAddressPrefixTable_indexes_set(rowreq_ctx,
                                             addr_rowreq_ctx->data->
                                             if_index,
                                             addr_rowreq_ctx->tbl_idx.
                                             ipAddressAddrType, tmp_pfx,
                                             addr_rowreq_ctx->data->
                                             ia_address_len,
                                             addr_rowreq_ctx->data->
                                             ia_prefix_len)) {
            snmp_log(LOG_ERR,
                     "error setting index while loading "
                     "ipAddressPrefixTable data.\n");
            ipAddressPrefixTable_release_rowreq_ctx(rowreq_ctx);
            continue;
        }

        /** do we already have this prefix? */
        tmp_rowreq_ctx = CONTAINER_FIND(container, rowreq_ctx);
        if (NULL != tmp_rowreq_ctx)
            continue;

        /*
         * TODO:352:r: |   |-> populate ipAddressPrefixTable data context.
         * Populate data context here. (optionally, delay until row prep)
         */
        rowreq_ctx->data.ipAddressPrefixOrigin =
            addr_rowreq_ctx->data->ia_origin;

        /** defer the rest til row prep */

        /*
         * insert into table container, clear ptr so we reallocate
         */
        CONTAINER_INSERT(container, rowreq_ctx);
        rowreq_ctx = NULL;
        ++count;
    }

    DEBUGMSGT(("verbose:ipAddressPrefixTable:ipAddressPrefixTable_container_load", "inserted %d records\n", count));

    return MFD_SUCCESS;
}                               /* ipAddressPrefixTable_container_load */
Esempio n. 19
0
int netsnmp_arch_idedisk_container_load(netsnmp_container* container)
{
    cpqIdeControllerTable_entry *cntlr;
    cpqIdeAtaDiskTable_entry *disk;
    cpqIdeAtaDiskTable_entry *old;
    netsnmp_container *cntlr_container;
    netsnmp_iterator  *it;
    netsnmp_cache *cntlr_cache;
    netsnmp_container *fw_container = NULL;
    netsnmp_cache *fw_cache = NULL;

    char buffer[256];
    char attribute[256];
    char *value;
    long long size;
    char *scsi;
    char *generic;

    int Cntlr, Bus, Index, Target;
    char * OS_name;
    char *serialnum = NULL;
    int j;
    long  rc = 0;
    int disk_fd;
    int Health = 0, Temp = -1, Mcot = -1, Wear = -1;
    unsigned char *Temperature;

    netsnmp_index tmp;
    oid oid_index[2];

    DEBUGMSGTL(("idedisk:container:load", "loading\n"));
    /*
     * find  the HBa container.
     */
    SataDiskCondition = CPQ_REG_OK;

    cntlr_cache = netsnmp_cache_find_by_oid(cpqIdeControllerTable_oid, 
                                            cpqIdeControllerTable_oid_len);
    if (cntlr_cache == NULL) {
        return -1;
    }
    cntlr_container = cntlr_cache->magic;
    DEBUGMSGTL(("idedisk:container:load", "Container=%p\n",cntlr_container));
    DEBUGMSGTL(("idedisk:container:load", 
                "ContainerSize=%ld\n", CONTAINER_SIZE(cntlr_container)));
    it = CONTAINER_ITERATOR( cntlr_container );
    cntlr = ITERATOR_FIRST( it );
    DEBUGMSGTL(("idedisk:container:load", "cntlr=%p\n",cntlr));
    while ( cntlr != NULL ) {
        current_Cntlr = cntlr->host;
        DEBUGMSGTL(("idedisk:container:load", 
                    "Starting Loop %s\n", current_Cntlr));
        cntlr->cpqIdeControllerOverallCondition =
                  MAKE_CONDITION(cntlr->cpqIdeControllerOverallCondition,
                                 cntlr->cpqIdeControllerCondition);
        /* Now chack for those HBA's in  the SCSI diskss */
        if ((NumScsiDisk = scandir(ScsiDiskDir, &ScsiDisklist, 
                                   disk_select, alphasort)) <= 0) {
            free(ScsiDisklist);
            cntlr = ITERATOR_NEXT( it );
            continue;
        }	

        for (j= 0; j< NumScsiDisk; j++) {
            memset(&buffer, 0, sizeof(buffer));
            strncpy(buffer, ScsiDiskDir, sizeof(buffer) - 1);
            strncat(buffer, ScsiDisklist[j]->d_name, 
                    sizeof(buffer) - strlen(buffer) - 1);
            DEBUGMSGTL(("idedisk:container:load", "Working on disk %s\n", 
                        ScsiDisklist[j]->d_name));

            sscanf(ScsiDisklist[j]->d_name,"%d:%d:%d:%d",
                        &Cntlr, &Bus, &Index, &Target);

            DEBUGMSGTL(("idedisk:container:load",
                        "looking for cntlr=%ld, Disk = %d\n", 
                        cntlr->cpqIdeControllerIndex, Index));
            oid_index[0] = cntlr->cpqIdeControllerIndex;
            oid_index[1] = Index;
            tmp.len = 2;
            tmp.oids = &oid_index[0];

            old = CONTAINER_FIND(container, &tmp);
            DEBUGMSGTL(("idedisk:container:load", "Old disk=%p\n",old));
            if (old != NULL ) {
                DEBUGMSGTL(("idedisk:container:load", "Re-Use old entry\n"));
                old->OldStatus = old->cpqIdeAtaDiskStatus;
                disk = old;
            } else {
                disk = cpqIdeAtaDiskTable_createEntry(container,
                            (oid)cntlr->cpqIdeControllerIndex, 
                            Index);
                if (disk == NULL) 
                    continue;

                DEBUGMSGTL(("idedisk:container:load", "Entry created\n"));

		scsi = ScsiDisklist[j]->d_name;

                if ((value = get_DiskModel(scsi)) != NULL) {
                    strncpy(disk->cpqIdeAtaDiskModel, value,
                            sizeof(disk->cpqIdeAtaDiskModel) - 1);
                    disk->cpqIdeAtaDiskModel_len = 
                                               strlen(disk->cpqIdeAtaDiskModel);
                    free(value);
                }

                if ((value = get_DiskState(scsi)) != NULL) {
                    if (strcmp(value, "running") == 0)
                        disk->cpqIdeAtaDiskStatus = SAS_PHYS_STATUS_OK;
                    free(value);
                }

                disk->cpqIdeAtaDiskCapacity = get_BlockSize(scsi) >> 11;

                generic = get_ScsiGeneric(scsi);
                if (generic != NULL) {
                    memset(disk->cpqIdeAtaDiskOsName, 0, 256);
                    disk->cpqIdeAtaDiskOsName_len =
                                        sprintf(disk->cpqIdeAtaDiskOsName,
                                                "/dev/%s",
                                                generic);
                    free(generic);
                }
                DEBUGMSGTL(("idedisk:container:load", "generic dev is %s\n",
                                disk->cpqIdeAtaDiskOsName));

            }
            if ((disk_fd = open(disk->cpqIdeAtaDiskOsName, 
                                O_RDWR | O_NONBLOCK)) >= 0) {
                if ((serialnum = get_unit_sn(disk_fd)) != NULL  ) {
                    strncpy(disk->cpqIdeAtaDiskSerialNumber, serialnum, 40);
                    disk->cpqIdeAtaDiskSerialNumber_len = strlen(serialnum);
                    free(serialnum);
                }

                if ((value = get_sata_DiskRev(disk_fd)) != NULL) {
                    strncpy(disk->cpqIdeAtaDiskFwRev, value,
                            sizeof(disk->cpqIdeAtaDiskFwRev) - 1);
                    disk->cpqIdeAtaDiskFwRev_len = 
                                               strlen(disk->cpqIdeAtaDiskFwRev);
                    free(value);
                }

                disk->cpqIdeAtaDiskPowerOnHours = get_sata_pwron(disk_fd);

                disk->cpqIdeAtaDiskSSDEstTimeRemainingHours = -1;

                if ((Temperature = get_sata_temp(disk_fd)) != NULL ) {
                    Temp = sata_parse_current(Temperature);
                    Mcot = sata_parse_mcot(Temperature);
                    disk->cpqIdeAtaDiskCurrTemperature = Temp;
                    disk->cpqIdeAtaDiskTemperatureThreshold = Mcot;
                    free(Temperature);
                }

                if (is_unit_ssd(disk_fd)) {
                    disk->cpqIdeAtaDiskMediaType = PHYS_DRV_SOLID_STATE;
                    Wear = get_sata_ssd_wear(disk_fd);
                    disk->cpqIdeAtaDiskSSDPercntEndrnceUsed = Wear;
                    disk->cpqIdeAtaDiskSSDWearStatus = SSD_WEAR_OK;
                    if (disk->cpqIdeAtaDiskSSDPercntEndrnceUsed >= 95)
                        disk->cpqIdeAtaDiskSSDWearStatus = 
                                                        SSD_WEAR_5PERCENT_LEFT;
                    if (disk->cpqIdeAtaDiskSSDPercntEndrnceUsed >= 98)
                        disk->cpqIdeAtaDiskSSDWearStatus = 
                                                        SSD_WEAR_2PERCENT_LEFT;
                    if (disk->cpqIdeAtaDiskSSDPercntEndrnceUsed >= 100)
                        disk->cpqIdeAtaDiskSSDWearStatus = SSD_WEAR_OUT;
                } else {
                    disk->cpqIdeAtaDiskMediaType = PHYS_DRV_ROTATING_PLATTERS;
                    disk->cpqIdeAtaDiskSSDPercntEndrnceUsed = 
                                                    get_sata_ssd_wear(disk_fd);
                    disk->cpqIdeAtaDiskSSDWearStatus = SSD_WEAR_OTHER;
                } 

                if ((Health = get_sata_health(disk_fd)) >= 0) {
                    disk->cpqIdeAtaDiskSmartEnabled = 2;
                    if (Health == 0) {
                        disk->cpqIdeAtaDiskStatus = 2; /* OK */
                        disk->cpqIdeAtaDiskCondition = 2; /* OK */
                    } else {
                        disk->cpqIdeAtaDiskStatus = 3; /* predictive failure */
                        disk->cpqIdeAtaDiskCondition = 3; /*  Degraded*/
                    }
                } else {
                    disk->cpqIdeAtaDiskSmartEnabled = 1;
                    disk->cpqIdeAtaDiskStatus = 1; /* Other */
                    disk->cpqIdeAtaDiskCondition = 1; /* Other */
                }
                close(disk_fd);
            }
            disk->cpqIdeAtaDiskChannel = 4; /* Serial */
            disk->cpqIdeAtaDiskTransferMode = 1;
            disk->cpqIdeAtaDiskLogicalDriveMember = 1;
            disk->cpqIdeAtaDiskIsSpare = 1;
            disk->cpqIdeAtaDiskType = 3;  /* Only SATA drives */
            disk->cpqIdeAtaDiskSataVersion = 3; /*assume version 2 */
            disk->cpqIdeAtaDiskNumber = 4 + Index;

            disk->cpqIdeAtaDiskCondition = 
                            MAKE_CONDITION(disk->cpqIdeAtaDiskCondition, 
                                           disk->cpqIdeAtaDiskStatus);
            cntlr->cpqIdeControllerOverallCondition =
                MAKE_CONDITION(cntlr->cpqIdeControllerOverallCondition,
                               disk->cpqIdeAtaDiskStatus);
            if (old == NULL) {
                rc = CONTAINER_INSERT(container, disk);
                DEBUGMSGTL(("idedisk:container:load", "entry inserted\n"));
            }
            if (((old == NULL) || (disk->OldStatus == 2)) &&
                    (disk->cpqIdeAtaDiskStatus == 3)){
                SendIdeTrap(IDE_TRAP_DISK_STATUS_CHANGE, disk);
                DEBUGMSGTL(("idedisk:container:load", "Sending Trap\n"));
            }
            free(ScsiDisklist[j]);
            SataDiskCondition |=  (1 < (disk->cpqIdeAtaDiskCondition -1));
        }
        free(ScsiDisklist);
        cntlr = ITERATOR_NEXT( it );
    }
    ITERATOR_RELEASE( it );

    if ((SataDiskCondition & (1 < (CPQ_REG_DEGRADED -1))) != 0)
        cpqHoMibHealthStatusArray[CPQMIBHEALTHINDEX] = CPQ_REG_DEGRADED;

    DEBUGMSGTL(("idedisk:container", "init\n"));
    return 1;
}