1
|
<?php
|
2
|
/*
|
3
|
$Id$
|
4
|
part of m0n0wall (http://m0n0.ch/wall)
|
5
|
|
6
|
Copyrigth (C) 2009 Ermal Lu?i
|
7
|
Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
|
8
|
All rights reserved.
|
9
|
|
10
|
Redistribution and use in source and binary forms, with or without
|
11
|
modification, are permitted provided that the following conditions are met:
|
12
|
|
13
|
1. Redistributions of source code must retain the above copyright notice,
|
14
|
this list of conditions and the following disclaimer.
|
15
|
|
16
|
2. Redistributions in binary form must reproduce the above copyright
|
17
|
notice, this list of conditions and the following disclaimer in the
|
18
|
documentation and/or other materials provided with the distribution.
|
19
|
|
20
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
21
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
22
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
23
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
24
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
POSSIBILITY OF SUCH DAMAGE.
|
30
|
*/
|
31
|
/*
|
32
|
pfSense_MODULE: captiveportal
|
33
|
*/
|
34
|
|
35
|
require_once("auth.inc");
|
36
|
require_once("functions.inc");
|
37
|
require_once("captiveportal.inc");
|
38
|
|
39
|
$errormsg = "Invalid credentials specified.";
|
40
|
|
41
|
header("Expires: 0");
|
42
|
header("Cache-Control: no-store, no-cache, must-revalidate");
|
43
|
header("Cache-Control: post-check=0, pre-check=0", false);
|
44
|
header("Pragma: no-cache");
|
45
|
header("Connection: close");
|
46
|
|
47
|
$cpzone = $_REQUEST['zone'];
|
48
|
$cpcfg = $config['captiveportal'][$cpzone];
|
49
|
|
50
|
$orig_host = $_ENV['HTTP_HOST'];
|
51
|
/* NOTE: IE 8/9 is buggy and that is why this is needed */
|
52
|
$orig_request = trim($_REQUEST['redirurl'], " /");
|
53
|
$clientip = $_SERVER['REMOTE_ADDR'];
|
54
|
|
55
|
if (!$clientip) {
|
56
|
/* not good - bail out */
|
57
|
log_error("Captive portal could not determine client's IP address.");
|
58
|
$error_message = "An error occurred. Please check the system logs for more information.";
|
59
|
portal_reply_page($redirurl, "error", $errormsg);
|
60
|
exit;
|
61
|
}
|
62
|
|
63
|
$listenporthttps = $cpcfg['listenporthttps'] ? $cpcfg['listenporthttps'] : ($cpcfg['zoneid'] + 1);
|
64
|
$listenporthttp = $cpcfg['listenporthttp'] ? $cpcfg['listenporthttp'] : $cpcfg['zoneid'];
|
65
|
|
66
|
if (isset($config['captiveportal'][$cpzone]['httpslogin']))
|
67
|
$ourhostname = $config['captiveportal'][$cpzone]['httpsname'] . ":" . $listenporthttps;
|
68
|
else {
|
69
|
$ifip = portal_ip_from_client_ip($clientip);
|
70
|
if (!$ifip)
|
71
|
$ourhostname = "{$config['system']['hostname']}.{$config['system']['domain']}:{$listenporthttp}";
|
72
|
else
|
73
|
$ourhostname = "{$ifip}:{$listenporthttp}";
|
74
|
}
|
75
|
|
76
|
if ($orig_host != $ourhostname) {
|
77
|
/* the client thinks it's connected to the desired web server, but instead
|
78
|
it's connected to us. Issue a redirect... */
|
79
|
|
80
|
if (isset($config['captiveportal'][$cpzone]['httpslogin']))
|
81
|
header("Location: https://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
|
82
|
else
|
83
|
header("Location: http://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
|
84
|
|
85
|
exit;
|
86
|
}
|
87
|
if (!empty($config['captiveportal'][$cpzone]['redirurl']))
|
88
|
$redirurl = $config['captiveportal'][$cpzone]['redirurl'];
|
89
|
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
|
90
|
$redirurl = urldecode($matches[1]);
|
91
|
else if ($_REQUEST['redirurl'])
|
92
|
$redirurl = $_REQUEST['redirurl'];
|
93
|
|
94
|
$macfilter = !isset($config['captiveportal'][$cpzone]['nomacfilter']);
|
95
|
$passthrumac = isset($config['captiveportal'][$cpzone]['passthrumacadd']);
|
96
|
|
97
|
/* find MAC address for client */
|
98
|
if ($macfilter || $passthrumac)
|
99
|
$clientmac = arp_get_mac_by_ip($clientip);
|
100
|
if (!$clientmac && ($macfilter || $passthrumac)) {
|
101
|
/* unable to find MAC address - shouldn't happen! - bail out */
|
102
|
captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
|
103
|
echo "An error occurred. Please check the system logs for more information.";
|
104
|
log_error("Captive portal could not determine client's MAC address. Disable MAC address filtering in captive portal if you do not need this functionality.");
|
105
|
exit;
|
106
|
}
|
107
|
|
108
|
/* find out if we need RADIUS + RADIUSMAC or not */
|
109
|
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
|
110
|
$radius_enable = TRUE;
|
111
|
if (isset($config['captiveportal'][$cpzone]['radmac_enable']))
|
112
|
$radmac_enable = TRUE;
|
113
|
}
|
114
|
|
115
|
/* find radius context */
|
116
|
$radiusctx = 'first';
|
117
|
if ($_POST['auth_user2'])
|
118
|
$radiusctx = 'second';
|
119
|
|
120
|
if ($_POST['logout_id']) {
|
121
|
echo <<<EOD
|
122
|
<HTML>
|
123
|
<HEAD><TITLE>Disconnecting...</TITLE></HEAD>
|
124
|
<BODY BGCOLOR="#435370">
|
125
|
<SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
|
126
|
<B>You have been disconnected.</B>
|
127
|
</SPAN>
|
128
|
<SCRIPT LANGUAGE="JavaScript">
|
129
|
<!--
|
130
|
setTimeout('window.close();',5000) ;
|
131
|
-->
|
132
|
</SCRIPT>
|
133
|
</BODY>
|
134
|
</HTML>
|
135
|
|
136
|
EOD;
|
137
|
captiveportal_disconnect_client($_POST['logout_id']);
|
138
|
exit;
|
139
|
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
|
140
|
/* radius functions handle everything so we exit here since we're done */
|
141
|
exit;
|
142
|
|
143
|
} else if (portal_consume_passthrough_credit($clientmac)) {
|
144
|
/* allow the client through if it had a pass-through credit for its MAC */
|
145
|
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
|
146
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
147
|
|
148
|
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
|
149
|
|
150
|
$voucher = trim($_POST['auth_voucher']);
|
151
|
$timecredit = voucher_auth($voucher);
|
152
|
// $timecredit contains either a credit in minutes or an error message
|
153
|
if ($timecredit > 0) { // voucher is valid. Remaining minutes returned
|
154
|
// if multiple vouchers given, use the first as username
|
155
|
$a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher);
|
156
|
$voucher = $a_vouchers[0];
|
157
|
$attr = array( 'voucher' => 1,
|
158
|
'session_timeout' => $timecredit*60,
|
159
|
'session_terminate_time' => 0);
|
160
|
if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
|
161
|
|
162
|
// YES: user is good for $timecredit minutes.
|
163
|
captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
|
164
|
} else {
|
165
|
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
|
166
|
}
|
167
|
} else if (-1 == $timecredit) { // valid but expired
|
168
|
captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
|
169
|
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
|
170
|
} else {
|
171
|
captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
|
172
|
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
|
173
|
}
|
174
|
|
175
|
} else if ($_POST['accept'] && $radius_enable) {
|
176
|
|
177
|
if (($_POST['auth_user'] && $_POST['auth_pass']) || ($_POST['auth_user2'] && $_POST['auth_pass2'])) {
|
178
|
if (!empty($_POST['auth_user'])) {
|
179
|
$user = $_POST['auth_user'];
|
180
|
$paswd = $_POST['auth_pass'];
|
181
|
} else if (!empty($_POST['auth_user2'])) {
|
182
|
$user = $_POST['auth_user2'];
|
183
|
$paswd = $_POST['auth_pass2'];
|
184
|
}
|
185
|
$auth_list = radius($user,$paswd,$clientip,$clientmac,"USER LOGIN", $radiusctx);
|
186
|
$type = "error";
|
187
|
if (!empty($auth_list['url_redirection'])) {
|
188
|
$redirurl = $auth_list['url_redirection'];
|
189
|
$type = "redir";
|
190
|
}
|
191
|
|
192
|
if ($auth_list['auth_val'] == 1) {
|
193
|
captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']);
|
194
|
portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
|
195
|
}
|
196
|
else if ($auth_list['auth_val'] == 3) {
|
197
|
captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
|
198
|
portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
|
199
|
}
|
200
|
} else {
|
201
|
if (!empty($_POST['auth_user']))
|
202
|
$user = $_POST['auth_user'];
|
203
|
else if (!empty($_POST['auth_user2']))
|
204
|
$user = $_POST['auth_user2'];
|
205
|
else
|
206
|
$user = 'unknown';
|
207
|
captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR");
|
208
|
portal_reply_page($redirurl, "error", $errormsg);
|
209
|
}
|
210
|
|
211
|
} else if ($_POST['accept'] && $config['captiveportal'][$cpzone]['auth_method'] == "local") {
|
212
|
|
213
|
if ($_POST['auth_user'] && $_POST['auth_pass']) {
|
214
|
//check against local user manager
|
215
|
$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
|
216
|
if ($loginok){
|
217
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
|
218
|
portal_allow($clientip, $clientmac,$_POST['auth_user']);
|
219
|
} else {
|
220
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
|
221
|
portal_reply_page($redirurl, "error", $errormsg);
|
222
|
}
|
223
|
} else
|
224
|
portal_reply_page($redirurl, "error", $errormsg);
|
225
|
} else if ($_POST['accept'] && $clientip && $config['captiveportal'][$cpzone]['auth_method'] == "none") {
|
226
|
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
|
227
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
228
|
} else {
|
229
|
/* display captive portal page */
|
230
|
portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
|
231
|
}
|
232
|
|
233
|
exit;
|
234
|
|
235
|
|
236
|
?>
|