Debian Codegraph @

agent/mibgroup/mibII/vacm_context.c

raw
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 #ifdef HAVE_STRING_H
15 #include <string.h>
16 #else
17 #include <strings.h>
18 #endif
19
20 #include <net-snmp/net-snmp-includes.h>
21 #include <net-snmp/agent/net-snmp-agent-includes.h>
22 #include <net-snmp/agent/table.h>
23 #include <net-snmp/agent/table_iterator.h>
24
25 #include "vacm_context.h"
26
27 static oid vacm_context_oid[] = { 1, 3, 6, 1, 6, 3, 16, 1, 1 };
28
29 #define CONTEXTNAME_COLUMN 1
30
31 /*
32 * return the index data from the first node in the agent's
33 * subtree_context_cache list.
34 */
35 netsnmp_variable_list *
36 get_first_context(void **my_loop_context, void **my_data_context,
37 netsnmp_variable_list * put_data,
38 netsnmp_iterator_info *iinfo)
39 {
40 subtree_context_cache *context_ptr;
41 context_ptr = get_top_context_cache();
42
43 if (!context_ptr)
44 return NULL;
45
46 *my_loop_context = context_ptr;
47 *my_data_context = context_ptr;
48
49 snmp_set_var_value(put_data, context_ptr->context_name,
50 strlen(context_ptr->context_name));
51 return put_data;
52 }
53
54 /*
55 * return the next index data from the first node in the agent's
56 * subtree_context_cache list.
57 */
58 netsnmp_variable_list *
59 get_next_context(void **my_loop_context,
60 void **my_data_context,
61 netsnmp_variable_list * put_data,
62 netsnmp_iterator_info *iinfo)
63 {
64 subtree_context_cache *context_ptr;
65
66 if (!my_loop_context || !*my_loop_context)
67 return NULL;
68
69 context_ptr = (subtree_context_cache *) (*my_loop_context);
70 context_ptr = context_ptr->next;
71 *my_loop_context = context_ptr;
72 *my_data_context = context_ptr;
73
74 if (!context_ptr)
75 return NULL;
76
77 snmp_set_var_value(put_data, context_ptr->context_name,
78 strlen(context_ptr->context_name));
79 return put_data;
80 }
81
82 void
83 init_vacm_context(void)
84 {
85 /*
86 * table vacm_context
87 */
88 netsnmp_handler_registration *my_handler;
89 netsnmp_table_registration_info *table_info;
90 netsnmp_iterator_info *iinfo;
91
92 my_handler = netsnmp_create_handler_registration("vacm_context",
93 vacm_context_handler,
94 vacm_context_oid,
95 sizeof
96 (vacm_context_oid) /
97 sizeof(oid),
98 HANDLER_CAN_RONLY);
99
100 if (!my_handler)
101 return;
102
103 table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
104 iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
105
106 if (!table_info || !iinfo) {
107 SNMP_FREE(table_info);
108 SNMP_FREE(iinfo);
109 netsnmp_handler_registration_free(my_handler);
110 return;
111 }
112
113 netsnmp_table_helper_add_index(table_info, ASN_OCTET_STR)
114 table_info->min_column = 1;
115 table_info->max_column = 1;
116 iinfo->get_first_data_point = get_first_context;
117 iinfo->get_next_data_point = get_next_context;
118 iinfo->table_reginfo = table_info;
119 netsnmp_register_table_iterator2(my_handler, iinfo);
120 }
121
122 /*
123 * returns a list of known context names
124 */
125
126 int
127 vacm_context_handler(netsnmp_mib_handler *handler,
128 netsnmp_handler_registration *reginfo,
129 netsnmp_agent_request_info *reqinfo,
130 netsnmp_request_info *requests)
131 {
132 subtree_context_cache *context_ptr;
133
134 for(; requests; requests = requests->next) {
135 netsnmp_variable_list *var = requests->requestvb;
136
137 if (requests->processed != 0)
138 continue;
139
140
141 context_ptr = (subtree_context_cache *)
142 netsnmp_extract_iterator_context(requests);
143
144 if (context_ptr == NULL) {
145 snmp_log(LOG_ERR,
146 "vacm_context_handler called without data\n");
147 continue;
148 }
149
150 switch (reqinfo->mode) {
151 case MODE_GET:
152 /*
153 * if here we should have a context_ptr passed in already
154 */
155 /*
156 * only one column should ever reach us, so don't check it
157 */
158 snmp_set_var_typed_value(var, ASN_OCTET_STR,
159 context_ptr->context_name,
160 strlen(context_ptr->context_name));
161
162 break;
163 default:
164 /*
165 * We should never get here, getnext already have been
166 * handled by the table_iterator and we're read_only
167 */
168 snmp_log(LOG_ERR,
169 "vacm_context table accessed as mode=%d. We're improperly registered!",
170 reqinfo->mode);
171 break;
172 }
173 }
174
175 return SNMP_ERR_NOERROR;
176 }