]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/captive/index.cgi
Captive-Portal: add web-part
[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 # enable only the following on debugging purpose
27 #use warnings;
28 #use CGI::Carp 'fatalsToBrowser';
29
30 require '/var/ipfire/general-functions.pl';
31 require "${General::swroot}/lang.pl";
32
33 #Set Variables
34 my %voucherhash=();
35 my %clientshash=();
36 my %cgiparams=();
37 my %settings=();
38 my $voucherout="${General::swroot}/captive/voucher_out";
39 my $clients="${General::swroot}/captive/clients";
40 my $settingsfile="${General::swroot}/captive/settings";
41 my $redir=0;
42 my $errormessage;
43 my $url=param('redirect');
44 #Create /var/ipfire/captive/clients if not exist
45 unless (-f $clients){ system("touch $clients"); }
46
47 #Get GUI variables
48 &getcgihash(\%cgiparams);
49
50 #Read settings
51 &General::readhash("$settingsfile", \%settings) if(-f $settingsfile);
52
53 #Actions
54 if ($cgiparams{'ACTION'} eq "$Lang::tr{'gpl i accept these terms and conditions'}"){
55 my $key = &General::findhasharraykey(\%clientshash);
56 my($sec,$min,$hour) = gmtime(time);
57 my $hour1=$hour+$settings{'TIME'};
58 $min="0".$min if ($min < 10);
59 $hour="0".$hour if ($hour < 10);
60 $hour1="0".$hour1 if ($hour1 < 10);
61
62 #Get Clients IP-Address
63 my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
64
65 #Ask arp to give the corresponding MAC-Address
66 my $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4);
67 $mac_address =~ s/\n+\z//;
68
69 &General::readhasharray("$clients", \%clientshash);
70
71 if (!$errormessage){
72 foreach my $i (0 .. 6) { $clientshash{$key}[$i] = "";}
73 $clientshash{$key}[0] = $mac_address;
74 $clientshash{$key}[1] = $ip_address;
75 $clientshash{$key}[2] = $hour.":".$min;
76 $clientshash{$key}[3] = $hour1.":".$min;
77 $clientshash{$key}[4] = $Lang::tr{'Captive auth_lic'};
78 $clientshash{$key}[5] = $settings{'TIME'};
79 $clientshash{$key}[6] = time();
80
81 &General::writehasharray("$clients", \%clientshash);
82 system("/usr/local/bin/captivectrl");
83 &General::log("Captive", "Internet Access granted via license-agreement for $ip_address until $clientshash{$key}[3]");
84 $redir=1;
85 }
86 }
87
88 if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive activate'}"){
89 my $ip_address;
90 my $mac_address;
91
92 #Convert voucherinput to uppercase
93 $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'};
94 #Get Clients IP-Address
95 $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||"";
96 #Ask arp to give the corresponding MAC-Address
97 $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4);
98 $mac_address =~ s/\n+\z//;
99 #Check if voucher is valid and write client to clients file, delete voucher from voucherout
100 &General::readhasharray("$voucherout", \%voucherhash);
101 &General::readhasharray("$clients", \%clientshash);
102 foreach my $key (keys %voucherhash) {
103 if($voucherhash{$key}[1] eq $cgiparams{'VOUCHER'}){
104 #Voucher valid, write to clients, then delete from voucherout
105 my ($sec,$min,$hour)=gmtime(time());
106 my $hour1;
107 $min="0".$min if ($min < 10);
108 $hour="0".$hour if ($hour < 10);
109 $hour1=$hour+$voucherhash{$key}[2];
110 $hour1="0".$hour1 if ($hour1 < 10);
111 my $key1 = &General::findhasharraykey(\%clientshash);
112 foreach my $i (0 .. 7) { $clientshash{$key1}[$i] = "";}
113 $clientshash{$key1}[0] = $mac_address;
114 $clientshash{$key1}[1] = $ip_address;
115 $clientshash{$key1}[2] = $hour.":".$min;
116 $clientshash{$key1}[3] = $hour1.":".$min;
117 $clientshash{$key1}[4] = $cgiparams{'VOUCHER'};
118 $clientshash{$key1}[5] = $voucherhash{$key}[2];
119 $clientshash{$key1}[6] = time();
120 $clientshash{$key1}[7] = $voucherhash{$key}[4];
121
122 &General::writehasharray("$clients", \%clientshash);
123 $clientshash{$key1}[7]=HTML::Entities::decode_entities($clientshash{$key1}[7]);
124 &General::log("Captive", "Internet Access granted via voucher no. $clientshash{$key1}[4] for $ip_address until $clientshash{$key}[3] Remark: $clientshash{$key1}[7]");
125
126 delete $voucherhash{$key};
127 &General::writehasharray("$voucherout", \%voucherhash);
128 last;
129 }
130 }
131 system("/usr/local/bin/captivectrl");
132 $redir=1;
133 }
134
135 if($redir == 1){
136 print "Status: 302 Moved Temporarily\n";
137 print "Location: $url\n";
138 print "Connection: close\n";
139 print "\n";
140 exit 0;
141 }
142
143
144 #Open HTML Page, load header and css
145 &head();
146 &error();
147 &start();
148
149 #Functions
150
151 sub start(){
152 if ($settings{'AUTH'} eq 'VOUCHER'){
153 &voucher();
154 }else{
155 &agb();
156 }
157 }
158
159 sub error(){
160 if ($errormessage){
161 print "<div id='title'><br>$errormessage<br></diV>";
162 }
163 }
164
165 sub head(){
166 print<<END
167 Content-type: text/html\n\n
168 <html>
169 <head>
170 <meta charset="utf-8">
171 <title>$settings{'TITLE'}</title>
172 <link href="../assets/captive.css" type="text/css" rel="stylesheet">
173 </head>
174 END
175 ;
176 }
177 sub agb(){
178 print<<END
179 <body>
180 <center>
181 <div class="title">
182 <h1>$settings{'TITLE'}
183 </div>
184 <br>
185 <div class="agb">
186 <textarea style="width:100%;" rows='40'>
187 END
188 ;
189 &getagb();
190 print<<END
191 </textarea>
192 <center>
193 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
194 <br><input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'gpl i accept these terms and conditions'}"/>
195 </form>
196 </center>
197 </div>
198 </center>
199 </body>
200 </html>
201 END
202 ;
203 }
204
205 sub voucher(){
206 print<<END
207 <body>
208 <center>
209 <div class="title">
210 <h1>LOGIN</h1>
211 </div>
212 <br>
213 <div class="login">
214 END
215 ;
216
217 print<<END
218 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
219 <center>
220 <table>
221 <tr>
222 <td>
223 <b>$Lang::tr{'Captive voucher'}</b>&nbsp<input type='text' maxlength="8" size='10' style="font-size: 24px;font-weight: bold;" name='VOUCHER'>
224 </td>
225 <td>
226 <input type='submit' name='ACTION' value="$Lang::tr{'Captive activate'}"/>
227 </td>
228 </tr>
229 </table>
230 </form>
231 </div>
232 <br>
233 <div class="agb">
234 <textarea style="width:100%;" rows='40'>
235 END
236 ;
237 &getagb();
238 print<<END
239 </textarea>
240 <br><br>
241 </div>
242 </body>
243 </html>
244 END
245 ;
246 }
247
248 sub getcgihash {
249 my ($hash, $params) = @_;
250 my $cgi = CGI->new ();
251 $hash->{'__CGI__'} = $cgi;
252 return if ($ENV{'REQUEST_METHOD'} ne 'POST');
253 if (!$params->{'wantfile'}) {
254 $CGI::DISABLE_UPLOADS = 1;
255 $CGI::POST_MAX = 1024 * 1024;
256 } else {
257 $CGI::POST_MAX = 10 * 1024 * 1024;
258 }
259 $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/;
260 my $referer = $1;
261 $cgi->url() =~ m/^http?\:\/\/([^\/]+)/;
262 my $servername = $1;
263 return if ($referer ne $servername);
264
265 ### Modified for getting multi-vars, split by |
266 my %temp = $cgi->Vars();
267 foreach my $key (keys %temp) {
268 $hash->{$key} = $temp{$key};
269 $hash->{$key} =~ s/\0/|/g;
270 $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/;
271 }
272
273 if (($params->{'wantfile'})&&($params->{'filevar'})) {
274 $hash->{$params->{'filevar'}} = $cgi->upload
275 ($params->{'filevar'});
276 }
277 return;
278 }
279
280 sub getagb(){
281 open( my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!");
282 while(<$handle>){
283 $_ = HTML::Entities::decode_entities($_);
284 print $_;
285 }
286 close( $handle );
287 }