Debian Codegraph @

agent/mibgroup/agentx/subagent.c

raw
1 /*
2 * AgentX sub-agent
3 */
4 #include <net-snmp/net-snmp-config.h>
5 #include <net-snmp/net-snmp-features.h>
6
7 #include <sys/types.h>
8 #ifdef HAVE_STDLIB_H
9 #include <stdlib.h>
10 #endif
11 #ifdef TIME_WITH_SYS_TIME
12 # include <sys/time.h>
13 # include <time.h>
14 #else
15 # ifdef HAVE_SYS_TIME_H
16 # include <sys/time.h>
17 # else
18 # include <time.h>
19 # endif
20 #endif
21 #ifdef HAVE_SYS_SOCKET_H
22 #include <sys/socket.h>
23 #endif
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #else
27 #include <strings.h>
28 #endif
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32
33 #include <net-snmp/net-snmp-includes.h>
34 #include <net-snmp/agent/agent_index.h>
35 #include <net-snmp/agent/net-snmp-agent-includes.h>
36 #include <net-snmp/library/snmp_assert.h>
37
38 #include "snmpd.h"
39 #include "agent_global_vars.h"
40 #include "agentx/protocol.h"
41 #include "agentx/client.h"
42 #include "agentx/agentx_config.h"
43 #include <net-snmp/agent/agent_callbacks.h>
44 #include <net-snmp/agent/agent_trap.h>
45 #include <net-snmp/agent/sysORTable.h>
46 #include <net-snmp/agent/agent_sysORTable.h>
47
48 #include "subagent.h"
49
50 netsnmp_feature_child_of(agentx_subagent, agentx_all);
51 netsnmp_feature_child_of(agentx_enable_subagent, agentx_subagent);
52
53 netsnmp_feature_require(remove_trap_session);
54
55 #ifdef USING_AGENTX_SUBAGENT_MODULE
56
57 static SNMPCallback subagent_register_ping_alarm;
58 static SNMPAlarmCallback agentx_reopen_session;
59 void agentx_register_callbacks(netsnmp_session * s);
60 void agentx_unregister_callbacks(netsnmp_session * ss);
61 int handle_subagent_response(int op, netsnmp_session * session,
62 int reqid, netsnmp_pdu *pdu,
63 void *magic);
64 #ifndef NETSNMP_NO_WRITE_SUPPORT
65 int handle_subagent_set_response(int op,
66 netsnmp_session * session,
67 int reqid, netsnmp_pdu *pdu,
68 void *magic);
69 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
70 void subagent_startup_callback(unsigned int clientreg,
71 void *clientarg);
72 int subagent_open_master_session(void);
73
74 typedef struct _net_snmpsubagent_magic_s {
75 int original_command;
76 netsnmp_session *session;
77 netsnmp_variable_list *ovars;
78 } ns_subagent_magic;
79
80 struct agent_netsnmp_set_info {
81 int transID;
82 int mode;
83 int errstat;
84 time_t uptime;
85 netsnmp_session *sess;
86 netsnmp_variable_list *var_list;
87
88 struct agent_netsnmp_set_info *next;
89 };
90
91 #ifndef NETSNMP_NO_WRITE_SUPPORT
92 static struct agent_netsnmp_set_info *Sets = NULL;
93 #endif
94
95 netsnmp_session *agentx_callback_sess = NULL;
96
97 int
98 subagent_startup(int majorID, int minorID,
99 void *serverarg, void *clientarg)
100 {
101 DEBUGMSGTL(("agentx/subagent", "connecting to master...\n"));
102 /*
103 * if a valid ping interval has been defined, call agentx_reopen_session
104 * to try to connect to master or setup a ping alarm if it couldn't
105 * succeed. if no ping interval was set up, just try to connect once.
106 */
107 if (netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
108 NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL) > 0)
109 agentx_reopen_session(0, NULL);
110 else {
111 subagent_open_master_session();
112 }
113 return 0;
114 }
115
116 static void
117 subagent_init_callback_session(void)
118 {
119 if (agentx_callback_sess == NULL) {
120 agentx_callback_sess = netsnmp_callback_open(callback_master_num,
121 handle_subagent_response,
122 NULL, NULL);
123 DEBUGMSGTL(("agentx/subagent", "subagent_init sess %p\n",
124 agentx_callback_sess));
125 }
126 }
127
128 static int subagent_init_init = 0;
129 /**
130 * init subagent callback (local) session and connect to master agent
131 *
132 * @returns 0 for success, !0 otherwise
133 */
134 int
135 subagent_init(void)
136 {
137 int rc = 0;
138
139 DEBUGMSGTL(("agentx/subagent", "initializing....\n"));
140
141 if (++subagent_init_init != 1)
142 return 0;
143
144 netsnmp_assert(netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
145 NETSNMP_DS_AGENT_ROLE) == SUB_AGENT);
146
147 #ifndef NETSNMP_TRANSPORT_CALLBACK_DOMAIN
148 snmp_log(LOG_WARNING,"AgentX subagent has been disabled because "
149 "the callback transport is not available.\n");
150 return -1;
151 #endif /* NETSNMP_TRANSPORT_CALLBACK_DOMAIN */
152
153 /*
154 * open (local) callback session
155 */
156 subagent_init_callback_session();
157 if (NULL == agentx_callback_sess)
158 return -1;
159
160 snmp_register_callback(SNMP_CALLBACK_LIBRARY,
161 SNMP_CALLBACK_POST_READ_CONFIG,
162 subagent_startup, NULL);
163
164 DEBUGMSGTL(("agentx/subagent", "initializing.... DONE\n"));
165
166 return rc;
167 }
168
169 #ifndef NETSNMP_FEATURE_REMOVE_AGENTX_ENABLE_SUBAGENT
170 void
171 netsnmp_enable_subagent(void) {
172 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE,
173 SUB_AGENT);
174 }
175 #endif /* NETSNMP_FEATURE_REMOVE_AGENTX_ENABLE_SUBAGENT */
176
177 #ifndef NETSNMP_NO_WRITE_SUPPORT
178 struct agent_netsnmp_set_info *
179 save_set_vars(netsnmp_session * ss, netsnmp_pdu *pdu)
180 {
181 struct agent_netsnmp_set_info *ptr;
182
183 ptr = (struct agent_netsnmp_set_info *)
184 malloc(sizeof(struct agent_netsnmp_set_info));
185 if (ptr == NULL)
186 return NULL;
187
188 /*
189 * Save the important information
190 */
191 ptr->transID = pdu->transid;
192 ptr->sess = ss;
193 ptr->mode = SNMP_MSG_INTERNAL_SET_RESERVE1;
194 ptr->uptime = netsnmp_get_agent_uptime();
195
196 ptr->var_list = snmp_clone_varbind(pdu->variables);
197 if (ptr->var_list == NULL) {
198 free(ptr);
199 return NULL;
200 }
201
202 ptr->next = Sets;
203 Sets = ptr;
204
205 return ptr;
206 }
207
208 struct agent_netsnmp_set_info *
209 restore_set_vars(netsnmp_session * sess, netsnmp_pdu *pdu)
210 {
211 struct agent_netsnmp_set_info *ptr;
212
213 for (ptr = Sets; ptr != NULL; ptr = ptr->next)
214 if (ptr->sess == sess && ptr->transID == pdu->transid)
215 break;
216
217 if (ptr == NULL || ptr->var_list == NULL)
218 return NULL;
219
220 pdu->variables = snmp_clone_varbind(ptr->var_list);
221 if (pdu->variables == NULL)
222 return NULL;
223
224 return ptr;
225 }
226
227
228 void
229 free_set_vars(netsnmp_session * ss, netsnmp_pdu *pdu)
230 {
231 struct agent_netsnmp_set_info *ptr, *prev = NULL;
232
233 for (ptr = Sets; ptr != NULL; ptr = ptr->next) {
234 if (ptr->sess == ss && ptr->transID == pdu->transid) {
235 if (prev)
236 prev->next = ptr->next;
237 else
238 Sets = ptr->next;
239 snmp_free_varbind(ptr->var_list);
240 free(ptr);
241 return;
242 }
243 prev = ptr;
244 }
245 }
246
247 static void
248 send_agentx_error(netsnmp_session *session, netsnmp_pdu *pdu, int errstat, int errindex)
249 {
250 pdu = snmp_clone_pdu(pdu);
251 if (!pdu)
252 return;
253 pdu->command = AGENTX_MSG_RESPONSE;
254 pdu->version = session->version;
255 pdu->errstat = errstat;
256 pdu->errindex = errindex;
257 snmp_free_varbind(pdu->variables);
258 pdu->variables = NULL;
259
260 DEBUGMSGTL(("agentx/subagent", "Sending AgentX response error stat %d idx %d\n",
261 errstat, errindex));
262 if (!snmp_send(session, pdu)) {
263 snmp_free_pdu(pdu);
264 }
265 }
266 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
267
268 int
269 handle_agentx_packet(int operation, netsnmp_session * session, int reqid,
270 netsnmp_pdu *pdu, void *magic)
271 {
272 #ifndef NETSNMP_NO_WRITE_SUPPORT
273 struct agent_netsnmp_set_info *asi = NULL;
274 #endif
275 snmp_callback mycallback;
276 netsnmp_pdu *internal_pdu = NULL;
277 void *retmagic = NULL;
278 ns_subagent_magic *smagic = NULL;
279 int result;
280
281 if (operation == NETSNMP_CALLBACK_OP_DISCONNECT) {
282 struct synch_state *state = (struct synch_state *) magic;
283 int period =
284 netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
285 NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL);
286 DEBUGMSGTL(("agentx/subagent",
287 "transport disconnect indication\n"));
288
289 /*
290 * deal with existing session. This happend if agentx sends
291 * a message to the master, but the master goes away before
292 * a response is sent. agentx will spin in snmp_synch_response_cb,
293 * waiting for a response. At the very least, the waiting
294 * flag must be set to break that loop. The rest is copied
295 * from disconnect handling in snmp_sync_input.
296 */
297 if(state) {
298 state->waiting = 0;
299 state->pdu = NULL;
300 state->status = STAT_ERROR;
301 session->s_snmp_errno = SNMPERR_ABORT;
302 SET_SNMP_ERROR(SNMPERR_ABORT);
303 }
304
305 /*
306 * Deregister the ping alarm, if any, and invalidate all other
307 * references to this session.
308 */
309 if (session->securityModel != SNMP_DEFAULT_SECMODEL) {
310 snmp_alarm_unregister(session->securityModel);
311 }
312 snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
313 SNMPD_CALLBACK_INDEX_STOP, (void *) session);
314 agentx_unregister_callbacks(session);
315 remove_trap_session(session);
316 register_mib_detach();
317 main_session = NULL;
318 if (period != 0) {
319 /*
320 * Pings are enabled, so periodically attempt to re-establish contact
321 * with the master agent. Don't worry about the handle,
322 * agentx_reopen_session unregisters itself if it succeeds in talking
323 * to the master agent.
324 */
325 snmp_alarm_register(period, SA_REPEAT, agentx_reopen_session, NULL);
326 snmp_log(LOG_INFO, "AgentX master disconnected us, reconnecting in %d\n", period);
327 } else {
328 snmp_log(LOG_INFO, "AgentX master disconnected us, not reconnecting\n");
329 }
330 return 0;
331 } else if (operation != NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
332 DEBUGMSGTL(("agentx/subagent", "unexpected callback op %d\n",
333 operation));
334 return 1;
335 }
336
337 /*
338 * ok, we have a pdu from the net. Modify as needed
339 */
340
341 DEBUGMSGTL(("agentx/subagent", "handling AgentX request (req=0x%x,trans="
342 "0x%x,sess=0x%x)\n", (unsigned)pdu->reqid,
343 (unsigned)pdu->transid, (unsigned)pdu->sessid));
344 pdu->version = AGENTX_VERSION_1;
345 pdu->flags |= UCD_MSG_FLAG_ALWAYS_IN_VIEW;
346
347 /* Master agent is alive, no need to ping */
348 if (session->securityModel != SNMP_DEFAULT_SECMODEL) {
349 snmp_alarm_reset(session->securityModel);
350 }
351
352 if (pdu->command == AGENTX_MSG_GET
353 || pdu->command == AGENTX_MSG_GETNEXT
354 || pdu->command == AGENTX_MSG_GETBULK) {
355 smagic =
356 (ns_subagent_magic *) calloc(1, sizeof(ns_subagent_magic));
357 if (smagic == NULL) {
358 DEBUGMSGTL(("agentx/subagent", "couldn't malloc() smagic\n"));
359 /* would like to send_agentx_error(), but it needs memory too */
360 return 1;
361 }
362 smagic->original_command = pdu->command;
363 smagic->session = session;
364 smagic->ovars = NULL;
365 retmagic = (void *) smagic;
366 }
367
368 switch (pdu->command) {
369 case AGENTX_MSG_GET:
370 DEBUGMSGTL(("agentx/subagent", " -> get\n"));
371 pdu->command = SNMP_MSG_GET;
372 mycallback = handle_subagent_response;
373 break;
374
375 case AGENTX_MSG_GETNEXT:
376 DEBUGMSGTL(("agentx/subagent", " -> getnext\n"));
377 pdu->command = SNMP_MSG_GETNEXT;
378
379 /*
380 * We have to save a copy of the original variable list here because
381 * if the master agent has requested scoping for some of the varbinds
382 * that information is stored there.
383 */
384
385 smagic->ovars = snmp_clone_varbind(pdu->variables);
386 DEBUGMSGTL(("agentx/subagent", "saved variables\n"));
387 mycallback = handle_subagent_response;
388 break;
389
390 case AGENTX_MSG_GETBULK:
391 /*
392 * WWWXXX
393 */
394 DEBUGMSGTL(("agentx/subagent", " -> getbulk\n"));
395 pdu->command = SNMP_MSG_GETBULK;
396
397 /*
398 * We have to save a copy of the original variable list here because
399 * if the master agent has requested scoping for some of the varbinds
400 * that information is stored there.
401 */
402
403 smagic->ovars = snmp_clone_varbind(pdu->variables);
404 DEBUGMSGTL(("agentx/subagent", "saved variables at %p\n",
405 smagic->ovars));
406 mycallback = handle_subagent_response;
407 break;
408
409 case AGENTX_MSG_RESPONSE:
410 SNMP_FREE(smagic);
411 DEBUGMSGTL(("agentx/subagent", " -> response\n"));
412 return 1;
413
414 #ifndef NETSNMP_NO_WRITE_SUPPORT
415 case AGENTX_MSG_TESTSET:
416 /*
417 * XXXWWW we have to map this twice to both RESERVE1 and RESERVE2
418 */
419 DEBUGMSGTL(("agentx/subagent", " -> testset\n"));
420 asi = save_set_vars(session, pdu);
421 if (asi == NULL) {
422 SNMP_FREE(smagic);
423 snmp_log(LOG_WARNING, "save_set_vars() failed\n");
424 send_agentx_error(session, pdu, AGENTX_ERR_PARSE_FAILED, 0);
425 return 1;
426 }
427 asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_RESERVE1;
428 mycallback = handle_subagent_set_response;
429 retmagic = asi;
430 break;
431
432 case AGENTX_MSG_COMMITSET:
433 DEBUGMSGTL(("agentx/subagent", " -> commitset\n"));
434 asi = restore_set_vars(session, pdu);
435 if (asi == NULL) {
436 SNMP_FREE(smagic);
437 snmp_log(LOG_WARNING, "restore_set_vars() failed\n");
438 send_agentx_error(session, pdu, AGENTX_ERR_PROCESSING_ERROR, 0);
439 return 1;
440 }
441 if (asi->mode != SNMP_MSG_INTERNAL_SET_RESERVE2) {
442 SNMP_FREE(smagic);
443 snmp_log(LOG_WARNING,
444 "dropping bad AgentX request (wrong mode %d)\n",
445 asi->mode);
446 send_agentx_error(session, pdu, AGENTX_ERR_PROCESSING_ERROR, 0);
447 return 1;
448 }
449 asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_ACTION;
450 mycallback = handle_subagent_set_response;
451 retmagic = asi;
452 break;
453
454 case AGENTX_MSG_CLEANUPSET:
455 DEBUGMSGTL(("agentx/subagent", " -> cleanupset\n"));
456 asi = restore_set_vars(session, pdu);
457 if (asi == NULL) {
458 SNMP_FREE(smagic);
459 snmp_log(LOG_WARNING, "restore_set_vars() failed\n");
460 send_agentx_error(session, pdu, AGENTX_ERR_PROCESSING_ERROR, 0);
461 return 1;
462 }
463 if (asi->mode == SNMP_MSG_INTERNAL_SET_RESERVE1 ||
464 asi->mode == SNMP_MSG_INTERNAL_SET_RESERVE2) {
465 asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_FREE;
466 } else if (asi->mode == SNMP_MSG_INTERNAL_SET_ACTION) {
467 asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_COMMIT;
468 } else {
469 snmp_log(LOG_WARNING,
470 "dropping bad AgentX request (wrong mode %d)\n",
471 asi->mode);
472 SNMP_FREE(retmagic);
473 return 1;
474 }
475 mycallback = handle_subagent_set_response;
476 retmagic = asi;
477 break;
478
479 case AGENTX_MSG_UNDOSET:
480 DEBUGMSGTL(("agentx/subagent", " -> undoset\n"));
481 asi = restore_set_vars(session, pdu);
482 if (asi == NULL) {
483 SNMP_FREE(smagic);
484 snmp_log(LOG_WARNING, "restore_set_vars() failed\n");
485 send_agentx_error(session, pdu, AGENTX_ERR_PROCESSING_ERROR, 0);
486 return 1;
487 }
488 asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_UNDO;
489 mycallback = handle_subagent_set_response;
490 retmagic = asi;
491 break;
492 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
493
494 default:
495 SNMP_FREE(smagic);
496 DEBUGMSGTL(("agentx/subagent", " -> unknown command %d (%02x)\n",
497 pdu->command, pdu->command));
498 return 0;
499 }
500
501 /*
502 * submit the pdu to the internal handler
503 */
504
505 /*
506 * We have to clone the PDU here, because when we return from this
507 * callback, sess_process_packet will free(pdu), but this call also
508 * free()s its argument PDU.
509 */
510
511 internal_pdu = snmp_clone_pdu(pdu);
512 if (!internal_pdu) {
513 free(smagic);
514 return 1;
515 }
516 free(internal_pdu->contextName);
517 internal_pdu->contextName = (char *) internal_pdu->community;
518 internal_pdu->contextNameLen = internal_pdu->community_len;
519 internal_pdu->community = NULL;
520 internal_pdu->community_len = 0;
521 result = snmp_async_send(agentx_callback_sess, internal_pdu, mycallback,
522 retmagic);
523 if (result == 0) {
524 snmp_free_pdu( internal_pdu );
525 }
526 return 1;
527 }
528
529 static int
530 _invalid_op_and_magic(int op, ns_subagent_magic *smagic)
531 {
532 int invalid = 0;
533
534 if (smagic && (snmp_sess_pointer(smagic->session) == NULL ||
535 op == NETSNMP_CALLBACK_OP_TIMED_OUT)) {
536 if (smagic->ovars != NULL) {
537 snmp_free_varbind(smagic->ovars);
538 }
539 free(smagic);
540 invalid = 1;
541 }
542
543 if (op != NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE || smagic == NULL)
544 invalid = 1;
545
546 return invalid;
547 }
548
549 int
550 handle_subagent_response(int op, netsnmp_session * session, int reqid,
551 netsnmp_pdu *pdu, void *magic)
552 {
553 ns_subagent_magic *smagic = (ns_subagent_magic *) magic;
554 netsnmp_variable_list *u = NULL, *v = NULL;
555 int rc = 0;
556
557 if (_invalid_op_and_magic(op, magic)) {
558 return 1;
559 }
560
561 pdu = snmp_clone_pdu(pdu);
562 if (!pdu)
563 return 1;
564 DEBUGMSGTL(("agentx/subagent",
565 "handling AgentX response (cmd 0x%02x orig_cmd 0x%02x)"
566 " (req=0x%x,trans=0x%x,sess=0x%x)\n",
567 pdu->command, smagic->original_command,
568 (unsigned)pdu->reqid, (unsigned)pdu->transid,
569 (unsigned)pdu->sessid));
570
571 #ifndef NETSNMP_NO_WRITE_SUPPORT
572 if (pdu->command == SNMP_MSG_INTERNAL_SET_FREE ||
573 pdu->command == SNMP_MSG_INTERNAL_SET_UNDO ||
574 pdu->command == SNMP_MSG_INTERNAL_SET_COMMIT) {
575 free_set_vars(smagic->session, pdu);
576 }
577 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
578
579 if (smagic->original_command == AGENTX_MSG_GETNEXT) {
580 DEBUGMSGTL(("agentx/subagent",
581 "do getNext scope processing %p %p\n", smagic->ovars,
582 pdu->variables));
583 for (u = smagic->ovars, v = pdu->variables; u != NULL && v != NULL;
584 u = u->next_variable, v = v->next_variable) {
585 if (snmp_oid_compare
586 (u->val.objid, u->val_len / sizeof(oid), nullOid,
587 nullOidLen/sizeof(oid)) != 0) {
588 /*
589 * The master agent requested scoping for this variable.
590 */
591 rc = snmp_oid_compare(v->name, v->name_length,
592 u->val.objid,
593 u->val_len / sizeof(oid));
594 DEBUGMSGTL(("agentx/subagent", "result "));
595 DEBUGMSGOID(("agentx/subagent", v->name, v->name_length));
596 DEBUGMSG(("agentx/subagent", " scope to "));
597 DEBUGMSGOID(("agentx/subagent",
598 u->val.objid, u->val_len / sizeof(oid)));
599 DEBUGMSG(("agentx/subagent", " result %d\n", rc));
600
601 if (rc >= 0) {
602 /*
603 * The varbind is out of scope. From RFC2741, p. 66: "If
604 * the subagent cannot locate an appropriate variable,
605 * v.name is set to the starting OID, and the VarBind is
606 * set to `endOfMibView'".
607 */
608 snmp_set_var_objid(v, u->name, u->name_length);
609 snmp_set_var_typed_value(v, SNMP_ENDOFMIBVIEW, NULL, 0);
610 DEBUGMSGTL(("agentx/subagent",
611 "scope violation -- return endOfMibView\n"));
612 }
613 } else {
614 DEBUGMSGTL(("agentx/subagent", "unscoped var\n"));
615 }
616 }
617 }
618
619 /*
620 * XXXJBPN: similar for GETBULK but the varbinds can get re-ordered I
621 * think which makes it er more difficult.
622 */
623
624 if (smagic->ovars != NULL) {
625 snmp_free_varbind(smagic->ovars);
626 }
627
628 pdu->command = AGENTX_MSG_RESPONSE;
629 pdu->version = smagic->session->version;
630
631 if (!snmp_send(smagic->session, pdu)) {
632 snmp_free_pdu(pdu);
633 }
634 DEBUGMSGTL(("agentx/subagent", " FINISHED\n"));
635 free(smagic);
636 return 1;
637 }
638
639 #ifndef NETSNMP_NO_WRITE_SUPPORT
640 int
641 handle_subagent_set_response(int op, netsnmp_session * session, int reqid,
642 netsnmp_pdu *pdu, void *magic)
643 {
644 netsnmp_session *retsess;
645 struct agent_netsnmp_set_info *asi;
646 int result;
647
648 if (op != NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE || magic == NULL) {
649 return 1;
650 }
651
652 DEBUGMSGTL(("agentx/subagent",
653 "handling agentx subagent set response (mode=%d,req=0x%x,"
654 "trans=0x%x,sess=0x%x)\n",
655 (unsigned)pdu->command, (unsigned)pdu->reqid,
656 (unsigned)pdu->transid, (unsigned)pdu->sessid));
657 pdu = snmp_clone_pdu(pdu);
658 if (!pdu)
659 return 1;
660
661 asi = (struct agent_netsnmp_set_info *) magic;
662 retsess = asi->sess;
663 asi->errstat = pdu->errstat;
664
665 if (asi->mode == SNMP_MSG_INTERNAL_SET_RESERVE1) {
666 /*
667 * reloop for RESERVE2 mode, an internal only agent mode
668 */
669 /*
670 * XXX: check exception statuses of reserve1 first
671 */
672 if (!pdu->errstat) {
673 asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_RESERVE2;
674 result = snmp_async_send(agentx_callback_sess, pdu,
675 handle_subagent_set_response, asi);
676 if (result == 0) {
677 snmp_free_pdu( pdu );
678 }
679 DEBUGMSGTL(("agentx/subagent",
680 " going from RESERVE1 -> RESERVE2\n"));
681 return 1;
682 }
683 } else {
684 if (asi->mode == SNMP_MSG_INTERNAL_SET_FREE ||
685 asi->mode == SNMP_MSG_INTERNAL_SET_UNDO ||
686 asi->mode == SNMP_MSG_INTERNAL_SET_COMMIT) {
687 free_set_vars(retsess, pdu);
688 }
689 snmp_free_varbind(pdu->variables);
690 pdu->variables = NULL; /* the variables were added by us */
691 }
692
693 if (retsess && pdu) {
694 pdu->command = AGENTX_MSG_RESPONSE;
695 pdu->version = retsess->version;
696
697 if (!snmp_send(retsess, pdu))
698 snmp_free_pdu(pdu);
699 } else if (pdu) {
700 snmp_free_pdu(pdu);
701 }
702 DEBUGMSGTL(("agentx/subagent", " FINISHED\n"));
703 return 1;
704 }
705 #endif /* !NETSNMP_NO_WRITE_SUPPORT */
706
707
708 int
709 agentx_registration_callback(int majorID, int minorID, void *serverarg,
710 void *clientarg)
711 {
712 struct register_parameters *reg_parms =
713 (struct register_parameters *) serverarg;
714 netsnmp_session *agentx_ss = *(netsnmp_session **)clientarg;
715
716 if (minorID == SNMPD_CALLBACK_REGISTER_OID)
717 return agentx_register(agentx_ss,
718 reg_parms->name, reg_parms->namelen,
719 reg_parms->priority,
720 reg_parms->range_subid,
721 reg_parms->range_ubound, reg_parms->timeout,
722 reg_parms->flags,
723 reg_parms->contextName);
724 else
725 return agentx_unregister(agentx_ss,
726 reg_parms->name, reg_parms->namelen,
727 reg_parms->priority,
728 reg_parms->range_subid,
729 reg_parms->range_ubound,
730 reg_parms->contextName);
731 }
732
733
734 static int
735 agentx_sysOR_callback(int majorID, int minorID, void *serverarg,
736 void *clientarg)
737 {
738 const struct register_sysOR_parameters *reg_parms =
739 (const struct register_sysOR_parameters *) serverarg;
740 netsnmp_session *agentx_ss = *(netsnmp_session **)clientarg;
741
742 if (minorID == SNMPD_CALLBACK_REG_SYSOR)
743 return agentx_add_agentcaps(agentx_ss,
744 reg_parms->name, reg_parms->namelen,
745 reg_parms->descr);
746 else
747 return agentx_remove_agentcaps(agentx_ss,
748 reg_parms->name,
749 reg_parms->namelen);
750 }
751
752
753 static int
754 subagent_shutdown(int majorID, int minorID, void *serverarg, void *clientarg)
755 {
756 netsnmp_session *thesession = *(netsnmp_session **)clientarg;
757 DEBUGMSGTL(("agentx/subagent", "shutting down session....\n"));
758 if (thesession == NULL) {
759 DEBUGMSGTL(("agentx/subagent", "Empty session to shutdown\n"));
760 main_session = NULL;
761 return 0;
762 }
763 agentx_close_session(thesession, AGENTX_CLOSE_SHUTDOWN);
764 if (main_session != NULL) {
765 remove_trap_session(main_session);
766 main_session = NULL;
767 }
768 snmp_close(thesession);
769 DEBUGMSGTL(("agentx/subagent", "shut down finished.\n"));
770
771 subagent_init_init = 0;
772 return 1;
773 }
774
775
776
777 /*
778 * Register all the "standard" AgentX callbacks for the given session.
779 */
780
781 void
782 agentx_register_callbacks(netsnmp_session * s)
783 {
784 netsnmp_session *sess_p;
785
786 DEBUGMSGTL(("agentx/subagent",
787 "registering callbacks for session %p\n", s));
788 sess_p = netsnmp_memdup(&s, sizeof(s));
789 netsnmp_assert(sess_p);
790 s->myvoid = sess_p;
791 if (!sess_p)
792 return;
793 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN,
794 subagent_shutdown, sess_p);
795 snmp_register_callback(SNMP_CALLBACK_APPLICATION,
796 SNMPD_CALLBACK_REGISTER_OID,
797 agentx_registration_callback, sess_p);
798 snmp_register_callback(SNMP_CALLBACK_APPLICATION,
799 SNMPD_CALLBACK_UNREGISTER_OID,
800 agentx_registration_callback, sess_p);
801 snmp_register_callback(SNMP_CALLBACK_APPLICATION,
802 SNMPD_CALLBACK_REG_SYSOR,
803 agentx_sysOR_callback, sess_p);
804 snmp_register_callback(SNMP_CALLBACK_APPLICATION,
805 SNMPD_CALLBACK_UNREG_SYSOR,
806 agentx_sysOR_callback, sess_p);
807 }
808
809 /*
810 * Unregister all the callbacks associated with this session.
811 */
812
813 void
814 agentx_unregister_callbacks(netsnmp_session * ss)
815 {
816 DEBUGMSGTL(("agentx/subagent",
817 "unregistering callbacks for session %p\n", ss));
818 snmp_unregister_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN,
819 subagent_shutdown, ss->myvoid, 1);
820 snmp_unregister_callback(SNMP_CALLBACK_APPLICATION,
821 SNMPD_CALLBACK_REGISTER_OID,
822 agentx_registration_callback, ss->myvoid, 1);
823 snmp_unregister_callback(SNMP_CALLBACK_APPLICATION,
824 SNMPD_CALLBACK_UNREGISTER_OID,
825 agentx_registration_callback, ss->myvoid, 1);
826 snmp_unregister_callback(SNMP_CALLBACK_APPLICATION,
827 SNMPD_CALLBACK_REG_SYSOR,
828 agentx_sysOR_callback, ss->myvoid, 1);
829 snmp_unregister_callback(SNMP_CALLBACK_APPLICATION,
830 SNMPD_CALLBACK_UNREG_SYSOR,
831 agentx_sysOR_callback, ss->myvoid, 1);
832 SNMP_FREE(ss->myvoid);
833 }
834
835 /*
836 * Open a session to the master agent.
837 */
838 int
839 subagent_open_master_session(void)
840 {
841 netsnmp_transport *t;
842 netsnmp_session sess;
843 const char *agentx_socket;
844
845 DEBUGMSGTL(("agentx/subagent", "opening session...\n"));
846
847 if (main_session) {
848 snmp_log(LOG_WARNING,
849 "AgentX session to master agent attempted to be re-opened.\n");
850 return -1;
851 }
852
853 snmp_sess_init(&sess);
854 sess.version = AGENTX_VERSION_1;
855 sess.retries = SNMP_DEFAULT_RETRIES;
856 sess.timeout = SNMP_DEFAULT_TIMEOUT;
857 sess.flags |= SNMP_FLAGS_STREAM_SOCKET;
858 sess.callback = handle_agentx_packet;
859 sess.authenticator = NULL;
860
861 agentx_socket = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
862 NETSNMP_DS_AGENT_X_SOCKET);
863 t = netsnmp_transport_open_client("agentx", agentx_socket);
864 if (t == NULL) {
865 /*
866 * Diagnose snmp_open errors with the input
867 * netsnmp_session pointer.
868 */
869 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
870 NETSNMP_DS_AGENT_NO_CONNECTION_WARNINGS)) {
871 char buf[1024];
872 snprintf(buf, sizeof(buf), "Warning: "
873 "Failed to connect to the agentx master agent (%s)",
874 agentx_socket ? agentx_socket : "[NIL]");
875 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
876 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
877 netsnmp_sess_log_error(LOG_WARNING, buf, &sess);
878 } else {
879 snmp_sess_perror(buf, &sess);
880 }
881 }
882 return -1;
883 }
884
885 main_session =
886 snmp_add_full(&sess, t, NULL, agentx_parse, NULL, NULL,
887 agentx_realloc_build, agentx_check_packet, NULL);
888
889 if (main_session == NULL) {
890 /* snmp_add_full() frees 't' upon failure. */
891 t = NULL;
892 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
893 NETSNMP_DS_AGENT_NO_CONNECTION_WARNINGS)) {
894 char buf[1024];
895 snprintf(buf, sizeof(buf), "Error: "
896 "Failed to create the agentx master agent session (%s)",
897 agentx_socket);
898 snmp_sess_perror(buf, &sess);
899 }
900 netsnmp_transport_free(t);
901 return -1;
902 }
903
904 /*
905 * I don't know why 1 is success instead of the usual 0 = noerr,
906 * but that's what the function returns.
907 */
908 if (1 != agentx_open_session(main_session)) {
909 snmp_close(main_session);
910 main_session = NULL;
911 return -1;
912 }
913
914 /*
915 * subagent_register_ping_alarm assumes that securityModel will
916 * be set to SNMP_DEFAULT_SECMODEL on new AgentX sessions.
917 * This field is then (ab)used to hold the alarm stash.
918 *
919 * Why is the securityModel field used for this purpose, I hear you ask.
920 * Damn good question! (See SVN revision 4886)
921 */
922 main_session->securityModel = SNMP_DEFAULT_SECMODEL;
923
924 if (add_trap_session(main_session, AGENTX_MSG_NOTIFY, 1,
925 AGENTX_VERSION_1)) {
926 DEBUGMSGTL(("agentx/subagent", " trap session registered OK\n"));
927 } else {
928 DEBUGMSGTL(("agentx/subagent",
929 "trap session registration failed\n"));
930 snmp_close(main_session);
931 main_session = NULL;
932 return -1;
933 }
934
935 agentx_register_callbacks(main_session);
936
937 snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
938 SNMPD_CALLBACK_INDEX_START, (void *) main_session);
939
940 snmp_log(LOG_INFO, "NET-SNMP version %s AgentX subagent connected\n",
941 netsnmp_get_version());
942 DEBUGMSGTL(("agentx/subagent", "opening session... DONE (%p)\n",
943 main_session));
944
945 return 0;
946 }
947
948 static void
949 agentx_reopen_sysORTable(const struct sysORTable* data, void* v)
950 {
951 netsnmp_session *agentx_ss = (netsnmp_session *) v;
952
953 agentx_add_agentcaps(agentx_ss, data->OR_oid, data->OR_oidlen,
954 data->OR_descr);
955 }
956
957 /*
958 * Alarm callback function to open a session to the master agent. If a
959 * transport disconnection callback occurs, indicating that the master agent
960 * has died (or there has been some strange communication problem), this
961 * alarm is called repeatedly to try to re-open the connection.
962 */
963
964 void
965 agentx_reopen_session(unsigned int clientreg, void *clientarg)
966 {
967 DEBUGMSGTL(("agentx/subagent", "agentx_reopen_session(%d) called\n",
968 clientreg));
969
970 if (subagent_open_master_session() == 0) {
971 /*
972 * Successful. Delete the alarm handle if one exists.
973 */
974 if (clientreg != 0) {
975 snmp_alarm_unregister(clientreg);
976 }
977
978 /*
979 * Reregister all our nodes.
980 */
981 register_mib_reattach();
982
983 /*
984 * Reregister all our sysOREntries
985 */
986 netsnmp_sysORTable_foreach(&agentx_reopen_sysORTable, main_session);
987
988 /*
989 * Register a ping alarm (if need be).
990 */
991 subagent_register_ping_alarm(0, 0, NULL, main_session);
992 } else {
993 if (clientreg == 0) {
994 /*
995 * Register a reattach alarm for later
996 */
997 subagent_register_ping_alarm(0, 0, NULL, main_session);
998 }
999 }
1000 }
1001
1002 /*
1003 * If a valid session is passed in (through clientarg), register a
1004 * ping handler to ping it frequently, else register an attempt to try
1005 * and open it again later.
1006 */
1007
1008 static int
1009 subagent_register_ping_alarm(int majorID, int minorID,
1010 void *serverarg, void *clientarg)
1011 {
1012
1013 netsnmp_session *ss = (netsnmp_session *) clientarg;
1014 int ping_interval =
1015 netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
1016 NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL);
1017
1018 if (!ping_interval) /* don't do anything if not setup properly */
1019 return 0;
1020
1021 /*
1022 * register a ping alarm, if desired
1023 */
1024 if (ss) {
1025 if (ss->securityModel != SNMP_DEFAULT_SECMODEL) {
1026 DEBUGMSGTL(("agentx/subagent",
1027 "unregister existing alarm %d\n",
1028 ss->securityModel));
1029 snmp_alarm_unregister(ss->securityModel);
1030 }
1031
1032 DEBUGMSGTL(("agentx/subagent",
1033 "register ping alarm every %d seconds\n",
1034 ping_interval));
1035 /*
1036 * we re-use the securityModel parameter for an alarm stash,
1037 * since agentx doesn't need it
1038 */
1039 ss->securityModel = snmp_alarm_register(ping_interval, SA_REPEAT,
1040 agentx_check_session, ss);
1041 } else {
1042 /*
1043 * attempt to open it later instead
1044 */
1045 DEBUGMSGTL(("agentx/subagent",
1046 "subagent not properly attached, postponing registration till later....\n"));
1047 snmp_alarm_register(ping_interval, SA_REPEAT,
1048 agentx_reopen_session, NULL);
1049 }
1050 return 0;
1051 }
1052
1053 /*
1054 * check a session validity for connectivity to the master agent. If
1055 * not functioning, close and start attempts to reopen the session
1056 */
1057 void
1058 agentx_check_session(unsigned int clientreg, void *clientarg)
1059 {
1060 netsnmp_session *ss = (netsnmp_session *) clientarg;
1061 if (!ss) {
1062 if (clientreg)
1063 snmp_alarm_unregister(clientreg);
1064 return;
1065 }
1066 DEBUGMSGTL(("agentx/subagent", "checking status of session %p\n", ss));
1067
1068 if (!agentx_send_ping(ss)) {
1069 snmp_log(LOG_WARNING,
1070 "AgentX master agent failed to respond to ping. Attempting to re-register.\n");
1071 /*
1072 * master agent disappeared? Try and re-register.
1073 * close first, just to be sure .
1074 */
1075 agentx_unregister_callbacks(ss);
1076 agentx_close_session(ss, AGENTX_CLOSE_TIMEOUT);
1077 snmp_alarm_unregister(clientreg); /* delete ping alarm timer */
1078 snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
1079 SNMPD_CALLBACK_INDEX_STOP, (void *) ss);
1080 register_mib_detach();
1081 if (main_session != NULL) {
1082 remove_trap_session(ss);
1083 snmp_close(main_session);
1084 /*
1085 * We need to remove the callbacks attached to the callback
1086 * session because they have a magic callback data structure
1087 * which includes a pointer to the main session
1088 * (which is no longer valid).
1089 *
1090 * Given that the main session is not responsive anyway.
1091 * it shoudn't matter if we lose some outstanding requests.
1092 */
1093 if (agentx_callback_sess != NULL ) {
1094 snmp_close(agentx_callback_sess);
1095 agentx_callback_sess = NULL;
1096
1097 subagent_init_callback_session();
1098 }
1099 main_session = NULL;
1100 agentx_reopen_session(0, NULL);
1101 }
1102 else {
1103 snmp_close(main_session);
1104 main_session = NULL;
1105 }
1106 } else {
1107 DEBUGMSGTL(("agentx/subagent", "session %p responded to ping\n",
1108 ss));
1109 }
1110 }
1111
1112
1113 #endif /* USING_AGENTX_SUBAGENT_MODULE */