Coverage Report

Created: 2026-10-19 06:44

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
1
#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
67
#define SYNTAX_MASK     0x80
158
/*
159
 * types of tokens
160
 * Tokens with the SYNTAX_MASK bit set are syntax tokens 
161
 */
162
3
#define CONTINUE    -1
163
329
#define ENDOFFILE   0
164
2.81k
#define LABEL       1
165
#define SUBTREE     2
166
0
#define SYNTAX      3
167
1
#define OBJID       (4 | SYNTAX_MASK)
168
1
#define OCTETSTR    (5 | SYNTAX_MASK)
169
4
#define INTEGER     (6 | SYNTAX_MASK)
170
4
#define NETADDR     (7 | SYNTAX_MASK)
171
4
#define IPADDR      (8 | SYNTAX_MASK)
172
4
#define COUNTER     (9 | SYNTAX_MASK)
173
4
#define GAUGE       (10 | SYNTAX_MASK)
174
4
#define TIMETICKS   (11 | SYNTAX_MASK)
175
4
#define KW_OPAQUE   (12 | SYNTAX_MASK)
176
1
#define NUL         (13 | SYNTAX_MASK)
177
0
#define SEQUENCE    14
178
0
#define OF          15          /* SEQUENCE OF */
179
0
#define OBJTYPE     16
180
0
#define ACCESS      17
181
0
#define READONLY    18
182
0
#define READWRITE   19
183
0
#define WRITEONLY   20
184
#ifdef NOACCESS
185
#undef NOACCESS                 /* agent 'NOACCESS' token */
186
#endif
187
0
#define NOACCESS    21
188
0
#define STATUS      22
189
0
#define MANDATORY   23
190
0
#define KW_OPTIONAL    24
191
0
#define OBSOLETE    25
192
/*
193
 * #define RECOMMENDED 26 
194
 */
195
#define PUNCT       27
196
0
#define EQUALS      28
197
0
#define NUMBER      29
198
3
#define LEFTBRACKET 30
199
0
#define RIGHTBRACKET 31
200
0
#define LEFTPAREN   32
201
0
#define RIGHTPAREN  33
202
0
#define COMMA       34
203
0
#define DESCRIPTION 35
204
0
#define QUOTESTRING 36
205
0
#define INDEX       37
206
0
#define DEFVAL      38
207
0
#define DEPRECATED  39
208
0
#define SIZE        40
209
1
#define BITSTRING   (41 | SYNTAX_MASK)
210
4
#define NSAPADDRESS (42 | SYNTAX_MASK)
211
4
#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
4
#define UINTEGER32 (56 | SYNTAX_MASK)
225
0
#define CURRENT     57
226
0
#define DEFINITIONS 58
227
0
#define END         59
228
0
#define SEMI        60
229
1
#define TRAPTYPE    61
230
0
#define ENTERPRISE  62
231
/*
232
 * #define DISPLAYSTR (63 | SYNTAX_MASK) 
233
 */
234
0
#define BEGIN       64
235
0
#define IMPORTS     65
236
0
#define EXPORTS     66
237
0
#define ACCNOTIFY   67
238
0
#define BAR         68
239
0
#define RANGE       69
240
0
#define CONVENTION  70
241
0
#define DISPLAYHINT 71
242
0
#define FROM        72
243
1
#define AGENTCAP    73
244
0
#define MACRO       74
245
0
#define IMPLIED     75
246
0
#define SUPPORTS    76
247
0
#define INCLUDES    77
248
0
#define VARIATION   78
249
0
#define REVISION    79
250
0
#define NOTIMPL     80
251
0
#define OBJECTS     81
252
0
#define NOTIFICATIONS 82
253
0
#define MODULE      83
254
0
#define MINACCESS   84
255
0
#define PRODREL     85
256
0
#define WRSYNTAX    86
257
0
#define CREATEREQ   87
258
1
#define NOTIFGROUP  88
259
0
#define MANDATORYGROUPS 89
260
0
#define GROUP     90
261
0
#define OBJECT      91
262
0
#define IDENTIFIER  92
263
0
#define CHOICE      93
264
0
#define LEFTSQBRACK 95
265
0
#define RIGHTSQBRACK  96
266
0
#define IMPLICIT    97
267
3
#define APPSYNTAX (98 | SYNTAX_MASK)
268
3
#define OBJSYNTAX (99 | SYNTAX_MASK)
269
3
#define SIMPLESYNTAX  (100 | SYNTAX_MASK)
270
3
#define OBJNAME   (101 | SYNTAX_MASK)
271
3
#define NOTIFNAME (102 | SYNTAX_MASK)
272
0
#define VARIABLES 103
273
4
#define UNSIGNED32  (104 | SYNTAX_MASK)
274
4
#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.23k
#define MODULE_NOT_FOUND  0
514
3
#define MODULE_LOADED_OK  1
515
1.23k
#define MODULE_ALREADY_LOADED 2
516
/*
517
 * #define MODULE_LOAD_FAILED   3       
518
 */
519
0
#define MODULE_LOAD_FAILED  MODULE_NOT_FOUND
520
1.24k
#define MODULE_SYNTAX_ERROR     4
521
522
int gMibError = 0,gLoop = 0;
523
static char *gpMibErrorString;
524
char gMibNames[STRINGMAX];
525
526
607
#define HASHSIZE        32
527
607
#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
0
{
637
0
    if (options) {
638
0
        while (*options) {
639
0
            switch (*options) {
640
0
            case 'u':
641
0
                netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_PARSE_LABEL,
642
0
                               !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
643
0
                                               NETSNMP_DS_LIB_MIB_PARSE_LABEL));
644
0
                break;
645
646
0
            case 'c':
647
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID,
648
0
            NETSNMP_DS_LIB_MIB_COMMENT_TERM);
649
0
                break;
650
651
0
            case 'e':
652
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID,
653
0
            NETSNMP_DS_LIB_MIB_ERRORS);
654
0
                break;
655
656
0
            case 'w':
657
0
                netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
658
0
           NETSNMP_DS_LIB_MIB_WARNINGS, 1);
659
0
                break;
660
661
0
            case 'W':
662
0
                netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
663
0
           NETSNMP_DS_LIB_MIB_WARNINGS, 2);
664
0
                break;
665
666
0
            case 'd':
667
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, 
668
0
            NETSNMP_DS_LIB_SAVE_MIB_DESCRS);
669
0
                break;
670
671
0
            case 'R':
672
0
                netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, 
673
0
            NETSNMP_DS_LIB_MIB_REPLACE);
674
0
                break;
675
676
0
            default:
677
                /*
678
                 * return at the unknown option 
679
                 */
680
0
                return options;
681
0
            }
682
0
            options++;
683
0
        }
684
0
    }
685
0
    return NULL;
