Blame snmplib/default_store.c

Packit fcad23
/*
Packit fcad23
 * default_store.c: storage space for defaults 
Packit fcad23
 */
Packit fcad23
/* Portions of this file are subject to the following copyright(s).  See
Packit fcad23
 * the Net-SNMP's COPYING file for more details and other copyrights
Packit fcad23
 * that may apply:
Packit fcad23
 */
Packit fcad23
/*
Packit fcad23
 * Portions of this file are copyrighted by:
Packit fcad23
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
Packit fcad23
 * Use is subject to license terms specified in the COPYING file
Packit fcad23
 * distributed with the Net-SNMP package.
Packit fcad23
 */
Packit fcad23
/** @defgroup default_store storage space for defaults 
Packit fcad23
 *  @ingroup library
Packit fcad23
 *
Packit fcad23
       The purpose of the default storage is three-fold:
Packit fcad23
Packit fcad23
       1)     To create a global storage space without creating a
Packit fcad23
              whole  bunch  of globally accessible variables or a
Packit fcad23
              whole bunch of access functions to work  with  more
Packit fcad23
              privately restricted variables.
Packit fcad23
Packit fcad23
       2)     To provide a single location where the thread lock-
Packit fcad23
              ing needs to be implemented. At the  time  of  this
Packit fcad23
              writing,  however,  thread  locking  is  not yet in
Packit fcad23
              place.
Packit fcad23
Packit fcad23
       3)     To reduce the number of cross dependencies  between
Packit fcad23
              code  pieces that may or may not be linked together
Packit fcad23
              in the long run. This provides for a  single  loca-
Packit fcad23
              tion  in which configuration data, for example, can
Packit fcad23
              be stored for a separate section of code  that  may
Packit fcad23
              not be linked in to the application in question.
Packit fcad23
Packit fcad23
       The functions defined here implement these goals.
Packit fcad23
Packit fcad23
       Currently, three data types are supported: booleans, inte-
Packit fcad23
       gers, and strings. Each of these data types have  separate
Packit fcad23
       storage  spaces.  In  addition, the storage space for each
Packit fcad23
       data type is divided further  by  the  application  level.
Packit fcad23
       Currently,  there  are  two  storage  spaces. The first is
Packit fcad23
       reserved for  the  SNMP  library  itself.  The  second  is
Packit fcad23
       intended  for  use  in applications and is not modified or
Packit fcad23
       checked by the library, and, therefore, this is the  space
Packit fcad23
       usable by you.
Packit fcad23
           
Packit fcad23
       These definitions correspond with the "storid" argument to the API
Packit fcad23
       - \#define NETSNMP_DS_LIBRARY_ID     0
Packit fcad23
       - \#define NETSNMP_DS_APPLICATION_ID 1
Packit fcad23
       - \#define NETSNMP_DS_TOKEN_ID       2
Packit fcad23
Packit fcad23
       These definitions correspond with the "which" argument to the API,
Packit fcad23
       when the storeid argument is NETSNMP_DS_LIBRARY_ID
Packit fcad23
Packit fcad23
       library booleans
Packit fcad23
            
Packit fcad23
       - \#define NETSNMP_DS_LIB_MIB_ERRORS          0
Packit fcad23
       - \#define NETSNMP_DS_LIB_SAVE_MIB_DESCRS     1
Packit fcad23
       - \#define NETSNMP_DS_LIB_MIB_COMMENT_TERM    2
Packit fcad23
       - \#define NETSNMP_DS_LIB_MIB_PARSE_LABEL     3
Packit fcad23
       - \#define NETSNMP_DS_LIB_DUMP_PACKET         4
Packit fcad23
       - \#define NETSNMP_DS_LIB_LOG_TIMESTAMP       5
Packit fcad23
       - \#define NETSNMP_DS_LIB_DONT_READ_CONFIGS   6
Packit fcad23
       - \#define NETSNMP_DS_LIB_MIB_REPLACE         7 replace objects from latest module 
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM  8 print only numeric enum values
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS  9 print only numeric enum values 
Packit fcad23
       - \#define NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS 10 dont print oid indexes specially 
Packit fcad23
       - \#define NETSNMP_DS_LIB_ALARM_DONT_USE_SIG  11 don't use the alarm() signal 
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRINT_FULL_OID      12 print fully qualified oids 
Packit fcad23
       - \#define NETSNMP_DS_LIB_QUICK_PRINT         13 print very brief output for parsing
Packit fcad23
       - \#define NETSNMP_DS_LIB_RANDOM_ACCESS       14 random access to oid labels
Packit fcad23
       - \#define NETSNMP_DS_LIB_REGEX_ACCESS        15 regex matching to oid labels
Packit fcad23
       - \#define NETSNMP_DS_LIB_DONT_CHECK_RANGE    16 don't check values for ranges on send
Packit fcad23
       - \#define NETSNMP_DS_LIB_NO_TOKEN_WARNINGS   17 no warn about unknown config tokens
Packit fcad23
       - \#define NETSNMP_DS_LIB_NUMERIC_TIMETICKS   18 print timeticks as a number 
Packit fcad23
       - \#define NETSNMP_DS_LIB_ESCAPE_QUOTES       19 shell escape quote marks in oids
Packit fcad23
       - \#define NETSNMP_DS_LIB_REVERSE_ENCODE      20 encode packets from back to front
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRINT_BARE_VALUE    21 just print value (not OID = value)
Packit fcad23
       - \#define NETSNMP_DS_LIB_EXTENDED_INDEX      22 print extended index format [x1][x2]
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRINT_HEX_TEXT      23 print ASCII text along with hex strings
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRINT_UCD_STYLE_OID 24 print OID's using the UCD-style prefix suppression
Packit fcad23
       - \#define NETSNMP_DS_LIB_READ_UCD_STYLE_OID  25 require top-level OIDs to be prefixed with a dot
Packit fcad23
       - \#define NETSNMP_DS_LIB_HAVE_READ_PREMIB_CONFIG 26 have the pre-mib parsing config tokens been processed
Packit fcad23
       - \#define NETSNMP_DS_LIB_HAVE_READ_CONFIG    27 have the config tokens been processed
Packit fcad23
       - \#define NETSNMP_DS_LIB_QUICKE_PRINT        28
Packit fcad23
       - \#define NETSNMP_DS_LIB_DONT_PRINT_UNITS    29 don't print UNITS suffix
Packit fcad23
       - \#define NETSNMP_DS_LIB_NO_DISPLAY_HINT     30 don't apply DISPLAY-HINTs
Packit fcad23
       - \#define NETSNMP_DS_LIB_16BIT_IDS           31 restrict requestIDs, etc to 16-bit values
Packit fcad23
       - \#define NETSNMP_DS_LIB_DONT_PERSIST_STATE  32 don't save/load any persistant state
Packit fcad23
       - \#define NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT   33 print a leading 0 on hex values <= 'f'
Packit fcad23
Packit fcad23
Packit fcad23
       library integers
Packit fcad23
Packit fcad23
       - \#define NETSNMP_DS_LIB_MIB_WARNINGS  0
Packit fcad23
       - \#define NETSNMP_DS_LIB_SECLEVEL      1
Packit fcad23
       - \#define NETSNMP_DS_LIB_SNMPVERSION   2
Packit fcad23
       - \#define NETSNMP_DS_LIB_DEFAULT_PORT  3
Packit fcad23
       - \#define NETSNMP_DS_LIB_OID_OUTPUT_FORMAT  4
Packit fcad23
       - \#define NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT 5
Packit fcad23
Packit fcad23
       library strings
Packit fcad23
Packit fcad23
       - \#define NETSNMP_DS_LIB_SECNAME           0
Packit fcad23
       - \#define NETSNMP_DS_LIB_CONTEXT           1
Packit fcad23
       - \#define NETSNMP_DS_LIB_PASSPHRASE        2
Packit fcad23
       - \#define NETSNMP_DS_LIB_AUTHPASSPHRASE    3
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRIVPASSPHRASE    4
Packit fcad23
       - \#define NETSNMP_DS_LIB_OPTIONALCONFIG    5
Packit fcad23
       - \#define NETSNMP_DS_LIB_APPTYPE           6
Packit fcad23
       - \#define NETSNMP_DS_LIB_COMMUNITY         7
Packit fcad23
       - \#define NETSNMP_DS_LIB_PERSISTENT_DIR    8
Packit fcad23
       - \#define NETSNMP_DS_LIB_CONFIGURATION_DIR 9
Packit fcad23
       - \#define NETSNMP_DS_LIB_SECMODEL          10
Packit fcad23
       - \#define NETSNMP_DS_LIB_MIBDIRS           11
Packit fcad23
       - \#define NETSNMP_DS_LIB_OIDSUFFIX         12
Packit fcad23
       - \#define NETSNMP_DS_LIB_OIDPREFIX         13
Packit fcad23
       - \#define NETSNMP_DS_LIB_CLIENT_ADDR       14
Packit fcad23
       - \#define NETSNMP_DS_LIB_TEMP_FILE_PATTERN 15
Packit fcad23
       - \#define NETSNMP_DS_LIB_AUTHMASTERKEY     16
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRIVMASTERKEY     17
Packit fcad23
       - \#define NETSNMP_DS_LIB_AUTHLOCALIZEDKEY  18
Packit fcad23
       - \#define NETSNMP_DS_LIB_PRIVLOCALIZEDKEY  19
Packit fcad23
Packit fcad23
 *  @{
Packit fcad23
 */
