]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/wireless.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
25 # enable only the following on debugging purpose
27 #use CGI::Carp 'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header
::colouryellow
} );
39 my $errormessage = '';
40 my $filename = "${General::swroot}/wireless/config";
41 my $hostsfile = "${General::swroot}/main/hosts";
45 $cgiparams{'ENABLED'} = 'off';
46 $cgiparams{'ACTION'} = '';
47 $cgiparams{'VALID'} = '';
48 $cgiparams{'SOURCE_IP'} ='';
49 $cgiparams{'SOURCE_MAC'} ='';
50 $cgiparams{'REMARK'} ='';
52 &Header
::getcgihash
(\
%cgiparams);
54 &General
::readhash
("${General::swroot}/dhcp/settings", \
%dhcpsettings);
55 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
57 &Header
::showhttpheaders
();
59 open(FILE
, $filename) or die 'Unable to open config file.';
63 if ($cgiparams{'ACTION'} eq 'add')
66 if ($cgiparams{'SOURCE_IP'} eq '' && $cgiparams{'SOURCE_MAC'} eq '')
71 $cgiparams{'SOURCE_MAC'} =~ tr/-/:/;
74 foreach my $line (@current)
77 my @temp = split(/\,/,$line);
79 if ($temp[1] ne '' && $cgiparams{'SOURCE_IP'} eq $temp[1] && $cgiparams{'EDITING'} ne $key)
81 $errormessage = $Lang::tr
{'duplicate ip'};
84 if ($temp[2] ne '' && lc($cgiparams{'SOURCE_MAC'}) eq lc($temp[2]) && $cgiparams{'EDITING'} ne $key)
86 $errormessage = $Lang::tr
{'duplicate mac'};
91 if ($cgiparams{'SOURCE_IP'} eq '')
93 $cgiparams{'SOURCE_IP'} = 'NONE';
95 unless(&General
::validipormask
($cgiparams{'SOURCE_IP'}))
97 $errormessage = $Lang::tr
{'invalid fixed ip address'};
101 if ($cgiparams{'SOURCE_MAC'} eq '')
103 $cgiparams{'SOURCE_MAC'} = 'NONE';
105 unless(&General
::validmac
($cgiparams{'SOURCE_MAC'}))
107 $errormessage = $Lang::tr
{'invalid fixed mac address'};
114 $cgiparams{'SOURCE_MAC'} = '' if $cgiparams{'SOURCE_MAC'} eq 'NONE';
115 $cgiparams{'SOURCE_IP'} = '' if $cgiparams{'SOURCE_IP'} eq 'NONE';
117 if ($cgiparams{'EDITING'} eq 'no') {
118 open(FILE
,">>$filename") or die 'Unable to open config file.';
120 print FILE
"$key,$cgiparams{'SOURCE_IP'},$cgiparams{'SOURCE_MAC'},$cgiparams{'ENABLED'},$cgiparams{'REMARK'}\n";
122 open(FILE
,">$filename") or die 'Unable to open config file.';
125 foreach my $line (@current)
128 if ($cgiparams{'EDITING'} eq $id) {
129 print FILE
"$id,$cgiparams{'SOURCE_IP'},$cgiparams{'SOURCE_MAC'},$cgiparams{'ENABLED'},$cgiparams{'REMARK'}\n";
130 } else { print FILE
"$line"; }
135 &General
::log($Lang::tr
{'wireless config added'});
136 system('/usr/local/bin/wirelessctrl');
141 if ($cgiparams{'ACTION'} eq 'edit')
144 foreach my $line (@current)
147 if ($cgiparams{'ID'} eq $id)
150 my @temp = split(/\,/,$line);
151 $cgiparams{'SOURCE_IP'} = $temp[1];
152 $cgiparams{'SOURCE_MAC'} = $temp[2];
153 $cgiparams{'ENABLED'} = $temp[3];
154 $cgiparams{'REMARK'} = $temp[4];
155 $cgiparams{'SOURCE_IP'} = '' if $cgiparams{'SOURCE_IP'} eq 'NONE';
156 $cgiparams{'SOURCE_MAC'} = '' if $cgiparams{'SOURCE_MAC'} eq 'NONE';
159 &General
::log($Lang::tr
{'wireless config changed'});
160 system('/usr/local/bin/wirelessctrl');
163 if ($cgiparams{'ACTION'} eq 'remove' || $cgiparams{'ACTION'} eq 'toggle')
166 open(FILE
, ">$filename") or die 'Unable to open config file.';
168 foreach my $line (@current)
171 unless ($cgiparams{'ID'} eq $id) { print FILE
"$line"; }
172 elsif ($cgiparams{'ACTION'} eq 'toggle')
175 my @temp = split(/\,/,$line);
176 print FILE
"$temp[0],$temp[1],$temp[2],$cgiparams{'ENABLE'},$temp[4]\n";
180 &General
::log($Lang::tr
{'wireless config changed'});
181 system('/usr/local/bin/wirelessctrl');
185 $checked{'ENABLED'}{'off'} = '';
186 $checked{'ENABLED'}{'on'} = '';
187 $checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
190 &Header
::openpage
($Lang::tr
{'wireless configuration'}, 1, '');
192 &Header
::openbigbox
('100%', 'left', '', $errormessage);
195 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
196 print "<class name='base'>$errormessage\n";
197 print " </class>\n";
201 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
203 my $buttontext = $Lang::tr
{'add'};
204 if ($cgiparams{'ACTION'} eq 'edit') {
205 &Header
::openbox
('100%', 'left', "$Lang::tr{'edit device'}");
206 $buttontext = $Lang::tr
{'update'};
208 &Header
::openbox
('100%', 'left', "$Lang::tr{'add device'}");
214 <td width='25%' class='base'>$Lang::tr{'source ip'}: </td>
215 <td width='25%' ><input type='text' name='SOURCE_IP' value='$cgiparams{'SOURCE_IP'}' size='25' /></td>
216 <td width='25%' class='base' align='right'>$Lang::tr{'enabled'} </td>
217 <td width='25%'><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
220 <td width='25%' class='base'>$Lang::tr{'source'} $Lang::tr{'mac address'}: </td>
221 <td colspan='3'><input type='text' name='SOURCE_MAC' value='$cgiparams{'SOURCE_MAC'}' size='25' /></td>
224 <td width='25%' class='base'>$Lang::tr{'remark'}: <img src='/blob.gif' alt='*' /></td>
225 <td colspan='3'><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' /></td>
232 <td class='base' valign='top'><img src='/blob.gif' alt='*' /> $Lang::tr{'this field may be blank'}</td>
233 <td width='40%' align='right'>
234 <input type='hidden' name='ACTION' value='add' />
235 <input type='submit' name='SUBMIT' value='$buttontext' />
242 if ($cgiparams{'ACTION'} eq 'edit') {
243 print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";
245 print "<input type='hidden' name='EDITING' value='no' />\n";
252 &Header
::openbox
('100%', 'left', "$Lang::tr{'devices on blue'}");
257 open (FILE
, "$filename");
258 my @current = <FILE
>;
262 <table width='100%' class='tbl'>
264 <th align='center' width='20%'><b>$Lang::tr{'hostname'}</b></th>
265 <th align='center' width='20%'><b>$Lang::tr{'source ip'}</b></th>
266 <th align='center' width='20%'><b>$Lang::tr{'mac address'}</b></t>
267 <th align='center' width='35%'><b>$Lang::tr{'remark'}</b></th>
268 <th align='center' colspan='3'><b>$Lang::tr{'action'}</b></th>
275 open (HOSTFILE
, "$hostsfile");
276 my @curhosts = <HOSTFILE
>;
279 my $connstate = &Header
::connectionstatus
();
280 my @arp = `/sbin/arp -n`;
283 foreach my $line (@current)
291 my @temp = split(/\,/,$line);
292 my $wirelessid = $temp[0];
293 my $sourceip = $temp[1];
294 my $sourcemac = $temp[2];
295 if ( $sourceip eq 'NONE' ) {
296 foreach my $aline ( @arp )
299 my @atemp = split( m{\s+}, $aline );
300 my $aipaddr = $atemp[0];
301 my $amacaddr = lc( $atemp[2] );
302 if ( $amacaddr eq $sourcemac ) {
303 $sourceip = $aipaddr;
309 # SourceIP could now have been set by the ARP probe.
310 if ( $sourceip ne 'NONE' ) {
311 foreach my $hline (@curhosts)
314 my @htemp = split(/\,/,$hline);
315 my $hkey = $htemp[0];
316 my $hipaddr = $htemp[1];
317 my $hostname = $htemp[2];
318 my $domainname = $htemp[3];
319 if ($sourceip eq $hipaddr) {
320 $hname = "$hostname.$domainname";
324 if ( $hname eq "" ) {
325 my ($aliases, $addrtype, $length, @addrs);
326 ($hname, $aliases, $addrtype, $length, @addrs) =
327 gethostbyaddr(pack("C4", split(/\./, $sourceip)), 2);
331 if ($temp[3] eq 'on') { $gif = 'on.gif'; $toggle='off'; $gdesc=$Lang::tr
{'click to disable'};}
332 else { $gif = 'off.gif'; $toggle='on'; $gdesc=$Lang::tr
{'click to enable'};}
334 my $remark = &Header
::cleanhtml
($temp[4]);
337 if ($cgiparams{'ACTION'} eq 'edit' && $cgiparams{'ID'} eq $id) {
339 $col="bgcolor='${Header::colouryellow}'";
342 $col="bgcolor='${Header::table1colour}'";
345 $col="bgcolor='${Header::table2colour}'";
347 print "<td align='center' $col>$hname</td>\n";
348 print "<td align='center' $col>$sourceip</td>\n";
349 print "<td align='center' $col>$sourcemac</td>\n";
350 print "<td align='center' $col>$remark</td>\n";
352 <td align='center' $col>
353 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
354 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
355 <input type='hidden' name='ACTION' value='toggle'}' />
356 <input type='hidden' name='ID' value='$id' />
357 <input type='hidden' name='ENABLE' value='$toggle' />
361 <td align='center' $col>
362 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
363 <input type='hidden' name='ACTION' value='edit' />
364 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
365 <input type='hidden' name='ID' value='$id' />
369 <td align='center' $col>
370 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
371 <input type='hidden' name='ACTION' value='remove' />
372 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
373 <input type='hidden' name='ID' value='$id' />
386 if ( $dhcpsettings{"ENABLE_BLUE"} eq 'on') {
390 &Header
::closebigbox
();
392 &Header
::closepage
();
401 my @a = split(/\./,$entries{$a}->{$qs});
402 my @b = split(/\./,$entries{$b}->{$qs});
409 &Header
::openbox
('100%', 'left', "$Lang::tr{'current dhcp leases on blue'}");
411 <table width='100%' class='tbl'>
413 <th width='25%' align='center'><b>$Lang::tr{'ip address'}</b></th>
414 <th width='25%' align='center'><b>$Lang::tr{'mac address'}</b></th>
415 <th width='20%' align='center'><b>$Lang::tr{'hostname'}</b></th>
416 <th width='30%' align='center'><b>$Lang::tr{'lease expires'} (local time d/m/y)</b></th>
422 my ($ip, $endtime, $ether, $hostname, @record, $record);
423 open(LEASES
,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";
424 while (my $line = <LEASES
>) {
425 next if( $line =~ /^\s*#/ );
427 my @temp = split (' ', $line);
429 if ($line =~ /^\s*lease/) {
431 # All fields are not necessarily read. Clear everything
435 } elsif ($line =~ /^\s*ends never;/) {
437 } elsif ($line =~ /^\s*ends/) {
438 $line =~ /(\d+)\/(\d
+)\
/(\d+) (\d+):(\d+):(\d+)/;
439 $endtime = timegm
($6, $5, $4, $3, $2 - 1, $1 - 1900);
440 } elsif ($line =~ /^\s*hardware ethernet/) {
443 } elsif ($line =~ /^\s*client-hostname/) {
445 $hostname = join (' ',@temp);
447 $hostname =~ s/\"//g;
448 } elsif ($line eq "}") {
449 # Select records in Blue subnet
450 if ( &General
::IpInSubnet
( $ip,
451 $netsettings{"BLUE_NETADDRESS"},
452 $netsettings{"BLUE_NETMASK"} ) ) {
453 @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname);
454 $record = {}; # create a reference to empty hash
455 %{$record} = @record; # populate that hash with @record
456 $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes
463 foreach my $key (sort blueleasesort
keys %entries) {
465 my $hostname = &Header
::cleanhtml
($entries{$key}->{HOSTNAME
},"y");
470 $col="bgcolor='$Header::table2colour'";
473 $col="bgcolor='$Header::table1colour'";
477 <td align='center' $col>$entries{$key}->{IPADDR}</td>
478 <td align='center' $col>$entries{$key}->{ETHER}</td>
479 <td align='center' $col> $hostname </td>
480 <td align='center' $col>
484 if ($entries{$key}->{ENDTIME
} eq 'never') {
485 print "$Lang::tr{'no time limit'}";
487 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst);
488 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME
});
489 my $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec);
491 if ($entries{$key}->{ENDTIME
} < time() ){
492 print "<strike>$enddate</strike>";
498 if ( $hostname eq '' ) {
499 $hostname = $Lang::tr
{'device'};
503 <td align='center' $col>
504 <form method='post' name='frmd$id' action='$ENV{'SCRIPT_NAME'}'>
505 <input type='hidden' name='ACTION' value='add' />
506 <input type='hidden' name='SOURCE_IP' value='' />
507 <input type='hidden' name='SOURCE_MAC' value='$entries{$key}->{ETHER}' />
508 <input type='hidden' name='REMARK' value='$hostname $Lang::tr{'added from dhcp lease list'}' />
509 <input type='hidden' name='ENABLED' value='on' />
510 <input type='hidden' name='EDITING' value='no' />
511 <input type='image' name='$Lang::tr{'add device'}' src='/images/addblue.gif' alt='$Lang::tr{'add device'}' title='$Lang::tr{'add device'}' />