]> git.sesse.net Git - nms/blob - config/make-dhcpd.pl
Insert extra magic for splitting nets by option 82.
[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 key DHCP_UPDATER {
44         algorithm HMAC-MD5.SIG-ALG.REG.INT;
45         secret removed;
46 }
47
48 default-lease-time 14400;
49 max-lease-time 28800;
50
51 # Tele-nett
52 subnet 194.0.254.0 netmask 255.255.255.0 {
53 }
54
55 # Server-nett
56 subnet 194.0.255.0 netmask 255.255.255.0 {
57 }
58
59 zone 0.194.in-addr.arpa. {
60         primary 127.0.0.1;
61         key DHCP_UPDATER;
62 }
63
64 EOF
65
66 for my $net (@nets) {
67         my $domain = $netnames{$net};
68
69         my ($netmask, $numpc);
70         if ($netmasks{$net} == 24) {
71                 $netmask = "255.255.255.0";
72                 $numpc = 256;
73         } elsif ($netmasks{$net} == 25) {
74                 $netmask = "255.255.255.128";
75                 $numpc = 128;
76         } elsif ($netmasks{$net} == 26) {
77                 $netmask = "255.255.255.192";
78                 $numpc = 64;
79         } else {
80                 die "Unknown netmask /$netmasks{$net}";
81         }
82
83         $net =~ /(\d+)\.(\d+)/ or die "Unknown net $net";
84         my ($majorsubnet,$minorsubnet) = ($1,$2);
85         
86         my $gw = "194.0.$majorsubnet." . ($minorsubnet + 1);
87         my $rangestart = "194.0.$majorsubnet." . ($minorsubnet + 10);
88         my $rangeend = "194.0.$majorsubnet." . ($minorsubnet + $numpc - 2);
89
90         if ($domain =~ /^split:(.*)/) {
91                 my @domains = split /,/, $1;
92                 for my $d (@domains) {
93                         print <<"EOF";
94 zone $d.tg06.gathering.org. {
95         primary 127.0.0.1;
96         key DHCP_UPDATER;
97 }
98 EOF
99                 }
100                 print <<"EOF";
101 subnet 194.0.$net netmask $netmask {
102         authoritative;
103         option routers $gw;
104
105 EOF
106                 my $numpc_sub = int($numpc / scalar(@domains));
107                 for my $d (@domains) {
108                         print <<"EOF";
109         class "$d" {
110                 match if substring ( option agent.circuit-id, 2, extract-int ( substring ( option agent.circuit-id, 3, 1 ), 8 ) ) = "$d";
111         }
112 EOF
113                 }
114
115                 my $i = 0;
116                 for my $d (@domains) {
117                         my $rangestart = "194.0.$majorsubnet." . ($minorsubnet + $i * $numpc_sub + 10);
118                         my $rangeend = "194.0.$majorsubnet." . ($minorsubnet + $i * $numpc_sub + $numpc_sub - 2);
119                         
120                         print <<"EOF";
121         pool {
122                 allow members of "$d";
123                 range $rangestart $rangeend;
124                 option domain-name "$d.tg06.gathering.org";
125                 ddns-domainname "$.tg06.gathering.org";
126                 ignore client-updates;
127         }
128 EOF
129                         ++$i;
130                 }
131         } else {
132                 print <<"EOF";
133 zone $domain.tg06.gathering.org. {
134         primary 127.0.0.1;
135         key DHCP_UPDATER;
136 }
137 subnet 194.0.$net netmask $netmask {
138         authoritative;
139         option routers $gw;
140
141         range $rangestart $rangeend;
142         option domain-name "$domain.tg06.gathering.org";
143         ddns-domainname "$domain.tg06.gathering.org";
144         ignore client-updates;
145 EOF
146
147         # hack for sesse =)
148 #       if ($net eq '250.0') {
149 #               print <<"EOF";
150 #       host trofast {
151 #               hardware ethernet 00:0e:0c:36:a7:66;
152 #               filename "/pxelinux.0";
153 #               next-server 194.0.254.89;
154 #       }
155 #EOF
156         }
157         
158         print "}\n";
159 }