]>
git.ipfire.org Git - people/dweismueller/ipfire-2.x.git/blob - html/cgi-bin/captive.cgi
0a16dc12b18f1567b2af4ba2b1b2fed84129b1db
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 if ( $cgiparams { 'UNLIMITED' } eq 'on' ){
60 $cgiparams { 'EXP_HOUR' } = '0' ;
61 $cgiparams { 'EXP_DAY' } = '0' ;
62 $cgiparams { 'EXP_WEEK' } = '0' ;
63 $cgiparams { 'EXP_MONTH' } = '0' ;
66 $settings { 'ENABLE_GREEN' } = $cgiparams { 'ENABLE_GREEN' };
67 $settings { 'ENABLE_BLUE' } = $cgiparams { 'ENABLE_BLUE' };
68 $settings { 'AUTH' } = $cgiparams { 'AUTH' };
69 $settings { 'EXPIRE' } = $cgiparams { 'EXP_HOUR' }+ $cgiparams { 'EXP_DAY' }+ $cgiparams { 'EXP_WEEK' }+ $cgiparams { 'EXP_MONTH' };
70 $settings { 'EXP_HOUR' } = $cgiparams { 'EXP_HOUR' };
71 $settings { 'EXP_DAY' } = $cgiparams { 'EXP_DAY' };
72 $settings { 'EXP_WEEK' } = $cgiparams { 'EXP_WEEK' };
73 $settings { 'EXP_MONTH' } = $cgiparams { 'EXP_MONTH' };
74 $settings { 'TITLE' } = $cgiparams { 'TITLE' };
75 $settings { 'UNLIMITED' } = $cgiparams { 'UNLIMITED' };
76 & General
:: writehash
( " $settingsfile " , \
%settings );
78 #write Licensetext if defined
79 if ( $cgiparams { 'AGB' }){
80 $cgiparams { 'AGB' } = & Header
:: escape
( $cgiparams { 'AGB' });
81 open ( FH
, ">:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
82 print FH
$cgiparams { 'AGB' };
86 #execute binary to reload firewall rules
87 system ( "/usr/local/bin/captivectrl" );
90 if ( $cgiparams { 'ACTION' } eq " $Lang ::tr{'Captive voucherout'}" ){
91 #generates a voucher and writes it to /var/ipfire/voucher_out
93 #check if we already have a voucher with same code
94 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
95 foreach my $key ( keys %voucherhash ) {
96 if ( $voucherhash { $key }[ 1 ] eq $cgiparams { 'CODE' }){
97 $errormessage = $Lang :: tr
{ 'Captive err doublevoucher' };
103 if ( $cgiparams { 'REMARK' } ne '' && !& validremark
( $cgiparams { 'REMARK' })){
104 $errormessage = $Lang :: tr
{ 'fwhost err remark' };
107 #if no error detected, write to disk
109 my $date = time (); #seconds in utc
111 #first get new key from hash
112 my $key =& General
:: findhasharraykey
( \
%voucherhash );
113 #initialize all fields with ''
114 foreach my $i ( 0 .. 3 ) { $voucherhash { $key }[ $i ] = "" ;}
116 $voucherhash { $key }[ 0 ] = $date ;
117 $voucherhash { $key }[ 1 ] = $cgiparams { 'CODE' };
118 $voucherhash { $key }[ 2 ] = $settings { 'EXPIRE' };
119 $voucherhash { $key }[ 3 ] = $cgiparams { 'REMARK' };
120 #write values to disk
121 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
123 #now prepare log entry, get expiring date for voucher and decode remark for logfile
124 my $expdate = localtime ( time ()+ $voucherhash { $key }[ 3 ]);
125 my $rem = HTML
:: Entities
:: decode_entities
( $voucherhash { $key }[ 4 ]);
128 & General
:: log ( "Captive" , "Generated new voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $expdate remark $rem " );
132 if ( $cgiparams { 'ACTION' } eq 'delvoucherout' ){
133 #deletes an already generated but unused voucher
135 #read all generated vouchers
136 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
137 foreach my $key ( keys %voucherhash ) {
138 if ( $cgiparams { 'key' } eq $voucherhash { $key }[ 0 ]){
139 #write logenty with decoded remark
140 my $rem = HTML
:: Entities
:: decode_entities
( $voucherhash { $key }[ 4 ]);
141 & General
:: log ( "Captive" , "Delete unused voucher $voucherhash { $key }[1] $voucherhash { $key }[2] hours valid expires on $voucherhash { $key }[3] remark $rem " );
142 #delete line from hash
143 delete $voucherhash { $key };
148 & General
:: writehasharray
( " $voucherout " , \
%voucherhash );
151 if ( $cgiparams { 'ACTION' } eq 'delvoucherinuse' ){
152 #delete voucher and connection in use
154 #read all active clients
155 & General
:: readhasharray
( " $clients " , \
%clientshash );
156 foreach my $key ( keys %clientshash ) {
157 if ( $cgiparams { 'key' } eq $clientshash { $key }[ 0 ]){
158 #prepare log entry with decoded remark
159 my $rem = HTML
:: Entities
:: decode_entities
( $clientshash { $key }[ 7 ]);
161 & 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" );
162 #delete line from hash
163 delete $clientshash { $key };
168 & General
:: writehasharray
( " $clients " , \
%clientshash );
169 #reload firewallrules to kill connection of client
170 system ( "/usr/local/bin/captivectrl" );
173 #open webpage, print header and open box
174 & Header
:: openpage
( $Lang :: tr
{ 'Captive menu' }, 1 , '' );
175 & Header
:: openbigbox
();
177 #call error() to see if we have to print an errormessage on website
180 #call config() to display the configuration box
184 #open textfile from /var/ipfire/captive/agb.txt
185 open ( my $handle , "<:utf8" , "/var/ipfire/captive/agb.txt" ) or die ( "$!" );
187 #read line by line and print on screen
188 $cgiparams { 'AGB' }.= HTML
:: Entities
:: decode_entities
( $_ );
194 #prints the config box on the website
195 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive config' });
197 <form method='post' action=' $ENV {'SCRIPT_NAME'}'> \n
198 <table width='100%' border="0">
202 #check which parameters have to be enabled (from settings file)
203 $checked { 'ENABLE_GREEN' }{ 'off' } = '' ;
204 $checked { 'ENABLE_GREEN' }{ 'on' } = '' ;
205 $checked { 'ENABLE_GREEN' }{ $settings { 'ENABLE_GREEN' }} = "checked='checked'" ;
207 $checked { 'ENABLE_BLUE' }{ 'off' } = '' ;
208 $checked { 'ENABLE_BLUE' }{ 'on' } = '' ;
209 $checked { 'ENABLE_BLUE' }{ $settings { 'ENABLE_BLUE' }} = "checked='checked'" ;
211 $checked { 'UNLIMITED' }{ 'off' } = '' ;
212 $checked { 'UNLIMITED' }{ 'on' } = '' ;
213 $checked { 'UNLIMITED' }{ $settings { 'UNLIMITED' }} = "checked='checked'" ;
215 if ( $netsettings { 'GREEN_DEV' }){
216 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>" ;
218 if ( $netsettings { 'BLUE_DEV' }){
219 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>" ;
226 $Lang ::tr{'Captive title'}
229 <input type='text' name='TITLE' value=" $settings {'TITLE'}" size='40'>
238 $Lang ::tr{'Captive authentication'}
241 <select name='AUTH' style='width:8em;'>
244 print "<option value='LICENSE' " ;
245 print " selected='selected'" if ( $settings { 'AUTH' } eq 'LICENSE' );
246 print "> $Lang ::tr{'Captive auth_lic'}</option>" ;
248 print "<option value='VOUCHER' " ;
249 print " selected='selected'" if ( $settings { 'AUTH' } eq 'VOUCHER' );
250 print "> $Lang ::tr{'Captive auth_vou'}</option>" ;
259 if ( $settings { 'AUTH' } eq 'LICENSE' ){
263 print "<tr><td> $Lang ::tr{'Captive vouchervalid'}</td><td>" ;
265 print "<br><table border='0' with=100%>" ;
266 print "<th> $Lang ::tr{'hours'}</th><th> $Lang ::tr{'days'}</th><th> $Lang ::tr{'weeks'}</th><th> $Lang ::tr{'months'}</th>" ;
268 #print hour-dropdownbox
270 print "<tr><td><select name='EXP_HOUR' style='width:8em;'>" ;
271 print "<option value='0' " ;
272 print " selected='selected'" if ( $settings { 'EXP_HOUR' } eq '0' );
273 print ">--</option>" ;
274 for ( my $i = 1 ; $i < 25 ; $i ++){
275 my $exp_sec = $i * $hrs ;
276 print "<option value=' $exp_sec ' " ;
277 print " selected='selected'" if ( $settings { 'EXP_HOUR' } eq $exp_sec );
278 print "> $i </option>" ;
282 #print day-dropdownbox
284 print "<select name='EXP_DAY' style='width:8em;'>" ;
285 print "<option value='0' " ;
286 print " selected='selected'" if ( $settings { 'EXP_DAY' } eq '0' );
287 print ">--</option>" ;
288 for ( my $i = 1 ; $i < 8 ; $i ++){
289 my $exp_sec = $i * $days ;
290 print "<option value=' $exp_sec ' " ;
291 print " selected='selected'" if ( $settings { 'EXP_DAY' } eq $exp_sec );
292 print "> $i </option>" ;
296 #print week-dropdownbox
298 print "<select name='EXP_WEEK' style='width:8em;'>" ;
299 print "<option value='0' " ;
300 print " selected='selected'" if ( $settings { 'EXP_WEEK' } eq '0' );
301 print ">--</option>" ;
302 for ( my $i = 1 ; $i < 5 ; $i ++){
303 my $exp_sec = $i * $week ;
304 print "<option value=' $exp_sec ' " ;
305 print " selected='selected'" if ( $settings { 'EXP_WEEK' } eq $exp_sec );
306 print "> $i </option>" ;
310 #print month-dropdownbox
311 my $month = 3600 * 24 * 30 ;
312 print "<select name='EXP_MONTH' style='width:8em;'>" ;
313 print "<option value='0' " ;
314 print " selected='selected'" if ( $settings { 'EXP_MONTH' } eq '0' );
315 print ">--</option>" ;
316 for ( my $i = 1 ; $i < 13 ; $i ++){
317 my $exp_sec = $i * $month ;
318 print "<option value=' $exp_sec ' " ;
319 print " selected='selected'" if ( $settings { 'EXP_MONTH' } eq $exp_sec );
320 print "> $i </option>" ;
324 print "<td> <input type='checkbox' name='UNLIMITED' $checked {'UNLIMITED'}{'on'} /></td><td> <b> $Lang ::tr{'Captive nolimit'}</b></td>" ;
326 print "</tr></table>" ;
333 <input type='submit' name='ACTION' value=" $Lang ::tr{'save'}"/>
344 #if settings is set to use vouchers, the voucher part has to be displayed
345 if ( $settings { 'AUTH' } eq 'VOUCHER' ){
348 #otherwise we show the licensepart
349 & show_license_connections
();
362 <textarea cols="50" rows="10" name="AGB"> $cgiparams {'AGB'}</textarea>
370 #generate a random code only letters from A-Z except 'O' and 0-9
371 my @chars = ( "A" .. "N" , "P" .. "Z" , "0" .. "9" );
373 $randomstring .= $chars [ rand @chars ] for 1 . .8 ;
374 return $randomstring ;
379 #calculate expiredate
381 if ( $settings { 'UNLIMITED' } eq 'on' ){
382 $expire = $Lang :: tr
{ 'Captive nolimit' };
384 $expire = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( time ()+ $settings { 'EXPIRE' }));
387 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voucher' });
389 <form method='post' action=' $ENV {'SCRIPT_NAME'}'>
392 <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>
396 $cgiparams { 'CODE' } = & gencode
();
397 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>" ;
398 print "</table><br>" ;
399 print "<center><input type='submit' name='ACTION' value=' $Lang ::tr{'Captive voucherout'}'><input type='hidden' name='CODE' value=' $cgiparams {'CODE'}'</center></form>" ;
401 if (! - z
$voucherout ) { & show_voucher_out
();}
402 if (! - z
$clients ) { & show_voucher_in_use
();}
405 sub show_license_connections
(){
406 #if there are active clients, show the box with active connections
407 return if ( - z
$clients || ! - f
$clients );
410 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
412 <center><table class='tbl'>
414 <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>
417 #read all clients from hash and show table
418 & General
:: readhasharray
( " $clients " , \
%clientshash );
419 foreach my $key ( keys %clientshash ){
420 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]));
422 if ( $clientshash { $key }[ 3 ] eq '0' ){
423 $endtime = $Lang :: tr
{ 'Captive nolimit' };
425 $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]+ $clientshash { $key }[ 3 ]));
430 $col = "bgcolor=' $color {'color20'}'" ;
432 $col = "bgcolor=' $color {'color22'}'" ;
435 print "<td $col ><center> $clientshash { $key }[4]</td><td $col ><center> $starttime " ;
436 print "</center></td><td $col ><center> $endtime " ;
437 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>" ;
445 sub show_voucher_out
(){
446 #if there are already generated but unsused vouchers, print a table
447 return if ( - z
$voucherout );
450 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive vout' });
452 <center><table class='tbl' border='0'>
454 <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>
457 & General
:: readhasharray
( " $voucherout " , \
%voucherhash );
458 foreach my $key ( keys %voucherhash )
460 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $voucherhash { $key }[ 0 ]));
462 if ( $voucherhash { $key }[ 2 ] eq '0' ){
463 $endtime = $Lang :: tr
{ 'Captive nolimit' };
465 $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $voucherhash { $key }[ 0 ]+ $voucherhash { $key }[ 2 ]));
470 $col = "bgcolor=' $color {'color20'}'" ;
472 $col = "bgcolor=' $color {'color22'}'" ;
476 print "<td $col ><center><b> $voucherhash { $key }[1]</b></td>" ;
477 print "<td $col ><center> $starttime </td>" ;
478 print "<td $col ><center> $endtime </td>" ;
479 print "<td $col align='center'> $voucherhash { $key }[3]</td>" ;
480 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>" ;
488 sub show_voucher_in_use
(){
489 #if there are active clients which use vouchers show table
490 return if ( - z
$clients || ! - f
$clients );
493 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'Captive voactive' });
495 <center><table class='tbl' width='100%'>
497 <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>
500 & General
:: readhasharray
( " $clients " , \
%clientshash );
501 foreach my $key ( keys %clientshash )
503 #calculate time from clientshash (starttime)
504 my $starttime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]));
505 #calculate endtime from clientshash
507 if ( $clientshash { $key }[ 3 ] eq '0' ){
508 $endtime = $Lang :: tr
{ 'Captive nolimit' };
510 $endtime = sub { sprintf ' %02d . %02d . %04d %02d : %02d ' , $_ [ 3 ], $_ [ 4 ]+ 1 , $_ [ 5 ]+ 1900 , $_ [ 2 ], $_ [ 1 ] }->( localtime ( $clientshash { $key }[ 2 ]+ $clientshash { $key }[ 3 ]));
515 $col = "bgcolor=' $color {'color20'}'" ;
517 $col = "bgcolor=' $color {'color22'}'" ;
521 print "<td $col ><center><b> $clientshash { $key }[4]</b></td><td $col ><center> $starttime " ;
522 print "</center></td><td $col ><center> $endtime </center></td><td $col ><center> $clientshash { $key }[0]</td><td $col ><center> $clientshash { $key }[5]</center>" ;
523 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>" ;
533 # Checks a hostname against RFC1035
535 # Each part should be at least two characters in length
536 # but no more than 63 characters
537 if ( length ( $remark ) < 1 || length ( $remark ) > 255 ) {
539 # Only valid characters are a-z, A-Z, 0-9 and -
540 if ( $remark !~ /^[a-zäöüA-ZÖÄÜ0-9-.:;\|_()\/ \s
]*$/) {
542 # First character can only be a letter or a digit
543 if ( substr ( $remark , 0 , 1 ) !~ /^[a-zäöüA-ZÖÄÜ0-9]*$/ ) {
545 # Last character can only be a letter or a digit
546 if ( substr ( $remark , - 1 , 1 ) !~ /^[a-zöäüA-ZÖÄÜ0-9.:;_)]*$/ ) {
552 #if an errormessage exits, show a box with errormessage
554 & Header
:: openbox
( '100%' , 'left' , $Lang :: tr
{ 'error messages' });
555 print "<class name='base'> $errormessage \n " ;
556 print " </class> \n " ;
561 & Header
:: closebigbox
();
562 & Header
:: closepage
();