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