Project

General

Profile

Download (9.48 KB) Statistics
| Branch: | Tag: | Revision:
1 8c1ce6c7 Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 ac24dc24 Renato Botelho
 * index.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 880ed461 jim-p
 * Copyright (c) 2004-2020 Rubicon Communications, LLC (Netgate)
7 ac24dc24 Renato Botelho
 * All rights reserved.
8
 *
9
 * Originally part of m0n0wall (http://m0n0.ch/wall)
10 c5d81585 Renato Botelho
 * Copyright (c) 2003-2006 Manuel Kasper <mk@neon1.net>.
11 ac24dc24 Renato Botelho
 * All rights reserved.
12
 *
13 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
18 ac24dc24 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
25 5b237745 Scott Ullrich
26 0092b3bd mgrooms
require_once("auth.inc");
27 65fbb388 Scott Ullrich
require_once("functions.inc");
28 483e6de8 Scott Ullrich
require_once("captiveportal.inc");
29 65fbb388 Scott Ullrich
30
header("Expires: 0");
31 2ebbb0bc Jose Luis Duran
header("Cache-Control: no-cache, no-store, must-revalidate");
32 65fbb388 Scott Ullrich
header("Pragma: no-cache");
33 232846a2 Ermal
header("Connection: close");
34 5b237745 Scott Ullrich
35 baec2b00 Ermal
global $cpzone, $cpzoneid;
36 5370146c Ermal
37 7949eac7 Renato Botelho
$cpzone = strtolower($_REQUEST['zone']);
38 b4792bf8 Ermal
$cpcfg = $config['captiveportal'][$cpzone];
39 09294e84 Ermal
if (empty($cpcfg)) {
40 762a7b89 Phil Davis
	log_error("Submission to captiveportal with unknown parameter zone: " . htmlspecialchars($cpzone));
41 eb43c5b1 Augustin FL
	portal_reply_page($redirurl, "error", gettext("Internal error"));
42 09294e84 Ermal
	ob_flush();
43
	return;
44
}
45 b4792bf8 Ermal
46 baec2b00 Ermal
$cpzoneid = $cpcfg['zoneid'];
47
48 362ec35d Ermal
$orig_host = $_SERVER['HTTP_HOST'];
49 6e895d5f Ermal
/* NOTE: IE 8/9 is buggy and that is why this is needed */
50 f89afb47 Ermal
$orig_request = trim($_REQUEST['redirurl'], " /");
51 6fa4bdc6 Scott Ullrich
$clientip = $_SERVER['REMOTE_ADDR'];
52 5b237745 Scott Ullrich
53
if (!$clientip) {
54 c9cb32c4 Ermal
	/* not good - bail out */
55 12feed15 Ermal
	log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
56 eb43c5b1 Augustin FL
	$errormsg = gettext("An error occurred.  Please check the system logs for more information.");
57 c9cb32c4 Ermal
	portal_reply_page($redirurl, "error", $errormsg);
58 4a5feb83 Ermal
	ob_flush();
59
	return;
60 65fbb388 Scott Ullrich
}
61
62 d2ecbddc jim-p
$cpsession = captiveportal_isip_logged($clientip);
63 de132ae3 bcyrill
$ourhostname = portal_hostname_from_client_ip($clientip);
64 d2ecbddc jim-p
/* Automatically switching to the logout page requires a custom logout page to be present. */
65
if ((!empty($cpsession)) && (! $_POST['logout_id']) && (!empty($cpcfg['page']['logouttext']))) {
66 63da30ff Augustin-FL
	/* if client already connected and a custom logout page is set : show logout page */
67 d2ecbddc jim-p
	$protocol = (isset($config['captiveportal'][$cpzone]['httpslogin'])) ? 'https://' : 'http://';
68
	$logouturl = "{$protocol}{$ourhostname}/";
69
70
	$sessionid = $cpsession['sessionid'];
71
	$attributes = array();
72
	if (!empty($cpsession['session_timeout']))
73
		$attributes['session_timeout'] = $cpsession['session_timeout'];
74
	if (!empty($cpsession['session_terminate_time']))
75
		$attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
76
77
	include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
78
	ob_flush();
79
	return;
80 63da30ff Augustin-FL
} elseif (!empty($cpsession) && (!isset($_POST['logout_id']) || !isset($config['captiveportal'][$cpzone]['logoutwin_enable']))) {
81
	/* If client try to access captive portal page while already connected, 
82
		but no custom logout page does exist and logout popup is disabled */	
83 eb43c5b1 Augustin FL
	echo gettext("You are connected.");
84
	ob_flush();
85
	return;
86
} elseif ($orig_host != $ourhostname) {
87 3b832418 bcyrill
	/* the client thinks it's connected to the desired web server, but instead
88
	   it's connected to us. Issue a redirect... */
89 de132ae3 bcyrill
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
90
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
91 65fbb388 Scott Ullrich
92 3b832418 bcyrill
	ob_flush();
93
	return;
94 5b237745 Scott Ullrich
}
95 de132ae3 bcyrill
96 db95c2d8 bechaire
if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
97 adbb495c Ermal
	$redirurl = urldecode($matches[1]);
98 eb43c5b1 Augustin FL
} elseif ($_REQUEST['redirurl']) {
99 adbb495c Ermal
	$redirurl = $_REQUEST['redirurl'];
100 db95c2d8 bechaire
} elseif (!empty($cpcfg['redirurl'])) {
101
	$redirurl = $cpcfg['redirurl'];
102 7d61beba Phil Davis
}
103 65fbb388 Scott Ullrich
104 b7ae00c0 bcyrill
$macfilter = !isset($cpcfg['nomacfilter']);
105 65fbb388 Scott Ullrich
106 5b237745 Scott Ullrich
/* find MAC address for client */
107 20588aac Augustin-FL
if ($macfilter || isset($cpcfg['passthrumacadd'])) {
108 0d20a040 Ermal
	$tmpres = pfSense_ip_to_mac($clientip);
109
	if (!is_array($tmpres)) {
110 3b832418 bcyrill
		/* unable to find MAC address - shouldn't happen! - bail out */
111 6c07db48 Phil Davis
		captiveportal_logportalauth("unauthenticated", "noclientmac", $clientip, "ERROR");
112 3b832418 bcyrill
		echo "An error occurred.  Please check the system logs for more information.";
113 12feed15 Ermal
		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.");
114 3b832418 bcyrill
		ob_flush();
115
		return;
116 007161dc Ermal
	}
117 0d20a040 Ermal
	$clientmac = $tmpres['macaddr'];
118
	unset($tmpres);
119 5b237745 Scott Ullrich
}
120
121 65fbb388 Scott Ullrich
if ($_POST['logout_id']) {
122 532cb894 Ermal
	echo <<<EOD
123 1d7ba683 ayvis
<html>
124
<head><title>Disconnecting...</title></head>
125
<body bgcolor="#435370">
126
<span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
127
<b>You have been disconnected.</b>
128
</span>
129 91f026b0 ayvis
<script type="text/javascript">
130 5b237745 Scott Ullrich
<!--
131
setTimeout('window.close();',5000) ;
132
-->
133 1d7ba683 ayvis
</script>
134
</body>
135
</html>
136 5b237745 Scott Ullrich
137
EOD;
138 69c97c32 Chris Buechler
139
	$safe_logout_id = SQLite3::escapeString($_POST['logout_id']);
140
	captiveportal_disconnect_client($safe_logout_id);
141 3b832418 bcyrill
142 5225415a Augustin FL
} elseif (($_POST['accept'] || $cpcfg['auth_method'] === 'radmac' || !empty($cpcfg['blockedmacsurl'])) && $macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
143 6c07db48 Phil Davis
	captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
144 7d61beba Phil Davis
	if (!empty($cpcfg['blockedmacsurl'])) {
145 8d5ddc09 Renato Botelho
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
146 7d61beba Phil Davis
	} else {
147 eb43c5b1 Augustin FL
		if ($cpcfg['auth_method'] === 'radmac') {
148
			echo gettext("This MAC address has been blocked");
149
		} else {
150
			portal_reply_page($redirurl, "error", "This MAC address has been blocked");
151
		}
152 7d61beba Phil Davis
	}
153 eb43c5b1 Augustin FL
} elseif (portal_consume_passthrough_credit($clientmac)) {
154 3b832418 bcyrill
	/* allow the client through if it had a pass-through credit for its MAC */
155 6c07db48 Phil Davis
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
156 3b832418 bcyrill
	portal_allow($clientip, $clientmac, "unauthenticated");
157 8015e67b Erik Fonnesbeck
158 eb43c5b1 Augustin FL
} elseif (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
159 3b832418 bcyrill
	$voucher = trim($_POST['auth_voucher']);
160 eb43c5b1 Augustin FL
	$errormsg = gettext("Invalid credentials specified.");
161 3b832418 bcyrill
	$timecredit = voucher_auth($voucher);
162
	// $timecredit contains either a credit in minutes or an error message
163
	if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
164
		// if multiple vouchers given, use the first as username
165 6c07db48 Phil Davis
		$a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
166 3b832418 bcyrill
		$voucher = $a_vouchers[0];
167 6c07db48 Phil Davis
		$attr = array(
168
			'voucher' => 1,
169
			'session_timeout' => $timecredit*60,
170
			'session_terminate_time' => 0);
171 eb43c5b1 Augustin FL
		if (portal_allow($clientip, $clientmac, $voucher, null, $attr, null, 'voucher', 'voucher')) {
172 3b832418 bcyrill
			// YES: user is good for $timecredit minutes.
173 6c07db48 Phil Davis
			captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
174 3b832418 bcyrill
		} else {
175 4828d672 Gertjan
			portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
176 3b832418 bcyrill
		}
177 eb43c5b1 Augustin FL
	} elseif (-1 == $timecredit) {  // valid but expired
178 6c07db48 Phil Davis
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
179 4828d672 Gertjan
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
180 3b832418 bcyrill
	} else {
181 6c07db48 Phil Davis
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
182 4828d672 Gertjan
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgnoaccess'] ? $config['voucher'][$cpzone]['descrmsgnoaccess'] : $errormsg);
183 3b832418 bcyrill
	}
