Coverage Report

Created: 2026-03-17 06:29

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