1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/*
* default_store.c: storage space for defaults
*/
/* Portions of this file are subject to the following copyright(s). See
* the Net-SNMP's COPYING file for more details and other copyrights
* that may apply:
*/
/*
* Portions of this file are copyrighted by:
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*/
/** @defgroup default_store storage space for defaults
* @ingroup library
*
The purpose of the default storage is three-fold:
1) To create a global storage space without creating a
whole bunch of globally accessible variables or a
whole bunch of access functions to work with more
privately restricted variables.
2) To provide a single location where the thread lock-
ing needs to be implemented. At the time of this
writing, however, thread locking is not yet in
place.
3) To reduce the number of cross dependencies between
code pieces that may or may not be linked together
in the long run. This provides for a single loca-
tion in which configuration data, for example, can
be stored for a separate section of code that may
not be linked in to the application in question.
The functions defined here implement these goals.
Currently, three data types are supported: booleans, inte-
gers, and strings. Each of these data types have separate
storage spaces. In addition, the storage space for each
data type is divided further by the application level.
Currently, there are two storage spaces. The first is
reserved for the SNMP library itself. The second is
intended for use in applications and is not modified or
checked by the library, and, therefore, this is the space
usable by you.
These definitions correspond with the "storid" argument to the API
- \#define NETSNMP_DS_LIBRARY_ID 0
- \#define NETSNMP_DS_APPLICATION_ID 1
- \#define NETSNMP_DS_TOKEN_ID 2
These definitions correspond with the "which" argument to the API,
when the storeid argument is NETSNMP_DS_LIBRARY_ID
library booleans
- \#define NETSNMP_DS_LIB_MIB_ERRORS 0
- \#define NETSNMP_DS_LIB_SAVE_MIB_DESCRS 1
- \#define NETSNMP_DS_LIB_MIB_COMMENT_TERM 2
- \#define NETSNMP_DS_LIB_MIB_PARSE_LABEL 3
- \#define NETSNMP_DS_LIB_DUMP_PACKET 4
- \#define NETSNMP_DS_LIB_LOG_TIMESTAMP 5
- \#define NETSNMP_DS_LIB_DONT_READ_CONFIGS 6
- \#define NETSNMP_DS_LIB_MIB_REPLACE 7 replace objects from latest module
- \#define NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM 8 print only numeric enum values
- \#define NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS 9 print only numeric enum values
- \#define NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS 10 dont print oid indexes specially
- \#define NETSNMP_DS_LIB_ALARM_DONT_USE_SIG 11 don't use the alarm() signal
- \#define NETSNMP_DS_LIB_PRINT_FULL_OID 12 print fully qualified oids
- \#define NETSNMP_DS_LIB_QUICK_PRINT 13 print very brief output for parsing
- \#define NETSNMP_DS_LIB_RANDOM_ACCESS 14 random access to oid labels
- \#define NETSNMP_DS_LIB_REGEX_ACCESS 15 regex matching to oid labels
- \#define NETSNMP_DS_LIB_DONT_CHECK_RANGE 16 don't check values for ranges on send
- \#define NETSNMP_DS_LIB_NO_TOKEN_WARNINGS 17 no warn about unknown config tokens
- \#define NETSNMP_DS_LIB_NUMERIC_TIMETICKS 18 print timeticks as a number
- \#define NETSNMP_DS_LIB_ESCAPE_QUOTES 19 shell escape quote marks in oids
- \#define NETSNMP_DS_LIB_REVERSE_ENCODE 20 encode packets from back to front
- \#define NETSNMP_DS_LIB_PRINT_BARE_VALUE 21 just print value (not OID = value)
- \#define NETSNMP_DS_LIB_EXTENDED_INDEX 22 print extended index format [x1][x2]
- \#define NETSNMP_DS_LIB_PRINT_HEX_TEXT 23 print ASCII text along with hex strings
- \#define NETSNMP_DS_LIB_PRINT_UCD_STYLE_OID 24 print OID's using the UCD-style prefix suppression
- \#define NETSNMP_DS_LIB_READ_UCD_STYLE_OID 25 require top-level OIDs to be prefixed with a dot
- \#define NETSNMP_DS_LIB_HAVE_READ_PREMIB_CONFIG 26 have the pre-mib parsing config tokens been processed
- \#define NETSNMP_DS_LIB_HAVE_READ_CONFIG 27 have the config tokens been processed
- \#define NETSNMP_DS_LIB_QUICKE_PRINT 28
- \#define NETSNMP_DS_LIB_DONT_PRINT_UNITS 29 don't print UNITS suffix
- \#define NETSNMP_DS_LIB_NO_DISPLAY_HINT 30 don't apply DISPLAY-HINTs
- \#define NETSNMP_DS_LIB_16BIT_IDS 31 restrict requestIDs, etc to 16-bit values
- \#define NETSNMP_DS_LIB_DONT_PERSIST_STATE 32 don't save/load any persistant state
- \#define NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT 33 print a leading 0 on hex values <= 'f'
library integers
- \#define NETSNMP_DS_LIB_MIB_WARNINGS 0
- \#define NETSNMP_DS_LIB_SECLEVEL 1
- \#define NETSNMP_DS_LIB_SNMPVERSION 2
- \#define NETSNMP_DS_LIB_DEFAULT_PORT 3
- \#define NETSNMP_DS_LIB_OID_OUTPUT_FORMAT 4
- \#define NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT 5
library strings
- \#define NETSNMP_DS_LIB_SECNAME 0
- \#define NETSNMP_DS_LIB_CONTEXT 1
- \#define NETSNMP_DS_LIB_PASSPHRASE 2
- \#define NETSNMP_DS_LIB_AUTHPASSPHRASE 3
- \#define NETSNMP_DS_LIB_PRIVPASSPHRASE 4
- \#define NETSNMP_DS_LIB_OPTIONALCONFIG 5
- \#define NETSNMP_DS_LIB_APPTYPE 6
- \#define NETSNMP_DS_LIB_COMMUNITY 7
- \#define NETSNMP_DS_LIB_PERSISTENT_DIR 8
- \#define NETSNMP_DS_LIB_CONFIGURATION_DIR 9
- \#define NETSNMP_DS_LIB_SECMODEL 10
- \#define NETSNMP_DS_LIB_MIBDIRS 11
- \#define NETSNMP_DS_LIB_OIDSUFFIX 12
- \#define NETSNMP_DS_LIB_OIDPREFIX 13
- \#define NETSNMP_DS_LIB_CLIENT_ADDR 14
- \#define NETSNMP_DS_LIB_TEMP_FILE_PATTERN 15
- \#define NETSNMP_DS_LIB_AUTHMASTERKEY 16
- \#define NETSNMP_DS_LIB_PRIVMASTERKEY 17
- \#define NETSNMP_DS_LIB_AUTHLOCALIZEDKEY 18
- \#define NETSNMP_DS_LIB_PRIVLOCALIZEDKEY 19
* @{
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <sys/types.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <net-snmp/types.h>
#include <net-snmp/output_api.h>
#include <net-snmp/config_api.h>
#include <net-snmp/library/default_store.h> /* for "internal" definitions */
#include <net-snmp/utilities.h>
#include <net-snmp/library/snmp_api.h>
netsnmp_feature_child_of(default_store_all, libnetsnmp);
netsnmp_feature_child_of(default_store_void, default_store_all);
#ifndef NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID
#endif /* NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID */
static const char * stores [NETSNMP_DS_MAX_IDS] = { "LIB", "APP", "TOK" };
typedef struct netsnmp_ds_read_config_s {
u_char type;
char *token;
char *ftype;
int storeid;
int which;
struct netsnmp_ds_read_config_s *next;
} netsnmp_ds_read_config;
static netsnmp_ds_read_config *netsnmp_ds_configs = NULL;
static int netsnmp_ds_integers[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
static char netsnmp_ds_booleans[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS/8];
static char *netsnmp_ds_strings[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
#ifndef NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID
static void *netsnmp_ds_voids[NETSNMP_DS_MAX_IDS][NETSNMP_DS_MAX_SUBIDS];
#endif /* NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID */
/**
* Stores "true" or "false" given an int value for value into
* netsnmp_ds_booleans[store][which] slot.
*
* @param storeid an index to the boolean storage container's first index(store)
*
* @param which an index to the boolean storage container's second index(which)
*
* @param value if > 0, "true" is set into the slot otherwise "false"
*
* @return Returns SNMPPERR_GENERR if the storeid and which parameters do not
* correspond to a valid slot, or SNMPERR_SUCCESS otherwise.
*/
int
netsnmp_ds_set_boolean(int storeid, int which, int value)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
DEBUGMSGTL(("netsnmp_ds_set_boolean", "Setting %s:%d = %d/%s\n",
stores[storeid], which, value, ((value) ? "True" : "False")));
if (value > 0) {
netsnmp_ds_booleans[storeid][which/8] |= (1 << (which % 8));
} else {
netsnmp_ds_booleans[storeid][which/8] &= (0xff7f >> (7 - (which % 8)));
}
return SNMPERR_SUCCESS;
}
int
netsnmp_ds_toggle_boolean(int storeid, int which)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
if ((netsnmp_ds_booleans[storeid][which/8] & (1 << (which % 8))) == 0) {
netsnmp_ds_booleans[storeid][which/8] |= (1 << (which % 8));
} else {
netsnmp_ds_booleans[storeid][which/8] &= (0xff7f >> (7 - (which % 8)));
}
DEBUGMSGTL(("netsnmp_ds_toggle_boolean", "Setting %s:%d = %d/%s\n",
stores[storeid], which, netsnmp_ds_booleans[storeid][which/8],
((netsnmp_ds_booleans[storeid][which/8]) ? "True" : "False")));
return SNMPERR_SUCCESS;
}
int
netsnmp_ds_get_boolean(int storeid, int which)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
return (netsnmp_ds_booleans[storeid][which/8] & (1 << (which % 8))) ? 1:0;
}
int
netsnmp_ds_set_int(int storeid, int which, int value)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
DEBUGMSGTL(("netsnmp_ds_set_int", "Setting %s:%d = %d\n",
stores[storeid], which, value));
netsnmp_ds_integers[storeid][which] = value;
return SNMPERR_SUCCESS;
}
int
netsnmp_ds_get_int(int storeid, int which)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
return netsnmp_ds_integers[storeid][which];
}
int
netsnmp_ds_set_string(int storeid, int which, const char *value)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
DEBUGMSGTL(("netsnmp_ds_set_string", "Setting %s:%d = \"%s\"\n",
stores[storeid], which, (value ? value : "(null)")));
/*
* is some silly person is calling us with our own pointer?
*/
if (netsnmp_ds_strings[storeid][which] == value)
return SNMPERR_SUCCESS;
snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSION);
if (netsnmp_ds_strings[storeid][which] != NULL) {
free(netsnmp_ds_strings[storeid][which]);
netsnmp_ds_strings[storeid][which] = NULL;
}
snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_SESSION);
if (value) {
netsnmp_ds_strings[storeid][which] = strdup(value);
} else {
netsnmp_ds_strings[storeid][which] = NULL;
}
return SNMPERR_SUCCESS;
}
char *
netsnmp_ds_get_string(int storeid, int which)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return NULL;
}
return netsnmp_ds_strings[storeid][which];
}
#ifndef NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID
int
netsnmp_ds_set_void(int storeid, int which, void *value)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return SNMPERR_GENERR;
}
DEBUGMSGTL(("netsnmp_ds_set_void", "Setting %s:%d = %p\n",
stores[storeid], which, value));
netsnmp_ds_voids[storeid][which] = value;
return SNMPERR_SUCCESS;
}
void *
netsnmp_ds_get_void(int storeid, int which)
{
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS) {
return NULL;
}
return netsnmp_ds_voids[storeid][which];
}
#endif /* NETSNMP_FEATURE_REMOVE_DEFAULT_STORE_VOID */
int
netsnmp_ds_parse_boolean(char *line)
{
char *value, *endptr;
int itmp;
char *st;
value = strtok_r(line, " \t\n", &st);
if (strcasecmp(value, "yes") == 0 ||
strcasecmp(value, "true") == 0) {
return 1;
} else if (strcasecmp(value, "no") == 0 ||
strcasecmp(value, "false") == 0) {
return 0;
} else {
itmp = strtol(value, &endptr, 10);
if (*endptr != 0 || itmp < 0 || itmp > 1) {
config_perror("Should be yes|no|true|false|0|1");
return -1;
}
return itmp;
}
}
void
netsnmp_ds_handle_config(const char *token, char *line)
{
netsnmp_ds_read_config *drsp;
char buf[SNMP_MAXBUF];
char *value, *endptr;
int itmp;
char *st;
DEBUGMSGTL(("netsnmp_ds_handle_config", "handling %s\n", token));
for (drsp = netsnmp_ds_configs;
drsp != NULL && strcasecmp(token, drsp->token) != 0;
drsp = drsp->next);
if (drsp != NULL) {
DEBUGMSGTL(("netsnmp_ds_handle_config",
"setting: token=%s, type=%d, id=%s, which=%d\n",
drsp->token, drsp->type, stores[drsp->storeid],
drsp->which));
switch (drsp->type) {
case ASN_BOOLEAN:
itmp = netsnmp_ds_parse_boolean(line);
if ( itmp != -1 )
netsnmp_ds_set_boolean(drsp->storeid, drsp->which, itmp);
DEBUGMSGTL(("netsnmp_ds_handle_config", "bool: %d\n", itmp));
break;
case ASN_INTEGER:
value = strtok_r(line, " \t\n", &st);
itmp = strtol(value, &endptr, 10);
if (*endptr != 0) {
config_perror("Bad integer value");
} else {
netsnmp_ds_set_int(drsp->storeid, drsp->which, itmp);
}
DEBUGMSGTL(("netsnmp_ds_handle_config", "int: %d\n", itmp));
break;
case ASN_OCTET_STR:
if (*line == '"') {
copy_nword(line, buf, sizeof(buf));
netsnmp_ds_set_string(drsp->storeid, drsp->which, buf);
} else {
netsnmp_ds_set_string(drsp->storeid, drsp->which, line);
}
DEBUGMSGTL(("netsnmp_ds_handle_config", "string: %s\n", line));
break;
default:
snmp_log(LOG_ERR, "netsnmp_ds_handle_config: type %d (0x%02x)\n",
drsp->type, drsp->type);
break;
}
} else {
snmp_log(LOG_ERR, "netsnmp_ds_handle_config: no registration for %s\n",
token);
}
}
int
netsnmp_ds_register_config(u_char type, const char *ftype, const char *token,
int storeid, int which)
{
netsnmp_ds_read_config *drsp;
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS || token == NULL) {
return SNMPERR_GENERR;
}
if (netsnmp_ds_configs == NULL) {
netsnmp_ds_configs = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
if (netsnmp_ds_configs == NULL)
return SNMPERR_GENERR;
drsp = netsnmp_ds_configs;
} else {
for (drsp = netsnmp_ds_configs; drsp->next != NULL; drsp = drsp->next);
drsp->next = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
if (drsp->next == NULL)
return SNMPERR_GENERR;
drsp = drsp->next;
}
drsp->type = type;
drsp->ftype = strdup(ftype);
drsp->token = strdup(token);
drsp->storeid = storeid;
drsp->which = which;
switch (type) {
case ASN_BOOLEAN:
register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
"(1|yes|true|0|no|false)");
break;
case ASN_INTEGER:
register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
"integerValue");
break;
case ASN_OCTET_STR:
register_config_handler(ftype, token, netsnmp_ds_handle_config, NULL,
"string");
break;
}
return SNMPERR_SUCCESS;
}
int
netsnmp_ds_register_premib(u_char type, const char *ftype, const char *token,
int storeid, int which)
{
netsnmp_ds_read_config *drsp;
if (storeid < 0 || storeid >= NETSNMP_DS_MAX_IDS ||
which < 0 || which >= NETSNMP_DS_MAX_SUBIDS || token == NULL) {
return SNMPERR_GENERR;
}
if (netsnmp_ds_configs == NULL) {
netsnmp_ds_configs = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
if (netsnmp_ds_configs == NULL)
return SNMPERR_GENERR;
drsp = netsnmp_ds_configs;
} else {
for (drsp = netsnmp_ds_configs; drsp->next != NULL; drsp = drsp->next);
drsp->next = SNMP_MALLOC_TYPEDEF(netsnmp_ds_read_config);
if (drsp->next == NULL)
return SNMPERR_GENERR;
drsp = drsp->next;
}
drsp->type = type;
drsp->ftype = strdup(ftype);
drsp->token = strdup(token);
drsp->storeid = storeid;
drsp->which = which;
switch (type) {
case ASN_BOOLEAN:
register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
NULL, "(1|yes|true|0|no|false)");
break;
case ASN_INTEGER:
register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
NULL, "integerValue");
break;
case ASN_OCTET_STR:
register_prenetsnmp_mib_handler(ftype, token, netsnmp_ds_handle_config,
NULL, "string");
break;
}
return SNMPERR_SUCCESS;
}
void
netsnmp_ds_shutdown(void)
{
netsnmp_ds_read_config *drsp;
int i, j;
snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSION);
for (drsp = netsnmp_ds_configs; drsp; drsp = netsnmp_ds_configs) {
netsnmp_ds_configs = drsp->next;
if (drsp->ftype && drsp->token) {
unregister_config_handler(drsp->ftype, drsp->token);
}
if (drsp->ftype != NULL) {
free(drsp->ftype);
}
if (drsp->token != NULL) {
free(drsp->token);
}
free(drsp);
}
for (i = 0; i < NETSNMP_DS_MAX_IDS; i++) {
for (j = 0; j < NETSNMP_DS_MAX_SUBIDS; j++) {
if (netsnmp_ds_strings[i][j] != NULL) {
free(netsnmp_ds_strings[i][j]);
netsnmp_ds_strings[i][j] = NULL;
}
}
}
snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_SESSION);
}
/** @} */
|