]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/captive.cgi
a42e398154882d7ce15d2e6a242462480ed4a3ac
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" ;
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
$settingsfile ) { system ( "touch $settingsfile " ); }
46 unless (- e
$voucherout ) { system ( "touch $voucherout " ); }
48 & Header
:: getcgihash
( \
%cgiparams );
50 & General
:: readhash
( "${General::swroot}/main/settings" , \
%mainsettings );
51 & General
:: readhash
( "/srv/web/ipfire/html/themes/" . $mainsettings { 'THEME' }. "/include/colors.txt" , \
%color );
52 & General
:: readhash
( " $settingsfile " , \
%settings ) if (- f
$settingsfile );
53 & General
:: readhash
( "${General::swroot}/ethernet/settings" , \
%netsettings );
55 & Header
:: showhttpheaders
();
58 if ( $cgiparams { 'ACTION' } eq " $Lang ::tr{'save'}" ){
59 #saves the Captiveportal settings to disk
60 if ( $cgiparams { 'UNLIMITED' } eq 'on' ){
61 $cgiparams { 'EXP_HOUR' } = '0' ;
62 $cgiparams { 'EXP_DAY' } = '0' ;
63 $cgiparams { 'EXP_WEEK' } = '0' ;
64 $cgiparams { 'EXP_MONTH' } = '0' ;
67 $settings { 'ENABLE_GREEN' } = $cgiparams { 'ENABLE_GREEN' };
68 $settings { 'ENABLE_BLUE' } = $cgiparams { 'ENABLE_BLUE' };
69 $settings { 'AUTH' } = $cgiparams { 'AUTH' };
70 $settings { 'EXPIRE' } = $cgiparams { 'EXP_HOUR' }+ $cgiparams { 'EXP_DAY' }+ $cgiparams { 'EXP_WEEK' }+ $cgiparams { 'EXP_MONTH' };
71 $settings { 'EXP_HOUR' } = $cgiparams { 'EXP_HOUR' };
72 $settings { 'EXP_DAY' } = $cgiparams { 'EXP_DAY' };
73 $settings { 'EXP_WEEK' } = $cgiparams { 'EXP_WEEK' };
74 $settings { 'EXP_MONTH' } = $cgiparams { 'EXP_MONTH' };
75 $settings { 'TITLE' } = $cgiparams { 'TITLE' };
76 $settings { 'UNLIMITED' } = $cgiparams { 'UNLIMITED' };
77 & General
:: writehash
( " $settingsfile " , \
%settings );
79 #write Licensetext if defined
80 if ( $cgiparams { 'AGB' }){
81 $cgiparams { 'AGB' } = & Header
:: escape
( $cgiparams { 'AGB' });
82 open ( FH
, ">:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
83 print FH
$cgiparams { 'AGB' };
87 #execute binary to reload firewall rules
88 system ( "/usr/local/bin/captivectrl" );
91 if ( $cgiparams { 'ACTION' } eq " $Lang ::tr{'Captive voucherout'}" ){
92 #generates a voucher and writes it to /var/ipfire/voucher_out
94 #check if we already have a voucher with same code
95 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
96 foreach my $key ( keys %voucherhash ) {
97 if ( $voucherhash { $key }[ 1 ] eq $cgiparams { 'CODE' }){
98 $errormessage = $Lang :: tr
{ 'Captive err doublevoucher' };
104 if ( $cgiparams { 'REMARK' } ne '' && !& validremark
( $cgiparams { 'REMARK' })){
105 $errormessage = $Lang :: tr
{ 'fwhost err remark' };
108 #if no error detected, write to disk
110 my $date = time (); #seconds in utc
112 #first get new key from hash
113 my $key =& General
:: findhasharraykey
( \
%voucherhash );
114 #initialize all fields with ''
115 foreach my $i ( 0 .. 3 ) { $voucherhash { $key }[ $i ] = "" ;}
117 $voucherhash { $key }[ 0 ] = $date ;
118 $voucherhash { $key }[ 1 ] = $cgiparams { 'CODE' };
119 $voucherhash { $key }[ 2 ] = $settings { 'EXPIRE' };
120 $voucherhash { $key }[ 3 ] = $cgiparams { 'REMARK' };
121 #write values to disk
122 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
124 #now prepare log entry, get expiring date for voucher and decode remark for logfile
125 my $expdate = localtime ( time ()+ $voucherhash { $key }[ 3 ]);
126 my $rem = HTML
:: Entities
:: decode_entities
( $voucherhash { $key }[ 4 ]);
129 & General
:: log ( "Captive" , "Generated new voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $expdate remark $rem " );
133 if ( $cgiparams { 'ACTION' } eq 'delvoucherout' ){
134 #deletes an already generated but unused voucher
136 #read all generated vouchers
137 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
138 foreach my $key ( keys %voucherhash ) {
139 if ( $cgiparams { 'key' } eq $voucherhash { $key }[ 0 ]){
140 #write logenty with decoded remark
141 my $rem = HTML
:: Entities
:: decode_entities
( $voucherhash { $key }[ 4 ]);
142 & General
:: log ( "Captive" , "Delete unused voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $voucherhash { $key }[3] remark $rem " );
143 #delete line from hash
144 delete $voucherhash { $key };
149 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
152 if ( $cgiparams { 'ACTION' } eq 'delvoucherinuse' ){
153 #delete voucher and connection in use
155 #read all active clients
156 & General
:: readhasharray
( " $clients " , \
%clientshash );
157 foreach my $key ( keys %clientshash ) {
158 if ( $cgiparams { 'key' } eq $clientshash { $key }[ 0 ]){
159 #prepare log entry with decoded remark
160 my $rem = HTML
:: Entities
:: decode_entities
( $clientshash { $key }[ 7 ]);
162 & 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" );
163 #delete line from hash
164 delete $clientshash { $key };
169 & General
:: writehasharray
( " $clients " , \
%clientshash );
170 #reload firewallrules to kill connection of client
171 system ( "/usr/local/bin/captivectrl" );
174 #open webpage, print header and open box
175 & Header
:: openpage
( $Lang :: tr
{ 'Captive menu' }, 1 , '' );
176 & Header
:: openbigbox
();
178 #call error() to see if we have to print an errormessage on website
181 #call config() to display the configuration box
185 #open textfile from /var/ipfire/captive/agb.txt
186 open ( my $handle , "<:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
188 #read line by line and print on screen
189 $cgiparams { 'AGB' }.= HTML
:: Entities
:: decode_entities
( $_ );
195 #prints the config box on the website
196 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive config' });
198 <form method='post' action=' $ENV {'SCRIPT_NAME'}'> \n
199 <table width='100%' border="0">
203 #check which parameters have to be enabled (from settings file)
204 $checked { 'ENABLE_GREEN' }{ 'off' } = '' ;
205 $checked { 'ENABLE_GREEN' }{ 'on' } = '' ;
206 $checked { 'ENABLE_GREEN' }{ $settings { 'ENABLE_GREEN' }} = "checked='checked'" ;
208 $checked { 'ENABLE_BLUE' }{ 'off' } = '' ;
209 $checked { 'ENABLE_BLUE' }{ 'on' } = '' ;
210 $checked { 'ENABLE_BLUE' }{ $settings { 'ENABLE_BLUE' }} = "checked='checked'" ;
212 $checked { 'UNLIMITED' }{ 'off' } = '' ;
213 $checked { 'UNLIMITED' }{ 'on' } = '' ;
214 $checked { 'UNLIMITED' }{ $settings { 'UNLIMITED' }} = "checked='checked'" ;
216 if ( $netsettings { 'GREEN_DEV' }){
217 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>" ;
219 if ( $netsettings { 'BLUE_DEV' }){
220 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>" ;
227 $Lang ::tr{'Captive title'}
230 <input type='text' name='TITLE' value=" $settings {'TITLE'}" size='40'>
239 $Lang ::tr{'Captive authentication'}
242 <select name='AUTH' style='width:8em;'>
245 print "<option value='LICENSE' " ;
246 print " selected='selected'" if ( $settings { 'AUTH' } eq 'LICENSE' );
247 print "> $Lang ::tr{'Captive auth_lic'}</option>" ;
249 print "<option value='VOUCHER' " ;
250 print " selected='selected'" if ( $settings { 'AUTH' } eq 'VOUCHER' );
251 print "> $Lang ::tr{'Captive auth_vou'}</option>" ;
262 print "<tr><td> $Lang ::tr{'Captive vouchervalid'}</td><td>" ;
263 print "<br><table border='0' with=100%>" ;
264 print "<th> $Lang ::tr{'hours'}</th><th> $Lang ::tr{'days'}</th><th> $Lang ::tr{'weeks'}</th><th> $Lang ::tr{'months'}</th>" ;
266 #print hour-dropdownbox
268 print "<tr><td><select name='EXP_HOUR' style='width:8em;'>" ;
269 print "<option value='0' " ;
270 print " selected='selected'" if ( $settings { 'EXP_HOUR' } eq '0' );
271 print ">--</option>" ;
272 for ( my $i = 1 ; $i < 25 ; $i ++){
273 my $exp_sec = $i * $hrs ;
274 print "<option value=' $exp_sec ' " ;
275 print " selected='selected'" if ( $settings { 'EXP_HOUR' } eq $exp_sec );
276 print "> $i </option>" ;
280 #print day-dropdownbox
282 print "<select name='EXP_DAY' style='width:8em;'>" ;
283 print "<option value='0' " ;
284 print " selected='selected'" if ( $settings { 'EXP_DAY' } eq '0' );
285 print ">--</option>" ;
286 for ( my $i = 1 ; $i < 8 ; $i ++){
287 my $exp_sec = $i * $days ;
288 print "<option value=' $exp_sec ' " ;
289 print " selected='selected'" if ( $settings { 'EXP_DAY' } eq $exp_sec );
290 print "> $i </option>" ;
294 #print week-dropdownbox
296 print "<select name='EXP_WEEK' style='width:8em;'>" ;
297 print "<option value='0' " ;
298 print " selected='selected'" if ( $settings { 'EXP_WEEK' } eq '0' );
299 print ">--</option>" ;
300 for ( my $i = 1 ; $i < 5 ; $i ++){
301 my $exp_sec = $i * $week ;
302 print "<option value=' $exp_sec ' " ;
303 print " selected='selected'" if ( $settings { 'EXP_WEEK' } eq $exp_sec );
304 print "> $i </option>" ;
308 #print month-dropdownbox
309 my $month = 3600 * 24 * 30 ;
310 print "<select name='EXP_MONTH' style='width:8em;'>" ;
311 print "<option value='0' " ;
312 print " selected='selected'" if ( $settings { 'EXP_MONTH' } eq '0' );
313 print ">--</option>" ;
314 for ( my $i = 1 ; $i < 13 ; $i ++){
315 my $exp_sec = $i * $month ;
316 print "<option value=' $exp_sec ' " ;
317 print " selected='selected'" if ( $settings { 'EXP_MONTH' } eq $exp_sec );
318 print "> $i </option>" ;
322 print "<td> <input type='checkbox' name='UNLIMITED' $checked {'UNLIMITED'}{'on'} /></td><td> <b> $Lang ::tr{'Captive nolimit'}</b></td>" ;
324 print "</tr></table>" ;
331 <input type='submit' name='ACTION' value=" $Lang ::tr{'save'}"/>
342 #if settings is set to use vouchers, the voucher part has to be displayed
343 if ( $settings { 'AUTH' } eq 'VOUCHER' ){
346 #otherwise we show the licensepart
347 & show_license_connections
();
360 <textarea cols="50" rows="10" name="AGB"> $cgiparams {'AGB'}</textarea>
368 #generate a random code only letters from A-Z except 'O' and 0-9
369 my @chars = ( "A" .. "N" , "P" .. "Z" , "0" .. "9" );
371 $randomstring .= $chars [ rand @chars ] for 1 . .8 ;
372 return $randomstring ;
377 #calculate expiredate
379 if ( $settings { 'UNLIMITED' } eq 'on' ){
380 $expire = $Lang :: tr
{ 'Captive nolimit' };
382 $expire = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( time ()+ $settings { 'EXPIRE' }));
385 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voucher' });
387 <form method='post' action=' $ENV {'SCRIPT_NAME'}'>
390 <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>
394 $cgiparams { 'CODE' } = & gencode
();
395 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>" ;
396 print "</table><br>" ;
397 print "<center><input type='submit' name='ACTION' value=' $Lang ::tr{'Captive voucherout'}'><input type='hidden' name='CODE' value=' $cgiparams {'CODE'}'</center></form>" ;
399 if (! - z
$voucherout ) { & show_voucher_out
();}
400 if (! - z
$clients ) { & show_voucher_in_use
();}
403 sub show_license_connections
(){
404 #if there are active clients, show the box with active connections
405 return if ( - z
$clients || ! - f
$clients );
408 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
410 <center><table class='tbl'>
412 <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 align='center' width='50%'><font size='1'> $Lang ::tr{'Captive mac'}</th><th th align='center' width='5%'> $Lang ::tr{'delete'}</th></tr>
415 #read all clients from hash and show table
416 & General
:: readhasharray
( " $clients " , \
%clientshash );
417 foreach my $key ( keys %clientshash ){
418 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]));
420 if ( $clientshash { $key }[ 3 ] eq '0' ){
421 $endtime = $Lang :: tr
{ 'Captive nolimit' };
423 $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]+ $clientshash { $key }[ 3 ]));
428 $col = "bgcolor=' $color {'color20'}'" ;
430 $col = "bgcolor=' $color {'color22'}'" ;
433 print "<td $col ><center> $clientshash { $key }[4]</td><td $col ><center> $starttime " ;
434 print "</center></td><td $col ><center> $endtime " ;
435 print "</td><td $col ><center> $clientshash { $key }[0]</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>" ;
443 sub show_voucher_out
(){
444 #if there are already generated but unsused vouchers, print a table
445 return if ( - z
$voucherout );
448 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive vout' });
450 <center><table class='tbl' border='0'>
452 <th align='center' width='15%'> $Lang ::tr{'Captive voucher'}</th><th align='center' width='15%'> $Lang ::tr{'date'}</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>
455 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
456 foreach my $key ( keys %voucherhash )
458 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $voucherhash { $key }[ 0 ]));
460 if ( $voucherhash { $key }[ 2 ] eq '0' ){
461 $endtime = $Lang :: tr
{ 'Captive nolimit' };
463 $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( time ()+ $voucherhash { $key }[ 2 ]));
468 $col = "bgcolor=' $color {'color20'}'" ;
470 $col = "bgcolor=' $color {'color22'}'" ;
474 print "<td $col ><center><b> $voucherhash { $key }[1]</b></td>" ;
475 print "<td $col ><center> $starttime </td>" ;
476 print "<td $col ><center> $endtime </td>" ;
477 print "<td $col align='center'> $voucherhash { $key }[3]</td>" ;
478 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>" ;
486 sub show_voucher_in_use
(){
487 #if there are active clients which use vouchers show table
488 return if ( - z
$clients || ! - f
$clients );
491 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
493 <center><table class='tbl' width='100%'>
495 <th align='center' width='15%'> $Lang ::tr{'Captive voucher'}</th><th th align='center' width='15%'> $Lang ::tr{'Captive activated'}</th><th align='center' width='15%'> $Lang ::tr{'Captive expire'}</th><th align='center' width='10%'> $Lang ::tr{'Captive mac'}</th><th align='center' width='43%'> $Lang ::tr{'remark'}</th><th th align='center' width='5%'> $Lang ::tr{'delete'}</th></tr>
498 & General
:: readhasharray
( " $clients " , \
%clientshash );
499 foreach my $key ( keys %clientshash )
501 #calculate time from clientshash (starttime)
502 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]));
503 #calculate endtime from clientshash
505 if ( $clientshash { $key }[ 3 ] eq '0' ){
506 $endtime = $Lang :: tr
{ 'Captive nolimit' };
508 $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]+ $clientshash { $key }[ 3 ]));
513 $col = "bgcolor=' $color {'color20'}'" ;
515 $col = "bgcolor=' $color {'color22'}'" ;
519 print "<td $col ><center><b> $clientshash { $key }[4]</b></td><td $col ><center> $starttime " ;
520 print "</center></td><td $col ><center> $endtime </center></td><td $col ><center> $clientshash { $key }[0]</td><td $col ><center> $clientshash { $key }[5]</center>" ;
521 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>" ;
531 # Checks a hostname against RFC1035
533 # Each part should be at least two characters in length
534 # but no more than 63 characters
535 if ( length ( $remark ) < 1 || length ( $remark ) > 255 ) {
537 # Only valid characters are a-z, A-Z, 0-9 and -
538 if ( $remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/ \s
]*$/) {
540 # First character can only be a letter or a digit
541 if ( substr ( $remark , 0 , 1 ) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/ ) {
543 # Last character can only be a letter or a digit
544 if ( substr ( $remark , - 1 , 1 ) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/ ) {
550 #if an errormessage exits, show a box with errormessage
552 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'error messages' });
553 print "<class name='base'> $errormessage \n " ;
554 print " </class> \n " ;
559 & Header
:: closebigbox
();
560 & Header
:: closepage
();