686
0
}
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.23k
{
704
1.23k
    register struct tok *tp;
705
1.23k
    register int    b, i;
706
1.23k
    int             max_modc;
707
708
1.23k
    if (tree_head)
709
1.23k
        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
3
{
784
3
    erroneousMibs++;
785
3
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
786
3
                                NETSNMP_DS_LIB_MIB_ERRORS))
787
3
  return;
788
0
    DEBUGMSGTL(("parse-mibs", "\n"));
789
0
    if (type == ENDOFFILE)
790
0
        snmp_log(LOG_ERR, "%s (EOF): At line %d in %s\n", str, mibLine,
791
0
                 File);
792
0
    else if (token && *token)
793
0
        snmp_log(LOG_ERR, "%s (%s): At line %d in %s\n", str, token,
794
0
                 mibLine, File);
795
0
    else
796
0
        snmp_log(LOG_ERR, "%s: At line %d in %s\n", str, mibLine, File);
797
0
}
798
799
static void
800
print_module_not_found(const char *cp)
801
0
{
802
0
    if (first_err_module) {
803
0
        snmp_log(LOG_ERR, "MIB search path: %s\n",
804
0
                           netsnmp_get_mib_directory());
805
0
        first_err_module = 0;
806
0
    }
807
0
    if (!last_err_module || strcmp(cp, last_err_module))
808
0
        print_error("Cannot find module", cp, CONTINUE);
809
0
    if (last_err_module)
810
0
        free(last_err_module);
811
0
    last_err_module = strdup(cp);
812
0
}
813
814
static struct node *
815
alloc_node(int modid)
816
0
{
817
0
    struct node    *np;
818
819
0
    np = calloc(1, sizeof(struct node));
820
0
    if (!np)
821
0
        return NULL;
822
823
0
    np->tc_index = -1;
824
0
    np->modid = modid;
825
0
    np->filename = strdup(File);
826
0
    np->lineno = mibLine;
827
828
0
    return np;
829
0
}
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
0
{
921
0
    if (!np)
922
0
        return;
923
924
0
    free_enums(&np->enums);
925
0
    free_ranges(&np->ranges);
926
0
    free_indexes(&np->indexes);
927
0
    free_varbinds(&np->varbinds);
928
0
    free(np->label);
929
0
    free(np->hint);
930
0
    free(np->units);
931
0
    free(np->description);
932
0
    free(np->reference);
933
0
    free(np->defaultValue);
934
0
    free(np->parent);
935
0
    free(np->augments);
936
0
    free(np->filename);
937
0
    free(np);
938
0
}
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
6.56k
#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
0
{
2154
0
    struct subid_s *id = id_arg;
2155
0
    int             i, count, type;
2156
0
    char            token[MAXTOKEN];
2157
2158
0
    if ((type = get_token(fp, token, MAXTOKEN)) != LEFTBRACKET) {
2159
0
        print_error("Expected \"{\"", token, type);
2160
0
        return 0;
2161
0
    }
2162
0
    type = get_token(fp, token, MAXTOKEN);
2163
0
    for (count = 0; count < length; count++, id++) {
2164
0
        id->label = NULL;
2165
0
        id->modid = current_module;
2166
0
        id->subid = -1;
2167
0
        if (type == RIGHTBRACKET)
2168
0
            return count;
2169
0
        if (type == LABEL) {
2170
            /*
2171
             * this entry has a label 
2172
             */
2173
0
            id->label = strdup(token);
2174
0
            type = get_token(fp, token, MAXTOKEN);
2175
0
            if (type == LEFTPAREN) {
2176
0
                type = get_token(fp, token, MAXTOKEN);
2177
0
                if (type == NUMBER) {
2178
0
                    id->subid = strtoul(token, NULL, 10);
2179
0
                    if ((type =
2180
0
                         get_token(fp, token, MAXTOKEN)) != RIGHTPAREN) {
2181
0
                        print_error("Expected a closing parenthesis",
2182
0
                                    token, type);
2183
0
                        goto free_labels;
2184
0
                    }
2185
0
                } else {
2186
0
                    print_error("Expected a number", token, type);
2187
0
                    goto free_labels;
2188
0
                }
2189
0
            } else {
2190
0
                continue;
2191
0
            }
2192
0
        } else if (type == NUMBER) {
2193
            /*
2194
             * this entry  has just an integer sub-identifier 
2195
             */
2196
0
            id->subid = strtoul(token, NULL, 10);
2197
0
        } else {
2198
0
            print_error("Expected label or number", token, type);
2199
0
            goto free_labels;
2200
0
        }
2201
0
        type = get_token(fp, token, MAXTOKEN);
2202
0
    }
2203
0
    print_error("Too long OID", token, type);
2204
0
    --count;
2205
2206
0
free_labels:
2207
0
    for (i = 0; i <= count; i++) {
2208
0
        free(id_arg[i].label);
2209
0
        id_arg[i].label = NULL;
2210
0
    }
2211
2212
0
    return 0;
2213
0
}
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
0
{
2240
0
    register int    count;
2241
0
    register struct subid_s *op, *nop;
2242
0
    int             length;
2243
0
    struct subid_s  loid[32];
2244
0
    struct node    *np, *root = NULL, *oldnp = NULL;
2245
0
    struct tree    *tp;
2246
2247
0
    if ((length = getoid(fp, loid, 32)) == 0) {
2248
0
        print_error("Bad object identifier", NULL, CONTINUE);
2249
0
        return NULL;
2250
0
    }
2251
2252
    /*
2253
     * Handle numeric-only object identifiers,
2254
     *  by labeling the first sub-identifier
2255
     */
2256
0
    op = loid;
2257
0
    if (!op->label) {
2258
0
        if (length == 1) {
2259
0
            print_error("Attempt to define a root oid", name, OBJECT);
2260
0
            return NULL;
2261
0
        }
2262
0
        for (tp = tree_head; tp; tp = tp->next_peer)
2263
0
            if ((int) tp->subid == op->subid) {
2264
0
                op->label = strdup(tp->label);
2265
0
                break;
2266
0
            }
2267
0
    }
2268
2269
    /*
2270
     * Handle  "label OBJECT-IDENTIFIER ::= { subid }"
2271
     */
2272
0
    if (length == 1) {
2273
0
        op = loid;
2274
0
        np = alloc_node(op->modid);
2275
0
        if (np == NULL)
2276
0
            return (NULL);
2277
0
        np->subid = op->subid;
2278
0
        np->label = strdup(name);
2279
0
        np->parent = op->label;
2280
0
        return np;
2281
0
    }
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
0
    for (count = 0, op = loid, nop = loid + 1; count < (length - 1);
2288
0
         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
0
        if (op->label && (nop->label || (nop->subid != -1))) {
2296
0
            np = alloc_node(nop->modid);
2297
0
            if (np == NULL)
2298
0
                goto err;
2299
0
            if (root == NULL) {
2300
0
                root = np;
2301
0
            } else {
2302
0
                netsnmp_assert(oldnp);
2303
0
                oldnp->next = np;
2304
0
            }
2305
0
            oldnp = np;
2306
2307
0
            np->parent = strdup(op->label);
2308
0
            if (count == (length - 2)) {
2309
                /*
2310
                 * The name for this node is the label for this entry 
2311
                 */
2312
0
                np->label = strdup(name);
2313
0
                if (np->label == NULL)
2314
0
                    goto err;
2315
0
            } else {
2316
0
                if (!nop->label) {
2317
0
                    if (asprintf(&nop->label, "%s%d", ANON, anonymous++) < 0)
2318
0
                        goto err;
2319
0
                }
2320
0
                np->label = strdup(nop->label);
2321
0
            }
2322
0
            if (nop->subid != -1)
2323
0
                np->subid = nop->subid;
2324
0
            else
2325
0
                print_error("Warning: This entry is pretty silly",
2326
0
                            np->label, CONTINUE);
2327
0
        }                       /* end if(op->label... */
2328
0
    }
2329
2330
0
out:
2331
    /*
2332
     * free the loid array 
2333
     */
2334
0
    for (count = 0, op = loid; count < length; count++, op++) {
2335
0
        free(op->label);
2336
0
        op->label = NULL;
2337
0
    }
2338
2339
0
    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
0
}
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
0
{
2355
0
    int             i;
2356
0
    struct tc      *tcp;
2357
2358
0
    i = get_tc_index(descriptor, modid);
2359
0
    if (tc_index)
2360
0
        *tc_index = i;
2361
0
    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
0
    return LABEL;
2379
0
}
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
0
{
2388
0
    int             i;
2389
0
    struct tc      *tcp;
2390
0
    struct module  *mp;
2391
0
    struct module_import *mip;
2392
2393
    /*
2394
     * Check that the descriptor isn't imported
2395
     *  by searching the import list
2396
     */
2397
2398
0
    for (mp = module_head; mp; mp = mp->next)
2399
0
        if (mp->modid == modid)
2400
0
            break;
2401
0
    if (mp)
2402
0
        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
0
    for (i = 0, tcp = tclist; i < tc_alloc; i++, tcp++) {
2414
0
        if (tcp->type == 0)
2415
0
            break;
2416
0
        if (!label_compare(descriptor, tcp->descriptor) &&
2417
0
            ((modid == tcp->modid) || (modid == -1))) {
2418
0
            return i;
2419
0
        }
2420
0
    }
2421
0
    return -1;
2422
0
}
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
0
{
2459
0
    register int    type;
2460
0
    char            token[MAXTOKEN];
2461
0
    struct enum_list *ep = NULL, **epp = &ep;
2462
2463
0
    free_enums(retp);
2464
2465
0
    while ((type = get_token(fp, token, MAXTOKEN)) != ENDOFFILE) {
2466
0
        if (type == RIGHTBRACKET)
2467
0
            break;
2468
        /* some enums use "deprecated" to indicate a no longer value label */
2469
        /* (EG: IP-MIB's IpAddressStatusTC) */
2470
0
        if (type == LABEL || type == DEPRECATED) {
2471
            /*
2472
             * this is an enumerated label 
2473
             */
2474
0
            *epp = calloc(1, sizeof(struct enum_list));
2475
0
            if (*epp == NULL)
2476
0
                return (NULL);
2477
            /*
2478
             * a reasonable approximation for the length 
2479
             */
2480
0
            (*epp)->label = strdup(token);
2481
0
            type = get_token(fp, token, MAXTOKEN);
2482
0
            if (type != LEFTPAREN) {
2483
0
                print_error("Expected \"(\"", token, type);
2484
0
                goto err;
2485
0
            }
2486
0
            type = get_token(fp, token, MAXTOKEN);
2487
0
            if (type != NUMBER) {
2488
0
                print_error("Expected integer", token, type);
2489
0
                goto err;
2490
0
            }
2491
0
            (*epp)->value = strtol(token, NULL, 10);
2492
0
            (*epp)->lineno = mibLine;
2493
0
            type = get_token(fp, token, MAXTOKEN);
2494
0
            if (type != RIGHTPAREN) {
2495
0
                print_error("Expected \")\"", token, type);
2496
0
                goto err;
2497
0
            } 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
0
    }
2520
0
    if (type == ENDOFFILE) {
2521
0
        print_error("Expected \"}\"", token, type);
2522
0
        goto err;
2523
0
    }
2524
0
    *retp = ep;
2525
0
    return ep;
2526
2527
0
err:
2528
0
    free_enums(&ep);
2529
0
    return NULL;
2530
0
}
2531
2532
static struct range_list *
2533
parse_ranges(FILE * fp, struct range_list **retp)
2534
0
{
2535
0
    int             low, high;
2536
0
    char            nexttoken[MAXTOKEN];
2537
0
    int             nexttype;
2538
0
    struct range_list *rp = NULL, **rpp = &rp;
2539
0
    int             size = 0, taken = 1;
2540
2541
0
    free_ranges(retp);
2542
2543
0
    nexttype = get_token(fp, nexttoken, MAXTOKEN);
2544
0
    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
0
    do {
2553
0
        if (!taken)
2554
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2555
0
        else
2556
0
            taken = 0;
2557
0
        high = low = strtoul(nexttoken, NULL, 10);
2558
0
        nexttype = get_token(fp, nexttoken, MAXTOKEN);
2559
0
        if (nexttype == RANGE) {
2560
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2561
0
            errno = 0;
2562
0
            high = strtoul(nexttoken, NULL, 10);
2563
0
            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
0
            nexttype = get_token(fp, nexttoken, MAXTOKEN);
2571
0
        }
2572
0
        *rpp = (struct range_list *) calloc(1, sizeof(struct range_list));
2573
0
        if (*rpp == NULL)
2574
0
            break;
2575
0
        (*rpp)->low = low;
2576
0
        (*rpp)->high = high;
2577
0
        rpp = &(*rpp)->next;
2578
2579
0
    } while (nexttype == BAR);
2580
0
    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
0
    if (nexttype != RIGHTPAREN)
2586
0
        print_error("Expected \")\"", nexttoken, nexttype);
2587
2588
0
    *retp = rp;
2589
0
    return rp;
2590
0
}
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
0
{
2599
0
    int             type, i;
2600
0
    char            token[MAXTOKEN];
2601
0
    char            quoted_string_buffer[MAXQUOTESTR];
2602
0
    char           *hint = NULL;
2603
0
    char           *descr = NULL;
2604
0
    struct tc      *tcp;
2605
0
    int             level;
2606
0
    int             is_duplicate = 0;
2607
2608
0
    type = get_token(fp, token, MAXTOKEN);
2609
0
    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
0
    } else if (type == LEFTBRACKET) {
2622
0
        struct node    *np;
2623
0
        int             ch_next = '{';
2624
0
        ungetc(ch_next, fp);
2625
0
        np = parse_objectid(fp, name);
2626
0
        if (np != NULL) {
2627
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2628
0
            return np;
2629
0
        }
2630
0
        return NULL;
2631
0
    } else if (type == LEFTSQBRACK) {
2632
0
        int             size = 0;
2633
0
        do {
2634
0
            type = get_token(fp, token, MAXTOKEN);
2635
0
        } while (type != ENDOFFILE && type != RIGHTSQBRACK);
2636
0
        if (type != RIGHTSQBRACK) {
2637
0
            print_error("Expected \"]\"", token, type);
2638
0
            return NULL;
2639
0
        }
2640
0
        type = get_token(fp, token, MAXTOKEN);
2641
0
        if (type == IMPLICIT)
2642
0
            type = get_token(fp, token, MAXTOKEN);
2643
0
        *ntype = get_token(fp, ntoken, MAXTOKEN);
2644
0
        if (*ntype == LEFTPAREN) {
2645
0
            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
0
            }
2687
0
        }
2688
0
        return NULL;
2689
0
    } else {
2690
0
        if (type == CONVENTION) {
2691
0
            while (type != SYNTAX && type != ENDOFFILE) {
2692
0
                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
0
                } 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
0
                    type =
2714
0
                        get_token(fp, quoted_string_buffer, MAXQUOTESTR);
2715
0
            }
2716
0
            type = get_token(fp, token, MAXTOKEN);
2717
0
            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
0
        } 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