Packit fcad23
#include <net-snmp/net-snmp-config.h>
Packit fcad23
#include <net-snmp/net-snmp-features.h>
Packit fcad23
#include <sys/types.h>
Packit fcad23
#if HAVE_STDLIB_H
Packit fcad23
#include <stdlib.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_NETINET_IN_H
Packit fcad23
#include <netinet/in.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_STDLIB_H
Packit fcad23
#include <stdlib.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_STRING_H
Packit fcad23
#include <string.h>
Packit fcad23
#else
Packit fcad23
#include <strings.h>
Packit fcad23
#endif
Packit fcad23
Packit fcad23
#if HAVE_UNISTD_H
Packit fcad23
#include <unistd.h>
Packit fcad23
#endif
Packit fcad23
#if HAVE_DMALLOC_H
Packit fcad23
#include <dmalloc.h>
Packit fcad23
#endif
Packit fcad23
Packit fcad23
#include <net-snmp/types.h>
Packit fcad23
#include <net-snmp/output_api.h>
Packit fcad23
#include <net-snmp/config_api.h>
Packit fcad23
#include <net-snmp/library/default_store.h>    /* for "internal" definitions */
Packit fcad23
#include <net-snmp/utilities.h>
Packit fcad23
Packit fcad23
#include <net-snmp/library/snmp_api.h>
Packit fcad23
Packit fcad23
netsnmp_feature_child_of(default_store_all, libnetsnmp)
Packit fcad23
Packit fcad23
netsnmp_feature_child_of(default_store_void, default_store_all)
Packit fcad23
Packit fcad23
#ifndef NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID
Packit fcad23
#endif /* NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID */
Packit fcad23
Packit fcad23
Packit fcad23
static const char * stores [NETSNMP_DS_MAX_IDS] = { "LIB", "APP", "TOK" };
Packit fcad23
Packit fcad23
typedef struct netsnmp_ds_read_config_s {
Packit fcad23
  u_char          type;
Packit fcad23
  char           *token;
Packit fcad23
  char           *ftype;
Packit fcad23
  int             storeid;
Packit fcad23
  int             which;
Packit fcad23
  struct netsnmp_ds_read_config_s *next;
Packit fcad23
} netsnmp_ds_read_config;
Packit fcad23
Packit fcad23
static netsnmp_ds_read_config *netsnmp_ds_configs = NULL;
Packit fcad23
Packit fcad23
static int   netsnmp_ds_integers[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
Packit fcad23
static char  netsnmp_ds_booleans[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS/8];
Packit fcad23
static char *netsnmp_ds_strings[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
Packit fcad23
#ifndef NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID
Packit fcad23
static void *netsnmp_ds_voids[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
Packit fcad23
#endif /* NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID */
Packit fcad23
Packit fcad23
/**
Packit fcad23
 * Stores "true" or "false" given an int value for value into
Packit fcad23
 * netsnmp_ds_booleans[store][which] slot.  
Packit fcad23
 *
Packit fcad23
 * @param storeid an index to the boolean storage container's first index(store)
Packit fcad23
 *
Packit fcad23
 * @param which an index to the boolean storage container's second index(which)
Packit fcad23
 *
Packit fcad23
 * @param value if > 0, "true" is set into the slot otherwise "false"
Packit fcad23
 *
Packit fcad23
 * @return Returns SNMPPERR_GENERR if the storeid and which parameters do not
Packit fcad23
 * correspond to a valid slot, or  SNMPERR_SUCCESS otherwise.
Packit fcad23
 */
Packit fcad23
int
Packit fcad23
netsnmp_ds_set_boolean(int storeid, int which, int value)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("netsnmp_ds_set_boolean", "Setting %s:%d = %d/%s\n",
Packit fcad23
                stores[storeid], which, value, ((value) ? "True" : "False")));
Packit fcad23
Packit fcad23
    if (value > 0) {
Packit fcad23
        netsnmp_ds_booleans[storeid][which/8] |= (1 << (which % 8));
Packit fcad23
    } else {
Packit fcad23
        netsnmp_ds_booleans[storeid][which/8] &= (0xff7f >> (7 - (which % 8)));
Packit fcad23
    }
Packit fcad23
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_toggle_boolean(int storeid, int which)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    if ((netsnmp_ds_booleans[storeid][which/8] & (1 << (which % 8))) == 0) {
Packit fcad23
        netsnmp_ds_booleans[storeid][which/8] |= (1 << (which % 8));
Packit fcad23
    } else {
Packit fcad23
        netsnmp_ds_booleans[storeid][which/8] &= (0xff7f >> (7 - (which % 8)));
Packit fcad23
    }
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("netsnmp_ds_toggle_boolean", "Setting %s:%d = %d/%s\n",
Packit fcad23
                stores[storeid], which, netsnmp_ds_booleans[storeid][which/8],
Packit fcad23
                ((netsnmp_ds_booleans[storeid][which/8]) ? "True" : "False")));
Packit fcad23
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_get_boolean(int storeid, int which)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    return (netsnmp_ds_booleans[storeid][which/8] & (1 << (which % 8))) ? 1:0;
Packit fcad23
}
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_set_int(int storeid, int which, int value)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("netsnmp_ds_set_int", "Setting %s:%d = %d\n",
Packit fcad23
                stores[storeid], which, value));
