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