Project

General

Profile

Download (9.31 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-cache, no-store, must-revalidate");
44
header("Pragma: no-cache");
45
header("Connection: close");
46

    
47
global $cpzone, $cpzoneid;
48

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

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

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

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

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

    
81
	ob_flush();
82
	return;
83
}
84

    
85
if (!empty($cpcfg['redirurl'])) {
86
	$redirurl = $cpcfg['redirurl'];
87
} else if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
88
	$redirurl = urldecode($matches[1]);
89
} else if ($_REQUEST['redirurl']) {
90
	$redirurl = $_REQUEST['redirurl'];
91
}
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

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

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

    
141
EOD;
142
	captiveportal_disconnect_client($_POST['logout_id']);
143

    
144
} else if ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
145
	captiveportal_logportalauth($clientmac,$clientmac,$clientip,"Blocked MAC address");
146
	if (!empty($cpcfg['blockedmacsurl'])) {
147
		portal_reply_page($cpcfg['blockedmacsurl'], "redir");
148
	} else {
149
		portal_reply_page($redirurl, "error", "This MAC address has been blocked");
150
	}
151

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

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

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

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

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

    
220
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
221
	if ($_POST['auth_user'] && $_POST['auth_pass']) {
222
		//check against local user manager
223
		$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
224

    
225
		if ($loginok && isset($cpcfg['localauth_priv'])) {
226
			$loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
227
		}
228

    
229
		if ($loginok){
230
			captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
231
			portal_allow($clientip, $clientmac,$_POST['auth_user']);
232
		} else {
233
			captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
234
			portal_reply_page($redirurl, "error", $errormsg);
235
		}
236
	} else {
237
		portal_reply_page($redirurl, "error", $errormsg);
238
	}
239

    
240
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
241
	captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
242
	portal_allow($clientip, $clientmac, "unauthenticated");
243

    
244
} else {
245
	/* display captive portal page */
246
	portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
247
}
248

    
249
ob_flush();
250

    
251
?>
(1-1/3)