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
	pfSense_MODULE:	captiveportal
33
*/
34

    
35
require_once("auth.inc");
36
require_once("functions.inc");
37
require_once("captiveportal.inc");
38

    
39
$errormsg = "Invalid credentials specified.";
40

    
41
header("Expires: 0");
42
header("Cache-Control: no-cache, no-store, must-revalidate");
43
header("Pragma: no-cache");
44
header("Connection: close");
45

    
46
global $cpzone, $cpzoneid;
47

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

    
57
$cpzoneid = $cpcfg['zoneid'];
58

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

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

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

    
81
	$sessionid = $cpsession['sessionid'];
82
	$attributes = array();
83
	if (!empty($cpsession['session_timeout']))
84
		$attributes['session_timeout'] = $cpsession['session_timeout'];
85
	if (!empty($cpsession['session_terminate_time']))
86
		$attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
87

    
88
	include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
89
	ob_flush();
90
	return;
91
} else if ($orig_host != $ourhostname) {
92
	/* the client thinks it's connected to the desired web server, but instead
93
	   it's connected to us. Issue a redirect... */
94
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
95
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
96

    
97
	ob_flush();
98
	return;
99
}
100

    
101
if (!empty($cpcfg['redirurl'])) {
102
	$redirurl = $cpcfg['redirurl'];
103
} else if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
104
	$redirurl = urldecode($matches[1]);
105
} else if ($_REQUEST['redirurl']) {
106
	$redirurl = $_REQUEST['redirurl'];
107
}
108

    
109
$macfilter = !isset($cpcfg['nomacfilter']);
110
$passthrumac = isset($cpcfg['passthrumacadd']);
111

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

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

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

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

    
157
EOD;
158
	captiveportal_disconnect_client($_POST['logout_id']);
159

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

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

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

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

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

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

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

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

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

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

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

    
266
ob_flush();
267

    
268
?>
(1-1/3)