Project

General

Profile

Download (9.46 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
$cpzone = $_REQUEST['zone'];
48
$cpcfg = $config['captiveportal'][$cpzone];
49

    
50
$orig_host = $_ENV['HTTP_HOST'];
51
/* NOTE: IE 8/9 is buggy and that is why this is needed */
52
$orig_request = rtrim($_REQUEST['redirurl'], " /");
53
$orig_request = empty($orig_request) ? '' : $orig_request;
54
$clientip = $_SERVER['REMOTE_ADDR'];
55

    
56
if (!$clientip) {
57
	/* 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
	ob_flush();
62
	return;
63
}
64

    
65
$listenporthttps = $cpcfg['listenporthttps'] ? $cpcfg['listenporthttps'] : ($cpcfg['zoneid'] + 1);
66
$listenporthttp  = $cpcfg['listenporthttp']  ? $cpcfg['listenporthttp']  : $cpcfg['zoneid'];
67

    
68
if (isset($cpcfg['httpslogin']))
69
	$ourhostname = $cpcfg['httpsname'] . ":" . $listenporthttps;
70
else {
71
	$ifip = portal_ip_from_client_ip($clientip);
72
	if (!$ifip) {
73
		$ourhostname = "{$config['system']['hostname']}.{$config['system']['domain']}:{$listenporthttp}";
74
	} else {
75
		if (is_ipaddrv6($ifip))
76
			$ourhostname = "[{$ifip}]:{$listenporthttp}";
77
		else
78
			$ourhostname = "{$ifip}:{$listenporthttp}";
79
	}
80
}
81

    
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
    if (isset($cpcfg['httpslogin']))
87
        header("Location: https://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
88
    else
89
        header("Location: http://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
90

    
91
    ob_flush();
92
    return;
93
}
94
if (!empty($cpcfg['redirurl']))
95
	$redirurl = $cpcfg['redirurl'];
96
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
97
	$redirurl = urldecode($matches[1]);
98
else if ($_REQUEST['redirurl'])
99
	$redirurl = $_REQUEST['redirurl'];
100

    
101
$macfilter = !isset($cpcfg['nomacfilter']);
102
$passthrumac = isset($cpcfg['passthrumacadd']);
103

    
104
/* find MAC address for client */
105
if ($macfilter || $passthrumac) {
106
	$tmpres = pfSense_ip_to_mac($clientip);
107
	if (!is_array($tmpres)) {
108
	    /* 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
	    ob_flush();
113
	    return;
114
	}
115
	$clientmac = $tmpres['macaddr'];
116
	unset($tmpres);
117
}
118

    
119
/* find out if we need RADIUS + RADIUSMAC or not */
120
if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
121
    $radius_enable = TRUE;
122
    if (isset($cpcfg['radmac_enable']))
123
        $radmac_enable = TRUE;
124
}
125

    
126
/* find radius context */
127
$radiusctx = 'first';
128
if ($_POST['auth_user2'])
129
	$radiusctx = 'second';
130

    
131
if ($_POST['logout_id']) {
132
	echo <<<EOD
133
<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
<B>You have been disconnected.</B>
138
</SPAN>
139
<SCRIPT LANGUAGE="JavaScript">
140
<!--
141
setTimeout('window.close();',5000) ;
142
-->
143
</SCRIPT>
144
</BODY>
145
</HTML>
146

    
147
EOD;
148
	captiveportal_disconnect_client($_POST['logout_id']);
149
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
150
    /* radius functions handle everything so we exit here since we're done */
151

    
152
} 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
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
158

    
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
        $a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher);
165
        $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
            captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
173
        } else {
174
            portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
175
        }
176
    } else if (-1 == $timecredit) {  // valid but expired
177
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
178
        portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
179
    } else {
180
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
181
        portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
182
    }
183

    
184
} else if ($_POST['accept'] && $radius_enable) {
185

    
186
    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
	$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
        }
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
	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

    
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
	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
        portal_reply_page($redirurl, "error", $errormsg);
234
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
235
    captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
236
    portal_allow($clientip, $clientmac, "unauthenticated");
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)