Packit fcad23
Packit fcad23
    netsnmp_ds_integers[storeid][which] = value;
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_get_int(int storeid, int which)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    return netsnmp_ds_integers[storeid][which];
Packit fcad23
}
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_set_string(int storeid, int which, const char *value)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("netsnmp_ds_set_string", "Setting %s:%d = \"%s\"\n",
Packit fcad23
                stores[storeid], which, (value ? value : "(null)")));
Packit fcad23
Packit fcad23
    /*
Packit fcad23
     * is some silly person is calling us with our own pointer?
Packit fcad23
     */
Packit fcad23
    if (netsnmp_ds_strings[storeid][which] == value)
Packit fcad23
        return SNMPERR_SUCCESS;
Packit fcad23
Packit fcad23
    snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSION);
Packit fcad23
    if (netsnmp_ds_strings[storeid][which] != NULL) {
Packit fcad23
        free(netsnmp_ds_strings[storeid][which]);
Packit fcad23
	netsnmp_ds_strings[storeid][which] = NULL;
Packit fcad23
    }
Packit fcad23
    snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_SESSION);
Packit fcad23
Packit fcad23
    if (value) {
Packit fcad23
        netsnmp_ds_strings[storeid][which] = strdup(value);
Packit fcad23
    } else {
Packit fcad23
        netsnmp_ds_strings[storeid][which] = NULL;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
char *
Packit fcad23
netsnmp_ds_get_string(int storeid, int which)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return NULL;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    return netsnmp_ds_strings[storeid][which];
Packit fcad23
}
Packit fcad23
Packit fcad23
#ifndef NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID
Packit fcad23
int
Packit fcad23
netsnmp_ds_set_void(int storeid, int which, void *value)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("netsnmp_ds_set_void", "Setting %s:%d = %p\n",
Packit fcad23
                stores[storeid], which, value));
