Project

General

Profile

Download (14.1 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-2024 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("util.inc");
30
require_once("functions.inc");
31
require_once("captiveportal.inc");
32

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

    
38
global $cpzone, $cpzoneid, $cpzoneprefix;
39

    
40
$cpzone = strtolower($_REQUEST['zone']);
41
$cpcfg = config_get_path("captiveportal/{$cpzone}");
42

    
43
/* NOTE: IE 8/9 is buggy and that is why this is needed */
44
$orig_request = trim($_REQUEST['redirurl'], " /");
45

    
46
/* If the post-auth redirect is set, always use it. Otherwise take what was supplied in URL. */
47
if (!empty($cpcfg) && is_URL($cpcfg['redirurl'], true)) {
48
	$redirurl = $cpcfg['redirurl'];
49
} elseif (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
50
	$redirurl = urldecode($matches[1]);
51
} elseif ($_REQUEST['redirurl']) {
52
	$redirurl = $_REQUEST['redirurl'];
53
}
54
/* Sanity check: If the redirect target is not a URL, do not attempt to use it like one. */
55
if (!is_URL(urldecode($redirurl), true)) {
56
	$redirurl = "";
57
}
58

    
59
if (empty($cpcfg)) {
60
	log_error("Submission to captiveportal with unknown parameter zone: " . htmlspecialchars($cpzone));
61
	portal_reply_page($redirurl, "error", gettext("Internal error"));
62
	ob_flush();
63
	return;
64
}
65

    
66
$cpzoneid = $cpcfg['zoneid'];
67
$cpzoneprefix = CPPREFIX . $cpzoneid;
68
$orig_host = $_SERVER['HTTP_HOST'];
69
$clientip = $_SERVER['REMOTE_ADDR'];
70

    
71
if (!$clientip) {
72
	/* not good - bail out */
73
	log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
74
	$errormsg = gettext("An error occurred.  Please check the system logs for more information.");
75
	portal_reply_page($redirurl, "error", $errormsg);
76
	ob_flush();
77
	return;
78
}
79

    
80
$ourhostname = portal_hostname_from_client_ip($clientip);
81
$protocol = (isset($config['captiveportal'][$cpzone]['httpslogin'])) ? 'https://' : 'http://';
82
$logouturl = "{$protocol}{$ourhostname}/";
83

    
84
$cpsession = captiveportal_isip_logged($clientip);
85
if (!empty($cpsession)) {
86
	$sessionid = $cpsession['sessionid'];
87
}
88

    
89
/* Automatically switching to the logout page requires a custom logout page to be present. */
90
if ((!empty($cpsession)) && (! $_POST['logout_id']) && (!empty($cpcfg['page']['logouttext']))) {
91
	/* if client already connected and a custom logout page is set : show logout page */
92
	$attributes = array();
93
	if (!empty($cpsession['session_timeout']))
94
		$attributes['session_timeout'] = $cpsession['session_timeout'];
95
	if (!empty($cpsession['session_terminate_time']))
96
		$attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
97

    
98
	include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
99
	ob_flush();
100
	return;
101
} elseif (!empty($cpsession) && !isset($_POST['logout_id'])) {
102
	/* If the client tries to access the captive portal page while already connected,
103
		but no custom logout page exists */
104
	$logo_src = get_captive_portal_logo();
105
	$bg_src = get_captive_portal_bg();
106
?>
107
<!DOCTYPE html>
108
<html>
109
<head>
110
  <meta charset="UTF-8">
111
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
112
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
113
  <title>Captive Portal</title>
114
  <style>
115
	  #content,.login,.login-card a,.login-card h1,.login-help{text-align:center}body,html{margin:0;padding:0;width:100%;height:100%;display:table}#content{font-family:'Source Sans Pro',sans-serif;background-color:#1C1275;background:<?= $bg_src ?>;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;display:table-cell;vertical-align:middle}.login-card{padding:40px;width:280px;background-color:#F7F7F7;margin:100px auto 10px;border-radius:2px;box-shadow:0 2px 2px rgba(0,0,0,.3);overflow:hidden}.login-card h1{font-weight:400;font-size:2.3em;color:#1383c6}.login-card h1 span{color:#f26721}.login-card img{width:70%;height:70%}.login-card input[type=submit]{width:100%;display:block;margin-bottom:10px;position:relative}.login-card input[type=text],input[type=password]{height:44px;font-size:16px;width:100%;margin-bottom:10px;-webkit-appearance:none;background:#fff;border:1px solid #d9d9d9;border-top:1px solid silver;padding:0 8px;box-sizing:border-box;-moz-box-sizing:border-box}.login-card input[type=text]:hover,input[type=password]:hover{border:1px solid #b9b9b9;border-top:1px solid #a0a0a0;-moz-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);-webkit-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);box-shadow:inset 0 1px 2px rgba(0,0,0,.1)}.login{font-size:14px;font-family:Arial,sans-serif;font-weight:700;height:36px;padding:0 8px}.login-submit{-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;color:#fff;text-shadow:0 1px rgba(0,0,0,.1);background-color:#4d90fe}.login-submit:disabled{opacity:.6}.login-submit:hover{border:0;text-shadow:0 1px rgba(0,0,0,.3);background-color:#357ae8}.login-card a{text-decoration:none;color:#222;font-weight:400;display:inline-block;opacity:.6;transition:opacity ease .5s}.login-card a:hover{opacity:1}.login-help{width:100%;font-size:12px}.list{list-style-type:none;padding:0}.list__item{margin:0 0 .7rem;padding:0}label{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-align:center;-webkit-align-items:center;-ms-flex-align:center;align-items:center;text-align:left;font-size:14px;}input[type=checkbox]{-webkit-box-flex:0;-webkit-flex:none;-ms-flex:none;flex:none;margin-right:10px;float:left}@media screen and (max-width:450px){.login-card{width:70%!important}.login-card img{width:30%;height:30%}}textarea{width:66%;margin:auto;height:120px;max-height:120px;background-color:#f7f7f7;padding:20px}#terms{display:none;padding-top:100px;padding-bottom:300px;}.auth_source{border: 1px solid lightgray; padding:20px 8px 0px 8px; margin-top: -2em; border-radius: 2px; }.auth_head{background-color:#f7f7f7;display:inline-block;}.auth_head_div{text-align:left;}#error-message{text-align:left;color:#ff3e3e;font-style:italic;}
116
  </style>
117
</head>
118

    
119
<body>
120
<div id="content">
121
	<div class="login-card">
122
		<img src="<?= $logo_src ?>"/><br>
123
		<h1></h1>
124
		<div class="login-help">
125
			<?= gettext("The portal session is connected.") ?>
126
<?php if (!empty($redirurl)):
127
		$redirurl = htmlspecialchars($redirurl); ?>
128
			<br/><br/>
129
			<?= gettext("Proceed to: ") ?>
130
			<a href="<?=$redirurl?>"><?=$redirurl?></a>
131
<?php endif; ?>
132
		</div>
133
<br/>
134
	<form method="POST" action="<?=$logouturl;?>">
135
		<input name="logout_id" type="hidden" value="<?=$sessionid;?>" />
136
		<input name="zone" type="hidden" value="<?=$cpzone;?>" />
137
		<input name="logout" type="submit" value="<?= gettext("Disconnect") ?>" />
138
	</form>
139
	<br  />
140
	<span> <i>Made with &hearts; by</i> <strong>Netgate</strong></span>
141
	</div>
142
</div>
143
</body>
144
</html>
145
<?php
146
	ob_flush();
147
	return;
148
} elseif ($orig_host != $ourhostname) {
149
	/* the client thinks it's connected to the desired web server, but instead
150
	   it's connected to us. Issue a redirect... */
151
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
152
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
153

    
154
	ob_flush();
155
	return;
156
}
157

    
158
/* find MAC address for client */
159
$tmpres = pfSense_ip_to_mac($clientip);
160
if (!is_array($tmpres)) {
161
	if (!isset($cpcfg['nomacfilter']) || isset($cpcfg['passthrumacadd'])) {
162
		/* unable to find MAC address - shouldn't happen! - bail out */
163
		captiveportal_logportalauth("unauthenticated", "noclientmac", $clientip, "ERROR");
164
		echo "An error occurred.  Please check the system logs for more information.";
165
		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.");
166
		ob_flush();
167
		return;
168
	}
169
} else {
170
	/* always save MAC address in DB to allow macfilter/nomacfilter switching without flushing all clients */
171
	$clientmac = $tmpres['macaddr'];
172
}
173
unset($tmpres);
174

    
175
if ($_POST['logout_id']) {
176
	$safe_logout_id = SQLite3::escapeString($_POST['logout_id']);
177
	captiveportal_disconnect_client($safe_logout_id);
178
	header("Location: index.php?zone=" . $cpzone);
179
	ob_flush();
180
	return;
181
} elseif (($_POST['accept'] || $cpcfg['auth_method'] === 'radmac' || !empty($cpcfg['blockedmacsurl'])) && !isset($cpcfg['nomacfilter']) && $clientmac && captiveportal_blocked_mac($clientmac)) {
182
	captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
183
	if (!empty($cpcfg['blockedmacsurl'])) {
184
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
185
	} else {
186
		if ($cpcfg['auth_method'] === 'radmac') {
187
			echo gettext("This MAC address has been blocked");
188
		} else {
189
			portal_reply_page($redirurl, "error", "This MAC address has been blocked", $clientmac, $clientip);
190
		}
191
	}
192
} elseif (portal_consume_passthrough_credit($clientmac)) {
193
	/* allow the client through if it had a pass-through credit for its MAC */
194
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
195
	portal_allow($clientip, $clientmac, "unauthenticated", null, $redirurl);
196

    
197
} elseif (isset($config['voucher'][$cpzone]['enable']) && ($_POST['accept'] && $_POST['auth_voucher']) || $_GET['voucher']) {
198
	if (isset($_POST['auth_voucher'])) {
199
		$voucher = trim($_POST['auth_voucher']);
200
	} else {
201
		/* submit voucher via URL, see https://redmine.pfsense.org/issues/1984 */
202
		$voucher = trim($_GET['voucher']);
203
		portal_reply_page($redirurl, "login", null, $clientmac, $clientip, null, null, $voucher);
204
		return;
205
	}
206
	$errormsg = gettext("Invalid credentials specified.");
207
	$timecredit = voucher_auth($voucher);
208
	// $timecredit contains either a credit in minutes or an error message
209
	if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
210
		// if multiple vouchers given, use the first as username
211
		$a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
212
		$voucher = $a_vouchers[0];
213
		$attr = array(
214
			'voucher' => 1,
215
			'session_timeout' => $timecredit*60,
216
			'session_terminate_time' => 0);
217
		if (portal_allow($clientip, $clientmac, $voucher, null, $redirurl, $attr, null, 'voucher', 'voucher') === 2) {
218
			portal_reply_page($redirurl, "error", "Reuse of identification not allowed.", $clientmac, $clientip);
219
		} elseif (portal_allow($clientip, $clientmac, $voucher, null, $redirurl, $attr, null, 'voucher', 'voucher')) {
220
			// YES: user is good for $timecredit minutes.
221
			captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
222
		} else {
223
			portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg, $clientmac, $clientip);
224
		}
225
	} elseif (-1 == $timecredit) {  // valid but expired
226
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
227
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg, $clientmac, $clientip);
228
	} else {
229
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
230
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgnoaccess'] ? $config['voucher'][$cpzone]['descrmsgnoaccess'] : $errormsg, $clientmac, $clientip);
231
	}
