Coverage Report

Created: 2024-09-08 06:40

/src/net-snmp/snmplib/mib.c
Line
Count
Source (jump to first uncovered line)
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
2
{
215
2
    int             centisecs, seconds, minutes, hours, days;
216
217
2
    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
2
    centisecs = timeticks % 100;
224
2
    timeticks /= 100;
225
2
    days = timeticks / (60 * 60 * 24);
226
2
    timeticks %= (60 * 60 * 24);
227
228
2
    hours = timeticks / (60 * 60);
229
2
    timeticks %= (60 * 60);
230
231
2
    minutes = timeticks / 60;
232
2
    seconds = timeticks % 60;
233
234
2
    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
2
    else {
238
2
        if (days == 0) {
239
2
            snprintf(buf, buflen, "%d:%02d:%02d.%02d",
240
2
                    hours, minutes, seconds, centisecs);
241
2
        } 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
2
    }
249
2
    return buf;
250
2
}
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
5.72k
{
297
5.72k
    const u_char   *tp;
298
5.72k
    const u_char   *cp2 = cp;
299
5.72k
    size_t          lenleft = line_len;
300
301
    /*
302
     * Make sure there's enough room for the hex output....
303
     */
304
10.9k
    while ((*out_len + line_len*3+1) >= *buf_len) {
305
5.17k
        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
306
0
            return 0;
307
0
        }
308
5.17k
    }
309
310
    /*
311
     * .... and display the hex values themselves....
312
     */
313
7.87k
    for (; lenleft >= 8; lenleft-=8) {
314
2.14k
        sprintf((char *) (*buf + *out_len),
315
2.14k
                "%02X %02X %02X %02X %02X %02X %02X %02X ", cp[0], cp[1],
316
2.14k
                cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);
317
2.14k
        *out_len += strlen((char *) (*buf + *out_len));
318
2.14k
        cp       += 8;
319
2.14k
    }
320
24.5k
    for (; lenleft > 0; lenleft--) {
321
18.8k
        sprintf((char *) (*buf + *out_len), "%02X ", *cp++);
322
18.8k
        *out_len += strlen((char *) (*buf + *out_len));
323
18.8k
    }
324
325
    /*
326
     * .... plus (optionally) do the same for the ASCII equivalent.
327
     */
328
5.72k
    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
        sprintf((char *) (*buf + *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
        sprintf((char *) (*buf + *out_len), "]");
341
0
        *out_len += strlen((char *) (*buf + *out_len));
342
0
    }
343
5.72k
    return 1;
344
5.72k
}
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
5.27k
{
350
5.27k
    int line_len = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
351
5.27k
                                      NETSNMP_DS_LIB_HEX_OUTPUT_LENGTH);
352
5.27k
    if (line_len <= 0)
353
158
        line_len = len;
354
355
5.72k
    for (; (int)len > line_len; len -= line_len) {
356
451
        if(!_sprint_hexstring_line(buf, buf_len, out_len, allow_realloc, cp, line_len))
357
0
            return 0;
358
451
        *(*buf + (*out_len)++) = '\n';
359
451
        *(*buf + *out_len) = 0;
360
451
        cp += line_len;
361
451
    }
362
5.27k
    if(!_sprint_hexstring_line(buf, buf_len, out_len, allow_realloc, cp, len))
363
0
        return 0;
364
5.27k
    *(*buf + *out_len) = 0;
365
5.27k
    return 1;
366
5.27k
}
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
1.23k
{
394
1.23k
    int             i;
395
396
11.1k
    for (i = 0; i < (int) len; i++) {
397
9.93k
        if (isprint(*cp) || isspace(*cp)) {
398
5.95k
            if (*cp == '\\' || *cp == '"') {
399
1.88k
                if ((*out_len >= *buf_len) &&
400
1.88k
                    !(allow_realloc && snmp_realloc(buf, buf_len))) {
401
0
                    return 0;
402
0
                }
403
1.88k
                *(*buf + (*out_len)++) = '\\';
404
1.88k
            }
405
5.95k
            if ((*out_len >= *buf_len) &&
406
5.95k
                !(allow_realloc && snmp_realloc(buf, buf_len))) {
407
0
                return 0;
408
0
            }
409
5.95k
            *(*buf + (*out_len)++) = *cp++;
410
5.95k
        } else {
411
3.97k
            if ((*out_len >= *buf_len) &&
412
3.97k
                !(allow_realloc && snmp_realloc(buf, buf_len))) {
413
0
                return 0;
414
0
            }
415
3.97k
            *(*buf + (*out_len)++) = '.';
416
3.97k
            cp++;
417
3.97k
        }
418
9.93k
    }
419
1.23k
    if ((*out_len >= *buf_len) &&
420
1.23k
        !(allow_realloc && snmp_realloc(buf, buf_len))) {
421
0
        return 0;
422
0
    }
423
1.23k
    *(*buf + *out_len) = '\0';
424
1.23k
    return 1;
425
1.23k
}
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
153
{
456
153
    size_t          saved_out_len = *out_len;
457
153
    const char     *saved_hint = hint;
458
153
    int             hex = 0, x = 0;
459
153
    u_char         *cp;
460
153
    int             output_format, cnt;
461
462
153
    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
153
    if (hint) {
476
0
        int             repeat, width = 1;
477
0
        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
                while ('0' <= *hint && *hint <= '9')
501
0
                    width = (width * 10) + (*hint++ - '0');
502
0
                code = *hint++;
503
0
                if ((ch = *hint) && ch != '*' && (ch < '0' || ch > '9')
504
0
                    && (width != 0
505
0
                        || (ch != 'x' && ch != 'd' && ch != 'o')))
506
0
                    separ = *hint++;
507
0
                else
508
0
                    separ = 0;
509
0
                if ((ch = *hint) && ch != '*' && (ch < '0' || ch > '9')
510
0
                    && (width != 0
511
0
                        || (ch != 'x' && ch != 'd' && ch != 'o')))
512
0
                    term = *hint++;
513
0
                else
514
0
                    term = 0;
515
0
                if (width == 0)  /* Handle malformed hint strings */
516
0
                    width = 1;
517
0
            }
518
519
0
            while (repeat && cp < ecp) {
520
0
                value = 0;
521
0
                if (code != 'a' && code != 't') {
522
0
                    for (x = 0; x < width; x++) {
523
0
                        value = value * 256 + *cp++;
524
0
                    }
525
0
                }
526
0
                switch (code) {
527
0
                case 'x':
528
0
                    if (HEX2DIGIT_NEED_INIT == hex2digit)
529
0
                        hex2digit = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
530
0
                                                           NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT);
531
                    /*
532
                     * if value is < 16, it will be a single hex digit. If the
533
                     * width is 1 (we are outputting a byte at a time), pat it
534
                     * to 2 digits if NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT is set
535
                     * or all of the following are true:
536
                     *  - we do not have a separation character
537
                     *  - there is no hint left (or there never was a hint)
538
                     *
539
                     * e.g. for the data 0xAA01BB, would anyone really ever
540
                     * want the string "AA1BB"??
541
                     */
542
0
                    if (((value < 16) && (1 == width)) &&
543
0
                        (hex2digit || ((0 == separ) && (0 == *hint)))) {
544
0
                        sprintf(intbuf, "0%lx", value);
545
0
                    } else {
546
0
                        sprintf(intbuf, "%lx", value);
547
0
                    }
548
0
                    if (!snmp_cstrcat
549
0
                        (buf, buf_len, out_len, allow_realloc, intbuf)) {
550
0
                        return 0;
551
0
                    }
552
0
                    break;
553
0
                case 'd':
554
0
                    sprintf(intbuf, "%ld", value);
555
0
                    if (!snmp_cstrcat
556
0
                        (buf, buf_len, out_len, allow_realloc, intbuf)) {
557
0
                        return 0;
558
0
                    }
559
0
                    break;
560
0
                case 'o':
561
0
                    sprintf(intbuf, "%lo", value);
562
0
                    if (!snmp_cstrcat
563
0
                        (buf, buf_len, out_len, allow_realloc, intbuf)) {
564
0
                        return 0;
565
0
                    }
566
0
                    break;
567
0
                case 't': /* new in rfc 3411 */
568
0
                case 'a':
569
                    /* A string hint gives the max size - we may not need this much */
570
0
                    cnt = SNMP_MIN(width, ecp - cp);
571
0
                    while ((*out_len + cnt + 1) > *buf_len) {
572
0
                        if (!allow_realloc || !snmp_realloc(buf, buf_len))
573
0
                            return 0;
574
0
                    }
575
0
                    if (memchr(cp, '\0', cnt) == NULL) {
576
                        /* No embedded '\0' - use memcpy() to preserve UTF-8 */
577
0
                        memcpy(*buf + *out_len, cp, cnt);
578
0
                        *out_len += cnt;
579
0
                        *(*buf + *out_len) = '\0';
580
0
                    } else if (!sprint_realloc_asciistring(buf, buf_len,
581
0
                                     out_len, allow_realloc, cp, cnt)) {
582
0
                        return 0;
583
0
                    }
584
0
                    cp += cnt;
585
0
                    break;
586
0
                default:
587
0
                    *out_len = saved_out_len;
588
0
                    if (snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
589
0
                                     "(Bad hint ignored: ")
590
0
                        && snmp_cstrcat(buf, buf_len, out_len,
591
0
                                       allow_realloc, saved_hint)
592
0
                        && snmp_cstrcat(buf, buf_len, out_len,
593
0
                                       allow_realloc, ") ")) {
594
0
                        return sprint_realloc_octet_string(buf, buf_len,
595
0
                                                           out_len,
596
0
                                                           allow_realloc,
597
0
                                                           var, enums,
598
0
                                                           NULL, NULL);
599
0
                    } else {
600
0
                        return 0;
601
0
                    }
602
0
                }
603
604
0
                if (cp < ecp && separ) {
605
0
                    while ((*out_len + 1) >= *buf_len) {
606
0
                        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
607
0
                            return 0;
608
0
                        }
609
0
                    }
610
0
                    *(*buf + *out_len) = separ;
611
0
                    (*out_len)++;
612
0
                    *(*buf + *out_len) = '\0';
613
0
                }
614
0
                repeat--;
615
0
            }
616
617
0
            if (term && cp < ecp) {
618
0
                while ((*out_len + 1) >= *buf_len) {
619
0
                    if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
620
0
                        return 0;
621
0
                    }
622
0
                }
623
0
                *(*buf + *out_len) = term;
624
0
                (*out_len)++;
625
0
                *(*buf + *out_len) = '\0';
626
0
            }
627
0
        }
628
629
0
        if (units) {
630
0
            return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
631
0
                    snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
632
0
        }
633
0
        if ((*out_len >= *buf_len) &&
634
0
            !(allow_realloc && snmp_realloc(buf, buf_len))) {
635
0
            return 0;
636
0
        }
637
0
        *(*buf + *out_len) = '\0';
638
639
0
        return 1;
640
0
    }
641
642
153
    output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT);
643
153
    if (0 == output_format) {
644
153
        output_format = NETSNMP_STRING_OUTPUT_GUESS;
645
153
    }
646
153
    switch (output_format) {
647
153
    case NETSNMP_STRING_OUTPUT_GUESS:
648
153
        hex = 0;
649
7.47k
        for (cp = var->val.string, x = 0; x < (int) var->val_len; x++, cp++) {
650
7.32k
            if ((!isprint(*cp) || !isascii(*cp)) && !isspace(*cp)) {
651
1.43k
                hex = 1;
652
1.43k
            }
653
7.32k
        }
654
153
        break;
655
656
0
    case NETSNMP_STRING_OUTPUT_ASCII:
657
0
        hex = 0;
658
0
        break;
659
660
0
    case NETSNMP_STRING_OUTPUT_HEX:
661
0
        hex = 1;
662
0
        break;
663
153
    }
664
665
153
    if (var->val_len == 0) {
666
15
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"\"");
667
15
    }
668
669
138
    if (hex) {
670
102
        if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
671
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"")) {
672
0
                return 0;
673
0
            }
674
102
        } else {
675
102
            if (!snmp_cstrcat
676
102
                (buf, buf_len, out_len, allow_realloc, "Hex-STRING: ")) {
677
0
                return 0;
678
0
            }
679
102
        }
680
681
102
        if (!sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc,
682
102
                                      var->val.string, var->val_len)) {
683
0
            return 0;
684
0
        }
685
686
102
        if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
687
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"")) {
688
0
                return 0;
689
0
            }
690
0
        }
691
102
    } else {
692
36
        if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
693
36
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
694
36
                             "STRING: ")) {
695
0
                return 0;
696
0
            }
697
36
        }
698
36
        if (!snmp_cstrcat
699
36
            (buf, buf_len, out_len, allow_realloc, "\"")) {
700
0
            return 0;
701
0
        }
702
36
        if (!sprint_realloc_asciistring
703
36
            (buf, buf_len, out_len, allow_realloc, var->val.string,
704
36
             var->val_len)) {
705
0
            return 0;
706
0
        }
707
36
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\"")) {
708
0
            return 0;
709
0
        }
710
36
    }
711
712
138
    if (units) {
713
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
714
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
715
0
    }
716
138
    return 1;
717
138
}
718
719
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
720
721
/**
722
 * Prints a float into a buffer.
723
 *
724
 * The variable var is encoded as a floating point value.
725
 * 
726
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
727
 * needed size. (Note: *buf may change due to this.)
728
 * 
729
 * @param buf      Address of the buffer to print to.
730
 * @param buf_len  Address to an integer containing the size of buf.
731
 * @param out_len  Incremented by the number of characters printed.
732
 * @param allow_realloc if not zero reallocate the buffer to fit the 
733
 *                      needed size.
734
 * @param var      The variable to encode.
735
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
736
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
737
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
738
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
739
 * 
740
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
741
 *         small when not allowed to realloc.)
742
 */
743
int
744
sprint_realloc_float(u_char ** buf, size_t * buf_len,
745
                     size_t * out_len, int allow_realloc,
746
                     const netsnmp_variable_list * var,
747
                     const struct enum_list *enums,
748
                     const char *hint, const char *units)
749
23
{
750
23
    char *printf_format_string = NULL;
751
752
23
    if (var->type != ASN_OPAQUE_FLOAT) {
753
0
        if (!netsnmp_ds_get_boolean(
754
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
755
0
            static const char str[] = "Wrong Type (should be Float): ";
756
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
757
0
                return 0;
758
0
        }
759
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
760
0
                                          allow_realloc, var, NULL, NULL,
761
0
                                          NULL);
762
0
    }
763
764
23
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
765
23
        if (!snmp_cstrcat
766
23
            (buf, buf_len, out_len, allow_realloc, "Opaque: Float: ")) {
767
0
            return 0;
768
0
        }
769
23
    }
770
771
772
    /*
773
     * How much space needed for max. length float?  128 is overkill.  
774
     */
775
776
38
    while ((*out_len + 128 + 1) >= *buf_len) {
777
15
        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
778
0
            return 0;
779
0
        }
780
15
    }
781
782
23
    printf_format_string = make_printf_format_string("%f");
783
23
    if (!printf_format_string) {
784
0
        return 0;
785
0
    }
786
23
    snprintf((char *)(*buf + *out_len), 128, printf_format_string, *var->val.floatVal);
787
23
    free(printf_format_string);
788
23
    *out_len += strlen((char *) (*buf + *out_len));
789
790
23
    if (units) {
791
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
792
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
793
0
    }
794
23
    return 1;
795
23
}
796
797
798
/**
799
 * Prints a double into a buffer.
800
 *
801
 * The variable var is encoded as a double precision floating point value.
802
 * 
803
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
804
 * needed size. (Note: *buf may change due to this.)
805
 * 
806
 * @param buf      Address of the buffer to print to.
807
 * @param buf_len  Address to an integer containing the size of buf.
808
 * @param out_len  Incremented by the number of characters printed.
809
 * @param allow_realloc if not zero reallocate the buffer to fit the 
810
 *                      needed size.
811
 * @param var      The variable to encode.
812
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
813
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
814
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
815
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
816
 * 
817
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
818
 *         small when not allowed to realloc.)
819
 */
820
int
821
sprint_realloc_double(u_char ** buf, size_t * buf_len,
822
                      size_t * out_len, int allow_realloc,
823
                      const netsnmp_variable_list * var,
824
                      const struct enum_list *enums,
825
                      const char *hint, const char *units)
826
14
{
827
14
    char *printf_format_string = NULL;
828
829
14
    if (var->type != ASN_OPAQUE_DOUBLE) {
830
0
        if (!netsnmp_ds_get_boolean(
831
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
832
0
            static const char str[] = "Wrong Type (should be Double): ";
833
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
834
0
                return 0;
835
0
        }
836
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
837
0
                                          allow_realloc, var, NULL, NULL,
838
0
                                          NULL);
839
0
    }
840
841
14
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
842
14
        if (!snmp_cstrcat
843
14
            (buf, buf_len, out_len, allow_realloc, "Opaque: Float: ")) {
844
0
            return 0;
845
0
        }
846
14
    }
847
848
    /*
849
     * How much space needed for max. length double?  128 is overkill.  
850
     */
851
852
23
    while ((*out_len + 128 + 1) >= *buf_len) {
853
9
        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
854
0
            return 0;
855
0
        }
856
9
    }
857
858
14
    printf_format_string = make_printf_format_string("%f");
859
14
    if (!printf_format_string) {
860
0
        return 0;
861
0
    }
862
14
    snprintf((char *)(*buf + *out_len), 128, printf_format_string, *var->val.doubleVal);
863
14
    free(printf_format_string);
864
14
    *out_len += strlen((char *) (*buf + *out_len));
865
866
14
    if (units) {
867
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
868
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
869
0
    }
870
14
    return 1;
871
14
}
872
873
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
874
875
876
/**
877
 * Prints a counter into a buffer.
878
 *
879
 * The variable var is encoded as a counter value.
880
 * 
881
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
882
 * needed size. (Note: *buf may change due to this.)
883
 * 
884
 * @param buf      Address of the buffer to print to.
885
 * @param buf_len  Address to an integer containing the size of buf.
886
 * @param out_len  Incremented by the number of characters printed.
887
 * @param allow_realloc if not zero reallocate the buffer to fit the 
888
 *                      needed size.
889
 * @param var      The variable to encode.
890
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
891
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
892
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
893
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
894
 * 
895
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
896
 *         small when not allowed to realloc.)
897
 */
898
int
899
sprint_realloc_counter64(u_char ** buf, size_t * buf_len, size_t * out_len,
900
                         int allow_realloc,
901
                         const netsnmp_variable_list * var,
902
                         const struct enum_list *enums,
903
                         const char *hint, const char *units)
904
3
{
905
3
    char            a64buf[I64CHARSZ + 1];
906
907
3
    if (var->type != ASN_COUNTER64
908
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
909
3
        && var->type != ASN_OPAQUE_COUNTER64
910
3
        && var->type != ASN_OPAQUE_I64 && var->type != ASN_OPAQUE_U64
911
3
#endif
912
3
        ) {
913
0
        if (!netsnmp_ds_get_boolean(
914
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
915
0
            static const char str[] = "Wrong Type (should be Counter64): ";
916
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
917
0
                return 0;
918
0
        }
919
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
920
0
                                          allow_realloc, var, NULL, NULL,
921
0
                                          NULL);
922
0
    }
923
924
3
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
925
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
926
3
        if (var->type != ASN_COUNTER64) {
927
0
            if (!snmp_cstrcat
928
0
                (buf, buf_len, out_len, allow_realloc, "Opaque: ")) {
929
0
                return 0;
930
0
            }
931
0
        }
932
3
#endif
933
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
934
3
        switch (var->type) {
935
0
        case ASN_OPAQUE_U64:
936
0
            if (!snmp_cstrcat
937
0
                (buf, buf_len, out_len, allow_realloc, "UInt64: ")) {
938
0
                return 0;
939
0
            }
940
0
            break;
941
0
        case ASN_OPAQUE_I64:
942
0
            if (!snmp_cstrcat
943
0
                (buf, buf_len, out_len, allow_realloc, "Int64: ")) {
944
0
                return 0;
945
0
            }
946
0
            break;
947
3
        case ASN_COUNTER64:
948
3
        case ASN_OPAQUE_COUNTER64:
949
3
#endif
950
3
            if (!snmp_cstrcat
951
3
                (buf, buf_len, out_len, allow_realloc, "Counter64: ")) {
952
0
                return 0;
953
0
            }
954
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
955
3
        }
956
3
#endif
957
3
    }
958
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
959
3
    if (var->type == ASN_OPAQUE_I64) {
960
0
        printI64(a64buf, var->val.counter64);
961
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, a64buf)) {
962
0
            return 0;
963
0
        }
964
3
    } else {
965
3
#endif
966
3
        printU64(a64buf, var->val.counter64);
967
3
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, a64buf)) {
968
0
            return 0;
969
0
        }
970
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
971
3
    }
972
3
#endif
973
974
3
    if (units) {
975
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
976
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
977
0
    }
978
3
    return 1;
