X-Git-Url: http://git.ipfire.org/?p=people%2Fpmueller%2Fipfire-2.x.git;a=blobdiff_plain;f=html%2Fcgi-bin%2Fcaptive%2Findex.cgi;h=932467eb296abaa4bd190a57a26ade4ce0ca2e1f;hp=dd459319966be4f130bb068a4a1d134085d1b9be;hb=4b33d29de7ea95d2ae9a128d6fb2304331d9bdc1;hpb=c7e78cc62e6699f00962caf440ed25270c461334 diff --git a/html/cgi-bin/captive/index.cgi b/html/cgi-bin/captive/index.cgi index dd45931996..932467eb29 100755 --- a/html/cgi-bin/captive/index.cgi +++ b/html/cgi-bin/captive/index.cgi @@ -23,6 +23,7 @@ use strict; use CGI ':standard'; use URI::Escape; use HTML::Entities(); +use HTML::Template; # enable only the following on debugging purpose #use warnings; @@ -39,7 +40,6 @@ my %settings=(); my $voucherout="${General::swroot}/captive/voucher_out"; my $clients="${General::swroot}/captive/clients"; my $settingsfile="${General::swroot}/captive/settings"; -my $redir=0; my $errormessage; my $url=param('redirect'); @@ -52,188 +52,122 @@ unless (-f $clients){ system("touch $clients"); } #Read settings &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); -#Actions -if ($cgiparams{'ACTION'} eq "$Lang::tr{'gpl i accept these terms and conditions'}"){ +# Actions +if ($cgiparams{'ACTION'} eq "SUBMIT") { + # Get client IP address + my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR}; + + # Retrieve the MAC address from the ARP table + my $mac_address = &Network::get_hardware_address($ip_address); + + &General::readhasharray("$clients", \%clientshash); my $key = &General::findhasharraykey(\%clientshash); - #Get Clients IP-Address - my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||""; + # Create a new client line + foreach my $i (0 .. 5) { $clientshash{$key}[$i] = ""; } - #Ask arp to give the corresponding MAC-Address - my $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4); - $mac_address =~ s/\n+\z//; + # MAC address of the client + $clientshash{$key}[0] = $mac_address; - &General::readhasharray("$clients", \%clientshash); + # IP address of the client + $clientshash{$key}[1] = $ip_address; - if (!$errormessage){ - foreach my $i (0 .. 5) { $clientshash{$key}[$i] = "";} + # Current time + $clientshash{$key}[2] = time(); - $clientshash{$key}[0] = $mac_address; #mac address of actual client - $clientshash{$key}[1] = $ip_address; #ip address of actual client - $clientshash{$key}[2] = time(); #actual time in unix seconds (timestamp of first conenction) - $clientshash{$key}[3] = $settings{'EXPIRE'}; #Expire time in seconds (1day, 1 week ....) - $clientshash{$key}[4] = $Lang::tr{'Captive auth_lic'}; #Type of license (license or voucher) - $clientshash{$key}[5] = ''; + if ($settings{"AUTH"} eq "VOUCHER") { + &General::readhasharray("$voucherout", \%voucherhash); - &General::writehasharray("$clients", \%clientshash); - system("/usr/local/bin/captivectrl"); - &General::log("Captive", "Internet Access granted via license-agreement for $ip_address until $clientshash{$key}[3]"); - $redir=1; - } -} + # Convert voucher input to uppercase + $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'}; -if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive activate'}"){ - my $ip_address; - my $mac_address; - - #Convert voucherinput to uppercase - $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'}; - #Get Clients IP-Address - $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||""; - #Ask arp to give the corresponding MAC-Address - $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4); - $mac_address =~ s/\n+\z//; - #Check if voucher is valid and write client to clients file, delete voucher from voucherout - &General::readhasharray("$voucherout", \%voucherhash); - &General::readhasharray("$clients", \%clientshash); - foreach my $key (keys %voucherhash) { - if($voucherhash{$key}[1] eq $cgiparams{'VOUCHER'}){ - #Voucher valid, write to clients, then delete from voucherout - my $key1 = &General::findhasharraykey(\%clientshash); - foreach my $i (0 .. 5) { $clientshash{$key1}[$i] = "";} - - $clientshash{$key1}[0] = $mac_address; - $clientshash{$key1}[1] = $ip_address; - $clientshash{$key1}[2] = time(); - $clientshash{$key1}[3] = $voucherhash{$key}[2]; - $clientshash{$key1}[4] = $cgiparams{'VOUCHER'}; - $clientshash{$key1}[5] = HTML::Entities::decode_entities($voucherhash{$key}[3]); - - &General::writehasharray("$clients", \%clientshash); - &General::log("Captive", "Internet Access granted via voucher no. $clientshash{$key1}[4] for $ip_address until $clientshash{$key}[3] Remark: $clientshash{$key1}[7]"); - - delete $voucherhash{$key}; - &General::writehasharray("$voucherout", \%voucherhash); - last; + # Walk through all valid vouchers and find the right one + my $found = 0; + foreach my $voucher (keys %voucherhash) { + if ($voucherhash{$voucher}[1] eq $cgiparams{'VOUCHER'}) { + $found = 1; + + # Copy expiry time + $clientshash{$key}[3] = $voucherhash{$voucher}[2]; + + # Save voucher code + $clientshash{$key}[4] = $cgiparams{'VOUCHER'}; + + # Copy voucher remark + $clientshash{$key}[5] = $voucherhash{$voucher}[3]; + + # Delete used voucher + delete $voucherhash{$voucher}; + &General::writehasharray("$voucherout", \%voucherhash); + + last; + } } - } - system("/usr/local/bin/captivectrl"); - $redir=1; -} -if($redir == 1){ - sleep(4); - print "Status: 302 Moved Temporarily\n"; - print "Location: $url\n"; - print "Connection: close\n"; - print "\n"; - exit 0; -} + if ($found == 1) { + &General::log("Captive", "Internet access granted via voucher ($clientshash{$key}[4]) for $ip_address until $clientshash{$key}[3]"); + } else { + $errormessage = $Lang::tr{"Captive invalid_voucher"}; + } + + # License + } else { + # Copy expiry time + $clientshash{$key}[3] = $settings{'EXPIRE'}; -#Open HTML Page, load header and css -&head(); -&error(); -&start(); - -#Functions -sub start(){ - if ($settings{'AUTH'} eq 'VOUCHER'){ - &voucher(); - }else{ - &agb(); + # No voucher code + $clientshash{$key}[4] = "LICENSE"; + + &General::log("Captive", "Internet access granted via license agreement for $ip_address until $clientshash{$key}[3]"); } -} -sub error(){ - if ($errormessage){ - print "