0
        if (type == LABEL) {
2735
0
            type = get_tc(token, current_module, NULL, NULL, NULL, NULL);
2736
0
        }
2737
2738
        /*
2739
         * textual convention 
2740
         */
2741
0
        tcp = NULL;
2742
0
        is_duplicate = 0;
2743
0
        for (i = 0; i < tc_alloc; i++) {
2744
0
            if (tclist[i].type == 0) {
2745
0
                if (tcp == NULL)
2746
0
                    tcp = &tclist[i];
2747
0
            } else if (strcmp(name, tclist[i].descriptor) == 0 &&
2748
0
                       tclist[i].modid == current_module) {
2749
0
                snmp_log(LOG_ERR,
2750
0
                         "Duplicate TEXTUAL-CONVENTION '%s' at line %d in %s. First at line %d\n",
2751
0
                         name, mibLine, File, tclist[i].lineno);
2752
0
                erroneousMibs++;
2753
0
                is_duplicate = 1;
2754
0
                break;
2755
0
            }
2756
0
        }
2757
2758
0
        if (is_duplicate) {
2759
0
            struct range_list *dummy_ranges = NULL;
2760
0
            struct enum_list *dummy_enums = NULL;
2761
2762
0
            free(hint);
2763
0
            free(descr);
2764
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2765
0
            if (*ntype == LEFTPAREN) {
2766
0
                dummy_ranges = parse_ranges(fp, &dummy_ranges);
2767
0
                free_ranges(&dummy_ranges);
2768
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2769
0
            } else if (*ntype == LEFTBRACKET) {
2770
0
                dummy_enums = parse_enumlist(fp, &dummy_enums);
2771
0
                free_enums(&dummy_enums);
2772
0
                *ntype = get_token(fp, ntoken, MAXTOKEN);
2773
0
            }
2774
0
            return NULL;
2775
0
        }
