/src/net-snmp/snmplib/mib.c
Line | Count | Source |
1 | | /* |
2 | | * mib.c |
3 | | * |
4 | | * $Id$ |
5 | | * |
6 | | * Update: 1998-07-17 <jhy@gsu.edu> |
7 | | * Added print_oid_report* functions. |
8 | | * |
9 | | */ |
10 | | /* Portions of this file are subject to the following copyrights. See |
11 | | * the Net-SNMP's COPYING file for more details and other copyrights |
12 | | * that may apply: |
13 | | */ |
14 | | /********************************************************************** |
15 | | Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University |
16 | | |
17 | | All Rights Reserved |
18 | | |
19 | | Permission to use, copy, modify, and distribute this software and its |
20 | | documentation for any purpose and without fee is hereby granted, |
21 | | provided that the above copyright notice appear in all copies and that |
22 | | both that copyright notice and this permission notice appear in |
23 | | supporting documentation, and that the name of CMU not be |
24 | | used in advertising or publicity pertaining to distribution of the |
25 | | software without specific, written prior permission. |
26 | | |
27 | | CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
28 | | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL |
29 | | CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
30 | | ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
31 | | WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
32 | | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
33 | | SOFTWARE. |
34 | | ******************************************************************/ |
35 | | /* |
36 | | * Copyright © 2003 Sun Microsystems, Inc. All rights reserved. |
37 | | * Use is subject to license terms specified in the COPYING file |
38 | | * distributed with the Net-SNMP package. |
39 | | * |
40 | | * Portions of this file are copyrighted by: |
41 | | * Copyright (c) 2016 VMware, Inc. All rights reserved. |
42 | | * Use is subject to license terms specified in the COPYING file |
43 | | * distributed with the Net-SNMP package. |
44 | | */ |
45 | | #include <net-snmp/net-snmp-config.h> |
46 | | #include <net-snmp/net-snmp-features.h> |
47 | | |
48 | | #include <stdio.h> |
49 | | #include <ctype.h> |
50 | | #include <sys/types.h> |
51 | | |
52 | | #ifdef HAVE_DIRENT_H |
53 | | #include <dirent.h> |
54 | | #endif |
55 | | |
56 | | #ifdef HAVE_INTTYPES_H |
57 | | #include <inttypes.h> |
58 | | #endif |
59 | | #ifdef HAVE_NETINET_IN_H |
60 | | #include <netinet/in.h> |
61 | | #endif |
62 | | #ifdef TIME_WITH_SYS_TIME |
63 | | # include <sys/time.h> |
64 | | # include <time.h> |
65 | | #else |
66 | | # ifdef HAVE_SYS_TIME_H |
67 | | # include <sys/time.h> |
68 | | # else |
69 | | # include <time.h> |
70 | | # endif |
71 | | #endif |
72 | | #ifdef HAVE_STRING_H |
73 | | #include <string.h> |
74 | | #else |
75 | | #include <strings.h> |
76 | | #endif |
77 | | #ifdef HAVE_STDLIB_H |
78 | | #include <stdlib.h> |
79 | | #endif |
80 | | #ifdef HAVE_SYS_SELECT_H |
81 | | #include <sys/select.h> |
82 | | #endif |
83 | | |
84 | | #ifdef HAVE_UNISTD_H |
85 | | #include <unistd.h> |
86 | | #endif |
87 | | |
88 | | #include <net-snmp/types.h> |
89 | | #include <net-snmp/output_api.h> |
90 | | #include <net-snmp/config_api.h> |
91 | | #include <net-snmp/utilities.h> |
92 | | |
93 | | #include <net-snmp/library/asn1.h> |
94 | | #include <net-snmp/library/snmp_api.h> |
95 | | #include <net-snmp/library/mib.h> |
96 | | #include <net-snmp/library/parse.h> |
97 | | #include <net-snmp/library/int64.h> |
98 | | #include <net-snmp/library/snmp_client.h> |
99 | | |
100 | | netsnmp_feature_child_of(mib_api, libnetsnmp); |
101 | | netsnmp_feature_child_of(mib_strings_all, mib_api); |
102 | | |
103 | | netsnmp_feature_child_of(mib_snprint, mib_strings_all); |
104 | | netsnmp_feature_child_of(mib_snprint_description, mib_strings_all); |
105 | | netsnmp_feature_child_of(mib_snprint_variable, mib_strings_all); |
106 | | netsnmp_feature_child_of(mib_string_conversions, mib_strings_all); |
107 | | netsnmp_feature_child_of(print_mib, mib_strings_all); |
108 | | netsnmp_feature_child_of(snprint_objid, mib_strings_all); |
109 | | netsnmp_feature_child_of(snprint_value, mib_strings_all); |
110 | | |
111 | | netsnmp_feature_child_of(mib_to_asn_type, mib_api); |
112 | | |
113 | | /** @defgroup mib_utilities mib parsing and datatype manipulation routines. |
114 | | * @ingroup library |
115 | | * |
116 | | * @{ |
117 | | */ |
118 | | |
119 | | static char *uptimeString(u_long, char *, size_t); |
120 | | |
121 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
122 | | static struct tree *_get_realloc_symbol(const oid * objid, size_t objidlen, |
123 | | struct tree *subtree, |
124 | | u_char ** buf, size_t * buf_len, |
125 | | size_t * out_len, |
126 | | int allow_realloc, |
127 | | int *buf_overflow, |
128 | | struct index_list *in_dices, |
129 | | size_t * end_of_known); |
130 | | |
131 | | static int print_tree_node(u_char ** buf, size_t * buf_len, |
132 | | size_t * out_len, int allow_realloc, |
133 | | struct tree *tp, int width); |
134 | | static void handle_mibdirs_conf(const char *token, char *line); |
135 | | static void handle_mibs_conf(const char *token, char *line); |
136 | | static void handle_mibfile_conf(const char *token, char *line); |
137 | | #endif /*NETSNMP_DISABLE_MIB_LOADING */ |
138 | | |
139 | | static void _oid_finish_printing(const oid * objid, size_t objidlen, |
140 | | u_char ** buf, size_t * buf_len, |
141 | | size_t * out_len, |
142 | | int allow_realloc, int *buf_overflow); |
143 | | |
144 | | /* |
145 | | * helper functions for get_module_node |
146 | | */ |
147 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
148 | | static int node_to_oid(struct tree *, oid *, size_t *); |
149 | | static int _add_strings_to_oid(struct tree *, char *, |
150 | | oid *, size_t *, size_t); |
151 | | #else |
152 | | static int _add_strings_to_oid(void *, char *, |
153 | | oid *, size_t *, size_t); |
154 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
155 | | |
156 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
157 | | NETSNMP_IMPORT struct tree *tree_head; |
158 | | static struct tree *tree_top; |
159 | | |
160 | | NETSNMP_IMPORT struct tree *Mib; |
161 | | struct tree *Mib; /* Backwards compatibility */ |
162 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
163 | | |
164 | | static char Standard_Prefix[] = ".1.3.6.1.2.1"; |
165 | | |
166 | | /* |
167 | | * Set default here as some uses of read_objid require valid pointer. |
168 | | */ |
169 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
170 | | static char *Prefix = &Standard_Prefix[0]; |
171 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
172 | | typedef struct _PrefixList { |
173 | | const char *str; |
174 | | int len; |
175 | | } *PrefixListPtr, PrefixList; |
176 | | |
177 | | /* |
178 | | * Here are the prefix strings. |
179 | | * Note that the first one finds the value of Prefix or Standard_Prefix. |
180 | | * Any of these MAY start with period; all will NOT end with period. |
181 | | * Period is added where needed. See use of Prefix in this module. |
182 | | */ |
183 | | PrefixList mib_prefixes[] = { |
184 | | {&Standard_Prefix[0]}, /* placeholder for Prefix data */ |
185 | | {".iso.org.dod.internet.mgmt.mib-2"}, |
186 | | {".iso.org.dod.internet.experimental"}, |
187 | | {".iso.org.dod.internet.private"}, |
188 | | {".iso.org.dod.internet.snmpParties"}, |
189 | | {".iso.org.dod.internet.snmpSecrets"}, |
190 | | {NULL, 0} /* end of list */ |
191 | | }; |
192 | | |
193 | | enum inet_address_type { |
194 | | IPV4 = 1, |
195 | | IPV6 = 2, |
196 | | IPV4Z = 3, |
197 | | IPV6Z = 4, |
198 | | DNS = 16 |
199 | | }; |
200 | | |
201 | | |
202 | | /** |
203 | | * @internal |
204 | | * Converts timeticks to hours, minutes, seconds string. |
205 | | * |
206 | | * @param timeticks The timeticks to convert. |
207 | | * @param buf Buffer to write to, has to be at |
208 | | * least 40 Bytes large. |
209 | | * |
210 | | * @return The buffer. |
211 | | */ |
212 | | static char * |
213 | | uptimeString(u_long timeticks, char *buf, size_t buflen) |
214 | 0 | { |
215 | 0 | int centisecs, seconds, minutes, hours, days; |
216 | |
|
217 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS)) { |
218 | 0 | snprintf(buf, buflen, "%lu", timeticks); |
219 | 0 | return buf; |
220 | 0 | } |
221 | | |
222 | | |
223 | 0 | centisecs = timeticks % 100; |
224 | 0 | timeticks /= 100; |
225 | 0 | days = timeticks / (60 * 60 * 24); |
226 | 0 | timeticks %= (60 * 60 * 24); |
227 | |
|
228 | 0 | hours = timeticks / (60 * 60); |
229 | 0 | timeticks %= (60 * 60); |
230 | |
|
231 | 0 | minutes = timeticks / 60; |
232 | 0 | seconds = timeticks % 60; |
233 | |
|
234 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) |
235 | 0 | snprintf(buf, buflen, "%d:%d:%02d:%02d.%02d", |
236 | 0 | days, hours, minutes, seconds, centisecs); |
237 | 0 | else { |
238 | 0 | if (days == 0) { |
239 | 0 | snprintf(buf, buflen, "%d:%02d:%02d.%02d", |
240 | 0 | hours, minutes, seconds, centisecs); |
241 | 0 | } else if (days == 1) { |
242 | 0 | snprintf(buf, buflen, "%d day, %d:%02d:%02d.%02d", |
243 | 0 | days, hours, minutes, seconds, centisecs); |
244 | 0 | } else { |
245 | 0 | snprintf(buf, buflen, "%d days, %d:%02d:%02d.%02d", |
246 | 0 | days, hours, minutes, seconds, centisecs); |
247 | 0 | } |
248 | 0 | } |
249 | 0 | return buf; |
250 | 0 | } |
251 | | |
252 | | |
253 | | |
254 | | /** |
255 | | * @internal |
256 | | * Prints the character pointed to if in human-readable ASCII range, |
257 | | * otherwise prints a dot. |
258 | | * |
259 | | * @param buf Buffer to print the character to. |
260 | | * @param ch Character to print. |
261 | | */ |
262 | | static void |
263 | | sprint_char(char *buf, const u_char ch) |
264 | 0 | { |
265 | 0 | if (isprint(ch) || isspace(ch)) { |
266 | 0 | sprintf(buf, "%c", (int) ch); |
267 | 0 | } else { |
268 | 0 | sprintf(buf, "."); |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | | |
273 | | |
274 | | /** |
275 | | * Prints a hexadecimal string into a buffer. |
276 | | * |
277 | | * The characters pointed by *cp are encoded as hexadecimal string. |
278 | | * |
279 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
280 | | * needed size. (Note: *buf may change due to this.) |
281 | | * |
282 | | * @param buf address of the buffer to print to. |
283 | | * @param buf_len address to an integer containing the size of buf. |
284 | | * @param out_len incremented by the number of characters printed. |
285 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
286 | | * needed size. |
287 | | * @param cp the array of characters to encode. |
288 | | * @param line_len the array length of cp. |
289 | | * |
290 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
291 | | * small when not allowed to realloc.) |
292 | | */ |
293 | | int |
294 | | _sprint_hexstring_line(u_char ** buf, size_t * buf_len, size_t * out_len, |
295 | | int allow_realloc, const u_char * cp, size_t line_len) |
296 | 0 | { |
297 | 0 | const u_char *tp; |
298 | 0 | const u_char *cp2 = cp; |
299 | 0 | size_t lenleft = line_len; |
300 | | |
301 | | /* |
302 | | * Make sure there's enough room for the hex output.... |
303 | | */ |
304 | 0 | while ((*out_len + line_len*3+1) >= *buf_len) { |
305 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
306 | 0 | return 0; |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | | /* |
311 | | * .... and display the hex values themselves.... |
312 | | */ |
313 | 0 | for (; lenleft >= 8; lenleft-=8) { |
314 | 0 | snprintf((char *) (*buf + *out_len), *buf_len - *out_len, |
315 | 0 | "%02X %02X %02X %02X %02X %02X %02X %02X ", cp[0], cp[1], |
316 | 0 | cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]); |
317 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
318 | 0 | cp += 8; |
319 | 0 | } |
320 | 0 | for (; lenleft > 0; lenleft--) { |
321 | 0 | snprintf((char *) (*buf + *out_len), *buf_len - *out_len, "%02X ", *cp++); |
322 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
323 | 0 | } |
324 | | |
325 | | /* |
326 | | * .... plus (optionally) do the same for the ASCII equivalent. |
327 | | */ |
328 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT)) { |
329 | 0 | while ((*out_len + line_len+5) >= *buf_len) { |
330 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
331 | 0 | return 0; |
332 | 0 | } |
333 | 0 | } |
334 | 0 | snprintf((char *) (*buf + *out_len), *buf_len - *out_len, " ["); |
335 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
336 | 0 | for (tp = cp2; tp < cp; tp++) { |
337 | 0 | sprint_char((char *) (*buf + *out_len), *tp); |
338 | 0 | (*out_len)++; |
339 | 0 | } |
340 | 0 | snprintf((char *) (*buf + *out_len), *buf_len - *out_len, "]"); |
341 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
342 | 0 | } |
343 | 0 | return 1; |
344 | 0 | } |
345 | | |
346 | | int |
347 | | sprint_realloc_hexstring(u_char ** buf, size_t * buf_len, size_t * out_len, |
348 | | int allow_realloc, const u_char * cp, size_t len) |
349 | 0 | { |
350 | 0 | int line_len = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, |
351 | 0 | NETSNMP_DS_LIB_HEX_OUTPUT_LENGTH); |
352 | 0 | if (line_len <= 0) |
353 | 0 | line_len = len; |
354 | |
|
355 | 0 | for (; (int)len > line_len; len -= line_len) { |
356 | 0 | if(!_sprint_hexstring_line(buf, buf_len, out_len, allow_realloc, cp, line_len)) |
357 | 0 | return 0; |
358 | 0 | *(*buf + (*out_len)++) = '\n'; |
359 | 0 | *(*buf + *out_len) = 0; |
360 | 0 | cp += line_len; |
361 | 0 | } |
362 | 0 | if(!_sprint_hexstring_line(buf, buf_len, out_len, allow_realloc, cp, len)) |
363 | 0 | return 0; |
364 | 0 | *(*buf + *out_len) = 0; |
365 | 0 | return 1; |
366 | 0 | } |
367 | | |
368 | | |
369 | | |
370 | | /** |
371 | | * Prints an ascii string into a buffer. |
372 | | * |
373 | | * The characters pointed by *cp are encoded as an ascii string. |
374 | | * |
375 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
376 | | * needed size. (Note: *buf may change due to this.) |
377 | | * |
378 | | * @param buf address of the buffer to print to. |
379 | | * @param buf_len address to an integer containing the size of buf. |
380 | | * @param out_len incremented by the number of characters printed. |
381 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
382 | | * needed size. |
383 | | * @param cp the array of characters to encode. |
384 | | * @param len the array length of cp. |
385 | | * |
386 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
387 | | * small when not allowed to realloc.) |
388 | | */ |
389 | | int |
390 | | sprint_realloc_asciistring(u_char ** buf, size_t * buf_len, |
391 | | size_t * out_len, int allow_realloc, |
392 | | const u_char * cp, size_t len) |
393 | 0 | { |
394 | 0 | int i; |
395 | |
|
396 | 0 | for (i = 0; i < (int) len; i++) { |
397 | 0 | if (isprint(*cp) || isspace(*cp)) { |
398 | 0 | if (*cp == '\\' || *cp == '"') { |
399 | 0 | if ((*out_len >= *buf_len) && |
400 | 0 | !(allow_realloc && snmp_realloc(buf, buf_len))) { |
401 | 0 | return 0; |
402 | 0 | } |
403 | 0 | *(*buf + (*out_len)++) = '\\'; |
404 | 0 | } |
405 | 0 | if ((*out_len >= *buf_len) && |
406 | 0 | !(allow_realloc && snmp_realloc(buf, buf_len))) { |
407 | 0 | return 0; |
408 | 0 | } |
409 | 0 | *(*buf + (*out_len)++) = *cp++; |
410 | 0 | } else { |
411 | 0 | if ((*out_len >= *buf_len) && |
412 | 0 | !(allow_realloc && snmp_realloc(buf, buf_len))) { |
413 | 0 | return 0; |
414 | 0 | } |
415 | 0 | *(*buf + (*out_len)++) = '.'; |
416 | 0 | cp++; |
417 | 0 | } |
418 | 0 | } |
419 | 0 | if ((*out_len >= *buf_len) && |
420 | 0 | !(allow_realloc && snmp_realloc(buf, buf_len))) { |
421 | 0 | return 0; |
422 | 0 | } |
423 | 0 | *(*buf + *out_len) = '\0'; |
424 | 0 | return 1; |
425 | 0 | } |
426 | | |
427 | | /** |
428 | | * Prints an octet string into a buffer. |
429 | | * |
430 | | * The variable var is encoded as octet string. |
431 | | * |
432 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
433 | | * needed size. (Note: *buf may change due to this.) |
434 | | * |
435 | | * @param buf Address of the buffer to print to. |
436 | | * @param buf_len Address to an integer containing the size of buf. |
437 | | * @param out_len Incremented by the number of characters printed. |
438 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
439 | | * needed size. |
440 | | * @param var The variable to encode. |
441 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
442 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
443 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
444 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
445 | | * |
446 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
447 | | * small when not allowed to realloc.) |
448 | | */ |
449 | | int |
450 | | sprint_realloc_octet_string(u_char ** buf, size_t * buf_len, |
451 | | size_t * out_len, int allow_realloc, |
452 | | const netsnmp_variable_list * var, |
453 | | const struct enum_list *enums, const char *hint, |
454 | | const char *units) |
455 | 0 | { |
456 | 0 | size_t saved_out_len = *out_len; |
457 | 0 | const char *saved_hint = hint; |
458 | 0 | int hex = 0, x = 0; |
459 | 0 | u_char *cp; |
460 | 0 | int output_format, cnt; |
461 | |
|
462 | 0 | if (var->type != ASN_OCTET_STR) { |
463 | 0 | if (!netsnmp_ds_get_boolean( |
464 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
465 | 0 | const char str[] = "Wrong Type (should be OCTET STRING): "; |
466 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
467 | 0 | return 0; |
468 | 0 | } |
469 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
470 | 0 | allow_realloc, var, NULL, NULL, |
471 | 0 | NULL); |
472 | 0 | } |
473 | | |
474 | | |
475 | 0 | if (hint) { |
476 | 0 | int repeat, width = 1, has_width; |
477 | 0 | unsigned long value; |
478 | 0 | char code = 'd', separ = 0, term = 0, ch, intbuf[32]; |
479 | 0 | #define HEX2DIGIT_NEED_INIT 3 |
480 | 0 | char hex2digit = HEX2DIGIT_NEED_INIT; |
481 | 0 | u_char *ecp; |
482 | |
|
483 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
484 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
485 | 0 | "STRING: ")) { |
486 | 0 | return 0; |
487 | 0 | } |
488 | 0 | } |
489 | 0 | cp = var->val.string; |
490 | 0 | ecp = cp + var->val_len; |
491 | |
|
492 | 0 | while (cp < ecp) { |
493 | 0 | repeat = 1; |
494 | 0 | if (*hint) { |
495 | 0 | if (*hint == '*') { |
496 | 0 | repeat = *cp++; |
497 | 0 | hint++; |
498 | 0 | } |
499 | 0 | width = 0; |
500 | 0 | has_width = '0' <= *hint && *hint <= '9'; |
501 | 0 | while ('0' <= *hint && *hint <= '9') |
502 | 0 | width = (width * 10) + (*hint++ - '0'); |
503 | 0 | code = *hint++; |
504 | 0 | if ((ch = *hint) && ch != '*' && (ch < '0' || ch > '9') |
505 | 0 | && (width != 0 |
506 | 0 | || (ch != 'x' && ch != 'd' && ch != 'o'))) |
507 | 0 | separ = *hint++; |
508 | 0 | else |
509 | 0 | separ = 0; |
510 | 0 | if ((ch = *hint) && ch != '*' && (ch < '0' || ch > '9') |
511 | 0 | && (width != 0 |
512 | 0 | || (ch != 'x' && ch != 'd' && ch != 'o'))) |
513 | 0 | term = *hint++; |
514 | 0 | else |
515 | 0 | term = 0; |
516 | | /* |
517 | | * A spec without an octet length (e.g. "t") is malformed, |
518 | | * use one octet like before. Only an explicit zero length |
519 | | * (e.g. "0a[") is a zero-width spec. |
520 | | */ |
521 | 0 | if (!has_width) |
522 | 0 | width = 1; |
523 | 0 | } |
524 | | |
525 | | /* |
526 | | * Handle zero-width display hint specs (RFC 2579 Section 3.1). |
527 | | * Zero-width specs emit literal characters (separator/terminator) |
528 | | * without consuming any data bytes. |
529 | | */ |
530 | 0 | if (width == 0) { |
531 | 0 | char literal[2] = {0, 0}; |
532 | 0 | int is_last_spec = (*hint == '\0'); |
533 | 0 | while (repeat > 0 && separ) { |
534 | 0 | repeat--; |
535 | | /* RFC 2579: suppress trailing separator */ |
536 | 0 | if (is_last_spec && repeat == 0) |
537 | 0 | break; |
538 | 0 | literal[0] = separ; |
539 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
540 | 0 | literal)) |
541 | 0 | return 0; |
542 | 0 | } |
543 | | /* RFC 2579: suppress trailing terminator */ |
544 | 0 | if (term && !is_last_spec) { |
545 | 0 | literal[0] = term; |
546 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
547 | 0 | literal)) |
548 | 0 | return 0; |
549 | 0 | } |
550 | 0 | if (is_last_spec) |
551 | 0 | break; /* Hint exhausted, cannot consume more data */ |
552 | 0 | continue; |
553 | 0 | } |
554 | | |
555 | 0 | while (repeat && cp < ecp) { |
556 | 0 | value = 0; |
557 | 0 | if (code != 'a' && code != 't') { |
558 | 0 | for (x = 0; x < width && cp < ecp; x++) { |
559 | 0 | value = value * 256 + *cp++; |
560 | 0 | } |
561 | 0 | } |
562 | 0 | switch (code) { |
563 | 0 | case 'x': |
564 | 0 | if (HEX2DIGIT_NEED_INIT == hex2digit) |
565 | 0 | hex2digit = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
566 | 0 | NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT); |
567 | | /* |
568 | | * if value is < 16, it will be a single hex digit. If the |
569 | | * width is 1 (we are outputting a byte at a time), pat it |
570 | | * to 2 digits if NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT is set |
571 | | * or all of the following are true: |
572 | | * - we do not have a separation character |
573 | | * - there is no hint left (or there never was a hint) |
574 | | * |
575 | | * e.g. for the data 0xAA01BB, would anyone really ever |
576 | | * want the string "AA1BB"?? |
577 | | */ |
578 | 0 | if (((value < 16) && (1 == width)) && |
579 | 0 | (hex2digit || ((0 == separ) && (0 == *hint)))) { |
580 | 0 | snprintf(intbuf, sizeof(intbuf), "0%lx", value); |
581 | 0 | } else { |
582 | 0 | snprintf(intbuf, sizeof(intbuf), "%lx", value); |
583 | 0 | } |
584 | 0 | if (!snmp_cstrcat |
585 | 0 | (buf, buf_len, out_len, allow_realloc, intbuf)) { |
586 | 0 | return 0; |
587 | 0 | } |
588 | 0 | break; |
589 | 0 | case 'd': |
590 | 0 | snprintf(intbuf, sizeof(intbuf), "%lu", value); |
591 | 0 | if (!snmp_cstrcat |
592 | 0 | (buf, buf_len, out_len, allow_realloc, intbuf)) { |
593 | 0 | return 0; |
594 | 0 | } |
595 | 0 | break; |
596 | 0 | case 'o': |
597 | 0 | snprintf(intbuf, sizeof(intbuf), "%lo", value); |
598 | 0 | if (!snmp_cstrcat |
599 | 0 | (buf, buf_len, out_len, allow_realloc, intbuf)) { |
600 | 0 | return 0; |
601 | 0 | } |
602 | 0 | break; |
603 | 0 | case 't': /* new in rfc 3411 */ |
604 | 0 | case 'a': |
605 | | /* A string hint gives the max size - we may not need this much */ |
606 | 0 | cnt = SNMP_MIN(width, ecp - cp); |
607 | 0 | while ((*out_len + cnt + 1) > *buf_len) { |
608 | 0 | if (!allow_realloc || !snmp_realloc(buf, buf_len)) |
609 | 0 | return 0; |
610 | 0 | } |
611 | 0 | if (memchr(cp, '\0', cnt) == NULL) { |
612 | | /* No embedded '\0' - use memcpy() to preserve UTF-8 */ |
613 | 0 | memcpy(*buf + *out_len, cp, cnt); |
614 | 0 | *out_len += cnt; |
615 | 0 | *(*buf + *out_len) = '\0'; |
616 | 0 | } else if (!sprint_realloc_asciistring(buf, buf_len, |
617 | 0 | out_len, allow_realloc, cp, cnt)) { |
618 | 0 | return 0; |
619 | 0 | } |
620 | 0 | cp += cnt; |
621 | 0 | break; |
622 | 0 | default: |
623 | 0 | *out_len = saved_out_len; |
624 | 0 | if (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
625 | 0 | "(Bad hint ignored: ") |
626 | 0 | && snmp_cstrcat(buf, buf_len, out_len, |
627 | 0 | allow_realloc, saved_hint) |
628 | 0 | && snmp_cstrcat(buf, buf_len, out_len, |
629 | 0 | allow_realloc, ") ")) { |
630 | 0 | return sprint_realloc_octet_string(buf, buf_len, |
631 | 0 | out_len, |
632 | 0 | allow_realloc, |
633 | 0 | var, enums, |
634 | 0 | NULL, NULL); |
635 | 0 | } else { |
636 | 0 | return 0; |
637 | 0 | } |
638 | 0 | } |
639 | | |
640 | 0 | if (cp < ecp && separ) { |
641 | 0 | while ((*out_len + 1) >= *buf_len) { |
642 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
643 | 0 | return 0; |
644 | 0 | } |
645 | 0 | } |
646 | 0 | *(*buf + *out_len) = separ; |
647 | 0 | (*out_len)++; |
648 | 0 | *(*buf + *out_len) = '\0'; |
649 | 0 | } |
650 | 0 | repeat--; |
651 | 0 | } |
652 | | |
653 | 0 | if (term && cp < ecp) { |
654 | 0 | while ((*out_len + 1) >= *buf_len) { |
655 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
656 | 0 | return 0; |
657 | 0 | } |
658 | 0 | } |
659 | 0 | *(*buf + *out_len) = term; |
660 | 0 | (*out_len)++; |
661 | 0 | *(*buf + *out_len) = '\0'; |
662 | 0 | } |
663 | 0 | } |
664 | | |
665 | 0 | if (units) { |
666 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
667 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
668 | 0 | } |
669 | 0 | if ((*out_len >= *buf_len) && |
670 | 0 | !(allow_realloc && snmp_realloc(buf, buf_len))) { |
671 | 0 | return 0; |
672 | 0 | } |
673 | 0 | *(*buf + *out_len) = '\0'; |
674 | |
|
675 | 0 | return 1; |
676 | 0 | } |
677 | | |
678 | 0 | output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT); |
679 | 0 | if (0 == output_format) { |
680 | 0 | output_format = NETSNMP_STRING_OUTPUT_GUESS; |
681 | 0 | } |
682 | 0 | switch (output_format) { |
683 | 0 | case NETSNMP_STRING_OUTPUT_GUESS: |
684 | 0 | hex = 0; |
685 | 0 | for (cp = var->val.string, x = 0; x < (int) var->val_len; x++, cp++) { |
686 | 0 | if ((!isprint(*cp) || !isascii(*cp)) && !isspace(*cp)) { |
687 | 0 | hex = 1; |
688 | 0 | } |
689 | 0 | } |
690 | 0 | break; |
691 | | |
692 | 0 | case NETSNMP_STRING_OUTPUT_ASCII: |
693 | 0 | hex = 0; |
694 | 0 | break; |
695 | | |
696 | 0 | case NETSNMP_STRING_OUTPUT_HEX: |
697 | 0 | hex = 1; |
698 | 0 | break; |
699 | 0 | } |
700 | | |
701 | 0 | if (var->val_len == 0) { |
702 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"\""); |
703 | 0 | } |
704 | | |
705 | 0 | if (hex) { |
706 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
707 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"")) { |
708 | 0 | return 0; |
709 | 0 | } |
710 | 0 | } else { |
711 | 0 | if (!snmp_cstrcat |
712 | 0 | (buf, buf_len, out_len, allow_realloc, "Hex-STRING: ")) { |
713 | 0 | return 0; |
714 | 0 | } |
715 | 0 | } |
716 | | |
717 | 0 | if (!sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc, |
718 | 0 | var->val.string, var->val_len)) { |
719 | 0 | return 0; |
720 | 0 | } |
721 | | |
722 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
723 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"")) { |
724 | 0 | return 0; |
725 | 0 | } |
726 | 0 | } |
727 | 0 | } else { |
728 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
729 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
730 | 0 | "STRING: ")) { |
731 | 0 | return 0; |
732 | 0 | } |
733 | 0 | } |
734 | 0 | if (!snmp_cstrcat |
735 | 0 | (buf, buf_len, out_len, allow_realloc, "\"")) { |
736 | 0 | return 0; |
737 | 0 | } |
738 | 0 | if (!sprint_realloc_asciistring |
739 | 0 | (buf, buf_len, out_len, allow_realloc, var->val.string, |
740 | 0 | var->val_len)) { |
741 | 0 | return 0; |
742 | 0 | } |
743 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"")) { |
744 | 0 | return 0; |
745 | 0 | } |
746 | 0 | } |
747 | | |
748 | 0 | if (units) { |
749 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
750 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
751 | 0 | } |
752 | 0 | return 1; |
753 | 0 | } |
754 | | |
755 | | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
756 | | |
757 | | /** |
758 | | * Prints a float into a buffer. |
759 | | * |
760 | | * The variable var is encoded as a floating point value. |
761 | | * |
762 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
763 | | * needed size. (Note: *buf may change due to this.) |
764 | | * |
765 | | * @param buf Address of the buffer to print to. |
766 | | * @param buf_len Address to an integer containing the size of buf. |
767 | | * @param out_len Incremented by the number of characters printed. |
768 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
769 | | * needed size. |
770 | | * @param var The variable to encode. |
771 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
772 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
773 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
774 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
775 | | * |
776 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
777 | | * small when not allowed to realloc.) |
778 | | */ |
779 | | int |
780 | | sprint_realloc_float(u_char ** buf, size_t * buf_len, |
781 | | size_t * out_len, int allow_realloc, |
782 | | const netsnmp_variable_list * var, |
783 | | const struct enum_list *enums, |
784 | | const char *hint, const char *units) |
785 | 0 | { |
786 | 0 | char *printf_format_string = NULL; |
787 | |
|
788 | 0 | if (var->type != ASN_OPAQUE_FLOAT) { |
789 | 0 | if (!netsnmp_ds_get_boolean( |
790 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
791 | 0 | static const char str[] = "Wrong Type (should be Float): "; |
792 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
793 | 0 | return 0; |
794 | 0 | } |
795 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
796 | 0 | allow_realloc, var, NULL, NULL, |
797 | 0 | NULL); |
798 | 0 | } |
799 | | |
800 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
801 | 0 | if (!snmp_cstrcat |
802 | 0 | (buf, buf_len, out_len, allow_realloc, "Opaque: Float: ")) { |
803 | 0 | return 0; |
804 | 0 | } |
805 | 0 | } |
806 | | |
807 | | |
808 | | /* |
809 | | * How much space needed for max. length float? 128 is overkill. |
810 | | */ |
811 | | |
812 | 0 | while ((*out_len + 128 + 1) >= *buf_len) { |
813 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
814 | 0 | return 0; |
815 | 0 | } |
816 | 0 | } |
817 | | |
818 | 0 | printf_format_string = make_printf_format_string("%f"); |
819 | 0 | if (!printf_format_string) { |
820 | 0 | return 0; |
821 | 0 | } |
822 | 0 | snprintf((char *)(*buf + *out_len), 128, printf_format_string, *var->val.floatVal); |
823 | 0 | free(printf_format_string); |
824 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
825 | |
|
826 | 0 | if (units) { |
827 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
828 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
829 | 0 | } |
830 | 0 | return 1; |
831 | 0 | } |
832 | | |
833 | | |
834 | | /** |
835 | | * Prints a double into a buffer. |
836 | | * |
837 | | * The variable var is encoded as a double precision floating point value. |
838 | | * |
839 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
840 | | * needed size. (Note: *buf may change due to this.) |
841 | | * |
842 | | * @param buf Address of the buffer to print to. |
843 | | * @param buf_len Address to an integer containing the size of buf. |
844 | | * @param out_len Incremented by the number of characters printed. |
845 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
846 | | * needed size. |
847 | | * @param var The variable to encode. |
848 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
849 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
850 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
851 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
852 | | * |
853 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
854 | | * small when not allowed to realloc.) |
855 | | */ |
856 | | int |
857 | | sprint_realloc_double(u_char ** buf, size_t * buf_len, |
858 | | size_t * out_len, int allow_realloc, |
859 | | const netsnmp_variable_list * var, |
860 | | const struct enum_list *enums, |
861 | | const char *hint, const char *units) |
862 | 0 | { |
863 | 0 | char *printf_format_string = NULL; |
864 | |
|
865 | 0 | if (var->type != ASN_OPAQUE_DOUBLE) { |
866 | 0 | if (!netsnmp_ds_get_boolean( |
867 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
868 | 0 | static const char str[] = "Wrong Type (should be Double): "; |
869 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
870 | 0 | return 0; |
871 | 0 | } |
872 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
873 | 0 | allow_realloc, var, NULL, NULL, |
874 | 0 | NULL); |
875 | 0 | } |
876 | | |
877 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
878 | 0 | if (!snmp_cstrcat |
879 | 0 | (buf, buf_len, out_len, allow_realloc, "Opaque: Float: ")) { |
880 | 0 | return 0; |
881 | 0 | } |
882 | 0 | } |
883 | | |
884 | | /* |
885 | | * How much space needed for max. length double? 128 is overkill. |
886 | | */ |
887 | | |
888 | 0 | while ((*out_len + 128 + 1) >= *buf_len) { |
889 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
890 | 0 | return 0; |
891 | 0 | } |
892 | 0 | } |
893 | | |
894 | 0 | printf_format_string = make_printf_format_string("%f"); |
895 | 0 | if (!printf_format_string) { |
896 | 0 | return 0; |
897 | 0 | } |
898 | 0 | snprintf((char *)(*buf + *out_len), 128, printf_format_string, *var->val.doubleVal); |
899 | 0 | free(printf_format_string); |
900 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
901 | |
|
902 | 0 | if (units) { |
903 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
904 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
905 | 0 | } |
906 | 0 | return 1; |
907 | 0 | } |
908 | | |
909 | | #endif /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */ |
910 | | |
911 | | |
912 | | /** |
913 | | * Prints a counter into a buffer. |
914 | | * |
915 | | * The variable var is encoded as a counter value. |
916 | | * |
917 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
918 | | * needed size. (Note: *buf may change due to this.) |
919 | | * |
920 | | * @param buf Address of the buffer to print to. |
921 | | * @param buf_len Address to an integer containing the size of buf. |
922 | | * @param out_len Incremented by the number of characters printed. |
923 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
924 | | * needed size. |
925 | | * @param var The variable to encode. |
926 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
927 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
928 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
929 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
930 | | * |
931 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
932 | | * small when not allowed to realloc.) |
933 | | */ |
934 | | int |
935 | | sprint_realloc_counter64(u_char ** buf, size_t * buf_len, size_t * out_len, |
936 | | int allow_realloc, |
937 | | const netsnmp_variable_list * var, |
938 | | const struct enum_list *enums, |
939 | | const char *hint, const char *units) |
940 | 0 | { |
941 | 0 | char a64buf[I64CHARSZ + 1]; |
942 | |
|
943 | 0 | if (var->type != ASN_COUNTER64 |
944 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
945 | 0 | && var->type != ASN_OPAQUE_COUNTER64 |
946 | 0 | && var->type != ASN_OPAQUE_I64 && var->type != ASN_OPAQUE_U64 |
947 | 0 | #endif |
948 | 0 | ) { |
949 | 0 | if (!netsnmp_ds_get_boolean( |
950 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
951 | 0 | static const char str[] = "Wrong Type (should be Counter64): "; |
952 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
953 | 0 | return 0; |
954 | 0 | } |
955 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
956 | 0 | allow_realloc, var, NULL, NULL, |
957 | 0 | NULL); |
958 | 0 | } |
959 | | |
960 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
961 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
962 | 0 | if (var->type != ASN_COUNTER64) { |
963 | 0 | if (!snmp_cstrcat |
964 | 0 | (buf, buf_len, out_len, allow_realloc, "Opaque: ")) { |
965 | 0 | return 0; |
966 | 0 | } |
967 | 0 | } |
968 | 0 | #endif |
969 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
970 | 0 | switch (var->type) { |
971 | 0 | case ASN_OPAQUE_U64: |
972 | 0 | if (!snmp_cstrcat |
973 | 0 | (buf, buf_len, out_len, allow_realloc, "UInt64: ")) { |
974 | 0 | return 0; |
975 | 0 | } |
976 | 0 | break; |
977 | 0 | case ASN_OPAQUE_I64: |
978 | 0 | if (!snmp_cstrcat |
979 | 0 | (buf, buf_len, out_len, allow_realloc, "Int64: ")) { |
980 | 0 | return 0; |
981 | 0 | } |
982 | 0 | break; |
983 | 0 | case ASN_COUNTER64: |
984 | 0 | case ASN_OPAQUE_COUNTER64: |
985 | 0 | #endif |
986 | 0 | if (!snmp_cstrcat |
987 | 0 | (buf, buf_len, out_len, allow_realloc, "Counter64: ")) { |
988 | 0 | return 0; |
989 | 0 | } |
990 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
991 | 0 | } |
992 | 0 | #endif |
993 | 0 | } |
994 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
995 | 0 | if (var->type == ASN_OPAQUE_I64) { |
996 | 0 | printI64(a64buf, var->val.counter64); |
997 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, a64buf)) { |
998 | 0 | return 0; |
999 | 0 | } |
1000 | 0 | } else { |
1001 | 0 | #endif |
1002 | 0 | printU64(a64buf, var->val.counter64); |
1003 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, a64buf)) { |
1004 | 0 | return 0; |
1005 | 0 | } |
1006 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
1007 | 0 | } |
1008 | 0 | #endif |
1009 | | |
1010 | 0 | if (units) { |
1011 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1012 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1013 | 0 | } |
1014 | 0 | return 1; |
1015 | 0 | } |
1016 | | |
1017 | | |
1018 | | /** |
1019 | | * Prints an object identifier into a buffer. |
1020 | | * |
1021 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1022 | | * needed size. (Note: *buf may change due to this.) |
1023 | | * |
1024 | | * @param buf Address of the buffer to print to. |
1025 | | * @param buf_len Address to an integer containing the size of buf. |
1026 | | * @param out_len Incremented by the number of characters printed. |
1027 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1028 | | * needed size. |
1029 | | * @param var The variable to encode. |
1030 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1031 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1032 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1033 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1034 | | * |
1035 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1036 | | * small when not allowed to realloc.) |
1037 | | */ |
1038 | | int |
1039 | | sprint_realloc_opaque(u_char ** buf, size_t * buf_len, |
1040 | | size_t * out_len, int allow_realloc, |
1041 | | const netsnmp_variable_list * var, |
1042 | | const struct enum_list *enums, |
1043 | | const char *hint, const char *units) |
1044 | 0 | { |
1045 | 0 | if (var->type != ASN_OPAQUE |
1046 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
1047 | 0 | && var->type != ASN_OPAQUE_COUNTER64 |
1048 | 0 | && var->type != ASN_OPAQUE_U64 |
1049 | 0 | && var->type != ASN_OPAQUE_I64 |
1050 | 0 | && var->type != ASN_OPAQUE_FLOAT && var->type != ASN_OPAQUE_DOUBLE |
1051 | 0 | #endif /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */ |
1052 | 0 | ) { |
1053 | 0 | if (!netsnmp_ds_get_boolean( |
1054 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1055 | 0 | static const char str[] = "Wrong Type (should be Opaque): "; |
1056 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1057 | 0 | return 0; |
1058 | 0 | } |
1059 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1060 | 0 | allow_realloc, var, NULL, NULL, |
1061 | 0 | NULL); |
1062 | 0 | } |
1063 | | |
1064 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
1065 | 0 | switch (var->type) { |
1066 | 0 | case ASN_OPAQUE_COUNTER64: |
1067 | 0 | case ASN_OPAQUE_U64: |
1068 | 0 | case ASN_OPAQUE_I64: |
1069 | 0 | return sprint_realloc_counter64(buf, buf_len, out_len, |
1070 | 0 | allow_realloc, var, enums, hint, |
1071 | 0 | units); |
1072 | 0 | break; |
1073 | | |
1074 | 0 | case ASN_OPAQUE_FLOAT: |
1075 | 0 | return sprint_realloc_float(buf, buf_len, out_len, allow_realloc, |
1076 | 0 | var, enums, hint, units); |
1077 | 0 | break; |
1078 | | |
1079 | 0 | case ASN_OPAQUE_DOUBLE: |
1080 | 0 | return sprint_realloc_double(buf, buf_len, out_len, allow_realloc, |
1081 | 0 | var, enums, hint, units); |
1082 | 0 | break; |
1083 | | |
1084 | 0 | case ASN_OPAQUE: |
1085 | 0 | #endif |
1086 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1087 | 0 | static const char str[] = "OPAQUE: "; |
1088 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1089 | 0 | return 0; |
1090 | 0 | } |
1091 | 0 | } |
1092 | 0 | if (!sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc, |
1093 | 0 | var->val.string, var->val_len)) { |
1094 | 0 | return 0; |
1095 | 0 | } |
1096 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
1097 | 0 | } |
1098 | 0 | #endif |
1099 | 0 | if (units) { |
1100 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1101 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1102 | 0 | } |
1103 | 0 | return 1; |
1104 | 0 | } |
1105 | | |
1106 | | |
1107 | | /** |
1108 | | * Prints an object identifier into a buffer. |
1109 | | * |
1110 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1111 | | * needed size. (Note: *buf may change due to this.) |
1112 | | * |
1113 | | * @param buf Address of the buffer to print to. |
1114 | | * @param buf_len Address to an integer containing the size of buf. |
1115 | | * @param out_len Incremented by the number of characters printed. |
1116 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1117 | | * needed size. |
1118 | | * @param var The variable to encode. |
1119 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1120 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1121 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1122 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1123 | | * |
1124 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1125 | | * small when not allowed to realloc.) |
1126 | | */ |
1127 | | int |
1128 | | sprint_realloc_object_identifier(u_char ** buf, size_t * buf_len, |
1129 | | size_t * out_len, int allow_realloc, |
1130 | | const netsnmp_variable_list * var, |
1131 | | const struct enum_list *enums, |
1132 | | const char *hint, const char *units) |
1133 | 0 | { |
1134 | 0 | int buf_overflow = 0; |
1135 | |
|
1136 | 0 | if (var->type != ASN_OBJECT_ID) { |
1137 | 0 | if (!netsnmp_ds_get_boolean( |
1138 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1139 | 0 | static const char str[] = "Wrong Type (should be OBJECT IDENTIFIER): "; |
1140 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1141 | 0 | return 0; |
1142 | 0 | } |
1143 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1144 | 0 | allow_realloc, var, NULL, NULL, |
1145 | 0 | NULL); |
1146 | 0 | } |
1147 | | |
1148 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1149 | 0 | static const char str[] = "OID: "; |
1150 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1151 | 0 | return 0; |
1152 | 0 | } |
1153 | 0 | } |
1154 | | |
1155 | 0 | netsnmp_sprint_realloc_objid_tree(buf, buf_len, out_len, allow_realloc, |
1156 | 0 | &buf_overflow, |
1157 | 0 | (oid *) (var->val.objid), |
1158 | 0 | var->val_len / sizeof(oid)); |
1159 | |
|
1160 | 0 | if (buf_overflow) { |
1161 | 0 | return 0; |
1162 | 0 | } |
1163 | | |
1164 | 0 | if (units) { |
1165 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1166 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1167 | 0 | } |
1168 | 0 | return 1; |
1169 | 0 | } |
1170 | | |
1171 | | |
1172 | | |
1173 | | /** |
1174 | | * Prints a timetick variable into a buffer. |
1175 | | * |
1176 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1177 | | * needed size. (Note: *buf may change due to this.) |
1178 | | * |
1179 | | * @param buf Address of the buffer to print to. |
1180 | | * @param buf_len Address to an integer containing the size of buf. |
1181 | | * @param out_len Incremented by the number of characters printed. |
1182 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1183 | | * needed size. |
1184 | | * @param var The variable to encode. |
1185 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1186 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1187 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1188 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1189 | | * |
1190 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1191 | | * small when not allowed to realloc.) |
1192 | | */ |
1193 | | int |
1194 | | sprint_realloc_timeticks(u_char ** buf, size_t * buf_len, size_t * out_len, |
1195 | | int allow_realloc, |
1196 | | const netsnmp_variable_list * var, |
1197 | | const struct enum_list *enums, |
1198 | | const char *hint, const char *units) |
1199 | 0 | { |
1200 | 0 | char timebuf[40]; |
1201 | |
|
1202 | 0 | if (var->type != ASN_TIMETICKS) { |
1203 | 0 | if (!netsnmp_ds_get_boolean( |
1204 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1205 | 0 | static const char str[] = "Wrong Type (should be Timeticks): "; |
1206 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1207 | 0 | return 0; |
1208 | 0 | } |
1209 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1210 | 0 | allow_realloc, var, NULL, NULL, |
1211 | 0 | NULL); |
1212 | 0 | } |
1213 | | |
1214 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS)) { |
1215 | 0 | char str[32]; |
1216 | 0 | snprintf(str, sizeof(str), "%lu", *(u_long *) var->val.integer); |
1217 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1218 | 0 | return 0; |
1219 | 0 | } |
1220 | 0 | return 1; |
1221 | 0 | } |
1222 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1223 | 0 | char str[32]; |
1224 | 0 | snprintf(str, sizeof(str), "Timeticks: (%lu) ", |
1225 | 0 | *(u_long *) var->val.integer); |
1226 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1227 | 0 | return 0; |
1228 | 0 | } |
1229 | 0 | } |
1230 | 0 | uptimeString(*(u_long *) (var->val.integer), timebuf, sizeof(timebuf)); |
1231 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, timebuf)) { |
1232 | 0 | return 0; |
1233 | 0 | } |
1234 | 0 | if (units) { |
1235 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1236 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1237 | 0 | } |
1238 | 0 | return 1; |
1239 | 0 | } |
1240 | | |
1241 | | |
1242 | | /** |
1243 | | * Prints an integer according to the hint into a buffer. |
1244 | | * |
1245 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1246 | | * needed size. (Note: *buf may change due to this.) |
1247 | | * |
1248 | | * @param buf Address of the buffer to print to. |
1249 | | * @param buf_len Address to an integer containing the size of buf. |
1250 | | * @param out_len Incremented by the number of characters printed. |
1251 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1252 | | * needed size. |
1253 | | * @param val The variable to encode. |
1254 | | * @param decimaltype 'd' or 'u' depending on integer type |
1255 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1256 | | * See RFC 1903 Section 3.1 for details. may _NOT_ be NULL. |
1257 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1258 | | * |
1259 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1260 | | * small when not allowed to realloc.) |
1261 | | */ |
1262 | | int |
1263 | | sprint_realloc_hinted_integer(u_char ** buf, size_t * buf_len, |
1264 | | size_t * out_len, int allow_realloc, |
1265 | | long val, const char decimaltype, |
1266 | | const char *hint, const char *units) |
1267 | 0 | { |
1268 | 0 | char fmt[10] = "%l@", tmp[256]; |
1269 | 0 | int shift = 0, len, negative = 0; |
1270 | |
|
1271 | 0 | if (!strchr("bdoux", decimaltype)) { |
1272 | 0 | snmp_log(LOG_ERR, "Invalid decimal type '%c'\n", decimaltype); |
1273 | 0 | return 0; |
1274 | 0 | } |
1275 | | |
1276 | 0 | switch (hint[0]) { |
1277 | 0 | case 'd': |
1278 | | /* |
1279 | | * We might *actually* want a 'u' here. |
1280 | | */ |
1281 | 0 | if (hint[1] == '-') { |
1282 | 0 | shift = atoi(hint + 2); |
1283 | 0 | if (shift < 0) |
1284 | 0 | shift = 0; |
1285 | 0 | } |
1286 | 0 | fmt[2] = decimaltype; |
1287 | 0 | if (val < 0 && decimaltype == 'd') { |
1288 | 0 | negative = 1; |
1289 | 0 | fmt[2] = 'u'; |
1290 | 0 | snprintf(tmp, sizeof(tmp), fmt, 0UL - (unsigned long)val); |
1291 | 0 | } else { |
1292 | 0 | snprintf(tmp, sizeof(tmp), fmt, val); |
1293 | 0 | } |
1294 | 0 | break; |
1295 | 0 | case 'o': |
1296 | 0 | case 'x': |
1297 | 0 | fmt[2] = hint[0]; |
1298 | 0 | snprintf(tmp, sizeof(tmp), fmt, val); |
1299 | 0 | break; |
1300 | 0 | case 'b': { |
1301 | 0 | unsigned long int bit = 0x80000000LU; |
1302 | 0 | char *bp = tmp; |
1303 | 0 | while (bit) { |
1304 | 0 | *bp++ = val & bit ? '1' : '0'; |
1305 | 0 | bit >>= 1; |
1306 | 0 | } |
1307 | 0 | *bp = 0; |
1308 | 0 | break; |
1309 | 0 | } |
1310 | 0 | default: |
1311 | 0 | return 0; |
1312 | 0 | } |
1313 | | |
1314 | 0 | if (shift != 0) { |
1315 | 0 | len = strlen(tmp); |
1316 | 0 | if (shift <= len) { |
1317 | 0 | if (len + 2 > (int)sizeof(tmp)) |
1318 | 0 | return 0; |
1319 | 0 | tmp[len + 1] = 0; |
1320 | 0 | while (shift--) { |
1321 | 0 | tmp[len] = tmp[len - 1]; |
1322 | 0 | len--; |
1323 | 0 | } |
1324 | 0 | tmp[len] = '.'; |
1325 | 0 | } else if (shift < (int)sizeof(tmp) - 1) { |
1326 | 0 | tmp[shift + 1] = 0; |
1327 | 0 | while (shift) { |
1328 | 0 | if (len-- > 0) { |
1329 | 0 | tmp[shift] = tmp[len]; |
1330 | 0 | } else { |
1331 | 0 | tmp[shift] = '0'; |
1332 | 0 | } |
1333 | 0 | shift--; |
1334 | 0 | } |
1335 | 0 | tmp[0] = '.'; |
1336 | 0 | } |
1337 | 0 | } |
1338 | 0 | if (negative) { |
1339 | 0 | len = strlen(tmp)+1; |
1340 | 0 | if (len + 1 > (int)sizeof(tmp)) |
1341 | 0 | return 0; |
1342 | 0 | while (len) { |
1343 | 0 | tmp[len] = tmp[len-1]; |
1344 | 0 | len--; |
1345 | 0 | } |
1346 | 0 | tmp[0] = '-'; |
1347 | 0 | } |
1348 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmp); |
1349 | 0 | } |
1350 | | |
1351 | | |
1352 | | /** |
1353 | | * Prints an integer into a buffer. |
1354 | | * |
1355 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1356 | | * needed size. (Note: *buf may change due to this.) |
1357 | | * |
1358 | | * @param buf Address of the buffer to print to. |
1359 | | * @param buf_len Address to an integer containing the size of buf. |
1360 | | * @param out_len Incremented by the number of characters printed. |
1361 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1362 | | * needed size. |
1363 | | * @param var The variable to encode. |
1364 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1365 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1366 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1367 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1368 | | * |
1369 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1370 | | * small when not allowed to realloc.) |
1371 | | */ |
1372 | | int |
1373 | | sprint_realloc_integer(u_char ** buf, size_t * buf_len, size_t * out_len, |
1374 | | int allow_realloc, |
1375 | | const netsnmp_variable_list * var, |
1376 | | const struct enum_list *enums, |
1377 | | const char *hint, const char *units) |
1378 | 0 | { |
1379 | 0 | char *enum_string = NULL; |
1380 | |
|
1381 | 0 | if (var->type != ASN_INTEGER) { |
1382 | 0 | if (!netsnmp_ds_get_boolean( |
1383 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1384 | 0 | static const char str[] = "Wrong Type (should be INTEGER): "; |
1385 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1386 | 0 | return 0; |
1387 | 0 | } |
1388 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1389 | 0 | allow_realloc, var, NULL, NULL, |
1390 | 0 | NULL); |
1391 | 0 | } |
1392 | | |
1393 | 0 | for (; enums; enums = enums->next) { |
1394 | 0 | if (enums->value == *var->val.integer) { |
1395 | 0 | enum_string = enums->label; |
1396 | 0 | break; |
1397 | 0 | } |
1398 | 0 | } |
1399 | |
|
1400 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1401 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "INTEGER: ")) { |
1402 | 0 | return 0; |
1403 | 0 | } |
1404 | 0 | } |
1405 | | |
1406 | 0 | if (enum_string == NULL || |
1407 | 0 | netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM)) { |
1408 | 0 | if (hint) { |
1409 | 0 | if (!(sprint_realloc_hinted_integer(buf, buf_len, out_len, |
1410 | 0 | allow_realloc, |
1411 | 0 | *var->val.integer, 'd', |
1412 | 0 | hint, units))) { |
1413 | 0 | return 0; |
1414 | 0 | } |
1415 | 0 | } else { |
1416 | 0 | char str[32]; |
1417 | 0 | snprintf(str, sizeof(str), "%ld", *var->val.integer); |
1418 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1419 | 0 | return 0; |
1420 | 0 | } |
1421 | 0 | } |
1422 | 0 | } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1423 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) { |
1424 | 0 | return 0; |
1425 | 0 | } |
1426 | 0 | } else { |
1427 | 0 | char str[32]; |
1428 | 0 | snprintf(str, sizeof(str), "(%ld)", *var->val.integer); |
1429 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) { |
1430 | 0 | return 0; |
1431 | 0 | } |
1432 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1433 | 0 | return 0; |
1434 | 0 | } |
1435 | 0 | } |
1436 | | |
1437 | 0 | if (units) { |
1438 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1439 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1440 | 0 | } |
1441 | 0 | return 1; |
1442 | 0 | } |
1443 | | |
1444 | | |
1445 | | /** |
1446 | | * Prints an unsigned integer into a buffer. |
1447 | | * |
1448 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1449 | | * needed size. (Note: *buf may change due to this.) |
1450 | | * |
1451 | | * @param buf Address of the buffer to print to. |
1452 | | * @param buf_len Address to an integer containing the size of buf. |
1453 | | * @param out_len Incremented by the number of characters printed. |
1454 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1455 | | * needed size. |
1456 | | * @param var The variable to encode. |
1457 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1458 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1459 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1460 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1461 | | * |
1462 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1463 | | * small when not allowed to realloc.) |
1464 | | */ |
1465 | | int |
1466 | | sprint_realloc_uinteger(u_char ** buf, size_t * buf_len, size_t * out_len, |
1467 | | int allow_realloc, |
1468 | | const netsnmp_variable_list * var, |
1469 | | const struct enum_list *enums, |
1470 | | const char *hint, const char *units) |
1471 | 0 | { |
1472 | 0 | char *enum_string = NULL; |
1473 | |
|
1474 | 0 | if (var->type != ASN_UINTEGER) { |
1475 | 0 | if (!netsnmp_ds_get_boolean( |
1476 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1477 | 0 | static const char str[] = "Wrong Type (should be UInteger32): "; |
1478 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1479 | 0 | return 0; |
1480 | 0 | } |
1481 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1482 | 0 | allow_realloc, var, NULL, NULL, |
1483 | 0 | NULL); |
1484 | 0 | } |
1485 | | |
1486 | 0 | for (; enums; enums = enums->next) { |
1487 | 0 | if (enums->value == *var->val.integer) { |
1488 | 0 | enum_string = enums->label; |
1489 | 0 | break; |
1490 | 0 | } |
1491 | 0 | } |
1492 | |
|
1493 | 0 | if (enum_string == NULL || |
1494 | 0 | netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM)) { |
1495 | 0 | if (hint) { |
1496 | 0 | if (!(sprint_realloc_hinted_integer(buf, buf_len, out_len, |
1497 | 0 | allow_realloc, |
1498 | 0 | *var->val.integer, 'u', |
1499 | 0 | hint, units))) { |
1500 | 0 | return 0; |
1501 | 0 | } |
1502 | 0 | } else { |
1503 | 0 | char str[32]; |
1504 | 0 | snprintf(str, sizeof(str), "%lu", *var->val.integer); |
1505 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1506 | 0 | return 0; |
1507 | 0 | } |
1508 | 0 | } |
1509 | 0 | } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1510 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) { |
1511 | 0 | return 0; |
1512 | 0 | } |
1513 | 0 | } else { |
1514 | 0 | char str[32]; |
1515 | 0 | snprintf(str, sizeof(str), "(%lu)", *var->val.integer); |
1516 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) { |
1517 | 0 | return 0; |
1518 | 0 | } |
1519 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1520 | 0 | return 0; |
1521 | 0 | } |
1522 | 0 | } |
1523 | | |
1524 | 0 | if (units) { |
1525 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1526 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1527 | 0 | } |
1528 | 0 | return 1; |
1529 | 0 | } |
1530 | | |
1531 | | |
1532 | | /** |
1533 | | * Prints a gauge value into a buffer. |
1534 | | * |
1535 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1536 | | * needed size. (Note: *buf may change due to this.) |
1537 | | * |
1538 | | * @param buf Address of the buffer to print to. |
1539 | | * @param buf_len Address to an integer containing the size of buf. |
1540 | | * @param out_len Incremented by the number of characters printed. |
1541 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1542 | | * needed size. |
1543 | | * @param var The variable to encode. |
1544 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1545 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1546 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1547 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1548 | | * |
1549 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1550 | | * small when not allowed to realloc.) |
1551 | | */ |
1552 | | int |
1553 | | sprint_realloc_gauge(u_char ** buf, size_t * buf_len, size_t * out_len, |
1554 | | int allow_realloc, |
1555 | | const netsnmp_variable_list * var, |
1556 | | const struct enum_list *enums, |
1557 | | const char *hint, const char *units) |
1558 | 0 | { |
1559 | 0 | char tmp[32]; |
1560 | |
|
1561 | 0 | if (var->type != ASN_GAUGE) { |
1562 | 0 | if (!netsnmp_ds_get_boolean( |
1563 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1564 | 0 | static const char str[] = "Wrong Type (should be Gauge32 or Unsigned32): "; |
1565 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1566 | 0 | return 0; |
1567 | 0 | } |
1568 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1569 | 0 | allow_realloc, var, NULL, NULL, |
1570 | 0 | NULL); |
1571 | 0 | } |
1572 | | |
1573 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1574 | 0 | static const char str[] = "Gauge32: "; |
1575 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1576 | 0 | return 0; |
1577 | 0 | } |
1578 | 0 | } |
1579 | 0 | if (hint) { |
1580 | 0 | if (!sprint_realloc_hinted_integer(buf, buf_len, out_len, |
1581 | 0 | allow_realloc, |
1582 | 0 | *var->val.integer, 'u', hint, |
1583 | 0 | units)) { |
1584 | 0 | return 0; |
1585 | 0 | } |
1586 | 0 | } else { |
1587 | 0 | snprintf(tmp, sizeof(tmp), "%u", (unsigned int)(*var->val.integer & 0xffffffff)); |
1588 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmp)) { |
1589 | 0 | return 0; |
1590 | 0 | } |
1591 | 0 | } |
1592 | 0 | if (units) { |
1593 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1594 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1595 | 0 | } |
1596 | 0 | return 1; |
1597 | 0 | } |
1598 | | |
1599 | | |
1600 | | /** |
1601 | | * Prints a counter value into a buffer. |
1602 | | * |
1603 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1604 | | * needed size. (Note: *buf may change due to this.) |
1605 | | * |
1606 | | * @param buf Address of the buffer to print to. |
1607 | | * @param buf_len Address to an integer containing the size of buf. |
1608 | | * @param out_len Incremented by the number of characters printed. |
1609 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1610 | | * needed size. |
1611 | | * @param var The variable to encode. |
1612 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1613 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1614 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1615 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1616 | | * |
1617 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1618 | | * small when not allowed to realloc.) |
1619 | | */ |
1620 | | int |
1621 | | sprint_realloc_counter(u_char ** buf, size_t * buf_len, size_t * out_len, |
1622 | | int allow_realloc, |
1623 | | const netsnmp_variable_list * var, |
1624 | | const struct enum_list *enums, |
1625 | | const char *hint, const char *units) |
1626 | 0 | { |
1627 | 0 | char tmp[32]; |
1628 | |
|
1629 | 0 | if (var->type != ASN_COUNTER) { |
1630 | 0 | if (!netsnmp_ds_get_boolean( |
1631 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1632 | 0 | static const char str[] = "Wrong Type (should be Counter32): "; |
1633 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1634 | 0 | return 0; |
1635 | 0 | } |
1636 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1637 | 0 | allow_realloc, var, NULL, NULL, |
1638 | 0 | NULL); |
1639 | 0 | } |
1640 | | |
1641 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1642 | 0 | static const char str[] = "Counter32: "; |
1643 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1644 | 0 | return 0; |
1645 | 0 | } |
1646 | 0 | } |
1647 | 0 | snprintf(tmp, sizeof(tmp), "%u", (unsigned int)(*var->val.integer & 0xffffffff)); |
1648 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmp)) { |
1649 | 0 | return 0; |
1650 | 0 | } |
1651 | 0 | if (units) { |
1652 | 0 | return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") && |
1653 | 0 | snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units)); |
1654 | 0 | } |
1655 | 0 | return 1; |
1656 | 0 | } |
1657 | | |
1658 | | |
1659 | | /** |
1660 | | * Prints a network address into a buffer. |
1661 | | * |
1662 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1663 | | * needed size. (Note: *buf may change due to this.) |
1664 | | * |
1665 | | * @param buf Address of the buffer to print to. |
1666 | | * @param buf_len Address to an integer containing the size of buf. |
1667 | | * @param out_len Incremented by the number of characters printed. |
1668 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1669 | | * needed size. |
1670 | | * @param var The variable to encode. |
1671 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1672 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1673 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1674 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1675 | | * |
1676 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1677 | | * small when not allowed to realloc.) |
1678 | | */ |
1679 | | int |
1680 | | sprint_realloc_networkaddress(u_char ** buf, size_t * buf_len, |
1681 | | size_t * out_len, int allow_realloc, |
1682 | | const netsnmp_variable_list * var, |
1683 | | const struct enum_list *enums, const char *hint, |
1684 | | const char *units) |
1685 | 0 | { |
1686 | 0 | size_t i; |
1687 | |
|
1688 | 0 | if (var->type != ASN_IPADDRESS) { |
1689 | 0 | if (!netsnmp_ds_get_boolean( |
1690 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1691 | 0 | static const char str[] = "Wrong Type (should be NetworkAddress): "; |
1692 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1693 | 0 | return 0; |
1694 | 0 | } |
1695 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1696 | 0 | allow_realloc, var, NULL, NULL, |
1697 | 0 | NULL); |
1698 | 0 | } |
1699 | | |
1700 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1701 | 0 | static const char str[] = "Network Address: "; |
1702 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1703 | 0 | return 0; |
1704 | 0 | } |
1705 | 0 | } |
1706 | | |
1707 | 0 | while ((*out_len + (var->val_len * 3) + 2) >= *buf_len) { |
1708 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
1709 | 0 | return 0; |
1710 | 0 | } |
1711 | 0 | } |
1712 | | |
1713 | 0 | for (i = 0; i < var->val_len; i++) { |
1714 | 0 | snprintf((char *) (*buf + *out_len), *buf_len - *out_len, "%02X", var->val.string[i]); |
1715 | 0 | *out_len += 2; |
1716 | 0 | if (i < var->val_len - 1) { |
1717 | 0 | *(*buf + *out_len) = ':'; |
1718 | 0 | (*out_len)++; |
1719 | 0 | } |
1720 | 0 | } |
1721 | 0 | return 1; |
1722 | 0 | } |
1723 | | |
1724 | | |
1725 | | /** |
1726 | | * Prints an ip-address into a buffer. |
1727 | | * |
1728 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1729 | | * needed size. (Note: *buf may change due to this.) |
1730 | | * |
1731 | | * @param buf Address of the buffer to print to. |
1732 | | * @param buf_len Address to an integer containing the size of buf. |
1733 | | * @param out_len Incremented by the number of characters printed. |
1734 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1735 | | * needed size. |
1736 | | * @param var The variable to encode. |
1737 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1738 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1739 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1740 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1741 | | * |
1742 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1743 | | * small when not allowed to realloc.) |
1744 | | */ |
1745 | | int |
1746 | | sprint_realloc_ipaddress(u_char ** buf, size_t * buf_len, size_t * out_len, |
1747 | | int allow_realloc, |
1748 | | const netsnmp_variable_list * var, |
1749 | | const struct enum_list *enums, |
1750 | | const char *hint, const char *units) |
1751 | 0 | { |
1752 | 0 | u_char *ip = var->val.string; |
1753 | |
|
1754 | 0 | if (var->type != ASN_IPADDRESS) { |
1755 | 0 | if (!netsnmp_ds_get_boolean( |
1756 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1757 | 0 | static const char str[] = "Wrong Type (should be IpAddress): "; |
1758 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1759 | 0 | return 0; |
1760 | 0 | } |
1761 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1762 | 0 | allow_realloc, var, NULL, NULL, |
1763 | 0 | NULL); |
1764 | 0 | } |
1765 | | |
1766 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1767 | 0 | static const char str[] = "IpAddress: "; |
1768 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1769 | 0 | return 0; |
1770 | 0 | } |
1771 | 0 | } |
1772 | 0 | while ((*out_len + 17) >= *buf_len) { |
1773 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
1774 | 0 | return 0; |
1775 | 0 | } |
1776 | 0 | } |
1777 | 0 | if (ip) |
1778 | 0 | snprintf((char *) (*buf + *out_len), *buf_len - *out_len, "%d.%d.%d.%d", |
1779 | 0 | ip[0], ip[1], ip[2], ip[3]); |
1780 | 0 | *out_len += strlen((char *) (*buf + *out_len)); |
1781 | 0 | return 1; |
1782 | 0 | } |
1783 | | |
1784 | | |
1785 | | /** |
1786 | | * Prints a null value into a buffer. |
1787 | | * |
1788 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1789 | | * needed size. (Note: *buf may change due to this.) |
1790 | | * |
1791 | | * @param buf Address of the buffer to print to. |
1792 | | * @param buf_len Address to an integer containing the size of buf. |
1793 | | * @param out_len Incremented by the number of characters printed. |
1794 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1795 | | * needed size. |
1796 | | * @param var The variable to encode. |
1797 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1798 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1799 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1800 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1801 | | * |
1802 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1803 | | * small when not allowed to realloc.) |
1804 | | */ |
1805 | | int |
1806 | | sprint_realloc_null(u_char ** buf, size_t * buf_len, size_t * out_len, |
1807 | | int allow_realloc, |
1808 | | const netsnmp_variable_list * var, |
1809 | | const struct enum_list *enums, |
1810 | | const char *hint, const char *units) |
1811 | 0 | { |
1812 | 0 | static const char str[] = "NULL"; |
1813 | |
|
1814 | 0 | if (var->type != ASN_NULL) { |
1815 | 0 | if (!netsnmp_ds_get_boolean( |
1816 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1817 | 0 | static const char str[] = "Wrong Type (should be NULL): "; |
1818 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1819 | 0 | return 0; |
1820 | 0 | } |
1821 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1822 | 0 | allow_realloc, var, NULL, NULL, |
1823 | 0 | NULL); |
1824 | 0 | } |
1825 | | |
1826 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str); |
1827 | 0 | } |
1828 | | |
1829 | | |
1830 | | /** |
1831 | | * Prints a bit string into a buffer. |
1832 | | * |
1833 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1834 | | * needed size. (Note: *buf may change due to this.) |
1835 | | * |
1836 | | * @param buf Address of the buffer to print to. |
1837 | | * @param buf_len Address to an integer containing the size of buf. |
1838 | | * @param out_len Incremented by the number of characters printed. |
1839 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1840 | | * needed size. |
1841 | | * @param var The variable to encode. |
1842 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1843 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1844 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1845 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1846 | | * |
1847 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1848 | | * small when not allowed to realloc.) |
1849 | | */ |
1850 | | int |
1851 | | sprint_realloc_bitstring(u_char ** buf, size_t * buf_len, size_t * out_len, |
1852 | | int allow_realloc, |
1853 | | const netsnmp_variable_list * var, |
1854 | | const struct enum_list *enums, |
1855 | | const char *hint, const char *units) |
1856 | 0 | { |
1857 | 0 | int len, bit; |
1858 | 0 | u_char *cp; |
1859 | 0 | char *enum_string; |
1860 | |
|
1861 | 0 | if (var->type != ASN_BIT_STR && var->type != ASN_OCTET_STR) { |
1862 | 0 | if (!netsnmp_ds_get_boolean( |
1863 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1864 | 0 | static const char str[] = "Wrong Type (should be BITS): "; |
1865 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1866 | 0 | return 0; |
1867 | 0 | } |
1868 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1869 | 0 | allow_realloc, var, NULL, NULL, |
1870 | 0 | NULL); |
1871 | 0 | } |
1872 | | |
1873 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1874 | 0 | static const char str[] = "\""; |
1875 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1876 | 0 | return 0; |
1877 | 0 | } |
1878 | 0 | } else { |
1879 | 0 | static const char str[] = "BITS: "; |
1880 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1881 | 0 | return 0; |
1882 | 0 | } |
1883 | 0 | } |
1884 | 0 | if (!sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc, |
1885 | 0 | var->val.bitstring, var->val_len)) { |
1886 | 0 | return 0; |
1887 | 0 | } |
1888 | | |
1889 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1890 | 0 | static const char str[] = "\""; |
1891 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1892 | 0 | return 0; |
1893 | 0 | } |
1894 | 0 | } else { |
1895 | 0 | cp = var->val.bitstring; |
1896 | 0 | for (len = 0; len < (int) var->val_len; len++) { |
1897 | 0 | for (bit = 0; bit < 8; bit++) { |
1898 | 0 | if (*cp & (0x80 >> bit)) { |
1899 | 0 | enum_string = NULL; |
1900 | 0 | for (; enums; enums = enums->next) { |
1901 | 0 | if (enums->value == (len * 8) + bit) { |
1902 | 0 | enum_string = enums->label; |
1903 | 0 | break; |
1904 | 0 | } |
1905 | 0 | } |
1906 | 0 | if (enum_string == NULL || |
1907 | 0 | netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
1908 | 0 | NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM)) { |
1909 | 0 | char str[32]; |
1910 | 0 | snprintf(str, sizeof(str), "%d ", (len * 8) + bit); |
1911 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
1912 | 0 | str)) { |
1913 | 0 | return 0; |
1914 | 0 | } |
1915 | 0 | } else { |
1916 | 0 | char str[32]; |
1917 | 0 | snprintf(str, sizeof(str), "(%d) ", (len * 8) + bit); |
1918 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
1919 | 0 | enum_string)) { |
1920 | 0 | return 0; |
1921 | 0 | } |
1922 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
1923 | 0 | str)) { |
1924 | 0 | return 0; |
1925 | 0 | } |
1926 | 0 | } |
1927 | 0 | } |
1928 | 0 | } |
1929 | 0 | cp++; |
1930 | 0 | } |
1931 | 0 | } |
1932 | 0 | return 1; |
1933 | 0 | } |
1934 | | |
1935 | | int |
1936 | | sprint_realloc_nsapaddress(u_char ** buf, size_t * buf_len, |
1937 | | size_t * out_len, int allow_realloc, |
1938 | | const netsnmp_variable_list * var, |
1939 | | const struct enum_list *enums, const char *hint, |
1940 | | const char *units) |
1941 | 0 | { |
1942 | 0 | if (var->type != ASN_NSAP) { |
1943 | 0 | if (!netsnmp_ds_get_boolean( |
1944 | 0 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
1945 | 0 | static const char str[] = "Wrong Type (should be NsapAddress): "; |
1946 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
1947 | 0 | return 0; |
1948 | 0 | } |
1949 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
1950 | 0 | allow_realloc, var, NULL, NULL, |
1951 | 0 | NULL); |
1952 | 0 | } |
1953 | | |
1954 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
1955 | 0 | static const char str[] = "NsapAddress: "; |
1956 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) { |
1957 | 0 | return 0; |
1958 | 0 | } |
1959 | 0 | } |
1960 | | |
1961 | 0 | return sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc, |
1962 | 0 | var->val.string, var->val_len); |
1963 | 0 | } |
1964 | | |
1965 | | |
1966 | | /** |
1967 | | * Fallback routine for a bad type, prints "Variable has bad type" into a buffer. |
1968 | | * |
1969 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
1970 | | * needed size. (Note: *buf may change due to this.) |
1971 | | * |
1972 | | * @param buf Address of the buffer to print to. |
1973 | | * @param buf_len Address to an integer containing the size of buf. |
1974 | | * @param out_len Incremented by the number of characters printed. |
1975 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
1976 | | * needed size. |
1977 | | * @param var The variable to encode. |
1978 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
1979 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
1980 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
1981 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
1982 | | * |
1983 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
1984 | | * small when not allowed to realloc.) |
1985 | | */ |
1986 | | int |
1987 | | sprint_realloc_badtype(u_char ** buf, size_t * buf_len, size_t * out_len, |
1988 | | int allow_realloc, |
1989 | | const netsnmp_variable_list * var, |
1990 | | const struct enum_list *enums, |
1991 | | const char *hint, const char *units) |
1992 | 0 | { |
1993 | 0 | static const char str[] = "Variable has bad type"; |
1994 | |
|
1995 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str); |
1996 | 0 | } |
1997 | | |
1998 | | |
1999 | | |
2000 | | /** |
2001 | | * Universal print routine, prints a variable into a buffer according to the variable |
2002 | | * type. |
2003 | | * |
2004 | | * If allow_realloc is true the buffer will be (re)allocated to fit in the |
2005 | | * needed size. (Note: *buf may change due to this.) |
2006 | | * |
2007 | | * @param buf Address of the buffer to print to. |
2008 | | * @param buf_len Address to an integer containing the size of buf. |
2009 | | * @param out_len Incremented by the number of characters printed. |
2010 | | * @param allow_realloc if not zero reallocate the buffer to fit the |
2011 | | * needed size. |
2012 | | * @param var The variable to encode. |
2013 | | * @param enums The enumeration ff this variable is enumerated. may be NULL. |
2014 | | * @param hint Contents of the DISPLAY-HINT clause of the MIB. |
2015 | | * See RFC 1903 Section 3.1 for details. may be NULL. |
2016 | | * @param units Contents of the UNITS clause of the MIB. may be NULL. |
2017 | | * |
2018 | | * @return 1 on success, or 0 on failure (out of memory, or buffer to |
2019 | | * small when not allowed to realloc.) |
2020 | | */ |
2021 | | int |
2022 | | sprint_realloc_by_type(u_char ** buf, size_t * buf_len, size_t * out_len, |
2023 | | int allow_realloc, |
2024 | | const netsnmp_variable_list * var, |
2025 | | const struct enum_list *enums, |
2026 | | const char *hint, const char *units) |
2027 | 0 | { |
2028 | 0 | DEBUGMSGTL(("output", "sprint_by_type, type %d\n", var->type)); |
2029 | |
|
2030 | 0 | switch (var->type) { |
2031 | 0 | case ASN_INTEGER: |
2032 | 0 | return sprint_realloc_integer(buf, buf_len, out_len, allow_realloc, |
2033 | 0 | var, enums, hint, units); |
2034 | 0 | case ASN_OCTET_STR: |
2035 | 0 | return sprint_realloc_octet_string(buf, buf_len, out_len, |
2036 | 0 | allow_realloc, var, enums, hint, |
2037 | 0 | units); |
2038 | 0 | case ASN_BIT_STR: |
2039 | 0 | return sprint_realloc_bitstring(buf, buf_len, out_len, |
2040 | 0 | allow_realloc, var, enums, hint, |
2041 | 0 | units); |
2042 | 0 | case ASN_OPAQUE: |
2043 | 0 | return sprint_realloc_opaque(buf, buf_len, out_len, allow_realloc, |
2044 | 0 | var, enums, hint, units); |
2045 | 0 | case ASN_OBJECT_ID: |
2046 | 0 | return sprint_realloc_object_identifier(buf, buf_len, out_len, |
2047 | 0 | allow_realloc, var, enums, |
2048 | 0 | hint, units); |
2049 | 0 | case ASN_TIMETICKS: |
2050 | 0 | return sprint_realloc_timeticks(buf, buf_len, out_len, |
2051 | 0 | allow_realloc, var, enums, hint, |
2052 | 0 | units); |
2053 | 0 | case ASN_GAUGE: |
2054 | 0 | return sprint_realloc_gauge(buf, buf_len, out_len, allow_realloc, |
2055 | 0 | var, enums, hint, units); |
2056 | 0 | case ASN_COUNTER: |
2057 | 0 | return sprint_realloc_counter(buf, buf_len, out_len, allow_realloc, |
2058 | 0 | var, enums, hint, units); |
2059 | 0 | case ASN_IPADDRESS: |
2060 | 0 | return sprint_realloc_ipaddress(buf, buf_len, out_len, |
2061 | 0 | allow_realloc, var, enums, hint, |
2062 | 0 | units); |
2063 | 0 | case ASN_NULL: |
2064 | 0 | return sprint_realloc_null(buf, buf_len, out_len, allow_realloc, |
2065 | 0 | var, enums, hint, units); |
2066 | 0 | case ASN_UINTEGER: |
2067 | 0 | return sprint_realloc_uinteger(buf, buf_len, out_len, |
2068 | 0 | allow_realloc, var, enums, hint, |
2069 | 0 | units); |
2070 | 0 | case ASN_COUNTER64: |
2071 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
2072 | 0 | case ASN_OPAQUE_U64: |
2073 | 0 | case ASN_OPAQUE_I64: |
2074 | 0 | case ASN_OPAQUE_COUNTER64: |
2075 | 0 | #endif /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */ |
2076 | 0 | return sprint_realloc_counter64(buf, buf_len, out_len, |
2077 | 0 | allow_realloc, var, enums, hint, |
2078 | 0 | units); |
2079 | 0 | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
2080 | 0 | case ASN_OPAQUE_FLOAT: |
2081 | 0 | return sprint_realloc_float(buf, buf_len, out_len, allow_realloc, |
2082 | 0 | var, enums, hint, units); |
2083 | 0 | case ASN_OPAQUE_DOUBLE: |
2084 | 0 | return sprint_realloc_double(buf, buf_len, out_len, allow_realloc, |
2085 | 0 | var, enums, hint, units); |
2086 | 0 | #endif /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */ |
2087 | 0 | default: |
2088 | 0 | DEBUGMSGTL(("sprint_by_type", "bad type: %d\n", var->type)); |
2089 | 0 | return sprint_realloc_badtype(buf, buf_len, out_len, allow_realloc, |
2090 | 0 | var, enums, hint, units); |
2091 | 0 | } |
2092 | 0 | } |
2093 | | |
2094 | | /** |
2095 | | * Generates a printf format string. |
2096 | | * |
2097 | | * The original format string is combined with the optional |
2098 | | * NETSNMP_DS_LIB_OUTPUT_PRECISION string (the -Op parameter). |
2099 | | * |
2100 | | * Example: |
2101 | | * If the original format string is "%f", and the NETSNMP_DS_LIB_OUTPUT_PRECISION |
2102 | | * is "5.2", the returned format string will be "%5.2f". |
2103 | | * |
2104 | | * The PRECISION string is inserted after the '%' of the original format string. |
2105 | | * To prevent buffer overflow if NETSNMP_DS_LIB_OUTPUT_PRECISION is set to an |
2106 | | * illegal size (e.g. with -Op 10000) snprintf should be used to prevent buffer |
2107 | | * overflow. |
2108 | | * |
2109 | | * @param printf_format_default The format string used by the original printf. |
2110 | | * |
2111 | | * @return The address of of the new allocated format string (which must be freed |
2112 | | * if no longer used), or NULL if any error (malloc). |
2113 | | */ |
2114 | | char * |
2115 | | make_printf_format_string(const char *printf_format_default) |
2116 | 0 | { |
2117 | 0 | const char *cp_printf_format_default; |
2118 | 0 | const char *printf_precision; |
2119 | 0 | const char *cp_printf_precision; |
2120 | 0 | char *printf_format_string; |
2121 | 0 | char *cp_out; |
2122 | 0 | char c; |
2123 | |
|
2124 | 0 | printf_precision = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION); |
2125 | 0 | if (!printf_precision) { |
2126 | 0 | printf_precision = ""; |
2127 | 0 | } |
2128 | | |
2129 | | /* reserve new format string buffer */ |
2130 | 0 | printf_format_string = (char *) malloc(strlen(printf_format_default)+strlen(printf_precision)+1); |
2131 | 0 | if (!printf_format_string) |
2132 | 0 | { |
2133 | 0 | DEBUGMSGTL(("make_printf_format_string", "malloc failed\n")); |
2134 | 0 | return NULL; |
2135 | 0 | } |
2136 | | |
2137 | | /* copy default format string, including the '%' */ |
2138 | 0 | cp_out = printf_format_string; |
2139 | 0 | cp_printf_format_default = printf_format_default; |
2140 | 0 | while((c = *cp_printf_format_default) != '\0') |
2141 | 0 | { |
2142 | 0 | *cp_out++ = c; |
2143 | 0 | cp_printf_format_default++; |
2144 | 0 | if (c == '%') break; |
2145 | 0 | } |
2146 | | |
2147 | | /* insert the precision string */ |
2148 | 0 | cp_printf_precision = printf_precision; |
2149 | 0 | while ((c = *cp_printf_precision++) != '\0') |
2150 | 0 | { |
2151 | 0 | *cp_out++ = c; |
2152 | 0 | } |
2153 | | |
2154 | | /* copy the remaining default format string, including the terminating '\0' */ |
2155 | 0 | strcpy(cp_out, cp_printf_format_default); |
2156 | |
|
2157 | 0 | DEBUGMSGTL(("make_printf_format_string", "\"%s\"+\"%s\"->\"%s\"\n", |
2158 | 0 | printf_format_default, printf_precision, printf_format_string)); |
2159 | 0 | return printf_format_string; |
2160 | 0 | } |
2161 | | |
2162 | | |
2163 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
2164 | | /** |
2165 | | * Retrieves the tree head. |
2166 | | * |
2167 | | * @return the tree head. |
2168 | | */ |
2169 | | struct tree * |
2170 | | get_tree_head(void) |
2171 | 0 | { |
2172 | 0 | return (tree_head); |
2173 | 0 | } |
2174 | | |
2175 | | static char *confmibdir = NULL; |
2176 | | static char *confmibs = NULL; |
2177 | | |
2178 | | static void |
2179 | | handle_mibdirs_conf(const char *token, char *line) |
2180 | 0 | { |
2181 | 0 | char *ctmp; |
2182 | 0 | size_t ctmp_len; |
2183 | |
|
2184 | 0 | if (confmibdir) { |
2185 | 0 | if ((*line == '+') || (*line == '-')) { |
2186 | 0 | ctmp_len = strlen(confmibdir) + strlen(line) + 2; |
2187 | 0 | ctmp = (char *) malloc(ctmp_len); |
2188 | 0 | if (!ctmp) { |
2189 | 0 | DEBUGMSGTL(("read_config:initmib", |
2190 | 0 | "mibdir conf malloc failed")); |
2191 | 0 | return; |
2192 | 0 | } |
2193 | 0 | if(*line++ == '+') |
2194 | 0 | snprintf(ctmp, ctmp_len, "%s%c%s", confmibdir, ENV_SEPARATOR_CHAR, line); |
2195 | 0 | else |
2196 | 0 | snprintf(ctmp, ctmp_len, "%s%c%s", line, ENV_SEPARATOR_CHAR, confmibdir); |
2197 | 0 | } else { |
2198 | 0 | ctmp = strdup(line); |
2199 | 0 | if (!ctmp) { |
2200 | 0 | DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed")); |
2201 | 0 | return; |
2202 | 0 | } |
2203 | 0 | } |
2204 | 0 | SNMP_FREE(confmibdir); |
2205 | 0 | } else { |
2206 | 0 | ctmp = strdup(line); |
2207 | 0 | if (!ctmp) { |
2208 | 0 | DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed")); |
2209 | 0 | return; |
2210 | 0 | } |
2211 | 0 | } |
2212 | 0 | confmibdir = ctmp; |
2213 | 0 | DEBUGMSGTL(("read_config:initmib", "using mibdirs: %s\n", confmibdir)); |
2214 | 0 | } |
2215 | | |
2216 | | static void |
2217 | | handle_mibs_conf(const char *token, char *line) |
2218 | 0 | { |
2219 | 0 | char *ctmp; |
2220 | |
|
2221 | 0 | if (confmibs) { |
2222 | 0 | if ((*line == '+') || (*line == '-')) { |
2223 | 0 | int res; |
2224 | |
|
2225 | 0 | res = *line++ == '+' ? |
2226 | | /* Add specified dirs after existing ones */ |
2227 | 0 | asprintf(&ctmp, "%s%c%s", confmibs, ENV_SEPARATOR_CHAR, line) : |
2228 | | /* Add specified dirs before existing ones */ |
2229 | 0 | asprintf(&ctmp, "%s%c%s", line, ENV_SEPARATOR_CHAR, confmibs); |
2230 | 0 | if (res < 0) { |
2231 | 0 | DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed")); |
2232 | 0 | return; |
2233 | 0 | } |
2234 | 0 | } else { |
2235 | 0 | ctmp = strdup(line); |
2236 | 0 | if (!ctmp) { |
2237 | 0 | DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed")); |
2238 | 0 | return; |
2239 | 0 | } |
2240 | 0 | } |
2241 | 0 | SNMP_FREE(confmibs); |
2242 | 0 | } else { |
2243 | 0 | ctmp = strdup(line); |
2244 | 0 | if (!ctmp) { |
2245 | 0 | DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed")); |
2246 | 0 | return; |
2247 | 0 | } |
2248 | 0 | } |
2249 | 0 | confmibs = ctmp; |
2250 | 0 | DEBUGMSGTL(("read_config:initmib", "using mibs: %s\n", confmibs)); |
2251 | 0 | } |
2252 | | |
2253 | | |
2254 | | static void |
2255 | | handle_mibfile_conf(const char *token, char *line) |
2256 | 0 | { |
2257 | 0 | DEBUGMSGTL(("read_config:initmib", "reading mibfile: %s\n", line)); |
2258 | 0 | read_mib(line); |
2259 | 0 | } |
2260 | | #endif |
2261 | | |
2262 | | static void |
2263 | | handle_print_numeric(const char *token, char *line) |
2264 | 0 | { |
2265 | 0 | const char *value; |
2266 | 0 | char *st; |
2267 | |
|
2268 | 0 | value = strtok_r(line, " \t\n", &st); |
2269 | 0 | if (value && ( |
2270 | 0 | (strcasecmp(value, "yes") == 0) || |
2271 | 0 | (strcasecmp(value, "true") == 0) || |
2272 | 0 | (*value == '1') )) { |
2273 | |
|
2274 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2275 | 0 | NETSNMP_OID_OUTPUT_NUMERIC); |
2276 | 0 | } |
2277 | 0 | } |
2278 | | |
2279 | | char * |
2280 | | snmp_out_options(char *options, int argc, char *const *argv) |
2281 | 0 | { |
2282 | 0 | while (*options) { |
2283 | 0 | switch (*options++) { |
2284 | 0 | case '0': |
2285 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, |
2286 | 0 | NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT); |
2287 | 0 | break; |
2288 | 0 | case 'a': |
2289 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT, |
2290 | 0 | NETSNMP_STRING_OUTPUT_ASCII); |
2291 | 0 | break; |
2292 | 0 | case 'b': |
2293 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS); |
2294 | 0 | break; |
2295 | 0 | case 'e': |
2296 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM); |
2297 | 0 | break; |
2298 | 0 | case 'E': |
2299 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES); |
2300 | 0 | break; |
2301 | 0 | case 'f': |
2302 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2303 | 0 | NETSNMP_OID_OUTPUT_FULL); |
2304 | 0 | break; |
2305 | 0 | case 'F': |
2306 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2307 | 0 | NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC); |
2308 | 0 | break; |
2309 | 0 | case 'n': |
2310 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2311 | 0 | NETSNMP_OID_OUTPUT_NUMERIC); |
2312 | 0 | break; |
2313 | 0 | case 'p': |
2314 | | /* What if argc/argv are null ? */ |
2315 | 0 | if (!*(options)) { |
2316 | 0 | if (optind == argc || argc == 0) { |
2317 | 0 | fprintf(stderr, "Missing precision for -Op\n"); |
2318 | 0 | return options-1; |
2319 | 0 | } |
2320 | 0 | options = argv[optind++]; |
2321 | 0 | } |
2322 | 0 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
2323 | 0 | NETSNMP_DS_LIB_OUTPUT_PRECISION, |
2324 | 0 | options); |
2325 | 0 | return NULL; /* -Op... is a standalone option, so we're done here */ |
2326 | 0 | case 'q': |
2327 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT); |
2328 | 0 | break; |
2329 | 0 | case 'Q': |
2330 | 0 | netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT, 1); |
2331 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT); |
2332 | 0 | break; |
2333 | 0 | case 's': |
2334 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2335 | 0 | NETSNMP_OID_OUTPUT_SUFFIX); |
2336 | 0 | break; |
2337 | 0 | case 'S': |
2338 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2339 | 0 | NETSNMP_OID_OUTPUT_MODULE); |
2340 | 0 | break; |
2341 | 0 | case 't': |
2342 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS); |
2343 | 0 | break; |
2344 | 0 | case 'T': |
2345 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT); |
2346 | 0 | break; |
2347 | 0 | case 'u': |
2348 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, |
2349 | 0 | NETSNMP_OID_OUTPUT_UCD); |
2350 | 0 | break; |
2351 | 0 | case 'U': |
2352 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS); |
2353 | 0 | break; |
2354 | 0 | case 'v': |
2355 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE); |
2356 | 0 | break; |
2357 | 0 | case 'x': |
2358 | 0 | netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT, |
2359 | 0 | NETSNMP_STRING_OUTPUT_HEX); |
2360 | 0 | break; |
2361 | 0 | case 'X': |
2362 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX); |
2363 | 0 | break; |
2364 | 0 | default: |
2365 | 0 | return options - 1; |
2366 | 0 | } |
2367 | 0 | } |
2368 | 0 | return NULL; |
2369 | 0 | } |
2370 | | |
2371 | | char * |
2372 | | snmp_out_toggle_options(char *options) |
2373 | 0 | { |
2374 | 0 | return snmp_out_options( options, 0, NULL ); |
2375 | 0 | } |
2376 | | |
2377 | | void |
2378 | | snmp_out_toggle_options_usage(const char *lead, FILE * outf) |
2379 | 0 | { |
2380 | 0 | fprintf(outf, "%s0: print leading 0 for single-digit hex characters\n", lead); |
2381 | 0 | fprintf(outf, "%sa: print all strings in ascii format\n", lead); |
2382 | 0 | fprintf(outf, "%sb: do not break OID indexes down\n", lead); |
2383 | 0 | fprintf(outf, "%se: print enums numerically\n", lead); |
2384 | 0 | fprintf(outf, "%sE: escape quotes in string indices\n", lead); |
2385 | 0 | fprintf(outf, "%sf: print full OIDs on output\n", lead); |
2386 | 0 | fprintf(outf, "%sn: print OIDs numerically\n", lead); |
2387 | 0 | fprintf(outf, "%sp PRECISION: display floating point values with specified PRECISION (printf format string)\n", lead); |
2388 | 0 | fprintf(outf, "%sq: quick print for easier parsing\n", lead); |
2389 | 0 | fprintf(outf, "%sQ: quick print with equal-signs\n", lead); /* @@JDW */ |
2390 | 0 | fprintf(outf, "%ss: print only last symbolic element of OID\n", lead); |
2391 | 0 | fprintf(outf, "%sS: print MIB module-id plus last element\n", lead); |
2392 | 0 | fprintf(outf, "%st: print timeticks unparsed as numeric integers\n", |
2393 | 0 | lead); |
2394 | 0 | fprintf(outf, |
2395 | 0 | "%sT: print human-readable text along with hex strings\n", |
2396 | 0 | lead); |
2397 | 0 | fprintf(outf, "%su: print OIDs using UCD-style prefix suppression\n", |
2398 | 0 | lead); |
2399 | 0 | fprintf(outf, "%sU: don't print units\n", lead); |
2400 | 0 | fprintf(outf, "%sv: print values only (not OID = value)\n", lead); |
2401 | 0 | fprintf(outf, "%sx: print all strings in hex format\n", lead); |
2402 | 0 | fprintf(outf, "%sX: extended index format\n", lead); |
2403 | 0 | } |
2404 | | |
2405 | | const char * |
2406 | | snmp_in_options(char *optarg, int argc, char *const *argv) |
2407 | 0 | { |
2408 | 0 | char *cp; |
2409 | |
|
2410 | 0 | for (cp = optarg; *cp; cp++) { |
2411 | 0 | switch (*cp) { |
2412 | 0 | case 'b': |
2413 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_REGEX_ACCESS); |
2414 | 0 | break; |
2415 | 0 | case 'R': |
2416 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS); |
2417 | 0 | break; |
2418 | 0 | case 'r': |
2419 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_CHECK_RANGE); |
2420 | 0 | break; |
2421 | 0 | case 'h': |
2422 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_DISPLAY_HINT); |
2423 | 0 | break; |
2424 | 0 | case 'u': |
2425 | 0 | netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_READ_UCD_STYLE_OID); |
2426 | 0 | break; |
2427 | 0 | case 's': |
2428 | 0 | if (!*(++cp)) { |
2429 | 0 | cp = optind < argc ? argv[optind++] : NULL; |
2430 | 0 | if (!cp) |
2431 | 0 | return "?"; |
2432 | 0 | } |
2433 | 0 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
2434 | 0 | NETSNMP_DS_LIB_OIDSUFFIX, |
2435 | 0 | cp); |
2436 | 0 | return NULL; /* -Is... is a standalone option, so we're done here */ |
2437 | | |
2438 | 0 | case 'S': |
2439 | 0 | if (!*(++cp)) { |
2440 | 0 | cp = optind < argc ? argv[optind++] : NULL; |
2441 | 0 | if (!cp) |
2442 | 0 | return "?"; |
2443 | 0 | } |
2444 | 0 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, |
2445 | 0 | NETSNMP_DS_LIB_OIDPREFIX, |
2446 | 0 | cp); |
2447 | 0 | return NULL; /* -IS... is a standalone option, so we're done here */ |
2448 | | |
2449 | 0 | default: |
2450 | | /* |
2451 | | * Here? Or in snmp_parse_args? |
2452 | | snmp_log(LOG_ERR, "Unknown input option passed to -I: %c.\n", *cp); |
2453 | | */ |
2454 | 0 | return cp; |
2455 | 0 | } |
2456 | 0 | } |
2457 | 0 | return NULL; |
2458 | 0 | } |
2459 | | |
2460 | | const char * |
2461 | | snmp_in_toggle_options(char *options) |
2462 | 0 | { |
2463 | 0 | return snmp_in_options( options, 0, NULL ); |
2464 | 0 | } |
2465 | | |
2466 | | |
2467 | | /** |
2468 | | * Prints out a help usage for the in* toggle options. |
2469 | | * |
2470 | | * @param lead The lead to print for every line. |
2471 | | * @param outf The file descriptor to write to. |
2472 | | * |
2473 | | */ |
2474 | | void |
2475 | | snmp_in_toggle_options_usage(const char *lead, FILE * outf) |
2476 | 0 | { |
2477 | 0 | fprintf(outf, "%sb: do best/regex matching to find a MIB node\n", lead); |
2478 | 0 | fprintf(outf, "%sh: don't apply DISPLAY-HINTs\n", lead); |
2479 | 0 | fprintf(outf, "%sr: do not check values for range/type legality\n", lead); |
2480 | 0 | fprintf(outf, "%sR: do random access to OID labels\n", lead); |
2481 | 0 | fprintf(outf, |
2482 | 0 | "%su: top-level OIDs must have '.' prefix (UCD-style)\n", lead); |
2483 | 0 | fprintf(outf, |
2484 | 0 | "%ss SUFFIX: Append all textual OIDs with SUFFIX before parsing\n", |
2485 | 0 | lead); |
2486 | 0 | fprintf(outf, |
2487 | 0 | "%sS PREFIX: Prepend all textual OIDs with PREFIX before parsing\n", |
2488 | 0 | lead); |
2489 | 0 | } |
2490 | | |
2491 | | /*** |
2492 | | * |
2493 | | */ |
2494 | | void |
2495 | | register_mib_handlers(void) |
2496 | 66 | { |
2497 | 66 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
2498 | 66 | register_prenetsnmp_mib_handler("snmp", "mibdirs", |
2499 | 66 | handle_mibdirs_conf, NULL, |
2500 | 66 | "[mib-dirs|+mib-dirs|-mib-dirs]"); |
2501 | 66 | register_prenetsnmp_mib_handler("snmp", "mibs", |
2502 | 66 | handle_mibs_conf, NULL, |
2503 | 66 | "[mib-tokens|+mib-tokens|-mib-tokens]"); |
2504 | 66 | register_config_handler("snmp", "mibfile", |
2505 | 66 | handle_mibfile_conf, NULL, "mibfile-to-read"); |
2506 | | /* |
2507 | | * register the snmp.conf configuration handlers for default |
2508 | | * parsing behaviour |
2509 | | */ |
2510 | | |
2511 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "showMibErrors", |
2512 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_ERRORS); |
2513 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "commentToEOL", /* Describes actual behaviour */ |
2514 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_COMMENT_TERM); |
2515 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "strictCommentTerm", /* Backward compatibility */ |
2516 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_COMMENT_TERM); |
2517 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "mibAllowUnderline", |
2518 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_PARSE_LABEL); |
2519 | 66 | netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "mibWarningLevel", |
2520 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_WARNINGS); |
2521 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "mibReplaceWithLatest", |
2522 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_REPLACE); |
2523 | 66 | #endif |
2524 | | |
2525 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "printNumericEnums", |
2526 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM); |
2527 | 66 | register_prenetsnmp_mib_handler("snmp", "printNumericOids", |
2528 | 66 | handle_print_numeric, NULL, "(1|yes|true|0|no|false)"); |
2529 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "escapeQuotes", |
2530 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES); |
2531 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "dontBreakdownOids", |
2532 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS); |
2533 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "quickPrinting", |
2534 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT); |
2535 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "numericTimeticks", |
2536 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS); |
2537 | 66 | netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "oidOutputFormat", |
2538 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT); |
2539 | 66 | netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "suffixPrinting", |
2540 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT); |
2541 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "extendedIndex", |
2542 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX); |
2543 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "printHexText", |
2544 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT); |
2545 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "printValueOnly", |
2546 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE); |
2547 | 66 | netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "dontPrintUnits", |
2548 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS); |
2549 | 66 | netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "hexOutputLength", |
2550 | 66 | NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_HEX_OUTPUT_LENGTH); |
2551 | 66 | } |
2552 | | |
2553 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
2554 | | /* |
2555 | | * function : netsnmp_set_mib_directory |
2556 | | * - This function sets the string of the directories |
2557 | | * from which the MIB modules will be searched or |
2558 | | * loaded. |
2559 | | * arguments: const char *dir, which are the directories |
2560 | | * from which the MIB modules will be searched or |
2561 | | * loaded. |
2562 | | * returns : - |
2563 | | */ |
2564 | | void |
2565 | | netsnmp_set_mib_directory(const char *dir) |
2566 | 131 | { |
2567 | 131 | const char *newdir; |
2568 | 131 | char *olddir, *tmpdir = NULL; |
2569 | | |
2570 | 131 | DEBUGTRACE; |
2571 | 131 | if (NULL == dir) { |
2572 | 0 | return; |
2573 | 0 | } |
2574 | | |
2575 | 131 | olddir = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
2576 | 131 | NETSNMP_DS_LIB_MIBDIRS); |
2577 | 131 | if (olddir) { |
2578 | 66 | if ((*dir == '+') || (*dir == '-')) { |
2579 | | /** New dir starts with '+', thus we add it. */ |
2580 | 0 | if (*dir++ == '+') { |
2581 | 0 | if (asprintf(&tmpdir, "%s%c%s", olddir, ENV_SEPARATOR_CHAR, dir) < 0) |
2582 | 0 | tmpdir = NULL; |
2583 | 0 | } else { |
2584 | 0 | if (asprintf(&tmpdir, "%s%c%s", dir, ENV_SEPARATOR_CHAR, olddir) < 0) |
2585 | 0 | tmpdir = NULL; |
2586 | 0 | } |
2587 | 0 | if (!tmpdir) { |
2588 | 0 | DEBUGMSGTL(("read_config:initmib", "set mibdir malloc failed")); |
2589 | 0 | return; |
2590 | 0 | } |
2591 | 0 | newdir = tmpdir; |
2592 | 66 | } else { |
2593 | 66 | newdir = dir; |
2594 | 66 | } |
2595 | 66 | } else { |
2596 | | /** If dir starts with '+' skip '+' it. */ |
2597 | 65 | newdir = ((*dir == '+') ? ++dir : dir); |
2598 | 65 | } |
2599 | 131 | netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS, |
2600 | 131 | newdir); |
2601 | | |
2602 | | /** set_string calls strdup, so if we allocated memory, free it */ |
2603 | 131 | if (tmpdir == newdir) { |
2604 | 0 | SNMP_FREE(tmpdir); |
2605 | 0 | } |
2606 | 131 | } |
2607 | | |
2608 | | /* |
2609 | | * function : netsnmp_get_mib_directory |
2610 | | * - This function returns a string of the directories |
2611 | | * from which the MIB modules will be searched or |
2612 | | * loaded. |
2613 | | * If the value still does not exists, it will be made |
2614 | | * from the environment variable 'MIBDIRS' and/or the |
2615 | | * default. |
2616 | | * arguments: - |
2617 | | * returns : char * of the directories in which the MIB modules |
2618 | | * will be searched/loaded. |
2619 | | */ |
2620 | | |
2621 | | char * |
2622 | | netsnmp_get_mib_directory(void) |
2623 | 198 | { |
2624 | 198 | char *dir; |
2625 | | |
2626 | 198 | DEBUGTRACE; |
2627 | 198 | dir = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS); |
2628 | 198 | if (dir == NULL) { |
2629 | 65 | DEBUGMSGTL(("get_mib_directory", "no mib directories set\n")); |
2630 | | |
2631 | | /** Check if the environment variable is set */ |
2632 | 65 | dir = netsnmp_getenv("MIBDIRS"); |
2633 | 65 | if (dir == NULL) { |
2634 | 65 | DEBUGMSGTL(("get_mib_directory", "no mib directories set by environment\n")); |
2635 | | /** Not set use hard coded path */ |
2636 | 65 | if (confmibdir == NULL) { |
2637 | 65 | DEBUGMSGTL(("get_mib_directory", "no mib directories set by config\n")); |
2638 | 65 | netsnmp_set_mib_directory(NETSNMP_DEFAULT_MIBDIRS); |
2639 | 65 | } |
2640 | 0 | else if ((*confmibdir == '+') || (*confmibdir == '-')) { |
2641 | 0 | DEBUGMSGTL(("get_mib_directory", "mib directories set by config (but added)\n")); |
2642 | 0 | netsnmp_set_mib_directory(NETSNMP_DEFAULT_MIBDIRS); |
2643 | 0 | netsnmp_set_mib_directory(confmibdir); |
2644 | 0 | } |
2645 | 0 | else { |
2646 | 0 | DEBUGMSGTL(("get_mib_directory", "mib directories set by config\n")); |
2647 | 0 | netsnmp_set_mib_directory(confmibdir); |
2648 | 0 | } |
2649 | 65 | } else if ((*dir == '+') || (*dir == '-')) { |
2650 | 0 | DEBUGMSGTL(("get_mib_directory", "mib directories set by environment (but added)\n")); |
2651 | 0 | netsnmp_set_mib_directory(NETSNMP_DEFAULT_MIBDIRS); |
2652 | 0 | netsnmp_set_mib_directory(dir); |
2653 | 0 | } else { |
2654 | 0 | DEBUGMSGTL(("get_mib_directory", "mib directories set by environment\n")); |
2655 | 0 | netsnmp_set_mib_directory(dir); |
2656 | 0 | } |
2657 | 65 | dir = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS); |
2658 | 65 | } |
2659 | 198 | DEBUGMSGTL(("get_mib_directory", "mib directories set '%s'\n", dir)); |
2660 | 198 | return(dir); |
2661 | 198 | } |
2662 | | |
2663 | | /* |
2664 | | * function : netsnmp_fixup_mib_directory |
2665 | | * arguments: - |
2666 | | * returns : - |
2667 | | */ |
2668 | | void |
2669 | | netsnmp_fixup_mib_directory(void) |
2670 | 66 | { |
2671 | 66 | const char *homepath = netsnmp_gethomedir(); |
2672 | 66 | char *mibpath = netsnmp_get_mib_directory(); |
2673 | 66 | char *oldmibpath = NULL; |
2674 | 66 | char *ptr_home; |
2675 | 66 | char *new_mibpath; |
2676 | 66 | size_t new_mibpath_len; |
2677 | | |
2678 | 66 | DEBUGTRACE; |
2679 | 66 | if (homepath && mibpath) { |
2680 | 66 | DEBUGMSGTL(("fixup_mib_directory", "mib directories '%s'\n", mibpath)); |
2681 | 131 | while ((ptr_home = strstr(mibpath, "$HOME"))) { |
2682 | 65 | new_mibpath_len = strlen(mibpath) - strlen("$HOME") + |
2683 | 65 | strlen(homepath)+1; |
2684 | 65 | new_mibpath = (char *)malloc(new_mibpath_len); |
2685 | 65 | if (new_mibpath) { |
2686 | 65 | *ptr_home = 0; /* null out the spot where we stop copying */ |
2687 | 65 | snprintf(new_mibpath, new_mibpath_len, "%s%s%s", mibpath, homepath, |
2688 | 65 | ptr_home + strlen("$HOME")); |
2689 | | /** swap in the new value and repeat */ |
2690 | 65 | mibpath = new_mibpath; |
2691 | 65 | if (oldmibpath != NULL) { |
2692 | 0 | SNMP_FREE(oldmibpath); |
2693 | 0 | } |
2694 | 65 | oldmibpath = new_mibpath; |
2695 | 65 | } else { |
2696 | 0 | break; |
2697 | 0 | } |
2698 | 65 | } |
2699 | | |
2700 | 66 | netsnmp_set_mib_directory(mibpath); |
2701 | | |
2702 | | /* The above copies the mibpath for us, so... */ |
2703 | | |
2704 | 66 | if (oldmibpath != NULL) { |
2705 | 65 | SNMP_FREE(oldmibpath); |
2706 | 65 | } |
2707 | | |
2708 | 66 | } |
2709 | | |
2710 | 66 | } |
2711 | | |
2712 | | /** |
2713 | | * Initialises the mib reader. |
2714 | | * |
2715 | | * Reads in all settings from the environment. |
2716 | | */ |
2717 | | void |
2718 | | netsnmp_init_mib(void) |
2719 | 66 | { |
2720 | 66 | const char *prefix; |
2721 | 66 | char *env_var, *entry; |
2722 | 66 | PrefixListPtr pp = &mib_prefixes[0]; |
2723 | 66 | char *st = NULL; |
2724 | | |
2725 | 66 | if (Mib) |
2726 | 0 | return; |
2727 | 66 | netsnmp_init_mib_internals(); |
2728 | | |
2729 | | /* |
2730 | | * Initialise the MIB directory/ies |
2731 | | */ |
2732 | 66 | netsnmp_fixup_mib_directory(); |
2733 | 66 | env_var = strdup(netsnmp_get_mib_directory()); |
2734 | 66 | if (!env_var) |
2735 | 0 | return; |
2736 | | |
2737 | 66 | DEBUGMSGTL(("init_mib", |
2738 | 66 | "Seen MIBDIRS: Looking in '%s' for mib dirs ...\n", |
2739 | 66 | env_var)); |
2740 | | |
2741 | 66 | entry = strtok_r(env_var, ENV_SEPARATOR, &st); |
2742 | 197 | while (entry) { |
2743 | 131 | add_mibdir(entry); |
2744 | 131 | entry = strtok_r(NULL, ENV_SEPARATOR, &st); |
2745 | 131 | } |
2746 | 66 | SNMP_FREE(env_var); |
2747 | | |
2748 | 66 | env_var = netsnmp_getenv("MIBFILES"); |
2749 | 66 | if (env_var != NULL) { |
2750 | 0 | if (*env_var == '+') |
2751 | 0 | entry = strtok_r(env_var+1, ENV_SEPARATOR, &st); |
2752 | 0 | else |
2753 | 0 | entry = strtok_r(env_var, ENV_SEPARATOR, &st); |
2754 | 0 | while (entry) { |
2755 | 0 | add_mibfile(entry, NULL); |
2756 | 0 | entry = strtok_r(NULL, ENV_SEPARATOR, &st); |
2757 | 0 | } |
2758 | 0 | } |
2759 | | |
2760 | 66 | netsnmp_init_mib_internals(); |
2761 | | |
2762 | | /* |
2763 | | * Read in any modules or mibs requested |
2764 | | */ |
2765 | | |
2766 | 66 | env_var = netsnmp_getenv("MIBS"); |
2767 | 66 | if (env_var == NULL) { |
2768 | 66 | if (confmibs != NULL) |
2769 | 0 | env_var = strdup(confmibs); |
2770 | 66 | else |
2771 | 66 | env_var = strdup(NETSNMP_DEFAULT_MIBS); |
2772 | 66 | } else { |
2773 | 0 | env_var = strdup(env_var); |
2774 | 0 | } |
2775 | 66 | if (env_var && ((*env_var == '+') || (*env_var == '-'))) { |
2776 | 0 | int res; |
2777 | |
|
2778 | 0 | if (*env_var == '+') |
2779 | 0 | res = asprintf(&entry, "%s%c%s", NETSNMP_DEFAULT_MIBS, |
2780 | 0 | ENV_SEPARATOR_CHAR, env_var + 1); |
2781 | 0 | else |
2782 | 0 | res = asprintf(&entry, "%s%c%s", env_var + 1, ENV_SEPARATOR_CHAR, |
2783 | 0 | NETSNMP_DEFAULT_MIBS); |
2784 | 0 | SNMP_FREE(env_var); |
2785 | 0 | if (res < 0) { |
2786 | 0 | DEBUGMSGTL(("init_mib", "env mibs malloc failed")); |
2787 | 0 | return; |
2788 | 0 | } |
2789 | 0 | env_var = entry; |
2790 | 0 | } |
2791 | | |
2792 | 66 | if (env_var != NULL) { |
2793 | 66 | DEBUGMSGTL(("init_mib", |
2794 | 66 | "Seen MIBS: Looking in '%s' for mib files ...\n", |
2795 | 66 | env_var)); |
2796 | 66 | entry = strtok_r(env_var, ENV_SEPARATOR, &st); |
2797 | 3.23k | while (entry) { |
2798 | 3.16k | if (strcasecmp(entry, DEBUG_ALWAYS_TOKEN) == 0) { |
2799 | 0 | read_all_mibs(); |
2800 | 3.16k | } else if (strstr(entry, "/") != NULL) { |
2801 | 0 | read_mib(entry); |
2802 | 3.16k | } else { |
2803 | 3.16k | netsnmp_read_module(entry); |
2804 | 3.16k | } |
2805 | 3.16k | entry = strtok_r(NULL, ENV_SEPARATOR, &st); |
2806 | 3.16k | } |
2807 | 66 | adopt_orphans(); |
2808 | 66 | SNMP_FREE(env_var); |
2809 | 66 | } |
2810 | | |
2811 | 66 | env_var = netsnmp_getenv("MIBFILES"); |
2812 | 66 | if (env_var != NULL) { |
2813 | 0 | if ((*env_var == '+') || (*env_var == '-')) { |
2814 | | #ifdef NETSNMP_DEFAULT_MIBFILES |
2815 | | int res; |
2816 | | |
2817 | | if (*env_var++ == '+') |
2818 | | res = asprintf(&entry, "%s%c%s", NETSNMP_DEFAULT_MIBFILES, |
2819 | | ENV_SEPARATOR_CHAR, env_var); |
2820 | | else |
2821 | | res = asprintf(&entry, "%s%c%s", env_var, |
2822 | | ENV_SEPARATOR_CHAR, NETSNMP_DEFAULT_MIBFILES); |
2823 | | if (res >= 0) |
2824 | | env_var = entry; |
2825 | | else |
2826 | | DEBUGMSGTL(("init_mib", "env mibfiles malloc failed")); |
2827 | | #else |
2828 | 0 | env_var = strdup(env_var + 1); |
2829 | 0 | #endif |
2830 | 0 | } else { |
2831 | 0 | env_var = strdup(env_var); |
2832 | 0 | } |
2833 | 66 | } else { |
2834 | | #ifdef NETSNMP_DEFAULT_MIBFILES |
2835 | | env_var = strdup(NETSNMP_DEFAULT_MIBFILES); |
2836 | | #endif |
2837 | 66 | } |
2838 | | |
2839 | 66 | if (env_var != NULL) { |
2840 | 0 | DEBUGMSGTL(("init_mib", |
2841 | 0 | "Seen MIBFILES: Looking in '%s' for mib files ...\n", |
2842 | 0 | env_var)); |
2843 | 0 | entry = strtok_r(env_var, ENV_SEPARATOR, &st); |
2844 | 0 | while (entry) { |
2845 | 0 | read_mib(entry); |
2846 | 0 | entry = strtok_r(NULL, ENV_SEPARATOR, &st); |
2847 | 0 | } |
2848 | 0 | SNMP_FREE(env_var); |
2849 | 0 | } |
2850 | | |
2851 | 66 | prefix = netsnmp_getenv("PREFIX"); |
2852 | | |
2853 | 66 | if (!prefix) |
2854 | 66 | prefix = Standard_Prefix; |
2855 | | |
2856 | 66 | Prefix = (char *) malloc(strlen(prefix) + 2); |
2857 | 66 | if (!Prefix) |
2858 | 0 | DEBUGMSGTL(("init_mib", "Prefix malloc failed")); |
2859 | 66 | else |
2860 | 66 | strcpy(Prefix, prefix); |
2861 | | |
2862 | 66 | DEBUGMSGTL(("init_mib", |
2863 | 66 | "Seen PREFIX: Looking in '%s' for prefix ...\n", Prefix)); |
2864 | | |
2865 | | /* |
2866 | | * remove trailing dot |
2867 | | */ |
2868 | 66 | if (Prefix) { |
2869 | 66 | env_var = &Prefix[strlen(Prefix) - 1]; |
2870 | 66 | if (*env_var == '.') |
2871 | 0 | *env_var = '\0'; |
2872 | 66 | } |
2873 | | |
2874 | 66 | pp->str = Prefix; /* fixup first mib_prefix entry */ |
2875 | | /* |
2876 | | * now that the list of prefixes is built, save each string length. |
2877 | | */ |
2878 | 462 | while (pp->str) { |
2879 | 396 | pp->len = strlen(pp->str); |
2880 | 396 | pp++; |
2881 | 396 | } |
2882 | | |
2883 | 66 | Mib = tree_head; /* Backwards compatibility */ |
2884 | 66 | tree_top = calloc(1, sizeof(struct tree)); |
2885 | | /* |
2886 | | * XX error check ? |
2887 | | */ |
2888 | 66 | if (tree_top) { |
2889 | 66 | tree_top->label = strdup("(top)"); |
2890 | 66 | tree_top->child_list = tree_head; |
2891 | 66 | } |
2892 | 66 | } |
2893 | | |
2894 | | #ifndef NETSNMP_NO_LEGACY_DEFINITIONS |
2895 | | void |
2896 | | init_mib(void) |
2897 | 0 | { |
2898 | 0 | netsnmp_init_mib(); |
2899 | 0 | } |
2900 | | #endif |
2901 | | |
2902 | | |
2903 | | /** |
2904 | | * Unloads all mibs. |
2905 | | */ |
2906 | | void |
2907 | | shutdown_mib(void) |
2908 | 66 | { |
2909 | 66 | unload_all_mibs(); |
2910 | 66 | if (tree_top) { |
2911 | 66 | if (tree_top->label) |
2912 | 66 | SNMP_FREE(tree_top->label); |
2913 | 66 | SNMP_FREE(tree_top); |
2914 | 66 | } |
2915 | 66 | tree_head = NULL; |
2916 | 66 | Mib = NULL; |
2917 | 66 | if (Prefix != NULL && Prefix != &Standard_Prefix[0]) |
2918 | 66 | SNMP_FREE(Prefix); |
2919 | 66 | if (Prefix) |
2920 | 0 | Prefix = NULL; |
2921 | 66 | SNMP_FREE(confmibs); |
2922 | 66 | SNMP_FREE(confmibdir); |
2923 | 66 | } |
2924 | | |
2925 | | /** |
2926 | | * Prints the MIBs to the file fp. |
2927 | | * |
2928 | | * @param fp The file descriptor to print to. |
2929 | | */ |
2930 | | #ifndef NETSNMP_FEATURE_REMOVE_PRINT_MIB |
2931 | | void |
2932 | | print_mib(FILE * fp) |
2933 | 0 | { |
2934 | 0 | print_subtree(fp, tree_head, 0); |
2935 | 0 | } |
2936 | | #endif /* NETSNMP_FEATURE_REMOVE_PRINT_MIB */ |
2937 | | |
2938 | | void |
2939 | | print_ascii_dump(FILE * fp) |
2940 | 0 | { |
2941 | 0 | fprintf(fp, "dump DEFINITIONS ::= BEGIN\n"); |
2942 | 0 | print_ascii_dump_tree(fp, tree_head, 0); |
2943 | 0 | fprintf(fp, "END\n"); |
2944 | 0 | } |
2945 | | |
2946 | | |
2947 | | /** |
2948 | | * Set's the printing function printomat in a subtree according |
2949 | | * it's type |
2950 | | * |
2951 | | * @param subtree The subtree to set. |
2952 | | */ |
2953 | | void |
2954 | | set_function(struct tree *subtree) |
2955 | 198 | { |
2956 | 198 | subtree->printer = NULL; |
2957 | 198 | switch (subtree->type) { |
2958 | 0 | case TYPE_OBJID: |
2959 | 0 | subtree->printomat = sprint_realloc_object_identifier; |
2960 | 0 | break; |
2961 | 0 | case TYPE_OCTETSTR: |
2962 | 0 | subtree->printomat = sprint_realloc_octet_string; |
2963 | 0 | break; |
2964 | 0 | case TYPE_INTEGER: |
2965 | 0 | subtree->printomat = sprint_realloc_integer; |
2966 | 0 | break; |
2967 | 0 | case TYPE_INTEGER32: |
2968 | 0 | subtree->printomat = sprint_realloc_integer; |
2969 | 0 | break; |
2970 | 0 | case TYPE_NETADDR: |
2971 | 0 | subtree->printomat = sprint_realloc_networkaddress; |
2972 | 0 | break; |
2973 | 0 | case TYPE_IPADDR: |
2974 | 0 | subtree->printomat = sprint_realloc_ipaddress; |
2975 | 0 | break; |
2976 | 0 | case TYPE_COUNTER: |
2977 | 0 | subtree->printomat = sprint_realloc_counter; |
2978 | 0 | break; |
2979 | 0 | case TYPE_GAUGE: |
2980 | 0 | subtree->printomat = sprint_realloc_gauge; |
2981 | 0 | break; |
2982 | 0 | case TYPE_TIMETICKS: |
2983 | 0 | subtree->printomat = sprint_realloc_timeticks; |
2984 | 0 | break; |
2985 | 0 | case TYPE_OPAQUE: |
2986 | 0 | subtree->printomat = sprint_realloc_opaque; |
2987 | 0 | break; |
2988 | 0 | case TYPE_NULL: |
2989 | 0 | subtree->printomat = sprint_realloc_null; |
2990 | 0 | break; |
2991 | 0 | case TYPE_BITSTRING: |
2992 | 0 | subtree->printomat = sprint_realloc_bitstring; |
2993 | 0 | break; |
2994 | 0 | case TYPE_NSAPADDRESS: |
2995 | 0 | subtree->printomat = sprint_realloc_nsapaddress; |
2996 | 0 | break; |
2997 | 0 | case TYPE_COUNTER64: |
2998 | 0 | subtree->printomat = sprint_realloc_counter64; |
2999 | 0 | break; |
3000 | 0 | case TYPE_UINTEGER: |
3001 | 0 | subtree->printomat = sprint_realloc_uinteger; |
3002 | 0 | break; |
3003 | 0 | case TYPE_UNSIGNED32: |
3004 | 0 | subtree->printomat = sprint_realloc_gauge; |
3005 | 0 | break; |
3006 | 198 | case TYPE_OTHER: |
3007 | 198 | default: |
3008 | 198 | subtree->printomat = sprint_realloc_by_type; |
3009 | 198 | break; |
3010 | 198 | } |
3011 | 198 | } |
3012 | | |
3013 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3014 | | |
3015 | | /** |
3016 | | * Reads an object identifier from an input string into internal OID form. |
3017 | | * |
3018 | | * When called, out_len must hold the maximum length of the output array. |
3019 | | * |
3020 | | * @param input the input string. |
3021 | | * @param output the oid write. |
3022 | | * @param out_len number of subid's in output. |
3023 | | * |
3024 | | * @return 1 if successful. |
3025 | | * |
3026 | | * If an error occurs, this function returns 0 and MAY set snmp_errno. |
3027 | | * snmp_errno is NOT set if SET_SNMP_ERROR evaluates to nothing. |
3028 | | * This can make multi-threaded use a tiny bit more robust. |
3029 | | */ |
3030 | | int |
3031 | | read_objid(const char *input, oid * output, size_t * out_len) |
3032 | 0 | { |
3033 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3034 | 0 | struct tree *root = tree_top; |
3035 | 0 | char buf[SPRINT_MAX_LEN]; |
3036 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3037 | 0 | int ret, max_out_len; |
3038 | 0 | char *name, ch; |
3039 | 0 | const char *cp; |
3040 | |
|
3041 | 0 | cp = input; |
3042 | 0 | while ((ch = *cp)) { |
3043 | 0 | if (('0' <= ch && ch <= '9') |
3044 | 0 | || ('a' <= ch && ch <= 'z') |
3045 | 0 | || ('A' <= ch && ch <= 'Z') |
3046 | 0 | || ch == '-') |
3047 | 0 | cp++; |
3048 | 0 | else |
3049 | 0 | break; |
3050 | 0 | } |
3051 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3052 | 0 | if (ch == ':') |
3053 | 0 | return get_node(input, output, out_len); |
3054 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3055 | | |
3056 | 0 | if (*input == '.') |
3057 | 0 | input++; |
3058 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3059 | 0 | else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_READ_UCD_STYLE_OID)) { |
3060 | | /* |
3061 | | * get past leading '.', append '.' to Prefix. |
3062 | | */ |
3063 | 0 | if (*Prefix == '.') |
3064 | 0 | strlcpy(buf, Prefix + 1, sizeof(buf)); |
3065 | 0 | else |
3066 | 0 | strlcpy(buf, Prefix, sizeof(buf)); |
3067 | 0 | strlcat(buf, ".", sizeof(buf)); |
3068 | 0 | strlcat(buf, input, sizeof(buf)); |
3069 | 0 | input = buf; |
3070 | 0 | } |
3071 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3072 | |
|
3073 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3074 | 0 | if ((root == NULL) && (tree_head != NULL)) { |
3075 | 0 | root = tree_head; |
3076 | 0 | } |
3077 | 0 | else if (root == NULL) { |
3078 | 0 | SET_SNMP_ERROR(SNMPERR_NOMIB); |
3079 | 0 | *out_len = 0; |
3080 | 0 | return 0; |
3081 | 0 | } |
3082 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3083 | 0 | name = strdup(input); |
3084 | 0 | max_out_len = *out_len; |
3085 | 0 | *out_len = 0; |
3086 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3087 | 0 | if ((ret = |
3088 | 0 | _add_strings_to_oid(root, name, output, out_len, |
3089 | 0 | max_out_len)) <= 0) |
3090 | | #else |
3091 | | if ((ret = |
3092 | | _add_strings_to_oid(NULL, name, output, out_len, |
3093 | | max_out_len)) <= 0) |
3094 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3095 | 0 | { |
3096 | 0 | if (ret == 0) |
3097 | 0 | ret = SNMPERR_UNKNOWN_OBJID; |
3098 | 0 | SET_SNMP_ERROR(ret); |
3099 | 0 | SNMP_FREE(name); |
3100 | 0 | return 0; |
3101 | 0 | } |
3102 | 0 | SNMP_FREE(name); |
3103 | |
|
3104 | 0 | return 1; |
3105 | 0 | } |
3106 | | |
3107 | | /** |
3108 | | * |
3109 | | */ |
3110 | | void |
3111 | | netsnmp_sprint_realloc_objid(u_char ** buf, size_t * buf_len, |
3112 | | size_t * out_len, int allow_realloc, |
3113 | | int *buf_overflow, |
3114 | | const oid * objid, size_t objidlen) |
3115 | 0 | { |
3116 | 0 | u_char *tbuf = NULL, *cp = NULL; |
3117 | 0 | size_t tbuf_len = 256, tout_len = 0; |
3118 | 0 | int tbuf_overflow = 0; |
3119 | 0 | int output_format; |
3120 | |
|
3121 | 0 | if ((tbuf = calloc(tbuf_len, 1)) == NULL) { |
3122 | 0 | tbuf_overflow = 1; |
3123 | 0 | } else { |
3124 | 0 | *tbuf = '.'; |
3125 | 0 | tout_len = 1; |
3126 | 0 | } |
3127 | |
|
3128 | 0 | _oid_finish_printing(objid, objidlen, |
3129 | 0 | &tbuf, &tbuf_len, &tout_len, |
3130 | 0 | allow_realloc, &tbuf_overflow); |
3131 | |
|
3132 | 0 | if (tbuf_overflow) { |
3133 | 0 | if (!*buf_overflow) { |
3134 | 0 | snmp_strcat(buf, buf_len, out_len, allow_realloc, tbuf); |
3135 | 0 | *buf_overflow = 1; |
3136 | 0 | } |
3137 | 0 | SNMP_FREE(tbuf); |
3138 | 0 | return; |
3139 | 0 | } |
3140 | | |
3141 | 0 | output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT); |
3142 | 0 | if (0 == output_format) { |
3143 | 0 | output_format = NETSNMP_OID_OUTPUT_NUMERIC; |
3144 | 0 | } |
3145 | 0 | switch (output_format) { |
3146 | 0 | case NETSNMP_OID_OUTPUT_FULL: |
3147 | 0 | case NETSNMP_OID_OUTPUT_NUMERIC: |
3148 | 0 | case NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC: |
3149 | 0 | case NETSNMP_OID_OUTPUT_SUFFIX: |
3150 | 0 | case NETSNMP_OID_OUTPUT_MODULE: |
3151 | 0 | cp = tbuf; |
3152 | 0 | break; |
3153 | | |
3154 | 0 | case NETSNMP_OID_OUTPUT_NONE: |
3155 | 0 | default: |
3156 | 0 | cp = NULL; |
3157 | 0 | } |
3158 | | |
3159 | 0 | if (!*buf_overflow && |
3160 | 0 | !snmp_strcat(buf, buf_len, out_len, allow_realloc, cp)) { |
3161 | 0 | *buf_overflow = 1; |
3162 | 0 | } |
3163 | 0 | SNMP_FREE(tbuf); |
3164 | 0 | } |
3165 | | |
3166 | | /** |
3167 | | * |
3168 | | */ |
3169 | | #ifdef NETSNMP_DISABLE_MIB_LOADING |
3170 | | void |
3171 | | netsnmp_sprint_realloc_objid_tree(u_char ** buf, size_t * buf_len, |
3172 | | size_t * out_len, int allow_realloc, |
3173 | | int *buf_overflow, |
3174 | | const oid * objid, size_t objidlen) |
3175 | | { |
3176 | | netsnmp_sprint_realloc_objid(buf, buf_len, out_len, allow_realloc, |
3177 | | buf_overflow, objid, objidlen); |
3178 | | } |
3179 | | #else |
3180 | | struct tree * |
3181 | | netsnmp_sprint_realloc_objid_tree(u_char ** buf, size_t * buf_len, |
3182 | | size_t * out_len, int allow_realloc, |
3183 | | int *buf_overflow, |
3184 | | const oid * objid, size_t objidlen) |
3185 | 0 | { |
3186 | 0 | u_char *tbuf = NULL, *cp = NULL; |
3187 | 0 | size_t tbuf_len = 512, tout_len = 0; |
3188 | 0 | struct tree *subtree = tree_head; |
3189 | 0 | size_t midpoint_offset = 0; |
3190 | 0 | int tbuf_overflow = 0; |
3191 | 0 | int output_format; |
3192 | |
|
3193 | 0 | if ((tbuf = calloc(tbuf_len, 1)) == NULL) { |
3194 | 0 | tbuf_overflow = 1; |
3195 | 0 | } else { |
3196 | 0 | *tbuf = '.'; |
3197 | 0 | tout_len = 1; |
3198 | 0 | } |
3199 | |
|
3200 | 0 | subtree = _get_realloc_symbol(objid, objidlen, subtree, |
3201 | 0 | &tbuf, &tbuf_len, &tout_len, |
3202 | 0 | allow_realloc, &tbuf_overflow, NULL, |
3203 | 0 | &midpoint_offset); |
3204 | |
|
3205 | 0 | if (tbuf_overflow) { |
3206 | 0 | if (!*buf_overflow) { |
3207 | 0 | snmp_strcat(buf, buf_len, out_len, allow_realloc, tbuf); |
3208 | 0 | *buf_overflow = 1; |
3209 | 0 | } |
3210 | 0 | SNMP_FREE(tbuf); |
3211 | 0 | return subtree; |
3212 | 0 | } |
3213 | | |
3214 | 0 | output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT); |
3215 | 0 | if (0 == output_format) { |
3216 | 0 | output_format = NETSNMP_OID_OUTPUT_MODULE; |
3217 | 0 | } |
3218 | 0 | switch (output_format) { |
3219 | 0 | case NETSNMP_OID_OUTPUT_FULL: |
3220 | 0 | case NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC: |
3221 | 0 | case NETSNMP_OID_OUTPUT_NUMERIC: |
3222 | 0 | cp = tbuf; |
3223 | 0 | break; |
3224 | | |
3225 | 0 | case NETSNMP_OID_OUTPUT_SUFFIX: |
3226 | 0 | case NETSNMP_OID_OUTPUT_MODULE: |
3227 | 0 | for (cp = tbuf; *cp; cp++); |
3228 | |
|
3229 | 0 | if (midpoint_offset != 0) { |
3230 | 0 | cp = tbuf + midpoint_offset - 2; /* beyond the '.' */ |
3231 | 0 | } else { |
3232 | 0 | while (cp >= tbuf) { |
3233 | 0 | if (isalpha(*cp)) { |
3234 | 0 | break; |
3235 | 0 | } |
3236 | 0 | cp--; |
3237 | 0 | } |
3238 | 0 | } |
3239 | |
|
3240 | 0 | while (cp >= tbuf) { |
3241 | 0 | if (*cp == '.') { |
3242 | 0 | break; |
3243 | 0 | } |
3244 | 0 | cp--; |
3245 | 0 | } |
3246 | |
|
3247 | 0 | cp++; |
3248 | |
|
3249 | 0 | if ((NETSNMP_OID_OUTPUT_MODULE == output_format) |
3250 | 0 | && cp > tbuf) { |
3251 | 0 | char modbuf[256] = { 0 }, *mod = |
3252 | 0 | module_name(subtree->modid, modbuf); |
3253 | | |
3254 | | /* |
3255 | | * Don't add the module ID if it's just numeric (i.e. we couldn't look |
3256 | | * it up properly. |
3257 | | */ |
3258 | |
|
3259 | 0 | if (!*buf_overflow && modbuf[0] != '#') { |
3260 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, mod) || |
3261 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "::")) { |
3262 | 0 | *buf_overflow = 1; |
3263 | 0 | } |
3264 | 0 | } |
3265 | 0 | } |
3266 | 0 | break; |
3267 | | |
3268 | 0 | case NETSNMP_OID_OUTPUT_UCD: |
3269 | 0 | { |
3270 | 0 | PrefixListPtr pp = &mib_prefixes[0]; |
3271 | 0 | size_t ilen, tlen; |
3272 | 0 | const char *testcp; |
3273 | |
|
3274 | 0 | cp = tbuf; |
3275 | 0 | tlen = strlen((char *) tbuf); |
3276 | |
|
3277 | 0 | while (pp->str) { |
3278 | 0 | ilen = pp->len; |
3279 | 0 | testcp = pp->str; |
3280 | |
|
3281 | 0 | if ((tlen > ilen) && memcmp(tbuf, testcp, ilen) == 0) { |
3282 | 0 | cp += (ilen + 1); |
3283 | 0 | break; |
3284 | 0 | } |
3285 | 0 | pp++; |
3286 | 0 | } |
3287 | 0 | break; |
3288 | 0 | } |
3289 | | |
3290 | 0 | case NETSNMP_OID_OUTPUT_NONE: |
3291 | 0 | default: |
3292 | 0 | cp = NULL; |
3293 | 0 | } |
3294 | | |
3295 | 0 | if (!*buf_overflow && |
3296 | 0 | !snmp_strcat(buf, buf_len, out_len, allow_realloc, cp)) { |
3297 | 0 | *buf_overflow = 1; |
3298 | 0 | } |
3299 | 0 | SNMP_FREE(tbuf); |
3300 | 0 | return subtree; |
3301 | 0 | } |
3302 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3303 | | |
3304 | | int |
3305 | | sprint_realloc_objid(u_char ** buf, size_t * buf_len, |
3306 | | size_t * out_len, int allow_realloc, |
3307 | | const oid * objid, size_t objidlen) |
3308 | 0 | { |
3309 | 0 | int buf_overflow = 0; |
3310 | |
|
3311 | 0 | netsnmp_sprint_realloc_objid_tree(buf, buf_len, out_len, allow_realloc, |
3312 | 0 | &buf_overflow, objid, objidlen); |
3313 | 0 | return !buf_overflow; |
3314 | 0 | } |
3315 | | |
3316 | | #ifndef NETSNMP_FEATURE_REMOVE_SPRINT_OBJID |
3317 | | int |
3318 | | snprint_objid(char *buf, size_t buf_len, |
3319 | | const oid * objid, size_t objidlen) |
3320 | 0 | { |
3321 | 0 | size_t out_len = 0; |
3322 | |
|
3323 | 0 | if (sprint_realloc_objid((u_char **) & buf, &buf_len, &out_len, 0, |
3324 | 0 | objid, objidlen)) { |
3325 | 0 | return (int) out_len; |
3326 | 0 | } else { |
3327 | 0 | return -1; |
3328 | 0 | } |
3329 | 0 | } |
3330 | | #endif /* NETSNMP_FEATURE_REMOVE_SPRINT_OBJID */ |
3331 | | |
3332 | | /** |
3333 | | * Prints an oid to stdout. |
3334 | | * |
3335 | | * @param objid The oid to print |
3336 | | * @param objidlen The length of oidid. |
3337 | | */ |
3338 | | void |
3339 | | print_objid(const oid * objid, size_t objidlen) |
3340 | 0 | { /* number of subidentifiers */ |
3341 | 0 | fprint_objid(stdout, objid, objidlen); |
3342 | 0 | } |
3343 | | |
3344 | | |
3345 | | /** |
3346 | | * Prints an oid to a file descriptor. |
3347 | | * |
3348 | | * @param f The file descriptor to print to. |
3349 | | * @param objid The oid to print |
3350 | | * @param objidlen The length of oidid. |
3351 | | */ |
3352 | | void |
3353 | | fprint_objid(FILE * f, const oid * objid, size_t objidlen) |
3354 | 0 | { /* number of subidentifiers */ |
3355 | 0 | u_char *buf = NULL; |
3356 | 0 | size_t buf_len = 256, out_len = 0; |
3357 | 0 | int buf_overflow = 0; |
3358 | |
|
3359 | 0 | if ((buf = calloc(buf_len, 1)) == NULL) { |
3360 | 0 | fprintf(f, "[TRUNCATED]\n"); |
3361 | 0 | return; |
3362 | 0 | } else { |
3363 | 0 | netsnmp_sprint_realloc_objid_tree(&buf, &buf_len, &out_len, 1, |
3364 | 0 | &buf_overflow, objid, objidlen); |
3365 | 0 | if (buf_overflow) { |
3366 | 0 | fprintf(f, "%s [TRUNCATED]\n", buf); |
3367 | 0 | } else { |
3368 | 0 | fprintf(f, "%s\n", buf); |
3369 | 0 | } |
3370 | 0 | } |
3371 | | |
3372 | 0 | SNMP_FREE(buf); |
3373 | 0 | } |
3374 | | |
3375 | | int |
3376 | | sprint_realloc_variable(u_char ** buf, size_t * buf_len, |
3377 | | size_t * out_len, int allow_realloc, |
3378 | | const oid * objid, size_t objidlen, |
3379 | | const netsnmp_variable_list * variable) |
3380 | 0 | { |
3381 | 0 | int buf_overflow = 0; |
3382 | |
|
3383 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3384 | 0 | struct tree *subtree = tree_head; |
3385 | |
|
3386 | 0 | subtree = |
3387 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3388 | 0 | netsnmp_sprint_realloc_objid_tree(buf, buf_len, out_len, |
3389 | 0 | allow_realloc, &buf_overflow, |
3390 | 0 | objid, objidlen); |
3391 | |
|
3392 | 0 | if (buf_overflow) { |
3393 | 0 | return 0; |
3394 | 0 | } |
3395 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE)) { |
3396 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) { |
3397 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " = ")) { |
3398 | 0 | return 0; |
3399 | 0 | } |
3400 | 0 | } else { |
3401 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) { |
3402 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ")) { |
3403 | 0 | return 0; |
3404 | 0 | } |
3405 | 0 | } else { |
3406 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3407 | 0 | " = ")) { |
3408 | 0 | return 0; |
3409 | 0 | } |
3410 | 0 | } /* end if-else NETSNMP_DS_LIB_QUICK_PRINT */ |
3411 | 0 | } /* end if-else NETSNMP_DS_LIB_QUICKE_PRINT */ |
3412 | 0 | } else { |
3413 | 0 | *out_len = 0; |
3414 | 0 | } |
3415 | | |
3416 | 0 | if (variable->type == SNMP_NOSUCHOBJECT) { |
3417 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3418 | 0 | "No Such Object available on this agent at this OID"); |
3419 | 0 | } else if (variable->type == SNMP_NOSUCHINSTANCE) { |
3420 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3421 | 0 | "No Such Instance currently exists at this OID"); |
3422 | 0 | } else if (variable->type == SNMP_ENDOFMIBVIEW) { |
3423 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3424 | 0 | "No more variables left in this MIB View (It is past the end of the MIB tree)"); |
3425 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3426 | 0 | } else if (subtree) { |
3427 | 0 | const char *units = NULL; |
3428 | 0 | const char *hint = NULL; |
3429 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
3430 | 0 | NETSNMP_DS_LIB_DONT_PRINT_UNITS)) { |
3431 | 0 | units = subtree->units; |
3432 | 0 | } |
3433 | |
|
3434 | 0 | if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
3435 | 0 | NETSNMP_DS_LIB_NO_DISPLAY_HINT)) { |
3436 | 0 | hint = subtree->hint; |
3437 | 0 | } |
3438 | |
|
3439 | 0 | if (subtree->printomat) { |
3440 | 0 | return (*subtree->printomat) (buf, buf_len, out_len, |
3441 | 0 | allow_realloc, variable, |
3442 | 0 | subtree->enums, hint, |
3443 | 0 | units); |
3444 | 0 | } else { |
3445 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
3446 | 0 | allow_realloc, variable, |
3447 | 0 | subtree->enums, hint, |
3448 | 0 | units); |
3449 | 0 | } |
3450 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3451 | 0 | } else { |
3452 | | /* |
3453 | | * Handle rare case where tree is empty. |
3454 | | */ |
3455 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, allow_realloc, |
3456 | 0 | variable, NULL, NULL, NULL); |
3457 | 0 | } |
3458 | 0 | } |
3459 | | |
3460 | | #ifndef NETSNMP_FEATURE_REMOVE_SNPRINT_VARABLE |
3461 | | int |
3462 | | snprint_variable(char *buf, size_t buf_len, |
3463 | | const oid * objid, size_t objidlen, |
3464 | | const netsnmp_variable_list * variable) |
3465 | 0 | { |
3466 | 0 | size_t out_len = 0; |
3467 | |
|
3468 | 0 | if (sprint_realloc_variable((u_char **) & buf, &buf_len, &out_len, 0, |
3469 | 0 | objid, objidlen, variable)) { |
3470 | 0 | return (int) out_len; |
3471 | 0 | } else { |
3472 | 0 | return -1; |
3473 | 0 | } |
3474 | 0 | } |
3475 | | #endif /* NETSNMP_FEATURE_REMOVE_SNPRINT_VARABLE */ |
3476 | | |
3477 | | /** |
3478 | | * Prints a variable to stdout. |
3479 | | * |
3480 | | * @param objid The object id. |
3481 | | * @param objidlen The length of the object id. |
3482 | | * @param variable The variable to print. |
3483 | | */ |
3484 | | void |
3485 | | print_variable(const oid * objid, |
3486 | | size_t objidlen, const netsnmp_variable_list * variable) |
3487 | 0 | { |
3488 | 0 | fprint_variable(stdout, objid, objidlen, variable); |
3489 | 0 | } |
3490 | | |
3491 | | |
3492 | | /** |
3493 | | * Prints a variable to a file descriptor. |
3494 | | * |
3495 | | * @param f The file descriptor to print to. |
3496 | | * @param objid The object id. |
3497 | | * @param objidlen The length of the object id. |
3498 | | * @param variable The variable to print. |
3499 | | */ |
3500 | | void |
3501 | | fprint_variable(FILE * f, |
3502 | | const oid * objid, |
3503 | | size_t objidlen, const netsnmp_variable_list * variable) |
3504 | 0 | { |
3505 | 0 | u_char *buf = NULL; |
3506 | 0 | size_t buf_len = 256, out_len = 0; |
3507 | |
|
3508 | 0 | if ((buf = calloc(buf_len, 1)) == NULL) { |
3509 | 0 | fprintf(f, "[TRUNCATED]\n"); |
3510 | 0 | return; |
3511 | 0 | } else { |
3512 | 0 | if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1, |
3513 | 0 | objid, objidlen, variable)) { |
3514 | 0 | fprintf(f, "%s\n", buf); |
3515 | 0 | } else { |
3516 | 0 | fprintf(f, "%s [TRUNCATED]\n", buf); |
3517 | 0 | } |
3518 | 0 | } |
3519 | | |
3520 | 0 | SNMP_FREE(buf); |
3521 | 0 | } |
3522 | | |
3523 | | int |
3524 | | sprint_realloc_value(u_char ** buf, size_t * buf_len, |
3525 | | size_t * out_len, int allow_realloc, |
3526 | | const oid * objid, size_t objidlen, |
3527 | | const netsnmp_variable_list * variable) |
3528 | 0 | { |
3529 | 0 | if (variable->type == SNMP_NOSUCHOBJECT) { |
3530 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3531 | 0 | "No Such Object available on this agent at this OID"); |
3532 | 0 | } else if (variable->type == SNMP_NOSUCHINSTANCE) { |
3533 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3534 | 0 | "No Such Instance currently exists at this OID"); |
3535 | 0 | } else if (variable->type == SNMP_ENDOFMIBVIEW) { |
3536 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
3537 | 0 | "No more variables left in this MIB View (It is past the end of the MIB tree)"); |
3538 | 0 | } else { |
3539 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
3540 | 0 | const char *units = NULL; |
3541 | 0 | struct tree *subtree = tree_head; |
3542 | 0 | subtree = get_tree(objid, objidlen, subtree); |
3543 | 0 | if (subtree && !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
3544 | 0 | NETSNMP_DS_LIB_DONT_PRINT_UNITS)) { |
3545 | 0 | units = subtree->units; |
3546 | 0 | } |
3547 | 0 | if (subtree) { |
3548 | 0 | if(subtree->printomat) { |
3549 | 0 | return (*subtree->printomat) (buf, buf_len, out_len, |
3550 | 0 | allow_realloc, variable, |
3551 | 0 | subtree->enums, subtree->hint, |
3552 | 0 | units); |
3553 | 0 | } else { |
3554 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
3555 | 0 | allow_realloc, variable, |
3556 | 0 | subtree->enums, subtree->hint, |
3557 | 0 | units); |
3558 | 0 | } |
3559 | 0 | } |
3560 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
3561 | 0 | return sprint_realloc_by_type(buf, buf_len, out_len, |
3562 | 0 | allow_realloc, variable, |
3563 | 0 | NULL, NULL, NULL); |
3564 | 0 | } |
3565 | 0 | } |
3566 | | |
3567 | | #ifndef NETSNMP_FEATURE_REMOVE_SNPRINT_VALUE |
3568 | | /* used in the perl module */ |
3569 | | int |
3570 | | snprint_value(char *buf, size_t buf_len, |
3571 | | const oid * objid, size_t objidlen, |
3572 | | const netsnmp_variable_list * variable) |
3573 | 0 | { |
3574 | 0 | size_t out_len = 0; |
3575 | |
|
3576 | 0 | if (sprint_realloc_value((u_char **) & buf, &buf_len, &out_len, 0, |
3577 | 0 | objid, objidlen, variable)) { |
3578 | 0 | return (int) out_len; |
3579 | 0 | } else { |
3580 | 0 | return -1; |
3581 | 0 | } |
3582 | 0 | } |
3583 | | #endif /* NETSNMP_FEATURE_REMOVE_SNPRINT_VALUE */ |
3584 | | |
3585 | | void |
3586 | | print_value(const oid * objid, |
3587 | | size_t objidlen, const netsnmp_variable_list * variable) |
3588 | 0 | { |
3589 | 0 | fprint_value(stdout, objid, objidlen, variable); |
3590 | 0 | } |
3591 | | |
3592 | | void |
3593 | | fprint_value(FILE * f, |
3594 | | const oid * objid, |
3595 | | size_t objidlen, const netsnmp_variable_list * variable) |
3596 | 0 | { |
3597 | 0 | u_char *buf = NULL; |
3598 | 0 | size_t buf_len = 256, out_len = 0; |
3599 | |
|
3600 | 0 | if ((buf = calloc(buf_len, 1)) == NULL) { |
3601 | 0 | fprintf(f, "[TRUNCATED]\n"); |
3602 | 0 | return; |
3603 | 0 | } else { |
3604 | 0 | if (sprint_realloc_value(&buf, &buf_len, &out_len, 1, |
3605 | 0 | objid, objidlen, variable)) { |
3606 | 0 | fprintf(f, "%s\n", buf); |
3607 | 0 | } else { |
3608 | 0 | fprintf(f, "%s [TRUNCATED]\n", buf); |
3609 | 0 | } |
3610 | 0 | } |
3611 | | |
3612 | 0 | SNMP_FREE(buf); |
3613 | 0 | } |
3614 | | |
3615 | | |
3616 | | /** |
3617 | | * Takes the value in VAR and turns it into an OID segment in var->name. |
3618 | | * |
3619 | | * @param var The variable. |
3620 | | * |
3621 | | * @return SNMPERR_SUCCESS or SNMPERR_GENERR |
3622 | | */ |
3623 | | int |
3624 | | build_oid_segment(netsnmp_variable_list * var) |
3625 | 0 | { |
3626 | 0 | int i; |
3627 | 0 | uint32_t ipaddr; |
3628 | |
|
3629 | 0 | if (var->name && var->name != var->name_loc) |
3630 | 0 | SNMP_FREE(var->name); |
3631 | 0 | switch (var->type) { |
3632 | 0 | case ASN_INTEGER: |
3633 | 0 | case ASN_COUNTER: |
3634 | 0 | case ASN_GAUGE: |
3635 | 0 | case ASN_TIMETICKS: |
3636 | 0 | var->name_length = 1; |
3637 | 0 | var->name = var->name_loc; |
3638 | 0 | var->name[0] = *(var->val.integer); |
3639 | 0 | break; |
3640 | | |
3641 | 0 | case ASN_IPADDRESS: |
3642 | 0 | var->name_length = 4; |
3643 | 0 | var->name = var->name_loc; |
3644 | 0 | memcpy(&ipaddr, var->val.string, sizeof(ipaddr)); |
3645 | 0 | var->name[0] = (ipaddr >> 24) & 0xff; |
3646 | 0 | var->name[1] = (ipaddr >> 16) & 0xff; |
3647 | 0 | var->name[2] = (ipaddr >> 8) & 0xff; |
3648 | 0 | var->name[3] = (ipaddr >> 0) & 0xff; |
3649 | 0 | break; |
3650 | | |
3651 | 0 | case ASN_PRIV_IMPLIED_OBJECT_ID: |
3652 | 0 | var->name_length = var->val_len / sizeof(oid); |
3653 | 0 | if (var->name_length > (OID_LENGTH(var->name_loc))) |
3654 | 0 | var->name = (oid *) malloc(sizeof(oid) * (var->name_length)); |
3655 | 0 | else |
3656 | 0 | var->name = var->name_loc; |
3657 | 0 | if (var->name == NULL) |
3658 | 0 | return SNMPERR_GENERR; |
3659 | | |
3660 | 0 | for (i = 0; i < (int) var->name_length; i++) |
3661 | 0 | var->name[i] = var->val.objid[i]; |
3662 | 0 | break; |
3663 | | |
3664 | 0 | case ASN_OBJECT_ID: |
3665 | 0 | var->name_length = var->val_len / sizeof(oid) + 1; |
3666 | 0 | if (var->name_length > (OID_LENGTH(var->name_loc))) |
3667 | 0 | var->name = (oid *) malloc(sizeof(oid) * (var->name_length)); |
3668 | 0 | else |
3669 | 0 | var->name = var->name_loc; |
3670 | 0 | if (var->name == NULL) |
3671 | 0 | return SNMPERR_GENERR; |
3672 | | |
3673 | 0 | var->name[0] = var->name_length - 1; |
3674 | 0 | for (i = 0; i < (int) var->name_length - 1; i++) |
3675 | 0 | var->name[i + 1] = var->val.objid[i]; |
3676 | 0 | break; |
3677 | | |
3678 | 0 | case ASN_PRIV_IMPLIED_OCTET_STR: |
3679 | 0 | var->name_length = var->val_len; |
3680 | 0 | if (var->name_length > (OID_LENGTH(var->name_loc))) |
3681 | 0 | var->name = (oid *) malloc(sizeof(oid) * (var->name_length)); |
3682 | 0 | else |
3683 | 0 | var->name = var->name_loc; |
3684 | 0 | if (var->name == NULL) |
3685 | 0 | return SNMPERR_GENERR; |
3686 | | |
3687 | 0 | for (i = 0; i < (int) var->val_len; i++) |
3688 | 0 | var->name[i] = (oid) var->val.string[i]; |
3689 | 0 | break; |
3690 | | |
3691 | 0 | case ASN_OPAQUE: |
3692 | 0 | case ASN_OCTET_STR: |
3693 | 0 | var->name_length = var->val_len + 1; |
3694 | 0 | if (var->name_length > (OID_LENGTH(var->name_loc))) |
3695 | 0 | var->name = (oid *) malloc(sizeof(oid) * (var->name_length)); |
3696 | 0 | else |
3697 | 0 | var->name = var->name_loc; |
3698 | 0 | if (var->name == NULL) |
3699 | 0 | return SNMPERR_GENERR; |
3700 | | |
3701 | 0 | var->name[0] = (oid) var->val_len; |
3702 | 0 | for (i = 0; i < (int) var->val_len; i++) |
3703 | 0 | var->name[i + 1] = (oid) var->val.string[i]; |
3704 | 0 | break; |
3705 | | |
3706 | 0 | default: |
3707 | 0 | DEBUGMSGTL(("build_oid_segment", |
3708 | 0 | "invalid asn type: %d\n", var->type)); |
3709 | 0 | return SNMPERR_GENERR; |
3710 | 0 | } |
3711 | | |
3712 | 0 | if (var->name_length > MAX_OID_LEN) { |
3713 | 0 | DEBUGMSGTL(("build_oid_segment", |
3714 | 0 | "Something terribly wrong, namelen = %lu\n", |
3715 | 0 | (unsigned long)var->name_length)); |
3716 | 0 | return SNMPERR_GENERR; |
3717 | 0 | } |
3718 | | |
3719 | 0 | return SNMPERR_SUCCESS; |
3720 | 0 | } |
3721 | | |
3722 | | |
3723 | | /** |
3724 | | * Concatenate a prefix and the OIDs of a variable list. |
3725 | | * |
3726 | | * @param[out] in Output buffer. |
3727 | | * @param[in] in_len Maximum number of OID components that fit in @p in. |
3728 | | * @param[out] out_len Number of OID components of the result. |
3729 | | * @param[in] prefix OID to be copied to the start of the output buffer. |
3730 | | * @param[in] prefix_len Number of OID components to copy from @p prefix. |
3731 | | * @param[in,out] indexes Variable list for which var->name should be set |
3732 | | * for each variable var in the list and whose OIDs |
3733 | | * should be appended to @p in. |
3734 | | */ |
3735 | | int |
3736 | | build_oid_noalloc(oid * in, size_t in_len, size_t * out_len, |
3737 | | const oid * prefix, size_t prefix_len, |
3738 | | netsnmp_variable_list * indexes) |
3739 | 0 | { |
3740 | 0 | netsnmp_variable_list *var; |
3741 | |
|
3742 | 0 | if (prefix) { |
3743 | 0 | if (in_len < prefix_len) |
3744 | 0 | return SNMPERR_GENERR; |
3745 | 0 | memcpy(in, prefix, prefix_len * sizeof(oid)); |
3746 | 0 | *out_len = prefix_len; |
3747 | 0 | } else { |
3748 | 0 | *out_len = 0; |
3749 | 0 | } |
3750 | | |
3751 | 0 | for (var = indexes; var != NULL; var = var->next_variable) { |
3752 | 0 | if (build_oid_segment(var) != SNMPERR_SUCCESS) |
3753 | 0 | return SNMPERR_GENERR; |
3754 | 0 | if (var->name_length + *out_len <= in_len) { |
3755 | 0 | memcpy(&(in[*out_len]), var->name, |
3756 | 0 | sizeof(oid) * var->name_length); |
3757 | 0 | *out_len += var->name_length; |
3758 | 0 | } else { |
3759 | 0 | return SNMPERR_GENERR; |
3760 | 0 | } |
3761 | 0 | } |
3762 | | |
3763 | 0 | DEBUGMSGTL(("build_oid_noalloc", "generated: ")); |
3764 | 0 | DEBUGMSGOID(("build_oid_noalloc", in, *out_len)); |
3765 | 0 | DEBUGMSG(("build_oid_noalloc", "\n")); |
3766 | 0 | return SNMPERR_SUCCESS; |
3767 | 0 | } |
3768 | | |
3769 | | int |
3770 | | build_oid(oid ** out, size_t * out_len, |
3771 | | oid * prefix, size_t prefix_len, netsnmp_variable_list * indexes) |
3772 | 0 | { |
3773 | 0 | oid tmpout[MAX_OID_LEN]; |
3774 | | |
3775 | | /* |
3776 | | * xxx-rks: inefficient. try only building segments to find index len: |
3777 | | * for (var = indexes; var != NULL; var = var->next_variable) { |
3778 | | * if (build_oid_segment(var) != SNMPERR_SUCCESS) |
3779 | | * return SNMPERR_GENERR; |
3780 | | * *out_len += var->name_length; |
3781 | | * |
3782 | | * then see if it fits in existing buffer, or realloc buffer. |
3783 | | */ |
3784 | 0 | if (build_oid_noalloc(tmpout, sizeof(tmpout) / sizeof(tmpout[0]), out_len, |
3785 | 0 | prefix, prefix_len, indexes) != SNMPERR_SUCCESS) |
3786 | 0 | return SNMPERR_GENERR; |
3787 | | |
3788 | | /** xxx-rks: should free previous value? */ |
3789 | 0 | snmp_clone_mem((void **) out, (void *) tmpout, *out_len * sizeof(oid)); |
3790 | |
|
3791 | 0 | return SNMPERR_SUCCESS; |
3792 | 0 | } |
3793 | | |
3794 | | /* |
3795 | | * vblist_out must contain a pre-allocated string of variables into |
3796 | | * which indexes can be extracted based on the previously existing |
3797 | | * types in the variable chain |
3798 | | * returns: |
3799 | | * SNMPERR_GENERR on error |
3800 | | * SNMPERR_SUCCESS on success |
3801 | | */ |
3802 | | |
3803 | | int |
3804 | | parse_oid_indexes(oid * oidIndex, size_t oidLen, |
3805 | | netsnmp_variable_list * data) |
3806 | 0 | { |
3807 | 0 | netsnmp_variable_list *var = data; |
3808 | |
|
3809 | 0 | while (var && oidLen > 0) { |
3810 | |
|
3811 | 0 | if (parse_one_oid_index(&oidIndex, &oidLen, var, 0) != |
3812 | 0 | SNMPERR_SUCCESS) |
3813 | 0 | break; |
3814 | | |
3815 | 0 | var = var->next_variable; |
3816 | 0 | } |
3817 | |
|
3818 | 0 | if (var != NULL || oidLen != 0) |
3819 | 0 | return SNMPERR_GENERR; |
3820 | 0 | return SNMPERR_SUCCESS; |
3821 | 0 | } |
3822 | | |
3823 | | |
3824 | | int |
3825 | | parse_one_oid_index(oid ** oidStart, size_t * oidLen, |
3826 | | netsnmp_variable_list * data, int complete) |
3827 | 0 | { |
3828 | 0 | netsnmp_variable_list *var = data; |
3829 | 0 | oid tmpout[MAX_OID_LEN]; |
3830 | 0 | unsigned int i; |
3831 | 0 | unsigned int uitmp = 0; |
3832 | |
|
3833 | 0 | oid *oidIndex = *oidStart; |
3834 | |
|
3835 | 0 | if (var == NULL || ((*oidLen == 0) && (complete == 0))) |
3836 | 0 | return SNMPERR_GENERR; |
3837 | 0 | else { |
3838 | 0 | switch (var->type) { |
3839 | 0 | case ASN_INTEGER: |
3840 | 0 | case ASN_COUNTER: |
3841 | 0 | case ASN_GAUGE: |
3842 | 0 | case ASN_TIMETICKS: |
3843 | 0 | if (*oidLen) { |
3844 | 0 | snmp_set_var_value(var, (u_char *) oidIndex++, |
3845 | 0 | sizeof(oid)); |
3846 | 0 | --(*oidLen); |
3847 | 0 | } else { |
3848 | 0 | snmp_set_var_value(var, (u_char *) oidLen, sizeof(long)); |
3849 | 0 | } |
3850 | 0 | DEBUGMSGTL(("parse_oid_indexes", |
3851 | 0 | "Parsed int(%d): %ld\n", var->type, |
3852 | 0 | *var->val.integer)); |
3853 | 0 | break; |
3854 | | |
3855 | 0 | case ASN_IPADDRESS: |
3856 | 0 | if ((4 > *oidLen) && (complete == 0)) |
3857 | 0 | return SNMPERR_GENERR; |
3858 | | |
3859 | 0 | for (i = 0; i < 4 && i < *oidLen; ++i) { |
3860 | 0 | if (oidIndex[i] > 255) { |
3861 | 0 | DEBUGMSGTL(("parse_oid_indexes", |
3862 | 0 | "illegal oid in index: %" NETSNMP_PRIo "d\n", |
3863 | 0 | oidIndex[0])); |
3864 | 0 | return SNMPERR_GENERR; /* sub-identifier too large */ |
3865 | 0 | } |
3866 | 0 | uitmp = uitmp + (oidIndex[i] << (8*(3-i))); |
3867 | 0 | } |
3868 | 0 | if (4 > (int) (*oidLen)) { |
3869 | 0 | oidIndex += *oidLen; |
3870 | 0 | (*oidLen) = 0; |
3871 | 0 | } else { |
3872 | 0 | oidIndex += 4; |
3873 | 0 | (*oidLen) -= 4; |
3874 | 0 | } |
3875 | 0 | uitmp = htonl(uitmp); /* put it in proper order for byte copies */ |
3876 | 0 | uitmp = |
3877 | 0 | snmp_set_var_value(var, (u_char *) &uitmp, 4); |
3878 | 0 | DEBUGMSGTL(("parse_oid_indexes", |
3879 | 0 | "Parsed ipaddr(%d): %d.%d.%d.%d\n", var->type, |
3880 | 0 | var->val.string[0], var->val.string[1], |
3881 | 0 | var->val.string[2], var->val.string[3])); |
3882 | 0 | break; |
3883 | | |
3884 | 0 | case ASN_OBJECT_ID: |
3885 | 0 | case ASN_PRIV_IMPLIED_OBJECT_ID: |
3886 | 0 | if (var->type == ASN_PRIV_IMPLIED_OBJECT_ID) { |
3887 | | /* |
3888 | | * might not be implied, might be fixed len. check if |
3889 | | * caller set up val len, and use it if they did. |
3890 | | */ |
3891 | 0 | if (0 == var->val_len) |
3892 | 0 | uitmp = *oidLen; |
3893 | 0 | else { |
3894 | 0 | DEBUGMSGTL(("parse_oid_indexes:fix", "fixed len oid\n")); |
3895 | 0 | uitmp = var->val_len; |
3896 | 0 | } |
3897 | 0 | } else { |
3898 | 0 | if (*oidLen) { |
3899 | 0 | uitmp = *oidIndex++; |
3900 | 0 | --(*oidLen); |
3901 | 0 | } else { |
3902 | 0 | uitmp = 0; |
3903 | 0 | } |
3904 | 0 | if ((uitmp > *oidLen) && (complete == 0)) |
3905 | 0 | return SNMPERR_GENERR; |
3906 | 0 | } |
3907 | | |
3908 | 0 | if (uitmp > MAX_OID_LEN) |
3909 | 0 | return SNMPERR_GENERR; /* too big and illegal */ |
3910 | | |
3911 | 0 | if (uitmp > *oidLen) { |
3912 | 0 | memcpy(tmpout, oidIndex, sizeof(oid) * (*oidLen)); |
3913 | 0 | memset(&tmpout[*oidLen], 0x00, |
3914 | 0 | sizeof(oid) * (uitmp - *oidLen)); |
3915 | 0 | snmp_set_var_value(var, (u_char *) tmpout, |
3916 | 0 | sizeof(oid) * uitmp); |
3917 | 0 | oidIndex += *oidLen; |
3918 | 0 | (*oidLen) = 0; |
3919 | 0 | } else { |
3920 | 0 | snmp_set_var_value(var, (u_char *) oidIndex, |
3921 | 0 | sizeof(oid) * uitmp); |
3922 | 0 | oidIndex += uitmp; |
3923 | 0 | (*oidLen) -= uitmp; |
3924 | 0 | } |
3925 | |
|
3926 | 0 | DEBUGMSGTL(("parse_oid_indexes", "Parsed oid: ")); |
3927 | 0 | DEBUGMSGOID(("parse_oid_indexes", |
3928 | 0 | var->val.objid, var->val_len / sizeof(oid))); |
3929 | 0 | DEBUGMSG(("parse_oid_indexes", "\n")); |
3930 | 0 | break; |
3931 | | |
3932 | 0 | case ASN_OPAQUE: |
3933 | 0 | case ASN_OCTET_STR: |
3934 | 0 | case ASN_PRIV_IMPLIED_OCTET_STR: |
3935 | 0 | if (var->type == ASN_PRIV_IMPLIED_OCTET_STR) { |
3936 | | /* |
3937 | | * might not be implied, might be fixed len. check if |
3938 | | * caller set up val len, and use it if they did. |
3939 | | */ |
3940 | 0 | if (0 == var->val_len) |
3941 | 0 | uitmp = *oidLen; |
3942 | 0 | else { |
3943 | 0 | DEBUGMSGTL(("parse_oid_indexes:fix", "fixed len str\n")); |
3944 | 0 | uitmp = var->val_len; |
3945 | 0 | } |
3946 | 0 | } else { |
3947 | 0 | if (*oidLen) { |
3948 | 0 | uitmp = *oidIndex++; |
3949 | 0 | --(*oidLen); |
3950 | 0 | } else { |
3951 | 0 | uitmp = 0; |
3952 | 0 | } |
3953 | 0 | if ((uitmp > *oidLen) && (complete == 0)) |
3954 | 0 | return SNMPERR_GENERR; |
3955 | 0 | } |
3956 | | |
3957 | | /* |
3958 | | * we handle this one ourselves since we don't have |
3959 | | * pre-allocated memory to copy from using |
3960 | | * snmp_set_var_value() |
3961 | | */ |
3962 | | |
3963 | 0 | if (uitmp == 0) |
3964 | 0 | break; /* zero length strings shouldn't malloc */ |
3965 | | |
3966 | 0 | if (uitmp > MAX_OID_LEN) |
3967 | 0 | return SNMPERR_GENERR; /* too big and illegal */ |
3968 | | |
3969 | | /* |
3970 | | * malloc by size+1 to allow a null to be appended. |
3971 | | */ |
3972 | 0 | var->val_len = uitmp; |
3973 | 0 | var->val.string = calloc(1, uitmp + 1); |
3974 | 0 | if (var->val.string == NULL) |
3975 | 0 | return SNMPERR_GENERR; |
3976 | | |
3977 | 0 | if ((size_t)uitmp > (*oidLen)) { |
3978 | 0 | for (i = 0; i < *oidLen; ++i) |
3979 | 0 | var->val.string[i] = (u_char) * oidIndex++; |
3980 | 0 | for (i = *oidLen; i < uitmp; ++i) |
3981 | 0 | var->val.string[i] = '\0'; |
3982 | 0 | (*oidLen) = 0; |
3983 | 0 | } else { |
3984 | 0 | for (i = 0; i < uitmp; ++i) |
3985 | 0 | var->val.string[i] = (u_char) * oidIndex++; |
3986 | 0 | (*oidLen) -= uitmp; |
3987 | 0 | } |
3988 | 0 | var->val.string[uitmp] = '\0'; |
3989 | |
|
3990 | 0 | DEBUGMSGTL(("parse_oid_indexes", |
3991 | 0 | "Parsed str(%d): %s\n", var->type, |
3992 | 0 | var->val.string)); |
3993 | 0 | break; |
3994 | | |
3995 | 0 | default: |
3996 | 0 | DEBUGMSGTL(("parse_oid_indexes", |
3997 | 0 | "invalid asn type: %d\n", var->type)); |
3998 | 0 | return SNMPERR_GENERR; |
3999 | 0 | } |
4000 | 0 | } |
4001 | 0 | (*oidStart) = oidIndex; |
4002 | 0 | return SNMPERR_SUCCESS; |
4003 | 0 | } |
4004 | | |
4005 | | /* |
4006 | | * dump_realloc_oid_to_inetaddress: |
4007 | | * return 0 for failure, |
4008 | | * return 1 for success, |
4009 | | * return 2 for not handled |
4010 | | */ |
4011 | | |
4012 | | int |
4013 | | dump_realloc_oid_to_inetaddress(const int addr_type, const oid * objid, size_t objidlen, |
4014 | | u_char ** buf, size_t * buf_len, |
4015 | | size_t * out_len, int allow_realloc, |
4016 | | char quotechar) |
4017 | 0 | { |
4018 | 0 | int i, len; |
4019 | 0 | char intbuf[64], *p; |
4020 | 0 | char *const end = intbuf + sizeof(intbuf); |
4021 | 0 | unsigned char *zc; |
4022 | 0 | unsigned long zone; |
4023 | |
|
4024 | 0 | if (!buf) |
4025 | 0 | return 1; |
4026 | | |
4027 | 0 | for (i = 0; i < objidlen; i++) |
4028 | 0 | if (objid[i] > 255) |
4029 | 0 | return 2; |
4030 | | |
4031 | 0 | p = intbuf; |
4032 | 0 | *p++ = quotechar; |
4033 | |
|
4034 | 0 | switch (addr_type) { |
4035 | 0 | case IPV4: |
4036 | 0 | case IPV4Z: |
4037 | 0 | if ((addr_type == IPV4 && objidlen != 4) || |
4038 | 0 | (addr_type == IPV4Z && objidlen != 8)) |
4039 | 0 | return 2; |
4040 | | |
4041 | 0 | len = snprintf(p, end - p, "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u." |
4042 | 0 | "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u", |
4043 | 0 | objid[0], objid[1], objid[2], objid[3]); |
4044 | 0 | p += len; |
4045 | 0 | if (p >= end) |
4046 | 0 | return 2; |
4047 | 0 | if (addr_type == IPV4Z) { |
4048 | 0 | zc = (unsigned char*)&zone; |
4049 | 0 | zc[0] = objid[4]; |
4050 | 0 | zc[1] = objid[5]; |
4051 | 0 | zc[2] = objid[6]; |
4052 | 0 | zc[3] = objid[7]; |
4053 | 0 | zone = ntohl(zone); |
4054 | 0 | len = snprintf(p, end - p, "%%%lu", zone); |
4055 | 0 | p += len; |
4056 | 0 | if (p >= end) |
4057 | 0 | return 2; |
4058 | 0 | } |
4059 | | |
4060 | 0 | break; |
4061 | | |
4062 | 0 | case IPV6: |
4063 | 0 | case IPV6Z: |
4064 | 0 | if ((addr_type == IPV6 && objidlen != 16) || |
4065 | 0 | (addr_type == IPV6Z && objidlen != 20)) |
4066 | 0 | return 2; |
4067 | | |
4068 | 0 | len = 0; |
4069 | 0 | for (i = 0; i < 16; i ++) { |
4070 | 0 | len = snprintf(p, end - p, "%s%02" NETSNMP_PRIo "x", i ? ":" : "", |
4071 | 0 | objid[i]); |
4072 | 0 | p += len; |
4073 | 0 | if (p >= end) |
4074 | 0 | return 2; |
4075 | 0 | } |
4076 | | |
4077 | 0 | if (addr_type == IPV6Z) { |
4078 | 0 | zc = (unsigned char*)&zone; |
4079 | 0 | zc[0] = objid[16]; |
4080 | 0 | zc[1] = objid[17]; |
4081 | 0 | zc[2] = objid[18]; |
4082 | 0 | zc[3] = objid[19]; |
4083 | 0 | zone = ntohl(zone); |
4084 | 0 | len = snprintf(p, end - p, "%%%lu", zone); |
4085 | 0 | p += len; |
4086 | 0 | if (p >= end) |
4087 | 0 | return 2; |
4088 | 0 | } |
4089 | | |
4090 | 0 | break; |
4091 | | |
4092 | 0 | case DNS: |
4093 | 0 | default: |
4094 | | /* DNS can just be handled by dump_realloc_oid_to_string() */ |
4095 | 0 | return 2; |
4096 | 0 | } |
4097 | | |
4098 | 0 | *p++ = quotechar; |
4099 | 0 | if (p >= end) |
4100 | 0 | return 2; |
4101 | | |
4102 | 0 | *p++ = '\0'; |
4103 | 0 | if (p >= end) |
4104 | 0 | return 2; |
4105 | | |
4106 | 0 | return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf); |
4107 | 0 | } |
4108 | | |
4109 | | int |
4110 | | dump_realloc_oid_to_string(const oid * objid, size_t objidlen, |
4111 | | u_char ** buf, size_t * buf_len, |
4112 | | size_t * out_len, int allow_realloc, |
4113 | | char quotechar) |
4114 | 0 | { |
4115 | 0 | if (buf) { |
4116 | 0 | int i, alen; |
4117 | |
|
4118 | 0 | for (i = 0, alen = 0; i < (int) objidlen; i++) { |
4119 | 0 | oid tst = objid[i]; |
4120 | 0 | if ((tst > 254) || (!isprint(tst))) { |
4121 | 0 | tst = (oid) '.'; |
4122 | 0 | } |
4123 | |
|
4124 | 0 | if (alen == 0) { |
4125 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) { |
4126 | 0 | while ((*out_len + 2) >= *buf_len) { |
4127 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
4128 | 0 | return 0; |
4129 | 0 | } |
4130 | 0 | } |
4131 | 0 | *(*buf + *out_len) = '\\'; |
4132 | 0 | (*out_len)++; |
4133 | 0 | } |
4134 | 0 | while ((*out_len + 2) >= *buf_len) { |
4135 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
4136 | 0 | return 0; |
4137 | 0 | } |
4138 | 0 | } |
4139 | 0 | *(*buf + *out_len) = quotechar; |
4140 | 0 | (*out_len)++; |
4141 | 0 | } |
4142 | | |
4143 | 0 | while ((*out_len + 2) >= *buf_len) { |
4144 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
4145 | 0 | return 0; |
4146 | 0 | } |
4147 | 0 | } |
4148 | 0 | *(*buf + *out_len) = (char) tst; |
4149 | 0 | (*out_len)++; |
4150 | 0 | alen++; |
4151 | 0 | } |
4152 | | |
4153 | 0 | if (alen) { |
4154 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) { |
4155 | 0 | while ((*out_len + 2) >= *buf_len) { |
4156 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
4157 | 0 | return 0; |
4158 | 0 | } |
4159 | 0 | } |
4160 | 0 | *(*buf + *out_len) = '\\'; |
4161 | 0 | (*out_len)++; |
4162 | 0 | } |
4163 | 0 | while ((*out_len + 2) >= *buf_len) { |
4164 | 0 | if (!(allow_realloc && snmp_realloc(buf, buf_len))) { |
4165 | 0 | return 0; |
4166 | 0 | } |
4167 | 0 | } |
4168 | 0 | *(*buf + *out_len) = quotechar; |
4169 | 0 | (*out_len)++; |
4170 | 0 | } |
4171 | | |
4172 | 0 | *(*buf + *out_len) = '\0'; |
4173 | 0 | } |
4174 | | |
4175 | 0 | return 1; |
4176 | 0 | } |
4177 | | |
4178 | | void |
4179 | | _oid_finish_printing(const oid * objid, size_t objidlen, |
4180 | | u_char ** buf, size_t * buf_len, size_t * out_len, |
4181 | 0 | int allow_realloc, int *buf_overflow) { |
4182 | 0 | char intbuf[64]; |
4183 | 0 | if (*buf != NULL && *(*buf + *out_len - 1) != '.') { |
4184 | 0 | if (!*buf_overflow && !snmp_cstrcat(buf, buf_len, out_len, |
4185 | 0 | allow_realloc, ".")) { |
4186 | 0 | *buf_overflow = 1; |
4187 | 0 | } |
4188 | 0 | } |
4189 | |
|
4190 | 0 | while (objidlen-- > 0) { /* output rest of name, uninterpreted */ |
4191 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u.", *objid++); |
4192 | 0 | if (!*buf_overflow && !snmp_cstrcat(buf, buf_len, out_len, |
4193 | 0 | allow_realloc, intbuf)) { |
4194 | 0 | *buf_overflow = 1; |
4195 | 0 | } |
4196 | 0 | } |
4197 | |
|
4198 | 0 | if (*buf != NULL) { |
4199 | 0 | *(*buf + *out_len - 1) = '\0'; /* remove trailing dot */ |
4200 | 0 | *out_len = *out_len - 1; |
4201 | 0 | } |
4202 | 0 | } |
4203 | | |
4204 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
4205 | | static void |
4206 | | _get_realloc_symbol_octet_string(size_t numids, const oid * objid, |
4207 | | u_char ** buf, size_t * buf_len, |
4208 | | size_t * out_len, int allow_realloc, |
4209 | | int *buf_overflow, struct tree* tp) |
4210 | 0 | { |
4211 | 0 | netsnmp_variable_list var = { 0 }; |
4212 | 0 | u_char buffer[1024]; |
4213 | 0 | size_t i; |
4214 | |
|
4215 | 0 | if (numids > sizeof(buffer)) |
4216 | 0 | numids = sizeof(buffer); |
4217 | 0 | for (i = 0; i < numids; i++) |
4218 | 0 | buffer[i] = (u_char) objid[i]; |
4219 | 0 | var.type = ASN_OCTET_STR; |
4220 | 0 | var.val.string = buffer; |
4221 | 0 | var.val_len = numids; |
4222 | 0 | if (!*buf_overflow) { |
4223 | 0 | if (!sprint_realloc_octet_string(buf, buf_len, out_len, |
4224 | 0 | allow_realloc, &var, |
4225 | 0 | NULL, tp->hint, |
4226 | 0 | NULL)) { |
4227 | 0 | *buf_overflow = 1; |
4228 | 0 | } |
4229 | 0 | } |
4230 | 0 | } |
4231 | | |
4232 | | static struct tree * |
4233 | | _get_realloc_symbol(const oid * objid, size_t objidlen, |
4234 | | struct tree *subtree, |
4235 | | u_char ** buf, size_t * buf_len, size_t * out_len, |
4236 | | int allow_realloc, int *buf_overflow, |
4237 | | struct index_list *in_dices, size_t * end_of_known) |
4238 | 0 | { |
4239 | 0 | struct tree *return_tree = NULL; |
4240 | 0 | int extended_index = |
4241 | 0 | netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX); |
4242 | 0 | int output_format = |
4243 | 0 | netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT); |
4244 | 0 | char intbuf[64]; |
4245 | 0 | struct tree *orgtree = subtree; |
4246 | |
|
4247 | 0 | if (!objid || !buf) { |
4248 | 0 | return NULL; |
4249 | 0 | } |
4250 | | |
4251 | 0 | for (; subtree; subtree = subtree->next_peer) { |
4252 | 0 | if (*objid == subtree->subid) { |
4253 | 0 | while (subtree->next_peer && subtree->next_peer->subid == *objid) |
4254 | 0 | subtree = subtree->next_peer; |
4255 | 0 | if (subtree->indexes) { |
4256 | 0 | in_dices = subtree->indexes; |
4257 | 0 | } else if (subtree->augments) { |
4258 | 0 | struct tree *tp2 = |
4259 | 0 | find_tree_node(subtree->augments, -1); |
4260 | 0 | if (tp2) { |
4261 | 0 | in_dices = tp2->indexes; |
4262 | 0 | } |
4263 | 0 | } |
4264 | |
|
4265 | 0 | if (!strncmp(subtree->label, ANON, ANON_LEN) || |
4266 | 0 | (NETSNMP_OID_OUTPUT_NUMERIC == output_format)) { |
4267 | 0 | snprintf(intbuf, sizeof(intbuf), "%lu", subtree->subid); |
4268 | 0 | if (!*buf_overflow && !snmp_cstrcat(buf, buf_len, out_len, |
4269 | 0 | allow_realloc, intbuf)) { |
4270 | 0 | *buf_overflow = 1; |
4271 | 0 | } |
4272 | 0 | } else { |
4273 | 0 | if (!*buf_overflow && |
4274 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4275 | 0 | subtree->label)) { |
4276 | 0 | *buf_overflow = 1; |
4277 | 0 | } |
4278 | 0 | if (output_format == NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC) { |
4279 | 0 | snprintf(intbuf, sizeof intbuf, "(%lu)", subtree->subid); |
4280 | 0 | if (!*buf_overflow && |
4281 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4282 | 0 | intbuf)) { |
4283 | 0 | *buf_overflow = 1; |
4284 | 0 | } |
4285 | 0 | } |
4286 | 0 | } |
4287 | |
|
4288 | 0 | if (objidlen > 1) { |
4289 | 0 | if (!*buf_overflow && |
4290 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, ".")) { |
4291 | 0 | *buf_overflow = 1; |
4292 | 0 | } |
4293 | |
|
4294 | 0 | return_tree = _get_realloc_symbol(objid + 1, objidlen - 1, |
4295 | 0 | subtree->child_list, |
4296 | 0 | buf, buf_len, out_len, |
4297 | 0 | allow_realloc, |
4298 | 0 | buf_overflow, in_dices, |
4299 | 0 | end_of_known); |
4300 | 0 | } |
4301 | |
|
4302 | 0 | if (return_tree != NULL) { |
4303 | 0 | return return_tree; |
4304 | 0 | } else { |
4305 | 0 | return subtree; |
4306 | 0 | } |
4307 | 0 | } |
4308 | 0 | } |
4309 | | |
4310 | 0 | if (end_of_known) { |
4311 | 0 | *end_of_known = *out_len; |
4312 | 0 | } |
4313 | | |
4314 | | /* |
4315 | | * Subtree not found. |
4316 | | */ |
4317 | |
|
4318 | 0 | if (orgtree && in_dices && objidlen > 0) { |
4319 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u.", *objid); |
4320 | 0 | if (!*buf_overflow |
4321 | 0 | && !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf)) { |
4322 | 0 | *buf_overflow = 1; |
4323 | 0 | } |
4324 | 0 | objid++; |
4325 | 0 | objidlen--; |
4326 | 0 | } |
4327 | |
|
4328 | 0 | while (in_dices && (objidlen > 0) && |
4329 | 0 | (NETSNMP_OID_OUTPUT_NUMERIC != output_format) && |
4330 | 0 | !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS)) { |
4331 | 0 | size_t numids; |
4332 | 0 | struct tree *tp; |
4333 | |
|
4334 | 0 | tp = find_tree_node(in_dices->ilabel, -1); |
4335 | |
|
4336 | 0 | if (!tp) { |
4337 | | /* |
4338 | | * Can't find an index in the mib tree. Bail. |
4339 | | */ |
4340 | 0 | goto finish_it; |
4341 | 0 | } |
4342 | | |
4343 | 0 | if (extended_index) { |
4344 | 0 | if (*buf != NULL && *(*buf + *out_len - 1) == '.') { |
4345 | 0 | (*out_len)--; |
4346 | 0 | } |
4347 | 0 | if (!*buf_overflow && |
4348 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "[")) { |
4349 | 0 | *buf_overflow = 1; |
4350 | 0 | } |
4351 | 0 | } |
4352 | |
|
4353 | 0 | switch (tp->type) { |
4354 | 0 | case TYPE_OCTETSTR: |
4355 | 0 | if (extended_index && tp->hint) { |
4356 | 0 | if (in_dices->isimplied) { |
4357 | 0 | numids = objidlen; |
4358 | 0 | if (numids > objidlen) |
4359 | 0 | goto finish_it; |
4360 | 0 | } else if (tp->ranges && !tp->ranges->next |
4361 | 0 | && tp->ranges->low == tp->ranges->high) { |
4362 | 0 | numids = tp->ranges->low; |
4363 | 0 | if (numids > objidlen) |
4364 | 0 | goto finish_it; |
4365 | 0 | } else { |
4366 | 0 | numids = *objid; |
4367 | 0 | if (numids >= objidlen) |
4368 | 0 | goto finish_it; |
4369 | 0 | objid++; |
4370 | 0 | objidlen--; |
4371 | 0 | } |
4372 | 0 | if (numids > objidlen) |
4373 | 0 | goto finish_it; |
4374 | 0 | _get_realloc_symbol_octet_string(numids, objid, buf, buf_len, |
4375 | 0 | out_len, allow_realloc, |
4376 | 0 | buf_overflow, tp); |
4377 | 0 | } else if (in_dices->isimplied) { |
4378 | 0 | numids = objidlen; |
4379 | 0 | if (numids > objidlen) |
4380 | 0 | goto finish_it; |
4381 | | |
4382 | 0 | if (!*buf_overflow) { |
4383 | 0 | if (!dump_realloc_oid_to_string |
4384 | 0 | (objid, numids, buf, buf_len, out_len, |
4385 | 0 | allow_realloc, '\'')) { |
4386 | 0 | *buf_overflow = 1; |
4387 | 0 | } |
4388 | 0 | } |
4389 | 0 | } else if (tp->ranges && !tp->ranges->next |
4390 | 0 | && tp->ranges->low == tp->ranges->high) { |
4391 | | /* |
4392 | | * a fixed-length octet string |
4393 | | */ |
4394 | 0 | numids = tp->ranges->low; |
4395 | 0 | if (numids > objidlen) |
4396 | 0 | goto finish_it; |
4397 | | |
4398 | 0 | if (!*buf_overflow) { |
4399 | 0 | if (!dump_realloc_oid_to_string |
4400 | 0 | (objid, numids, buf, buf_len, out_len, |
4401 | 0 | allow_realloc, '\'')) { |
4402 | 0 | *buf_overflow = 1; |
4403 | 0 | } |
4404 | 0 | } |
4405 | 0 | } else { |
4406 | 0 | numids = (size_t) * objid + 1; |
4407 | 0 | if (numids > objidlen) |
4408 | 0 | goto finish_it; |
4409 | 0 | if (numids == 1) { |
4410 | 0 | if (netsnmp_ds_get_boolean |
4411 | 0 | (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) { |
4412 | 0 | if (!*buf_overflow && |
4413 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4414 | 0 | "\\")) { |
4415 | 0 | *buf_overflow = 1; |
4416 | 0 | } |
4417 | 0 | } |
4418 | 0 | if (!*buf_overflow && |
4419 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4420 | 0 | "\"")) { |
4421 | 0 | *buf_overflow = 1; |
4422 | 0 | } |
4423 | 0 | if (netsnmp_ds_get_boolean |
4424 | 0 | (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) { |
4425 | 0 | if (!*buf_overflow && |
4426 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4427 | 0 | "\\")) { |
4428 | 0 | *buf_overflow = 1; |
4429 | 0 | } |
4430 | 0 | } |
4431 | 0 | if (!*buf_overflow && |
4432 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4433 | 0 | "\"")) { |
4434 | 0 | *buf_overflow = 1; |
4435 | 0 | } |
4436 | 0 | } else { |
4437 | 0 | if (!*buf_overflow) { |
4438 | 0 | struct tree * next_peer; |
4439 | 0 | int normal_handling = 1; |
4440 | |
|
4441 | 0 | if (tp->next_peer) { |
4442 | 0 | next_peer = tp->next_peer; |
4443 | 0 | } |
4444 | | |
4445 | | /* Try handling the InetAddress in the OID, in case of failure, |
4446 | | * use the normal_handling. |
4447 | | */ |
4448 | 0 | if (tp->next_peer && |
4449 | 0 | tp->tc_index != -1 && |
4450 | 0 | next_peer->tc_index != -1 && |
4451 | 0 | strcmp(get_tc_descriptor(tp->tc_index), "InetAddress") == 0 && |
4452 | 0 | strcmp(get_tc_descriptor(next_peer->tc_index), |
4453 | 0 | "InetAddressType") == 0 ) { |
4454 | |
|
4455 | 0 | int ret; |
4456 | 0 | int addr_type = *(objid - 1); |
4457 | |
|
4458 | 0 | ret = dump_realloc_oid_to_inetaddress(addr_type, |
4459 | 0 | objid + 1, numids - 1, buf, buf_len, out_len, |
4460 | 0 | allow_realloc, '"'); |
4461 | 0 | if (ret != 2) { |
4462 | 0 | normal_handling = 0; |
4463 | 0 | if (ret == 0) { |
4464 | 0 | *buf_overflow = 1; |
4465 | 0 | } |
4466 | |
|
4467 | 0 | } |
4468 | 0 | } |
4469 | 0 | if (normal_handling && !dump_realloc_oid_to_string |
4470 | 0 | (objid + 1, numids - 1, buf, buf_len, out_len, |
4471 | 0 | allow_realloc, '"')) { |
4472 | 0 | *buf_overflow = 1; |
4473 | 0 | } |
4474 | 0 | } |
4475 | 0 | } |
4476 | 0 | } |
4477 | 0 | objid += numids; |
4478 | 0 | objidlen -= numids; |
4479 | 0 | break; |
4480 | | |
4481 | 0 | case TYPE_INTEGER32: |
4482 | 0 | case TYPE_UINTEGER: |
4483 | 0 | case TYPE_UNSIGNED32: |
4484 | 0 | case TYPE_GAUGE: |
4485 | 0 | case TYPE_INTEGER: |
4486 | 0 | if (tp->enums) { |
4487 | 0 | struct enum_list *ep = tp->enums; |
4488 | 0 | while (ep && ep->value != (int) (*objid)) { |
4489 | 0 | ep = ep->next; |
4490 | 0 | } |
4491 | 0 | if (ep) { |
4492 | 0 | if (!*buf_overflow && |
4493 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4494 | 0 | ep->label)) { |
4495 | 0 | *buf_overflow = 1; |
4496 | 0 | } |
4497 | 0 | } else { |
4498 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u", *objid); |
4499 | 0 | if (!*buf_overflow && |
4500 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4501 | 0 | intbuf)) { |
4502 | 0 | *buf_overflow = 1; |
4503 | 0 | } |
4504 | 0 | } |
4505 | 0 | } else { |
4506 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u", *objid); |
4507 | 0 | if (!*buf_overflow && |
4508 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4509 | 0 | intbuf)) { |
4510 | 0 | *buf_overflow = 1; |
4511 | 0 | } |
4512 | 0 | } |
4513 | 0 | objid++; |
4514 | 0 | objidlen--; |
4515 | 0 | break; |
4516 | | |
4517 | 0 | case TYPE_TIMETICKS: |
4518 | | /* In an index, this is probably a timefilter */ |
4519 | 0 | if (extended_index) { |
4520 | 0 | uptimeString( *objid, intbuf, sizeof( intbuf ) ); |
4521 | 0 | } else { |
4522 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u", *objid); |
4523 | 0 | } |
4524 | 0 | if (!*buf_overflow && |
4525 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf)) { |
4526 | 0 | *buf_overflow = 1; |
4527 | 0 | } |
4528 | 0 | objid++; |
4529 | 0 | objidlen--; |
4530 | 0 | break; |
4531 | | |
4532 | 0 | case TYPE_OBJID: |
4533 | 0 | if (in_dices->isimplied) { |
4534 | 0 | numids = objidlen; |
4535 | 0 | } else { |
4536 | 0 | numids = (size_t) * objid + 1; |
4537 | 0 | } |
4538 | 0 | if (numids > objidlen) |
4539 | 0 | goto finish_it; |
4540 | 0 | if (extended_index) { |
4541 | 0 | if (in_dices->isimplied) { |
4542 | 0 | if (!*buf_overflow |
4543 | 0 | && !netsnmp_sprint_realloc_objid_tree(buf, buf_len, |
4544 | 0 | out_len, |
4545 | 0 | allow_realloc, |
4546 | 0 | buf_overflow, |
4547 | 0 | objid, |
4548 | 0 | numids)) { |
4549 | 0 | *buf_overflow = 1; |
4550 | 0 | } |
4551 | 0 | } else { |
4552 | 0 | if (!*buf_overflow |
4553 | 0 | && !netsnmp_sprint_realloc_objid_tree(buf, buf_len, |
4554 | 0 | out_len, |
4555 | 0 | allow_realloc, |
4556 | 0 | buf_overflow, |
4557 | 0 | objid + 1, |
4558 | 0 | numids - |
4559 | 0 | 1)) { |
4560 | 0 | *buf_overflow = 1; |
4561 | 0 | } |
4562 | 0 | } |
4563 | 0 | } else { |
4564 | 0 | _get_realloc_symbol(objid, numids, NULL, buf, buf_len, |
4565 | 0 | out_len, allow_realloc, buf_overflow, |
4566 | 0 | NULL, NULL); |
4567 | 0 | } |
4568 | 0 | objid += (numids); |
4569 | 0 | objidlen -= (numids); |
4570 | 0 | break; |
4571 | | |
4572 | 0 | case TYPE_IPADDR: |
4573 | 0 | if (objidlen < 4) |
4574 | 0 | goto finish_it; |
4575 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u." |
4576 | 0 | "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u", |
4577 | 0 | objid[0], objid[1], objid[2], objid[3]); |
4578 | 0 | objid += 4; |
4579 | 0 | objidlen -= 4; |
4580 | 0 | if (!*buf_overflow && |
4581 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf)) { |
4582 | 0 | *buf_overflow = 1; |
4583 | 0 | } |
4584 | 0 | break; |
4585 | | |
4586 | 0 | case TYPE_NETADDR:{ |
4587 | 0 | oid ntype = *objid++; |
4588 | |
|
4589 | 0 | objidlen--; |
4590 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u.", ntype); |
4591 | 0 | if (!*buf_overflow && |
4592 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4593 | 0 | intbuf)) { |
4594 | 0 | *buf_overflow = 1; |
4595 | 0 | } |
4596 | |
|
4597 | 0 | if (ntype == 1 && objidlen >= 4) { |
4598 | 0 | snprintf(intbuf, sizeof(intbuf), "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u." |
4599 | 0 | "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u", |
4600 | 0 | objid[0], objid[1], objid[2], objid[3]); |
4601 | 0 | if (!*buf_overflow && |
4602 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4603 | 0 | intbuf)) { |
4604 | 0 | *buf_overflow = 1; |
4605 | 0 | } |
4606 | 0 | objid += 4; |
4607 | 0 | objidlen -= 4; |
4608 | 0 | } else { |
4609 | 0 | goto finish_it; |
4610 | 0 | } |
4611 | 0 | } |
4612 | 0 | break; |
4613 | | |
4614 | 0 | case TYPE_NSAPADDRESS: |
4615 | 0 | default: |
4616 | 0 | goto finish_it; |
4617 | 0 | break; |
4618 | 0 | } |
4619 | | |
4620 | 0 | if (extended_index) { |
4621 | 0 | if (!*buf_overflow && |
4622 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "]")) { |
4623 | 0 | *buf_overflow = 1; |
4624 | 0 | } |
4625 | 0 | } else { |
4626 | 0 | if (!*buf_overflow && |
4627 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, ".")) { |
4628 | 0 | *buf_overflow = 1; |
4629 | 0 | } |
4630 | 0 | } |
4631 | 0 | in_dices = in_dices->next; |
4632 | 0 | } |
4633 | | |
4634 | 0 | finish_it: |
4635 | 0 | _oid_finish_printing(objid, objidlen, |
4636 | 0 | buf, buf_len, out_len, |
4637 | 0 | allow_realloc, buf_overflow); |
4638 | 0 | return NULL; |
4639 | 0 | } |
4640 | | |
4641 | | struct tree * |
4642 | | get_tree(const oid * objid, size_t objidlen, struct tree *subtree) |
4643 | 0 | { |
4644 | 0 | struct tree *return_tree = NULL; |
4645 | |
|
4646 | 0 | for (; subtree; subtree = subtree->next_peer) { |
4647 | 0 | if (*objid == subtree->subid) |
4648 | 0 | goto found; |
4649 | 0 | } |
4650 | | |
4651 | 0 | return NULL; |
4652 | | |
4653 | 0 | found: |
4654 | 0 | while (subtree->next_peer && subtree->next_peer->subid == *objid) |
4655 | 0 | subtree = subtree->next_peer; |
4656 | 0 | if (objidlen > 1) |
4657 | 0 | return_tree = |
4658 | 0 | get_tree(objid + 1, objidlen - 1, subtree->child_list); |
4659 | 0 | if (return_tree != NULL) |
4660 | 0 | return return_tree; |
4661 | 0 | else |
4662 | 0 | return subtree; |
4663 | 0 | } |
4664 | | |
4665 | | /** |
4666 | | * Prints on oid description on stdout. |
4667 | | * |
4668 | | * @see fprint_description |
4669 | | */ |
4670 | | void |
4671 | | print_description(oid * objid, size_t objidlen, /* number of subidentifiers */ |
4672 | | int width) |
4673 | 0 | { |
4674 | 0 | fprint_description(stdout, objid, objidlen, width); |
4675 | 0 | } |
4676 | | |
4677 | | |
4678 | | /** |
4679 | | * Prints on oid description into a file descriptor. |
4680 | | * |
4681 | | * @param f The file descriptor to print to. |
4682 | | * @param objid The object identifier. |
4683 | | * @param objidlen The object id length. |
4684 | | * @param width Number of subidentifiers. |
4685 | | */ |
4686 | | void |
4687 | | fprint_description(FILE * f, oid * objid, size_t objidlen, |
4688 | | int width) |
4689 | 0 | { |
4690 | 0 | u_char *buf = NULL; |
4691 | 0 | size_t buf_len = 256, out_len = 0; |
4692 | |
|
4693 | 0 | if ((buf = calloc(buf_len, 1)) == NULL) { |
4694 | 0 | fprintf(f, "[TRUNCATED]\n"); |
4695 | 0 | return; |
4696 | 0 | } else { |
4697 | 0 | if (!sprint_realloc_description(&buf, &buf_len, &out_len, 1, |
4698 | 0 | objid, objidlen, width)) { |
4699 | 0 | fprintf(f, "%s [TRUNCATED]\n", buf); |
4700 | 0 | } else { |
4701 | 0 | fprintf(f, "%s\n", buf); |
4702 | 0 | } |
4703 | 0 | } |
4704 | | |
4705 | 0 | SNMP_FREE(buf); |
4706 | 0 | } |
4707 | | |
4708 | | #ifndef NETSNMP_FEATURE_REMOVE_MIB_SNPRINT_DESCRIPTION |
4709 | | int |
4710 | | snprint_description(char *buf, size_t buf_len, |
4711 | | oid * objid, size_t objidlen, int width) |
4712 | 0 | { |
4713 | 0 | size_t out_len = 0; |
4714 | |
|
4715 | 0 | if (sprint_realloc_description((u_char **) & buf, &buf_len, &out_len, 0, |
4716 | 0 | objid, objidlen, width)) { |
4717 | 0 | return (int) out_len; |
4718 | 0 | } else { |
4719 | 0 | return -1; |
4720 | 0 | } |
4721 | 0 | } |
4722 | | #endif /* NETSNMP_FEATURE_REMOVE_MIB_SNPRINT_DESCRIPTION */ |
4723 | | |
4724 | | int |
4725 | | sprint_realloc_description(u_char ** buf, size_t * buf_len, |
4726 | | size_t * out_len, int allow_realloc, |
4727 | | oid * objid, size_t objidlen, int width) |
4728 | 0 | { |
4729 | 0 | struct tree *tp = get_tree(objid, objidlen, tree_head); |
4730 | 0 | struct tree *subtree = tree_head; |
4731 | 0 | int pos, len; |
4732 | 0 | char tmpbuf[128]; |
4733 | 0 | const char *cp; |
4734 | |
|
4735 | 0 | if (NULL == tp) |
4736 | 0 | return 0; |
4737 | | |
4738 | 0 | if (tp->type <= TYPE_SIMPLE_LAST) |
4739 | 0 | cp = " OBJECT-TYPE"; |
4740 | 0 | else |
4741 | 0 | switch (tp->type) { |
4742 | 0 | case TYPE_TRAPTYPE: |
4743 | 0 | cp = " TRAP-TYPE"; |
4744 | 0 | break; |
4745 | 0 | case TYPE_NOTIFTYPE: |
4746 | 0 | cp = " NOTIFICATION-TYPE"; |
4747 | 0 | break; |
4748 | 0 | case TYPE_OBJGROUP: |
4749 | 0 | cp = " OBJECT-GROUP"; |
4750 | 0 | break; |
4751 | 0 | case TYPE_AGENTCAP: |
4752 | 0 | cp = " AGENT-CAPABILITIES"; |
4753 | 0 | break; |
4754 | 0 | case TYPE_MODID: |
4755 | 0 | cp = " MODULE-IDENTITY"; |
4756 | 0 | break; |
4757 | 0 | case TYPE_OBJIDENTITY: |
4758 | 0 | cp = " OBJECT-IDENTITY"; |
4759 | 0 | break; |
4760 | 0 | case TYPE_MODCOMP: |
4761 | 0 | cp = " MODULE-COMPLIANCE"; |
4762 | 0 | break; |
4763 | 0 | default: |
4764 | 0 | snprintf(tmpbuf, sizeof(tmpbuf), " type_%d", tp->type); |
4765 | 0 | cp = tmpbuf; |
4766 | 0 | } |
4767 | | |
4768 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tp->label) || |
4769 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, cp) || |
4770 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) { |
4771 | 0 | return 0; |
4772 | 0 | } |
4773 | 0 | if (!print_tree_node(buf, buf_len, out_len, allow_realloc, tp, width)) |
4774 | 0 | return 0; |
4775 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "::= {")) |
4776 | 0 | return 0; |
4777 | 0 | pos = 5; |
4778 | 0 | while (objidlen > 1) { |
4779 | 0 | for (; subtree; subtree = subtree->next_peer) { |
4780 | 0 | if (*objid == subtree->subid) { |
4781 | 0 | while (subtree->next_peer && subtree->next_peer->subid == *objid) |
4782 | 0 | subtree = subtree->next_peer; |
4783 | 0 | if (strncmp(subtree->label, ANON, ANON_LEN)) { |
4784 | 0 | snprintf(tmpbuf, sizeof(tmpbuf), " %s(%lu)", subtree->label, subtree->subid); |
4785 | 0 | } else |
4786 | 0 | snprintf(tmpbuf, sizeof(tmpbuf), " %lu", subtree->subid); |
4787 | 0 | len = strlen(tmpbuf); |
4788 | 0 | if (pos + len + 2 > width) { |
4789 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, |
4790 | 0 | allow_realloc, "\n ")) |
4791 | 0 | return 0; |
4792 | 0 | pos = 5; |
4793 | 0 | } |
4794 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmpbuf)) |
4795 | 0 | return 0; |
4796 | 0 | pos += len; |
4797 | 0 | objid++; |
4798 | 0 | objidlen--; |
4799 | 0 | break; |
4800 | 0 | } |
4801 | 0 | } |
4802 | 0 | if (subtree) |
4803 | 0 | subtree = subtree->child_list; |
4804 | 0 | else |
4805 | 0 | break; |
4806 | 0 | } |
4807 | 0 | while (objidlen > 1) { |
4808 | 0 | snprintf(tmpbuf, sizeof(tmpbuf), " %" NETSNMP_PRIo "u", *objid); |
4809 | 0 | len = strlen(tmpbuf); |
4810 | 0 | if (pos + len + 2 > width) { |
4811 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n ")) |
4812 | 0 | return 0; |
4813 | 0 | pos = 5; |
4814 | 0 | } |
4815 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmpbuf)) |
4816 | 0 | return 0; |
4817 | 0 | pos += len; |
4818 | 0 | objid++; |
4819 | 0 | objidlen--; |
4820 | 0 | } |
4821 | 0 | snprintf(tmpbuf, sizeof(tmpbuf), " %" NETSNMP_PRIo "u }", *objid); |
4822 | 0 | len = strlen(tmpbuf); |
4823 | 0 | if (pos + len + 2 > width) { |
4824 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n ")) |
4825 | 0 | return 0; |
4826 | 0 | pos = 5; |
4827 | 0 | } |
4828 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmpbuf)) |
4829 | 0 | return 0; |
4830 | 0 | return 1; |
4831 | 0 | } |
4832 | | |
4833 | | static int |
4834 | | print_tree_node(u_char ** buf, size_t * buf_len, |
4835 | | size_t * out_len, int allow_realloc, |
4836 | | struct tree *tp, int width) |
4837 | 0 | { |
4838 | 0 | const char *cp; |
4839 | 0 | char str[MAXTOKEN]; |
4840 | 0 | int i, prevmod, pos, len; |
4841 | |
|
4842 | 0 | if (tp) { |
4843 | 0 | module_name(tp->modid, str); |
4844 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4845 | 0 | " -- FROM\t") || |
4846 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
4847 | 0 | return 0; |
4848 | 0 | pos = 16+strlen(str); |
4849 | 0 | for (i = 1, prevmod = tp->modid; i < tp->number_modules; i++) { |
4850 | 0 | if (prevmod != tp->module_list[i]) { |
4851 | 0 | module_name(tp->module_list[i], str); |
4852 | 0 | len = strlen(str); |
4853 | 0 | if (pos + len + 2 > width) { |
4854 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4855 | 0 | ",\n --\t\t")) |
4856 | 0 | return 0; |
4857 | 0 | pos = 16; |
4858 | 0 | } |
4859 | 0 | else { |
4860 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4861 | 0 | ", ")) |
4862 | 0 | return 0; |
4863 | 0 | pos += 2; |
4864 | 0 | } |
4865 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
4866 | 0 | return 0; |
4867 | 0 | pos += len; |
4868 | 0 | } |
4869 | 0 | prevmod = tp->module_list[i]; |
4870 | 0 | } |
4871 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) |
4872 | 0 | return 0; |
4873 | 0 | if (tp->tc_index != -1) { |
4874 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4875 | 0 | " -- TEXTUAL CONVENTION ") || |
4876 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4877 | 0 | get_tc_descriptor(tp->tc_index)) || |
4878 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) |
4879 | 0 | return 0; |
4880 | 0 | } |
4881 | 0 | switch (tp->type) { |
4882 | 0 | case TYPE_OBJID: |
4883 | 0 | cp = "OBJECT IDENTIFIER"; |
4884 | 0 | break; |
4885 | 0 | case TYPE_OCTETSTR: |
4886 | 0 | cp = "OCTET STRING"; |
4887 | 0 | break; |
4888 | 0 | case TYPE_INTEGER: |
4889 | 0 | cp = "INTEGER"; |
4890 | 0 | break; |
4891 | 0 | case TYPE_NETADDR: |
4892 | 0 | cp = "NetworkAddress"; |
4893 | 0 | break; |
4894 | 0 | case TYPE_IPADDR: |
4895 | 0 | cp = "IpAddress"; |
4896 | 0 | break; |
4897 | 0 | case TYPE_COUNTER: |
4898 | 0 | cp = "Counter32"; |
4899 | 0 | break; |
4900 | 0 | case TYPE_GAUGE: |
4901 | 0 | cp = "Gauge32"; |
4902 | 0 | break; |
4903 | 0 | case TYPE_TIMETICKS: |
4904 | 0 | cp = "TimeTicks"; |
4905 | 0 | break; |
4906 | 0 | case TYPE_OPAQUE: |
4907 | 0 | cp = "Opaque"; |
4908 | 0 | break; |
4909 | 0 | case TYPE_NULL: |
4910 | 0 | cp = "NULL"; |
4911 | 0 | break; |
4912 | 0 | case TYPE_COUNTER64: |
4913 | 0 | cp = "Counter64"; |
4914 | 0 | break; |
4915 | 0 | case TYPE_BITSTRING: |
4916 | 0 | cp = "BITS"; |
4917 | 0 | break; |
4918 | 0 | case TYPE_NSAPADDRESS: |
4919 | 0 | cp = "NsapAddress"; |
4920 | 0 | break; |
4921 | 0 | case TYPE_UINTEGER: |
4922 | 0 | cp = "UInteger32"; |
4923 | 0 | break; |
4924 | 0 | case TYPE_UNSIGNED32: |
4925 | 0 | cp = "Unsigned32"; |
4926 | 0 | break; |
4927 | 0 | case TYPE_INTEGER32: |
4928 | 0 | cp = "Integer32"; |
4929 | 0 | break; |
4930 | 0 | default: |
4931 | 0 | cp = NULL; |
4932 | 0 | break; |
4933 | 0 | } |
4934 | | #ifdef NETSNMP_ENABLE_TESTING_CODE |
4935 | | if (!cp && (tp->ranges || tp->enums)) { /* ranges without type ? */ |
4936 | | snprintf(str, sizeof(str), "?0 with %s %s ?", |
4937 | | tp->ranges ? "Range" : "", tp->enums ? "Enum" : ""); |
4938 | | cp = str; |
4939 | | } |
4940 | | #endif /* NETSNMP_ENABLE_TESTING_CODE */ |
4941 | 0 | if (cp) |
4942 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4943 | 0 | " SYNTAX\t") || |
4944 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, cp)) |
4945 | 0 | return 0; |
4946 | 0 | if (tp->ranges) { |
4947 | 0 | struct range_list *rp = tp->ranges; |
4948 | 0 | int first = 1; |
4949 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " (")) |
4950 | 0 | return 0; |
4951 | 0 | while (rp) { |
4952 | 0 | switch (tp->type) { |
4953 | 0 | case TYPE_INTEGER: |
4954 | 0 | case TYPE_INTEGER32: |
4955 | 0 | if (rp->low == rp->high) |
4956 | 0 | snprintf(str, sizeof(str), "%s%d", first ? "" : " | ", |
4957 | 0 | rp->low ); |
4958 | 0 | else |
4959 | 0 | snprintf(str, sizeof(str), "%s%d..%d", |
4960 | 0 | first ? "" : " | ", rp->low, rp->high); |
4961 | 0 | break; |
4962 | 0 | case TYPE_UNSIGNED32: |
4963 | 0 | case TYPE_OCTETSTR: |
4964 | 0 | case TYPE_GAUGE: |
4965 | 0 | case TYPE_UINTEGER: |
4966 | 0 | if (rp->low == rp->high) |
4967 | 0 | snprintf(str, sizeof(str), "%s%u", first ? "" : " | ", |
4968 | 0 | (unsigned)rp->low ); |
4969 | 0 | else |
4970 | 0 | snprintf(str, sizeof(str), "%s%u..%u", |
4971 | 0 | first ? "" : " | ", (unsigned)rp->low, |
4972 | 0 | (unsigned)rp->high); |
4973 | 0 | break; |
4974 | 0 | default: |
4975 | | /* No other range types allowed */ |
4976 | 0 | break; |
4977 | 0 | } |
4978 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
4979 | 0 | return 0; |
4980 | 0 | if (first) |
4981 | 0 | first = 0; |
4982 | 0 | rp = rp->next; |
4983 | 0 | } |
4984 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, ") ")) |
4985 | 0 | return 0; |
4986 | 0 | } |
4987 | 0 | if (tp->enums) { |
4988 | 0 | struct enum_list *ep = tp->enums; |
4989 | 0 | int first = 1; |
4990 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " {")) |
4991 | 0 | return 0; |
4992 | 0 | pos = 16 + strlen(cp) + 2; |
4993 | 0 | while (ep) { |
4994 | 0 | if (first) |
4995 | 0 | first = 0; |
4996 | 0 | else |
4997 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
4998 | 0 | ", ")) |
4999 | 0 | return 0; |
5000 | 0 | snprintf(str, sizeof(str), "%s(%d)", ep->label, ep->value); |
5001 | 0 | len = strlen(str); |
5002 | 0 | if (pos + len + 2 > width) { |
5003 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, |
5004 | 0 | allow_realloc, "\n\t\t ")) |
5005 | 0 | return 0; |
5006 | 0 | pos = 18; |
5007 | 0 | } |
5008 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
5009 | 0 | return 0; |
5010 | 0 | pos += len + 2; |
5011 | 0 | ep = ep->next; |
5012 | 0 | } |
5013 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "} ")) |
5014 | 0 | return 0; |
5015 | 0 | } |
5016 | 0 | if (cp) |
5017 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) |
5018 | 0 | return 0; |
5019 | 0 | if (tp->hint) |
5020 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5021 | 0 | " DISPLAY-HINT\t\"") || |
5022 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tp->hint) || |
5023 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"\n")) |
5024 | 0 | return 0; |
5025 | 0 | if (tp->units) |
5026 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5027 | 0 | " UNITS\t\t\"") || |
5028 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5029 | 0 | tp->units) || |
5030 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"\n")) |
5031 | 0 | return 0; |
5032 | 0 | switch (tp->access) { |
5033 | 0 | case MIB_ACCESS_READONLY: |
5034 | 0 | cp = "read-only"; |
5035 | 0 | break; |
5036 | 0 | case MIB_ACCESS_READWRITE: |
5037 | 0 | cp = "read-write"; |
5038 | 0 | break; |
5039 | 0 | case MIB_ACCESS_WRITEONLY: |
5040 | 0 | cp = "write-only"; |
5041 | 0 | break; |
5042 | 0 | case MIB_ACCESS_NOACCESS: |
5043 | 0 | cp = "not-accessible"; |
5044 | 0 | break; |
5045 | 0 | case MIB_ACCESS_NOTIFY: |
5046 | 0 | cp = "accessible-for-notify"; |
5047 | 0 | break; |
5048 | 0 | case MIB_ACCESS_CREATE: |
5049 | 0 | cp = "read-create"; |
5050 | 0 | break; |
5051 | 0 | case 0: |
5052 | 0 | cp = NULL; |
5053 | 0 | break; |
5054 | 0 | default: |
5055 | 0 | snprintf(str, sizeof(str), "access_%d", tp->access); |
5056 | 0 | cp = str; |
5057 | 0 | } |
5058 | 0 | if (cp) |
5059 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5060 | 0 | " MAX-ACCESS\t") || |
5061 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, cp) || |
5062 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) |
5063 | 0 | return 0; |
5064 | 0 | switch (tp->status) { |
5065 | 0 | case MIB_STATUS_MANDATORY: |
5066 | 0 | cp = "mandatory"; |
5067 | 0 | break; |
5068 | 0 | case MIB_STATUS_OPTIONAL: |
5069 | 0 | cp = "optional"; |
5070 | 0 | break; |
5071 | 0 | case MIB_STATUS_OBSOLETE: |
5072 | 0 | cp = "obsolete"; |
5073 | 0 | break; |
5074 | 0 | case MIB_STATUS_DEPRECATED: |
5075 | 0 | cp = "deprecated"; |
5076 | 0 | break; |
5077 | 0 | case MIB_STATUS_CURRENT: |
5078 | 0 | cp = "current"; |
5079 | 0 | break; |
5080 | 0 | case 0: |
5081 | 0 | cp = NULL; |
5082 | 0 | break; |
5083 | 0 | default: |
5084 | 0 | snprintf(str, sizeof(str), "status_%d", tp->status); |
5085 | 0 | cp = str; |
5086 | 0 | } |
5087 | | #ifdef NETSNMP_ENABLE_TESTING_CODE |
5088 | | if (!cp && (tp->indexes)) { /* index without status ? */ |
5089 | | snprintf(str, sizeof(str), "?0 with %s ?", |
5090 | | tp->indexes ? "Index" : ""); |
5091 | | cp = str; |
5092 | | } |
5093 | | #endif /* NETSNMP_ENABLE_TESTING_CODE */ |
5094 | 0 | if (cp) |
5095 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5096 | 0 | " STATUS\t") || |
5097 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, cp) || |
5098 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) |
5099 | 0 | return 0; |
5100 | 0 | if (tp->augments) |
5101 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5102 | 0 | " AUGMENTS\t{ ") || |
5103 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5104 | 0 | tp->augments) || |
5105 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " }\n")) |
5106 | 0 | return 0; |
5107 | 0 | if (tp->indexes) { |
5108 | 0 | struct index_list *ip = tp->indexes; |
5109 | 0 | int first = 1; |
5110 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5111 | 0 | " INDEX\t\t{ ")) |
5112 | 0 | return 0; |
5113 | 0 | pos = 16 + 2; |
5114 | 0 | while (ip) { |
5115 | 0 | if (first) |
5116 | 0 | first = 0; |
5117 | 0 | else |
5118 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5119 | 0 | ", ")) |
5120 | 0 | return 0; |
5121 | 0 | snprintf(str, sizeof(str), "%s%s", |
5122 | 0 | ip->isimplied ? "IMPLIED " : "", |
5123 | 0 | ip->ilabel); |
5124 | 0 | len = strlen(str); |
5125 | 0 | if (pos + len + 2 > width) { |
5126 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5127 | 0 | "\n\t\t ")) |
5128 | 0 | return 0; |
5129 | 0 | pos = 16 + 2; |
5130 | 0 | } |
5131 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
5132 | 0 | return 0; |
5133 | 0 | pos += len + 2; |
5134 | 0 | ip = ip->next; |
5135 | 0 | } |
5136 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " }\n")) |
5137 | 0 | return 0; |
5138 | 0 | } |
5139 | 0 | if (tp->varbinds) { |
5140 | 0 | struct varbind_list *vp = tp->varbinds; |
5141 | 0 | int first = 1; |
5142 | |
|
5143 | 0 | if (tp->type == TYPE_TRAPTYPE) { |
5144 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5145 | 0 | " VARIABLES\t{ ")) |
5146 | 0 | return 0; |
5147 | 0 | } else { |
5148 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5149 | 0 | " OBJECTS\t{ ")) |
5150 | 0 | return 0; |
5151 | 0 | } |
5152 | 0 | pos = 16 + 2; |
5153 | 0 | while (vp) { |
5154 | 0 | if (first) |
5155 | 0 | first = 0; |
5156 | 0 | else |
5157 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5158 | 0 | ", ")) |
5159 | 0 | return 0; |
5160 | 0 | strlcpy(str, vp->vblabel, sizeof(str)); |
5161 | 0 | len = strlen(str); |
5162 | 0 | if (pos + len + 2 > width) { |
5163 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5164 | 0 | "\n\t\t ")) |
5165 | 0 | return 0; |
5166 | 0 | pos = 16 + 2; |
5167 | 0 | } |
5168 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) |
5169 | 0 | return 0; |
5170 | 0 | pos += len + 2; |
5171 | 0 | vp = vp->next; |
5172 | 0 | } |
5173 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " }\n")) |
5174 | 0 | return 0; |
5175 | 0 | } |
5176 | 0 | if (tp->description) |
5177 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5178 | 0 | " DESCRIPTION\t\"") || |
5179 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5180 | 0 | tp->description) || |
5181 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"\n")) |
5182 | 0 | return 0; |
5183 | 0 | if (tp->defaultValue) |
5184 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5185 | 0 | " DEFVAL\t{ ") || |
5186 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5187 | 0 | tp->defaultValue) || |
5188 | 0 | !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " }\n")) |
5189 | 0 | return 0; |
5190 | 0 | } else |
5191 | 0 | if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, |
5192 | 0 | "No description\n")) |
5193 | 0 | return 0; |
5194 | 0 | return 1; |
5195 | 0 | } |
5196 | | |
5197 | | int |
5198 | | get_module_node(const char *fname, |
5199 | | const char *module, oid * objid, size_t * objidlen) |
5200 | 0 | { |
5201 | 0 | int modid, rc = 0; |
5202 | 0 | struct tree *tp; |
5203 | 0 | char *name, *cp; |
5204 | |
|
5205 | 0 | if (!strcmp(module, "ANY")) |
5206 | 0 | modid = -1; |
5207 | 0 | else { |
5208 | 0 | netsnmp_read_module(module); |
5209 | 0 | modid = which_module(module); |
5210 | 0 | if (modid == -1) |
5211 | 0 | return 0; |
5212 | 0 | } |
5213 | | |
5214 | | /* |
5215 | | * Isolate the first component of the name ... |
5216 | | */ |
5217 | 0 | name = strdup(fname); |
5218 | 0 | if (name == NULL) |
5219 | 0 | return -1; |
5220 | 0 | cp = strchr(name, '.'); |
5221 | 0 | if (cp != NULL) { |
5222 | 0 | *cp = '\0'; |
5223 | 0 | cp++; |
5224 | 0 | } |
5225 | | /* |
5226 | | * ... and locate it in the tree. |
5227 | | */ |
5228 | 0 | tp = find_tree_node(name, modid); |
5229 | 0 | if (tp) { |
5230 | 0 | size_t maxlen = *objidlen; |
5231 | | |
5232 | | /* |
5233 | | * Set the first element of the object ID |
5234 | | */ |
5235 | 0 | if (node_to_oid(tp, objid, objidlen)) { |
5236 | 0 | rc = 1; |
5237 | | |
5238 | | /* |
5239 | | * If the name requested was more than one element, |
5240 | | * tag on the rest of the components |
5241 | | */ |
5242 | 0 | if (cp != NULL) |
5243 | 0 | rc = _add_strings_to_oid(tp, cp, objid, objidlen, maxlen); |
5244 | 0 | } |
5245 | 0 | } |
5246 | |
|
5247 | 0 | SNMP_FREE(name); |
5248 | 0 | return (rc); |
5249 | 0 | } |
5250 | | |
5251 | | |
5252 | | /** |
5253 | | * @internal |
5254 | | * |
5255 | | * Populates the object identifier from a node in the MIB hierarchy. |
5256 | | * Builds up the object ID, working backwards, |
5257 | | * starting from the end of the objid buffer. |
5258 | | * When the top of the MIB tree is reached, the buffer is adjusted. |
5259 | | * |
5260 | | * The buffer length is set to the number of subidentifiers |
5261 | | * for the object identifier associated with the MIB node. |
5262 | | * |
5263 | | * @return the number of subidentifiers copied. |
5264 | | * |
5265 | | * If 0 is returned, the objid buffer is too small, |
5266 | | * and the buffer contents are indeterminate. |
5267 | | * The buffer length can be used to create a larger buffer. |
5268 | | */ |
5269 | | static int |
5270 | | node_to_oid(struct tree *tp, oid * objid, size_t * objidlen) |
5271 | 0 | { |
5272 | 0 | size_t numids, lenids; |
5273 | 0 | oid *op; |
5274 | |
|
5275 | 0 | if (!tp || !objid || !objidlen) |
5276 | 0 | return 0; |
5277 | | |
5278 | 0 | lenids = *objidlen; |
5279 | 0 | op = objid + lenids; /* points after the last element */ |
5280 | |
|
5281 | 0 | for (numids = 0; tp; tp = tp->parent, numids++) { |
5282 | 0 | if (numids >= lenids) |
5283 | 0 | continue; |
5284 | 0 | --op; |
5285 | 0 | *op = tp->subid; |
5286 | 0 | } |
5287 | |
|
5288 | 0 | *objidlen = numids; |
5289 | 0 | if (numids > lenids) { |
5290 | 0 | return 0; |
5291 | 0 | } |
5292 | | |
5293 | 0 | if (numids < lenids) |
5294 | 0 | memmove(objid, op, numids * sizeof(oid)); |
5295 | |
|
5296 | 0 | return numids; |
5297 | 0 | } |
5298 | | |
5299 | | /* |
5300 | | * Replace \x with x stop at eos_marker |
5301 | | * return NULL if eos_marker not found |
5302 | | */ |
5303 | | static char *_apply_escapes(char *src, char eos_marker) |
5304 | 0 | { |
5305 | 0 | char *dst; |
5306 | 0 | int backslash = 0; |
5307 | | |
5308 | 0 | dst = src; |
5309 | 0 | while (*src) { |
5310 | 0 | if (backslash) { |
5311 | 0 | backslash = 0; |
5312 | 0 | *dst++ = *src; |
5313 | 0 | } else { |
5314 | 0 | if (eos_marker == *src) break; |
5315 | 0 | if ('\\' == *src) { |
5316 | 0 | backslash = 1; |
5317 | 0 | } else { |
5318 | 0 | *dst++ = *src; |
5319 | 0 | } |
5320 | 0 | } |
5321 | 0 | src++; |
5322 | 0 | } |
5323 | 0 | if (!*src) { |
5324 | | /* never found eos_marker */ |
5325 | 0 | return NULL; |
5326 | 0 | } else { |
5327 | 0 | *dst = 0; |
5328 | 0 | return src; |
5329 | 0 | } |
5330 | 0 | } |
5331 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
5332 | | |
5333 | | static int |
5334 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
5335 | | _add_strings_to_oid(struct tree *tp, char *cp, |
5336 | | oid * objid, size_t * objidlen, size_t maxlen) |
5337 | | #else |
5338 | | _add_strings_to_oid(void *tp, char *cp, |
5339 | | oid * objid, size_t * objidlen, size_t maxlen) |
5340 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
5341 | 0 | { |
5342 | 0 | oid subid; |
5343 | 0 | char *fcp, *ecp, *cp2 = NULL; |
5344 | 0 | char doingquote; |
5345 | 0 | int len = -1; |
5346 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
5347 | 0 | struct tree *tp2 = NULL; |
5348 | 0 | struct index_list *in_dices = NULL; |
5349 | 0 | int pos = -1; |
5350 | 0 | int check = |
5351 | 0 | !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_CHECK_RANGE); |
5352 | 0 | int do_hint = !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_DISPLAY_HINT); |
5353 | 0 | int len_index = 1000000; |
5354 | |
|
5355 | 0 | while (cp && tp && tp->child_list) { |
5356 | 0 | fcp = cp; |
5357 | 0 | tp2 = tp->child_list; |
5358 | | /* |
5359 | | * Isolate the next entry |
5360 | | */ |
5361 | 0 | cp2 = strchr(cp, '.'); |
5362 | 0 | if (cp2) |
5363 | 0 | *cp2++ = '\0'; |
5364 | | |
5365 | | /* |
5366 | | * Search for the appropriate child |
5367 | | */ |
5368 | 0 | if (isdigit((unsigned char)(*cp))) { |
5369 | 0 | subid = strtoul(cp, &ecp, 0); |
5370 | 0 | if (*ecp) |
5371 | 0 | goto bad_id; |
5372 | 0 | while (tp2 && tp2->subid != subid) |
5373 | 0 | tp2 = tp2->next_peer; |
5374 | 0 | } else { |
5375 | 0 | while (tp2 && strcmp(tp2->label, fcp)) |
5376 | 0 | tp2 = tp2->next_peer; |
5377 | 0 | if (!tp2) |
5378 | 0 | goto bad_id; |
5379 | 0 | subid = tp2->subid; |
5380 | 0 | } |
5381 | 0 | if (*objidlen >= maxlen) |
5382 | 0 | goto bad_id; |
5383 | 0 | while (tp2 && tp2->next_peer && tp2->next_peer->subid == subid) |
5384 | 0 | tp2 = tp2->next_peer; |
5385 | 0 | objid[*objidlen] = subid; |
5386 | 0 | (*objidlen)++; |
5387 | |
|
5388 | 0 | cp = cp2; |
5389 | 0 | if (!tp2) |
5390 | 0 | break; |
5391 | 0 | tp = tp2; |
5392 | 0 | } |
5393 | | |
5394 | 0 | if (tp && !tp->child_list) { |
5395 | 0 | if ((tp2 = tp->parent)) { |
5396 | 0 | if (tp2->indexes) |
5397 | 0 | in_dices = tp2->indexes; |
5398 | 0 | else if (tp2->augments) { |
5399 | 0 | tp2 = find_tree_node(tp2->augments, -1); |
5400 | 0 | if (tp2) |
5401 | 0 | in_dices = tp2->indexes; |
5402 | 0 | } |
5403 | 0 | } |
5404 | 0 | tp = NULL; |
5405 | 0 | } |
5406 | |
|
5407 | 0 | while (cp && in_dices) { |
5408 | 0 | fcp = cp; |
5409 | |
|
5410 | 0 | tp = find_tree_node(in_dices->ilabel, -1); |
5411 | 0 | if (!tp) |
5412 | 0 | break; |
5413 | 0 | switch (tp->type) { |
5414 | 0 | case TYPE_INTEGER: |
5415 | 0 | case TYPE_INTEGER32: |
5416 | 0 | case TYPE_UINTEGER: |
5417 | 0 | case TYPE_UNSIGNED32: |
5418 | 0 | case TYPE_TIMETICKS: |
5419 | | /* |
5420 | | * Isolate the next entry |
5421 | | */ |
5422 | 0 | cp2 = strchr(cp, '.'); |
5423 | 0 | if (cp2) |
5424 | 0 | *cp2++ = '\0'; |
5425 | 0 | if (isdigit((unsigned char)(*cp))) { |
5426 | 0 | subid = strtoul(cp, &ecp, 0); |
5427 | 0 | if (*ecp) |
5428 | 0 | goto bad_id; |
5429 | 0 | } else { |
5430 | 0 | if (tp->enums) { |
5431 | 0 | struct enum_list *ep = tp->enums; |
5432 | 0 | while (ep && strcmp(ep->label, cp)) |
5433 | 0 | ep = ep->next; |
5434 | 0 | if (!ep) |
5435 | 0 | goto bad_id; |
5436 | 0 | subid = ep->value; |
5437 | 0 | } else |
5438 | 0 | goto bad_id; |
5439 | 0 | } |
5440 | 0 | if (check && tp->ranges) { |
5441 | 0 | struct range_list *rp = tp->ranges; |
5442 | 0 | int ok = 0; |
5443 | 0 | if (tp->type == TYPE_INTEGER || |
5444 | 0 | tp->type == TYPE_INTEGER32) { |
5445 | 0 | while (!ok && rp) { |
5446 | 0 | if ((rp->low <= (int) subid) |
5447 | 0 | && ((int) subid <= rp->high)) |
5448 | 0 | ok = 1; |
5449 | 0 | else |
5450 | 0 | rp = rp->next; |
5451 | 0 | } |
5452 | 0 | } else { /* check unsigned range */ |
5453 | 0 | while (!ok && rp) { |
5454 | 0 | if (((unsigned int)rp->low <= subid) |
5455 | 0 | && (subid <= (unsigned int)rp->high)) |
5456 | 0 | ok = 1; |
5457 | 0 | else |
5458 | 0 | rp = rp->next; |
5459 | 0 | } |
5460 | 0 | } |
5461 | 0 | if (!ok) |
5462 | 0 | goto bad_id; |
5463 | 0 | } |
5464 | 0 | if (*objidlen >= maxlen) |
5465 | 0 | goto bad_id; |
5466 | 0 | objid[*objidlen] = subid; |
5467 | 0 | (*objidlen)++; |
5468 | 0 | break; |
5469 | 0 | case TYPE_IPADDR: |
5470 | 0 | if (*objidlen + 4 > maxlen) |
5471 | 0 | goto bad_id; |
5472 | 0 | for (subid = 0; cp && subid < 4; subid++) { |
5473 | 0 | fcp = cp; |
5474 | 0 | cp2 = strchr(cp, '.'); |
5475 | 0 | if (cp2) |
5476 | 0 | *cp2++ = 0; |
5477 | 0 | objid[*objidlen] = strtoul(cp, &ecp, 0); |
5478 | 0 | if (*ecp) |
5479 | 0 | goto bad_id; |
5480 | 0 | if (check && objid[*objidlen] > 255) |
5481 | 0 | goto bad_id; |
5482 | 0 | (*objidlen)++; |
5483 | 0 | cp = cp2; |
5484 | 0 | } |
5485 | 0 | break; |
5486 | 0 | case TYPE_OCTETSTR: |
5487 | 0 | if (tp->ranges && !tp->ranges->next |
5488 | 0 | && tp->ranges->low == tp->ranges->high) |
5489 | 0 | len = tp->ranges->low; |
5490 | 0 | else |
5491 | 0 | len = -1; |
5492 | 0 | pos = 0; |
5493 | 0 | if (*cp == '"' || *cp == '\'') { |
5494 | 0 | doingquote = *cp++; |
5495 | | /* |
5496 | | * insert length if requested |
5497 | | */ |
5498 | 0 | if (!in_dices->isimplied && len == -1) { |
5499 | 0 | if (doingquote == '\'') { |
5500 | 0 | snmp_set_detail |
5501 | 0 | ("'-quote is for fixed length strings"); |
5502 | 0 | return 0; |
5503 | 0 | } |
5504 | 0 | if (*objidlen >= maxlen) |
5505 | 0 | goto bad_id; |
5506 | 0 | len_index = *objidlen; |
5507 | 0 | (*objidlen)++; |
5508 | 0 | } else if (doingquote == '"') { |
5509 | 0 | snmp_set_detail |
5510 | 0 | ("\"-quote is for variable length strings"); |
5511 | 0 | return 0; |
5512 | 0 | } |
5513 | | |
5514 | 0 | cp2 = _apply_escapes(cp, doingquote); |
5515 | 0 | if (!cp2) goto bad_id; |
5516 | 0 | else { |
5517 | 0 | unsigned char *new_val; |
5518 | 0 | int new_val_len; |
5519 | 0 | int parsed_hint = 0; |
5520 | 0 | const char *parsed_value; |
5521 | |
|
5522 | 0 | if (do_hint && tp->hint) { |
5523 | 0 | parsed_value = parse_octet_hint(tp->hint, cp, |
5524 | 0 | &new_val, &new_val_len); |
5525 | 0 | parsed_hint = parsed_value == NULL; |
5526 | 0 | } |
5527 | 0 | if (parsed_hint) { |
5528 | 0 | int i; |
5529 | 0 | for (i = 0; i < new_val_len; i++) { |
5530 | 0 | if (*objidlen >= maxlen) { |
5531 | 0 | SNMP_FREE(new_val); |
5532 | 0 | goto bad_id; |
5533 | 0 | } |
5534 | 0 | objid[ *objidlen ] = new_val[i]; |
5535 | 0 | (*objidlen)++; |
5536 | 0 | pos++; |
5537 | 0 | } |
5538 | 0 | SNMP_FREE(new_val); |
5539 | 0 | } else { |
5540 | 0 | while(*cp) { |
5541 | 0 | if (*objidlen >= maxlen) goto bad_id; |
5542 | 0 | objid[ *objidlen ] = *cp++; |
5543 | 0 | (*objidlen)++; |
5544 | 0 | pos++; |
5545 | 0 | } |
5546 | 0 | } |
5547 | 0 | } |
5548 | | |
5549 | 0 | cp2++; |
5550 | 0 | if (!*cp2) |
5551 | 0 | cp2 = NULL; |
5552 | 0 | else if (*cp2 != '.') |
5553 | 0 | goto bad_id; |
5554 | 0 | else |
5555 | 0 | cp2++; |
5556 | 0 | if (check) { |
5557 | 0 | if (len == -1) { |
5558 | 0 | struct range_list *rp = tp->ranges; |
5559 | 0 | int ok = 0; |
5560 | 0 | while (rp && !ok) |
5561 | 0 | if (rp->low <= pos && pos <= rp->high) |
5562 | 0 | ok = 1; |
5563 | 0 | else |
5564 | 0 | rp = rp->next; |
5565 | 0 | if (!ok) |
5566 | 0 | goto bad_id; |
5567 | 0 | if (!in_dices->isimplied) |
5568 | 0 | objid[len_index] = pos; |
5569 | 0 | } else if (pos != len) |
5570 | 0 | goto bad_id; |
5571 | 0 | } |
5572 | 0 | else if (len == -1 && !in_dices->isimplied) |
5573 | 0 | objid[len_index] = pos; |
5574 | 0 | } else { |
5575 | 0 | if (!in_dices->isimplied && len == -1) { |
5576 | 0 | fcp = cp; |
5577 | 0 | cp2 = strchr(cp, '.'); |
5578 | 0 | if (cp2) |
5579 | 0 | *cp2++ = 0; |
5580 | 0 | len = strtoul(cp, &ecp, 0); |
5581 | 0 | if (len < 0 || *ecp) |
5582 | 0 | goto bad_id; |
5583 | 0 | if (*objidlen + len + 1 >= maxlen) |
5584 | 0 | goto bad_id; |
5585 | 0 | objid[*objidlen] = len; |
5586 | 0 | (*objidlen)++; |
5587 | 0 | cp = cp2; |
5588 | 0 | } |
5589 | 0 | while (len && cp) { |
5590 | 0 | fcp = cp; |
5591 | 0 | cp2 = strchr(cp, '.'); |
5592 | 0 | if (cp2) |
5593 | 0 | *cp2++ = 0; |
5594 | 0 | if (*objidlen >= maxlen) |
5595 | 0 | goto bad_id; |
5596 | 0 | objid[*objidlen] = strtoul(cp, &ecp, 0); |
5597 | 0 | if (*ecp) |
5598 | 0 | goto bad_id; |
5599 | 0 | if (check && objid[*objidlen] > 255) |
5600 | 0 | goto bad_id; |
5601 | 0 | (*objidlen)++; |
5602 | 0 | len--; |
5603 | 0 | cp = cp2; |
5604 | 0 | } |
5605 | 0 | } |
5606 | 0 | break; |
5607 | 0 | case TYPE_OBJID: |
5608 | 0 | in_dices = NULL; |
5609 | 0 | cp2 = cp; |
5610 | 0 | break; |
5611 | 0 | case TYPE_NETADDR: |
5612 | 0 | fcp = cp; |
5613 | 0 | cp2 = strchr(cp, '.'); |
5614 | 0 | if (cp2) |
5615 | 0 | *cp2++ = 0; |
5616 | 0 | subid = strtoul(cp, &ecp, 0); |
5617 | 0 | if (*ecp) |
5618 | 0 | goto bad_id; |
5619 | 0 | if (*objidlen + 1 >= maxlen) |
5620 | 0 | goto bad_id; |
5621 | 0 | objid[*objidlen] = subid; |
5622 | 0 | (*objidlen)++; |
5623 | 0 | cp = cp2; |
5624 | 0 | if (subid == 1) { |
5625 | 0 | for (len = 0; cp && len < 4; len++) { |
5626 | 0 | fcp = cp; |
5627 | 0 | cp2 = strchr(cp, '.'); |
5628 | 0 | if (cp2) |
5629 | 0 | *cp2++ = 0; |
5630 | 0 | subid = strtoul(cp, &ecp, 0); |
5631 | 0 | if (*ecp) |
5632 | 0 | goto bad_id; |
5633 | 0 | if (*objidlen + 1 >= maxlen) |
5634 | 0 | goto bad_id; |
5635 | 0 | if (check && subid > 255) |
5636 | 0 | goto bad_id; |
5637 | 0 | objid[*objidlen] = subid; |
5638 | 0 | (*objidlen)++; |
5639 | 0 | cp = cp2; |
5640 | 0 | } |
5641 | 0 | } |
5642 | 0 | else { |
5643 | 0 | in_dices = NULL; |
5644 | 0 | } |
5645 | 0 | break; |
5646 | 0 | default: |
5647 | 0 | snmp_log(LOG_ERR, "Unexpected index type: %d %s %s\n", |
5648 | 0 | tp->type, in_dices->ilabel, cp); |
5649 | 0 | in_dices = NULL; |
5650 | 0 | cp2 = cp; |
5651 | 0 | break; |
5652 | 0 | } |
5653 | 0 | cp = cp2; |
5654 | 0 | if (in_dices) |
5655 | 0 | in_dices = in_dices->next; |
5656 | 0 | } |
5657 | | |
5658 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
5659 | 0 | while (cp) { |
5660 | 0 | fcp = cp; |
5661 | 0 | switch (*cp) { |
5662 | 0 | case '0': |
5663 | 0 | case '1': |
5664 | 0 | case '2': |
5665 | 0 | case '3': |
5666 | 0 | case '4': |
5667 | 0 | case '5': |
5668 | 0 | case '6': |
5669 | 0 | case '7': |
5670 | 0 | case '8': |
5671 | 0 | case '9': |
5672 | 0 | cp2 = strchr(cp, '.'); |
5673 | 0 | if (cp2) |
5674 | 0 | *cp2++ = 0; |
5675 | 0 | subid = strtoul(cp, &ecp, 0); |
5676 | 0 | if (*ecp) |
5677 | 0 | goto bad_id; |
5678 | 0 | if (*objidlen >= maxlen) |
5679 | 0 | goto bad_id; |
5680 | 0 | objid[*objidlen] = subid; |
5681 | 0 | (*objidlen)++; |
5682 | 0 | break; |
5683 | 0 | case '"': |
5684 | 0 | case '\'': |
5685 | 0 | doingquote = *cp++; |
5686 | 0 | if (*cp == '\0') |
5687 | 0 | goto bad_id; |
5688 | | /* |
5689 | | * insert length if requested |
5690 | | */ |
5691 | 0 | if (doingquote == '"') { |
5692 | 0 | char *endq; |
5693 | 0 | if (*objidlen >= maxlen) |
5694 | 0 | goto bad_id; |
5695 | 0 | endq = strchr(cp, doingquote); |
5696 | 0 | if (!endq) |
5697 | 0 | goto bad_id; |
5698 | 0 | objid[*objidlen] = len = endq - cp; |
5699 | 0 | (*objidlen)++; |
5700 | 0 | } |
5701 | | |
5702 | 0 | while (*cp && *cp != doingquote) { |
5703 | 0 | if (*objidlen >= maxlen) |
5704 | 0 | goto bad_id; |
5705 | 0 | objid[*objidlen] = *cp++; |
5706 | 0 | (*objidlen)++; |
5707 | 0 | } |
5708 | 0 | if (!*cp) |
5709 | 0 | goto bad_id; |
5710 | 0 | cp2 = cp + 1; |
5711 | 0 | if (!*cp2) |
5712 | 0 | cp2 = NULL; |
5713 | 0 | else if (*cp2 == '.') |
5714 | 0 | cp2++; |
5715 | 0 | else |
5716 | 0 | goto bad_id; |
5717 | 0 | break; |
5718 | 0 | default: |
5719 | 0 | goto bad_id; |
5720 | 0 | } |
5721 | 0 | cp = cp2; |
5722 | 0 | } |
5723 | 0 | return 1; |
5724 | | |
5725 | 0 | bad_id: |
5726 | 0 | { |
5727 | 0 | char buf[256]; |
5728 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
5729 | 0 | if (in_dices) |
5730 | 0 | snprintf(buf, sizeof(buf), "Index out of range: %s (%s)", |
5731 | 0 | fcp, in_dices->ilabel); |
5732 | 0 | else if (tp) |
5733 | 0 | snprintf(buf, sizeof(buf), "Sub-id not found: %s -> %s", tp->label, fcp); |
5734 | 0 | else |
5735 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
5736 | 0 | snprintf(buf, sizeof(buf), "%s", fcp); |
5737 | |
|
5738 | 0 | snmp_set_detail(buf); |
5739 | 0 | } |
5740 | 0 | return 0; |
5741 | 0 | } |
5742 | | |
5743 | | |
5744 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
5745 | | /** |
5746 | | * @see comments on find_best_tree_node for usage after first time. |
5747 | | */ |
5748 | | int |
5749 | | get_wild_node(const char *name, oid * objid, size_t * objidlen) |
5750 | 0 | { |
5751 | 0 | struct tree *tp = find_best_tree_node(name, tree_head, NULL); |
5752 | 0 | if (!tp) |
5753 | 0 | return 0; |
5754 | 0 | return get_node(tp->label, objid, objidlen); |
5755 | 0 | } |
5756 | | |
5757 | | int |
5758 | | get_node(const char *name, oid * objid, size_t * objidlen) |
5759 | 0 | { |
5760 | 0 | const char *cp; |
5761 | 0 | char ch; |
5762 | 0 | int res; |
5763 | |
|
5764 | 0 | cp = name; |
5765 | 0 | while ((ch = *cp)) |
5766 | 0 | if (('0' <= ch && ch <= '9') |
5767 | 0 | || ('a' <= ch && ch <= 'z') |
5768 | 0 | || ('A' <= ch && ch <= 'Z') |
5769 | 0 | || ch == '-') |
5770 | 0 | cp++; |
5771 | 0 | else |
5772 | 0 | break; |
5773 | 0 | if (ch != ':') |
5774 | 0 | if (*name == '.') |
5775 | 0 | res = get_module_node(name + 1, "ANY", objid, objidlen); |
5776 | 0 | else |
5777 | 0 | res = get_module_node(name, "ANY", objid, objidlen); |
5778 | 0 | else { |
5779 | 0 | char *module; |
5780 | 0 | size_t module_len; |
5781 | | /* |
5782 | | * requested name is of the form |
5783 | | * "module:subidentifier" |
5784 | | */ |
5785 | 0 | module_len = (size_t) (cp - name + 1); |
5786 | 0 | module = (char *) malloc(module_len); |
5787 | 0 | if (!module) |
5788 | 0 | return SNMPERR_GENERR; |
5789 | 0 | snprintf(module, module_len, "%.*s", (int) (cp - name), name); |
5790 | 0 | cp++; /* cp now point to the subidentifier */ |
5791 | 0 | if (*cp == ':') |
5792 | 0 | cp++; |
5793 | | |
5794 | | /* |
5795 | | * 'cp' and 'name' *do* go that way round! |
5796 | | */ |
5797 | 0 | res = get_module_node(cp, module, objid, objidlen); |
5798 | 0 | SNMP_FREE(module); |
5799 | 0 | } |
5800 | 0 | if (res == 0) { |
5801 | 0 | SET_SNMP_ERROR(SNMPERR_UNKNOWN_OBJID); |
5802 | 0 | } |
5803 | |
|
5804 | 0 | return res; |
5805 | 0 | } |
5806 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
5807 | | |
5808 | | #ifdef testing |
5809 | | |
5810 | | main(int argc, char *argv[]) |
5811 | | { |
5812 | | oid objid[MAX_OID_LEN]; |
5813 | | int objidlen = MAX_OID_LEN; |
5814 | | int count; |
5815 | | netsnmp_variable_list variable; |
5816 | | |
5817 | | netsnmp_init_mib(); |
5818 | | if (argc < 2) |
5819 | | print_subtree(stdout, tree_head, 0); |
5820 | | variable.type = ASN_INTEGER; |
5821 | | variable.val.integer = 3; |
5822 | | variable.val_len = 4; |
5823 | | for (argc--; argc; argc--, argv++) { |
5824 | | objidlen = MAX_OID_LEN; |
5825 | | printf("read_objid(%s) = %d\n", |
5826 | | argv[1], read_objid(argv[1], objid, &objidlen)); |
5827 | | for (count = 0; count < objidlen; count++) |
5828 | | printf("%d.", objid[count]); |
5829 | | printf("\n"); |
5830 | | print_variable(objid, objidlen, &variable); |
5831 | | } |
5832 | | } |
5833 | | |
5834 | | #endif /* testing */ |
5835 | | |
5836 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
5837 | | /* |
5838 | | * initialize: no peers included in the report. |
5839 | | */ |
5840 | | void |
5841 | | clear_tree_flags(register struct tree *tp) |
5842 | 0 | { |
5843 | 0 | for (; tp; tp = tp->next_peer) { |
5844 | 0 | tp->reported = 0; |
5845 | 0 | if (tp->child_list) |
5846 | 0 | clear_tree_flags(tp->child_list); |
5847 | 0 | /*RECURSE*/} |
5848 | 0 | } |
5849 | | |
5850 | | /* |
5851 | | * Update: 1998-07-17 <jhy@gsu.edu> |
5852 | | * Added print_oid_report* functions. |
5853 | | */ |
5854 | | static int print_subtree_oid_report_labeledoid = 0; |
5855 | | static int print_subtree_oid_report_oid = 0; |
5856 | | static int print_subtree_oid_report_symbolic = 0; |
5857 | | static int print_subtree_oid_report_mibchildoid = 0; |
5858 | | static int print_subtree_oid_report_suffix = 0; |
5859 | | |
5860 | | /* |
5861 | | * These methods recurse. |
5862 | | */ |
5863 | | static void print_parent_labeledoid(FILE *, struct tree *); |
5864 | | static void print_parent_oid(FILE *, struct tree *); |
5865 | | static void print_parent_mibchildoid(FILE *, struct tree *); |
5866 | | static void print_parent_label(FILE *, struct tree *); |
5867 | | static void print_subtree_oid_report(FILE *, struct tree *, int); |
5868 | | |
5869 | | |
5870 | | void |
5871 | | print_oid_report(FILE * fp) |
5872 | 0 | { |
5873 | 0 | struct tree *tp; |
5874 | 0 | clear_tree_flags(tree_head); |
5875 | 0 | for (tp = tree_head; tp; tp = tp->next_peer) |
5876 | 0 | print_subtree_oid_report(fp, tp, 0); |
5877 | 0 | } |
5878 | | |
5879 | | void |
5880 | | print_oid_report_enable_labeledoid(void) |
5881 | 0 | { |
5882 | 0 | print_subtree_oid_report_labeledoid = 1; |
5883 | 0 | } |
5884 | | |
5885 | | void |
5886 | | print_oid_report_enable_oid(void) |
5887 | 0 | { |
5888 | 0 | print_subtree_oid_report_oid = 1; |
5889 | 0 | } |
5890 | | |
5891 | | void |
5892 | | print_oid_report_enable_suffix(void) |
5893 | 0 | { |
5894 | 0 | print_subtree_oid_report_suffix = 1; |
5895 | 0 | } |
5896 | | |
5897 | | void |
5898 | | print_oid_report_enable_symbolic(void) |
5899 | 0 | { |
5900 | 0 | print_subtree_oid_report_symbolic = 1; |
5901 | 0 | } |
5902 | | |
5903 | | void |
5904 | | print_oid_report_enable_mibchildoid(void) |
5905 | 0 | { |
5906 | 0 | print_subtree_oid_report_mibchildoid = 1; |
5907 | 0 | } |
5908 | | |
5909 | | /* |
5910 | | * helper methods for print_subtree_oid_report() |
5911 | | * each one traverses back up the node tree |
5912 | | * until there is no parent. Then, the label combination |
5913 | | * is output, such that the parent is displayed first. |
5914 | | * |
5915 | | * Warning: these methods are all recursive. |
5916 | | */ |
5917 | | |
5918 | | static void |
5919 | | print_parent_labeledoid(FILE * f, struct tree *tp) |
5920 | 0 | { |
5921 | 0 | if (tp) { |
5922 | 0 | if (tp->parent) { |
5923 | 0 | print_parent_labeledoid(f, tp->parent); |
5924 | 0 | /*RECURSE*/} |
5925 | 0 | fprintf(f, ".%s(%lu)", tp->label, tp->subid); |
5926 | 0 | } |
5927 | 0 | } |
5928 | | |
5929 | | static void |
5930 | | print_parent_oid(FILE * f, struct tree *tp) |
5931 | 0 | { |
5932 | 0 | if (tp) { |
5933 | 0 | if (tp->parent) { |
5934 | 0 | print_parent_oid(f, tp->parent); |
5935 | 0 | /*RECURSE*/} |
5936 | 0 | fprintf(f, ".%lu", tp->subid); |
5937 | 0 | } |
5938 | 0 | } |
5939 | | |
5940 | | |
5941 | | static void print_parent_mibchildoid(FILE * f, struct tree *tp) |
5942 | 0 | { |
5943 | 0 | static struct tree *temp; |
5944 | 0 | unsigned long elems[MAX_OID_LEN]; |
5945 | 0 | int elem_cnt = 0; |
5946 | 0 | int i = 0; |
5947 | 0 | temp = tp; |
5948 | 0 | if (temp) { |
5949 | 0 | while (temp->parent && |
5950 | 0 | elem_cnt < (int)(sizeof(elems) / sizeof(elems[0])) - 1) { |
5951 | 0 | elems[elem_cnt++] = temp->subid; |
5952 | 0 | temp = temp->parent; |
5953 | 0 | } |
5954 | 0 | if (elem_cnt < (int)(sizeof(elems) / sizeof(elems[0]))) |
5955 | 0 | elems[elem_cnt++] = temp->subid; |
5956 | 0 | } |
5957 | 0 | for (i = elem_cnt - 1; i >= 0; i--) { |
5958 | 0 | if (i == elem_cnt - 1) { |
5959 | 0 | fprintf(f, "%lu", elems[i]); |
5960 | 0 | } else { |
5961 | 0 | fprintf(f, ".%lu", elems[i]); |
5962 | 0 | } |
5963 | 0 | } |
5964 | 0 | } |
5965 | | |
5966 | | static void |
5967 | | print_parent_label(FILE * f, struct tree *tp) |
5968 | 0 | { |
5969 | 0 | if (tp) { |
5970 | 0 | if (tp->parent) { |
5971 | 0 | print_parent_label(f, tp->parent); |
5972 | 0 | /*RECURSE*/} |
5973 | 0 | fprintf(f, ".%s", tp->label); |
5974 | 0 | } |
5975 | 0 | } |
5976 | | |
5977 | | /** |
5978 | | * @internal |
5979 | | * This methods generates variations on the original print_subtree() report. |
5980 | | * Traverse the tree depth first, from least to greatest sub-identifier. |
5981 | | * Warning: this methods recurses and calls methods that recurse. |
5982 | | * |
5983 | | * @param f File descriptor to print to. |
5984 | | * @param tree ??? |
5985 | | * @param count ??? |
5986 | | */ |
5987 | | |
5988 | | static void |
5989 | | print_subtree_oid_report(FILE * f, struct tree *tree, int count) |
5990 | 0 | { |
5991 | 0 | struct tree *tp; |
5992 | |
|
5993 | 0 | count++; |
5994 | | |
5995 | | /* |
5996 | | * sanity check |
5997 | | */ |
5998 | 0 | if (!tree) { |
5999 | 0 | return; |
6000 | 0 | } |
6001 | | |
6002 | | /* |
6003 | | * find the not reported peer with the lowest sub-identifier. |
6004 | | * if no more, break the loop and cleanup. |
6005 | | * set "reported" flag, and create report for this peer. |
6006 | | * recurse using the children of this peer, if any. |
6007 | | */ |
6008 | 0 | while (1) { |
6009 | 0 | register struct tree *ntp; |
6010 | |
|
6011 | 0 | tp = NULL; |
6012 | 0 | for (ntp = tree->child_list; ntp; ntp = ntp->next_peer) { |
6013 | 0 | if (ntp->reported) |
6014 | 0 | continue; |
6015 | | |
6016 | 0 | if (!tp || (tp->subid > ntp->subid)) |
6017 | 0 | tp = ntp; |
6018 | 0 | } |
6019 | 0 | if (!tp) |
6020 | 0 | break; |
6021 | | |
6022 | 0 | tp->reported = 1; |
6023 | |
|
6024 | 0 | if (print_subtree_oid_report_labeledoid) { |
6025 | 0 | print_parent_labeledoid(f, tp); |
6026 | 0 | fprintf(f, "\n"); |
6027 | 0 | } |
6028 | 0 | if (print_subtree_oid_report_oid) { |
6029 | 0 | print_parent_oid(f, tp); |
6030 | 0 | fprintf(f, "\n"); |
6031 | 0 | } |
6032 | 0 | if (print_subtree_oid_report_symbolic) { |
6033 | 0 | print_parent_label(f, tp); |
6034 | 0 | fprintf(f, "\n"); |
6035 | 0 | } |
6036 | 0 | if (print_subtree_oid_report_mibchildoid) { |
6037 | 0 | fprintf(f, "\"%s\"\t", tp->label); |
6038 | 0 | fprintf(f, "\t\t\""); |
6039 | 0 | print_parent_mibchildoid(f, tp); |
6040 | 0 | fprintf(f, "\"\n"); |
6041 | 0 | } |
6042 | 0 | if (print_subtree_oid_report_suffix) { |
6043 | 0 | int i; |
6044 | 0 | for (i = 0; i < count; i++) |
6045 | 0 | fprintf(f, " "); |
6046 | 0 | fprintf(f, "%s(%ld) type=%d", tp->label, tp->subid, tp->type); |
6047 | 0 | if (tp->tc_index != -1) |
6048 | 0 | fprintf(f, " tc=%d", tp->tc_index); |
6049 | 0 | if (tp->hint) |
6050 | 0 | fprintf(f, " hint=%s", tp->hint); |
6051 | 0 | if (tp->units) |
6052 | 0 | fprintf(f, " units=%s", tp->units); |
6053 | |
|
6054 | 0 | fprintf(f, "\n"); |
6055 | 0 | } |
6056 | 0 | print_subtree_oid_report(f, tp, count); |
6057 | 0 | /*RECURSE*/} |
6058 | 0 | } |
6059 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
6060 | | |
6061 | | |
6062 | | /** |
6063 | | * Converts timeticks to hours, minutes, seconds string. |
6064 | | * |
6065 | | * @param timeticks The timeticks to convert. |
6066 | | * @param buf Buffer to write to, has to be at |
6067 | | * least 40 Bytes large. |
6068 | | * |
6069 | | * @return The buffer |
6070 | | * |
6071 | | * @see uptimeString |
6072 | | */ |
6073 | | char * |
6074 | | uptime_string(u_long timeticks, char *buf) |
6075 | 0 | { |
6076 | 0 | return uptime_string_n( timeticks, buf, 40); |
6077 | 0 | } |
6078 | | |
6079 | | char * |
6080 | | uptime_string_n(u_long timeticks, char *buf, size_t buflen) |
6081 | 0 | { |
6082 | 0 | uptimeString(timeticks, buf, buflen); |
6083 | 0 | return buf; |
6084 | 0 | } |
6085 | | |
6086 | | /** |
6087 | | * Given a string, parses an oid out of it (if possible). |
6088 | | * It will try to parse it based on predetermined configuration if |
6089 | | * present or by every method possible otherwise. |
6090 | | * If a suffix has been registered using NETSNMP_DS_LIB_OIDSUFFIX, it |
6091 | | * will be appended to the input string before processing. |
6092 | | * |
6093 | | * @param argv The OID to string parse |
6094 | | * @param root An OID array where the results are stored. |
6095 | | * @param rootlen The max length of the array going in and the data |
6096 | | * length coming out. |
6097 | | * |
6098 | | * @return The root oid pointer if successful, or NULL otherwise. |
6099 | | */ |
6100 | | |
6101 | | oid * |
6102 | | snmp_parse_oid(const char *argv, oid * root, size_t * rootlen) |
6103 | 0 | { |
6104 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
6105 | 0 | size_t savlen = *rootlen; |
6106 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
6107 | 0 | char *tmpbuf = NULL; |
6108 | 0 | const char *suffix, *prefix; |
6109 | |
|
6110 | 0 | suffix = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
6111 | 0 | NETSNMP_DS_LIB_OIDSUFFIX); |
6112 | 0 | prefix = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, |
6113 | 0 | NETSNMP_DS_LIB_OIDPREFIX); |
6114 | 0 | if ((suffix && suffix[0]) || (prefix && prefix[0])) { |
6115 | 0 | if (!suffix) |
6116 | 0 | suffix = ""; |
6117 | 0 | if (!prefix) |
6118 | 0 | prefix = ""; |
6119 | 0 | if (asprintf(&tmpbuf, "%s%s%s%s", prefix, argv, |
6120 | 0 | suffix[0] == '.' || suffix[0] == '\0' ? "" : ".", |
6121 | 0 | suffix) < 0) { |
6122 | 0 | DEBUGMSGTL(("snmp_parse_oid", "Out of memory\n")); |
6123 | 0 | return NULL; |
6124 | 0 | } |
6125 | 0 | argv = tmpbuf; |
6126 | 0 | DEBUGMSGTL(("snmp_parse_oid","Parsing: %s\n",argv)); |
6127 | 0 | } |
6128 | | |
6129 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
6130 | 0 | if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
6131 | 0 | NETSNMP_DS_LIB_RANDOM_ACCESS) |
6132 | 0 | || strchr(argv, ':')) { |
6133 | 0 | if (get_node(argv, root, rootlen)) |
6134 | 0 | goto out; |
6135 | 0 | } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, |
6136 | 0 | NETSNMP_DS_LIB_REGEX_ACCESS)) { |
6137 | 0 | clear_tree_flags(tree_head); |
6138 | 0 | if (get_wild_node(argv, root, rootlen)) |
6139 | 0 | goto out; |
6140 | 0 | } else { |
6141 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
6142 | 0 | if (read_objid(argv, root, rootlen)) |
6143 | 0 | goto out; |
6144 | 0 | #ifndef NETSNMP_DISABLE_MIB_LOADING |
6145 | 0 | *rootlen = savlen; |
6146 | 0 | if (get_node(argv, root, rootlen)) |
6147 | 0 | goto out; |
6148 | 0 | *rootlen = savlen; |
6149 | 0 | DEBUGMSGTL(("parse_oid", "wildly parsing\n")); |
6150 | 0 | clear_tree_flags(tree_head); |
6151 | 0 | if (get_wild_node(argv, root, rootlen)) |
6152 | 0 | goto out; |
6153 | 0 | } |
6154 | 0 | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
6155 | | |
6156 | 0 | root = NULL; |
6157 | |
|
6158 | 0 | out: |
6159 | 0 | free(tmpbuf); |
6160 | 0 | return root; |
6161 | 0 | } |
6162 | | |
6163 | | #ifndef NETSNMP_DISABLE_MIB_LOADING |
6164 | | /* |
6165 | | * Use DISPLAY-HINT to parse a value into an octet string. |
6166 | | * |
6167 | | * note that "1d1d", "11" could have come from an octet string that |
6168 | | * looked like { 1, 1 } or an octet string that looked like { 11 } |
6169 | | * because of this, it's doubtful that anyone would use such a display |
6170 | | * string. Therefore, the parser ignores this case. |
6171 | | */ |
6172 | | |
6173 | | struct parse_hints { |
6174 | | int length; |
6175 | | int repeat; |
6176 | | int format; |
6177 | | int separator; |
6178 | | int terminator; |
6179 | | unsigned char *result; |
6180 | | int result_max; |
6181 | | int result_len; |
6182 | | }; |
6183 | | |
6184 | | static void parse_hints_reset(struct parse_hints *ph) |
6185 | 0 | { |
6186 | 0 | ph->length = 0; |
6187 | 0 | ph->repeat = 0; |
6188 | 0 | ph->format = 0; |
6189 | 0 | ph->separator = 0; |
6190 | 0 | ph->terminator = 0; |
6191 | 0 | } |
6192 | | |
6193 | | static void parse_hints_ctor(struct parse_hints *ph) |
6194 | 0 | { |
6195 | 0 | parse_hints_reset(ph); |
6196 | 0 | ph->result = NULL; |
6197 | 0 | ph->result_max = 0; |
6198 | 0 | ph->result_len = 0; |
6199 | 0 | } |
6200 | | |
6201 | | static int parse_hints_add_result_octet(struct parse_hints *ph, unsigned char octet) |
6202 | 0 | { |
6203 | 0 | if (!(ph->result_len < ph->result_max)) { |
6204 | 0 | unsigned char *new_res; |
6205 | 0 | ph->result_max = ph->result_len + 32; |
6206 | 0 | new_res = (unsigned char *)realloc(ph->result, ph->result_max); |
6207 | 0 | if (!new_res) { |
6208 | 0 | return 0; /* failed */ |
6209 | 0 | } |
6210 | 0 | ph->result = new_res; |
6211 | 0 | } |
6212 | | |
6213 | 0 | if (!ph->result) { |
6214 | 0 | return 0; /* failed */ |
6215 | 0 | } |
6216 | | |
6217 | 0 | ph->result[ph->result_len++] = octet; |
6218 | 0 | return 1; /* success */ |
6219 | 0 | } |
6220 | | |
6221 | | static int parse_hints_parse(struct parse_hints *ph, const char **v_in_out) |
6222 | 0 | { |
6223 | 0 | const char *v = *v_in_out; |
6224 | 0 | char *nv; |
6225 | 0 | int base; |
6226 | 0 | int repeats = 0; |
6227 | 0 | int repeat_fixup = ph->result_len; |
6228 | | |
6229 | 0 | if (ph->length <= 0) |
6230 | 0 | return 0; |
6231 | 0 | if (ph->repeat) { |
6232 | 0 | if (!parse_hints_add_result_octet(ph, 0)) { |
6233 | 0 | return 0; |
6234 | 0 | } |
6235 | 0 | } |
6236 | 0 | do { |
6237 | 0 | base = 0; |
6238 | 0 | switch (ph->format) { |
6239 | 0 | case 'x': base += 6; NETSNMP_FALLTHROUGH; |
6240 | 0 | case 'd': base += 2; NETSNMP_FALLTHROUGH; |
6241 | 0 | case 'o': base += 8; |
6242 | 0 | { |
6243 | 0 | int i; |
6244 | 0 | unsigned long number; |
6245 | 0 | if (ph->length > (int)sizeof(unsigned long)) |
6246 | 0 | return 0; |
6247 | 0 | number = strtol(v, &nv, base); |
6248 | 0 | if (nv == v) return 0; |
6249 | 0 | v = nv; |
6250 | 0 | for (i = 0; i < ph->length; i++) { |
6251 | 0 | int shift = 8 * (ph->length - 1 - i); |
6252 | 0 | if (!parse_hints_add_result_octet(ph, (u_char)(number >> shift) )) { |
6253 | 0 | return 0; /* failed */ |
6254 | 0 | } |
6255 | 0 | } |
6256 | 0 | } |
6257 | 0 | break; |
6258 | | |
6259 | 0 | case 'a': |
6260 | 0 | { |
6261 | 0 | int i; |
6262 | 0 | for (i = 0; i < ph->length && *v; i++) { |
6263 | 0 | if (!parse_hints_add_result_octet(ph, *v++)) { |
6264 | 0 | return 0; /* failed */ |
6265 | 0 | } |
6266 | 0 | } |
6267 | 0 | } |
6268 | 0 | break; |
6269 | 0 | default: |
6270 | 0 | return 0; |
6271 | 0 | } |
6272 | | |
6273 | 0 | repeats++; |
6274 | |
|
6275 | 0 | if (ph->separator && *v) { |
6276 | 0 | if (*v == ph->separator) { |
6277 | 0 | v++; |
6278 | 0 | } else { |
6279 | 0 | return 0; /* failed */ |
6280 | 0 | } |
6281 | 0 | } |
6282 | | |
6283 | 0 | if (ph->terminator) { |
6284 | 0 | if (*v == ph->terminator) { |
6285 | 0 | v++; |
6286 | 0 | break; |
6287 | 0 | } |
6288 | 0 | } |
6289 | 0 | } while (ph->repeat && *v); |
6290 | 0 | if (ph->repeat) { |
6291 | 0 | ph->result[repeat_fixup] = repeats; |
6292 | 0 | } |
6293 | |
|
6294 | 0 | *v_in_out = v; |
6295 | 0 | return 1; |
6296 | 0 | } |
6297 | | |
6298 | | static void parse_hints_length_add_digit(struct parse_hints *ph, int digit) |
6299 | 0 | { |
6300 | 0 | int d = digit - '0'; |
6301 | |
|
6302 | 0 | if (ph->length <= (INT_MAX - d) / 10) |
6303 | 0 | ph->length = ph->length * 10 + d; |
6304 | 0 | else |
6305 | 0 | ph->length = INT_MAX; |
6306 | 0 | } |
6307 | | |
6308 | | const char *parse_octet_hint(const char *hint, const char *value, unsigned char **new_val, int *new_val_len) |
6309 | 0 | { |
6310 | 0 | const char *h = hint; |
6311 | 0 | const char *v = value; |
6312 | 0 | struct parse_hints ph; |
6313 | 0 | int retval = 1; |
6314 | | /* See RFC 1443 */ |
6315 | 0 | enum { |
6316 | 0 | HINT_1_2, |
6317 | 0 | HINT_2_3, |
6318 | 0 | HINT_1_2_4, |
6319 | 0 | HINT_1_2_5 |
6320 | 0 | } state = HINT_1_2; |
6321 | |
|
6322 | 0 | *new_val = NULL; |
6323 | 0 | *new_val_len = 0; |
6324 | 0 | parse_hints_ctor(&ph); |
6325 | 0 | while (*h && *v && retval) { |
6326 | 0 | switch (state) { |
6327 | 0 | case HINT_1_2: |
6328 | 0 | if ('*' == *h) { |
6329 | 0 | ph.repeat = 1; |
6330 | 0 | state = HINT_2_3; |
6331 | 0 | } else if (isdigit((unsigned char)(*h))) { |
6332 | 0 | parse_hints_length_add_digit(&ph, *h); |
6333 | 0 | state = HINT_2_3; |
6334 | 0 | } else { |
6335 | 0 | SNMP_FREE(ph.result); |
6336 | 0 | return v; /* failed */ |
6337 | 0 | } |
6338 | 0 | break; |
6339 | | |
6340 | 0 | case HINT_2_3: |
6341 | 0 | if (isdigit((unsigned char)(*h))) { |
6342 | 0 | parse_hints_length_add_digit(&ph, *h); |
6343 | | /* state = HINT_2_3 */ |
6344 | 0 | } else if ('x' == *h || 'd' == *h || 'o' == *h || 'a' == *h) { |
6345 | 0 | ph.format = *h; |
6346 | 0 | state = HINT_1_2_4; |
6347 | 0 | } else { |
6348 | 0 | SNMP_FREE(ph.result); |
6349 | 0 | return v; /* failed */ |
6350 | 0 | } |
6351 | 0 | break; |
6352 | | |
6353 | 0 | case HINT_1_2_4: |
6354 | 0 | if ('*' == *h) { |
6355 | 0 | retval = parse_hints_parse(&ph, &v); |
6356 | 0 | parse_hints_reset(&ph); |
6357 | | |
6358 | 0 | ph.repeat = 1; |
6359 | 0 | state = HINT_2_3; |
6360 | 0 | } else if (isdigit((unsigned char)(*h))) { |
6361 | 0 | retval = parse_hints_parse(&ph, &v); |
6362 | 0 | parse_hints_reset(&ph); |
6363 | | |
6364 | 0 | parse_hints_length_add_digit(&ph, *h); |
6365 | 0 | state = HINT_2_3; |
6366 | 0 | } else { |
6367 | 0 | ph.separator = *h; |
6368 | 0 | state = HINT_1_2_5; |
6369 | 0 | } |
6370 | 0 | break; |
6371 | | |
6372 | 0 | case HINT_1_2_5: |
6373 | 0 | if ('*' == *h) { |
6374 | 0 | retval = parse_hints_parse(&ph, &v); |
6375 | 0 | parse_hints_reset(&ph); |
6376 | | |
6377 | 0 | ph.repeat = 1; |
6378 | 0 | state = HINT_2_3; |
6379 | 0 | } else if (isdigit((unsigned char)(*h))) { |
6380 | 0 | retval = parse_hints_parse(&ph, &v); |
6381 | 0 | parse_hints_reset(&ph); |
6382 | | |
6383 | 0 | parse_hints_length_add_digit(&ph, *h); |
6384 | 0 | state = HINT_2_3; |
6385 | 0 | } else { |
6386 | 0 | ph.terminator = *h; |
6387 | |
|
6388 | 0 | retval = parse_hints_parse(&ph, &v); |
6389 | 0 | parse_hints_reset(&ph); |
6390 | |
|
6391 | 0 | state = HINT_1_2; |
6392 | 0 | } |
6393 | 0 | break; |
6394 | 0 | } |
6395 | 0 | h++; |
6396 | 0 | } |
6397 | 0 | while (*v && retval) { |
6398 | 0 | retval = parse_hints_parse(&ph, &v); |
6399 | 0 | } |
6400 | 0 | if (retval) { |
6401 | 0 | *new_val = ph.result; |
6402 | 0 | *new_val_len = ph.result_len; |
6403 | 0 | } else { |
6404 | 0 | if (ph.result) { |
6405 | 0 | SNMP_FREE(ph.result); |
6406 | 0 | } |
6407 | 0 | *new_val = NULL; |
6408 | 0 | *new_val_len = 0; |
6409 | 0 | } |
6410 | 0 | return retval ? NULL : v; |
6411 | 0 | } |
6412 | | #endif /* NETSNMP_DISABLE_MIB_LOADING */ |
6413 | | |
6414 | | #ifdef test_display_hint |
6415 | | |
6416 | | int main(int argc, const char **argv) |
6417 | | { |
6418 | | const char *hint; |
6419 | | const char *value; |
6420 | | unsigned char *new_val; |
6421 | | int new_val_len; |
6422 | | char *r; |
6423 | | |
6424 | | if (argc < 3) { |
6425 | | fprintf(stderr, "usage: dh <hint> <value>\n"); |
6426 | | exit(2); |
6427 | | } |
6428 | | hint = argv[1]; |
6429 | | value = argv[2]; |
6430 | | r = parse_octet_hint(hint, value, &new_val, &new_val_len); |
6431 | | printf("{\"%s\", \"%s\"}: \n\t", hint, value); |
6432 | | if (r) { |
6433 | | *r = 0; |
6434 | | printf("returned failed\n"); |
6435 | | printf("value syntax error at: %s\n", value); |
6436 | | } |
6437 | | else { |
6438 | | int i; |
6439 | | printf("returned success\n"); |
6440 | | for (i = 0; i < new_val_len; i++) { |
6441 | | int c = new_val[i] & 0xFF; |
6442 | | printf("%02X(%c) ", c, isprint(c) ? c : ' '); |
6443 | | } |
6444 | | SNMP_FREE(new_val); |
6445 | | } |
6446 | | printf("\n"); |
6447 | | exit(0); |
6448 | | } |
6449 | | |
6450 | | #endif /* test_display_hint */ |
6451 | | |
6452 | | #ifndef NETSNMP_FEATURE_REMOVE_MIB_TO_ASN_TYPE |
6453 | | u_char |
6454 | | mib_to_asn_type(int mib_type) |
6455 | 0 | { |
6456 | 0 | switch (mib_type) { |
6457 | 0 | case TYPE_OBJID: |
6458 | 0 | return ASN_OBJECT_ID; |
6459 | | |
6460 | 0 | case TYPE_OCTETSTR: |
6461 | 0 | return ASN_OCTET_STR; |
6462 | | |
6463 | 0 | case TYPE_NETADDR: |
6464 | 0 | case TYPE_IPADDR: |
6465 | 0 | return ASN_IPADDRESS; |
6466 | | |
6467 | 0 | case TYPE_INTEGER32: |
6468 | 0 | case TYPE_INTEGER: |
6469 | 0 | return ASN_INTEGER; |
6470 | | |
6471 | 0 | case TYPE_COUNTER: |
6472 | 0 | return ASN_COUNTER; |
6473 | | |
6474 | 0 | case TYPE_GAUGE: |
6475 | 0 | return ASN_GAUGE; |
6476 | | |
6477 | 0 | case TYPE_TIMETICKS: |
6478 | 0 | return ASN_TIMETICKS; |
6479 | | |
6480 | 0 | case TYPE_OPAQUE: |
6481 | 0 | return ASN_OPAQUE; |
6482 | | |
6483 | 0 | case TYPE_NULL: |
6484 | 0 | return ASN_NULL; |
6485 | | |
6486 | 0 | case TYPE_COUNTER64: |
6487 | 0 | return ASN_COUNTER64; |
6488 | | |
6489 | 0 | case TYPE_BITSTRING: |
6490 | 0 | return ASN_BIT_STR; |
6491 | | |
6492 | 0 | case TYPE_UINTEGER: |
6493 | 0 | case TYPE_UNSIGNED32: |
6494 | 0 | return ASN_UNSIGNED; |
6495 | | |
6496 | 0 | case TYPE_NSAPADDRESS: |
6497 | 0 | return ASN_NSAP; |
6498 | |
|
6499 | 0 | } |
6500 | 0 | return -1; |
6501 | 0 | } |
6502 | | #endif /* NETSNMP_FEATURE_REMOVE_MIB_TO_ASN_TYPE */ |
6503 | | |
6504 | | /** |
6505 | | * Converts a string to its OID form. |
6506 | | * in example "hello" = 5 . 'h' . 'e' . 'l' . 'l' . 'o' |
6507 | | * |
6508 | | * @param S The string. |
6509 | | * @param O The oid. |
6510 | | * @param L The length of the oid. |
6511 | | * |
6512 | | * @return 0 on success, 1 on failure. |
6513 | | */ |
6514 | | #ifndef NETSNMP_FEATURE_REMOVE_MIB_STRING_CONVERSIONS |
6515 | | int |
6516 | | netsnmp_str2oid(const char *S, oid * O, int L) |
6517 | 0 | { |
6518 | 0 | const char *c = S; |
6519 | 0 | oid *o = &O[1]; |
6520 | |
|
6521 | 0 | --L; /* leave room for length prefix */ |
6522 | |
|
6523 | 0 | for (; *c && L; --L, ++o, ++c) |
6524 | 0 | *o = *c; |
6525 | | |
6526 | | /* |
6527 | | * make sure we got to the end of the string |
6528 | | */ |
6529 | 0 | if (*c != 0) |
6530 | 0 | return 1; |
6531 | | |
6532 | | /* |
6533 | | * set the length of the oid |
6534 | | */ |
6535 | 0 | *O = c - S; |
6536 | |
|
6537 | 0 | return 0; |
6538 | 0 | } |
6539 | | |
6540 | | /** |
6541 | | * Converts an OID to its character form. |
6542 | | * in example 5 . 1 . 2 . 3 . 4 . 5 = 12345 |
6543 | | * |
6544 | | * @param C The character buffer. |
6545 | | * @param L The length of the buffer. |
6546 | | * @param O The oid. |
6547 | | * |
6548 | | * @return 0 on success, 1 on failure. |
6549 | | */ |
6550 | | int |
6551 | | netsnmp_oid2chars(char *C, int L, const oid * O) |
6552 | 0 | { |
6553 | 0 | char *c = C; |
6554 | 0 | const oid *o = &O[1]; |
6555 | |
|
6556 | 0 | if (L < (int)*O) |
6557 | 0 | return 1; |
6558 | | |
6559 | 0 | L = *O; /** length */ |
6560 | 0 | for (; L; --L, ++o, ++c) { |
6561 | 0 | if (*o > 0xFF) |
6562 | 0 | return 1; |
6563 | 0 | *c = (char)*o; |
6564 | 0 | } |
6565 | 0 | return 0; |
6566 | 0 | } |
6567 | | |
6568 | | /** |
6569 | | * Converts an OID to its string form. |
6570 | | * in example 5 . 'h' . 'e' . 'l' . 'l' . 'o' = "hello\0" (null terminated) |
6571 | | * |
6572 | | * @param S The character string buffer. |
6573 | | * @param L The length of the string buffer. |
6574 | | * @param O The oid. |
6575 | | * |
6576 | | * @return 0 on success, 1 on failure. |
6577 | | */ |
6578 | | int |
6579 | | netsnmp_oid2str(char *S, int L, oid * O) |
6580 | 0 | { |
6581 | 0 | int rc; |
6582 | |
|
6583 | 0 | if (L <= (int)*O) |
6584 | 0 | return 1; |
6585 | | |
6586 | 0 | rc = netsnmp_oid2chars(S, L, O); |
6587 | 0 | if (rc) |
6588 | 0 | return 1; |
6589 | | |
6590 | 0 | S[ *O ] = 0; |
6591 | |
|
6592 | 0 | return 0; |
6593 | 0 | } |
6594 | | #endif /* NETSNMP_FEATURE_REMOVE_MIB_STRING_CONVERSIONS */ |
6595 | | |
6596 | | |
6597 | | #ifndef NETSNMP_FEATURE_REMOVE_MIB_SNPRINT |
6598 | | int |
6599 | | snprint_by_type(char *buf, size_t buf_len, |
6600 | | netsnmp_variable_list * var, |
6601 | | const struct enum_list *enums, |
6602 | | const char *hint, const char *units) |
6603 | 0 | { |
6604 | 0 | size_t out_len = 0; |
6605 | 0 | if (sprint_realloc_by_type((u_char **) & buf, &buf_len, &out_len, 0, |
6606 | 0 | var, enums, hint, units)) |
6607 | 0 | return (int) out_len; |
6608 | 0 | else |
6609 | 0 | return -1; |
6610 | 0 | } |
6611 | | |
6612 | | int |
6613 | | snprint_hexstring(char *buf, size_t buf_len, const u_char * cp, size_t len) |
6614 | 0 | { |
6615 | 0 | size_t out_len = 0; |
6616 | 0 | if (sprint_realloc_hexstring((u_char **) & buf, &buf_len, &out_len, 0, |
6617 | 0 | cp, len)) |
6618 | 0 | return (int) out_len; |
6619 | 0 | else |
6620 | 0 | return -1; |
6621 | 0 | } |
6622 | | |
6623 | | int |
6624 | | snprint_asciistring(char *buf, size_t buf_len, |
6625 | | const u_char * cp, size_t len) |
6626 | 0 | { |
6627 | 0 | size_t out_len = 0; |
6628 | 0 | if (sprint_realloc_asciistring |
6629 | 0 | ((u_char **) & buf, &buf_len, &out_len, 0, cp, len)) |
6630 | 0 | return (int) out_len; |
6631 | 0 | else |
6632 | 0 | return -1; |
6633 | 0 | } |
6634 | | |
6635 | | int |
6636 | | snprint_octet_string(char *buf, size_t buf_len, |
6637 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6638 | | const char *hint, const char *units) |
6639 | 0 | { |
6640 | 0 | size_t out_len = 0; |
6641 | 0 | if (sprint_realloc_octet_string |
6642 | 0 | ((u_char **) & buf, &buf_len, &out_len, 0, var, enums, hint, |
6643 | 0 | units)) |
6644 | 0 | return (int) out_len; |
6645 | 0 | else |
6646 | 0 | return -1; |
6647 | 0 | } |
6648 | | |
6649 | | int |
6650 | | snprint_opaque(char *buf, size_t buf_len, |
6651 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6652 | | const char *hint, const char *units) |
6653 | 0 | { |
6654 | 0 | size_t out_len = 0; |
6655 | 0 | if (sprint_realloc_opaque((u_char **) & buf, &buf_len, &out_len, 0, |
6656 | 0 | var, enums, hint, units)) |
6657 | 0 | return (int) out_len; |
6658 | 0 | else |
6659 | 0 | return -1; |
6660 | 0 | } |
6661 | | |
6662 | | int |
6663 | | snprint_object_identifier(char *buf, size_t buf_len, |
6664 | | const netsnmp_variable_list * var, |
6665 | | const struct enum_list *enums, const char *hint, |
6666 | | const char *units) |
6667 | 0 | { |
6668 | 0 | size_t out_len = 0; |
6669 | 0 | if (sprint_realloc_object_identifier |
6670 | 0 | ((u_char **) & buf, &buf_len, &out_len, 0, var, enums, hint, |
6671 | 0 | units)) |
6672 | 0 | return (int) out_len; |
6673 | 0 | else |
6674 | 0 | return -1; |
6675 | 0 | } |
6676 | | |
6677 | | int |
6678 | | snprint_timeticks(char *buf, size_t buf_len, |
6679 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6680 | | const char *hint, const char *units) |
6681 | 0 | { |
6682 | 0 | size_t out_len = 0; |
6683 | 0 | if (sprint_realloc_timeticks((u_char **) & buf, &buf_len, &out_len, 0, |
6684 | 0 | var, enums, hint, units)) |
6685 | 0 | return (int) out_len; |
6686 | 0 | else |
6687 | 0 | return -1; |
6688 | 0 | } |
6689 | | |
6690 | | int |
6691 | | snprint_hinted_integer(char *buf, size_t buf_len, |
6692 | | long val, const char *hint, const char *units) |
6693 | 0 | { |
6694 | 0 | size_t out_len = 0; |
6695 | 0 | if (sprint_realloc_hinted_integer |
6696 | 0 | ((u_char **) & buf, &buf_len, &out_len, 0, val, 'd', hint, units)) |
6697 | 0 | return (int) out_len; |
6698 | 0 | else |
6699 | 0 | return -1; |
6700 | 0 | } |
6701 | | |
6702 | | int |
6703 | | snprint_integer(char *buf, size_t buf_len, |
6704 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6705 | | const char *hint, const char *units) |
6706 | 0 | { |
6707 | 0 | size_t out_len = 0; |
6708 | 0 | if (sprint_realloc_integer((u_char **) & buf, &buf_len, &out_len, 0, |
6709 | 0 | var, enums, hint, units)) |
6710 | 0 | return (int) out_len; |
6711 | 0 | else |
6712 | 0 | return -1; |
6713 | 0 | } |
6714 | | |
6715 | | int |
6716 | | snprint_uinteger(char *buf, size_t buf_len, |
6717 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6718 | | const char *hint, const char *units) |
6719 | 0 | { |
6720 | 0 | size_t out_len = 0; |
6721 | 0 | if (sprint_realloc_uinteger((u_char **) & buf, &buf_len, &out_len, 0, |
6722 | 0 | var, enums, hint, units)) |
6723 | 0 | return (int) out_len; |
6724 | 0 | else |
6725 | 0 | return -1; |
6726 | 0 | } |
6727 | | |
6728 | | int |
6729 | | snprint_gauge(char *buf, size_t buf_len, |
6730 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6731 | | const char *hint, const char *units) |
6732 | 0 | { |
6733 | 0 | size_t out_len = 0; |
6734 | 0 | if (sprint_realloc_gauge((u_char **) & buf, &buf_len, &out_len, 0, |
6735 | 0 | var, enums, hint, units)) |
6736 | 0 | return (int) out_len; |
6737 | 0 | else |
6738 | 0 | return -1; |
6739 | 0 | } |
6740 | | |
6741 | | int |
6742 | | snprint_counter(char *buf, size_t buf_len, |
6743 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6744 | | const char *hint, const char *units) |
6745 | 0 | { |
6746 | 0 | size_t out_len = 0; |
6747 | 0 | if (sprint_realloc_counter((u_char **) & buf, &buf_len, &out_len, 0, |
6748 | 0 | var, enums, hint, units)) |
6749 | 0 | return (int) out_len; |
6750 | 0 | else |
6751 | 0 | return -1; |
6752 | 0 | } |
6753 | | |
6754 | | int |
6755 | | snprint_networkaddress(char *buf, size_t buf_len, |
6756 | | const netsnmp_variable_list * var, |
6757 | | const struct enum_list *enums, const char *hint, |
6758 | | const char *units) |
6759 | 0 | { |
6760 | 0 | size_t out_len = 0; |
6761 | 0 | if (sprint_realloc_networkaddress |
6762 | 0 | ((u_char **) & buf, &buf_len, &out_len, 0, var, enums, hint, |
6763 | 0 | units)) |
6764 | 0 | return (int) out_len; |
6765 | 0 | else |
6766 | 0 | return -1; |
6767 | 0 | } |
6768 | | |
6769 | | int |
6770 | | snprint_ipaddress(char *buf, size_t buf_len, |
6771 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6772 | | const char *hint, const char *units) |
6773 | 0 | { |
6774 | 0 | size_t out_len = 0; |
6775 | 0 | if (sprint_realloc_ipaddress((u_char **) & buf, &buf_len, &out_len, 0, |
6776 | 0 | var, enums, hint, units)) |
6777 | 0 | return (int) out_len; |
6778 | 0 | else |
6779 | 0 | return -1; |
6780 | 0 | } |
6781 | | |
6782 | | int |
6783 | | snprint_null(char *buf, size_t buf_len, |
6784 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6785 | | const char *hint, const char *units) |
6786 | 0 | { |
6787 | 0 | size_t out_len = 0; |
6788 | 0 | if (sprint_realloc_null((u_char **) & buf, &buf_len, &out_len, 0, |
6789 | 0 | var, enums, hint, units)) |
6790 | 0 | return (int) out_len; |
6791 | 0 | else |
6792 | 0 | return -1; |
6793 | 0 | } |
6794 | | |
6795 | | int |
6796 | | snprint_bitstring(char *buf, size_t buf_len, |
6797 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6798 | | const char *hint, const char *units) |
6799 | 0 | { |
6800 | 0 | size_t out_len = 0; |
6801 | 0 | if (sprint_realloc_bitstring((u_char **) & buf, &buf_len, &out_len, 0, |
6802 | 0 | var, enums, hint, units)) |
6803 | 0 | return (int) out_len; |
6804 | 0 | else |
6805 | 0 | return -1; |
6806 | 0 | } |
6807 | | |
6808 | | int |
6809 | | snprint_nsapaddress(char *buf, size_t buf_len, |
6810 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6811 | | const char *hint, const char *units) |
6812 | 0 | { |
6813 | 0 | size_t out_len = 0; |
6814 | 0 | if (sprint_realloc_nsapaddress |
6815 | 0 | ((u_char **) & buf, &buf_len, &out_len, 0, var, enums, hint, |
6816 | 0 | units)) |
6817 | 0 | return (int) out_len; |
6818 | 0 | else |
6819 | 0 | return -1; |
6820 | 0 | } |
6821 | | |
6822 | | int |
6823 | | snprint_counter64(char *buf, size_t buf_len, |
6824 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6825 | | const char *hint, const char *units) |
6826 | 0 | { |
6827 | 0 | size_t out_len = 0; |
6828 | 0 | if (sprint_realloc_counter64((u_char **) & buf, &buf_len, &out_len, 0, |
6829 | 0 | var, enums, hint, units)) |
6830 | 0 | return (int) out_len; |
6831 | 0 | else |
6832 | 0 | return -1; |
6833 | 0 | } |
6834 | | |
6835 | | int |
6836 | | snprint_badtype(char *buf, size_t buf_len, |
6837 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6838 | | const char *hint, const char *units) |
6839 | 0 | { |
6840 | 0 | size_t out_len = 0; |
6841 | 0 | if (sprint_realloc_badtype((u_char **) & buf, &buf_len, &out_len, 0, |
6842 | 0 | var, enums, hint, units)) |
6843 | 0 | return (int) out_len; |
6844 | 0 | else |
6845 | 0 | return -1; |
6846 | 0 | } |
6847 | | |
6848 | | #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES |
6849 | | int |
6850 | | snprint_float(char *buf, size_t buf_len, |
6851 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6852 | | const char *hint, const char *units) |
6853 | 0 | { |
6854 | 0 | size_t out_len = 0; |
6855 | 0 | if (sprint_realloc_float((u_char **) & buf, &buf_len, &out_len, 0, |
6856 | 0 | var, enums, hint, units)) |
6857 | 0 | return (int) out_len; |
6858 | 0 | else |
6859 | 0 | return -1; |
6860 | 0 | } |
6861 | | |
6862 | | int |
6863 | | snprint_double(char *buf, size_t buf_len, |
6864 | | const netsnmp_variable_list * var, const struct enum_list *enums, |
6865 | | const char *hint, const char *units) |
6866 | 0 | { |
6867 | 0 | size_t out_len = 0; |
6868 | 0 | if (sprint_realloc_double((u_char **) & buf, &buf_len, &out_len, 0, |
6869 | 0 | var, enums, hint, units)) |
6870 | 0 | return (int) out_len; |
6871 | 0 | else |
6872 | 0 | return -1; |
6873 | 0 | } |
6874 | | #endif |
6875 | | #endif /* NETSNMP_FEATURE_REMOVE_MIB_SNPRINT */ |
6876 | | /** @} */ |
6877 | | |