Coverage Report

Created: 2026-12-01 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/net-snmp/snmplib/parse.c
Line
Count
Source
1
/*
2
 * parse.c
3
 *
4
 */
5
/* Portions of this file are subject to the following copyrights.  See
6
 * the Net-SNMP's COPYING file for more details and other copyrights
7
 * that may apply:
8
 */
9
/******************************************************************
10
        Copyright 1989, 1991, 1992 by Carnegie Mellon University
11
12
                      All Rights Reserved
13
14
Permission to use, copy, modify, and distribute this software and its
15
documentation for any purpose and without fee is hereby granted,
16
provided that the above copyright notice appear in all copies and that
17
both that copyright notice and this permission notice appear in
18
supporting documentation, and that the name of CMU not be
19
used in advertising or publicity pertaining to distribution of the
20
software without specific, written prior permission.
21
22
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
23
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
24
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
25
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
26
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
27
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28
SOFTWARE.
29
******************************************************************/
30
/*
31
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
32
 * Use is subject to license terms specified in the COPYING file
33
 * distributed with the Net-SNMP package.
34
 */
35
#include <net-snmp/net-snmp-config.h>
36
#include <net-snmp/net-snmp-features.h>
37
38
#ifndef NETSNMP_DISABLE_MIB_LOADING
39
40
#ifdef HAVE_LIMITS_H
41
#include <limits.h>
42
#endif
43
#include <stdio.h>
44
#ifdef HAVE_STDLIB_H
45
#include <stdlib.h>
46
#endif
47
#ifdef HAVE_STRING_H
48
#include <string.h>
49
#else
50
#include <strings.h>
51
#endif
52
#include <ctype.h>
53
#include <sys/types.h>
54
#ifdef HAVE_SYS_STAT_H
55
#include <sys/stat.h>
56
#endif
57
58
#ifdef HAVE_DIRENT_H
59
#include <dirent.h>
60
#endif
61
#ifdef TIME_WITH_SYS_TIME
62
# include <sys/time.h>
63
# include <time.h>
64
#else
65
# ifdef HAVE_SYS_TIME_H
66
#  include <sys/time.h>
67
# else
68
#  include <time.h>
69
# endif
70
#endif
71
#ifdef HAVE_NETINET_IN_H
72
#include <netinet/in.h>
73
#endif
74
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
75
#include <regex.h>
76
#endif
77
#ifdef HAVE_UNISTD_H
78
#include <unistd.h>
79
#endif
80
81
#include <errno.h>
82
83
#include <net-snmp/types.h>
84
#include <net-snmp/output_api.h>
85
#include <net-snmp/config_api.h>
86
#include <net-snmp/utilities.h>
87
88
#include <net-snmp/library/parse.h>
89
#include <net-snmp/library/mib.h>
90
#include <net-snmp/library/snmp_api.h>
91
#include <net-snmp/library/tools.h>
92
93
netsnmp_feature_child_of(find_module, mib_api);
94
netsnmp_feature_child_of(get_tc_description, mib_api);
95
96
/*
97
 * A linked list of nodes.
98
 */
99
struct node {
100
    struct node    *next;
101
    char           *label;  /* This node's (unique) textual name */
102
    u_long          subid;  /* This node's integer subidentifier */
103
    int             modid;  /* The module containing this node */
104
    char           *parent; /* The parent's textual name */
105
    int             tc_index; /* index into tclist (-1 if NA) */
106
    int             type;   /* The type of object this represents */
107
    int             access;
108
    int             status;
109
    struct enum_list *enums; /* (optional) list of enumerated integers */
110
    struct range_list *ranges;
111
    struct index_list *indexes;
112
    char           *augments;
113
    struct varbind_list *varbinds;
114
    char           *hint;
115
    char           *units;
116
    char           *description; /* description (a quoted string) */
117
    char           *reference; /* references (a quoted string) */
118
    char           *defaultValue;
119
    char           *filename;
120
    int             lineno;
121
};
122
123
/*
124
 * This is one element of an object identifier with either an integer
125
 * subidentifier, or a textual string label, or both.
126
 * The subid is -1 if not present, and label is NULL if not present.
127
 */
128
struct subid_s {
129
    int             subid;
130
    int             modid;
131
    char           *label;
132
};
133
134
2
#define TC_INCR 100
135
struct tc {                     /* textual conventions */
136
    int             type;
137
    int             modid;
138
    char           *descriptor;
139
    char           *hint;
140
    struct enum_list *enums;
141
    struct range_list *ranges;
142
    char           *description;
143
    int             lineno;
144
} *tclist;
145
int tc_alloc;
146
147
static int      mibLine;
148
static const char *File = "(none)";
149
static int      anonymous;
150
151
struct objgroup {
152
    char           *name;
153
    int             line;
154
    struct objgroup *next;
155
}              *objgroups = NULL, *objects = NULL, *notifs = NULL;
156
157
32
#define SYNTAX_MASK     0x80
158
/*
159
 * types of tokens
160
 * Tokens with the SYNTAX_MASK bit set are syntax tokens 
161
 */
162
273
#define CONTINUE    -1
163
0
#define ENDOFFILE   0
164
0
#define LABEL       1
165
#define SUBTREE     2
166
0
#define SYNTAX      3
167
2
#define OBJID       (4 | SYNTAX_MASK)
168
2
#define OCTETSTR    (5 | SYNTAX_MASK)
169
2
#define INTEGER     (6 | SYNTAX_MASK)
170
2
#define NETADDR     (7 | SYNTAX_MASK)
171
2
#define IPADDR      (8 | SYNTAX_MASK)
172
2
#define COUNTER     (9 | SYNTAX_MASK)
173
2
#define GAUGE       (10 | SYNTAX_MASK)
174
2
#define TIMETICKS   (11 | SYNTAX_MASK)
175
2
#define KW_OPAQUE   (12 | SYNTAX_MASK)
176
2
#define NUL         (13 | SYNTAX_MASK)
177
0
#define SEQUENCE    14
178
0
#define OF          15          /* SEQUENCE OF */
179
0
#define OBJTYPE     16
180
0
#define ACCESS      17
181
0
#define READONLY    18
182
0
#define READWRITE   19
183
0
#define WRITEONLY   20
184
#ifdef NOACCESS
185
#undef NOACCESS                 /* agent 'NOACCESS' token */
186
#endif
187
0
#define NOACCESS    21
188
0
#define STATUS      22
189
0
#define MANDATORY   23
190
0
#define KW_OPTIONAL    24
191
0
#define OBSOLETE    25
192
/*
193
 * #define RECOMMENDED 26 
194
 */
195
#define PUNCT       27
196
0
#define EQUALS      28
197
0
#define NUMBER      29
198
0
#define LEFTBRACKET 30
199
0
#define RIGHTBRACKET 31
200
0
#define LEFTPAREN   32
201
0
#define RIGHTPAREN  33
202
0
#define COMMA       34
203
0
#define DESCRIPTION 35
204
0
#define QUOTESTRING 36
205
0
#define INDEX       37
206
0
#define DEFVAL      38
207
0
#define DEPRECATED  39
208
0
#define SIZE        40
209
2
#define BITSTRING   (41 | SYNTAX_MASK)
210
2
#define NSAPADDRESS (42 | SYNTAX_MASK)
211
2
#define COUNTER64   (43 | SYNTAX_MASK)
212
2
#define OBJGROUP    44
213
2
#define NOTIFTYPE   45
214
0
#define AUGMENTS    46
215
2
#define COMPLIANCE  47
216
0
#define READCREATE  48
217
0
#define UNITS       49
218
0
#define REFERENCE   50
219
0
#define NUM_ENTRIES 51
220
2
#define MODULEIDENTITY 52
221
0
#define LASTUPDATED 53
222
0
#define ORGANIZATION 54
223
0
#define CONTACTINFO 55
224
2
#define UINTEGER32 (56 | SYNTAX_MASK)
225
0
#define CURRENT     57
226
0
#define DEFINITIONS 58
227
0
#define END         59
228
0
#define SEMI        60
229
2
#define TRAPTYPE    61
230
0
#define ENTERPRISE  62
231
/*
232
 * #define DISPLAYSTR (63 | SYNTAX_MASK) 
233
 */
234
0
#define BEGIN       64
235
0
#define IMPORTS     65
236
0
#define EXPORTS     66
237
0
#define ACCNOTIFY   67
238
0
#define BAR         68
239
0
#define RANGE       69
240
0
#define CONVENTION  70
241
0
#define DISPLAYHINT 71
242
0
#define FROM        72
243
2
#define AGENTCAP    73
244
0
#define MACRO       74
245
0
#define IMPLIED     75
246
0
#define SUPPORTS    76
247
0
#define INCLUDES    77
248
0
#define VARIATION   78
249
0
#define REVISION    79
250
0
#define NOTIMPL     80
251
0
#define OBJECTS     81
252
0
#define NOTIFICATIONS 82
253
0
#define MODULE      83
254
0
#define MINACCESS   84
255
0
#define PRODREL     85
256
0
#define WRSYNTAX    86
257
0
#define CREATEREQ   87
258
2
#define NOTIFGROUP  88
259
0
#define MANDATORYGROUPS 89
260
0
#define GROUP     90
261
0
#define OBJECT      91
262
0
#define IDENTIFIER  92
263
0
#define CHOICE      93
264
0
#define LEFTSQBRACK 95
265
0
#define RIGHTSQBRACK  96
266
0
#define IMPLICIT    97
267
0
#define APPSYNTAX (98 | SYNTAX_MASK)
268
0
#define OBJSYNTAX (99 | SYNTAX_MASK)
269
0
#define SIMPLESYNTAX  (100 | SYNTAX_MASK)
270
0
#define OBJNAME   (101 | SYNTAX_MASK)
271
0
#define NOTIFNAME (102 | SYNTAX_MASK)
272
0
#define VARIABLES 103
273
2
#define UNSIGNED32  (104 | SYNTAX_MASK)
274
2
#define INTEGER32 (105 | SYNTAX_MASK)
275
2
#define OBJIDENTITY 106
276
/*
277
 * Beware of reaching SYNTAX_MASK (0x80) 
278
 */
279
280
struct tok {
281
    const char     *name;       /* token name */
282
    int             len;        /* length not counting nul */
283
    int             token;      /* value */
284
    int             hash;       /* hash of name */
285
    struct tok     *next;       /* pointer to next in hash table */
286
};
287
288
289
static struct tok tokens[] = {
290
    {"obsolete", sizeof("obsolete") - 1, OBSOLETE}
291
    ,
292
    {"Opaque", sizeof("Opaque") - 1, KW_OPAQUE}
293
    ,
294
    {"optional", sizeof("optional") - 1, KW_OPTIONAL}
295
    ,
296
    {"LAST-UPDATED", sizeof("LAST-UPDATED") - 1, LASTUPDATED}
297
    ,
298
    {"ORGANIZATION", sizeof("ORGANIZATION") - 1, ORGANIZATION}
299
    ,
300
    {"CONTACT-INFO", sizeof("CONTACT-INFO") - 1, CONTACTINFO}
301
    ,
302
    {"MODULE-IDENTITY", sizeof("MODULE-IDENTITY") - 1, MODULEIDENTITY}
303
    ,
304
    {"MODULE-COMPLIANCE", sizeof("MODULE-COMPLIANCE") - 1, COMPLIANCE}
305
    ,
306
    {"DEFINITIONS", sizeof("DEFINITIONS") - 1, DEFINITIONS}
307
    ,
308
    {"END", sizeof("END") - 1, END}
309
    ,
310
    {"AUGMENTS", sizeof("AUGMENTS") - 1, AUGMENTS}
311
    ,
312
    {"not-accessible", sizeof("not-accessible") - 1, NOACCESS}
313
    ,
314
    {"write-only", sizeof("write-only") - 1, WRITEONLY}
315
    ,
316
    {"NsapAddress", sizeof("NsapAddress") - 1, NSAPADDRESS}
317
    ,
318
    {"UNITS", sizeof("Units") - 1, UNITS}
319
    ,
320
    {"REFERENCE", sizeof("REFERENCE") - 1, REFERENCE}
321
    ,
322
    {"NUM-ENTRIES", sizeof("NUM-ENTRIES") - 1, NUM_ENTRIES}
323
    ,
324
    {"BITSTRING", sizeof("BITSTRING") - 1, BITSTRING}
325
    ,
326
    {"BIT", sizeof("BIT") - 1, CONTINUE}
327
    ,
328
    {"BITS", sizeof("BITS") - 1, BITSTRING}
329
    ,
330
    {"Counter64", sizeof("Counter64") - 1, COUNTER64}
331
    ,
332
    {"TimeTicks", sizeof("TimeTicks") - 1, TIMETICKS}
333
    ,
334
    {"NOTIFICATION-TYPE", sizeof("NOTIFICATION-TYPE") - 1, NOTIFTYPE}
335
    ,
336
    {"OBJECT-GROUP", sizeof("OBJECT-GROUP") - 1, OBJGROUP}
337
    ,
338
    {"OBJECT-IDENTITY", sizeof("OBJECT-IDENTITY") - 1, OBJIDENTITY}
339
    ,
340
    {"IDENTIFIER", sizeof("IDENTIFIER") - 1, IDENTIFIER}
341
    ,
342
    {"OBJECT", sizeof("OBJECT") - 1, OBJECT}
343
    ,
344
    {"NetworkAddress", sizeof("NetworkAddress") - 1, NETADDR}
345
    ,
346
    {"Gauge", sizeof("Gauge") - 1, GAUGE}
347
    ,
348
    {"Gauge32", sizeof("Gauge32") - 1, GAUGE}
349
    ,
350
    {"Unsigned32", sizeof("Unsigned32") - 1, UNSIGNED32}
351
    ,
352
    {"read-write", sizeof("read-write") - 1, READWRITE}
353
    ,
354
    {"read-create", sizeof("read-create") - 1, READCREATE}
355
    ,
356
    {"OCTETSTRING", sizeof("OCTETSTRING") - 1, OCTETSTR}
357
    ,
358
    {"OCTET", sizeof("OCTET") - 1, CONTINUE}
359
    ,
360
    {"OF", sizeof("OF") - 1, OF}
361
    ,
362
    {"SEQUENCE", sizeof("SEQUENCE") - 1, SEQUENCE}
363
    ,
364
    {"NULL", sizeof("NULL") - 1, NUL}
365
    ,
366
    {"IpAddress", sizeof("IpAddress") - 1, IPADDR}
367
    ,
368
    {"UInteger32", sizeof("UInteger32") - 1, UINTEGER32}
369
    ,
370
    {"INTEGER", sizeof("INTEGER") - 1, INTEGER}
371
    ,
372
    {"Integer32", sizeof("Integer32") - 1, INTEGER32}
373
    ,
374
    {"Counter", sizeof("Counter") - 1, COUNTER}
375
    ,
376
    {"Counter32", sizeof("Counter32") - 1, COUNTER}
377
    ,
378
    {"read-only", sizeof("read-only") - 1, READONLY}
379
    ,
380
    {"DESCRIPTION", sizeof("DESCRIPTION") - 1, DESCRIPTION}
381
    ,
382
    {"INDEX", sizeof("INDEX") - 1, INDEX}
383
    ,
384
    {"DEFVAL", sizeof("DEFVAL") - 1, DEFVAL}
385
    ,
386
    {"deprecated", sizeof("deprecated") - 1, DEPRECATED}
387
    ,
388
    {"SIZE", sizeof("SIZE") - 1, SIZE}
389
    ,
390
    {"MAX-ACCESS", sizeof("MAX-ACCESS") - 1, ACCESS}
391
    ,
392
    {"ACCESS", sizeof("ACCESS") - 1, ACCESS}
393
    ,
394
    {"mandatory", sizeof("mandatory") - 1, MANDATORY}
395
    ,
396
    {"current", sizeof("current") - 1, CURRENT}
397
    ,
398
    {"STATUS", sizeof("STATUS") - 1, STATUS}
399
    ,
400
    {"SYNTAX", sizeof("SYNTAX") - 1, SYNTAX}
401
    ,
402
    {"OBJECT-TYPE", sizeof("OBJECT-TYPE") - 1, OBJTYPE}
403
    ,
404
    {"TRAP-TYPE", sizeof("TRAP-TYPE") - 1, TRAPTYPE}
405
    ,
406
    {"ENTERPRISE", sizeof("ENTERPRISE") - 1, ENTERPRISE}
407
    ,
408
    {"BEGIN", sizeof("BEGIN") - 1, BEGIN}
409
    ,
410
    {"IMPORTS", sizeof("IMPORTS") - 1, IMPORTS}
411
    ,
412
    {"EXPORTS", sizeof("EXPORTS") - 1, EXPORTS}
413
    ,
414
    {"accessible-for-notify", sizeof("accessible-for-notify") - 1,
415
     ACCNOTIFY}
416
    ,
417
    {"TEXTUAL-CONVENTION", sizeof("TEXTUAL-CONVENTION") - 1, CONVENTION}
418
    ,
419
    {"NOTIFICATION-GROUP", sizeof("NOTIFICATION-GROUP") - 1, NOTIFGROUP}
420
    ,
421
    {"DISPLAY-HINT", sizeof("DISPLAY-HINT") - 1, DISPLAYHINT}
422
    ,
423
    {"FROM", sizeof("FROM") - 1, FROM}
424
    ,
425
    {"AGENT-CAPABILITIES", sizeof("AGENT-CAPABILITIES") - 1, AGENTCAP}
426
    ,
427
    {"MACRO", sizeof("MACRO") - 1, MACRO}
428
    ,
429
    {"IMPLIED", sizeof("IMPLIED") - 1, IMPLIED}
430
    ,
431
    {"SUPPORTS", sizeof("SUPPORTS") - 1, SUPPORTS}
432
    ,
433
    {"INCLUDES", sizeof("INCLUDES") - 1, INCLUDES}
434
    ,
435
    {"VARIATION", sizeof("VARIATION") - 1, VARIATION}
436
    ,
437
    {"REVISION", sizeof("REVISION") - 1, REVISION}
438
    ,
439
    {"not-implemented", sizeof("not-implemented") - 1, NOTIMPL}
440
    ,
441
    {"OBJECTS", sizeof("OBJECTS") - 1, OBJECTS}
442
    ,
443
    {"NOTIFICATIONS", sizeof("NOTIFICATIONS") - 1, NOTIFICATIONS}
444
    ,
445
    {"MODULE", sizeof("MODULE") - 1, MODULE}
446
    ,
447
    {"MIN-ACCESS", sizeof("MIN-ACCESS") - 1, MINACCESS}
448
    ,
449
    {"PRODUCT-RELEASE", sizeof("PRODUCT-RELEASE") - 1, PRODREL}
450
    ,
451
    {"WRITE-SYNTAX", sizeof("WRITE-SYNTAX") - 1, WRSYNTAX}
452
    ,
453
    {"CREATION-REQUIRES", sizeof("CREATION-REQUIRES") - 1, CREATEREQ}
454
    ,
455
    {"MANDATORY-GROUPS", sizeof("MANDATORY-GROUPS") - 1, MANDATORYGROUPS}
456
    ,
457
    {"GROUP", sizeof("GROUP") - 1, GROUP}
458
    ,
459
    {"CHOICE", sizeof("CHOICE") - 1, CHOICE}
460
    ,
461
    {"IMPLICIT", sizeof("IMPLICIT") - 1, IMPLICIT}
462
    ,
463
    {"ObjectSyntax", sizeof("ObjectSyntax") - 1, OBJSYNTAX}
464
    ,
465
    {"SimpleSyntax", sizeof("SimpleSyntax") - 1, SIMPLESYNTAX}
466
    ,
467
    {"ApplicationSyntax", sizeof("ApplicationSyntax") - 1, APPSYNTAX}
468
    ,
469
    {"ObjectName", sizeof("ObjectName") - 1, OBJNAME}
470
    ,
471
    {"NotificationName", sizeof("NotificationName") - 1, NOTIFNAME}
472
    ,
473
    {"VARIABLES", sizeof("VARIABLES") - 1, VARIABLES}
474
    ,
475
    {NULL}
476
};
477
478
static struct module_compatability *module_map_head;
479
static struct module_compatability module_map[] = {
480
    {"RFC1065-SMI", "RFC1155-SMI", NULL, 0},
481
    {"RFC1066-MIB", "RFC1156-MIB", NULL, 0},
482
    /*
483
     * 'mib' -> 'mib-2' 
484
     */
485
    {"RFC1156-MIB", "RFC1158-MIB", NULL, 0},
486
    /*
487
     * 'snmpEnableAuthTraps' -> 'snmpEnableAuthenTraps' 
488
     */
489
    {"RFC1158-MIB", "RFC1213-MIB", NULL, 0},
490
    /*
491
     * 'nullOID' -> 'zeroDotZero' 
492
     */
493
    {"RFC1155-SMI", "SNMPv2-SMI", NULL, 0},
494
    {"RFC1213-MIB", "SNMPv2-SMI", "mib-2", 0},
495
    {"RFC1213-MIB", "SNMPv2-MIB", "sys", 3},
496
    {"RFC1213-MIB", "IF-MIB", "if", 2},
497
    {"RFC1213-MIB", "IP-MIB", "ip", 2},
498
    {"RFC1213-MIB", "IP-MIB", "icmp", 4},
499
    {"RFC1213-MIB", "TCP-MIB", "tcp", 3},
500
    {"RFC1213-MIB", "UDP-MIB", "udp", 3},
501
    {"RFC1213-MIB", "SNMPv2-SMI", "transmission", 0},
502
    {"RFC1213-MIB", "SNMPv2-MIB", "snmp", 4},
503
    {"RFC1231-MIB", "TOKENRING-MIB", NULL, 0},
504
    {"RFC1271-MIB", "RMON-MIB", NULL, 0},
505
    {"RFC1286-MIB", "SOURCE-ROUTING-MIB", "dot1dSr", 7},
506
    {"RFC1286-MIB", "BRIDGE-MIB", NULL, 0},
507
    {"RFC1315-MIB", "FRAME-RELAY-DTE-MIB", NULL, 0},
508
    {"RFC1316-MIB", "CHARACTER-MIB", NULL, 0},
509
    {"RFC1406-MIB", "DS1-MIB", NULL, 0},
510
    {"RFC-1213", "RFC1213-MIB", NULL, 0},
511
};
512
513
594
#define MODULE_NOT_FOUND  0
514
0
#define MODULE_LOADED_OK  1
515
0
#define MODULE_ALREADY_LOADED 2
516
/*
517
 * #define MODULE_LOAD_FAILED   3       
518
 */
519
0
#define MODULE_LOAD_FAILED  MODULE_NOT_FOUND
520
0
#define MODULE_SYNTAX_ERROR     4
521
522
int gMibError = 0,gLoop = 0;
523
static char *gpMibErrorString;
524
char gMibNames[STRINGMAX];
525
526
184
#define HASHSIZE        32
527
184
#define BUCKET(x)       (x & (HASHSIZE-1))
528
529
145
#define NHASHSIZE    128
530
145
#define NBUCKET(x)   (x & (NHASHSIZE-1))
531
532
static struct tok *buckets[HASHSIZE];
533
534
static struct node *nbuckets[NHASHSIZE];
535
static struct tree *tbuckets[NHASHSIZE];
536
static struct module *module_head = NULL;
537
538
static struct node *orphan_nodes = NULL;
539
NETSNMP_IMPORT struct tree *tree_head;
540
struct tree        *tree_head = NULL;
541
542
0
#define NUMBER_OF_ROOT_NODES  3
543
static struct module_import root_imports[NUMBER_OF_ROOT_NODES];
544
545
static int      current_module = 0;
546
static int      max_module = 0;
547
static int      first_err_module = 1;
548
static char    *last_err_module = NULL; /* no repeats on "Cannot find module..." */
549
550
static void     tree_from_node(struct tree *tp, struct node *np);
551
static void     do_subtree(struct tree *, struct node **);
552
static void     do_linkup(struct module *, struct node *);
553
static void     dump_module_list(void);
554
static int      get_token(FILE *, char *, int);
555
static int      parseQuoteString(FILE *, char *, int);
556
static int      tossObjectIdentifier(FILE *);
557
static int      name_hash(const char *);
558
static void     init_node_hash(struct node *);
559
static void     print_error(const char *, const char *, int);
560
static void     free_tree(struct tree *);
561
static void     free_partial_tree(struct tree *, int);
562
static void     free_node(struct node *);
563
static void     build_translation_table(void);
564
static void     init_tree_roots(void);
565
static void     merge_anon_children(struct tree *, struct tree *);
566
static void     unlink_tbucket(struct tree *);
567
static void     unlink_tree(struct tree *);
568
struct child_index;
569
static void     child_index_modified(const struct tree *,
570
                                     const struct child_index *);
571
static struct node *parse_objectid(FILE *, char *);
572
static int      get_tc(const char *, int, int *, struct enum_list **,
573
                       struct range_list **, char **);
574
static int      get_tc_index(const char *, int);
575
static struct enum_list *parse_enumlist(FILE *, struct enum_list **);
576
static struct range_list *parse_ranges(FILE * fp, struct range_list **);
577
static struct node *parse_asntype(FILE *, char *, int *, char *);
578
static struct node *parse_objecttype(FILE *, char *);
579
static struct node *parse_objectgroup(FILE *, char *, int,
580
                                      struct objgroup **);
581
static struct node *parse_notificationDefinition(FILE *, char *);
582
static struct node *parse_trapDefinition(FILE *, char *);
583
static struct node *parse_compliance(FILE *, char *);
584
static struct node *parse_capabilities(FILE *, char *);
585
static struct node *parse_moduleIdentity(FILE *, char *);
586
static struct node *parse_macro(FILE *, char *);
587
static void     parse_imports(FILE *);
588
static struct node *parse(FILE *);
589
590
static int     read_module_internal(const char *);
591
static int     read_module_replacements(const char *);
592
static int     read_import_replacements(const char *,
593
                                         struct module_import *);
594
595
static struct node *merge_parse_objectid(struct node *, FILE *, char *);
596
static struct index_list *getIndexes(FILE * fp, struct index_list **);
597
static struct varbind_list *getVarbinds(FILE * fp, struct varbind_list **);
598
static void     free_indexes(struct index_list **);
599
static void     free_varbinds(struct varbind_list **);
600
static void     free_ranges(struct range_list **);
601
static void     free_enums(struct enum_list **);
602
static struct range_list *copy_ranges(struct range_list *);
603
static struct enum_list *copy_enums(struct enum_list *);
604
605
void
606
snmp_mib_toggle_options_usage(const char *lead, FILE * outf)
607
0
{
608
0
    fprintf(outf, "%su:  %sallow the use of underlines in MIB symbols\n",
609
0
            lead, ((netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
610
0
             NETSNMP_DS_LIB_MIB_PARSE_LABEL)) ?
611
0
       "dis" : ""));
612
0
    fprintf(outf, "%sc:  %sallow the use of \"--\" to terminate comments\n",
613
0
            lead, ((netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
614
0
             NETSNMP_DS_LIB_MIB_COMMENT_TERM)) ?
615
0
       "" : "dis"));
616
617
0
    fprintf(outf, "%sd:  %ssave the DESCRIPTIONs of the MIB objects\n",
618
0
            lead, ((netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
619
0
             NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) ?
620
0
       "do not " : ""));
621
622
0
    fprintf(outf, "%se:  disable errors when MIB symbols conflict\n", lead);
623
624
0
    fprintf(outf, "%sw:  enable warnings when MIB symbols conflict\n", lead);
625
626
0
    fprintf(outf, "%sW:  enable detailed warnings when MIB symbols conflict\n",
627
0
            lead);
628
629
0
    fprintf(outf, "%sR:  replace MIB symbols from latest module\n", lead);
630
0
}
631
632
char           *
633
snmp_mib_toggle_options(char *options)
634
0
{
635
0
    if (options) {
636
0
        while (*options) {
637
0
            switch (*options) {
638
0
            case 'u':
639
0
                netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_PARSE_LABEL,
640
0
                               !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
641
0
                                               NETSNMP_DS_LIB_MIB_PARSE_LABEL));
642
0
                break;
643
644
0
            case 'c':
645
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID,
646
0
            NETSNMP_DS_LIB_MIB_COMMENT_TERM);
647
0
                break;
648
649
0
            case 'e':
650
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID,
651
0
            NETSNMP_DS_LIB_MIB_ERRORS);
652
0
                break;
653
654
0
            case 'w':
655
0
                netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
656
0
           NETSNMP_DS_LIB_MIB_WARNINGS, 1);
657
0
                break;
658
659
0
            case 'W':
660
0
                netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
661
0
           NETSNMP_DS_LIB_MIB_WARNINGS, 2);
662
0
                break;
663
664
0
            case 'd':
665
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, 
666
0
            NETSNMP_DS_LIB_SAVE_MIB_DESCRS);
667
0
                break;
668
669
0
            case 'R':
670
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, 
671
0
            NETSNMP_DS_LIB_MIB_REPLACE);
672
0
                break;
673
674
0
            default:
675
                /*
676
                 * return at the unknown option 
677
                 */
678
0
                return options;
679
0
            }
680
0
            options++;
681
0
        }
682
0
    }
683
0
    return NULL;
684
0
}
685
686
static int
687
name_hash(const char *name)
688
329
{
689
329
    int             hash = 0;
690
329
    const char     *cp;
691
692
329
    if (!name)
693
0
        return 0;
694
6.34k
    for (cp = name; *cp; cp++)
695
6.01k
        hash += tolower((unsigned char)(*cp));
696
329
    return (hash);
697
329
}
698
699
void
700
netsnmp_init_mib_internals(void)
701
301
{
702
301
    register struct tok *tp;
703
301
    register int    b, i;
704
301
    int             max_modc;
705
706
301
    if (tree_head)
707
299
        return;
708
709
    /*
710
     * Set up hash list of pre-defined tokens
711
     */
712
2
    memset(buckets, 0, sizeof(buckets));
713
186
    for (tp = tokens; tp->name; tp++) {
714
184
        tp->hash = name_hash(tp->name);
715
184
        b = BUCKET(tp->hash);
716
184
        if (buckets[b])
717
120
            tp->next = buckets[b];      /* BUG ??? */
718
184
        buckets[b] = tp;
719
184
    }
720
721
    /*
722
     * Initialise other internal structures
723
     */
724
725
2
    max_modc = sizeof(module_map) / sizeof(module_map[0]) - 1;
726
44
    for (i = 0; i < max_modc; ++i)
727
42
        module_map[i].next = &(module_map[i + 1]);
728
2
    module_map[max_modc].next = NULL;
729
2
    module_map_head = module_map;
730
731
2
    memset(nbuckets, 0, sizeof(nbuckets));
732
2
    memset(tbuckets, 0, sizeof(tbuckets));
733
2
    free(tclist);
734
2
    tc_alloc = TC_INCR;
735
2
    tclist = calloc(tc_alloc, sizeof(struct tc));
736
2
    build_translation_table();
737
2
    init_tree_roots();          /* Set up initial roots */
738
    /*
739
     * Relies on 'add_mibdir' having set up the modules 
740
     */
741
2
}
742
743
#ifndef NETSNMP_NO_LEGACY_DEFINITIONS
744
void
745
init_mib_internals(void)
746
0
{
747
0
    netsnmp_init_mib_internals();
748
0
}
749
#endif
750
751
static void
752
init_node_hash(struct node *nodes)
753
0
{
754
0
    struct node    *np, *nextp;
755
0
    int             hash;
756
757
0
    memset(nbuckets, 0, sizeof(nbuckets));
758
0
    for (np = nodes; np;) {
759
0
        nextp = np->next;
760
0
        hash = NBUCKET(name_hash(np->parent));
761
0
        np->next = nbuckets[hash];
762
0
        nbuckets[hash] = np;
763
0
        np = nextp;
764
0
    }
765
0
}
766
767
static int      erroneousMibs = 0;
768
769
netsnmp_feature_child_of(parse_get_error_count, netsnmp_unused);
770
#ifndef NETSNMP_FEATURE_REMOVE_PARSE_GET_ERROR_COUNT
771
int
772
get_mib_parse_error_count(void)
773
0
{
774
0
    return erroneousMibs;
775
0
}
776
#endif /* NETSNMP_FEATURE_REMOVE_PARSE_GET_ERROR_COUNT */
777
778
779
static void
780
print_error(const char *str, const char *token, int type)
781
273
{
782
273
    erroneousMibs++;
783
273
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
784
273
                                NETSNMP_DS_LIB_MIB_ERRORS))