232

    
233
} elseif ($_POST['accept'] || $cpcfg['auth_method'] === 'radmac') {
234
	
235
		if ($cpcfg['auth_method'] === 'radmac' && !isset($_POST['accept'])) {
236
			$user = $clientmac; 
237
			$passwd = $cpcfg['radmac_secret'];
238
			$context = 'radmac'; // Radius MAC authentication
239
		} elseif (!empty(trim($_POST['auth_user2']))) { 
240
			$user = trim($_POST['auth_user2']);
241
			$passwd = $_POST['auth_pass2'];
242
			$context = 'second'; // Assume users to use the first context if auth_user2 is empty/does not exist
243
		} else {
244
			$user = trim($_POST['auth_user']);
245
			$passwd = $_POST['auth_pass'];
246
			$context = 'first';
247
		}
248
	
249
	$pipeno = captiveportal_get_next_dn_ruleno('auth', 2000, 64500, true);
250
	/* if the pool is empty, return appropriate message and exit */
251
	if (is_null($pipeno)) {
252
		$replymsg = gettext("System reached maximum login capacity");
253
		if ($cpcfg['auth_method'] === 'radmac') {
254
			echo $replymsg;
255
			ob_flush();
256
			return;
257
		} else {
258
			portal_reply_page($redirurl, "error", $replymsg, $clientmac, $clientip);
259
		}
260
		log_error("Zone: {$cpzone} - WARNING!  Captive portal has reached maximum login capacity");
261
		
262
	}
263
	
264
	$auth_result = captiveportal_authenticate_user($user, $passwd, $clientmac, $clientip, $pipeno, $context);
265
	
266
	if ($auth_result['result']) {
267
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status']);
268
		portal_allow($clientip, $clientmac, $user, $passwd, $redirurl, $auth_result['attributes'], null, $auth_result['auth_method'], $context);
269
	} else {
270
		$type = "error";
271
			
272
		if (is_URL($auth_result['attributes']['url_redirection'], true)) {
273
			$redirurl = $auth_result['attributes']['url_redirection'];
274
			$type = "redir";
275
		}
276
		
277
		if ($auth_result['login_message']) {
278
			$replymsg = $auth_result['login_message'];
279
		} else {
280
			$replymsg = gettext("Invalid credentials specified.");
281
		}
282
		
283
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status'], $replymsg);
284

    
285
		/* Radius MAC authentication. */
286
		if ($context === 'radmac' && $type !== 'redir' && !isset($cpcfg['radmac_fallback'])) {
287
			echo $replymsg;
288
		} else {
289
			portal_reply_page($redirurl, $type, $replymsg, $clientmac, $clientip);
290
		}
291
	}
292
} else {
293
	/* display captive portal page */
294
	portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
295
}
296

    
297
ob_flush();
298

    
299
?>
(2-2/2)