Project

General

Profile

Download (9.37 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
250
ob_flush();
251

    
252
?>
(1-1/3)