Coverage Report

Created: 2023-06-07 06:42

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