]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/captive/index.cgi
bootstrap: New package
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / captive / index.cgi
CommitLineData
8b920789
AM
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2016 Alexander Marx alexander.marx@ipfire.org #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22use strict;
23use CGI ':standard';
24use URI::Escape;
25use HTML::Entities();
a2c26388 26use HTML::Template;
e01c5ab7 27
8b920789
AM
28# enable only the following on debugging purpose
29#use warnings;
30#use CGI::Carp 'fatalsToBrowser';
31
32require '/var/ipfire/general-functions.pl';
33require "${General::swroot}/lang.pl";
34
35#Set Variables
36my %voucherhash=();
37my %clientshash=();
38my %cgiparams=();
39my %settings=();
40my $voucherout="${General::swroot}/captive/voucher_out";
41my $clients="${General::swroot}/captive/clients";
42my $settingsfile="${General::swroot}/captive/settings";
43my $redir=0;
44my $errormessage;
45my $url=param('redirect');
e01c5ab7 46
8b920789
AM
47#Create /var/ipfire/captive/clients if not exist
48unless (-f $clients){ system("touch $clients"); }
49
50#Get GUI variables
51&getcgihash(\%cgiparams);
52
53#Read settings
54&General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
55
56#Actions
57if ($cgiparams{'ACTION'} eq "$Lang::tr{'gpl i accept these terms and conditions'}"){
8b920789
AM
58 #Get Clients IP-Address
59 my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
60
61 #Ask arp to give the corresponding MAC-Address
62 my $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4);
63 $mac_address =~ s/\n+\z//;
64
65 &General::readhasharray("$clients", \%clientshash);
11fc9575 66 my $key = &General::findhasharraykey(\%clientshash);
8b920789
AM
67
68 if (!$errormessage){
e01c5ab7
AM
69 foreach my $i (0 .. 5) { $clientshash{$key}[$i] = "";}
70
71 $clientshash{$key}[0] = $mac_address; #mac address of actual client
72 $clientshash{$key}[1] = $ip_address; #ip address of actual client
73 $clientshash{$key}[2] = time(); #actual time in unix seconds (timestamp of first conenction)
74 $clientshash{$key}[3] = $settings{'EXPIRE'}; #Expire time in seconds (1day, 1 week ....)
75 $clientshash{$key}[4] = $Lang::tr{'Captive auth_lic'}; #Type of license (license or voucher)
76 $clientshash{$key}[5] = '';
77
8b920789
AM
78 &General::writehasharray("$clients", \%clientshash);
79 system("/usr/local/bin/captivectrl");
80 &General::log("Captive", "Internet Access granted via license-agreement for $ip_address until $clientshash{$key}[3]");
81 $redir=1;
82 }
83}
84
85if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive activate'}"){
86 my $ip_address;
87 my $mac_address;
5dc32e58 88 my $granted=0;
8b920789
AM
89 #Convert voucherinput to uppercase
90 $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'};
91 #Get Clients IP-Address
92 $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
93 #Ask arp to give the corresponding MAC-Address
94 $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4);
95 $mac_address =~ s/\n+\z//;
96 #Check if voucher is valid and write client to clients file, delete voucher from voucherout
97 &General::readhasharray("$voucherout", \%voucherhash);
98 &General::readhasharray("$clients", \%clientshash);
99 foreach my $key (keys %voucherhash) {
100 if($voucherhash{$key}[1] eq $cgiparams{'VOUCHER'}){
101 #Voucher valid, write to clients, then delete from voucherout
8b920789 102 my $key1 = &General::findhasharraykey(\%clientshash);
e01c5ab7
AM
103 foreach my $i (0 .. 5) { $clientshash{$key1}[$i] = "";}
104
8b920789
AM
105 $clientshash{$key1}[0] = $mac_address;
106 $clientshash{$key1}[1] = $ip_address;
e01c5ab7 107 $clientshash{$key1}[2] = time();
c7e78cc6 108 $clientshash{$key1}[3] = $voucherhash{$key}[2];
8b920789 109 $clientshash{$key1}[4] = $cgiparams{'VOUCHER'};
c7e78cc6 110 $clientshash{$key1}[5] = HTML::Entities::decode_entities($voucherhash{$key}[3]);
e01c5ab7 111
8b920789 112 &General::writehasharray("$clients", \%clientshash);
8b920789
AM
113 &General::log("Captive", "Internet Access granted via voucher no. $clientshash{$key1}[4] for $ip_address until $clientshash{$key}[3] Remark: $clientshash{$key1}[7]");
114
115 delete $voucherhash{$key};
116 &General::writehasharray("$voucherout", \%voucherhash);
5dc32e58 117 $granted=1;
8b920789
AM
118 last;
119 }
120 }
5dc32e58
AM
121 if($granted==1){
122 system("/usr/local/bin/captivectrl");
123 $redir=1;
124 }else{
125 $errormessage="$Lang::tr{'Captive invalid_voucher'}";
126 }
8b920789
AM
127}
128
129if($redir == 1){
130 print "Status: 302 Moved Temporarily\n";
131 print "Location: $url\n";
132 print "Connection: close\n";
133 print "\n";
134 exit 0;
135}
8b920789
AM
136
137#Open HTML Page, load header and css
a2c26388
MT
138my $tmpl = HTML::Template->new(
139 filename => "/srv/web/ipfire/html/captive/template.html",
140 die_on_bad_params => 0
141);
8b920789 142
a2c26388 143$tmpl->param(REDIRECT_URL => $url);
8b920789 144
a2c26388
MT
145$tmpl->param(AUTH => $settings{'AUTH'});
146$tmpl->param(TITLE => $settings{'TITLE'});
147$tmpl->param(ERROR => $errormessage);
e01c5ab7 148
a2c26388
MT
149# Print header
150print "Pragma: no-cache\n";
151print "Cache-control: no-cache\n";
152print "Connection: close\n";
153print "Content-type: text/html\n\n";
8b920789 154
a2c26388
MT
155# Print rendered template
156print $tmpl->output;
8b920789
AM
157
158sub getcgihash {
159 my ($hash, $params) = @_;
160 my $cgi = CGI->new ();
161 $hash->{'__CGI__'} = $cgi;
162 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
163 if (!$params->{'wantfile'}) {
164 $CGI::DISABLE_UPLOADS = 1;
165 $CGI::POST_MAX = 1024 * 1024;
166 } else {
167 $CGI::POST_MAX = 10 * 1024 * 1024;
168 }
169 $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/;
170 my $referer = $1;
171 $cgi->url() =~ m/^http?\:\/\/([^\/]+)/;
172 my $servername = $1;
173 return if ($referer ne $servername);
174
175 ### Modified for getting multi-vars, split by |
176 my %temp = $cgi->Vars();
177 foreach my $key (keys %temp) {
178 $hash->{$key} = $temp{$key};
179 $hash->{$key} =~ s/\0/|/g;
180 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
181 }
182
183 if (($params->{'wantfile'})&&($params->{'filevar'})) {
184 $hash->{$params->{'filevar'}} = $cgi->upload
185 ($params->{'filevar'});
186 }
187 return;
188}
189
190sub getagb(){
191 open( my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
192 while(<$handle>){
193 $_ = HTML::Entities::decode_entities($_);
194 print $_;
195 }
196 close( $handle );
197}