2776
2777
0
        if (tcp == NULL) {
2778
0
            tclist = realloc(tclist, (tc_alloc + TC_INCR)*sizeof(struct tc));
2779
0
            memset(tclist+tc_alloc, 0, TC_INCR*sizeof(struct tc));
2780
0
            tcp = tclist + tc_alloc;
2781
0
            tc_alloc += TC_INCR;
2782
0
        }
2783
0
        if (!(type & SYNTAX_MASK)) {
2784
0
            print_error("Textual convention doesn't map to real type",
2785
0
                        token, type);
2786
0
            goto err;
2787
0
        }
2788
0
        tcp->modid = current_module;
2789
0
        tcp->descriptor = strdup(name);
2790
0
        tcp->hint = hint;
2791
0
        tcp->description = descr;
2792
0
        tcp->lineno = mibLine;
2793
0
        tcp->type = type;
2794
0
        *ntype = get_token(fp, ntoken, MAXTOKEN);
2795
0
        if (*ntype == LEFTPAREN) {
2796
0
            tcp->ranges = parse_ranges(fp, &tcp->ranges);
2797
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2798
0
        } else if (*ntype == LEFTBRACKET) {
2799
            /*
2800
             * if there is an enumeration list, parse it 
2801
             */
2802
0
            tcp->enums = parse_enumlist(fp, &tcp->enums);
2803
0
            *ntype = get_token(fp, ntoken, MAXTOKEN);
2804
0
        }
2805
0
        return NULL;
2806
0
    }
2807
2808
0
err:
2809
0
    SNMP_FREE(descr);
2810
0
    SNMP_FREE(hint);
2811
0
    return NULL;
