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