immernoch das alte Problem...
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / wireless.cgi
CommitLineData
cd1a2927
MT
1#!/usr/bin/perl\r
2#\r
3# IPCop CGIs\r
4#\r
5# This code is distributed under the terms of the GPL\r
6#\r
7# (c) 2003 Alan Hourihane <alanh@fairlite.demon.co.uk>\r
8# (c) 2005 Eric Oberlander, Robert Kerr - Inline editing & DHCP leases\r
9#\r
10# $Id: wireless.cgi,v 1.4.2.15 2005/06/11 12:14:49 eoberlander Exp $\r
11#\r
12\r
13use strict;\r
14use Time::Local;\r
15\r
16# enable only the following on debugging purpose\r
17#use warnings;\r
18#use CGI::Carp 'fatalsToBrowser';\r
19\r
20require 'CONFIG_ROOT/general-functions.pl';\r
21require "${General::swroot}/lang.pl";\r
22require "${General::swroot}/header.pl";\r
23\r
24#workaround to suppress a warning when a variable is used only once\r
25my @dummy = ( ${Header::colouryellow} );\r
26undef (@dummy);\r
27\r
28my %cgiparams=();\r
29my %checked=();\r
30my $errormessage = '';\r
31my $filename = "${General::swroot}/wireless/config";\r
32my $hostsfile = "${General::swroot}/main/hosts";\r
33our %dhcpsettings=(); \r
34our %netsettings=();\r
35\r
36$cgiparams{'ENABLED'} = 'off';\r
37$cgiparams{'ACTION'} = '';\r
38$cgiparams{'VALID'} = '';\r
39$cgiparams{'SOURCE_IP'} ='';\r
40$cgiparams{'SOURCE_MAC'} ='';\r
41$cgiparams{'REMARK'} ='';\r
42\r
43&Header::getcgihash(\%cgiparams);\r
44\r
45&General::readhash("${General::swroot}/dhcp/settings", \%dhcpsettings);\r
46&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);\r
47\r
48&Header::showhttpheaders();\r
49\r
50open(FILE, $filename) or die 'Unable to open config file.';\r
51my @current = <FILE>;\r
52close(FILE);\r
53\r
54if ($cgiparams{'ACTION'} eq 'add')\r
55{\r
56\r
57 if ($cgiparams{'SOURCE_IP'} eq '' && $cgiparams{'SOURCE_MAC'} eq '')\r
58 {\r
59 goto ADDEXIT;\r
60 }\r
61\r
62 $cgiparams{'SOURCE_MAC'} =~ tr/-/:/;\r
63\r
64 my $key = 0;\r
65 foreach my $line (@current)\r
66 {\r
67 $key++;\r
68 my @temp = split(/\,/,$line);\r
69\r
70 if ($temp[1] ne '' && $cgiparams{'SOURCE_IP'} eq $temp[1] && $cgiparams{'EDITING'} ne $key)\r
71 {\r
72 $errormessage = $Lang::tr{'duplicate ip'};\r
73 goto ADDERROR;\r
74 }\r
75 if ($temp[2] ne '' && lc($cgiparams{'SOURCE_MAC'}) eq lc($temp[2]) && $cgiparams{'EDITING'} ne $key)\r
76 {\r
77 $errormessage = $Lang::tr{'duplicate mac'};\r
78 goto ADDERROR;\r
79 }\r
80 }\r
81\r
82 if ($cgiparams{'SOURCE_IP'} eq '')\r
83 {\r
84 $cgiparams{'SOURCE_IP'} = 'NONE';\r
85 } else {\r
86 unless(&General::validip($cgiparams{'SOURCE_IP'})) \r
87 {\r
88 $errormessage = $Lang::tr{'invalid fixed ip address'}; \r
89 goto ADDERROR;\r
90 }\r
91 }\r
92 if ($cgiparams{'SOURCE_MAC'} eq '')\r
93 {\r
94 $cgiparams{'SOURCE_MAC'} = 'NONE';\r
95 } else {\r
96 unless(&General::validmac($cgiparams{'SOURCE_MAC'})) \r
97 { \r
98 $errormessage = $Lang::tr{'invalid fixed mac address'}; \r
99 }\r
100 }\r
101\r
102ADDERROR:\r
103 if ($errormessage)\r
104 {\r
105 $cgiparams{'SOURCE_MAC'} = '' if $cgiparams{'SOURCE_MAC'} eq 'NONE';\r
106 $cgiparams{'SOURCE_IP'} = '' if $cgiparams{'SOURCE_IP'} eq 'NONE';\r
107 } else {\r
108 if ($cgiparams{'EDITING'} eq 'no') {\r
109 open(FILE,">>$filename") or die 'Unable to open config file.';\r
110 flock FILE, 2;\r
111 print FILE "$key,$cgiparams{'SOURCE_IP'},$cgiparams{'SOURCE_MAC'},$cgiparams{'ENABLED'},$cgiparams{'REMARK'}\n";\r
112 } else {\r
113 open(FILE,">$filename") or die 'Unable to open config file.';\r
114 flock FILE, 2;\r
115 my $id = 0;\r
116 foreach my $line (@current)\r
117 {\r
118 $id++;\r
119 if ($cgiparams{'EDITING'} eq $id) {\r
120 print FILE "$id,$cgiparams{'SOURCE_IP'},$cgiparams{'SOURCE_MAC'},$cgiparams{'ENABLED'},$cgiparams{'REMARK'}\n";\r
121 } else { print FILE "$line"; }\r
122 }\r
123 }\r
124 close(FILE);\r
125 undef %cgiparams;\r
126 &General::log($Lang::tr{'wireless config added'});\r
127 system('/usr/local/bin/restartwireless');\r
128 }\r
129ADDEXIT:\r
130}\r
131\r
132if ($cgiparams{'ACTION'} eq 'edit')\r
133{\r
134 my $id = 0;\r
135 foreach my $line (@current)\r
136 {\r
137 $id++;\r
138 if ($cgiparams{'ID'} eq $id)\r
139 {\r
140 chomp($line);\r
141 my @temp = split(/\,/,$line);\r
142 $cgiparams{'SOURCE_IP'} = $temp[1];\r
143 $cgiparams{'SOURCE_MAC'} = $temp[2];\r
144 $cgiparams{'ENABLED'} = $temp[3];\r
145 $cgiparams{'REMARK'} = $temp[4];\r
146 $cgiparams{'SOURCE_IP'} = '' if $cgiparams{'SOURCE_IP'} eq 'NONE';\r
147 $cgiparams{'SOURCE_MAC'} = '' if $cgiparams{'SOURCE_MAC'} eq 'NONE';\r
148 }\r
149 }\r
150 &General::log($Lang::tr{'wireless config changed'});\r
151 system('/usr/local/bin/restartwireless');\r
152}\r
153\r
154if ($cgiparams{'ACTION'} eq 'remove' || $cgiparams{'ACTION'} eq 'toggle')\r
155{\r
156 my $id = 0;\r
157 open(FILE, ">$filename") or die 'Unable to open config file.';\r
158 flock FILE, 2;\r
159 foreach my $line (@current)\r
160 {\r
161 $id++;\r
162 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }\r
163 elsif ($cgiparams{'ACTION'} eq 'toggle')\r
164 {\r
165 chomp($line);\r
166 my @temp = split(/\,/,$line);\r
167 print FILE "$temp[0],$temp[1],$temp[2],$cgiparams{'ENABLE'},$temp[4]\n";\r
168 }\r
169 }\r
170 close(FILE);\r
171 &General::log($Lang::tr{'wireless config changed'});\r
172 system('/usr/local/bin/restartwireless');\r
173}\r
174\r
175\r
176$checked{'ENABLED'}{'off'} = '';\r
177$checked{'ENABLED'}{'on'} = '';\r
178$checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";\r
179\r
180\r
181&Header::openpage($Lang::tr{'wireless configuration'}, 1, '');\r
182\r
183&Header::openbigbox('100%', 'left', '', $errormessage);\r
184\r
185if ($errormessage) {\r
186 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});\r
187 print "<class name='base'>$errormessage\n";\r
188 print "&nbsp;</class>\n";\r
189 &Header::closebox();\r
190}\r
191\r
192print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";\r
193\r
194my $buttontext = $Lang::tr{'add'};\r
195if ($cgiparams{'ACTION'} eq 'edit') {\r
196 &Header::openbox('100%', 'left', "$Lang::tr{'edit device'}");\r
197 $buttontext = $Lang::tr{'update'};\r
198} else {\r
199 &Header::openbox('100%', 'left', "$Lang::tr{'add device'}");\r
200}\r
201\r
202print <<END\r
203<table width='100%'>\r
204<tr>\r
205<td width='25%' class='base'>$Lang::tr{'source ip'}:&nbsp;</td>\r
206<td width='25%' ><input type='text' name='SOURCE_IP' value='$cgiparams{'SOURCE_IP'}' size='25' /></td>\r
207<td width='25%' class='base' align='right'>$Lang::tr{'enabled'}&nbsp;</td>\r
208<td width='25%'><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>\r
209</tr>\r
210<tr>\r
211<td width='25%' class='base'>$Lang::tr{'source'} $Lang::tr{'mac address'}:&nbsp;</td>\r
212<td colspan='3'><input type='text' name='SOURCE_MAC' value='$cgiparams{'SOURCE_MAC'}' size='25' /></td>\r
213</tr>\r
214<tr>\r
215<td width='25%' class='base'>$Lang::tr{'remark'}:&nbsp;<img src='/blob.gif' alt='*' /></td>\r
216<td colspan='3'><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' /></td>\r
217</tr>\r
218</table>\r
219<hr />\r
220<table width='100%'>\r
221<tr>\r
222 <td class='base' valign='top'><img src='/blob.gif' alt='*' /></td>\r
223 <td width='55%' class='base'>$Lang::tr{'this field may be blank'}</td>\r
224 <td width='40%' align='center'>\r
225 <input type='hidden' name='ACTION' value='add' />\r
226 <input type='submit' name='SUBMIT' value='$buttontext' />\r
227 </td>\r
228 <td width='5%' align='right'>\r
229 <a href='${General::adminmanualurl}/section-firewall.html#section-blue-access' target='_blank'>\r
230 <img src='/images/web-support.png' alt='$Lang::tr{'online help en'}' title='$Lang::tr{'online help en'}' /></a></td>\r
231</tr>\r
232</table>\r
233END\r
234;\r
235\r
236if ($cgiparams{'ACTION'} eq 'edit') {\r
237 print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";\r
238} else {\r
239 print "<input type='hidden' name='EDITING' value='no' />\n";\r
240}\r
241\r
242&Header::closebox();\r
243\r
244print "</form>\n";\r
245\r
246&Header::openbox('100%', 'left', "$Lang::tr{'devices on blue'}");\r
247print <<END\r
248<div align='center'>\r
249END\r
250;\r
251open (FILE, "$filename");\r
252my @current = <FILE>;\r
253close (FILE);\r
254\r
255print <<END\r
256<table width='100%'>\r
257<tr>\r
258<td align='center' width='20%'><b>$Lang::tr{'hostname'}</b></td>\r
259<td align='center' width='20%'><b>$Lang::tr{'source ip'}</b></td>\r
260<td align='center' width='20%'><b>$Lang::tr{'mac address'}</b></td>\r
261<td align='center' width='35%'><b>$Lang::tr{'remark'}</b></td>\r
262<td align='center' colspan='3'><b>$Lang::tr{'action'}</b></td>\r
263</tr>\r
264END\r
265;\r
266\r
267my $id = 0;\r
268\r
269open (HOSTFILE, "$hostsfile");\r
270my @curhosts = <HOSTFILE>;\r
271close (HOSTFILE);\r
272\r
273my $connstate = &Header::connectionstatus();\r
274my @arp = `/sbin/arp -n`;\r
275shift @arp;\r
276\r
277foreach my $line (@current)\r
278{\r
279 $id++;\r
280 chomp($line);\r
281 my $gif = "";\r
282 my $gdesc = "";\r
283 my $hname = "";\r
284 my $toggle = "";\r
285 my @temp = split(/\,/,$line);\r
286 my $wirelessid = $temp[0];\r
287 my $sourceip = $temp[1];\r
288 my $sourcemac = $temp[2];\r
289 if ( $sourceip eq 'NONE' ) {\r
290 foreach my $aline ( @arp )\r
291 {\r
292 chomp($aline);\r
293 my @atemp = split( m{\s+}, $aline );\r
294 my $aipaddr = $atemp[0];\r
295 my $amacaddr = lc( $atemp[2] );\r
296 if ( $amacaddr eq $sourcemac ) {\r
297 $sourceip = $aipaddr;\r
298 last;\r
299 }\r
300 }\r
301 }\r
302\r
303 # SourceIP could now have been set by the ARP probe.\r
304 if ( $sourceip ne 'NONE' ) {\r
305 foreach my $hline (@curhosts)\r
306 {\r
307 chomp($hline);\r
308 my @htemp = split(/\,/,$hline);\r
309 my $hkey = $htemp[0];\r
310 my $hipaddr = $htemp[1];\r
311 my $hostname = $htemp[2];\r
312 my $domainname = $htemp[3];\r
313 if ($sourceip eq $hipaddr) {\r
314 $hname = "$hostname.$domainname";\r
315 last;\r
316 }\r
317 }\r
318 if ( $hname eq "" ) {\r
319 my ($aliases, $addrtype, $length, @addrs);\r
320 ($hname, $aliases, $addrtype, $length, @addrs) = \r
321 gethostbyaddr(pack("C4", split(/\./, $sourceip)), 2);\r
322 }\r
323 }\r
324\r
325 if ($temp[3] eq 'on') { $gif = 'on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};}\r
326 else { $gif = 'off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'};}\r
327\r
328 my $remark = &Header::cleanhtml($temp[4]);\r
329\r
330 if ($cgiparams{'ACTION'} eq 'edit' && $cgiparams{'ID'} eq $id) {\r
331 print "<tr bgcolor='${Header::colouryellow}'>\n";\r
332 } elsif ($id % 2) {\r
333 print "<tr bgcolor='${Header::table1colour}'>\n";\r
334 } else {\r
335 print "<tr bgcolor='${Header::table2colour}'>\n";\r
336 }\r
337 print "<td align='center'>$hname</td>\n";\r
338 print "<td align='center'>$sourceip</td>\n";\r
339 print "<td align='center'>$sourcemac</td>\n";\r
340 print "<td align='center'>$remark</td>\n";\r
341print<<END\r
342<td align='center'>\r
343 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>\r
344 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />\r
345 <input type='hidden' name='ACTION' value='toggle'}' />\r
346 <input type='hidden' name='ID' value='$id' />\r
347 <input type='hidden' name='ENABLE' value='$toggle' />\r
348 </form>\r
349</td>\r
350\r
351<td align='center'>\r
352 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>\r
353 <input type='hidden' name='ACTION' value='edit' />\r
354 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />\r
355 <input type='hidden' name='ID' value='$id' />\r
356 </form>\r
357</td>\r
358\r
359<td align='center'>\r
360 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>\r
361 <input type='hidden' name='ACTION' value='remove' />\r
362 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />\r
363 <input type='hidden' name='ID' value='$id' />\r
364 </form>\r
365</td>\r
366END\r
367 ;\r
368 print "</tr>\n";\r
369}\r
370print "</table>\n";\r
371\r
372print "</div>\n";\r
373\r
374&Header::closebox();\r
375\r
376if ( $dhcpsettings{"ENABLE_BLUE"} eq 'on') {\r
377 &printblueleases;\r
378}\r
379\r
380&Header::closebigbox();\r
381\r
382&Header::closepage();\r
383\r
384sub printblueleases\r
385{\r
386 our %entries = ();\r
387\r
388 sub blueleasesort {\r
389 # Sort by IP address\r
390 my $qs ='IPADDR';\r
391 my @a = split(/\./,$entries{$a}->{$qs});\r
392 my @b = split(/\./,$entries{$b}->{$qs});\r
393 ($a[0]<=>$b[0]) ||\r
394 ($a[1]<=>$b[1]) ||\r
395 ($a[2]<=>$b[2]) ||\r
396 ($a[3]<=>$b[3]);\r
397 }\r
398\r
399 &Header::openbox('100%', 'left', "$Lang::tr{'current dhcp leases on blue'}");\r
400 print <<END\r
401<table width='100%'>\r
402<tr>\r
403<td width='25%' align='center'><b>$Lang::tr{'ip address'}</b></td>\r
404<td width='25%' align='center'><b>$Lang::tr{'mac address'}</b></td>\r
405<td width='20%' align='center'><b>$Lang::tr{'hostname'}</b></td>\r
406<td width='30%' align='center'><b>$Lang::tr{'lease expires'} (local time d/m/y)</b></td>\r
407</tr>\r
408END\r
409 ;\r
410\r
411 my ($ip, $endtime, $ether, $hostname, @record, $record);\r
412 open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";\r
413 while (my $line = <LEASES>) {\r
414 next if( $line =~ /^\s*#/ );\r
415 chomp($line);\r
416 my @temp = split (' ', $line);\r
417\r
418 if ($line =~ /^\s*lease/) {\r
419 $ip = $temp[1];\r
420 # All fields are not necessarily read. Clear everything\r
421 $endtime = 0;\r
422 $ether = "";\r
423 $hostname = "";\r
424 } elsif ($line =~ /^\s*ends never;/) {\r
425 $endtime = 'never';\r
426 } elsif ($line =~ /^\s*ends/) {\r
427 $line =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/;\r
428 $endtime = timegm($6, $5, $4, $3, $2 - 1, $1 - 1900);\r
429 } elsif ($line =~ /^\s*hardware ethernet/) {\r
430 $ether = $temp[2];\r
431 $ether =~ s/;//g;\r
432 } elsif ($line =~ /^\s*client-hostname/) {\r
433 shift (@temp);\r
434 $hostname = join (' ',@temp);\r
435 $hostname =~ s/;//g;\r
436 $hostname =~ s/\"//g;\r
437 } elsif ($line eq "}") {\r
438 # Select records in Blue subnet\r
439 if ( &General::IpInSubnet ( $ip,\r
440 $netsettings{"BLUE_NETADDRESS"},\r
441 $netsettings{"BLUE_NETMASK"} ) ) {\r
442 @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname);\r
443 $record = {}; # create a reference to empty hash\r
444 %{$record} = @record; # populate that hash with @record\r
445 $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes\r
446 }\r
447 }\r
448 }\r
449 close(LEASES);\r
450\r
451 my $id = 0;\r
452 foreach my $key (sort blueleasesort keys %entries) {\r
453\r
454 my $hostname = &Header::cleanhtml($entries{$key}->{HOSTNAME},"y");\r
455\r
456 if ($id % 2) {\r
457 print "<tr bgcolor='$Header::table2colour'>";\r
458 } else {\r
459 print "<tr bgcolor='$Header::table1colour'>";\r
460 }\r
461\r
462 print <<END\r
463<td align='center'>$entries{$key}->{IPADDR}</td>\r
464<td align='center'>$entries{$key}->{ETHER}</td>\r
465<td align='center'>&nbsp;$hostname </td>\r
466<td align='center'>\r
467END\r
468 ;\r
469\r
470 if ($entries{$key}->{ENDTIME} eq 'never') {\r
471 print "$Lang::tr{'no time limit'}";\r
472 } else {\r
473 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst);\r
474 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME});\r
475 my $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec);\r
476\r
477 if ($entries{$key}->{ENDTIME} < time() ){\r
478 print "<strike>$enddate</strike>";\r
479 } else {\r
480 print "$enddate";\r
481 }\r
482 }\r
483\r
484 if ( $hostname eq '' ) {\r
485 $hostname = $Lang::tr{'device'};\r
486 }\r
487\r
488 print <<END\r
489<td align='center'>\r
490 <form method='post' name='frmd$id' action='$ENV{'SCRIPT_NAME'}'>\r
491 <input type='hidden' name='ACTION' value='add' />\r
492 <input type='hidden' name='SOURCE_IP' value='' />\r
493 <input type='hidden' name='SOURCE_MAC' value='$entries{$key}->{ETHER}' />\r
494 <input type='hidden' name='REMARK' value='$hostname $Lang::tr{'added from dhcp lease list'}' />\r
495 <input type='hidden' name='ENABLED' value='on' />\r
496 <input type='hidden' name='EDITING' value='no' />\r
497 <input type='image' name='$Lang::tr{'add device'}' src='/images/addblue.gif' alt='$Lang::tr{'add device'}' title='$Lang::tr{'add device'}' />\r
498 </form>\r
499</td></tr>\r
500END\r
501 ;\r
502 $id++;\r
503 }\r
504\r
505 print "</table>";\r
506 &Header::closebox();\r
507}\r
508\r