2812
0
}
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
3
{
4126
3
    struct module  *mp;
4127
4128
6
    for (mp = module_head; mp; mp = mp->next)
4129
3
        if (!label_compare(mp->name, name))
4130
0
            return (mp->modid);
4131
4132
3
    DEBUGMSGTL(("parse-mibs", "Module %s not found\n", name));
4133
3
    return (-1);
4134
3
}
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
0
{
4190
0
    struct module_compatability *mcp;
4191
4192
0
    for (mcp = module_map_head; mcp; mcp = mcp->next) {
4193
0
        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
0
    }
4204
0
    return 0;
4205
0
}
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.23k
{
4253
1.23k
    const char     *oldFile = File;
4254
1.23k
    int             oldLine = mibLine;
4255
1.23k
    int             oldModule = current_module;
4256
1.23k
    FILE           *fp;
4257
1.23k
    struct node    *np;
4258
1.23k
    int             res;
4259
4260
1.23k
    if (mp->no_imports != -1) {
4261
1.23k
        DEBUGMSGTL(("parse-mibs", "Module %s already loaded\n",
4262
1.23k
                    name));
4263
1.23k
        return MODULE_ALREADY_LOADED;
4264
1.23k
    }
4265
3
    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
3
#ifdef HAVE_FLOCKFILE
4275
3
    flockfile(fp);
4276
3
#endif
4277
3
    mp->no_imports = 0; /* Note that we've read the file */
4278
3
    File = mp->file;
4279
3
    mibLine = 1;
4280
3
    current_module = mp->modid;
4281
    /*
4282
     * Parse the file
4283
     */
4284
3
    np = parse(fp);
4285
3
#ifdef HAVE_FUNLOCKFILE
4286
3
    funlockfile(fp);
4287
3
#endif
4288
3
    fclose(fp);
4289
3
    File = oldFile;
4290
3
    mibLine = oldLine;
4291
3
    current_module = oldModule;
4292
3
    res = !np && gMibError == MODULE_SYNTAX_ERROR ?
4293
3
        MODULE_SYNTAX_ERROR : MODULE_LOADED_OK;
4294
3
    while (np) {
4295
0
        struct node *nnp = np->next;
4296
0
        free_node(np);
4297
0
        np = nnp;
4298
0
    }
4299
3
    return res;
4300
3
}
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.23k
{
4310
1.23k
    struct module  *mp;
4311
4312
1.23k
    netsnmp_init_mib_internals();
4313
4314
2.66k
    for (mp = module_head; mp; mp = mp->next)
4315
2.66k
        if (!label_compare(mp->name, name))
4316
1.23k
            return read_from_file(mp, name);
4317
4318
0
    return MODULE_NOT_FOUND;
4319
1.23k
}
4320
4321
void
4322
adopt_orphans(void)
4323
0
{
4324
0
    struct node    *np = NULL, *onp;
4325
0
    struct tree    *tp;
4326
0
    int             i, adopted = 1;
4327
4328
0
    if (!orphan_nodes)
4329
0
        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.23k
{
4402
1.23k
    int status = 0;
4403
1.23k
    status = read_module_internal(name);
4404
4405
1.23k
    if (status == MODULE_NOT_FOUND) {
4406
0
        if (!read_module_replacements(name))
4407
0
            print_module_not_found(name);
4408
1.23k
    } else if (status == MODULE_SYNTAX_ERROR) {
4409
3
        gMibError = 0;
4410
3
        gLoop = 1;
4411
4412
3
        strncat(gMibNames, " ", sizeof(gMibNames) - strlen(gMibNames) - 1);
4413
3
        strncat(gMibNames, name, sizeof(gMibNames) - strlen(gMibNames) - 1);
4414
3
    }
4415
4416
1.23k
    return tree_head;
4417
1.23k
}
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.23k
{
4623
1.23k
    struct module  *mp;
4624
4625
2.66k
    for (mp = module_head; mp; mp = mp->next)
4626
2.66k
        if (!label_compare(mp->name, name)) {
4627
1.23k
            DEBUGMSGTL(("parse-mibs", "  Module %s already noted\n", name));
4628
            /*
4629
             * Not the same file 
4630
             */
4631
1.23k
            if (label_compare(mp->file, file)) {
4632
373
                DEBUGMSGTL(("parse-mibs", "    %s is now in %s\n",
4633
373
                            name, file));
4634
373
                if (netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
4635
373
               NETSNMP_DS_LIB_MIB_WARNINGS)) {
4636
0
                    snmp_log(LOG_WARNING,
4637
0
                             "Warning: Module %s was in %s now is %s\n",
4638
0
                             name, mp->file, file);
4639
0
    }
4640
4641
                /*
4642
                 * Use the new one in preference 
4643
                 */
4644
373
                free(mp->file);
4645
373
                mp->file = strdup(file);
4646
373
            }
4647
1.23k
            return;
4648
1.23k
        }
4649
4650
    /*
4651
     * Add this module to the list 
4652
     */
4653
3
    DEBUGMSGTL(("parse-mibs", "  Module %d %s is in %s\n", max_module,
4654
3
                name, file));
4655
3
    mp = calloc(1, sizeof(struct module));
4656
3
    if (mp == NULL)
4657
0
        return;
4658
3
    mp->name = strdup(name);
4659
3
    mp->file = strdup(file);
4660
3
    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
3
    mp->imports = NULL;
4667
3
    mp->no_imports = -1;        /* Not yet loaded */
4668
3
    mp->modid = max_module;
4669
3
    ++max_module;
4670
4671
3
    mp->next = module_head;     /* Or add to the *end* of the list? */
4672
3
    module_head = mp;
4673
3
}
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
9
{
4711
9
    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
9
}
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
0
{
4746
0
    unsigned int    h = 2166136261u;
4747
4748
0
    for (; *label; label++) {
4749
0
        h ^= (unsigned char)*label;
4750
0
        h *= 16777619u;
4751
0
    }
4752
0
    return h;
4753
0
}
4754
4755
static struct label_entry *
4756
label_table_find(const struct label_table *lt, const char *label,
4757
                 unsigned int hash)
