Project

General

Profile

Download (9.4 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
$orig_request = $_REQUEST['redirurl'];
52
$clientip = $_SERVER['REMOTE_ADDR'];
53

    
54
if (!$clientip) {
55
	/* not good - bail out */
56
	log_error("Captive portal could not determine client's IP address.");
57
	$error_message = "An error occurred.  Please check the system logs for more information.";
58
	portal_reply_page($redirurl, "error", $errormsg);
59
	exit;
60
}
61

    
62
$listenporthttps = $cpcfg['listenporthttps'] ? $cpcfg['listenporthttps'] : ($cpcfg['zoneid'] + 1);
63
$listenporthttp  = $cpcfg['listenporthttp']  ? $cpcfg['listenporthttp']  : $cpcfg['zoneid'];
64

    
65
if (isset($config['captiveportal'][$cpzone]['httpslogin']))
66
	$ourhostname = $config['captiveportal'][$cpzone]['httpsname'] . ":" . $listenporthttps;
67
else {
68
	$ifip = portal_ip_from_client_ip($clientip);
69
	if (!$ifip)
70
		$ourhostname = "{$config['system']['hostname']}.{$config['system']['domain']}:{$listenporthttp}";
71
	else
72
		$ourhostname = "{$ifip}:{$listenporthttp}";
73
}
74

    
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

    
79
    if (isset($config['captiveportal'][$cpzone]['httpslogin']))
80
        header("Location: https://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
81
    else
82
        header("Location: http://{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
83

    
84
    exit;
85
}
86
if (!empty($config['captiveportal'][$cpzone]['redirurl']))
87
	$redirurl = $config['captiveportal'][$cpzone]['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
$macfilter = !isset($config['captiveportal'][$cpzone]['nomacfilter']);
94
$passthrumac = isset($config['captiveportal'][$cpzone]['passthrumacadd']);
95

    
96
/* find MAC address for client */
97
$clientmac = arp_get_mac_by_ip($clientip);
98
if (!$clientmac && ($macfilter || $passthrumac)) {
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("Captive portal could not determine client's MAC address.  Disable MAC address filtering in captive portal if you do not need this functionality.");
103
    exit;
104
}
105

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

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

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

    
134
EOD;
135
	captiveportal_disconnect_client($_POST['logout_id']);
136
	exit;
137
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
138
    /* radius functions handle everything so we exit here since we're done */
139
    exit;
140

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

    
146
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
147

    
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

    
160
            // YES: user is good for $timecredit minutes.
161
            captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
162
        } else {
163
            portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
164
        }
165
    } else if (-1 == $timecredit) {  // valid but expired
166
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
167
        portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
168
    } else {
169
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
170
        portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
171
    }
172

    
173
} else if ($_POST['accept'] && $radius_enable) {
174

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

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

    
209
} else if ($_POST['accept'] && $config['captiveportal'][$cpzone]['auth_method'] == "local") {
210

    
211
    if ($_POST['auth_user'] && $_POST['auth_pass']) {
212
	//check against local user manager
213
	$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
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
} else if ($_POST['accept'] && $clientip && $config['captiveportal'][$cpzone]['auth_method'] == "none") {
224
    captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
225
    portal_allow($clientip, $clientmac, "unauthenticated");
226
} else {
227
    /* display captive portal page */
228
    portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
229
}
230

    
231
exit;
232

    
233

    
234
?>
(1-1/3)