785
273
  return;
786
0
    DEBUGMSGTL(("parse-mibs", "\n"));
787
0
    if (type == ENDOFFILE)
788
0
        snmp_log(LOG_ERR, "%s (EOF): At line %d in %s\n", str, mibLine,
789
0
                 File);
790
0
    else if (token && *token)
791
0
        snmp_log(LOG_ERR, "%s (%s): At line %d in %s\n", str, token,
792
0
                 mibLine, File);
793
0
    else
794
0
        snmp_log(LOG_ERR, "%s: At line %d in %s\n", str, mibLine, File);
795
0
}
796
797
static void
798
print_module_not_found(const char *cp)
799
282
{
800
282
    if (first_err_module) {
801
2
        snmp_log(LOG_ERR, "MIB search path: %s\n",
802
2
                           netsnmp_get_mib_directory());
803
2
        first_err_module = 0;
804
2
    }
805
282
    if (!last_err_module || strcmp(cp, last_err_module))
806
273
        print_error("Cannot find module", cp, CONTINUE);
807
282
    if (last_err_module)
808
280
        free(last_err_module);
809
282
    last_err_module = strdup(cp);
810
282
}
811
812
static struct node *
813
alloc_node(int modid)
814
0
{
815
0
    struct node    *np;
816
817
0
    np = calloc(1, sizeof(struct node));
818
0
    if (!np)
819
0
        return NULL;
820
821
0
    np->tc_index = -1;
822
0
    np->modid = modid;
823
0
    np->filename = strdup(File);
824
0
    np->lineno = mibLine;
825
826
0
    return np;
827
0
}
828
829
static void
830
unlink_tbucket(struct tree *tp)
831
0
{
832
0
    int             hash = NBUCKET(name_hash(tp->label));
833
0
    struct tree    *otp = NULL, *ntp = tbuckets[hash];
834
835
0
    while (ntp && ntp != tp) {
836
0
        otp = ntp;
837
0
        ntp = ntp->next;
838
0
    }
839
0
    if (!ntp)
840
0
        snmp_log(LOG_EMERG, "Can't find %s in tbuckets\n", tp->label);
841
0
    else if (otp)
842
0
        otp->next = ntp->next;
843
0
    else
844
0
        tbuckets[hash] = tp->next;
845
0
}
846
847
static void
848
unlink_tree(struct tree *tp)
849
0
{
850
0
    struct tree    *otp = NULL, *ntp = tp->parent;
851
852
0
    if (!ntp) {                 /* this tree has no parent */
853
0
        DEBUGMSGTL(("unlink_tree", "Tree node %s has no parent\n",
854
0
                    tp->label));
855
0
    } else {
856
0
        child_index_modified(ntp, NULL);
857
0
        ntp = ntp->child_list;
858
859
0
        while (ntp && ntp != tp) {
860
0
            otp = ntp;
861
0
            ntp = ntp->next_peer;
862
0
        }
863
0
        if (!ntp)
864
0
            snmp_log(LOG_EMERG, "Can't find %s in %s's children\n",
865
0
                     tp->label, tp->parent->label);
866
0
        else if (otp)
867
0
            otp->next_peer = ntp->next_peer;
868
0
        else
869
0
            tp->parent->child_list = tp->next_peer;
870
0
    }
871
872
0
    if (tree_head == tp)
873
0
        tree_head = tp->next_peer;
874
0
}
875
876
static void
877
free_partial_tree(struct tree *tp, int keep_label)
878
0
{
879
0
    if (!tp)
880
0
        return;
881
882
    /*
883
     * remove the data from this tree node 
884
     */
885
0
    free_enums(&tp->enums);
886
0
    free_ranges(&tp->ranges);
887
0
    free_indexes(&tp->indexes);
888
0
    free_varbinds(&tp->varbinds);
889
0
    if (!keep_label)
890
0
        SNMP_FREE(tp->label);
891
0
    SNMP_FREE(tp->hint);
892
0
    SNMP_FREE(tp->units);
893
0
    SNMP_FREE(tp->description);
894
0
    SNMP_FREE(tp->reference);
895
0
    SNMP_FREE(tp->augments);
896
0
    SNMP_FREE(tp->defaultValue);
897
0
}
898
899
/*
900
 * free a tree node. Note: the node must already have been unlinked
901
 * from the tree when calling this routine
902
 */
903
static void
904
free_tree(struct tree *Tree)
905
0
{
906
0
    if (!Tree)
907
0
        return;
908
909
0
    unlink_tbucket(Tree);
910
0
    free_partial_tree(Tree, FALSE);
911
0
    if (Tree->module_list != &Tree->modid)
912
0
        free(Tree->module_list);
913
0
    free(Tree);
914
0
}
915
916
static void
917
free_node(struct node *np)
918
0
{
919
0
    if (!np)
920
0
        return;
921
922
0
    free_enums(&np->enums);
923
0
    free_ranges(&np->ranges);
924
0
    free_indexes(&np->indexes);
925
0
    free_varbinds(&np->varbinds);
926
0
    free(np->label);
927
0
    free(np->hint);
928
0
    free(np->units);
929
0
    free(np->description);
930
0
    free(np->reference);
931
0
    free(np->defaultValue);
932
0
    free(np->parent);
933
0
    free(np->augments);
934
0
    free(np->filename);
935
0
    free(np);
936
0
}
937
938
static void
939
print_range_value(FILE * fp, int type, struct range_list * rp)
940
0
{
941
0
    switch (type) {
942
0
    case TYPE_INTEGER:
943
0
    case TYPE_INTEGER32:
944
0
        if (rp->low == rp->high)
945
0
            fprintf(fp, "%d", rp->low);
946
0
        else
947
0
            fprintf(fp, "%d..%d", rp->low, rp->high);
948
0
        break;
949
0
    case TYPE_UNSIGNED32:
950
0
    case TYPE_OCTETSTR:
951
0
    case TYPE_GAUGE:
952
0
    case TYPE_UINTEGER:
953
0
        if (rp->low == rp->high)
954
0
            fprintf(fp, "%u", (unsigned)rp->low);
955
0
        else
956
0
            fprintf(fp, "%u..%u", (unsigned)rp->low, (unsigned)rp->high);
957
0
        break;
958
0
    default:
959
        /* No other range types allowed */
960
0
        break;
961
0
    }
962
0
}
963
964
#ifdef TEST
965
static void
966
print_nodes(FILE * fp, struct node *root)
967
{
968
    struct enum_list *ep;
969
    struct index_list *ip;
970
    struct varbind_list *vp;
971
    struct node    *np;
972
973
    for (np = root; np; np = np->next) {
974
        fprintf(fp, "%s ::= { %s %ld } (%d)\n", np->label, np->parent,
975
                np->subid, np->type);
976
        if (np->tc_index >= 0)
977
            fprintf(fp, "  TC = %s\n", tclist[np->tc_index].descriptor);
978
        if (np->enums) {
979
            fprintf(fp, "  Enums: \n");
980
            for (ep = np->enums; ep; ep = ep->next) {
981
                fprintf(fp, "    %s(%d)\n", ep->label, ep->value);
982
            }
983
        }
984
        if (np->ranges) {
985
            struct range_list *rp;
986
            fprintf(fp, "  Ranges: ");
987
            for (rp = np->ranges; rp; rp = rp->next) {
988
                fprintf(fp, "\n    ");
989
                print_range_value(fp, np->type, rp);
990
            }
991
            fprintf(fp, "\n");
992
        }
993
        if (np->indexes) {
994
            fprintf(fp, "  Indexes: \n");
995
            for (ip = np->indexes; ip; ip = ip->next) {
996
                fprintf(fp, "    %s\n", ip->ilabel);
997
            }
998
        }
999
        if (np->augments)
1000
            fprintf(fp, "  Augments: %s\n", np->augments);
1001
        if (np->varbinds) {
1002
            fprintf(fp, "  Varbinds: \n");
1003
            for (vp = np->varbinds; vp; vp = vp->next) {
1004
                fprintf(fp, "    %s\n", vp->vblabel);
1005
            }
1006
        }
1007
        if (np->hint)
1008
            fprintf(fp, "  Hint: %s\n", np->hint);
1009
        if (np->units)
1010
            fprintf(fp, "  Units: %s\n", np->units);
1011
        if (np->defaultValue)
1012
            fprintf(fp, "  DefaultValue: %s\n", np->defaultValue);
1013
    }
1014
}
1015
#endif
1016
1017
void
1018
print_subtree(FILE * f, struct tree *tree, int count)
1019
0
{
1020
0
    struct tree    *tp;
1021
0
    int             i;
1022
0
    char            modbuf[256];
1023
1024
0
    for (i = 0; i < count; i++)
1025
0
        fprintf(f, "  ");
1026
0
    fprintf(f, "Children of %s(%ld):\n", tree->label, tree->subid);
1027
0
    count++;
1028
0
    for (tp = tree->child_list; tp; tp = tp->next_peer) {
1029
0
        for (i = 0; i < count; i++)
1030
0
            fprintf(f, "  ");
1031
0
        fprintf(f, "%s:%s(%ld) type=%d",
1032
0
                module_name(tp->module_list[0], modbuf),
1033
0
                tp->label, tp->subid, tp->type);
1034
0
        if (tp->tc_index != -1)
1035
0
            fprintf(f, " tc=%d", tp->tc_index);
1036
0
        if (tp->hint)
1037
0
            fprintf(f, " hint=%s", tp->hint);
1038
0
        if (tp->units)
1039
0
            fprintf(f, " units=%s", tp->units);
1040
0
        if (tp->number_modules > 1) {
1041
0
            fprintf(f, " modules:");
1042
0
            for (i = 1; i < tp->number_modules; i++)
1043
0
                fprintf(f, " %s", module_name(tp->module_list[i], modbuf));
1044
0
        }
1045
0
        fprintf(f, "\n");
1046
0
    }
1047
0
    for (tp = tree->child_list; tp; tp = tp->next_peer) {
1048
0
        if (tp->child_list)
1049
0
            print_subtree(f, tp, count);
1050
0
    }
1051
0
}
1052
1053
void
1054
print_ascii_dump_tree(FILE * f, struct tree *tree, int count)
1055
0
{
1056
0
    struct tree    *tp;
1057
1058
0
    count++;
1059
0
    for (tp = tree->child_list; tp; tp = tp->next_peer) {
1060
0
        fprintf(f, "%s OBJECT IDENTIFIER ::= { %s %ld }\n", tp->label,
1061
0
                tree->label, tp->subid);
1062
0
    }
1063
0
    for (tp = tree->child_list; tp; tp = tp->next_peer) {
1064
0
        if (tp->child_list)
1065
0
            print_ascii_dump_tree(f, tp, count);
1066
0
    }
1067
0
}
1068
1069
static int      translation_table[256];
1070
1071
static void
1072
build_translation_table(void)
1073
2
{
1074
2
    int             count;
1075
1076
514
    for (count = 0; count < 256; count++) {
1077
512
        switch (count) {
1078
2
        case OBJID:
1079
2
            translation_table[count] = TYPE_OBJID;
1080
2
            break;
1081
2
        case OCTETSTR:
1082
2
            translation_table[count] = TYPE_OCTETSTR;
1083
2
            break;
1084
2
        case INTEGER:
1085
2
            translation_table[count] = TYPE_INTEGER;
1086
2
            break;
1087
2
        case NETADDR:
1088
2
            translation_table[count] = TYPE_NETADDR;
1089
2
            break;
1090
2
        case IPADDR:
1091
2
            translation_table[count] = TYPE_IPADDR;
1092
2
            break;
1093
2
        case COUNTER:
1094
2
            translation_table[count] = TYPE_COUNTER;
1095
2
            break;
1096
2
        case GAUGE:
1097
2
            translation_table[count] = TYPE_GAUGE;
1098
2
            break;
1099
2
        case TIMETICKS:
1100
2
            translation_table[count] = TYPE_TIMETICKS;
1101
2
            break;
1102
2
        case KW_OPAQUE:
1103
2
            translation_table[count] = TYPE_OPAQUE;
1104
2
            break;
1105
2
        case NUL:
1106
2
            translation_table[count] = TYPE_NULL;
1107
2
            break;
1108
2
        case COUNTER64:
1109
2
            translation_table[count] = TYPE_COUNTER64;
1110
2
            break;
1111
2
        case BITSTRING:
1112
2
            translation_table[count] = TYPE_BITSTRING;
1113
2
            break;
1114
2
        case NSAPADDRESS:
1115
2
            translation_table[count] = TYPE_NSAPADDRESS;
1116
2
            break;
1117
2
        case INTEGER32:
1118
2
            translation_table[count] = TYPE_INTEGER32;
1119
2
            break;
1120
2
        case UINTEGER32:
1121
2
            translation_table[count] = TYPE_UINTEGER;
1122
2
            break;
1123
2
        case UNSIGNED32:
1124
2
            translation_table[count] = TYPE_UNSIGNED32;
1125
2
            break;
1126
2
        case TRAPTYPE:
1127
2
            translation_table[count] = TYPE_TRAPTYPE;
1128
2
            break;
1129
2
        case NOTIFTYPE:
1130
2
            translation_table[count] = TYPE_NOTIFTYPE;
1131
2
            break;
1132
2
        case NOTIFGROUP:
1133
2
            translation_table[count] = TYPE_NOTIFGROUP;
1134
2
            break;
1135
2
        case OBJGROUP:
1136
2
            translation_table[count] = TYPE_OBJGROUP;
1137
2
            break;
1138
2
        case MODULEIDENTITY:
1139
2
            translation_table[count] = TYPE_MODID;
1140
2
            break;
1141
2
        case OBJIDENTITY:
1142
2
            translation_table[count] = TYPE_OBJIDENTITY;
1143
2
            break;
1144
2
        case AGENTCAP:
1145
2
            translation_table[count] = TYPE_AGENTCAP;
1146
2
            break;
1147
2
        case COMPLIANCE:
1148
2
            translation_table[count] = TYPE_MODCOMP;
1149
2
            break;
1150
464
        default:
1151
464
            translation_table[count] = TYPE_OTHER;
1152
464
            break;
1153
512
        }
1154
512
    }
1155
2
}
1156
1157
static void
1158
init_tree_roots(void)
1159
2
{
1160
2
    struct tree    *tp, *lasttp;
1161
2
    int             base_modid;
1162
2
    int             hash;
1163
1164
2
    base_modid = which_module("SNMPv2-SMI");
1165
2
    if (base_modid == -1)
1166
2
        base_modid = which_module("RFC1155-SMI");
1167
2
    if (base_modid == -1)
1168
2
        base_modid = which_module("RFC1213-MIB");
1169
1170
    /*
1171
     * build root node 
1172
     */
1173
2
    tp = calloc(1, sizeof(struct tree));
1174
2
    if (tp == NULL)
1175
0
        return;
1176
2
    tp->label = strdup("joint-iso-ccitt");
1177
2
    tp->modid = base_modid;
1178
2
    tp->number_modules = 1;
1179
2
    tp->module_list = &(tp->modid);
1180
2
    tp->subid = 2;
1181
2
    tp->tc_index = -1;
1182
2
    set_function(tp);           /* from mib.c */
1183
2
    hash = NBUCKET(name_hash(tp->label));
1184
2
    tp->next = tbuckets[hash];
1185
2
    tbuckets[hash] = tp;
1186
2
    lasttp = tp;
1187
2
    root_imports[0].label = strdup(tp->label);
1188
2
    root_imports[0].modid = base_modid;
1189
1190
    /*
1191
     * build root node 
1192
     */
1193
2
    tp = calloc(1, sizeof(struct tree));
1194
2
    if (tp == NULL)
1195
0
        return;
1196
2
    tp->next_peer = lasttp;
1197
2
    tp->label = strdup("ccitt");
1198
2
    tp->modid = base_modid;
1199
2
    tp->number_modules = 1;
1200
2
    tp->module_list = &(tp->modid);
1201
2
    tp->subid = 0;
1202
2
    tp->tc_index = -1;
1203
2
    set_function(tp);           /* from mib.c */
1204
2
    hash = NBUCKET(name_hash(tp->label));
1205
2
    tp->next = tbuckets[hash];
1206
2
    tbuckets[hash] = tp;
1207
2
    lasttp = tp;
1208
2
    root_imports[1].label = strdup(tp->label);
1209
2
    root_imports[1].modid = base_modid;
1210
1211
    /*
1212
     * build root node 
1213
     */
1214
2
    tp = calloc(1, sizeof(struct tree));
1215
2
    if (tp == NULL)
1216
0
        return;
1217
2
    tp->next_peer = lasttp;
1218
2
    tp->label = strdup("iso");
1219
2
    tp->modid = base_modid;
1220
2
    tp->number_modules = 1;
1221
2
    tp->module_list = &(tp->modid);
1222
2
    tp->subid = 1;
1223
2
    tp->tc_index = -1;
1224
2
    set_function(tp);           /* from mib.c */
1225
2
    hash = NBUCKET(name_hash(tp->label));
1226
2
    tp->next = tbuckets[hash];
1227
2
    tbuckets[hash] = tp;
1228
2
    lasttp = tp;
1229
2
    root_imports[2].label = strdup(tp->label);
1230
2
    root_imports[2].modid = base_modid;
1231
1232
2
    tree_head = tp;
1233
2
}
1234
1235
#ifdef STRICT_MIB_PARSEING
1236
#define label_compare strcasecmp
1237
#else
1238
6.43k
#define label_compare strcmp
1239
#endif
1240
1241
1242
struct tree    *
1243
find_tree_node(const char *name, int modid)
1244
140
{
1245
140
    struct tree    *tp, *headtp;
1246
140
    int             count, *int_p;
1247
1248
140
    if (!name || !*name)
1249
1
        return (NULL);
1250
1251
139
    headtp = tbuckets[NBUCKET(name_hash(name))];
1252
266
    for (tp = headtp; tp; tp = tp->next) {
1253
134
        if (tp->label && !label_compare(tp->label, name)) {
1254
1255
7
            if (modid == -1)    /* Any module */
1256
7
                return (tp);
1257
1258
0
            for (int_p = tp->module_list, count = 0;
1259
0
                 count < tp->number_modules; ++count, ++int_p)
1260
0
                if (*int_p == modid)
1261
0
                    return (tp);
1262
0
        }
1263
134
    }
1264
1265
132
    return (NULL);
1266
139
}
1267
1268
/*
1269
 * computes a value which represents how close name1 is to name2.
1270
 * * high scores mean a worse match.
1271
 * * (yes, the algorithm sucks!)
1272
 */
1273
0
#define MAX_BAD 0xffffff
1274
1275
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
1276
/*
1277
 * Limits on regular expressions passed to regcomp():
1278
 * - MAX_REGEX_LEN: MIB identifiers are at most 64 characters (RFC 2578,
1279
 *   section 3.1), so bound pattern length.
1280
 * - MAX_REGEX_NESTING_DEPTH: glibc's regcomp() recurses once per parenthesis
1281
 *   nesting level, so deeply nested patterns cause a stack overflow.
1282
 * - MAX_REGEX_PLUS: glibc's parse_dup_op() clones the preceding AST subtree
1283
 *   via duplicate_tree() for each '+' quantifier, so k '+' operators can
1284
 *   grow the AST by up to 2^k nodes and exhaust memory.
1285
 * - '{' interval quantifiers are rejected because duplicate_tree() clones
1286
 *   the preceding subtree up to RE_DUP_MAX (32767) times per interval.
1287
 * - glibc's regcomp() run time can grow exponentially with the number of
1288
 *   epsilon transitions in the NFA it builds. One example is
1289
 *   calc_eclosure_iter(), which recomputes the incomplete epsilon closures
1290
 *   of nodes that are part of a cycle of epsilon transitions. Another
1291
 *   example is the duplication of nodes for each combination of anchor
1292
 *   constraints. Hence the following restrictions:
1293
 *   - The quantifiers '*', '+' and '?' are only accepted directly after an
1294
 *     atom that always consumes exactly one character: an ordinary
1295
 *     character, '.', an escaped punctuation character or a bracket
1296
 *     expression. Quantifying a group or another quantifier is rejected.
1297
 *   - '^' is only accepted at the start and '$' only at the end of the
1298
 *     pattern.
1299
 *   - Escaped alphanumeric characters (GNU extensions like \b and \w, and
1300
 *     back-references) and the GNU anchors \<, \>, \` and \' are rejected.
1301
 *   Examples of short patterns for which regcomp() takes minutes or longer
1302
 *   without these restrictions are "(()|())(()|())...*",
1303
 *   "(((((((^|$)*)*)*)*)*)*)*", "$a++*?*+++**" and "(.*|^|$)(.*|^|$)...".
1304
 */
1305
0
#define MAX_REGEX_LEN           256
1306
0
#define MAX_REGEX_NESTING_DEPTH 32
1307
0
#define MAX_REGEX_PLUS          8
1308
1309
/*
1310
 * Skip a bracket expression. @p points at the opening '['. Returns a pointer
1311
 * to the closing ']' or NULL if the bracket expression is not terminated.
1312
 */
1313
static const char *
1314
skip_bracket_expr(const char *p)
1315
0
{
1316
0
    p++;
1317
0
    if (*p == '^')
1318
0
        p++;
1319
0
    if (*p == ']')
1320
0
        p++;
1321
0
    for (; *p; p++) {
1322
0
        if (*p == ']')
1323
0
            return p;
1324
0
        if (*p == '[' && (p[1] == ':' || p[1] == '=' || p[1] == '.')) {
1325
0
            const char      delim = p[1];
1326
1327
0
            for (p += 2; *p && !(p[0] == delim && p[1] == ']'); p++)
1328
0
                ;
1329
0
            if (!*p)
1330
0
                return NULL;
1331
0
            p++;
1332
0
        }
1333
0
    }
1334
0
    return NULL;
1335
0
}
1336
1337
static int
1338
regex_too_complex(const char *key)
1339
0
{
1340
0
    const char     *const start = key;
1341
0
    int             depth = 0;
1342
0
    int             plus_count = 0;
1343
0
    int             quantifiable = 0;
1344
0
    const char     *end;
1345
1346
0
    if (memchr(key, '\0', MAX_REGEX_LEN + 1) == NULL)
1347
0
        return 1;
1348
1349
0
    for (; *key; key++) {
1350
0
        switch (*key) {
1351
0
        case '\\':
1352
0
            if (!key[1])
1353
0
                return 1;
1354
0
            key++;
1355
0
            if (isalnum((unsigned char)*key) || strchr("<>`'", *key))
1356
0
                return 1;
1357
0
            quantifiable = 1;
1358
0
            break;
1359
0
        case '[':
1360
0
            end = skip_bracket_expr(key);
1361
0
            if (!end)
1362
0
                return 1;
1363
0
            key = end;
1364
0
            quantifiable = 1;
1365
0
            break;
1366
0
        case '(':
1367
0
            if (++depth > MAX_REGEX_NESTING_DEPTH)
1368
0
                return 1;
1369
0
            quantifiable = 0;
1370
0
            break;
1371
0
        case ')':
1372
0
            if (depth > 0)
1373
0
                depth--;
1374
0
            quantifiable = 0;
1375
0
            break;
1376
0
        case '+':
1377
0
            if (++plus_count > MAX_REGEX_PLUS)
1378
0
                return 1;
1379
            /* FALL THROUGH */
1380
0
        case '*':
1381
0
        case '?':
1382
0
            if (!quantifiable)
1383
0
                return 1;
1384
0
            quantifiable = 0;
1385
0
            break;
1386
0
        case '{':
1387
0
            return 1;
1388
0
        case '^':
1389
0
            if (key != start)
1390
0
                return 1;
1391
0
            quantifiable = 0;
1392
0
            break;
1393
0
        case '$':
1394
0
            if (key[1])
1395
0
                return 1;
1396
0
            quantifiable = 0;
1397
0
            break;
1398
0
        case '|':
1399
0
            quantifiable = 0;
1400
0
            break;
1401
0
        default:
1402
0
            quantifiable = 1;
1403
0
            break;
1404
0
        }
1405
0
    }
1406
0
    return 0;
1407
0
}
1408
1409
static          u_int
1410
compute_match(const char *search_base, const regex_t *parsetree)
1411
0
{
1412
0
    regmatch_t      pmatch;
1413
1414
0
    if (regexec(parsetree, search_base, 1, &pmatch, 0) == 0)
1415
0
        return pmatch.rm_so;
1416
0
    return MAX_BAD;
1417
0
}
1418
#else                           /* use our own wildcard matcher */
1419
static          u_int
1420
compute_match(const char *search_base, const char *key)
1421
{
1422
    /*
1423
     * first find the longest matching substring (ick) 
1424
     */
1425
    char           *first = NULL, *result = NULL, *entry;
1426
    const char     *position;
1427
    char           *newkey = strdup(key);
1428
    char           *st;
1429
1430
1431
    entry = strtok_r(newkey, "*", &st);
1432
    position = search_base;
1433
    while (entry) {
1434
        result = strcasestr(position, entry);
1435
1436
        if (result == NULL) {
1437
            free(newkey);
1438
            return MAX_BAD;
1439
        }
1440
1441
        if (first == NULL)
1442
            first = result;
1443
1444
        position = result + strlen(entry);
1445
        entry = strtok_r(NULL, "*", &st);
1446
    }
1447
    free(newkey);
1448
    if (result)
1449
        return (first - search_base);
1450
1451
    /*
1452
     * not found 
1453
     */
1454
    return MAX_BAD;
1455
}
1456
#endif
1457
1458
/*
1459
 * Find the tree node that best matches the pattern string.
1460
 * Use the "reported" flag such that only one match
1461
 * is attempted for every node.
1462
 *
1463
 * Warning! This function may recurse.
1464
 *
1465
 * Caller _must_ invoke clear_tree_flags before first call
1466
 * to this function.  This function may be called multiple times
1467
 * to ensure that the entire tree is traversed.
1468
 */
1469
1470
static struct tree *
1471
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
1472
find_best_tree_node_r(const regex_t *parsetree, struct tree *tree_top,
1473
                      u_int * match)
1474
#else
1475
find_best_tree_node_r(const char *pattrn, struct tree *tree_top,
1476
                      u_int * match)
1477
#endif
1478
0
{
1479
0
    struct tree    *tp, *best_so_far = NULL, *retptr;
1480
0
    u_int           old_match = MAX_BAD, new_match = MAX_BAD;
1481
1482
0
    for (tp = tree_top; tp; tp = tp->next_peer) {
1483
0
        if (!tp->reported && tp->label)
1484
0
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
1485
0
            new_match = compute_match(tp->label, parsetree);
1486
#else
1487
            new_match = compute_match(tp->label, pattrn);
1488
#endif
1489
0
        tp->reported = 1;
1490
1491
0
        if (new_match < old_match) {
1492
0
            best_so_far = tp;
1493
0
            old_match = new_match;
1494
0
        }
1495
0
        if (new_match == 0)
1496
0
            break;              /* this is the best result we can get */
1497
0
        if (tp->child_list) {
1498
0
            retptr =
1499
0
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
1500
0
                find_best_tree_node_r(parsetree, tp->child_list, &new_match);
1501
#else
1502
                find_best_tree_node_r(pattrn, tp->child_list, &new_match);
1503
#endif
1504
0
            if (new_match < old_match) {
1505
0
                best_so_far = retptr;
1506
0
                old_match = new_match;
1507
0
            }
1508
0
            if (new_match == 0)
1509
0
                break;          /* this is the best result we can get */
1510
0
        }
1511
0
    }
1512
0
    if (match)
1513
0
        *match = old_match;
1514
0
    return (best_so_far);
1515
0
}
1516
1517
struct tree    *
1518
find_best_tree_node(const char *pattrn, struct tree *tree_top,
1519
                    u_int * match)
1520
0
{
1521
0
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
1522
0
    regex_t         parsetree;
1523
0
    struct tree    *best_so_far;
1524
0
#endif
1525
1526
0
    if (match)
1527
0
        *match = MAX_BAD;
1528
1529
0
    if (!pattrn || !*pattrn)
1530
0
        return (NULL);
1531
1532
0
    if (!tree_top)
1533
0
        tree_top = get_tree_head();
1534
1535
0
#if defined(HAVE_REGEX_H) && defined(HAVE_REGCOMP)
1536
0
    if (regex_too_complex(pattrn))
1537
0
        return (NULL);
1538
0
    if (regcomp(&parsetree, pattrn, REG_ICASE | REG_EXTENDED) != 0)
1539
0
        return (NULL);
1540
0
    best_so_far = find_best_tree_node_r(&parsetree, tree_top, match);
1541
0
    regfree(&parsetree);
1542
0
    return best_so_far;
1543
#else
1544
    return find_best_tree_node_r(pattrn, tree_top, match);
1545
#endif
1546
0
}
1547
1548
1549
static void
1550
merge_anon_children(struct tree *tp1, struct tree *tp2)
1551
                /*
1552
                 * NB: tp1 is the 'anonymous' node 
1553
                 */