979
3
}
980
981
982
/**
983
 * Prints an object identifier into a buffer.
984
 *
985
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
986
 * needed size. (Note: *buf may change due to this.)
987
 * 
988
 * @param buf      Address of the buffer to print to.
989
 * @param buf_len  Address to an integer containing the size of buf.
990
 * @param out_len  Incremented by the number of characters printed.
991
 * @param allow_realloc if not zero reallocate the buffer to fit the 
992
 *                      needed size.
993
 * @param var      The variable to encode.
994
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
995
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
996
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
997
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
998
 * 
999
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1000
 *         small when not allowed to realloc.)
1001
 */
1002
int
1003
sprint_realloc_opaque(u_char ** buf, size_t * buf_len,
1004
                      size_t * out_len, int allow_realloc,
1005
                      const netsnmp_variable_list * var,
1006
                      const struct enum_list *enums,
1007
                      const char *hint, const char *units)
1008
26
{
1009
26
    if (var->type != ASN_OPAQUE
1010
26
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
1011
26
        && var->type != ASN_OPAQUE_COUNTER64
1012
26
        && var->type != ASN_OPAQUE_U64
1013
26
        && var->type != ASN_OPAQUE_I64
1014
26
        && var->type != ASN_OPAQUE_FLOAT && var->type != ASN_OPAQUE_DOUBLE
1015
26
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
1016
26
        ) {
1017
0
        if (!netsnmp_ds_get_boolean(
1018
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1019
0
            static const char str[] = "Wrong Type (should be Opaque): ";
1020
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1021
0
                return 0;
1022
0
        }
1023
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1024
0
                                          allow_realloc, var, NULL, NULL,
1025
0
                                          NULL);
1026
0
    }
1027
1028
26
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
1029
26
    switch (var->type) {
1030
0
    case ASN_OPAQUE_COUNTER64:
1031
0
    case ASN_OPAQUE_U64:
1032
0
    case ASN_OPAQUE_I64:
1033
0
        return sprint_realloc_counter64(buf, buf_len, out_len,
1034
0
                                        allow_realloc, var, enums, hint,
1035
0
                                        units);
1036
0
        break;
1037
1038
0
    case ASN_OPAQUE_FLOAT:
1039
0
        return sprint_realloc_float(buf, buf_len, out_len, allow_realloc,
1040
0
                                    var, enums, hint, units);
1041
0
        break;
1042
1043
0
    case ASN_OPAQUE_DOUBLE:
1044
0
        return sprint_realloc_double(buf, buf_len, out_len, allow_realloc,
1045
0
                                     var, enums, hint, units);
1046
0
        break;
1047
1048
26
    case ASN_OPAQUE:
1049
26
#endif
1050
26
        if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1051
26
            static const char str[] = "OPAQUE: ";
1052
26
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1053
0
                return 0;
1054
0
            }
1055
26
        }
1056
26
        if (!sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc,
1057
26
                                      var->val.string, var->val_len)) {
1058
0
            return 0;
1059
0
        }
1060
26
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
1061
26
    }
1062
26
#endif
1063
26
    if (units) {
1064
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1065
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1066
0
    }
1067
26
    return 1;
1068
26
}
1069
1070
1071
/**
1072
 * Prints an object identifier into a buffer.
1073
 *
1074
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1075
 * needed size. (Note: *buf may change due to this.)
1076
 * 
1077
 * @param buf      Address of the buffer to print to.
1078
 * @param buf_len  Address to an integer containing the size of buf.
1079
 * @param out_len  Incremented by the number of characters printed.
1080
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1081
 *                      needed size.
1082
 * @param var      The variable to encode.
1083
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1084
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1085
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1086
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1087
 * 
1088
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1089
 *         small when not allowed to realloc.)
1090
 */
1091
int
1092
sprint_realloc_object_identifier(u_char ** buf, size_t * buf_len,
1093
                                 size_t * out_len, int allow_realloc,
1094
                                 const netsnmp_variable_list * var,
1095
                                 const struct enum_list *enums,
1096
                                 const char *hint, const char *units)
1097
17
{
1098
17
    int             buf_overflow = 0;
1099
1100
17
    if (var->type != ASN_OBJECT_ID) {
1101
0
        if (!netsnmp_ds_get_boolean(
1102
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1103
0
            static const char str[] = "Wrong Type (should be OBJECT IDENTIFIER): ";
1104
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1105
0
                return 0;
1106
0
        }
1107
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1108
0
                                          allow_realloc, var, NULL, NULL,
1109
0
                                          NULL);
1110
0
    }
1111
1112
17
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1113
17
        static const char str[] = "OID: ";
1114
17
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1115
0
            return 0;
1116
0
        }
1117
17
    }
1118
1119
17
    netsnmp_sprint_realloc_objid_tree(buf, buf_len, out_len, allow_realloc,
1120
17
                                      &buf_overflow,
1121
17
                                      (oid *) (var->val.objid),
1122
17
                                      var->val_len / sizeof(oid));
1123
1124
17
    if (buf_overflow) {
1125
0
        return 0;
1126
0
    }
1127
1128
17
    if (units) {
1129
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1130
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1131
0
    }
1132
17
    return 1;
1133
17
}
1134
1135
1136
1137
/**
1138
 * Prints a timetick variable into a buffer.
1139
 *
1140
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1141
 * needed size. (Note: *buf may change due to this.)
1142
 * 
1143
 * @param buf      Address of the buffer to print to.
1144
 * @param buf_len  Address to an integer containing the size of buf.
1145
 * @param out_len  Incremented by the number of characters printed.
1146
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1147
 *                      needed size.
1148
 * @param var      The variable to encode.
1149
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1150
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1151
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1152
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1153
 * 
1154
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1155
 *         small when not allowed to realloc.)
1156
 */
1157
int
1158
sprint_realloc_timeticks(u_char ** buf, size_t * buf_len, size_t * out_len,
1159
                         int allow_realloc,
1160
                         const netsnmp_variable_list * var,
1161
                         const struct enum_list *enums,
1162
                         const char *hint, const char *units)
1163
12
{
1164
12
    char            timebuf[40];
1165
1166
12
    if (var->type != ASN_TIMETICKS) {
1167
0
        if (!netsnmp_ds_get_boolean(
1168
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1169
0
            static const char str[] = "Wrong Type (should be Timeticks): ";
1170
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1171
0
                return 0;
1172
0
        }
1173
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1174
0
                                          allow_realloc, var, NULL, NULL,
1175
0
                                          NULL);
1176
0
    }
1177
1178
12
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS)) {
1179
10
        char            str[32];
1180
10
        snprintf(str, sizeof(str), "%lu", *(u_long *) var->val.integer);
1181
10
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1182
0
            return 0;
1183
0
        }
1184
10
        return 1;
1185
10
    }
1186
2
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1187
2
        char            str[32];
1188
2
        snprintf(str, sizeof(str), "Timeticks: (%lu) ",
1189
2
                 *(u_long *) var->val.integer);
1190
2
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1191
0
            return 0;
1192
0
        }
1193
2
    }
1194
2
    uptimeString(*(u_long *) (var->val.integer), timebuf, sizeof(timebuf));
1195
2
    if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, timebuf)) {
1196
0
        return 0;
1197
0
    }
1198
2
    if (units) {
1199
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1200
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1201
0
    }
1202
2
    return 1;
1203
2
}
1204
1205
1206
/**
1207
 * Prints an integer according to the hint into a buffer.
1208
 *
1209
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1210
 * needed size. (Note: *buf may change due to this.)
1211
 * 
1212
 * @param buf      Address of the buffer to print to.
1213
 * @param buf_len  Address to an integer containing the size of buf.
1214
 * @param out_len  Incremented by the number of characters printed.
1215
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1216
 *                      needed size.
1217
 * @param val      The variable to encode.
1218
 * @param decimaltype 'd' or 'u' depending on integer type
1219
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1220
 *                 See RFC 1903 Section 3.1 for details. may _NOT_ be NULL.
1221
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1222
 * 
1223
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1224
 *         small when not allowed to realloc.)
1225
 */
1226
int
1227
sprint_realloc_hinted_integer(u_char ** buf, size_t * buf_len,
1228
                              size_t * out_len, int allow_realloc,
1229
                              long val, const char decimaltype,
1230
                              const char *hint, const char *units)
1231
448
{
1232
448
    char            fmt[10] = "%l@", tmp[256];
1233
448
    int             shift = 0, len, negative = 0;
1234
1235
448
    if (!strchr("bdoux", decimaltype)) {
1236
0
        snmp_log(LOG_ERR, "Invalid decimal type '%c'\n", decimaltype);
1237
0
        return 0;
1238
0
    }
1239
1240
448
    switch (hint[0]) {
1241
206
    case 'd':
1242
        /*
1243
         * We might *actually* want a 'u' here.  
1244
         */
1245
206
        if (hint[1] == '-') {
1246
190
            shift = atoi(hint + 2);
1247
190
            if (shift < 0)
1248
64
                shift = 0;
1249
190
        }
1250
206
        fmt[2] = decimaltype;
1251
206
        if (val < 0) {
1252
0
            negative = 1;
1253
0
            val = -val;
1254
0
        }
1255
206
        snprintf(tmp, sizeof(tmp), fmt, val);
1256
206
        break;
1257
22
    case 'o':
1258
24
    case 'x':
1259
24
        fmt[2] = hint[0];
1260
24
        snprintf(tmp, sizeof(tmp), fmt, val);
1261
24
        break;
1262
2
    case 'b': {
1263
2
  unsigned long int bit = 0x80000000LU;
1264
2
  char *bp = tmp;
1265
66
  while (bit) {
1266
64
      *bp++ = val & bit ? '1' : '0';
1267
64
      bit >>= 1;
1268
64
  }
1269
2
  *bp = 0;
1270
2
        break;
1271
22
    }
1272
216
    default:
1273
216
        return 0;
1274
448
    }
1275
1276
232
    if (shift != 0) {
1277
124
        len = strlen(tmp);
1278
124
        if (shift <= len) {
1279
2
            tmp[len + 1] = 0;
1280
4
            while (shift--) {
1281
2
                tmp[len] = tmp[len - 1];
1282
2
                len--;
1283
2
            }
1284
2
            tmp[len] = '.';
1285
122
        } else if (shift < sizeof(tmp) - 1) {
1286
30
            tmp[shift + 1] = 0;
1287
3.38k
            while (shift) {
1288
3.35k
                if (len-- > 0) {
1289
30
                    tmp[shift] = tmp[len];
1290
3.32k
                } else {
1291
3.32k
                    tmp[shift] = '0';
1292
3.32k
                }
1293
3.35k
                shift--;
1294
3.35k
            }
1295
30
            tmp[0] = '.';
1296
30
        }
1297
124
    }
1298
232
    if (negative) {
1299
0
        len = strlen(tmp)+1;
1300
0
        while (len) {
1301
0
            tmp[len] = tmp[len-1];
1302
0
            len--;
1303
0
        }
1304
0
        tmp[0] = '-';
1305
0
    }
1306
232
    return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmp);
1307
448
}
1308
1309
1310
/**
1311
 * Prints an integer into a buffer.
1312
 *
1313
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1314
 * needed size. (Note: *buf may change due to this.)
1315
 * 
1316
 * @param buf      Address of the buffer to print to.
1317
 * @param buf_len  Address to an integer containing the size of buf.
1318
 * @param out_len  Incremented by the number of characters printed.
1319
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1320
 *                      needed size.
1321
 * @param var      The variable to encode.
1322
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1323
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1324
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1325
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1326
 * 
1327
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1328
 *         small when not allowed to realloc.)
1329
 */
1330
int
1331
sprint_realloc_integer(u_char ** buf, size_t * buf_len, size_t * out_len,
1332
                       int allow_realloc,
1333
                       const netsnmp_variable_list * var,
1334
                       const struct enum_list *enums,
1335
                       const char *hint, const char *units)
1336
382
{
1337
382
    char           *enum_string = NULL;
1338
1339
382
    if (var->type != ASN_INTEGER) {
1340
0
        if (!netsnmp_ds_get_boolean(
1341
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1342
0
            static const char str[] = "Wrong Type (should be INTEGER): ";
1343
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1344
0
                return 0;
1345
0
        }
1346
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1347
0
                                          allow_realloc, var, NULL, NULL,
1348
0
                                          NULL);
1349
0
    }
1350
1351
382
    for (; enums; enums = enums->next) {
1352
0
        if (enums->value == *var->val.integer) {
1353
0
            enum_string = enums->label;
1354
0
            break;
1355
0
        }
1356
0
    }
1357
1358
382
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1359
382
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "INTEGER: ")) {
1360
0
            return 0;
1361
0
        }
1362
382
    }
1363
1364
382
    if (enum_string == NULL ||
1365
382
        netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM)) {
1366
382
        if (hint) {
1367
224
            if (!(sprint_realloc_hinted_integer(buf, buf_len, out_len,
1368
224
                                                allow_realloc,
1369
224
                                                *var->val.integer, 'd',
1370
224
                                                hint, units))) {
1371
115
                return 0;
1372
115
            }
1373
224
        } else {
1374
158
            char            str[32];
1375
158
            snprintf(str, sizeof(str), "%ld", *var->val.integer);
1376
158
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1377
0
                return 0;
1378
0
            }
1379
158
        }
1380
382
    } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1381
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) {
1382
0
            return 0;
1383
0
        }
1384
0
    } else {
1385
0
        char            str[32];
1386
0
        snprintf(str, sizeof(str), "(%ld)", *var->val.integer);
1387
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) {
1388
0
            return 0;
1389
0
        }
1390
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1391
0
            return 0;
1392
0
        }
1393
0
    }
1394
1395
267
    if (units) {
1396
109
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1397
109
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1398
109
    }
1399
158
    return 1;
1400
267
}
1401
1402
1403
/**
1404
 * Prints an unsigned integer into a buffer.
1405
 *
1406
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1407
 * needed size. (Note: *buf may change due to this.)
1408
 * 
1409
 * @param buf      Address of the buffer to print to.
1410
 * @param buf_len  Address to an integer containing the size of buf.
1411
 * @param out_len  Incremented by the number of characters printed.
1412
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1413
 *                      needed size.
1414
 * @param var      The variable to encode.
1415
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1416
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1417
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1418
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1419
 * 
1420
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1421
 *         small when not allowed to realloc.)
1422
 */
1423
int
1424
sprint_realloc_uinteger(u_char ** buf, size_t * buf_len, size_t * out_len,
1425
                        int allow_realloc,
1426
                        const netsnmp_variable_list * var,
1427
                        const struct enum_list *enums,
1428
                        const char *hint, const char *units)
1429
237
{
1430
237
    char           *enum_string = NULL;
1431
1432
237
    if (var->type != ASN_UINTEGER) {
1433
0
        if (!netsnmp_ds_get_boolean(
1434
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1435
0
            static const char str[] = "Wrong Type (should be UInteger32): ";
1436
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1437
0
                return 0;
1438
0
        }
1439
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1440
0
                                          allow_realloc, var, NULL, NULL,
1441
0
                                          NULL);
1442
0
    }
1443
1444
237
    for (; enums; enums = enums->next) {
1445
0
        if (enums->value == *var->val.integer) {
1446
0
            enum_string = enums->label;
1447
0
            break;
1448
0
        }
1449
0
    }
1450
1451
237
    if (enum_string == NULL ||
1452
237
        netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM)) {
1453
237
        if (hint) {
1454
224
            if (!(sprint_realloc_hinted_integer(buf, buf_len, out_len,
1455
224
                                                allow_realloc,
1456
224
                                                *var->val.integer, 'u',
1457
224
                                                hint, units))) {
1458
114
                return 0;
1459
114
            }
1460
224
        } else {
1461
13
            char            str[32];
1462
13
            snprintf(str, sizeof(str), "%lu", *var->val.integer);
1463
13
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1464
0
                return 0;
1465
0
            }
1466
13
        }
1467
237
    } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1468
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) {
1469
0
            return 0;
1470
0
        }
1471
0
    } else {
1472
0
        char            str[32];
1473
0
        snprintf(str, sizeof(str), "(%lu)", *var->val.integer);
1474
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, enum_string)) {
1475
0
            return 0;
1476
0
        }
1477
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1478
0
            return 0;
1479
0
        }
1480
0
    }
1481
1482
123
    if (units) {
1483
110
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1484
110
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1485
110
    }
1486
13
    return 1;
1487
123
}
1488
1489
1490
/**
1491
 * Prints a gauge value into a buffer.
1492
 *
1493
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1494
 * needed size. (Note: *buf may change due to this.)
1495
 * 
1496
 * @param buf      Address of the buffer to print to.
1497
 * @param buf_len  Address to an integer containing the size of buf.
1498
 * @param out_len  Incremented by the number of characters printed.
1499
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1500
 *                      needed size.
1501
 * @param var      The variable to encode.
1502
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1503
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1504
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1505
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1506
 * 
1507
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1508
 *         small when not allowed to realloc.)
1509
 */
1510
int
1511
sprint_realloc_gauge(u_char ** buf, size_t * buf_len, size_t * out_len,
1512
                     int allow_realloc,
1513
                     const netsnmp_variable_list * var,
1514
                     const struct enum_list *enums,
1515
                     const char *hint, const char *units)
1516
8
{
1517
8
    char            tmp[32];
1518
1519
8
    if (var->type != ASN_GAUGE) {
1520
0
        if (!netsnmp_ds_get_boolean(
1521
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1522
0
            static const char str[] = "Wrong Type (should be Gauge32 or Unsigned32): ";
1523
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1524
0
                return 0;
1525
0
        }
1526
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1527
0
                                          allow_realloc, var, NULL, NULL,
1528
0
                                          NULL);
1529
0
    }
1530
1531
8
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1532
8
        static const char str[] = "Gauge32: ";
1533
8
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1534
0
            return 0;
1535
0
        }
1536
8
    }
1537
8
    if (hint) {
1538
0
        if (!sprint_realloc_hinted_integer(buf, buf_len, out_len,
1539
0
                                           allow_realloc,
1540
0
                                           *var->val.integer, 'u', hint,
1541
0
                                           units)) {
1542
0
            return 0;
1543
0
        }
1544
8
    } else {
1545
8
        sprintf(tmp, "%u", (unsigned int)(*var->val.integer & 0xffffffff));
1546
8
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmp)) {
1547
0
            return 0;
1548
0
        }
1549
8
    }
1550
8
    if (units) {
1551
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1552
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1553
0
    }
1554
8
    return 1;
1555
8
}
1556
1557
1558
/**
1559
 * Prints a counter value into a buffer.
1560
 *
1561
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1562
 * needed size. (Note: *buf may change due to this.)
1563
 * 
1564
 * @param buf      Address of the buffer to print to.
1565
 * @param buf_len  Address to an integer containing the size of buf.
1566
 * @param out_len  Incremented by the number of characters printed.
1567
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1568
 *                      needed size.
1569
 * @param var      The variable to encode.
1570
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1571
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1572
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1573
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1574
 * 
1575
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1576
 *         small when not allowed to realloc.)
1577
 */
1578
int
1579
sprint_realloc_counter(u_char ** buf, size_t * buf_len, size_t * out_len,
1580
                       int allow_realloc,
1581
                       const netsnmp_variable_list * var,
1582
                       const struct enum_list *enums,
1583
                       const char *hint, const char *units)
1584
5
{
1585
5
    char            tmp[32];
1586
1587
5
    if (var->type != ASN_COUNTER) {
1588
0
        if (!netsnmp_ds_get_boolean(
1589
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1590
0
            static const char str[] = "Wrong Type (should be Counter32): ";
1591
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1592
0
                return 0;
1593
0
        }
1594
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1595
0
                                          allow_realloc, var, NULL, NULL,
1596
0
                                          NULL);
1597
0
    }
1598
1599
5
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1600
5
        static const char str[] = "Counter32: ";
1601
5
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1602
0
            return 0;
1603
0
        }
1604
5
    }
1605
5
    sprintf(tmp, "%u", (unsigned int)(*var->val.integer & 0xffffffff));
1606
5
    if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmp)) {
1607
0
        return 0;
1608
0
    }
1609
5
    if (units) {
1610
0
        return (snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ") &&
1611
0
                snmp_cstrcat(buf, buf_len, out_len, allow_realloc, units));
1612
0
    }
1613
5
    return 1;
1614
5
}
1615
1616
1617
/**
1618
 * Prints a network address into a buffer.
1619
 *
1620
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1621
 * needed size. (Note: *buf may change due to this.)
1622
 * 
1623
 * @param buf      Address of the buffer to print to.
1624
 * @param buf_len  Address to an integer containing the size of buf.
1625
 * @param out_len  Incremented by the number of characters printed.
1626
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1627
 *                      needed size.
1628
 * @param var      The variable to encode.
1629
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1630
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1631
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1632
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1633
 * 
1634
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1635
 *         small when not allowed to realloc.)
1636
 */
1637
int
1638
sprint_realloc_networkaddress(u_char ** buf, size_t * buf_len,
1639
                              size_t * out_len, int allow_realloc,
1640
                              const netsnmp_variable_list * var,
1641
                              const struct enum_list *enums, const char *hint,
1642
                              const char *units)
