Coverage Report

Created: 2025-11-11 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/agent/agent_handler.c
Line
Count
Source
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 © 2003 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
21
#ifdef HAVE_STRING_H
22
#include <string.h>
23
#endif
24
25
#include <net-snmp/net-snmp-includes.h>
26
#include <net-snmp/agent/net-snmp-agent-includes.h>
27
28
#include <net-snmp/agent/bulk_to_next.h>
29
30
netsnmp_feature_child_of(agent_handler, libnetsnmpagent);
31
32
netsnmp_feature_child_of(handler_mark_requests_as_delegated, agent_handler);
33
34
static netsnmp_mib_handler *_clone_handler(netsnmp_mib_handler *it);
35
36
/***********************************************************************/
37
/*
38
 * New Handler based API 
39
 */
40
/***********************************************************************/
41
/** @defgroup handler Net-SNMP Agent handler and extensibility API
42
 *  @ingroup agent
43
 *
44
 *  The basic theory goes something like this: In the past, with the
45
 *  original mib module api (which derived from the original CMU SNMP
46
 *  code) the underlying mib modules were passed very little
47
 *  information (only the truly most basic information about a
48
 *  request).  This worked well at the time but in today's world of
49
 *  subagents, device instrumentation, low resource consumption, etc,
50
 *  it just isn't flexible enough.  "handlers" are here to fix all that.
51
 *
52
 *  With the rewrite of the agent internals for the net-snmp 5.0
53
 *  release, we introduce a modular calling scheme that allows agent
54
 *  modules to be written in a very flexible manner, and more
55
 *  importantly allows reuse of code in a decent way (and without the
56
 *  memory and speed overheads of OO languages like C++).
57
 *
58
 *  Functionally, the notion of what a handler does is the same as the
59
 *  older api: A handler is @link netsnmp_create_handler() created@endlink and
60
 *  then @link netsnmp_register_handler() registered@endlink with the main
61
 *  agent at a given OID in the OID tree and gets called any time a
62
 *  request is made that it should respond to.  You probably should
63
 *  use one of the convenience helpers instead of doing anything else
64
 *  yourself though:
65
 *
66
 *  Most importantly, though, is that the handlers are built on the
67
 *  notion of modularity and reuse.  Specifically, rather than do all
68
 *  the really hard work (like parsing table indexes out of an
69
 *  incoming oid request) in each module, the API is designed to make
70
 *  it easy to write "helper" handlers that merely process some aspect
71
 *  of the request before passing it along to the final handler that
72
 *  returns the real answer.  Most people will want to make use of the
73
 *  @link instance instance@endlink, @link table table@endlink, @link
74
 *  table_iterator table_iterator@endlink, @link table_data
75
 *  table_data@endlink, or @link table_dataset table_dataset@endlink
76
 *  helpers to make their life easier.  These "helpers" interpret
77
 *  important aspects of the request and pass them on to you.
78
 *
79
 *  For instance, the @link table table@endlink helper is designed to
80
 *  hand you a list of extracted index values from an incoming
81
 *  request.  The @link table_iterator table_iterator@endlink helper
82
 *  is built on top of the table helper, and is designed to help you
83
 *  iterate through data stored elsewhere (like in a kernel) that is
84
 *  not in OID lexographical order (ie, don't write your own index/oid
85
 *  sorting routine, use this helper instead).  The beauty of the
86
 *  @link table_iterator table_iterator helper@endlink, as well as the @link
87
 *  instance instance@endlink helper is that they take care of the complex
88
 *  GETNEXT processing entirely for you and hand you everything you
89
 *  need to merely return the data as if it was a GET request.  Much
90
 *  less code and hair pulling.  I've pulled all my hair out to help
91
 *  you so that only one of us has to be bald.
92
 *
93
 * @{
94
 */
95
96
/** Creates a MIB handler structure.
97
 *  The new structure is allocated and filled using the given name
98
 *  and access method.
99
 *  The returned handler should then be @link netsnmp_register_handler()
100
 *  registered @endlink.
101
 *
102
 *  @param name is the handler name and is copied then assigned to
103
 *              netsnmp_mib_handler->handler_name
104
 *
105
 *  @param handler_access_method is a function pointer used as the access
106
 *     method for this handler registration instance for whatever required
107
 *         needs.
108
 *
109
 *  @return a pointer to a populated netsnmp_mib_handler struct to be
110
 *          registered
111
 *
112
 *  @see netsnmp_create_handler_registration()
113
 *  @see netsnmp_register_handler()
114
 */
115
netsnmp_mib_handler *
116
netsnmp_create_handler(const char *name,
117
                       Netsnmp_Node_Handler * handler_access_method)
118
45.4k
{
119
45.4k
    netsnmp_mib_handler *ret = SNMP_MALLOC_TYPEDEF(netsnmp_mib_handler);
120
45.4k
    if (ret) {
121
45.4k
        ret->access_method = handler_access_method;
122
45.4k
        if (NULL != name) {
123
45.4k
            ret->handler_name = strdup(name);
124
45.4k
            if (NULL == ret->handler_name)
125
0
                SNMP_FREE(ret);
126
45.4k
        }
127
45.4k
    }
128
45.4k
    return ret;
129
45.4k
}
130
131
/** Creates a MIB handler structure.
132
 *  The new structure is allocated and filled using the given name,
133
 *  access function, registration location OID and list of modes that
134
 *  the handler supports. If modes == 0, then modes will automatically
135
 *  be set to the default value of only HANDLER_CAN_DEFAULT, which is
136
 *  by default read-only GET and GETNEXT requests. A handler which supports
137
 *  sets but not row creation should set us a mode of HANDLER_CAN_SET_ONLY.
138
 *  @note This ends up calling netsnmp_create_handler(name, handler_access_method)
139
 *  @param name is the handler name and is copied then assigned to
140
 *              netsnmp_handler_registration->handlerName.
141
 *
142
 *  @param handler is a function pointer used as the access
143
 *  method for this handler registration instance for whatever required
144
 *  needs.
145
 *
146
 *  @param reg_oid is the registration location oid.
147
 *
148
 *  @param reg_oid_len is the length of reg_oid; can use the macro,
149
 *         OID_LENGTH
150
 *
151
 *  @param modes is used to configure read/write access.  If modes == 0, 
152
 *  then modes will automatically be set to the default 
153
 *  value of only HANDLER_CAN_DEFAULT, which is by default read-only GET 
154
 *  and GETNEXT requests.  The other two mode options are read only, 
155
 *  HANDLER_CAN_RONLY, and read/write, HANDLER_CAN_RWRITE.
156
 *
157
 *    - HANDLER_CAN_GETANDGETNEXT
158
 *    - HANDLER_CAN_SET
159
 *    - HANDLER_CAN_GETBULK      
160
 *
161
 *    - HANDLER_CAN_RONLY   (HANDLER_CAN_GETANDGETNEXT)
162
 *    - HANDLER_CAN_RWRITE  (HANDLER_CAN_GETANDGETNEXT | 
163
 *      HANDLER_CAN_SET)
164
 *    - HANDLER_CAN_DEFAULT HANDLER_CAN_RONLY
165
 *
166
 *  @return Returns a pointer to a netsnmp_handler_registration struct.
167
 *          NULL is returned only when memory could not be allocated for the 
168
 *          netsnmp_handler_registration struct.
169
 *
170
 *
171
 *  @see netsnmp_create_handler()
172
 *  @see netsnmp_register_handler()
173
 */