1554
0
{
1555
0
    struct tree    *child1, *child2, *previous;
1556
1557
0
    child_index_modified(tp1, NULL);
1558
0
    child_index_modified(tp2, NULL);
1559
0
    for (child1 = tp1->child_list; child1;) {
1560
1561
0
        for (child2 = tp2->child_list, previous = NULL;
1562
0
             child2; previous = child2, child2 = child2->next_peer) {
1563
1564
0
            if (child1->subid == child2->subid) {
1565
                /*
1566
                 * Found 'matching' children,
1567
                 *  so merge them
1568
                 */
1569
0
                if (!strncmp(child1->label, ANON, ANON_LEN)) {
1570
0
                    merge_anon_children(child1, child2);
1571
1572
0
                    child1->child_list = NULL;
1573
0
                    previous = child1;  /* Finished with 'child1' */
1574
0
                    child1 = child1->next_peer;
1575
0
                    free_tree(previous);
1576
0
                    goto next;
1577
0
                }
1578
1579
0
                else if (!strncmp(child2->label, ANON, ANON_LEN)) {
1580
0
                    merge_anon_children(child2, child1);
1581
1582
0
                    if (previous)
1583
0
                        previous->next_peer = child2->next_peer;
1584
0
                    else
1585
0
                        tp2->child_list = child2->next_peer;
1586
0
                    free_tree(child2);
1587
1588
0
                    previous = child1;  /* Move 'child1' to 'tp2' */
1589
0
                    child1 = child1->next_peer;
1590
0
                    previous->next_peer = tp2->child_list;
1591
0
                    tp2->child_list = previous;
1592
0
                    for (previous = tp2->child_list;
1593
0
                         previous; previous = previous->next_peer)
1594
0
                        previous->parent = tp2;
1595
0
                    goto next;
1596
0
                } else if (!label_compare(child1->label, child2->label)) {
1597
0
                    if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
1598
0
             NETSNMP_DS_LIB_MIB_WARNINGS)) {
1599
0
                        snmp_log(LOG_WARNING,
1600
0
                                 "Warning: %s.%ld is both %s and %s (%s)\n",
1601
0
                                 tp2->label, child1->subid, child1->label,
1602
0
                                 child2->label, File);
1603
0
        }
1604
0
                    continue;
1605
0
                } else {
1606
                    /*
1607
                     * Two copies of the same node.
1608
                     * 'child2' adopts the children of 'child1'
1609
                     */
1610
1611
0
                    child_index_modified(child1, NULL);
1612
0
                    child_index_modified(child2, NULL);
1613
0
                    if (child2->child_list) {
1614
0
                        for (previous = child2->child_list; previous->next_peer; previous = previous->next_peer);       /* Find the end of the list */
1615
0
                        previous->next_peer = child1->child_list;
1616
0
                    } else
1617
0
                        child2->child_list = child1->child_list;
1618
0
                    for (previous = child1->child_list;
1619
0
                         previous; previous = previous->next_peer)
1620
0
                        previous->parent = child2;
1621
0
                    child1->child_list = NULL;
1622
1623
0
                    previous = child1;  /* Finished with 'child1' */
1624
0
                    child1 = child1->next_peer;
1625
0
                    free_tree(previous);
1626
0
                    goto next;
1627
0
                }
1628
0
            }
1629
0
        }
1630
        /*
1631
         * If no match, move 'child1' to 'tp2' child_list
1632
         */
1633
0
        if (child1) {
1634
0
            previous = child1;
1635
0
            child1 = child1->next_peer;
1636
0
            previous->parent = tp2;
1637
0
            previous->next_peer = tp2->child_list;
1638
0
            tp2->child_list = previous;
1639
0
        }
1640
0
      next:;
1641
0
    }
1642
0
}
1643
1644
/*
1645
 * Index of the child list of a tree node, used by do_subtree() to look up
1646
 * children by subid in O(1) instead of scanning the entire child list for
1647
 * every node that is added to the tree. For each subid, the first child
1648
 * with that subid and its predecessor in the child list are stored.
1649
 *
1650
 * do_subtree() updates its own index for the modifications it makes to the
1651
 * indexed child list. Child lists may also be modified by recursive
1652
 * do_subtree() calls and by merge_anon_children(). Hence, all active
1653
 * indexes are kept on a stack and child_index_modified() invalidates the
1654
 * indexes of a child list that has been modified. An invalidated index is
1655
 * rebuilt the next time it is used.
1656
 */
1657
struct child_entry {
1658
    struct child_entry *next;
1659
    struct tree    *first;
1660
    struct tree    *pred;
1661
};
1662
1663
struct child_index {
1664
    struct child_index *up;
1665
    struct tree    *parent;
1666
    struct child_entry **buckets;
1667
    size_t          size;
1668
    size_t          count;
1669
    struct tree    *tail;
1670
    int             valid;
1671
};
1672
1673
static struct child_index *active_child_indexes;
1674
1675
/*
1676
 * Invalidate all active indexes of the child list of @parent except @except.
1677
 */
1678
static void
1679
child_index_modified(const struct tree *parent,
1680
                     const struct child_index *except)
1681
0
{
1682
0
    struct child_index *ci;
1683
1684
0
    for (ci = active_child_indexes; ci; ci = ci->up)
1685
0
        if (ci->parent == parent && ci != except)
1686
0
            ci->valid = 0;
1687
0
}
1688
1689
static size_t
1690
child_hash(const struct child_index *ci, u_long subid)
1691
0
{
1692
0
    return (size_t)((subid ^ (subid >> 17)) * 2654435761u) & (ci->size - 1);
1693
0
}
1694
1695
static void
1696
child_index_clear(struct child_index *ci)
1697
0
{
1698
0
    size_t          i;
1699
1700
0
    for (i = 0; i < ci->size; i++) {
1701
0
        struct child_entry *ce, *next;
1702
1703
0
        for (ce = ci->buckets[i]; ce; ce = next) {
1704
0
            next = ce->next;
1705
0
            free(ce);
1706
0
        }
1707
0
    }
1708
0
    free(ci->buckets);
1709
0
    ci->buckets = NULL;
1710
0
    ci->size = 0;
1711
0
    ci->count = 0;
1712
0
    ci->tail = NULL;
1713
0
    ci->valid = 0;
1714
0
}
1715
1716
static struct child_entry *
1717
child_index_find(const struct child_index *ci, u_long subid)
1718
0
{
1719
0
    struct child_entry *ce;
1720
1721
0
    for (ce = ci->buckets[child_hash(ci, subid)]; ce; ce = ce->next)
1722
0
        if (ce->first->subid == subid)
1723
0
            return ce;
1724
0
    return NULL;
1725
0
}
1726
1727
static int
1728
child_index_grow(struct child_index *ci)
1729
0
{
1730
0
    struct child_index new_ci = *ci;
1731
0
    size_t          i;
1732
1733
0
    new_ci.size = ci->size ? 2 * ci->size : 64;
1734
0
    new_ci.buckets = calloc(new_ci.size, sizeof(*new_ci.buckets));
1735
0
    if (!new_ci.buckets)
1736
0
        return -1;
1737
0
    for (i = 0; i < ci->size; i++) {
1738
0
        struct child_entry *ce, *next;
1739
1740
0
        for (ce = ci->buckets[i]; ce; ce = next) {
1741
0
            struct child_entry **head =
1742
0
                &new_ci.buckets[child_hash(&new_ci, ce->first->subid)];
1743
1744
0
            next = ce->next;
1745
0
            ce->next = *head;
1746
0
            *head = ce;
1747
0
        }
1748
0
    }
1749
0
    free(ci->buckets);
1750
0
    ci->buckets = new_ci.buckets;
1751
0
    ci->size = new_ci.size;
1752
0
    return 0;
1753
0
}
1754
1755
/*
1756
 * Record that @tp, the first child with subid @tp->subid, has predecessor
1757
 * @pred. Returns 0 upon success and -1 if memory allocation failed.
1758
 */
1759
static int
1760
child_index_add(struct child_index *ci, struct tree *tp, struct tree *pred)
1761
0
{
1762
0
    struct child_entry *ce, **head;
1763
1764
0
    if (ci->count >= ci->size / 2 && child_index_grow(ci) < 0)
1765
0
        return -1;
1766
0
    ce = malloc(sizeof(*ce));
1767
0
    if (!ce)
1768
0
        return -1;
1769
0
    ce->first = tp;
1770
0
    ce->pred = pred;
1771
0
    head = &ci->buckets[child_hash(ci, tp->subid)];
1772
0
    ce->next = *head;
1773
0
    *head = ce;
1774
0
    ci->count++;
1775
0
    return 0;
1776
0
}
1777
1778
/*
1779
 * (Re)build the index of the child list of ci->parent. Leaves ci->valid
1780
 * zero if memory allocation fails.
1781
 */
1782
static void
1783
child_index_build(struct child_index *ci)
1784
0
{
1785
0
    struct tree    *tp, *pred = NULL;
1786
1787
0
    child_index_clear(ci);
1788
0
    if (child_index_grow(ci) < 0)
1789
0
        return;
1790
0
    for (tp = ci->parent->child_list; tp; pred = tp, tp = tp->next_peer) {
1791
0
        if (!child_index_find(ci, tp->subid) &&
1792
0
            child_index_add(ci, tp, pred) < 0) {
1793
0
            child_index_clear(ci);
1794
0
            return;
1795
0
        }
1796
0
    }
1797
0
    ci->tail = pred;
1798
0
    ci->valid = 1;
1799
0
}
1800
1801
/*
1802
 * Unlink @tp from the tree and update index @ci, if not NULL, if @tp is the
1803
 * first of multiple children of ci->parent with the same subid.
1804
 */
1805
static void
1806
unlink_indexed_tree(struct child_index *ci, struct tree *tp)
1807
0
{
1808
0
    struct tree    *next = tp->next_peer;
1809
0
    int             keep = ci && ci->valid && ci->parent == tp->parent;
1810
0
    struct child_entry *ce;
1811
1812
0
    unlink_tree(tp);
1813
0
    if (!keep || !next || next->subid != tp->subid)
1814
0
        return;
1815
0
    ce = child_index_find(ci, tp->subid);
1816
0
    if (ce && ce->first == tp) {
1817
0
        ce->first = next;
1818
0
        ci->valid = 1;
1819
0
    }
1820
0
}
1821
1822
1823
/*
1824
 * Find all the children of root in the list of nodes.  Link them into the
1825
 * tree and out of the nodes list.
1826
 */
1827
static void
1828
do_subtree(struct tree *root, struct node **nodes)
1829
0
{
1830
0
    struct tree    *tp, *anon_tp = NULL;
1831
0
    struct tree    *xroot = root;
1832
0
    struct node    *np, **headp;
1833
0
    struct node    *oldnp = NULL, *child_list = NULL, *childp = NULL;
1834
0
    int             hash;
1835
0
    int            *int_p;
1836
0
    int             depth = 0;
1837
0
    struct tree    *p;
1838
0
    struct child_index *ci = NULL;
1839
1840
0
    for (p = root; p; p = p->parent) {
1841
0
        depth++;
1842
0
        if (depth >= MAX_OID_LEN) {
1843
0
            snmp_log(LOG_ERR, "MIB tree depth exceeds MAX_OID_LEN (%d); aborting subtree parsing\n", MAX_OID_LEN);
1844
0
            return;
1845
0
        }
1846
0
    }
1847
1848
0
    while (xroot->next_peer && xroot->next_peer->subid == root->subid) {
1849
#if 0
1850
        printf("xroot: %s.%s => %s\n", xroot->parent->label, xroot->label,
1851
               xroot->next_peer->label);
1852
#endif
1853
0
        xroot = xroot->next_peer;
1854
0
    }
1855
1856
0
    tp = root;
1857
0
    headp = &nbuckets[NBUCKET(name_hash(tp->label))];
1858
    /*
1859
     * Search each of the nodes for one whose parent is root, and
1860
     * move each into a separate list.
1861
     */
1862
0
    for (np = *headp; np; np = np->next) {
1863
0
        if (!label_compare(tp->label, np->parent)) {
1864
            /*
1865
             * take this node out of the node list 
1866
             */
1867
0
            if (oldnp == NULL) {
1868
0
                *headp = np->next;      /* fix root of node list */
1869
0
            } else {
1870
0
                oldnp->next = np->next; /* link around this node */
1871
0
            }
1872
0
            if (child_list)
1873
0
                childp->next = np;
1874
0
            else
1875
0
                child_list = np;
1876
0
            childp = np;
1877
0
        } else {
1878
0
            oldnp = np;
1879
0
        }
1880
1881
0
    }
1882
0
    if (childp)
1883
0
        childp->next = NULL;
1884
0
    if (child_list) {
1885
        /*
1886
         * If this allocation fails, fall back to linear child list scans.
1887
         */
1888
0
        ci = calloc(1, sizeof(*ci));
1889
0
        if (ci) {
1890
0
            ci->parent = xroot;
1891
0
            ci->up = active_child_indexes;
1892
0
            active_child_indexes = ci;
1893
0
        }
1894
0
    }
1895
    /*
1896
     * Take each element in the child list and place it into the tree.
1897
     */
1898
0
    for (np = child_list; np; np = np->next) {
1899
0
        struct tree    *otp = NULL;
1900
0
        struct tree    *xxroot = xroot;
1901
0
        int             indexed = 0;
1902
0
        anon_tp = NULL;
1903
0
        tp = xroot->child_list;
1904
1905
0
        if (np->subid == -1) {
1906
            /*
1907
             * name ::= { parent } 
1908
             */
1909
0
            np->subid = xroot->subid;
1910
0
            tp = xroot;
1911
0
            xxroot = xroot->parent;
1912
0
        } else if (ci) {
1913
0
            if (!ci->valid)
1914
0
                child_index_build(ci);
1915
0
            indexed = ci->valid;
1916
0
        }
1917
1918
0
        if (indexed) {
1919
0
            struct child_entry *ce = child_index_find(ci, np->subid);
1920
1921
0
            if (ce) {
1922
0
                tp = ce->first;
1923
0
                otp = ce->pred;
1924
0
            } else {
1925
0
                tp = NULL;
1926
0
                otp = ci->tail;
1927
0
            }
1928
0
        } else {
1929
0
            while (tp) {
1930
0
                if (tp->subid == np->subid)
1931
0
                    break;
1932
0
                else {
1933
0
                    otp = tp;
1934
0
                    tp = tp->next_peer;
1935
0
                }
1936
0
            }
1937
0
        }
1938
0
        if (tp) {
1939
0
            if (!label_compare(tp->label, np->label)) {
1940
                /*
1941
                 * Update list of modules 
1942
                 */
1943
0
                int_p = malloc((tp->number_modules + 1) * sizeof(int));
1944
0
                if (int_p == NULL)
1945
0
                    goto cleanup;
1946
0
                memcpy(int_p, tp->module_list,
1947
0
                       tp->number_modules * sizeof(int));
1948
0
                int_p[tp->number_modules] = np->modid;
1949
0
                if (tp->module_list != &tp->modid)
1950
0
                    free(tp->module_list);
1951
0
                ++tp->number_modules;
1952
0
                tp->module_list = int_p;
1953
1954
0
                if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
1955
0
             NETSNMP_DS_LIB_MIB_REPLACE)) {
1956
                    /*
1957
                     * Replace from node 
1958
                     */
1959
0
                    tree_from_node(tp, np);
1960
0
                }
1961
                /*
1962
                 * Handle children 
1963
                 */
1964
0
                do_subtree(tp, nodes);
1965
0
                continue;
1966
0
            }
1967
0
            if (!strncmp(np->label, ANON, ANON_LEN) ||
1968
0
                !strncmp(tp->label, ANON, ANON_LEN)) {
1969
0
                anon_tp = tp;   /* Need to merge these two trees later */
1970
0
            } else if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
1971
0
            NETSNMP_DS_LIB_MIB_WARNINGS)) {
1972
0
                snmp_log(LOG_WARNING,
1973
0
                         "Warning: %s.%ld is both %s and %s (%s)\n",
1974
0
                         root->label, np->subid, tp->label, np->label,
1975
0
                         File);
1976
0
      }
1977
0
        }
1978
1979
0
        tp = calloc(1, sizeof(struct tree));
1980
0
        if (tp == NULL)
1981
0
            goto cleanup;
1982
0
        tp->parent = xxroot;
1983
0
        tp->modid = np->modid;
1984
0
        tp->number_modules = 1;
1985
0
        tp->module_list = &(tp->modid);
1986
0
        tree_from_node(tp, np);
1987
0
        if (!otp && !xxroot) {
1988
0
          free_tree(tp);
1989
0
          goto cleanup;
1990
0
        }
1991
0
        tp->next_peer = otp ? otp->next_peer : xxroot->child_list;
1992
0
        if (otp)
1993
0
            otp->next_peer = tp;
1994
0
        else
1995
0
            xxroot->child_list = tp;
1996
0
        child_index_modified(xxroot, ci);
1997
0
        if (indexed) {
1998
0
            struct child_entry *ce = child_index_find(ci, tp->subid);
1999
2000
            /*
2001
             * tp has been inserted in front of the first child with the
2002
             * same subid or has been appended to the child list.
2003
             */
2004
0
            if (ce) {
2005
0
                ce->first = tp;
2006
0
            } else {
2007
0
                ci->tail = tp;
2008
0
                if (child_index_add(ci, tp, otp) < 0)
2009
0
                    child_index_clear(ci);
2010
0
            }
2011
0
        }
2012
0
        hash = NBUCKET(name_hash(tp->label));
2013
0
        tp->next = tbuckets[hash];
2014
0
        tbuckets[hash] = tp;
2015
0
        do_subtree(tp, nodes);
2016
2017
0
        if (anon_tp) {
2018
0
            if (!strncmp(tp->label, ANON, ANON_LEN)) {
2019
                /*
2020
                 * The new node is anonymous,
2021
                 *  so merge it with the existing one.
2022
                 */
2023
0
                merge_anon_children(tp, anon_tp);
2024
2025
                /*
2026
                 * unlink and destroy tp 
2027
                 */
2028
0
                unlink_indexed_tree(ci, tp);
2029
0
                free_tree(tp);
2030
0
            } else if (!strncmp(anon_tp->label, ANON, ANON_LEN)) {
2031
0
                struct tree    *ntp;
2032
                /*
2033
                 * The old node was anonymous,
2034
                 *  so merge it with the existing one,
2035
                 *  and fill in the full information.
2036
                 */
2037
0
                merge_anon_children(anon_tp, tp);
2038
2039
                /*
2040
                 * unlink anon_tp from the hash 
2041
                 */
2042
0
                unlink_tbucket(anon_tp);
2043
2044
                /*
2045
                 * get rid of old contents of anon_tp 
2046
                 */
2047
0
                free_partial_tree(anon_tp, FALSE);
2048
2049
                /*
2050
                 * put in the current information 
2051
                 */
2052
0
                anon_tp->label = tp->label;
2053
0
                child_index_modified(anon_tp, NULL);
2054
0
                anon_tp->child_list = tp->child_list;
2055
0
                anon_tp->modid = tp->modid;
2056
0
                anon_tp->tc_index = tp->tc_index;
2057
0
                anon_tp->type = tp->type;
2058
0
                anon_tp->enums = tp->enums;
2059
0
                anon_tp->indexes = tp->indexes;
2060
0
                anon_tp->augments = tp->augments;
2061
0
                anon_tp->varbinds = tp->varbinds;
2062
0
                anon_tp->ranges = tp->ranges;
2063
0
                anon_tp->hint = tp->hint;
2064
0
                anon_tp->units = tp->units;
2065
0
                anon_tp->description = tp->description;
2066
0
                anon_tp->reference = tp->reference;
2067
0
                anon_tp->defaultValue = tp->defaultValue;
2068
0
                anon_tp->parent = tp->parent;
2069
2070
0
                set_function(anon_tp);
2071
2072
                /*
2073
                 * update parent pointer in moved children 
2074
                 */
2075
0
                ntp = anon_tp->child_list;
2076
0
                while (ntp) {
2077
0
                    ntp->parent = anon_tp;
2078
0
                    ntp = ntp->next_peer;
2079
0
                }
2080
2081
                /*
2082
                 * hash in anon_tp in its new place 
2083
                 */
2084
0
                hash = NBUCKET(name_hash(anon_tp->label));
2085
0
                anon_tp->next = tbuckets[hash];
2086
0
                tbuckets[hash] = anon_tp;
2087
2088
                /*
2089
                 * unlink and destroy tp 
2090
                 */
2091
0
                unlink_tbucket(tp);
2092
0
                unlink_indexed_tree(ci, tp);
2093
0
                free(tp);
2094
0
            } else {
2095
                /*
2096
                 * Uh?  One of these two should have been anonymous! 
2097
                 */
2098
0
                if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
2099
0
               NETSNMP_DS_LIB_MIB_WARNINGS)) {
2100
0
                    snmp_log(LOG_WARNING,
2101
0
                             "Warning: expected anonymous node (either %s or %s) in %s\n",
2102
0
                             tp->label, anon_tp->label, File);
2103
0
    }
2104
0
            }
2105
0
            anon_tp = NULL;
2106
0
        }
2107
0
    }
2108
0
cleanup:
2109
0
    if (ci) {
2110
0
        netsnmp_assert(active_child_indexes == ci);
2111
0
        active_child_indexes = ci->up;
2112
0
        child_index_clear(ci);
2113
0
        free(ci);
2114
0
    }
2115
    /*
2116
     * free all nodes that were copied into tree 
2117
     */
2118
0
    oldnp = NULL;
2119
0
    for (np = child_list; np; np = np->next) {
2120
0
        if (oldnp)
2121
0
            free_node(oldnp);
2122
0
        oldnp = np;
2123
0
    }
2124
0
    if (oldnp)
2125
0
        free_node(oldnp);
2126
0
}
2127
2128
static void
2129
do_linkup(struct module *mp, struct node *np)
2130
0
{
2131
0
    struct module_import *mip;
2132
0
    struct node    *onp, *oldp, *newp;
2133
0
    struct tree    *tp;
2134
0
    int             i, more;
2135
    /*
2136
     * All modules implicitly import
2137
     *   the roots of the tree
2138
     */
2139
0
    if (snmp_get_do_debugging() > 1)
2140
0
        dump_module_list();
2141
0
    DEBUGMSGTL(("parse-mibs", "Processing IMPORTS for module %d %s\n",
2142
0
                mp->modid, mp->name));
2143
0
    if (mp->no_imports == 0) {
2144
0
        mp->no_imports = NUMBER_OF_ROOT_NODES;
2145
0
        mp->imports = root_imports;
2146
0
    }
2147
2148
    /*
2149
     * Build the tree
2150
     */
2151
0
    init_node_hash(np);
2152
0
    for (i = 0, mip = mp->imports; i < mp->no_imports; ++i, ++mip) {
2153
0
        char            modbuf[256];
2154
0
        DEBUGMSGTL(("parse-mibs", "  Processing import: %s\n",
2155
0
                    mip->label));
2156
0
        if (get_tc_index(mip->label, mip->modid) != -1)
2157
0
            continue;
2158
0
        tp = find_tree_node(mip->label, mip->modid);
2159
0
        if (!tp) {
2160
0
      if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_ERRORS))
2161
0
                snmp_log(LOG_WARNING,
2162
0
                         "Did not find '%s' in module %s (%s)\n",
2163
0
                         mip->label, module_name(mip->modid, modbuf),
2164
0
                         File);
2165
0
            continue;
2166
0
        }
2167
0
        do_subtree(tp, &np);
2168
0
    }
2169
2170
    /*
2171
     * If any nodes left over,
2172
     *   check that they're not the result of a "fully qualified"
2173
     *   name, and then add them to the list of orphans
2174
     */
2175
2176
0
    if (!np)
2177
0
        return;
2178
0
    for (tp = tree_head; tp; tp = tp->next_peer)
2179
0
        do_subtree(tp, &np);
2180
0
    if (!np)
2181
0
        return;
2182
2183
    /*
2184
     * quietly move all internal references to the orphan list 
2185
     */
2186
0
    oldp = orphan_nodes;
2187
0
    for (i = 0; i < NHASHSIZE; i++) {
2188
0
        struct node    *next_onp;
2189
0
        for (onp = nbuckets[i]; onp; onp = next_onp) {
2190
0
            struct node    *op = NULL;
2191
0
            int             hash = NBUCKET(name_hash(onp->label));
2192
2193
0
            next_onp = onp->next;
2194
0
            np = nbuckets[hash];
2195
0
            while (np) {
2196
0
                if (label_compare(onp->label, np->parent)) {
2197
0
                    op = np;
2198
0
                    np = np->next;
2199
0
                } else {
2200
0
                    if (np == next_onp)
2201
0
                        next_onp = np->next;
2202
0
                    if (op)
2203
0
                        op->next = np->next;
2204
0
                    else
2205
0
                        nbuckets[hash] = np->next;
2206
0
                    DEBUGMSGTL(("parse-mibs", "Moving %s to orphanage", np->label));
2207
0
                    np->next = orphan_nodes;
2208
0
                    orphan_nodes = np;
2209
0
                    np = op ? op->next : nbuckets[hash];
2210
0
                }
2211
0
            }
2212
0
        }
2213
0
    }
2214
0
    do {
2215
0
        newp = orphan_nodes;
2216
0
        more = 0;
2217
0
        for (onp = orphan_nodes; onp != oldp; onp = onp->next) {
2218
0
            struct node    *op = NULL;
2219
0
            int             hash = NBUCKET(name_hash(onp->label));
2220
2221
0
            np = nbuckets[hash];
2222
0
            while (np) {
2223
0
                if (label_compare(onp->label, np->parent)) {
2224
0
                    op = np;
2225
0
                    np = np->next;
2226
0
                } else {
2227
0
                    if (op)
2228
0
                        op->next = np->next;
2229
0
                    else
2230
0
                        nbuckets[hash] = np->next;
2231
0
                    np->next = orphan_nodes;
2232
0
                    orphan_nodes = np;
2233
0
                    np = op ? op->next : nbuckets[hash];
2234
0
                    more = 1;
2235
0
                }
2236
0
            }
2237
0
        }
2238
0
        oldp = newp;
2239
0
    } while (more);
2240
2241
    /*
2242
     * complain about left over nodes 
2243
     */
2244
0
    for (i = 0; i < NHASHSIZE; i++)
2245
0
        if (nbuckets[i]) {
2246
0
            for (onp = nbuckets[i]; onp; onp = onp->next) {
2247
0
                snmp_log(LOG_WARNING,
2248
0
                         "Cannot resolve OID in %s: %s ::= { %s %ld } at line %d in %s\n",
2249
0
                         (mp->name ? mp->name : "<no module>"),
2250
0
                         (onp->label ? onp->label : "<no label>"),
2251
0
                         (onp->parent ? onp->parent : "<no parent>"),
2252
0
                         onp->subid, onp->lineno, onp->filename);
2253
0
                np = onp;
2254
0
            }
2255
0
            np->next = orphan_nodes;
2256
0
            orphan_nodes = nbuckets[i];
2257
0
            nbuckets[i] = NULL;
2258
0
        }
2259
0
    return;
2260
0
}
2261
2262
2263
/**
2264
 * Read an OID from a file.
2265
 * @param[in]  file   File to read from.
2266
 * @param[out] id_arg Array to store the OID in.
2267
 * @param[in]  length Number of elements in the @id_arg array.
2268
 *
2269
 * Takes a list of the form:
2270
 * { iso org(3) dod(6) 1 }
2271
 * and creates several nodes, one for each parent-child pair.
2272
 * Returns 0 on error.
2273
 */
2274
static int
2275
getoid(FILE * fp, struct subid_s *id_arg, int length)
2276
0
{
2277
0
    struct subid_s *id = id_arg;
2278
0
    int             i, count, type;
2279
0
    char            token[MAXTOKEN];
2280
2281
0
    if ((type = get_token(fp, token, MAXTOKEN)) != LEFTBRACKET) {
2282
0
        print_error("Expected \"{\"", token, type);
2283
0
        return 0;
2284
0
    }
2285
0
    type = get_token(fp, token, MAXTOKEN);
2286
0
    for (count = 0; count < length; count++, id++) {
2287
0
        id->label = NULL;
2288
0
        id->modid = current_module;
2289
0
        id->subid = -1;
2290
0
        if (type == RIGHTBRACKET)
2291
0
            return count;
2292
0
        if (type == LABEL) {
2293
            /*
2294
             * this entry has a label 
2295
             */
2296
0
            id->label = strdup(token);
2297
0
            type = get_token(fp, token, MAXTOKEN);
2298
0
            if (type == LEFTPAREN) {
2299
0
                type = get_token(fp, token, MAXTOKEN);
2300
0
                if (type == NUMBER) {
2301
0
                    id->subid = strtoul(token, NULL, 10);
2302
0
                    if ((type =
2303
0
                         get_token(fp, token, MAXTOKEN)) != RIGHTPAREN) {
2304
0
                        print_error("Expected a closing parenthesis",
2305
0
                                    token, type);
2306
0
                        goto free_labels;
2307
0
                    }
2308
0
                } else {
2309
0
                    print_error("Expected a number", token, type);
2310
0
                    goto free_labels;
2311
0
                }
2312
0
            } else {
2313
0
                continue;
2314
0
            }
2315
0
        } else if (type == NUMBER) {
2316
            /*
2317
             * this entry  has just an integer sub-identifier 
2318
             */
2319
0
            id->subid = strtoul(token, NULL, 10);
2320
0
        } else {
2321
0
            print_error("Expected label or number", token, type);
2322
0
            goto free_labels;
2323
0
        }
2324
0
        type = get_token(fp, token, MAXTOKEN);
2325
0
    }
2326
0
    print_error("Too long OID", token, type);
2327
0
    --count;
2328
2329
0
free_labels:
2330
0
    for (i = 0; i <= count; i++) {
2331
0
        free(id_arg[i].label);
2332
0
        id_arg[i].label = NULL;
2333
0
    }
2334
2335
0
    return 0;
2336
0
}
2337
2338
/*
2339
 * Parse a sequence of object subidentifiers for the given name.
2340
 * The "label OBJECT IDENTIFIER ::=" portion has already been parsed.
2341
 *
2342
 * The majority of cases take this form :
2343
 * label OBJECT IDENTIFIER ::= { parent 2 }
2344
 * where a parent label and a child subidentifier number are specified.
2345
 *
2346
 * Variations on the theme include cases where a number appears with
2347
 * the parent, or intermediate subidentifiers are specified by label,
2348
 * by number, or both.
2349
 *
2350
 * Here are some representative samples :
2351
 * internet        OBJECT IDENTIFIER ::= { iso org(3) dod(6) 1 }
2352
 * mgmt            OBJECT IDENTIFIER ::= { internet 2 }
2353
 * rptrInfoHealth  OBJECT IDENTIFIER ::= { snmpDot3RptrMgt 0 4 }
2354
 *
2355
 * Here is a very rare form :
2356
 * iso             OBJECT IDENTIFIER ::= { 1 }
2357
 *
2358
 * Returns NULL on error.  When this happens, memory may be leaked.
2359
 */