4758
0
{
4759
0
    struct label_entry *le;
4760
4761
0
    if (!lt->size)
4762
0
        return NULL;
4763
0
    for (le = lt->buckets[hash & (lt->size - 1)]; le; le = le->next)
4764
0
        if (le->hash == hash && strcmp(le->first->label, label) == 0)
4765
0
            return le;
4766
0
    return NULL;
4767
0
}
4768
4769
static int
4770
label_table_grow(struct label_table *lt)
4771
0
{
4772
0
    size_t          new_size = lt->size ? 2 * lt->size : 64;
4773
0
    struct label_entry **new_buckets;
4774
0
    size_t          i;
4775
4776
0
    new_buckets = calloc(new_size, sizeof(*new_buckets));
4777
0
    if (!new_buckets)
4778
0
        return -1;
4779
0
    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
0
    free(lt->buckets);
4792
0
    lt->buckets = new_buckets;
4793
0
    lt->size = new_size;
4794
0
    return 0;
4795
0
}
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
0
{
4804
0
    struct label_entry *le;
4805
4806
0
    if (!np->label)
4807
0
        return NULL;
4808
0
    le = label_table_find(lt, np->label, label_hash(np->label));
4809
0
    if (!le)
4810
0
        return NULL;
4811
0
    if (le->first->subid != np->subid)
4812
0
        return le->first;
4813
0
    return le->other;
4814
0
}
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
0
{
4823
0
    struct label_entry *le, **head;
4824
0
    unsigned int    hash;
4825
4826
0
    if (!np->label)
4827
0
        return;
4828
0
    hash = label_hash(np->label);
4829
0
    le = label_table_find(lt, np->label, hash);
4830
0
    if (le) {
4831
0
        if (!le->other && le->first->subid != np->subid)
4832
0
            le->other = np;
4833
0
        return;
4834
0
    }
4835
0
    if (lt->count >= lt->size && label_table_grow(lt) < 0)
4836
0
        return;
4837
0
    le = malloc(sizeof(*le));
4838
0
    if (!le)
4839
0
        return;
4840
0
    le->hash = hash;
4841
0
    le->first = np;
4842
0
    le->other = NULL;
4843
0
    head = &lt->buckets[hash & (lt->size - 1)];
4844
0
    le->next = *head;
4845
0
    *head = le;
4846
0
    lt->count++;
4847
0
}
4848
4849
static void
4850
label_table_clear(struct label_table *lt)
4851
3
{
4852
3
    size_t          i;
4853
4854
3
    for (i = 0; i < lt->size; i++) {
4855
0
        struct label_entry *le, *next;
4856
4857
0
        for (le = lt->buckets[i]; le; le = next) {
4858
0
            next = le->next;
4859
0
            free(le);
4860
0
        }
4861
0
    }
4862
3
    free(lt->buckets);
4863
3
    lt->buckets = NULL;
4864
3
    lt->size = 0;
4865
3
    lt->count = 0;
4866
3
}
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
3
{
4875
#ifdef TEST
4876
    extern void     xmalloc_stats(FILE *);
4877
#endif
4878
3
    char            token[MAXTOKEN];
4879
3
    char            name[MAXTOKEN+1];
4880
3
    int             type = LABEL;
4881
3
    int             lasttype = LABEL;
4882
4883
3
#define BETWEEN_MIBS          1
4884
3
#define IN_MIB                2
4885
3
    int             state = BETWEEN_MIBS;
4886
3
    struct node    *np = NULL, *root = NULL;
4887
3
    struct label_table labels = { NULL, 0, 0 };
4888
3
    struct objgroup *oldgroups = objgroups, *oldobjects = objects,
4889
3
                    *oldnotifs = notifs;
4890
4891
3
    DEBUGMSGTL(("parse-file", "Parsing file:  %s...\n", File));
4892
4893
3
    if (last_err_module)
4894
0
        free(last_err_module);
4895
3
    last_err_module = NULL;
4896
3
    objgroups = NULL;
4897
3
    objects = NULL;
4898
3
    notifs = NULL;
4899
4900
3
    while (type != ENDOFFILE) {
4901
3
        struct node *nnp;
4902
4903
3
        if (lasttype == CONTINUE)
4904
0
            lasttype = type;
4905
3
        else
4906
3
            type = lasttype = get_token(fp, token, MAXTOKEN);
4907
4908
3
        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
        case LABEL:
4949
3
        case INTEGER:
4950
3
        case INTEGER32:
4951
3
        case UINTEGER32:
4952
3
        case UNSIGNED32:
4953
3
        case COUNTER:
4954
3
        case COUNTER64:
4955
3
        case GAUGE:
4956
3
        case IPADDR:
4957
3
        case NETADDR:
4958
3
        case NSAPADDRESS:
4959
3
        case OBJSYNTAX:
4960
3
        case APPSYNTAX:
4961
3
        case SIMPLESYNTAX:
4962
3
        case OBJNAME:
4963
3
        case NOTIFNAME:
4964
3
        case KW_OPAQUE:
4965
3
        case TIMETICKS:
4966
3
            break;
4967
0
        case ENDOFFILE:
4968
0
            continue;
4969
0
        default:
4970
0
            strlcpy(name, token, sizeof(name));
4971
0
            type = get_token(fp, token, MAXTOKEN);
4972
0
            nnp = NULL;
4973
0
            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
0
                print_error(name, "is a reserved word", lasttype);
4983
0
            continue;           /* see if we can parse the rest of the file */
4984
3
        }
4985
3
        strlcpy(name, token, sizeof(name));
4986
3
        type = get_token(fp, token, MAXTOKEN);
4987
3
        nnp = NULL;
4988
4989
        /*
4990
         * Handle obsolete method to assign an object identifier to a
4991
         * module
4992
         */
4993
3
        if (lasttype == LABEL && type == LEFTBRACKET) {
4994
0
            while (type != RIGHTBRACKET && type != ENDOFFILE)
4995
0
                type = get_token(fp, token, MAXTOKEN);
4996
0
            if (type == ENDOFFILE) {
4997
0
                print_error("Expected \"}\"", token, type);
4998
0
                gMibError = MODULE_SYNTAX_ERROR;
4999
0
                goto free_mib;
5000
0
            }
5001
0
            type = get_token(fp, token, MAXTOKEN);
5002
0
        }
5003
5004
3
        switch (type) {
5005
0
        case DEFINITIONS:
5006
0
            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
0
            state = IN_MIB;
5012
0
            current_module = which_module(name);
5013
0
            free_objgroup(objgroups);
5014
0
            objgroups = NULL;
5015
0
            free_objgroup(objects);
5016
0
            objects = NULL;
5017
0
            free_objgroup(notifs);
5018
0
            notifs = NULL;
5019
0
            if (current_module == -1) {
5020
0
                new_module(name, File);
5021
0
                current_module = which_module(name);
5022
0
            }
5023
0
            {
5024
0
                struct module  *mp;
5025
5026
0
                for (mp = module_head; mp; mp = mp->next) {
5027
0
                    if (mp->modid == current_module) {
5028
0
                        if (mp->no_imports == -1)
5029
0
                            mp->no_imports = 0;
5030
0
                        break;
5031
0
                    }
5032
0
                }
5033
0
            }
5034
0
            DEBUGMSGTL(("parse-mibs", "Parsing MIB: %d %s\n",
5035
0
                        current_module, name));
5036
0
            while ((type = get_token(fp, token, MAXTOKEN)) != ENDOFFILE)
5037
0
                if (type == BEGIN)
5038
0
                    break;
5039
0
            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
0
        case OBJECT:
5122
0
            type = get_token(fp, token, MAXTOKEN);
5123
0
            if (type != IDENTIFIER) {
5124
0
                print_error("Expected IDENTIFIER", token, type);
5125
0
                gMibError = MODULE_SYNTAX_ERROR;
5126
0
                goto free_mib;
5127
0
            }
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
0
        case EQUALS:
5142
0
            nnp = parse_asntype(fp, name, &type, token);
5143
0
            lasttype = CONTINUE;
5144
0
            break;
5145
0
        case ENDOFFILE:
5146
0
            break;
5147
3
        default:
5148
3
            print_error("Bad operator", token, type);
5149
3
            gMibError = MODULE_SYNTAX_ERROR;
5150
3
            goto free_mib;
5151
3
        }
5152
0
        if (nnp) {
5153
0
            struct node *op = label_table_find_dup(&labels, nnp);
5154
5155
0
            if (op) {
5156
0
                struct node *next_node;
5157
5158
0
                snmp_log(LOG_ERR, 
5159
0
                    "Duplicate Object '%s' at line %d in %s. First at line %d\n",
5160
0
                    op->label, mibLine, File, op->lineno);
5161
0
                erroneousMibs++;
5162
0
                while (nnp) {
5163
0
                    next_node = nnp->next;
5164
0
                    free_node(nnp);
5165
0
                    nnp = next_node;
5166
0
                }
5167
0
            } else {
5168
0
                if (np)
5169
0
                    np->next = nnp;
5170
0
                else
5171
0
                    np = root = nnp;
5172
0
                label_table_add(&labels, np);
5173
0
                while (np->next) {
5174
0
                    np = np->next;
5175
0
                    label_table_add(&labels, np);
5176
0
                }
5177
0
                if (np->type == TYPE_OTHER)
5178
0
                    np->type = type;
5179
0
            }
5180
0
        }
5181
0
    }
5182
0
    DEBUGMSGTL(("parse-file", "End of file (%s)\n", File));
5183
0
    label_table_clear(&labels);
5184
0
    free_objgroup(objgroups);
5185
0
    objgroups = oldgroups;
5186
0
    free_objgroup(objects);
5187
0
    objects = oldobjects;
5188
0
    free_objgroup(notifs);