174
netsnmp_handler_registration *
175
netsnmp_handler_registration_create(const char *name,
176
                                    netsnmp_mib_handler *handler,
177
                                    const oid * reg_oid, size_t reg_oid_len,
178
                                    int modes)
179
0
{
180
0
    netsnmp_handler_registration *the_reg;
181
0
    the_reg = SNMP_MALLOC_TYPEDEF(netsnmp_handler_registration);
182
0
    if (!the_reg)
183
0
        return NULL;
184
185
0
    if (modes)
186
0
        the_reg->modes = modes;
187
0
    else
188
0
        the_reg->modes = HANDLER_CAN_DEFAULT;
189
190
0
    the_reg->handler = handler;
191
0
    the_reg->priority = DEFAULT_MIB_PRIORITY;
192
0
    if (name)
193
0
        the_reg->handlerName = strdup(name);
194
0
    the_reg->rootoid = snmp_duplicate_objid(reg_oid, reg_oid_len);
195
0
    the_reg->rootoid_len = reg_oid_len;
196
0
    return the_reg;
197
0
}
198
199
/** Creates a handler registration structure with a new MIB handler.
200
 *  This function first @link netsnmp_create_handler() creates @endlink
201
 *  a MIB handler, then @link netsnmp_handler_registration_create()
202
 *  makes registration structure @endlink for it.
203
 *
204
 *  @param name is the handler name for netsnmp_create_handler()
205
 *
206
 *  @param handler_access_method is a function pointer used as the access
207
 *     method for netsnmp_create_handler()
208
 *
209
 *  @param reg_oid is the registration location oid.
210
 *
211
 *  @param reg_oid_len is the length of reg_oid; can use the macro,
212
 *         OID_LENGTH
213
 *
214
 *  @param modes is used to configure read/write access, as in
215
 *         netsnmp_handler_registration_create()
216
 *
217
 *  @return Returns a pointer to a netsnmp_handler_registration struct.
218
 *          If the structures creation failed, NULL is returned.
219
 *
220
 *  @see netsnmp_create_handler()
221
 *  @see netsnmp_handler_registration_create()
222
 */
223
netsnmp_handler_registration *
224
netsnmp_create_handler_registration(const char *name,
225
                                    Netsnmp_Node_Handler *
226
                                    handler_access_method, const oid * reg_oid,
227
                                    size_t reg_oid_len, int modes)
228
0
{
229
0
    netsnmp_handler_registration *rv = NULL;
230
0
    netsnmp_mib_handler *handler =
231
0
        netsnmp_create_handler(name, handler_access_method);
232
0
    if (handler) {
233
0
        rv = netsnmp_handler_registration_create(
234
0
            name, handler, reg_oid, reg_oid_len, modes);
235
0
        if (!rv)
236
0
            netsnmp_handler_free(handler);
237
0
    }
238
0
    return rv;
239
0
}
240
241
/** Registers a MIB handler inside the registration structure.
242
 *  Checks given registration handler for sanity, then
243
 *  @link netsnmp_register_mib() performs registration @endlink
244
 *  in the MIB tree, as defined by the netsnmp_handler_registration
245
 *  pointer. On success, SNMP_CALLBACK_APPLICATION is called.
246
 *  The registration struct may be created by call of
247
 *  netsnmp_create_handler_registration().
248
 *
249
 *  @param reginfo Pointer to a netsnmp_handler_registration struct.
250
 *
251
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
252
 *
253
 *  @see netsnmp_create_handler_registration()
254
 *  @see netsnmp_register_mib()
255
 */
256
int
257
netsnmp_register_handler(netsnmp_handler_registration *reginfo)
258
19.4k
{
259
19.4k
    netsnmp_mib_handler *handler;
260
19.4k
    int flags = 0;
261
262
19.4k
    if (reginfo == NULL) {
263
0
        snmp_log(LOG_ERR, "netsnmp_register_handler() called illegally\n");
264
0
        netsnmp_assert(reginfo != NULL);
265
0
        return SNMP_ERR_GENERR;
266
0
    }
267
268
19.4k
    DEBUGIF("handler::register") {
269
0
        DEBUGMSGTL(("handler::register", "Registering %s (", reginfo->handlerName));
270
0
        for (handler = reginfo->handler; handler; handler = handler->next) {
271
0
            DEBUGMSG(("handler::register", "::%s", handler->handler_name));
272
0
        }
273
274
0
        DEBUGMSG(("handler::register", ") at "));
275
0
        if (reginfo->rootoid && reginfo->range_subid) {
276
0
            DEBUGMSGOIDRANGE(("handler::register", reginfo->rootoid,
277
0
                              reginfo->rootoid_len, reginfo->range_subid,
278
0
                              reginfo->range_ubound));
279
0
        } else if (reginfo->rootoid) {
280
0
            DEBUGMSGOID(("handler::register", reginfo->rootoid,
281
0
                         reginfo->rootoid_len));
282
0
        } else {
283
0
            DEBUGMSG(("handler::register", "[null]"));
284
0
        }
285
0
        DEBUGMSG(("handler::register", "\n"));
286
0
    }
287
288
    /*
289
     * don't let them register for absolutely nothing.  Probably a mistake 
290
     */
291
19.4k
    if (0 == reginfo->modes) {
292
0
        reginfo->modes = HANDLER_CAN_DEFAULT;
293
0
        snmp_log(LOG_WARNING, "no registration modes specified for %s. "
294
0
                 "Defaulting to 0x%x\n", reginfo->handlerName, reginfo->modes);
295
0
    }
296
297
    /*
298
     * for handlers that can't GETBULK, force a conversion handler on them 
299
     */
300
19.4k
    if (!(reginfo->modes & HANDLER_CAN_GETBULK)) {
301
0
        handler = netsnmp_get_bulk_to_next_handler();
302
0
        if (!handler ||
303
0
            (netsnmp_inject_handler(reginfo, handler) != SNMPERR_SUCCESS)) {
304
0
            snmp_log(LOG_WARNING, "could not inject bulk to next handler\n");
305
0
            if (handler)
306
0
                netsnmp_handler_free(handler);
307
            /** should this be a critical error? */
308
0
            netsnmp_handler_registration_free(reginfo);
309
0
            return SNMP_ERR_GENERR;
310
0
        }
311
0
    }
312
313
38.9k
    for (handler = reginfo->handler; handler; handler = handler->next) {
314
19.4k
        if (handler->flags & MIB_HANDLER_INSTANCE)
315
0
            flags = FULLY_QUALIFIED_INSTANCE;
316
19.4k
    }
317
318
19.4k
    return netsnmp_register_mib(reginfo->handlerName,
319
19.4k
                                NULL, 0, 0,
320
19.4k
                                reginfo->rootoid, reginfo->rootoid_len,
321
19.4k
                                reginfo->priority,
322
19.4k
                                reginfo->range_subid,
323
19.4k
                                reginfo->range_ubound, NULL,
324
19.4k
                                reginfo->contextName, reginfo->timeout, flags,
325
19.4k
                                reginfo, 1);
326
19.4k
}
327
328
/** Unregisters a MIB handler described inside the registration structure.
329
 *  Removes a registration, performed earlier by
330
 *  netsnmp_register_handler(), from the MIB tree.
331
 *  Uses unregister_mib_context() to do the task.
332
 *
333
 *  @param reginfo Pointer to a netsnmp_handler_registration struct.
334
 *
335
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
336
 *
337
 *  @see netsnmp_register_handler()
338
 *  @see unregister_mib_context()
339
 */