2360
static struct node *
2361
parse_objectid(FILE * fp, char *name)
2362
0
{
2363
0
    register int    count;
2364
0
    register struct subid_s *op, *nop;
2365
0
    int             length;
2366
0
    struct subid_s  loid[32];
2367
0
    struct node    *np, *root = NULL, *oldnp = NULL;
2368
0
    struct tree    *tp;
2369
2370
0
    if ((length = getoid(fp, loid, 32)) == 0) {
2371
0
        print_error("Bad object identifier", NULL, CONTINUE);
2372
0
        return NULL;
2373
0
    }
2374
2375
    /*
2376
     * Handle numeric-only object identifiers,
2377
     *  by labeling the first sub-identifier
2378
     */
2379
0
    op = loid;
2380
0
    if (!op->label) {
2381
0
        if (length == 1) {
2382
0
            print_error("Attempt to define a root oid", name, OBJECT);
2383
0
            return NULL;
2384
0
        }
2385
0
        for (tp = tree_head; tp; tp = tp->next_peer)
2386
0
            if ((int) tp->subid == op->subid) {
2387
0
                op->label = strdup(tp->label);
2388
0
                break;
2389
0
            }
2390
0
    }
2391
2392
    /*
2393
     * Handle  "label OBJECT-IDENTIFIER ::= { subid }"
2394
     */
2395
0
    if (length == 1) {
2396
0
        op = loid;
2397
0
        np = alloc_node(op->modid);
2398
0
        if (np == NULL)
2399
0
            return (NULL);
2400
0
        np->subid = op->subid;
2401
0
        np->label = strdup(name);
2402
0
        np->parent = op->label;
2403
0
        return np;
2404
0
    }
2405
2406
    /*
2407
     * For each parent-child subid pair in the subid array,
2408
     * create a node and link it into the node list.
2409
     */
2410
0
    for (count = 0, op = loid, nop = loid + 1; count < (length - 1);
2411
0
         count++, op++, nop++) {
2412
        /*
2413
         * every node must have parent's name and child's name or number 
2414
         */
2415
        /*
2416
         * XX the next statement is always true -- does it matter ?? 
2417
         */
2418
0
        if (op->label && (nop->label || (nop->subid != -1))) {
2419
0
            np = alloc_node(nop->modid);
2420
0
            if (np == NULL)
2421
0
                goto err;
2422
0
            if (root == NULL) {
2423
0
                root = np;
2424
0
            } else {
2425
0
                netsnmp_assert(oldnp);
2426
0
                oldnp->next = np;
2427
0
            }
2428
0
            oldnp = np;
2429
2430
0
            np->parent = strdup(op->label);
2431
0
            if (count == (length - 2)) {
2432
                /*
2433
                 * The name for this node is the label for this entry 
2434
                 */
2435
0
                np->label = strdup(name);
2436
0
                if (np->label == NULL)
2437
0
                    goto err;
2438
0
            } else {
2439
0
                if (!nop->label) {
2440
0
                    if (asprintf(&nop->label, "%s%d", ANON, anonymous++) < 0)
2441
0
                        goto err;
2442
0
                }
2443
0
                np->label = strdup(nop->label);
2444
0
            }
2445
0
            if (nop->subid != -1)
2446
0
                np->subid = nop->subid;
2447
0
            else
2448
0
                print_error("Warning: This entry is pretty silly",
2449
0
                            np->label, CONTINUE);
2450
0
        }                       /* end if(op->label... */
2451
0
    }
2452
2453
0
out:
2454
    /*
2455
     * free the loid array 
2456
     */
2457
0
    for (count = 0, op = loid; count < length; count++, op++) {
2458
0
        free(op->label);
2459
0
        op->label = NULL;
2460
0
    }
2461
2462
0
    return root;
2463
2464
0
err:
2465
0
    for (; root; root = np) {
2466
0
        np = root->next;
2467
0
        free_node(root);
2468
0
    }
2469
0
    goto out;
2470
0
}
2471
2472
static int
2473
get_tc(const char *descriptor,
2474
       int modid,
2475
       int *tc_index,
2476
       struct enum_list **ep, struct range_list **rp, char **hint)
2477
0
{
2478
0
    int             i;
2479
0
    struct tc      *tcp;
2480
2481
0
    i = get_tc_index(descriptor, modid);
2482
0
    if (tc_index)
2483
0
        *tc_index = i;
2484
0
    if (i != -1) {
2485
0
        tcp = &tclist[i];
2486
0
        if (ep) {
2487
0
            free_enums(ep);
2488
0
            *ep = copy_enums(tcp->enums);
2489
0
        }
2490
0
        if (rp) {
2491
0
            free_ranges(rp);
2492
0
            *rp = copy_ranges(tcp->ranges);
2493
0
        }
2494
0
        if (hint) {
2495
0
            if (*hint)
2496
0
                free(*hint);
2497
0
            *hint = (tcp->hint ? strdup(tcp->hint) : NULL);
2498
0
        }
2499
0
        return tcp->type;
2500
0
    }
2501
0
    return LABEL;
2502
0
}
2503
2504
/*
2505
 * return index into tclist of given TC descriptor
2506
 * return -1 if not found
2507
 */
2508
static int
2509
get_tc_index(const char *descriptor, int modid)
2510
0
{
2511
0
    int             i;
2512
0
    struct tc      *tcp;
2513
0
    struct module  *mp;
2514
0
    struct module_import *mip;
2515
2516
    /*
2517
     * Check that the descriptor isn't imported
2518
     *  by searching the import list
2519
     */
2520
2521
0
    for (mp = module_head; mp; mp = mp->next)
2522
0
        if (mp->modid == modid)
2523
0
            break;
2524
0
    if (mp)
2525
0
        for (i = 0, mip = mp->imports; i < mp->no_imports; ++i, ++mip) {
2526
0
            if (!label_compare(mip->label, descriptor)) {
2527
                /*
2528
                 * Found it - so amend the module ID 
2529
                 */
2530
0
                modid = mip->modid;
2531
0
                break;
2532
0
            }
2533
0
        }
2534
2535
2536
0
    for (i = 0, tcp = tclist; i < tc_alloc; i++, tcp++) {
2537
0
        if (tcp->type == 0)
2538
0
            break;
2539
0
        if (!label_compare(descriptor, tcp->descriptor) &&
2540
0
            ((modid == tcp->modid) || (modid == -1))) {
2541
0
            return i;
2542
0
        }
2543
0
    }
2544
0
    return -1;
2545
0
}
2546
2547
/*
2548
 * translate integer tc_index to string identifier from tclist
2549
 * *
2550
 * * Returns pointer to string in table (should not be modified) or NULL
2551
 */
2552
const char     *
2553
get_tc_descriptor(int tc_index)
2554
0
{
2555
0
    if (tc_index < 0 || tc_index >= tc_alloc)
2556
0
        return NULL;
2557
0
    return tclist[tc_index].descriptor;
2558
0
}
2559
2560
#ifndef NETSNMP_FEATURE_REMOVE_GET_TC_DESCRIPTION
2561
/* used in the perl module */
2562
const char     *
2563
get_tc_description(int tc_index)
2564
0
{
2565
0
    if (tc_index < 0 || tc_index >= tc_alloc)
2566
0
        return NULL;
2567
0
    return tclist[tc_index].description;
2568
0
}
2569
#endif /* NETSNMP_FEATURE_REMOVE_GET_TC_DESCRIPTION */
2570
2571
2572
/*
2573
 * Parses an enumeration list of the form:
2574
 *        { label(value) label(value) ... }
2575
 * The initial { has already been parsed.
2576
 * Returns NULL on error.
2577
 */
2578
2579
static struct enum_list *
2580
parse_enumlist(FILE * fp, struct enum_list **retp)
2581
0
{
2582
0
    register int    type;
2583
0
    char            token[MAXTOKEN];
2584
0
    struct enum_list *ep = NULL, **epp = &ep;
2585
2586
0
    free_enums(retp);
2587
2588
0
    while ((type = get_token(fp, token, MAXTOKEN)) != ENDOFFILE) {
2589
0
        if (type == RIGHTBRACKET)
2590
0
            break;
2591
        /* some enums use "deprecated" to indicate a no longer value label */
2592
        /* (EG: IP-MIB's IpAddressStatusTC) */
2593
0
        if (type == LABEL || type == DEPRECATED) {
2594
            /*
2595
             * this is an enumerated label 
2596
             */
2597
0
            *epp = calloc(1, sizeof(struct enum_list));
2598
0
            if (*epp == NULL)
2599
0
                return (NULL);
2600
            /*
2601
             * a reasonable approximation for the length 
2602
             */
2603
0
            (*epp)->label = strdup(token);
2604
0
            type = get_token(fp, token, MAXTOKEN);
2605
0
            if (type != LEFTPAREN) {
2606
0
                print_error("Expected \"(\"", token, type);
2607
0
                goto err;
2608
0
            }
2609
0
            type = get_token(fp, token, MAXTOKEN);
2610
0
            if (type != NUMBER) {
2611
0
                print_error("Expected integer", token, type);
2612
0
                goto err;
2613
0
            }
2614
0
            (*epp)->value = strtol(token, NULL, 10);
2615
0
            (*epp)->lineno = mibLine;
2616
0
            type = get_token(fp, token, MAXTOKEN);
2617
0
            if (type != RIGHTPAREN) {
2618
0
                print_error("Expected \")\"", token, type);
2619
0
                goto err;
2620
0
            } else {
2621
0
                struct enum_list *op = ep;
2622
0
                while (op != *epp) {
2623
0
                    if (strcmp((*epp)->label, op->label) == 0) {
2624
0
                        snmp_log(LOG_ERR,
2625
0
                            "Duplicate enum label '%s' at line %d in %s. First at line %d\n",
2626
0
                            (*epp)->label, mibLine, File, op->lineno);
2627
0
                        erroneousMibs++;
2628
0
                        break;
2629
0
                    }
2630
0
                    else if ((*epp)->value == op->value) {
2631
0
                        snmp_log(LOG_ERR,
2632
0
                            "Duplicate enum value '%d' at line %d in %s. First at line %d\n",
2633
0
                            (*epp)->value, mibLine, File, op->lineno);
2634
0
                        erroneousMibs++;
2635
0
                        break;
2636
0
                    }
2637
0
                    op = op->next;
2638
0
                }
2639
0
            }
2640
0
            epp = &(*epp)->next;
2641
0
        }
2642
0
    }
2643
0
    if (type == ENDOFFILE) {
2644
0
        print_error("Expected \"}\"", token, type);
2645
0
        goto err;
2646
0
    }
2647
0
    *retp = ep;
2648
0
    return ep;
2649
2650
0
err:
2651
0
    free_enums(&ep);
2652
0
    return NULL;
2653
0
}
2654
2655
static struct range_list *
2656
parse_ranges(FILE * fp, struct range_list **retp)
2657
0
{
2658
0
    int             low, high;
2659
0
    char            nexttoken[MAXTOKEN];
2660
0
    int             nexttype;
2661
0
    struct range_list *rp = NULL, **rpp = &rp;
2662
0
    int             size = 0, taken = 1;
2663
2664
0
    free_ranges(retp);
2665
2666
0
    nexttype = get_token(fp, nexttoken, MAXTOKEN);
2667
0
    if (nexttype == SIZE) {
2668
0
        size = 1;
2669
0
        taken = 0;
2670
0
        nexttype = get_token(fp, nexttoken, MAXTOKEN);
2671
0
        if (nexttype != LEFTPAREN)
2672
0
            print_error("Expected \"(\" after SIZE", nexttoken, nexttype);
2673
0
    }
2674
2675
0
    do {
2676
0
        if (!taken)
2677
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2678
0
        else
2679
0
            taken = 0;
2680
0
        high = low = strtoul(nexttoken, NULL, 10);
2681
0
        nexttype = get_token(fp, nexttoken, MAXTOKEN);
2682
0
        if (nexttype == RANGE) {
2683
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2684
0
            errno = 0;
2685
0
            high = strtoul(nexttoken, NULL, 10);
2686
0
            if ( errno == ERANGE ) {
2687
0
                if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
2688
0
                                       NETSNMP_DS_LIB_MIB_WARNINGS))
2689
0
                    snmp_log(LOG_WARNING,
2690
0
                             "Warning: Upper bound not handled correctly (%s != %d): At line %d in %s\n",
2691
0
                                 nexttoken, high, mibLine, File);
2692
0
            }
2693
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2694
0
        }
2695
0
        *rpp = (struct range_list *) calloc(1, sizeof(struct range_list));
2696
0
        if (*rpp == NULL)
2697
0
            break;
2698
0
        (*rpp)->low = low;
2699
0
        (*rpp)->high = high;
2700
0
        rpp = &(*rpp)->next;
2701
2702
0
    } while (nexttype == BAR);
2703
0
    if (size) {
2704
0
        if (nexttype != RIGHTPAREN)
2705
0
            print_error("Expected \")\" after SIZE", nexttoken, nexttype);
2706
0
        nexttype = get_token(fp, nexttoken, MAXTOKEN);
2707
0
    }
2708
0
    if (nexttype != RIGHTPAREN)
2709
0
        print_error("Expected \")\"", nexttoken, nexttype);
2710
2711
0
    *retp = rp;
2712
0
    return rp;
2713
0
}
2714
2715
/*
2716
 * Parses an asn type.  Structures are ignored by this parser.
2717
 * Returns NULL on error.
2718
 */
2719
static struct node *
2720
parse_asntype(FILE * fp, char *name, int *ntype, char *ntoken)
2721
0
{
2722
0
    int             type, i;
2723
0
    char            token[MAXTOKEN];
2724
0
    char            quoted_string_buffer[MAXQUOTESTR];
2725
0
    char           *hint = NULL;
2726
0
    char           *descr = NULL;
2727
0
    struct tc      *tcp;
2728
0
    int             level;
2729
0
    int             is_duplicate = 0;
2730
2731
0
    type = get_token(fp, token, MAXTOKEN);
2732
0
    if (type == SEQUENCE || type == CHOICE) {
2733
0
        level = 0;
2734
0
        while ((type = get_token(fp, token, MAXTOKEN)) != ENDOFFILE) {
2735
0
            if (type == LEFTBRACKET) {
2736
0
                level++;
2737
0
            } else if (type == RIGHTBRACKET && --level == 0) {
2738
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2739
0
                return NULL;
2740
0
            }
2741
0
        }
2742
0
        print_error("Expected \"}\"", token, type);
2743
0
        return NULL;
2744
0
    } else if (type == LEFTBRACKET) {
2745
0
        struct node    *np;
2746
0
        int             ch_next = '{';
2747
0
        ungetc(ch_next, fp);
2748
0
        np = parse_objectid(fp, name);
2749
0
        if (np != NULL) {
2750
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2751
0
            return np;
2752
0
        }
2753
0
        return NULL;
2754
0
    } else if (type == LEFTSQBRACK) {
2755
0
        int             size = 0;
2756
0
        do {
2757
0
            type = get_token(fp, token, MAXTOKEN);
2758
0
        } while (type != ENDOFFILE && type != RIGHTSQBRACK);
2759
0
        if (type != RIGHTSQBRACK) {
2760
0
            print_error("Expected \"]\"", token, type);
2761
0
            return NULL;
2762
0
        }
2763
0
        type = get_token(fp, token, MAXTOKEN);
2764
0
        if (type == IMPLICIT)
2765
0
            type = get_token(fp, token, MAXTOKEN);
2766
0
        *ntype = get_token(fp, ntoken, MAXTOKEN);
2767
0
        if (*ntype == LEFTPAREN) {
2768
0
            switch (type) {
2769
0
            case OCTETSTR:
2770
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2771
0
                if (*ntype != SIZE) {
2772
0
                    print_error("Expected SIZE", ntoken, *ntype);
2773
0
                    return NULL;
2774
0
                }
2775
0
                size = 1;
2776
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2777
0
                if (*ntype != LEFTPAREN) {
2778
0
                    print_error("Expected \"(\" after SIZE", ntoken,
2779
0
                                *ntype);
2780
0
                    return NULL;
2781
0
                }
2782
0
                NETSNMP_FALLTHROUGH;
2783
0
            case INTEGER:
2784
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2785
0
                do {
2786
0
                    if (*ntype != NUMBER)
2787
0
                        print_error("Expected NUMBER", ntoken, *ntype);
2788
0
                    *ntype = get_token(fp, ntoken, MAXTOKEN);
2789
0
                    if (*ntype == RANGE) {
2790
0
                        *ntype = get_token(fp, ntoken, MAXTOKEN);
2791
0
                        if (*ntype != NUMBER)
2792
0
                            print_error("Expected NUMBER", ntoken, *ntype);
2793
0
                        *ntype = get_token(fp, ntoken, MAXTOKEN);
2794
0
                    }
2795
0
                } while (*ntype == BAR);
2796
0
                if (*ntype != RIGHTPAREN) {
2797
0
                    print_error("Expected \")\"", ntoken, *ntype);
2798
0
                    return NULL;
2799
0
                }
2800
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2801
0
                if (size) {
2802
0
                    if (*ntype != RIGHTPAREN) {
2803
0
                        print_error("Expected \")\" to terminate SIZE",
2804
0
                                    ntoken, *ntype);
2805
0
                        return NULL;
2806
0
                    }
2807
0
                    *ntype = get_token(fp, ntoken, MAXTOKEN);
2808
0
                }
2809
0
            }
2810
0
        }
2811
0
        return NULL;
2812
0
    } else {
2813
0
        if (type == CONVENTION) {
2814
0
            while (type != SYNTAX && type != ENDOFFILE) {
2815
0
                if (type == DISPLAYHINT) {
2816
0
                    type = get_token(fp, token, MAXTOKEN);
2817
0
                    if (type != QUOTESTRING) {
2818
0
                        print_error("DISPLAY-HINT must be string", token,
2819
0
                                    type);
2820
0
                    } else {
2821
0
                        free(hint);
2822
0
                        hint = strdup(token);
2823
0
                    }
2824
0
                } else if (type == DESCRIPTION &&
2825
0
                           netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
2826
0
                                                  NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
2827
0
                    type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
2828
0
                    if (type != QUOTESTRING) {
2829
0
                        print_error("DESCRIPTION must be string", token,
2830
0
                                    type);
2831
0
                    } else {
2832
0
                        free(descr);
2833
0
                        descr = strdup(quoted_string_buffer);
2834
0
                    }
2835
0
                } else
2836
0
                    type =
2837
0
                        get_token(fp, quoted_string_buffer, MAXQUOTESTR);
2838
0
            }
2839
0
            type = get_token(fp, token, MAXTOKEN);
2840
0
            if (type == OBJECT) {
2841
0
                type = get_token(fp, token, MAXTOKEN);
2842
0
                if (type != IDENTIFIER) {
2843
0
                    print_error("Expected IDENTIFIER", token, type);
2844
0
                    goto err;
2845
0
                }
2846
0
                type = OBJID;
2847
0
            }
2848
0
        } else if (type == OBJECT) {
2849
0
            type = get_token(fp, token, MAXTOKEN);
2850
0
            if (type != IDENTIFIER) {
2851
0
                print_error("Expected IDENTIFIER", token, type);
2852
0
                goto err;
2853
0
            }
2854
0
            type = OBJID;
2855
0
        }
2856
2857
0
        if (type == LABEL) {
2858
0
            type = get_tc(token, current_module, NULL, NULL, NULL, NULL);
2859
0
        }
2860
2861
        /*
2862
         * textual convention 
2863
         */
2864
0
        tcp = NULL;
2865
0
        is_duplicate = 0;
2866
0
        for (i = 0; i < tc_alloc; i++) {
2867
0
            if (tclist[i].type == 0) {
2868
0
                if (tcp == NULL)
2869
0
                    tcp = &tclist[i];
2870
0
            } else if (strcmp(name, tclist[i].descriptor) == 0 &&
2871
0
                       tclist[i].modid == current_module) {
2872
0
                snmp_log(LOG_ERR,
2873
0
                         "Duplicate TEXTUAL-CONVENTION '%s' at line %d in %s. First at line %d\n",
2874
0
                         name, mibLine, File, tclist[i].lineno);
2875
0
                erroneousMibs++;
2876
0
                is_duplicate = 1;
2877
0
                break;
2878
0
            }
2879
0
        }
2880
2881
0
        if (is_duplicate) {
2882
0
            struct range_list *dummy_ranges = NULL;
2883
0
            struct enum_list *dummy_enums = NULL;
2884
2885
0
            free(hint);
2886
0
            free(descr);
2887
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2888
0
            if (*ntype == LEFTPAREN) {
2889
0
                dummy_ranges = parse_ranges(fp, &dummy_ranges);
2890
0
                free_ranges(&dummy_ranges);
2891
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2892
0
            } else if (*ntype == LEFTBRACKET) {
2893
0
                dummy_enums = parse_enumlist(fp, &dummy_enums);
2894
0
                free_enums(&dummy_enums);
2895
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2896
0
            }
2897
0
            return NULL;
2898
0
        }
2899
2900
0
        if (tcp == NULL) {
2901
0
            tclist = realloc(tclist, (tc_alloc + TC_INCR)*sizeof(struct tc));
2902
0
            memset(tclist+tc_alloc, 0, TC_INCR*sizeof(struct tc));
2903
0
            tcp = tclist + tc_alloc;
2904
0
            tc_alloc += TC_INCR;
2905
0
        }
2906
0
        if (!(type & SYNTAX_MASK)) {
2907
0
            print_error("Textual convention doesn't map to real type",
2908
0
                        token, type);
2909
0
            goto err;
2910
0
        }
2911
0
        tcp->modid = current_module;
2912
0
        tcp->descriptor = strdup(name);
2913
0
        tcp->hint = hint;
2914
0
        tcp->description = descr;
2915
0
        tcp->lineno = mibLine;
2916
0
        tcp->type = type;
2917
0
        *ntype = get_token(fp, ntoken, MAXTOKEN);
2918
0
        if (*ntype == LEFTPAREN) {
2919
0
            tcp->ranges = parse_ranges(fp, &tcp->ranges);
2920
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2921
0
        } else if (*ntype == LEFTBRACKET) {
2922
            /*
2923
             * if there is an enumeration list, parse it 
2924
             */
2925
0
            tcp->enums = parse_enumlist(fp, &tcp->enums);
2926
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2927
0
        }
2928
0
        return NULL;
2929
0
    }
2930
2931
0
err:
2932
0
    SNMP_FREE(descr);
2933
0
    SNMP_FREE(hint);
2934
0
    return NULL;
2935
0
}
2936
2937
2938
/*
2939
 * Parses an OBJECT TYPE macro.
2940
 * Returns 0 on error.
2941
 */
2942
static struct node *
2943
parse_objecttype(FILE * fp, char *name)
2944
0
{
2945
0
    register int    type;
2946
0
    char            token[MAXTOKEN];
2947
0
    char            nexttoken[MAXTOKEN];
2948
0
    char            quoted_string_buffer[MAXQUOTESTR];
2949
0
    int             nexttype, tctype;
2950
0
    register struct node *np;
2951
2952
0
    type = get_token(fp, token, MAXTOKEN);
2953
0
    if (type != SYNTAX) {
2954
0
        print_error("Bad format for OBJECT-TYPE", token, type);
2955
0
        return NULL;
2956
0
    }
2957
0
    np = alloc_node(current_module);
2958
0
    if (np == NULL)
2959
0
        return (NULL);
2960
0
    type = get_token(fp, token, MAXTOKEN);
2961
0
    if (type == OBJECT) {
2962
0
        type = get_token(fp, token, MAXTOKEN);
2963
0
        if (type != IDENTIFIER) {
2964
0
            print_error("Expected IDENTIFIER", token, type);
2965
0
            free_node(np);
2966
0
            return NULL;
2967
0
        }
2968
0
        type = OBJID;
2969
0
    }
2970
0
    if (type == LABEL) {
2971
0
        int             tmp_index;
2972
0
        tctype = get_tc(token, current_module, &tmp_index,
2973
0
                        &np->enums, &np->ranges, &np->hint);
2974
0
        if (tctype == LABEL &&
2975
0
            netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
2976
0
             NETSNMP_DS_LIB_MIB_WARNINGS) > 1) {
2977
0
            print_error("Warning: No known translation for type", token,
2978
0
                        type);
2979
0
        }
2980
0
        type = tctype;
2981
0
        np->tc_index = tmp_index;       /* store TC for later reference */
2982
0
    }
2983
0
    np->type = type;
2984
0
    nexttype = get_token(fp, nexttoken, MAXTOKEN);
2985
0
    switch (type) {
2986
0
    case SEQUENCE:
2987
0
        if (nexttype == OF) {
2988
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2989
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2990
2991
0
        }
2992
0
        break;
2993
0
    case INTEGER:
2994
0
    case INTEGER32:
2995
0
    case UINTEGER32:
2996
0
    case UNSIGNED32:
2997
0
    case COUNTER:
2998
0
    case GAUGE:
2999
0
    case BITSTRING:
3000
0
    case LABEL:
3001
0
        if (nexttype == LEFTBRACKET) {
3002
            /*
3003
             * if there is an enumeration list, parse it 
3004
             */
3005
0
            np->enums = parse_enumlist(fp, &np->enums);
3006
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
3007
0
        } else if (nexttype == LEFTPAREN) {
3008
            /*
3009
             * if there is a range list, parse it 
3010
             */
3011
0
            np->ranges = parse_ranges(fp, &np->ranges);
3012
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
3013
0
        }
3014
0
        break;
3015
0
    case OCTETSTR:
3016
0
    case KW_OPAQUE:
3017
        /*
3018
         * parse any SIZE specification 
3019
         */
3020
0
        if (nexttype == LEFTPAREN) {
3021
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
3022
0
            if (nexttype == SIZE) {
3023
0
                nexttype = get_token(fp, nexttoken, MAXTOKEN);
3024
0
                if (nexttype == LEFTPAREN) {
3025
0
                    np->ranges = parse_ranges(fp, &np->ranges);
3026
0
                    nexttype = get_token(fp, nexttoken, MAXTOKEN);      /* ) */
3027
0
                    if (nexttype == RIGHTPAREN) {
3028
0
                        nexttype = get_token(fp, nexttoken, MAXTOKEN);
3029
0
                        break;
3030
0
                    }
3031
0
                }
3032
0
            }
3033
0
            print_error("Bad SIZE syntax", token, type);
3034
0
            free_node(np);
3035
0
            return NULL;
3036
0
        }
3037
0
        break;
3038
0
    case OBJID:
3039
0
    case NETADDR:
3040
0
    case IPADDR:
3041
0
    case TIMETICKS:
3042
0
    case NUL:
3043
0
    case NSAPADDRESS:
3044
0
    case COUNTER64:
3045
0
        break;
3046
0
    default:
3047
0
        print_error("Bad syntax", token, type);
3048
0
        free_node(np);
3049
0
        return NULL;
3050
0
    }
3051
0
    if (nexttype == UNITS) {
3052
0
        type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3053
0
        if (type != QUOTESTRING) {
3054
0
            print_error("Bad UNITS", quoted_string_buffer, type);
3055
0
            free_node(np);
3056
0
            return NULL;
3057
0
        }
3058
0
        np->units = strdup(quoted_string_buffer);
3059
0
        nexttype = get_token(fp, nexttoken, MAXTOKEN);
3060
0
    }
3061
0
    if (nexttype != ACCESS) {
3062
0
        print_error("Should be ACCESS", nexttoken, nexttype);
3063
0
        free_node(np);
3064
0
        return NULL;
3065
0
    }
3066
0
    type = get_token(fp, token, MAXTOKEN);
3067
0
    if (type != READONLY && type != READWRITE && type != WRITEONLY
3068
0
        && type != NOACCESS && type != READCREATE && type != ACCNOTIFY) {
3069
0
        print_error("Bad ACCESS type", token, type);
3070
0
        free_node(np);
3071
0
        return NULL;
3072
0
    }
3073
0
    np->access = type;
3074
0
    type = get_token(fp, token, MAXTOKEN);
3075
0
    if (type != STATUS) {
3076
0
        print_error("Should be STATUS", token, type);
3077
0
        free_node(np);
3078
0
        return NULL;
3079
0
    }
3080
0
    type = get_token(fp, token, MAXTOKEN);
3081
0
    if (type != MANDATORY && type != CURRENT && type != KW_OPTIONAL &&
3082
0
        type != OBSOLETE && type != DEPRECATED) {
3083
0
        print_error("Bad STATUS", token, type);
3084
0
        free_node(np);
3085
0
        return NULL;
3086
0
    }
3087
0
    np->status = type;
3088
    /*
3089
     * Optional parts of the OBJECT-TYPE macro
3090
     */
3091
0
    type = get_token(fp, token, MAXTOKEN);
3092
0
    while (type != EQUALS && type != ENDOFFILE) {
3093
0
        switch (type) {
3094
0
        case DESCRIPTION:
3095
0
            type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3096
3097
0
            if (type != QUOTESTRING) {
3098
0
                print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3099
0
                free_node(np);
3100
0
                return NULL;
3101
0
            }
3102
0
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
3103
0
               NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
3104
0
                free(np->description);
3105
0
                np->description = strdup(quoted_string_buffer);
3106
0
            }
3107
0
            break;
3108
3109
0
        case REFERENCE:
3110
0
            type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3111
0
            if (type != QUOTESTRING) {
3112
0
                print_error("Bad REFERENCE", quoted_string_buffer, type);
3113
0
                free_node(np);
3114
0
                return NULL;
3115
0
            }
3116
0
            free(np->reference);
3117
0
            np->reference = strdup(quoted_string_buffer);
3118
0
            break;
3119
0
        case INDEX:
3120
0
            if (np->augments) {
3121
0
                print_error("Cannot have both INDEX and AUGMENTS", token,
3122
0
                            type);
3123
0
                free_node(np);
3124
0
                return NULL;
3125
0
            }
3126
0
            np->indexes = getIndexes(fp, &np->indexes);
3127
0
            if (np->indexes == NULL) {
3128
0
                print_error("Bad INDEX list", token, type);
3129
0
                free_node(np);
3130
0
                return NULL;
3131
0
            }
3132
0
            break;
3133
0
        case AUGMENTS:
3134
0
            if (np->indexes) {
3135
0
                print_error("Cannot have both INDEX and AUGMENTS", token,
3136
0
                            type);
3137
0
                free_node(np);
3138
0
                return NULL;
3139
0
            }
3140
0
            np->indexes = getIndexes(fp, &np->indexes);
3141
0
            if (np->indexes == NULL) {
3142
0
                print_error("Bad AUGMENTS list", token, type);
3143
0
                free_node(np);
3144
0
                return NULL;
3145
0
            }
3146
0
            free(np->augments);
3147
0
            np->augments = strdup(np->indexes->ilabel);
3148
0
            free_indexes(&np->indexes);
3149
0
            break;
3150
0
        case DEFVAL:
3151
            /*
3152
             * Mark's defVal section 
3153
             */
3154
0
            type = get_token(fp, quoted_string_buffer,
3155
0
                             sizeof(quoted_string_buffer));
3156
0
            if (type != LEFTBRACKET) {
3157
0
                print_error("Bad DEFAULTVALUE", quoted_string_buffer,
3158
0
                            type);
3159
0
                free_node(np);
3160
0
                return NULL;
3161
0
            }
3162
3163
0
            {
3164
0
                int             level = 1;
3165
0
                char            defbuf[512];
3166
3167
0
                defbuf[0] = 0;
3168
0
                while (1) {
3169
0
                    type = get_token(fp, quoted_string_buffer,
3170
0
                                     sizeof(quoted_string_buffer));
3171
0
                    if ((type == RIGHTBRACKET && --level == 0)
3172
0
                        || type == ENDOFFILE)
3173
0
                        break;
3174
0
                    else if (type == LEFTBRACKET)
3175
0
                        level++;
3176
0
                    if (type == QUOTESTRING)
3177
0
                        strlcat(defbuf, "\\\"", sizeof(defbuf));
3178
0
                    strlcat(defbuf, quoted_string_buffer, sizeof(defbuf));
3179
0
                    if (type == QUOTESTRING)
3180
0
                        strlcat(defbuf, "\\\"", sizeof(defbuf));
3181
0
                    strlcat(defbuf, " ", sizeof(defbuf));
3182
0
                }
3183
3184
0
                if (type != RIGHTBRACKET) {
3185
0
                    print_error("Bad DEFAULTVALUE", quoted_string_buffer,
3186
0
                                type);
3187
0
                    free_node(np);
3188
0
                    return NULL;
3189
0
                }
3190
3191
                /*
3192
                 * Ensure strlen(defbuf) is above zero
3193
                 */
3194
0
                if (strlen(defbuf) == 0) {
3195
0
                    print_error("Bad DEFAULTVALUE", quoted_string_buffer,
3196
0
                                type);
3197
0
                    free_node(np);
3198
0
                    return NULL;
3199
0
                }
3200
0
                defbuf[strlen(defbuf) - 1] = 0;
3201
0
                free(np->defaultValue);
3202
0
                np->defaultValue = strdup(defbuf);
3203
0
            }
3204
3205
0
            break;
3206
3207
0
        case NUM_ENTRIES:
3208
0
            if (tossObjectIdentifier(fp) != OBJID) {
3209
0
                print_error("Bad Object Identifier", token, type);
3210
0
                free_node(np);
3211
0
                return NULL;
3212
0
            }
3213
0
            break;
3214
3215
0
        default:
3216
0
            print_error("Bad format of optional clauses", token, type);
3217
0
            free_node(np);
3218
0
            return NULL;
3219
3220
0
        }
