makemib 6.4 KB

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