1 /*
2  * Portions of this file are subject to the following copyright(s).  See
3  * the Net-SNMP's COPYING file for more details and other copyrights
4  * that may apply:
5  *
6  * Portions of this file are copyrighted by:
7  * Copyright (c) 2016 VMware, 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 
12 #include <net-snmp/net-snmp-config.h>
13 
14 #include <net-snmp/net-snmp-includes.h>
15 #include <net-snmp/agent/net-snmp-agent-includes.h>
16 
17 #include <net-snmp/agent/read_only.h>
18 
19 /** @defgroup read_only read_only
20  *  Make your handler read_only automatically
21  *  The only purpose of this handler is to return an
22  *  appropriate error for any requests passed to it in a SET mode.
23  *  Inserting it into your handler chain will ensure you're never
24  *  asked to perform a SET request so you can ignore those error
25  *  conditions.
26  *  @ingroup utilities
27  *  @{
28  */
29 
30 /** returns a read_only handler that can be injected into a given
31  *  handler chain.
32  */
33 netsnmp_mib_handler *
netsnmp_get_read_only_handler(void)34 netsnmp_get_read_only_handler(void)
35 {
36     netsnmp_mib_handler *ret = NULL;
37 
38     ret = netsnmp_create_handler("read_only",
39                                  netsnmp_read_only_helper);
40     if (ret) {
41         ret->flags |= MIB_HANDLER_AUTO_NEXT;
42     }
43     return ret;
44 }
45 
46 /** @internal Implements the read_only handler */
47 int
netsnmp_read_only_helper(netsnmp_mib_handler * handler,netsnmp_handler_registration * reginfo,netsnmp_agent_request_info * reqinfo,netsnmp_request_info * requests)48 netsnmp_read_only_helper(netsnmp_mib_handler *handler,
49                          netsnmp_handler_registration *reginfo,
50                          netsnmp_agent_request_info *reqinfo,
51                          netsnmp_request_info *requests)
52 {
53 
54     DEBUGMSGTL(("helper:read_only", "Got request\n"));
55 
56     switch (reqinfo->mode) {
57 
58 #ifndef NETSNMP_NO_WRITE_SUPPORT
59     case MODE_SET_RESERVE1:
60     case MODE_SET_RESERVE2:
61     case MODE_SET_ACTION:
62     case MODE_SET_COMMIT:
63     case MODE_SET_FREE:
64     case MODE_SET_UNDO:
65         netsnmp_request_set_error_all(requests, SNMP_ERR_NOTWRITABLE);
66         return SNMP_ERR_NOTWRITABLE;
67 #endif /* NETSNMP_NO_WRITE_SUPPORT */
68 
69     case MODE_GET:
70     case MODE_GETNEXT:
71     case MODE_GETBULK:
72         /* next handler called automatically - 'AUTO_NEXT' */
73         return SNMP_ERR_NOERROR;
74     }
75 
76     netsnmp_request_set_error_all(requests, SNMP_ERR_GENERR);
77     return SNMP_ERR_GENERR;
78 }
79 
80 /** initializes the read_only helper which then registers a read_only
81  *  handler as a run-time injectable handler for configuration file
82  *  use.
83  */
84 void
netsnmp_init_read_only_helper(void)85 netsnmp_init_read_only_helper(void)
86 {
87     netsnmp_mib_handler *handler = netsnmp_get_read_only_handler();
88     if (!handler) {
89         snmp_log(LOG_ERR, "could not create read_only handler\n");
90         return;
91     }
92     netsnmp_register_handler_by_name("read_only", handler);
93 }
94 /**  @} */
95 
96