3221
0
        type = get_token(fp, token, MAXTOKEN);
3222
0
    }
3223
0
    if (type != EQUALS) {
3224
0
        print_error("Bad format", token, type);
3225
0
        free_node(np);
3226
0
        return NULL;
3227
0
    }
3228
0
    return merge_parse_objectid(np, fp, name);
3229
0
}
3230
3231
/*
3232
 * Parses an OBJECT GROUP macro.
3233
 * Returns 0 on error.
3234
 *
3235
 * Also parses object-identity, since they are similar (ignore STATUS).
3236
 *   - WJH 10/96
3237
 */
3238
static struct node *
3239
parse_objectgroup(FILE * fp, char *name, int what, struct objgroup **ol)
3240
0
{
3241
0
    int             type;
3242
0
    char            token[MAXTOKEN];
3243
0
    char            quoted_string_buffer[MAXQUOTESTR];
3244
0
    struct node    *np;
3245
3246
0
    np = alloc_node(current_module);
3247
0
    if (np == NULL)
3248
0
        return (NULL);
3249
0
    type = get_token(fp, token, MAXTOKEN);
3250
0
    if (type == what) {
3251
0
        type = get_token(fp, token, MAXTOKEN);
3252
0
        if (type != LEFTBRACKET) {
3253
0
            print_error("Expected \"{\"", token, type);
3254
0
            goto skip;
3255
0
        }
3256
0
        do {
3257
0
            struct objgroup *o;
3258
0
            type = get_token(fp, token, MAXTOKEN);
3259
0
            if (type != LABEL) {
3260
0
                print_error("Bad identifier", token, type);
3261
0
                goto skip;
3262
0
            }
3263
0
            o = (struct objgroup *) malloc(sizeof(struct objgroup));
3264
0
            if (!o) {
3265
0
                print_error("Resource failure", token, type);
3266
0
                goto skip;
3267
0
            }
3268
0
            o->line = mibLine;
3269
0
            o->name = strdup(token);
3270
0
            o->next = *ol;
3271
0
            *ol = o;
3272
0
            type = get_token(fp, token, MAXTOKEN);
3273
0
        } while (type == COMMA);
3274
0
        if (type != RIGHTBRACKET) {
3275
0
            print_error("Expected \"}\" after list", token, type);
3276
0
            goto skip;
3277
0
        }
3278
0
        type = get_token(fp, token, type);
3279
0
    }
3280
0
    if (type != STATUS) {
3281
0
        print_error("Expected STATUS", token, type);
3282
0
        goto skip;
3283
0
    }
3284
0
    type = get_token(fp, token, MAXTOKEN);
3285
0
    if (type != CURRENT && type != DEPRECATED && type != OBSOLETE) {
3286
0
        print_error("Bad STATUS value", token, type);
3287
0
        goto skip;
3288
0
    }
3289
0
    type = get_token(fp, token, MAXTOKEN);
3290
0
    if (type != DESCRIPTION) {
3291
0
        print_error("Expected DESCRIPTION", token, type);
3292
0
        goto skip;
3293
0
    }
3294
0
    type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3295
0
    if (type != QUOTESTRING) {
3296
0
        print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3297
0
        free_node(np);
3298
0
        return NULL;
3299
0
    }
3300
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
3301
0
             NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
3302
0
        np->description = strdup(quoted_string_buffer);
3303
0
    }
3304
0
    type = get_token(fp, token, MAXTOKEN);
3305
0
    if (type == REFERENCE) {
3306
0
        type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3307
0
        if (type != QUOTESTRING) {
3308
0
            print_error("Bad REFERENCE", quoted_string_buffer, type);
3309
0
            free_node(np);
3310
0
            return NULL;
3311
0
        }
3312
0
        np->reference = strdup(quoted_string_buffer);
3313
0
        type = get_token(fp, token, MAXTOKEN);
3314
0
    }
3315
0
    if (type != EQUALS)
3316
0
        print_error("Expected \"::=\"", token, type);
3317
0
  skip:
3318
0
    while (type != EQUALS && type != ENDOFFILE)
3319
0
        type = get_token(fp, token, MAXTOKEN);
3320
3321
0
    return merge_parse_objectid(np, fp, name);
3322
0
}
3323
3324
/*
3325
 * Parses a NOTIFICATION-TYPE macro.
3326
 * Returns 0 on error.
3327
 */
3328
static struct node *
3329
parse_notificationDefinition(FILE * fp, char *name)
3330
0
{
3331
0
    register int    type;
3332
0
    char            token[MAXTOKEN];
3333
0
    char            quoted_string_buffer[MAXQUOTESTR];
3334
0
    register struct node *np;
3335
3336
0
    np = alloc_node(current_module);
3337
0
    if (np == NULL)
3338
0
        return (NULL);
3339
0
    type = get_token(fp, token, MAXTOKEN);
3340
0
    while (type != EQUALS && type != ENDOFFILE) {
3341
0
        switch (type) {
3342
0
        case DESCRIPTION:
3343
0
            type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3344
0
            if (type != QUOTESTRING) {
3345
0
                print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3346
0
                goto free_node;
3347
0
            }
3348
0
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
3349
0
               NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
3350
0
                free(np->description);
3351
0
                np->description = strdup(quoted_string_buffer);
3352
0
            }
3353
0
            break;
3354
0
        case REFERENCE:
3355
0
            type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3356
0
            if (type != QUOTESTRING) {
3357
0
                print_error("Bad REFERENCE", quoted_string_buffer, type);
3358
0
                goto free_node;
3359
0
            }
3360
0
            free(np->reference);
3361
0
            np->reference = strdup(quoted_string_buffer);
3362
0
            break;
3363
0
        case OBJECTS:
3364
0
            np->varbinds = getVarbinds(fp, &np->varbinds);
3365
0
            if (!np->varbinds) {
3366
0
                print_error("Bad OBJECTS list", token, type);
3367
0
                goto free_node;
3368
0
            }
3369
0
            break;
3370
0
        default:
3371
            /*
3372
             * NOTHING 
3373
             */
3374
0
            break;
3375
0
        }
3376
0
        type = get_token(fp, token, MAXTOKEN);
3377
0
    }
3378
0
    return merge_parse_objectid(np, fp, name);
3379
3380
0
free_node:
3381
0
    free_node(np);
3382
0
    return NULL;
3383
0
}
3384
3385
/*
3386
 * Parses a TRAP-TYPE macro.
3387
 * Returns 0 on error.
3388
 */
3389
static struct node *
3390
parse_trapDefinition(FILE * fp, char *name)
3391
0
{
3392
0
    register int    type;
3393
0
    char            token[MAXTOKEN];
3394
0
    char            quoted_string_buffer[MAXQUOTESTR];
3395
0
    register struct node *np;
3396
3397
0
    np = alloc_node(current_module);
3398
0
    if (np == NULL)
3399
0
        return (NULL);
3400
0
    type = get_token(fp, token, MAXTOKEN);
3401
0
    while (type != EQUALS && type != ENDOFFILE) {
3402
0
        switch (type) {
3403
0
        case DESCRIPTION:
3404
0
            type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3405
0
            if (type != QUOTESTRING) {
3406
0
                print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3407
0
                goto free_node;
3408
0
            }
3409
0
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
3410
0
               NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
3411
0
                free(np->description);
3412
0
                np->description = strdup(quoted_string_buffer);
3413
0
            }
3414
0
            break;
3415
0
        case REFERENCE:
3416
            /* I'm not sure REFERENCEs are legal in smiv1 traps??? */
3417
0
            type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3418
0
            if (type != QUOTESTRING) {
3419
0
                print_error("Bad REFERENCE", quoted_string_buffer, type);
3420
0
                goto free_node;
3421
0
            }
3422
0
            free(np->reference);
3423
0
            np->reference = strdup(quoted_string_buffer);
3424
0
            break;
3425
0
        case ENTERPRISE:
3426
0
            type = get_token(fp, token, MAXTOKEN);
3427
0
            if (type == LEFTBRACKET) {
3428
0
                type = get_token(fp, token, MAXTOKEN);
3429
0
                if (type != LABEL) {
3430
0
                    print_error("Bad Trap Format", token, type);
3431
0
                    goto free_node;
3432
0
                }
3433
0
                free(np->parent);
3434
0
                np->parent = strdup(token);
3435
                /*
3436
                 * Get right bracket 
3437
                 */
3438
0
                type = get_token(fp, token, MAXTOKEN);
3439
0
            } else if (type == LABEL) {
3440
0
                free(np->parent);
3441
0
                np->parent = strdup(token);
3442
0
            } else {
3443
0
                goto free_node;
3444
0
            }
3445
0
            break;
3446
0
        case VARIABLES:
3447
0
            np->varbinds = getVarbinds(fp, &np->varbinds);
3448
0
            if (!np->varbinds) {
3449
0
                print_error("Bad VARIABLES list", token, type);
3450
0
                goto free_node;
3451
0
            }
3452
0
            break;
3453
0
        default:
3454
            /*
3455
             * NOTHING 
3456
             */
3457
0
            break;
3458
0
        }
3459
0
        type = get_token(fp, token, MAXTOKEN);
3460
0
    }
3461
0
    type = get_token(fp, token, MAXTOKEN);
3462
3463
0
    np->label = strdup(name);
3464
3465
0
    if (type != NUMBER) {
3466
0
        print_error("Expected a Number", token, type);
3467
0
        goto free_node;
3468
0
    }
3469
0
    np->subid = strtoul(token, NULL, 10);
3470
0
    np->next = alloc_node(current_module);
3471
0
    if (np->next == NULL)
3472
0
        goto free_node;
3473
3474
    /* Catch the syntax error */
3475
0
    if (np->parent == NULL) {
3476
0
        gMibError = MODULE_SYNTAX_ERROR;
3477
0
        goto free_next_node;
3478
0
    }
3479
3480
0
    np->next->parent = np->parent;
3481
0
    np->parent = NULL;
3482
0
    if (asprintf(&np->parent, "%s#", np->next->parent) < 0)
3483
0
        goto free_next_node;
3484
0
    np->next->label = strdup(np->parent);
3485
0
    return np;
3486
3487
0
free_next_node:
3488
0
    free_node(np->next);
3489
3490
0
free_node:
3491
0
    free_node(np);
3492
0
    return NULL;
3493
0
}
3494
3495
3496
/*
3497
 * Parses a compliance macro
3498
 * Returns 0 on error.
3499
 */
3500
static int
3501
eat_syntax(FILE * fp, char *token, int maxtoken)
3502
0
{
3503
0
    int             type, nexttype;
3504
0
    struct node    *np = alloc_node(current_module);
3505
0
    char            nexttoken[MAXTOKEN];
3506
3507
0
    if (!np)
3508
0
  return 0;
3509
3510
0
    type = get_token(fp, token, maxtoken);
3511
0
    nexttype = get_token(fp, nexttoken, MAXTOKEN);
3512
0
    switch (type) {
3513
0
    case INTEGER:
3514
0
    case INTEGER32:
3515
0
    case UINTEGER32:
3516
0
    case UNSIGNED32:
3517
0
    case COUNTER:
3518
0
    case GAUGE:
3519
0
    case BITSTRING:
3520
0
    case LABEL:
3521
0
        if (nexttype == LEFTBRACKET) {
3522
            /*
3523
             * if there is an enumeration list, parse it 
3524
             */
3525
0
            np->enums = parse_enumlist(fp, &np->enums);
3526
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
3527
0
        } else if (nexttype == LEFTPAREN) {
3528
            /*
3529
             * if there is a range list, parse it 
3530
             */
3531
0
            np->ranges = parse_ranges(fp, &np->ranges);
3532
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
3533
0
        }
3534
0
        break;
3535
0
    case OCTETSTR:
3536
0
    case KW_OPAQUE:
3537
        /*
3538
         * parse any SIZE specification 
3539
         */
3540
0
        if (nexttype == LEFTPAREN) {
3541
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
3542
0
            if (nexttype == SIZE) {
3543
0
                nexttype = get_token(fp, nexttoken, MAXTOKEN);
3544
0
                if (nexttype == LEFTPAREN) {
3545
0
                    np->ranges = parse_ranges(fp, &np->ranges);
3546
0
                    nexttype = get_token(fp, nexttoken, MAXTOKEN);      /* ) */
3547
0
                    if (nexttype == RIGHTPAREN) {
3548
0
                        nexttype = get_token(fp, nexttoken, MAXTOKEN);
3549
0
                        break;
3550
0
                    }
3551
0
                }
3552
0
            }
3553
0
            print_error("Bad SIZE syntax", token, type);
3554
0
            free_node(np);
3555
0
            return nexttype;
3556
0
        }
3557
0
        break;
3558
0
    case OBJID:
3559
0
    case NETADDR:
3560
0
    case IPADDR:
3561
0
    case TIMETICKS:
3562
0
    case NUL:
3563
0
    case NSAPADDRESS:
3564
0
    case COUNTER64:
3565
0
        break;
3566
0
    default:
3567
0
        print_error("Bad syntax", token, type);
3568
0
        free_node(np);
3569
0
        return nexttype;
3570
0
    }
3571
0
    free_node(np);
3572
0
    return nexttype;
3573
0
}
3574
3575
static int
3576
compliance_lookup(const char *name, int modid)
3577
0
{
3578
0
    struct objgroup *op;
3579
3580
0
    if (modid != -1)
3581
0
        return find_tree_node(name, modid) != NULL;
3582
3583
0
    op = malloc(sizeof(struct objgroup));
3584
0
    if (!op)
3585
0
        return 0;
3586
3587
0
    op->next = objgroups;
3588
0
    op->name = strdup(name);
3589
0
    if (!op->name) {
3590
0
        free(op);
3591
0
        return 0;
3592
0
    }
3593
0
    op->line = mibLine;
3594
0
    objgroups = op;
3595
0
    return 1;
3596
0
}
3597
3598
static struct node *
3599
parse_compliance(FILE * fp, char *name)
3600
0
{
3601
0
    int             type;
3602
0
    char            token[MAXTOKEN];
3603
0
    char            quoted_string_buffer[MAXQUOTESTR];
3604
0
    struct node    *np;
3605
3606
0
    np = alloc_node(current_module);
3607
0
    if (np == NULL)
3608
0
        return (NULL);
3609
0
    type = get_token(fp, token, MAXTOKEN);
3610
0
    if (type != STATUS) {
3611
0
        print_error("Expected STATUS", token, type);
3612
0
        goto skip;
3613
0
    }
3614
0
    type = get_token(fp, token, MAXTOKEN);
3615
0
    if (type != CURRENT && type != DEPRECATED && type != OBSOLETE) {
3616
0
        print_error("Bad STATUS", token, type);
3617
0
        goto skip;
3618
0
    }
3619
0
    type = get_token(fp, token, MAXTOKEN);
3620
0
    if (type != DESCRIPTION) {
3621
0
        print_error("Expected DESCRIPTION", token, type);
3622
0
        goto skip;
3623
0
    }
3624
0
    type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3625
0
    if (type != QUOTESTRING) {
3626
0
        print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3627
0
        goto skip;
3628
0
    }
3629
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
3630
0
             NETSNMP_DS_LIB_SAVE_MIB_DESCRS))
3631
0
        np->description = strdup(quoted_string_buffer);
3632
0
    type = get_token(fp, token, MAXTOKEN);
3633
0
    if (type == REFERENCE) {
3634
0
        type = get_token(fp, quoted_string_buffer,
3635
0
                         sizeof(quoted_string_buffer));
3636
0
        if (type != QUOTESTRING) {
3637
0
            print_error("Bad REFERENCE", quoted_string_buffer, type);
3638
0
            goto skip;
3639
0
        }
3640
0
        np->reference = strdup(quoted_string_buffer);
3641
0
        type = get_token(fp, token, MAXTOKEN);
3642
0
    }
3643
0
    if (type != MODULE) {
3644
0
        print_error("Expected MODULE", token, type);
3645
0
        goto skip;
3646
0
    }
3647
0
    while (type == MODULE) {
3648
0
        int             modid = -1;
3649
0
        char            modname[MAXTOKEN];
3650
0
        type = get_token(fp, token, MAXTOKEN);
3651
0
        if (type == LABEL
3652
0
            && strcmp(token, module_name(current_module, modname))) {
3653
0
            modid = read_module_internal(token);
3654
0
            if (modid != MODULE_LOADED_OK
3655
0
                && modid != MODULE_ALREADY_LOADED) {
3656
0
                print_error("Unknown module", token, type);
3657
0
                goto skip;
3658
0
            }
3659
0
            modid = which_module(token);
3660
0
            type = get_token(fp, token, MAXTOKEN);
3661
0
        }
3662
0
        if (type == MANDATORYGROUPS) {
3663
0
            type = get_token(fp, token, MAXTOKEN);
3664
0
            if (type != LEFTBRACKET) {
3665
0
                print_error("Expected \"{\"", token, type);
3666
0
                goto skip;
3667
0
            }
3668
0
            do {
3669
0
                type = get_token(fp, token, MAXTOKEN);
3670
0
                if (type != LABEL) {
3671
0
                    print_error("Bad group name", token, type);
3672
0
                    goto skip;
3673
0
                }
3674
0
                if (!compliance_lookup(token, modid))
3675
0
                    print_error("Unknown group", token, type);
3676
0
                type = get_token(fp, token, MAXTOKEN);
3677
0
            } while (type == COMMA);
3678
0
            if (type != RIGHTBRACKET) {
3679
0
                print_error("Expected \"}\"", token, type);
3680
0
                goto skip;
3681
0
            }
3682
0
            type = get_token(fp, token, MAXTOKEN);
3683
0
        }
3684
0
        while (type == GROUP || type == OBJECT) {
3685
0
            if (type == GROUP) {
3686
0
                type = get_token(fp, token, MAXTOKEN);
3687
0
                if (type != LABEL) {
3688
0
                    print_error("Bad group name", token, type);
3689
0
                    goto skip;
3690
0
                }
3691
0
                if (!compliance_lookup(token, modid))
3692
0
                    print_error("Unknown group", token, type);
3693
0
                type = get_token(fp, token, MAXTOKEN);
3694
0
            } else {
3695
0
                type = get_token(fp, token, MAXTOKEN);
3696
0
                if (type != LABEL) {
3697
0
                    print_error("Bad object name", token, type);
3698
0
                    goto skip;
3699
0
                }
3700
0
                if (!compliance_lookup(token, modid))
3701
0
                    print_error("Unknown group", token, type);
3702
0
                type = get_token(fp, token, MAXTOKEN);
3703
0
                if (type == SYNTAX)
3704
0
                    type = eat_syntax(fp, token, MAXTOKEN);
3705
0
                if (type == WRSYNTAX)
3706
0
                    type = eat_syntax(fp, token, MAXTOKEN);
3707
0
                if (type == MINACCESS) {
3708
0
                    type = get_token(fp, token, MAXTOKEN);
3709
0
                    if (type != NOACCESS && type != ACCNOTIFY
3710
0
                        && type != READONLY && type != WRITEONLY
3711
0
                        && type != READCREATE && type != READWRITE) {
3712
0
                        print_error("Bad MIN-ACCESS spec", token, type);
3713
0
                        goto skip;
3714
0
                    }
3715
0
                    type = get_token(fp, token, MAXTOKEN);
3716
0
                }
3717
0
            }
3718
0
            if (type != DESCRIPTION) {
3719
0
                print_error("Expected DESCRIPTION", token, type);
3720
0
                goto skip;
3721
0
            }
3722
0
            type = get_token(fp, token, MAXTOKEN);
3723
0
            if (type != QUOTESTRING) {
3724
0
                print_error("Bad DESCRIPTION", token, type);
3725
0
                goto skip;
3726
0
            }
3727
0
            type = get_token(fp, token, MAXTOKEN);
3728
0
        }
3729
0
    }
3730
0
  skip:
3731
0
    while (type != EQUALS && type != ENDOFFILE)
3732
0
        type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3733
3734
0
    return merge_parse_objectid(np, fp, name);
3735
0
}
3736
3737
3738
/*
3739
 * Parses a capabilities macro
3740
 * Returns 0 on error.
3741
 */
3742
static struct node *
3743
parse_capabilities(FILE * fp, char *name)
3744
0
{
3745
0
    int             type;
3746
0
    char            token[MAXTOKEN];
3747
0
    char            quoted_string_buffer[MAXQUOTESTR];
3748
0
    struct node    *np;
3749
3750
0
    np = alloc_node(current_module);
3751
0
    if (np == NULL)
3752
0
        return (NULL);
3753
0
    type = get_token(fp, token, MAXTOKEN);
3754
0
    if (type != PRODREL) {
3755
0
        print_error("Expected PRODUCT-RELEASE", token, type);
3756
0
        goto skip;
3757
0
    }
3758
0
    type = get_token(fp, token, MAXTOKEN);
3759
0
    if (type != QUOTESTRING) {
3760
0
        print_error("Expected STRING after PRODUCT-RELEASE", token, type);
3761
0
        goto skip;
3762
0
    }
3763
0
    type = get_token(fp, token, MAXTOKEN);
3764
0
    if (type != STATUS) {
3765
0
        print_error("Expected STATUS", token, type);
3766
0
        goto skip;
3767
0
    }
3768
0
    type = get_token(fp, token, MAXTOKEN);
3769
0
    if (type != CURRENT && type != OBSOLETE) {
3770
0
        print_error("STATUS should be current or obsolete", token, type);
3771
0
        goto skip;
3772
0
    }
3773
0
    type = get_token(fp, token, MAXTOKEN);
3774
0
    if (type != DESCRIPTION) {
3775
0
        print_error("Expected DESCRIPTION", token, type);
3776
0
        goto skip;
3777
0
    }
3778
0
    type = get_token(fp, quoted_string_buffer, sizeof(quoted_string_buffer));
3779
0
    if (type != QUOTESTRING) {
3780
0
        print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3781
0
        goto skip;
3782
0
    }
3783
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
3784
0
             NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
3785
0
        np->description = strdup(quoted_string_buffer);
3786
0
    }
3787
0
    type = get_token(fp, token, MAXTOKEN);
3788
0
    if (type == REFERENCE) {
3789
0
        type = get_token(fp, quoted_string_buffer,
3790
0
                         sizeof(quoted_string_buffer));
3791
0
        if (type != QUOTESTRING) {
3792
0
            print_error("Bad REFERENCE", quoted_string_buffer, type);
3793
0
            goto skip;
3794
0
        }
3795
0
        np->reference = strdup(quoted_string_buffer);
3796
0
        type = get_token(fp, token, type);
3797
0
    }
3798
0
    while (type == SUPPORTS) {
3799
0
        int             modid;
3800
0
        struct tree    *tp;
3801
3802
0
        type = get_token(fp, token, MAXTOKEN);
3803
0
        if (type != LABEL) {
3804
0
            print_error("Bad module name", token, type);
3805
0
            goto skip;
3806
0
        }
3807
0
        modid = read_module_internal(token);
3808
0
        if (modid != MODULE_LOADED_OK && modid != MODULE_ALREADY_LOADED) {
3809
0
            print_error("Module not found", token, type);
3810
0
            goto skip;
3811
0
        }
3812
0
        modid = which_module(token);
3813
0
        type = get_token(fp, token, MAXTOKEN);
3814
0
        if (type != INCLUDES) {
3815
0
            print_error("Expected INCLUDES", token, type);
3816
0
            goto skip;
3817
0
        }
3818
0
        type = get_token(fp, token, MAXTOKEN);
3819
0
        if (type != LEFTBRACKET) {
3820
0
            print_error("Expected \"{\"", token, type);
3821
0
            goto skip;
3822
0
        }
3823
0
        do {
3824
0
            type = get_token(fp, token, MAXTOKEN);
3825
0
            if (type != LABEL) {
3826
0
                print_error("Expected group name", token, type);
3827
0
                goto skip;
3828
0
            }
3829
0
            tp = find_tree_node(token, modid);
3830
0
            if (!tp)
3831
0
                print_error("Group not found in module", token, type);
3832
0
            type = get_token(fp, token, MAXTOKEN);
3833
0
        } while (type == COMMA);
3834
0
        if (type != RIGHTBRACKET) {
3835
0
            print_error("Expected \"}\" after group list", token, type);
3836
0
            goto skip;
3837
0
        }
3838
0
        type = get_token(fp, token, MAXTOKEN);
3839
0
        while (type == VARIATION) {
3840
0
            type = get_token(fp, token, MAXTOKEN);
3841
0
            if (type != LABEL) {
3842
0
                print_error("Bad object name", token, type);
3843
0
                goto skip;
3844
0
            }
3845
0
            tp = find_tree_node(token, modid);
3846
0
            if (!tp)
3847
0
                print_error("Object not found in module", token, type);
3848
0
            type = get_token(fp, token, MAXTOKEN);
3849
0
            if (type == SYNTAX) {
3850
0
                type = eat_syntax(fp, token, MAXTOKEN);
3851
0
            }
3852
0
            if (type == WRSYNTAX) {
3853
0
                type = eat_syntax(fp, token, MAXTOKEN);
3854
0
            }
3855
0
            if (type == ACCESS) {
3856
0
                type = get_token(fp, token, MAXTOKEN);
3857
0
                if (type != ACCNOTIFY && type != READONLY
3858
0
                    && type != READWRITE && type != READCREATE
3859
0
                    && type != WRITEONLY && type != NOTIMPL) {
3860
0
                    print_error("Bad ACCESS", token, type);
3861
0
                    goto skip;
3862
0
                }
3863
0
                type = get_token(fp, token, MAXTOKEN);
3864
0
            }
3865
0
            if (type == CREATEREQ) {
3866
0
                type = get_token(fp, token, MAXTOKEN);
3867
0
                if (type != LEFTBRACKET) {
3868
0
                    print_error("Expected \"{\"", token, type);
3869
0
                    goto skip;
3870
0
                }
3871
0
                do {
3872
0
                    type = get_token(fp, token, MAXTOKEN);
3873
0
                    if (type != LABEL) {
3874
0
                        print_error("Bad object name in list", token,
3875
0
                                    type);
3876
0
                        goto skip;
3877
0
                    }
3878
0
                    type = get_token(fp, token, MAXTOKEN);
3879
0
                } while (type == COMMA);
3880
0
                if (type != RIGHTBRACKET) {
3881
0
                    print_error("Expected \"}\" after list", token, type);
3882
0
                    goto skip;
3883
0
                }
3884
0
                type = get_token(fp, token, MAXTOKEN);
3885
0
            }
3886
0
            if (type == DEFVAL) {
3887
0
                int             level = 1;
3888
0
                type = get_token(fp, token, MAXTOKEN);
3889
0
                if (type != LEFTBRACKET) {
3890
0
                    print_error("Expected \"{\" after DEFVAL", token,
3891
0
                                type);
3892
0
                    goto skip;
3893
0
                }
3894
0
                do {
3895
0
                    type = get_token(fp, token, MAXTOKEN);
3896
0
                    if (type == LEFTBRACKET)
3897
0
                        level++;
3898
0
                    else if (type == RIGHTBRACKET)
3899
0
                        level--;
3900
0
                } while ((type != RIGHTBRACKET || level != 0)
3901
0
                         && type != ENDOFFILE);
3902
0
                if (type != RIGHTBRACKET) {
3903
0
                    print_error("Missing \"}\" after DEFVAL", token, type);
3904
0
                    goto skip;
3905
0
                }
3906
0
                type = get_token(fp, token, MAXTOKEN);
3907
0
            }
3908
0
            if (type != DESCRIPTION) {
3909
0
                print_error("Expected DESCRIPTION", token, type);
3910
0
                goto skip;
3911
0
            }
3912
0
            type = get_token(fp, quoted_string_buffer,
3913
0
                             sizeof(quoted_string_buffer));
3914
0
            if (type != QUOTESTRING) {
3915
0
                print_error("Bad DESCRIPTION", quoted_string_buffer, type);
3916
0
                goto skip;
3917
0
            }
3918
0
            type = get_token(fp, token, MAXTOKEN);
3919
0
        }
3920
0
    }
3921
0
    if (type != EQUALS)
3922
0
        print_error("Expected \"::=\"", token, type);
3923
0
  skip:
3924
0
    while (type != EQUALS && type != ENDOFFILE) {
3925
0
        type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
3926
0
    }
3927
0
    return merge_parse_objectid(np, fp, name);
3928
0
}
3929
3930
/*
3931
 * Parses a module identity macro
3932
 * Returns 0 on error.
3933
 */
