Coverage Report

Created: 2026-10-17 06:54

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