Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	part of m0n0wall (http://m0n0.ch/wall)
4

    
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	Copyright (C) 2009 Ermal Luçi
7
	Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
require_once("auth.inc");
33
require_once("functions.inc");
34
require_once("captiveportal.inc");
35

    
36
$errormsg = "Invalid credentials specified.";
37

    
38
header("Expires: 0");
39
header("Cache-Control: no-cache, no-store, must-revalidate");
40
header("Pragma: no-cache");
41
header("Connection: close");
42

    
43
global $cpzone, $cpzoneid;
44

    
45
$cpzone = $_REQUEST['zone'];
46
$cpcfg = $config['captiveportal'][$cpzone];
47
if (empty($cpcfg)) {
48
	log_error("Submission to captiveportal with unknown parameter zone: " . htmlspecialchars($cpzone));
49
	portal_reply_page($redirurl, "error", $errormsg);
50
	ob_flush();
51
	return;
52
}
53

    
54
$cpzoneid = $cpcfg['zoneid'];
55

    
56
$orig_host = $_SERVER['HTTP_HOST'];
57
/* NOTE: IE 8/9 is buggy and that is why this is needed */
58
$orig_request = trim($_REQUEST['redirurl'], " /");
59
$clientip = $_SERVER['REMOTE_ADDR'];
60

    
61
if (!$clientip) {
62
	/* not good - bail out */
63
	log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
64
	$error_message = "An error occurred.  Please check the system logs for more information.";
65
	portal_reply_page($redirurl, "error", $errormsg);
66
	ob_flush();
67
	return;
68
}
69

    
70
$cpsession = captiveportal_isip_logged($clientip);
71
$ourhostname = portal_hostname_from_client_ip($clientip);
72
/* Automatically switching to the logout page requires a custom logout page to be present. */
73
if ((!empty($cpsession)) && (! $_POST['logout_id']) && (!empty($cpcfg['page']['logouttext']))) {
74
	/* if client already logged in so show logout page */
75
	$protocol = (isset($config['captiveportal'][$cpzone]['httpslogin'])) ? 'https://' : 'http://';
76
	$logouturl = "{$protocol}{$ourhostname}/";
77

    
78
	$sessionid = $cpsession['sessionid'];
79
	$attributes = array();
80
	if (!empty($cpsession['session_timeout']))
81
		$attributes['session_timeout'] = $cpsession['session_timeout'];
82
	if (!empty($cpsession['session_terminate_time']))
83
		$attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
84

    
85
	include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
86
	ob_flush();
87
	return;
88
} else if ($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 (!empty($cpcfg['redirurl'])) {
99
	$redirurl = $cpcfg['redirurl'];
100
} else if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
101
	$redirurl = urldecode($matches[1]);
102
} else if ($_REQUEST['redirurl']) {
103
	$redirurl = $_REQUEST['redirurl'];
104
}
105

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

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

    
124
/* find out if we need RADIUS + RADIUSMAC or not */
125
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
126
	$radius_enable = TRUE;
127
	if (isset($cpcfg['radmac_enable'])) {
128
		$radmac_enable = TRUE;
129
	}
130
}
131

    
132
/* find radius context */
133
$radiusctx = 'first';
134
if ($_POST['auth_user2']) {
135
	$radiusctx = 'second';
136
}
137

    
138
if ($_POST['logout_id']) {
139
	echo <<<EOD
140
<html>
141
<head><title>Disconnecting...</title></head>
142
<body bgcolor="#435370">
143
<span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
144
<b>You have been disconnected.</b>
145
</span>
146
<script type="text/javascript">
147
<!--
148
setTimeout('window.close();',5000) ;
149
-->
150
</script>
151
</body>
152
</html>
153

    
154
EOD;
155

    
156
	$safe_logout_id = SQLite3::escapeString($_POST['logout_id']);
157
	captiveportal_disconnect_client($safe_logout_id);
158

    
159
} else if ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
160
	captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
