source: genesis/config/iris/node/FreeBSD/8.0-RELEASE/wleiden.pl@ 9255

Last change on this file since 9255 was 9255, checked in by rick, 14 years ago

Actually run over all DHCP interfaces.

  • Property svn:executable set to *
File size: 10.3 KB
Line 
1#!/usr/bin/perl -w
2#
3# Copyright 2005 Stichting Wireless Leiden
4# maart 2004 rick@wirelessleiden.nl
5#
6
7# Config located at other file
8my $conf_file="./genesis.conf";
9do($conf_file) || die("Cann't open $conf_file");
10################ END OF CONFIG ##########################
11
12#variablen
13my $time=gmtime();
14my $source=`/bin/hostname`;
15chomp($source);
16
17
18#slurp IP berekeningen info
19do ("$IP_pmPath") || die ("Cann't open $IP_pmPath");
20#slurp dns info
21#do ("$dnsheader_confPath") || die ("Cann't open $dnsheader_confPath");
22
23
24#zoek uit of het master_ip addr voorkomt in de configs
25#belangrijk van aliassen
26sub master_ipNotUsed {
27 if( $debug ) {
28 print "running master_ipNotUsed...\n";
29 }
30 foreach my $if (keys %config) {
31 if( $IP{$if} =~ /([0-9\.]+).*/ ) {
32 if( $1 eq $master_ip ) {
33 return(0);
34 };
35 };
36 };
37 return(1);
38};
39
40
41
42sub genHeader {
43 my $comment = $_[0];
44 my $output =
45 "$comment This file specific to wireless\n" .
46 "$comment leiden. Please make all changes in Genesis.\n" .
47 "$comment\n" .
48 "$comment Generated by $source\n" .
49 "$comment on $time\n" .
50 "$comment\n" .
51 "$comment $author\n" .
52 "$comment\n\n\n";
53 return ($output);
54};
55
56
57sub txtconfig {
58 my $output = "";
59 foreach $interface (keys %config) {
60 $output .= $config{$interface};
61 };
62 return($output);
63};
64
65
66sub dnsmasq_conf {
67 my $output = genHeader("#");
68 $output .=
69 "# DHCP server options \n" .
70 "dhcp-authoritative \n" .
71 "dhcp-fqdn \n" .
72 "domain=dhcp.$nodename.$domain. \n" .
73 "domain-needed \n" .
74 "expand-hosts \n" .
75 "\n" .
76 "# Low memory footprint \n" .
77 "cache-size=10000 \n" .
78 "\n";
79
80 foreach my $interface (sort keys %config) {
81 if( $interface =~ /^[a-z]+[0-9]+$/i ) {
82 (my $ip, my $netmask) = split('/', $IP{$interface});
83 my $subnet = IP::toSubnet($netmask);
84
85 $output .=
86 "## $interface $DESC{$interface}\n";
87
88 if ( $DHCP{$interface} =~ /[0-9]+\-[0-9]+/i ) {
89 my $dhcp_part = $ip;
90 $dhcp_part =~ s/[0-9]+$//;
91 (my $dhcp_start, my $dhcp_stop) = $DHCP{$interface} =~ /([0-9]+)\-([0-9]+)/i;
92 $dhcp_start = $dhcp_part . $dhcp_start;
93 $dhcp_stop = $dhcp_part . $dhcp_stop;
94 $output .= "dhcp-range=$interface,$dhcp_start,$dhcp_stop,$subnet,24h\n";
95 foreach my $dhcp_static (split(';', $DHCP_STATIC{$interface})) {
96 $output .= "dhcp-host=$dhcp_static\n";
97 };
98 $output .= "\n";
99 }
100 else {
101 $output .= "# not autoritive \n\n";
102 };
103
104 };
105 };
106
107 return($output);
108};
109
110
111sub dhcpd_conf {
112 my $output = genHeader("#");
113 $output .=
114 "option domain-name \"$domain\";\n" .
115 " \n" .
116 "default-lease-time 7200;\n" .
117 "max-lease-time 2592000;\n" .
118 "\n" .
119 "ddns-update-style none;\n" .
120 "\n" .
121 "# Hack for the WET11\n" .
122 "#\n" .
123 "always-broadcast on;\n" .
124 "\n" .
125 "option domain-name-servers ${master_ip};\n" .
126 "\n";
127
128 foreach my $interface (sort keys %config) {
129 if( $interface =~ /^[a-z]+[0-9]+$/i ) {
130 (my $ip, my $netmask) = split('/', $IP{$interface});
131 my $subnet = IP::toSubnet($netmask);
132 my $broadcast = IP::getBroadcastAddr($ip, $subnet);
133 my $network = IP::getNetworkAddr($ip, $subnet);
134
135 $output .=
136 "# $interface $DESC{$interface}\n";
137
138 if ( $DHCP{$interface} =~ /[0-9]+\-[0-9]+/i ) {
139 my $dhcp_part = $ip;
140 $dhcp_part =~ s/[0-9]+$//;
141 (my $dhcp_start, my $dhcp_stop) = $DHCP{$interface} =~ /([0-9]+)\-([0-9]+)/i;
142 $dhcp_start = $dhcp_part . $dhcp_start;
143 $dhcp_stop = $dhcp_part . $dhcp_stop;
144 $output .=
145 "subnet $network netmask $subnet {\n" .
146 " range $dhcp_start $dhcp_stop;\n" .
147 " option broadcast-address $broadcast;\n" .
148 " option subnet-mask $subnet;\n" .
149 " option routers $ip;\n" .
150 $DHCP_STATIC{$if} .
151 "}\n" .
152 "\n";
153 }
154 else {
155 $output .=
156 "subnet $network netmask $subnet {not authoritative; }\n" .
157 "\n";
158 };
159 };
160 };
161
162 return($output);
163};
164
165sub named_conf {
166 my $output = genHeader("#");
167 $output .=
168 "options {\n" .
169 " directory \"/etc/namedb\"\;\n" .
170 " pid-file \"/var/run/named/pid\"\;\n" .
171 " forwarders {\n";
172 foreach my $forward (@forwarder) {
173 $output .= "$forward;\n";
174 };
175 $output .=
176 " };\n" .
177 "};\n" .
178 "\n" .
179 "\n" .
180 "zone \"\.\" {\n" .
181 " type hint;\n" .
182 " file \"/etc/namedb/named.root\"\;\n" .
183 "}\;\n" .
184 "\n" .
185 "zone \"0\.0\.127\.IN-ADDR.ARPA\" {\n" .
186 " type master\;\n" .
187 " file \"/etc/namedb/master/localhost.rev\"\;\n" .
188 "}\;\n" .
189 "\n" .
190 "zone \"1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.IP6.INT\" {\n" .
191 " type master\;\n" .
192 " file \"/etc/namedb/master/localhost-v6.rev\"\;\n" .
193 "};\n" .
194 "\n";
195
196
197 foreach my $tmpZone (sort keys %zone) {
198 my $dnsZone = $zone{$tmpZone};
199 $output .=
200 "zone \"$tmpZone\" {\n" .
201 " type slave\;\n" .
202 " file \"slave/slave-$tmpZone\"\;\n" .
203 " masters {\n";
204 foreach my $tmpIP (sort @$dnsZone) {
205 $output .= " $tmpIP\;\n";
206 };
207 $output .=
208 " };\n" .
209 "};\n" .
210 "\n";
211 };
212
213 return($output);
214};
215
216sub wleiden_conf {
217 # Generate our header
218 my $output = genHeader("#");
219
220 # Get al proxies
221 opendir(BIN, $ndir) or die "Can't open $dir: $!";
222 @proxyfiles = grep(/proxy/, readdir(BIN));
223 closedir(BIN);
224
225 # Walk through all our config files and get master_ip (and are up)
226 @proxies = ();
227 foreach $file (@proxyfiles) {
228 $nfile = "$ndir/$file/wleiden.conf";
229 parse_config($nfile);
230 if ($status == "up") {
231 $proxies[$proxyid] = $master_ip;
232 }
233 }
234
235 # Print the Ip's in shell variable script format
236 $output .= "PROXIES=`cat <<EOF\n";
237 foreach $ip (@proxies) {
238 if ($ip) {
239 $output .= "$ip\n";
240 }
241 }
242 $output .= "`\n";
243}
244
245sub rc_conf_local {
246 my $output = genHeader("#");
247 my $masterNotUsed = master_ipNotUsed();
248 $output.=
249 "hostname=\"$nodetype$nodename.$domain\"\n" .
250 "location=\"$location\"\n" .
251 "\n";
252
253 if( $tproxy ) {
254 if( $tproxy =~ m/\d+\.\d+\.\d+\.\d+\/\d+/ ) {
255 $output .=
256 "# Tproxy is ran on this system\n".
257 "tproxy_enable='YES'\n".
258 "tproxy_range='$tproxy'\n".
259 "\n";
260 }
261 elsif( $tproxy !~ m/no/i ) {
262 $output .= "# WARNING - specification propably wrong - check " .
263 "genesis. It should be a pure CIDR\n";
264 };
265 };
266
267 $iplist = "172.31.255.1/32";
268 if( $masterNotUsed ) {
269 $iplist .= " $master_ip/32";
270 }
271 $output .= "ipv4_addrs_lo0=\"127.0.0.1/8 $iplist\"\n";
272
273 @whitelist = ();
274 @dhcp_interfaces = ();
275 $WLAN_NR = 0;
276 foreach my $interface (sort keys %config) {
277 (my $if, my $number) = split(/:/, $interface);
278 if( not defined $number ) {
279 # No special syntax for aliases anymore
280 $IFNAME = $if;
281 if( $TYPE{$if} =~ /wireless/i ) {
282 $IFNAME = "wlan$WLAN_NR";
283 $WLAN_NR++;
284 $output .= "wlans_$if=\"$IFNAME\"\n";
285 $output .= "create_args_$IFNAME=\"";
286 if( $MODE{$if} =~/master/i ) {
287 $output .= "wlanmode ap";
288 } else {
289 $output .= "wlanmode sta"
290 }
291 if ( $SUBTYPE{$if} =~ /802.11a/i ) {
292 $output .= " mode 11a";
293 } elsif ( $SUBTYPE{$if} =~ /802.11g/i ) {
294 $output .= " mode 11g";
295 } else {
296 # Default output
297 $output .= " mode 11b";
298 }
299 $output .= " ssid $ESSID{$interface} regdomain ETSI country NL";
300 if( $MODE{$if} =~/master/i ) {
301 $output .= " channel $CHANNEL{$interface}";
302 }
303 $output .= "\"\n";
304 }
305
306 if ( $CAPTIVE_PORTAL_WHITELIST{$if} ) {
307 push(@whitelist, $CAPTIVE_PORTAL_WHITELIST{$if});
308 }
309 if ($DHCP{$if} =~ /[0-9]+\-[0-9]+/i) {
310 push(@dhcp_interfaces, $IFNAME);
311 }
312
313 @iplist = ();
314 foreach my $interface (sort keys %config) {
315 (my $if_t, my $number) = split(/:/, $interface);
316 if ( $if_t eq $if ) {
317 push(@iplist,$IP{$interface});
318 };
319 };
320 $output .= "ipv4_addrs_$IFNAME=\"" . join(' ',@iplist) . "\"\n";
321 $output .= "\n";
322 };
323 };
324 $output .= "\ncaptive_portal_whitelist=\"" .join(' ',@whitelist) . "\"\n";
325 $output .= "captive_portal_interfaces=\"" .join(' ',@dhcp_interfaces) . "\"\n";
326 return($output);
327};
328
329
330sub resolv_conf {
331 my $output = genHeader"#";
332
333 $output .=
334 "search wleiden.net\n" .
335 "# Try local (cache) first \n" .
336 "nameserver 127.0.0.1\n" .
337 "\n";
338
339 $output .=
340 "# proxies are also nameservers \n" .
341 "nameserver 172.17.8.68 # proxy1\n" .
342 "nameserver 172.17.143.4 # proxy2\n" .
343 "nameserver 172.20.128.98 # proxy3\n" .
344 "nameserver 172.16.2.254 # proxy4\n" .
345 "nameserver 172.19.168.66 # proxy5\n" .
346 "nameserver 172.16.3.146 # proxy6\n" .
347 "nameserver 172.17.16.66 # proxy62\n" .
348 "nameserver 172.17.0.1 # proxy7\n" .
349 "nameserver 172.16.4.54 # proxy9\n" .
350 "nameserver 172.22.0.66 # proxy10\n" .
351 "nameserver 172.23.25.66 # proxy11\n" .
352 "nameserver 172.17.169.66 # proxy97\n";
353
354 return($output);
355};
356
357
358sub parse_config {
359 my $workingfile = $_[0];
360 do($workingfile) || die("Cann't open/parse $workingfile");
361 foreach my $if (keys %config) {
362 my $cfg=$config{$if};
363 while ($cfg) {
364 $cfg=~s/^([^\n\r]+)[\r\n]*//m;
365 my $line=$1;
366 $line=~s/\s*#.*//;
367 if (((my $name, my $value)=split(/=/,$line)) eq 2) {
368 my $doit="if (exists(\$$name\{\"$if\"\})) {\$$name\{\"$if\"\}.=\";$value\";} else {\$$name\{\"$if\"\}.=\"$value\";}";
369 eval($doit);
370 };
371 $cfg=~s/[\r\n]*$//m;
372 };
373 };
374};
375
376sub authorized_keys {
377 my $output = genHeader("#");
378 if( -e "$global_keyPath" ) {
379 open( GLOBAL, "$global_keyPath" ) || die ("Cann't open $global_keyPath");
380 $output .= join("", <GLOBAL>);
381 close( GLOBAL );
382 }
383 else {
384 $output .= "# No $global_keyPath\n";
385 };
386
387 if( -e "$ndir/$nodetype$nodename/$ssh_file" ) {
388 open( NODE, "$ndir/$nodetype$nodename/$ssh_file" ) || die ("Cann't open $home/$nodename/$ssh_file");
389 $output .= join("", <NODE>);
390 close( NODE );
391 }
392 else {
393 $output .= "# No $ndir/$nodetype$nodename/$ssh_file\n";
394 };
395
396 return($output);
397};
398
399sub do_it {
400 my $file = $_[0];
401 my $body = "";
402
403 $file =~ s/\./_/g;
404 $body=&$file;
405 return($body);
406};
407
408if( exists $ARGV[0] ) {
409 if( exists $ARGV[1] ) {
410 parse_config($ARGV[0]);
411 print do_it($ARGV[1]);
412 }
413 else {
414 print "Usage `perl wleiden.pl 'inputfile' 'outputfile'`\n";
415 };
416};
417
4181;
Note: See TracBrowser for help on using the repository browser.