Coverage Report

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