]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/captive.cgi
Captive-Portal: add web-part
[ipfire-2.x.git] / html / cgi-bin / captive.cgi
CommitLineData
8b920789
AM
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2016 IPFire Team <alexander.marx@ipfire.org> #
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
22use strict;
23use HTML::Entities();
24# enable only the following on debugging purpose
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
27
28require '/var/ipfire/general-functions.pl';
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
31unless (-e "${General::swroot}/captive/settings") { system("touch ${General::swroot}/captive/settings"); }
32my %settings=();
33my %mainsettings;
34my %color;
35my %cgiparams=();
36my %netsettings=();
37my %checked=();
38my $errormessage='';
39my $voucherout="${General::swroot}/captive/voucher_out";
40my $clients="${General::swroot}/captive/clients";
41my %voucherhash=();
42my %clientshash=();
43my $settingsfile="${General::swroot}/captive/settings";
44
45unless (-e $voucherout) { system("touch $voucherout"); }
46
47&Header::getcgihash(\%cgiparams);
48
49&General::readhash("${General::swroot}/main/settings", \%mainsettings);
50&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
51&General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
52&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
53
54&Header::showhttpheaders();
55
56#actions
57if ($cgiparams{'ACTION'} eq "$Lang::tr{'save'}"){
58 #saves the Captiveportal settings to disk
59 $settings{'ENABLE_GREEN'} = $cgiparams{'ENABLE_GREEN'};
60 $settings{'ENABLE_BLUE'} = $cgiparams{'ENABLE_BLUE'};
61 $settings{'AUTH'} = $cgiparams{'AUTH'};
62 $settings{'TIME'} = $cgiparams{'TIME'};
63 $settings{'EXPIRE'} = $cgiparams{'EXPIRE'};
64 $settings{'TITLE'} = $cgiparams{'TITLE'};
65 &General::writehash("$settingsfile", \%settings);
66
67 #write Licensetext if defined
68 if ($cgiparams{'AGB'}){
69 $cgiparams{'AGB'} = &Header::escape($cgiparams{'AGB'});
70 open( FH, ">:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
71 print FH $cgiparams{'AGB'};
72 close( FH );
73 $cgiparams{'AGB'}="";
74 }
75 #execute binary to reload firewall rules
76 system("/usr/local/bin/captivectrl");
77}
78
79if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive voucherout'}"){
80 #generates a voucher and writes it to /var/ipfire/voucher_out
81
82 #check if we already have a voucher with same code
83 &General::readhasharray("$voucherout", \%voucherhash);
84 foreach my $key (keys %voucherhash) {
85 if($voucherhash{$key}[1] eq $cgiparams{'CODE'}){
86 $errormessage=$Lang::tr{'Captive err doublevoucher'};
87 last;
88 }
89 }
90
91 #if no error detected, write to disk
92 if (!$errormessage){
93 my $date=time(); #seconds in utc
94
95 #first get new key from hash
96 my $key=&General::findhasharraykey (\%voucherhash);
97 #initialize all fields with ''
98 foreach my $i (0 .. 4) { $voucherhash{$key}[$i] = "";}
99 #define fields
100 $voucherhash{$key}[0] = $date;
101 $voucherhash{$key}[1] = $cgiparams{'CODE'};
102 $voucherhash{$key}[2] = $settings{'TIME'};
103 $voucherhash{$key}[3] = $settings{'EXPIRE'};
104 $voucherhash{$key}[4] = &Header::escape($cgiparams{'REMARK'});
105 #write values to disk
106 &General::writehasharray("$voucherout", \%voucherhash);
107
108 #now prepare log entry, get expiring date for voucher and decode remark for logfile
109 my $expdate=localtime(time()+$voucherhash{$key}[3]);
110 my $rem=HTML::Entities::decode_entities($voucherhash{$key}[4]);
111
112 #write logfile entry
113 &General::log("Captive", "Generated new voucher $voucherhash{$key}[1] $voucherhash{$key}[2] hours valid expires on $expdate remark $rem");
114 }
115}
116
117if ($cgiparams{'ACTION'} eq 'delvoucherout'){
118 #deletes an already generated but unused voucher
119
120 #read all generated vouchers
121 &General::readhasharray("$voucherout", \%voucherhash);
122 foreach my $key (keys %voucherhash) {
123 if($cgiparams{'key'} eq $voucherhash{$key}[0]){
124 #write logenty with decoded remark
125 my $rem=HTML::Entities::decode_entities($voucherhash{$key}[4]);
126 &General::log("Captive", "Delete unused voucher $voucherhash{$key}[1] $voucherhash{$key}[2] hours valid expires on $voucherhash{$key}[3] remark $rem");
127 #delete line from hash
128 delete $voucherhash{$key};
129 last;
130 }
131 }
132 #write back hash
133 &General::writehasharray("$voucherout", \%voucherhash);
134}
135
136if ($cgiparams{'ACTION'} eq 'delvoucherinuse'){
137 #delete voucher and connection in use
138
139 #read all active clients
140 &General::readhasharray("$clients", \%clientshash);
141 foreach my $key (keys %clientshash) {
142 if($cgiparams{'key'} eq $clientshash{$key}[0]){
143 #prepare log entry with decoded remark
144 my $rem=HTML::Entities::decode_entities($clientshash{$key}[7]);
145 #write logentry
146 &General::log("Captive", "Delete voucher in use $clientshash{$key}[1] $clientshash{$key}[2] hours valid expires on $clientshash{$key}[3] remark $rem - Connection will be terminated");
147 #delete line from hash
148 delete $clientshash{$key};
149 last;
150 }
151 }
152 #write back hash
153 &General::writehasharray("$clients", \%clientshash);
154 #reload firewallrules to kill connection of client
155 system("/usr/local/bin/captivectrl");
156}
157
158#open webpage, print header and open box
159&Header::openpage($Lang::tr{'Captive menu'}, 1, '');
160&Header::openbigbox();
161
162#call error() to see if we have to print an errormessage on website
163&error();
164
165#call config() to display the configuration box
166&config();
167
168sub getagb(){
169 #open textfile from /var/ipfire/captive/agb.txt
170 open( my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
171 while(<$handle>){
172 #read line by line and print on screen
173 $cgiparams{'AGB'}.= HTML::Entities::decode_entities($_);
174 }
175 close( $handle );
176}
177
178sub config(){
179 #prints the config box on the website
180 &Header::openbox('100%', 'left', $Lang::tr{'Captive config'});
181 print <<END
182 <form method='post' action='$ENV{'SCRIPT_NAME'}'>\n
183 <table width='100%' border="0">
184 <tr>
185END
186;
187 #check which parameters have to be enabled (from settings file)
188 $checked{'ENABLE_GREEN'}{'off'} = '';
189 $checked{'ENABLE_GREEN'}{'on'} = '';
190 $checked{'ENABLE_GREEN'}{$settings{'ENABLE_GREEN'}} = "checked='checked'";
191
192 $checked{'ENABLE_BLUE'}{'off'} = '';
193 $checked{'ENABLE_BLUE'}{'on'} = '';
194 $checked{'ENABLE_BLUE'}{$settings{'ENABLE_BLUE'}} = "checked='checked'";
195
196 if ($netsettings{'GREEN_DEV'}){
197 print "<td width='30%'>$Lang::tr{'Captive active on'} <font color='$Header::colourgreen'>Green</font></td><td><input type='checkbox' name='ENABLE_GREEN' $checked{'ENABLE_GREEN'}{'on'} /></td></tr>";
198 }
199 if ($netsettings{'BLUE_DEV'}){
200 print "<td width='30%'>$Lang::tr{'Captive active on'} <font color='$Header::colourblue'>Blue</font></td><td><input type='checkbox' name='ENABLE_BLUE' $checked{'ENABLE_BLUE'}{'on'} /></td></tr>";
201 }
202
203 print<<END
204 </tr>
205 <tr>
206 <td>
207 $Lang::tr{'Captive authentication'}
208 </td>
209 <td>
210 <select name='AUTH' style='width:8em;'>
211END
212;
213 print "<option value='LICENSE' ";
214 print " selected='selected'" if ($settings{'AUTH'} eq 'LICENSE');
215 print ">$Lang::tr{'Captive auth_lic'}</option>";
216
217 print "<option value='VOUCHER' ";
218 print " selected='selected'" if ($settings{'AUTH'} eq 'VOUCHER');
219 print ">$Lang::tr{'Captive auth_vou'}</option>";
220
221 print<<END
222 </select>
223 </td>
224 </tr>
225 <tr>
226 <td>
227 $Lang::tr{'Captive time'}
228 </td>
229 <td>
230 <select name='TIME' style='width:8em;'>
231END
232;
233 print "<option value='nolimit' ";
234 print " selected='selected'" if ($settings{'TIME'} eq 'nolimit');
235 print ">$Lang::tr{'Captive nolimit'}</option>";
236
237 print "<option value='1' ";
238 print " selected='selected'" if ($settings{'TIME'} eq '1');
239 print ">1</option>";
240
241 print "<option value='3' ";
242 print " selected='selected'" if ($settings{'TIME'} eq '3');
243 print ">3</option>";
244
245 print "<option value='8' ";
246 print " selected='selected'" if ($settings{'TIME'} eq '8');
247 print ">8</option>";
248
249
250print<<END
251 </td>
252 </tr>
253 <tr>
254 <td>
255 $Lang::tr{'Captive vouchervalid'}
256 </td>
257 <td>
258 <select name='EXPIRE' style='width:8em;'>
259END
260;
261 print "<option value='86400' ";
262 print " selected='selected'" if ($settings{'EXPIRE'} eq '86400');
263 print ">$Lang::tr{'Captive 1day'}</option>";
264
265 print "<option value='604800' ";
266 print " selected='selected'" if ($settings{'EXPIRE'} eq '604800');
267 print ">$Lang::tr{'Captive 1week'}</option>";
268
269 print "<option value='2592000' ";
270 print " selected='selected'" if ($settings{'EXPIRE'} eq '2592000');
271 print ">$Lang::tr{'Captive 1month'}</option></td></tr>";
272
273print<<END
274 <tr>
275 <td><br>
276 $Lang::tr{'Captive title'}
277 </td>
278 <td><br>
279 <input type='text' name='TITLE' value="$settings{'TITLE'}" size='40'>
280 </td>
281END
282;
283
284 if($settings{'AUTH'} eq 'LICENSE'){ &agbbox();}
285print<<END
286 <tr>
287 <td>
288 </td>
289 <td align='right'>
290 <input type='submit' name='ACTION' value="$Lang::tr{'save'}"/>
291 </td>
292 </tr>
293 </table>
294 <br><br>
295END
296;
297 print "</form>";
298 &Header::closebox();
299
300 #if settings is set to use vouchers, the voucher part has to be displayed
301 if ($settings{'AUTH'} eq 'VOUCHER'){
302 &voucher();
303 }else{
304 #otherwise we show the licensepart
305 &show_license_connections();
306 }
307}
308
309sub agbbox(){
310 &getagb();
311print<<END
312 <tr>
313 <td>
314 License agreement
315 </td>
316 <td>
317 <br>
318 <textarea cols="50" rows="10" name="AGB">$cgiparams{'AGB'}</textarea>
319 </td>
320 </tr>
321END
322;
323}
324
325sub gencode(){
326 #generate a random code only letters from A-Z except 'O' and 0-9
327 my @chars = ("A".."N", "P".."Z", "0".."9");
328 my $randomstring;
329 $randomstring .= $chars[rand @chars] for 1..8;
330 return $randomstring;
331}
332
333sub voucher(){
334 #show voucher part
335 my $expire;
336 &Header::openbox('100%', 'left', $Lang::tr{'Captive voucher'});
337print<<END
338 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
339 <table class='tbl'>
340 <tr>
341 <th align='center' width='30%'>$Lang::tr{'Captive voucher'}</th><th align='center' width='15%'>$Lang::tr{'hours'}</th><th th align='center' width='15%'>$Lang::tr{'Captive expire'}</th></tr>
342END
343;
344 if ($settings{'EXPIRE'} eq '86400') { $expire = $Lang::tr{'Captive 1day'};}
345 if ($settings{'EXPIRE'} eq '604800') { $expire = $Lang::tr{'Captive 1week'};}
346 if ($settings{'EXPIRE'} eq '2592000') { $expire = $Lang::tr{'Captive 1month'};}
347 if ($settings{'TIME'} eq 'nolimit') { $settings{'TIME'} = $Lang::tr{'Captive nolimit'};}
348 $cgiparams{'CODE'} = &gencode();
349 print "<tr><td><center><b><font size='20'>$cgiparams{'CODE'}</font></b></center></td><td><center><b><font size='5'>$settings{'TIME'}</font></b></center></td><td><center><b><font size='5'>$expire</font></b></center></td></tr>";
350 print "<tr><td colspan='3'><br>$Lang::tr{'remark'}<input type='text' name='REMARK' align='left' size='60' style='font-size: 22px;'></td></tr>";
351 print "</table><br>";
352 print "<center><input type='submit' name='ACTION' value='$Lang::tr{'Captive voucherout'}'><input type='hidden' name='CODE' value='$cgiparams{'CODE'}'</center></form>";
353 &Header::closebox();
354 if (! -z $voucherout) { &show_voucher_out();}
355 if (! -z $clients) { &show_voucher_in_use();}
356}
357
358sub show_license_connections(){
359 #if there are active clients, show the box with active connections
360 return if ( -z $clients || ! -f $clients );
361 my $count=0;
362 my $col;
363 &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'});
364print<<END
365 <center><table class='tbl'>
366 <tr>
367 <th align='center' width='15%'><font size='1'>$Lang::tr{'Captive mac'}</th><th align='center' width='15%'>$Lang::tr{'Captive ip'}</th><th align='center' width='15%'>$Lang::tr{'Captive voucher'}</th><th th align='center' width='15%'>$Lang::tr{'Captive activated'}</th><th th align='center' width='15%'>$Lang::tr{'Captive expire'}</th><th th align='center' width='15%'>$Lang::tr{'delete'}</th></tr>
368END
369;
370 #read all clients from hash and show table
371 &General::readhasharray("$clients", \%clientshash);
372 foreach my $key (keys %clientshash){
373 my ($sec, $min, $hour, $mday, $mon, $year) = localtime($clientshash{$key}[6]);
374 my ($secx,$minx,$hourx) = localtime($clientshash{$key}[6]+($clientshash{$key}[5]*3600));
375 $mon = '0'.++$mon if $mon<10;
376 $min = '0'.$min if $min<10;
377 $hour = '0'.$hour if $hour<10;
378 $year=$year+1900;
379 if ($count % 2){
380 print" <tr>";
381 $col="bgcolor='$color{'color20'}'";
382 }else{
383 $col="bgcolor='$color{'color22'}'";
384 print" <tr>";
385 }
386 print "<td $col><center>$clientshash{$key}[0]</td><td $col><center>$clientshash{$key}[1]</td><td $col><center>$clientshash{$key}[4]</td><td $col><center>$mday.$mon.$year ";
387 printf("%02d",$hour);
388 print ":";
389 printf("%02d",$min);
390 print "</center></td><td $col><center>$mday.$mon.$year ";
391 printf("%02d",$hourx);
392 print ":";
393 printf("%02d",$minx);
394 print "</td><td $col><form method='post'><center><input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /><form method='post'><input type='hidden' name='ACTION' value='delvoucherinuse' /><input type='hidden' name='key' value='$clientshash{$key}[0]' /></form></tr>";
395 $count++;
396 }
397
398 print "</table>";
399 &Header::closebox();
400}
401
402sub show_voucher_out(){
403 #if there are already generated but unsused vouchers, print a table
404 return if ( -z $voucherout);
405 my $count=0;
406 my $col;
407 &Header::openbox('100%', 'left', $Lang::tr{'Captive vout'});
408 print<<END
409 <center><table class='tbl'>
410 <tr>
411 <th align='center' width='15%'><font size='1'>$Lang::tr{'date'}</th><th align='center' width='15%'>$Lang::tr{'Captive voucher'}</th><th align='center' width='5%'>$Lang::tr{'hours'}</th><th th align='center' width='15%'>$Lang::tr{'Captive expire'}</th><th align='center'>$Lang::tr{'remark'}</th><th align='center' width='15%'>$Lang::tr{'delete'}</th></tr>
412END
413;
414 &General::readhasharray("$voucherout", \%voucherhash);
415 foreach my $key (keys %voucherhash)
416 {
417 my ($sec, $min, $hour, $mday, $mon, $year) = localtime($voucherhash{$key}[0]);
418 my ($secx, $minx, $hourx, $mdayx, $monx, $yearx) = localtime($voucherhash{$key}[0]+$voucherhash{$key}[3]);
419 $mon++;
420 $year=$year+1900;
421 $monx++;
422 $yearx=$yearx+1900;
423 if ($count % 2){
424 print" <tr>";
425 $col="bgcolor='$color{'color20'}'";
426 }else{
427 $col="bgcolor='$color{'color22'}'";
428 print" <tr>";
429 }
430 print "<td $col><center>";
431 printf("%02d",$mday);
432 print ".";
433 printf("%02d",$mon);
434 print ".";
435 print"$year ";
436
437 printf("%02d",$hour);
438 print ":";
439 printf("%02d",$min);
440 print "</td><td $col><center><b>$voucherhash{$key}[1]</b></td><td $col><center>$voucherhash{$key}[2]</td><td $col><center>";
441 printf("%02d",$mdayx);
442 print ".";
443 printf("%02d",$monx);
444 print ".";
445 print"$yearx ";
446
447 printf("%02d",$hourx);
448 print ":";
449 printf("%02d",$minx);
450 print "</td>";
451 $voucherhash{$key}[4] = HTML::Entities::decode_entities($voucherhash{$key}[4]);
452 print "<td $col align='center'>$voucherhash{$key}[4]</td>";
453 print "<td $col><form method='post'><center><input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /><form method='post'><input type='hidden' name='ACTION' value='delvoucherout' /><input type='hidden' name='key' value='$voucherhash{$key}[0]' /></form></tr>";
454 $count++;
455 }
456
457 print "</table>";
458 &Header::closebox();
459}
460
461sub show_voucher_in_use(){
462 #if there are active clients which use vouchers show table
463 return if ( -z $clients || ! -f $clients );
464 my $count=0;
465 my $col;
466 &Header::openbox('100%', 'left', $Lang::tr{'Captive voactive'});
467print<<END
468 <center><table class='tbl'>
469 <tr>
470 <th align='center' width='15%'><font size='1'>$Lang::tr{'Captive mac'}</th><th align='center' width='15%'>$Lang::tr{'Captive ip'}</th><th align='center' width='15%'>$Lang::tr{'Captive voucher'}</th><th th align='center' width='15%'>$Lang::tr{'Captive activated'}</th><th th align='center' width='15%'>$Lang::tr{'Captive expire'}</th><th th align='center' width='15%'>$Lang::tr{'delete'}</th></tr>
471END
472;
473 &General::readhasharray("$clients", \%clientshash);
474 foreach my $key (keys %clientshash)
475 {
476 my ($sec, $min, $hour, $mday, $mon, $year) = localtime($clientshash{$key}[6]);
477 my ($secx,$minx,$hourx) = localtime($clientshash{$key}[6]+($clientshash{$key}[5]*3600));
478 $mon = '0'.++$mon if $mon<10;
479 $min = '0'.$min if $min<10;
480 $hour = '0'.$hour if $hour<10;
481 $year=$year+1900;
482 if ($count % 2){
483 print" <tr>";
484 $col="bgcolor='$color{'color20'}'";
485 }else{
486 $col="bgcolor='$color{'color22'}'";
487 print" <tr>";
488 }
489 print "<td $col><center>$clientshash{$key}[0]</td><td $col><center>$clientshash{$key}[1]</td><td $col><center>$clientshash{$key}[4]</td><td $col><center>$mday.$mon.$year ";
490 printf("%02d",$hour);
491 print ":";
492 printf("%02d",$min);
493 print "</center></td><td $col><center>$mday.$mon.$year ";
494 printf("%02d",$hourx);
495 print ":";
496 printf("%02d",$minx);
497 print "</td><td $col><form method='post'><center><input type='image' src='/images/delete.gif' align='middle' alt='$Lang::tr{'delete'}' title='$Lang::tr{'delete'}' /><form method='post'><input type='hidden' name='ACTION' value='delvoucherinuse' /><input type='hidden' name='key' value='$clientshash{$key}[0]' /></form></tr>";
498 $count++;
499 }
500
501 print "</table>";
502 &Header::closebox();
503}
504
505sub error{
506 #if an errormessage exits, show a box with errormessage
507 if ($errormessage) {
508 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
509 print "<class name='base'>$errormessage\n";
510 print "&nbsp;</class>\n";
511 &Header::closebox();
512 }
513}
514
515&Header::closebigbox();
516&Header::closepage();