$errormessage
"; + # If no errors were found, save configruation and reload + if (!$errormessage) { + &General::writehasharray("$clients", \%clientshash); + + system("/usr/local/bin/captivectrl"); + + # Redirect client to the original URL + print "Status: 302 Moved Temporarily\n"; + print "Location: $url\n"; + print "Connection: close\n\n"; + exit 0; } } -sub head(){ -print< - - - $settings{'TITLE'} - - -END -; -} +my $tmpl = HTML::Template->new( + filename => "/srv/web/ipfire/html/captive/template.html", + die_on_bad_params => 0 +); -sub agb(){ -print< -
-
-

$settings{'TITLE'} -

-
-
- -
-
-
-
-
-
-
- - -END -; -} +$tmpl->param(REDIRECT_URL => $url); -sub voucher(){ - print< -
-
-

LOGIN

-
-
- -
-
- -

-
- - -END -; +# Voucher +if ($settings{'AUTH'} eq "VOUCHER") { + $tmpl->param(VOUCHER => 1); } +$tmpl->param(TITLE => $settings{'TITLE'}); +$tmpl->param(ERROR => $errormessage); + +$tmpl->param(TAC => &gettac()); + +# Some translated strings +$tmpl->param(L_ACTIVATE => $Lang::tr{'Captive ACTIVATE'}); +$tmpl->param(L_GAIN_ACCESS => $Lang::tr{'Captive GAIN ACCESS'}); +$tmpl->param(L_HEADING_TAC => $Lang::tr{'Captive heading tac'}); +$tmpl->param(L_HEADING_VOUCHER => $Lang::tr{'Captive heading voucher'}); +$tmpl->param(L_AGREE_TAC => $Lang::tr{'Captive agree tac'}); + +# Print header +print "Pragma: no-cache\n"; +print "Cache-control: no-cache\n"; +print "Connection: close\n"; +print "Content-type: text/html\n\n"; + +# Print rendered template +print $tmpl->output(); + sub getcgihash { my ($hash, $params) = @_; my $cgi = CGI->new (); @@ -266,11 +200,20 @@ sub getcgihash { return; } -sub getagb(){ - open( my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!"); - while(<$handle>){ - $_ = HTML::Entities::decode_entities($_); - print $_; - } - close( $handle ); +sub gettac() { + my @tac = (); + + open(my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!"); + while(<$handle>) { + $_ = HTML::Entities::decode_entities($_); + push(@tac, $_); + } + close($handle); + + my $tac = join("\n", @tac); + + # Format paragraphs + $tac =~ s/\n\n/<\/p>\n

/g; + + return $tac; }