]> git.ipfire.org Git - people/dweismueller/ipfire-2.x.git/blame - html/cgi-bin/captive/index.cgi
Captive-Portal: add Errormessage when wrong code is entered
[people/dweismueller/ipfire-2.x.git] / html / cgi-bin / captive / index.cgi
CommitLineData
a2ad8b77
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();
16fd278a 26
a2ad8b77
AM
27# enable only the following on debugging purpose
28#use warnings;
29#use CGI::Carp 'fatalsToBrowser';
30
31require '/var/ipfire/general-functions.pl';
32require "${General::swroot}/lang.pl";
33
34#Set Variables
35my %voucherhash=();
36my %clientshash=();
37my %cgiparams=();
38my %settings=();
39my $voucherout="${General::swroot}/captive/voucher_out";
40my $clients="${General::swroot}/captive/clients";
41my $settingsfile="${General::swroot}/captive/settings";
42my $redir=0;
43my $errormessage;
44my $url=param('redirect');
16fd278a 45
a2ad8b77
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
55#Actions
56if ($cgiparams{'ACTION'} eq "$Lang::tr{'gpl i accept these terms and conditions'}"){
57 my $key = &General::findhasharraykey(\%clientshash);
a2ad8b77
AM
58
59 #Get Clients IP-Address
60 my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
61
62 #Ask arp to give the corresponding MAC-Address
63 my $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4);
64 $mac_address =~ s/\n+\z//;
65
66 &General::readhasharray("$clients", \%clientshash);
67
68 if (!$errormessage){
16fd278a
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
a2ad8b77
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;
e7d556dc 88 my $granted=0;
a2ad8b77
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
a2ad8b77 102 my $key1 = &General::findhasharraykey(\%clientshash);
16fd278a
AM
103 foreach my $i (0 .. 5) { $clientshash{$key1}[$i] = "";}
104
a2ad8b77
AM
105 $clientshash{$key1}[0] = $mac_address;
106 $clientshash{$key1}[1] = $ip_address;
16fd278a 107 $clientshash{$key1}[2] = time();
9dadbe4b 108 $clientshash{$key1}[3] = $voucherhash{$key}[2];
a2ad8b77 109 $clientshash{$key1}[4] = $cgiparams{'VOUCHER'};
9dadbe4b 110 $clientshash{$key1}[5] = HTML::Entities::decode_entities($voucherhash{$key}[3]);
16fd278a 111
a2ad8b77 112 &General::writehasharray("$clients", \%clientshash);
a2ad8b77
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);
e7d556dc 117 $granted=1;
a2ad8b77
AM
118 last;
119 }
120 }
e7d556dc
AM
121 if($granted==1){
122 system("/usr/local/bin/captivectrl");
123 $redir=1;
124 }else{
125 $errormessage="$Lang::tr{'Captive invalid_voucher'}";
126 }
a2ad8b77
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}
a2ad8b77
AM
136
137#Open HTML Page, load header and css
138&head();
139&error();
140&start();
141
142#Functions
a2ad8b77
AM
143sub start(){
144 if ($settings{'AUTH'} eq 'VOUCHER'){
145 &voucher();
146 }else{
147 &agb();
148 }
149}
150
151sub error(){
152 if ($errormessage){
e7d556dc 153 print "<center><div class='title'><br><font color='red'>$errormessage</font><br></div><br>";
a2ad8b77
AM
154 }
155}
156
157sub head(){
158print<<END
159Content-type: text/html\n\n
160<html>
161 <head>
162 <meta charset="utf-8">
163 <title>$settings{'TITLE'}</title>
164 <link href="../assets/captive.css" type="text/css" rel="stylesheet">
165 </head>
e7d556dc 166 <body>
a2ad8b77
AM
167END
168;
169}
16fd278a 170
a2ad8b77
AM
171sub agb(){
172print<<END
a2ad8b77
AM
173 <center>
174 <div class="title">
83c6e6ec 175 <h1>$settings{'TITLE'}</h1>
a2ad8b77
AM
176 </div>
177 <br>
178 <div class="agb">
179 <textarea style="width:100%;" rows='40'>
180END
181;
182&getagb();
183print<<END
184 </textarea>
185 <center>
186 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
187 <br><input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'gpl i accept these terms and conditions'}"/>
188 </form>
189 </center>
190 </div>
191 </center>
192 </body>
193 </html>
194END
195;
196}
197
198sub voucher(){
199 print<<END
a2ad8b77
AM
200 <center>
201 <div class="title">
83c6e6ec 202 <h1>$settings{'TITLE'}</h1>
a2ad8b77
AM
203 </div>
204 <br>
205 <div class="login">
206END
207;
208
209print<<END
210 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
211 <center>
212 <table>
213 <tr>
214 <td>
215 <b>$Lang::tr{'Captive voucher'}</b>&nbsp<input type='text' maxlength="8" size='10' style="font-size: 24px;font-weight: bold;" name='VOUCHER'>
216 </td>
217 <td>
1dd640c0 218 <input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'Captive activate'}"/>
a2ad8b77
AM
219 </td>
220 </tr>
221 </table>
222 </form>
223 </div>
224 <br>
225 <div class="agb">
226 <textarea style="width:100%;" rows='40'>
227END
228;
229&getagb();
230print<<END
231 </textarea>
232 <br><br>
233 </div>
234 </body>
235 </html>
236END
237;
238}
239
240sub getcgihash {
241 my ($hash, $params) = @_;
242 my $cgi = CGI->new ();
243 $hash->{'__CGI__'} = $cgi;
244 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
245 if (!$params->{'wantfile'}) {
246 $CGI::DISABLE_UPLOADS = 1;
247 $CGI::POST_MAX = 1024 * 1024;
248 } else {
249 $CGI::POST_MAX = 10 * 1024 * 1024;
250 }
251 $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/;
252 my $referer = $1;
253 $cgi->url() =~ m/^http?\:\/\/([^\/]+)/;
254 my $servername = $1;
255 return if ($referer ne $servername);
256
257 ### Modified for getting multi-vars, split by |
258 my %temp = $cgi->Vars();
259 foreach my $key (keys %temp) {
260 $hash->{$key} = $temp{$key};
261 $hash->{$key} =~ s/\0/|/g;
262 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
263 }
264
265 if (($params->{'wantfile'})&&($params->{'filevar'})) {
266 $hash->{$params->{'filevar'}} = $cgi->upload
267 ($params->{'filevar'});
268 }
269 return;
270}
271
272sub getagb(){
273 open( my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
274 while(<$handle>){
275 $_ = HTML::Entities::decode_entities($_);
276 print $_;
277 }
278 close( $handle );
279}