340
int
341
netsnmp_unregister_handler(netsnmp_handler_registration *reginfo)
342
0
{
343
0
    if (!reginfo)
344
0
        return SNMPERR_SUCCESS;
345
0
    return unregister_mib_context(reginfo->rootoid, reginfo->rootoid_len,
346
0
                                  reginfo->priority,
347
0
                                  reginfo->range_subid, reginfo->range_ubound,
348
0
                                  reginfo->contextName);
349
0
}
350
351
/** Registers a MIB handler inside the registration structure.
352
 *  Checks given registration handler for sanity, then
353
 *  @link netsnmp_register_mib() performs registration @endlink
354
 *  in the MIB tree, as defined by the netsnmp_handler_registration
355
 *  pointer. Never calls SNMP_CALLBACK_APPLICATION.
356
 *  The registration struct may be created by call of
357
 *  netsnmp_create_handler_registration().
358
 *
359
 *  @param reginfo Pointer to a netsnmp_handler_registration struct.
360
 *
361
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
362
 *
363
 *  @see netsnmp_create_handler_registration()
364
 *  @see netsnmp_register_mib()
365
 */
366
int
367
netsnmp_register_handler_nocallback(netsnmp_handler_registration *reginfo)
368
0
{
369
0
    netsnmp_mib_handler *handler;
370
0
    if (reginfo == NULL) {
371
0
        snmp_log(LOG_ERR, "netsnmp_register_handler_nocallback() called illegally\n");
372
0
        netsnmp_assert(reginfo != NULL);
373
0
        return SNMP_ERR_GENERR;
374
0
    }
375
0
    DEBUGIF("handler::register") {
376
0
        DEBUGMSGTL(("handler::register",
377
0
                    "Registering (with no callback) "));
378
0
        for (handler = reginfo->handler; handler; handler = handler->next) {
379
0
            DEBUGMSG(("handler::register", "::%s", handler->handler_name));
380
0
        }
381
382
0
        DEBUGMSG(("handler::register", " at "));
383
0
        if (reginfo->rootoid && reginfo->range_subid) {
384
0
            DEBUGMSGOIDRANGE(("handler::register", reginfo->rootoid,
385
0
                              reginfo->rootoid_len, reginfo->range_subid,
386
0
                              reginfo->range_ubound));
387
0
        } else if (reginfo->rootoid) {
388
0
            DEBUGMSGOID(("handler::register", reginfo->rootoid,
389
0
                         reginfo->rootoid_len));
390
0
        } else {
391
0
            DEBUGMSG(("handler::register", "[null]"));
392
0
        }
393
0
        DEBUGMSG(("handler::register", "\n"));
394
0
    }
395
396
    /*
397
     * don't let them register for absolutely nothing.  Probably a mistake 
398
     */
399
0
    if (0 == reginfo->modes) {
400
0
        reginfo->modes = HANDLER_CAN_DEFAULT;
401
0
    }
402
403
0
    return netsnmp_register_mib(reginfo->handler->handler_name,
404
0
                                NULL, 0, 0,
405
0
                                reginfo->rootoid, reginfo->rootoid_len,
406
0
                                reginfo->priority,
407
0
                                reginfo->range_subid,
408
0
                                reginfo->range_ubound, NULL,
409
0
                                reginfo->contextName, reginfo->timeout, 0,
410
0
                                reginfo, 0);
411
0
}
412
413
/** Injects handler into the calling chain of handlers.
414
 *  The given MIB handler is inserted after the handler named before_what.
415
 *  If before_what is NULL, the handler is put at the top of the list,
416
 *  and hence will be the handler to be called first.
417
 *
418
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
419
 *
420
 *  @see netsnmp_create_handler_registration()
421
 *  @see netsnmp_inject_handler()
422
 */
423
int
424
netsnmp_inject_handler_before(netsnmp_handler_registration *reginfo,
425
                              netsnmp_mib_handler *handler,
426
                              const char *before_what)
427
0
{
428
0
    netsnmp_mib_handler *handler2 = handler;
429
430
0
    if (handler == NULL || reginfo == NULL) {
431
0
        snmp_log(LOG_ERR, "netsnmp_inject_handler() called illegally\n");
432
0
        netsnmp_assert(reginfo != NULL);
433
0
        netsnmp_assert(handler != NULL);
434
0
        return SNMP_ERR_GENERR;
435
0
    }
436
0
    while (handler2->next) {
437
0
        handler2 = handler2->next;  /* Find the end of a handler sub-chain */
438
0
    }
439
0
    if (reginfo->handler == NULL) {
440
0
        DEBUGMSGTL(("handler:inject", "injecting %s\n", handler->handler_name));
441
0
    }
442
0
    else {
443
0
        DEBUGMSGTL(("handler:inject", "injecting %s before %s\n",
444
0
                    handler->handler_name, reginfo->handler->handler_name));
445
0
    }
446
0
    if (before_what) {
447
0
        netsnmp_mib_handler *nexth, *prevh = NULL;
448
0
        if (reginfo->handler == NULL) {
449
0
            snmp_log(LOG_ERR, "no handler to inject before\n");
450
0
            return SNMP_ERR_GENERR;
451
0
        }
452
0
        for(nexth = reginfo->handler; nexth;
453
0
            prevh = nexth, nexth = nexth->next) {
454
0
            if (strcmp(nexth->handler_name, before_what) == 0)
455
0
                break;
456
0
        }
457
0
        if (!nexth) {
458
0
      snmp_log(LOG_ERR, "Cannot inject '%s' before '%s': not found\n", handler->handler_name, before_what);
459
0
      snmp_log(LOG_ERR, "The handlers are:\n");
460
0
      for (nexth = reginfo->handler; nexth; nexth = nexth->next)
461
0
    snmp_log(LOG_ERR, "  %s\n", nexth->handler_name);
462
0
            return SNMP_ERR_GENERR;
463
0
  }
464
0
        if (prevh) {
465
            /* after prevh and before nexth */
466
0
            prevh->next = handler;
467
0
            handler2->next = nexth;
468
0
            handler->prev = prevh;
469
0
            nexth->prev = handler2;
470
0
            return SNMPERR_SUCCESS;
471
0
        }
472
        /* else we're first, which is what we do next anyway so fall through */
473
0
    }
474
0
    handler2->next = reginfo->handler;
475
0
    if (reginfo->handler)
476
0
        reginfo->handler->prev = handler2;
477
0
    reginfo->handler = handler;
478
0
    return SNMPERR_SUCCESS;
479
0
}
480
481
/** Injects handler into the calling chain of handlers.
482
 *  The given MIB handler is put at the top of the list,
483
 *  and hence will be the handler to be called first.
484
 *
485
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
486
 *
487
 *  @see netsnmp_create_handler_registration()
488
 *  @see netsnmp_inject_handler_before()
489
 */
