Project

General

Profile

Download (9.58 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * index.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Originally part of m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2006 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
require_once("auth.inc");
29
require_once("functions.inc");
30
require_once("captiveportal.inc");
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", gettext("Internal error"));
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
	$errormsg = gettext("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 connected and a custom logout page is set : 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
} elseif (!empty($cpsession) && (!isset($_POST['logout_id']) || !isset($config['captiveportal'][$cpzone]['logoutwin_enable']))) {
83
	/* If client try to access captive portal page while already connected, 
84
		but no custom logout page does exist and logout popup is disabled */	
85
	echo gettext("You are connected.");
86
	ob_flush();
87
	return;
88
} elseif ($orig_host != $ourhostname) {
89
	/* the client thinks it's connected to the desired web server, but instead
90
	   it's connected to us. Issue a redirect... */
91
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
92
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
93

    
94
	ob_flush();
95
	return;
96
}
97

    
98
if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
99
	$redirurl = urldecode($matches[1]);
100
} elseif ($_REQUEST['redirurl']) {
101
	$redirurl = $_REQUEST['redirurl'];
102
} elseif (!empty($cpcfg['redirurl'])) {
103
	$redirurl = $cpcfg['redirurl'];
104
}
105

    
106
$macfilter = !isset($cpcfg['nomacfilter']);
107

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

    
123
if ($_POST['logout_id']) {
124
	echo <<<EOD
125
<html>
126
<head><title>Disconnecting...</title></head>
127
<body bgcolor="#435370">
128
<span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
129
<b>You have been disconnected.</b>
130
</span>
131
<script type="text/javascript">
132
<!--
133
setTimeout('window.close();',5000) ;
134
-->
135
</script>
136
</body>
137
</html>
138

    
139
EOD;
140

    
141
	$safe_logout_id = SQLite3::escapeString($_POST['logout_id']);
142
	captiveportal_disconnect_client($safe_logout_id);
143

    
144
} elseif (($_POST['accept'] || $cpcfg['auth_method'] === 'radmac' || !empty($cpcfg['blockedmacsurl'])) && $macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
145
	captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
146
	if (!empty($cpcfg['blockedmacsurl'])) {
147
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
148
	} else {
149
		if ($cpcfg['auth_method'] === 'radmac') {
150
			echo gettext("This MAC address has been blocked");
151
		} else {
152
			portal_reply_page($redirurl, "error", "This MAC address has been blocked");
153
		}
154
	}
155
} elseif (portal_consume_passthrough_credit($clientmac)) {
156
	/* allow the client through if it had a pass-through credit for its MAC */
157
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
158
	portal_allow($clientip, $clientmac, "unauthenticated");
159

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

    
187
} elseif ($_POST['accept'] || $cpcfg['auth_method'] === 'radmac') {
188
	
189
		if ($cpcfg['auth_method'] === 'radmac' && !isset($_POST['accept'])) {
190
			$user = $clientmac; 
191
			$passwd = $cpcfg['radmac_secret'];
192
			$context = 'radmac'; // Radius MAC authentication
193
		} elseif (!empty(trim($_POST['auth_user2']))) { 
194
			$user = trim($_POST['auth_user2']);
195
			$passwd = $_POST['auth_pass2'];
196
			$context = 'second'; // Assume users to use the first context if auth_user2 is empty/does not exist
197
		} else {
198
			$user = trim($_POST['auth_user']);
199
			$passwd = $_POST['auth_pass'];
200
			$context = 'first';
201
		}
202
	
203
	$pipeno = captiveportal_get_next_dn_ruleno('auth');
204
	/* if the pool is empty, return appropriate message and exit */
205
	if (is_null($pipeno)) {
206
		$replymsg = gettext("System reached maximum login capacity");
207
		if ($cpcfg['auth_method'] === 'radmac') {
208
			echo $replymsg;
209
			ob_flush();
210
			return;
211
		} else {
212
			portal_reply_page($redirurl, "error", $replymsg);
213
		}
214
		log_error("Zone: {$cpzone} - WARNING!  Captive portal has reached maximum login capacity");
215
		
216
	}
217
	
218
	$auth_result = captiveportal_authenticate_user($user, $passwd, $clientmac, $clientip, $pipeno, $context);
219
	
220
	if ($auth_result['result']) {
221
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status']);
222
		portal_allow($clientip, $clientmac, $user, $passwd, $auth_result['attributes'], $pipeno, $auth_result['auth_method'], $context);
223

    
224
	} else {
225
		captiveportal_free_dn_ruleno($pipeno);
226
		$type = "error";
227
			
228
		if (!empty($auth_result['attributes']['url_redirection'])) {
229
			$redirurl = $auth_result['attributes']['url_redirection'];
230
			$type = "redir";
231
		}
232
		
233
		if ($auth_result['login_message']) {
234
			$replymsg = $auth_result['login_message'];
235
		} else {
236
			$replymsg = gettext("Invalid credentials specified.");
237
		}
238
		
239
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status'], $replymsg);
240

    
241
		/* Radius MAC authentication. */
242
		if ($context === 'radmac' && $type !== 'redir' && !isset($cpcfg['radmac_fallback'])) {
243
			echo $replymsg;
244
		} else {
245
			portal_reply_page($redirurl, $type, $replymsg);
246
		}
247
	}
248
} else {
249
	/* display captive portal page */
250
	portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
251
}
252

    
253
ob_flush();
254

    
255
?>
(2-2/2)