5189
0
    notifs = oldnotifs;
5190
0
    return root;
5191
5192
3
free_mib:
5193
3
    label_table_clear(&labels);
5194
3
    for (; root; root = np) {
5195
0
        np = root->next;
5196
0
        free_node(root);
5197
0
    }
5198
3
    free_objgroup(objgroups);
5199
3
    objgroups = oldgroups;
5200
3
    free_objgroup(objects);
5201
3
    objects = oldobjects;
5202
3
    free_objgroup(notifs);
5203
3
    notifs = oldnotifs;
5204
3
    return NULL;
5205
3
}
5206
5207
/*
5208
 * return zero if character is not a label character. 
5209
 */
5210
static int
5211
is_labelchar(int ich)
5212
3.29k
{
5213
3.29k
    netsnmp_assert(ich == EOF || (0 <= ich && ich < 256));
5214
3.29k
    if ((isalnum(ich)) || (ich == '-'))
5215
2.05k
        return 1;
5216
1.24k
    if (ich == '_' && netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
5217
0
               NETSNMP_DS_LIB_MIB_PARSE_LABEL)) {
5218
0
        return 1;
5219
0
    }
5220
5221
1.24k
    return 0;
5222
1.24k
}
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
3.62k
{
5232
3.62k
#ifdef HAVE_FGETC_UNLOCKED
5233
3.62k
    return fgetc_unlocked(stream);
5234
#else
5235
    return getc(stream);
5236
#endif
5237
3.62k
}
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.56k
{
5247
1.56k
    int             ch, ch_next;
5248
1.56k
    char           *cp;
5249
1.56k
    int             hash;
5250
1.56k
    struct tok     *tp;
5251
1.56k
    int             too_long;
5252
1.56k
    enum { bdigits, xdigits, other } seenSymbols;
5253
5254
1.56k
fetch_next_token:
5255
1.56k
    cp = token;
5256
1.56k
    hash = 0;
5257
1.56k
    too_long = 0;
5258
    /*
5259
     * skip all white space 
5260
     */
5261
1.56k
    do {
5262
1.56k
        ch = netsnmp_getc(fp);
5263
1.56k
        if (ch == '\n')
5264
0
            mibLine++;
5265
1.56k
    }
5266
1.56k
    while (isspace(ch) && ch != EOF);
5267
1.56k
    *cp++ = ch;
5268
1.56k
    *cp = '\0';
5269
1.56k
    switch (ch) {
5270
326
    case EOF:
5271
326
        return ENDOFFILE;
5272
0
    case '"':
5273
0
        return parseQuoteString(fp, token, maxtlen);
5274
0
    case '\'':                 /* binary or hex constant */
5275
0
        seenSymbols = bdigits;
5276
0
        while ((ch = netsnmp_getc(fp)) != EOF && ch != '\'') {
5277
0
            switch (seenSymbols) {
5278
0
            case bdigits:
5279
0
                if (ch == '0' || ch == '1')
5280
0
                    break;
5281
0
                seenSymbols = xdigits;
5282
0
                NETSNMP_FALLTHROUGH;
5283
0
            case xdigits:
5284
0
                if (isxdigit(ch))
5285
0
                    break;
5286
0
                seenSymbols = other;
5287
0
                NETSNMP_FALLTHROUGH;
5288
0
            case other:
5289
0
                break;
5290
0
            }
5291
0
            if (cp - token < maxtlen - 2)
5292
0
                *cp++ = ch;
5293
0
        }
5294
0
        if (ch == '\'') {
5295
0
            unsigned long   val = 0;
5296
0
            char           *run = token + 1;
5297
0
            ch = netsnmp_getc(fp);
5298
0
            switch (ch) {
5299
0
            case EOF:
5300
0
                return ENDOFFILE;
5301
0
            case 'b':
5302
0
            case 'B':
5303
0
                if (seenSymbols > bdigits) {
5304
0
                    *cp++ = '\'';
5305
0
                    *cp = 0;
5306
0
                    return LABEL;
5307
0
                }
5308
0
                while (run != cp)
5309
0
                    val = val * 2 + *run++ - '0';
5310
0
                break;
5311
0
            case 'h':
5312
0
            case 'H':
5313
0
                if (seenSymbols > xdigits) {
5314
0
                    *cp++ = '\'';
5315
0
                    *cp = 0;
5316
0
                    return LABEL;
5317
0
                }
5318
0
                while (run != cp) {
5319
0
                    ch = *run++;
5320
0
                    if ('0' <= ch && ch <= '9')
5321
0
                        val = val * 16 + ch - '0';
5322
0
                    else if ('a' <= ch && ch <= 'f')
5323
0
                        val = val * 16 + ch - 'a' + 10;
5324
0
                    else if ('A' <= ch && ch <= 'F')
5325
0
                        val = val * 16 + ch - 'A' + 10;
5326
0
                }
5327
0
                break;
5328
0
            default:
5329
0
                *cp++ = '\'';
5330
0
                *cp = 0;
5331
0
                return LABEL;
5332
0
            }
5333
0
            snprintf(token, maxtlen, "%ld", val);
5334
0
            return NUMBER;
5335
0
        } else
5336
0
            return LABEL;
5337
0
    case '(':
5338
0
        return LEFTPAREN;
5339
0
    case ')':
5340
0
        return RIGHTPAREN;
5341
0
    case '{':
5342
0
        return LEFTBRACKET;
5343
0
    case '}':
5344
0
        return RIGHTBRACKET;
5345
0
    case '[':
5346
0
        return LEFTSQBRACK;
5347
0
    case ']':
5348
0
        return RIGHTSQBRACK;
5349
0
    case ';':
5350
0
        return SEMI;
5351
0
    case ',':
5352
0
        return COMMA;
5353
0
    case '|':
5354
0
        return BAR;
5355
0
    case '.':
5356
0
        ch_next = netsnmp_getc(fp);
5357
0
        if (ch_next == '.')
5358
0
            return RANGE;
5359
0
        ungetc(ch_next, fp);
5360
0
        return LABEL;
5361
1
    case ':':
5362
1
        ch_next = netsnmp_getc(fp);
5363
1
        if (ch_next != ':') {
5364
1
            ungetc(ch_next, fp);
5365
1
            return LABEL;
5366
1
        }
5367
0
        ch_next = netsnmp_getc(fp);
5368
0
        if (ch_next != '=') {
5369
0
            ungetc(ch_next, fp);
5370
0
            return LABEL;
5371
0
        }
5372
0
        return EQUALS;
5373
0
    case '-':
5374
0
        ch_next = netsnmp_getc(fp);
5375
0
        if (ch_next == '-') {
5376
0
            if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
5377
0
               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
0
            } 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
0
                ch = ' ';
5391
0
                ch_next = netsnmp_getc(fp);
5392
0
                while (ch_next != EOF && ch_next != '\n' &&
5393
0
                       (ch != '-' || ch_next != '-')) {
5394
0
                    ch = ch_next;
5395
0
                    ch_next = netsnmp_getc(fp);
5396
0
                }
5397
0
            }
5398
0
            if (ch_next == EOF)
5399
0
                return ENDOFFILE;
5400
0
            if (ch_next == '\n')
5401
0
                mibLine++;
5402
0
            goto fetch_next_token;
5403
0
        }