3934
static void
3935
check_utc(const char *utc)
3936
0
{
3937
0
    int             len, year, month, day, hour, minute;
3938
3939
0
    len = strlen(utc);
3940
0
    if (len == 0) {
3941
0
        print_error("Timestamp has zero length", utc, QUOTESTRING);
3942
0
        return;
3943
0
    }
3944
0
    if (utc[len - 1] != 'Z' && utc[len - 1] != 'z') {
3945
0
        print_error("Timestamp should end with Z", utc, QUOTESTRING);
3946
0
        return;
3947
0
    }
3948
0
    if (len == 11) {
3949
0
        len = sscanf(utc, "%2d%2d%2d%2d%2dZ", &year, &month, &day, &hour,
3950
0
                     &minute);
3951
0
        year += 1900;
3952
0
    } else if (len == 13)
3953
0
        len = sscanf(utc, "%4d%2d%2d%2d%2dZ", &year, &month, &day, &hour,
3954
0
                     &minute);
3955
0
    else {
3956
0
        print_error("Bad timestamp format (11 or 13 characters)",
3957
0
                    utc, QUOTESTRING);
3958
0
        return;
3959
0
    }
3960
0
    if (len != 5) {
3961
0
        print_error("Bad timestamp format", utc, QUOTESTRING);
3962
0
        return;
3963
0
    }
3964
0
    if (month < 1 || month > 12)
3965
0
        print_error("Bad month in timestamp", utc, QUOTESTRING);
3966
0
    if (day < 1 || day > 31)
3967
0
        print_error("Bad day in timestamp", utc, QUOTESTRING);
3968
0
    if (hour < 0 || hour > 23)
3969
0
        print_error("Bad hour in timestamp", utc, QUOTESTRING);
3970
0
    if (minute < 0 || minute > 59)
3971
0
        print_error("Bad minute in timestamp", utc, QUOTESTRING);
3972
0
}
3973
3974
static struct node *
3975
parse_moduleIdentity(FILE * fp, char *name)
3976
0
{
3977
0
    register int    type;
3978
0
    char            token[MAXTOKEN];
3979
0
    char            quoted_string_buffer[MAXQUOTESTR];
3980
0
    register struct node *np;
3981
3982
0
    np = alloc_node(current_module);
3983
0
    if (np == NULL)
3984
0
        return (NULL);
3985
0
    type = get_token(fp, token, MAXTOKEN);
3986
0
    if (type != LASTUPDATED) {
3987
0
        print_error("Expected LAST-UPDATED", token, type);
3988
0
        goto skip;
3989
0
    }
3990
0
    type = get_token(fp, token, MAXTOKEN);
3991
0
    if (type != QUOTESTRING) {
3992
0
        print_error("Need STRING for LAST-UPDATED", token, type);
3993
0
        goto skip;
3994
0
    }
3995
0
    check_utc(token);
3996
0
    type = get_token(fp, token, MAXTOKEN);
3997
0
    if (type != ORGANIZATION) {
3998
0
        print_error("Expected ORGANIZATION", token, type);
3999
0
        goto skip;
4000
0
    }
4001
0
    type = get_token(fp, token, MAXTOKEN);
4002
0
    if (type != QUOTESTRING) {
4003
0
        print_error("Bad ORGANIZATION", token, type);
4004
0
        goto skip;
4005
0
    }
4006
0
    type = get_token(fp, token, MAXTOKEN);
4007
0
    if (type != CONTACTINFO) {
4008
0
        print_error("Expected CONTACT-INFO", token, type);
4009
0
        goto skip;
4010
0
    }
4011
0
    type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
4012
0
    if (type != QUOTESTRING) {
4013
0
        print_error("Bad CONTACT-INFO", quoted_string_buffer, type);
4014
0
        goto skip;
4015
0
    }
4016
0
    type = get_token(fp, token, MAXTOKEN);
4017
0
    if (type != DESCRIPTION) {
4018
0
        print_error("Expected DESCRIPTION", token, type);
4019
0
        goto skip;
4020
0
    }
4021
0
    type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
4022
0
    if (type != QUOTESTRING) {
4023
0
        print_error("Bad DESCRIPTION", quoted_string_buffer, type);
4024
0
        goto skip;
4025
0
    }
4026
0
    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
4027
0
             NETSNMP_DS_LIB_SAVE_MIB_DESCRS)) {
4028
0
        np->description = strdup(quoted_string_buffer);
4029
0
    }
4030
0
    type = get_token(fp, token, MAXTOKEN);
4031
0
    while (type == REVISION) {
4032
0
        type = get_token(fp, token, MAXTOKEN);
4033
0
        if (type != QUOTESTRING) {
4034
0
            print_error("Bad REVISION", token, type);
4035
0
            goto skip;
4036
0
        }
4037
0
        check_utc(token);
4038
0
        type = get_token(fp, token, MAXTOKEN);
4039
0
        if (type != DESCRIPTION) {
4040
0
            print_error("Expected DESCRIPTION", token, type);
4041
0
            goto skip;
4042
0
        }
4043
0
        type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
4044
0
        if (type != QUOTESTRING) {
4045
0
            print_error("Bad DESCRIPTION", quoted_string_buffer, type);
4046
0
            goto skip;
4047
0
        }
4048
0
        type = get_token(fp, token, MAXTOKEN);
4049
0
    }
4050
0
    if (type != EQUALS)
4051
0
        print_error("Expected \"::=\"", token, type);
4052
0
  skip:
4053
0
    while (type != EQUALS && type != ENDOFFILE) {
4054
0
        type = get_token(fp, quoted_string_buffer, MAXQUOTESTR);
4055
0
    }
4056
0
    return merge_parse_objectid(np, fp, name);
4057
0
}
4058
4059
4060
/*
4061
 * Parses a MACRO definition
4062
 * Expect BEGIN, discard everything to end.
4063
 * Returns 0 on error.
4064
 */
4065
static struct node *
4066
parse_macro(FILE * fp, char *name)
4067
0
{
4068
0
    register int    type;
4069
0
    char            token[MAXTOKEN];
4070
0
    struct node    *np;
4071
0
    int             iLine = mibLine;
4072
4073
0
    np = alloc_node(current_module);
4074
0
    if (np == NULL)
4075
0
        return (NULL);
4076
0
    type = get_token(fp, token, sizeof(token));
4077
0
    while (type != EQUALS && type != ENDOFFILE) {
4078
0
        type = get_token(fp, token, sizeof(token));
4079
0
    }
4080
0
    if (type != EQUALS) {
4081
0
        if (np)
4082
0
            free_node(np);
4083
0
        return NULL;
4084
0
    }
4085
0
    while (type != BEGIN && type != ENDOFFILE) {
4086
0
        type = get_token(fp, token, sizeof(token));
4087
0
    }
4088
0
    if (type != BEGIN) {
4089
0
        if (np)
4090
0
            free_node(np);
4091
0
        return NULL;
4092
0
    }
4093
0
    while (type != END && type != ENDOFFILE) {
4094
0
        type = get_token(fp, token, sizeof(token));
4095
0
    }
4096
0
    if (type != END) {
4097
0
        if (np)
4098
0
            free_node(np);
4099
0
        return NULL;
4100
0
    }
4101
4102
0
    if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
4103
0
         NETSNMP_DS_LIB_MIB_WARNINGS)) {
4104
0
        snmp_log(LOG_WARNING,
4105
0
                 "%s MACRO (lines %d..%d parsed and ignored).\n", name,
4106
0
                 iLine, mibLine);
4107
0
    }
4108
4109
0
    return np;
4110
0
}
4111
4112
/*
4113
 * Parses a module import clause
4114
 *   loading any modules referenced
4115
 */
4116
static void
4117
parse_imports(FILE * fp)
4118
0
{
4119
0
    register int    type;
4120
0
    char            token[MAXTOKEN];
4121
0
    char            modbuf[256];
4122
0
#define MAX_IMPORTS 512
4123
0
    struct module_import *import_list;
4124
0
    int             this_module;
4125
0
    struct module  *mp;
4126
4127
0
    int             import_count = 0;   /* Total number of imported descriptors */
4128
0
    int             i = 0, old_i;       /* index of first import from each module */
4129
4130
0
    import_list = malloc(MAX_IMPORTS * sizeof(*import_list));
4131
4132
0
    type = get_token(fp, token, MAXTOKEN);
4133
4134
    /*
4135
     * Parse the IMPORTS clause
4136
     */
4137
0
    while (type != SEMI && type != ENDOFFILE) {
4138
0
        if (type == LABEL) {
4139
0
            if (import_count == MAX_IMPORTS) {
4140
0
                print_error("Too many imported symbols", token, type);
4141
0
                do {
4142
0
                    type = get_token(fp, token, MAXTOKEN);
4143
0
                } while (type != SEMI && type != ENDOFFILE);
4144
0
                goto out;
4145
0
            }
4146
0
            import_list[import_count++].label = strdup(token);
4147
0
        } else if (type == FROM) {
4148
0
            type = get_token(fp, token, MAXTOKEN);
4149
0
            if (import_count == i) {    /* All imports are handled internally */
4150
0
                type = get_token(fp, token, MAXTOKEN);
4151
0
                continue;
4152
0
            }
4153
0
            this_module = which_module(token);
4154
4155
0
            for (old_i = i; i < import_count; ++i)
4156
0
                import_list[i].modid = this_module;
4157
4158
            /*
4159
             * Recursively read any pre-requisite modules
4160
             */
4161
0
            if (read_module_internal(token) == MODULE_NOT_FOUND) {
4162
0
    int found = 0;
4163
0
                for (; old_i < import_count; ++old_i) {
4164
0
                    found += read_import_replacements(token, &import_list[old_i]);
4165
0
                }
4166
0
    if (!found)
4167
0
        print_module_not_found(token);
4168
0
            }
4169
0
        }
4170
0
        type = get_token(fp, token, MAXTOKEN);
4171
0
    }
4172
4173
    /* Initialize modid in case the module name was missing. */
4174
0
    for (; i < import_count; ++i)
4175
0
        import_list[i].modid = -1;
4176
4177
    /*
4178
     * Save the import information
4179
     *   in the global module table
4180
     */
4181
0
    for (mp = module_head; mp; mp = mp->next) {
4182
0
        if (mp->modid == current_module) {
4183
0
            if (import_count == 0)
4184
0
                goto out;
4185
0
            if (mp->imports && (mp->imports != root_imports)) {
4186
                /*
4187
                 * this can happen if all modules are in one source file. 
4188
                 */
4189
0
                for (i = 0; i < mp->no_imports; ++i) {
4190
0
                    DEBUGMSGTL(("parse-mibs",
4191
0
                                "#### freeing Module %d '%s' %d\n",
4192
0
                                mp->modid, mp->imports[i].label,
4193
0
                                mp->imports[i].modid));
4194
0
                    free(mp->imports[i].label);
4195
0
                }
4196
0
                free(mp->imports);
4197
0
            }
4198
0
            mp->imports = (struct module_import *)
4199
0
                calloc(import_count, sizeof(struct module_import));
4200
0
            if (mp->imports == NULL)
4201
0
                goto out;
4202
0
            for (i = 0; i < import_count; ++i) {
4203
0
                mp->imports[i].label = import_list[i].label;
4204
0
                import_list[i].label = NULL;
4205
0
                mp->imports[i].modid = import_list[i].modid;
4206
0
                DEBUGMSGTL(("parse-mibs",
4207
0
                            "#### adding Module %d '%s' %d\n", mp->modid,
4208
0
                            mp->imports[i].label, mp->imports[i].modid));
4209
0
            }
4210
0
            mp->no_imports = import_count;
4211
0
            goto out;
4212
0
        }
4213
0
    }
4214
4215
    /*
4216
     * Shouldn't get this far
4217
     */
4218
0
    print_module_not_found(module_name(current_module, modbuf));
4219
4220
0
out:
4221
0
    for (i = 0; i < import_count; ++i)
4222
0
        free(import_list[i].label);
4223
0
    free(import_list);
4224
0
    return;
4225
0
}
4226
4227
4228
4229
/*
4230
 * MIB module handling routines
4231
 */
4232
4233
static void
4234
dump_module_list(void)
4235
0
{
4236
0
    struct module  *mp = module_head;
4237
4238
0
    DEBUGMSGTL(("parse-mibs", "Module list:\n"));
4239
0
    while (mp) {
4240
0
        DEBUGMSGTL(("parse-mibs", "  %s %d %s %d\n", mp->name, mp->modid,
4241
0
                    mp->file, mp->no_imports));
4242
0
        mp = mp->next;
4243
0
    }
4244
0
}
4245
4246
int
4247
which_module(const char *name)
4248
192
{
4249
192
    struct module  *mp;
4250
4251
192
    for (mp = module_head; mp; mp = mp->next)
4252
0
        if (!label_compare(mp->name, name))
4253
0
            return (mp->modid);
4254
4255
192
    DEBUGMSGTL(("parse-mibs", "Module %s not found\n", name));
4256
192
    return (-1);
4257
192
}
4258
4259
/*
4260
 * module_name - copy module name to user buffer, return ptr to same.
4261
 */
4262
char           *
4263
module_name(int modid, char *cp)
4264
316
{
4265
316
    struct module  *mp;
4266
4267
316
    for (mp = module_head; mp; mp = mp->next)
4268
0
        if (mp->modid == modid) {
4269
0
            strcpy(cp, mp->name);
4270
0
            return (cp);
4271
0
        }
4272
4273
316
    if (modid != -1) DEBUGMSGTL(("parse-mibs", "Module %d not found\n", modid));
4274
316
    sprintf(cp, "#%d", modid);
4275
316
    return (cp);
4276
316
}
4277
4278
/*
4279
 *  Backwards compatability
4280
 *  Read newer modules that replace the one specified:-
4281
 *      either all of them (read_module_replacements),
4282
 *      or those relating to a specified identifier (read_import_replacements)
4283
 *      plus an interface to add new replacement requirements
4284
 */
4285
netsnmp_feature_child_of(parse_add_module_replacement, netsnmp_unused);
4286
#ifndef NETSNMP_FEATURE_REMOVE_PARSE_ADD_MODULE_REPLACEMENT
4287
void
4288
add_module_replacement(const char *old_module,
4289
                       const char *new_module_name,
4290
                       const char *tag, int len)
4291
0
{
4292
0
    struct module_compatability *mcp;
4293
4294
0
    mcp = (struct module_compatability *)
4295
0
        calloc(1, sizeof(struct module_compatability));
4296
0
    if (mcp == NULL)
4297
0
        return;
4298
4299
0
    mcp->old_module = strdup(old_module);
4300
0
    mcp->new_module = strdup(new_module_name);
4301
0
    if (tag)
4302
0
        mcp->tag = strdup(tag);
4303
0
    mcp->tag_len = len;
4304
4305
0
    mcp->next = module_map_head;
4306
0
    module_map_head = mcp;
4307
0
}
4308
#endif /* NETSNMP_FEATURE_REMOVE_PARSE_ADD_MODULE_REPLACEMENT */
4309
4310
static int
4311
read_module_replacements(const char *name)
4312
297
{
4313
297
    struct module_compatability *mcp;
4314
4315
6.58k
    for (mcp = module_map_head; mcp; mcp = mcp->next) {
4316
6.30k
        if (!label_compare(mcp->old_module, name)) {
4317
15
            if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
4318
15
           NETSNMP_DS_LIB_MIB_WARNINGS)) {
4319
0
                snmp_log(LOG_WARNING,
4320
0
                         "Loading replacement module %s for %s (%s)\n",
4321
0
                         mcp->new_module, name, File);
4322
0
      }
4323
15
            (void) netsnmp_read_module(mcp->new_module);
4324
15
            return 1;
4325
15
        }
4326
6.30k
    }
4327
282
    return 0;
4328
297
}
4329
4330
static int
4331
read_import_replacements(const char *old_module_name,
4332
                         struct module_import *identifier)
4333
0
{
4334
0
    struct module_compatability *mcp;
4335
4336
    /*
4337
     * Look for matches first
4338
     */
4339
0
    for (mcp = module_map_head; mcp; mcp = mcp->next) {
4340
0
        if (!label_compare(mcp->old_module, old_module_name)) {
4341
4342
0
            if (                /* exact match */
4343
0
                   (mcp->tag_len == 0 &&
4344
0
                    (mcp->tag == NULL ||
4345
0
                     !label_compare(mcp->tag, identifier->label))) ||
4346
                   /*
4347
                    * prefix match 
4348
                    */
4349
0
                   (mcp->tag_len != 0 &&
4350
0
                    !strncmp(mcp->tag, identifier->label, mcp->tag_len))
4351
0
                ) {
4352
4353
0
                if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
4354
0
               NETSNMP_DS_LIB_MIB_WARNINGS)) {
4355
0
                    snmp_log(LOG_WARNING,
4356
0
                             "Importing %s from replacement module %s instead of %s (%s)\n",
4357
0
                             identifier->label, mcp->new_module,
4358
0
                             old_module_name, File);
4359
0
    }
4360
0
                (void) netsnmp_read_module(mcp->new_module);
4361
0
                identifier->modid = which_module(mcp->new_module);
4362
0
                return 1;         /* finished! */
4363
0
            }
4364
0
        }
4365
0
    }
4366
4367
    /*
4368
     * If no exact match, load everything relevant
4369
     */
4370
0
    return read_module_replacements(old_module_name);
4371
0
}
4372
4373
static int
4374
read_from_file(struct module *mp, const char *name)
4375
0
{
4376
0
    const char     *oldFile = File;
4377
0
    int             oldLine = mibLine;
4378
0
    int             oldModule = current_module;
4379
0
    FILE           *fp;
4380
0
    struct node    *np;
4381
0
    int             res;
4382
4383
0
    if (mp->no_imports != -1) {
4384
0
        DEBUGMSGTL(("parse-mibs", "Module %s already loaded\n",
4385
0
                    name));
4386
0
        return MODULE_ALREADY_LOADED;
4387
0
    }
4388
0
    if ((fp = fopen(mp->file, "r")) == NULL) {
4389
0
        int rval;
4390
0
        if (errno == ENOTDIR || errno == ENOENT)
4391
0
            rval = MODULE_NOT_FOUND;
4392
0
        else
4393
0
            rval = MODULE_LOAD_FAILED;
4394
0
        snmp_log_perror(mp->file);
4395
0
        return rval;
4396
0
    }
4397
0
#ifdef HAVE_FLOCKFILE
4398
0
    flockfile(fp);
4399
0
#endif
4400
0
    mp->no_imports = 0; /* Note that we've read the file */
4401
0
    File = mp->file;
4402
0
    mibLine = 1;
4403
0
    current_module = mp->modid;
4404
    /*
4405
     * Parse the file
4406
     */
4407
0
    np = parse(fp);
4408
0
#ifdef HAVE_FUNLOCKFILE
4409
0
    funlockfile(fp);
4410
0
#endif
4411
0
    fclose(fp);
4412
0
    File = oldFile;
4413
0
    mibLine = oldLine;
4414
0
    current_module = oldModule;
4415
0
    res = !np && gMibError == MODULE_SYNTAX_ERROR ?
4416
0
        MODULE_SYNTAX_ERROR : MODULE_LOADED_OK;
4417
0
    while (np) {
4418
0
        struct node *nnp = np->next;
4419
0
        free_node(np);
4420
0
        np = nnp;
4421
0
    }
4422
0
    return res;
4423
0
}
4424
4425
/*
4426
 *  Read in the named module
4427
 *      Returns the root of the whole tree
4428
 *      (by analogy with 'read_mib')
4429
 */
4430
static int
4431
read_module_internal(const char *name)
4432
297
{
4433
297
    struct module  *mp;
4434
4435
297
    netsnmp_init_mib_internals();
4436
4437
297
    for (mp = module_head; mp; mp = mp->next)
4438
0
        if (!label_compare(mp->name, name))
4439
0
            return read_from_file(mp, name);
4440
4441
297
    return MODULE_NOT_FOUND;
4442
297
}
4443
4444
void
4445
adopt_orphans(void)
4446
2
{
4447
2
    struct node    *np = NULL, *onp;
4448
2
    struct tree    *tp;
4449
2
    int             i, adopted = 1;
4450
4451
2
    if (!orphan_nodes)
4452
2
        return;
4453
0
    init_node_hash(orphan_nodes);
4454
0
    orphan_nodes = NULL;
4455
4456
0
    while (adopted) {
4457
0
        adopted = 0;
4458
0
        for (i = 0; i < NHASHSIZE; i++)
4459
0
            if (nbuckets[i]) {
4460
0
                for (np = nbuckets[i]; np != NULL; np = np->next) {
4461
0
                    tp = find_tree_node(np->parent, -1);
4462
0
        if (tp) {
4463
0
      do_subtree(tp, &np);
4464
0
      adopted = 1;
4465
                        /*
4466
                         * if do_subtree adopted the entire bucket, stop
4467
                         */
4468
0
                        if(NULL == nbuckets[i])
4469
0
                            break;
4470
4471
                        /*
4472
                         * do_subtree may modify nbuckets, and if np
4473
                         * was adopted, np->next probably isn't an orphan
4474
                         * anymore. if np is still in the bucket (do_subtree
4475
                         * didn't adopt it) keep on plugging. otherwise
4476
                         * start over, at the top of the bucket.
4477
                         */
4478
0
                        for(onp = nbuckets[i]; onp; onp = onp->next)
4479
0
                            if(onp == np)
4480
0
                                break;
4481
0
                        if(NULL == onp) { /* not in the list */
4482
0
                            np = nbuckets[i]; /* start over */
4483
0
                        }
4484
0
        }
4485
0
    }
4486
0
            }
4487
0
    }
4488
4489
    /*
4490
     * Report on outstanding orphans
4491
     *    and link them back into the orphan list
4492
     */
4493
0
    for (i = 0; i < NHASHSIZE; i++)
4494
0
        if (nbuckets[i]) {
4495
0
            if (orphan_nodes)
4496
0
                onp = np->next = nbuckets[i];
4497
0
            else
4498
0
                onp = orphan_nodes = nbuckets[i];
4499
0
            nbuckets[i] = NULL;
4500
0
            while (onp) {
4501
0
                char            modbuf[256];
4502
0
                snmp_log(LOG_WARNING,
4503
0
                         "Cannot resolve OID in %s: %s ::= { %s %ld } at line %d in %s\n",
4504
0
                         module_name(onp->modid, modbuf),
4505
0
                         (onp->label ? onp->label : "<no label>"),
4506
0
                         (onp->parent ? onp->parent : "<no parent>"),
4507
0
                         onp->subid, onp->lineno, onp->filename);
4508
0
                np = onp;
4509
0
                onp = onp->next;
4510
0
            }
4511
0
        }
4512
0
}
4513
4514
#ifndef NETSNMP_NO_LEGACY_DEFINITIONS
4515
struct tree    *
4516
read_module(const char *name)
4517
0
{
4518
0
    return netsnmp_read_module(name);
4519
0
}
4520
#endif
4521
4522
struct tree    *
4523
netsnmp_read_module(const char *name)
4524
297
{
4525
297
    int status = 0;
4526
297
    status = read_module_internal(name);
4527
4528
297
    if (status == MODULE_NOT_FOUND) {
4529
297
        if (!read_module_replacements(name))
4530
282
            print_module_not_found(name);
4531
297
    } else if (status == MODULE_SYNTAX_ERROR) {
4532
0
        gMibError = 0;
4533
0
        gLoop = 1;
4534
4535
0
        strncat(gMibNames, " ", sizeof(gMibNames) - strlen(gMibNames) - 1);
4536
0
        strncat(gMibNames, name, sizeof(gMibNames) - strlen(gMibNames) - 1);
4537
0
    }
4538
4539
297
    return tree_head;
4540
297
}
4541
4542
/*
4543
 * Prototype definition 
4544
 */
4545
void            unload_module_by_ID(int modID, struct tree *tree_top);
4546
4547
void
4548
unload_module_by_ID(int modID, struct tree *tree_top)
4549
0
{
4550
0
    struct tree    *tp, *next;
4551
0
    int             i;
4552
4553
0
    for (tp = tree_top; tp; tp = next) {
4554
        /*
4555
         * Essentially, this is equivalent to the code fragment:
4556
         *      if (tp->modID == modID)
4557
         *        tp->number_modules--;
4558
         * but handles one tree node being part of several modules,
4559
         * and possible multiple copies of the same module ID.
4560
         */
4561
0
        int             nmod = tp->number_modules;
4562
0
        if (nmod > 0) {         /* in some module */
4563
            /*
4564
             * Remove all copies of this module ID
4565
             */
4566
0
            int             cnt = 0, *pi1, *pi2 = tp->module_list;
4567
0
            for (i = 0, pi1 = pi2; i < nmod; i++, pi2++) {
4568
0
                if (*pi2 == modID)
4569
0
                    continue;
4570
0
                cnt++;
4571
0
                *pi1++ = *pi2;
4572
0
            }
4573
0
            if (nmod != cnt) {  /* in this module */
4574
                /*
4575
                 * if ( (nmod - cnt) > 1)
4576
                 * printf("Dup modid %d,  %d times, '%s'\n", tp->modid, (nmod-cnt), tp->label); fflush(stdout); ?* XXDEBUG 
4577
                 */
4578
0
                tp->number_modules = cnt;
4579
0
                switch (cnt) {
4580
0
                case 0:
4581
0
                    tp->module_list[0] = -1;    /* Mark unused, */
4582
0
        NETSNMP_FALLTHROUGH;
4583
4584
0
                case 1:        /* save the remaining module */
4585
0
                    if (&(tp->modid) != tp->module_list) {
4586
0
                        tp->modid = tp->module_list[0];
4587
0
                        free(tp->module_list);
4588
0
                        tp->module_list = &(tp->modid);
4589
0
                    }
4590
0
                    break;
4591
4592
0
                default:
4593
0
                    break;
4594
0
                }
4595
0
            }                   /* if tree node is in this module */
4596
0
        }
4597
        /*
4598
         * if tree node is in some module 
4599
         */
4600
0
        next = tp->next_peer;
4601
4602
4603
        /*
4604
         *  OK - that's dealt with *this* node.
4605
         *    Now let's look at the children.
4606
         *    (Isn't recursion wonderful!)
4607
         */
4608
0
        if (tp->child_list)
4609
0
            unload_module_by_ID(modID, tp->child_list);
4610
4611
4612
0
        if (tp->number_modules == 0) {
4613
            /*
4614
             * This node isn't needed any more (except perhaps
4615
             * for the sake of the children) 
4616
             */
4617
0
            if (tp->child_list == NULL) {
4618
0
                unlink_tree(tp);
4619
0
                free_tree(tp);
4620
0
            } else {
4621
0
                free_partial_tree(tp, TRUE);
4622
0
            }
4623
0
        }
4624
0
    }
4625
0
}
4626
4627
#ifndef NETSNMP_NO_LEGACY_DEFINITIONS
4628
int
4629
unload_module(const char *name)
4630
0
{
4631
0
    return netsnmp_unload_module(name);
4632
0
}
4633
#endif
4634
4635
int
4636
netsnmp_unload_module(const char *name)
4637
0
{
4638
0
    struct module  *mp;
4639
0
    int             modID = -1;
4640
4641
0
    for (mp = module_head; mp; mp = mp->next)
4642
0
        if (!label_compare(mp->name, name)) {
4643
0
            modID = mp->modid;
4644
0
            break;
4645
0
        }
4646
4647
0
    if (modID == -1) {
4648
0
        DEBUGMSGTL(("unload-mib", "Module %s not found to unload\n",
4649
0
                    name));
4650
0
        return MODULE_NOT_FOUND;
4651
0
    }
4652
0
    unload_module_by_ID(modID, tree_head);
4653
0
    mp->no_imports = -1;        /* mark as unloaded */
4654
0
    return MODULE_LOADED_OK;    /* Well, you know what I mean! */
4655
0
}
4656
4657
/*
4658
 * Clear module map, tree nodes, textual convention table.
4659
 */
4660
void
4661
unload_all_mibs(void)
4662
0
{
4663
0
    struct module  *mp;
4664
0
    struct module_compatability *mcp;
4665
0
    struct tc      *ptc;
4666
0
    unsigned int    i;
4667
4668
0
    for (mcp = module_map_head; mcp; mcp = module_map_head) {
4669
0
        if (mcp == module_map)
4670
0
            break;
4671
0
        module_map_head = mcp->next;
4672
0
        if (mcp->tag) free(NETSNMP_REMOVE_CONST(char *, mcp->tag));
4673
0
        free(NETSNMP_REMOVE_CONST(char *, mcp->old_module));
4674
0
        free(NETSNMP_REMOVE_CONST(char *, mcp->new_module));
4675
0
        free(mcp);
4676
0
    }
4677
4678
0
    for (mp = module_head; mp; mp = module_head) {
4679
0
        struct module_import *mi = mp->imports;
4680
0
        if (mi) {
4681
0
            for (i = 0; i < (unsigned int)mp->no_imports; ++i) {
4682
0
                SNMP_FREE((mi + i)->label);
4683
0
            }
4684
0
            mp->no_imports = 0;
4685
0
            if (mi == root_imports)
4686
0
                memset(mi, 0, sizeof(*mi));
4687
0
            else
4688
0
                free(mi);
4689
0
        }
4690
4691
0
        unload_module_by_ID(mp->modid, tree_head);
4692
0
        module_head = mp->next;
4693
0
        free(mp->name);
4694
0
        free(mp->file);
4695
0
        free(mp);
4696
0
    }
4697
0
    unload_module_by_ID(-1, tree_head);
4698
    /*
4699
     * tree nodes are cleared 
4700
     */
4701
4702
0
    for (i = 0, ptc = tclist; i < tc_alloc; i++, ptc++) {
4703
0
        if (ptc->type == 0)
4704
0
            continue;
4705
0
        free_enums(&ptc->enums);
4706
0
        free_ranges(&ptc->ranges);
4707
0
        free(ptc->descriptor);
4708
0
        if (ptc->hint)
4709
0
            free(ptc->hint);
4710
0
        if (ptc->description)
4711
0
            free(ptc->description);
4712
0
    }
4713
0
    SNMP_FREE(tclist);
4714
0
    tc_alloc = 0;
4715
4716
0
    memset(buckets, 0, sizeof(buckets));
4717
0
    memset(nbuckets, 0, sizeof(nbuckets));
4718
0
    memset(tbuckets, 0, sizeof(tbuckets));
4719
4720
0
    for (i = 0; i < sizeof(root_imports) / sizeof(root_imports[0]); i++) {
4721
0
        SNMP_FREE(root_imports[i].label);
4722
0
    }
4723
4724
0
    max_module = 0;
4725
0
    current_module = 0;
4726
0
    module_map_head = NULL;
4727
0
    SNMP_FREE(last_err_module);
4728
4729
0
    {
4730
0
        struct node *np = orphan_nodes;
4731
4732
0
        while (np) {
4733
0
            struct node *nnp = np->next;
4734
0
            free_node(np);
4735
0
            np = nnp;
4736
0
        }
4737
0
        orphan_nodes = NULL;
4738
0
    }
4739
0
    erroneousMibs = 0;
4740
0
    first_err_module = 1;
4741
0
}
4742
4743
static void
4744
new_module(const char *name, const char *file)
4745
0
{
4746
0
    struct module  *mp;
4747
4748
0
    for (mp = module_head; mp; mp = mp->next)
4749
0
        if (!label_compare(mp->name, name)) {
4750
0
            DEBUGMSGTL(("parse-mibs", "  Module %s already noted\n", name));
4751
            /*
4752
             * Not the same file 
4753
             */
4754
0
            if (label_compare(mp->file, file)) {
4755
0
                DEBUGMSGTL(("parse-mibs", "    %s is now in %s\n",
4756
0
                            name, file));
4757
0
                if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
4758
0
               NETSNMP_DS_LIB_MIB_WARNINGS)) {
4759
0
                    snmp_log(LOG_WARNING,
4760
0
                             "Warning: Module %s was in %s now is %s\n",
4761
0
                             name, mp->file, file);
4762
0
    }
4763
4764
                /*
4765
                 * Use the new one in preference 
4766
                 */
4767
0
                free(mp->file);
4768
0
                mp->file = strdup(file);
4769
0
            }
4770
0
            return;
4771
0
        }
4772
4773
    /*
4774
     * Add this module to the list 
4775
     */
4776
0
    DEBUGMSGTL(("parse-mibs", "  Module %d %s is in %s\n", max_module,
4777
0
                name, file));
4778
0
    mp = calloc(1, sizeof(struct module));
4779
0
    if (mp == NULL)
4780
0
        return;
4781
0
    mp->name = strdup(name);
4782
0
    mp->file = strdup(file);
