]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/wireless.cgi
Merge remote-tracking branch 'dweismueller/vdr_vnsiserver5_new' into next
[ipfire-2.x.git] / html / cgi-bin / wireless.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
ac1cfefa
MT
21
22use strict;
23use Time::Local;
24
25# enable only the following on debugging purpose
26#use warnings;
27#use CGI::Carp 'fatalsToBrowser';
28
986e08d9 29require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
30require "${General::swroot}/lang.pl";
31require "${General::swroot}/header.pl";
32
33#workaround to suppress a warning when a variable is used only once
34my @dummy = ( ${Header::colouryellow} );
35undef (@dummy);
36
37my %cgiparams=();
38my %checked=();
39my $errormessage = '';
40my $filename = "${General::swroot}/wireless/config";
41my $hostsfile = "${General::swroot}/main/hosts";
42our %dhcpsettings=();
43our %netsettings=();
44
45$cgiparams{'ENABLED'} = 'off';
46$cgiparams{'ACTION'} = '';
47$cgiparams{'VALID'} = '';
48$cgiparams{'SOURCE_IP'} ='';
49$cgiparams{'SOURCE_MAC'} ='';
50$cgiparams{'REMARK'} ='';
51
52&Header::getcgihash(\%cgiparams);
53
54&General::readhash("${General::swroot}/dhcp/settings", \%dhcpsettings);
55&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
56
57&Header::showhttpheaders();
58
59open(FILE, $filename) or die 'Unable to open config file.';
60my @current = <FILE>;
61close(FILE);
62
63if ($cgiparams{'ACTION'} eq 'add')
64{
65
66 if ($cgiparams{'SOURCE_IP'} eq '' && $cgiparams{'SOURCE_MAC'} eq '')
67 {
68 goto ADDEXIT;
69 }
70
71 $cgiparams{'SOURCE_MAC'} =~ tr/-/:/;
72
73 my $key = 0;
74 foreach my $line (@current)
75 {
76 $key++;
77 my @temp = split(/\,/,$line);
78
79 if ($temp[1] ne '' && $cgiparams{'SOURCE_IP'} eq $temp[1] && $cgiparams{'EDITING'} ne $key)
80 {
81 $errormessage = $Lang::tr{'duplicate ip'};
82 goto ADDERROR;
83 }
84 if ($temp[2] ne '' && lc($cgiparams{'SOURCE_MAC'}) eq lc($temp[2]) && $cgiparams{'EDITING'} ne $key)
85 {
86 $errormessage = $Lang::tr{'duplicate mac'};
87 goto ADDERROR;
88 }
89 }
90
91 if ($cgiparams{'SOURCE_IP'} eq '')
92 {
93 $cgiparams{'SOURCE_IP'} = 'NONE';
94 } else {
45f26735 95 unless(&General::validipormask($cgiparams{'SOURCE_IP'}))
ac1cfefa
MT
96 {
97 $errormessage = $Lang::tr{'invalid fixed ip address'};
98 goto ADDERROR;
99 }
100 }
101 if ($cgiparams{'SOURCE_MAC'} eq '')
102 {
103 $cgiparams{'SOURCE_MAC'} = 'NONE';
104 } else {
105 unless(&General::validmac($cgiparams{'SOURCE_MAC'}))
106 {
107 $errormessage = $Lang::tr{'invalid fixed mac address'};
108 }
109 }
110
111ADDERROR:
112 if ($errormessage)
113 {
114 $cgiparams{'SOURCE_MAC'} = '' if $cgiparams{'SOURCE_MAC'} eq 'NONE';
115 $cgiparams{'SOURCE_IP'} = '' if $cgiparams{'SOURCE_IP'} eq 'NONE';
116 } else {
117 if ($cgiparams{'EDITING'} eq 'no') {
118 open(FILE,">>$filename") or die 'Unable to open config file.';
119 flock FILE, 2;
120 print FILE "$key,$cgiparams{'SOURCE_IP'},$cgiparams{'SOURCE_MAC'},$cgiparams{'ENABLED'},$cgiparams{'REMARK'}\n";
121 } else {
122 open(FILE,">$filename") or die 'Unable to open config file.';
123 flock FILE, 2;
124 my $id = 0;
125 foreach my $line (@current)
126 {
127 $id++;
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"; }
131 }
132 }
133 close(FILE);
134 undef %cgiparams;
135 &General::log($Lang::tr{'wireless config added'});
900832fa 136 system('/usr/local/bin/wirelessctrl');
ac1cfefa
MT
137 }
138ADDEXIT:
139}
140
141if ($cgiparams{'ACTION'} eq 'edit')
142{
143 my $id = 0;
144 foreach my $line (@current)
145 {
146 $id++;
147 if ($cgiparams{'ID'} eq $id)
148 {
149 chomp($line);
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';
157 }
158 }
159 &General::log($Lang::tr{'wireless config changed'});
900832fa 160 system('/usr/local/bin/wirelessctrl');
ac1cfefa
MT
161}
162
163if ($cgiparams{'ACTION'} eq 'remove' || $cgiparams{'ACTION'} eq 'toggle')
164{
165 my $id = 0;
166 open(FILE, ">$filename") or die 'Unable to open config file.';
167 flock FILE, 2;
168 foreach my $line (@current)
169 {
170 $id++;
171 unless ($cgiparams{'ID'} eq $id) { print FILE "$line"; }
172 elsif ($cgiparams{'ACTION'} eq 'toggle')
173 {
174 chomp($line);
175 my @temp = split(/\,/,$line);
176 print FILE "$temp[0],$temp[1],$temp[2],$cgiparams{'ENABLE'},$temp[4]\n";
177 }
178 }
179 close(FILE);
180 &General::log($Lang::tr{'wireless config changed'});
900832fa 181 system('/usr/local/bin/wirelessctrl');
ac1cfefa
MT
182}
183
184
185$checked{'ENABLED'}{'off'} = '';
186$checked{'ENABLED'}{'on'} = '';
187$checked{'ENABLED'}{$cgiparams{'ENABLED'}} = "checked='checked'";
188
189
190&Header::openpage($Lang::tr{'wireless configuration'}, 1, '');
191
192&Header::openbigbox('100%', 'left', '', $errormessage);
193
194if ($errormessage) {
195 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
196 print "<class name='base'>$errormessage\n";
197 print "&nbsp;</class>\n";
198 &Header::closebox();
199}
200
201print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
202
203my $buttontext = $Lang::tr{'add'};
204if ($cgiparams{'ACTION'} eq 'edit') {
205 &Header::openbox('100%', 'left', "$Lang::tr{'edit device'}");
206 $buttontext = $Lang::tr{'update'};
207} else {
208 &Header::openbox('100%', 'left', "$Lang::tr{'add device'}");
209}
210
211print <<END
212<table width='100%'>
213<tr>
214<td width='25%' class='base'>$Lang::tr{'source ip'}:&nbsp;</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'}&nbsp;</td>
217<td width='25%'><input type='checkbox' name='ENABLED' $checked{'ENABLED'}{'on'} /></td>
218</tr>
219<tr>
220<td width='25%' class='base'>$Lang::tr{'source'} $Lang::tr{'mac address'}:&nbsp;</td>
221<td colspan='3'><input type='text' name='SOURCE_MAC' value='$cgiparams{'SOURCE_MAC'}' size='25' /></td>
222</tr>
223<tr>
224<td width='25%' class='base'>$Lang::tr{'remark'}:&nbsp;<img src='/blob.gif' alt='*' /></td>
225<td colspan='3'><input type='text' name='REMARK' value='$cgiparams{'REMARK'}' size='40' /></td>
226</tr>
227</table>
77a516cd
AM
228<br>
229<hr>
ac1cfefa
MT
230<table width='100%'>
231<tr>
77a516cd
AM
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'>
ac1cfefa
MT
234 <input type='hidden' name='ACTION' value='add' />
235 <input type='submit' name='SUBMIT' value='$buttontext' />
236 </td>
ac1cfefa
MT
237</tr>
238</table>
239END
240;
241
242if ($cgiparams{'ACTION'} eq 'edit') {
243 print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";
244} else {
245 print "<input type='hidden' name='EDITING' value='no' />\n";
246}
247
248&Header::closebox();
249
250print "</form>\n";
251
252&Header::openbox('100%', 'left', "$Lang::tr{'devices on blue'}");
253print <<END
254<div align='center'>
255END
256;
257open (FILE, "$filename");
258my @current = <FILE>;
259close (FILE);
260
261print <<END
633b0df5 262<table width='100%' class='tbl'>
ac1cfefa 263<tr>
633b0df5
AM
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>
ac1cfefa
MT
269</tr>
270END
271;
272
273my $id = 0;
274
275open (HOSTFILE, "$hostsfile");
276my @curhosts = <HOSTFILE>;
277close (HOSTFILE);
278
279my $connstate = &Header::connectionstatus();
280my @arp = `/sbin/arp -n`;
281shift @arp;
282
283foreach my $line (@current)
284{
285 $id++;
286 chomp($line);
287 my $gif = "";
288 my $gdesc = "";
289 my $hname = "";
290 my $toggle = "";
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 )
297 {
298 chomp($aline);
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;
304 last;
305 }
306 }
307 }
308
309 # SourceIP could now have been set by the ARP probe.
310 if ( $sourceip ne 'NONE' ) {
311 foreach my $hline (@curhosts)
312 {
313 chomp($hline);
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";
321 last;
322 }
323 }
324 if ( $hname eq "" ) {
325 my ($aliases, $addrtype, $length, @addrs);
326 ($hname, $aliases, $addrtype, $length, @addrs) =
327 gethostbyaddr(pack("C4", split(/\./, $sourceip)), 2);
328 }
329 }
330
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'};}
333
334 my $remark = &Header::cleanhtml($temp[4]);
633b0df5 335 my $col="";
ac1cfefa
MT
336
337 if ($cgiparams{'ACTION'} eq 'edit' && $cgiparams{'ID'} eq $id) {
633b0df5
AM
338 print "<tr>";
339 $col="bgcolor='${Header::colouryellow}'";
ac1cfefa 340 } elsif ($id % 2) {
633b0df5
AM
341 print "<tr>";
342 $col="bgcolor='${Header::table1colour}'";
ac1cfefa 343 } else {
633b0df5
AM
344 print "<tr>";
345 $col="bgcolor='${Header::table2colour}'";
ac1cfefa 346 }
633b0df5
AM
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";
ac1cfefa 351print<<END
633b0df5 352<td align='center' $col>
ac1cfefa
MT
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' />
358 </form>
359</td>
360
633b0df5 361<td align='center' $col>
ac1cfefa
MT
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' />
366 </form>
367</td>
368
633b0df5 369<td align='center' $col>
ac1cfefa
MT
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' />
374 </form>
375</td>
376END
377 ;
378 print "</tr>\n";
379}
380print "</table>\n";
381
382print "</div>\n";
383
384&Header::closebox();
385
386if ( $dhcpsettings{"ENABLE_BLUE"} eq 'on') {
387 &printblueleases;
388}
389
390&Header::closebigbox();
391
392&Header::closepage();
393
394sub printblueleases
395{
396 our %entries = ();
397
398 sub blueleasesort {
399 # Sort by IP address
400 my $qs ='IPADDR';
401 my @a = split(/\./,$entries{$a}->{$qs});
402 my @b = split(/\./,$entries{$b}->{$qs});
403 ($a[0]<=>$b[0]) ||
404 ($a[1]<=>$b[1]) ||
405 ($a[2]<=>$b[2]) ||
406 ($a[3]<=>$b[3]);
407 }
408
409 &Header::openbox('100%', 'left', "$Lang::tr{'current dhcp leases on blue'}");
410 print <<END
633b0df5 411<table width='100%' class='tbl'>
ac1cfefa 412<tr>
633b0df5
AM
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>
417<th></th>
ac1cfefa
MT
418</tr>
419END
420 ;
421
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*#/ );
426 chomp($line);
427 my @temp = split (' ', $line);
428
429 if ($line =~ /^\s*lease/) {
430 $ip = $temp[1];
431 # All fields are not necessarily read. Clear everything
432 $endtime = 0;
433 $ether = "";
434 $hostname = "";
435 } elsif ($line =~ /^\s*ends never;/) {
436 $endtime = '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/) {
441 $ether = $temp[2];
442 $ether =~ s/;//g;
443 } elsif ($line =~ /^\s*client-hostname/) {
444 shift (@temp);
445 $hostname = join (' ',@temp);
446 $hostname =~ s/;//g;
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
457 }
458 }
459 }
460 close(LEASES);
461
462 my $id = 0;
463 foreach my $key (sort blueleasesort keys %entries) {
464
465 my $hostname = &Header::cleanhtml($entries{$key}->{HOSTNAME},"y");
633b0df5 466 my $col="";
ac1cfefa
MT
467
468 if ($id % 2) {
633b0df5
AM
469 print "<tr>";
470 $col="bgcolor='$Header::table2colour'";
ac1cfefa 471 } else {
633b0df5
AM
472 print "<tr>";
473 $col="bgcolor='$Header::table1colour'";
ac1cfefa
MT
474 }
475
476 print <<END
633b0df5
AM
477<td align='center' $col>$entries{$key}->{IPADDR}</td>
478<td align='center' $col>$entries{$key}->{ETHER}</td>
479<td align='center' $col>&nbsp;$hostname </td>
480<td align='center' $col>
ac1cfefa
MT
481END
482 ;
483
484 if ($entries{$key}->{ENDTIME} eq 'never') {
485 print "$Lang::tr{'no time limit'}";
486 } else {
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);
490
491 if ($entries{$key}->{ENDTIME} < time() ){
492 print "<strike>$enddate</strike>";
493 } else {
494 print "$enddate";
495 }
496 }
497
498 if ( $hostname eq '' ) {
499 $hostname = $Lang::tr{'device'};
500 }
501
502 print <<END
633b0df5 503<td align='center' $col>
ac1cfefa
MT
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'}' />
512 </form>
513</td></tr>
514END
515 ;
516 $id++;
517 }
518
519 print "</table>";
520 &Header::closebox();
521}
522