Allow also ip/netmask for blue access.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / wireless.cgi
1 #!/usr/bin/perl
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 ###############################################################################
21
22 use strict;
23 use Time::Local;
24
25 # enable only the following on debugging purpose
26 #use warnings;
27 #use CGI::Carp 'fatalsToBrowser';
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header::colouryellow} );
35 undef (@dummy);
36
37 my %cgiparams=();
38 my %checked=();
39 my $errormessage = '';
40 my $filename = "${General::swroot}/wireless/config";
41 my $hostsfile = "${General::swroot}/main/hosts";
42 our %dhcpsettings=();
43 our %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
59 open(FILE, $filename) or die 'Unable to open config file.';
60 my @current = <FILE>;
61 close(FILE);
62
63 if ($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 {
95 unless(&General::validipormask($cgiparams{'SOURCE_IP'}))
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
111 ADDERROR:
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'});
136 system('/usr/local/bin/wirelessctrl');
137 }
138 ADDEXIT:
139 }
140
141 if ($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'});
160 system('/usr/local/bin/wirelessctrl');
161 }
162
163 if ($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'});
181 system('/usr/local/bin/wirelessctrl');
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
194 if ($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
201 print "<form method='post' action='$ENV{'SCRIPT_NAME'}'>\n";
202
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'};
207 } else {
208 &Header::openbox('100%', 'left', "$Lang::tr{'add device'}");
209 }
210
211 print <<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>
228 <hr />
229 <table width='100%'>
230 <tr>
231 <td class='base' valign='top'><img src='/blob.gif' alt='*' /></td>
232 <td width='55%' class='base'>$Lang::tr{'this field may be blank'}</td>
233 <td width='40%' align='center'>
234 <input type='hidden' name='ACTION' value='add' />
235 <input type='submit' name='SUBMIT' value='$buttontext' />
236 </td>
237 <td width='5%' align='right'>&nbsp;</td>
238 </tr>
239 </table>
240 END
241 ;
242
243 if ($cgiparams{'ACTION'} eq 'edit') {
244 print "<input type='hidden' name='EDITING' value='$cgiparams{'ID'}' />\n";
245 } else {
246 print "<input type='hidden' name='EDITING' value='no' />\n";
247 }
248
249 &Header::closebox();
250
251 print "</form>\n";
252
253 &Header::openbox('100%', 'left', "$Lang::tr{'devices on blue'}");
254 print <<END
255 <div align='center'>
256 END
257 ;
258 open (FILE, "$filename");
259 my @current = <FILE>;
260 close (FILE);
261
262 print <<END
263 <table width='100%'>
264 <tr>
265 <td align='center' width='20%'><b>$Lang::tr{'hostname'}</b></td>
266 <td align='center' width='20%'><b>$Lang::tr{'source ip'}</b></td>
267 <td align='center' width='20%'><b>$Lang::tr{'mac address'}</b></td>
268 <td align='center' width='35%'><b>$Lang::tr{'remark'}</b></td>
269 <td align='center' colspan='3'><b>$Lang::tr{'action'}</b></td>
270 </tr>
271 END
272 ;
273
274 my $id = 0;
275
276 open (HOSTFILE, "$hostsfile");
277 my @curhosts = <HOSTFILE>;
278 close (HOSTFILE);
279
280 my $connstate = &Header::connectionstatus();
281 my @arp = `/sbin/arp -n`;
282 shift @arp;
283
284 foreach my $line (@current)
285 {
286 $id++;
287 chomp($line);
288 my $gif = "";
289 my $gdesc = "";
290 my $hname = "";
291 my $toggle = "";
292 my @temp = split(/\,/,$line);
293 my $wirelessid = $temp[0];
294 my $sourceip = $temp[1];
295 my $sourcemac = $temp[2];
296 if ( $sourceip eq 'NONE' ) {
297 foreach my $aline ( @arp )
298 {
299 chomp($aline);
300 my @atemp = split( m{\s+}, $aline );
301 my $aipaddr = $atemp[0];
302 my $amacaddr = lc( $atemp[2] );
303 if ( $amacaddr eq $sourcemac ) {
304 $sourceip = $aipaddr;
305 last;
306 }
307 }
308 }
309
310 # SourceIP could now have been set by the ARP probe.
311 if ( $sourceip ne 'NONE' ) {
312 foreach my $hline (@curhosts)
313 {
314 chomp($hline);
315 my @htemp = split(/\,/,$hline);
316 my $hkey = $htemp[0];
317 my $hipaddr = $htemp[1];
318 my $hostname = $htemp[2];
319 my $domainname = $htemp[3];
320 if ($sourceip eq $hipaddr) {
321 $hname = "$hostname.$domainname";
322 last;
323 }
324 }
325 if ( $hname eq "" ) {
326 my ($aliases, $addrtype, $length, @addrs);
327 ($hname, $aliases, $addrtype, $length, @addrs) =
328 gethostbyaddr(pack("C4", split(/\./, $sourceip)), 2);
329 }
330 }
331
332 if ($temp[3] eq 'on') { $gif = 'on.gif'; $toggle='off'; $gdesc=$Lang::tr{'click to disable'};}
333 else { $gif = 'off.gif'; $toggle='on'; $gdesc=$Lang::tr{'click to enable'};}
334
335 my $remark = &Header::cleanhtml($temp[4]);
336
337 if ($cgiparams{'ACTION'} eq 'edit' && $cgiparams{'ID'} eq $id) {
338 print "<tr bgcolor='${Header::colouryellow}'>\n";
339 } elsif ($id % 2) {
340 print "<tr bgcolor='${Header::table1colour}'>\n";
341 } else {
342 print "<tr bgcolor='${Header::table2colour}'>\n";
343 }
344 print "<td align='center'>$hname</td>\n";
345 print "<td align='center'>$sourceip</td>\n";
346 print "<td align='center'>$sourcemac</td>\n";
347 print "<td align='center'>$remark</td>\n";
348 print<<END
349 <td align='center'>
350 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
351 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
352 <input type='hidden' name='ACTION' value='toggle'}' />
353 <input type='hidden' name='ID' value='$id' />
354 <input type='hidden' name='ENABLE' value='$toggle' />
355 </form>
356 </td>
357
358 <td align='center'>
359 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
360 <input type='hidden' name='ACTION' value='edit' />
361 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
362 <input type='hidden' name='ID' value='$id' />
363 </form>
364 </td>
365
366 <td align='center'>
367 <form method='post' name='frmc$id' action='$ENV{'SCRIPT_NAME'}'>
368 <input type='hidden' name='ACTION' value='remove' />
369 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
370 <input type='hidden' name='ID' value='$id' />
371 </form>
372 </td>
373 END
374 ;
375 print "</tr>\n";
376 }
377 print "</table>\n";
378
379 print "</div>\n";
380
381 &Header::closebox();
382
383 if ( $dhcpsettings{"ENABLE_BLUE"} eq 'on') {
384 &printblueleases;
385 }
386
387 &Header::closebigbox();
388
389 &Header::closepage();
390
391 sub printblueleases
392 {
393 our %entries = ();
394
395 sub blueleasesort {
396 # Sort by IP address
397 my $qs ='IPADDR';
398 my @a = split(/\./,$entries{$a}->{$qs});
399 my @b = split(/\./,$entries{$b}->{$qs});
400 ($a[0]<=>$b[0]) ||
401 ($a[1]<=>$b[1]) ||
402 ($a[2]<=>$b[2]) ||
403 ($a[3]<=>$b[3]);
404 }
405
406 &Header::openbox('100%', 'left', "$Lang::tr{'current dhcp leases on blue'}");
407 print <<END
408 <table width='100%'>
409 <tr>
410 <td width='25%' align='center'><b>$Lang::tr{'ip address'}</b></td>
411 <td width='25%' align='center'><b>$Lang::tr{'mac address'}</b></td>
412 <td width='20%' align='center'><b>$Lang::tr{'hostname'}</b></td>
413 <td width='30%' align='center'><b>$Lang::tr{'lease expires'} (local time d/m/y)</b></td>
414 </tr>
415 END
416 ;
417
418 my ($ip, $endtime, $ether, $hostname, @record, $record);
419 open(LEASES,"/var/state/dhcp/dhcpd.leases") or die "Can't open dhcpd.leases";
420 while (my $line = <LEASES>) {
421 next if( $line =~ /^\s*#/ );
422 chomp($line);
423 my @temp = split (' ', $line);
424
425 if ($line =~ /^\s*lease/) {
426 $ip = $temp[1];
427 # All fields are not necessarily read. Clear everything
428 $endtime = 0;
429 $ether = "";
430 $hostname = "";
431 } elsif ($line =~ /^\s*ends never;/) {
432 $endtime = 'never';
433 } elsif ($line =~ /^\s*ends/) {
434 $line =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/;
435 $endtime = timegm($6, $5, $4, $3, $2 - 1, $1 - 1900);
436 } elsif ($line =~ /^\s*hardware ethernet/) {
437 $ether = $temp[2];
438 $ether =~ s/;//g;
439 } elsif ($line =~ /^\s*client-hostname/) {
440 shift (@temp);
441 $hostname = join (' ',@temp);
442 $hostname =~ s/;//g;
443 $hostname =~ s/\"//g;
444 } elsif ($line eq "}") {
445 # Select records in Blue subnet
446 if ( &General::IpInSubnet ( $ip,
447 $netsettings{"BLUE_NETADDRESS"},
448 $netsettings{"BLUE_NETMASK"} ) ) {
449 @record = ('IPADDR',$ip,'ENDTIME',$endtime,'ETHER',$ether,'HOSTNAME',$hostname);
450 $record = {}; # create a reference to empty hash
451 %{$record} = @record; # populate that hash with @record
452 $entries{$record->{'IPADDR'}} = $record; # add this to a hash of hashes
453 }
454 }
455 }
456 close(LEASES);
457
458 my $id = 0;
459 foreach my $key (sort blueleasesort keys %entries) {
460
461 my $hostname = &Header::cleanhtml($entries{$key}->{HOSTNAME},"y");
462
463 if ($id % 2) {
464 print "<tr bgcolor='$Header::table2colour'>";
465 } else {
466 print "<tr bgcolor='$Header::table1colour'>";
467 }
468
469 print <<END
470 <td align='center'>$entries{$key}->{IPADDR}</td>
471 <td align='center'>$entries{$key}->{ETHER}</td>
472 <td align='center'>&nbsp;$hostname </td>
473 <td align='center'>
474 END
475 ;
476
477 if ($entries{$key}->{ENDTIME} eq 'never') {
478 print "$Lang::tr{'no time limit'}";
479 } else {
480 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst);
481 ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $dst) = localtime ($entries{$key}->{ENDTIME});
482 my $enddate = sprintf ("%02d/%02d/%d %02d:%02d:%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec);
483
484 if ($entries{$key}->{ENDTIME} < time() ){
485 print "<strike>$enddate</strike>";
486 } else {
487 print "$enddate";
488 }
489 }
490
491 if ( $hostname eq '' ) {
492 $hostname = $Lang::tr{'device'};
493 }
494
495 print <<END
496 <td align='center'>
497 <form method='post' name='frmd$id' action='$ENV{'SCRIPT_NAME'}'>
498 <input type='hidden' name='ACTION' value='add' />
499 <input type='hidden' name='SOURCE_IP' value='' />
500 <input type='hidden' name='SOURCE_MAC' value='$entries{$key}->{ETHER}' />
501 <input type='hidden' name='REMARK' value='$hostname $Lang::tr{'added from dhcp lease list'}' />
502 <input type='hidden' name='ENABLED' value='on' />
503 <input type='hidden' name='EDITING' value='no' />
504 <input type='image' name='$Lang::tr{'add device'}' src='/images/addblue.gif' alt='$Lang::tr{'add device'}' title='$Lang::tr{'add device'}' />
505 </form>
506 </td></tr>
507 END
508 ;
509 $id++;
510 }
511
512 print "</table>";
513 &Header::closebox();
514 }
515