Packit fcad23
Packit fcad23
    netsnmp_ds_voids[storeid][which] = value;
Packit fcad23
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
void *
Packit fcad23
netsnmp_ds_get_void(int storeid, int which)
Packit fcad23
{
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS) {
Packit fcad23
        return NULL;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    return netsnmp_ds_voids[storeid][which];
Packit fcad23
}
Packit fcad23
#endif /* NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID */
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_parse_boolean(char *line)
Packit fcad23
{
Packit fcad23
    char           *value, *endptr;
Packit fcad23
    int             itmp;
Packit fcad23
    char           *st;
Packit fcad23
Packit fcad23
    value = strtok_r(line, " \t\n", &st);
Packit fcad23
    if (strcasecmp(value, "yes") == 0 || 
Packit fcad23
	strcasecmp(value, "true") == 0) {
Packit fcad23
        return 1;
Packit fcad23
    } else if (strcasecmp(value, "no") == 0 ||
Packit fcad23
	       strcasecmp(value, "false") == 0) {
Packit fcad23
        return 0;
Packit fcad23
    } else {
Packit fcad23
        itmp = strtol(value, &endptr, 10);
Packit fcad23
        if (*endptr != 0 || itmp < 0 || itmp > 1) {
Packit fcad23
            config_perror("Should be yes|no|true|false|0|1");
Packit fcad23
            return -1;
Packit fcad23
	}
Packit fcad23
        return itmp;
Packit fcad23
    }
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
netsnmp_ds_handle_config(const char *token, char *line)
Packit fcad23
{
Packit fcad23
    netsnmp_ds_read_config *drsp;
Packit fcad23
    char            buf[SNMP_MAXBUF];
Packit fcad23
    char           *value, *endptr;
Packit fcad23
    int             itmp;
Packit fcad23
    char           *st;
Packit fcad23
Packit fcad23
    DEBUGMSGTL(("netsnmp_ds_handle_config", "handling %s\n", token));
Packit fcad23
Packit fcad23
    for (drsp = netsnmp_ds_configs;
Packit fcad23
         drsp != NULL && strcasecmp(token, drsp->token) != 0;
Packit fcad23
         drsp = drsp->next);
Packit fcad23
Packit fcad23
    if (drsp != NULL) {
Packit fcad23
        DEBUGMSGTL(("netsnmp_ds_handle_config",
Packit fcad23
                    "setting: token=%s, type=%d, id=%s, which=%d\n",
Packit fcad23
                    drsp->token, drsp->type, stores[drsp->storeid],
Packit fcad23
                    drsp->which));
Packit fcad23
Packit fcad23
        switch (drsp->type) {
Packit fcad23
        case ASN_BOOLEAN:
Packit fcad23
            itmp = netsnmp_ds_parse_boolean(line);
Packit fcad23
            if ( itmp != -1 )
Packit fcad23
                netsnmp_ds_set_boolean(drsp->storeid, drsp->which, itmp);
Packit fcad23
            DEBUGMSGTL(("netsnmp_ds_handle_config", "bool: %d\n", itmp));
Packit fcad23
            break;
Packit fcad23
Packit fcad23
        case ASN_INTEGER:
Packit fcad23
            value = strtok_r(line, " \t\n", &st);
Packit fcad23
            itmp = strtol(value, &endptr, 10);
Packit fcad23
            if (*endptr != 0) {
Packit fcad23
                config_perror("Bad integer value");
Packit fcad23
	    } else {
Packit fcad23
                netsnmp_ds_set_int(drsp->storeid, drsp->which, itmp);
Packit fcad23
	    }
Packit fcad23
            DEBUGMSGTL(("netsnmp_ds_handle_config", "int: %d\n", itmp));
Packit fcad23
            break;
Packit fcad23
Packit fcad23
        case ASN_OCTET_STR:
Packit fcad23
            if (*line == '"') {
Packit fcad23
                copy_nword(line, buf, sizeof(buf));
Packit fcad23
                netsnmp_ds_set_string(drsp->storeid, drsp->which, buf);
Packit fcad23
            } else {
Packit fcad23
                netsnmp_ds_set_string(drsp->storeid, drsp->which, line);
Packit fcad23
            }
Packit fcad23
            DEBUGMSGTL(("netsnmp_ds_handle_config", "string: %s\n", line));
Packit fcad23
            break;
Packit fcad23
Packit fcad23
        default:
Packit fcad23
            snmp_log(LOG_ERR, "netsnmp_ds_handle_config: type %d (0x%02x)\n",
Packit fcad23
                     drsp->type, drsp->type);
Packit fcad23
            break;
Packit fcad23
        }
Packit fcad23
    } else {
Packit fcad23
        snmp_log(LOG_ERR, "netsnmp_ds_handle_config: no registration for %s\n",
Packit fcad23
                 token);
Packit fcad23
    }
Packit fcad23
}
Packit fcad23
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_register_config(u_char type, const char *ftype, const char *token,
Packit fcad23
			   int storeid, int which)
Packit fcad23
{
Packit fcad23
    netsnmp_ds_read_config *drsp;
Packit fcad23
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS    || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS || token == NULL) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    if (netsnmp_ds_configs == NULL) {
Packit fcad23
        netsnmp_ds_configs = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
Packit fcad23
        if (netsnmp_ds_configs == NULL)
Packit fcad23
            return SNMPERR_GENERR;
Packit fcad23
        drsp = netsnmp_ds_configs;
Packit fcad23
    } else {
Packit fcad23
        for (drsp = netsnmp_ds_configs; drsp->next != NULL; drsp = drsp->next);
Packit fcad23
        drsp->next = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
Packit fcad23
        if (drsp->next == NULL)
Packit fcad23
            return SNMPERR_GENERR;
Packit fcad23
        drsp = drsp->next;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    drsp->type    = type;
Packit fcad23
    drsp->ftype   = strdup(ftype);
Packit fcad23
    drsp->token   = strdup(token);
Packit fcad23
    drsp->storeid = storeid;
Packit fcad23
    drsp->which   = which;
Packit fcad23
Packit fcad23
    switch (type) {
Packit fcad23
    case ASN_BOOLEAN:
Packit fcad23
        register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
Packit fcad23
                                "(1|yes|true|0|no|false)");
Packit fcad23
        break;
Packit fcad23
Packit fcad23
    case ASN_INTEGER:
Packit fcad23
        register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
Packit fcad23
                                "integerValue");
Packit fcad23
        break;
Packit fcad23
Packit fcad23
    case ASN_OCTET_STR:
Packit fcad23
        register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
Packit fcad23
                                "string");
