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