Project

General

Profile

Download (13.9 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-2021 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;
39

    
40
$cpzone = strtolower($_REQUEST['zone']);
41
$cpcfg = $config['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
$orig_host = $_SERVER['HTTP_HOST'];
68
$clientip = $_SERVER['REMOTE_ADDR'];
69

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

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

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

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

    
97
	include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
98
	ob_flush();
99
	return;
100
} elseif (!empty($cpsession) && !isset($_POST['logout_id'])) {
101
	/* If the client tries to access the captive portal page while already connected,
102
		but no custom logout page exists */
103
	$logo_src = get_captive_portal_logo();
104
	$bg_src = get_captive_portal_bg();
105
?>
106
<!DOCTYPE html>
107
<html>
108
<head>
109
  <meta charset="UTF-8">
110
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
111
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
112
  <title>Captive Portal</title>
113
  <style>
114
	  #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;}
115
  </style>
116
</head>
117

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

    
153
	ob_flush();
154
	return;
155
}
156

    
157
$macfilter = !isset($cpcfg['nomacfilter']);
158

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

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

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

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

    
269
	} else {
270
		captiveportal_free_dn_ruleno($pipeno);
271
		$type = "error";
272
			
273
		if (is_URL($auth_result['attributes']['url_redirection'], true)) {
274
			$redirurl = $auth_result['attributes']['url_redirection'];
275
			$type = "redir";
276
		}
277
		
278
		if ($auth_result['login_message']) {
279
			$replymsg = $auth_result['login_message'];
280
		} else {
281
			$replymsg = gettext("Invalid credentials specified.");
282
		}
283
		
284
		captiveportal_logportalauth($user, $clientmac, $clientip, $auth_result['login_status'], $replymsg);
285

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

    
298
ob_flush();
299

    
300
?>
(2-2/2)