1643
0
{
1644
0
    size_t          i;
1645
1646
0
    if (var->type != ASN_IPADDRESS) {
1647
0
        if (!netsnmp_ds_get_boolean(
1648
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1649
0
            static const char str[] = "Wrong Type (should be NetworkAddress): ";
1650
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1651
0
                return 0;
1652
0
        }
1653
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1654
0
                                          allow_realloc, var, NULL, NULL,
1655
0
                                          NULL);
1656
0
    }
1657
1658
0
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1659
0
        static const char str[] = "Network Address: ";
1660
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1661
0
            return 0;
1662
0
        }
1663
0
    }
1664
1665
0
    while ((*out_len + (var->val_len * 3) + 2) >= *buf_len) {
1666
0
        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
1667
0
            return 0;
1668
0
        }
1669
0
    }
1670
1671
0
    for (i = 0; i < var->val_len; i++) {
1672
0
        sprintf((char *) (*buf + *out_len), "%02X", var->val.string[i]);
1673
0
        *out_len += 2;
1674
0
        if (i < var->val_len - 1) {
1675
0
            *(*buf + *out_len) = ':';
1676
0
            (*out_len)++;
1677
0
        }
1678
0
    }
1679
0
    return 1;
1680
0
}
1681
1682
1683
/**
1684
 * Prints an ip-address into a buffer.
1685
 *
1686
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1687
 * needed size. (Note: *buf may change due to this.)
1688
 * 
1689
 * @param buf      Address of the buffer to print to.
1690
 * @param buf_len  Address to an integer containing the size of buf.
1691
 * @param out_len  Incremented by the number of characters printed.
1692
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1693
 *                      needed size.
1694
 * @param var      The variable to encode.
1695
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1696
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1697
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1698
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1699
 * 
1700
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1701
 *         small when not allowed to realloc.)
1702
 */
1703
int
1704
sprint_realloc_ipaddress(u_char ** buf, size_t * buf_len, size_t * out_len,
1705
                         int allow_realloc,
1706
                         const netsnmp_variable_list * var,
1707
                         const struct enum_list *enums,
1708
                         const char *hint, const char *units)
1709
12
{
1710
12
    u_char         *ip = var->val.string;
1711
1712
12
    if (var->type != ASN_IPADDRESS) {
1713
0
        if (!netsnmp_ds_get_boolean(
1714
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1715
0
            static const char str[] = "Wrong Type (should be IpAddress): ";
1716
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1717
0
                return 0;
1718
0
        }
1719
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1720
0
                                          allow_realloc, var, NULL, NULL,
1721
0
                                          NULL);
1722
0
    }
1723
1724
12
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1725
12
        static const char str[] = "IpAddress: ";
1726
12
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1727
0
            return 0;
1728
0
        }
1729
12
    }
1730
19
    while ((*out_len + 17) >= *buf_len) {
1731
7
        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
1732
0
            return 0;
1733
0
        }
1734
7
    }
1735
12
    if (ip)
1736
12
        sprintf((char *) (*buf + *out_len), "%d.%d.%d.%d",
1737
12
                                            ip[0], ip[1], ip[2], ip[3]);
1738
12
    *out_len += strlen((char *) (*buf + *out_len));
1739
12
    return 1;
1740
12
}
1741
1742
1743
/**
1744
 * Prints a null value into a buffer.
1745
 *
1746
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1747
 * needed size. (Note: *buf may change due to this.)
1748
 * 
1749
 * @param buf      Address of the buffer to print to.
1750
 * @param buf_len  Address to an integer containing the size of buf.
1751
 * @param out_len  Incremented by the number of characters printed.
1752
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1753
 *                      needed size.
1754
 * @param var      The variable to encode.
1755
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1756
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1757
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1758
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1759
 * 
1760
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1761
 *         small when not allowed to realloc.)
1762
 */
1763
int
1764
sprint_realloc_null(u_char ** buf, size_t * buf_len, size_t * out_len,
1765
                    int allow_realloc,
1766
                    const netsnmp_variable_list * var,
1767
                    const struct enum_list *enums,
1768
                    const char *hint, const char *units)
1769
8
{
1770
8
    static const char str[] = "NULL";
1771
1772
8
    if (var->type != ASN_NULL) {
1773
0
        if (!netsnmp_ds_get_boolean(
1774
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1775
0
            static const char str[] = "Wrong Type (should be NULL): ";
1776
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1777
0
                return 0;
1778
0
        }
1779
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1780
0
                                          allow_realloc, var, NULL, NULL,
1781
0
                                          NULL);
1782
0
    }
1783
1784
8
    return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str);
1785
8
}
1786
1787
1788
/**
1789
 * Prints a bit string into a buffer.
1790
 *
1791
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1792
 * needed size. (Note: *buf may change due to this.)
1793
 * 
1794
 * @param buf      Address of the buffer to print to.
1795
 * @param buf_len  Address to an integer containing the size of buf.
1796
 * @param out_len  Incremented by the number of characters printed.
1797
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1798
 *                      needed size.
1799
 * @param var      The variable to encode.
1800
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1801
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1802
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1803
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1804
 * 
1805
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1806
 *         small when not allowed to realloc.)
1807
 */
1808
int
1809
sprint_realloc_bitstring(u_char ** buf, size_t * buf_len, size_t * out_len,
1810
                         int allow_realloc,
1811
                         const netsnmp_variable_list * var,
1812
                         const struct enum_list *enums,
1813
                         const char *hint, const char *units)
1814
30
{
1815
30
    int             len, bit;
1816
30
    u_char         *cp;
1817
30
    char           *enum_string;
1818
1819
30
    if (var->type != ASN_BIT_STR && var->type != ASN_OCTET_STR) {
1820
0
        if (!netsnmp_ds_get_boolean(
1821
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1822
0
            static const char str[] = "Wrong Type (should be BITS): ";
1823
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1824
0
                return 0;
1825
0
        }
1826
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1827
0
                                          allow_realloc, var, NULL, NULL,
1828
0
                                          NULL);
1829
0
    }
1830
1831
30
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1832
0
        static const char str[] = "\"";
1833
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1834
0
            return 0;
1835
0
        }
1836
30
    } else {
1837
30
        static const char str[] = "BITS: ";
1838
30
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1839
0
            return 0;
1840
0
        }
1841
30
    }
1842
30
    if (!sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc,
1843
30
                                  var->val.bitstring, var->val_len)) {
1844
0
        return 0;
1845
0
    }
1846
1847
30
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1848
0
        static const char str[] = "\"";
1849
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1850
0
            return 0;
1851
0
        }
1852
30
    } else {
1853
30
        cp = var->val.bitstring;
1854
685
        for (len = 0; len < (int) var->val_len; len++) {
1855
5.89k
            for (bit = 0; bit < 8; bit++) {
1856
5.24k
                if (*cp & (0x80 >> bit)) {
1857
3.28k
                    enum_string = NULL;
1858
3.28k
                    for (; enums; enums = enums->next) {
1859
0
                        if (enums->value == (len * 8) + bit) {
1860
0
                            enum_string = enums->label;
1861
0
                            break;
1862
0
                        }
1863
0
                    }
1864
3.28k
                    if (enum_string == NULL ||
1865
3.28k
                        netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
1866
3.28k
                                       NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM)) {
1867
3.28k
                        char            str[32];
1868
3.28k
                        snprintf(str, sizeof(str), "%d ", (len * 8) + bit);
1869
3.28k
                        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
1870
3.28k
                                          str)) {
1871
0
                            return 0;
1872
0
                        }
1873
3.28k
                    } else {
1874
0
                        char            str[32];
1875
0
                        snprintf(str, sizeof(str), "(%d) ", (len * 8) + bit);
1876
0
                        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
1877
0
                                          enum_string)) {
1878
0
                            return 0;
1879
0
                        }
1880
0
                        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
1881
0
                                          str)) {
1882
0
                            return 0;
1883
0
                        }
1884
0
                    }
1885
3.28k
                }
1886
5.24k
            }
1887
655
            cp++;
1888
655
        }
1889
30
    }
1890
30
    return 1;
1891
30
}
1892
1893
int
1894
sprint_realloc_nsapaddress(u_char ** buf, size_t * buf_len,
1895
                           size_t * out_len, int allow_realloc,
1896
                           const netsnmp_variable_list * var,
1897
                           const struct enum_list *enums, const char *hint,
1898
                           const char *units)
1899
0
{
1900
0
    if (var->type != ASN_NSAP) {
1901
0
        if (!netsnmp_ds_get_boolean(
1902
0
                NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
1903
0
            static const char str[] = "Wrong Type (should be NsapAddress): ";
1904
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str))
1905
0
                return 0;
1906
0
        }
1907
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
1908
0
                                          allow_realloc, var, NULL, NULL,
1909
0
                                          NULL);
1910
0
    }
1911
1912
0
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
1913
0
        static const char str[] = "NsapAddress: ";
1914
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str)) {
1915
0
            return 0;
1916
0
        }
1917
0
    }
1918
1919
0
    return sprint_realloc_hexstring(buf, buf_len, out_len, allow_realloc,
1920
0
                                    var->val.string, var->val_len);
1921
0
}
1922
1923
1924
/**
1925
 * Fallback routine for a bad type, prints "Variable has bad type" into a buffer.
1926
 *
1927
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1928
 * needed size. (Note: *buf may change due to this.)
1929
 * 
1930
 * @param buf      Address of the buffer to print to.
1931
 * @param buf_len  Address to an integer containing the size of buf.
1932
 * @param out_len  Incremented by the number of characters printed.
1933
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1934
 *                      needed size.
1935
 * @param var      The variable to encode.
1936
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1937
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1938
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1939
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1940
 * 
1941
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1942
 *         small when not allowed to realloc.)
1943
 */
1944
int
1945
sprint_realloc_badtype(u_char ** buf, size_t * buf_len, size_t * out_len,
1946
                       int allow_realloc,
1947
                       const netsnmp_variable_list * var,
1948
                       const struct enum_list *enums,
1949
                       const char *hint, const char *units)
1950
0
{
1951
0
    static const char str[] = "Variable has bad type";
1952
1953
0
    return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, str);
1954
0
}
1955
1956
1957
1958
/**
1959
 * Universal print routine, prints a variable into a buffer according to the variable 
1960
 * type.
1961
 *
1962
 * If allow_realloc is true the buffer will be (re)allocated to fit in the 
1963
 * needed size. (Note: *buf may change due to this.)
1964
 * 
1965
 * @param buf      Address of the buffer to print to.
1966
 * @param buf_len  Address to an integer containing the size of buf.
1967
 * @param out_len  Incremented by the number of characters printed.
1968
 * @param allow_realloc if not zero reallocate the buffer to fit the 
1969
 *                      needed size.
1970
 * @param var      The variable to encode.
1971
 * @param enums    The enumeration ff this variable is enumerated. may be NULL.
1972
 * @param hint     Contents of the DISPLAY-HINT clause of the MIB.
1973
 *                 See RFC 1903 Section 3.1 for details. may be NULL.
1974
 * @param units    Contents of the UNITS clause of the MIB. may be NULL.
1975
 * 
1976
 * @return 1 on success, or 0 on failure (out of memory, or buffer to
1977
 *         small when not allowed to realloc.)
1978
 */
1979
int
1980
sprint_realloc_by_type(u_char ** buf, size_t * buf_len, size_t * out_len,
1981
                       int allow_realloc,
1982
                       const netsnmp_variable_list * var,
1983
                       const struct enum_list *enums,
1984
                       const char *hint, const char *units)
1985
482
{
1986
482
    DEBUGMSGTL(("output", "sprint_by_type, type %d\n", var->type));
1987
1988
482
    switch (var->type) {
1989
158
    case ASN_INTEGER:
1990
158
        return sprint_realloc_integer(buf, buf_len, out_len, allow_realloc,
1991
158
                                      var, enums, hint, units);
1992
153
    case ASN_OCTET_STR:
1993
153
        return sprint_realloc_octet_string(buf, buf_len, out_len,
1994
153
                                           allow_realloc, var, enums, hint,
1995
153
                                           units);
1996
30
    case ASN_BIT_STR:
1997
30
        return sprint_realloc_bitstring(buf, buf_len, out_len,
1998
30
                                        allow_realloc, var, enums, hint,
1999
30
                                        units);
2000
26
    case ASN_OPAQUE:
2001
26
        return sprint_realloc_opaque(buf, buf_len, out_len, allow_realloc,
2002
26
                                     var, enums, hint, units);
2003
17
    case ASN_OBJECT_ID:
2004
17
        return sprint_realloc_object_identifier(buf, buf_len, out_len,
2005
17
                                                allow_realloc, var, enums,
2006
17
                                                hint, units);
2007
12
    case ASN_TIMETICKS:
2008
12
        return sprint_realloc_timeticks(buf, buf_len, out_len,
2009
12
                                        allow_realloc, var, enums, hint,
2010
12
                                        units);
2011
8
    case ASN_GAUGE:
2012
8
        return sprint_realloc_gauge(buf, buf_len, out_len, allow_realloc,
2013
8
                                    var, enums, hint, units);
2014
5
    case ASN_COUNTER:
2015
5
        return sprint_realloc_counter(buf, buf_len, out_len, allow_realloc,
2016
5
                                      var, enums, hint, units);
2017
12
    case ASN_IPADDRESS:
2018
12
        return sprint_realloc_ipaddress(buf, buf_len, out_len,
2019
12
                                        allow_realloc, var, enums, hint,
2020
12
                                        units);
2021
8
    case ASN_NULL:
2022
8
        return sprint_realloc_null(buf, buf_len, out_len, allow_realloc,
2023
8
                                   var, enums, hint, units);
2024
13
    case ASN_UINTEGER:
2025
13
        return sprint_realloc_uinteger(buf, buf_len, out_len,
2026
13
                                       allow_realloc, var, enums, hint,
2027
13
                                       units);
2028
3
    case ASN_COUNTER64:
2029
3
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
2030
3
    case ASN_OPAQUE_U64:
2031
3
    case ASN_OPAQUE_I64:
2032
3
    case ASN_OPAQUE_COUNTER64:
2033
3
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
2034
3
        return sprint_realloc_counter64(buf, buf_len, out_len,
2035
3
                                        allow_realloc, var, enums, hint,
2036
3
                                        units);
2037
0
#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
2038
23
    case ASN_OPAQUE_FLOAT:
2039
23
        return sprint_realloc_float(buf, buf_len, out_len, allow_realloc,
2040
23
                                    var, enums, hint, units);
2041
14
    case ASN_OPAQUE_DOUBLE:
2042
14
        return sprint_realloc_double(buf, buf_len, out_len, allow_realloc,
2043
14
                                     var, enums, hint, units);
2044
0
#endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
2045
0
    default:
2046
0
        DEBUGMSGTL(("sprint_by_type", "bad type: %d\n", var->type));
2047
0
        return sprint_realloc_badtype(buf, buf_len, out_len, allow_realloc,
2048
0
                                      var, enums, hint, units);
2049
482
    }
2050
482
}
2051
2052
/**
2053
 * Generates a printf format string.
2054
 *
2055
 * The original format string is combined with the optional
2056
 * NETSNMP_DS_LIB_OUTPUT_PRECISION string (the -Op parameter).
2057
 *
2058
 * Example:
2059
 * If the original format string is "%f", and the NETSNMP_DS_LIB_OUTPUT_PRECISION
2060
 * is "5.2", the returned format string will be "%5.2f".
2061
 * 
2062
 * The PRECISION string is inserted after the '%' of the original format string.
2063
 * To prevent buffer overflow if NETSNMP_DS_LIB_OUTPUT_PRECISION is set to an
2064
 * illegal size (e.g. with -Op 10000) snprintf should be used to prevent buffer
2065
 * overflow.
2066
 * 
2067
 * @param printf_format_default  The format string used by the original printf.
2068
 * 
2069
 * @return The address of of the new allocated format string (which must be freed
2070
 *         if no longer used), or NULL if any error (malloc).
2071
 */
2072
char *
2073
make_printf_format_string(const char *printf_format_default)
2074
37
{
2075
37
    const char *cp_printf_format_default;
2076
37
    const char *printf_precision;
2077
37
    const char *cp_printf_precision;
2078
37
    char       *printf_format_string;
2079
37
    char       *cp_out;
2080
37
    char       c;
2081
2082
37
    printf_precision = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION);
2083
37
    if (!printf_precision) {
2084
37
        printf_precision = "";
2085
37
    }
2086
2087
    /* reserve new format string buffer */
2088
37
    printf_format_string = (char *) malloc(strlen(printf_format_default)+strlen(printf_precision)+1);
2089
37
    if (!printf_format_string)
2090
0
    {
2091
0
        DEBUGMSGTL(("make_printf_format_string", "malloc failed\n"));
2092
0
        return NULL;
2093
0
    }
2094
2095
    /* copy default format string, including the '%' */
2096
37
    cp_out = printf_format_string;
2097
37
    cp_printf_format_default = printf_format_default;
2098
37
    while((c = *cp_printf_format_default) != '\0')
2099
37
    {
2100
37
        *cp_out++ = c;
2101
37
        cp_printf_format_default++;
2102
37
        if (c == '%') break;
2103
37
    }
2104
2105
    /* insert the precision string */
2106
37
    cp_printf_precision = printf_precision;
2107
37
    while ((c = *cp_printf_precision++) != '\0')
2108
0
    {
2109
0
        *cp_out++ = c;
2110
0
    }
2111
2112
    /* copy the remaining default format string, including the terminating '\0' */
2113
37
    strcpy(cp_out, cp_printf_format_default);
2114
2115
37
    DEBUGMSGTL(("make_printf_format_string", "\"%s\"+\"%s\"->\"%s\"\n",
2116
37
                printf_format_default, printf_precision, printf_format_string));
2117
37
    return printf_format_string;
2118
37
}
2119
2120
2121
#ifndef NETSNMP_DISABLE_MIB_LOADING
2122
/**
2123
 * Retrieves the tree head.
2124
 *
2125
 * @return the tree head.
2126
 */
2127
struct tree    *
2128
get_tree_head(void)
2129
4.42k
{
2130
4.42k
    return (tree_head);
2131
4.42k
}
2132
2133
static char    *confmibdir = NULL;
2134
static char    *confmibs = NULL;
2135
2136
static void
2137
handle_mibdirs_conf(const char *token, char *line)
2138
0
{
2139
0
    char           *ctmp;
2140
2141
0
    if (confmibdir) {
2142
0
        if ((*line == '+') || (*line == '-')) {
2143
0
            ctmp = (char *) malloc(strlen(confmibdir) + strlen(line) + 2);
2144
0
            if (!ctmp) {
2145
0
                DEBUGMSGTL(("read_config:initmib",
2146
0
                            "mibdir conf malloc failed"));
2147
0
                return;
2148
0
            }
2149
0
            if(*line++ == '+')
2150
0
                sprintf(ctmp, "%s%c%s", confmibdir, ENV_SEPARATOR_CHAR, line);
2151
0
            else
2152
0
                sprintf(ctmp, "%s%c%s", line, ENV_SEPARATOR_CHAR, confmibdir);
2153
0
        } else {
2154
0
            ctmp = strdup(line);
2155
0
            if (!ctmp) {
2156
0
                DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed"));
2157
0
                return;
2158
0
            }
2159
0
        }
2160
0
        SNMP_FREE(confmibdir);
2161
0
    } else {
2162
0
        ctmp = strdup(line);
2163
0
        if (!ctmp) {
2164
0
            DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed"));
2165
0
            return;
2166
0
        }
2167
0
    }
2168
0
    confmibdir = ctmp;
2169
0
    DEBUGMSGTL(("read_config:initmib", "using mibdirs: %s\n", confmibdir));
2170
0
}
2171
2172
static void
2173
handle_mibs_conf(const char *token, char *line)
2174
0
{
2175
0
    char           *ctmp;
2176
2177
0
    if (confmibs) {
2178
0
        if ((*line == '+') || (*line == '-')) {
2179
0
            int res;
2180
2181
0
            res = *line++ == '+' ?
2182
                /* Add specified dirs after existing ones */
2183
0
                asprintf(&ctmp, "%s%c%s", confmibs, ENV_SEPARATOR_CHAR, line) :
2184
                /* Add specified dirs before existing ones */
2185
0
                asprintf(&ctmp, "%s%c%s", line, ENV_SEPARATOR_CHAR, confmibs);
2186
0
            if (res < 0) {
2187
0
                DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed"));
2188
0
                return;
2189
0
            }
2190
0
        } else {
2191
0
            ctmp = strdup(line);
2192
0
            if (!ctmp) {
2193
0
                DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed"));
2194
0
                return;
2195
0
            }
2196
0
        }
2197
0
        SNMP_FREE(confmibs);
2198
0
    } else {
2199
0
        ctmp = strdup(line);
2200
0
        if (!ctmp) {
2201
0
            DEBUGMSGTL(("read_config:initmib", "mibs conf malloc failed"));
2202
0
            return;
2203
0
        }
2204
0
    }
2205
0
    confmibs = ctmp;
2206
0
    DEBUGMSGTL(("read_config:initmib", "using mibs: %s\n", confmibs));
2207
0
}
2208
2209
2210
static void
2211
handle_mibfile_conf(const char *token, char *line)
2212
424
{
2213
424
    DEBUGMSGTL(("read_config:initmib", "reading mibfile: %s\n", line));
2214
424
    read_mib(line);
2215
424
}
2216
#endif
2217
2218
static void
2219
handle_print_numeric(const char *token, char *line)
2220
0
{
2221
0
    const char *value;
2222
0
    char       *st;
2223
2224
0
    value = strtok_r(line, " \t\n", &st);
2225
0
    if (value && (
2226
0
      (strcasecmp(value, "yes")  == 0) || 
2227
0
      (strcasecmp(value, "true") == 0) ||
2228
0
      (*value == '1') )) {
2229
2230
0
        netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2231
0
                                                  NETSNMP_OID_OUTPUT_NUMERIC);
2232
0
    }
