]>
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(); | |
a2c26388 | 26 | use HTML::Template; |
e01c5ab7 | 27 | |
8b920789 AM |
28 | # enable only the following on debugging purpose |
29 | #use warnings; | |
30 | #use CGI::Carp 'fatalsToBrowser'; | |
31 | ||
32 | require '/var/ipfire/general-functions.pl'; | |
33 | require "${General::swroot}/lang.pl"; | |
34 | ||
5f1b9511 MT |
35 | # Load the most appropriate language from the browser configuration |
36 | my @langs = &Lang::DetectBrowserLanguages(); | |
37 | &Lang::reload(@langs); | |
38 | ||
97b91e8a MT |
39 | my $coupons = "${General::swroot}/captive/coupons"; |
40 | my %couponhash = (); | |
41 | ||
8b920789 AM |
42 | my %clientshash=(); |
43 | my %cgiparams=(); | |
44 | my %settings=(); | |
8b920789 AM |
45 | my $clients="${General::swroot}/captive/clients"; |
46 | my $settingsfile="${General::swroot}/captive/settings"; | |
8b920789 AM |
47 | my $errormessage; |
48 | my $url=param('redirect'); | |
e01c5ab7 | 49 | |
8b920789 AM |
50 | #Create /var/ipfire/captive/clients if not exist |
51 | unless (-f $clients){ system("touch $clients"); } | |
52 | ||
53 | #Get GUI variables | |
54 | &getcgihash(\%cgiparams); | |
55 | ||
56 | #Read settings | |
57 | &General::readhash("$settingsfile", \%settings) if(-f $settingsfile); | |
58 | ||
966971e5 MT |
59 | # Actions |
60 | if ($cgiparams{'ACTION'} eq "SUBMIT") { | |
4b33d29d MT |
61 | # Get client IP address |
62 | my $ip_address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR}; | |
8b920789 | 63 | |
dbfd2622 MT |
64 | # Retrieve the MAC address from the ARP table |
65 | my $mac_address = &Network::get_hardware_address($ip_address); | |
8b920789 AM |
66 | |
67 | &General::readhasharray("$clients", \%clientshash); | |
11fc9575 | 68 | my $key = &General::findhasharraykey(\%clientshash); |
8b920789 | 69 | |
4b33d29d MT |
70 | # Create a new client line |
71 | foreach my $i (0 .. 5) { $clientshash{$key}[$i] = ""; } | |
e01c5ab7 | 72 | |
4b33d29d MT |
73 | # MAC address of the client |
74 | $clientshash{$key}[0] = $mac_address; | |
e01c5ab7 | 75 | |
4b33d29d MT |
76 | # IP address of the client |
77 | $clientshash{$key}[1] = $ip_address; | |
8b920789 | 78 | |
4b33d29d MT |
79 | # Current time |
80 | $clientshash{$key}[2] = time(); | |
81 | ||
97b91e8a MT |
82 | if ($settings{"AUTH"} eq "COUPON") { |
83 | &General::readhasharray($coupons, \%couponhash); | |
4b33d29d | 84 | |
aed55ef9 MT |
85 | if ($cgiparams{'COUPON'}) { |
86 | # Convert coupon input to uppercase | |
87 | $cgiparams{'COUPON'} = uc $cgiparams{'COUPON'}; | |
4b33d29d | 88 | |
aed55ef9 MT |
89 | # Walk through all valid coupons and find the right one |
90 | my $found = 0; | |
91 | foreach my $coupon (keys %couponhash) { | |
92 | if ($couponhash{$coupon}[1] eq $cgiparams{'COUPON'}) { | |
93 | $found = 1; | |
4b33d29d | 94 | |
aed55ef9 MT |
95 | # Copy expiry time |
96 | $clientshash{$key}[3] = $couponhash{$coupon}[2]; | |
4b33d29d | 97 | |
aed55ef9 MT |
98 | # Save coupon code |
99 | $clientshash{$key}[4] = $cgiparams{'COUPON'}; | |
4b33d29d | 100 | |
aed55ef9 MT |
101 | # Copy coupon remark |
102 | $clientshash{$key}[5] = $couponhash{$coupon}[3]; | |
4b33d29d | 103 | |
aed55ef9 MT |
104 | # Delete used coupon |
105 | delete $couponhash{$coupon}; | |
106 | &General::writehasharray($coupons, \%couponhash); | |
4b33d29d | 107 | |
aed55ef9 MT |
108 | last; |
109 | } | |
110 | } | |
111 | ||
112 | if ($found == 1) { | |
113 | &General::log("Captive", "Internet access granted via coupon ($clientshash{$key}[4]) for $ip_address until $clientshash{$key}[3]"); | |
114 | } else { | |
115 | $errormessage = $Lang::tr{"Captive invalid coupon"}; | |
4b33d29d | 116 | } |
4b33d29d | 117 | |
aed55ef9 | 118 | # No coupon given |
4b33d29d | 119 | } else { |
aed55ef9 | 120 | $errormessage = $Lang::tr{"Captive please enter a coupon code"}; |
8b920789 | 121 | } |
4b33d29d | 122 | |
497b9108 | 123 | # Terms |
4b33d29d | 124 | } else { |
497b9108 MT |
125 | # Make sure that they have been accepted |
126 | if ($cgiparams{'TERMS'} eq "on") { | |
127 | # Copy session expiry time | |
128 | $clientshash{$key}[3] = $settings{'SESSION_TIME'} || "0"; | |
4b33d29d | 129 | |
497b9108 MT |
130 | # No coupon code |
131 | $clientshash{$key}[4] = "TERMS"; | |
4b33d29d | 132 | |
497b9108 MT |
133 | &General::log("Captive", "Internet access granted via license agreement for $ip_address until $clientshash{$key}[3]"); |
134 | ||
135 | # The terms have not been accepted | |
136 | } else { | |
137 | $errormessage = $Lang::tr{'Captive please accept the terms and conditions'}; | |
138 | } | |
8b920789 | 139 | } |
4b33d29d MT |
140 | |
141 | # If no errors were found, save configruation and reload | |
142 | if (!$errormessage) { | |
143 | &General::writehasharray("$clients", \%clientshash); | |
144 | ||
5dc32e58 | 145 | system("/usr/local/bin/captivectrl"); |
8b920789 | 146 | |
4b33d29d MT |
147 | # Redirect client to the original URL |
148 | print "Status: 302 Moved Temporarily\n"; | |
149 | print "Location: $url\n"; | |
150 | print "Connection: close\n\n"; | |
151 | exit 0; | |
152 | } | |
8b920789 | 153 | } |
8b920789 | 154 | |
a2c26388 MT |
155 | my $tmpl = HTML::Template->new( |
156 | filename => "/srv/web/ipfire/html/captive/template.html", | |
157 | die_on_bad_params => 0 | |
158 | ); | |
8b920789 | 159 | |
963beecd | 160 | $tmpl->param(REDIRECT => $url); |
8b920789 | 161 | |
8b0679cc | 162 | # Coupon |
97b91e8a MT |
163 | if ($settings{'AUTH'} eq "COUPON") { |
164 | $tmpl->param(COUPON => 1); | |
8b0679cc MT |
165 | $tmpl->param(L_HEADING => $Lang::tr{'Captive coupon'}); |
166 | } else { | |
167 | $tmpl->param(L_HEADING => $Lang::tr{'Captive terms'}); | |
48fb1d3b MT |
168 | } |
169 | ||
a2c26388 | 170 | $tmpl->param(TITLE => $settings{'TITLE'}); |
f8d35875 | 171 | $tmpl->param(COLOR => $settings{'COLOR'}); |
a2c26388 | 172 | $tmpl->param(ERROR => $errormessage); |
e01c5ab7 | 173 | |
9735e167 | 174 | $tmpl->param(TERMS => &getterms()); |
48fb1d3b MT |
175 | |
176 | # Some translated strings | |
177 | $tmpl->param(L_ACTIVATE => $Lang::tr{'Captive ACTIVATE'}); | |
178 | $tmpl->param(L_GAIN_ACCESS => $Lang::tr{'Captive GAIN ACCESS'}); | |
8b0679cc | 179 | $tmpl->param(L_AGREE_TERMS => $Lang::tr{'Captive agree tac'}); |
48fb1d3b | 180 | |
a2c26388 MT |
181 | # Print header |
182 | print "Pragma: no-cache\n"; | |
183 | print "Cache-control: no-cache\n"; | |
184 | print "Connection: close\n"; | |
185 | print "Content-type: text/html\n\n"; | |
8b920789 | 186 | |
a2c26388 | 187 | # Print rendered template |
48fb1d3b | 188 | print $tmpl->output(); |
8b920789 AM |
189 | |
190 | sub getcgihash { | |
191 | my ($hash, $params) = @_; | |
192 | my $cgi = CGI->new (); | |
193 | $hash->{'__CGI__'} = $cgi; | |
194 | return if ($ENV{'REQUEST_METHOD'} ne 'POST'); | |
195 | if (!$params->{'wantfile'}) { | |
196 | $CGI::DISABLE_UPLOADS = 1; | |
197 | $CGI::POST_MAX = 1024 * 1024; | |
198 | } else { | |
199 | $CGI::POST_MAX = 10 * 1024 * 1024; | |
200 | } | |
201 | $cgi->referer() =~ m/^http?\:\/\/([^\/]+)/; | |
202 | my $referer = $1; | |
203 | $cgi->url() =~ m/^http?\:\/\/([^\/]+)/; | |
204 | my $servername = $1; | |
205 | return if ($referer ne $servername); | |
206 | ||
207 | ### Modified for getting multi-vars, split by | | |
208 | my %temp = $cgi->Vars(); | |
209 | foreach my $key (keys %temp) { | |
210 | $hash->{$key} = $temp{$key}; | |
211 | $hash->{$key} =~ s/\0/|/g; | |
212 | $hash->{$key} =~ s/^\s*(.*?)\s*$/$1/; | |
213 | } | |
214 | ||
215 | if (($params->{'wantfile'})&&($params->{'filevar'})) { | |
216 | $hash->{$params->{'filevar'}} = $cgi->upload | |
217 | ($params->{'filevar'}); | |
218 | } | |
219 | return; | |
220 | } | |
221 | ||
9735e167 MT |
222 | sub getterms() { |
223 | my @terms = (); | |
48fb1d3b | 224 | |
9735e167 | 225 | open(my $handle, "<:utf8", "/var/ipfire/captive/terms.txt"); |
48fb1d3b MT |
226 | while(<$handle>) { |
227 | $_ = HTML::Entities::decode_entities($_); | |
9735e167 | 228 | push(@terms, $_); |
48fb1d3b MT |
229 | } |
230 | close($handle); | |
231 | ||
9735e167 | 232 | my $terms = join("\n", @terms); |
48fb1d3b MT |
233 | |
234 | # Format paragraphs | |
9735e167 | 235 | $terms =~ s/\n\n/<\/p>\n<p>/g; |
48fb1d3b | 236 | |
9735e167 | 237 | return $terms; |
8b920789 | 238 | } |