490
int
491
netsnmp_inject_handler(netsnmp_handler_registration *reginfo,
492
                       netsnmp_mib_handler *handler)
493
0
{
494
0
    return netsnmp_inject_handler_before(reginfo, handler, NULL);
495
0
}
496
497
/** Calls a MIB handlers chain, starting with specific handler.
498
 *  The given arguments and MIB handler are checked
499
 *  for sanity, then the handlers are called, one by one,
500
 *  until next handler is NULL.
501
 *
502
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
503
 */
504
NETSNMP_INLINE int
505
netsnmp_call_handler(netsnmp_mib_handler *next_handler,
506
                     netsnmp_handler_registration *reginfo,
507
                     netsnmp_agent_request_info *reqinfo,
508
                     netsnmp_request_info *requests)
509
171
{
510
171
    Netsnmp_Node_Handler *nh;
511
171
    int             ret;
512
513
171
    if (next_handler == NULL || reginfo == NULL || reqinfo == NULL ||
514
171
        requests == NULL) {
515
0
        snmp_log(LOG_ERR, "netsnmp_call_handler() called illegally\n");
516
0
        netsnmp_assert(next_handler != NULL);
517
0
        netsnmp_assert(reqinfo != NULL);
518
0
        netsnmp_assert(reginfo != NULL);
519
0
        netsnmp_assert(requests != NULL);
520
0
        return SNMP_ERR_GENERR;
521
0
    }
522
523
171
    do {
524
171
        nh = next_handler->access_method;
525
171
        if (!nh) {
526
0
            if (next_handler->next) {
527
0
                snmp_log(LOG_ERR, "no access method specified in handler %s.",
528
0
                         next_handler->handler_name);
529
0
                return SNMP_ERR_GENERR;
530
0
            }
531
            /*
532
             * The final handler registration in the chain may well not need
533
             * to include a handler routine, if the processing of this object
534
             * is handled completely by the agent toolkit helpers.
535
             */
536
0
            return SNMP_ERR_NOERROR;
537
0
        }
538
539
171
        DEBUGMSGTL(("handler:calling", "calling handler %s for mode %s\n",
540
171
                    next_handler->handler_name,
541
171
                    se_find_label_in_slist("agent_mode", reqinfo->mode)));
542
543
        /*
544
         * XXX: define acceptable return statuses
545
         */
546
171
        ret = (*nh) (next_handler, reginfo, reqinfo, requests);
547
548
171
        DEBUGMSGTL(("handler:returned", "handler %s returned %d\n",
549
171
                    next_handler->handler_name, ret));
550
551
171
        if (! (next_handler->flags & MIB_HANDLER_AUTO_NEXT))
552
171
            break;
553
554
        /*
555
         * did handler signal that it didn't want auto next this time around?
556
         */
557
0
        if(next_handler->flags & MIB_HANDLER_AUTO_NEXT_OVERRIDE_ONCE) {
558
0
            next_handler->flags &= ~MIB_HANDLER_AUTO_NEXT_OVERRIDE_ONCE;
559
0
            break;
560
0
        }
561
562
0
        next_handler = next_handler->next;
563
564
0
    } while(next_handler);
565
566
171
    return ret;
567
171
}
568
569
/** @private
570
 *  Calls all the MIB Handlers in registration struct for a given mode.
571
 *
572
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
573
 */
574
int
575
netsnmp_call_handlers(netsnmp_handler_registration *reginfo,
576
                      netsnmp_agent_request_info *reqinfo,
577
                      netsnmp_request_info *requests)
578
491
{
579
491
    netsnmp_request_info *request;
580
491
    int             status;
581
582
491
    if (reginfo == NULL || reqinfo == NULL || requests == NULL) {
583
0
        snmp_log(LOG_ERR, "netsnmp_call_handlers() called illegally\n");
584
0
        netsnmp_assert(reqinfo != NULL);
585
0
        netsnmp_assert(reginfo != NULL);
586
0
        netsnmp_assert(requests != NULL);
587
0
        return SNMP_ERR_GENERR;
588
0
    }
589
590
491
    if (reginfo->handler == NULL) {
591
0
        snmp_log(LOG_ERR, "no handler specified.");
592
0
        return SNMP_ERR_GENERR;
593
0
    }
594
595
491
    switch (reqinfo->mode) {
596
0
    case MODE_GETBULK:
597
171
    case MODE_GET:
598
171
    case MODE_GETNEXT:
599
171
        if (!(reginfo->modes & HANDLER_CAN_GETANDGETNEXT))
600
0
            return SNMP_ERR_NOERROR;    /* legal */
601
171
        break;
602
603
171
#ifndef NETSNMP_NO_WRITE_SUPPORT
604
171
    case MODE_SET_RESERVE1:
605
160
    case MODE_SET_RESERVE2:
606
160
    case MODE_SET_ACTION:
607
160
    case MODE_SET_COMMIT:
608
320
    case MODE_SET_FREE:
609
320
    case MODE_SET_UNDO:
610
320
        if (!(reginfo->modes & HANDLER_CAN_SET)) {
611
694
            for (; requests; requests = requests->next) {
612
374
                netsnmp_set_request_error(reqinfo, requests,
613
374
                                          SNMP_ERR_NOTWRITABLE);
614
374
            }
615
320
            return SNMP_ERR_NOERROR;
616
320
        }
617
0
        break;
618
0
#endif /* NETSNMP_NO_WRITE_SUPPORT */
619
620
0
    default:
621
0
        snmp_log(LOG_ERR, "unknown mode in netsnmp_call_handlers! bug!\n");
622
0
        return SNMP_ERR_GENERR;
623
491
    }
624
171
    DEBUGMSGTL(("handler:calling", "main handler %s\n",
625
171
                reginfo->handler->handler_name));
626
627
385
    for (request = requests ; request; request = request->next) {
628
214
        request->processed = 0;
629
214
    }
630
631
171
    status = netsnmp_call_handler(reginfo->handler, reginfo, reqinfo, requests);
632
633
171
    return status;
634
491
}
635
636
/** @private
637
 *  Calls the next MIB handler in the chain, after the current one.
638
 *  The given arguments and MIB handler are checked
639
 *  for sanity, then the next handler is called.
640
 *
641
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
642
 */
643
NETSNMP_INLINE int
644
netsnmp_call_next_handler(netsnmp_mib_handler *current,
645
                          netsnmp_handler_registration *reginfo,
646
                          netsnmp_agent_request_info *reqinfo,
647
                          netsnmp_request_info *requests)
648
0
{
649
650
0
    if (current == NULL || reginfo == NULL || reqinfo == NULL ||
651
0
        requests == NULL) {
652
0
        snmp_log(LOG_ERR, "netsnmp_call_next_handler() called illegally\n");
653
0
        netsnmp_assert(current != NULL);
654
0
        netsnmp_assert(reginfo != NULL);
655
0
        netsnmp_assert(reqinfo != NULL);
656
0
        netsnmp_assert(requests != NULL);
657
0
        return SNMP_ERR_GENERR;
658
0
    }
659
660
0
    return netsnmp_call_handler(current->next, reginfo, reqinfo, requests);
661
0
}
662
663
/** @private
664
 *  Calls the next MIB handler in the chain, after the current one.
665
 *  The given arguments and MIB handler are not validated before
666
 *  the call, only request is checked.
667
 *
668
 *  @return Returns SNMPERR_SUCCESS or SNMP_ERR_* error code.
669
 */
