Project

General

Profile

Download (8.49 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
$orig_host = $_ENV['HTTP_HOST'];
48
/* NOTE: IE 8/9 is buggy and that is why this is needed */
49
$orig_request = rtrim($_REQUEST['redirurl'], " /");
50
$clientip = $_SERVER['REMOTE_ADDR'];
51

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

    
60
if (isset($config['captiveportal']['httpslogin']))
61
    $ourhostname = $config['captiveportal']['httpsname'] . ":8001";
62
else {
63
    $ifip = portal_ip_from_client_ip($clientip);
64
    if (!$ifip)
65
    	$ourhostname = $config['system']['hostname'] . ":8000";
66
    else
67
    	$ourhostname = "{$ifip}:8000";
68
}
69

    
70
if ($orig_host != $ourhostname) {
71
    /* the client thinks it's connected to the desired web server, but instead
72
       it's connected to us. Issue a redirect... */
73

    
74
    if (isset($config['captiveportal']['httpslogin']))
75
        header("Location: https://{$ourhostname}/index.php?redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
76
    else
77
        header("Location: http://{$ourhostname}/index.php?redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
78

    
79
    exit;
80
}
81
if (!empty($config['captiveportal']['redirurl']))
82
	$redirurl = $config['captiveportal']['redirurl'];
83
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
84
	$redirurl = urldecode($matches[1]);
85
else if ($_REQUEST['redirurl'])
86
	$redirurl = $_REQUEST['redirurl'];
87

    
88
$macfilter = !isset($config['captiveportal']['nomacfilter']);
89
$passthrumac = isset($config['captiveportal']['passthrumacadd']);
90

    
91
/* find MAC address for client */
92
$clientmac = arp_get_mac_by_ip($clientip);
93
if (!$clientmac && ($macfilter || $passthrumac)) {
94
    /* unable to find MAC address - shouldn't happen! - bail out */
95
    captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
96
    echo "An error occurred.  Please check the system logs for more information.";
97
    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.");
98
    exit;
99
}
100

    
101
/* find out if we need RADIUS + RADIUSMAC or not */
102
if (file_exists("{$g['vardb_path']}/captiveportal_radius.db")) {
103
    $radius_enable = TRUE;
104
    if (isset($config['captiveportal']['radmac_enable']))
105
        $radmac_enable = TRUE;
106
}
107

    
108
if ($_POST['logout_id']) {
109
	echo <<<EOD
110
<HTML>
111
<HEAD><TITLE>Disconnecting...</TITLE></HEAD>
112
<BODY BGCOLOR="#435370">
113
<SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
114
<B>You have been disconnected.</B>
115
</SPAN>
116
<SCRIPT LANGUAGE="JavaScript">
117
<!--
118
setTimeout('window.close();',5000) ;
119
-->
120
</SCRIPT>
121
</BODY>
122
</HTML>
123

    
124
EOD;
125
	captiveportal_disconnect_client($_POST['logout_id']);
126
	exit;
127
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip)) {
128
    /* radius functions handle everything so we exit here since we're done */
129
    exit;
130

    
131
} else if (portal_consume_passthrough_credit($clientmac)) {
132
    /* allow the client through if it had a pass-through credit for its MAC */
133
    captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
134
    portal_allow($clientip, $clientmac, "unauthenticated");
135

    
136
} else if ($_POST['accept'] && $_POST['auth_voucher']) {
137

    
138
    $voucher = trim($_POST['auth_voucher']);
139
    $timecredit = voucher_auth($voucher);
140
    // $timecredit contains either a credit in minutes or an error message
141
    if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
142
        // if multiple vouchers given, use the first as username
143
        $a_vouchers = split("[\t\n\r ]+",$voucher);
144
        $voucher = $a_vouchers[0];
145
        $attr = array( 'voucher' => 1,
146
                'session_timeout' => $timecredit*60,
147
                'session_terminate_time' => 0);
148
        if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
149

    
150
            // YES: user is good for $timecredit minutes.
151
            captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
152
        } else {
153
            portal_reply_page($redirurl, "error", $config['voucher']['msgexpired'] ? $config['voucher']['msgexpired']: $errormsg);
154
        }
155
    } else if (-1 == $timecredit) {  // valid but expired
156
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
157
        portal_reply_page($redirurl, "error", $config['voucher']['msgexpired'] ? $config['voucher']['msgexpired']: $errormsg);
158
    } else {
159
        captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
160
        portal_reply_page($redirurl, "error", $config['voucher']['msgnoaccess'] ? $config['voucher']['msgnoaccess'] : $errormsg);
161
    }
162

    
163
} else if ($_POST['accept'] && $radius_enable) {
164

    
165
    if ($_POST['auth_user'] && $_POST['auth_pass']) {
166
        $auth_list = radius($_POST['auth_user'],$_POST['auth_pass'],$clientip,$clientmac,"USER LOGIN");
167
	$type = "error";
168
	if (!empty($auth_list['url_redirection'])) {
169
		$redirurl = $auth_list['url_redirection'];
170
		$type = "redir";
171
	}
172

    
173
        if ($auth_list['auth_val'] == 1) {
174
            captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"ERROR",$auth_list['error']);
175
 	    portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
176
        }
177
        else if ($auth_list['auth_val'] == 3) {
178
            captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
179
            portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
180
        }
181
    } else {
182
        captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"ERROR");
183
        portal_reply_page($redirurl, "error", $errormsg);
184
    }
185

    
186
} else if ($_POST['accept'] && $config['captiveportal']['auth_method'] == "local" && $_POST['auth_user']) {
187

    
188
    if ($_POST['auth_user'] && $_POST['auth_pass']) {
189
	//check against local user manager
190
	$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
191
	if ($loginok){
192
		captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
193
		portal_allow($clientip, $clientmac,$_POST['auth_user']);
194
	} else {
195
		captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
196
		portal_reply_page($redirurl, "error", $errormsg);
197
	}
198
    } else
199
        portal_reply_page($redirurl, "error", $errormsg);
200
} else if ($_POST['accept'] && $clientip && $config['captiveportal']['auth_method'] == "none") {
201
    captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
202
    portal_allow($clientip, $clientmac, "unauthenticated");
203
} else {
204
    /* display captive portal page */
205
    portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
206
}
207

    
208
exit;
209

    
210

    
211
?>
(1-1/3)