184 336e3c1c Charlie
185 eb43c5b1 Augustin FL
} elseif ($_POST['accept'] || $cpcfg['auth_method'] === 'radmac') {
186
	
187 bb90e3c5 Augustin-FL
		if ($cpcfg['auth_method'] === 'radmac' && !isset($_POST['accept'])) {
188
			$user = $clientmac; 
189
			$passwd = $cpcfg['radmac_secret'];
190
			$context = 'radmac'; // Radius MAC authentication
191 ef2e3b5d Augustin-FL
		} elseif (!empty(trim($_POST['auth_user2']))) { 
192 a9a90af6 heper
			$user = trim($_POST['auth_user2']);
193 eb43c5b1 Augustin FL
			$passwd = $_POST['auth_pass2'];
194
			$context = 'second'; // Assume users to use the first context if auth_user2 is empty/does not exist
195
		} else {
196 a9a90af6 heper
			$user = trim($_POST['auth_user']);
197 eb43c5b1 Augustin FL
			$passwd = $_POST['auth_pass'];
198
			$context = 'first';
199
		}
200
	
201
	$pipeno = captiveportal_get_next_dn_ruleno();
202
	/* if the pool is empty, return appropriate message and exit */
203
	if (is_null($pipeno)) {
204
		$replymsg = gettext("System reached maximum login capacity");
205
		if ($cpcfg['auth_method'] === 'radmac') {
206
			echo $replymsg;
207
			ob_flush();
208
			return;
209 7d61beba Phil Davis
		} else {
210 eb43c5b1 Augustin FL
			portal_reply_page($redirurl, "error", $replymsg);
211 7d61beba Phil Davis
		}
212 eb43c5b1 Augustin FL
		log_error("Zone: {$cpzone} - WARNING!  Captive portal has reached maximum login capacity");
213
		
214 814992f7 Ermal
	}
215 eb43c5b1 Augustin FL
	
216
	$auth_result = captiveportal_authenticate_user($user, $passwd, $clientmac, $clientip, $pipeno, $context);
217
	
218
	if ($auth_result['result']) {
219
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status']);
220
		portal_allow($clientip, $clientmac, $user, $passwd, $auth_result['attributes'], $pipeno, $auth_result['auth_method'], $context);
221 65fbb388 Scott Ullrich
222 eb43c5b1 Augustin FL
	} else {
223
		captiveportal_free_dn_ruleno($pipeno);
224
		$type = "error";
225
			
226
		if (!empty($auth_result['attributes']['url_redirection'])) {
227
			$redirurl = $auth_result['attributes']['url_redirection'];
228
			$type = "redir";
229 7d61beba Phil Davis
		}
230 eb43c5b1 Augustin FL
		
231
		if ($auth_result['login_message']) {
232
			$replymsg = $auth_result['login_message'];
233 3b832418 bcyrill
		} else {
234 eb43c5b1 Augustin FL
			$replymsg = gettext("Invalid credentials specified.");
235
		}
236
		
237
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status'], $replymsg);
238
239 bb90e3c5 Augustin-FL
		/* Radius MAC authentication. */
240
		if ($context === 'radmac' && $type !== 'redir' && !isset($cpcfg['radmac_fallback'])) {
241
			echo $replymsg;
242 eb43c5b1 Augustin FL
		} else {
243
			portal_reply_page($redirurl, $type, $replymsg);
244 3b832418 bcyrill
		}
245 7d61beba Phil Davis
	}
246 65fbb388 Scott Ullrich
} else {
247 3b832418 bcyrill
	/* display captive portal page */
248 6c07db48 Phil Davis
	portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
249 5b237745 Scott Ullrich
}
250
251 4a5feb83 Ermal
ob_flush();
252 03552507 Erik Fonnesbeck
253 60b66b60 Ermal
?>