1
|
<?php
|
2
|
/*
|
3
|
* index.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* Originally part of m0n0wall (http://m0n0.ch/wall)
|
10
|
* Copyright (c) 2003-2006 Manuel Kasper <mk@neon1.net>.
|
11
|
* All rights reserved.
|
12
|
*
|
13
|
* Redistribution and use in source and binary forms, with or without
|
14
|
* modification, are permitted provided that the following conditions are met:
|
15
|
*
|
16
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17
|
* this list of conditions and the following disclaimer.
|
18
|
*
|
19
|
* 2. Redistributions in binary form must reproduce the above copyright
|
20
|
* notice, this list of conditions and the following disclaimer in
|
21
|
* the documentation and/or other materials provided with the
|
22
|
* distribution.
|
23
|
*
|
24
|
* 3. All advertising materials mentioning features or use of this software
|
25
|
* must display the following acknowledgment:
|
26
|
* "This product includes software developed by the pfSense Project
|
27
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
28
|
*
|
29
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
30
|
* endorse or promote products derived from this software without
|
31
|
* prior written permission. For written permission, please contact
|
32
|
* coreteam@pfsense.org.
|
33
|
*
|
34
|
* 5. Products derived from this software may not be called "pfSense"
|
35
|
* nor may "pfSense" appear in their names without prior written
|
36
|
* permission of the Electric Sheep Fencing, LLC.
|
37
|
*
|
38
|
* 6. Redistributions of any form whatsoever must retain the following
|
39
|
* acknowledgment:
|
40
|
*
|
41
|
* "This product includes software developed by the pfSense Project
|
42
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
43
|
*
|
44
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
45
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
46
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
47
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
48
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
49
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
50
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
51
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
52
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
53
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
54
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
55
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
56
|
*/
|
57
|
|
58
|
require_once("auth.inc");
|
59
|
require_once("functions.inc");
|
60
|
require_once("captiveportal.inc");
|
61
|
|
62
|
$errormsg = "Invalid credentials specified.";
|
63
|
|
64
|
header("Expires: 0");
|
65
|
header("Cache-Control: no-cache, no-store, must-revalidate");
|
66
|
header("Pragma: no-cache");
|
67
|
header("Connection: close");
|
68
|
|
69
|
global $cpzone, $cpzoneid;
|
70
|
|
71
|
$cpzone = strtolower($_REQUEST['zone']);
|
72
|
$cpcfg = $config['captiveportal'][$cpzone];
|
73
|
if (empty($cpcfg)) {
|
74
|
log_error("Submission to captiveportal with unknown parameter zone: " . htmlspecialchars($cpzone));
|
75
|
portal_reply_page($redirurl, "error", $errormsg);
|
76
|
ob_flush();
|
77
|
return;
|
78
|
}
|
79
|
|
80
|
$cpzoneid = $cpcfg['zoneid'];
|
81
|
|
82
|
$orig_host = $_SERVER['HTTP_HOST'];
|
83
|
/* NOTE: IE 8/9 is buggy and that is why this is needed */
|
84
|
$orig_request = trim($_REQUEST['redirurl'], " /");
|
85
|
$clientip = $_SERVER['REMOTE_ADDR'];
|
86
|
|
87
|
if (!$clientip) {
|
88
|
/* not good - bail out */
|
89
|
log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
|
90
|
$error_message = "An error occurred. Please check the system logs for more information.";
|
91
|
portal_reply_page($redirurl, "error", $errormsg);
|
92
|
ob_flush();
|
93
|
return;
|
94
|
}
|
95
|
|
96
|
$cpsession = captiveportal_isip_logged($clientip);
|
97
|
$ourhostname = portal_hostname_from_client_ip($clientip);
|
98
|
/* Automatically switching to the logout page requires a custom logout page to be present. */
|
99
|
if ((!empty($cpsession)) && (! $_POST['logout_id']) && (!empty($cpcfg['page']['logouttext']))) {
|
100
|
/* if client already logged in so show logout page */
|
101
|
$protocol = (isset($config['captiveportal'][$cpzone]['httpslogin'])) ? 'https://' : 'http://';
|
102
|
$logouturl = "{$protocol}{$ourhostname}/";
|
103
|
|
104
|
$sessionid = $cpsession['sessionid'];
|
105
|
$attributes = array();
|
106
|
if (!empty($cpsession['session_timeout']))
|
107
|
$attributes['session_timeout'] = $cpsession['session_timeout'];
|
108
|
if (!empty($cpsession['session_terminate_time']))
|
109
|
$attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
|
110
|
|
111
|
include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
|
112
|
ob_flush();
|
113
|
return;
|
114
|
} else if ($orig_host != $ourhostname) {
|
115
|
/* the client thinks it's connected to the desired web server, but instead
|
116
|
it's connected to us. Issue a redirect... */
|
117
|
$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
|
118
|
header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
|
119
|
|
120
|
ob_flush();
|
121
|
return;
|
122
|
}
|
123
|
|
124
|
if (!empty($cpcfg['redirurl'])) {
|
125
|
$redirurl = $cpcfg['redirurl'];
|
126
|
} else if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
|
127
|
$redirurl = urldecode($matches[1]);
|
128
|
} else if ($_REQUEST['redirurl']) {
|
129
|
$redirurl = $_REQUEST['redirurl'];
|
130
|
}
|
131
|
|
132
|
$macfilter = !isset($cpcfg['nomacfilter']);
|
133
|
$passthrumac = isset($cpcfg['passthrumacadd']);
|
134
|
|
135
|
/* find MAC address for client */
|
136
|
if ($macfilter || $passthrumac) {
|
137
|
$tmpres = pfSense_ip_to_mac($clientip);
|
138
|
if (!is_array($tmpres)) {
|
139
|
/* unable to find MAC address - shouldn't happen! - bail out */
|
140
|
captiveportal_logportalauth("unauthenticated", "noclientmac", $clientip, "ERROR");
|
141
|
echo "An error occurred. Please check the system logs for more information.";
|
142
|
log_error("Zone: {$cpzone} - Captive portal could not determine client's MAC address. Disable MAC address filtering in captive portal if you do not need this functionality.");
|
143
|
ob_flush();
|
144
|
return;
|
145
|
}
|
146
|
$clientmac = $tmpres['macaddr'];
|
147
|
unset($tmpres);
|
148
|
}
|
149
|
|
150
|
/* find out if we need RADIUS + RADIUSMAC or not */
|
151
|
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
|
152
|
$radius_enable = TRUE;
|
153
|
if (isset($cpcfg['radmac_enable'])) {
|
154
|
$radmac_enable = TRUE;
|
155
|
}
|
156
|
}
|
157
|
|
158
|
/* find radius context */
|
159
|
$radiusctx = 'first';
|
160
|
if ($_POST['auth_user2']) {
|
161
|
$radiusctx = 'second';
|
162
|
}
|
163
|
|
164
|
if ($_POST['logout_id']) {
|
165
|
echo <<<EOD
|
166
|
<html>
|
167
|
<head><title>Disconnecting...</title></head>
|
168
|
<body bgcolor="#435370">
|
169
|
<span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
|
170
|
<b>You have been disconnected.</b>
|
171
|
</span>
|
172
|
<script type="text/javascript">
|
173
|
<!--
|
174
|
setTimeout('window.close();',5000) ;
|
175
|
-->
|
176
|
</script>
|
177
|
</body>
|
178
|
</html>
|
179
|
|
180
|
EOD;
|
181
|
|
182
|
$safe_logout_id = SQLite3::escapeString($_POST['logout_id']);
|
183
|
captiveportal_disconnect_client($safe_logout_id);
|
184
|
|
185
|
} else if ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
|
186
|
captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
|
187
|
if (!empty($cpcfg['blockedmacsurl'])) {
|
188
|
portal_reply_page($cpcfg['blockedmacsurl'], "redir");
|
189
|
} else {
|
190
|
portal_reply_page($redirurl, "error", "This MAC address has been blocked");
|
191
|
}
|
192
|
|
193
|
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac, $clientip, $radiusctx)) {
|
194
|
/* radius functions handle everything so we exit here since we're done */
|
195
|
|
196
|
} else if (portal_consume_passthrough_credit($clientmac)) {
|
197
|
/* allow the client through if it had a pass-through credit for its MAC */
|
198
|
captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
|
199
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
200
|
|
201
|
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
|
202
|
$voucher = trim($_POST['auth_voucher']);
|
203
|
$timecredit = voucher_auth($voucher);
|
204
|
// $timecredit contains either a credit in minutes or an error message
|
205
|
if ($timecredit > 0) { // voucher is valid. Remaining minutes returned
|
206
|
// if multiple vouchers given, use the first as username
|
207
|
$a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
|
208
|
$voucher = $a_vouchers[0];
|
209
|
$attr = array(
|
210
|
'voucher' => 1,
|
211
|
'session_timeout' => $timecredit*60,
|
212
|
'session_terminate_time' => 0);
|
213
|
if (portal_allow($clientip, $clientmac, $voucher, null, $attr)) {
|
214
|
// YES: user is good for $timecredit minutes.
|
215
|
captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
|
216
|
} else {
|
217
|
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
|
218
|
}
|
219
|
} else if (-1 == $timecredit) { // valid but expired
|
220
|
captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
|
221
|
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
|
222
|
} else {
|
223
|
captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
|
224
|
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgnoaccess'] ? $config['voucher'][$cpzone]['descrmsgnoaccess'] : $errormsg);
|
225
|
}
|
226
|
|
227
|
} else if ($_POST['accept'] && $radius_enable) {
|
228
|
if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
|
229
|
if (!empty($_POST['auth_user'])) {
|
230
|
$user = $_POST['auth_user'];
|
231
|
$paswd = $_POST['auth_pass'];
|
232
|
} else if (!empty($_POST['auth_user2'])) {
|
233
|
$user = $_POST['auth_user2'];
|
234
|
$paswd = $_POST['auth_pass2'];
|
235
|
}
|
236
|
$auth_list = radius($user, $paswd, $clientip, $clientmac, "USER LOGIN", $radiusctx);
|
237
|
$type = "error";
|
238
|
if (!empty($auth_list['url_redirection'])) {
|
239
|
$redirurl = $auth_list['url_redirection'];
|
240
|
$type = "redir";
|
241
|
}
|
242
|
|
243
|
if ($auth_list['auth_val'] == 1) {
|
244
|
captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR", $auth_list['error']);
|
245
|
portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
|
246
|
} else if ($auth_list['auth_val'] == 3) {
|
247
|
captiveportal_logportalauth($user, $clientmac, $clientip, "FAILURE", $auth_list['reply_message']);
|
248
|
portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
|
249
|
}
|
250
|
} else {
|
251
|
if (!empty($_POST['auth_user'])) {
|
252
|
$user = $_POST['auth_user'];
|
253
|
} else if (!empty($_POST['auth_user2'])) {
|
254
|
$user = $_POST['auth_user2'];
|
255
|
} else {
|
256
|
$user = 'unknown';
|
257
|
}
|
258
|
captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR");
|
259
|
portal_reply_page($redirurl, "error", $errormsg);
|
260
|
}
|
261
|
|
262
|
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
|
263
|
if ($_POST['auth_user'] && $_POST['auth_pass']) {
|
264
|
//check against local user manager
|
265
|
$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
|
266
|
|
267
|
if ($loginok && isset($cpcfg['localauth_priv'])) {
|
268
|
$loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
|
269
|
}
|
270
|
|
271
|
if ($loginok) {
|
272
|
captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "LOGIN");
|
273
|
portal_allow($clientip, $clientmac, $_POST['auth_user']);
|
274
|
} else {
|
275
|
captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "FAILURE");
|
276
|
portal_reply_page($redirurl, "error", $errormsg);
|
277
|
}
|
278
|
} else {
|
279
|
portal_reply_page($redirurl, "error", $errormsg);
|
280
|
}
|
281
|
|
282
|
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
|
283
|
captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
|
284
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
285
|
|
286
|
} else {
|
287
|
/* display captive portal page */
|
288
|
portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
|
289
|
}
|
290
|
|
291
|
ob_flush();
|
292
|
|
293
|
?>
|