2233
0
}
2234
2235
char *
2236
snmp_out_options(char *options, int argc, char *const *argv)
2237
0
{
2238
0
    while (*options) {
2239
0
        switch (*options++) {
2240
0
        case '0':
2241
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID,
2242
0
                                      NETSNMP_DS_LIB_2DIGIT_HEX_OUTPUT);
2243
0
            break;
2244
0
        case 'a':
2245
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT,
2246
0
                                                      NETSNMP_STRING_OUTPUT_ASCII);
2247
0
            break;
2248
0
        case 'b':
2249
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS);
2250
0
            break;
2251
0
        case 'e':
2252
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
2253
0
            break;
2254
0
        case 'E':
2255
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES);
2256
0
            break;
2257
0
        case 'f':
2258
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2259
0
                                                      NETSNMP_OID_OUTPUT_FULL);
2260
0
            break;
2261
0
        case 'F':
2262
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2263
0
                                                      NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC);
2264
0
            break;
2265
0
        case 'n':
2266
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2267
0
                                                      NETSNMP_OID_OUTPUT_NUMERIC);
2268
0
            break;
2269
0
        case 'p':
2270
            /* What if argc/argv are null ? */
2271
0
            if (!*(options)) {
2272
0
    if (optind == argc || argc == 0) {
2273
0
        fprintf(stderr, "Missing precision for -Op\n");
2274
0
        return options-1;
2275
0
    }
2276
0
                options = argv[optind++];
2277
0
            }
2278
0
            netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
2279
0
                                  NETSNMP_DS_LIB_OUTPUT_PRECISION,
2280
0
                                  options);
2281
0
            return NULL;  /* -Op... is a standalone option, so we're done here */
2282
0
        case 'q':
2283
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
2284
0
            break;
2285
0
        case 'Q':
2286
0
            netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT, 1);
2287
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
2288
0
            break;
2289
0
        case 's':
2290
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2291
0
                                                      NETSNMP_OID_OUTPUT_SUFFIX);
2292
0
            break;
2293
0
        case 'S':
2294
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2295
0
                                                      NETSNMP_OID_OUTPUT_MODULE);
2296
0
            break;
2297
0
        case 't':
2298
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
2299
0
            break;
2300
0
        case 'T':
2301
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT);
2302
0
            break;
2303
0
        case 'u':
2304
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
2305
0
                                                      NETSNMP_OID_OUTPUT_UCD);
2306
0
            break;
2307
0
        case 'U':
2308
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS);
2309
0
            break;
2310
0
        case 'v':
2311
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE);
2312
0
            break;
2313
0
        case 'x':
2314
0
            netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_STRING_OUTPUT_FORMAT,
2315
0
                                                      NETSNMP_STRING_OUTPUT_HEX);
2316
0
            break;
2317
0
        case 'X':
2318
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX);
2319
0
            break;
2320
0
        default:
2321
0
            return options - 1;
2322
0
        }
2323
0
    }
2324
0
    return NULL;
2325
0
}
2326
2327
char           *
2328
snmp_out_toggle_options(char *options)
2329
0
{
2330
0
    return snmp_out_options( options, 0, NULL );
2331
0
}
2332
2333
void
2334
snmp_out_toggle_options_usage(const char *lead, FILE * outf)
2335
0
{
2336
0
    fprintf(outf, "%s0:  print leading 0 for single-digit hex characters\n", lead);
2337
0
    fprintf(outf, "%sa:  print all strings in ascii format\n", lead);
2338
0
    fprintf(outf, "%sb:  do not break OID indexes down\n", lead);
2339
0
    fprintf(outf, "%se:  print enums numerically\n", lead);
2340
0
    fprintf(outf, "%sE:  escape quotes in string indices\n", lead);
2341
0
    fprintf(outf, "%sf:  print full OIDs on output\n", lead);
2342
0
    fprintf(outf, "%sn:  print OIDs numerically\n", lead);
2343
0
    fprintf(outf, "%sp PRECISION:  display floating point values with specified PRECISION (printf format string)\n", lead);
2344
0
    fprintf(outf, "%sq:  quick print for easier parsing\n", lead);
2345
0
    fprintf(outf, "%sQ:  quick print with equal-signs\n", lead);    /* @@JDW */
2346
0
    fprintf(outf, "%ss:  print only last symbolic element of OID\n", lead);
2347
0
    fprintf(outf, "%sS:  print MIB module-id plus last element\n", lead);
2348
0
    fprintf(outf, "%st:  print timeticks unparsed as numeric integers\n",
2349
0
            lead);
2350
0
    fprintf(outf,
2351
0
            "%sT:  print human-readable text along with hex strings\n",
2352
0
            lead);
2353
0
    fprintf(outf, "%su:  print OIDs using UCD-style prefix suppression\n",
2354
0
            lead);
2355
0
    fprintf(outf, "%sU:  don't print units\n", lead);
2356
0
    fprintf(outf, "%sv:  print values only (not OID = value)\n", lead);
2357
0
    fprintf(outf, "%sx:  print all strings in hex format\n", lead);
2358
0
    fprintf(outf, "%sX:  extended index format\n", lead);
2359
0
}
2360
2361
const char *
2362
snmp_in_options(char *optarg, int argc, char *const *argv)
2363
0
{
2364
0
    char *cp;
2365
2366
0
    for (cp = optarg; *cp; cp++) {
2367
0
        switch (*cp) {
2368
0
        case 'b':
2369
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_REGEX_ACCESS);
2370
0
            break;
2371
0
        case 'R':
2372
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS);
2373
0
            break;
2374
0
        case 'r':
2375
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_CHECK_RANGE);
2376
0
            break;
2377
0
        case 'h':
2378
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_DISPLAY_HINT);
2379
0
            break;
2380
0
        case 'u':
2381
0
            netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_READ_UCD_STYLE_OID);
2382
0
            break;
2383
0
        case 's':
2384
0
            if (!*(++cp)) {
2385
0
                cp = argv[optind++];
2386
0
                if (!cp)
2387
0
                    return "?";
2388
0
            }
2389
0
            netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
2390
0
                                  NETSNMP_DS_LIB_OIDSUFFIX,
2391
0
                                  cp);
2392
0
            return NULL;  /* -Is... is a standalone option, so we're done here */
2393
2394
0
        case 'S':
2395
0
            if (!*(++cp)) {
2396
0
                cp = argv[optind++];
2397
0
                if (!cp)
2398
0
                    return "?";
2399
0
            }
2400
0
            netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
2401
0
                                  NETSNMP_DS_LIB_OIDPREFIX,
2402
0
                                  cp);
2403
0
            return NULL;  /* -IS... is a standalone option, so we're done here */
2404
2405
0
        default:
2406
           /*
2407
            *  Here?  Or in snmp_parse_args?
2408
            snmp_log(LOG_ERR, "Unknown input option passed to -I: %c.\n", *cp);
2409
            */
2410
0
            return cp;
2411
0
        }
2412
0
    }
2413
0
    return NULL;
2414
0
}
2415
2416
const char     *
2417
snmp_in_toggle_options(char *options)
2418
0
{
2419
0
    return snmp_in_options( options, 0, NULL );
2420
0
}
2421
2422
2423
/**
2424
 * Prints out a help usage for the in* toggle options.
2425
 *
2426
 * @param lead      The lead to print for every line.
2427
 * @param outf      The file descriptor to write to.
2428
 * 
2429
 */
2430
void
2431
snmp_in_toggle_options_usage(const char *lead, FILE * outf)
2432
0
{
2433
0
    fprintf(outf, "%sb:  do best/regex matching to find a MIB node\n", lead);
2434
0
    fprintf(outf, "%sh:  don't apply DISPLAY-HINTs\n", lead);
2435
0
    fprintf(outf, "%sr:  do not check values for range/type legality\n", lead);
2436
0
    fprintf(outf, "%sR:  do random access to OID labels\n", lead);
2437
0
    fprintf(outf,
2438
0
            "%su:  top-level OIDs must have '.' prefix (UCD-style)\n", lead);
2439
0
    fprintf(outf,
2440
0
            "%ss SUFFIX:  Append all textual OIDs with SUFFIX before parsing\n",
2441
0
            lead);
2442
0
    fprintf(outf,
2443
0
            "%sS PREFIX:  Prepend all textual OIDs with PREFIX before parsing\n",
2444
0
            lead);
2445
0
}
2446
2447
/***
2448
 *
2449
 */ 
2450
void
2451
register_mib_handlers(void)
2452
3.71k
{
2453
3.71k
#ifndef NETSNMP_DISABLE_MIB_LOADING
2454
3.71k
    register_prenetsnmp_mib_handler("snmp", "mibdirs",
2455
3.71k
                                    handle_mibdirs_conf, NULL,
2456
3.71k
                                    "[mib-dirs|+mib-dirs|-mib-dirs]");
2457
3.71k
    register_prenetsnmp_mib_handler("snmp", "mibs",
2458
3.71k
                                    handle_mibs_conf, NULL,
2459
3.71k
                                    "[mib-tokens|+mib-tokens|-mib-tokens]");
2460
3.71k
    register_config_handler("snmp", "mibfile",
2461
3.71k
                            handle_mibfile_conf, NULL, "mibfile-to-read");
2462
    /*
2463
     * register the snmp.conf configuration handlers for default
2464
     * parsing behaviour 
2465
     */
2466
2467
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "showMibErrors",
2468
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_ERRORS);
2469
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "commentToEOL",     /* Describes actual behaviour */
2470
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_COMMENT_TERM);
2471
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "strictCommentTerm",    /* Backward compatibility */
2472
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_COMMENT_TERM);
2473
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "mibAllowUnderline",
2474
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_PARSE_LABEL);
2475
3.71k
    netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "mibWarningLevel",
2476
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_WARNINGS);
2477
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "mibReplaceWithLatest",
2478
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_REPLACE);
2479
3.71k
#endif
2480
2481
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "printNumericEnums",
2482
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
2483
3.71k
    register_prenetsnmp_mib_handler("snmp", "printNumericOids",
2484
3.71k
                       handle_print_numeric, NULL, "(1|yes|true|0|no|false)");
2485
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "escapeQuotes",
2486
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES);
2487
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "dontBreakdownOids",
2488
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS);
2489
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "quickPrinting",
2490
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
2491
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "numericTimeticks",
2492
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
2493
3.71k
    netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "oidOutputFormat",
2494
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
2495
3.71k
    netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "suffixPrinting",
2496
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
2497
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "extendedIndex",
2498
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX);
2499
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "printHexText",
2500
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT);
2501
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "printValueOnly",
2502
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE);
2503
3.71k
    netsnmp_ds_register_premib(ASN_BOOLEAN, "snmp", "dontPrintUnits",
2504
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS);
2505
3.71k
    netsnmp_ds_register_premib(ASN_INTEGER, "snmp", "hexOutputLength",
2506
3.71k
                       NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_HEX_OUTPUT_LENGTH);
2507
3.71k
}
2508
2509
#ifndef NETSNMP_DISABLE_MIB_LOADING
2510
/*
2511
 * function : netsnmp_set_mib_directory
2512
 *            - This function sets the string of the directories
2513
 *              from which the MIB modules will be searched or
2514
 *              loaded.
2515
 * arguments: const char *dir, which are the directories
2516
 *              from which the MIB modules will be searched or
2517
 *              loaded.
2518
 * returns  : -
2519
 */
2520
void
2521
netsnmp_set_mib_directory(const char *dir)
2522
5.13k
{
2523
5.13k
    const char *newdir;
2524
5.13k
    char *olddir, *tmpdir = NULL;
2525
2526
5.13k
    DEBUGTRACE;
2527
5.13k
    if (NULL == dir) {
2528
0
        return;
2529
0
    }
2530
    
2531
5.13k
    olddir = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
2532
5.13k
           NETSNMP_DS_LIB_MIBDIRS);
2533
5.13k
    if (olddir) {
2534
2.56k
        if ((*dir == '+') || (*dir == '-')) {
2535
            /** New dir starts with '+', thus we add it. */
2536
0
            tmpdir = (char *)malloc(strlen(dir) + strlen(olddir) + 2);
2537
0
            if (!tmpdir) {
2538
0
                DEBUGMSGTL(("read_config:initmib", "set mibdir malloc failed"));
2539
0
                return;
2540
0
            }
2541
0
            if (*dir++ == '+')
2542
0
                sprintf(tmpdir, "%s%c%s", olddir, ENV_SEPARATOR_CHAR, dir);
2543
0
            else
2544
0
                sprintf(tmpdir, "%s%c%s", dir, ENV_SEPARATOR_CHAR, olddir);
2545
0
            newdir = tmpdir;
2546
2.56k
        } else {
2547
2.56k
            newdir = dir;
2548
2.56k
        }
2549
2.57k
    } else {
2550
        /** If dir starts with '+' skip '+' it. */
2551
2.57k
        newdir = ((*dir == '+') ? ++dir : dir);
2552
2.57k
    }
2553
5.13k
    netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS,
2554
5.13k
                          newdir);
2555
2556
    /** set_string calls strdup, so if we allocated memory, free it */
2557
5.13k
    if (tmpdir == newdir) {
2558
0
        SNMP_FREE(tmpdir);
2559
0
    }
2560
5.13k
}
2561
2562
/*
2563
 * function : netsnmp_get_mib_directory
2564
 *            - This function returns a string of the directories
2565
 *              from which the MIB modules will be searched or
2566
 *              loaded.
2567
 *              If the value still does not exists, it will be made
2568
 *              from the environment variable 'MIBDIRS' and/or the
2569
 *              default.
2570
 * arguments: -
2571
 * returns  : char * of the directories in which the MIB modules
2572
 *            will be searched/loaded.
2573
 */
2574
2575
char *
2576
netsnmp_get_mib_directory(void)
2577
5.14k
{
2578
5.14k
    char *dir;
2579
2580
5.14k
    DEBUGTRACE;
2581
5.14k
    dir = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS);
2582
5.14k
    if (dir == NULL) {
2583
2.57k
        DEBUGMSGTL(("get_mib_directory", "no mib directories set\n"));
2584
2585
        /** Check if the environment variable is set */
2586
2.57k
        dir = netsnmp_getenv("MIBDIRS");
2587
2.57k
        if (dir == NULL) {
2588
8
            DEBUGMSGTL(("get_mib_directory", "no mib directories set by environment\n"));
2589
            /** Not set use hard coded path */
2590
8
            if (confmibdir == NULL) {
2591
8
                DEBUGMSGTL(("get_mib_directory", "no mib directories set by config\n"));
2592
8
                netsnmp_set_mib_directory(NETSNMP_DEFAULT_MIBDIRS);
2593
8
            }
2594
0
            else if ((*confmibdir == '+') || (*confmibdir == '-')) {
2595
0
                DEBUGMSGTL(("get_mib_directory", "mib directories set by config (but added)\n"));
2596
0
                netsnmp_set_mib_directory(NETSNMP_DEFAULT_MIBDIRS);
2597
0
                netsnmp_set_mib_directory(confmibdir);
2598
0
            }
2599
0
            else {
2600
0
                DEBUGMSGTL(("get_mib_directory", "mib directories set by config\n"));
2601
0
                netsnmp_set_mib_directory(confmibdir);
2602
0
            }
2603
2.56k
        } else if ((*dir == '+') || (*dir == '-')) {
2604
0
            DEBUGMSGTL(("get_mib_directory", "mib directories set by environment (but added)\n"));
2605
0
            netsnmp_set_mib_directory(NETSNMP_DEFAULT_MIBDIRS);
2606
0
            netsnmp_set_mib_directory(dir);
2607
2.56k
        } else {
2608
2.56k
            DEBUGMSGTL(("get_mib_directory", "mib directories set by environment\n"));
2609
2.56k
            netsnmp_set_mib_directory(dir);
2610
2.56k
        }
2611
2.57k
        dir = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIBDIRS);
2612
2.57k
    }
2613
5.14k
    DEBUGMSGTL(("get_mib_directory", "mib directories set '%s'\n", dir));
2614
5.14k
    return(dir);
2615
5.14k
}
2616
2617
/*
2618
 * function : netsnmp_fixup_mib_directory
2619
 * arguments: -
2620
 * returns  : -
2621
 */
2622
void
2623
netsnmp_fixup_mib_directory(void)
2624
2.56k
{
2625
2.56k
    char *homepath = netsnmp_getenv("HOME");
2626
2.56k
    char *mibpath = netsnmp_get_mib_directory();
2627
2.56k
    char *oldmibpath = NULL;
2628
2.56k
    char *ptr_home;
2629
2.56k
    char *new_mibpath;
2630
2631
2.56k
    DEBUGTRACE;
2632
2.56k
    if (homepath && mibpath) {
2633
2.56k
        DEBUGMSGTL(("fixup_mib_directory", "mib directories '%s'\n", mibpath));
2634
2.57k
        while ((ptr_home = strstr(mibpath, "$HOME"))) {
2635
3
            new_mibpath = (char *)malloc(strlen(mibpath) - strlen("$HOME") +
2636
3
           strlen(homepath)+1);
2637
3
            if (new_mibpath) {
2638
3
                *ptr_home = 0; /* null out the spot where we stop copying */
2639
3
                sprintf(new_mibpath, "%s%s%s", mibpath, homepath,
2640
3
      ptr_home + strlen("$HOME"));
2641
                /** swap in the new value and repeat */
2642
3
                mibpath = new_mibpath;
2643
3
    if (oldmibpath != NULL) {
2644
0
        SNMP_FREE(oldmibpath);
2645
0
    }
2646
3
    oldmibpath = new_mibpath;
2647
3
            } else {
2648
0
                break;
2649
0
            }
2650
3
        }
2651
2652
2.56k
        netsnmp_set_mib_directory(mibpath);
2653
  
2654
  /*  The above copies the mibpath for us, so...  */
2655
2656
2.56k
  if (oldmibpath != NULL) {
2657
3
      SNMP_FREE(oldmibpath);
2658
3
  }
2659
2660
2.56k
    }
2661
2662
2.56k
}
2663
2664
/**
2665
 * Initialises the mib reader.
2666
 *
2667
 * Reads in all settings from the environment.
2668
 */