670
netsnmp_feature_child_of(netsnmp_call_next_handler_one_request,netsnmp_unused);
671
#ifndef NETSNMP_FEATURE_REMOVE_NETSNMP_CALL_NEXT_HANDLER_ONE_REQUEST
672
NETSNMP_INLINE int
673
netsnmp_call_next_handler_one_request(netsnmp_mib_handler *current,
674
                                      netsnmp_handler_registration *reginfo,
675
                                      netsnmp_agent_request_info *reqinfo,
676
                                      netsnmp_request_info *requests)
677
0
{
678
0
    netsnmp_request_info *request;
679
0
    int ret;
680
    
681
0
    if (!requests) {
682
0
        snmp_log(LOG_ERR, "netsnmp_call_next_handler_ONE_REQUEST() called illegally\n");
683
0
        netsnmp_assert(requests != NULL);
684
0
        return SNMP_ERR_GENERR;
685
0
    }
686
687
0
    request = requests->next;
688
0
    requests->next = NULL;
689
0
    ret = netsnmp_call_handler(current->next, reginfo, reqinfo, requests);
690
0
    requests->next = request;
691
0
    return ret;
692
0
}
693
#endif /* NETSNMP_FEATURE_REMOVE_NETSNMP_CALL_NEXT_HANDLER_ONE_REQUEST */
694
695
/** Deallocates resources associated with a given handler.
696
 *  The handler is removed from chain and then freed.
697
 *  After calling this function, the handler pointer is invalid
698
 *  and should be set to NULL.
699
 *
700
 *  @param handler is the MIB Handler to be freed
701
 */
702
void
703
netsnmp_handler_free(netsnmp_mib_handler *handler)
704
45.4k
{
705
45.4k
    if (handler != NULL) {
706
45.4k
        if (handler->next != NULL) {
707
            /** make sure we aren't pointing to ourselves.  */
708
6.48k
            netsnmp_assert(handler != handler->next); /* bugs caught: 1 */
709
6.48k
            netsnmp_handler_free(handler->next);
710
6.48k
            handler->next = NULL;
711
6.48k
        }
712
45.4k
        if ((handler->myvoid != NULL) && (handler->data_free != NULL))
713
3.24k
        {
714
3.24k
            handler->data_free(handler->myvoid);
715
3.24k
        }
716
45.4k
        SNMP_FREE(handler->handler_name);
717
45.4k
        SNMP_FREE(handler);
718
45.4k
    }
719
45.4k
}
720
721
/** Duplicates a MIB handler and all subsequent handlers.
722
 *  Creates a copy of all data in given handlers chain.
723
 *
724
 *  @param handler is the MIB Handler to be duplicated
725
 *
726
 *  @return Returns a pointer to the complete copy,
727
 *         or NULL if any problem occurred.
728
 *
729
 * @see _clone_handler()
730
 */
731
netsnmp_mib_handler *
732
netsnmp_handler_dup(netsnmp_mib_handler *handler)
733
0
{
734
0
    netsnmp_mib_handler *h = NULL;
735
736
0
    if (!handler)
737
0
        goto err;
738
739
0
    h = _clone_handler(handler);
740
0
    if (!h)
741
0
        goto err;
742
743
    /*
744
     * Providing a clone function without a free function is asking for
745
     * memory leaks, and providing a free function without clone function
746
     * is asking for memory corruption. Hence the log statement below.
747
     */
748
0
    if (!!handler->data_clone != !!handler->data_free)
749
0
        snmp_log(LOG_ERR, "data_clone / data_free inconsistent (%s)\n",
750
0
                 handler->handler_name);
751
0
    if (handler->myvoid && handler->data_clone) {
752
0
        h->myvoid = handler->data_clone(handler->myvoid);
753
0
        if (!h->myvoid)
754
0
            goto err;
755
0
    } else
756
0
        h->myvoid = handler->myvoid;
757
0
    h->data_clone = handler->data_clone;
758
0
    h->data_free = handler->data_free;
759
760
0
    if (handler->next != NULL) {
761
0
        h->next = netsnmp_handler_dup(handler->next);
762
0
        if (!h->next)
763
0
            goto err;
764
0
        h->next->prev = h;
765
0
    }
766
0
    h->prev = NULL;
767
0
    return h;
768
769
0
err:
770
0
    netsnmp_handler_free(h);
771
0
    return NULL;
772
0
}
773
774
/** Free resources associated with a handler registration object.
775
 *  The registration object and all MIB Handlers in the chain are
776
 *  freed. When the function ends, given pointer is no longer valid
777
 *  and should be set to NULL.
778
 *
779
 *  @param reginfo is the handler registration object to be freed
780
 */
781
void
782
netsnmp_handler_registration_free(netsnmp_handler_registration *reginfo)
783
19.4k
{
784
19.4k
    if (reginfo != NULL) {
785
19.4k
        netsnmp_handler_free(reginfo->handler);
786
19.4k
        SNMP_FREE(reginfo->handlerName);
787
19.4k
        SNMP_FREE(reginfo->contextName);
788
19.4k
        SNMP_FREE(reginfo->rootoid);
789
19.4k
        reginfo->rootoid_len = 0;
790
19.4k
        SNMP_FREE(reginfo);
791
19.4k
    }
792
19.4k
}
793
794
/** Duplicates handler registration object and all subsequent handlers.
795
 *  Creates a copy of the handler registration object and all its data.
796
 *
797
 *  @param reginfo is the handler registration object to be duplicated
798
 *
799
 *  @return Returns a pointer to the complete copy,
800
 *         or NULL if any problem occurred.
801
 *
802
 * @see netsnmp_handler_dup()
803
 */
804
netsnmp_handler_registration *
805
netsnmp_handler_registration_dup(netsnmp_handler_registration *reginfo)
806
0
{
807
0
    netsnmp_handler_registration *r = NULL;
808
809
0
    if (reginfo == NULL)
810
0
        return NULL;
811
812
0
    r = calloc(1, sizeof(netsnmp_handler_registration));
813
0
    if (!r)
814
0
        return r;
815
0
    r->modes = reginfo->modes;
816
0
    r->priority = reginfo->priority;
817
0
    r->range_subid = reginfo->range_subid;
818
0
    r->timeout = reginfo->timeout;
819
0
    r->range_ubound = reginfo->range_ubound;
820
0
    r->rootoid_len = reginfo->rootoid_len;
821
822
0
    if (reginfo->handlerName != NULL) {
823
0
        r->handlerName = strdup(reginfo->handlerName);
824
0
        if (r->handlerName == NULL)
825
0
            goto err;
826
0
    }
827
828
0
    if (reginfo->contextName != NULL) {
829
0
        r->contextName = strdup(reginfo->contextName);
830
0
        if (r->contextName == NULL)
831
0
            goto err;
832
0
    }
833
834
0
    if (reginfo->rootoid != NULL) {
835
        /*
836
         * + 1 to make the following code safe:
837
         * reginfo->rootoid[reginfo->rootoid_len++] = 0;
838
         * See also netsnmp_scalar_helper_handler().
839
         */
840
0
        r->rootoid = malloc((reginfo->rootoid_len + 1) * sizeof(oid));
841
0
        if (r->rootoid == NULL)
842
0
            goto err;
843
0
        memcpy(r->rootoid, reginfo->rootoid,
844
0
               reginfo->rootoid_len * sizeof(oid));
845
0
    }
846
847
0
    r->handler = netsnmp_handler_dup(reginfo->handler);
848
0
    if (r->handler == NULL)
849
0
        goto err;
850
0
    return r;
851
852
0
err:
853
0
    netsnmp_handler_registration_free(r);
854
0
    return NULL;
855
0
}
856
857
/** Creates a cache of information which can be saved for future reference.
858
 *  The cache is filled with pointers from parameters. Note that
859
 *  the input structures are not duplicated, but put directly into
860
 *  the new cache struct.
861
 *  Use netsnmp_handler_check_cache() later to make sure it's still
862
 *  valid before referencing it in the future.
863
 *
864
 * @see netsnmp_handler_check_cache()
865
 * @see netsnmp_free_delegated_cache()
866
 */
