Projet

Général

Profil

Télécharger (9,24 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / captiveportal / index.php @ 762a7b89

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, $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
$macfilter = !isset($cpcfg['nomacfilter']);
93
$passthrumac = isset($cpcfg['passthrumacadd']);
94

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
242
ob_flush();
243

    
244
?>
(1-1/3)