Project

General

Profile

Download (9.67 KB) Statistics
| Branch: | Tag: | Revision:
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
 * Licensed under the Apache License, Version 2.0 (the "License");
14
 * you may not use this file except in compliance with the License.
15
 * You may obtain a copy of the License at
16
 *
17
 * http://www.apache.org/licenses/LICENSE-2.0
18
 *
19
 * Unless required by applicable law or agreed to in writing, software
20
 * distributed under the License is distributed on an "AS IS" BASIS,
21
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
 * See the License for the specific language governing permissions and
23
 * limitations under the License.
24
 */
25

    
26
require_once("auth.inc");
27
require_once("functions.inc");
28
require_once("captiveportal.inc");
29

    
30
$errormsg = "Invalid credentials specified.";
31

    
32
header("Expires: 0");
33
header("Cache-Control: no-cache, no-store, must-revalidate");
34
header("Pragma: no-cache");
35
header("Connection: close");
36

    
37
global $cpzone, $cpzoneid;
38

    
39
$cpzone = strtolower($_REQUEST['zone']);
40
$cpcfg = $config['captiveportal'][$cpzone];
41
if (empty($cpcfg)) {
42
	log_error("Submission to captiveportal with unknown parameter zone: " . htmlspecialchars($cpzone));
43
	portal_reply_page($redirurl, "error", $errormsg);
44
	ob_flush();
45
	return;
46
}
47

    
48
$cpzoneid = $cpcfg['zoneid'];
49

    
50
$orig_host = $_SERVER['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("Zone: {$cpzone} - 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
	ob_flush();
61
	return;
62
}
63

    
64
$cpsession = captiveportal_isip_logged($clientip);
65
$ourhostname = portal_hostname_from_client_ip($clientip);
66
/* Automatically switching to the logout page requires a custom logout page to be present. */
67
if ((!empty($cpsession)) && (! $_POST['logout_id']) && (!empty($cpcfg['page']['logouttext']))) {
68
	/* if client already logged in so show logout page */
69
	$protocol = (isset($config['captiveportal'][$cpzone]['httpslogin'])) ? 'https://' : 'http://';
70
	$logouturl = "{$protocol}{$ourhostname}/";
71

    
72
	$sessionid = $cpsession['sessionid'];
73
	$attributes = array();
74
	if (!empty($cpsession['session_timeout']))
75
		$attributes['session_timeout'] = $cpsession['session_timeout'];
76
	if (!empty($cpsession['session_terminate_time']))
77
		$attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
78

    
79
	include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
80
	ob_flush();
81
	return;
82
} else if ($orig_host != $ourhostname) {
83
	/* the client thinks it's connected to the desired web server, but instead
84
	   it's connected to us. Issue a redirect... */
85
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
86
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
87

    
88
	ob_flush();
89
	return;
90
}
91

    
92
if (!empty($cpcfg['redirurl'])) {
93
	$redirurl = $cpcfg['redirurl'];
94
} else if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
95
	$redirurl = urldecode($matches[1]);
96
} else if ($_REQUEST['redirurl']) {
97
	$redirurl = $_REQUEST['redirurl'];
98
}
99

    
100
$macfilter = !isset($cpcfg['nomacfilter']);
101
$passthrumac = isset($cpcfg['passthrumacadd']);
102

    
103
/* find MAC address for client */
104
if ($macfilter || $passthrumac) {
105
	$tmpres = pfSense_ip_to_mac($clientip);
106
	if (!is_array($tmpres)) {
107
		/* unable to find MAC address - shouldn't happen! - bail out */
108
		captiveportal_logportalauth("unauthenticated", "noclientmac", $clientip, "ERROR");
109
		echo "An error occurred.  Please check the system logs for more information.";
110
		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.");
111
		ob_flush();
112
		return;
113
	}
114
	$clientmac = $tmpres['macaddr'];
115
	unset($tmpres);
116
}
117

    
118
/* find out if we need RADIUS + RADIUSMAC or not */
119
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
120
	$radius_enable = TRUE;
121
	if (isset($cpcfg['radmac_enable'])) {
122
		$radmac_enable = TRUE;
123
	}
