]> git.sesse.net Git - nms/blob - config/make-dhcpd.pl
Fix another typo.
[nms] / config / make-dhcpd.pl
1 #! /usr/bin/perl -w
2 use strict;
3
4 # les inn nettnavn
5 my %netnames = ();
6 my %netmasks = ();
7 my @nets = ();
8 open NAMES, "switches.txt"
9         or die "switches.txt: $!";
10 while (<NAMES>) {
11         chomp;
12         /194\.0\.(\d+\.\d+)\s+(\d+)\s+(\S+)/ or next;
13         $netmasks{$1} = $2;
14         $netnames{$1} = $3;
15         push @nets, $1;
16 }       
17
18 print <<"EOF";
19 # Autogenerated by make-dhcpd.pl. Do not edit manually!
20
21 option domain-name "tg06.gathering.org";
22 option domain-name-servers 194.0.254.2;
23
24 # ddns-update-style none;
25 ddns-update-style interim;
26
27 # extra logging for option 82
28 if exists agent.circuit-id
29 {
30         log (
31                 info,
32                 concat (
33                         "option-82 info for ", binary-to-ascii (16, 8, ":", hardware),
34                         ": interface ", binary-to-ascii (10, 8, "/", suffix ( option agent.circuit-id, 2)),
35                         ", VLAN ", binary-to-ascii (10, 16, "", substring( option agent.circuit-id, 2, 2)),
36                         ", switch '", substring( option agent.remote-id, 2, 6),
37                         "', port-name '", substring ( option agent.circuit-id, 2, extract-int ( substring ( option agent.circuit-id, 3, 1 ), 8 ) ),
38                         "'"
39                )
40         );
41 }
42
43 # dnssec-keygen -a HMAC-MD5 -b 128 -n HOST DHCP_UPDATER
44 key DHCP_UPDATER {
45         algorithm HMAC-MD5.SIG-ALG.REG.INT;
46         secret 5Yz1azvh7mE0IRGffTvtKg==;
47 }
48
49 default-lease-time 14400;
50 max-lease-time 28800;
51
52 # Tele-nett
53 subnet 194.0.254.0 netmask 255.255.255.0 {
54 }
55
56 # Server-nett
57 subnet 194.0.255.0 netmask 255.255.255.0 {
58 }
59
60 zone 0.194.in-addr.arpa. {
61         primary 127.0.0.1;
62         key DHCP_UPDATER;
63 }
64
65 EOF
66
67 for my $net (@nets) {
68         my $domain = $netnames{$net};
69
70         my ($netmask, $numpc);
71         if ($netmasks{$net} == 24) {
72                 $netmask = "255.255.255.0";
73                 $numpc = 256;
74         } elsif ($netmasks{$net} == 25) {
75                 $netmask = "255.255.255.128";
76                 $numpc = 128;
77         } elsif ($netmasks{$net} == 26) {
78                 $netmask = "255.255.255.192";
79                 $numpc = 64;
80         } else {
81                 die "Unknown netmask /$netmasks{$net}";
82         }
83
84         $net =~ /(\d+)\.(\d+)/ or die "Unknown net $net";
85         my ($majorsubnet,$minorsubnet) = ($1,$2);
86         
87         my $gw = "194.0.$majorsubnet." . ($minorsubnet + 1);
88         my $rangestart = "194.0.$majorsubnet." . ($minorsubnet + 10);
89         my $rangeend = "194.0.$majorsubnet." . ($minorsubnet + $numpc - 2);
90
91         if ($domain =~ /^split:(.*)/) {
92                 my @domains = split /,/, $1;
93                 for my $d (@domains) {
94                         print <<"EOF";
95 zone $d.tg06.gathering.org. {
96         primary 127.0.0.1;
97         key DHCP_UPDATER;
98 }
99 EOF
100                 }
101                 print <<"EOF";
102 subnet 194.0.$net netmask $netmask {
103         authoritative;
104         option routers $gw;
105
106 EOF
107                 my $numpc_sub = int($numpc / scalar(@domains));
108                 for my $d (@domains) {
109                         print <<"EOF";
110         class "$d" {
111                 match if substring ( option agent.circuit-id, 2, extract-int ( substring ( option agent.circuit-id, 3, 1 ), 8 ) ) = "$d";
112         }
113 EOF
114                 }
115
116                 my $i = 0;
117                 for my $d (@domains) {
118                         my $rangestart = "194.0.$majorsubnet." . ($minorsubnet + $i * $numpc_sub + 10);
119                         my $rangeend = "194.0.$majorsubnet." . ($minorsubnet + $i * $numpc_sub + $numpc_sub - 2);
120                         
121                         print <<"EOF";
122         pool {
123                 allow members of "$d";
124                 range $rangestart $rangeend;
125                 option domain-name "$d.tg06.gathering.org";
126                 ddns-domainname "$d.tg06.gathering.org";
127                 ignore client-updates;
128         }
129 EOF
130                         ++$i;
131                 }
132         } else {
133                 print <<"EOF";
134 zone $domain.tg06.gathering.org. {
135         primary 127.0.0.1;
136         key DHCP_UPDATER;
137 }
138 subnet 194.0.$net netmask $netmask {
139         authoritative;
140         option routers $gw;
141
142         range $rangestart $rangeend;
143         option domain-name "$domain.tg06.gathering.org";
144         ddns-domainname "$domain.tg06.gathering.org";
145         ignore client-updates;
146 EOF
147
148         # hack for sesse =)
149 #       if ($net eq '250.0') {
150 #               print <<"EOF";
151 #       host trofast {
152 #               hardware ethernet 00:0e:0c:36:a7:66;
153 #               filename "/pxelinux.0";
154 #               next-server 194.0.254.89;
155 #       }
156 #EOF
157         }
158         
159         print "}\n";
160 }