]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/captive/index.cgi
captive: Get MAC address of a device without calling arp
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / captive / index.cgi
index c3f9d035f40ad9c4eff32e0292b5fc39c0f669cb..bcaf8561af3275ccecf5e757b8453af207bf0d13 100755 (executable)
@@ -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;
@@ -52,14 +53,13 @@ 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 Clients IP-Address
        my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
 
-       #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//;
+       # 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);
@@ -81,17 +81,15 @@ if ($cgiparams{'ACTION'} eq "$Lang::tr{'gpl i accept these terms and conditions'
        }       
 }
 
-if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive activate'}"){
+if ($cgiparams{'ACTION'} eq "SUBMIT") {
        my $ip_address;
-       my $mac_address;
        my $granted=0;
        #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//;
+       my $mac_address = &Network::get_hardware_address($ip_address);
        #Check if voucher is valid and write client to clients file, delete voucher from voucherout
        &General::readhasharray("$voucherout", \%voucherhash);
        &General::readhasharray("$clients", \%clientshash);
@@ -134,107 +132,38 @@ if($redir == 1){
 }
 
 #Open HTML Page, load header and css
-&head();
-&error();
-&start();
-
-#Functions
-sub start(){
-       if ($settings{'AUTH'} eq 'VOUCHER'){
-               &voucher();
-       }else{
-               &agb();
-       }
-}
+my $tmpl = HTML::Template->new(
+       filename => "/srv/web/ipfire/html/captive/template.html",
+       die_on_bad_params => 0
+);
 
-sub error(){
-       if ($errormessage){
-               print "<center><div class='title'><br><font color='red'>$errormessage</font><br></div><br>";
-       }
-}
+$tmpl->param(REDIRECT_URL => $url);
 
-sub head(){
-print<<END
-Content-type: text/html\n\n
-<html> 
-       <head>
-               <meta charset="utf-8">
-               <title>$settings{'TITLE'}</title>
-               <link href="../assets/captive.css" type="text/css" rel="stylesheet">
-       </head>
-       <body>
-END
-;
+# Voucher
+if ($settings{'AUTH'} eq "VOUCHER") {
+       $tmpl->param(VOUCHER  => 1);
 }
 
-sub agb(){
-print<<END
-       <center>
-               <div class="title">
-                       <h1>$settings{'TITLE'}</h1>
-               </div>
-               <br>
-               <div class="agb">
-               <textarea style="width:100%;" rows='40'>
-END
-;
-&getagb();
-print<<END
-               </textarea>
-                       <center>
-                               <form method='post' action='$ENV{'SCRIPT_NAME'}'>
-                                       <br><input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'gpl i accept these terms and conditions'}"/>
-                               </form>
-                       </center>
-               </div>
-       </center>
-       </body>
-       </html>
-END
-;
-}
+$tmpl->param(TITLE => $settings{'TITLE'});
+$tmpl->param(ERROR => $errormessage);
 
-sub voucher(){
-       print<<END
-       <center>
-               <div class="title">
-                       <h1>$settings{'TITLE'}</h1>
-               </div>
-               <br>
-               <div class="login">
-END
-;
-
-print<<END
-               <form method='post' action='$ENV{'SCRIPT_NAME'}'>
-                       <center>
-                               <table>
-                                       <tr>
-                                               <td>
-                                                       <b>$Lang::tr{'Captive voucher'}</b>&nbsp<input type='text' maxlength="8" size='10' style="font-size: 24px;font-weight: bold;" name='VOUCHER'>
-                                               </td>
-                                               <td>
-                                                       <input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'Captive activate'}"/>
-                                               </td>
-                                       </tr>
-                               </table>
-               </form>
-               </div>
-               <br>
-               <div class="agb">
-                       <textarea style="width:100%;" rows='40'>
-END
-;
-&getagb();
-print<<END
-                       </textarea>
-                       <br><br>
-               </div>
-       </body>
-       </html>
-END
-;
-}
+$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) = @_;
@@ -268,11 +197,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<p>/g;
+
+       return $tac;
 }