5404
0
        ungetc(ch_next, fp);
5405
0
  NETSNMP_FALLTHROUGH;
5406
1.24k
    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.24k
        if (!is_labelchar(ch))
5413
725
            return LABEL;
5414
515
        hash += tolower(ch);
5415
515
      more:
5416
2.05k
        while (is_labelchar(ch_next = netsnmp_getc(fp))) {
5417
1.54k
            hash += tolower(ch_next);
5418
1.54k
            if (cp - token < maxtlen - 1)
5419
1.54k
                *cp++ = ch_next;
5420
0
            else
5421
0
                too_long = 1;
5422
1.54k
        }
5423
515
        ungetc(ch_next, fp);
5424
515
        *cp = '\0';
5425
5426
515
        if (too_long)
5427
0
            print_error("Warning: token too long", token, CONTINUE);
5428
2.57k
        for (tp = buckets[BUCKET(hash)]; tp; tp = tp->next) {
5429
2.06k
            if ((tp->hash == hash) && (!label_compare(tp->name, token)))
5430
0
                break;
5431
2.06k
        }
5432
515
        if (tp) {
5433
0
            if (tp->token != CONTINUE)
5434
0
                return (tp->token);
5435
0
            while (isspace((ch_next = netsnmp_getc(fp))))
5436
0
                if (ch_next == '\n')
5437
0
                    mibLine++;
5438
0
            if (ch_next == EOF)
5439
0
                return ENDOFFILE;
5440
0
            if (isalnum(ch_next)) {
5441
0
                *cp++ = ch_next;
5442
0
                hash += tolower(ch_next);
5443
0
                goto more;
5444
0
            }
5445
0
        }
5446
515
        if (token[0] == '-' || isdigit((unsigned char)(token[0]))) {
5447
0
            for (cp = token + 1; *cp; cp++)
5448
0
                if (!isdigit((unsigned char)(*cp)))
5449
0
                    return LABEL;
5450
0
            return NUMBER;
5451
0
        }
5452
515
        return LABEL;
5453
1.56k
    }
5454
1.56k
}
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
0
{
5512
0
    DIR            *dir, *dir2;
5513
0
    struct dirent  *file;
5514
0
    char          **filenames = NULL;
5515
0
    int             fname_len, i, filename_count = 0, array_size = 0;
5516
0
    char           *tmpstr;
5517
5518
0
    *result = NULL;
5519
5520
0
    dir = opendir(dirname);
5521
0
    if (!dir)
5522
0
        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
0
{
5582
0
    const char     *oldFile = File;
5583
0
    int             oldLine = mibLine;
5584
0
    char          **filenames;
5585
0
    int             count = 0;
5586
0
    int             filename_count, i;
5587
5588
0
    DEBUGMSGTL(("parse-mibs", "Scanning directory %s\n", dirname));
5589
5590
0
    filename_count = scan_directory(&filenames, dirname);
5591
5592
0
    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
0
    else
5604
0
        DEBUGMSGTL(("parse-mibs","cannot open MIB directory %s\n", dirname));
5605
5606
0
    return (-1);
5607
0
}
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.82k
{
5617
1.82k
    FILE           *fp;
5618
1.82k
    char            token[MAXTOKEN];
5619
1.82k
    const char     *oldFile = File;
5620
1.82k
    int             oldLine = mibLine;
5621
5622
1.82k
    fp = fopen(filename, "r");
5623
1.82k
    if (fp == NULL) {
5624
262
        snmp_log_perror(filename);
5625
262
        return NULL;
5626
262
    }
5627
1.56k
    mibLine = 1;
5628
1.56k
    File = filename;
5629
1.56k
    DEBUGMSGTL(("parse-mibs", "Parsing file: %s...\n", filename));
5630
1.56k
    if (get_token(fp, token, MAXTOKEN) != LABEL) {
5631
326
      snmp_log(LOG_ERR, "Failed to parse MIB file %s\n", filename);
5632
326
      fclose(fp);
5633
326
      File = oldFile;
5634
326
      mibLine = oldLine;
5635
326
      return NULL;
5636
326
    }
5637
1.23k
    fclose(fp);
5638
1.23k
    new_module(token, filename);
5639
1.23k
    (void) netsnmp_read_module(token);
5640
5641
1.23k
    File = oldFile;
5642
1.23k
    mibLine = oldLine;
5643
1.23k
    return tree_head;
5644
1.56k
}
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
0
{
5702
0
    register int    ch;
5703
0
    int             count = 0;
5704
0
    int             too_long = 0;
5705
0
    char           *token_start = token;
5706
5707
0
    for (ch = netsnmp_getc(fp); ch != EOF; ch = netsnmp_getc(fp)) {
5708
0
        if (ch == '\r')
5709
0
            continue;
5710
0
        if (ch == '\n') {
5711
0
            mibLine++;
5712
0
        } else if (ch == '"') {
5713
0
            netsnmp_assert(token - token_start < maxtlen);
5714
0
            *token = '\0';
5715
0
            if (too_long && netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, 
5716
0
             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
0
            return QUOTESTRING;
5729
0
        }
5730
        /*
5731
         * maximum description length check.  If greater, keep parsing
5732
         * but truncate the string 
5733
         */
5734
0
        if (++count < maxtlen)
5735
0
            *token++ = ch;
5736
0
        else
5737
0
            too_long = 1;
5738
0
    }
5739
5740
0
    return 0;
5741
0
}
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
0
{
5824
0
    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
0
}
5839
5840
static void
5841
free_varbinds(struct varbind_list **spp)
5842
0
{
5843
0
    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
0
}
5858
5859
static void
5860
free_ranges(struct range_list **spp)
5861
0
{
5862
0
    if (spp && *spp) {
5863
0
        struct range_list *pp, *npp;
5864
5865
0
        pp = *spp;
5866
0
        *spp = NULL;
5867
5868
0
        while (pp) {
5869
0
            npp = pp->next;
5870
0
            free(pp);
5871
0
            pp = npp;
5872
0
        }
5873
0
    }
5874
0
}
5875
5876
static void
5877
free_enums(struct enum_list **spp)
5878
0
{
5879
0
    if (spp && *spp) {
5880
0
        struct enum_list *pp, *npp;
5881
5882
0
        pp = *spp;
5883
0
        *spp = NULL;
5884
5885
0
        while (pp) {
5886
0
            npp = pp->next;
5887
0
            if (pp->label)
5888
0
                free(pp->label);
5889
0
            free(pp);
5890
0
            pp = npp;
5891
0
        }
5892
0
    }
5893
0
}
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 */