]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/captive/index.cgi
captive: Redesign clients list box
[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
97b91e8a
MT
35my $coupons = "${General::swroot}/captive/coupons";
36my %couponhash = ();
37
8b920789
AM
38my %clientshash=();
39my %cgiparams=();
40my %settings=();
8b920789
AM
41my $clients="${General::swroot}/captive/clients";
42my $settingsfile="${General::swroot}/captive/settings";
8b920789
AM
43my $errormessage;
44my $url=param('redirect');
e01c5ab7 45
8b920789
AM
46#Create /var/ipfire/captive/clients if not exist
47unless (-f $clients){ system("touch $clients"); }
48
49#Get GUI variables
50&getcgihash(\%cgiparams);
51
52#Read settings
53&General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
54
966971e5
MT
55# Actions
56if ($cgiparams{'ACTION'} eq "SUBMIT") {
4b33d29d
MT
57 # Get client IP address
58 my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR};
8b920789 59
dbfd2622
MT
60 # Retrieve the MAC address from the ARP table
61 my $mac_address = &Network::get_hardware_address($ip_address);
8b920789
AM
62
63 &General::readhasharray("$clients", \%clientshash);
11fc9575 64 my $key = &General::findhasharraykey(\%clientshash);
8b920789 65
4b33d29d
MT
66 # Create a new client line
67 foreach my $i (0 .. 5) { $clientshash{$key}[$i] = ""; }
e01c5ab7 68
4b33d29d
MT
69 # MAC address of the client
70 $clientshash{$key}[0] = $mac_address;
e01c5ab7 71
4b33d29d
MT
72 # IP address of the client
73 $clientshash{$key}[1] = $ip_address;
8b920789 74
4b33d29d
MT
75 # Current time
76 $clientshash{$key}[2] = time();
77
97b91e8a
MT
78 if ($settings{"AUTH"} eq "COUPON") {
79 &General::readhasharray($coupons, \%couponhash);
4b33d29d 80
97b91e8a
MT
81 # Convert coupon input to uppercase
82 $cgiparams{'COUPON'} = uc $cgiparams{'COUPON'};
4b33d29d 83
97b91e8a 84 # Walk through all valid coupons and find the right one
4b33d29d 85 my $found = 0;
97b91e8a
MT
86 foreach my $coupon (keys %couponhash) {
87 if ($couponhash{$coupon}[1] eq $cgiparams{'COUPON'}) {
4b33d29d
MT
88 $found = 1;
89
90 # Copy expiry time
97b91e8a 91 $clientshash{$key}[3] = $couponhash{$coupon}[2];
4b33d29d 92
97b91e8a
MT
93 # Save coupon code
94 $clientshash{$key}[4] = $cgiparams{'COUPON'};
4b33d29d 95
97b91e8a
MT
96 # Copy coupon remark
97 $clientshash{$key}[5] = $couponhash{$coupon}[3];
4b33d29d 98
97b91e8a
MT
99 # Delete used coupon
100 delete $couponhash{$coupon};
101 &General::writehasharray($coupons, \%couponhash);
4b33d29d
MT
102
103 last;
104 }
105 }
106
107 if ($found == 1) {
97b91e8a 108 &General::log("Captive", "Internet access granted via coupon ($clientshash{$key}[4]) for $ip_address until $clientshash{$key}[3]");
4b33d29d 109 } else {
97b91e8a 110 $errormessage = $Lang::tr{"Captive invalid coupon"};
8b920789 111 }
4b33d29d
MT
112
113 # License
114 } else {
278309b9
MT
115 # Copy session expiry time
116 $clientshash{$key}[3] = $settings{'SESSION_TIME'} || "0";
4b33d29d 117
97b91e8a 118 # No coupon code
41964aba 119 $clientshash{$key}[4] = "TERMS";
4b33d29d
MT
120
121 &General::log("Captive", "Internet access granted via license agreement for $ip_address until $clientshash{$key}[3]");
8b920789 122 }
4b33d29d
MT
123
124 # If no errors were found, save configruation and reload
125 if (!$errormessage) {
126 &General::writehasharray("$clients", \%clientshash);
127
5dc32e58 128 system("/usr/local/bin/captivectrl");
8b920789 129
4b33d29d
MT
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";
134 exit 0;
135 }
8b920789 136}
8b920789 137
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
48fb1d3b 145# Voucher
97b91e8a
MT
146if ($settings{'AUTH'} eq "COUPON") {
147 $tmpl->param(COUPON => 1);
48fb1d3b
MT
148}
149
a2c26388 150$tmpl->param(TITLE => $settings{'TITLE'});
f8d35875 151$tmpl->param(COLOR => $settings{'COLOR'});
a2c26388 152$tmpl->param(ERROR => $errormessage);
e01c5ab7 153
9735e167 154$tmpl->param(TERMS => &getterms());
48fb1d3b
MT
155
156# Some translated strings
157$tmpl->param(L_ACTIVATE => $Lang::tr{'Captive ACTIVATE'});
158$tmpl->param(L_GAIN_ACCESS => $Lang::tr{'Captive GAIN ACCESS'});
97b91e8a
MT
159$tmpl->param(L_HEADING_COUPON => $Lang::tr{'Captive coupon'});
160$tmpl->param(L_HEADING_TERMS => $Lang::tr{'Captive terms'});
48fb1d3b
MT
161$tmpl->param(L_AGREE_TAC => $Lang::tr{'Captive agree tac'});
162
a2c26388
MT
163# Print header
164print "Pragma: no-cache\n";
165print "Cache-control: no-cache\n";
166print "Connection: close\n";
167print "Content-type: text/html\n\n";
8b920789 168
a2c26388 169# Print rendered template
48fb1d3b 170print $tmpl->output();
8b920789
AM
171
172sub getcgihash {
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;
180 } else {
181 $CGI::POST_MAX = 10 * 1024 * 1024;
182 }
183 $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/;
184 my $referer = $1;
185 $cgi->url() =~ m/^http?\:\/\/([^\/]+)/;
186 my $servername = $1;
187 return if ($referer ne $servername);
188
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/;
195 }
196
197 if (($params->{'wantfile'})&&($params->{'filevar'})) {
198 $hash->{$params->{'filevar'}} = $cgi->upload
199 ($params->{'filevar'});
200 }
201 return;
202}
203
9735e167
MT
204sub getterms() {
205 my @terms = ();
48fb1d3b 206
9735e167 207 open(my $handle, "<:utf8", "/var/ipfire/captive/terms.txt");
48fb1d3b
MT
208 while(<$handle>) {
209 $_ = HTML::Entities::decode_entities($_);
9735e167 210 push(@terms, $_);
48fb1d3b
MT
211 }
212 close($handle);
213
9735e167 214 my $terms = join("\n", @terms);
48fb1d3b
MT
215
216 # Format paragraphs
9735e167 217 $terms =~ s/\n\n/<\/p>\n<p>/g;
48fb1d3b 218
9735e167 219 return $terms;
8b920789 220}