Debian Codegraph @

agent/helpers/baby_steps.c

raw
1 /*
2 * baby_steps.c
3 * $Id$
4 *
5 * Portions of this file are copyrighted by:
6 * Copyright (c) 2016 VMware, Inc. All rights reserved.
7 * Use is subject to license terms specified in the COPYING file
8 * distributed with the Net-SNMP package.
9 */
10 #include <net-snmp/net-snmp-config.h>
11 #include <net-snmp/net-snmp-features.h>
12 #include <net-snmp/net-snmp-includes.h>
13 #include <net-snmp/agent/net-snmp-agent-includes.h>
14
15 netsnmp_feature_provide(baby_steps);
16 netsnmp_feature_child_of(baby_steps, mib_helpers);
17
18 #ifdef NETSNMP_FEATURE_REQUIRE_BABY_STEPS
19 netsnmp_feature_require(check_requests_error);
20 #endif
21
22 #ifndef NETSNMP_FEATURE_REMOVE_BABY_STEPS
23
24 #include <net-snmp/agent/baby_steps.h>
25
26 #define BABY_STEPS_PER_MODE_MAX 4
27 #define BSTEP_USE_ORIGINAL 0xffff
28
29 static u_short get_mode_map[BABY_STEPS_PER_MODE_MAX] = {
30 MODE_BSTEP_PRE_REQUEST, MODE_BSTEP_OBJECT_LOOKUP, BSTEP_USE_ORIGINAL, MODE_BSTEP_POST_REQUEST };
31
32 #ifndef NETSNMP_NO_WRITE_SUPPORT
33 static u_short set_mode_map[SNMP_MSG_INTERNAL_SET_MAX][BABY_STEPS_PER_MODE_MAX] = {
34 /*R1*/
35 { MODE_BSTEP_PRE_REQUEST, MODE_BSTEP_OBJECT_LOOKUP, MODE_BSTEP_ROW_CREATE,
36 MODE_BSTEP_CHECK_VALUE },
37 /*R2*/
38 { MODE_BSTEP_UNDO_SETUP, BABY_STEP_NONE, BABY_STEP_NONE, BABY_STEP_NONE },
39 /*A */
40 { MODE_BSTEP_SET_VALUE,MODE_BSTEP_CHECK_CONSISTENCY,
41 MODE_BSTEP_COMMIT, BABY_STEP_NONE },
42 /*C */
43 { MODE_BSTEP_IRREVERSIBLE_COMMIT, MODE_BSTEP_UNDO_CLEANUP, MODE_BSTEP_POST_REQUEST,
44 BABY_STEP_NONE},
45 /*F */
46 { MODE_BSTEP_UNDO_CLEANUP, MODE_BSTEP_POST_REQUEST, BABY_STEP_NONE,
47 BABY_STEP_NONE },
48 /*U */
49 { MODE_BSTEP_UNDO_COMMIT, MODE_BSTEP_UNDO_SET, MODE_BSTEP_UNDO_CLEANUP,
50 MODE_BSTEP_POST_REQUEST}
51 };
52 #endif /* NETSNMP_NO_WRITE_SUPPORT */
53
54 static int
55 _baby_steps_helper(netsnmp_mib_handler *handler,
56 netsnmp_handler_registration *reginfo,
57 netsnmp_agent_request_info *reqinfo,
58 netsnmp_request_info *requests);
59 static int
60 _baby_steps_access_multiplexer(netsnmp_mib_handler *handler,
61 netsnmp_handler_registration *reginfo,
62 netsnmp_agent_request_info *reqinfo,
63 netsnmp_request_info *requests);
64
65 /** @defgroup baby_steps baby_steps
66 * Calls your handler in baby_steps for set processing.
67 * @ingroup handler
68 * @{
69 */
70
71 static void *
72 netsnmp_baby_steps_modes_ref(void *p)
73 {
74 netsnmp_baby_steps_modes *md = p;
75
76 md->refcnt++;
77 return md;
78 }
79
80 static void
81 netsnmp_baby_steps_modes_deref(void *p)
82 {
83 netsnmp_baby_steps_modes *md = p;
84
85 if (--md->refcnt == 0)
86 free(md);
87 }
88
89 /** returns a baby_steps handler that can be injected into a given
90 * handler chain.
91 */
92 netsnmp_mib_handler *
93 netsnmp_baby_steps_handler_get(u_long modes)
94 {
95 netsnmp_mib_handler *mh;
96 netsnmp_baby_steps_modes *md;
97
98 mh = netsnmp_create_handler("baby_steps", _baby_steps_helper);
99 if(!mh)
100 return NULL;
101
102 md = SNMP_MALLOC_TYPEDEF(netsnmp_baby_steps_modes);
103 if (NULL == md) {
104 snmp_log(LOG_ERR,"malloc failed in netsnmp_baby_steps_handler_get\n");
105 netsnmp_handler_free(mh);
106 mh = NULL;
107 }
108 else {
109 md->refcnt = 1;
110 mh->myvoid = md;
111 mh->data_clone = netsnmp_baby_steps_modes_ref;
112 mh->data_free = netsnmp_baby_steps_modes_deref;
113 if (0 == modes)
114 modes = BABY_STEP_ALL;
115 md->registered = modes;
116 }
117
118 /*
119 * don't set MIB_HANDLER_AUTO_NEXT, since we need to call lower
120 * handlers with a munged mode.
121 */
122
123 return mh;
124 }
125
126 /** @internal Implements the baby_steps handler */
127 static int
128 _baby_steps_helper(netsnmp_mib_handler *handler,
129 netsnmp_handler_registration *reginfo,
130 netsnmp_agent_request_info *reqinfo,
131 netsnmp_request_info *requests)
132 {
133 netsnmp_baby_steps_modes *bs_modes;
134 int save_mode, i, rc = SNMP_ERR_NOERROR;
135 u_short *mode_map_ptr;
136
137 DEBUGMSGTL(("baby_steps", "Got request, mode %s\n",
138 se_find_label_in_slist("agent_mode",reqinfo->mode)));
139
140 bs_modes = (netsnmp_baby_steps_modes*)handler->myvoid;
141 netsnmp_assert(NULL != bs_modes);
142
143 switch (reqinfo->mode) {
144
145 #ifndef NETSNMP_NO_WRITE_SUPPORT
146 case MODE_SET_RESERVE1:
147 /*
148 * clear completed modes
149 * xxx-rks: this will break for pdus with set requests to different
150 * rows in the same table when the handler is set up to use the row
151 * merge helper as well (or if requests are serialized).
152 */
153 bs_modes->completed = 0;
154 /* FALL THROUGH */
155 case MODE_SET_RESERVE2:
156 case MODE_SET_ACTION:
157 case MODE_SET_COMMIT:
158 case MODE_SET_FREE:
159 case MODE_SET_UNDO:
160 mode_map_ptr = set_mode_map[reqinfo->mode];
161 break;
162 #endif /* NETSNMP_NO_WRITE_SUPPORT */
163
164 default:
165 /*
166 * clear completed modes
167 */
168 bs_modes->completed = 0;
169
170 mode_map_ptr = get_mode_map;
171 }
172
173 /*
174 * NOTE: if you update this chart, please update the versions in
175 * local/mib2c-conf.d/parent-set.m2i
176 * agent/mibgroup/helpers/baby_steps.c
177 * while you're at it.
178 */
179 /*
180 ***********************************************************************
181 * Baby Steps Flow Chart (2004.06.05) *
182 * *
183 * +--------------+ +================+ U = unconditional path *
184 * |optional state| ||required state|| S = path for success *
185 * +--------------+ +================+ E = path for error *
186 ***********************************************************************
187 *
188 * +--------------+
189 * | pre |
190 * | request |
191 * +--------------+
192 * | U
193 * +-------------+ +==============+
194 * | row |f|<-------|| object ||
195 * | create |1| E || lookup ||
196 * +-------------+ +==============+
197 * E | | S | S
198 * | +------------------>|
199 * | +==============+
200 * | E || check ||
201 * |<---------------|| values ||
202 * | +==============+
203 * | | S
204 * | +==============+
205 * | +<-------|| undo ||
206 * | | E || setup ||
207 * | | +==============+
208 * | | | S
209 * | | +==============+
210 * | | || set ||-------------------------->+
211 * | | || value || E |
212 * | | +==============+ |
213 * | | | S |
214 * | | +--------------+ |
215 * | | | check |-------------------------->|
216 * | | | consistency | E |
217 * | | +--------------+ |
218 * | | | S |
219 * | | +==============+ +==============+ |
220 * | | || commit ||-------->|| undo || |
221 * | | || || E || commit || |
222 * | | +==============+ +==============+ |
223 * | | | S U |<--------+
224 * | | +--------------+ +==============+
225 * | | | irreversible | || undo ||
226 * | | | commit | || set ||
227 * | | +--------------+ +==============+
228 * | | | U U |
229 * | +-------------->|<------------------------+
230 * | +==============+
231 * | || undo ||
232 * | || cleanup ||
233 * | +==============+
234 * +---------------------->| U
235 * |
236 * (err && f1)------------------->+
237 * | |
238 * +--------------+ +--------------+
239 * | post |<--------| row |
240 * | request | U | release |
241 * +--------------+ +--------------+
242 *
243 */
244 /*
245 * save original mode
246 */
247 save_mode = reqinfo->mode;
248 for(i = 0; i < BABY_STEPS_PER_MODE_MAX; ++i ) {
249 /*
250 * break if we run out of baby steps for this mode
251 */
252 if(mode_map_ptr[i] == BABY_STEP_NONE)
253 break;
254
255 DEBUGMSGTL(("baby_steps", " baby step mode %s\n",
256 se_find_label_in_slist("babystep_mode",mode_map_ptr[i])));
257
258 /*
259 * skip modes the handler didn't register for
260 */
261 if (BSTEP_USE_ORIGINAL != mode_map_ptr[i]) {
262 u_int mode_flag;
263
264 #ifndef NETSNMP_NO_WRITE_SUPPORT
265 /*
266 * skip undo commit if commit wasn't hit, and
267 * undo_cleanup if undo_setup wasn't hit.
268 */
269 if((MODE_SET_UNDO == save_mode) &&
270 (MODE_BSTEP_UNDO_COMMIT == mode_map_ptr[i]) &&
271 !(BABY_STEP_COMMIT & bs_modes->completed)) {
272 DEBUGMSGTL(("baby_steps",
273 " skipping commit undo (no commit)\n"));
274 continue;
275 }
276 else if((MODE_SET_FREE == save_mode) &&
277 (MODE_BSTEP_UNDO_CLEANUP == mode_map_ptr[i]) &&
278 !(BABY_STEP_UNDO_SETUP & bs_modes->completed)) {
279 DEBUGMSGTL(("baby_steps",
280 " skipping undo cleanup (no undo setup)\n"));
281 continue;
282 }
283 #endif /* NETSNMP_NO_WRITE_SUPPORT */
284
285 reqinfo->mode = mode_map_ptr[i];
286 mode_flag = netsnmp_baby_step_mode2flag( mode_map_ptr[i] );
287 if((mode_flag & bs_modes->registered))
288 bs_modes->completed |= mode_flag;
289 else {
290 DEBUGMSGTL(("baby_steps",
291 " skipping mode (not registered)\n"));
292 continue;
293 }
294
295
296 }
297 else {
298 reqinfo->mode = save_mode;
299 }
300
301 #ifdef BABY_STEPS_NEXT_MODE
302 /*
303 * I can't remember why I wanted the next mode in the request,
304 * but it's not used anywhere, so don't use this code. saved,
305 * in case I remember why I thought needed it. - rstory 040911
306 */
307 if((BABY_STEPS_PER_MODE_MAX - 1) == i)
308 reqinfo->next_mode_ok = BABY_STEP_NONE;
309 else {
310 if(BSTEP_USE_ORIGINAL == mode_map_ptr[i+1])
311 reqinfo->next_mode_ok = save_mode;
312 else
313 reqinfo->next_mode_ok = mode_map_ptr[i+1];
314 }
315 #endif
316
317 /*
318 * call handlers for baby step
319 */
320 rc = netsnmp_call_next_handler(handler, reginfo, reqinfo,
321 requests);
322
323 /*
324 * check for error calling handler (unlikely, but...)
325 */
326 if(rc) {
327 DEBUGMSGTL(("baby_steps", " ERROR:handler error\n"));
328 break;
329 }
330
331 /*
332 * check for errors in any of the requests for GET-like, reserve1,
333 * reserve2 and action. (there is no recovery from errors
334 * in commit, free or undo.)
335 */
336 if (MODE_IS_GET(save_mode)
337 #ifndef NETSNMP_NO_WRITE_SUPPORT
338 || (save_mode < SNMP_MSG_INTERNAL_SET_COMMIT)
339 #endif /* NETSNMP_NO_WRITE_SUPPORT */
340 ) {
341 rc = netsnmp_check_requests_error(requests);
342 if(rc) {
343 DEBUGMSGTL(("baby_steps", " ERROR:request error\n"));
344 break;
345 }
346 }
347 }
348
349 /*
350 * restore original mode
351 */
352 reqinfo->mode = save_mode;
353
354
355 return rc;
356 }
357
358 /** initializes the baby_steps helper which then registers a baby_steps
359 * handler as a run-time injectable handler for configuration file
360 * use.
361 */
362 netsnmp_feature_child_of(netsnmp_baby_steps_handler_init,netsnmp_unused);
363 #ifndef NETSNMP_FEATURE_REMOVE_NETSNMP_BABY_STEPS_HANDLER_INIT
364 void
365 netsnmp_baby_steps_handler_init(void)
366 {
367 netsnmp_mib_handler *handler =
368 netsnmp_baby_steps_handler_get(BABY_STEP_ALL);
369 if (NULL == handler) {
370 netsnmp_handler_free(handler);
371 snmp_log(LOG_ERR, "could not create baby steps handler\n");
372 return;
373 }
374
375 netsnmp_register_handler_by_name("baby_steps", handler);
376 }
377 #endif /* NETSNMP_FEATURE_REMOVE_NETSNMP_BABY_STEPS_HANDLER_INIT */
378
379 /** @} */
380
381 /** @defgroup access_multiplexer baby_steps_access_multiplexer: calls individual access methods based on baby_step mode.
382 * @ingroup baby_steps
383 * @{
384 */
385
386 /** returns a baby_steps handler that can be injected into a given
387 * handler chain.
388 */
389 netsnmp_mib_handler *
390 netsnmp_baby_steps_access_multiplexer_get(netsnmp_baby_steps_access_methods *am)
391 {
392 netsnmp_mib_handler *mh;
393
394 mh = netsnmp_create_handler("baby_steps_mux",
395 _baby_steps_access_multiplexer);
396 if(!mh)
397 return NULL;
398
399 mh->myvoid = am;
400 mh->flags |= MIB_HANDLER_AUTO_NEXT;
401
402 return mh;
403 }
404
405 /** @internal Implements the baby_steps handler */
406 static int
407 _baby_steps_access_multiplexer(netsnmp_mib_handler *handler,
408 netsnmp_handler_registration *reginfo,
409 netsnmp_agent_request_info *reqinfo,
410 netsnmp_request_info *requests)
411 {
412 void *temp_void;
413 Netsnmp_Node_Handler *method = NULL;
414 netsnmp_baby_steps_access_methods *access_methods;
415 int rc = SNMP_ERR_NOERROR;
416
417 /** call handlers should enforce these */
418 netsnmp_assert((handler!=NULL) && (reginfo!=NULL) && (reqinfo!=NULL) &&
419 (requests!=NULL));
420
421 DEBUGMSGT(("baby_steps_mux", "mode %s\n",
422 se_find_label_in_slist("babystep_mode",reqinfo->mode)));
423
424 access_methods = (netsnmp_baby_steps_access_methods *)handler->myvoid;
425 if(!access_methods) {
426 snmp_log(LOG_ERR,"baby_steps_access_multiplexer has no methods\n");
427 return SNMPERR_GENERR;
428 }
429
430 switch(reqinfo->mode) {
431
432 case MODE_BSTEP_PRE_REQUEST:
433 if( access_methods->pre_request )
434 method = access_methods->pre_request;
435 break;
436
437 case MODE_BSTEP_OBJECT_LOOKUP:
438 if( access_methods->object_lookup )
439 method = access_methods->object_lookup;
440 break;
441
442 case SNMP_MSG_GET:
443 case SNMP_MSG_GETNEXT:
444 if( access_methods->get_values )
445 method = access_methods->get_values;
446 break;
447
448 #ifndef NETSNMP_NO_WRITE_SUPPORT
449 case MODE_BSTEP_CHECK_VALUE:
450 if( access_methods->object_syntax_checks )
451 method = access_methods->object_syntax_checks;
452 break;
453
454 case MODE_BSTEP_ROW_CREATE:
455 if( access_methods->row_creation )
456 method = access_methods->row_creation;
457 break;
458
459 case MODE_BSTEP_UNDO_SETUP:
460 if( access_methods->undo_setup )
461 method = access_methods->undo_setup;
462 break;
463
464 case MODE_BSTEP_SET_VALUE:
465 if( access_methods->set_values )
466 method = access_methods->set_values;
467 break;
468
469 case MODE_BSTEP_CHECK_CONSISTENCY:
470 if( access_methods->consistency_checks )
471 method = access_methods->consistency_checks;
472 break;
473
474 case MODE_BSTEP_UNDO_SET:
475 if( access_methods->undo_sets )
476 method = access_methods->undo_sets;
477 break;
478
479 case MODE_BSTEP_COMMIT:
480 if( access_methods->commit )
481 method = access_methods->commit;
482 break;
483
484 case MODE_BSTEP_UNDO_COMMIT:
485 if( access_methods->undo_commit )
486 method = access_methods->undo_commit;
487 break;
488
489 case MODE_BSTEP_IRREVERSIBLE_COMMIT:
490 if( access_methods->irreversible_commit )
491 method = access_methods->irreversible_commit;
492 break;
493
494 case MODE_BSTEP_UNDO_CLEANUP:
495 if( access_methods->undo_cleanup )
496 method = access_methods->undo_cleanup;
497 break;
498 #endif /* NETSNMP_NO_WRITE_SUPPORT */
499
500 case MODE_BSTEP_POST_REQUEST:
501 if( access_methods->post_request )
502 method = access_methods->post_request;
503 break;
504
505 default:
506 snmp_log(LOG_ERR,"unknown mode %d\n", reqinfo->mode);
507 return SNMP_ERR_GENERR;
508 }
509
510 /*
511 * if method exists, set up handler void and call method.
512 */
513 if(NULL != method) {
514 temp_void = handler->myvoid;
515 handler->myvoid = access_methods->my_access_void;
516 rc = (*method)(handler, reginfo, reqinfo, requests);
517 handler->myvoid = temp_void;
518 }
519 else {
520 rc = SNMP_ERR_GENERR;
521 snmp_log(LOG_ERR,"baby steps multiplexer handler called for a mode "
522 "with no handler\n");
523 netsnmp_assert(NULL != method);
524 }
525
526 /*
527 * don't call any lower handlers, it will be done for us
528 * since we set MIB_HANDLER_AUTO_NEXT
529 */
530
531 return rc;
532 }
533
534 /*
535 * give a baby step mode, return the flag for that mode
536 */
537 int
538 netsnmp_baby_step_mode2flag( u_int mode )
539 {
540 switch( mode ) {
541 case MODE_BSTEP_OBJECT_LOOKUP:
542 return BABY_STEP_OBJECT_LOOKUP;
543 #ifndef NETSNMP_NO_WRITE_SUPPORT
544 case MODE_BSTEP_SET_VALUE:
545 return BABY_STEP_SET_VALUE;
546 case MODE_BSTEP_IRREVERSIBLE_COMMIT:
547 return BABY_STEP_IRREVERSIBLE_COMMIT;
548 case MODE_BSTEP_CHECK_VALUE:
549 return BABY_STEP_CHECK_VALUE;
550 case MODE_BSTEP_PRE_REQUEST:
551 return BABY_STEP_PRE_REQUEST;
552 case MODE_BSTEP_POST_REQUEST:
553 return BABY_STEP_POST_REQUEST;
554 case MODE_BSTEP_UNDO_SETUP:
555 return BABY_STEP_UNDO_SETUP;
556 case MODE_BSTEP_UNDO_CLEANUP:
557 return BABY_STEP_UNDO_CLEANUP;
558 case MODE_BSTEP_UNDO_SET:
559 return BABY_STEP_UNDO_SET;
560 case MODE_BSTEP_ROW_CREATE:
561 return BABY_STEP_ROW_CREATE;
562 case MODE_BSTEP_CHECK_CONSISTENCY:
563 return BABY_STEP_CHECK_CONSISTENCY;
564 case MODE_BSTEP_COMMIT:
565 return BABY_STEP_COMMIT;
566 case MODE_BSTEP_UNDO_COMMIT:
567 return BABY_STEP_UNDO_COMMIT;
568 #endif /* NETSNMP_NO_WRITE_SUPPORT */
569 default:
570 netsnmp_assert("unknown flag");
571 break;
572 }
573 return 0;
574 }
575 /** @} */
576
577 #else /* NETSNMP_FEATURE_REMOVE_BABY_STEPS */
578 netsnmp_feature_unused(baby_steps);
579 #endif /* NETSNMP_FEATURE_REMOVE_BABY_STEPS */
580