home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1041 / emithosts next >
Encoding:
Text File  |  1990-12-28  |  5.4 KB  |  264 lines

  1. #!/bin/csh
  2. # This is a c-shell script to eat your hosts file and
  3. # spit out several files that let you run a domain-name resolver.
  4. # Hopefully the comments explain well enough what's happening
  5. # during the course of the program.
  6. #
  7. # (C) Copyright 1990 David A. Neal. All Rights Reserved.
  8. # May not be copyrighted under GNU protections.
  9. #
  10. # Step one reads the hosts table from /etc/hosts and strips
  11. # all the comments. You must run this command on the host
  12. # that is normally your yp master. I.E. The machine you generate
  13. # your YP (OOPS, NIS) maps from.
  14. #
  15. egrep -v '#' < /etc/hosts > hosts.1
  16. #
  17. # Step two strips the ip address and the last hostname.
  18. # If you have something like:
  19. # 192.1.1.3 fred foo fred.brc.shell.com
  20. # the hostname foo will be dropped. If you really need
  21. # multiple hostnames, hack the script.
  22. #
  23. awk -f filter.awk < hosts.1 > hosts.2
  24. #
  25. # Next, we strip things like 'loghost', 'timehost', etc,
  26. # just in case they somehow filter through.
  27. #
  28. egrep -v 'loghost' < hosts.2 | \
  29. egrep -v 'mailhost' | \
  30. egrep -v 'timehost' | \
  31. sed 's/shell\.com//' > hosts.3
  32. #
  33. # Now, we sort by the domain name.
  34. #
  35. sort -t'.' +1 < hosts.3 > hosts.4
  36. #
  37. # Finally, we collect the domains.
  38. #
  39. set domains = (`cat hosts.4 | cut -f2 -d. | sort | uniq `)
  40. #
  41. # Sort out the supported domains.
  42. #
  43. set supdomains = ""
  44. set unsupdomains = ""
  45.  
  46. foreach i ( $domains )
  47.  
  48.     set unsp = "n"
  49.  
  50.     switch ($i)
  51.  
  52.     case ic:
  53.         breaksw
  54.     case brc:
  55.         breaksw
  56.     default:
  57.         set unsp="y"
  58.         breaksw
  59.     endsw
  60.  
  61.     if ( $unsp == "n" ) then
  62.         set supdomains = ( $supdomains $i )
  63.     else
  64.         set unsupdomains = ( $unsupdomains $i )
  65.     endif    
  66. end
  67.  
  68. echo "Unsupported domains: $unsupdomains"
  69.  
  70. #
  71. # Create the new distfile, so we can rdist the named.*
  72. # files to our nameservers.
  73. #
  74.  
  75. cat >distfile<<FOODIST
  76. NAMEDFILES = ( named.boot named.ca named.hosts named.local named.rev named.pri)
  77.  
  78. FOODIST
  79.  
  80.  
  81. # Now loop through each domain, and create all the 
  82. # necessary support files.
  83. #
  84.  
  85. set servers = ""
  86.  
  87. foreach i ( $supdomains )
  88.  
  89. # Knock the domainname name to upper case --
  90. # we use upper case domainnames for NIS
  91. # here, and this helps keep things straight.
  92. # Comment it out if you don't need it.
  93.  
  94.     set D = `echo $i | tr a-z A-Z `
  95.  
  96.     if ( -f $D ) then 
  97.         echo "Can't create directory for $i, flat file exists with"
  98.         echo "that name!"
  99.         break
  100.     endif
  101.  
  102.     if ( ! -d $D ) then
  103.         mkdir $D
  104.     endif
  105.  
  106. # Strip excess, since we only resolve one level
  107. # deep, we want REDWOOD.ic.shell.com to become
  108. # REDWOOD.ic. And create a host table for
  109. # each domain with the resulting entries.
  110.  
  111.     grep "^.*\.$i\." < hosts.4 > $D/named.hosts
  112.  
  113.  
  114. # Next, create the bootfile for the domain.
  115. #
  116. # phost     = the primary host (top level host for the domain)
  117. # postmaster     = the postmaster for the domain
  118. # phostip    = the ip address for the top level host
  119.  
  120.     set unsp = "n"
  121.  
  122.     switch ($D)
  123.     case IC:
  124.         set phost = redwood
  125.         set postmaster = "david"
  126.         set phostip = "134.163.28.1"
  127.         set mailto = "$postmaster.$phost.$i."
  128.         breaksw
  129.     case BRC:
  130.         set phost = murex
  131.         set postmaster = "david"
  132.         set phostip = "88.3.0.58"
  133.         set mailto = "$postmaster.$phost.$i."
  134.         breaksw
  135.     default:
  136.         echo "Can't get master server info for: $D domain!"
  137.         set phost = "unknown"
  138.         set postmaster = "unknown"
  139.         set phostip = "0.0.0.0"
  140.         set mailto = "$postmaster.$phost.$i."
  141.         set unsp="y"
  142.         breaksw
  143.     endsw
  144.  
  145. # Cache servers that are supported for our rdist file
  146. # we are in the process of creating. 
  147.  
  148.     if ( $unsp == "n" ) set servers = ($servers $phost )
  149.  
  150. # Next create a named.boot for each domain.
  151. # Tack on the domainname for now to keep
  152. # them straight.
  153.  
  154.     set revip=( `echo $phostip | tr . '\040'` )
  155.     set revphostip = "$revip[3].$revip[2].$revip[1].in-addr-arpa"
  156.  
  157.     cat > $D/named.boot <<FOOBOOT
  158. domain    $D
  159. primary    $D    /etc/named.pri
  160. primary    $revphostip    /etc/named.rev
  161. primary     0.0.127.in-addr.arpa    /etc/named.local
  162. cache    .    /etc/named.ca
  163. FOOBOOT
  164.  
  165. # Create the subsidiary file mentioned in named.boot
  166. # that actually contains all the hosts.
  167.  
  168.     cat > $D/named.pri <<FOOBOOT
  169. ;
  170. ; ***DOMAIN $D
  171. ;
  172. ;\$ORIGIN $D
  173. @    IN     SOA     $phost.$i.    $mailto (
  174.             45;
  175.             3600;
  176.             600;
  177.             360;
  178.             300 )
  179. ;
  180.         IN    NS    $phost.$i.
  181. localhost    IN    A    127.0.0.1
  182. $phost        IN    A    $phostip
  183. ;
  184. \$INCLUDE "/etc/named.hosts"
  185. ;
  186. ;
  187. FOOBOOT
  188. #
  189. # We also need a file for reverse address resolution.
  190. # As far as my meager knowledge goes, this is only
  191. # needed for the nameserver. If someone out there
  192. # in netland needs more, this is the place to put it!
  193.     cat >$D/named.rev <<FOOBOOT
  194. @    IN    SOA    $phost.$i.    $mailto (
  195.                                 1
  196.                                 3600
  197.                                 300
  198.                                 3600000
  199.                                 3600 )
  200.     IN    NS    $phost.$i.
  201. $revip[4].$revip[3]    IN    PTR    localhost.
  202. FOOBOOT
  203.  
  204. #
  205. # Finally, we create the local file that points
  206. # back to host for 127.0.0.1 resolution.
  207. # In the likely event you know a better way to
  208. # do this, feel free.
  209. cat > $D/named.local <<FOOBOOT
  210. ;name    ttl    addr-class    entry-type    origin    person
  211. @        IN        SOA        $phost.$i.    $mailto    (
  212.                                 1
  213.                                 3600
  214.                                 300
  215.                                 3600000
  216.                                 3600 )
  217.         IN        NS        $phost.$i.
  218.     1    IN        PTR        localhost.
  219. ;
  220. FOOBOOT
  221. #
  222. # We also need a cache file.
  223. #
  224. cat >$D/named.ca<<FOOBOOT
  225. ;domain        ttl        addr-class    entry-type    server
  226. .        99999999    IN        NS        $phost.$i.
  227. $phost.$i.    99999999    IN        A        $phostip
  228. FOOBOOT
  229. #
  230. end
  231. #
  232. #
  233. set supdomains = ( `echo $supdomains | tr a-z A-Z` )
  234.  
  235. cat >>distfile <<FOODIST
  236. DOMAINS = ( $supdomains )
  237. FOODIST
  238.  
  239. #
  240. #
  241.  
  242. set q = 1
  243.  
  244. while ( $q <= $#supdomains )
  245.  
  246.     echo -n "$supdomains[$q]" >> distfile
  247.     echo ":" >> distfile
  248.     echo -n "$supdomains[$q]" >> distfile
  249.     echo -n '/${NAMEDFILES}' >> distfile
  250.     echo " -> $servers[$q]" >> distfile
  251.     echo "    install /etc;" >> distfile
  252.  
  253. @ q = $q + 1;
  254.  
  255. end
  256. #
  257. # Cleanup
  258. #
  259. rm hosts.[1-4]
  260. #
  261. #
  262. #
  263.