867
NETSNMP_INLINE netsnmp_delegated_cache *
868
netsnmp_create_delegated_cache(netsnmp_mib_handler *handler,
869
                               netsnmp_handler_registration *reginfo,
870
                               netsnmp_agent_request_info *reqinfo,
871
                               netsnmp_request_info *requests,
872
                               void *localinfo)
873
0
{
874
0
    netsnmp_delegated_cache *ret;
875
876
0
    ret = SNMP_MALLOC_TYPEDEF(netsnmp_delegated_cache);
877
0
    if (ret) {
878
0
        ret->transaction_id = reqinfo->asp->pdu->transid;
879
0
        ret->handler = handler;
880
0
        ret->reginfo = reginfo;
881
0
        ret->reqinfo = reqinfo;
882
0
        ret->requests = requests;
883
0
        ret->localinfo = localinfo;
884
0
    }
885
0
    return ret;
886
0
}
887
888
/** Check if a given delegated cache is still valid.
889
 *  The cache is valid if it's a part of transaction
890
 *  (ie, the agent still considers it to be an outstanding request).
891
 *
892
 *  @param dcache is the delegated cache to be checked.
893
 *
894
 *  @return Returns the cache pointer if it is still valid, NULL otherwise.
895
 *
896
 * @see netsnmp_create_delegated_cache()
897
 * @see netsnmp_free_delegated_cache()
898
 */
899
NETSNMP_INLINE netsnmp_delegated_cache *
900
netsnmp_handler_check_cache(netsnmp_delegated_cache *dcache)
901
0
{
902
0
    if (!dcache)
903
0
        return dcache;
904
905
0
    if (netsnmp_check_transaction_id(dcache->transaction_id) ==
906
0
        SNMPERR_SUCCESS)
907
0
        return dcache;
908
909
0
    return NULL;
910
0
}
911
912
/** Free a cache once it's no longer used.
913
 *  Deletes the data allocated by netsnmp_create_delegated_cache().
914
 *  Structures which were not allocated by netsnmp_create_delegated_cache()
915
 *  are not freed (pointers are dropped).
916
 *
917
 *  @param dcache is the delegated cache to be freed.
918
 *
919
 * @see netsnmp_create_delegated_cache()
920
 * @see netsnmp_handler_check_cache()
921
 */
922
NETSNMP_INLINE void
923
netsnmp_free_delegated_cache(netsnmp_delegated_cache *dcache)
924
0
{
925
    /*
926
     * right now, no extra data is there that needs to be freed 
927
     */
928
0
    if (dcache)
929
0
        SNMP_FREE(dcache);
930
931
0
    return;
932
0
}
933
934
935
#ifndef NETSNMP_FEATURE_REMOVE_HANDLER_MARK_REQUESTS_AS_DELEGATED
936
/** Sets a list of requests as delegated or not delegated.
937
 *  Sweeps through given chain of requests and sets 'delegated'
938
 *  flag accordingly to the isdelegaded parameter.
939
 *
940
 *  @param requests Request list.
941
 *  @param isdelegated New value of the 'delegated' flag.
942
 */
943
void
944
netsnmp_handler_mark_requests_as_delegated(netsnmp_request_info *requests,
945
                                           int isdelegated)
946
0
{
947
0
    while (requests) {
948
0
        requests->delegated = isdelegated;
949
0
        requests = requests->next;
950
0
    }
951
0
}
952
#endif /* NETSNMP_FEATURE_REMOVE_HANDLER_MARK_REQUESTS_AS_DELEGATED */
953
954
/** Adds data from node list to the request information structure.
955
 *  Data in the request can be later extracted and used by submodules.
956
 *
957
 * @param request Destination request information structure.
958
 *
959
 * @param node The data to be added to the linked list
960
 *             request->parent_data.
961
 *
962
 * @see netsnmp_request_remove_list_data()
963
 * @see netsnmp_request_get_list_data()
964
 */
965
NETSNMP_INLINE void
966
netsnmp_request_add_list_data(netsnmp_request_info *request,
967
                              netsnmp_data_list *node)
968
0
{
969
0
    if (request) {
970
0
        if (request->parent_data)
971
0
            netsnmp_add_list_data(&request->parent_data, node);
972
0
        else
973
0
            request->parent_data = node;
974
0
    }
975
0
}
976
977
/** Removes all data from a request.
978
 *
979
 * @param request the netsnmp request info structure
980
 *
981
 * @param name this is the name of the previously added data
982
 *
983
 * @return 0 on successful find-and-delete, 1 otherwise.
984
 *
985
 * @see netsnmp_request_add_list_data()
986
 * @see netsnmp_request_get_list_data()
987
 */
988
NETSNMP_INLINE int
989
netsnmp_request_remove_list_data(netsnmp_request_info *request,
990
                                 const char *name)
991
0
{
992
0
    if ((NULL == request) || (NULL ==request->parent_data))
993
0
        return 1;
994
995
0
    return netsnmp_remove_list_node(&request->parent_data, name);
996
0
}
997
998
/** Extracts data from a request.
999
 *  Retrieves data that was previously added to the request,
1000
 *  usually by a parent module.
1001
 *
1002
 * @param request Source request information structure.
1003
 *
1004
 * @param name Used to compare against the request->parent_data->name value;
1005
 *             if a match is found, request->parent_data->data is returned.
1006
 *
1007
 * @return Gives a void pointer(request->parent_data->data); NULL is
1008
 *         returned if source data is NULL or the object is not found.
1009
 *
1010
 * @see netsnmp_request_add_list_data()
1011
 * @see netsnmp_request_remove_list_data()
1012
 */
1013
void    *
1014
netsnmp_request_get_list_data(netsnmp_request_info *request,
1015
                              const char *name)
1016
0
{
1017
0
    if (request)
1018
0
        return netsnmp_get_list_data(request->parent_data, name);
1019
0
    return NULL;
1020
0
}
1021
1022
/** Free the extra data stored in a request.
1023
 *  Deletes the data in given request only. Other chain items
1024
 *  are unaffected.
1025
 *
1026
 * @param request Request information structure to be modified.
1027
 *
1028
 * @see netsnmp_request_add_list_data()
1029
 * @see netsnmp_free_list_data()
1030
 */
