Project

General

Profile

Download (9.48 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * index.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2019 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
header("Expires: 0");
31
header("Cache-Control: no-cache, no-store, must-revalidate");
32
header("Pragma: no-cache");
33
header("Connection: close");
34

    
35
global $cpzone, $cpzoneid;
36

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

    
46
$cpzoneid = $cpcfg['zoneid'];
47

    
48
$orig_host = $_SERVER['HTTP_HOST'];
49
/* NOTE: IE 8/9 is buggy and that is why this is needed */
50
$orig_request = trim($_REQUEST['redirurl'], " /");
51
$clientip = $_SERVER['REMOTE_ADDR'];
52

    
53
if (!$clientip) {
54
	/* not good - bail out */
55
	log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
56
	$errormsg = gettext("An error occurred.  Please check the system logs for more information.");
57
	portal_reply_page($redirurl, "error", $errormsg);
58
	ob_flush();
59
	return;
60
}
61

    
62
$cpsession = captiveportal_isip_logged($clientip);
63
$ourhostname = portal_hostname_from_client_ip($clientip);
64
/* 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
	/* if client already connected and a custom logout page is set : show logout page */
67
	$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
} 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
	echo gettext("You are connected.");
84
	ob_flush();
85
	return;
86
} elseif ($orig_host != $ourhostname) {
87
	/* the client thinks it's connected to the desired web server, but instead
88
	   it's connected to us. Issue a redirect... */
89
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
90
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
91

    
92
	ob_flush();
93
	return;
94
}
95

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

    
104
$macfilter = !isset($cpcfg['nomacfilter']);
105

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

    
121
if ($_POST['logout_id']) {
122
	echo <<<EOD
123
<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
<script type="text/javascript">
130
<!--
131
setTimeout('window.close();',5000) ;
132
-->
133
</script>
134
</body>
135
</html>
136

    
137
EOD;
138

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

    
142
} elseif (($_POST['accept'] || $cpcfg['auth_method'] === 'radmac' || !empty($cpcfg['blockedmacsurl'])) && $macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
143
	captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
144
	if (!empty($cpcfg['blockedmacsurl'])) {
145
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
146
	} else {
147
		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
	}
153
} elseif (portal_consume_passthrough_credit($clientmac)) {
154
	/* allow the client through if it had a pass-through credit for its MAC */
155
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
156
	portal_allow($clientip, $clientmac, "unauthenticated");
157

    
158
} elseif (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
159
	$voucher = trim($_POST['auth_voucher']);
160
	$errormsg = gettext("Invalid credentials specified.");
161
	$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
		$a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
166
		$voucher = $a_vouchers[0];
167
		$attr = array(
168
			'voucher' => 1,
169
			'session_timeout' => $timecredit*60,
170
			'session_terminate_time' => 0);
171
		if (portal_allow($clientip, $clientmac, $voucher, null, $attr, null, 'voucher', 'voucher')) {
172
			// YES: user is good for $timecredit minutes.
173
			captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
174
		} else {
175
			portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
176
		}
177
	} elseif (-1 == $timecredit) {  // valid but expired
178
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
179
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
180
	} else {
181
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
182
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgnoaccess'] ? $config['voucher'][$cpzone]['descrmsgnoaccess'] : $errormsg);
183
	}
184

    
185
} elseif ($_POST['accept'] || $cpcfg['auth_method'] === 'radmac') {
186
	
187
		if ($cpcfg['auth_method'] === 'radmac' && !isset($_POST['accept'])) {
188
			$user = $clientmac; 
189
			$passwd = $cpcfg['radmac_secret'];
190
			$context = 'radmac'; // Radius MAC authentication
191
		} elseif (!empty(trim($_POST['auth_user2']))) { 
192
			$user = trim($_POST['auth_user2']);
193
			$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
			$user = trim($_POST['auth_user']);
197
			$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
		} else {
210
			portal_reply_page($redirurl, "error", $replymsg);
211
		}
212
		log_error("Zone: {$cpzone} - WARNING!  Captive portal has reached maximum login capacity");
213
		
214
	}
215
	
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

    
222
	} 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
		}
230
		
231
		if ($auth_result['login_message']) {
232
			$replymsg = $auth_result['login_message'];
233
		} else {
234
			$replymsg = gettext("Invalid credentials specified.");
235
		}
236
		
237
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status'], $replymsg);
238

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

    
251
ob_flush();
252

    
253
?>
(2-2/2)