Packit fcad23
        break;
Packit fcad23
Packit fcad23
    }
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
int
Packit fcad23
netsnmp_ds_register_premib(u_char type, const char *ftype, const char *token,
Packit fcad23
			   int storeid, int which)
Packit fcad23
{
Packit fcad23
    netsnmp_ds_read_config *drsp;
Packit fcad23
Packit fcad23
    if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS    || 
Packit fcad23
	which   < 0 || which   >= NETSNMP_DS_MAX_SUBIDS || token == NULL) {
Packit fcad23
        return SNMPERR_GENERR;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    if (netsnmp_ds_configs == NULL) {
Packit fcad23
        netsnmp_ds_configs = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
Packit fcad23
        if (netsnmp_ds_configs == NULL)
Packit fcad23
            return SNMPERR_GENERR;
Packit fcad23
        drsp = netsnmp_ds_configs;
Packit fcad23
    } else {
Packit fcad23
        for (drsp = netsnmp_ds_configs; drsp->next != NULL; drsp = drsp->next);
Packit fcad23
        drsp->next = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
Packit fcad23
        if (drsp->next == NULL)
Packit fcad23
            return SNMPERR_GENERR;
Packit fcad23
        drsp = drsp->next;
Packit fcad23
    }
Packit fcad23
Packit fcad23
    drsp->type    = type;
Packit fcad23
    drsp->ftype   = strdup(ftype);
Packit fcad23
    drsp->token   = strdup(token);
Packit fcad23
    drsp->storeid = storeid;
Packit fcad23
    drsp->which   = which;
Packit fcad23
Packit fcad23
    switch (type) {
Packit fcad23
    case ASN_BOOLEAN:
Packit fcad23
        register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
Packit fcad23
                                        NULL, "(1|yes|true|0|no|false)");
Packit fcad23
        break;
Packit fcad23
Packit fcad23
    case ASN_INTEGER:
Packit fcad23
        register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
Packit fcad23
                                        NULL, "integerValue");
