]>
git.ipfire.org Git - 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 { 'EXPIRE' } = $cgiparams { 'EXP_HOUR' }+ $cgiparams { 'EXP_DAY' }+ $cgiparams { 'EXP_WEEK' }+ $cgiparams { 'EXP_MONTH' };
63 $settings { 'EXP_HOUR' } = $cgiparams { 'EXP_HOUR' };
64 $settings { 'EXP_DAY' } = $cgiparams { 'EXP_DAY' };
65 $settings { 'EXP_WEEK' } = $cgiparams { 'EXP_WEEK' };
66 $settings { 'EXP_MONTH' } = $cgiparams { 'EXP_MONTH' };
67 $settings { 'TITLE' } = $cgiparams { 'TITLE' };
68 & General
:: writehash
( " $settingsfile " , \
%settings );
70 #write Licensetext if defined
71 if ( $cgiparams { 'AGB' }){
72 $cgiparams { 'AGB' } = & Header
:: escape
( $cgiparams { 'AGB' });
73 open ( FH
, ">:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
74 print FH
$cgiparams { 'AGB' };
78 #execute binary to reload firewall rules
79 system ( "/usr/local/bin/captivectrl" );
82 if ( $cgiparams { 'ACTION' } eq " $Lang ::tr{'Captive voucherout'}" ){
83 #generates a voucher and writes it to /var/ipfire/voucher_out
85 #check if we already have a voucher with same code
86 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
87 foreach my $key ( keys %voucherhash ) {
88 if ( $voucherhash { $key }[ 1 ] eq $cgiparams { 'CODE' }){
89 $errormessage = $Lang :: tr
{ 'Captive err doublevoucher' };
95 if ( $cgiparams { 'REMARK' } ne '' && !& validremark
( $cgiparams { 'REMARK' })){
96 $errormessage = $Lang :: tr
{ 'fwhost err remark' };
99 #if no error detected, write to disk
101 my $date = time (); #seconds in utc
103 #first get new key from hash
104 my $key =& General
:: findhasharraykey
( \
%voucherhash );
105 #initialize all fields with ''
106 foreach my $i ( 0 .. 3 ) { $voucherhash { $key }[ $i ] = "" ;}
108 $voucherhash { $key }[ 0 ] = $date ;
109 $voucherhash { $key }[ 1 ] = $cgiparams { 'CODE' };
110 $voucherhash { $key }[ 2 ] = $settings { 'EXPIRE' };
111 $voucherhash { $key }[ 3 ] = $cgiparams { 'REMARK' };
112 #write values to disk
113 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
115 #now prepare log entry, get expiring date for voucher and decode remark for logfile
116 my $expdate = localtime ( time ()+ $voucherhash { $key }[ 3 ]);
117 my $rem = HTML
:: Entities
:: decode_entities
( $voucherhash { $key }[ 4 ]);
120 & General
:: log ( "Captive" , "Generated new voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $expdate remark $rem " );
124 if ( $cgiparams { 'ACTION' } eq 'delvoucherout' ){
125 #deletes an already generated but unused voucher
127 #read all generated vouchers
128 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
129 foreach my $key ( keys %voucherhash ) {
130 if ( $cgiparams { 'key' } eq $voucherhash { $key }[ 0 ]){
131 #write logenty with decoded remark
132 my $rem = HTML
:: Entities
:: decode_entities
( $voucherhash { $key }[ 4 ]);
133 & General
:: log ( "Captive" , "Delete unused voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $voucherhash { $key }[3] remark $rem " );
134 #delete line from hash
135 delete $voucherhash { $key };
140 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
143 if ( $cgiparams { 'ACTION' } eq 'delvoucherinuse' ){
144 #delete voucher and connection in use
146 #read all active clients
147 & General
:: readhasharray
( " $clients " , \
%clientshash );
148 foreach my $key ( keys %clientshash ) {
149 if ( $cgiparams { 'key' } eq $clientshash { $key }[ 0 ]){
150 #prepare log entry with decoded remark
151 my $rem = HTML
:: Entities
:: decode_entities
( $clientshash { $key }[ 7 ]);
153 & 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" );
154 #delete line from hash
155 delete $clientshash { $key };
160 & General
:: writehasharray
( " $clients " , \
%clientshash );
161 #reload firewallrules to kill connection of client
162 system ( "/usr/local/bin/captivectrl" );
165 #open webpage, print header and open box
166 & Header
:: openpage
( $Lang :: tr
{ 'Captive menu' }, 1 , '' );
167 & Header
:: openbigbox
();
169 #call error() to see if we have to print an errormessage on website
172 #call config() to display the configuration box
176 #open textfile from /var/ipfire/captive/agb.txt
177 open ( my $handle , "<:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
179 #read line by line and print on screen
180 $cgiparams { 'AGB' }.= HTML
:: Entities
:: decode_entities
( $_ );
186 #prints the config box on the website
187 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive config' });
189 <form method='post' action=' $ENV {'SCRIPT_NAME'}'> \n
190 <table width='100%' border="0">
194 #check which parameters have to be enabled (from settings file)
195 $checked { 'ENABLE_GREEN' }{ 'off' } = '' ;
196 $checked { 'ENABLE_GREEN' }{ 'on' } = '' ;
197 $checked { 'ENABLE_GREEN' }{ $settings { 'ENABLE_GREEN' }} = "checked='checked'" ;
199 $checked { 'ENABLE_BLUE' }{ 'off' } = '' ;
200 $checked { 'ENABLE_BLUE' }{ 'on' } = '' ;
201 $checked { 'ENABLE_BLUE' }{ $settings { 'ENABLE_BLUE' }} = "checked='checked'" ;
203 if ( $netsettings { 'GREEN_DEV' }){
204 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>" ;
206 if ( $netsettings { 'BLUE_DEV' }){
207 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>" ;
214 $Lang ::tr{'Captive title'}
217 <input type='text' name='TITLE' value=" $settings {'TITLE'}" size='40'>
227 $Lang ::tr{'Captive authentication'}
230 <select name='AUTH' style='width:8em;'>
233 print "<option value='LICENSE' " ;
234 print " selected='selected'" if ( $settings { 'AUTH' } eq 'LICENSE' );
235 print "> $Lang ::tr{'Captive auth_lic'}</option>" ;
237 print "<option value='VOUCHER' " ;
238 print " selected='selected'" if ( $settings { 'AUTH' } eq 'VOUCHER' );
239 print "> $Lang ::tr{'Captive auth_vou'}</option>" ;
248 if ( $settings { 'AUTH' } eq 'LICENSE' ){
252 print "<tr><td> $Lang ::tr{'Captive vouchervalid'}</td><td>" ;
254 print "<br><table border='0' with=100%>" ;
255 print "<th>Stunden</th><th>Tage</th><th>Wochen</th><th>Monate</th>" ;
257 #print hour-dropdownbox
259 print "<tr><td><select name='EXP_HOUR' style='width:8em;'>" ;
260 print "<option value='0' " ;
261 print " selected='selected'" if ( $settings { 'EXP_HOUR' } eq '0' );
262 print ">--</option>" ;
263 for ( my $i = 1 ; $i < 25 ; $i ++){
264 my $exp_sec = $i * $hrs ;
265 print "<option value=' $exp_sec ' " ;
266 print " selected='selected'" if ( $settings { 'EXP_HOUR' } eq $exp_sec );
267 print "> $i </option>" ;
271 #print day-dropdownbox
273 print "<select name='EXP_DAY' style='width:8em;'>" ;
274 print "<option value='0' " ;
275 print " selected='selected'" if ( $settings { 'EXP_DAY' } eq '0' );
276 print ">--</option>" ;
277 for ( my $i = 1 ; $i < 8 ; $i ++){
278 my $exp_sec = $i * $days ;
279 print "<option value=' $exp_sec ' " ;
280 print " selected='selected'" if ( $settings { 'EXP_DAY' } eq $exp_sec );
281 print "> $i </option>" ;
285 #print week-dropdownbox
287 print "<select name='EXP_WEEK' style='width:8em;'>" ;
288 print "<option value='0' " ;
289 print " selected='selected'" if ( $settings { 'EXP_WEEK' } eq '0' );
290 print ">--</option>" ;
291 for ( my $i = 1 ; $i < 5 ; $i ++){
292 my $exp_sec = $i * $week ;
293 print "<option value=' $exp_sec ' " ;
294 print " selected='selected'" if ( $settings { 'EXP_WEEK' } eq $exp_sec );
295 print "> $i </option>" ;
299 #print month-dropdownbox
300 my $month = 3600 * 24 * 30 ;
301 print "<select name='EXP_MONTH' style='width:8em;'>" ;
302 print "<option value='0' " ;
303 print " selected='selected'" if ( $settings { 'EXP_MONTH' } eq '0' );
304 print ">--</option>" ;
305 for ( my $i = 1 ; $i < 13 ; $i ++){
306 my $exp_sec = $i * $month ;
307 print "<option value=' $exp_sec ' " ;
308 print " selected='selected'" if ( $settings { 'EXP_MONTH' } eq $exp_sec );
309 print "> $i </option>" ;
313 print "<td> <input type='checkbox' name='unlimited'></td><td> <b>unlimited</b></td>" ;
315 print "</tr></table>" ;
322 <input type='submit' name='ACTION' value=" $Lang ::tr{'save'}"/>
333 #if settings is set to use vouchers, the voucher part has to be displayed
334 if ( $settings { 'AUTH' } eq 'VOUCHER' ){
337 #otherwise we show the licensepart
338 & show_license_connections
();
351 <textarea cols="50" rows="10" name="AGB"> $cgiparams {'AGB'}</textarea>
359 #generate a random code only letters from A-Z except 'O' and 0-9
360 my @chars = ( "A" .. "N" , "P" .. "Z" , "0" .. "9" );
362 $randomstring .= $chars [ rand @chars ] for 1 . .8 ;
363 return $randomstring ;
368 #calculate expiredate
369 my $expire = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( time ()+ $settings { 'EXPIRE' }));
371 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voucher' });
373 <form method='post' action=' $ENV {'SCRIPT_NAME'}'>
376 <th align='center' width='20%'> $Lang ::tr{'Captive voucher'}</th><th th align='center' width='25%'> $Lang ::tr{'Captive expire'}</th><th align='center' width='55%'> $Lang ::tr{'remark'}</th></tr>
380 $cgiparams { 'CODE' } = & gencode
();
381 print "<tr><td><center><b><font size='5'> $cgiparams {'CODE'}</font></b></center></td><td><center><font size='3'> $expire </font></center></td><td><input type='text' name='REMARK' align='left' size='80'></td></tr>" ;
382 print "</table><br>" ;
383 print "<center><input type='submit' name='ACTION' value=' $Lang ::tr{'Captive voucherout'}'><input type='hidden' name='CODE' value=' $cgiparams {'CODE'}'</center></form>" ;
385 if (! - z
$voucherout ) { & show_voucher_out
();}
386 if (! - z
$clients ) { & show_voucher_in_use
();}
389 sub show_license_connections
(){
390 #if there are active clients, show the box with active connections
391 return if ( - z
$clients || ! - f
$clients );
394 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
396 <center><table class='tbl'>
398 <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>
401 #read all clients from hash and show table
402 & General
:: readhasharray
( " $clients " , \
%clientshash );
403 foreach my $key ( keys %clientshash ){
404 my ( $sec , $min , $hour , $mday , $mon , $year ) = localtime ( $clientshash { $key }[ 6 ]);
405 my ( $secx , $minx , $hourx ) = localtime ( $clientshash { $key }[ 6 ]+( $clientshash { $key }[ 5 ]* 3600 ));
406 $mon = '0' .++ $mon if $mon < 10 ;
407 $min = '0' . $min if $min < 10 ;
408 $hour = '0' . $hour if $hour < 10 ;
412 $col = "bgcolor=' $color {'color20'}'" ;
414 $col = "bgcolor=' $color {'color22'}'" ;
417 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 " ;
418 printf ( " %02d " , $hour );
421 print "</center></td><td $col ><center> $mday . $mon . $year " ;
422 printf ( " %02d " , $hourx );
424 printf ( " %02d " , $minx );
425 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>" ;
433 sub show_voucher_out
(){
434 #if there are already generated but unsused vouchers, print a table
435 return if ( - z
$voucherout );
438 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive vout' });
440 <center><table class='tbl' border='0'>
442 <th align='center' width='15%'><font size='1'> $Lang ::tr{'date'}</th><th align='center' width='15%'> $Lang ::tr{'Captive voucher'}</th><th th align='center' width='15%'> $Lang ::tr{'Captive expire'}</th><th align='center' width='60%'> $Lang ::tr{'remark'}</th><th align='center' width='5%'> $Lang ::tr{'delete'}</th></tr>
445 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
446 foreach my $key ( keys %voucherhash )
448 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $voucherhash { $key }[ 0 ]));
449 my $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $voucherhash { $key }[ 0 ]+ $voucherhash { $key }[ 2 ]));
453 $col = "bgcolor=' $color {'color20'}'" ;
455 $col = "bgcolor=' $color {'color22'}'" ;
459 print "<td $col ><center> $starttime </td>" ;
460 print "<td $col ><center><b> $voucherhash { $key }[1]</b></td>" ;
461 print "<td $col ><center> $endtime </td>" ;
462 print "<td $col align='center'> $voucherhash { $key }[3]</td>" ;
463 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>" ;
471 sub show_voucher_in_use
(){
472 #if there are active clients which use vouchers show table
473 return if ( - z
$clients || ! - f
$clients );
476 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
478 <center><table class='tbl'>
480 <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>
483 & General
:: readhasharray
( " $clients " , \
%clientshash );
484 foreach my $key ( keys %clientshash )
486 my ( $sec , $min , $hour , $mday , $mon , $year ) = localtime ( $clientshash { $key }[ 6 ]);
487 my ( $secx , $minx , $hourx , $mdayx , $monx , $yearx ) = localtime ( $clientshash { $key }[ 6 ]+ $clientshash { $key }[ 7 ]);
489 $mon = '0' .++ $mon if $mon < 10 ;
490 $min = '0' . $min if $min < 10 ;
491 $hour = '0' . $hour if $hour < 10 ;
494 $monx = '0' .++ $mon if $mon < 10 ;
495 $minx = '0' . $min if $min < 10 ;
496 $hourx = '0' . $hour if $hour < 10 ;
501 $col = "bgcolor=' $color {'color20'}'" ;
503 $col = "bgcolor=' $color {'color22'}'" ;
507 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 " ;
508 printf ( " %02d " , $hour );
511 print "</center></td><td $col ><center> $mdayx . $monx . $yearx $clientshash { $key }[3]" ;
512 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>" ;
522 # Checks a hostname against RFC1035
524 # Each part should be at least two characters in length
525 # but no more than 63 characters
526 if ( length ( $remark ) < 1 || length ( $remark ) > 255 ) {
528 # Only valid characters are a-z, A-Z, 0-9 and -
529 if ( $remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/ \s
]*$/) {
531 # First character can only be a letter or a digit
532 if ( substr ( $remark , 0 , 1 ) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/ ) {
534 # Last character can only be a letter or a digit
535 if ( substr ( $remark , - 1 , 1 ) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/ ) {
541 #if an errormessage exits, show a box with errormessage
543 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'error messages' });
544 print "<class name='base'> $errormessage \n " ;
545 print " </class> \n " ;
550 & Header
:: closebigbox
();
551 & Header
:: closepage
();