124
}
125

    
126
/* find radius context */
127
$radiusctx = 'first';
128
if ($_POST['auth_user2']) {
129
	$radiusctx = 'second';
130
}
131

    
132
if ($_POST['logout_id']) {
133
	echo <<<EOD
134
<html>
135
<head><title>Disconnecting...</title></head>
136
<body bgcolor="#435370">
137
<span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
138
<b>You have been disconnected.</b>
139
</span>
140
<script type="text/javascript">
141
<!--
142
setTimeout('window.close();',5000) ;
143
-->
144
</script>
145
</body>
146
</html>
147

    
148
EOD;
149

    
150
	$safe_logout_id = SQLite3::escapeString($_POST['logout_id']);
151
	captiveportal_disconnect_client($safe_logout_id);
152

    
153
} else if ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
154
	captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
155
	if (!empty($cpcfg['blockedmacsurl'])) {
156
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
157
	} else {
158
		portal_reply_page($redirurl, "error", "This MAC address has been blocked");
159
	}
160

    
161
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac, $clientip, $radiusctx)) {
162
	/* radius functions handle everything so we exit here since we're done */
163

    
164
} else if (portal_consume_passthrough_credit($clientmac)) {
165
	/* allow the client through if it had a pass-through credit for its MAC */
166
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
167
	portal_allow($clientip, $clientmac, "unauthenticated");
168

    
169
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
170
	$voucher = trim($_POST['auth_voucher']);
171
	$timecredit = voucher_auth($voucher);
172
	// $timecredit contains either a credit in minutes or an error message
173
	if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
174
		// if multiple vouchers given, use the first as username
175
		$a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
176
		$voucher = $a_vouchers[0];
177
		$attr = array(
178
			'voucher' => 1,
179
			'session_timeout' => $timecredit*60,
180
			'session_terminate_time' => 0);
181
		if (portal_allow($clientip, $clientmac, $voucher, null, $attr)) {
182
			// YES: user is good for $timecredit minutes.
183
			captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
184
		} else {
185
			portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
186
		}
187
	} else if (-1 == $timecredit) {  // valid but expired
188
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
189
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
190
	} else {
191
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
192
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgnoaccess'] ? $config['voucher'][$cpzone]['descrmsgnoaccess'] : $errormsg);
193
	}
194

    
195
} else if ($_POST['accept'] && $radius_enable) {
196
	if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
197
		if (!empty($_POST['auth_user'])) {
198
			$user = $_POST['auth_user'];
199
			$paswd = $_POST['auth_pass'];
200
		} else if (!empty($_POST['auth_user2'])) {
201
			$user = $_POST['auth_user2'];
202
			$paswd = $_POST['auth_pass2'];
203
		}
204
		$auth_list = radius($user, $paswd, $clientip, $clientmac, "USER LOGIN", $radiusctx);
205
		$type = "error";
206
		if (!empty($auth_list['url_redirection'])) {
207
			$redirurl = $auth_list['url_redirection'];
208
			$type = "redir";
209
		}
210

    
211
		if ($auth_list['auth_val'] == 1) {
212
			captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR", $auth_list['error']);
213
			portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
214
		} else if ($auth_list['auth_val'] == 3) {
215
			captiveportal_logportalauth($user, $clientmac, $clientip, "FAILURE", $auth_list['reply_message']);
216
			portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
217
		}
218
	} else {
219
		if (!empty($_POST['auth_user'])) {
220
			$user = $_POST['auth_user'];
221
		} else if (!empty($_POST['auth_user2'])) {
222
			$user = $_POST['auth_user2'];
223
		} else {
224
			$user = 'unknown';
225
		}
226
		captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR");
227
		portal_reply_page($redirurl, "error", $errormsg);
228
	}
229

    
230
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
231
	if ($_POST['auth_user'] && $_POST['auth_pass']) {
232
		//check against local user manager
233
		$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
234

    
235
		if ($loginok && isset($cpcfg['localauth_priv'])) {
236
			$loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
237
		}
238

    
239
		if ($loginok) {
240
			captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "LOGIN");
241
			portal_allow($clientip, $clientmac, $_POST['auth_user']);
242
		} else {
243
			captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "FAILURE");
244
			portal_reply_page($redirurl, "error", $errormsg);
245
		}
246
	} else {
247
		portal_reply_page($redirurl, "error", $errormsg);
248
	}
249

    
250
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
251
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
252
	portal_allow($clientip, $clientmac, "unauthenticated");
253

    
254
} else {
255
	/* display captive portal page */
256
	portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
257
}
258

    
259
ob_flush();
260

    
261
?>
(1-1/3)