/src/net-snmp/agent/agent_index.c
Line | Count | Source |
1 | | /* |
2 | | * agent_index.c |
3 | | * |
4 | | * Maintain a registry of index allocations |
5 | | * (Primarily required for AgentX support, |
6 | | * but it could be more widely usable). |
7 | | */ |
8 | | |
9 | | |
10 | | #include <net-snmp/net-snmp-config.h> |
11 | | #include <net-snmp/net-snmp-features.h> |
12 | | #include <signal.h> |
13 | | #ifdef HAVE_STRING_H |
14 | | #include <string.h> |
15 | | #endif |
16 | | #ifdef HAVE_STDLIB_H |
17 | | #include <stdlib.h> |
18 | | #endif |
19 | | #include <sys/types.h> |
20 | | #include <stdio.h> |
21 | | #include <fcntl.h> |
22 | | #ifdef TIME_WITH_SYS_TIME |
23 | | # include <sys/time.h> |
24 | | # include <time.h> |
25 | | #else |
26 | | # ifdef HAVE_SYS_TIME_H |
27 | | # include <sys/time.h> |
28 | | # else |
29 | | # include <time.h> |
30 | | # endif |
31 | | #endif |
32 | | #ifdef HAVE_NETINET_IN_H |
33 | | #include <netinet/in.h> |
34 | | #endif |
35 | | |
36 | | #include <net-snmp/net-snmp-includes.h> |
37 | | #include <net-snmp/agent/net-snmp-agent-includes.h> |
38 | | #include <net-snmp/agent/agent_callbacks.h> |
39 | | #include <net-snmp/agent/agent_index.h> |
40 | | |
41 | | #include "snmpd.h" |
42 | | #include "agent_global_vars.h" |
43 | | #include "mibgroup/struct.h" |
44 | | #include <net-snmp/agent/table.h> |
45 | | #include <net-snmp/agent/table_iterator.h> |
46 | | |
47 | | #ifdef USING_AGENTX_SUBAGENT_MODULE |
48 | | #include "agentx/subagent.h" |
49 | | #include "agentx/client.h" |
50 | | #endif |
51 | | |
52 | | netsnmp_feature_child_of(agent_index_all, libnetsnmpagent); |
53 | | |
54 | | netsnmp_feature_child_of(remove_index, agent_index_all); |
55 | | |
56 | | /* |
57 | | * Initial support for index allocation |
58 | | */ |
59 | | |
60 | | struct snmp_index { |
61 | | netsnmp_variable_list *varbind; /* or pointer to var_list ? */ |
62 | | int allocated; |
63 | | netsnmp_session *session; |
64 | | struct snmp_index *next_oid; |
65 | | struct snmp_index *prev_oid; |
66 | | struct snmp_index *next_idx; |
67 | | } *snmp_index_head = NULL; |
68 | | |
69 | | /* |
70 | | * The caller is responsible for free()ing the memory returned by |
71 | | * this function. |
72 | | */ |
73 | | |
74 | | char * |
75 | | register_string_index(oid * name, size_t name_len, char *cp) |
76 | 0 | { |
77 | 0 | netsnmp_variable_list varbind, *res; |
78 | 0 | char *rv = NULL; |
79 | |
|
80 | 0 | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
81 | 0 | varbind.type = ASN_OCTET_STR; |
82 | 0 | snmp_set_var_objid(&varbind, name, name_len); |
83 | 0 | if (cp != ANY_STRING_INDEX) { |
84 | 0 | snmp_set_var_value(&varbind, (u_char *) cp, strlen(cp)); |
85 | 0 | res = register_index(&varbind, ALLOCATE_THIS_INDEX, main_session); |
86 | 0 | } else { |
87 | 0 | res = register_index(&varbind, ALLOCATE_ANY_INDEX, main_session); |
88 | 0 | } |
89 | |
|
90 | 0 | if (res != NULL) { |
91 | 0 | rv = (char *)malloc(res->val_len + 1); |
92 | 0 | if (rv) { |
93 | 0 | memcpy(rv, res->val.string, res->val_len); |
94 | 0 | rv[res->val_len] = 0; |
95 | 0 | } |
96 | 0 | snmp_free_var(res); |
97 | 0 | } |
98 | 0 | snmp_free_var_internals(&varbind); |
99 | 0 | return rv; |
100 | 0 | } |
101 | | |
102 | | int |
103 | | register_int_index(oid * name, size_t name_len, int val) |
104 | 0 | { |
105 | 0 | netsnmp_variable_list varbind, *res; |
106 | 0 | int rv = -1; |
107 | |
|
108 | 0 | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
109 | 0 | varbind.type = ASN_INTEGER; |
110 | 0 | snmp_set_var_objid(&varbind, name, name_len); |
111 | 0 | varbind.val.string = varbind.buf; |
112 | 0 | if (val != ANY_INTEGER_INDEX) { |
113 | 0 | varbind.val_len = sizeof(long); |
114 | 0 | *varbind.val.integer = val; |
115 | 0 | res = register_index(&varbind, ALLOCATE_THIS_INDEX, main_session); |
116 | 0 | } else { |
117 | 0 | res = register_index(&varbind, ALLOCATE_ANY_INDEX, main_session); |
118 | 0 | } |
119 | |
|
120 | 0 | if (res != NULL) { |
121 | 0 | rv = *(res->val.integer); |
122 | 0 | snmp_free_var(res); |
123 | 0 | } |
124 | 0 | snmp_free_var_internals(&varbind); |
125 | 0 | return rv; |
126 | 0 | } |
127 | | |
128 | | /* |
129 | | * The caller is responsible for free()ing the memory returned by |
130 | | * this function. |
131 | | */ |
132 | | |
133 | | netsnmp_variable_list * |
134 | | register_oid_index(oid * name, size_t name_len, |
135 | | oid * value, size_t value_len) |
136 | 0 | { |
137 | 0 | netsnmp_variable_list varbind; |
138 | 0 | netsnmp_variable_list *res; |
139 | |
|
140 | 0 | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
141 | 0 | varbind.type = ASN_OBJECT_ID; |
142 | 0 | snmp_set_var_objid(&varbind, name, name_len); |
143 | 0 | if (value != ANY_OID_INDEX) { |
144 | 0 | snmp_set_var_value(&varbind, (u_char *) value, |
145 | 0 | value_len * sizeof(oid)); |
146 | 0 | res = register_index(&varbind, ALLOCATE_THIS_INDEX, main_session); |
147 | 0 | } else { |
148 | 0 | res = register_index(&varbind, ALLOCATE_ANY_INDEX, main_session); |
149 | 0 | } |
150 | 0 | snmp_free_var_internals(&varbind); |
151 | 0 | return res; |
152 | 0 | } |
153 | | |
154 | | /* |
155 | | * The caller is responsible for free()ing the memory returned by |
156 | | * this function. |
157 | | */ |
158 | | |
159 | | netsnmp_variable_list * |
160 | | register_index(netsnmp_variable_list * varbind, int flags, |
161 | | netsnmp_session * ss) |
162 | 0 | { |
163 | 0 | netsnmp_variable_list *rv = NULL; |
164 | 0 | struct snmp_index *new_index, *idxptr, *idxptr2; |
165 | 0 | struct snmp_index *prev_oid_ptr, *prev_idx_ptr; |
166 | 0 | int res, res2, i; |
167 | |
|
168 | 0 | DEBUGMSGTL(("register_index", "register ")); |
169 | 0 | DEBUGMSGVAR(("register_index", varbind)); |
170 | 0 | DEBUGMSG(("register_index", "for session %8p\n", ss)); |
171 | |
|
172 | 0 | #if defined(USING_AGENTX_SUBAGENT_MODULE) && !defined(TESTING) |
173 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
174 | 0 | NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) { |
175 | 0 | return (agentx_register_index(ss, varbind, flags)); |
176 | 0 | } |
177 | 0 | #endif |
178 | | /* |
179 | | * Look for the requested OID entry |
180 | | */ |
181 | 0 | prev_oid_ptr = NULL; |
182 | 0 | prev_idx_ptr = NULL; |
183 | 0 | res = 1; |
184 | 0 | res2 = 1; |
185 | 0 | for (idxptr = snmp_index_head; idxptr != NULL; |
186 | 0 | prev_oid_ptr = idxptr, idxptr = idxptr->next_oid) { |
187 | 0 | if ((res = snmp_oid_compare(varbind->name, varbind->name_length, |
188 | 0 | idxptr->varbind->name, |
189 | 0 | idxptr->varbind->name_length)) <= 0) |
190 | 0 | break; |
191 | 0 | } |
192 | | |
193 | | /* |
194 | | * Found the OID - now look at the registered indices |
195 | | */ |
196 | 0 | if (res == 0 && idxptr) { |
197 | 0 | if (varbind->type != idxptr->varbind->type) |
198 | 0 | return NULL; /* wrong type */ |
199 | | |
200 | | /* |
201 | | * If we've been asked for an arbitrary new value, |
202 | | * then find the end of the list. |
203 | | * If we've been asked for any arbitrary value, |
204 | | * then look for an unused entry, and use that. |
205 | | * If there aren't any, continue as for new. |
206 | | * Otherwise, locate the given value in the (sorted) |
207 | | * list of already allocated values |
208 | | */ |
209 | 0 | if (flags & ALLOCATE_ANY_INDEX) { |
210 | 0 | for (idxptr2 = idxptr; idxptr2 != NULL; |
211 | 0 | prev_idx_ptr = idxptr2, idxptr2 = idxptr2->next_idx) { |
212 | |
|
213 | 0 | if (flags == ALLOCATE_ANY_INDEX && !(idxptr2->allocated)) { |
214 | 0 | if ((rv = |
215 | 0 | snmp_clone_varbind(idxptr2->varbind)) != NULL) { |
216 | 0 | idxptr2->session = ss; |
217 | 0 | idxptr2->allocated = 1; |
218 | 0 | } |
219 | 0 | return rv; |
220 | 0 | } |
221 | 0 | } |
222 | 0 | } else { |
223 | 0 | for (idxptr2 = idxptr; idxptr2 != NULL; |
224 | 0 | prev_idx_ptr = idxptr2, idxptr2 = idxptr2->next_idx) { |
225 | 0 | switch (varbind->type) { |
226 | 0 | case ASN_INTEGER: |
227 | 0 | res2 = |
228 | 0 | (*varbind->val.integer - |
229 | 0 | *idxptr2->varbind->val.integer); |
230 | 0 | break; |
231 | 0 | case ASN_OCTET_STR: |
232 | 0 | i = SNMP_MIN(varbind->val_len, |
233 | 0 | idxptr2->varbind->val_len); |
234 | 0 | res2 = |
235 | 0 | memcmp(varbind->val.string, |
236 | 0 | idxptr2->varbind->val.string, i); |
237 | 0 | if (res2 == 0) |
238 | 0 | res2 = (int)varbind->val_len - (int)idxptr2->varbind->val_len; |
239 | 0 | break; |
240 | 0 | case ASN_OBJECT_ID: |
241 | 0 | res2 = |
242 | 0 | snmp_oid_compare(varbind->val.objid, |
243 | 0 | varbind->val_len / sizeof(oid), |
244 | 0 | idxptr2->varbind->val.objid, |
245 | 0 | idxptr2->varbind->val_len / |
246 | 0 | sizeof(oid)); |
247 | 0 | break; |
248 | 0 | default: |
249 | 0 | return NULL; /* wrong type */ |
250 | 0 | } |
251 | 0 | if (res2 <= 0) |
252 | 0 | break; |
253 | 0 | } |
254 | 0 | if (res2 == 0) { |
255 | 0 | if (idxptr2->allocated) { |
256 | | /* |
257 | | * No good: the index is in use. |
258 | | */ |
259 | 0 | return NULL; |
260 | 0 | } else { |
261 | | /* |
262 | | * Okay, it's unallocated, we can just claim ownership |
263 | | * here. |
264 | | */ |
265 | 0 | if ((rv = |
266 | 0 | snmp_clone_varbind(idxptr2->varbind)) != NULL) { |
267 | 0 | idxptr2->session = ss; |
268 | 0 | idxptr2->allocated = 1; |
269 | 0 | } |
270 | 0 | return rv; |
271 | 0 | } |
272 | 0 | } |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | | /* |
277 | | * OK - we've now located where the new entry needs to |
278 | | * be fitted into the index registry tree |
279 | | * To recap: |
280 | | * 'prev_oid_ptr' points to the head of the OID index |
281 | | * list prior to this one. If this is null, then |
282 | | * it means that this is the first OID in the list. |
283 | | * 'idxptr' points either to the head of this OID list, |
284 | | * or the next OID (if this is a new OID request) |
285 | | * These can be distinguished by the value of 'res'. |
286 | | * |
287 | | * 'prev_idx_ptr' points to the index entry that sorts |
288 | | * immediately prior to the requested value (if any). |
289 | | * If an arbitrary value is required, then this will |
290 | | * point to the last allocated index. |
291 | | * If this pointer is null, then either this is a new |
292 | | * OID request, or the requested value is the first |
293 | | * in the list. |
294 | | * 'idxptr2' points to the next sorted index (if any) |
295 | | * but is not actually needed any more. |
296 | | * |
297 | | * Clear? Good! |
298 | | * I hope you've been paying attention. |
299 | | * There'll be a test later :-) |
300 | | */ |
301 | | |
302 | | /* |
303 | | * We proceed by creating the new entry |
304 | | * (by copying the entry provided) |
305 | | */ |
306 | 0 | new_index = calloc(1, sizeof(struct snmp_index)); |
307 | 0 | if (new_index == NULL) |
308 | 0 | return NULL; |
309 | | |
310 | 0 | if (NULL == snmp_varlist_add_variable(&new_index->varbind, |
311 | 0 | varbind->name, |
312 | 0 | varbind->name_length, |
313 | 0 | varbind->type, |
314 | 0 | varbind->val.string, |
315 | 0 | varbind->val_len)) { |
316 | | /* |
317 | | * if (snmp_clone_var( varbind, new_index->varbind ) != 0 ) |
318 | | */ |
319 | 0 | free(new_index); |
320 | 0 | return NULL; |
321 | 0 | } |
322 | 0 | new_index->session = ss; |
323 | 0 | new_index->allocated = 1; |
324 | |
|
325 | 0 | if (varbind->type == ASN_OCTET_STR && flags == ALLOCATE_THIS_INDEX) |
326 | 0 | new_index->varbind->val.string[new_index->varbind->val_len] = 0; |
327 | | |
328 | | /* |
329 | | * If we've been given a value, then we can use that, but |
330 | | * otherwise, we need to create a new value for this entry. |
331 | | * Note that ANY_INDEX and NEW_INDEX are both covered by this |
332 | | * test (since NEW_INDEX & ANY_INDEX = ANY_INDEX, remember?) |
333 | | */ |
334 | 0 | if (flags & ALLOCATE_ANY_INDEX) { |
335 | 0 | if (prev_idx_ptr) { |
336 | 0 | snmp_free_var_internals(new_index->varbind); |
337 | 0 | if (snmp_clone_var(prev_idx_ptr->varbind, new_index->varbind) |
338 | 0 | != 0) { |
339 | 0 | snmp_free_var(new_index->varbind); |
340 | 0 | free(new_index); |
341 | 0 | return NULL; |
342 | 0 | } |
343 | 0 | } else { |
344 | 0 | if (new_index->varbind->val.string != new_index->varbind->buf) |
345 | 0 | free(new_index->varbind->val.string); |
346 | 0 | new_index->varbind->val.string = new_index->varbind->buf; |
347 | 0 | } |
348 | | |
349 | 0 | switch (varbind->type) { |
350 | 0 | case ASN_INTEGER: |
351 | 0 | if (prev_idx_ptr) { |
352 | 0 | (*new_index->varbind->val.integer)++; |
353 | 0 | } else |
354 | 0 | *(new_index->varbind->val.integer) = 1; |
355 | 0 | new_index->varbind->val_len = sizeof(long); |
356 | 0 | break; |
357 | 0 | case ASN_OCTET_STR: |
358 | 0 | if (prev_idx_ptr && new_index->varbind->val_len > 0) { |
359 | 0 | i = new_index->varbind->val_len - 1; |
360 | 0 | while (i >= 0 && new_index->varbind->val.string[i] == 'z') { |
361 | 0 | new_index->varbind->val.string[i] = 'a'; |
362 | 0 | i--; |
363 | 0 | if (i < 0) { |
364 | 0 | if (new_index->varbind->val_len + 1 >= |
365 | 0 | sizeof(new_index->varbind->buf)) { |
366 | 0 | snmp_free_var(new_index->varbind); |
367 | 0 | free(new_index); |
368 | 0 | return NULL; |
369 | 0 | } |
370 | 0 | i = new_index->varbind->val_len; |
371 | 0 | new_index->varbind->val.string[i] = 'a' - 1; |
372 | 0 | new_index->varbind->val.string[i + 1] = 0; |
373 | 0 | new_index->varbind->val_len++; |
374 | 0 | break; |
375 | 0 | } |
376 | 0 | } |
377 | 0 | if (i >= 0) |
378 | 0 | new_index->varbind->val.string[i]++; |
379 | 0 | } else { |
380 | 0 | strcpy((char *) new_index->varbind->buf, "aaaa"); |
381 | 0 | new_index->varbind->val.string = new_index->varbind->buf; |
382 | 0 | new_index->varbind->val_len = 4; |
383 | 0 | } |
384 | 0 | break; |
385 | 0 | case ASN_OBJECT_ID: |
386 | 0 | if (prev_idx_ptr) { |
387 | 0 | if (new_index->varbind->val_len < sizeof(oid)) { |
388 | 0 | snmp_free_var(new_index->varbind); |
389 | 0 | free(new_index); |
390 | 0 | return NULL; |
391 | 0 | } |
392 | 0 | i = prev_idx_ptr->varbind->val_len / sizeof(oid) - 1; |
393 | 0 | while (i >= 0 && new_index->varbind->val.objid[i] == 255) { |
394 | 0 | new_index->varbind->val.objid[i] = 1; |
395 | 0 | i--; |
396 | 0 | if (i < 0 || (i == 0 && new_index->varbind->val.objid[0] >= 2)) { |
397 | 0 | if (new_index->varbind->val_len + sizeof(oid) > |
398 | 0 | sizeof(new_index->varbind->buf)) { |
399 | 0 | snmp_free_var(new_index->varbind); |
400 | 0 | free(new_index); |
401 | 0 | return NULL; |
402 | 0 | } |
403 | 0 | new_index->varbind->val.objid[0] = 1; |
404 | 0 | i = new_index->varbind->val_len / sizeof(oid); |
405 | 0 | new_index->varbind->val.objid[i] = 0; |
406 | 0 | new_index->varbind->val_len += sizeof(oid); |
407 | 0 | break; |
408 | 0 | } |
409 | 0 | } |
410 | 0 | if (i >= 0) |
411 | 0 | new_index->varbind->val.objid[i]++; |
412 | 0 | } else { |
413 | | /* |
414 | | * If the requested OID name is small enough, |
415 | | * * append another OID (1) and use this as the |
416 | | * * default starting value for new indexes. |
417 | | */ |
418 | 0 | if ((varbind->name_length + 1) * sizeof(oid) <= |
419 | 0 | sizeof(new_index->varbind->buf)) { |
420 | 0 | for (i = 0; i < (int) varbind->name_length; i++) |
421 | 0 | new_index->varbind->val.objid[i] = |
422 | 0 | varbind->name[i]; |
423 | 0 | new_index->varbind->val.objid[varbind->name_length] = |
424 | 0 | 1; |
425 | 0 | new_index->varbind->val_len = |
426 | 0 | (varbind->name_length + 1) * sizeof(oid); |
427 | 0 | } else { |
428 | | /* |
429 | | * Otherwise use '.1.1.1.1...' |
430 | | */ |
431 | 0 | i = sizeof(new_index->varbind->buf) / sizeof(oid); |
432 | 0 | if (i > 4) |
433 | 0 | i = 4; |
434 | 0 | new_index->varbind->val_len = i * (sizeof(oid)); |
435 | 0 | for (i--; i >= 0; i--) |
436 | 0 | new_index->varbind->val.objid[i] = 1; |
437 | 0 | } |
438 | 0 | } |
439 | 0 | break; |
440 | 0 | default: |
441 | 0 | snmp_free_var(new_index->varbind); |
442 | 0 | free(new_index); |
443 | 0 | return NULL; /* Index type not supported */ |
444 | 0 | } |
445 | 0 | } |
446 | | |
447 | | /* |
448 | | * Try to duplicate the new varbind for return. |
449 | | */ |
450 | | |
451 | 0 | if ((rv = snmp_clone_varbind(new_index->varbind)) == NULL) { |
452 | 0 | snmp_free_var(new_index->varbind); |
453 | 0 | free(new_index); |
454 | 0 | return NULL; |
455 | 0 | } |
456 | | |
457 | | /* |
458 | | * Right - we've set up the new entry. |
459 | | * All that remains is to link it into the tree. |
460 | | * There are a number of possible cases here, |
461 | | * so watch carefully. |
462 | | */ |
463 | 0 | if (prev_idx_ptr) { |
464 | 0 | new_index->next_idx = prev_idx_ptr->next_idx; |
465 | 0 | new_index->next_oid = prev_idx_ptr->next_oid; |
466 | 0 | prev_idx_ptr->next_idx = new_index; |
467 | 0 | } else { |
468 | 0 | if (res == 0 && idxptr) { |
469 | 0 | new_index->next_idx = idxptr; |
470 | 0 | new_index->next_oid = idxptr->next_oid; |
471 | 0 | } else { |
472 | 0 | new_index->next_idx = NULL; |
473 | 0 | new_index->next_oid = idxptr; |
474 | 0 | } |
475 | |
|
476 | 0 | if (prev_oid_ptr) { |
477 | 0 | while (prev_oid_ptr) { |
478 | 0 | prev_oid_ptr->next_oid = new_index; |
479 | 0 | prev_oid_ptr = prev_oid_ptr->next_idx; |
480 | 0 | } |
481 | 0 | } else |
482 | 0 | snmp_index_head = new_index; |
483 | 0 | } |
484 | 0 | return rv; |
485 | 0 | } |
486 | | |
487 | | /* |
488 | | * Release an allocated index, |
489 | | * to allow it to be used elsewhere |
490 | | */ |
491 | | netsnmp_feature_child_of(release_index,netsnmp_unused); |
492 | | #ifndef NETSNMP_FEATURE_REMOVE_RELEASE_INDEX |
493 | | int |
494 | | release_index(netsnmp_variable_list * varbind) |
495 | 0 | { |
496 | 0 | return (unregister_index(varbind, TRUE, NULL)); |
497 | 0 | } |
498 | | #endif /* NETSNMP_FEATURE_REMOVE_RELEASE_INDEX */ |
499 | | |
500 | | #ifndef NETSNMP_FEATURE_REMOVE_REMOVE_INDEX |
501 | | /* |
502 | | * Completely remove an allocated index, |
503 | | * due to errors in the registration process. |
504 | | */ |
505 | | int |
506 | | remove_index(netsnmp_variable_list * varbind, netsnmp_session * ss) |
507 | 0 | { |
508 | 0 | return (unregister_index(varbind, FALSE, ss)); |
509 | 0 | } |
510 | | #endif /* NETSNMP_FEATURE_REMOVE_REMOVE_INDEX */ |
511 | | |
512 | | void |
513 | | unregister_index_by_session(netsnmp_session * ss) |
514 | 3.84k | { |
515 | 3.84k | struct snmp_index *idxptr, *idxptr2; |
516 | 3.84k | for (idxptr = snmp_index_head; idxptr != NULL; |
517 | 3.84k | idxptr = idxptr->next_oid) |
518 | 0 | for (idxptr2 = idxptr; idxptr2 != NULL; |
519 | 0 | idxptr2 = idxptr2->next_idx) |
520 | 0 | if (idxptr2->session == ss || |
521 | 0 | (ss && !(ss->flags & SNMP_FLAGS_SUBSESSION) && |
522 | 0 | idxptr2->session && idxptr2->session->subsession == ss)) { |
523 | 0 | idxptr2->allocated = 0; |
524 | 0 | idxptr2->session = NULL; |
525 | 0 | } |
526 | 3.84k | } |
527 | | |
528 | | |
529 | | int |
530 | | unregister_index(netsnmp_variable_list * varbind, int remember, |
531 | | netsnmp_session * ss) |
532 | 0 | { |
533 | 0 | struct snmp_index *idxptr, *idxptr2; |
534 | 0 | struct snmp_index *prev_oid_ptr, *prev_idx_ptr; |
535 | 0 | int res, res2, i; |
536 | |
|
537 | 0 | #if defined(USING_AGENTX_SUBAGENT_MODULE) && !defined(TESTING) |
538 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, |
539 | 0 | NETSNMP_DS_AGENT_ROLE) == SUB_AGENT) { |
540 | 0 | return (agentx_unregister_index(ss, varbind)); |
541 | 0 | } |
542 | 0 | #endif |
543 | | /* |
544 | | * Look for the requested OID entry |
545 | | */ |
546 | 0 | prev_oid_ptr = NULL; |
547 | 0 | prev_idx_ptr = NULL; |
548 | 0 | res = 1; |
549 | 0 | res2 = 1; |
550 | 0 | for (idxptr = snmp_index_head; idxptr != NULL; |
551 | 0 | prev_oid_ptr = idxptr, idxptr = idxptr->next_oid) { |
552 | 0 | if ((res = snmp_oid_compare(varbind->name, varbind->name_length, |
553 | 0 | idxptr->varbind->name, |
554 | 0 | idxptr->varbind->name_length)) <= 0) |
555 | 0 | break; |
556 | 0 | } |
557 | |
|
558 | 0 | if (res != 0) |
559 | 0 | return INDEX_ERR_NOT_ALLOCATED; |
560 | 0 | if (varbind->type != idxptr->varbind->type) |
561 | 0 | return INDEX_ERR_WRONG_TYPE; |
562 | | |
563 | 0 | for (idxptr2 = idxptr; idxptr2 != NULL; |
564 | 0 | prev_idx_ptr = idxptr2, idxptr2 = idxptr2->next_idx) { |
565 | 0 | switch (varbind->type) { |
566 | 0 | case ASN_INTEGER: |
567 | 0 | res2 = |
568 | 0 | (*varbind->val.integer - |
569 | 0 | *idxptr2->varbind->val.integer); |
570 | 0 | break; |
571 | 0 | case ASN_OCTET_STR: |
572 | 0 | i = SNMP_MIN(varbind->val_len, |
573 | 0 | idxptr2->varbind->val_len); |
574 | 0 | res2 = |
575 | 0 | memcmp(varbind->val.string, |
576 | 0 | idxptr2->varbind->val.string, i); |
577 | 0 | if (res2 == 0) |
578 | 0 | res2 = (int)varbind->val_len - (int)idxptr2->varbind->val_len; |
579 | 0 | break; |
580 | 0 | case ASN_OBJECT_ID: |
581 | 0 | res2 = |
582 | 0 | snmp_oid_compare(varbind->val.objid, |
583 | 0 | varbind->val_len / sizeof(oid), |
584 | 0 | idxptr2->varbind->val.objid, |
585 | 0 | idxptr2->varbind->val_len / |
586 | 0 | sizeof(oid)); |
587 | 0 | break; |
588 | 0 | default: |
589 | 0 | return INDEX_ERR_WRONG_TYPE; /* wrong type */ |
590 | 0 | } |
591 | 0 | if (res2 <= 0) |
592 | 0 | break; |
593 | 0 | } |
594 | 0 | if (res2 != 0 || (res2 == 0 && !idxptr2->allocated)) { |
595 | 0 | return INDEX_ERR_NOT_ALLOCATED; |
596 | 0 | } |
597 | 0 | if (ss != idxptr2->session) |
598 | 0 | return INDEX_ERR_WRONG_SESSION; |
599 | | |
600 | | /* |
601 | | * If this is a "normal" index unregistration, |
602 | | * mark the index entry as unused, but leave |
603 | | * it in situ. This allows differentiation |
604 | | * between ANY_INDEX and NEW_INDEX |
605 | | */ |
606 | 0 | if (remember) { |
607 | 0 | idxptr2->allocated = 0; /* Unused index */ |
608 | 0 | idxptr2->session = NULL; |
609 | 0 | return SNMP_ERR_NOERROR; |
610 | 0 | } |
611 | | /* |
612 | | * If this is a failed attempt to register a |
613 | | * number of indexes, the successful ones |
614 | | * must be removed completely. |
615 | | */ |
616 | 0 | if (prev_idx_ptr) { |
617 | 0 | prev_idx_ptr->next_idx = idxptr2->next_idx; |
618 | 0 | } else if (prev_oid_ptr) { |
619 | 0 | if (idxptr2->next_idx) /* Use p_idx_ptr as a temp variable */ |
620 | 0 | prev_idx_ptr = idxptr2->next_idx; |
621 | 0 | else |
622 | 0 | prev_idx_ptr = idxptr2->next_oid; |
623 | 0 | while (prev_oid_ptr) { |
624 | 0 | prev_oid_ptr->next_oid = prev_idx_ptr; |
625 | 0 | prev_oid_ptr = prev_oid_ptr->next_idx; |
626 | 0 | } |
627 | 0 | } else { |
628 | 0 | if (idxptr2->next_idx) |
629 | 0 | snmp_index_head = idxptr2->next_idx; |
630 | 0 | else |
631 | 0 | snmp_index_head = idxptr2->next_oid; |
632 | 0 | } |
633 | 0 | snmp_free_var(idxptr2->varbind); |
634 | 0 | free(idxptr2); |
635 | 0 | return SNMP_ERR_NOERROR; |
636 | 0 | } |
637 | | |
638 | | netsnmp_feature_child_of(unregister_indexes,netsnmp_unused); |
639 | | #ifndef NETSNMP_FEATURE_REMOVE_UNREGISTER_INDEXES |
640 | | int |
641 | | unregister_string_index(oid * name, size_t name_len, char *cp) |
642 | 0 | { |
643 | 0 | netsnmp_variable_list varbind; |
644 | 0 | int res; |
645 | |
|
646 | 0 | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
647 | 0 | varbind.type = ASN_OCTET_STR; |
648 | 0 | snmp_set_var_objid(&varbind, name, name_len); |
649 | 0 | snmp_set_var_value(&varbind, (u_char *) cp, strlen(cp)); |
650 | 0 | res = unregister_index(&varbind, FALSE, main_session); |
651 | 0 | snmp_free_var_internals(&varbind); |
652 | 0 | return res; |
653 | 0 | } |
654 | | |
655 | | int |
656 | | unregister_int_index(oid * name, size_t name_len, int val) |
657 | 0 | { |
658 | 0 | netsnmp_variable_list varbind; |
659 | 0 | int res; |
660 | |
|
661 | 0 | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
662 | 0 | varbind.type = ASN_INTEGER; |
663 | 0 | snmp_set_var_objid(&varbind, name, name_len); |
664 | 0 | varbind.val.string = varbind.buf; |
665 | 0 | varbind.val_len = sizeof(long); |
666 | 0 | *varbind.val.integer = val; |
667 | 0 | res = unregister_index(&varbind, FALSE, main_session); |
668 | 0 | snmp_free_var_internals(&varbind); |
669 | 0 | return res; |
670 | 0 | } |
671 | | |
672 | | int |
673 | | unregister_oid_index(oid * name, size_t name_len, |
674 | | oid * value, size_t value_len) |
675 | 0 | { |
676 | 0 | netsnmp_variable_list varbind; |
677 | 0 | int res; |
678 | |
|
679 | 0 | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
680 | 0 | varbind.type = ASN_OBJECT_ID; |
681 | 0 | snmp_set_var_objid(&varbind, name, name_len); |
682 | 0 | snmp_set_var_value(&varbind, (u_char *) value, |
683 | 0 | value_len * sizeof(oid)); |
684 | 0 | res = unregister_index(&varbind, FALSE, main_session); |
685 | 0 | snmp_free_var_internals(&varbind); |
686 | 0 | return res; |
687 | 0 | } |
688 | | #endif /* NETSNMP_FEATURE_REMOVE_UNREGISTER_INDEXES */ |
689 | | |
690 | | void |
691 | | dump_idx_registry(void) |
692 | 0 | { |
693 | 0 | struct snmp_index *idxptr, *idxptr2; |
694 | 0 | u_char *sbuf = NULL, *ebuf = NULL; |
695 | 0 | size_t sbuf_len = 0, sout_len = 0, ebuf_len = 0, eout_len = 0; |
696 | |
|
697 | 0 | if (snmp_index_head != NULL) { |
698 | 0 | printf("\nIndex Allocations:\n"); |
699 | 0 | } |
700 | |
|
701 | 0 | for (idxptr = snmp_index_head; idxptr != NULL; |
702 | 0 | idxptr = idxptr->next_oid) { |
703 | 0 | sout_len = 0; |
704 | 0 | if (sprint_realloc_objid(&sbuf, &sbuf_len, &sout_len, 1, |
705 | 0 | idxptr->varbind->name, |
706 | 0 | idxptr->varbind->name_length)) { |
707 | 0 | printf("%s indexes:\n", sbuf); |
708 | 0 | } else { |
709 | 0 | printf("%s [TRUNCATED] indexes:\n", sbuf); |
710 | 0 | } |
711 | |
|
712 | 0 | for (idxptr2 = idxptr; idxptr2 != NULL; |
713 | 0 | idxptr2 = idxptr2->next_idx) { |
714 | 0 | switch (idxptr2->varbind->type) { |
715 | 0 | case ASN_INTEGER: |
716 | 0 | printf(" %ld for session %8p, allocated %d\n", |
717 | 0 | *idxptr2->varbind->val.integer, idxptr2->session, |
718 | 0 | idxptr2->allocated); |
719 | 0 | break; |
720 | 0 | case ASN_OCTET_STR: |
721 | 0 | printf(" \"%s\" for session %8p, allocated %d\n", |
722 | 0 | idxptr2->varbind->val.string, idxptr2->session, |
723 | 0 | idxptr2->allocated); |
724 | 0 | break; |
725 | 0 | case ASN_OBJECT_ID: |
726 | 0 | eout_len = 0; |
727 | 0 | if (sprint_realloc_objid(&ebuf, &ebuf_len, &eout_len, 1, |
728 | 0 | idxptr2->varbind->val.objid, |
729 | 0 | idxptr2->varbind->val_len / |
730 | 0 | sizeof(oid))) { |
731 | 0 | printf(" %s for session %8p, allocated %d\n", ebuf, |
732 | 0 | idxptr2->session, idxptr2->allocated); |
733 | 0 | } else { |
734 | 0 | printf |
735 | 0 | (" %s [TRUNCATED] for sess %8p, allocated %d\n", |
736 | 0 | ebuf, idxptr2->session, idxptr2->allocated); |
737 | 0 | } |
738 | 0 | break; |
739 | 0 | default: |
740 | 0 | printf("unsupported type (%d/0x%02x)\n", |
741 | 0 | idxptr2->varbind->type, idxptr2->varbind->type); |
742 | 0 | } |
743 | 0 | } |
744 | 0 | } |
745 | | |
746 | 0 | if (sbuf != NULL) { |
747 | 0 | free(sbuf); |
748 | 0 | } |
749 | 0 | if (ebuf != NULL) { |
750 | 0 | free(ebuf); |
751 | 0 | } |
752 | 0 | } |
753 | | |
754 | | netsnmp_feature_child_of(count_indexes, netsnmp_unused); |
755 | | #ifndef NETSNMP_FEATURE_REMOVE_UNUSED |
756 | | unsigned long |
757 | | count_indexes(oid * name, size_t namelen, int include_unallocated) |
758 | 0 | { |
759 | 0 | struct snmp_index *i = NULL, *j = NULL; |
760 | 0 | unsigned long n = 0; |
761 | |
|
762 | 0 | for (i = snmp_index_head; i != NULL; i = i->next_oid) { |
763 | 0 | if (netsnmp_oid_equals(name, namelen, |
764 | 0 | i->varbind->name, |
765 | 0 | i->varbind->name_length) == 0) { |
766 | 0 | for (j = i; j != NULL; j = j->next_idx) { |
767 | 0 | if (j->allocated || include_unallocated) { |
768 | 0 | n++; |
769 | 0 | } |
770 | 0 | } |
771 | 0 | } |
772 | 0 | } |
773 | 0 | return n; |
774 | 0 | } |
775 | | #endif /* NETSNMP_FEATURE_REMOVE_UNUSED */ |
776 | | |
777 | | #ifdef TESTING |
778 | | netsnmp_variable_list varbind; |
779 | | netsnmp_session main_sess, *main_session = &main_sess; |
780 | | |
781 | | void |
782 | | test_string_register(int n, char *cp) |
783 | | { |
784 | | varbind->name[4] = n; |
785 | | if (register_string_index(varbind->name, varbind.name_length, cp) == |
786 | | NULL) |
787 | | printf("allocating %s failed\n", cp); |
788 | | } |
789 | | |
790 | | void |
791 | | test_int_register(int n, int val) |
792 | | { |
793 | | varbind->name[4] = n; |
794 | | if (register_int_index(varbind->name, varbind.name_length, val) == -1) |
795 | | printf("allocating %d/%d failed\n", n, val); |
796 | | } |
797 | | |
798 | | void |
799 | | test_oid_register(int n, int subid) |
800 | | { |
801 | | netsnmp_variable_list *res; |
802 | | |
803 | | varbind->name[4] = n; |
804 | | if (subid != -1) { |
805 | | varbind->val.objid[5] = subid; |
806 | | res = register_oid_index(varbind->name, varbind.name_length, |
807 | | varbind->val.objid, |
808 | | varbind->val_len / sizeof(oid)); |
809 | | } else |
810 | | res = |
811 | | register_oid_index(varbind->name, varbind.name_length, NULL, |
812 | | 0); |
813 | | |
814 | | if (res == NULL) |
815 | | printf("allocating %d/%d failed\n", n, subid); |
816 | | } |
817 | | |
818 | | void |
819 | | main(int argc, char argv[]) |
820 | | { |
821 | | oid name[] = { 1, 2, 3, 4, 0 }; |
822 | | int i; |
823 | | |
824 | | memset(&varbind, 0, sizeof(netsnmp_variable_list)); |
825 | | snmp_set_var_objid(&varbind, name, 5); |
826 | | varbind->type = ASN_OCTET_STR; |
827 | | /* |
828 | | * Test index structure linking: |
829 | | * a) sorted by OID |
830 | | */ |
831 | | test_string_register(20, "empty OID"); |
832 | | test_string_register(10, "first OID"); |
833 | | test_string_register(40, "last OID"); |
834 | | test_string_register(30, "middle OID"); |
835 | | |
836 | | /* |
837 | | * b) sorted by index value |
838 | | */ |
839 | | test_string_register(25, "eee: empty IDX"); |
840 | | test_string_register(25, "aaa: first IDX"); |
841 | | test_string_register(25, "zzz: last IDX"); |
842 | | test_string_register(25, "mmm: middle IDX"); |
843 | | printf("This next one should fail....\n"); |
844 | | test_string_register(25, "eee: empty IDX"); /* duplicate */ |
845 | | printf("done\n"); |
846 | | |
847 | | /* |
848 | | * c) test initial index linking |
849 | | */ |
850 | | test_string_register(5, "eee: empty initial IDX"); |
851 | | test_string_register(5, "aaa: replace initial IDX"); |
852 | | |
853 | | /* |
854 | | * Did it all work? |
855 | | */ |
856 | | dump_idx_registry(); |
857 | | unregister_index_by_session(main_session); |
858 | | /* |
859 | | * Now test index allocation |
860 | | * a) integer values |
861 | | */ |
862 | | test_int_register(110, -1); /* empty */ |
863 | | test_int_register(110, -1); /* append */ |
864 | | test_int_register(110, 10); /* append exact */ |
865 | | printf("This next one should fail....\n"); |
866 | | test_int_register(110, 10); /* exact duplicate */ |
867 | | printf("done\n"); |
868 | | test_int_register(110, -1); /* append */ |
869 | | test_int_register(110, 5); /* insert exact */ |
870 | | |
871 | | /* |
872 | | * b) string values |
873 | | */ |
874 | | test_string_register(120, NULL); /* empty */ |
875 | | test_string_register(120, NULL); /* append */ |
876 | | test_string_register(120, "aaaz"); |
877 | | test_string_register(120, NULL); /* minor rollover */ |
878 | | test_string_register(120, "zzzz"); |
879 | | test_string_register(120, NULL); /* major rollover */ |
880 | | |
881 | | /* |
882 | | * c) OID values |
883 | | */ |
884 | | |
885 | | test_oid_register(130, -1); /* empty */ |
886 | | test_oid_register(130, -1); /* append */ |
887 | | |
888 | | varbind->val_len = varbind.name_length * sizeof(oid); |
889 | | memcpy(varbind->buf, varbind.name, varbind.val_len); |
890 | | varbind->val.objid = (oid *) varbind.buf; |
891 | | varbind->val_len += sizeof(oid); |
892 | | |
893 | | test_oid_register(130, 255); /* append exact */ |
894 | | test_oid_register(130, -1); /* minor rollover */ |
895 | | test_oid_register(130, 100); /* insert exact */ |
896 | | printf("This next one should fail....\n"); |
897 | | test_oid_register(130, 100); /* exact duplicate */ |
898 | | printf("done\n"); |
899 | | |
900 | | varbind->val.objid = (oid *) varbind.buf; |
901 | | for (i = 0; i < 6; i++) |
902 | | varbind->val.objid[i] = 255; |
903 | | varbind->val.objid[0] = 1; |
904 | | test_oid_register(130, 255); /* set up rollover */ |
905 | | test_oid_register(130, -1); /* medium rollover */ |
906 | | |
907 | | for (i = 0; i < 6; i++) |
908 | | varbind->val.objid[i] = 255; |
909 | | varbind->val.objid[0] = 2; |
910 | | test_oid_register(130, 255); /* set up rollover */ |
911 | | test_oid_register(130, -1); /* major rollover */ |
912 | | |
913 | | /* |
914 | | * Did it all work? |
915 | | */ |
916 | | dump_idx_registry(); |
917 | | |
918 | | /* |
919 | | * Test the various "invalid" requests |
920 | | * (unsupported types, mis-matched types, etc) |
921 | | */ |
922 | | printf("The rest of these should fail....\n"); |
923 | | test_oid_register(110, -1); |
924 | | test_oid_register(110, 100); |
925 | | test_oid_register(120, -1); |
926 | | test_oid_register(120, 100); |
927 | | test_string_register(110, NULL); |
928 | | test_string_register(110, "aaaa"); |
929 | | test_string_register(130, NULL); |
930 | | test_string_register(130, "aaaa"); |
931 | | test_int_register(120, -1); |
932 | | test_int_register(120, 1); |
933 | | test_int_register(130, -1); |
934 | | test_int_register(130, 1); |
935 | | printf("done - this dump should be the same as before\n"); |
936 | | dump_idx_registry(); |
937 | | } |
938 | | #endif |