2669
void
2670
netsnmp_init_mib(void)
2671
2.56k
{
2672
2.56k
    const char     *prefix;
2673
2.56k
    char           *env_var, *entry;
2674
2.56k
    PrefixListPtr   pp = &mib_prefixes[0];
2675
2.56k
    char           *st = NULL;
2676
2677
2.56k
    if (Mib)
2678
0
        return;
2679
2.56k
    netsnmp_init_mib_internals();
2680
2681
    /*
2682
     * Initialise the MIB directory/ies 
2683
     */
2684
2.56k
    netsnmp_fixup_mib_directory();
2685
2.56k
    env_var = strdup(netsnmp_get_mib_directory());
2686
2.56k
    if (!env_var)
2687
0
        return;
2688
2689
2.56k
    DEBUGMSGTL(("init_mib",
2690
2.56k
                "Seen MIBDIRS: Looking in '%s' for mib dirs ...\n",
2691
2.56k
                env_var));
2692
2693
2.56k
    entry = strtok_r(env_var, ENV_SEPARATOR, &st);
2694
5.13k
    while (entry) {
2695
2.57k
        add_mibdir(entry);
2696
2.57k
        entry = strtok_r(NULL, ENV_SEPARATOR, &st);
2697
2.57k
    }
2698
2.56k
    SNMP_FREE(env_var);
2699
2700
2.56k
    env_var = netsnmp_getenv("MIBFILES");
2701
2.56k
    if (env_var != NULL) {
2702
0
        if (*env_var == '+')
2703
0
            entry = strtok_r(env_var+1, ENV_SEPARATOR, &st);
2704
0
        else
2705
0
            entry = strtok_r(env_var, ENV_SEPARATOR, &st);
2706
0
        while (entry) {
2707
0
            add_mibfile(entry, NULL);
2708
0
            entry = strtok_r(NULL, ENV_SEPARATOR, &st);
2709
0
        }
2710
0
    }
2711
2712
2.56k
    netsnmp_init_mib_internals();
2713
2714
    /*
2715
     * Read in any modules or mibs requested 
2716
     */
2717
2718
2.56k
    env_var = netsnmp_getenv("MIBS");
2719
2.56k
    if (env_var == NULL) {
2720
2.56k
        if (confmibs != NULL)
2721
0
            env_var = strdup(confmibs);
2722
2.56k
        else
2723
2.56k
            env_var = strdup(NETSNMP_DEFAULT_MIBS);
2724
2.56k
    } else {
2725
0
        env_var = strdup(env_var);
2726
0
    }
2727
2.56k
    if (env_var && ((*env_var == '+') || (*env_var == '-'))) {
2728
0
        entry =
2729
0
            (char *) malloc(strlen(NETSNMP_DEFAULT_MIBS) + strlen(env_var) + 2);
2730
0
        if (!entry) {
2731
0
            DEBUGMSGTL(("init_mib", "env mibs malloc failed"));
2732
0
            SNMP_FREE(env_var);
2733
0
            return;
2734
0
        } else {
2735
0
            if (*env_var == '+')
2736
0
                sprintf(entry, "%s%c%s", NETSNMP_DEFAULT_MIBS, ENV_SEPARATOR_CHAR,
2737
0
                        env_var+1);
2738
0
            else
2739
0
                sprintf(entry, "%s%c%s", env_var+1, ENV_SEPARATOR_CHAR,
2740
0
                        NETSNMP_DEFAULT_MIBS );
2741
0
        }
2742
0
        SNMP_FREE(env_var);
2743
0
        env_var = entry;
2744
0
    }
2745
2746
2.56k
    DEBUGMSGTL(("init_mib",
2747
2.56k
                "Seen MIBS: Looking in '%s' for mib files ...\n",
2748
2.56k
                env_var));
2749
2.56k
    entry = strtok_r(env_var, ENV_SEPARATOR, &st);
2750
125k
    while (entry) {
2751
123k
        if (strcasecmp(entry, DEBUG_ALWAYS_TOKEN) == 0) {
2752
0
            read_all_mibs();
2753
123k
        } else if (strstr(entry, "/") != NULL) {
2754
0
            read_mib(entry);
2755
123k
        } else {
2756
123k
            netsnmp_read_module(entry);
2757
123k
        }
2758
123k
        entry = strtok_r(NULL, ENV_SEPARATOR, &st);
2759
123k
    }
2760
2.56k
    adopt_orphans();
2761
2.56k
    SNMP_FREE(env_var);
2762
2763
2.56k
    env_var = netsnmp_getenv("MIBFILES");
2764
2.56k
    if (env_var != NULL) {
2765
0
        if ((*env_var == '+') || (*env_var == '-')) {
2766
#ifdef NETSNMP_DEFAULT_MIBFILES
2767
            entry =
2768
                (char *) malloc(strlen(NETSNMP_DEFAULT_MIBFILES) +
2769
                                strlen(env_var) + 2);
2770
            if (!entry) {
2771
                DEBUGMSGTL(("init_mib", "env mibfiles malloc failed"));
2772
            } else {
2773
                if (*env_var++ == '+')
2774
                    sprintf(entry, "%s%c%s", NETSNMP_DEFAULT_MIBFILES, ENV_SEPARATOR_CHAR,
2775
                            env_var );
2776
                else
2777
                    sprintf(entry, "%s%c%s", env_var, ENV_SEPARATOR_CHAR,
2778
                            NETSNMP_DEFAULT_MIBFILES );
2779
            }
2780
            SNMP_FREE(env_var);
2781
            env_var = entry;
2782
#else
2783
0
            env_var = strdup(env_var + 1);
2784
0
#endif
2785
0
        } else {
2786
0
            env_var = strdup(env_var);
2787
0
        }
2788
2.56k
    } else {
2789
#ifdef NETSNMP_DEFAULT_MIBFILES
2790
        env_var = strdup(NETSNMP_DEFAULT_MIBFILES);
2791
#endif
2792
2.56k
    }
2793
2794
2.56k
    if (env_var != NULL) {
2795
0
        DEBUGMSGTL(("init_mib",
2796
0
                    "Seen MIBFILES: Looking in '%s' for mib files ...\n",
2797
0
                    env_var));
2798
0
        entry = strtok_r(env_var, ENV_SEPARATOR, &st);
2799
0
        while (entry) {
2800
0
            read_mib(entry);
2801
0
            entry = strtok_r(NULL, ENV_SEPARATOR, &st);
2802
0
        }
2803
0
        SNMP_FREE(env_var);
2804
0
    }
2805
2806
2.56k
    prefix = netsnmp_getenv("PREFIX");
2807
2808
2.56k
    if (!prefix)
2809
2.56k
        prefix = Standard_Prefix;
2810
2811
2.56k
    Prefix = (char *) malloc(strlen(prefix) + 2);
2812
2.56k
    if (!Prefix)
2813
0
        DEBUGMSGTL(("init_mib", "Prefix malloc failed"));
2814
2.56k
    else
2815
2.56k
        strcpy(Prefix, prefix);
2816
2817
2.56k
    DEBUGMSGTL(("init_mib",
2818
2.56k
                "Seen PREFIX: Looking in '%s' for prefix ...\n", Prefix));
2819
2820
    /*
2821
     * remove trailing dot 
2822
     */
2823
2.56k
    if (Prefix) {
2824
2.56k
        env_var = &Prefix[strlen(Prefix) - 1];
2825
2.56k
        if (*env_var == '.')
2826
0
            *env_var = '\0';
2827
2.56k
    }
2828
2829
2.56k
    pp->str = Prefix;           /* fixup first mib_prefix entry */
2830
    /*
2831
     * now that the list of prefixes is built, save each string length. 
2832
     */
2833
17.9k
    while (pp->str) {
2834
15.4k
        pp->len = strlen(pp->str);
2835
15.4k
        pp++;
2836
15.4k
    }
2837
2838
2.56k
    Mib = tree_head;            /* Backwards compatibility */
2839
2.56k
    tree_top = calloc(1, sizeof(struct tree));
2840
    /*
2841
     * XX error check ? 
2842
     */
2843
2.56k
    if (tree_top) {
2844
2.56k
        tree_top->label = strdup("(top)");
2845
2.56k
        tree_top->child_list = tree_head;
2846
2.56k
    }
2847
2.56k
}
2848
2849
#ifndef NETSNMP_NO_LEGACY_DEFINITIONS
2850
void
2851
init_mib(void)
2852
0
{
2853
0
    netsnmp_init_mib();
2854
0
}
2855
#endif
2856
2857
2858
/**
2859
 * Unloads all mibs.
2860
 */
2861
void
2862
shutdown_mib(void)
2863
2.56k
{
2864
2.56k
    unload_all_mibs();
2865
2.56k
    if (tree_top) {
2866
2.56k
        if (tree_top->label)
2867
2.56k
            SNMP_FREE(tree_top->label);
2868
2.56k
        SNMP_FREE(tree_top);
2869
2.56k
    }
2870
2.56k
    tree_head = NULL;
2871
2.56k
    Mib = NULL;
2872
2.56k
    if (Prefix != NULL && Prefix != &Standard_Prefix[0])
2873
2.56k
        SNMP_FREE(Prefix);
2874
2.56k
    if (Prefix)
2875
0
        Prefix = NULL;
2876
2.56k
    SNMP_FREE(confmibs);
2877
2.56k
    SNMP_FREE(confmibdir);
2878
2.56k
}
2879
2880
/**
2881
 * Prints the MIBs to the file fp.
2882
 *
2883
 * @param fp   The file descriptor to print to.
2884
 */
2885
#ifndef NETSNMP_FEATURE_REMOVE_PRINT_MIB
2886
void
2887
print_mib(FILE * fp)
2888
0
{
2889
0
    print_subtree(fp, tree_head, 0);
2890
0
}
2891
#endif /* NETSNMP_FEATURE_REMOVE_PRINT_MIB */
2892
2893
void
2894
print_ascii_dump(FILE * fp)
2895
0
{
2896
0
    fprintf(fp, "dump DEFINITIONS ::= BEGIN\n");
2897
0
    print_ascii_dump_tree(fp, tree_head, 0);
2898
0
    fprintf(fp, "END\n");
2899
0
}
2900
2901
2902
/**
2903
 * Set's the printing function printomat in a subtree according
2904
 * it's type
2905
 *
2906
 * @param subtree    The subtree to set.
2907
 */
2908
void
2909
set_function(struct tree *subtree)
2910
89.8k
{
2911
89.8k
    subtree->printer = NULL;
2912
89.8k
    switch (subtree->type) {
2913
0
    case TYPE_OBJID:
2914
0
        subtree->printomat = sprint_realloc_object_identifier;
2915
0
        break;
2916
0
    case TYPE_OCTETSTR:
2917
0
        subtree->printomat = sprint_realloc_octet_string;
2918
0
        break;
2919
67
    case TYPE_INTEGER:
2920
67
        subtree->printomat = sprint_realloc_integer;
2921
67
        break;
2922
0
    case TYPE_INTEGER32:
2923
0
        subtree->printomat = sprint_realloc_integer;
2924
0
        break;
2925
0
    case TYPE_NETADDR:
2926
0
        subtree->printomat = sprint_realloc_networkaddress;
2927
0
        break;
2928
0
    case TYPE_IPADDR:
2929
0
        subtree->printomat = sprint_realloc_ipaddress;
2930
0
        break;
2931
0
    case TYPE_COUNTER:
2932
0
        subtree->printomat = sprint_realloc_counter;
2933
0
        break;
2934
0
    case TYPE_GAUGE:
2935
0
        subtree->printomat = sprint_realloc_gauge;
2936
0
        break;
2937
1
    case TYPE_TIMETICKS:
2938
1
        subtree->printomat = sprint_realloc_timeticks;
2939
1
        break;
2940
1
    case TYPE_OPAQUE:
2941
1
        subtree->printomat = sprint_realloc_opaque;
2942
1
        break;
2943
10
    case TYPE_NULL:
2944
10
        subtree->printomat = sprint_realloc_null;
2945
10
        break;
2946
1
    case TYPE_BITSTRING:
2947
1
        subtree->printomat = sprint_realloc_bitstring;
2948
1
        break;
2949
0
    case TYPE_NSAPADDRESS:
2950
0
        subtree->printomat = sprint_realloc_nsapaddress;
2951
0
        break;
2952
1
    case TYPE_COUNTER64:
2953
1
        subtree->printomat = sprint_realloc_counter64;
2954
1
        break;
2955
0
    case TYPE_UINTEGER:
2956
0
        subtree->printomat = sprint_realloc_uinteger;
2957
0
        break;
2958
1
    case TYPE_UNSIGNED32:
2959
1
        subtree->printomat = sprint_realloc_gauge;
2960
1
        break;
2961
88.7k
    case TYPE_OTHER:
2962
89.7k
    default:
2963
89.7k
        subtree->printomat = sprint_realloc_by_type;
2964
89.7k
        break;
2965
89.8k
    }
2966
89.8k
}
2967
2968
#endif /* NETSNMP_DISABLE_MIB_LOADING */
2969
2970
/**
2971
 * Reads an object identifier from an input string into internal OID form.
2972
 * 
2973
 * When called, out_len must hold the maximum length of the output array.
2974
 *
2975
 * @param input     the input string.
2976
 * @param output    the oid write.
2977
 * @param out_len   number of subid's in output.
2978
 * 
2979
 * @return 1 if successful.
2980
 * 
2981
 * If an error occurs, this function returns 0 and MAY set snmp_errno.
2982
 * snmp_errno is NOT set if SET_SNMP_ERROR evaluates to nothing.
2983
 * This can make multi-threaded use a tiny bit more robust.
2984
 */
2985
int
2986
read_objid(const char *input, oid * output, size_t * out_len)
2987
18.4k
{
2988
18.4k
#ifndef NETSNMP_DISABLE_MIB_LOADING
2989
18.4k
    struct tree    *root = tree_top;
2990
18.4k
    char            buf[SPRINT_MAX_LEN];
2991
18.4k
#endif /* NETSNMP_DISABLE_MIB_LOADING */
2992
18.4k
    int             ret, max_out_len;
2993
18.4k
    char           *name, ch;
2994
18.4k
    const char     *cp;
2995
2996
18.4k
    cp = input;
2997
1.08M
    while ((ch = *cp)) {
2998
1.08M
        if (('0' <= ch && ch <= '9')
2999
1.08M
            || ('a' <= ch && ch <= 'z')
3000
1.08M
            || ('A' <= ch && ch <= 'Z')
3001
1.08M
            || ch == '-')
3002
1.07M
            cp++;
3003
17.8k
        else
3004
17.8k
            break;
3005
1.08M
    }
3006
18.4k
#ifndef NETSNMP_DISABLE_MIB_LOADING
3007
18.4k
    if (ch == ':')
3008
1.01k
        return get_node(input, output, out_len);
3009
17.4k
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3010
3011
17.4k
    if (*input == '.')
3012
15.5k
        input++;
3013
1.93k
#ifndef NETSNMP_DISABLE_MIB_LOADING
3014
1.93k
    else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_READ_UCD_STYLE_OID)) {
3015
        /*
3016
         * get past leading '.', append '.' to Prefix. 
3017
         */
3018
272
        if (*Prefix == '.')
3019
272
            strlcpy(buf, Prefix + 1, sizeof(buf));
3020
0
        else
3021
0
            strlcpy(buf, Prefix, sizeof(buf));
3022
272
        strlcat(buf, ".", sizeof(buf));
3023
272
        strlcat(buf, input, sizeof(buf));
3024
272
        input = buf;
3025
272
    }
3026
17.4k
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3027
3028
17.4k
#ifndef NETSNMP_DISABLE_MIB_LOADING
3029
17.4k
    if ((root == NULL) && (tree_head != NULL)) {
3030
1.31k
        root = tree_head;
3031
1.31k
    }
3032
16.1k
    else if (root == NULL) {
3033
60
        SET_SNMP_ERROR(SNMPERR_NOMIB);
3034
60
        *out_len = 0;
3035
60
        return 0;
3036
60
    }
3037
17.4k
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3038
17.4k
    name = strdup(input);
3039
17.4k
    max_out_len = *out_len;
3040
17.4k
    *out_len = 0;
3041
17.4k
#ifndef NETSNMP_DISABLE_MIB_LOADING
3042
17.4k
    if ((ret =
3043
17.4k
         _add_strings_to_oid(root, name, output, out_len,
3044
17.4k
                             max_out_len)) <= 0)
3045
#else
3046
    if ((ret =
3047
         _add_strings_to_oid(NULL, name, output, out_len,
3048
                             max_out_len)) <= 0)
3049
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3050
1.16k
    {
3051
1.16k
        if (ret == 0)
3052
1.16k
            ret = SNMPERR_UNKNOWN_OBJID;
3053
1.16k
        SET_SNMP_ERROR(ret);
3054
1.16k
        SNMP_FREE(name);
3055
1.16k
        return 0;
3056
1.16k
    }
3057
16.2k
    SNMP_FREE(name);
3058
3059
16.2k
    return 1;
3060
17.4k
}
3061
3062
/**
3063
 * 
3064
 */
3065
void
3066
netsnmp_sprint_realloc_objid(u_char ** buf, size_t * buf_len,
3067
                             size_t * out_len, int allow_realloc,
3068
                             int *buf_overflow,
3069
                             const oid * objid, size_t objidlen)
3070
0
{
3071
0
    u_char         *tbuf = NULL, *cp = NULL;
3072
0
    size_t          tbuf_len = 256, tout_len = 0;
3073
0
    int             tbuf_overflow = 0;
3074
0
    int             output_format;
3075
3076
0
    if ((tbuf = calloc(tbuf_len, 1)) == NULL) {
3077
0
        tbuf_overflow = 1;
3078
0
    } else {
3079
0
        *tbuf = '.';
3080
0
        tout_len = 1;
3081
0
    }
3082
3083
0
    _oid_finish_printing(objid, objidlen,
3084
0
                         &tbuf, &tbuf_len, &tout_len,
3085
0
                         allow_realloc, &tbuf_overflow);
3086
3087
0
    if (tbuf_overflow) {
3088
0
        if (!*buf_overflow) {
3089
0
            snmp_strcat(buf, buf_len, out_len, allow_realloc, tbuf);
3090
0
            *buf_overflow = 1;
3091
0
        }
3092
0
        SNMP_FREE(tbuf);
3093
0
        return;
3094
0
    }
3095
3096
0
    output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3097
0
    if (0 == output_format) {
3098
0
        output_format = NETSNMP_OID_OUTPUT_NUMERIC;
3099
0
    }
3100
0
    switch (output_format) {
3101
0
    case NETSNMP_OID_OUTPUT_FULL:
3102
0
    case NETSNMP_OID_OUTPUT_NUMERIC:
3103
0
    case NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC:
3104
0
    case NETSNMP_OID_OUTPUT_SUFFIX:
3105
0
    case NETSNMP_OID_OUTPUT_MODULE:
3106
0
        cp = tbuf;
3107
0
        break;
3108
3109
0
    case NETSNMP_OID_OUTPUT_NONE:
3110
0
    default:
3111
0
        cp = NULL;
3112
0
    }
3113
3114
0
    if (!*buf_overflow &&
3115
0
        !snmp_strcat(buf, buf_len, out_len, allow_realloc, cp)) {
3116
0
        *buf_overflow = 1;
3117
0
    }
3118
0
    SNMP_FREE(tbuf);
3119
0
}
3120
3121
/**
3122
 * 
3123
 */
3124
#ifdef NETSNMP_DISABLE_MIB_LOADING
3125
void
3126
netsnmp_sprint_realloc_objid_tree(u_char ** buf, size_t * buf_len,
3127
                                  size_t * out_len, int allow_realloc,
3128
                                  int *buf_overflow,
3129
                                  const oid * objid, size_t objidlen)
3130
{
3131
    netsnmp_sprint_realloc_objid(buf, buf_len, out_len, allow_realloc,
3132
                                 buf_overflow, objid, objidlen);
3133
}
3134
#else
3135
struct tree    *
3136
netsnmp_sprint_realloc_objid_tree(u_char ** buf, size_t * buf_len,
3137
                                  size_t * out_len, int allow_realloc,
3138
                                  int *buf_overflow,
3139
                                  const oid * objid, size_t objidlen)
3140
1.21k
{
3141
1.21k
    u_char         *tbuf = NULL, *cp = NULL;
3142
1.21k
    size_t          tbuf_len = 512, tout_len = 0;
3143
1.21k
    struct tree    *subtree = tree_head;
3144
1.21k
    size_t          midpoint_offset = 0;
3145
1.21k
    int             tbuf_overflow = 0;
3146
1.21k
    int             output_format;
3147
3148
1.21k
    if ((tbuf = calloc(tbuf_len, 1)) == NULL) {
3149
0
        tbuf_overflow = 1;
3150
1.21k
    } else {
3151
1.21k
        *tbuf = '.';
3152
1.21k
        tout_len = 1;
3153
1.21k
    }
3154
3155
1.21k
    subtree = _get_realloc_symbol(objid, objidlen, subtree,
3156
1.21k
                                  &tbuf, &tbuf_len, &tout_len,
3157
1.21k
                                  allow_realloc, &tbuf_overflow, NULL,
3158
1.21k
                                  &midpoint_offset);
3159
3160
1.21k
    if (tbuf_overflow) {
3161
0
        if (!*buf_overflow) {
3162
0
            snmp_strcat(buf, buf_len, out_len, allow_realloc, tbuf);
3163
0
            *buf_overflow = 1;
3164
0
        }
3165
0
        SNMP_FREE(tbuf);
3166
0
        return subtree;
3167
0
    }
3168
3169
1.21k
    output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
3170
1.21k
    if (0 == output_format) {
3171
1.21k
        output_format = NETSNMP_OID_OUTPUT_MODULE;
3172
1.21k
    }
3173
1.21k
    switch (output_format) {
3174
0
    case NETSNMP_OID_OUTPUT_FULL:
3175
0
    case NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC:
3176
0
    case NETSNMP_OID_OUTPUT_NUMERIC:
3177
0
        cp = tbuf;
3178
0
        break;
3179
3180
0
    case NETSNMP_OID_OUTPUT_SUFFIX:
3181
1.21k
    case NETSNMP_OID_OUTPUT_MODULE:
3182
92.6k
        for (cp = tbuf; *cp; cp++);
3183
3184
1.21k
        if (midpoint_offset != 0) {
3185
919
            cp = tbuf + midpoint_offset - 2;    /*  beyond the '.'  */
3186
919
        } else {
3187
586
            while (cp >= tbuf) {
3188
586
                if (isalpha(*cp)) {
3189
293
                    break;
3190
293
                }
3191
293
                cp--;
3192
293
            }
3193
293
        }
3194
3195
7.23k
        while (cp >= tbuf) {
3196
6.97k
            if (*cp == '.') {
3197
954
                break;
3198
954
            }
3199
6.02k
            cp--;
3200
6.02k
        }
3201
3202
1.21k
        cp++;
3203
3204
1.21k
        if ((NETSNMP_OID_OUTPUT_MODULE == output_format)
3205
1.21k
            && cp > tbuf) {
3206
954
            char            modbuf[256] = { 0 }, *mod =
3207
954
                module_name(subtree->modid, modbuf);
3208
3209
            /*
3210
             * Don't add the module ID if it's just numeric (i.e. we couldn't look
3211
             * it up properly.  
3212
             */
3213
3214
954
            if (!*buf_overflow && modbuf[0] != '#') {
3215
0
                if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, mod) ||
3216
0
                    !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "::")) {
3217
0
                    *buf_overflow = 1;
3218
0
                }
3219
0
            }
