1
|
<?php
|
2
|
/*
|
3
|
$Id$
|
4
|
part of m0n0wall (http://m0n0.ch/wall)
|
5
|
|
6
|
Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
|
7
|
All rights reserved.
|
8
|
|
9
|
Redistribution and use in source and binary forms, with or without
|
10
|
modification, are permitted provided that the following conditions are met:
|
11
|
|
12
|
1. Redistributions of source code must retain the above copyright notice,
|
13
|
this list of conditions and the following disclaimer.
|
14
|
|
15
|
2. Redistributions in binary form must reproduce the above copyright
|
16
|
notice, this list of conditions and the following disclaimer in the
|
17
|
documentation and/or other materials provided with the distribution.
|
18
|
|
19
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
20
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
21
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
22
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
23
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
24
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
25
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
26
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
27
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28
|
POSSIBILITY OF SUCH DAMAGE.
|
29
|
*/
|
30
|
|
31
|
require_once("functions.inc");
|
32
|
|
33
|
header("Expires: 0");
|
34
|
header("Cache-Control: no-store, no-cache, must-revalidate");
|
35
|
header("Cache-Control: post-check=0, pre-check=0", false);
|
36
|
header("Pragma: no-cache");
|
37
|
|
38
|
$orig_host = $_ENV['HTTP_HOST'];
|
39
|
$orig_request = $_GET['redirurl'];
|
40
|
$clientip = $_SERVER['REMOTE_ADDR'];
|
41
|
|
42
|
if (!$clientip) {
|
43
|
/* not good - bail out */
|
44
|
echo "An error occured. Please check the system logs for more information.";
|
45
|
log_error("Captive portal could not deterimine clients ip address.");
|
46
|
exit;
|
47
|
}
|
48
|
|
49
|
if (isset($config['captiveportal']['httpslogin']))
|
50
|
$ourhostname = $config['captiveportal']['httpsname'] . ":8001";
|
51
|
else
|
52
|
$ourhostname = $config['interfaces'][$config['captiveportal']['interface']]['ipaddr'] . ":8000";
|
53
|
|
54
|
if ($orig_host != $ourhostname) {
|
55
|
/* the client thinks it's connected to the desired web server, but instead
|
56
|
it's connected to us. Issue a redirect... */
|
57
|
|
58
|
if (isset($config['captiveportal']['httpslogin']))
|
59
|
header("Location: https://{$ourhostname}/index.php?redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
|
60
|
else
|
61
|
header("Location: http://{$ourhostname}/index.php?redirurl=" . urlencode("http://{$orig_host}{$orig_request}"));
|
62
|
|
63
|
exit;
|
64
|
}
|
65
|
|
66
|
if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
|
67
|
$redirurl = urldecode($matches[1]);
|
68
|
if ($_POST['redirurl'])
|
69
|
$redirurl = $_POST['redirurl'];
|
70
|
|
71
|
$macfilter = !isset($config['captiveportal']['nomacfilter']);
|
72
|
|
73
|
/* find MAC address for client */
|
74
|
$clientmac = arp_get_mac_by_ip($clientip);
|
75
|
if (!$clientmac && $macfilter) {
|
76
|
/* unable to find MAC address - shouldn't happen! - bail out */
|
77
|
captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
|
78
|
echo "An error occured. Please check the system logs for more information.";
|
79
|
log_error("Captive portal could not deterimine clients MAC address. Disable MAC address filtering in captive portal if you do not needs this functionality.");
|
80
|
exit;
|
81
|
}
|
82
|
|
83
|
/* find out if we need RADIUS + RADIUSMAC or not */
|
84
|
if (file_exists("{$g['vardb_path']}/captiveportal_radius.db")) {
|
85
|
$radius_enable = TRUE;
|
86
|
if ($radius_enable && isset($config['captiveportal']['radmac_enable']))
|
87
|
$radmac_enable = TRUE;
|
88
|
}
|
89
|
|
90
|
if ($_POST['logout_id']) {
|
91
|
disconnect_client($_POST['logout_id']);
|
92
|
echo <<<EOD
|
93
|
<HTML>
|
94
|
<HEAD><TITLE>Disconnecting...</TITLE></HEAD>
|
95
|
<BODY BGCOLOR="#435370">
|
96
|
<SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
|
97
|
<B>You have been disconnected.</B>
|
98
|
</SPAN>
|
99
|
<SCRIPT LANGUAGE="JavaScript">
|
100
|
<!--
|
101
|
setTimeout('window.close();',5000) ;
|
102
|
-->
|
103
|
</SCRIPT>
|
104
|
</BODY>
|
105
|
</HTML>
|
106
|
|
107
|
EOD;
|
108
|
/* The $macfilter can be removed safely since we first check if the $clientmac is present, if not we fail */
|
109
|
} else if ($clientmac && portal_mac_fixed($clientmac)) {
|
110
|
/* punch hole in ipfw for pass thru mac addresses */
|
111
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
112
|
exit;
|
113
|
|
114
|
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip)) {
|
115
|
/* radius functions handle everything so we exit here since we're done */
|
116
|
exit;
|
117
|
|
118
|
} else if ($_POST['accept'] && $radius_enable) {
|
119
|
|
120
|
if ($_POST['auth_user'] && $_POST['auth_pass']) {
|
121
|
$auth_list = radius($_POST['auth_user'],$_POST['auth_pass'],$clientip,$clientmac,"USER LOGIN");
|
122
|
|
123
|
if ($auth_list['auth_val'] == 1) {
|
124
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"ERROR",$auth_list['error']);
|
125
|
portal_reply_page($redirurl, "error", $auth_list['error']);
|
126
|
}
|
127
|
else if ($auth_list['auth_val'] == 3) {
|
128
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
|
129
|
portal_reply_page($redirurl, "error", $auth_list['reply_message']);
|
130
|
}
|
131
|
} else {
|
132
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"ERROR");
|
133
|
portal_reply_page($redirurl, "error");
|
134
|
}
|
135
|
|
136
|
} else if ($_POST['accept'] && $config['captiveportal']['auth_method'] == "local") {
|
137
|
|
138
|
//check against local usermanager
|
139
|
$userdb = &$config['captiveportal']['user'];
|
140
|
|
141
|
$loginok = false;
|
142
|
|
143
|
//erase expired accounts
|
144
|
if (is_array($userdb)) {
|
145
|
$moddb = false;
|
146
|
for ($i = 0; $i < count($userdb); $i++) {
|
147
|
if ($userdb[$i]['expirationdate'] && (strtotime("-1 day") > strtotime($userdb[$i]['expirationdate']))) {
|
148
|
unset($userdb[$i]);
|
149
|
$moddb = true;
|
150
|
}
|
151
|
}
|
152
|
if ($moddb)
|
153
|
write_config();
|
154
|
|
155
|
$userdb = &$config['captiveportal']['user'];
|
156
|
|
157
|
for ($i = 0; $i < count($userdb); $i++) {
|
158
|
if (($userdb[$i]['name'] == $_POST['auth_user']) && ($userdb[$i]['password'] == md5($_POST['auth_pass']))) {
|
159
|
$loginok = true;
|
160
|
break;
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
|
165
|
if ($loginok){
|
166
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
|
167
|
portal_allow($clientip, $clientmac,$_POST['auth_user']);
|
168
|
} else {
|
169
|
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
|
170
|
portal_reply_page($redirurl, "error");
|
171
|
}
|
172
|
} else if ($_POST['accept'] && $clientip) {
|
173
|
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
|
174
|
portal_allow($clientip, $clientmac, "unauthenticated");
|
175
|
} else {
|
176
|
/* display captive portal page */
|
177
|
portal_reply_page($redirurl, "login");
|
178
|
}
|
179
|
|
180
|
exit;
|
181
|
|
182
|
function portal_reply_page($redirurl, $type = null, $message = null) {
|
183
|
global $g, $config;
|
184
|
|
185
|
/* Get captive portal layout */
|
186
|
if ($type == "login")
|
187
|
$htmltext = file_get_contents("{$g['varetc_path']}/captiveportal.html");
|
188
|
else
|
189
|
$htmltext = file_get_contents("{$g['varetc_path']}/captiveportal-error.html");
|
190
|
|
191
|
/* substitute other variables */
|
192
|
if (isset($config['captiveportal']['httpslogin']))
|
193
|
$htmltext = str_replace("\$PORTAL_ACTION\$", "https://{$config['captiveportal']['httpsname']}:8001/", $htmltext);
|
194
|
else
|
195
|
$htmltext = str_replace("\$PORTAL_ACTION\$", "http://{$config['interfaces'][$config['captiveportal']['interface']]['ipaddr']}:8000/", $htmltext);
|
196
|
|
197
|
$htmltext = str_replace("\$PORTAL_REDIRURL\$", htmlspecialchars($redirurl), $htmltext);
|
198
|
$htmltext = str_replace("\$PORTAL_MESSAGE\$", htmlspecialchars($message), $htmltext);
|
199
|
|
200
|
echo $htmltext;
|
201
|
}
|
202
|
|
203
|
function portal_mac_radius($clientmac,$clientip) {
|
204
|
global $config ;
|
205
|
|
206
|
$radmac_secret = $config['captiveportal']['radmac_secret'];
|
207
|
|
208
|
/* authentication against the radius server */
|
209
|
$username = mac_format($clientmac);
|
210
|
$auth_list = radius($username,$radmac_secret,$clientip,$clientmac,"MACHINE LOGIN");
|
211
|
if ($auth_list['auth_val'] == 2) {
|
212
|
return TRUE;
|
213
|
}
|
214
|
return FALSE;
|
215
|
}
|
216
|
|
217
|
function portal_allow($clientip,$clientmac,$username,$password = null, $attributes = null, $ruleno = null) {
|
218
|
|
219
|
global $redirurl, $g, $config, $url_redirection, $type;
|
220
|
|
221
|
/* See if a ruleno is passed, if not start locking the sessions because this means there isn't one atm */
|
222
|
$lockedcpnow = false;
|
223
|
if ($ruleno == null) {
|
224
|
$cplock = lock('captiveportal');
|
225
|
$lockedcpnow = true;
|
226
|
$ruleno = captiveportal_get_next_ipfw_ruleno();
|
227
|
}
|
228
|
|
229
|
/* if the pool is empty, return appropriate message and exit */
|
230
|
if (is_null($ruleno)) {
|
231
|
portal_reply_page($redirurl, "error", "System reached maximum login capacity");
|
232
|
log_error("WARNING! Captive portal has reached maximum login capacity");
|
233
|
if ($lockedcpnow == true)
|
234
|
unlock($cplock);
|
235
|
exit;
|
236
|
}
|
237
|
|
238
|
// Ensure we create an array if we are missing attributes
|
239
|
if (!is_array($attributes))
|
240
|
$attributes = array();
|
241
|
|
242
|
/* read in client database */
|
243
|
$cpdb = captiveportal_read_db();
|
244
|
|
245
|
$radiusservers = captiveportal_get_radius_servers();
|
246
|
|
247
|
/* Find an existing session */
|
248
|
for ($i = 0; $i < count($cpdb); $i++) {
|
249
|
/* on the same ip */
|
250
|
if($cpdb[$i][2] == $clientip) {
|
251
|
captiveportal_logportalauth($cpdb[$i][4],$cpdb[$i][3],$cpdb[$i][2],"CONCURRENT LOGIN - REUSING OLD SESSION");
|
252
|
$sessionid = $cpdb[$i][5];
|
253
|
break;
|
254
|
}
|
255
|
elseif ((isset($config['captiveportal']['noconcurrentlogins'])) && ($username != 'unauthenticated')) {
|
256
|
/* on the same username */
|
257
|
if ($cpdb[$i][4] == $username) {
|
258
|
/* This user was already logged in so we disconnect the old one */
|
259
|
captiveportal_disconnect($cpdb[$i],$radiusservers,13);
|
260
|
captiveportal_logportalauth($cpdb[$i][4],$cpdb[$i][3],$cpdb[$i][2],"CONCURRENT LOGIN - TERMINATING OLD SESSION");
|
261
|
unset($cpdb[$i]);
|
262
|
break;
|
263
|
}
|
264
|
}
|
265
|
}
|
266
|
|
267
|
if (!isset($sessionid)) {
|
268
|
|
269
|
/* generate unique session ID */
|
270
|
$tod = gettimeofday();
|
271
|
$sessionid = substr(md5(mt_rand() . $tod['sec'] . $tod['usec'] . $clientip . $clientmac), 0, 16);
|
272
|
|
273
|
/* Add rules for traffic shaping
|
274
|
* We don't need to add extra l3 allow rules since traffic will pass due to the following kernel option
|
275
|
* net.inet.ip.fw.one_pass: 1
|
276
|
*/
|
277
|
$peruserbw = isset($config['captiveportal']['peruserbw']);
|
278
|
|
279
|
$bw_up = isset($attributes['bw_up']) ? trim($attributes['bw_up']) : $config['captiveportal']['bwdefaultup'];
|
280
|
$bw_down = isset($attributes['bw_down']) ? trim($attributes['bw_down']) : $config['captiveportal']['bwdefaultdn'];
|
281
|
|
282
|
if ($peruserbw && !empty($bw_up) && is_numeric($bw_up)) {
|
283
|
$bw_up_pipeno = $ruleno + 40500;
|
284
|
exec("/sbin/ipfw add $ruleno set 2 pipe $bw_up_pipeno ip from $clientip to any in");
|
285
|
exec("/sbin/ipfw pipe $bw_up_pipeno config bw {$bw_up}Kbit/s queue 100");
|
286
|
} else {
|
287
|
exec("/sbin/ipfw add $ruleno set 2 skipto 50000 ip from $clientip to any in");
|
288
|
}
|
289
|
if ($peruserbw && !empty($bw_down) && is_numeric($bw_down)) {
|
290
|
$bw_down_pipeno = $ruleno + 45500;
|
291
|
exec("/sbin/ipfw add $ruleno set 2 pipe $bw_down_pipeno ip from any to $clientip out");
|
292
|
exec("/sbin/ipfw pipe $bw_down_pipeno config bw {$bw_down}Kbit/s queue 100");
|
293
|
} else {
|
294
|
exec("/sbin/ipfw add $ruleno set 2 skipto 50000 ip from any to $clientip out");
|
295
|
}
|
296
|
|
297
|
/* add ipfw rules for layer 2 */
|
298
|
if (!isset($config['captiveportal']['nomacfilter'])) {
|
299
|
$l2ruleno = $ruleno + 10000;
|
300
|
exec("/sbin/ipfw add $l2ruleno set 3 deny all from $clientip to any not MAC any $clientmac layer2 in");
|
301
|
exec("/sbin/ipfw add $l2ruleno set 3 deny all from any to $clientip not MAC $clientmac any layer2 out");
|
302
|
}
|
303
|
|
304
|
/* encode password in Base64 just in case it contains commas */
|
305
|
$bpassword = base64_encode($password);
|
306
|
$cpdb[] = array(time(), $ruleno, $clientip, $clientmac, $username, $sessionid, $bpassword,
|
307
|
$attributes['session_timeout'],
|
308
|
$attributes['idle_timeout'],
|
309
|
$attributes['session_terminate_time']);
|
310
|
|
311
|
if (isset($config['captiveportal']['radacct_enable']) && isset($radiusservers[0])) {
|
312
|
$acct_val = RADIUS_ACCOUNTING_START($ruleno,
|
313
|
$username,
|
314
|
$sessionid,
|
315
|
$radiusservers[0]['ipaddr'],
|
316
|
$radiusservers[0]['acctport'],
|
317
|
$radiusservers[0]['key'],
|
318
|
$clientip,
|
319
|
$clientmac);
|
320
|
|
321
|
if ($acct_val == 1)
|
322
|
captiveportal_logportalauth($username,$clientmac,$clientip,$type,"RADIUS ACCOUNTING FAILED");
|
323
|
}
|
324
|
|
325
|
|
326
|
}
|
327
|
|
328
|
/* rewrite information to database */
|
329
|
captiveportal_write_db($cpdb);
|
330
|
|
331
|
if ($lockedcpnow == true)
|
332
|
unlock($cplock);
|
333
|
/* redirect user to desired destination */
|
334
|
if ($url_redirection)
|
335
|
$my_redirurl = $url_redirection;
|
336
|
else if ($config['captiveportal']['redirurl'])
|
337
|
$my_redirurl = $config['captiveportal']['redirurl'];
|
338
|
else
|
339
|
$my_redirurl = $redirurl;
|
340
|
|
341
|
if(isset($config['captiveportal']['logoutwin_enable'])) {
|
342
|
|
343
|
if (isset($config['captiveportal']['httpslogin']))
|
344
|
$logouturl = "https://{$config['captiveportal']['httpsname']}:8001/";
|
345
|
else
|
346
|
$logouturl = "http://{$config['interfaces'][$config['captiveportal']['interface']]['ipaddr']}:8000/";
|
347
|
|
348
|
echo <<<EOD
|
349
|
<HTML>
|
350
|
<HEAD><TITLE>Redirecting...</TITLE></HEAD>
|
351
|
<BODY>
|
352
|
<SPAN STYLE="font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
|
353
|
<B>Redirecting to <A HREF="{$my_redirurl}">{$my_redirurl}</A>...</B>
|
354
|
</SPAN>
|
355
|
<SCRIPT LANGUAGE="JavaScript">
|
356
|
<!--
|
357
|
LogoutWin = window.open('', 'Logout', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=256,height=64');
|
358
|
if (LogoutWin) {
|
359
|
LogoutWin.document.write('<HTML>');
|
360
|
LogoutWin.document.write('<HEAD><TITLE>Logout</TITLE></HEAD>') ;
|
361
|
LogoutWin.document.write('<BODY BGCOLOR="#435370">');
|
362
|
LogoutWin.document.write('<DIV ALIGN="center" STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">') ;
|
363
|
LogoutWin.document.write('<B>Click the button below to disconnect</B><P>');
|
364
|
LogoutWin.document.write('<FORM METHOD="POST" ACTION="{$logouturl}">');
|
365
|
LogoutWin.document.write('<INPUT NAME="logout_id" TYPE="hidden" VALUE="{$sessionid}">');
|
366
|
LogoutWin.document.write('<INPUT NAME="logout" TYPE="submit" VALUE="Logout">');
|
367
|
LogoutWin.document.write('</FORM>');
|
368
|
LogoutWin.document.write('</DIV></BODY>');
|
369
|
LogoutWin.document.write('</HTML>');
|
370
|
LogoutWin.document.close();
|
371
|
}
|
372
|
|
373
|
document.location.href="{$my_redirurl}";
|
374
|
-->
|
375
|
</SCRIPT>
|
376
|
</BODY>
|
377
|
</HTML>
|
378
|
|
379
|
EOD;
|
380
|
} else {
|
381
|
header("Location: " . $my_redirurl);
|
382
|
return $sessionid;
|
383
|
}
|
384
|
return $sessionid;
|
385
|
}
|
386
|
|
387
|
|
388
|
|
389
|
/* remove a single client by session ID
|
390
|
by Dinesh Nair
|
391
|
*/
|
392
|
function disconnect_client($sessionid, $logoutReason = "LOGOUT", $term_cause = 1) {
|
393
|
|
394
|
global $g, $config;
|
395
|
|
396
|
$cplock = lock('captiveportal');
|
397
|
|
398
|
/* read database */
|
399
|
$cpdb = captiveportal_read_db();
|
400
|
|
401
|
$radiusservers = captiveportal_get_radius_servers();
|
402
|
|
403
|
/* find entry */
|
404
|
for ($i = 0; $i < count($cpdb); $i++) {
|
405
|
if ($cpdb[$i][5] == $sessionid) {
|
406
|
captiveportal_disconnect($cpdb[$i],$radiusservers, $term_cause);
|
407
|
captiveportal_logportalauth($cpdb[$i][4],$cpdb[$i][3],$cpdb[$i][2],$logoutReason);
|
408
|
unset($cpdb[$i]);
|
409
|
break;
|
410
|
}
|
411
|
}
|
412
|
|
413
|
/* write database */
|
414
|
captiveportal_write_db($cpdb);
|
415
|
|
416
|
unlock($cplock);
|
417
|
}
|
418
|
|
419
|
?>
|