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
|
---|
8 | my $conf_file="/home/genesis/tools/FreeBSD5_x/genesis.conf";
|
---|
9 | do($conf_file) || die("Cann't open $conf_file");
|
---|
10 | ################ END OF CONFIG ##########################
|
---|
11 |
|
---|
12 | #variablen
|
---|
13 | my $time=gmtime();
|
---|
14 | my $source=`/bin/hostname`;
|
---|
15 | chomp($source);
|
---|
16 |
|
---|
17 |
|
---|
18 | #slurp IP berekeningen info
|
---|
19 | do ("$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
|
---|
26 | sub 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 |
|
---|
42 | sub 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 |
|
---|
57 | sub txtconfig {
|
---|
58 | my $output = "";
|
---|
59 | foreach $interface (keys %config) {
|
---|
60 | $output .= $config{$interface};
|
---|
61 | };
|
---|
62 | return($output);
|
---|
63 | };
|
---|
64 |
|
---|
65 |
|
---|
66 | sub dhcpd_conf {
|
---|
67 | my $output = genHeader("#");
|
---|
68 | $output .=
|
---|
69 | "option domain-name \"$domain\";\n" .
|
---|
70 | " \n" .
|
---|
71 | "default-lease-time 7200;\n" .
|
---|
72 | "max-lease-time 2592000;\n" .
|
---|
73 | "\n" .
|
---|
74 | "ddns-update-style none;\n" .
|
---|
75 | "\n" .
|
---|
76 | "# Hack for the WET11\n" .
|
---|
77 | "#\n" .
|
---|
78 | "always-broadcast on;\n" .
|
---|
79 | "\n" .
|
---|
80 | "option domain-name-servers ${master_ip};\n" .
|
---|
81 | "\n";
|
---|
82 |
|
---|
83 | foreach my $interface (sort keys %config) {
|
---|
84 | if( $interface =~ /^[a-z]+[0-9]+$/i ) {
|
---|
85 | (my $ip, my $netmask) = split('/', $IP{$interface});
|
---|
86 | my $subnet = IP::toSubnet($netmask);
|
---|
87 | my $broadcast = IP::getBroadcastAddr($ip, $subnet);
|
---|
88 | my $network = IP::getNetworkAddr($ip, $subnet);
|
---|
89 |
|
---|
90 | $output .=
|
---|
91 | "# $interface $DESC{$interface}\n";
|
---|
92 |
|
---|
93 | if ( $DHCP{$interface} =~ /[0-9]+\-[0-9]+/i ) {
|
---|
94 | my $dhcp_part = $ip;
|
---|
95 | $dhcp_part =~ s/[0-9]+$//;
|
---|
96 | (my $dhcp_start, my $dhcp_stop) = $DHCP{$interface} =~ /([0-9]+)\-([0-9]+)/i;
|
---|
97 | $dhcp_start = $dhcp_part . $dhcp_start;
|
---|
98 | $dhcp_stop = $dhcp_part . $dhcp_stop;
|
---|
99 | $output .=
|
---|
100 | "subnet $network netmask $subnet {\n" .
|
---|
101 | " range $dhcp_start $dhcp_stop;\n" .
|
---|
102 | " option broadcast-address $broadcast;\n" .
|
---|
103 | " option subnet-mask $subnet;\n" .
|
---|
104 | " option routers $ip;\n" .
|
---|
105 | "}\n" .
|
---|
106 | "\n";
|
---|
107 | }
|
---|
108 | else {
|
---|
109 | $output .=
|
---|
110 | "subnet $network netmask $subnet {not authoritative; }\n" .
|
---|
111 | "\n";
|
---|
112 | };
|
---|
113 | };
|
---|
114 | };
|
---|
115 |
|
---|
116 | return($output);
|
---|
117 | };
|
---|
118 |
|
---|
119 | sub named_conf {
|
---|
120 | my $output = genHeader("#");
|
---|
121 | $output .=
|
---|
122 | "options {\n" .
|
---|
123 | " directory \"/etc/namedb\"\;\n" .
|
---|
124 | " pid-file \"/var/run/named.pid\"\;\n" .
|
---|
125 | " forwarders {\n";
|
---|
126 | foreach my $forward (@forwarder) {
|
---|
127 | $output .= "$forward;\n";
|
---|
128 | };
|
---|
129 | $output .=
|
---|
130 | " };\n" .
|
---|
131 | "};\n" .
|
---|
132 | "\n" .
|
---|
133 | "\n" .
|
---|
134 | "zone \"\.\" {\n" .
|
---|
135 | " type hint;\n" .
|
---|
136 | " file \"/etc/namedb/named.root\"\;\n" .
|
---|
137 | "}\;\n" .
|
---|
138 | "\n" .
|
---|
139 | "zone \"0\.0\.127\.IN-ADDR.ARPA\" {\n" .
|
---|
140 | " type master\;\n" .
|
---|
141 | " file \"/etc/namedb/localhost.rev\"\;\n" .
|
---|
142 | "}\;\n" .
|
---|
143 | "\n" .
|
---|
144 | "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" .
|
---|
145 | " type master\;\n" .
|
---|
146 | " file \"/etc/namedb/localhost-v6.rev\"\;\n" .
|
---|
147 | "};\n" .
|
---|
148 | "\n";
|
---|
149 |
|
---|
150 |
|
---|
151 | foreach my $tmpZone (sort keys %zone) {
|
---|
152 | my $dnsZone = $zone{$tmpZone};
|
---|
153 | $output .=
|
---|
154 | "zone \"$tmpZone\" {\n" .
|
---|
155 | " type slave\;\n" .
|
---|
156 | " file \"slave-$tmpZone\"\;\n" .
|
---|
157 | " masters {\n";
|
---|
158 | foreach my $tmpIP (sort @$dnsZone) {
|
---|
159 | $output .= " $tmpIP\;\n";
|
---|
160 | };
|
---|
161 | $output .=
|
---|
162 | " };\n" .
|
---|
163 | "};\n" .
|
---|
164 | "\n";
|
---|
165 | };
|
---|
166 |
|
---|
167 | return($output);
|
---|
168 | };
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 | sub rc_node_local {
|
---|
173 | my $output = genHeader("#");
|
---|
174 | my $masterNotUsed = master_ipNotUsed();
|
---|
175 | $output.=
|
---|
176 | "hostname=\"$nodetype$nodename.$domain\"\n" .
|
---|
177 | "location=\"$location\"\n" .
|
---|
178 | "\n";
|
---|
179 |
|
---|
180 | if( $tproxy ) {
|
---|
181 | if( $tproxy =~ m/\d+\.\d+\.\d+\.\d+\/\d+/ ) {
|
---|
182 | $output .=
|
---|
183 | "# Tproxy is ran on this system\n".
|
---|
184 | "tproxy_enable='YES'\n".
|
---|
185 | "tproxy_range='$tproxy'\n".
|
---|
186 | "\n";
|
---|
187 | }
|
---|
188 | elsif( $tproxy !~ m/no/i ) {
|
---|
189 | $output .= "# WARNING - specification propably wrong - check " .
|
---|
190 | "genesis. It should be a pure CIDR\n";
|
---|
191 | };
|
---|
192 | };
|
---|
193 |
|
---|
194 | $output .= "ifconfig_lo0_alias0=\"inet 172.31.255.1/32\"\n";
|
---|
195 | if( $masterNotUsed ) {
|
---|
196 | $output .= "ifconfig_lo0_alias1=\"inet $master_ip/32\"\n\n";
|
---|
197 | }
|
---|
198 | else {
|
---|
199 | $output .= "#ifconfig_lo0_alias1=\"inet $master_ip/32\"\n\n";
|
---|
200 | };
|
---|
201 |
|
---|
202 | foreach my $interface (sort keys %config) {
|
---|
203 | (my $if, my $number) = split(/:/, $interface);
|
---|
204 | if( defined $number ) {
|
---|
205 | $output .= "ifconfig_$if\_alias$number=\"inet $IP{$interface}\"\n";
|
---|
206 | }
|
---|
207 | else {
|
---|
208 | $output .= "ifconfig_$if=\"inet $IP{$interface}";
|
---|
209 | $output .= " $CARD_OPTION{$interface}";
|
---|
210 | if( $TYPE{$if} =~ /wireless/i ) {
|
---|
211 | $output .= " ssid $ESSID{$interface}";
|
---|
212 | if( $MODE{$if}=~/master/i ) {
|
---|
213 | $output .= " channel $CHANNEL{$interface}";
|
---|
214 | $output .= " mediaopt hostap";
|
---|
215 | }
|
---|
216 | else {
|
---|
217 | $output .= "";
|
---|
218 | };
|
---|
219 | };
|
---|
220 | $output .= "\"\n";
|
---|
221 | };
|
---|
222 | };
|
---|
223 | return($output);
|
---|
224 | };
|
---|
225 |
|
---|
226 |
|
---|
227 | sub resolv_conf {
|
---|
228 | my $output = genHeader"#";
|
---|
229 |
|
---|
230 | $output .=
|
---|
231 | "search wleiden.net.\n" .
|
---|
232 | "nameserver 127.0.0.1\n" .
|
---|
233 | "\n";
|
---|
234 |
|
---|
235 | foreach my $if (sort keys %config) {
|
---|
236 | if( exists $POINT_TO_POINT{$if} ) {
|
---|
237 | foreach my $ip ($POINT_TO_POINT{$if}) {
|
---|
238 | $output .= "nameserver $ip # $DESC{$if}\n";
|
---|
239 | };
|
---|
240 | };
|
---|
241 | };
|
---|
242 | return($output);
|
---|
243 | };
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 | sub snmpd_local_conf {
|
---|
248 | my $output = genHeader("#");
|
---|
249 | my $masterNotUsed = master_ipNotUsed();
|
---|
250 | $output .=
|
---|
251 | "# Location of the physical node.\n" .
|
---|
252 | "#\n" .
|
---|
253 | "syslocation \"$location\"\n" .
|
---|
254 | "#\n" .
|
---|
255 | "# Maintained by\n" .
|
---|
256 | "syscontact \"$contact\"\n" .
|
---|
257 | "\n";
|
---|
258 |
|
---|
259 | if( $DISK =~ /flash/i ) {
|
---|
260 | $output .= "# Verify that disk is RO\n";
|
---|
261 | $output .= "sh diskro /usr/local/sbin/diskro.sh\n\n";
|
---|
262 | };
|
---|
263 |
|
---|
264 | $output .= "agentaddress 161,tcp:161\n";
|
---|
265 |
|
---|
266 | if( $masterNotUsed ) {
|
---|
267 | $output .= "agentaddress $master_ip\n";
|
---|
268 | };
|
---|
269 |
|
---|
270 | foreach my $if (keys %config) {
|
---|
271 | if( $IP{$if} =~ /([0-9\.]+).*/ ) {
|
---|
272 | $output .= "agentaddress $1\n";
|
---|
273 | };
|
---|
274 | };
|
---|
275 | return($output);
|
---|
276 | };
|
---|
277 |
|
---|
278 |
|
---|
279 |
|
---|
280 | sub parse_config {
|
---|
281 | my $workingfile = $_[0];
|
---|
282 | do($workingfile) || die("Cann't open/parse $workingfile");
|
---|
283 | foreach my $if (keys %config) {
|
---|
284 | my $cfg=$config{$if};
|
---|
285 | while ($cfg) {
|
---|
286 | $cfg=~s/^([^\n\r]+)[\r\n]*//m;
|
---|
287 | my $line=$1;
|
---|
288 | $line=~s/\s*#.*//;
|
---|
289 | if (((my $name, my $value)=split(/=/,$line)) eq 2) {
|
---|
290 | my $doit="if (exists(\$$name\{\"$if\"\})) {\$$name\{\"$if\"\}.=\";$value\";} else {\$$name\{\"$if\"\}.=\"$value\";}";
|
---|
291 | eval($doit);
|
---|
292 | };
|
---|
293 | $cfg=~s/[\r\n]*$//m;
|
---|
294 | };
|
---|
295 | };
|
---|
296 | };
|
---|
297 |
|
---|
298 | sub authorized_keys {
|
---|
299 | my $output = genHeader("#");
|
---|
300 | if( -e "$global_keyPath" ) {
|
---|
301 | open( GLOBAL, "$global_keyPath" ) || die ("Cann't open $global_keyPath");
|
---|
302 | $output .= join("", <GLOBAL>);
|
---|
303 | close( GLOBAL );
|
---|
304 | }
|
---|
305 | else {
|
---|
306 | $output .= "# No $global_keyPath\n";
|
---|
307 | };
|
---|
308 |
|
---|
309 | if( -e "$ndir/$nodetype$nodename/$ssh_file" ) {
|
---|
310 | open( NODE, "$ndir/$nodetype$nodename/$ssh_file" ) || die ("Cann't open $home/$nodename/$ssh_file");
|
---|
311 | $output .= join("", <NODE>);
|
---|
312 | close( NODE );
|
---|
313 | }
|
---|
314 | else {
|
---|
315 | $output .= "# No $ndir/$nodetype$nodename/$ssh_file\n";
|
---|
316 | };
|
---|
317 |
|
---|
318 | return($output);
|
---|
319 | };
|
---|
320 |
|
---|
321 | sub do_it {
|
---|
322 | my $file = $_[0];
|
---|
323 | my $body = "";
|
---|
324 |
|
---|
325 | $file =~ s/\./_/g;
|
---|
326 | $body=&$file;
|
---|
327 | return($body);
|
---|
328 | };
|
---|
329 |
|
---|
330 | if( exists $ARGV[0] ) {
|
---|
331 | if( exists $ARGV[1] ) {
|
---|
332 | parse_config($ARGV[0]);
|
---|
333 | print do_it($ARGV[1]);
|
---|
334 | }
|
---|
335 | else {
|
---|
336 | print "Usage `perl wleiden.pl 'inputfile' 'outputfile'`\n";
|
---|
337 | };
|
---|
338 | };
|
---|
339 |
|
---|
340 | 1;
|
---|