3220
954
        }
3221
1.21k
        break;
3222
3223
0
    case NETSNMP_OID_OUTPUT_UCD:
3224
0
    {
3225
0
        PrefixListPtr   pp = &mib_prefixes[0];
3226
0
        size_t          ilen, tlen;
3227
0
        const char     *testcp;
3228
3229
0
        cp = tbuf;
3230
0
        tlen = strlen((char *) tbuf);
3231
3232
0
        while (pp->str) {
3233
0
            ilen = pp->len;
3234
0
            testcp = pp->str;
3235
3236
0
            if ((tlen > ilen) && memcmp(tbuf, testcp, ilen) == 0) {
3237
0
                cp += (ilen + 1);
3238
0
                break;
3239
0
            }
3240
0
            pp++;
3241
0
        }
3242
0
        break;
3243
0
    }
3244
3245
0
    case NETSNMP_OID_OUTPUT_NONE:
3246
0
    default:
3247
0
        cp = NULL;
3248
1.21k
    }
3249
3250
1.21k
    if (!*buf_overflow &&
3251
1.21k
        !snmp_strcat(buf, buf_len, out_len, allow_realloc, cp)) {
3252
0
        *buf_overflow = 1;
3253
0
    }
3254
1.21k
    SNMP_FREE(tbuf);
3255
1.21k
    return subtree;
3256
1.21k
}
3257
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3258
3259
int
3260
sprint_realloc_objid(u_char ** buf, size_t * buf_len,
3261
                     size_t * out_len, int allow_realloc,
3262
                     const oid * objid, size_t objidlen)
3263
713
{
3264
713
    int             buf_overflow = 0;
3265
3266
713
    netsnmp_sprint_realloc_objid_tree(buf, buf_len, out_len, allow_realloc,
3267
713
                                      &buf_overflow, objid, objidlen);
3268
713
    return !buf_overflow;
3269
713
}
3270
3271
#ifndef NETSNMP_FEATURE_REMOVE_SPRINT_OBJID
3272
int
3273
snprint_objid(char *buf, size_t buf_len,
3274
              const oid * objid, size_t objidlen)
3275
0
{
3276
0
    size_t          out_len = 0;
3277
3278
0
    if (sprint_realloc_objid((u_char **) & buf, &buf_len, &out_len, 0,
3279
0
                             objid, objidlen)) {
3280
0
        return (int) out_len;
3281
0
    } else {
3282
0
        return -1;
3283
0
    }
3284
0
}
3285
#endif /* NETSNMP_FEATURE_REMOVE_SPRINT_OBJID */
3286
3287
/**
3288
 * Prints an oid to stdout.
3289
 *
3290
 * @param objid      The oid to print
3291
 * @param objidlen   The length of oidid.
3292
 */
3293
void
3294
print_objid(const oid * objid, size_t objidlen)
3295
0
{                               /* number of subidentifiers */
3296
0
    fprint_objid(stdout, objid, objidlen);
3297
0
}
3298
3299
3300
/**
3301
 * Prints an oid to a file descriptor.
3302
 *
3303
 * @param f          The file descriptor to print to.
3304
 * @param objid      The oid to print
3305
 * @param objidlen   The length of oidid.
3306
 */
3307
void
3308
fprint_objid(FILE * f, const oid * objid, size_t objidlen)
3309
0
{                               /* number of subidentifiers */
3310
0
    u_char         *buf = NULL;
3311
0
    size_t          buf_len = 256, out_len = 0;
3312
0
    int             buf_overflow = 0;
3313
3314
0
    if ((buf = calloc(buf_len, 1)) == NULL) {
3315
0
        fprintf(f, "[TRUNCATED]\n");
3316
0
        return;
3317
0
    } else {
3318
0
        netsnmp_sprint_realloc_objid_tree(&buf, &buf_len, &out_len, 1,
3319
0
                                          &buf_overflow, objid, objidlen);
3320
0
        if (buf_overflow) {
3321
0
            fprintf(f, "%s [TRUNCATED]\n", buf);
3322
0
        } else {
3323
0
            fprintf(f, "%s\n", buf);
3324
0
        }
3325
0
    }
3326
3327
0
    SNMP_FREE(buf);
3328
0
}
3329
3330
int
3331
sprint_realloc_variable(u_char ** buf, size_t * buf_len,
3332
                        size_t * out_len, int allow_realloc,
3333
                        const oid * objid, size_t objidlen,
3334
                        const netsnmp_variable_list * variable)
3335
482
{
3336
482
    int             buf_overflow = 0;
3337
3338
482
#ifndef NETSNMP_DISABLE_MIB_LOADING
3339
482
    struct tree    *subtree = tree_head;
3340
3341
482
    subtree =
3342
482
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3343
482
        netsnmp_sprint_realloc_objid_tree(buf, buf_len, out_len,
3344
482
                                          allow_realloc, &buf_overflow,
3345
482
                                          objid, objidlen);
3346
3347
482
    if (buf_overflow) {
3348
0
        return 0;
3349
0
    }
3350
482
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_BARE_VALUE)) {
3351
277
        if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICKE_PRINT)) {
3352
197
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " = ")) {
3353
0
                return 0;
3354
0
            }
3355
197
        } else {
3356
80
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT)) {
3357
0
                if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, " ")) {
3358
0
                    return 0;
3359
0
                }
3360
80
            } else {
3361
80
                if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3362
80
                                  " = ")) {
3363
0
                    return 0;
3364
0
                }
3365
80
            }                   /* end if-else NETSNMP_DS_LIB_QUICK_PRINT */
3366
80
        }                       /* end if-else NETSNMP_DS_LIB_QUICKE_PRINT */
3367
277
    } else {
3368
205
        *out_len = 0;
3369
205
    }
3370
3371
482
    if (variable->type == SNMP_NOSUCHOBJECT) {
3372
0
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3373
0
                            "No Such Object available on this agent at this OID");
3374
482
    } else if (variable->type == SNMP_NOSUCHINSTANCE) {
3375
0
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3376
0
                            "No Such Instance currently exists at this OID");
3377
482
    } else if (variable->type == SNMP_ENDOFMIBVIEW) {
3378
0
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3379
0
                            "No more variables left in this MIB View (It is past the end of the MIB tree)");
3380
0
#ifndef NETSNMP_DISABLE_MIB_LOADING
3381
482
    } else if (subtree) {
3382
238
        const char *units = NULL;
3383
238
        const char *hint = NULL;
3384
238
        if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
3385
238
                                    NETSNMP_DS_LIB_DONT_PRINT_UNITS)) {
3386
35
            units = subtree->units;
3387
35
        }
3388
3389
238
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
3390
238
                                    NETSNMP_DS_LIB_NO_DISPLAY_HINT)) {
3391
23
      hint = subtree->hint;
3392
23
        }
3393
3394
238
        if (subtree->printomat) {
3395
238
            return (*subtree->printomat) (buf, buf_len, out_len,
3396
238
                                          allow_realloc, variable,
3397
238
                                          subtree->enums, hint,
3398
238
                                          units);
3399
238
        } else {
3400
0
            return sprint_realloc_by_type(buf, buf_len, out_len,
3401
0
                                          allow_realloc, variable,
3402
0
                                          subtree->enums, hint,
3403
0
                                          units);
3404
0
        }
3405
238
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3406
244
    } else {
3407
        /*
3408
         * Handle rare case where tree is empty.  
3409
         */
3410
244
        return sprint_realloc_by_type(buf, buf_len, out_len, allow_realloc,
3411
244
                                      variable, NULL, NULL, NULL);
3412
244
    }
3413
482
}
3414
3415
#ifndef NETSNMP_FEATURE_REMOVE_SNPRINT_VARABLE
3416
int
3417
snprint_variable(char *buf, size_t buf_len,
3418
                 const oid * objid, size_t objidlen,
3419
                 const netsnmp_variable_list * variable)
3420
0
{
3421
0
    size_t          out_len = 0;
3422
3423
0
    if (sprint_realloc_variable((u_char **) & buf, &buf_len, &out_len, 0,
3424
0
                                objid, objidlen, variable)) {
3425
0
        return (int) out_len;
3426
0
    } else {
3427
0
        return -1;
3428
0
    }
3429
0
}
3430
#endif /* NETSNMP_FEATURE_REMOVE_SNPRINT_VARABLE  */
3431
3432
/**
3433
 * Prints a variable to stdout.
3434
 *
3435
 * @param objid     The object id.
3436
 * @param objidlen  The length of the object id.
3437
 * @param variable  The variable to print.
3438
 */
3439
void
3440
print_variable(const oid * objid,
3441
               size_t objidlen, const netsnmp_variable_list * variable)
3442
482
{
3443
482
    fprint_variable(stdout, objid, objidlen, variable);
3444
482
}
3445
3446
3447
/**
3448
 * Prints a variable to a file descriptor.
3449
 *
3450
 * @param f         The file descriptor to print to.
3451
 * @param objid     The object id.
3452
 * @param objidlen  The length of the object id.
3453
 * @param variable  The variable to print.
3454
 */
3455
void
3456
fprint_variable(FILE * f,
3457
                const oid * objid,
3458
                size_t objidlen, const netsnmp_variable_list * variable)
3459
482
{
3460
482
    u_char         *buf = NULL;
3461
482
    size_t          buf_len = 256, out_len = 0;
3462
3463
482
    if ((buf = calloc(buf_len, 1)) == NULL) {
3464
0
        fprintf(f, "[TRUNCATED]\n");
3465
0
        return;
3466
482
    } else {
3467
482
        if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,
3468
482
                                    objid, objidlen, variable)) {
3469
482
            fprintf(f, "%s\n", buf);
3470
482
        } else {
3471
0
            fprintf(f, "%s [TRUNCATED]\n", buf);
3472
0
        }
3473
482
    }
3474
3475
482
    SNMP_FREE(buf);
3476
482
}
3477
3478
int
3479
sprint_realloc_value(u_char ** buf, size_t * buf_len,
3480
                     size_t * out_len, int allow_realloc,
3481
                     const oid * objid, size_t objidlen,
3482
                     const netsnmp_variable_list * variable)
3483
0
{
3484
0
    if (variable->type == SNMP_NOSUCHOBJECT) {
3485
0
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3486
0
                            "No Such Object available on this agent at this OID");
3487
0
    } else if (variable->type == SNMP_NOSUCHINSTANCE) {
3488
0
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3489
0
                            "No Such Instance currently exists at this OID");
3490
0
    } else if (variable->type == SNMP_ENDOFMIBVIEW) {
3491
0
        return snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
3492
0
                            "No more variables left in this MIB View (It is past the end of the MIB tree)");
3493
0
    } else {
3494
0
#ifndef NETSNMP_DISABLE_MIB_LOADING
3495
0
        const char *units = NULL;
3496
0
        struct tree *subtree = tree_head;
3497
0
  subtree = get_tree(objid, objidlen, subtree);
3498
0
        if (subtree && !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
3499
0
                                            NETSNMP_DS_LIB_DONT_PRINT_UNITS)) {
3500
0
            units = subtree->units;
3501
0
        }
3502
0
        if (subtree) {
3503
0
      if(subtree->printomat) {
3504
0
    return (*subtree->printomat) (buf, buf_len, out_len,
3505
0
                allow_realloc, variable,
3506
0
                subtree->enums, subtree->hint,
3507
0
                units);
3508
0
      } else {
3509
0
    return sprint_realloc_by_type(buf, buf_len, out_len,
3510
0
                allow_realloc, variable,
3511
0
                subtree->enums, subtree->hint,
3512
0
                units);
3513
0
      }
3514
0
  }
3515
0
#endif /* NETSNMP_DISABLE_MIB_LOADING */
3516
0
        return sprint_realloc_by_type(buf, buf_len, out_len,
3517
0
                                      allow_realloc, variable,
3518
0
                                      NULL, NULL, NULL);
3519
0
    }
3520
0
}
3521
3522
#ifndef NETSNMP_FEATURE_REMOVE_SNPRINT_VALUE
3523
/* used in the perl module */
3524
int
3525
snprint_value(char *buf, size_t buf_len,
3526
              const oid * objid, size_t objidlen,
3527
              const netsnmp_variable_list * variable)
3528
0
{
3529
0
    size_t          out_len = 0;
3530
3531
0
    if (sprint_realloc_value((u_char **) & buf, &buf_len, &out_len, 0,
3532
0
                             objid, objidlen, variable)) {
3533
0
        return (int) out_len;
3534
0
    } else {
3535
0
        return -1;
3536
0
    }
3537
0
}
3538
#endif /* NETSNMP_FEATURE_REMOVE_SNPRINT_VALUE */
3539
3540
void
3541
print_value(const oid * objid,
3542
            size_t objidlen, const netsnmp_variable_list * variable)
3543
0
{
3544
0
    fprint_value(stdout, objid, objidlen, variable);
3545
0
}
3546
3547
void
3548
fprint_value(FILE * f,
3549
             const oid * objid,
3550
             size_t objidlen, const netsnmp_variable_list * variable)
3551
0
{
3552
0
    u_char         *buf = NULL;
3553
0
    size_t          buf_len = 256, out_len = 0;
3554
3555
0
    if ((buf = calloc(buf_len, 1)) == NULL) {
3556
0
        fprintf(f, "[TRUNCATED]\n");
3557
0
        return;
3558
0
    } else {
3559
0
        if (sprint_realloc_value(&buf, &buf_len, &out_len, 1,
3560
0
                                 objid, objidlen, variable)) {
3561
0
            fprintf(f, "%s\n", buf);
3562
0
        } else {
3563
0
            fprintf(f, "%s [TRUNCATED]\n", buf);
3564
0
        }
3565
0
    }
3566
3567
0
    SNMP_FREE(buf);
3568
0
}
3569
3570
3571
/**
3572
 * Takes the value in VAR and turns it into an OID segment in var->name.
3573
 *  
3574
 * @param var    The variable.
3575
 *
3576
 * @return SNMPERR_SUCCESS or SNMPERR_GENERR 
3577
 */
3578
int
3579
build_oid_segment(netsnmp_variable_list * var)
3580
0
{
3581
0
    int             i;
3582
0
    uint32_t        ipaddr;
3583
3584
0
    if (var->name && var->name != var->name_loc)
3585
0
        SNMP_FREE(var->name);
3586
0
    switch (var->type) {
3587
0
    case ASN_INTEGER:
3588
0
    case ASN_COUNTER:
3589
0
    case ASN_GAUGE:
3590
0
    case ASN_TIMETICKS:
3591
0
        var->name_length = 1;
3592
0
        var->name = var->name_loc;
3593
0
        var->name[0] = *(var->val.integer);
3594
0
        break;
3595
3596
0
    case ASN_IPADDRESS:
3597
0
        var->name_length = 4;
3598
0
        var->name = var->name_loc;
3599
0
        memcpy(&ipaddr, var->val.string, sizeof(ipaddr));
3600
0
        var->name[0] = (ipaddr >> 24) & 0xff;
3601
0
        var->name[1] = (ipaddr >> 16) & 0xff;
3602
0
        var->name[2] = (ipaddr >>  8) & 0xff;
3603
0
        var->name[3] = (ipaddr >>  0) & 0xff;
3604
0
        break;
3605
        
3606
0
    case ASN_PRIV_IMPLIED_OBJECT_ID:
3607
0
        var->name_length = var->val_len / sizeof(oid);
3608
0
        if (var->name_length > (OID_LENGTH(var->name_loc)))
3609
0
            var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
3610
0
        else
3611
0
            var->name = var->name_loc;
3612
0
        if (var->name == NULL)
3613
0
            return SNMPERR_GENERR;
3614
3615
0
        for (i = 0; i < (int) var->name_length; i++)
3616
0
            var->name[i] = var->val.objid[i];
3617
0
        break;
3618
3619
0
    case ASN_OBJECT_ID:
3620
0
        var->name_length = var->val_len / sizeof(oid) + 1;
3621
0
        if (var->name_length > (OID_LENGTH(var->name_loc)))
3622
0
            var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
3623
0
        else
3624
0
            var->name = var->name_loc;
3625
0
        if (var->name == NULL)
3626
0
            return SNMPERR_GENERR;
3627
3628
0
        var->name[0] = var->name_length - 1;
3629
0
        for (i = 0; i < (int) var->name_length - 1; i++)
3630
0
            var->name[i + 1] = var->val.objid[i];
3631
0
        break;
3632
3633
0
    case ASN_PRIV_IMPLIED_OCTET_STR:
3634
0
        var->name_length = var->val_len;
3635
0
        if (var->name_length > (OID_LENGTH(var->name_loc)))
3636
0
            var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
3637
0
        else
3638
0
            var->name = var->name_loc;
3639
0
        if (var->name == NULL)
3640
0
            return SNMPERR_GENERR;
3641
3642
0
        for (i = 0; i < (int) var->val_len; i++)
3643
0
            var->name[i] = (oid) var->val.string[i];
3644
0
        break;
3645
3646
0
    case ASN_OPAQUE:
3647
0
    case ASN_OCTET_STR:
3648
0
        var->name_length = var->val_len + 1;
3649
0
        if (var->name_length > (OID_LENGTH(var->name_loc)))
3650
0
            var->name = (oid *) malloc(sizeof(oid) * (var->name_length));
3651
0
        else
3652
0
            var->name = var->name_loc;
3653
0
        if (var->name == NULL)
3654
0
            return SNMPERR_GENERR;
3655
3656
0
        var->name[0] = (oid) var->val_len;
3657
0
        for (i = 0; i < (int) var->val_len; i++)
3658
0
            var->name[i + 1] = (oid) var->val.string[i];
3659
0
        break;
3660
3661
0
    default:
3662
0
        DEBUGMSGTL(("build_oid_segment",
3663
0
                    "invalid asn type: %d\n", var->type));
3664
0
        return SNMPERR_GENERR;
3665
0
    }
3666
3667
0
    if (var->name_length > MAX_OID_LEN) {
3668
0
        DEBUGMSGTL(("build_oid_segment",
3669
0
                    "Something terribly wrong, namelen = %lu\n",
3670
0
                    (unsigned long)var->name_length));
3671
0
        return SNMPERR_GENERR;
3672
0
    }
3673
3674
0
    return SNMPERR_SUCCESS;
3675
0
}
3676
3677
3678
/**
3679
 * Concatenate a prefix and the OIDs of a variable list.
3680
 *
3681
 * @param[out]    in         Output buffer.
3682
 * @param[in]     in_len     Maximum number of OID components that fit in @p in.
3683
 * @param[out]    out_len    Number of OID components of the result.
3684
 * @param[in]     prefix     OID to be copied to the start of the output buffer.
3685
 * @param[in]     prefix_len Number of OID components to copy from @p prefix.
3686
 * @param[in,out] indexes    Variable list for which var->name should be set
3687
 *                           for each variable var in the list and whose OIDs
3688
 *                           should be appended to @p in.
3689
 */
3690
int
3691
build_oid_noalloc(oid * in, size_t in_len, size_t * out_len,
3692
                  const oid * prefix, size_t prefix_len,
3693
                  netsnmp_variable_list * indexes)
3694
0
{
3695
0
    netsnmp_variable_list *var;
3696
3697
0
    if (prefix) {
3698
0
        if (in_len < prefix_len)
3699
0
            return SNMPERR_GENERR;
3700
0
        memcpy(in, prefix, prefix_len * sizeof(oid));
3701
0
        *out_len = prefix_len;
3702
0
    } else {
3703
0
        *out_len = 0;
3704
0
    }
3705
3706
0
    for (var = indexes; var != NULL; var = var->next_variable) {
3707
0
        if (build_oid_segment(var) != SNMPERR_SUCCESS)
3708
0
            return SNMPERR_GENERR;
3709
0
        if (var->name_length + *out_len <= in_len) {
3710
0
            memcpy(&(in[*out_len]), var->name,
3711
0
                   sizeof(oid) * var->name_length);
3712
0
            *out_len += var->name_length;
3713
0
        } else {
3714
0
            return SNMPERR_GENERR;
3715
0
        }
3716
0
    }
3717
3718
0
    DEBUGMSGTL(("build_oid_noalloc", "generated: "));
3719
0
    DEBUGMSGOID(("build_oid_noalloc", in, *out_len));
3720
0
    DEBUGMSG(("build_oid_noalloc", "\n"));
3721
0
    return SNMPERR_SUCCESS;
3722
0
}
3723
3724
int
3725
build_oid(oid ** out, size_t * out_len,
3726
          oid * prefix, size_t prefix_len, netsnmp_variable_list * indexes)
