Project

General

Profile

Download (9.46 KB) Statistics
| Branch: | Tag: | Revision:
1 8c1ce6c7 Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 65fbb388 Scott Ullrich
    $Id$
4
    part of m0n0wall (http://m0n0.ch/wall)
5
6 9568c1a1 Ermal Lu?i
    Copyrigth (C) 2009	    Ermal Lu?i
7 65fbb388 Scott Ullrich
    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 5b237745 Scott Ullrich
*/
31 f5024891 Scott Ullrich
/*
32
	pfSense_MODULE:	captiveportal
33
*/
34 5b237745 Scott Ullrich
35 0092b3bd mgrooms
require_once("auth.inc");
36 65fbb388 Scott Ullrich
require_once("functions.inc");
37 483e6de8 Scott Ullrich
require_once("captiveportal.inc");
38 65fbb388 Scott Ullrich
39 eb15decb Ermal
$errormsg = "Invalid credentials specified.";
40
41 65fbb388 Scott Ullrich
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 232846a2 Ermal
header("Connection: close");
46 5b237745 Scott Ullrich
47 b4792bf8 Ermal
$cpzone = $_REQUEST['zone'];
48
$cpcfg = $config['captiveportal'][$cpzone];
49
50 5b237745 Scott Ullrich
$orig_host = $_ENV['HTTP_HOST'];
51 6e895d5f Ermal
/* NOTE: IE 8/9 is buggy and that is why this is needed */
52 f89afb47 Ermal
$orig_request = trim($_REQUEST['redirurl'], " /");
53 e3c4516b Ermal
$orig_request = empty($orig_request) ? '' : $orig_request;
54 6fa4bdc6 Scott Ullrich
$clientip = $_SERVER['REMOTE_ADDR'];
55 5b237745 Scott Ullrich
56
if (!$clientip) {
57 c9cb32c4 Ermal
	/* not good - bail out */
58
	log_error("Captive portal could not determine client's IP address.");
59
	$error_message = "An error occurred.  Please check the system logs for more information.";
60
	portal_reply_page($redirurl, "error", $errormsg);
61 4a5feb83 Ermal
	ob_flush();
62
	return;
63 65fbb388 Scott Ullrich
}
64
65 470d24a3 Darren Embry
$listenporthttps = $cpcfg['listenporthttps'] ? $cpcfg['listenporthttps'] : ($cpcfg['zoneid'] + 1);
66
$listenporthttp  = $cpcfg['listenporthttp']  ? $cpcfg['listenporthttp']  : $cpcfg['zoneid'];
67
68 b7ae00c0 bcyrill
if (isset($cpcfg['httpslogin']))
69
	$ourhostname = $cpcfg['httpsname'] . ":" . $listenporthttps;
70 f8b11310 Ermal Lu?i
else {
71 ac10faad Warren Baker
	$ifip = portal_ip_from_client_ip($clientip);
72 905d1e8e bcyrill
	if (!$ifip) {
73 ac10faad Warren Baker
		$ourhostname = "{$config['system']['hostname']}.{$config['system']['domain']}:{$listenporthttp}";
74 905d1e8e bcyrill
	} else {
75
		if (is_ipaddrv6($ifip))
76
			$ourhostname = "[{$ifip}]:{$listenporthttp}";
77
		else
78
			$ourhostname = "{$ifip}:{$listenporthttp}";
79
	}
80 f8b11310 Ermal Lu?i
}
81 65fbb388 Scott Ullrich
82
if ($orig_host != $ourhostname) {
83
    /* the client thinks it's connected to the desired web server, but instead
84
       it's connected to us. Issue a redirect... */
85
86 b7ae00c0 bcyrill
    if (isset($cpcfg['httpslogin']))
87 f89afb47 Ermal
        header("Location: https://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
88 65fbb388 Scott Ullrich
    else
89 f89afb47 Ermal
        header("Location: http://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
90 65fbb388 Scott Ullrich
91 4a5feb83 Ermal
    ob_flush();
92
    return;
93 5b237745 Scott Ullrich
}
94 b7ae00c0 bcyrill
if (!empty($cpcfg['redirurl']))
95
	$redirurl = $cpcfg['redirurl'];
96 adbb495c Ermal
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
97
	$redirurl = urldecode($matches[1]);
98
else if ($_REQUEST['redirurl'])
99
	$redirurl = $_REQUEST['redirurl'];
100 65fbb388 Scott Ullrich
101 b7ae00c0 bcyrill
$macfilter = !isset($cpcfg['nomacfilter']);
102
$passthrumac = isset($cpcfg['passthrumacadd']);
103 65fbb388 Scott Ullrich
104 5b237745 Scott Ullrich
/* find MAC address for client */
105 007161dc Ermal
if ($macfilter || $passthrumac) {
106 0d20a040 Ermal
	$tmpres = pfSense_ip_to_mac($clientip);
107
	if (!is_array($tmpres)) {
108 007161dc Ermal
	    /* unable to find MAC address - shouldn't happen! - bail out */
109
	    captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
110
	    echo "An error occurred.  Please check the system logs for more information.";
111
	    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.");
112 4a5feb83 Ermal
	    ob_flush();
113
	    return;
114 007161dc Ermal
	}
115 0d20a040 Ermal
	$clientmac = $tmpres['macaddr'];
116
	unset($tmpres);
117 5b237745 Scott Ullrich
}
118
119 65fbb388 Scott Ullrich
/* find out if we need RADIUS + RADIUSMAC or not */
120 b4792bf8 Ermal
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
121 65fbb388 Scott Ullrich
    $radius_enable = TRUE;
122 b7ae00c0 bcyrill
    if (isset($cpcfg['radmac_enable']))
123 65fbb388 Scott Ullrich
        $radmac_enable = TRUE;
124
}
125 6e865a74 Scott Ullrich
126 ebc0e4b6 Ermal
/* find radius context */
127
$radiusctx = 'first';
128
if ($_POST['auth_user2'])
129
	$radiusctx = 'second';
130
131 65fbb388 Scott Ullrich
if ($_POST['logout_id']) {
132 532cb894 Ermal
	echo <<<EOD
133 5b237745 Scott Ullrich
<HTML>
134
<HEAD><TITLE>Disconnecting...</TITLE></HEAD>
135
<BODY BGCOLOR="#435370">
136
<SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
137 b2ce71ff Scott Ullrich
<B>You have been disconnected.</B>
138 5b237745 Scott Ullrich
</SPAN>
139
<SCRIPT LANGUAGE="JavaScript">
140
<!--
141
setTimeout('window.close();',5000) ;
142
-->
143
</SCRIPT>
144
</BODY>
145
</HTML>
146
147
EOD;
148 006802ab Ermal
	captiveportal_disconnect_client($_POST['logout_id']);
149 ebc0e4b6 Ermal
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
150 65fbb388 Scott Ullrich
    /* radius functions handle everything so we exit here since we're done */
151
152 8015e67b Erik Fonnesbeck
} 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 89341b50 Chris Buechler
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
158 336e3c1c Charlie
159
    $voucher = trim($_POST['auth_voucher']);
160
    $timecredit = voucher_auth($voucher);
161
    // $timecredit contains either a credit in minutes or an error message
162
    if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
163
        // if multiple vouchers given, use the first as username
164 48387425 Erik Fonnesbeck
        $a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher);
165 336e3c1c Charlie
        $voucher = $a_vouchers[0];
166
        $attr = array( 'voucher' => 1,
167
                'session_timeout' => $timecredit*60,
168
                'session_terminate_time' => 0);
169
        if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
170
171
            // YES: user is good for $timecredit minutes.
172 d102e3ed Chris Buechler
            captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
173 336e3c1c Charlie
        } else {
174 b4792bf8 Ermal
            portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
175 336e3c1c Charlie
        }
