| 1 |
/* |
| 2 |
* AgentX utility routines |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
#include <net-snmp/net-snmp-features.h> |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <errno.h> |
| 10 |
#ifdef HAVE_STDLIB_H |
| 11 |
#include <stdlib.h> |
| 12 |
#endif |
| 13 |
#ifdef HAVE_STRING_H |
| 14 |
#include <string.h> |
| 15 |
#else |
| 16 |
#include <strings.h> |
| 17 |
#endif |
| 18 |
#ifdef HAVE_UNISTD_H |
| 19 |
#include <unistd.h> |
| 20 |
#endif |
| 21 |
#include <sys/types.h> |
| 22 |
#ifdef TIME_WITH_SYS_TIME |
| 23 |
# include <sys/time.h> |
| 24 |
# include <time.h> |
| 25 |
#else |
| 26 |
# ifdef HAVE_SYS_TIME_H |
| 27 |
# include <sys/time.h> |
| 28 |
# else |
| 29 |
# include <time.h> |
| 30 |
# endif |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef HAVE_NETINET_IN_H |
| 34 |
#include <netinet/in.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include <net-snmp/net-snmp-includes.h> |
| 38 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 39 |
#include <net-snmp/agent/agent_index.h> |
| 40 |
#include "agent_global_vars.h" |
| 41 |
|
| 42 |
#include "agentx/protocol.h" |
| 43 |
#include "agentx/client.h" |
| 44 |
#include "agentx/subagent.h" |
| 45 |
|
| 46 |
netsnmp_feature_require(set_agent_uptime); |
| 47 |
|
| 48 |
/* |
| 49 |
* AgentX handling utility routines |
| 50 |
* |
| 51 |
* Mostly wrappers round, or re-writes of |
| 52 |
* the SNMP equivalents |
| 53 |
*/ |
| 54 |
|
| 55 |
static int |
| 56 |
agentx_synch_input(int op, |
| 57 |
netsnmp_session * session, |
| 58 |
int reqid, netsnmp_pdu *pdu, void *magic) |
| 59 |
{ |
| 60 |
struct synch_state *state = (struct synch_state *) magic; |
| 61 |
|
| 62 |
if (!state || reqid != state->reqid) { |
| 63 |
return handle_agentx_packet(op, session, reqid, pdu, magic); |
| 64 |
} |
| 65 |
|
| 66 |
DEBUGMSGTL(("agentx/subagent", "synching input, op 0x%02x\n", op)); |
| 67 |
state->waiting = 0; |
| 68 |
if (op == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) { |
| 69 |
if (pdu->command == AGENTX_MSG_RESPONSE) { |
| 70 |
state->pdu = snmp_clone_pdu(pdu); |
| 71 |
state->status = STAT_SUCCESS; |
| 72 |
session->s_snmp_errno = SNMPERR_SUCCESS; |
| 73 |
|
| 74 |
/* |
| 75 |
* Synchronise sysUpTime with the master agent |
| 76 |
*/ |
| 77 |
netsnmp_set_agent_uptime(pdu->time); |
| 78 |
} |
| 79 |
} else if (op == NETSNMP_CALLBACK_OP_TIMED_OUT) { |
| 80 |
state->pdu = NULL; |
| 81 |
state->status = STAT_TIMEOUT; |
| 82 |
session->s_snmp_errno = SNMPERR_TIMEOUT; |
| 83 |
} else if (op == NETSNMP_CALLBACK_OP_DISCONNECT) { |
| 84 |
return handle_agentx_packet(op, session, reqid, pdu, magic); |
| 85 |
} |
| 86 |
|
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
static int |
| 93 |
agentx_synch_response(netsnmp_session * ss, netsnmp_pdu *pdu, |
| 94 |
netsnmp_pdu **response) |
| 95 |
{ |
| 96 |
return snmp_synch_response_cb(ss, pdu, response, agentx_synch_input); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/* |
| 101 |
* AgentX PofE convenience functions |
| 102 |
*/ |
| 103 |
|
| 104 |
int |
| 105 |
agentx_open_session(netsnmp_session * ss) |
| 106 |
{ |
| 107 |
netsnmp_pdu *pdu, *response; |
| 108 |
int timeout; |
| 109 |
|
| 110 |
DEBUGMSGTL(("agentx/subagent", "opening session \n")); |
| 111 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
pdu = snmp_pdu_create(AGENTX_MSG_OPEN); |
| 116 |
if (pdu == NULL) |
| 117 |
return 0; |
| 118 |
timeout = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID, |
| 119 |
NETSNMP_DS_AGENT_AGENTX_TIMEOUT); |
| 120 |
if (timeout < 0) |
| 121 |
pdu->time = 0; |
| 122 |
else |
| 123 |
/* for master TIMEOUT is usec, but Agentx Open specifies sec */ |
| 124 |
pdu->time = timeout/ONE_SEC; |
| 125 |
|
| 126 |
snmp_add_var(pdu, version_sysoid, version_sysoid_len, |
| 127 |
's', "Net-SNMP AgentX sub-agent"); |
| 128 |
|
| 129 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 130 |
return 0; |
| 131 |
|
| 132 |
if (!response) |
| 133 |
return 0; |
| 134 |
|
| 135 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 136 |
snmp_free_pdu(response); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
ss->sessid = response->sessid; |
| 141 |
snmp_free_pdu(response); |
| 142 |
|
| 143 |
DEBUGMSGTL(("agentx/subagent", "open \n")); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
int |
| 148 |
agentx_close_session(netsnmp_session * ss, int why) |
| 149 |
{ |
| 150 |
netsnmp_pdu *pdu, *response; |
| 151 |
DEBUGMSGTL(("agentx/subagent", "closing session\n")); |
| 152 |
|
| 153 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
pdu = snmp_pdu_create(AGENTX_MSG_CLOSE); |
| 158 |
if (pdu == NULL) |
| 159 |
return 0; |
| 160 |
pdu->time = 0; |
| 161 |
pdu->errstat = why; |
| 162 |
pdu->sessid = ss->sessid; |
| 163 |
|
| 164 |
if (agentx_synch_response(ss, pdu, &response) == STAT_SUCCESS) |
| 165 |
snmp_free_pdu(response); |
| 166 |
DEBUGMSGTL(("agentx/subagent", "closed\n")); |
| 167 |
|
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
int |
| 172 |
agentx_register(netsnmp_session * ss, oid start[], size_t startlen, |
| 173 |
int priority, int range_subid, oid range_ubound, |
| 174 |
int timeout, u_char flags, const char *contextName) |
| 175 |
{ |
| 176 |
netsnmp_pdu *pdu, *response; |
| 177 |
|
| 178 |
DEBUGMSGTL(("agentx/subagent", "registering: ")); |
| 179 |
DEBUGMSGOIDRANGE(("agentx/subagent", start, startlen, range_subid, |
| 180 |
range_ubound)); |
| 181 |
DEBUGMSG(("agentx/subagent", "\n")); |
| 182 |
|
| 183 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
pdu = snmp_pdu_create(AGENTX_MSG_REGISTER); |
| 188 |
if (pdu == NULL) { |
| 189 |
return 0; |
| 190 |
} |
| 191 |
pdu->time = timeout; |
| 192 |
pdu->priority = priority; |
| 193 |
pdu->sessid = ss->sessid; |
| 194 |
pdu->range_subid = range_subid; |
| 195 |
if (contextName) { |
| 196 |
pdu->flags |= AGENTX_MSG_FLAG_NON_DEFAULT_CONTEXT; |
| 197 |
pdu->community = (u_char *) strdup(contextName); |
| 198 |
pdu->community_len = strlen(contextName); |
| 199 |
} |
| 200 |
|
| 201 |
if (flags & FULLY_QUALIFIED_INSTANCE) { |
| 202 |
pdu->flags |= AGENTX_MSG_FLAG_INSTANCE_REGISTER; |
| 203 |
} |
| 204 |
|
| 205 |
if (range_subid) { |
| 206 |
snmp_pdu_add_variable(pdu, start, startlen, ASN_OBJECT_ID, |
| 207 |
(u_char *) start, startlen * sizeof(oid)); |
| 208 |
pdu->variables->val.objid[range_subid - 1] = range_ubound; |
| 209 |
} else { |
| 210 |
snmp_add_null_var(pdu, start, startlen); |
| 211 |
} |
| 212 |
|
| 213 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) { |
| 214 |
DEBUGMSGTL(("agentx/subagent", "registering failed!\n")); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 219 |
snmp_log(LOG_ERR,"registering pdu failed: %ld!\n", response->errstat); |
| 220 |
snmp_free_pdu(response); |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
snmp_free_pdu(response); |
| 225 |
DEBUGMSGTL(("agentx/subagent", "registered\n")); |
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
int |
| 230 |
agentx_unregister(netsnmp_session * ss, oid start[], size_t startlen, |
| 231 |
int priority, int range_subid, oid range_ubound, |
| 232 |
const char *contextName) |
| 233 |
{ |
| 234 |
netsnmp_pdu *pdu, *response; |
| 235 |
|
| 236 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
DEBUGMSGTL(("agentx/subagent", "unregistering: ")); |
| 241 |
DEBUGMSGOIDRANGE(("agentx/subagent", start, startlen, range_subid, |
| 242 |
range_ubound)); |
| 243 |
DEBUGMSG(("agentx/subagent", "\n")); |
| 244 |
pdu = snmp_pdu_create(AGENTX_MSG_UNREGISTER); |
| 245 |
if (pdu == NULL) { |
| 246 |
return 0; |
| 247 |
} |
| 248 |
pdu->time = 0; |
| 249 |
pdu->priority = priority; |
| 250 |
pdu->sessid = ss->sessid; |
| 251 |
pdu->range_subid = range_subid; |
| 252 |
if (contextName) { |
| 253 |
pdu->flags |= AGENTX_MSG_FLAG_NON_DEFAULT_CONTEXT; |
| 254 |
pdu->community = (u_char *) strdup(contextName); |
| 255 |
pdu->community_len = strlen(contextName); |
| 256 |
} |
| 257 |
|
| 258 |
if (range_subid) { |
| 259 |
snmp_pdu_add_variable(pdu, start, startlen, ASN_OBJECT_ID, |
| 260 |
(u_char *) start, startlen * sizeof(oid)); |
| 261 |
pdu->variables->val.objid[range_subid - 1] = range_ubound; |
| 262 |
} else { |
| 263 |
snmp_add_null_var(pdu, start, startlen); |
| 264 |
} |
| 265 |
|
| 266 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 267 |
return 0; |
| 268 |
|
| 269 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 270 |
snmp_free_pdu(response); |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
snmp_free_pdu(response); |
| 275 |
DEBUGMSGTL(("agentx/subagent", "unregistered\n")); |
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
netsnmp_variable_list * |
| 280 |
agentx_register_index(netsnmp_session * ss, |
| 281 |
netsnmp_variable_list * varbind, int flags) |
| 282 |
{ |
| 283 |
netsnmp_pdu *pdu, *response; |
| 284 |
netsnmp_variable_list *varbind2; |
| 285 |
|
| 286 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 287 |
return NULL; |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
* Make a copy of the index request varbind |
| 292 |
* for the AgentX request PDU |
| 293 |
* (since the pdu structure will be freed) |
| 294 |
*/ |
| 295 |
varbind2 = |
| 296 |
(netsnmp_variable_list *) malloc(sizeof(netsnmp_variable_list)); |
| 297 |
if (varbind2 == NULL) |
| 298 |
return NULL; |
| 299 |
if (snmp_clone_var(varbind, varbind2)) { |
| 300 |
snmp_free_varbind(varbind2); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
if (varbind2->val.string == NULL) |
| 304 |
varbind2->val.string = varbind2->buf; /* ensure it points somewhere */ |
| 305 |
|
| 306 |
pdu = snmp_pdu_create(AGENTX_MSG_INDEX_ALLOCATE); |
| 307 |
if (pdu == NULL) { |
| 308 |
snmp_free_varbind(varbind2); |
| 309 |
return NULL; |
| 310 |
} |
| 311 |
pdu->time = 0; |
| 312 |
pdu->sessid = ss->sessid; |
| 313 |
if (flags == ALLOCATE_ANY_INDEX) |
| 314 |
pdu->flags |= AGENTX_MSG_FLAG_ANY_INSTANCE; |
| 315 |
if (flags == ALLOCATE_NEW_INDEX) |
| 316 |
pdu->flags |= AGENTX_MSG_FLAG_NEW_INSTANCE; |
| 317 |
|
| 318 |
/* |
| 319 |
* Just send a single index request varbind. |
| 320 |
* Although the AgentX protocol supports |
| 321 |
* multiple index allocations in a single |
| 322 |
* request, the model used in the net-snmp agent |
| 323 |
* doesn't currently take advantage of this. |
| 324 |
* I believe this is our prerogative - just as |
| 325 |
* long as the master side Index request handler |
| 326 |
* can cope with multiple index requests. |
| 327 |
*/ |
| 328 |
pdu->variables = varbind2; |
| 329 |
|
| 330 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 331 |
return NULL; |
| 332 |
|
| 333 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 334 |
snmp_free_pdu(response); |
| 335 |
return NULL; |
| 336 |
} |
| 337 |
|
| 338 |
/* |
| 339 |
* Unlink the (single) response varbind to return |
| 340 |
* to the main driving index request routine. |
| 341 |
* |
| 342 |
* This is a memory leak, as nothing will ever |
| 343 |
* release this varbind. If this becomes a problem, |
| 344 |
* we'll need to keep a list of these here, and |
| 345 |
* free the memory in the "index release" routine. |
| 346 |
* But the master side never frees these either (by |
| 347 |
* design, since it still needs them), so expecting |
| 348 |
* the subagent to is discrimination, pure & simple :-) |
| 349 |
*/ |
| 350 |
varbind2 = response->variables; |
| 351 |
response->variables = NULL; |
| 352 |
snmp_free_pdu(response); |
| 353 |
return varbind2; |
| 354 |
} |
| 355 |
|
| 356 |
int |
| 357 |
agentx_unregister_index(netsnmp_session * ss, |
| 358 |
netsnmp_variable_list * varbind) |
| 359 |
{ |
| 360 |
netsnmp_pdu *pdu, *response; |
| 361 |
netsnmp_variable_list *varbind2; |
| 362 |
|
| 363 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
/* |
| 368 |
* Make a copy of the index request varbind |
| 369 |
* for the AgentX request PDU |
| 370 |
* (since the pdu structure will be freed) |
| 371 |
*/ |
| 372 |
varbind2 = |
| 373 |
(netsnmp_variable_list *) malloc(sizeof(netsnmp_variable_list)); |
| 374 |
if (varbind2 == NULL) |
| 375 |
return -1; |
| 376 |
if (snmp_clone_var(varbind, varbind2)) { |
| 377 |
snmp_free_varbind(varbind2); |
| 378 |
return -1; |
| 379 |
} |
| 380 |
|
| 381 |
pdu = snmp_pdu_create(AGENTX_MSG_INDEX_DEALLOCATE); |
| 382 |
if (pdu == NULL) { |
| 383 |
snmp_free_varbind(varbind2); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
pdu->time = 0; |
| 387 |
pdu->sessid = ss->sessid; |
| 388 |
|
| 389 |
/* |
| 390 |
* Just send a single index release varbind. |
| 391 |
* (as above) |
| 392 |
*/ |
| 393 |
pdu->variables = varbind2; |
| 394 |
|
| 395 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 396 |
return -1; |
| 397 |
|
| 398 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 399 |
snmp_free_pdu(response); |
| 400 |
return -1; /* XXX - say why */ |
| 401 |
} |
| 402 |
|
| 403 |
snmp_free_pdu(response); |
| 404 |
return SNMP_ERR_NOERROR; |
| 405 |
} |
| 406 |
|
| 407 |
int |
| 408 |
agentx_add_agentcaps(netsnmp_session * ss, |
| 409 |
const oid * agent_cap, size_t agent_caplen, |
| 410 |
const char *descr) |
| 411 |
{ |
| 412 |
netsnmp_pdu *pdu, *response; |
| 413 |
|
| 414 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
pdu = snmp_pdu_create(AGENTX_MSG_ADD_AGENT_CAPS); |
| 419 |
if (pdu == NULL) |
| 420 |
return 0; |
| 421 |
pdu->time = 0; |
| 422 |
pdu->sessid = ss->sessid; |
| 423 |
snmp_add_var(pdu, agent_cap, agent_caplen, 's', descr); |
| 424 |
|
| 425 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 426 |
return 0; |
| 427 |
|
| 428 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 429 |
snmp_free_pdu(response); |
| 430 |
return 0; |
| 431 |
} |
| 432 |
|
| 433 |
snmp_free_pdu(response); |
| 434 |
return 1; |
| 435 |
} |
| 436 |
|
| 437 |
int |
| 438 |
agentx_remove_agentcaps(netsnmp_session * ss, |
| 439 |
const oid * agent_cap, size_t agent_caplen) |
| 440 |
{ |
| 441 |
netsnmp_pdu *pdu, *response; |
| 442 |
|
| 443 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
pdu = snmp_pdu_create(AGENTX_MSG_REMOVE_AGENT_CAPS); |
| 448 |
if (pdu == NULL) |
| 449 |
return 0; |
| 450 |
pdu->time = 0; |
| 451 |
pdu->sessid = ss->sessid; |
| 452 |
snmp_add_null_var(pdu, agent_cap, agent_caplen); |
| 453 |
|
| 454 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 455 |
return 0; |
| 456 |
|
| 457 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 458 |
snmp_free_pdu(response); |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
snmp_free_pdu(response); |
| 463 |
return 1; |
| 464 |
} |
| 465 |
|
| 466 |
int |
| 467 |
agentx_send_ping(netsnmp_session * ss) |
| 468 |
{ |
| 469 |
netsnmp_pdu *pdu, *response; |
| 470 |
|
| 471 |
if (ss == NULL || !IS_AGENTX_VERSION(ss->version)) { |
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
pdu = snmp_pdu_create(AGENTX_MSG_PING); |
| 476 |
if (pdu == NULL) |
| 477 |
return 0; |
| 478 |
pdu->time = 0; |
| 479 |
pdu->sessid = ss->sessid; |
| 480 |
|
| 481 |
if (agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS) |
| 482 |
return 0; |
| 483 |
|
| 484 |
if (response->errstat != SNMP_ERR_NOERROR) { |
| 485 |
snmp_free_pdu(response); |
| 486 |
return 0; |
| 487 |
} |
| 488 |
|
| 489 |
snmp_free_pdu(response); |
| 490 |
return 1; |
| 491 |
} |