3727
0
{
3728
0
    oid             tmpout[MAX_OID_LEN];
3729
3730
    /*
3731
     * xxx-rks: inefficient. try only building segments to find index len:
3732
     *   for (var = indexes; var != NULL; var = var->next_variable) {
3733
     *      if (build_oid_segment(var) != SNMPERR_SUCCESS)
3734
     *         return SNMPERR_GENERR;
3735
     *      *out_len += var->name_length;
3736
     *
3737
     * then see if it fits in existing buffer, or realloc buffer.
3738
     */
3739
0
    if (build_oid_noalloc(tmpout, sizeof(tmpout) / sizeof(tmpout[0]), out_len,
3740
0
                          prefix, prefix_len, indexes) != SNMPERR_SUCCESS)
3741
0
        return SNMPERR_GENERR;
3742
3743
    /** xxx-rks: should free previous value? */
3744
0
    snmp_clone_mem((void **) out, (void *) tmpout, *out_len * sizeof(oid));
3745
3746
0
    return SNMPERR_SUCCESS;
3747
0
}
3748
3749
/*
3750
 * vblist_out must contain a pre-allocated string of variables into
3751
 * which indexes can be extracted based on the previously existing
3752
 * types in the variable chain
3753
 * returns:
3754
 * SNMPERR_GENERR  on error
3755
 * SNMPERR_SUCCESS on success
3756
 */
3757
3758
int
3759
parse_oid_indexes(oid * oidIndex, size_t oidLen,
3760
                  netsnmp_variable_list * data)
3761
0
{
3762
0
    netsnmp_variable_list *var = data;
3763
3764
0
    while (var && oidLen > 0) {
3765
3766
0
        if (parse_one_oid_index(&oidIndex, &oidLen, var, 0) !=
3767
0
            SNMPERR_SUCCESS)
3768
0
            break;
3769
3770
0
        var = var->next_variable;
3771
0
    }
3772
3773
0
    if (var != NULL || oidLen != 0)
3774
0
        return SNMPERR_GENERR;
3775
0
    return SNMPERR_SUCCESS;
3776
0
}
3777
3778
3779
int
3780
parse_one_oid_index(oid ** oidStart, size_t * oidLen,
3781
                    netsnmp_variable_list * data, int complete)
3782
0
{
3783
0
    netsnmp_variable_list *var = data;
3784
0
    oid             tmpout[MAX_OID_LEN];
3785
0
    unsigned int    i;
3786
0
    unsigned int    uitmp = 0;
3787
3788
0
    oid            *oidIndex = *oidStart;
3789
3790
0
    if (var == NULL || ((*oidLen == 0) && (complete == 0)))
3791
0
        return SNMPERR_GENERR;
3792
0
    else {
3793
0
        switch (var->type) {
3794
0
        case ASN_INTEGER:
3795
0
        case ASN_COUNTER:
3796
0
        case ASN_GAUGE:
3797
0
        case ASN_TIMETICKS:
3798
0
            if (*oidLen) {
3799
0
                snmp_set_var_value(var, (u_char *) oidIndex++,
3800
0
                                   sizeof(oid));
3801
0
                --(*oidLen);
3802
0
            } else {
3803
0
                snmp_set_var_value(var, (u_char *) oidLen, sizeof(long));
3804
0
            }
3805
0
            DEBUGMSGTL(("parse_oid_indexes",
3806
0
                        "Parsed int(%d): %ld\n", var->type,
3807
0
                        *var->val.integer));
3808
0
            break;
3809
3810
0
        case ASN_IPADDRESS:
3811
0
            if ((4 > *oidLen) && (complete == 0))
3812
0
                return SNMPERR_GENERR;
3813
            
3814
0
            for (i = 0; i < 4 && i < *oidLen; ++i) {
3815
0
                if (oidIndex[i] > 255) {
3816
0
                    DEBUGMSGTL(("parse_oid_indexes",
3817
0
                                "illegal oid in index: %" NETSNMP_PRIo "d\n",
3818
0
                                oidIndex[0]));
3819
0
                        return SNMPERR_GENERR;  /* sub-identifier too large */
3820
0
                    }
3821
0
                    uitmp = uitmp + (oidIndex[i] << (8*(3-i)));
3822
0
                }
3823
0
            if (4 > (int) (*oidLen)) {
3824
0
                oidIndex += *oidLen;
3825
0
                (*oidLen) = 0;
3826
0
            } else {
3827
0
                oidIndex += 4;
3828
0
                (*oidLen) -= 4;
3829
0
            }
3830
0
            uitmp = htonl(uitmp); /* put it in proper order for byte copies */
3831
0
            uitmp = 
3832
0
                snmp_set_var_value(var, (u_char *) &uitmp, 4);
3833
0
            DEBUGMSGTL(("parse_oid_indexes",
3834
0
                        "Parsed ipaddr(%d): %d.%d.%d.%d\n", var->type,
3835
0
                        var->val.string[0], var->val.string[1],
3836
0
                        var->val.string[2], var->val.string[3]));
3837
0
            break;
3838
3839
0
        case ASN_OBJECT_ID:
3840
0
        case ASN_PRIV_IMPLIED_OBJECT_ID:
3841
0
            if (var->type == ASN_PRIV_IMPLIED_OBJECT_ID) {
3842
                /*
3843
                 * might not be implied, might be fixed len. check if
3844
                 * caller set up val len, and use it if they did.
3845
                 */
3846
0
                if (0 == var->val_len)
3847
0
                    uitmp = *oidLen;
3848
0
                else {
3849
0
                    DEBUGMSGTL(("parse_oid_indexes:fix", "fixed len oid\n"));
3850
0
                    uitmp = var->val_len;
3851
0
                }
3852
0
            } else {
3853
0
                if (*oidLen) {
3854
0
                    uitmp = *oidIndex++;
3855
0
                    --(*oidLen);
3856
0
                } else {
3857
0
                    uitmp = 0;
3858
0
                }
3859
0
                if ((uitmp > *oidLen) && (complete == 0))
3860
0
                    return SNMPERR_GENERR;
3861
0
            }
3862
3863
0
            if (uitmp > MAX_OID_LEN)
3864
0
                return SNMPERR_GENERR;  /* too big and illegal */
3865
3866
0
            if (uitmp > *oidLen) {
3867
0
                memcpy(tmpout, oidIndex, sizeof(oid) * (*oidLen));
3868
0
                memset(&tmpout[*oidLen], 0x00,
3869
0
                       sizeof(oid) * (uitmp - *oidLen));
3870
0
                snmp_set_var_value(var, (u_char *) tmpout,
3871
0
                                   sizeof(oid) * uitmp);
3872
0
                oidIndex += *oidLen;
3873
0
                (*oidLen) = 0;
3874
0
            } else {
3875
0
                snmp_set_var_value(var, (u_char *) oidIndex,
3876
0
                                   sizeof(oid) * uitmp);
3877
0
                oidIndex += uitmp;
3878
0
                (*oidLen) -= uitmp;
3879
0
            }
3880
3881
0
            DEBUGMSGTL(("parse_oid_indexes", "Parsed oid: "));
3882
0
            DEBUGMSGOID(("parse_oid_indexes",
3883
0
                         var->val.objid, var->val_len / sizeof(oid)));
3884
0
            DEBUGMSG(("parse_oid_indexes", "\n"));
3885
0
            break;
3886
3887
0
        case ASN_OPAQUE:
3888
0
        case ASN_OCTET_STR:
3889
0
        case ASN_PRIV_IMPLIED_OCTET_STR:
3890
0
            if (var->type == ASN_PRIV_IMPLIED_OCTET_STR) {
3891
                /*
3892
                 * might not be implied, might be fixed len. check if
3893
                 * caller set up val len, and use it if they did.
3894
                 */
3895
0
                if (0 == var->val_len)
3896
0
                    uitmp = *oidLen;
3897
0
                else {
3898
0
                    DEBUGMSGTL(("parse_oid_indexes:fix", "fixed len str\n"));
3899
0
                    uitmp = var->val_len;
3900
0
                }
3901
0
            } else {
3902
0
                if (*oidLen) {
3903
0
                    uitmp = *oidIndex++;
3904
0
                    --(*oidLen);
3905
0
                } else {
3906
0
                    uitmp = 0;
3907
0
                }
3908
0
                if ((uitmp > *oidLen) && (complete == 0))
3909
0
                    return SNMPERR_GENERR;
3910
0
            }
3911
3912
            /*
3913
             * we handle this one ourselves since we don't have
3914
             * pre-allocated memory to copy from using
3915
             * snmp_set_var_value() 
3916
             */
3917
3918
0
            if (uitmp == 0)
3919
0
                break;          /* zero length strings shouldn't malloc */
3920
3921
0
            if (uitmp > MAX_OID_LEN)
3922
0
                return SNMPERR_GENERR;  /* too big and illegal */
3923
3924
            /*
3925
             * malloc by size+1 to allow a null to be appended. 
3926
             */
3927
0
            var->val_len = uitmp;
3928
0
            var->val.string = calloc(1, uitmp + 1);
3929
0
            if (var->val.string == NULL)
3930
0
                return SNMPERR_GENERR;
3931
3932
0
            if ((size_t)uitmp > (*oidLen)) {
3933
0
                for (i = 0; i < *oidLen; ++i)
3934
0
                    var->val.string[i] = (u_char) * oidIndex++;
3935
0
                for (i = *oidLen; i < uitmp; ++i)
3936
0
                    var->val.string[i] = '\0';
3937
0
                (*oidLen) = 0;
3938
0
            } else {
3939
0
                for (i = 0; i < uitmp; ++i)
3940
0
                    var->val.string[i] = (u_char) * oidIndex++;
3941
0
                (*oidLen) -= uitmp;
3942
0
            }
3943
0
            var->val.string[uitmp] = '\0';
3944
3945
0
            DEBUGMSGTL(("parse_oid_indexes",
3946
0
                        "Parsed str(%d): %s\n", var->type,
3947
0
                        var->val.string));
3948
0
            break;
3949
3950
0
        default:
3951
0
            DEBUGMSGTL(("parse_oid_indexes",
3952
0
                        "invalid asn type: %d\n", var->type));
3953
0
            return SNMPERR_GENERR;
3954
0
        }
3955
0
    }
3956
0
    (*oidStart) = oidIndex;
3957
0
    return SNMPERR_SUCCESS;
3958
0
}
3959
3960
/*
3961
 * dump_realloc_oid_to_inetaddress:
3962
 *   return 0 for failure,
3963
 *   return 1 for success,
3964
 *   return 2 for not handled
3965
 */
3966
3967
int 
3968
dump_realloc_oid_to_inetaddress(const int addr_type, const oid * objid, size_t objidlen, 
3969
                                u_char ** buf, size_t * buf_len,
3970
                                size_t * out_len, int allow_realloc, 
3971
                                char quotechar)
3972
0
{
3973
0
    int             i, len;
3974
0
    char            intbuf[64], *p;
3975
0
    char *const     end = intbuf + sizeof(intbuf);
3976
0
    unsigned char  *zc;
3977
0
    unsigned long   zone;
3978
3979
0
    if (!buf)
3980
0
        return 1;
3981
3982
0
    for (i = 0; i < objidlen; i++)
3983
0
        if (objid[i] > 255)
3984
0
            return 2;
3985
3986
0
    p = intbuf;
3987
0
    *p++ = quotechar;
3988
3989
0
    switch (addr_type) {
3990
0
    case IPV4:
3991
0
    case IPV4Z:
3992
0
        if ((addr_type == IPV4  && objidlen != 4) ||
3993
0
            (addr_type == IPV4Z && objidlen != 8))
3994
0
            return 2;
3995
3996
0
        len = snprintf(p, end - p, "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u."
3997
0
                      "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u",
3998
0
                      objid[0], objid[1], objid[2], objid[3]);
3999
0
        p += len;
4000
0
        if (p >= end)
4001
0
            return 2;
4002
0
        if (addr_type == IPV4Z) {
4003
0
            zc = (unsigned char*)&zone;
4004
0
            zc[0] = objid[4];
4005
0
            zc[1] = objid[5];
4006
0
            zc[2] = objid[6];
4007
0
            zc[3] = objid[7];
4008
0
            zone = ntohl(zone);
4009
0
            len = snprintf(p, end - p, "%%%lu", zone);
4010
0
            p += len;
4011
0
            if (p >= end)
4012
0
                return 2;
4013
0
        }
4014
4015
0
        break;
4016
4017
0
    case IPV6:
4018
0
    case IPV6Z:
4019
0
        if ((addr_type == IPV6 && objidlen != 16) ||
4020
0
            (addr_type == IPV6Z && objidlen != 20))
4021
0
            return 2;
4022
4023
0
        len = 0;
4024
0
        for (i = 0; i < 16; i ++) {
4025
0
            len = snprintf(p, end - p, "%s%02" NETSNMP_PRIo "x", i ? ":" : "",
4026
0
                           objid[i]);
4027
0
            p += len;
4028
0
            if (p >= end)
4029
0
                return 2;
4030
0
        }
4031
4032
0
        if (addr_type == IPV6Z) {
4033
0
            zc = (unsigned char*)&zone;
4034
0
            zc[0] = objid[16];
4035
0
            zc[1] = objid[17];
4036
0
            zc[2] = objid[18];
4037
0
            zc[3] = objid[19];
4038
0
            zone = ntohl(zone);
4039
0
            len = snprintf(p, end - p, "%%%lu", zone);
4040
0
            p += len;
4041
0
            if (p >= end)
4042
0
                return 2;
4043
0
        }
4044
4045
0
        break;
4046
4047
0
    case DNS:
4048
0
    default: 
4049
        /* DNS can just be handled by dump_realloc_oid_to_string() */
4050
0
        return 2;
4051
0
    }
4052
4053
0
    *p++ = quotechar;
4054
0
    if (p >= end)
4055
0
        return 2;
4056
4057
0
    *p++ = '\0';
4058
0
    if (p >= end)
4059
0
        return 2;
4060
4061
0
    return snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf);
4062
0
}
4063
4064
int
4065
dump_realloc_oid_to_string(const oid * objid, size_t objidlen,
4066
                           u_char ** buf, size_t * buf_len,
4067
                           size_t * out_len, int allow_realloc,
4068
                           char quotechar)
4069
0
{
4070
0
    if (buf) {
4071
0
        int             i, alen;
4072
4073
0
        for (i = 0, alen = 0; i < (int) objidlen; i++) {
4074
0
            oid             tst = objid[i];
4075
0
            if ((tst > 254) || (!isprint(tst))) {
4076
0
                tst = (oid) '.';
4077
0
            }
4078
4079
0
            if (alen == 0) {
4080
0
                if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) {
4081
0
                    while ((*out_len + 2) >= *buf_len) {
4082
0
                        if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
4083
0
                            return 0;
4084
0
                        }
4085
0
                    }
4086
0
                    *(*buf + *out_len) = '\\';
4087
0
                    (*out_len)++;
4088
0
                }
4089
0
                while ((*out_len + 2) >= *buf_len) {
4090
0
                    if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
4091
0
                        return 0;
4092
0
                    }
4093
0
                }
4094
0
                *(*buf + *out_len) = quotechar;
4095
0
                (*out_len)++;
4096
0
            }
4097
4098
0
            while ((*out_len + 2) >= *buf_len) {
4099
0
                if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
4100
0
                    return 0;
4101
0
                }
4102
0
            }
4103
0
            *(*buf + *out_len) = (char) tst;
4104
0
            (*out_len)++;
4105
0
            alen++;
4106
0
        }
4107
4108
0
        if (alen) {
4109
0
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) {
4110
0
                while ((*out_len + 2) >= *buf_len) {
4111
0
                    if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
4112
0
                        return 0;
4113
0
                    }
4114
0
                }
4115
0
                *(*buf + *out_len) = '\\';
4116
0
                (*out_len)++;
4117
0
            }
4118
0
            while ((*out_len + 2) >= *buf_len) {
4119
0
                if (!(allow_realloc && snmp_realloc(buf, buf_len))) {
4120
0
                    return 0;
4121
0
                }
4122
0
            }
4123
0
            *(*buf + *out_len) = quotechar;
4124
0
            (*out_len)++;
4125
0
        }
4126
4127
0
        *(*buf + *out_len) = '\0';
4128
0
    }
4129
4130
0
    return 1;
4131
0
}
4132
4133
void
4134
_oid_finish_printing(const oid * objid, size_t objidlen,
4135
                     u_char ** buf, size_t * buf_len, size_t * out_len,
4136
919
                     int allow_realloc, int *buf_overflow) {
4137
919
    char            intbuf[64];
4138
919
    if (*buf != NULL && *(*buf + *out_len - 1) != '.') {
4139
0
        if (!*buf_overflow && !snmp_cstrcat(buf, buf_len, out_len,
4140
0
                                            allow_realloc, ".")) {
4141
0
            *buf_overflow = 1;
4142
0
        }
4143
0
    }
4144
4145
10.2k
    while (objidlen-- > 0) {    /* output rest of name, uninterpreted */
4146
9.28k
        sprintf(intbuf, "%" NETSNMP_PRIo "u.", *objid++);
4147
9.28k
        if (!*buf_overflow && !snmp_cstrcat(buf, buf_len, out_len,
4148
9.28k
                                            allow_realloc, intbuf)) {
4149
0
            *buf_overflow = 1;
4150
0
        }
4151
9.28k
    }
4152
4153
919
    if (*buf != NULL) {
4154
919
        *(*buf + *out_len - 1) = '\0';  /* remove trailing dot */
4155
919
        *out_len = *out_len - 1;
4156
919
    }
4157
919
}
4158
4159
#ifndef NETSNMP_DISABLE_MIB_LOADING
4160
static void
4161
_get_realloc_symbol_octet_string(size_t numids, const oid * objid,
4162
         u_char ** buf, size_t * buf_len,
4163
         size_t * out_len, int allow_realloc,
4164
         int *buf_overflow, struct tree* tp)
4165
0
{
4166
0
  netsnmp_variable_list var = { 0 };
4167
0
  u_char    buffer[1024];
4168
0
  size_t    i;
4169
4170
0
  for (i = 0; i < numids; i++)
4171
0
    buffer[i] = (u_char) objid[i];
4172
0
  var.type = ASN_OCTET_STR;
4173
0
  var.val.string = buffer;
4174
0
  var.val_len = numids;
4175
0
  if (!*buf_overflow) {
4176
0
    if (!sprint_realloc_octet_string(buf, buf_len, out_len,
4177
0
             allow_realloc, &var,
4178
0
             NULL, tp->hint,
4179
0
             NULL)) {
4180
0
      *buf_overflow = 1;
4181
0
    }
4182
0
  }
4183
0
}
4184
4185
static struct tree *
4186
_get_realloc_symbol(const oid * objid, size_t objidlen,
4187
                    struct tree *subtree,
4188
                    u_char ** buf, size_t * buf_len, size_t * out_len,
4189
                    int allow_realloc, int *buf_overflow,
4190
                    struct index_list *in_dices, size_t * end_of_known)
4191
1.87k
{
4192
1.87k
    struct tree    *return_tree = NULL;
4193
1.87k
    int             extended_index =
4194
1.87k
        netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_EXTENDED_INDEX);
4195
1.87k
    int             output_format =
4196
1.87k
        netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
4197
1.87k
    char            intbuf[64];
4198
1.87k
    struct tree    *orgtree = subtree;
4199
4200
1.87k
    if (!objid || !buf) {
4201
0
        return NULL;
4202
0
    }
4203
4204
3.46k
    for (; subtree; subtree = subtree->next_peer) {
4205
2.54k
        if (*objid == subtree->subid) {
4206
954
      while (subtree->next_peer && subtree->next_peer->subid == *objid)
4207
0
    subtree = subtree->next_peer;
4208
954
            if (subtree->indexes) {
4209
0
                in_dices = subtree->indexes;
4210
954
            } else if (subtree->augments) {
4211
0
                struct tree    *tp2 =
4212
0
                    find_tree_node(subtree->augments, -1);
4213
0
                if (tp2) {
4214
0
                    in_dices = tp2->indexes;
4215
0
                }
4216
0
            }
4217
4218
954
            if (!strncmp(subtree->label, ANON, ANON_LEN) ||
4219
954
                (NETSNMP_OID_OUTPUT_NUMERIC == output_format)) {
4220
0
                sprintf(intbuf, "%lu", subtree->subid);
4221
0
                if (!*buf_overflow && !snmp_cstrcat(buf, buf_len, out_len,
4222
0
                                                    allow_realloc, intbuf)) {
4223
0
                    *buf_overflow = 1;
4224
0
                }
4225
954
            } else {
4226
954
                if (!*buf_overflow &&
4227
954
                    !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4228
954
                                  subtree->label)) {
4229
0
                    *buf_overflow = 1;
4230
0
                }
4231
954
                if (output_format == NETSNMP_OID_OUTPUT_FULL_AND_NUMERIC) {
4232
0
                    snprintf(intbuf, sizeof intbuf, "(%lu)", subtree->subid);
4233
0
                    if (!*buf_overflow &&
4234
0
                        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4235
0
                                      intbuf)) {
4236
0
                        *buf_overflow = 1;
4237
0
                    }
4238
0
                }
4239
954
            }
