]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/captive.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2016 IPFire Team <alexander.marx@ipfire.org> #
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 ###############################################################################
24 # enable only the following on debugging purpose
26 #use CGI::Carp 'fatalsToBrowser';
28 require '/var/ipfire/general-functions.pl' ;
29 require "${General::swroot}/lang.pl" ;
30 require "${General::swroot}/header.pl" ;
31 unless (- e
"${General::swroot}/captive/settings" ) { system ( "touch ${General::swroot}/captive/settings" ); }
39 my $voucherout = "${General::swroot}/captive/voucher_out" ;
40 my $clients = "${General::swroot}/captive/clients" ;
43 my $settingsfile = "${General::swroot}/captive/settings" ;
45 unless (- e
$voucherout ) { system ( "touch $voucherout " ); }
47 & Header
:: getcgihash
( \
%cgiparams );
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 );
54 & Header
:: showhttpheaders
();
57 if ( $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 );
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' };
75 #execute binary to reload firewall rules
76 system ( "/usr/local/bin/captivectrl" );
79 if ( $cgiparams { 'ACTION' } eq " $Lang ::tr{'Captive voucherout'}" ){
80 #generates a voucher and writes it to /var/ipfire/voucher_out
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' };
91 #if no error detected, write to disk
93 my $date = time (); #seconds in utc
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 ] = "" ;}
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 );
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 ]);
113 & General
:: log ( "Captive" , "Generated new voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $expdate remark $rem " );
117 if ( $cgiparams { 'ACTION' } eq 'delvoucherout' ){
118 #deletes an already generated but unused voucher
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 };
133 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
136 if ( $cgiparams { 'ACTION' } eq 'delvoucherinuse' ){
137 #delete voucher and connection in use
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 ]);
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 };
153 & General
:: writehasharray
( " $clients " , \
%clientshash );
154 #reload firewallrules to kill connection of client
155 system ( "/usr/local/bin/captivectrl" );
158 #open webpage, print header and open box
159 & Header
:: openpage
( $Lang :: tr
{ 'Captive menu' }, 1 , '' );
160 & Header
:: openbigbox
();
162 #call error() to see if we have to print an errormessage on website
165 #call config() to display the configuration box
169 #open textfile from /var/ipfire/captive/agb.txt
170 open ( my $handle , "<:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
172 #read line by line and print on screen
173 $cgiparams { 'AGB' }.= HTML
:: Entities
:: decode_entities
( $_ );
179 #prints the config box on the website
180 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive config' });
182 <form method='post' action=' $ENV {'SCRIPT_NAME'}'> \n
183 <table width='100%' border="0">
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'" ;
192 $checked { 'ENABLE_BLUE' }{ 'off' } = '' ;
193 $checked { 'ENABLE_BLUE' }{ 'on' } = '' ;
194 $checked { 'ENABLE_BLUE' }{ $settings { 'ENABLE_BLUE' }} = "checked='checked'" ;
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>" ;
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>" ;
207 $Lang ::tr{'Captive authentication'}
210 <select name='AUTH' style='width:8em;'>
213 print "<option value='LICENSE' " ;
214 print " selected='selected'" if ( $settings { 'AUTH' } eq 'LICENSE' );
215 print "> $Lang ::tr{'Captive auth_lic'}</option>" ;
217 print "<option value='VOUCHER' " ;
218 print " selected='selected'" if ( $settings { 'AUTH' } eq 'VOUCHER' );
219 print "> $Lang ::tr{'Captive auth_vou'}</option>" ;
227 $Lang ::tr{'Captive time'}
230 <select name='TIME' style='width:8em;'>
233 print "<option value='nolimit' " ;
234 print " selected='selected'" if ( $settings { 'TIME' } eq 'nolimit' );
235 print "> $Lang ::tr{'Captive nolimit'}</option>" ;
237 print "<option value='1' " ;
238 print " selected='selected'" if ( $settings { 'TIME' } eq '1' );
241 print "<option value='3' " ;
242 print " selected='selected'" if ( $settings { 'TIME' } eq '3' );
245 print "<option value='8' " ;
246 print " selected='selected'" if ( $settings { 'TIME' } eq '8' );
255 $Lang ::tr{'Captive vouchervalid'}
258 <select name='EXPIRE' style='width:8em;'>
261 print "<option value='86400' " ;
262 print " selected='selected'" if ( $settings { 'EXPIRE' } eq '86400' );
263 print "> $Lang ::tr{'Captive 1day'}</option>" ;
265 print "<option value='604800' " ;
266 print " selected='selected'" if ( $settings { 'EXPIRE' } eq '604800' );
267 print "> $Lang ::tr{'Captive 1week'}</option>" ;
269 print "<option value='2592000' " ;
270 print " selected='selected'" if ( $settings { 'EXPIRE' } eq '2592000' );
271 print "> $Lang ::tr{'Captive 1month'}</option></td></tr>" ;
276 $Lang ::tr{'Captive title'}
279 <input type='text' name='TITLE' value=" $settings {'TITLE'}" size='40'>
284 if ( $settings { 'AUTH' } eq 'LICENSE' ){ & agbbox
();}
290 <input type='submit' name='ACTION' value=" $Lang ::tr{'save'}"/>
300 #if settings is set to use vouchers, the voucher part has to be displayed
301 if ( $settings { 'AUTH' } eq 'VOUCHER' ){
304 #otherwise we show the licensepart
305 & show_license_connections
();
318 <textarea cols="50" rows="10" name="AGB"> $cgiparams {'AGB'}</textarea>
326 #generate a random code only letters from A-Z except 'O' and 0-9
327 my @chars = ( "A" .. "N" , "P" .. "Z" , "0" .. "9" );
329 $randomstring .= $chars [ rand @chars ] for 1 . .8 ;
330 return $randomstring ;
336 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voucher' });
338 <form method='post' action=' $ENV {'SCRIPT_NAME'}'>
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>
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>" ;
354 if (! - z
$voucherout ) { & show_voucher_out
();}
355 if (! - z
$clients ) { & show_voucher_in_use
();}
358 sub show_license_connections
(){
359 #if there are active clients, show the box with active connections
360 return if ( - z
$clients || ! - f
$clients );
363 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
365 <center><table class='tbl'>
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>
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 ;
381 $col = "bgcolor=' $color {'color20'}'" ;
383 $col = "bgcolor=' $color {'color22'}'" ;
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 );
390 print "</center></td><td $col ><center> $mday . $mon . $year " ;
391 printf ( " %02d " , $hourx );
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>" ;
402 sub show_voucher_out
(){
403 #if there are already generated but unsused vouchers, print a table
404 return if ( - z
$voucherout );
407 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive vout' });
409 <center><table class='tbl'>
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>
414 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
415 foreach my $key ( keys %voucherhash )
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 ]);
425 $col = "bgcolor=' $color {'color20'}'" ;
427 $col = "bgcolor=' $color {'color22'}'" ;
430 print "<td $col ><center>" ;
431 printf ( " %02d " , $mday );
437 printf ( " %02d " , $hour );
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 );
443 printf ( " %02d " , $monx );
447 printf ( " %02d " , $hourx );
449 printf ( " %02d " , $minx );
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>" ;
461 sub show_voucher_in_use
(){
462 #if there are active clients which use vouchers show table
463 return if ( - z
$clients || ! - f
$clients );
466 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
468 <center><table class='tbl'>
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>
473 & General
:: readhasharray
( " $clients " , \
%clientshash );
474 foreach my $key ( keys %clientshash )
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 ;
484 $col = "bgcolor=' $color {'color20'}'" ;
486 $col = "bgcolor=' $color {'color22'}'" ;
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 );
493 print "</center></td><td $col ><center> $mday . $mon . $year " ;
494 printf ( " %02d " , $hourx );
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>" ;
506 #if an errormessage exits, show a box with errormessage
508 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'error messages' });
509 print "<class name='base'> $errormessage \n " ;
510 print " </class> \n " ;
515 & Header
:: closebigbox
();
516 & Header
:: closepage
();