1031
NETSNMP_INLINE void
1032
netsnmp_free_request_data_set(netsnmp_request_info *request)
1033
0
{
1034
0
    if (request)
1035
0
        netsnmp_free_list_data(request->parent_data);
1036
0
}
1037
1038
/** Free the extra data stored in a bunch of requests.
1039
 *  Deletes all data in the chain inside request.
1040
 *
1041
 * @param request Request information structure to be modified.
1042
 *
1043
 * @see netsnmp_request_add_list_data()
1044
 * @see netsnmp_free_request_data_set()
1045
 */
1046
NETSNMP_INLINE void
1047
netsnmp_free_request_data_sets(netsnmp_request_info *request)
1048
904
{
1049
904
    if (request && request->parent_data) {
1050
0
        netsnmp_free_all_list_data(request->parent_data);
1051
0
        request->parent_data = NULL;
1052
0
    }
1053
904
}
1054
1055
/** Returns a MIB handler from a chain based on the name.
1056
 *
1057
 * @param reginfo Handler registration struct which contains the chain.
1058
 *
1059
 * @param name Target MIB Handler name string. The name is case
1060
 *        sensitive.
1061
 *
1062
 * @return The MIB Handler is returned, or NULL if not found.
1063
 *
1064
 * @see netsnmp_request_add_list_data()
1065
 */
1066
netsnmp_mib_handler *
1067
netsnmp_find_handler_by_name(netsnmp_handler_registration *reginfo,
1068
                             const char *name)
1069
0
{
1070
0
    netsnmp_mib_handler *it;
1071
0
    if (reginfo == NULL || name == NULL )
1072
0
        return NULL;
1073
0
    for (it = reginfo->handler; it; it = it->next) {
1074
0
        if (strcmp(it->handler_name, name) == 0) {
1075
0
            return it;
1076
0
        }
1077
0
    }
1078
0
    return NULL;
1079
0
}
1080
1081
/** Returns a handler's void pointer from a chain based on the name.
1082
 *
1083
 * @warning The void pointer data may change as a handler evolves.
1084
 *        Handlers should really advertise some function for you
1085
 *        to use instead.
1086
 *
1087
 * @param reginfo Handler registration struct which contains the chain.
1088
 *
1089
 * @param name Target MIB Handler name string. The name is case
1090
 *        sensitive.
1091
 *
1092
 * @return The MIB Handler's void * pointer is returned,
1093
 *        or NULL if the handler is not found.
1094
 *
1095
 * @see netsnmp_find_handler_by_name()
1096
 */
1097
void           *
1098
netsnmp_find_handler_data_by_name(netsnmp_handler_registration *reginfo,
1099
                                  const char *name)
1100
0
{
1101
0
    netsnmp_mib_handler *it = netsnmp_find_handler_by_name(reginfo, name);
1102
0
    if (it)
1103
0
        return it->myvoid;
1104
0
    return NULL;
1105
0
}
1106
1107
/** @private
1108
 *  Clones a MIB Handler with its basic properties.
1109
 *  Creates a copy of the given MIB Handler. Copies name, flags and
1110
 *  access methods only; not myvoid.
1111
 *
1112
 *  @see netsnmp_handler_dup()
1113
 */
1114
static netsnmp_mib_handler *
1115
_clone_handler(netsnmp_mib_handler *it)
1116
0
{
1117
0
    netsnmp_mib_handler *dup;
1118
1119
0
    if(NULL == it)
1120
0
        return NULL;
1121
1122
0
    dup = netsnmp_create_handler(it->handler_name, it->access_method);
1123
0
    if(NULL != dup)
1124
0
        dup->flags = it->flags;
1125
1126
0
    return dup;
1127
0
}
1128
1129
static netsnmp_data_list *handler_reg = NULL;
1130
1131
void
1132
handler_free_callback(void *handler)
1133
19.4k
{
1134
19.4k
    netsnmp_handler_free((netsnmp_mib_handler *)handler);
1135
19.4k
}
1136
1137
/** Registers a given handler by name, so that it can be found easily later.
1138
 *  Pointer to the handler is put into a list where it can be easily located
1139
 *  at any time.
1140
 *
1141
 * @param name Name string to be associated with the handler.
1142
 *
1143
 * @param handler Pointer the MIB Handler.
1144
 *
1145
 * @see netsnmp_clear_handler_list()
1146
 */
1147
void
1148
netsnmp_register_handler_by_name(const char *name,
1149
                                 netsnmp_mib_handler *handler)
1150
19.4k
{
1151
19.4k
    netsnmp_add_list_data(&handler_reg,
1152
19.4k
                          netsnmp_create_data_list(name, (void *) handler,
1153
19.4k
                                                   handler_free_callback));
1154
19.4k
    DEBUGMSGTL(("handler_registry", "registering helper %s\n", name));
1155
19.4k
}
1156
1157
/** Clears the entire MIB Handlers registration list.
1158
 *  MIB Handlers registration list is used to access any MIB Handler by
1159
 *  its name. The function frees the list memory and sets pointer to NULL.
1160
 *  Instead of calling this function directly, use shutdown_agent().
1161
 *
1162
 *  @see shutdown_agent()
1163
 *  @see netsnmp_register_handler_by_name()
1164
 */
1165
void
1166
netsnmp_clear_handler_list(void)
1167
3.24k
{
1168
3.24k
    DEBUGMSGTL(("agent_handler", "netsnmp_clear_handler_list() called\n"));
1169
3.24k
    netsnmp_free_all_list_data(handler_reg);
1170
3.24k
    handler_reg = NULL;
1171
3.24k
}
1172
1173
/** @private
1174
 *  Injects a handler into a subtree, peers and children when a given
1175
 *  subtrees name matches a passed in name.
1176
 */
1177
void
1178
netsnmp_inject_handler_into_subtree(netsnmp_subtree *tp, const char *name,
1179
                                    netsnmp_mib_handler *handler,
1180
                                    const char *before_what)
1181
0
{
1182
0
    netsnmp_subtree *tptr;
1183
0
    netsnmp_mib_handler *mh;
1184
1185
0
    for (tptr = tp; tptr != NULL; tptr = tptr->next) {
1186
        /*  if (tptr->children) { 
1187
              netsnmp_inject_handler_into_subtree(tptr->children,name,handler);
1188
      }   */
1189
0
        if (strcmp(tptr->label_a, name) == 0) {
1190
0
            DEBUGMSGTL(("injectHandler", "injecting handler %s into %s\n",
1191
0
                        handler->handler_name, tptr->label_a));
1192
0
            netsnmp_inject_handler_before(tptr->reginfo, _clone_handler(handler),
1193
0
                                          before_what);
1194
0
        } else if (tptr->reginfo != NULL &&
1195
0
       tptr->reginfo->handlerName != NULL &&
1196
0
                   strcmp(tptr->reginfo->handlerName, name) == 0) {
1197
0
            DEBUGMSGTL(("injectHandler", "injecting handler into %s/%s\n",
1198
0
                        tptr->label_a, tptr->reginfo->handlerName));
1199
0
            netsnmp_inject_handler_before(tptr->reginfo, _clone_handler(handler),
1200
0
                                          before_what);
1201
0
        } else if (tptr->reginfo != NULL) {
1202
0
            for (mh = tptr->reginfo->handler; mh != NULL; mh = mh->next) {
1203
0
                if (mh->handler_name && strcmp(mh->handler_name, name) == 0) {
1204
0
                    DEBUGMSGTL(("injectHandler", "injecting handler into %s\n",
1205
0
                                tptr->label_a));
1206
0
                    netsnmp_inject_handler_before(tptr->reginfo,
1207
0
                                                  _clone_handler(handler),
1208
0
                                                  before_what);
1209
0
                    break;
1210
0
                } else {
1211
0
                    DEBUGMSGTL(("injectHandler",
1212
0
                                "not injecting handler into %s\n",
1213
0
                                mh->handler_name));
1214
0
                }
1215
0
            }
1216
0
        }
1217
0
    }
1218
0
}
1219
1220
static int      doneit = 0;
1221
/** @private
1222
 *  Parses the "injectHandler" token line.
1223
 */
