Project

General

Profile

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

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

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

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

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

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

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

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

    
42
header("Expires: 0");
43
header("Cache-Control: no-store, no-cache, must-revalidate");
44
header("Cache-Control: post-check=0, pre-check=0", false);
45
header("Pragma: no-cache");
46
header("Connection: close");
47

    
48
global $cpzone, $cpzoneid;
49

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

    
59
$cpzoneid = $cpcfg['zoneid'];
60

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

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

    
75
$ourhostname = portal_hostname_from_client_ip($clientip);
76
if ($orig_host != $ourhostname) {
77
	/* the client thinks it's connected to the desired web server, but instead
78
	   it's connected to us. Issue a redirect... */
79
	$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
80
	header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
81

    
82
	ob_flush();
83
	return;
84
}
85

    
86
if (!empty($cpcfg['redirurl']))
87
	$redirurl = $cpcfg['redirurl'];
88
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
89
	$redirurl = urldecode($matches[1]);
90
else if ($_REQUEST['redirurl'])
91
	$redirurl = $_REQUEST['redirurl'];
92

    
93
$macfilter = !isset($cpcfg['nomacfilter']);
94
$passthrumac = isset($cpcfg['passthrumacadd']);
95

    
96
/* find MAC address for client */
97
if ($macfilter || $passthrumac) {
98
	$tmpres = pfSense_ip_to_mac($clientip);
99
	if (!is_array($tmpres)) {
100
		/* unable to find MAC address - shouldn't happen! - bail out */
101
		captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
102
		echo "An error occurred.  Please check the system logs for more information.";
103
		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.");
104
		ob_flush();
105
		return;
106
	}
107
	$clientmac = $tmpres['macaddr'];
108
	unset($tmpres);
109
}
110

    
111
/* find out if we need RADIUS + RADIUSMAC or not */
112
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
113
	$radius_enable = TRUE;
114
	if (isset($cpcfg['radmac_enable']))
115
		$radmac_enable = TRUE;
116
}
117

    
118
/* find radius context */
119
$radiusctx = 'first';
120
if ($_POST['auth_user2'])
121
	$radiusctx = 'second';
122

    
123
if ($_POST['logout_id']) {
124
	echo <<<EOD
125
<html>
126
<head><title>Disconnecting...</title></head>
127
<body bgcolor="#435370">
128
<span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
129
<b>You have been disconnected.</b>
130
</span>
131
<script type="text/javascript">
132
<!--
133
setTimeout('window.close();',5000) ;
134
-->
135
</script>
136
</body>
137
</html>
138

    
139
EOD;
140
	captiveportal_disconnect_client($_POST['logout_id']);
141

    
142
} else if ($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
		portal_reply_page($redirurl, "error", "This MAC address has been blocked");
148

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

    
152
} else if (portal_consume_passthrough_credit($clientmac)) {
153
	/* allow the client through if it had a pass-through credit for its MAC */
154
	captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
155
	portal_allow($clientip, $clientmac, "unauthenticated");
156

    
157
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
158
	$voucher = trim($_POST['auth_voucher']);
159
	$timecredit = voucher_auth($voucher);
160
	// $timecredit contains either a credit in minutes or an error message
161
	if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
162
		// if multiple vouchers given, use the first as username
163
		$a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher);
164
		$voucher = $a_vouchers[0];
165
		$attr = array( 'voucher' => 1,
166
				'session_timeout' => $timecredit*60,
167
				'session_terminate_time' => 0);
168
		if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
169
			// YES: user is good for $timecredit minutes.
170
			captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
171
		} else {
172
			portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
173
		}
174
	} else if (-1 == $timecredit) {  // valid but expired
175
		captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
176
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
177
	} else {
178
		captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
179
		portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
180
	}
181

    
182
} else if ($_POST['accept'] && $radius_enable) {
183
	if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
184
		if (!empty($_POST['auth_user'])) {
185
			$user = $_POST['auth_user'];
186
			$paswd = $_POST['auth_pass'];
187
		} else if (!empty($_POST['auth_user2'])) {
188
			$user = $_POST['auth_user2'];
189
			$paswd = $_POST['auth_pass2'];
190
		}
191
		$auth_list = radius($user,$paswd,$clientip,$clientmac,"USER LOGIN", $radiusctx);
192
		$type = "error";
193
		if (!empty($auth_list['url_redirection'])) {
194
			$redirurl = $auth_list['url_redirection'];
195
			$type = "redir";
196
		}
197

    
198
		if ($auth_list['auth_val'] == 1) {
199
			captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']);
200
			portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
201
		} else if ($auth_list['auth_val'] == 3) {
202
			captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
203
			portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
204
		}
205
	} else {
206
		if (!empty($_POST['auth_user']))
207
			$user = $_POST['auth_user'];
208
		else if (!empty($_POST['auth_user2']))
209
			$user = $_POST['auth_user2'];
210
		else 
211
			$user = 'unknown';
212
		captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR");
213
		portal_reply_page($redirurl, "error", $errormsg);
214
	}
215

    
216
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
217
	if ($_POST['auth_user'] && $_POST['auth_pass']) {
218
		//check against local user manager
219
		$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
220

    
221
		if ($loginok && isset($cpcfg['localauth_priv']))
222
			$loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
223

    
224
		if ($loginok){
225
			captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
226
			portal_allow($clientip, $clientmac,$_POST['auth_user']);
227
		} else {
228
			captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
229
			portal_reply_page($redirurl, "error", $errormsg);
230
		}
231
	} else
232
		portal_reply_page($redirurl, "error", $errormsg);
233

    
234
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
235
	captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
236
	portal_allow($clientip, $clientmac, "unauthenticated");
237

    
238
} else {
239
	/* display captive portal page */
240
	portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
241
}
242

    
243
ob_flush();
244

    
245
?>
(1-1/3)