Project

General

Profile

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

    
6
	Copyrigth (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-store, no-cache, must-revalidate");
43
header("Cache-Control: post-check=0, pre-check=0", false);
44
header("Pragma: no-cache");
45
header("Connection: close");
46

    
47
global $cpzone;
48

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

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

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

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

    
79
	ob_flush();
80
	return;
81
}
82

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

    
90
$macfilter = !isset($cpcfg['nomacfilter']);
91
$passthrumac = isset($cpcfg['passthrumacadd']);
92

    
93
/* find MAC address for client */
94
if ($macfilter || $passthrumac) {
95
	$tmpres = pfSense_ip_to_mac($clientip);
96
	if (!is_array($tmpres)) {
97
		/* unable to find MAC address - shouldn't happen! - bail out */
98
		captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
99
		echo "An error occurred.  Please check the system logs for more information.";
100
		log_error("Captive portal could not determine client's MAC address.  Disable MAC address filtering in captive portal if you do not need this functionality.");
101
		ob_flush();
102
		return;
103
	}
104
	$clientmac = $tmpres['macaddr'];
105
	unset($tmpres);
106
}
107

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

    
115
/* find radius context */
116
$radiusctx = 'first';
117
if ($_POST['auth_user2'])
118
	$radiusctx = 'second';
119

    
120
if ($_POST['logout_id']) {
121
	echo <<<EOD
122
<HTML>
123
<HEAD><TITLE>Disconnecting...</TITLE></HEAD>
124
<BODY BGCOLOR="#435370">
125
<SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
126
<B>You have been disconnected.</B>
127
</SPAN>
128
<SCRIPT LANGUAGE="JavaScript">
129
<!--
130
setTimeout('window.close();',5000) ;
131
-->
132
</SCRIPT>
133
</BODY>
134
</HTML>
135

    
136
EOD;
137
	captiveportal_disconnect_client($_POST['logout_id']);
138

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

    
142
} else if (portal_consume_passthrough_credit($clientmac)) {
143
	/* allow the client through if it had a pass-through credit for its MAC */
144
	captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
145
	portal_allow($clientip, $clientmac, "unauthenticated");
146

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

    
172
} else if ($_POST['accept'] && $radius_enable) {
173
	if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
174
		if (!empty($_POST['auth_user'])) {
175
			$user = $_POST['auth_user'];
176
			$paswd = $_POST['auth_pass'];
177
		} else if (!empty($_POST['auth_user2'])) {
178
			$user = $_POST['auth_user2'];
179
			$paswd = $_POST['auth_pass2'];
180
		}
181
		$auth_list = radius($user,$paswd,$clientip,$clientmac,"USER LOGIN", $radiusctx);
182
		$type = "error";
183
		if (!empty($auth_list['url_redirection'])) {
184
			$redirurl = $auth_list['url_redirection'];
185
			$type = "redir";
186
		}
187

    
188
		if ($auth_list['auth_val'] == 1) {
189
			captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']);
190
			portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
191
		} else if ($auth_list['auth_val'] == 3) {
192
			captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
193
			portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
194
		}
195
	} else {
196
		if (!empty($_POST['auth_user']))
197
			$user = $_POST['auth_user'];
198
		else if (!empty($_POST['auth_user2']))
199
			$user = $_POST['auth_user2'];
200
		else 
201
			$user = 'unknown';
202
		captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR");
203
		portal_reply_page($redirurl, "error", $errormsg);
204
	}
205

    
206
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
207
	if ($_POST['auth_user'] && $_POST['auth_pass']) {
208
		//check against local user manager
209
		$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
210

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

    
214
		if ($loginok){
215
			captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
216
			portal_allow($clientip, $clientmac,$_POST['auth_user']);
217
		} else {
218
			captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
219
			portal_reply_page($redirurl, "error", $errormsg);
220
		}
221
	} else
222
		portal_reply_page($redirurl, "error", $errormsg);
223

    
224
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
225
	captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
226
	portal_allow($clientip, $clientmac, "unauthenticated");
227

    
228
} else {
229
	/* display captive portal page */
230
	portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
231
}
232

    
233
ob_flush();
234

    
235
?>
(1-1/3)