4783
0
    if (mp->name == NULL || mp->file == NULL) {
4784
0
        free(mp->name);
4785
0
        free(mp->file);
4786
0
        free(mp);
4787
0
        return;
4788
0
    }
4789
0
    mp->imports = NULL;
4790
0
    mp->no_imports = -1;        /* Not yet loaded */
4791
0
    mp->modid = max_module;
4792
0
    ++max_module;
4793
4794
0
    mp->next = module_head;     /* Or add to the *end* of the list? */
4795
0
    module_head = mp;
4796
0
}
4797
4798
4799
static void
4800
scan_objlist(struct node *root, struct module *mp, struct objgroup *list, const char *error)
4801
0
{
4802
0
    int             oLine = mibLine;
4803
4804
0
    while (list) {
4805
0
        struct objgroup *gp = list;
4806
0
        struct node    *np;
4807
0
        list = list->next;
4808
0
        np = root;
4809
0
        while (np)
4810
0
            if (label_compare(np->label, gp->name))
4811
0
                np = np->next;
4812
0
            else
4813
0
                break;
4814
0
        if (!np) {
4815
0
      int i;
4816
0
      struct module_import *mip;
4817
      /* if not local, check if it was IMPORTed */
4818
0
      for (i = 0, mip = mp->imports; i < mp->no_imports; i++, mip++)
4819
0
    if (strcmp(mip->label, gp->name) == 0)
4820
0
        break;
4821
0
      if (i == mp->no_imports) {
4822
0
    mibLine = gp->line;
4823
0
    print_error(error, gp->name, QUOTESTRING);
4824
0
      }
4825
0
        }
4826
0
        free(gp->name);
4827
0
        free(gp);
4828
0
    }
4829
0
    mibLine = oLine;
4830
0
}
4831
4832
static void free_objgroup(struct objgroup *o)
4833
0
{
4834
0
    while (o) {
4835
0
        struct objgroup *next = o->next;
4836
4837
0
        free(o->name);
4838
0
        free(o);
4839
0
        o = next;
4840
0
    }
4841
0
}
4842
4843
/*
4844
 * Hash table that maps node labels onto the nodes that have been parsed so
4845
 * far by parse(). Used to detect duplicate objects in O(1) instead of
4846
 * scanning the entire list of parsed nodes for every new node.
4847
 *
4848
 * For each label, two nodes are stored: the first node with that label and
4849
 * the first node with that label whose subid differs from the subid of the
4850
 * first node. This is sufficient to determine whether a node with a given
4851
 * label and subid exists that has a different subid.
4852
 */
4853
struct label_entry {
4854
    struct label_entry *next;
4855
    unsigned int    hash;
4856
    struct node    *first;
4857
    struct node    *other;
4858
};
4859
4860
struct label_table {
4861
    struct label_entry **buckets;
4862
    size_t          size;
4863
    size_t          count;
4864
};
4865
4866
static unsigned int
4867
label_hash(const char *label)
4868
0
{
4869
0
    unsigned int    h = 2166136261u;
4870
4871
0
    for (; *label; label++) {
4872
0
        h ^= (unsigned char)*label;
4873
0
        h *= 16777619u;
4874
0
    }
4875
0
    return h;
4876
0
}
4877
4878
static struct label_entry *
4879
label_table_find(const struct label_table *lt, const char *label,
4880
                 unsigned int hash)
4881
0
{
4882
0
    struct label_entry *le;
4883
4884
0
    if (!lt->size)
4885
0
        return NULL;
4886
0
    for (le = lt->buckets[hash & (lt->size - 1)]; le; le = le->next)
4887
0
        if (le->hash == hash && strcmp(le->first->label, label) == 0)
4888
0
            return le;
4889
0
    return NULL;
4890
0
}
4891
4892
static int
4893
label_table_grow(struct label_table *lt)
4894
0
{
4895
0
    size_t          new_size = lt->size ? 2 * lt->size : 64;
4896
0
    struct label_entry **new_buckets;
4897
0
    size_t          i;
4898
4899
0
    new_buckets = calloc(new_size, sizeof(*new_buckets));
4900
0
    if (!new_buckets)
4901
0
        return -1;
4902
0
    for (i = 0; i < lt->size; i++) {
4903
0
        struct label_entry *le, *next;
4904
4905
0
        for (le = lt->buckets[i]; le; le = next) {
4906
0
            struct label_entry **head =
4907
0
                &new_buckets[le->hash & (new_size - 1)];
4908
4909
0
            next = le->next;
4910
0
            le->next = *head;
4911
0
            *head = le;
4912
0
        }
4913
0
    }
4914
0
    free(lt->buckets);
4915
0
    lt->buckets = new_buckets;
4916
0
    lt->size = new_size;
4917
0
    return 0;
4918
0
}
4919
4920
/*
4921
 * Returns a node with label @np->label and a subid that differs from
4922
 * @np->subid or NULL if no such node has been added to @lt.
4923
 */
4924
static struct node *
4925
label_table_find_dup(const struct label_table *lt, const struct node *np)
4926
0
{
4927
0
    struct label_entry *le;
4928
4929
0
    if (!np->label)
4930
0
        return NULL;
4931
0
    le = label_table_find(lt, np->label, label_hash(np->label));
4932
0
    if (!le)
4933
0
        return NULL;
4934
0
    if (le->first->subid != np->subid)
4935
0
        return le->first;
4936
0
    return le->other;
4937
0
}
4938
4939
/*
4940
 * Adds @np to @lt. If memory allocation fails, @np is not added. The only
4941
 * consequence of this is that a duplicate object warning may be missed.
4942
 */
4943
static void
4944
label_table_add(struct label_table *lt, struct node *np)
4945
0
{
4946
0
    struct label_entry *le, **head;
4947
0
    unsigned int    hash;
4948
4949
0
    if (!np->label)
4950
0
        return;
4951
0
    hash = label_hash(np->label);
4952
0
    le = label_table_find(lt, np->label, hash);
4953
0
    if (le) {
4954
0
        if (!le->other && le->first->subid != np->subid)
4955
0
            le->other = np;
4956
0
        return;
4957
0
    }
4958
0
    if (lt->count >= lt->size && label_table_grow(lt) < 0)
4959
0
        return;
4960
0
    le = malloc(sizeof(*le));
4961
0
    if (!le)
4962
0
        return;
4963
0
    le->hash = hash;
4964
0
    le->first = np;
4965
0
    le->other = NULL;
4966
0
    head = &lt->buckets[hash & (lt->size - 1)];
4967
0
    le->next = *head;
4968
0
    *head = le;
4969
0
    lt->count++;
4970
0
}
4971
4972
static void
4973
label_table_clear(struct label_table *lt)
4974
0
{
4975
0
    size_t          i;
4976
4977
0
    for (i = 0; i < lt->size; i++) {
4978
0
        struct label_entry *le, *next;
4979
4980
0
        for (le = lt->buckets[i]; le; le = next) {
4981
0
            next = le->next;
4982
0
            free(le);
4983
0
        }
4984
0
    }
4985
0
    free(lt->buckets);
4986
0
    lt->buckets = NULL;
4987
0
    lt->size = 0;
4988
0
    lt->count = 0;
4989
0
}
4990
4991
/*
4992
 * Parses a mib file and returns a linked list of nodes found in the file.
4993
 * Returns NULL on error.
4994
 */
4995
static struct node *
4996
parse(FILE * fp)
4997
0
{
4998
#ifdef TEST
4999
    extern void     xmalloc_stats(FILE *);
5000
#endif
5001
0
    char            token[MAXTOKEN];
5002
0
    char            name[MAXTOKEN+1];
5003
0
    int             type = LABEL;
5004
0
    int             lasttype = LABEL;
5005
5006
0
#define BETWEEN_MIBS          1
5007
0
#define IN_MIB                2
5008
0
    int             state = BETWEEN_MIBS;
5009
0
    struct node    *np = NULL, *root = NULL;
5010
0
    struct label_table labels = { NULL, 0, 0 };
5011
0
    struct objgroup *oldgroups = objgroups, *oldobjects = objects,
5012
0
                    *oldnotifs = notifs;
5013
5014
0
    DEBUGMSGTL(("parse-file", "Parsing file:  %s...\n", File));
5015
5016
0
    if (last_err_module)
5017
0
        free(last_err_module);
5018
0
    last_err_module = NULL;
5019
0
    objgroups = NULL;
5020
0
    objects = NULL;
5021
0
    notifs = NULL;
5022
5023
0
    while (type != ENDOFFILE) {
5024
0
        struct node *nnp;
5025
5026
0
        if (lasttype == CONTINUE)
5027
0
            lasttype = type;
5028
0
        else
5029
0
            type = lasttype = get_token(fp, token, MAXTOKEN);
5030
5031
0
        switch (type) {
5032
0
        case END:
5033
0
            if (state != IN_MIB) {
5034
0
                print_error("Error, END before start of MIB", NULL, type);
5035
0
                gMibError = MODULE_SYNTAX_ERROR;
5036
0
                goto free_mib;
5037
0
            } else {
5038
0
                struct module  *mp;
5039
#ifdef TEST
5040
                printf("\nNodes for Module %s:\n", name);
5041
                print_nodes(stdout, root);
5042
#endif
5043
0
                for (mp = module_head; mp; mp = mp->next)
5044
0
                    if (mp->modid == current_module)
5045
0
                        break;
5046
0
                scan_objlist(root, mp, objgroups, "Undefined OBJECT-GROUP");
5047
0
                scan_objlist(root, mp, objects, "Undefined OBJECT");
5048
0
                scan_objlist(root, mp, notifs, "Undefined NOTIFICATION");
5049
0
                objgroups = NULL;
5050
0
                objects = NULL;
5051
0
                notifs = NULL;
5052
0
                do_linkup(mp, root);
5053
0
                np = root = NULL;
5054
0
                label_table_clear(&labels);
5055
0
            }
5056
0
            state = BETWEEN_MIBS;
5057
#ifdef TEST
5058
            if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
5059
           NETSNMP_DS_LIB_MIB_WARNINGS)) {
5060
                /* xmalloc_stats(stderr); */
5061
      }
5062
#endif
5063
0
            continue;
5064
0
        case IMPORTS:
5065
0
            parse_imports(fp);
5066
0
            continue;
5067
0
        case EXPORTS:
5068
0
            while (type != SEMI && type != ENDOFFILE)
5069
0
                type = get_token(fp, token, MAXTOKEN);
5070
0
            continue;
5071
0
        case LABEL:
5072
0
        case INTEGER:
5073
0
        case INTEGER32:
5074
0
        case UINTEGER32:
5075
0
        case UNSIGNED32:
5076
0
        case COUNTER:
5077
0
        case COUNTER64:
5078
0
        case GAUGE:
5079
0
        case IPADDR:
5080
0
        case NETADDR:
5081
0
        case NSAPADDRESS:
5082
0
        case OBJSYNTAX:
5083
0
        case APPSYNTAX:
5084
0
        case SIMPLESYNTAX:
5085
0
        case OBJNAME:
5086
0
        case NOTIFNAME:
5087
0
        case KW_OPAQUE:
5088
0
        case TIMETICKS:
5089
0
            break;
5090
0
        case ENDOFFILE:
5091
0
            continue;
5092
0
        default:
5093
0
            strlcpy(name, token, sizeof(name));
5094
0
            type = get_token(fp, token, MAXTOKEN);
5095
0
            nnp = NULL;
5096
0
            if (type == MACRO) {
5097
0
                nnp = parse_macro(fp, name);
5098
0
                if (nnp == NULL) {
5099
0
                    print_error("Bad parse of MACRO", NULL, type);
5100
0
                    gMibError = MODULE_SYNTAX_ERROR;
5101
0
                }
5102
0
                free_node(nnp); /* IGNORE */
5103
0
                nnp = NULL;
5104
0
            } else
5105
0
                print_error(name, "is a reserved word", lasttype);
5106
0
            continue;           /* see if we can parse the rest of the file */
5107
0
        }
5108
0
        strlcpy(name, token, sizeof(name));
5109
0
        type = get_token(fp, token, MAXTOKEN);
5110
0
        nnp = NULL;
5111
5112
        /*
5113
         * Handle obsolete method to assign an object identifier to a
5114
         * module
5115
         */
5116
0
        if (lasttype == LABEL && type == LEFTBRACKET) {
5117
0
            while (type != RIGHTBRACKET && type != ENDOFFILE)
5118
0
                type = get_token(fp, token, MAXTOKEN);
5119
0
            if (type == ENDOFFILE) {
5120
0
                print_error("Expected \"}\"", token, type);
5121
0
                gMibError = MODULE_SYNTAX_ERROR;
5122
0
                goto free_mib;
5123
0
            }
5124
0
            type = get_token(fp, token, MAXTOKEN);
5125
0
        }
5126
5127
0
        switch (type) {
5128
0
        case DEFINITIONS:
5129
0
            if (state != BETWEEN_MIBS) {
5130
0
                print_error("Error, nested MIBS", NULL, type);
5131
0
                gMibError = MODULE_SYNTAX_ERROR;
5132
0
                goto free_mib;
5133
0
            }
5134
0
            state = IN_MIB;
5135
0
            current_module = which_module(name);
5136
0
            free_objgroup(objgroups);
5137
0
            objgroups = NULL;
5138
0
            free_objgroup(objects);
5139
0
            objects = NULL;
5140
0
            free_objgroup(notifs);
5141
0
            notifs = NULL;
5142
0
            if (current_module == -1) {
5143
0
                new_module(name, File);
5144
0
                current_module = which_module(name);
5145
0
            }
5146
0
            {
5147
0
                struct module  *mp;
5148
5149
0
                for (mp = module_head; mp; mp = mp->next) {
5150
0
                    if (mp->modid == current_module) {
5151
0
                        if (mp->no_imports == -1)
5152
0
                            mp->no_imports = 0;
5153
0
                        break;
5154
0
                    }
5155
0
                }
5156
0
            }
5157
0
            DEBUGMSGTL(("parse-mibs", "Parsing MIB: %d %s\n",
5158
0
                        current_module, name));
5159
0
            while ((type = get_token(fp, token, MAXTOKEN)) != ENDOFFILE)
5160
0
                if (type == BEGIN)
5161
0
                    break;
5162
0
            break;
5163
0
        case OBJTYPE:
5164
0
            nnp = parse_objecttype(fp, name);
5165
0
            if (nnp == NULL) {
5166
0
                print_error("Bad parse of OBJECT-TYPE", NULL, type);
5167
0
                gMibError = MODULE_SYNTAX_ERROR;
5168
0
                goto free_mib;
5169
0
            }
5170
0
            break;
5171
0
        case OBJGROUP:
5172
0
            nnp = parse_objectgroup(fp, name, OBJECTS, &objects);
5173
0
            if (nnp == NULL) {
5174
0
                print_error("Bad parse of OBJECT-GROUP", NULL, type);
5175
0
                gMibError = MODULE_SYNTAX_ERROR;
5176
0
                goto free_mib;
5177
0
            }
5178
0
            break;
5179
0
        case NOTIFGROUP:
5180
0
            nnp = parse_objectgroup(fp, name, NOTIFICATIONS, &notifs);
5181
0
            if (nnp == NULL) {
5182
0
                print_error("Bad parse of NOTIFICATION-GROUP", NULL, type);
5183
0
                gMibError = MODULE_SYNTAX_ERROR;
5184
0
                goto free_mib;
5185
0
            }
5186
0
            break;
5187
0
        case TRAPTYPE:
5188
0
            nnp = parse_trapDefinition(fp, name);
5189
0
            if (nnp == NULL) {
5190
0
                print_error("Bad parse of TRAP-TYPE", NULL, type);
5191
0
                gMibError = MODULE_SYNTAX_ERROR;
5192
0
                goto free_mib;
5193
0
            }
5194
0
            break;
5195
0
        case NOTIFTYPE:
5196
0
            nnp = parse_notificationDefinition(fp, name);
5197
0
            if (nnp == NULL) {
5198
0
                print_error("Bad parse of NOTIFICATION-TYPE", NULL, type);
5199
0
                gMibError = MODULE_SYNTAX_ERROR;
5200
0
                goto free_mib;
5201
0
            }
5202
0
            break;
5203
0
        case COMPLIANCE:
5204
0
            nnp = parse_compliance(fp, name);
5205
0
            if (nnp == NULL) {
5206
0
                print_error("Bad parse of MODULE-COMPLIANCE", NULL, type);
5207
0
                gMibError = MODULE_SYNTAX_ERROR;
5208
0
                goto free_mib;
5209
0
            }
5210
0
            break;
5211
0
        case AGENTCAP:
5212
0
            nnp = parse_capabilities(fp, name);
5213
0
            if (nnp == NULL) {
5214
0
                print_error("Bad parse of AGENT-CAPABILITIES", NULL, type);
5215
0
                gMibError = MODULE_SYNTAX_ERROR;
5216
0
                goto free_mib;
5217
0
            }
5218
0
            break;
5219
0
        case MACRO:
5220
0
            nnp = parse_macro(fp, name);
5221
0
            if (nnp == NULL) {
5222
0
                print_error("Bad parse of MACRO", NULL, type);
5223
0
                gMibError = MODULE_SYNTAX_ERROR;
5224
0
            }
5225
0
            free_node(nnp);     /* IGNORE */
5226
0
            nnp = NULL;
5227
0
            break;
5228
0
        case MODULEIDENTITY:
5229
0
            nnp = parse_moduleIdentity(fp, name);
5230
0
            if (nnp == NULL) {
5231
0
                print_error("Bad parse of MODULE-IDENTITY", NULL, type);
5232
0
                gMibError = MODULE_SYNTAX_ERROR;
5233
0
                goto free_mib;
5234
0
            }
5235
0
            break;
5236
0
        case OBJIDENTITY:
5237
0
            nnp = parse_objectgroup(fp, name, OBJECTS, &objects);
5238
0
            if (nnp == NULL) {
5239
0
                print_error("Bad parse of OBJECT-IDENTITY", NULL, type);
5240
0
                gMibError = MODULE_SYNTAX_ERROR;
5241
0
                goto free_mib;
5242
0
            }
5243
0
            break;
5244
0
        case OBJECT:
5245
0
            type = get_token(fp, token, MAXTOKEN);
5246
0
            if (type != IDENTIFIER) {
5247
0
                print_error("Expected IDENTIFIER", token, type);
5248
0
                gMibError = MODULE_SYNTAX_ERROR;
5249
0
                goto free_mib;
5250
0
            }
5251
0
            type = get_token(fp, token, MAXTOKEN);
5252
0
            if (type != EQUALS) {
5253
0
                print_error("Expected \"::=\"", token, type);
5254
0
                gMibError = MODULE_SYNTAX_ERROR;
5255
0
                goto free_mib;
5256
0
            }
5257
0
            nnp = parse_objectid(fp, name);
5258
0
            if (nnp == NULL) {
5259
0
                print_error("Bad parse of OBJECT IDENTIFIER", NULL, type);
5260
0
                gMibError = MODULE_SYNTAX_ERROR;
5261
0
                goto free_mib;
5262
0
            }
5263
0
            break;
5264
0
        case EQUALS:
5265
0
            nnp = parse_asntype(fp, name, &type, token);
5266
0
            lasttype = CONTINUE;
5267
0
            break;
5268
0
        case ENDOFFILE:
5269
0
            break;
5270
0
        default:
5271
0
            print_error("Bad operator", token, type);
5272
0
            gMibError = MODULE_SYNTAX_ERROR;
5273
0
            goto free_mib;
5274
0
        }
5275
0
        if (nnp) {
5276
0
            struct node *op = label_table_find_dup(&labels, nnp);
5277
5278
0
            if (op) {
5279
0
                struct node *next_node;
5280
5281
0
                snmp_log(LOG_ERR, 
5282
0
                    "Duplicate Object '%s' at line %d in %s. First at line %d\n",
5283
0
                    op->label, mibLine, File, op->lineno);
5284
0
                erroneousMibs++;
5285
0
                while (nnp) {
5286
0
                    next_node = nnp->next;
5287
0
                    free_node(nnp);
5288
0
                    nnp = next_node;
5289
0
                }
5290
0
            } else {
5291
0
                if (np)
5292
0
                    np->next = nnp;
5293
0
                else
5294
0
                    np = root = nnp;
5295
0
                label_table_add(&labels, np);
5296
0
                while (np->next) {
5297
0
                    np = np->next;
5298
0
                    label_table_add(&labels, np);
5299
0
                }
5300
0
                if (np->type == TYPE_OTHER)
5301
0
                    np->type = type;
5302
0
            }
5303
0
        }
5304
0
    }
5305
0
    DEBUGMSGTL(("parse-file", "End of file (%s)\n", File));
5306
0
    label_table_clear(&labels);
5307
0
    free_objgroup(objgroups);
5308
0
    objgroups = oldgroups;
5309
0
    free_objgroup(objects);
5310
0
    objects = oldobjects;
5311
0
    free_objgroup(notifs);
5312
0
    notifs = oldnotifs;
5313
0
    return root;
5314
5315
0
free_mib:
5316
0
    label_table_clear(&labels);
5317
0
    for (; root; root = np) {
5318
0
        np = root->next;
5319
0
        free_node(root);
5320
0
    }
5321
0
    free_objgroup(objgroups);
5322
0
    objgroups = oldgroups;
5323
0
    free_objgroup(objects);
5324
0
    objects = oldobjects;
5325
0
    free_objgroup(notifs);
5326
0
    notifs = oldnotifs;
5327
0
    return NULL;
5328
0
}
5329
5330
/*
5331
 * return zero if character is not a label character. 
5332
 */
5333
static int
5334
is_labelchar(int ich)
5335
0
{
5336
0
    netsnmp_assert(ich == EOF || (0 <= ich && ich < 256));
5337
0
    if ((isalnum(ich)) || (ich == '-'))
5338
0
        return 1;
5339
0
    if (ich == '_' && netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
5340
0
               NETSNMP_DS_LIB_MIB_PARSE_LABEL)) {
5341
0
        return 1;
5342
0
    }
5343
5344
0
    return 0;
5345
0
}
5346
5347
/**
5348
 * Read a single character from a file. Assumes that the caller has invoked
5349
 * flockfile(). Uses fgetc_unlocked() instead of getc() since the former is
5350
 * implemented as an inline function in glibc. See also bug 3447196
5351
 * (http://sourceforge.net/tracker/?func=detail&aid=3447196&group_id=12694&atid=112694).
5352
 */
5353
static int netsnmp_getc(FILE *stream)
5354
0
{
5355
0
#ifdef HAVE_FGETC_UNLOCKED
5356
0
    return fgetc_unlocked(stream);
5357
#else
5358
    return getc(stream);
5359
#endif
5360
0
}
5361
5362
/*
5363
 * Parses a token from the file.  The type of the token parsed is returned,
5364
 * and the text is placed in the string pointed to by token.
5365
 * Warning: this method may recurse.
5366
 */
5367
static int
5368
get_token(FILE *const fp, char *const token, const int maxtlen)
5369
0
{
5370
0
    int             ch, ch_next;
5371
0
    char           *cp;
5372
0
    int             hash;
5373
0
    struct tok     *tp;
5374
0
    int             too_long;
5375
0
    enum { bdigits, xdigits, other } seenSymbols;
5376
5377
0
fetch_next_token:
5378
0
    cp = token;
5379
0
    hash = 0;
5380
0
    too_long = 0;
5381
    /*
5382
     * skip all white space 
5383
     */
5384
0
    do {
5385
0
        ch = netsnmp_getc(fp);
5386
0
        if (ch == '\n')
5387
0
            mibLine++;
5388
0
    }
5389
0
    while (isspace(ch) && ch != EOF);
5390
0
    *cp++ = ch;
5391
0
    *cp = '\0';
5392
0
    switch (ch) {
5393
0
    case EOF:
5394
0
        return ENDOFFILE;
5395
0
    case '"':
5396
0
        return parseQuoteString(fp, token, maxtlen);
5397
0
    case '\'':                 /* binary or hex constant */
5398
0
        seenSymbols = bdigits;
5399
0
        while ((ch = netsnmp_getc(fp)) != EOF && ch != '\'') {
5400
0
            switch (seenSymbols) {
5401
0
            case bdigits:
5402
0
                if (ch == '0' || ch == '1')
5403
0
                    break;
5404
0
                seenSymbols = xdigits;
5405
0
                NETSNMP_FALLTHROUGH;
5406
0
            case xdigits:
5407
0
                if (isxdigit(ch))
5408
0
                    break;
5409
0
                seenSymbols = other;
5410
0
                NETSNMP_FALLTHROUGH;
5411
0
            case other:
5412
0
                break;
5413
0
            }
5414
0
            if (cp - token < maxtlen - 2)
5415
0
                *cp++ = ch;
5416
0
        }
5417
0
        if (ch == '\'') {
5418
0
            unsigned long   val = 0;
5419
0
            char           *run = token + 1;
5420
0
            ch = netsnmp_getc(fp);
5421
0
            switch (ch) {
5422
0
            case EOF:
5423
0
                return ENDOFFILE;
5424
0
            case 'b':
5425
0
            case 'B':
5426
0
                if (seenSymbols > bdigits) {
5427
0
                    *cp++ = '\'';
5428
0
                    *cp = 0;
5429
0
                    return LABEL;
5430
0
                }
5431
0
                while (run != cp)
5432
0
                    val = val * 2 + *run++ - '0';
5433
0
                break;
5434
0
            case 'h':
5435
0
            case 'H':
5436
0
                if (seenSymbols > xdigits) {
5437
0
                    *cp++ = '\'';
5438
0
                    *cp = 0;
5439
0
                    return LABEL;
5440
0
                }
5441
0
                while (run != cp) {
5442
0
                    ch = *run++;
5443
0
                    if ('0' <= ch && ch <= '9')
5444
0
                        val = val * 16 + ch - '0';
5445
0
                    else if ('a' <= ch && ch <= 'f')
5446
0
                        val = val * 16 + ch - 'a' + 10;
5447
0
                    else if ('A' <= ch && ch <= 'F')
5448
0
                        val = val * 16 + ch - 'A' + 10;
5449
0
                }
5450
0
                break;
5451
0
            default:
5452
0
                *cp++ = '\'';
5453
0
                *cp = 0;
5454
0
                return LABEL;
5455
0
            }
5456
0
            snprintf(token, maxtlen, "%ld", val);
5457
0
            return NUMBER;
5458
0
        } else
5459
0
            return LABEL;
5460
0
    case '(':
5461
0
        return LEFTPAREN;
5462
0
    case ')':
5463
0
        return RIGHTPAREN;
5464
0
    case '{':
5465
0
        return LEFTBRACKET;
5466
0
    case '}':
5467
0
        return RIGHTBRACKET;
5468
0
    case '[':
5469
0
        return LEFTSQBRACK;
5470
0
    case ']':
5471
0
        return RIGHTSQBRACK;
5472
0
    case ';':
5473
0
        return SEMI;
5474
0
    case ',':
5475
0
        return COMMA;
5476
0
    case '|':
5477
0
        return BAR;
5478
0
    case '.':
5479
0
        ch_next = netsnmp_getc(fp);
5480
0
        if (ch_next == '.')
5481
0
            return RANGE;
5482
0
        ungetc(ch_next, fp);
5483
0
        return LABEL;
5484
0
    case ':':
5485
0
        ch_next = netsnmp_getc(fp);
5486
0
        if (ch_next != ':') {
5487
0
            ungetc(ch_next, fp);
5488
0
            return LABEL;
5489
0
        }
5490
0
        ch_next = netsnmp_getc(fp);
5491
0
        if (ch_next != '=') {
5492
0
            ungetc(ch_next, fp);
5493
0
            return LABEL;
5494
0
        }
5495
0
        return EQUALS;
5496
0
    case '-':
5497
0
        ch_next = netsnmp_getc(fp);
5498
0
        if (ch_next == '-') {
5499
0
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
5500
0
               NETSNMP_DS_LIB_MIB_COMMENT_TERM)) {
5501
                /*
5502
                 * Treat the rest of this line as a comment. 
5503
                 */
5504
0
                while ((ch_next != EOF) && (ch_next != '\n'))
5505
0
                    ch_next = netsnmp_getc(fp);
5506
0
            } else {
5507
                /*
5508
                 * Treat the rest of the line or until another '--' as a comment 
5509
                 */
5510
                /*
5511
                 * (this is the "technically" correct way to parse comments) 
5512
                 */
5513
0
                ch = ' ';
5514
0
                ch_next = netsnmp_getc(fp);
5515
0
                while (ch_next != EOF && ch_next != '\n' &&
5516
0
                       (ch != '-' || ch_next != '-')) {
5517
0
                    ch = ch_next;
5518
0
                    ch_next = netsnmp_getc(fp);
5519
0
                }
5520
0
            }
5521
0
            if (ch_next == EOF)
5522
0
                return ENDOFFILE;
5523
0
            if (ch_next == '\n')
5524
0
                mibLine++;
5525
0
            goto fetch_next_token;
5526
0
        }
5527
0
        ungetc(ch_next, fp);
5528
0
  NETSNMP_FALLTHROUGH;
5529
0
    default:
5530
        /*
5531
         * Accumulate characters until end of token is found.  Then attempt to
5532
         * match this token as a reserved word.  If a match is found, return the
5533
         * type.  Else it is a label.
5534
         */
5535
0
        if (!is_labelchar(ch))
5536
0
            return LABEL;
5537
0
        hash += tolower(ch);
5538
0
      more:
5539
0
        while (is_labelchar(ch_next = netsnmp_getc(fp))) {
5540
0
            hash += tolower(ch_next);
5541
0
            if (cp - token < maxtlen - 1)
5542
0
                *cp++ = ch_next;
5543
0
            else
5544
0
                too_long = 1;
5545
0
        }
5546
0
        ungetc(ch_next, fp);
5547
0
        *cp = '\0';
5548
5549
0
        if (too_long)
5550
0
            print_error("Warning: token too long", token, CONTINUE);
5551
0
        for (tp = buckets[BUCKET(hash)]; tp; tp = tp->next) {
5552
0
            if ((tp->hash == hash) && (!label_compare(tp->name, token)))
5553
0
                break;
5554
0
        }
5555
0
        if (tp) {
5556
0
            if (tp->token != CONTINUE)
5557
0
                return (tp->token);
5558
0
            while (isspace((ch_next = netsnmp_getc(fp))))
5559
0
                if (ch_next == '\n')
5560
0
                    mibLine++;
5561
0
            if (ch_next == EOF)
5562
0
                return ENDOFFILE;
5563
0
            if (isalnum(ch_next)) {
5564
0
                *cp++ = ch_next;
5565
0
                hash += tolower(ch_next);
5566
0
                goto more;
5567
0
            }
5568
0
        }
5569
0
        if (token[0] == '-' || isdigit((unsigned char)(token[0]))) {
5570
0
            for (cp = token + 1; *cp; cp++)
5571
0
                if (!isdigit((unsigned char)(*cp)))
5572
0
                    return LABEL;
5573
0
            return NUMBER;
5574
0
        }
5575
0
        return LABEL;
5576
0
    }