4240
4241
954
            if (objidlen > 1) {
4242
661
                if (!*buf_overflow &&
4243
661
                    !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, ".")) {
4244
0
                    *buf_overflow = 1;
4245
0
                }
4246
4247
661
                return_tree = _get_realloc_symbol(objid + 1, objidlen - 1,
4248
661
                                                  subtree->child_list,
4249
661
                                                  buf, buf_len, out_len,
4250
661
                                                  allow_realloc,
4251
661
                                                  buf_overflow, in_dices,
4252
661
                                                  end_of_known);
4253
661
            }
4254
4255
954
            if (return_tree != NULL) {
4256
0
                return return_tree;
4257
954
            } else {
4258
954
                return subtree;
4259
954
            }
4260
954
        }
4261
2.54k
    }
4262
4263
919
    if (end_of_known) {
4264
919
        *end_of_known = *out_len;
4265
919
    }
4266
4267
    /*
4268
     * Subtree not found.  
4269
     */
4270
4271
919
    if (orgtree && in_dices && objidlen > 0) {
4272
0
  sprintf(intbuf, "%" NETSNMP_PRIo "u.", *objid);
4273
0
  if (!*buf_overflow
4274
0
      && !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf)) {
4275
0
      *buf_overflow = 1;
4276
0
  }
4277
0
  objid++;
4278
0
  objidlen--;
4279
0
    }
4280
4281
919
    while (in_dices && (objidlen > 0) &&
4282
919
           (NETSNMP_OID_OUTPUT_NUMERIC != output_format) &&
4283
919
           !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_BREAKDOWN_OIDS)) {
4284
0
        size_t          numids;
4285
0
        struct tree    *tp;
4286
4287
0
        tp = find_tree_node(in_dices->ilabel, -1);
4288
4289
0
        if (!tp) {
4290
            /*
4291
             * Can't find an index in the mib tree.  Bail.  
4292
             */
4293
0
            goto finish_it;
4294
0
        }
4295
4296
0
        if (extended_index) {
4297
0
            if (*buf != NULL && *(*buf + *out_len - 1) == '.') {
4298
0
                (*out_len)--;
4299
0
            }
4300
0
            if (!*buf_overflow &&
4301
0
                !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "[")) {
4302
0
                *buf_overflow = 1;
4303
0
            }
4304
0
        }
4305
4306
0
        switch (tp->type) {
4307
0
        case TYPE_OCTETSTR:
4308
0
            if (extended_index && tp->hint) {
4309
0
                if (in_dices->isimplied) {
4310
0
                    numids = objidlen;
4311
0
                    if (numids > objidlen)
4312
0
                        goto finish_it;
4313
0
                } else if (tp->ranges && !tp->ranges->next
4314
0
                           && tp->ranges->low == tp->ranges->high) {
4315
0
                    numids = tp->ranges->low;
4316
0
                    if (numids > objidlen)
4317
0
                        goto finish_it;
4318
0
                } else {
4319
0
                    numids = *objid;
4320
0
                    if (numids >= objidlen)
4321
0
                        goto finish_it;
4322
0
                    objid++;
4323
0
                    objidlen--;
4324
0
                }
4325
0
                if (numids > objidlen)
4326
0
                    goto finish_it;
4327
0
    _get_realloc_symbol_octet_string(numids, objid, buf, buf_len,
4328
0
             out_len, allow_realloc,
4329
0
             buf_overflow, tp);
4330
0
            } else if (in_dices->isimplied) {
4331
0
                numids = objidlen;
4332
0
                if (numids > objidlen)
4333
0
                    goto finish_it;
4334
4335
0
                if (!*buf_overflow) {
4336
0
                    if (!dump_realloc_oid_to_string
4337
0
                        (objid, numids, buf, buf_len, out_len,
4338
0
                         allow_realloc, '\'')) {
4339
0
                        *buf_overflow = 1;
4340
0
                    }
4341
0
                }
4342
0
            } else if (tp->ranges && !tp->ranges->next
4343
0
                       && tp->ranges->low == tp->ranges->high) {
4344
                /*
4345
                 * a fixed-length octet string 
4346
                 */
4347
0
                numids = tp->ranges->low;
4348
0
                if (numids > objidlen)
4349
0
                    goto finish_it;
4350
4351
0
                if (!*buf_overflow) {
4352
0
                    if (!dump_realloc_oid_to_string
4353
0
                        (objid, numids, buf, buf_len, out_len,
4354
0
                         allow_realloc, '\'')) {
4355
0
                        *buf_overflow = 1;
4356
0
                    }
4357
0
                }
4358
0
            } else {
4359
0
                numids = (size_t) * objid + 1;
4360
0
                if (numids > objidlen)
4361
0
                    goto finish_it;
4362
0
                if (numids == 1) {
4363
0
                    if (netsnmp_ds_get_boolean
4364
0
                        (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) {
4365
0
                        if (!*buf_overflow &&
4366
0
                            !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4367
0
                                          "\\")) {
4368
0
                            *buf_overflow = 1;
4369
0
                        }
4370
0
                    }
4371
0
                    if (!*buf_overflow &&
4372
0
                        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4373
0
                                      "\"")) {
4374
0
                        *buf_overflow = 1;
4375
0
                    }
4376
0
                    if (netsnmp_ds_get_boolean
4377
0
                        (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_ESCAPE_QUOTES)) {
4378
0
                        if (!*buf_overflow &&
4379
0
                            !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4380
0
                                          "\\")) {
4381
0
                            *buf_overflow = 1;
4382
0
                        }
4383
0
                    }
4384
0
                    if (!*buf_overflow &&
4385
0
                        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4386
0
                                      "\"")) {
4387
0
                        *buf_overflow = 1;
4388
0
                    }
4389
0
                } else {
4390
0
                    if (!*buf_overflow) {
4391
0
                        struct tree * next_peer;
4392
0
                        int normal_handling = 1;
4393
4394
0
                        if (tp->next_peer) {
4395
0
                            next_peer = tp->next_peer;
4396
0
                        }
4397
4398
                        /* Try handling the InetAddress in the OID, in case of failure,
4399
                         * use the normal_handling. 
4400
                         */
4401
0
                        if (tp->next_peer &&
4402
0
                            tp->tc_index != -1 &&
4403
0
                            next_peer->tc_index != -1 &&
4404
0
                            strcmp(get_tc_descriptor(tp->tc_index), "InetAddress") == 0 &&
4405
0
                            strcmp(get_tc_descriptor(next_peer->tc_index), 
4406
0
                                    "InetAddressType") == 0 ) {
4407
4408
0
                            int ret;
4409
0
                            int addr_type = *(objid - 1);
4410
4411
0
                            ret = dump_realloc_oid_to_inetaddress(addr_type, 
4412
0
                                        objid + 1, numids - 1, buf, buf_len, out_len,
4413
0
                                        allow_realloc, '"');
4414
0
                            if (ret != 2) {
4415
0
                                normal_handling = 0;
4416
0
                                if (ret == 0) {
4417
0
                                    *buf_overflow = 1;
4418
0
                                }
4419
4420
0
                            }
4421
0
                        } 
4422
0
                        if (normal_handling && !dump_realloc_oid_to_string
4423
0
                            (objid + 1, numids - 1, buf, buf_len, out_len,
4424
0
                             allow_realloc, '"')) {
4425
0
                            *buf_overflow = 1;
4426
0
                        }
4427
0
                    }
4428
0
                }
4429
0
            }
4430
0
            objid += numids;
4431
0
            objidlen -= numids;
4432
0
            break;
4433
4434
0
        case TYPE_INTEGER32:
4435
0
        case TYPE_UINTEGER:
4436
0
        case TYPE_UNSIGNED32:
4437
0
        case TYPE_GAUGE:
4438
0
        case TYPE_INTEGER:
4439
0
            if (tp->enums) {
4440
0
                struct enum_list *ep = tp->enums;
4441
0
                while (ep && ep->value != (int) (*objid)) {
4442
0
                    ep = ep->next;
4443
0
                }
4444
0
                if (ep) {
4445
0
                    if (!*buf_overflow &&
4446
0
                        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4447
0
                                      ep->label)) {
4448
0
                        *buf_overflow = 1;
4449
0
                    }
4450
0
                } else {
4451
0
                    sprintf(intbuf, "%" NETSNMP_PRIo "u", *objid);
4452
0
                    if (!*buf_overflow &&
4453
0
                        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4454
0
                                      intbuf)) {
4455
0
                        *buf_overflow = 1;
4456
0
                    }
4457
0
                }
4458
0
            } else {
4459
0
                sprintf(intbuf, "%" NETSNMP_PRIo "u", *objid);
4460
0
                if (!*buf_overflow &&
4461
0
                    !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4462
0
                                  intbuf)) {
4463
0
                    *buf_overflow = 1;
4464
0
                }
4465
0
            }
4466
0
            objid++;
4467
0
            objidlen--;
4468
0
            break;
4469
4470
0
        case TYPE_TIMETICKS:
4471
            /* In an index, this is probably a timefilter */
4472
0
            if (extended_index) {
4473
0
                uptimeString( *objid, intbuf, sizeof( intbuf ) );
4474
0
            } else {
4475
0
                sprintf(intbuf, "%" NETSNMP_PRIo "u", *objid);
4476
0
            }   
4477
0
            if (!*buf_overflow &&
4478
0
                !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf)) {
4479
0
                *buf_overflow = 1;
4480
0
            }
4481
0
            objid++;
4482
0
            objidlen--;
4483
0
            break;
4484
4485
0
        case TYPE_OBJID:
4486
0
            if (in_dices->isimplied) {
4487
0
                numids = objidlen;
4488
0
            } else {
4489
0
                numids = (size_t) * objid + 1;
4490
0
            }
4491
0
            if (numids > objidlen)
4492
0
                goto finish_it;
4493
0
            if (extended_index) {
4494
0
                if (in_dices->isimplied) {
4495
0
                    if (!*buf_overflow
4496
0
                        && !netsnmp_sprint_realloc_objid_tree(buf, buf_len,
4497
0
                                                              out_len,
4498
0
                                                              allow_realloc,
4499
0
                                                              buf_overflow,
4500
0
                                                              objid,
4501
0
                                                              numids)) {
4502
0
                        *buf_overflow = 1;
4503
0
                    }
4504
0
                } else {
4505
0
                    if (!*buf_overflow
4506
0
                        && !netsnmp_sprint_realloc_objid_tree(buf, buf_len,
4507
0
                                                              out_len,
4508
0
                                                              allow_realloc,
4509
0
                                                              buf_overflow,
4510
0
                                                              objid + 1,
4511
0
                                                              numids -
4512
0
                                                              1)) {
4513
0
                        *buf_overflow = 1;
4514
0
                    }
4515
0
                }
4516
0
            } else {
4517
0
                _get_realloc_symbol(objid, numids, NULL, buf, buf_len,
4518
0
                                    out_len, allow_realloc, buf_overflow,
4519
0
                                    NULL, NULL);
4520
0
            }
4521
0
            objid += (numids);
4522
0
            objidlen -= (numids);
4523
0
            break;
4524
4525
0
        case TYPE_IPADDR:
4526
0
            if (objidlen < 4)
4527
0
                goto finish_it;
4528
0
            sprintf(intbuf, "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u."
4529
0
                    "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u",
4530
0
                    objid[0], objid[1], objid[2], objid[3]);
4531
0
            objid += 4;
4532
0
            objidlen -= 4;
4533
0
            if (!*buf_overflow &&
4534
0
                !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, intbuf)) {
4535
0
                *buf_overflow = 1;
4536
0
            }
4537
0
            break;
4538
4539
0
        case TYPE_NETADDR:{
4540
0
                oid             ntype = *objid++;
4541
4542
0
                objidlen--;
4543
0
                sprintf(intbuf, "%" NETSNMP_PRIo "u.", ntype);
4544
0
                if (!*buf_overflow &&
4545
0
                    !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4546
0
                                  intbuf)) {
4547
0
                    *buf_overflow = 1;
4548
0
                }
4549
4550
0
                if (ntype == 1 && objidlen >= 4) {
4551
0
                    sprintf(intbuf, "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u."
4552
0
                            "%" NETSNMP_PRIo "u.%" NETSNMP_PRIo "u",
4553
0
                            objid[0], objid[1], objid[2], objid[3]);
4554
0
                    if (!*buf_overflow &&
4555
0
                        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc,
4556
0
                                      intbuf)) {
4557
0
                        *buf_overflow = 1;
4558
0
                    }
4559
0
                    objid += 4;
4560
0
                    objidlen -= 4;
4561
0
                } else {
4562
0
                    goto finish_it;
4563
0
                }
4564
0
            }
4565
0
            break;
4566
4567
0
        case TYPE_NSAPADDRESS:
4568
0
        default:
4569
0
            goto finish_it;
4570
0
            break;
4571
0
        }
4572
4573
0
        if (extended_index) {
4574
0
            if (!*buf_overflow &&
4575
0
                !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "]")) {
4576
0
                *buf_overflow = 1;
4577
0
            }
4578
0
        } else {
4579
0
            if (!*buf_overflow &&
4580
0
                !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, ".")) {
4581
0
                *buf_overflow = 1;
4582
0
            }
4583
0
        }
4584
0
        in_dices = in_dices->next;
4585
0
    }
4586
4587
919
  finish_it:
4588
919
    _oid_finish_printing(objid, objidlen,
4589
919
                         buf, buf_len, out_len,
4590
919
                         allow_realloc, buf_overflow);
4591
919
    return NULL;
4592
919
}
4593
4594
struct tree    *
4595
get_tree(const oid * objid, size_t objidlen, struct tree *subtree)
4596
8.71k
{
4597
8.71k
    struct tree    *return_tree = NULL;
4598
4599
17.3k
    for (; subtree; subtree = subtree->next_peer) {
4600
12.9k
        if (*objid == subtree->subid)
4601
4.30k
            goto found;
4602
12.9k
    }
4603
4604
4.40k
    return NULL;
4605
4606
4.30k
  found:
4607
4.30k
    while (subtree->next_peer && subtree->next_peer->subid == *objid)
4608
0
  subtree = subtree->next_peer;
4609
4.30k
    if (objidlen > 1)
4610
4.30k
        return_tree =
4611
4.30k
            get_tree(objid + 1, objidlen - 1, subtree->child_list);
4612
4.30k
    if (return_tree != NULL)
4613
0
        return return_tree;
4614
4.30k
    else
4615
4.30k
        return subtree;
4616
4.30k
}
4617
4618
/**
4619
 * Prints on oid description on stdout.
4620
 *
4621
 * @see fprint_description
4622
 */
4623
void
4624
print_description(oid * objid, size_t objidlen, /* number of subidentifiers */
4625
                  int width)
4626
0
{
4627
0
    fprint_description(stdout, objid, objidlen, width);
4628
0
}
4629
4630
4631
/**
4632
 * Prints on oid description into a file descriptor.
4633
 * 
4634
 * @param f         The file descriptor to print to.
4635
 * @param objid     The object identifier.
4636
 * @param objidlen  The object id length.
4637
 * @param width     Number of subidentifiers.
4638
 */
4639
void
4640
fprint_description(FILE * f, oid * objid, size_t objidlen,
4641
                   int width)
4642
0
{
4643
0
    u_char         *buf = NULL;
4644
0
    size_t          buf_len = 256, out_len = 0;
4645
4646
0
    if ((buf = calloc(buf_len, 1)) == NULL) {
4647
0
        fprintf(f, "[TRUNCATED]\n");
4648
0
        return;
4649
0
    } else {
4650
0
        if (!sprint_realloc_description(&buf, &buf_len, &out_len, 1,
4651
0
                                   objid, objidlen, width)) {
4652
0
            fprintf(f, "%s [TRUNCATED]\n", buf);
4653
0
        } else {
4654
0
            fprintf(f, "%s\n", buf);
4655
0
        }
4656
0
    }
4657
4658
0
    SNMP_FREE(buf);
4659
0
}
4660
4661
#ifndef NETSNMP_FEATURE_REMOVE_MIB_SNPRINT_DESCRIPTION
4662
int
4663
snprint_description(char *buf, size_t buf_len,
4664
                    oid * objid, size_t objidlen, int width)
4665
0
{
4666
0
    size_t          out_len = 0;
4667
4668
0
    if (sprint_realloc_description((u_char **) & buf, &buf_len, &out_len, 0,
4669
0
                                    objid, objidlen, width)) {
4670
0
        return (int) out_len;
4671
0
    } else {
4672
0
        return -1;
4673
0
    }
4674
0
}
4675
#endif /* NETSNMP_FEATURE_REMOVE_MIB_SNPRINT_DESCRIPTION */
4676
4677
int
4678
sprint_realloc_description(u_char ** buf, size_t * buf_len,
4679
                     size_t * out_len, int allow_realloc,
4680
                     oid * objid, size_t objidlen, int width)
4681
0
{
4682
0
    struct tree    *tp = get_tree(objid, objidlen, tree_head);
4683
0
    struct tree    *subtree = tree_head;
4684
0
    int             pos, len;
4685
0
    char            tmpbuf[128];
4686
0
    const char     *cp;
4687
4688
0
    if (NULL == tp)
4689
0
        return 0;
4690
4691
0
    if (tp->type <= TYPE_SIMPLE_LAST)
4692
0
        cp = " OBJECT-TYPE";
4693
0
    else
4694
0
        switch (tp->type) {
4695
0
        case TYPE_TRAPTYPE:
4696
0
            cp = " TRAP-TYPE";
4697
0
            break;
4698
0
        case TYPE_NOTIFTYPE:
4699
0
            cp = " NOTIFICATION-TYPE";
4700
0
            break;
4701
0
        case TYPE_OBJGROUP:
4702
0
            cp = " OBJECT-GROUP";
4703
0
            break;
4704
0
        case TYPE_AGENTCAP:
4705
0
            cp = " AGENT-CAPABILITIES";
4706
0
            break;
4707
0
        case TYPE_MODID:
4708
0
            cp = " MODULE-IDENTITY";
4709
0
            break;
4710
0
        case TYPE_OBJIDENTITY:
4711
0
            cp = " OBJECT-IDENTITY";
4712
0
            break;
4713
0
        case TYPE_MODCOMP:
4714
0
            cp = " MODULE-COMPLIANCE";
4715
0
            break;
4716
0
        default:
4717
0
            sprintf(tmpbuf, " type_%d", tp->type);
4718
0
            cp = tmpbuf;
4719
0
        }
4720
4721
0
    if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tp->label) ||
4722
0
        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, cp) ||
4723
0
        !snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n")) {
4724
0
        return 0;
4725
0
    }
4726
0
    if (!print_tree_node(buf, buf_len, out_len, allow_realloc, tp, width))
4727
0
        return 0;
4728
0
    if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "::= {"))
4729
0
        return 0;
4730
0
    pos = 5;
4731
0
    while (objidlen > 1) {
4732
0
        for (; subtree; subtree = subtree->next_peer) {
4733
0
            if (*objid == subtree->subid) {
4734
0
                while (subtree->next_peer && subtree->next_peer->subid == *objid)
4735
0
                    subtree = subtree->next_peer;
4736
0
                if (strncmp(subtree->label, ANON, ANON_LEN)) {
4737
0
                    snprintf(tmpbuf, sizeof(tmpbuf), " %s(%lu)", subtree->label, subtree->subid);
4738
0
                    tmpbuf[ sizeof(tmpbuf)-1 ] = 0;
4739
0
                } else
4740
0
                    sprintf(tmpbuf, " %lu", subtree->subid);
4741
0
                len = strlen(tmpbuf);
4742
0
                if (pos + len + 2 > width) {
4743
0
                    if (!snmp_cstrcat(buf, buf_len, out_len,
4744
0
                                     allow_realloc, "\n     "))
4745
0
                        return 0;
4746
0
                    pos = 5;
4747
0
                }
4748
0
                if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmpbuf))
4749
0
                    return 0;
4750
0
                pos += len;
4751
0
                objid++;
4752
0
                objidlen--;
4753
0
                break;
4754
0
            }
4755
0
        }
4756
0
        if (subtree)
4757
0
            subtree = subtree->child_list;
4758
0
        else
4759
0
            break;
4760
0
    }
4761
0
    while (objidlen > 1) {
4762
0
        sprintf(tmpbuf, " %" NETSNMP_PRIo "u", *objid);
4763
0
        len = strlen(tmpbuf);
4764
0
        if (pos + len + 2 > width) {
4765
0
            if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n     "))
4766
0
                return 0;
4767
0
            pos = 5;
4768
0
        }
4769
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmpbuf))
4770
0
            return 0;
4771
0
        pos += len;
4772
0
        objid++;
4773
0
        objidlen--;
4774
0
    }
4775
0
    sprintf(tmpbuf, " %" NETSNMP_PRIo "u }", *objid);
4776
0
    len = strlen(tmpbuf);
4777
0
    if (pos + len + 2 > width) {
4778
0
        if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, "\n     "))
4779
0
            return 0;
4780
0
        pos = 5;
4781
0
    }
4782
0
    if (!snmp_cstrcat(buf, buf_len, out_len, allow_realloc, tmpbuf))
4783
0
        return 0;
4784
0
    return 1;
4785
0
}
4786