Debian Codegraph @

agent/mibgroup/disman/expr/expExpressionConf.c

raw
1 /*
2 * DisMan Expression MIB:
3 * Implementation of the expression table configuration handling.
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10
11 #include <ctype.h>
12
13 #include "utilities/iquery.h"
14 #include "disman/expr/expExpression.h"
15 #include "disman/expr/expObject.h"
16 #include "disman/expr/expExpressionConf.h"
17
18 netsnmp_feature_require(iquery);
19
20 /* Initializes the expExpressionConf module */
21 void
22 init_expExpressionConf(void)
23 {
24 init_expr_table_data();
25
26 /*
27 * Register config handler for user-level (fixed) expressions...
28 * XXX - TODO
29 */
30 snmpd_register_config_handler("expression", parse_expression, NULL, NULL);
31
32 /*
33 * ... and persistent storage of dynamically configured entries.
34 */
35 snmpd_register_config_handler("_expETable", parse_expETable, NULL, NULL);
36
37 /*
38 * Register to save (non-fixed) entries when the agent shuts down
39 */
40 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
41 store_expETable, NULL);
42 }
43
44
45
46 /* ================================================
47 *
48 * Handlers for loading/storing persistent expression entries
49 *
50 * ================================================ */
51
52 char *
53 _parse_expECols( char *line, struct expExpression *entry )
54 {
55 void *vp;
56 size_t tmp;
57 size_t len;
58
59 len = EXP_STR3_LEN; vp = entry->expExpression;
60 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
61
62 line = read_config_read_data(ASN_UNSIGNED, line, &tmp, NULL);
63 entry->expValueType = tmp;
64
65 len = EXP_STR2_LEN; vp = entry->expComment;
66 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
67
68 line = read_config_read_data(ASN_UNSIGNED, line, &tmp, NULL);
69 entry->expDeltaInterval = tmp;
70
71 vp = entry->expPrefix;
72 entry->expPrefix_len = MAX_OID_LEN;
73 line = read_config_read_data(ASN_OBJECT_ID, line, &vp,
74 &entry->expPrefix_len);
75
76 line = read_config_read_data(ASN_UNSIGNED, line, &tmp, NULL);
77 entry->flags |= (tmp & EXP_FLAG_ACTIVE);
78
79 return line;
80 }
81
82
83 void
84 parse_expression(const char *token, char *line)
85 {
86 char buf[ SPRINT_MAX_LEN];
87 char ename[EXP_STR1_LEN+1];
88 oid name_buf[MAX_OID_LEN];
89 size_t name_len;
90 char *cp, *cp2;
91 struct expExpression *entry;
92 struct expObject *object;
93 netsnmp_session *sess = NULL;
94 int type=EXPVALTYPE_COUNTER;
95 int i=1;
96
97 DEBUGMSGTL(("disman:expr:conf", "Parsing expression config... "));
98
99 memset(buf, 0, sizeof(buf));
100 memset(ename, 0, sizeof(ename));
101
102 for (cp = copy_nword(line, buf, SPRINT_MAX_LEN);
103 ;
104 cp = copy_nword(cp, buf, SPRINT_MAX_LEN)) {
105
106 if (buf[0] == '-' ) {
107 switch (buf[1]) {
108 case 't': /* type */
109 switch (buf[2]) {
110 case 'c': type = EXPVALTYPE_COUNTER; break;
111 case 'u': type = EXPVALTYPE_UNSIGNED; break;
112 case 't': type = EXPVALTYPE_TIMETICKS; break;
113 case 'i': type = EXPVALTYPE_INTEGER; break;
114 case 'a': type = EXPVALTYPE_IPADDRESS; break;
115 case 's': type = EXPVALTYPE_STRING; break;
116 case 'o': type = EXPVALTYPE_OID; break;
117 case 'C': type = EXPVALTYPE_COUNTER64; break;
118 }
119 break;
120 case 'u': /* user */
121 cp = copy_nword(cp, buf, SPRINT_MAX_LEN);
122 sess = netsnmp_iquery_user_session(buf);
123 break;
124 }
125 } else {
126 break;
127 }
128 }
129
130 memcpy(ename, buf, sizeof(ename));
131 /* cp = copy_nword(line, ename, sizeof(ename)); */
132 entry = expExpression_createEntry( "snmpd.conf", ename, 1 );
133 if (!entry)
134 return;
135
136 cp2 = entry->expExpression;
137 while (cp && *cp) {
138 /*
139 * Copy everything that can't possibly be a MIB
140 * object name into the expression field...
141 */
142 /* XXX - TODO - Handle string literals */
143 if (!isalpha(*cp & 0xFF)) {
144 *cp2++ = *cp++;
145 continue;
146 }
147 /*
148 * ... and copy the defined functions across as well
149 * XXX - TODO
150 */
151
152 /*
153 * Anything else is presumably a MIB object (or instance).
154 * Create an entry in the expObjectTable, and insert a
155 * corresponding parameter in the expression itself.
156 */
157 name_len = MAX_OID_LEN;
158 cp = copy_nword(cp, buf, SPRINT_MAX_LEN);
159 snmp_parse_oid( buf, name_buf, &name_len );
160 object = expObject_createEntry( "snmpd.conf", ename, i, 1 );
161 memcpy( object->expObjectID, name_buf, name_len*sizeof(oid));
162 object->expObjectID_len = name_len;
163 object->flags |= EXP_OBJ_FLAG_VALID
164 | EXP_OBJ_FLAG_ACTIVE
165 | EXP_OBJ_FLAG_OWILD;
166 /*
167 * The first such object can also be used as the
168 * expExpressionPrefix
169 */
170 if ( i == 1 ) {
171 memcpy( entry->expPrefix, name_buf, name_len*sizeof(oid));
172 entry->expPrefix_len = name_len;
173 object->flags |= EXP_OBJ_FLAG_PREFIX;
174 }
175 sprintf(cp2, "$%d", i++);
176 while (*cp2)
177 cp2++; /* Skip over this parameter */
178 }
179
180 if (sess)
181 entry->session = sess;
182 else
183 entry->session = netsnmp_query_get_default_session();
184 entry->expDeltaInterval = 10;
185 entry->expValueType = type;
186 entry->flags |= EXP_FLAG_VALID
187 | EXP_FLAG_ACTIVE;
188 expExpression_enable( entry );
189 DEBUGMSG(("disman:expr:conf", "(%s, %s)\n", ename,
190 entry->expExpression));
191 }
192
193 void
194 parse_expETable(const char *token, char *line)
195 {
196 char owner[EXP_STR1_LEN+1];
197 char ename[EXP_STR1_LEN+1];
198 void *vp;
199 size_t len;
200 struct expExpression *entry;
201
202 DEBUGMSGTL(("disman:expr:conf", "Parsing mteExpressionTable config... "));
203
204 /*
205 * Read in the index information for this entry
206 * and create a (non-fixed) data structure for it.
207 */
208 memset( owner, 0, sizeof(owner));
209 memset( ename, 0, sizeof(ename));
210 len = EXP_STR1_LEN; vp = owner;
211 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
212 len = EXP_STR1_LEN; vp = ename;
213 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
214 entry = expExpression_createEntry( owner, ename, 0 );
215
216 DEBUGMSG(("disman:expr:conf", "(%s, %s) ", owner, ename));
217
218 /*
219 * Read in the accessible column values.
220 */
221 line = _parse_expECols( line, entry );
222 /*
223 * XXX - Will need to read in the 'iquery' access information
224 */
225 entry->flags |= EXP_FLAG_VALID;
226
227 DEBUGMSG(("disman:expr:conf", "\n"));
228 }
229
230
231 int
232 store_expETable(int majorID, int minorID, void *serverarg, void *clientarg)
233 {
234 char line[SNMP_MAXBUF];
235 char *cptr, *cp;
236 void *vp;
237 size_t tint;
238 netsnmp_tdata_row *row;
239 struct expExpression *entry;
240
241
242 DEBUGMSGTL(("disman:expr:conf", "Storing expExpressionTable config:\n"));
243
244 for (row = netsnmp_tdata_row_first( expr_table_data );
245 row;
246 row = netsnmp_tdata_row_next( expr_table_data, row )) {
247
248 /*
249 * Skip entries that were set up via static config directives
250 */
251 entry = (struct expExpression *)netsnmp_tdata_row_entry( row );
252 if ( entry->flags & EXP_FLAG_FIXED )
253 continue;
254
255 DEBUGMSGTL(("disman:expr:conf", " Storing (%s %s)\n",
256 entry->expOwner, entry->expName));
257
258 /*
259 * Save the basic expExpression entry
260 */
261 memset(line, 0, sizeof(line));
262 strcat(line, "_expETable ");
263 cptr = line + strlen(line);
264
265 cp = entry->expOwner; tint = strlen( cp );
266 cptr = read_config_store_data( ASN_OCTET_STR, cptr, &cp, &tint );
267 cp = entry->expName; tint = strlen( cp );
268 cptr = read_config_store_data( ASN_OCTET_STR, cptr, &cp, &tint );
269
270 cp = entry->expExpression; tint = strlen( cp );
271 cptr = read_config_store_data( ASN_OCTET_STR, cptr, &cp, &tint );
272 tint = entry->expValueType;
273 cptr = read_config_store_data( ASN_UNSIGNED, cptr, &tint, NULL );
274 cp = entry->expComment; tint = strlen( cp );
275 cptr = read_config_store_data( ASN_OCTET_STR, cptr, &cp, &tint );
276 tint = entry->expDeltaInterval;
277 cptr = read_config_store_data( ASN_UNSIGNED, cptr, &tint, NULL );
278
279 vp = entry->expPrefix;
280 tint = entry->expPrefix_len;
281 cptr = read_config_store_data( ASN_OBJECT_ID, cptr, &vp, &tint );
282
283 tint = entry->flags;
284 cptr = read_config_store_data( ASN_UNSIGNED, cptr, &tint, NULL );
285
286 /* XXX - Need to store the 'iquery' access information */
287 snmpd_store_config(line);
288 }
289
290 DEBUGMSGTL(("disman:expr:conf", " done.\n"));
291 return SNMPERR_SUCCESS;
292 }