| 1 |
/* |
| 2 |
* Portions of this file are subject to the following copyright(s). See |
| 3 |
* the Net-SNMP's COPYING file for more details and other copyrights |
| 4 |
* that may apply: |
| 5 |
* |
| 6 |
* Portions of this file are copyrighted by: |
| 7 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 8 |
* Use is subject to license terms specified in the COPYING file |
| 9 |
* distributed with the Net-SNMP package. |
| 10 |
*/ |
| 11 |
#include <net-snmp/net-snmp-config.h> |
| 12 |
|
| 13 |
#include <net-snmp/net-snmp-includes.h> |
| 14 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 15 |
|
| 16 |
#include <net-snmp/agent/old_api.h> |
| 17 |
|
| 18 |
#ifdef HAVE_STRING_H |
| 19 |
#include <string.h> |
| 20 |
#else |
| 21 |
#include <strings.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <net-snmp/agent/agent_callbacks.h> |
| 25 |
|
| 26 |
#include <stddef.h> |
| 27 |
|
| 28 |
/* |
| 29 |
* mib clients are passed a pointer to a oid buffer. Some mib clients |
| 30 |
* * (namely, those first noticed in mibII/vacm.c) modify this oid buffer |
| 31 |
* * before they determine if they really need to send results back out |
| 32 |
* * using it. If the master agent determined that the client was not the |
| 33 |
* * right one to talk with, it will use the same oid buffer to pass to the |
| 34 |
* * rest of the clients, which may not longer be valid. This should be |
| 35 |
* * fixed in all clients rather than the master. However, its not a |
| 36 |
* * particularily easy bug to track down so this saves debugging time at |
| 37 |
* * the expense of a few memcpy's. |
| 38 |
*/ |
| 39 |
#define MIB_CLIENTS_ARE_EVIL 1 |
| 40 |
|
| 41 |
#ifdef HAVE_DMALLOC_H |
| 42 |
static void free_wrapper(void * p) |
| 43 |
{ |
| 44 |
free(p); |
| 45 |
} |
| 46 |
#else |
| 47 |
#define free_wrapper free |
| 48 |
#endif |
| 49 |
|
| 50 |
/* |
| 51 |
* don't use these! |
| 52 |
*/ |
| 53 |
void set_current_agent_session(netsnmp_agent_session *asp); |
| 54 |
|
| 55 |
/** @defgroup old_api old_api |
| 56 |
* Calls mib module code written in the old style of code. |
| 57 |
* @ingroup handler |
| 58 |
* This is a backwards compatilibity module that allows code written |
| 59 |
* in the old API to be run under the new handler based architecture. |
| 60 |
* Use it by calling netsnmp_register_old_api(). |
| 61 |
* @{ |
| 62 |
*/ |
| 63 |
|
| 64 |
/** returns a old_api handler that should be the final calling |
| 65 |
* handler. Don't use this function. Use the netsnmp_register_old_api() |
| 66 |
* function instead. |
| 67 |
*/ |
| 68 |
netsnmp_mib_handler * |
| 69 |
get_old_api_handler(void) |
| 70 |
{ |
| 71 |
return netsnmp_create_handler("old_api", netsnmp_old_api_helper); |
| 72 |
} |
| 73 |
|
| 74 |
static void * |
| 75 |
netsnmp_clone_variable(void *p) |
| 76 |
{ |
| 77 |
return netsnmp_duplicate_variable(p); |
| 78 |
} |
| 79 |
|
| 80 |
struct variable * |
| 81 |
netsnmp_duplicate_variable(const struct variable *var) |
| 82 |
{ |
| 83 |
struct variable *var2 = NULL; |
| 84 |
|
| 85 |
if (var) { |
| 86 |
const int varsize = offsetof(struct variable, name) + var->namelen * sizeof(var->name[0]); |
| 87 |
var2 = malloc(varsize); |
| 88 |
if (var2) |
| 89 |
memcpy(var2, var, varsize); |
| 90 |
} |
| 91 |
return var2; |
| 92 |
} |
| 93 |
|
| 94 |
/** Registers an old API set into the mib tree. Functionally this |
| 95 |
* mimics the old register_mib_context() function (and in fact the new |
| 96 |
* register_mib_context() function merely calls this new old_api one). |
| 97 |
*/ |
| 98 |
int |
| 99 |
netsnmp_register_old_api(const char *moduleName, |
| 100 |
const struct variable *var, |
| 101 |
size_t varsize, |
| 102 |
size_t numvars, |
| 103 |
const oid * mibloc, |
| 104 |
size_t mibloclen, |
| 105 |
int priority, |
| 106 |
int range_subid, |
| 107 |
oid range_ubound, |
| 108 |
netsnmp_session * ss, |
| 109 |
const char *context, int timeout, int flags) |
| 110 |
{ |
| 111 |
|
| 112 |
unsigned int i; |
| 113 |
int res; |
| 114 |
|
| 115 |
/* |
| 116 |
* register all subtree nodes |
| 117 |
*/ |
| 118 |
for (i = 0; i < numvars; i++) { |
| 119 |
struct variable *vp; |
| 120 |
netsnmp_handler_registration *reginfo = |
| 121 |
SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration); |
| 122 |
if (reginfo == NULL) |
| 123 |
return SNMP_ERR_GENERR; |
| 124 |
|
| 125 |
vp = netsnmp_duplicate_variable((const struct variable *) |
| 126 |
((const char *) var + varsize * i)); |
| 127 |
|
| 128 |
if (vp == NULL) { |
| 129 |
SNMP_FREE(reginfo); |
| 130 |
return SNMP_ERR_GENERR; |
| 131 |
} |
| 132 |
|
| 133 |
reginfo->handler = get_old_api_handler(); |
| 134 |
reginfo->handlerName = strdup(moduleName); |
| 135 |
reginfo->rootoid_len = (mibloclen + vp->namelen); |
| 136 |
reginfo->rootoid = |
| 137 |
(oid *) malloc(reginfo->rootoid_len * sizeof(oid)); |
| 138 |
if (NULL == reginfo->handler || NULL == reginfo->handlerName || |
| 139 |
NULL == reginfo->rootoid) { |
| 140 |
netsnmp_handler_free(reginfo->handler); |
| 141 |
SNMP_FREE(vp); |
| 142 |
SNMP_FREE(reginfo->handlerName); |
| 143 |
SNMP_FREE(reginfo->rootoid); |
| 144 |
SNMP_FREE(reginfo); |
| 145 |
return SNMP_ERR_GENERR; |
| 146 |
} |
| 147 |
|
| 148 |
memcpy(reginfo->rootoid, mibloc, mibloclen * sizeof(oid)); |
| 149 |
memcpy(reginfo->rootoid + mibloclen, vp->name, vp->namelen |
| 150 |
* sizeof(oid)); |
| 151 |
reginfo->handler->myvoid = (void *) vp; |
| 152 |
reginfo->handler->data_clone = netsnmp_clone_variable; |
| 153 |
reginfo->handler->data_free = free; |
| 154 |
|
| 155 |
reginfo->priority = priority; |
| 156 |
reginfo->range_subid = range_subid; |
| 157 |
|
| 158 |
reginfo->range_ubound = range_ubound; |
| 159 |
reginfo->timeout = timeout; |
| 160 |
reginfo->contextName = (context) ? strdup(context) : NULL; |
| 161 |
reginfo->modes = vp->acl == NETSNMP_OLDAPI_RONLY ? HANDLER_CAN_RONLY : |
| 162 |
HANDLER_CAN_RWRITE; |
| 163 |
|
| 164 |
/* |
| 165 |
* register ourselves in the mib tree |
| 166 |
*/ |
| 167 |
res = netsnmp_register_handler(reginfo); |
| 168 |
if (MIB_REGISTERED_OK != res) { |
| 169 |
/** reginfo already freed on error. */ |
| 170 |
snmp_log(LOG_WARNING, "old_api handler registration failed\n"); |
| 171 |
return res; |
| 172 |
} |
| 173 |
} |
| 174 |
return SNMPERR_SUCCESS; |
| 175 |
} |
| 176 |
|
| 177 |
/** registers a row within a mib table */ |
| 178 |
int |
| 179 |
netsnmp_register_mib_table_row(const char *moduleName, |
| 180 |
const struct variable *var, |
| 181 |
size_t varsize, |
| 182 |
size_t numvars, |
| 183 |
oid * mibloc, |
| 184 |
size_t mibloclen, |
| 185 |
int priority, |
| 186 |
int var_subid, |
| 187 |
netsnmp_session * ss, |
| 188 |
const char *context, int timeout, int flags) |
| 189 |
{ |
| 190 |
unsigned int i = 0, rc = 0; |
| 191 |
oid ubound = 0; |
| 192 |
|
| 193 |
for (i = 0; i < numvars; i++) { |
| 194 |
const struct variable *vr = |
| 195 |
(const struct variable *) ((const char *) var + (i * varsize)); |
| 196 |
netsnmp_handler_registration *r; |
| 197 |
if ( var_subid > (int)mibloclen ) { |
| 198 |
break; /* doesn't make sense */ |
| 199 |
} |
| 200 |
r = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration); |
| 201 |
|
| 202 |
if (r == NULL) { |
| 203 |
/* |
| 204 |
* Unregister whatever we have registered so far, and |
| 205 |
* return an error. |
| 206 |
*/ |
| 207 |
snmp_log(LOG_ERR, "mib table row registration failed\n"); |
| 208 |
rc = MIB_REGISTRATION_FAILED; |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
r->handler = get_old_api_handler(); |
| 213 |
r->handlerName = strdup(moduleName); |
| 214 |
r->rootoid_len = mibloclen; |
| 215 |
r->rootoid = (oid *) malloc(r->rootoid_len * sizeof(oid)); |
| 216 |
if (r->handler == NULL || r->handlerName == NULL || |
| 217 |
r->rootoid == NULL) { |
| 218 |
netsnmp_handler_registration_free(r); |
| 219 |
rc = MIB_REGISTRATION_FAILED; |
| 220 |
break; |
| 221 |
} |
| 222 |
memcpy(r->rootoid, mibloc, mibloclen * sizeof(oid)); |
| 223 |
memcpy((u_char *) (r->rootoid + (var_subid - vr->namelen)), vr->name, |
| 224 |
vr->namelen * sizeof(oid)); |
| 225 |
DEBUGMSGTL(("netsnmp_register_mib_table_row", "rootoid ")); |
| 226 |
DEBUGMSGOID(("netsnmp_register_mib_table_row", r->rootoid, |
| 227 |
r->rootoid_len)); |
| 228 |
DEBUGMSG(("netsnmp_register_mib_table_row", "(%d)\n", |
| 229 |
(var_subid - vr->namelen))); |
| 230 |
r->handler->myvoid = netsnmp_duplicate_variable(vr); |
| 231 |
r->handler->data_clone = netsnmp_clone_variable; |
| 232 |
r->handler->data_free = free; |
| 233 |
|
| 234 |
r->contextName = (context) ? strdup(context) : NULL; |
| 235 |
if (r->handler->myvoid == NULL || |
| 236 |
(context != NULL && r->contextName == NULL)) { |
| 237 |
netsnmp_handler_registration_free(r); |
| 238 |
rc = MIB_REGISTRATION_FAILED; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
r->priority = priority; |
| 243 |
r->range_subid = 0; /* var_subid; */ |
| 244 |
r->range_ubound = 0; /* range_ubound; */ |
| 245 |
r->timeout = timeout; |
| 246 |
r->modes = HANDLER_CAN_RWRITE; |
| 247 |
|
| 248 |
/* |
| 249 |
* Register this column and row |
| 250 |
*/ |
| 251 |
if ((rc = |
| 252 |
netsnmp_register_handler_nocallback(r)) != |
| 253 |
MIB_REGISTERED_OK) { |
| 254 |
snmp_log(LOG_ERR, "mib table row registration failed\n"); |
| 255 |
DEBUGMSGTL(("netsnmp_register_mib_table_row", |
| 256 |
"register failed %d\n", rc)); |
| 257 |
/** reginfo already freed */ |
| 258 |
break; |
| 259 |
} |
| 260 |
|
| 261 |
if (vr->namelen > 0) { |
| 262 |
if (vr->name[vr->namelen - 1] > ubound) { |
| 263 |
ubound = vr->name[vr->namelen - 1]; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if (rc == MIB_REGISTERED_OK) { |
| 269 |
struct register_parameters reg_parms; |
| 270 |
|
| 271 |
reg_parms.name = mibloc; |
| 272 |
reg_parms.namelen = mibloclen; |
| 273 |
reg_parms.priority = priority; |
| 274 |
reg_parms.flags = (u_char) flags; |
| 275 |
reg_parms.range_subid = var_subid; |
| 276 |
reg_parms.range_ubound = ubound; |
| 277 |
reg_parms.timeout = timeout; |
| 278 |
reg_parms.contextName = context; |
| 279 |
rc = snmp_call_callbacks(SNMP_CALLBACK_APPLICATION, |
| 280 |
SNMPD_CALLBACK_REGISTER_OID, ®_parms); |
| 281 |
} |
| 282 |
|
| 283 |
return rc; |
| 284 |
} |
| 285 |
|
| 286 |
/** implements the old_api handler */ |
| 287 |
int |
| 288 |
netsnmp_old_api_helper(netsnmp_mib_handler *handler, |
| 289 |
netsnmp_handler_registration *reginfo, |
| 290 |
netsnmp_agent_request_info *reqinfo, |
| 291 |
netsnmp_request_info *requests) |
| 292 |
{ |
| 293 |
|
| 294 |
#ifdef MIB_CLIENTS_ARE_EVIL |
| 295 |
oid save[MAX_OID_LEN]; |
| 296 |
size_t savelen = 0; |
| 297 |
#endif |
| 298 |
struct variable compat_var, *cvp = &compat_var; |
| 299 |
int exact = 1; |
| 300 |
int status; |
| 301 |
|
| 302 |
struct variable *const vp = handler->myvoid; |
| 303 |
netsnmp_old_api_cache *cacheptr; |
| 304 |
netsnmp_agent_session *oldasp = NULL; |
| 305 |
u_char *access = NULL; |
| 306 |
WriteMethod *write_method = NULL; |
| 307 |
size_t len; |
| 308 |
size_t tmp_len; |
| 309 |
oid tmp_name[MAX_OID_LEN]; |
| 310 |
|
| 311 |
/* |
| 312 |
* create old variable structure with right information |
| 313 |
*/ |
| 314 |
memcpy(cvp->name, reginfo->rootoid, |
| 315 |
reginfo->rootoid_len * sizeof(oid)); |
| 316 |
cvp->namelen = reginfo->rootoid_len; |
| 317 |
cvp->type = vp->type; |
| 318 |
cvp->magic = vp->magic; |
| 319 |
cvp->acl = vp->acl; |
| 320 |
cvp->findVar = vp->findVar; |
| 321 |
|
| 322 |
switch (reqinfo->mode) { |
| 323 |
case MODE_GETNEXT: |
| 324 |
case MODE_GETBULK: |
| 325 |
exact = 0; |
| 326 |
} |
| 327 |
|
| 328 |
for (; requests; requests = requests->next) { |
| 329 |
|
| 330 |
#ifdef MIB_CLIENTS_ARE_EVIL |
| 331 |
savelen = requests->requestvb->name_length; |
| 332 |
memcpy(save, requests->requestvb->name, savelen * sizeof(oid)); |
| 333 |
#endif |
| 334 |
|
| 335 |
switch (reqinfo->mode) { |
| 336 |
case MODE_GET: |
| 337 |
case MODE_GETNEXT: |
| 338 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 339 |
case MODE_SET_RESERVE1: |
| 340 |
#endif /* !NETSNMP_NO_WRITE_SUPPORT */ |
| 341 |
/* |
| 342 |
* Actually call the old mib-module function |
| 343 |
*/ |
| 344 |
if (vp->findVar) { |
| 345 |
tmp_len = requests->requestvb->name_length*sizeof(oid); |
| 346 |
memcpy(tmp_name, requests->requestvb->name, tmp_len); |
| 347 |
/** clear the rest of tmp_name to keep valgrind happy */ |
| 348 |
memset(&tmp_name[requests->requestvb->name_length], 0x0, |
| 349 |
sizeof(tmp_name)-tmp_len); |
| 350 |
tmp_len = requests->requestvb->name_length; |
| 351 |
access = (*(vp->findVar)) (cvp, tmp_name, &tmp_len, |
| 352 |
exact, &len, &write_method); |
| 353 |
snmp_set_var_objid( requests->requestvb, tmp_name, tmp_len ); |
| 354 |
} |
| 355 |
else |
| 356 |
access = NULL; |
| 357 |
|
| 358 |
#ifdef WWW_FIX |
| 359 |
if (IS_DELEGATED(cvp->type)) { |
| 360 |
add_method = (AddVarMethod *) statP; |
| 361 |
requests->delayed = 1; |
| 362 |
have_delegated = 1; |
| 363 |
continue; /* WWW: This may not get to the right place */ |
| 364 |
} |
| 365 |
#endif |
| 366 |
|
| 367 |
/* |
| 368 |
* WWW: end range checking |
| 369 |
*/ |
| 370 |
if (access) { |
| 371 |
/* |
| 372 |
* result returned |
| 373 |
*/ |
| 374 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 375 |
if (reqinfo->mode != MODE_SET_RESERVE1) |
| 376 |
#endif /* !NETSNMP_NO_WRITE_SUPPORT */ |
| 377 |
snmp_set_var_typed_value(requests->requestvb, |
| 378 |
cvp->type, access, len); |
| 379 |
} else { |
| 380 |
/* |
| 381 |
* no result returned |
| 382 |
*/ |
| 383 |
#ifdef MIB_CLIENTS_ARE_EVIL |
| 384 |
if (access == NULL) { |
| 385 |
if (netsnmp_oid_equals(requests->requestvb->name, |
| 386 |
requests->requestvb->name_length, |
| 387 |
save, savelen) != 0) { |
| 388 |
DEBUGMSGTL(("old_api", "evil_client: %s\n", |
| 389 |
reginfo->handlerName)); |
| 390 |
memcpy(requests->requestvb->name, save, |
| 391 |
savelen * sizeof(oid)); |
| 392 |
requests->requestvb->name_length = savelen; |
| 393 |
} |
| 394 |
} |
| 395 |
#endif |
| 396 |
} |
| 397 |
|
| 398 |
/* |
| 399 |
* AAA: fall through for everything that is a set (see BBB) |
| 400 |
*/ |
| 401 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 402 |
if (reqinfo->mode != MODE_SET_RESERVE1) |
| 403 |
#endif /* !NETSNMP_NO_WRITE_SUPPORT */ |
| 404 |
break; |
| 405 |
|
| 406 |
cacheptr = SNMP_MALLOC_TYPEDEF(netsnmp_old_api_cache); |
| 407 |
if (!cacheptr) |
| 408 |
return netsnmp_set_request_error(reqinfo, requests, |
| 409 |
SNMP_ERR_RESOURCEUNAVAILABLE); |
| 410 |
cacheptr->data = access; |
| 411 |
cacheptr->write_method = write_method; |
| 412 |
write_method = NULL; |
| 413 |
netsnmp_request_add_list_data(requests, |
| 414 |
netsnmp_create_data_list |
| 415 |
(OLD_API_NAME, cacheptr, |
| 416 |
&free_wrapper)); |
| 417 |
/* |
| 418 |
* BBB: fall through for everything that is a set (see AAA) |
| 419 |
*/ |
| 420 |
/* FALL THROUGH */ |
| 421 |
|
| 422 |
default: |
| 423 |
/* |
| 424 |
* WWW: explicitly list the SET conditions |
| 425 |
*/ |
| 426 |
/* |
| 427 |
* (the rest of the) SET contions |
| 428 |
*/ |
| 429 |
cacheptr = |
| 430 |
(netsnmp_old_api_cache *) |
| 431 |
netsnmp_request_get_list_data(requests, OLD_API_NAME); |
| 432 |
|
| 433 |
if (cacheptr == NULL || cacheptr->write_method == NULL) { |
| 434 |
/* |
| 435 |
* WWW: try to set ourselves if possible? |
| 436 |
*/ |
| 437 |
return netsnmp_set_request_error(reqinfo, requests, |
| 438 |
SNMP_ERR_NOTWRITABLE); |
| 439 |
} |
| 440 |
|
| 441 |
oldasp = netsnmp_get_current_agent_session(); |
| 442 |
set_current_agent_session(reqinfo->asp); |
| 443 |
status = |
| 444 |
(*(cacheptr->write_method)) (reqinfo->mode, |
| 445 |
requests->requestvb->val. |
| 446 |
string, |
| 447 |
requests->requestvb->type, |
| 448 |
requests->requestvb->val_len, |
| 449 |
cacheptr->data, |
| 450 |
requests->requestvb->name, |
| 451 |
requests->requestvb-> |
| 452 |
name_length); |
| 453 |
set_current_agent_session(oldasp); |
| 454 |
|
| 455 |
if (status != SNMP_ERR_NOERROR) { |
| 456 |
netsnmp_set_request_error(reqinfo, requests, status); |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
* clean up is done by the automatic freeing of the |
| 461 |
* cache stored in the request. |
| 462 |
*/ |
| 463 |
|
| 464 |
break; |
| 465 |
} |
| 466 |
} |
| 467 |
return SNMP_ERR_NOERROR; |
| 468 |
} |
| 469 |
|
| 470 |
/** @} */ |
| 471 |
|
| 472 |
/* |
| 473 |
* don't use this! |
| 474 |
*/ |
| 475 |
static netsnmp_agent_session *current_agent_session = NULL; |
| 476 |
netsnmp_agent_session * |
| 477 |
netsnmp_get_current_agent_session(void) |
| 478 |
{ |
| 479 |
return current_agent_session; |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* don't use this! |
| 484 |
*/ |
| 485 |
void |
| 486 |
set_current_agent_session(netsnmp_agent_session *asp) |
| 487 |
{ |
| 488 |
current_agent_session = asp; |
| 489 |
} |