]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | use strict; | |
23 | use CGI ':standard'; | |
24 | use URI::Escape; | |
25 | use HTML::Entities(); | |
e01c5ab7 | 26 | |
8b920789 AM |
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'); | |
e01c5ab7 | 45 | |
8b920789 AM |
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'}"){ | |
8b920789 AM |
57 | #Get Clients IP-Address |
58 | my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||""; | |
59 | ||
60 | #Ask arp to give the corresponding MAC-Address | |
61 | my $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4); | |
62 | $mac_address =~ s/\n+\z//; | |
63 | ||
64 | &General::readhasharray("$clients", \%clientshash); | |
11fc9575 | 65 | my $key = &General::findhasharraykey(\%clientshash); |
8b920789 AM |
66 | |
67 | if (!$errormessage){ | |
e01c5ab7 AM |
68 | foreach my $i (0 .. 5) { $clientshash{$key}[$i] = "";} |
69 | ||
70 | $clientshash{$key}[0] = $mac_address; #mac address of actual client | |
71 | $clientshash{$key}[1] = $ip_address; #ip address of actual client | |
72 | $clientshash{$key}[2] = time(); #actual time in unix seconds (timestamp of first conenction) | |
73 | $clientshash{$key}[3] = $settings{'EXPIRE'}; #Expire time in seconds (1day, 1 week ....) | |
74 | $clientshash{$key}[4] = $Lang::tr{'Captive auth_lic'}; #Type of license (license or voucher) | |
75 | $clientshash{$key}[5] = ''; | |
76 | ||
8b920789 AM |
77 | &General::writehasharray("$clients", \%clientshash); |
78 | system("/usr/local/bin/captivectrl"); | |
79 | &General::log("Captive", "Internet Access granted via license-agreement for $ip_address until $clientshash{$key}[3]"); | |
80 | $redir=1; | |
81 | } | |
82 | } | |
83 | ||
84 | if ($cgiparams{'ACTION'} eq "$Lang::tr{'Captive activate'}"){ | |
85 | my $ip_address; | |
86 | my $mac_address; | |
5dc32e58 | 87 | my $granted=0; |
8b920789 AM |
88 | #Convert voucherinput to uppercase |
89 | $cgiparams{'VOUCHER'} = uc $cgiparams{'VOUCHER'}; | |
90 | #Get Clients IP-Address | |
91 | $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} ||""; | |
92 | #Ask arp to give the corresponding MAC-Address | |
93 | $mac_address = qx(arp -a|grep $ip_address|cut -d ' ' -f 4); | |
94 | $mac_address =~ s/\n+\z//; | |
95 | #Check if voucher is valid and write client to clients file, delete voucher from voucherout | |
96 | &General::readhasharray("$voucherout", \%voucherhash); | |
97 | &General::readhasharray("$clients", \%clientshash); | |
98 | foreach my $key (keys %voucherhash) { | |
99 | if($voucherhash{$key}[1] eq $cgiparams{'VOUCHER'}){ | |
100 | #Voucher valid, write to clients, then delete from voucherout | |
8b920789 | 101 | my $key1 = &General::findhasharraykey(\%clientshash); |
e01c5ab7 AM |
102 | foreach my $i (0 .. 5) { $clientshash{$key1}[$i] = "";} |
103 | ||
8b920789 AM |
104 | $clientshash{$key1}[0] = $mac_address; |
105 | $clientshash{$key1}[1] = $ip_address; | |
e01c5ab7 | 106 | $clientshash{$key1}[2] = time(); |
c7e78cc6 | 107 | $clientshash{$key1}[3] = $voucherhash{$key}[2]; |
8b920789 | 108 | $clientshash{$key1}[4] = $cgiparams{'VOUCHER'}; |
c7e78cc6 | 109 | $clientshash{$key1}[5] = HTML::Entities::decode_entities($voucherhash{$key}[3]); |
e01c5ab7 | 110 | |
8b920789 | 111 | &General::writehasharray("$clients", \%clientshash); |
8b920789 AM |
112 | &General::log("Captive", "Internet Access granted via voucher no. $clientshash{$key1}[4] for $ip_address until $clientshash{$key}[3] Remark: $clientshash{$key1}[7]"); |
113 | ||
114 | delete $voucherhash{$key}; | |
115 | &General::writehasharray("$voucherout", \%voucherhash); | |
5dc32e58 | 116 | $granted=1; |
8b920789 AM |
117 | last; |
118 | } | |
119 | } | |
5dc32e58 AM |
120 | if($granted==1){ |
121 | system("/usr/local/bin/captivectrl"); | |
122 | $redir=1; | |
123 | }else{ | |
124 | $errormessage="$Lang::tr{'Captive invalid_voucher'}"; | |
125 | } | |
8b920789 AM |
126 | } |
127 | ||
128 | if($redir == 1){ | |
129 | print "Status: 302 Moved Temporarily\n"; | |
130 | print "Location: $url\n"; | |
131 | print "Connection: close\n"; | |
132 | print "\n"; | |
133 | exit 0; | |
134 | } | |
8b920789 AM |
135 | |
136 | #Open HTML Page, load header and css | |
137 | &head(); | |
138 | &error(); | |
139 | &start(); | |
140 | ||
141 | #Functions | |
8b920789 AM |
142 | sub start(){ |
143 | if ($settings{'AUTH'} eq 'VOUCHER'){ | |
144 | &voucher(); | |
145 | }else{ | |
146 | &agb(); | |
147 | } | |
148 | } | |
149 | ||
150 | sub error(){ | |
151 | if ($errormessage){ | |
5dc32e58 | 152 | print "<center><div class='title'><br><font color='red'>$errormessage</font><br></div><br>"; |
8b920789 AM |
153 | } |
154 | } | |
155 | ||
156 | sub head(){ | |
157 | print<<END | |
158 | Content-type: text/html\n\n | |
159 | <html> | |
160 | <head> | |
161 | <meta charset="utf-8"> | |
162 | <title>$settings{'TITLE'}</title> | |
163 | <link href="../assets/captive.css" type="text/css" rel="stylesheet"> | |
164 | </head> | |
5dc32e58 | 165 | <body> |
8b920789 AM |
166 | END |
167 | ; | |
168 | } | |
e01c5ab7 | 169 | |
8b920789 AM |
170 | sub agb(){ |
171 | print<<END | |
8b920789 AM |
172 | <center> |
173 | <div class="title"> | |
e14adf75 | 174 | <h1>$settings{'TITLE'}</h1> |
8b920789 AM |
175 | </div> |
176 | <br> | |
177 | <div class="agb"> | |
178 | <textarea style="width:100%;" rows='40'> | |
179 | END | |
180 | ; | |
181 | &getagb(); | |
182 | print<<END | |
183 | </textarea> | |
184 | <center> | |
185 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
186 | <br><input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'gpl i accept these terms and conditions'}"/> | |
187 | </form> | |
188 | </center> | |
189 | </div> | |
190 | </center> | |
191 | </body> | |
192 | </html> | |
193 | END | |
194 | ; | |
195 | } | |
196 | ||
197 | sub voucher(){ | |
198 | print<<END | |
8b920789 AM |
199 | <center> |
200 | <div class="title"> | |
e14adf75 | 201 | <h1>$settings{'TITLE'}</h1> |
8b920789 AM |
202 | </div> |
203 | <br> | |
204 | <div class="login"> | |
205 | END | |
206 | ; | |
207 | ||
208 | print<<END | |
209 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
210 | <center> | |
211 | <table> | |
212 | <tr> | |
213 | <td> | |
214 | <b>$Lang::tr{'Captive voucher'}</b> <input type='text' maxlength="8" size='10' style="font-size: 24px;font-weight: bold;" name='VOUCHER'> | |
215 | </td> | |
216 | <td> | |
facfdcd0 | 217 | <input type='hidden' name='redirect' value ='$url'><input type='submit' name='ACTION' value="$Lang::tr{'Captive activate'}"/> |
8b920789 AM |
218 | </td> |
219 | </tr> | |
220 | </table> | |
221 | </form> | |
222 | </div> | |
223 | <br> | |
224 | <div class="agb"> | |
225 | <textarea style="width:100%;" rows='40'> | |
226 | END | |
227 | ; | |
228 | &getagb(); | |
229 | print<<END | |
230 | </textarea> | |
231 | <br><br> | |
232 | </div> | |
233 | </body> | |
234 | </html> | |
235 | END | |
236 | ; | |
237 | } | |
238 | ||
239 | sub getcgihash { | |
240 | my ($hash, $params) = @_; | |
241 | my $cgi = CGI->new (); | |
242 | $hash->{'__CGI__'} = $cgi; | |
243 | return if ($ENV{'REQUEST_METHOD'} ne 'POST'); | |
244 | if (!$params->{'wantfile'}) { | |
245 | $CGI::DISABLE_UPLOADS = 1; | |
246 | $CGI::POST_MAX = 1024 * 1024; | |
247 | } else { | |
248 | $CGI::POST_MAX = 10 * 1024 * 1024; | |
249 | } | |
250 | $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/; | |
251 | my $referer = $1; | |
252 | $cgi->url() =~ m/^http?\:\/\/([^\/]+)/; | |
253 | my $servername = $1; | |
254 | return if ($referer ne $servername); | |
255 | ||
256 | ### Modified for getting multi-vars, split by | | |
257 | my %temp = $cgi->Vars(); | |
258 | foreach my $key (keys %temp) { | |
259 | $hash->{$key} = $temp{$key}; | |
260 | $hash->{$key} =~ s/\0/|/g; | |
261 | $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/; | |
262 | } | |
263 | ||
264 | if (($params->{'wantfile'})&&($params->{'filevar'})) { | |
265 | $hash->{$params->{'filevar'}} = $cgi->upload | |
266 | ($params->{'filevar'}); | |
267 | } | |
268 | return; | |
269 | } | |
270 | ||
271 | sub getagb(){ | |
272 | open( my $handle, "<:utf8", "/var/ipfire/captive/agb.txt" ) or die("$!"); | |
273 | while(<$handle>){ | |
274 | $_ = HTML::Entities::decode_entities($_); | |
275 | print $_; | |
276 | } | |
277 | close( $handle ); | |
278 | } |