Commit 0fe885b9 authored by Franco Fichtner's avatar Franco Fichtner

captiveportal: apply PSR2 style

parent 21377312
...@@ -46,10 +46,10 @@ global $cpzone, $cpzoneid; ...@@ -46,10 +46,10 @@ global $cpzone, $cpzoneid;
$cpzone = $_REQUEST['zone']; $cpzone = $_REQUEST['zone'];
$cpcfg = $config['captiveportal'][$cpzone]; $cpcfg = $config['captiveportal'][$cpzone];
if (empty($cpcfg)) { if (empty($cpcfg)) {
log_error("Submission to captiveportal with unkown parameter zone: " . htmlspecialchars($cpzone)); log_error("Submission to captiveportal with unkown parameter zone: " . htmlspecialchars($cpzone));
portal_reply_page($redirurl, "error", $errormsg); portal_reply_page($redirurl, "error", $errormsg);
ob_flush(); ob_flush();
return; return;
} }
$cpzoneid = $cpcfg['zoneid']; $cpzoneid = $cpcfg['zoneid'];
...@@ -60,83 +60,85 @@ $orig_request = trim($_REQUEST['redirurl'], " /"); ...@@ -60,83 +60,85 @@ $orig_request = trim($_REQUEST['redirurl'], " /");
$clientip = $_SERVER['REMOTE_ADDR']; $clientip = $_SERVER['REMOTE_ADDR'];
if (!$clientip) { if (!$clientip) {
/* not good - bail out */ /* not good - bail out */
log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address."); log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
$error_message = "An error occurred. Please check the system logs for more information."; $error_message = "An error occurred. Please check the system logs for more information.";
portal_reply_page($redirurl, "error", $errormsg); portal_reply_page($redirurl, "error", $errormsg);
ob_flush(); ob_flush();
return; return;
} }
$ourhostname = portal_hostname_from_client_ip($clientip); $ourhostname = portal_hostname_from_client_ip($clientip);
if ($orig_host != $ourhostname) { if ($orig_host != $ourhostname) {
/* the client thinks it's connected to the desired web server, but instead /* the client thinks it's connected to the desired web server, but instead
it's connected to us. Issue a redirect... */ it's connected to us. Issue a redirect... */
$protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://'; $protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}")); header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
ob_flush(); ob_flush();
return; return;
} }
if (!empty($cpcfg['redirurl'])) if (!empty($cpcfg['redirurl'])) {
$redirurl = $cpcfg['redirurl']; $redirurl = $cpcfg['redirurl'];
else if (preg_match("/redirurl=(.*)/", $orig_request, $matches)) } elseif (preg_match("/redirurl=(.*)/", $orig_request, $matches)) {
$redirurl = urldecode($matches[1]); $redirurl = urldecode($matches[1]);
else if ($_REQUEST['redirurl']) } elseif ($_REQUEST['redirurl']) {
$redirurl = $_REQUEST['redirurl']; $redirurl = $_REQUEST['redirurl'];
}
$macfilter = !isset($cpcfg['nomacfilter']); $macfilter = !isset($cpcfg['nomacfilter']);
$passthrumac = isset($cpcfg['passthrumacadd']); $passthrumac = isset($cpcfg['passthrumacadd']);
function ip_to_mac($addr) function ip_to_mac($addr)
{ {
$cmd = '/usr/sbin/arp -n ' . $addr; $cmd = '/usr/sbin/arp -n ' . $addr;
$ret = false; $ret = false;
exec($cmd, $out, $ret); exec($cmd, $out, $ret);
if ($ret) { if ($ret) {
log_error('The command `' . $cmd . '\' failed to execute'); log_error('The command `' . $cmd . '\' failed to execute');
} else { } else {
$mac = explode(' ', $out[0]); $mac = explode(' ', $out[0]);
if (isset($mac[3])) { if (isset($mac[3])) {
$ret = $mac[3]; $ret = $mac[3];
} }
} }
return $ret; return $ret;
} }
/* find MAC address for client */ /* find MAC address for client */
if ($macfilter || $passthrumac) { if ($macfilter || $passthrumac) {
$tmpres = ip_to_mac($clientip); $tmpres = ip_to_mac($clientip);
if (!$tmpres) { if (!$tmpres) {
/* unable to find MAC address - shouldn't happen! - bail out */ /* unable to find MAC address - shouldn't happen! - bail out */
captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR"); captiveportal_logportalauth("unauthenticated", "noclientmac", $clientip, "ERROR");
echo "An error occurred. Please check the system logs for more information."; echo "An error occurred. Please check the system logs for more information.";
log_error("Zone: {$cpzone} - Captive portal could not determine client's MAC address. Disable MAC address filtering in captive portal if you do not need this functionality."); log_error("Zone: {$cpzone} - Captive portal could not determine client's MAC address. Disable MAC address filtering in captive portal if you do not need this functionality.");
ob_flush(); ob_flush();
return; return;
} }
$clientmac = $tmpres; $clientmac = $tmpres;
unset($tmpres); unset($tmpres);
} }
/* find out if we need RADIUS + RADIUSMAC or not */ /* find out if we need RADIUS + RADIUSMAC or not */
if (file_exists("/var/db/captiveportal_radius_{$cpzone}.db")) { if (file_exists("/var/db/captiveportal_radius_{$cpzone}.db")) {
$radius_enable = TRUE; $radius_enable = true;
if (isset($cpcfg['radmac_enable'])) { if (isset($cpcfg['radmac_enable'])) {
$radmac_enable = TRUE; $radmac_enable = true;
} }
} }
/* find radius context */ /* find radius context */
$radiusctx = 'first'; $radiusctx = 'first';
if ($_POST['auth_user2']) if ($_POST['auth_user2']) {
$radiusctx = 'second'; $radiusctx = 'second';
}
if ($_POST['logout_id']) { if ($_POST['logout_id']) {
echo <<<EOD echo <<<EOD
<html> <html>
<head><title>Disconnecting...</title></head> <head><title>Disconnecting...</title></head>
<body bgcolor="#435370"> <body bgcolor="#435370">
...@@ -152,109 +154,113 @@ setTimeout('window.close();',5000) ; ...@@ -152,109 +154,113 @@ setTimeout('window.close();',5000) ;
</html> </html>
EOD; EOD;
captiveportal_disconnect_client($_POST['logout_id']); captiveportal_disconnect_client($_POST['logout_id']);
} else if ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) { } elseif ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
captiveportal_logportalauth($clientmac,$clientmac,$clientip,"Blocked MAC address"); captiveportal_logportalauth($clientmac, $clientmac, $clientip, "Blocked MAC address");
if (!empty($cpcfg['blockedmacsurl'])) if (!empty($cpcfg['blockedmacsurl'])) {
portal_reply_page($cpcfg['blockedmacsurl'], "redir"); portal_reply_page($cpcfg['blockedmacsurl'], "redir");
else } else {
portal_reply_page($redirurl, "error", "This MAC address has been blocked"); portal_reply_page($redirurl, "error", "This MAC address has been blocked");
}
} else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
/* radius functions handle everything so we exit here since we're done */ } elseif ($clientmac && $radmac_enable && portal_mac_radius($clientmac, $clientip, $radiusctx)) {
/* radius functions handle everything so we exit here since we're done */
} else if (portal_consume_passthrough_credit($clientmac)) {
/* allow the client through if it had a pass-through credit for its MAC */ } elseif (portal_consume_passthrough_credit($clientmac)) {
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT"); /* allow the client through if it had a pass-through credit for its MAC */
portal_allow($clientip, $clientmac, "unauthenticated"); captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
portal_allow($clientip, $clientmac, "unauthenticated");
} else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
$voucher = trim($_POST['auth_voucher']); } elseif (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
$timecredit = voucher_auth($voucher); $voucher = trim($_POST['auth_voucher']);
// $timecredit contains either a credit in minutes or an error message $timecredit = voucher_auth($voucher);
if ($timecredit > 0) { // voucher is valid. Remaining minutes returned // $timecredit contains either a credit in minutes or an error message
// if multiple vouchers given, use the first as username if ($timecredit > 0) { // voucher is valid. Remaining minutes returned
$a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher); // if multiple vouchers given, use the first as username
$voucher = $a_vouchers[0]; $a_vouchers = preg_split("/[\t\n\r ]+/s", $voucher);
$attr = array( 'voucher' => 1, $voucher = $a_vouchers[0];
'session_timeout' => $timecredit*60, $attr = array( 'voucher' => 1,
'session_terminate_time' => 0); 'session_timeout' => $timecredit*60,
if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) { 'session_terminate_time' => 0);
// YES: user is good for $timecredit minutes. if (portal_allow($clientip, $clientmac, $voucher, null, $attr)) {
captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min."); // YES: user is good for $timecredit minutes.
portal_reply_page($redirurl, "redir", "Just redirect the user."); captiveportal_logportalauth($voucher, $clientmac, $clientip, "Voucher login good for $timecredit min.");
} else { portal_reply_page($redirurl, "redir", "Just redirect the user.");
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg); } else {
} portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
} else if (-1 == $timecredit) { // valid but expired }
captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired"); } elseif (-1 == $timecredit) { // valid but expired
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg); captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE", "voucher expired");
} else { portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE"); } else {
portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg); captiveportal_logportalauth($voucher, $clientmac, $clientip, "FAILURE");
} portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
}
} else if ($_POST['accept'] && $radius_enable) {
if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) { } elseif ($_POST['accept'] && $radius_enable) {
if (!empty($_POST['auth_user'])) { if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
$user = $_POST['auth_user']; if (!empty($_POST['auth_user'])) {
$paswd = $_POST['auth_pass']; $user = $_POST['auth_user'];
} else if (!empty($_POST['auth_user2'])) { $paswd = $_POST['auth_pass'];
$user = $_POST['auth_user2']; } elseif (!empty($_POST['auth_user2'])) {
$paswd = $_POST['auth_pass2']; $user = $_POST['auth_user2'];
} $paswd = $_POST['auth_pass2'];
$auth_list = radius($user,$paswd,$clientip,$clientmac,"USER LOGIN", $radiusctx); }
$type = "error"; $auth_list = radius($user, $paswd, $clientip, $clientmac, "USER LOGIN", $radiusctx);
if (!empty($auth_list['url_redirection'])) { $type = "error";
$redirurl = $auth_list['url_redirection']; if (!empty($auth_list['url_redirection'])) {
$type = "redir"; $redirurl = $auth_list['url_redirection'];
} $type = "redir";
}
if ($auth_list['auth_val'] == 1) {
captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']); if ($auth_list['auth_val'] == 1) {
portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg); captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR", $auth_list['error']);
} else if ($auth_list['auth_val'] == 3) { portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']); } elseif ($auth_list['auth_val'] == 3) {
portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg); captiveportal_logportalauth($user, $clientmac, $clientip, "FAILURE", $auth_list['reply_message']);
} portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
} else { }
if (!empty($_POST['auth_user'])) } else {
$user = $_POST['auth_user']; if (!empty($_POST['auth_user'])) {
else if (!empty($_POST['auth_user2'])) $user = $_POST['auth_user'];
$user = $_POST['auth_user2']; } elseif (!empty($_POST['auth_user2'])) {
else $user = $_POST['auth_user2'];
$user = 'unknown'; } else {
captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR"); $user = 'unknown';
portal_reply_page($redirurl, "error", $errormsg); }
} captiveportal_logportalauth($user, $clientmac, $clientip, "ERROR");
portal_reply_page($redirurl, "error", $errormsg);
} else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") { }
if ($_POST['auth_user'] && $_POST['auth_pass']) {
//check against local user manager } elseif ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
$loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']); if ($_POST['auth_user'] && $_POST['auth_pass']) {
//check against local user manager
if ($loginok && isset($cpcfg['localauth_priv'])) $loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
$loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
if ($loginok && isset($cpcfg['localauth_priv'])) {
if ($loginok){ $loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN"); }
portal_allow($clientip, $clientmac,$_POST['auth_user']);
portal_reply_page($redirurl, "redir", "Just redirect the user."); if ($loginok) {
} else { captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "LOGIN");
captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE"); portal_allow($clientip, $clientmac, $_POST['auth_user']);
portal_reply_page($redirurl, "error", $errormsg); portal_reply_page($redirurl, "redir", "Just redirect the user.");
} } else {
} else captiveportal_logportalauth($_POST['auth_user'], $clientmac, $clientip, "FAILURE");
portal_reply_page($redirurl, "error", $errormsg); portal_reply_page($redirurl, "error", $errormsg);
}
} else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") { } else {
captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT"); portal_reply_page($redirurl, "error", $errormsg);
portal_allow($clientip, $clientmac, "unauthenticated"); }
} elseif ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
captiveportal_logportalauth("unauthenticated", $clientmac, $clientip, "ACCEPT");
portal_allow($clientip, $clientmac, "unauthenticated");
} else { } else {
/* display captive portal page */ /* display captive portal page */
portal_reply_page($redirurl, "login",null,$clientmac,$clientip); portal_reply_page($redirurl, "login", null, $clientmac, $clientip);
} }
ob_flush(); ob_flush();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment