]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/captive/index.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2016 Alexander Marx 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 ###############################################################################
28 # enable only the following on debugging purpose
30 #use CGI::Carp 'fatalsToBrowser';
32 require '/var/ipfire/general-functions.pl';
33 require "${General::swroot}/lang.pl";
40 my $voucherout="${General::swroot}/captive/voucher_out";
41 my $clients="${General::swroot}/captive/clients";
42 my $settingsfile="${General::swroot}/captive/settings";
44 my $url=param
('redirect');
46 #Create /var/ipfire/captive/clients if not exist
47 unless (-f
$clients){ system("touch $clients"); }
50 &getcgihash
(\
%cgiparams);
53 &General
::readhash
("$settingsfile", \
%settings) if(-f
$settingsfile);
56 if ($cgiparams{'ACTION'} eq "SUBMIT") {
57 # Get client IP address
58 my $ip_address = $ENV{X_FORWARDED_FOR
} || $ENV{REMOTE_ADDR
};
60 # Retrieve the MAC address from the ARP table
61 my $mac_address = &Network
::get_hardware_address
($ip_address);
63 &General
::readhasharray
("$clients", \
%clientshash);
64 my $key = &General
::findhasharraykey
(\
%clientshash);
66 # Create a new client line
67 foreach my $i (0 .. 5) { $clientshash{$key}[$i] = ""; }
69 # MAC address of the client
70 $clientshash{$key}[0] = $mac_address;
72 # IP address of the client
73 $clientshash{$key}[1] = $ip_address;
76 $clientshash{$key}[2] = time();
78 if ($settings{"AUTH"} eq "VOUCHER") {
79 &General
::readhasharray
("$voucherout", \
%voucherhash);
81 # Convert voucher input to uppercase
82 $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'};
84 # Walk through all valid vouchers and find the right one
86 foreach my $voucher (keys %voucherhash) {
87 if ($voucherhash{$voucher}[1] eq $cgiparams{'VOUCHER'}) {
91 $clientshash{$key}[3] = $voucherhash{$voucher}[2];
94 $clientshash{$key}[4] = $cgiparams{'VOUCHER'};
97 $clientshash{$key}[5] = $voucherhash{$voucher}[3];
100 delete $voucherhash{$voucher};
101 &General
::writehasharray
("$voucherout", \
%voucherhash);
108 &General
::log("Captive", "Internet access granted via voucher ($clientshash{$key}[4]) for $ip_address until $clientshash{$key}[3]");
110 $errormessage = $Lang::tr
{"Captive invalid_voucher"};
116 $clientshash{$key}[3] = $settings{'EXPIRE'};
119 $clientshash{$key}[4] = "LICENSE";
121 &General
::log("Captive", "Internet access granted via license agreement for $ip_address until $clientshash{$key}[3]");
124 # If no errors were found, save configruation and reload
125 if (!$errormessage) {
126 &General
::writehasharray
("$clients", \
%clientshash);
128 system("/usr/local/bin/captivectrl");
130 # Redirect client to the original URL
131 print "Status: 302 Moved Temporarily\n";
132 print "Location: $url\n";
133 print "Connection: close\n\n";
138 my $tmpl = HTML
::Template
->new(
139 filename
=> "/srv/web/ipfire/html/captive/template.html",
140 die_on_bad_params
=> 0
143 $tmpl->param(REDIRECT_URL
=> $url);
146 if ($settings{'AUTH'} eq "VOUCHER") {
147 $tmpl->param(VOUCHER
=> 1);
150 $tmpl->param(TITLE
=> $settings{'TITLE'});
151 $tmpl->param(COLOR
=> $settings{'COLOR'});
152 $tmpl->param(ERROR
=> $errormessage);
154 $tmpl->param(TAC
=> &gettac
());
156 # Some translated strings
157 $tmpl->param(L_ACTIVATE
=> $Lang::tr
{'Captive ACTIVATE'});
158 $tmpl->param(L_GAIN_ACCESS
=> $Lang::tr
{'Captive GAIN ACCESS'});
159 $tmpl->param(L_HEADING_TAC
=> $Lang::tr
{'Captive heading tac'});
160 $tmpl->param(L_HEADING_VOUCHER
=> $Lang::tr
{'Captive heading voucher'});
161 $tmpl->param(L_AGREE_TAC
=> $Lang::tr
{'Captive agree tac'});
164 print "Pragma: no-cache\n";
165 print "Cache-control: no-cache\n";
166 print "Connection: close\n";
167 print "Content-type: text/html\n\n";
169 # Print rendered template
170 print $tmpl->output();
173 my ($hash, $params) = @_;
174 my $cgi = CGI
->new ();
175 $hash->{'__CGI__'} = $cgi;
176 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
177 if (!$params->{'wantfile'}) {
178 $CGI::DISABLE_UPLOADS
= 1;
179 $CGI::POST_MAX
= 1024 * 1024;
181 $CGI::POST_MAX
= 10 * 1024 * 1024;
183 $cgi->referer() =~ m/^http?\:\/\
/([^\/]+)/;
185 $cgi->url() =~ m/^http?\:\/\
/([^\/]+)/;
187 return if ($referer ne $servername);
189 ### Modified for getting multi-vars, split by |
190 my %temp = $cgi->Vars();
191 foreach my $key (keys %temp) {
192 $hash->{$key} = $temp{$key};
193 $hash->{$key} =~ s/\0/|/g;
194 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
197 if (($params->{'wantfile'})&&($params->{'filevar'})) {
198 $hash->{$params->{'filevar'}} = $cgi->upload
199 ($params->{'filevar'});
207 open(my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
209 $_ = HTML
::Entities
::decode_entities
($_);
214 my $tac = join("\n", @tac);
217 $tac =~ s/\n\n/<\/p>\n<p
>/g
;