1224
void
1225
parse_injectHandler_conf(const char *token, char *cptr)
1226
0
{
1227
0
    char            handler_to_insert[256], reg_name[256];
1228
0
    subtree_context_cache *stc;
1229
0
    netsnmp_mib_handler *handler;
1230
1231
    /*
1232
     * XXXWWW: ensure instead that handler isn't inserted twice 
1233
     */
1234
0
    if (doneit)                 /* we only do this once without restart the agent */
1235
0
        return;
1236
1237
0
    cptr = copy_nword(cptr, handler_to_insert, sizeof(handler_to_insert));
1238
0
    handler = (netsnmp_mib_handler*)netsnmp_get_list_data(handler_reg, handler_to_insert);
1239
0
    if (!handler) {
1240
0
  netsnmp_config_error("no \"%s\" handler registered.",
1241
0
           handler_to_insert);
1242
0
        return;
1243
0
    }
1244
1245
0
    if (!cptr) {
1246
0
        config_perror("no INTONAME specified.  Can't do insertion.");
1247
0
        return;
1248
0
    }
1249
0
    cptr = copy_nword(cptr, reg_name, sizeof(reg_name));
1250
1251
0
    for (stc = get_top_context_cache(); stc; stc = stc->next) {
1252
0
        DEBUGMSGTL(("injectHandler", "Checking context tree %s (before=%s)\n",
1253
0
                    stc->context_name, (cptr)?cptr:"null"));
1254
0
        netsnmp_inject_handler_into_subtree(stc->first_subtree, reg_name,
1255
0
                                            handler, cptr);
1256
0
    }
1257
0
}
1258
1259
/** @private
1260
 *  Callback to ensure injectHandler parser doesn't do things twice.
1261
 *  @todo replace this with a method to check the handler chain instead.
1262
 */
1263
static int
1264
handler_mark_inject_handler_done(int majorID, int minorID,
1265
                    void *serverarg, void *clientarg)
1266
3.24k
{
1267
3.24k
    doneit = 1;
1268
3.24k
    return 0;
1269
3.24k
}
1270
1271
/** @private
1272
 *  Registers the injectHandle parser token.
1273
 *  Used in init_agent_read_config().
1274
 *
1275
 *  @see init_agent_read_config()
1276
 */
1277
void
1278
netsnmp_init_handler_conf(void)
1279
3.24k
{
1280
3.24k
    snmpd_register_config_handler("injectHandler",
1281
3.24k
                                  parse_injectHandler_conf,
1282
3.24k
                                  NULL, "injectHandler NAME INTONAME [BEFORE_OTHER_NAME]");
1283
3.24k
    snmp_register_callback(SNMP_CALLBACK_LIBRARY,
1284
3.24k
                           SNMP_CALLBACK_POST_READ_CONFIG,
1285
3.24k
                           handler_mark_inject_handler_done, NULL);
1286
1287
3.24k
    se_add_pair_to_slist("agent_mode", strdup("GET"), MODE_GET);
1288
3.24k
    se_add_pair_to_slist("agent_mode", strdup("GETNEXT"), MODE_GETNEXT);
1289
3.24k
    se_add_pair_to_slist("agent_mode", strdup("GETBULK"), MODE_GETBULK);
1290
3.24k
#ifndef NETSNMP_NO_WRITE_SUPPORT
1291
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_BEGIN"),
1292
3.24k
                         MODE_SET_BEGIN);
1293
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_RESERVE1"),
1294
3.24k
                         MODE_SET_RESERVE1);
1295
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_RESERVE2"),
1296
3.24k
                         MODE_SET_RESERVE2);
1297
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_ACTION"),
1298
3.24k
                         MODE_SET_ACTION);
1299
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_COMMIT"),
1300
3.24k
                         MODE_SET_COMMIT);
1301
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_FREE"), MODE_SET_FREE);
1302
3.24k
    se_add_pair_to_slist("agent_mode", strdup("SET_UNDO"), MODE_SET_UNDO);
1303
1304
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("pre-request"),
1305
3.24k
                         MODE_BSTEP_PRE_REQUEST);
1306
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("object_lookup"),
1307
3.24k
                         MODE_BSTEP_OBJECT_LOOKUP);
1308
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("check_value"),
1309
3.24k
                         MODE_BSTEP_CHECK_VALUE);
1310
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("row_create"),
1311
3.24k
                         MODE_BSTEP_ROW_CREATE);
1312
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("undo_setup"),
1313
3.24k
                         MODE_BSTEP_UNDO_SETUP);
1314
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("set_value"),
1315
3.24k
                         MODE_BSTEP_SET_VALUE);
1316
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("check_consistency"),
1317
3.24k
                         MODE_BSTEP_CHECK_CONSISTENCY);
1318
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("undo_set"),
1319
3.24k
                         MODE_BSTEP_UNDO_SET);
1320
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("commit"),
1321
3.24k
                         MODE_BSTEP_COMMIT);
1322
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("undo_commit"),
1323
3.24k
                         MODE_BSTEP_UNDO_COMMIT);
1324
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("irreversible_commit"),
1325
3.24k
                         MODE_BSTEP_IRREVERSIBLE_COMMIT);
1326
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("undo_cleanup"),
1327
3.24k
                         MODE_BSTEP_UNDO_CLEANUP);
1328
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("post_request"),
1329
3.24k
                         MODE_BSTEP_POST_REQUEST);
1330
3.24k
    se_add_pair_to_slist("babystep_mode", strdup("original"), 0xffff);
1331
3.24k
#endif /* NETSNMP_NO_WRITE_SUPPORT */
1332
1333
    /*
1334
     * xxx-rks: hmmm.. will this work for modes which are or'd together?
1335
     *          I'm betting not...
1336
     */
1337
3.24k
    se_add_pair_to_slist("handler_can_mode", strdup("GET/GETNEXT"),
1338
3.24k
                         HANDLER_CAN_GETANDGETNEXT);
1339
3.24k
    se_add_pair_to_slist("handler_can_mode", strdup("SET"),
1340
3.24k
                         HANDLER_CAN_SET);
1341
3.24k
    se_add_pair_to_slist("handler_can_mode", strdup("GETBULK"),
1342
3.24k
                         HANDLER_CAN_GETBULK);
1343
3.24k
    se_add_pair_to_slist("handler_can_mode", strdup("BABY_STEP"),
1344
3.24k
                         HANDLER_CAN_BABY_STEP);
1345
3.24k
}
1346
1347
/** @} */