161
	if (!empty($cpcfg['blockedmacsurl'])) {
162
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
163
	} else {
164
		portal_reply_page($redirurl, "error", "This MAC address has been blocked");
165
	}
166

    
167
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac, $clientip, $radiusctx)) {
168
	/* radius functions handle everything so we exit here since we're done */
169

    
170
} else if (portal_consume_passthrough_credit($clientmac)) {
171
	/* allow the client through if it had a pass-through credit for its MAC */
172
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
173
	portal_allow($clientip, $clientmac, "unauthenticated");
174

    
175
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
176
	$voucher = trim($_POST['auth_voucher']);
177
	$timecredit = voucher_auth($voucher);
178
	// $timecredit contains either a credit in minutes or an error message
179
	if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
180
		// if multiple vouchers given, use the first as username
181
		$a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
182
		$voucher = $a_vouchers[0];
183
		$attr = array(
184
			'voucher' => 1,
185
			'session_timeout' => $timecredit*60,
186
			'session_terminate_time' => 0);
187
		if (portal_allow($clientip, $clientmac, $voucher, null, $attr)) {
188
			// YES: user is good for $timecredit minutes.
189
			captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
190
		} else {
191
			portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
192
		}
193
	} else if (-1 == $timecredit) {  // valid but expired
194
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
195
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgexpired'] ? $config['voucher'][$cpzone]['descrmsgexpired']: $errormsg);
196
	} else {
197
		captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
198
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['descrmsgnoaccess'] ? $config['voucher'][$cpzone]['descrmsgnoaccess'] : $errormsg);
199
	}
200

    
201
} else if ($_POST['accept'] && $radius_enable) {
202
	if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
203
		if (!empty($_POST['auth_user'])) {
204
			$user = $_POST['auth_user'];
205
			$paswd = $_POST['auth_pass'];
206
		} else if (!empty($_POST['auth_user2'])) {
207
			$user = $_POST['auth_user2'];
208
			$paswd = $_POST['auth_pass2'];
209
		}
210
		$auth_list = radius($user, $paswd, $clientip, $clientmac, "USER LOGIN", $radiusctx);
211
		$type = "error";
212
		if (!empty($auth_list['url_redirection'])) {
213
			$redirurl = $auth_list['url_redirection'];
214
			$type = "redir";
215
		}
216

    
217
		if ($auth_list['auth_val'] == 1) {
218
			captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR", $auth_list['error']);
219
			portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
220
		} else if ($auth_list['auth_val'] == 3) {
221
			captiveportal_logportalauth($user, $clientmac, $clientip, "FAILURE", $auth_list['reply_message']);
222
			portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
223
		}
224
	} else {
225
		if (!empty($_POST['auth_user'])) {
226
			$user = $_POST['auth_user'];
227
		} else if (!empty($_POST['auth_user2'])) {
228
			$user = $_POST['auth_user2'];
229
		} else {
230
			$user = 'unknown';
231
		}
232
		captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR");
233
		portal_reply_page($redirurl, "error", $errormsg);
234
	}
235

    
236
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
237
	if ($_POST['auth_user'] && $_POST['auth_pass']) {
238
		//check against local user manager
239
		$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
240

    
241
		if ($loginok && isset($cpcfg['localauth_priv'])) {
242
			$loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
243
		}
244

    
245
		if ($loginok) {
246
			captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "LOGIN");
247
			portal_allow($clientip, $clientmac, $_POST['auth_user']);
248
		} else {
249
			captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "FAILURE");
250
			portal_reply_page($redirurl, "error", $errormsg);
251
		}
252
	} else {
253
		portal_reply_page($redirurl, "error", $errormsg);
254
	}
255

    
256
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
257
	captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
258
	portal_allow($clientip, $clientmac, "unauthenticated");
259

    
260
} else {
261
	/* display captive portal page */
262
	portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
263
}
264

    
265
ob_flush();
266

    
267
?>
(1-1/3)