| 1 |
/* Portions of this file are subject to the following copyright(s). See |
| 2 |
* the Net-SNMP's COPYING file for more details and other copyrights |
| 3 |
* that may apply: |
| 4 |
*/ |
| 5 |
/* |
| 6 |
* Portions of this file are copyrighted by: |
| 7 |
* Copyright @ 2009 Sun Microsystems, 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 |
* Portions of this file are copyrighted by: |
| 12 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 13 |
* Use is subject to license terms specified in the COPYING file |
| 14 |
* distributed with the Net-SNMP package. |
| 15 |
*/ |
| 16 |
#include <net-snmp/net-snmp-config.h> |
| 17 |
#include <net-snmp/net-snmp-features.h> |
| 18 |
|
| 19 |
#include <sys/types.h> |
| 20 |
#ifdef HAVE_STRING_H |
| 21 |
#include <string.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_UNISTD_H |
| 24 |
#include <unistd.h> |
| 25 |
#endif |
| 26 |
#ifdef HAVE_NETINET_IN_H |
| 27 |
#include <netinet/in.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <net-snmp/net-snmp-includes.h> |
| 31 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 32 |
|
| 33 |
#include "proxy.h" |
| 34 |
|
| 35 |
netsnmp_feature_require(handler_mark_requests_as_delegated); |
| 36 |
netsnmp_feature_require(request_set_error_idx); |
| 37 |
|
| 38 |
static struct simple_proxy *proxies = NULL; |
| 39 |
|
| 40 |
/* |
| 41 |
* this must be standardized somewhere, right? |
| 42 |
*/ |
| 43 |
#define MAX_ARGS 128 |
| 44 |
|
| 45 |
char *context_string; |
| 46 |
|
| 47 |
static void |
| 48 |
proxyOptProc(int argc, char *const *argv, int opt) |
| 49 |
{ |
| 50 |
switch (opt) { |
| 51 |
case 'C': |
| 52 |
while (*optarg) { |
| 53 |
switch (*optarg++) { |
| 54 |
case 'n': |
| 55 |
optind++; |
| 56 |
if (optind < argc) { |
| 57 |
context_string = argv[optind - 1]; |
| 58 |
} else { |
| 59 |
config_perror("No context name passed to -Cn"); |
| 60 |
} |
| 61 |
break; |
| 62 |
case 'c': |
| 63 |
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 64 |
NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY, 1); |
| 65 |
break; |
| 66 |
default: |
| 67 |
config_perror("unknown argument passed to -C"); |
| 68 |
break; |
| 69 |
} |
| 70 |
} |
| 71 |
break; |
| 72 |
default: |
| 73 |
break; |
| 74 |
/* |
| 75 |
* shouldn't get here |
| 76 |
*/ |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
proxy_parse_config(const char *token, char *line) |
| 82 |
{ |
| 83 |
/* |
| 84 |
* proxy args [base-oid] [remap-to-remote-oid] |
| 85 |
*/ |
| 86 |
|
| 87 |
netsnmp_session session, *ss; |
| 88 |
struct simple_proxy *newp, **listpp; |
| 89 |
char *argv[MAX_ARGS]; |
| 90 |
int argn, arg; |
| 91 |
char *cp; |
| 92 |
char *buff; |
| 93 |
netsnmp_handler_registration *reg; |
| 94 |
|
| 95 |
context_string = NULL; |
| 96 |
|
| 97 |
DEBUGMSGTL(("proxy_config", "entering\n")); |
| 98 |
|
| 99 |
/* Put the first string into the array */ |
| 100 |
argv[0] = strdup("snmpd-proxy"); |
| 101 |
if (!argv[0]) { |
| 102 |
config_perror("could not allocate memory for argv[0]"); |
| 103 |
return; |
| 104 |
} |
| 105 |
/* |
| 106 |
* create the argv[] like array |
| 107 |
*/ |
| 108 |
/* Allocates memory to store the parameters value */ |
| 109 |
buff = (char *) malloc (strlen(line)+1); |
| 110 |
if (!buff) { |
| 111 |
config_perror("could not allocate memory for buff"); |
| 112 |
/* Free the memory allocated */ |
| 113 |
SNMP_FREE(argv[0]); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
for (argn = 1, cp = line; cp && argn < MAX_ARGS;) { |
| 118 |
/* Copy a parameter into the buff */ |
| 119 |
cp = copy_nword(cp, buff, strlen(cp)+1); |
| 120 |
argv[argn] = strdup(buff); |
| 121 |
if (!argv[argn]) { |
| 122 |
config_perror("could not allocate memory for argv[n]"); |
| 123 |
SNMP_FREE(buff); |
| 124 |
goto free_argv; |
| 125 |
} |
| 126 |
argn++; |
| 127 |
} |
| 128 |
SNMP_FREE(buff); |
| 129 |
|
| 130 |
for (arg = 0; arg < argn; arg++) { |
| 131 |
DEBUGMSGTL(("proxy_args", "final args: %d = %s\n", arg, |
| 132 |
argv[arg])); |
| 133 |
} |
| 134 |
|
| 135 |
memset(&session, 0, sizeof(session)); |
| 136 |
|
| 137 |
DEBUGMSGTL(("proxy_config", "parsing args: %d\n", argn)); |
| 138 |
/* Call special parse_args that allows for no specified community string */ |
| 139 |
arg = netsnmp_parse_args(argn, argv, &session, "C:", proxyOptProc, |
| 140 |
NETSNMP_PARSE_ARGS_NOLOGGING | |
| 141 |
NETSNMP_PARSE_ARGS_NOZERO); |
| 142 |
|
| 143 |
/* reset this in case we modified it */ |
| 144 |
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, |
| 145 |
NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY, 0); |
| 146 |
|
| 147 |
if (arg < 0) { |
| 148 |
config_perror("failed to parse proxy args"); |
| 149 |
goto cleanup_session; |
| 150 |
} |
| 151 |
DEBUGMSGTL(("proxy_config", "done parsing args\n")); |
| 152 |
|
| 153 |
if (arg >= argn) { |
| 154 |
config_perror("missing base oid"); |
| 155 |
goto cleanup_session; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* usm_set_reportErrorOnUnknownID(0); |
| 160 |
* |
| 161 |
* hack, stupid v3 ASIs. |
| 162 |
*/ |
| 163 |
/* |
| 164 |
* XXX: on a side note, we don't really need to be a reference |
| 165 |
* platform any more so the proper thing to do would be to fix |
| 166 |
* snmplib/snmpusm.c to pass in the pdu type to usm_process_incoming |
| 167 |
* so this isn't needed. |
| 168 |
*/ |
| 169 |
ss = snmp_open(&session); |
| 170 |
SNMP_FREE(session.community); |
| 171 |
/* |
| 172 |
* usm_set_reportErrorOnUnknownID(1); |
| 173 |
*/ |
| 174 |
if (ss == NULL) { |
| 175 |
/* |
| 176 |
* diagnose snmp_open errors with the input netsnmp_session pointer |
| 177 |
*/ |
| 178 |
snmp_sess_perror("snmpget", &session); |
| 179 |
goto cleanup_session; |
| 180 |
} |
| 181 |
|
| 182 |
newp = (struct simple_proxy *) calloc(1, sizeof(struct simple_proxy)); |
| 183 |
|
| 184 |
newp->sess = ss; |
| 185 |
DEBUGMSGTL(("proxy_init", "name = %s\n", argv[arg])); |
| 186 |
newp->name_len = MAX_OID_LEN; |
| 187 |
if (!snmp_parse_oid(argv[arg++], newp->name, &newp->name_len)) { |
| 188 |
snmp_perror("proxy"); |
| 189 |
config_perror("illegal proxy oid specified\n"); |
| 190 |
/*deallocate the memory previously allocated*/ |
| 191 |
SNMP_FREE(newp); |
| 192 |
goto cleanup_session; |
| 193 |
} |
| 194 |
|
| 195 |
if (arg < argn) { |
| 196 |
DEBUGMSGTL(("proxy_init", "base = %s\n", argv[arg])); |
| 197 |
newp->base_len = MAX_OID_LEN; |
| 198 |
if (!snmp_parse_oid(argv[arg++], newp->base, &newp->base_len)) { |
| 199 |
snmp_perror("proxy"); |
| 200 |
config_perror("illegal variable name specified (base oid)\n"); |
| 201 |
SNMP_FREE(newp); |
| 202 |
goto cleanup_session; |
| 203 |
} |
| 204 |
} |
| 205 |
if ( context_string ) |
| 206 |
newp->context = strdup(context_string); |
| 207 |
|
| 208 |
DEBUGMSGTL(("proxy_init", "registering at: ")); |
| 209 |
DEBUGMSGOID(("proxy_init", newp->name, newp->name_len)); |
| 210 |
DEBUGMSG(("proxy_init", "\n")); |
| 211 |
|
| 212 |
/* |
| 213 |
* add to our chain |
| 214 |
*/ |
| 215 |
/* |
| 216 |
* must be sorted! |
| 217 |
*/ |
| 218 |
listpp = &proxies; |
| 219 |
while (*listpp && |
| 220 |
snmp_oid_compare(newp->name, newp->name_len, |
| 221 |
(*listpp)->name, (*listpp)->name_len) > 0) { |
| 222 |
listpp = &((*listpp)->next); |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* listpp should be next in line from us. |
| 227 |
*/ |
| 228 |
if (*listpp) { |
| 229 |
/* |
| 230 |
* make our next in the link point to the current link |
| 231 |
*/ |
| 232 |
newp->next = *listpp; |
| 233 |
} |
| 234 |
/* |
| 235 |
* replace current link with us |
| 236 |
*/ |
| 237 |
*listpp = newp; |
| 238 |
|
| 239 |
reg = netsnmp_create_handler_registration("proxy", |
| 240 |
proxy_handler, |
| 241 |
newp->name, |
| 242 |
newp->name_len, |
| 243 |
HANDLER_CAN_RWRITE); |
| 244 |
reg->handler->myvoid = newp; |
| 245 |
if (context_string) |
| 246 |
reg->contextName = strdup(context_string); |
| 247 |
|
| 248 |
netsnmp_register_handler(reg); |
| 249 |
|
| 250 |
cleanup_session: |
| 251 |
netsnmp_cleanup_session(&session); |
| 252 |
|
| 253 |
free_argv: |
| 254 |
/* Free the memory allocated */ |
| 255 |
while(argn--) |
| 256 |
SNMP_FREE(argv[argn]); |
| 257 |
} |
| 258 |
|
| 259 |
void |
| 260 |
proxy_free_config(void) |
| 261 |
{ |
| 262 |
struct simple_proxy *rm; |
| 263 |
|
| 264 |
DEBUGMSGTL(("proxy_free_config", "Free config\n")); |
| 265 |
while (proxies) { |
| 266 |
rm = proxies; |
| 267 |
proxies = rm->next; |
| 268 |
|
| 269 |
DEBUGMSGTL(( "proxy_free_config", "freeing ")); |
| 270 |
DEBUGMSGOID(("proxy_free_config", rm->name, rm->name_len)); |
| 271 |
DEBUGMSG(( "proxy_free_config", " (%s)\n", rm->context)); |
| 272 |
unregister_mib_context(rm->name, rm->name_len, |
| 273 |
DEFAULT_MIB_PRIORITY, 0, 0, |
| 274 |
rm->context); |
| 275 |
SNMP_FREE(rm->context); |
| 276 |
snmp_close(rm->sess); |
| 277 |
SNMP_FREE(rm); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* Configure special parameters on the session. |
| 283 |
* Currently takes the parameter configured and changes it if something |
| 284 |
* was configured. It becomes "-c" if the community string from the pdu |
| 285 |
* is placed on the session. |
| 286 |
*/ |
| 287 |
int |
| 288 |
proxy_fill_in_session(netsnmp_mib_handler *handler, |
| 289 |
netsnmp_agent_request_info *reqinfo, |
| 290 |
void **configured) |
| 291 |
{ |
| 292 |
netsnmp_session *session; |
| 293 |
struct simple_proxy *sp; |
| 294 |
|
| 295 |
sp = (struct simple_proxy *) handler->myvoid; |
| 296 |
if (!sp) { |
| 297 |
return 0; |
| 298 |
} |
| 299 |
session = sp->sess; |
| 300 |
if (!session) { |
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
#if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C) |
| 305 |
if ( |
| 306 |
#ifndef NETSNMP_DISABLE_SNMPV1 |
| 307 |
((session->version == SNMP_VERSION_1) && |
| 308 |
!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
| 309 |
NETSNMP_DS_LIB_DISABLE_V1)) || |
| 310 |
#endif |
| 311 |
#ifndef NETSNMP_DISABLE_SNMPV2C |
| 312 |
((session->version == SNMP_VERSION_2c) && |
| 313 |
!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
| 314 |
NETSNMP_DS_LIB_DISABLE_V2c)) || |
| 315 |
#endif |
| 316 |
0 ) { /* 0 to terminate '||' above */ |
| 317 |
|
| 318 |
/* |
| 319 |
* Check if session has community string defined for it. |
| 320 |
* If not, need to extract community string from the pdu. |
| 321 |
* Copy to session and set 'configured' to indicate this. |
| 322 |
*/ |
| 323 |
if (session->community_len == 0) { |
| 324 |
DEBUGMSGTL(("proxy", "session has no community string\n")); |
| 325 |
if (reqinfo->asp == NULL || reqinfo->asp->pdu == NULL || |
| 326 |
reqinfo->asp->pdu->community_len == 0) { |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
*configured = strdup("-c"); |
| 331 |
DEBUGMSGTL(("proxy", "pdu has community string\n")); |
| 332 |
session->community_len = reqinfo->asp->pdu->community_len; |
| 333 |
if (asprintf((char **)&session->community, "%.*s", |
| 334 |
(int)session->community_len, |
| 335 |
(const char *)reqinfo->asp->pdu->community) < 0) |
| 336 |
session->community = NULL; |
| 337 |
} |
| 338 |
} |
| 339 |
#endif |
| 340 |
|
| 341 |
return 1; |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
* Free any specially configured parameters used on the session. |
| 346 |
*/ |
| 347 |
void |
| 348 |
proxy_free_filled_in_session_args(netsnmp_session *session, void **configured) |
| 349 |
{ |
| 350 |
|
| 351 |
/* Only do comparisions, etc., if something was configured */ |
| 352 |
if (*configured == NULL) { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
/* If used community string from pdu, release it from session now */ |
| 357 |
if (strcmp((const char *)(*configured), "-c") == 0) { |
| 358 |
free(session->community); |
| 359 |
session->community = NULL; |
| 360 |
session->community_len = 0; |
| 361 |
} |
| 362 |
|
| 363 |
free(*configured); |
| 364 |
*configured = NULL; |
| 365 |
} |
| 366 |
|
| 367 |
void |
| 368 |
init_proxy(void) |
| 369 |
{ |
| 370 |
snmpd_register_config_handler("proxy", proxy_parse_config, |
| 371 |
proxy_free_config, |
| 372 |
"[snmpcmd args] host oid [remoteoid]"); |
| 373 |
} |
| 374 |
|
| 375 |
void |
| 376 |
shutdown_proxy(void) |
| 377 |
{ |
| 378 |
proxy_free_config(); |
| 379 |
} |
| 380 |
|
| 381 |
int |
| 382 |
proxy_handler(netsnmp_mib_handler *handler, |
| 383 |
netsnmp_handler_registration *reginfo, |
| 384 |
netsnmp_agent_request_info *reqinfo, |
| 385 |
netsnmp_request_info *requests) |
| 386 |
{ |
| 387 |
|
| 388 |
netsnmp_pdu *pdu; |
| 389 |
struct simple_proxy *sp; |
| 390 |
oid *ourname; |
| 391 |
size_t ourlength; |
| 392 |
netsnmp_request_info *request = requests; |
| 393 |
u_char *configured = NULL; |
| 394 |
|
| 395 |
DEBUGMSGTL(("proxy", "proxy handler starting, mode = %d\n", |
| 396 |
reqinfo->mode)); |
| 397 |
|
| 398 |
switch (reqinfo->mode) { |
| 399 |
case MODE_GET: |
| 400 |
case MODE_GETNEXT: |
| 401 |
case MODE_GETBULK: /* WWWXXX */ |
| 402 |
pdu = snmp_pdu_create(reqinfo->mode); |
| 403 |
break; |
| 404 |
|
| 405 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 406 |
case MODE_SET_ACTION: |
| 407 |
pdu = snmp_pdu_create(SNMP_MSG_SET); |
| 408 |
break; |
| 409 |
|
| 410 |
case MODE_SET_UNDO: |
| 411 |
/* |
| 412 |
* If we set successfully (status == NOERROR), |
| 413 |
* we can't back out again, so need to report the fact. |
| 414 |
* If we failed to set successfully, then we're fine. |
| 415 |
*/ |
| 416 |
for (request = requests; request; request=request->next) { |
| 417 |
if (request->status == SNMP_ERR_NOERROR) { |
| 418 |
netsnmp_set_request_error(reqinfo, requests, |
| 419 |
SNMP_ERR_UNDOFAILED); |
| 420 |
return SNMP_ERR_UNDOFAILED; |
| 421 |
} |
| 422 |
} |
| 423 |
return SNMP_ERR_NOERROR; |
| 424 |
|
| 425 |
case MODE_SET_RESERVE1: |
| 426 |
case MODE_SET_RESERVE2: |
| 427 |
case MODE_SET_FREE: |
| 428 |
case MODE_SET_COMMIT: |
| 429 |
/* |
| 430 |
* Nothing to do in this pass |
| 431 |
*/ |
| 432 |
return SNMP_ERR_NOERROR; |
| 433 |
#endif /* !NETSNMP_NO_WRITE_SUPPORT */ |
| 434 |
|
| 435 |
default: |
| 436 |
snmp_log(LOG_WARNING, "unsupported mode for proxy called (%d)\n", |
| 437 |
reqinfo->mode); |
| 438 |
return SNMP_ERR_NOERROR; |
| 439 |
} |
| 440 |
|
| 441 |
sp = (struct simple_proxy *) handler->myvoid; |
| 442 |
|
| 443 |
if (!pdu || !sp) { |
| 444 |
netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR); |
| 445 |
if (pdu) |
| 446 |
snmp_free_pdu(pdu); |
| 447 |
return SNMP_ERR_NOERROR; |
| 448 |
} |
| 449 |
|
| 450 |
while (request) { |
| 451 |
ourname = request->requestvb->name; |
| 452 |
ourlength = request->requestvb->name_length; |
| 453 |
|
| 454 |
if (sp->base_len && |
| 455 |
reqinfo->mode == MODE_GETNEXT && |
| 456 |
(snmp_oid_compare(ourname, ourlength, |
| 457 |
sp->name, sp->name_len) < 0)) { |
| 458 |
DEBUGMSGTL(( "proxy", "request is out of registered range\n")); |
| 459 |
/* |
| 460 |
* Create GETNEXT request with an OID so the |
| 461 |
* master returns the first OID in the registered range. |
| 462 |
*/ |
| 463 |
memcpy(ourname, sp->base, sp->base_len * sizeof(oid)); |
| 464 |
ourlength = sp->base_len; |
| 465 |
if (ourname[ourlength-1] <= 1) { |
| 466 |
/* |
| 467 |
* The registered range ends with x.y.z.1 |
| 468 |
* -> ask for the next of x.y.z |
| 469 |
*/ |
| 470 |
ourlength--; |
| 471 |
} else { |
| 472 |
/* |
| 473 |
* The registered range ends with x.y.z.A |
| 474 |
* -> ask for the next of x.y.z.A-1.MAX_SUBID |
| 475 |
*/ |
| 476 |
ourname[ourlength-1]--; |
| 477 |
ourname[ourlength] = MAX_SUBID; |
| 478 |
ourlength++; |
| 479 |
} |
| 480 |
} else if (sp->base_len > 0) { |
| 481 |
if ((ourlength - sp->name_len + sp->base_len) > MAX_OID_LEN) { |
| 482 |
/* |
| 483 |
* too large |
| 484 |
*/ |
| 485 |
if (pdu) |
| 486 |
snmp_free_pdu(pdu); |
| 487 |
snmp_log(LOG_ERR, |
| 488 |
"proxy oid request length is too long\n"); |
| 489 |
return SNMP_ERR_NOERROR; |
| 490 |
} |
| 491 |
/* |
| 492 |
* suffix appended? |
| 493 |
*/ |
| 494 |
DEBUGMSGTL(("proxy", "length=%d, base_len=%d, name_len=%d\n", |
| 495 |
(int)ourlength, (int)sp->base_len, (int)sp->name_len)); |
| 496 |
if (ourlength > sp->name_len) |
| 497 |
memcpy(&(sp->base[sp->base_len]), &(ourname[sp->name_len]), |
| 498 |
sizeof(oid) * (ourlength - sp->name_len)); |
| 499 |
ourlength = ourlength - sp->name_len + sp->base_len; |
| 500 |
ourname = sp->base; |
| 501 |
} |
| 502 |
|
| 503 |
snmp_pdu_add_variable(pdu, ourname, ourlength, |
| 504 |
request->requestvb->type, |
| 505 |
request->requestvb->val.string, |
| 506 |
request->requestvb->val_len); |
| 507 |
request->delegated = 1; |
| 508 |
request = request->next; |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
* Customize session parameters based on request information |
| 513 |
*/ |
| 514 |
if (!proxy_fill_in_session(handler, reqinfo, (void **)&configured)) { |
| 515 |
netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR); |
| 516 |
if (pdu) |
| 517 |
snmp_free_pdu(pdu); |
| 518 |
return SNMP_ERR_NOERROR; |
| 519 |
} |
| 520 |
|
| 521 |
/* |
| 522 |
* send the request out |
| 523 |
*/ |
| 524 |
DEBUGMSGTL(("proxy", "sending pdu\n")); |
| 525 |
snmp_async_send(sp->sess, pdu, proxy_got_response, |
| 526 |
netsnmp_create_delegated_cache(handler, reginfo, |
| 527 |
reqinfo, requests, |
| 528 |
(void *) sp)); |
| 529 |
|
| 530 |
/* Free any special parameters generated on the session */ |
| 531 |
proxy_free_filled_in_session_args(sp->sess, (void **)&configured); |
| 532 |
|
| 533 |
return SNMP_ERR_NOERROR; |
| 534 |
} |
| 535 |
|
| 536 |
int |
| 537 |
proxy_got_response(int operation, netsnmp_session * sess, int reqid, |
| 538 |
netsnmp_pdu *pdu, void *cb_data) |
| 539 |
{ |
| 540 |
netsnmp_delegated_cache *cache = (netsnmp_delegated_cache *) cb_data; |
| 541 |
netsnmp_request_info *requests, *request = NULL; |
| 542 |
netsnmp_variable_list *vars, *var = NULL; |
| 543 |
|
| 544 |
struct simple_proxy *sp; |
| 545 |
oid myname[MAX_OID_LEN]; |
| 546 |
size_t myname_len = MAX_OID_LEN; |
| 547 |
|
| 548 |
cache = netsnmp_handler_check_cache(cache); |
| 549 |
|
| 550 |
if (!cache) { |
| 551 |
DEBUGMSGTL(("proxy", "a proxy request was no longer valid.\n")); |
| 552 |
return SNMP_ERR_NOERROR; |
| 553 |
} |
| 554 |
|
| 555 |
requests = cache->requests; |
| 556 |
|
| 557 |
|
| 558 |
sp = (struct simple_proxy *) cache->localinfo; |
| 559 |
|
| 560 |
if (!sp) { |
| 561 |
DEBUGMSGTL(("proxy", "a proxy request was no longer valid.\n")); |
| 562 |
return SNMP_ERR_NOERROR; |
| 563 |
} |
| 564 |
|
| 565 |
switch (operation) { |
| 566 |
case NETSNMP_CALLBACK_OP_RESEND: |
| 567 |
/* |
| 568 |
* Issue#147: Net-SNMP not responding when proxy requests times out |
| 569 |
* |
| 570 |
* When snmp_api issue a resend, the default case was hit and the |
| 571 |
* delagated cache was freed. |
| 572 |
* As a result, the NETSNMP_CALLBACK_OP_TIMED_OUT never came in. |
| 573 |
*/ |
| 574 |
DEBUGMSGTL(("proxy", "pdu has been resent for request = %8p\n", requests)); |
| 575 |
return SNMP_ERR_NOERROR; |
| 576 |
|
| 577 |
case NETSNMP_CALLBACK_OP_TIMED_OUT: |
| 578 |
/* |
| 579 |
* WWWXXX: don't leave requests delayed if operation is |
| 580 |
* something like TIMEOUT |
| 581 |
*/ |
| 582 |
DEBUGMSGTL(("proxy", "got timed out... requests = %8p\n", requests)); |
| 583 |
|
| 584 |
netsnmp_handler_mark_requests_as_delegated(requests, |
| 585 |
REQUEST_IS_NOT_DELEGATED); |
| 586 |
if(cache->reqinfo->mode != MODE_GETNEXT) { |
| 587 |
DEBUGMSGTL(("proxy", " ignoring timeout\n")); |
| 588 |
netsnmp_set_request_error(cache->reqinfo, requests, /* XXXWWW: should be index = 0 */ |
| 589 |
SNMP_ERR_GENERR); |
| 590 |
} |
| 591 |
netsnmp_free_delegated_cache(cache); |
| 592 |
return 0; |
| 593 |
|
| 594 |
case NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE: |
| 595 |
vars = pdu->variables; |
| 596 |
|
| 597 |
if (pdu->errstat != SNMP_ERR_NOERROR) { |
| 598 |
/* |
| 599 |
* If we receive an error from the proxy agent, pass it on up. |
| 600 |
* The higher-level processing seems to Do The Right Thing. |
| 601 |
* |
| 602 |
* 2005/06 rks: actually, it doesn't do the right thing for |
| 603 |
* a get-next request that returns NOSUCHNAME. If we do nothing, |
| 604 |
* it passes that error back to the comman initiator. What it should |
| 605 |
* do is ignore the error and move on to the next tree. To |
| 606 |
* accomplish that, all we need to do is clear the delegated flag. |
| 607 |
* Not sure if any other error codes need the same treatment. Left |
| 608 |
* as an exercise to the reader... |
| 609 |
*/ |
| 610 |
DEBUGMSGTL(("proxy", "got error response (%ld)\n", pdu->errstat)); |
| 611 |
if((cache->reqinfo->mode == MODE_GETNEXT) && |
| 612 |
(SNMP_ERR_NOSUCHNAME == pdu->errstat)) { |
| 613 |
DEBUGMSGTL(("proxy", " ignoring error response\n")); |
| 614 |
netsnmp_handler_mark_requests_as_delegated(requests, |
| 615 |
REQUEST_IS_NOT_DELEGATED); |
| 616 |
} |
| 617 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 618 |
else if (cache->reqinfo->mode == MODE_SET_ACTION) { |
| 619 |
/* |
| 620 |
* In order for netsnmp_wrap_up_request to consider the |
| 621 |
* SET request complete, |
| 622 |
* there must be no delegated requests pending. |
| 623 |
* https://sourceforge.net/tracker/ |
| 624 |
* ?func=detail&atid=112694&aid=1554261&group_id=12694 |
| 625 |
*/ |
| 626 |
DEBUGMSGTL(("proxy", |
| 627 |
"got SET error %s, index %ld\n", |
| 628 |
snmp_errstring(pdu->errstat), pdu->errindex)); |
| 629 |
netsnmp_handler_mark_requests_as_delegated( |
| 630 |
requests, REQUEST_IS_NOT_DELEGATED); |
| 631 |
netsnmp_request_set_error_idx(requests, pdu->errstat, |
| 632 |
pdu->errindex); |
| 633 |
} |
| 634 |
#endif /* !NETSNMP_NO_WRITE_SUPPORT */ |
| 635 |
else { |
| 636 |
netsnmp_handler_mark_requests_as_delegated( requests, |
| 637 |
REQUEST_IS_NOT_DELEGATED); |
| 638 |
netsnmp_request_set_error_idx(requests, pdu->errstat, |
| 639 |
pdu->errindex); |
| 640 |
} |
| 641 |
|
| 642 |
/* |
| 643 |
* update the original request varbinds with the results |
| 644 |
*/ |
| 645 |
} else for (var = vars, request = requests; |
| 646 |
request && var; |
| 647 |
request = request->next, var = var->next_variable) { |
| 648 |
/* |
| 649 |
* XXX - should this be done here? |
| 650 |
* Or wait until we know it's OK? |
| 651 |
*/ |
| 652 |
snmp_set_var_typed_value(request->requestvb, var->type, |
| 653 |
var->val.string, var->val_len); |
| 654 |
|
| 655 |
DEBUGMSGTL(("proxy", "got response... ")); |
| 656 |
DEBUGMSGOID(("proxy", var->name, var->name_length)); |
| 657 |
DEBUGMSG(("proxy", "\n")); |
| 658 |
request->delegated = 0; |
| 659 |
|
| 660 |
/* |
| 661 |
* Check the response oid is legitimate, |
| 662 |
* and discard the value if not. |
| 663 |
* |
| 664 |
* XXX - what's the difference between these cases? |
| 665 |
*/ |
| 666 |
if (sp->base_len && |
| 667 |
(var->name_length < sp->base_len || |
| 668 |
snmp_oid_compare(var->name, sp->base_len, sp->base, |
| 669 |
sp->base_len) != 0)) { |
| 670 |
DEBUGMSGTL(( "proxy", "out of registered range... ")); |
| 671 |
DEBUGMSGOID(("proxy", var->name, sp->base_len)); |
| 672 |
DEBUGMSG(( "proxy", " (%d) != ", (int)sp->base_len)); |
| 673 |
DEBUGMSGOID(("proxy", sp->base, sp->base_len)); |
| 674 |
DEBUGMSG(( "proxy", "\n")); |
| 675 |
snmp_set_var_typed_value(request->requestvb, ASN_NULL, NULL, 0); |
| 676 |
|
| 677 |
continue; |
| 678 |
} else if (!sp->base_len && |
| 679 |
(var->name_length < sp->name_len || |
| 680 |
snmp_oid_compare(var->name, sp->name_len, sp->name, |
| 681 |
sp->name_len) != 0)) { |
| 682 |
DEBUGMSGTL(( "proxy", "out of registered base range... ")); |
| 683 |
DEBUGMSGOID(("proxy", var->name, sp->name_len)); |
| 684 |
DEBUGMSG(( "proxy", " (%d) != ", (int)sp->name_len)); |
| 685 |
DEBUGMSGOID(("proxy", sp->name, sp->name_len)); |
| 686 |
DEBUGMSG(( "proxy", "\n")); |
| 687 |
snmp_set_var_typed_value(request->requestvb, ASN_NULL, NULL, 0); |
| 688 |
continue; |
| 689 |
} else { |
| 690 |
/* |
| 691 |
* If the returned OID is legitimate, then update |
| 692 |
* the original request varbind accordingly. |
| 693 |
*/ |
| 694 |
if (sp->base_len) { |
| 695 |
/* |
| 696 |
* XXX: oid size maxed? |
| 697 |
*/ |
| 698 |
memcpy(myname, sp->name, sizeof(oid) * sp->name_len); |
| 699 |
myname_len = |
| 700 |
sp->name_len + var->name_length - sp->base_len; |
| 701 |
if (myname_len > MAX_OID_LEN) { |
| 702 |
snmp_log(LOG_WARNING, |
| 703 |
"proxy OID return length too long.\n"); |
| 704 |
netsnmp_set_request_error(cache->reqinfo, requests, |
| 705 |
SNMP_ERR_GENERR); |
| 706 |
netsnmp_free_delegated_cache(cache); |
| 707 |
return 1; |
| 708 |
} |
| 709 |
|
| 710 |
if (var->name_length > sp->base_len) |
| 711 |
memcpy(&myname[sp->name_len], |
| 712 |
&var->name[sp->base_len], |
| 713 |
sizeof(oid) * (var->name_length - |
| 714 |
sp->base_len)); |
| 715 |
snmp_set_var_objid(request->requestvb, myname, |
| 716 |
myname_len); |
| 717 |
} else { |
| 718 |
snmp_set_var_objid(request->requestvb, var->name, |
| 719 |
var->name_length); |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
if (request || var) { |
| 725 |
/* |
| 726 |
* ack, this is bad. The # of varbinds don't match and |
| 727 |
* there is no way to fix the problem |
| 728 |
*/ |
| 729 |
snmp_log(LOG_ERR, |
| 730 |
"response to proxy request illegal. We're screwed.\n"); |
| 731 |
netsnmp_set_request_error(cache->reqinfo, requests, |
| 732 |
SNMP_ERR_GENERR); |
| 733 |
} |
| 734 |
|
| 735 |
/* fix bulk_to_next operations */ |
| 736 |
if (cache->reqinfo->mode == MODE_GETBULK) |
| 737 |
netsnmp_bulk_to_next_fix_requests(requests); |
| 738 |
|
| 739 |
break; |
| 740 |
|
| 741 |
default: |
| 742 |
DEBUGMSGTL(("proxy", "no response received: op = %d\n", |
| 743 |
operation)); |
| 744 |
break; |
| 745 |
} |
| 746 |
|
| 747 |
netsnmp_free_delegated_cache(cache); |
| 748 |
return 1; |
| 749 |
} |