1#!/bin/sh
2#	$OpenBSD: makemib,v 1.7 2001/08/11 22:26:39 jakob Exp $
3#
4# Copyright (c) 1990, 1996
5#     John Robert LoVerso. All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30#
31# This script will read either ASN.1-style MIB files or the ".defs" files
32# created by the ISODE "mosy" program on such files.
33#
34# The output of this script is the "mib.h" file used by tcpdumps' ASN.1/SNMP
35# decoding code.
36#
37# This script needs to be run by "gawk" (GNU awk).  "nawk" will work, but
38# dump will get a recursion error if you process LARGE mibs.  While it would
39# by farily easy to rewrite this not to use recursion (and also easy to
40# eliminate use of gsub and functions to use classic "awk"), you have to 
41# order the structure declarations in defined-first order for the compiler
42# not to barf; too bad tsort doesn't take arguments.
43#
44
45cat << EOF
46/*
47 * This file was generated by tcpdump/makemib on `date`
48 * You probably don't want to edit this by hand!
49 *
50 * struct mib somename = { desc, oid-octet, type, child-pointer, next-pointer 
51};
52 */
53
54EOF
55
56# use sed to make the ASN.1 easier to parse
57# I should really just use a recursive descent parser in awk, but...
58sed \
59	-e 's/--\*.*\*--//' \
60	-e 's/--.*//' \
61	-e 's/\([{}]\)/ \1 /g' \
62	$@ \
63| gawk '
64BEGIN {
65	# for sanity, we prep the namespace with objects from RFC-1155
66	# (we manually establish the root)
67	oid["iso"]=1
68	oidadd("org", "iso", 3)
69	oidadd("dod", "org", 6)
70	oidadd("internet", "dod", 1)
71	oidadd("directory", "internet", 1)
72	oidadd("mgmt", "internet", 2)
73	oidadd("mib", "mgmt", 1)
74	oidadd("experimental", "internet", 3)
75	oidadd("private", "internet", 4)
76	oidadd("enterprises", "private", 1)
77
78	holddesc="none"
79}
80
81#
82# Read mosy "*.defs" file.  mosy does all the parsing work; we just read
83# its simple and straightforward output.  It would not be too hard to make
84# tcpdump directly read mosy output, but...
85#
86
87NF > 1 && index($2,".")>0 {
88	# currently ignore items of the form "{ iso.3.6.1 }"
89	if (split($2, p, ".") == 2)
90		oidadd($1, p[1], p[2])
91	next
92}
93
94#
95# this next section is simple and naive, but does the job 100%
96#
97
98$2$3$4 == "OBJECTIDENTIFIER::=" {
99	holddesc="none"
100	if (NF == 8)
101		oidadd($1, $6, $7)
102}
103$2 == "OBJECT-TYPE" {
104	holddesc=$1
105}
106$1 == "::=" && holddesc != "none" && NF == 5 {
107	oidadd(holddesc, $3, $4)
108	holddesc="none"
109}
110
111# 
112# End of the road - output the data.
113#
114
115END {
116	print "struct obj"
117	dump("iso")
118	print "*mibroot = &_iso_obj;"
119}
120
121#
122# add a new object to the tree
123#
124#		new OBJECT IDENTIFIER ::= { parent value }
125#
126
127function oidadd(new, parent, value) {
128	# use safe C identifiers
129	gsub(/[-&\/]/,"",new)
130	gsub(/[-&\/]/,"",parent)
131	# check if parent missing
132	if (oid[parent] == 0) {
133		printf "/* parse problem: no parent for %s.%s(%d) */\n", \
134			parent, new, value
135		return
136	}
137	# check if parent.value already exists
138	if (oid[new] > 0 && oid[new] != value) {
139		printf "/* parse problem: dup %s.%s(%d) != old (%d) */\n", \
140			parent, new, value, oid[new]
141		return
142	}
143	# check for new name for parent.value
144	if (child[parent] != "") {
145		for (sib = child[parent]; sib != ""; sib = sibling[sib])
146			if (oid[sib] == value) {
147				printf "/* parse problem: new name \"%s\"" \
148					" for %s.%s(%d) ignored */\n", \
149						new, parent, sib, value
150				return
151			}
152	}
153
154	oid[new]=value
155	if (child[parent] == "") {
156		child[parent] = new
157	} else {
158		sibling[new] = child[parent]
159		child[parent] = new
160	}
161}
162
163#
164# old(?) routine to recurse down the tree (in postfix order for convenience)
165#
166
167function dump(item, c, s) {
168#	newitem=sofar"."item"("oid[item]")"
169#	printf "/* %s c=%s s=%s */\n", newitem, child[item], sibling[item]
170	c="NULL"
171	if (child[item] != "") {
172		dump(child[item])
173		c = "&_"child[item]"_obj"
174	}
175	s="NULL"
176	if (sibling[item] != "") {
177		dump(sibling[item])
178		s = "&_"sibling[item]"_obj"
179	}
180	printf "_%s_obj = {\n\t\"%s\", %d, 0,\n\t%s, %s\n},\n", \
181		item, item, oid[item], c, s
182}
183'
184exit 0
185