176
    } else if (-1 == $timecredit) {  // valid but expired
177
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
178 b4792bf8 Ermal
        portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
179 336e3c1c Charlie
    } else {
180
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
181 b4792bf8 Ermal
        portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
182 336e3c1c Charlie
    }
183
184 65fbb388 Scott Ullrich
} else if ($_POST['accept'] && $radius_enable) {
185
186 ebc0e4b6 Ermal
    if (($_POST['auth_user'] && $_POST['auth_pass']) || ($_POST['auth_user2'] && $_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 814992f7 Ermal
	$type = "error";
196
	if (!empty($auth_list['url_redirection'])) {
197
		$redirurl = $auth_list['url_redirection'];
198
		$type = "redir";
199
	}
200 65fbb388 Scott Ullrich
201
        if ($auth_list['auth_val'] == 1) {
202 ebc0e4b6 Ermal
            captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']);
203 eb15decb Ermal
 	    portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
204 65fbb388 Scott Ullrich
        }
205
        else if ($auth_list['auth_val'] == 3) {
206 ebc0e4b6 Ermal
            captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
207 eb15decb Ermal
            portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
208 65fbb388 Scott Ullrich
        }
209
    } else {
210 ebc0e4b6 Ermal
	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
	captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR");
217 eb15decb Ermal
        portal_reply_page($redirurl, "error", $errormsg);
218 65fbb388 Scott Ullrich
    }
219 8c1ce6c7 Scott Ullrich
220 b7ae00c0 bcyrill
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
221 8c1ce6c7 Scott Ullrich
222 90477318 Ermal
    if ($_POST['auth_user'] && $_POST['auth_pass']) {
223 0092b3bd mgrooms
	//check against local user manager
224
	$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
225 90477318 Ermal
	if ($loginok){
226
		captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
227
		portal_allow($clientip, $clientmac,$_POST['auth_user']);
228
	} else {
229
		captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
230
		portal_reply_page($redirurl, "error", $errormsg);
231
	}
232
    } else
233 eb15decb Ermal
        portal_reply_page($redirurl, "error", $errormsg);
234 b7ae00c0 bcyrill
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
235 65fbb388 Scott Ullrich
    captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
236
    portal_allow($clientip, $clientmac, "unauthenticated");
237
} else {
238
    /* display captive portal page */
239 4993f81c Chris Buechler
    portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
240 5b237745 Scott Ullrich
}
241
242 4a5feb83 Ermal
ob_flush();
243 03552507 Erik Fonnesbeck
244 60b66b60 Ermal
?>