5577
0
}
5578
5579
netsnmp_feature_child_of(parse_get_token, netsnmp_unused);
5580
#ifndef NETSNMP_FEATURE_REMOVE_PARSE_GET_TOKEN
5581
int
5582
snmp_get_token(FILE * fp, char *token, int maxtlen)
5583
0
{
5584
0
    return get_token(fp, token, maxtlen);
5585
0
}
5586
#endif /* NETSNMP_FEATURE_REMOVE_PARSE_GET_TOKEN */
5587
5588
int
5589
add_mibfile(const char* tmpstr, const char* d_name)
5590
0
{
5591
0
    FILE           *fp;
5592
0
    char            token[MAXTOKEN], token2[MAXTOKEN];
5593
5594
    /*
5595
     * which module is this 
5596
     */
5597
0
    if ((fp = fopen(tmpstr, "r")) == NULL) {
5598
0
        snmp_log_perror(tmpstr);
5599
0
        return 1;
5600
0
    }
5601
0
    DEBUGMSGTL(("parse-mibs", "Checking file: %s...\n",
5602
0
                tmpstr));
5603
0
    mibLine = 1;
5604
0
    File = tmpstr;
5605
0
    if (get_token(fp, token, MAXTOKEN) != LABEL) {
5606
0
      fclose(fp);
5607
0
      return 1;
5608
0
    }
5609
    /*
5610
     * simple test for this being a MIB 
5611
     */
5612
0
    if (get_token(fp, token2, MAXTOKEN) == DEFINITIONS) {
5613
0
        new_module(token, tmpstr);
5614
0
        fclose(fp);
5615
0
        return 0;
5616
0
    } else {
5617
0
        fclose(fp);
5618
0
        return 1;
5619
0
    }
5620
0
}
5621
5622
static int elemcmp(const void *a, const void *b)
5623
0
{
5624
0
    const char *const *s1 = a, *const *s2 = b;
5625
5626
0
    return strcmp(*s1, *s2);
5627
0
}
5628
5629
/*
5630
 * Scan a directory and return all filenames found as an array of pointers to
5631
 * directory entries (@result).
5632
 */
5633
static int scan_directory(char ***result, const char *dirname)
5634
4
{
5635
4
    DIR            *dir, *dir2;
5636
4
    struct dirent  *file;
5637
4
    char          **filenames = NULL;
5638
4
    int             fname_len, i, filename_count = 0, array_size = 0;
5639
4
    char           *tmpstr;
5640
5641
4
    *result = NULL;
5642
5643
4
    dir = opendir(dirname);
5644
4
    if (!dir)
5645
4
        return -1;
5646
5647
0
    while ((file = readdir(dir))) {
5648
        /*
5649
         * Only parse file names that don't begin with a '.'
5650
         * Also skip files ending in '~', or starting/ending
5651
         * with '#' which are typically editor backup files.
5652
         */
5653
0
        fname_len = strlen(file->d_name);
5654
0
        if (fname_len > 0 && file->d_name[0] != '.'
5655
0
            && file->d_name[0] != '#'
5656
0
            && file->d_name[fname_len-1] != '#'
5657
0
            && file->d_name[fname_len-1] != '~') {
5658
0
            if (asprintf(&tmpstr, "%s/%s", dirname, file->d_name) < 0)
5659
0
                continue;
5660
0
            dir2 = opendir(tmpstr);
5661
0
            if (dir2) {
5662
                /* file is a directory, don't read it */
5663
0
                closedir(dir2);
5664
0
            } else {
5665
0
                if (filename_count >= array_size) {
5666
0
                    char **new_filenames;
5667
5668
0
                    array_size = (array_size + 16) * 2;
5669
0
                    new_filenames = realloc(filenames,
5670
0
                                        array_size * sizeof(filenames[0]));
5671
0
                    if (!new_filenames) {
5672
0
                        free(tmpstr);
5673
0
                        for (i = 0; i < filename_count; i++)
5674
0
                            free(filenames[i]);
5675
0
                        free(filenames);
5676
0
                        closedir(dir);
5677
0
                        return -1;
5678
0
                    }
5679
0
                    filenames = new_filenames;
5680
0
                }
5681
0
                filenames[filename_count++] = tmpstr;
5682
0
                tmpstr = NULL;
5683
0
            }
5684
0
            free(tmpstr);
5685
0
        }
5686
0
    }
5687
0
    closedir(dir);
5688
5689
0
    if (filenames)
5690
0
        qsort(filenames, filename_count, sizeof(filenames[0]), elemcmp);
5691
0
    *result = filenames;
5692
5693
0
    return filename_count;
5694
0
}
5695
5696
/* For Win32 platforms, the directory does not maintain a last modification
5697
 * date that we can compare with the modification date of the .index file.
5698
 * Therefore there is no way to know whether any .index file is valid.
5699
 * This is the reason for the #if !(defined(WIN32) || defined(cygwin))
5700
 * in the add_mibdir function
5701
 */
5702
int
5703
add_mibdir(const char *dirname)
5704
4
{
5705
4
    const char     *oldFile = File;
5706
4
    int             oldLine = mibLine;
5707
4
    char          **filenames;
5708
4
    int             count = 0;
5709
4
    int             filename_count, i;
5710
5711
4
    DEBUGMSGTL(("parse-mibs", "Scanning directory %s\n", dirname));
5712
5713
4
    filename_count = scan_directory(&filenames, dirname);
5714
5715
4
    if (filename_count >= 0) {
5716
0
        for (i = 0; i < filename_count; i++) {
5717
0
            if (add_mibfile(filenames[i], strrchr(filenames[i], '/')) == 0)
5718
0
                count++;
5719
0
      free(filenames[i]);
5720
0
        }
5721
0
        File = oldFile;
5722
0
        mibLine = oldLine;
5723
0
        free(filenames);
5724
0
        return (count);
5725
0
    }
5726
4
    else
5727
4
        DEBUGMSGTL(("parse-mibs","cannot open MIB directory %s\n", dirname));
5728
5729
4
    return (-1);
5730
4
}
5731
5732
5733
/*
5734
 * Returns the root of the whole tree
5735
 *   (for backwards compatability)
5736
 */
5737
struct tree    *
5738
read_mib(const char *filename)
5739
0
{
5740
0
    FILE           *fp;
5741
0
    char            token[MAXTOKEN];
5742
0
    const char     *oldFile = File;
5743
0
    int             oldLine = mibLine;
5744
5745
0
    fp = fopen(filename, "r");
5746
0
    if (fp == NULL) {
5747
0
        snmp_log_perror(filename);
5748
0
        return NULL;
5749
0
    }
5750
0
    mibLine = 1;
5751
0
    File = filename;
5752
0
    DEBUGMSGTL(("parse-mibs", "Parsing file: %s...\n", filename));
5753
0
    if (get_token(fp, token, MAXTOKEN) != LABEL) {
5754
0
      snmp_log(LOG_ERR, "Failed to parse MIB file %s\n", filename);
5755
0
      fclose(fp);
5756
0
      File = oldFile;
5757
0
      mibLine = oldLine;
5758
0
      return NULL;
5759
0
    }
5760
0
    fclose(fp);
5761
0
    new_module(token, filename);
5762
0
    (void) netsnmp_read_module(token);
5763
5764
0
    File = oldFile;
5765
0
    mibLine = oldLine;
5766
0
    return tree_head;
5767
0
}
5768
5769
5770
struct tree    *
5771
read_all_mibs(void)
5772
0
{
5773
0
    struct module  *mp;
5774
5775
0
    for (mp = module_head; mp; mp = mp->next)
5776
0
        if (mp->no_imports == -1)
5777
0
            netsnmp_read_module(mp->name);
5778
0
    adopt_orphans();
5779
5780
    /* If entered the syntax error loop in "read_module()" */
5781
0
    if (gLoop == 1) {
5782
0
        gLoop = 0;
5783
0
        free(gpMibErrorString);
5784
0
        gpMibErrorString = NULL;
5785
0
        if (asprintf(&gpMibErrorString, "Error in parsing MIB module(s): %s !"
5786
0
                     " Unable to load corresponding MIB(s)", gMibNames) < 0) {
5787
0
            snmp_log(LOG_CRIT,
5788
0
                     "failed to allocated memory for gpMibErrorString\n");
5789
0
        }
5790
0
    }
5791
5792
    /* Caller's responsibility to free this memory */
5793
0
    tree_head->parseErrorString = gpMibErrorString;
5794
  
5795
0
    return tree_head;
5796
0
}
5797
5798
5799
#ifdef TEST
5800
int main(int argc, char *argv[])
5801
{
5802
    int             i;
5803
    struct tree    *tp;
5804
    netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_WARNINGS, 2);
5805
5806
    netsnmp_init_mib();
5807
5808
    if (argc == 1)
5809
        (void) read_all_mibs();
5810
    else
5811
        for (i = 1; i < argc; i++)
5812
            read_mib(argv[i]);
5813
5814
    for (tp = tree_head; tp; tp = tp->next_peer)
5815
        print_subtree(stdout, tp, 0);
5816
    free_tree(tree_head);
5817
5818
    return 0;
5819
}
5820
#endif                          /* TEST */
5821
5822
static int
5823
parseQuoteString(FILE * fp, char *token, int maxtlen)
5824
0
{
5825
0
    register int    ch;
5826
0
    int             count = 0;
5827
0
    int             too_long = 0;
5828
0
    char           *token_start = token;
5829
5830
0
    for (ch = netsnmp_getc(fp); ch != EOF; ch = netsnmp_getc(fp)) {
5831
0
        if (ch == '\r')
5832
0
            continue;
5833
0
        if (ch == '\n') {
5834
0
            mibLine++;
5835
0
        } else if (ch == '"') {
5836
0
            netsnmp_assert(token - token_start < maxtlen);
5837
0
            *token = '\0';
5838
0
            if (too_long && netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
5839
0
             NETSNMP_DS_LIB_MIB_WARNINGS) > 1) {
5840
                /*
5841
                 * show short form for brevity sake 
5842
                 */
5843
0
                int             truncate_at = SNMP_MIN(50, maxtlen - 1);
5844
0
                char            ch_save = *(token_start + truncate_at);
5845
5846
0
                *(token_start + truncate_at) = '\0';
5847
0
                print_error("Warning: string too long",
5848
0
                            token_start, QUOTESTRING);
5849
0
                *(token_start + truncate_at) = ch_save;
5850
0
            }
5851
0
            return QUOTESTRING;
5852
0
        }
5853
        /*
5854
         * maximum description length check.  If greater, keep parsing
5855
         * but truncate the string 
5856
         */
5857
0
        if (++count < maxtlen)
5858
0
            *token++ = ch;
5859
0
        else
5860
0
            too_long = 1;
5861
0
    }
5862
5863
0
    return 0;
5864
0
}
5865
5866
/*
5867
 * struct index_list *
5868
 * getIndexes(FILE *fp):
5869
 *   This routine parses a string like  { blah blah blah } and returns a
5870
 *   list of the strings enclosed within it.
5871
 *
5872
 */
5873
static struct index_list *
5874
getIndexes(FILE * fp, struct index_list **retp)
5875
0
{
5876
0
    int             type;
5877
0
    char            token[MAXTOKEN];
5878
0
    char            nextIsImplied = 0;
5879
5880
0
    struct index_list *mylist = NULL;
5881
0
    struct index_list **mypp = &mylist;
5882
5883
0
    free_indexes(retp);
5884
5885
0
    type = get_token(fp, token, MAXTOKEN);
5886
5887
0
    if (type != LEFTBRACKET) {
5888
0
        return NULL;
5889
0
    }
5890
5891
0
    type = get_token(fp, token, MAXTOKEN);
5892
0
    while (type != RIGHTBRACKET && type != ENDOFFILE) {
5893
0
        if ((type == LABEL) || (type & SYNTAX_MASK)) {
5894
0
            *mypp = calloc(1, sizeof(struct index_list));
5895
0
            if (*mypp) {
5896
0
                (*mypp)->ilabel = strdup(token);
5897
0
                (*mypp)->isimplied = nextIsImplied;
5898
0
                mypp = &(*mypp)->next;
5899
0
                nextIsImplied = 0;
5900
0
            }
5901
0
        } else if (type == IMPLIED) {
5902
0
            nextIsImplied = 1;
5903
0
        }
5904
0
        type = get_token(fp, token, MAXTOKEN);
5905
0
    }
5906
5907
0
    *retp = mylist;
5908
0
    return mylist;
5909
0
}
5910
5911
static struct varbind_list *
5912
getVarbinds(FILE * fp, struct varbind_list **retp)
5913
0
{
5914
0
    int             type;
5915
0
    char            token[MAXTOKEN];
5916
5917
0
    struct varbind_list *mylist = NULL;
5918
0
    struct varbind_list **mypp = &mylist;
5919
5920
0
    free_varbinds(retp);
5921
5922
0
    type = get_token(fp, token, MAXTOKEN);
5923
5924
0
    if (type != LEFTBRACKET) {
5925
0
        return NULL;
5926
0
    }
5927
5928
0
    type = get_token(fp, token, MAXTOKEN);
5929
0
    while (type != RIGHTBRACKET && type != ENDOFFILE) {
5930
0
        if ((type == LABEL) || (type & SYNTAX_MASK)) {
5931
0
            *mypp = calloc(1, sizeof(struct varbind_list));
5932
0
            if (*mypp) {
5933
0
                (*mypp)->vblabel = strdup(token);
5934
0
                mypp = &(*mypp)->next;
5935
0
            }
5936
0
        }
5937
0
        type = get_token(fp, token, MAXTOKEN);
5938
0
    }
5939
5940
0
    *retp = mylist;
5941
0
    return mylist;
5942
0
}
5943
5944
static void
5945
free_indexes(struct index_list **spp)
5946
0
{
5947
0
    if (spp && *spp) {
5948
0
        struct index_list *pp, *npp;
5949
5950
0
        pp = *spp;
5951
0
        *spp = NULL;
5952
5953
0
        while (pp) {
5954
0
            npp = pp->next;
5955
0
            if (pp->ilabel)
5956
0
                free(pp->ilabel);
5957
0
            free(pp);
5958
0
            pp = npp;
5959
0
        }
5960
0
    }
5961
0
}
5962
5963
static void
5964
free_varbinds(struct varbind_list **spp)
5965
0
{
5966
0
    if (spp && *spp) {
5967
0
        struct varbind_list *pp, *npp;
5968
5969
0
        pp = *spp;
5970
0
        *spp = NULL;
5971
5972
0
        while (pp) {
5973
0
            npp = pp->next;
5974
0
            if (pp->vblabel)
5975
0
                free(pp->vblabel);
5976
0
            free(pp);
5977
0
            pp = npp;
5978
0
        }
5979
0
    }
5980
0
}
5981
5982
static void
5983
free_ranges(struct range_list **spp)
5984
0
{
5985
0
    if (spp && *spp) {
5986
0
        struct range_list *pp, *npp;
5987
5988
0
        pp = *spp;
5989
0
        *spp = NULL;
5990
5991
0
        while (pp) {
5992
0
            npp = pp->next;
5993
0
            free(pp);
5994
0
            pp = npp;
5995
0
        }
5996
0
    }
5997
0
}
5998
5999
static void
6000
free_enums(struct enum_list **spp)
6001
0
{
6002
0
    if (spp && *spp) {
6003
0
        struct enum_list *pp, *npp;
6004
6005
0
        pp = *spp;
6006
0
        *spp = NULL;
6007
6008
0
        while (pp) {
6009
0
            npp = pp->next;
6010
0
            if (pp->label)
6011
0
                free(pp->label);
6012
0
            free(pp);
6013
0
            pp = npp;
6014
0
        }
6015
0
    }
6016
0
}
6017
6018
static struct enum_list *
6019
copy_enums(struct enum_list *sp)
6020
0
{
6021
0
    struct enum_list *xp = NULL, **spp = &xp;
6022
6023
0
    while (sp) {
6024
0
        *spp = calloc(1, sizeof(struct enum_list));
6025
0
        if (!*spp)
6026
0
            break;
6027
0
        (*spp)->label = strdup(sp->label);
6028
0
        (*spp)->value = sp->value;
6029
0
        spp = &(*spp)->next;
6030
0
        sp = sp->next;
6031
0
    }
6032
0
    return (xp);
6033
0
}
6034
6035
static struct range_list *
6036
copy_ranges(struct range_list *sp)
6037
0
{
6038
0
    struct range_list *xp = NULL, **spp = &xp;
6039
6040
0
    while (sp) {
6041
0
        *spp = calloc(1, sizeof(struct range_list));
6042
0
        if (!*spp)
6043
0
            break;
6044
0
        (*spp)->low = sp->low;
6045
0
        (*spp)->high = sp->high;
6046
0
        spp = &(*spp)->next;
6047
0
        sp = sp->next;
6048
0
    }
6049
0
    return (xp);
6050
0
}
6051
6052
/*
6053
 * This routine parses a string like  { blah blah blah } and returns OBJID if
6054
 * it is well formed, and NULL if not.
6055
 */
6056
static int
6057
tossObjectIdentifier(FILE * fp)
6058
0
{
6059
0
    int             type;
6060
0
    char            token[MAXTOKEN];
6061
0
    int             bracketcount = 1;
6062
6063
0
    type = get_token(fp, token, MAXTOKEN);
6064
6065
0
    if (type != LEFTBRACKET)
6066
0
        return 0;
6067
0
    while ((type != RIGHTBRACKET || bracketcount > 0) && type != ENDOFFILE) {
6068
0
        type = get_token(fp, token, MAXTOKEN);
6069
0
        if (type == LEFTBRACKET)
6070
0
            bracketcount++;
6071
0
        else if (type == RIGHTBRACKET)
6072
0
            bracketcount--;
6073
0
    }
6074
6075
0
    if (type == RIGHTBRACKET)
6076
0
        return OBJID;
6077
0
    else
6078
0
        return 0;
6079
0
}
6080
6081
/* Find node in any MIB module
6082
   Used by Perl modules   */
6083
struct tree    *
6084
find_node(const char *name, struct tree *subtree)
6085
0
{                               /* Unused */
6086
0
    return (find_tree_node(name, -1));
6087
0
}
6088
6089
netsnmp_feature_child_of(parse_find_node2, netsnmp_unused);
6090
#ifndef NETSNMP_FEATURE_REMOVE_PARSE_FIND_NODE2
6091
struct tree    *
6092
find_node2(const char *name, const char *module)
6093
0
{                               
6094
0
  int modid = -1;
6095
0
  if (module) {
6096
0
    modid = which_module(module);
6097
0
  }
6098
0
  if (modid == -1)
6099
0
  {
6100
0
    return (NULL);
6101
0
  }
6102
0
  return (find_tree_node(name, modid));
6103
0
}
6104
#endif /* NETSNMP_FEATURE_REMOVE_PARSE_FIND_NODE2 */
6105
6106
#ifndef NETSNMP_FEATURE_REMOVE_FIND_MODULE
6107
/* Used in the perl module */
6108
struct module  *
6109
find_module(int mid)
6110
0
{
6111
0
    struct module  *mp;
6112
6113
0
    for (mp = module_head; mp != NULL; mp = mp->next) {
6114
0
        if (mp->modid == mid)
6115
0
            break;
6116
0
    }
6117
0
    return mp;
6118
0
}
6119
#endif /* NETSNMP_FEATURE_REMOVE_FIND_MODULE */
6120
6121
6122
static char     leave_indent[256];
6123
static int      leave_was_simple;
6124
6125
static void
6126
print_mib_leaves(FILE * f, struct tree *tp, int width)
6127
0
{
6128
0
    struct tree    *ntp;
6129
0
    char           *ip = leave_indent + strlen(leave_indent) - 1;
6130
0
    char            last_ipch = *ip;
6131
6132
0
    *ip = '+';
6133
0
    if (tp->type == TYPE_OTHER || tp->type > TYPE_SIMPLE_LAST) {
6134
0
        fprintf(f, "%s--%s(%ld)\n", leave_indent, tp->label, tp->subid);
6135
0
        if (tp->indexes) {
6136
0
            struct index_list *xp = tp->indexes;
6137
0
            int             first = 1, cpos = 0, len, cmax =
6138
0
                width - strlen(leave_indent) - 12;
6139
0
            *ip = last_ipch;
6140
0
            fprintf(f, "%s  |  Index: ", leave_indent);
6141
0
            while (xp) {
6142
0
                if (first)
6143
0
                    first = 0;
6144
0
                else
6145
0
                    fprintf(f, ", ");
6146
0
                cpos += (len = strlen(xp->ilabel) + 2);
6147
0
                if (cpos > cmax) {
6148
0
                    fprintf(f, "\n");
6149
0
                    fprintf(f, "%s  |         ", leave_indent);
6150
0
                    cpos = len;
6151
0
                }
6152
0
                fprintf(f, "%s", xp->ilabel);
6153
0
                xp = xp->next;
6154
0
            }
6155
0
            fprintf(f, "\n");
6156
0
            *ip = '+';
6157
0
        }
6158
0
    } else {
6159
0
        const char     *acc, *typ;
6160
0
        int             size = 0;
6161
0
        switch (tp->access) {
6162
0
        case MIB_ACCESS_NOACCESS:
6163
0
            acc = "----";
6164
0
            break;
6165
0
        case MIB_ACCESS_READONLY:
6166
0
            acc = "-R--";
6167
0
            break;
6168
0
        case MIB_ACCESS_WRITEONLY:
6169
0
            acc = "--W-";
6170
0
            break;
6171
0
        case MIB_ACCESS_READWRITE:
6172
0
            acc = "-RW-";
6173
0
            break;
6174
0
        case MIB_ACCESS_NOTIFY:
6175
0
            acc = "---N";
6176
0
            break;
6177
0
        case MIB_ACCESS_CREATE:
6178
0
            acc = "CR--";
6179
0
            break;
6180
0
        default:
6181
0
            acc = "    ";
6182
0
            break;
6183
0
        }
6184
0
        switch (tp->type) {
6185
0
        case TYPE_OBJID:
6186
0
            typ = "ObjID    ";
6187
0
            break;
6188
0
        case TYPE_OCTETSTR:
6189
0
            typ = "String   ";
6190
0
            size = 1;
6191
0
            break;
6192
0
        case TYPE_INTEGER:
6193
0
            if (tp->enums)
6194
0
                typ = "EnumVal  ";
6195
0
            else
6196
0
                typ = "INTEGER  ";
6197
0
            break;
6198
0
        case TYPE_NETADDR:
6199
0
            typ = "NetAddr  ";
6200
0
            break;
6201
0
        case TYPE_IPADDR:
6202
0
            typ = "IpAddr   ";
6203
0
            break;
6204
0
        case TYPE_COUNTER:
6205
0
            typ = "Counter  ";
6206
0
            break;
6207
0
        case TYPE_GAUGE:
6208
0
            typ = "Gauge    ";
6209
0
            break;
6210
0
        case TYPE_TIMETICKS:
6211
0
            typ = "TimeTicks";
6212
0
            break;
6213
0
        case TYPE_OPAQUE:
6214
0
            typ = "Opaque   ";
6215
0
            size = 1;
6216
0
            break;
6217
0
        case TYPE_NULL:
6218
0
            typ = "Null     ";
6219
0
            break;
6220
0
        case TYPE_COUNTER64:
6221
0
            typ = "Counter64";
6222
0
            break;
6223
0
        case TYPE_BITSTRING:
6224
0
            typ = "BitString";
6225
0
            break;
6226
0
        case TYPE_NSAPADDRESS:
6227
0
            typ = "NsapAddr ";
6228
0
            break;
6229
0
        case TYPE_UNSIGNED32:
6230
0
            typ = "Unsigned ";
6231
0
            break;
6232
0
        case TYPE_UINTEGER:
6233
0
            typ = "UInteger ";
6234
0
            break;
6235
0
        case TYPE_INTEGER32:
6236
0
            typ = "Integer32";
6237
0
            break;
6238
0
        default:
6239
0
            typ = "         ";
6240
0
            break;
6241
0
        }
6242
0
        fprintf(f, "%s-- %s %s %s(%ld)\n", leave_indent, acc, typ,
6243
0
                tp->label, tp->subid);
6244
0
        *ip = last_ipch;
6245
0
        if (tp->tc_index >= 0)
6246
0
            fprintf(f, "%s        Textual Convention: %s\n", leave_indent,
6247
0
                    tclist[tp->tc_index].descriptor);
6248
0
        if (tp->enums) {
6249
0
            struct enum_list *ep = tp->enums;
6250
0
            int             cpos = 0, cmax =
6251
0
                width - strlen(leave_indent) - 16;
6252
0
            fprintf(f, "%s        Values: ", leave_indent);
6253
0
            while (ep) {
6254
0
                char            buf[80];
6255
0
                int             bufw;
6256
0
                if (ep != tp->enums)
6257
0
                    fprintf(f, ", ");
6258
0
                snprintf(buf, sizeof(buf), "%s(%d)", ep->label, ep->value);
6259
0
                cpos += (bufw = strlen(buf) + 2);
6260
0
                if (cpos >= cmax) {
6261
0
                    fprintf(f, "\n%s                ", leave_indent);
6262
0
                    cpos = bufw;
6263
0
                }
6264
0
                fprintf(f, "%s", buf);
6265
0
                ep = ep->next;
6266
0
            }
6267
0
            fprintf(f, "\n");
6268
0
        }
6269
0
        if (tp->ranges) {
6270
0
            struct range_list *rp = tp->ranges;
6271
0
            if (size)
6272
0
                fprintf(f, "%s        Size: ", leave_indent);
6273
0
            else
6274
0
                fprintf(f, "%s        Range: ", leave_indent);
6275
0
            while (rp) {
6276
0
                if (rp != tp->ranges)
6277
0
                    fprintf(f, " | ");
6278
0
                print_range_value(f, tp->type, rp);
6279
0
                rp = rp->next;
6280
0
            }
6281
0
            fprintf(f, "\n");
6282
0
        }
6283
0
    }
6284
0
    *ip = last_ipch;
6285
0
    strcat(leave_indent, "  |");
6286
0
    leave_was_simple = tp->type != TYPE_OTHER;
6287
6288
0
    {
6289
0
        int             i, j, count = 0;
6290
0
        struct leave {
6291
0
            oid             id;
6292
0
            struct tree    *tp;
6293
0
        }              *leaves, *lp;
6294
6295
0
        for (ntp = tp->child_list; ntp; ntp = ntp->next_peer)
6296
0
            count++;
6297
0
        if (count) {
6298
0
            leaves = calloc(count, sizeof(struct leave));
6299
0
            if (!leaves)
6300
0
                return;
6301
0
            for (ntp = tp->child_list, count = 0; ntp;
6302
0
                 ntp = ntp->next_peer) {
6303
0
                for (i = 0, lp = leaves; i < count; i++, lp++)
6304
0
                    if (lp->id >= ntp->subid)
6305
0
                        break;
6306
0
                for (j = count; j > i; j--)
6307
0
                    leaves[j] = leaves[j - 1];
6308
0
                lp->id = ntp->subid;
6309
0
                lp->tp = ntp;
6310
0
                count++;
6311
0
            }
6312
0
            for (i = 1, lp = leaves; i <= count; i++, lp++) {
6313
0
                if (!leave_was_simple || lp->tp->type == 0)
6314
0
                    fprintf(f, "%s\n", leave_indent);
6315
0
                if (i == count)
6316
0
                    ip[3] = ' ';
6317
0
                print_mib_leaves(f, lp->tp, width);
6318
0
            }
6319
0
            free(leaves);
6320
0
            leave_was_simple = 0;
6321
0
        }
6322
0
    }
6323
0
    ip[1] = 0;
6324
0
}
6325
6326
void
6327
print_mib_tree(FILE * f, struct tree *tp, int width)
6328
0
{
6329
0
    leave_indent[0] = ' ';
6330
0
    leave_indent[1] = 0;
6331
0
    leave_was_simple = 1;
6332
0
    print_mib_leaves(f, tp, width);
6333
0
}
6334
6335
6336
/*
6337
 * Merge the parsed object identifier with the existing node.
6338
 * If there is a problem with the identifier, release the existing node.
6339
 */
6340
static struct node *
6341
merge_parse_objectid(struct node *np, FILE * fp, char *name)
6342
0
{
6343
0
    struct node    *nnp;
6344
    /*
6345
     * printf("merge defval --> %s\n",np->defaultValue); 
6346
     */
6347
0
    nnp = parse_objectid(fp, name);
6348
0
    if (nnp) {
6349
6350
        /*
6351
         * apply last OID sub-identifier data to the information 
6352
         */
6353
        /*
6354
         * already collected for this node. 
6355
         */
6356
0
        struct node    *headp, *nextp;
6357
0
        int             ncount = 0;
6358
0
        nextp = headp = nnp;
6359
0
        while (nnp->next) {
6360
0
            nextp = nnp;
6361
0
            ncount++;
6362
0
            nnp = nnp->next;
6363
0
        }
6364
6365
0
        np->label = nnp->label;
6366
0
        np->subid = nnp->subid;
6367
0
        np->modid = nnp->modid;
6368
0
        np->parent = nnp->parent;
6369
0
  if (nnp->filename != NULL) {
6370
0
    free(nnp->filename);
6371
0
  }
6372
0
        free(nnp);
6373
6374
0
        if (ncount) {
6375
0
            nextp->next = np;
6376
0
            np = headp;
6377
0
        }
6378
0
    } else {
6379
0
        free_node(np);
6380
0
        np = NULL;
6381
0
    }
6382
6383
0
    return np;
6384
0
}
6385
6386
/*
6387
 * transfer data to tree from node
6388
 *
6389
 * move pointers for alloc'd data from np to tp.
6390
 * this prevents them from being freed when np is released.
6391
 * parent member is not moved.
6392
 *
6393
 * CAUTION: nodes may be repeats of existing tree nodes.
6394
 * This can happen especially when resolving IMPORT clauses.
6395
 *
6396
 */
6397
static void
6398
tree_from_node(struct tree *tp, struct node *np)
6399
0
{
6400
0
    free_partial_tree(tp, FALSE);
6401
6402
0
    tp->label = np->label;
6403
0
    np->label = NULL;
6404
0
    tp->enums = np->enums;
6405
0
    np->enums = NULL;
6406
0
    tp->ranges = np->ranges;
6407
0
    np->ranges = NULL;
6408
0
    tp->indexes = np->indexes;
6409
0
    np->indexes = NULL;
6410
0
    tp->augments = np->augments;
6411
0
    np->augments = NULL;
6412
0
    tp->varbinds = np->varbinds;
6413
0
    np->varbinds = NULL;
6414
0
    tp->hint = np->hint;
6415
0
    np->hint = NULL;
6416
0
    tp->units = np->units;
6417
0
    np->units = NULL;
6418
0
    tp->description = np->description;
6419
0
    np->description = NULL;
6420
0
    tp->reference = np->reference;
6421
0
    np->reference = NULL;
6422
0
    tp->defaultValue = np->defaultValue;
6423
0
    np->defaultValue = NULL;
6424
0
    tp->subid = np->subid;
6425
0
    tp->tc_index = np->tc_index;
6426
0
    tp->type = translation_table[np->type];
6427
0
    tp->access = np->access;
6428
0
    tp->status = np->status;
6429
6430
0
    set_function(tp);
6431
0
}
6432
6433
#endif /* NETSNMP_DISABLE_MIB_LOADING */