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
|
$orig_request = $_REQUEST['redirurl'];
|
49
|
$clientip = $_SERVER['REMOTE_ADDR'];
|
50
|
|
51
|
if (!$clientip) {
|
52
|
/* not good - bail out */
|
53
|
log_error("Captive portal could not determine client's IP address.");
|
54
|
$error_message = "An error occurred. Please check the system logs for more information.";
|
55
|
portal_reply_page($redirurl, "error", $errormsg);
|
56
|
exit;
|
57
|
}
|
58
|
|
59
|
if (isset($config['captiveportal']['httpslogin']))
|
60
|
$ourhostname = $config['captiveportal']['httpsname'] . ":8001";
|
61
|
else {
|
62
|
$ifip = portal_ip_from_client_ip($clientip);
|
63
|
if (!$ifip)
|
64
|
$ourhostname = $config['system']['hostname'] . ":8000";
|
65
|
else
|
66
|
$ourhostname = "{$ifip}:8000";
|
67
|
}
|
68
|
|
69
|
if ($orig_host != $ourhostname) {
|
70
|
/* the client thinks it's connected to the desired web server, but instead
|
71
|
it's connected to us. Issue a redirect... */
|
72
|
|
73
|
if (isset($config['captiveportal']['httpslogin']))
|
74
|
header("Location: https://{$ourhostname}/index.php?redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
|
75
|
else
|
76
|
header("Location: http://{$ourhostname}/index.php?redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
|
77
|
|
78
|
exit;
|
79
|
}
|
80
|
if (!empty($config['captiveportal']['redirurl']))
|
81
|
$redirurl = $config['captiveportal']['redirurl'];
|
82
|
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
|
83
|
$redirurl = urldecode($matches[1]);
|
84
|
else if ($_REQUEST['redirurl'])
|
85
|
$redirurl = $_REQUEST['redirurl'];
|
86
|
|
87
|
$macfilter = !isset($config['captiveportal']['nomacfilter']);
|
88
|
$passthrumac = isset($config['captiveportal']['passthrumacadd']);
|
89
|
|
90
|
/* find MAC address for client */
|
91
|
$clientmac = arp_get_mac_by_ip($clientip);
|
92
|
if (!$clientmac && ($macfilter || $passthrumac)) {
|
93
|
/* unable to find MAC address - shouldn't happen! - bail out */
|
94
|
captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
|
95
|
echo "An error occurred. Please check the system logs for more information.";
|
96
|
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.");
|
97
|
exit;
|
98
|
}
|
99
|
|
100
|
/* find out if we need RADIUS + RADIUSMAC or not */
|
101
|
if (file_exists("{$g['vardb_path']}/captiveportal_radius.db")) {
|
102
|
$radius_enable = TRUE;
|
103
|
if (isset($config['captiveportal']['radmac_enable']))
|
104
|
$radmac_enable = TRUE;
|
105
|
}
|
106
|
|
107
|
if ($_POST['logout_id']) {
|
108
|
echo <<<EOD
|
109
|
<HTML>
|
110
|
<HEAD><TITLE>Disconnecting...</TITLE></HEAD>
|
111
|
<BODY BGCOLOR="#435370">
|
112
|
<SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
|
113
|
<B>You have been disconnected.</B>
|
114
|
</SPAN>
|
115
|
<SCRIPT LANGUAGE="JavaScript">
|
116
|
<!--
|
117
|
setTimeout('window.close();',5000) ;
|
118
|
-->
|
119
|
</SCRIPT>
|
120
|
</BODY>
|
121
|
</HTML>
|
122
|
|
123
|
EOD;
|
124
|
captiveportal_disconnect_client($_POST['logout_id']);
|
125
|
exit;
|
126
|
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip)) {
|
127
|
/* radius functions handle everything so we exit here since we're done */
|
128
|
exit;
|
129
|
|
130
|
} else if (portal_consume_passthrough_credit($clientmac)) {
|
131
|
/* allow the client through if it had a pass-through credit for its MAC */
|
132
|
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
|
133
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
134
|
|
135
|
} else if ($_POST['accept'] && $_POST['auth_voucher']) {
|
136
|
|
137
|
$voucher = trim($_POST['auth_voucher']);
|
138
|
$timecredit = voucher_auth($voucher);
|
139
|
// $timecredit contains either a credit in minutes or an error message
|
140
|
if ($timecredit > 0) { // voucher is valid. Remaining minutes returned
|
141
|
// if multiple vouchers given, use the first as username
|
142
|
$a_vouchers = split("[\t\n\r ]+",$voucher);
|
143
|
$voucher = $a_vouchers[0];
|
144
|
$attr = array( 'voucher' => 1,
|
145
|
'session_timeout' => $timecredit*60,
|
146
|
'session_terminate_time' => 0);
|
147
|
if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
|
148
|
|
149
|
// YES: user is good for $timecredit minutes.
|
150
|
captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
|
151
|
} else {
|
152
|
portal_reply_page($redirurl, "error", $config['voucher']['msgexpired'] ? $config['voucher']['msgexpired']: $errormsg);
|
153
|
}
|
154
|
} else if (-1 == $timecredit) { // valid but expired
|
155
|
captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
|
156
|
portal_reply_page($redirurl, "error", $config['voucher']['msgexpired'] ? $config['voucher']['msgexpired']: $errormsg);
|
157
|
} else {
|
158
|
captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
|
159
|
portal_reply_page($redirurl, "error", $config['voucher']['msgnoaccess'] ? $config['voucher']['msgnoaccess'] : $errormsg);
|
160
|
}
|
161
|
|
162
|
} else if ($_POST['accept'] && $radius_enable) {
|
163
|
|
164
|
if ($_POST['auth_user'] && $_POST['auth_pass']) {
|
165
|
$auth_list = radius($_POST['auth_user'],$_POST['auth_pass'],$clientip,$clientmac,"USER LOGIN");
|
166
|
$type = "error";
|
167
|
if (!empty($auth_list['url_redirection'])) {
|
168
|
$redirurl = $auth_list['url_redirection'];
|
169
|
$type = "redir";
|
170
|
}
|
171
|
|
172
|
if ($auth_list['auth_val'] == 1) {
|
173
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"ERROR",$auth_list['error']);
|
174
|
portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
|
175
|
}
|
176
|
else if ($auth_list['auth_val'] == 3) {
|
177
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
|
178
|
portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
|
179
|
}
|
180
|
} else {
|
181
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"ERROR");
|
182
|
portal_reply_page($redirurl, "error", $errormsg);
|
183
|
}
|
184
|
|
185
|
} else if ($_POST['accept'] && $config['captiveportal']['auth_method'] == "local") {
|
186
|
|
187
|
//check against local user manager
|
188
|
$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
|
189
|
if ($loginok){
|
190
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
|
191
|
portal_allow($clientip, $clientmac,$_POST['auth_user']);
|
192
|
} else {
|
193
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
|
194
|
portal_reply_page($redirurl, "error", $errormsg);
|
195
|
}
|
196
|
} else if ($_POST['accept'] && $clientip) {
|
197
|
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
|
198
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
199
|
} else {
|
200
|
/* display captive portal page */
|
201
|
portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
|
202
|
}
|
203
|
|
204
|
exit;
|
205
|
|
206
|
|
207
|
?>
|