Packit fcad23
        break;
Packit fcad23
Packit fcad23
    case ASN_OCTET_STR:
Packit fcad23
        register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
Packit fcad23
                                        NULL, "string");
Packit fcad23
        break;
Packit fcad23
Packit fcad23
    }
Packit fcad23
    return SNMPERR_SUCCESS;
Packit fcad23
}
Packit fcad23
Packit fcad23
void
Packit fcad23
netsnmp_ds_shutdown(void)
Packit fcad23
{
Packit fcad23
    netsnmp_ds_read_config *drsp;
Packit fcad23
    int             i, j;
Packit fcad23
Packit fcad23
    snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSION);
Packit fcad23
    for (drsp = netsnmp_ds_configs; drsp; drsp = netsnmp_ds_configs) {
Packit fcad23
        netsnmp_ds_configs = drsp->next;
Packit fcad23
Packit fcad23
        if (drsp->ftype && drsp->token) {
Packit fcad23
            unregister_config_handler(drsp->ftype, drsp->token);
Packit fcad23
        }
Packit fcad23
	if (drsp->ftype != NULL) {
Packit fcad23
	    free(drsp->ftype);
Packit fcad23
	}
Packit fcad23
	if (drsp->token != NULL) {
Packit fcad23
	    free(drsp->token);
Packit fcad23
	}
Packit fcad23
        free(drsp);
Packit fcad23
    }
Packit fcad23
Packit fcad23
    for (i = 0; i < NETSNMP_DS_MAX_IDS; i++) {
Packit fcad23
        for (j = 0; j < NETSNMP_DS_MAX_SUBIDS; j++) {
Packit fcad23
            if (netsnmp_ds_strings[i][j] != NULL) {
Packit fcad23
                free(netsnmp_ds_strings[i][j]);
Packit fcad23
                netsnmp_ds_strings[i][j] = NULL;
Packit fcad23
            }
Packit fcad23
        }
Packit fcad23
    }
Packit fcad23
    snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_SESSION);
Packit fcad23
}
Packit fcad23
/**  @} */