SHARE
TWEET

index.php - entire file - version 2015-02-06

Nowwhat47 Jan 21st, 2015 (edited) 791 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.         $Id$
  4.         part of m0n0wall (http://m0n0.ch/wall)
  5.  
  6.         Copyright (C) 2013-2015 Electric Sheep Fencing, LP
  7.         Copyright (C) 2009 Ermal Lu├ži
  8.         Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
  9.         All rights reserved.
  10.  
  11.         Redistribution and use in source and binary forms, with or without
  12.         modification, are permitted provided that the following conditions are met:
  13.  
  14.         1. Redistributions of source code must retain the above copyright notice,
  15.            this list of conditions and the following disclaimer.
  16.  
  17.         2. Redistributions in binary form must reproduce the above copyright
  18.            notice, this list of conditions and the following disclaimer in the
  19.            documentation and/or other materials provided with the distribution.
  20.  
  21.         THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  22.         INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23.         AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24.         AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25.         OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.         SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.         INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.         CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.         ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.         POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33.         pfSense_MODULE: captiveportal
  34. */
  35.  
  36. require_once("auth.inc");
  37. require_once("functions.inc");
  38. require_once("captiveportal.inc");
  39.  
  40. $errormsg = "Invalid credentials specified.";
  41.  
  42. header("Expires: 0");
  43. header("Cache-Control: no-store, no-cache, must-revalidate");
  44. header("Cache-Control: post-check=0, pre-check=0", false);
  45. header("Pragma: no-cache");
  46. header("Connection: close");
  47.  
  48. global $cpzone, $cpzoneid;
  49.  
  50. $cpzone = $_REQUEST['zone'];
  51. $cpcfg = $config['captiveportal'][$cpzone];
  52. if (empty($cpcfg)) {
  53.         log_error("Submission to captiveportal with unknown parameter zone: " . htmlspecialchars($cpzone));
  54.         portal_reply_page($redirurl, "error", $errormsg);
  55.         ob_flush();
  56.         return;
  57. }
  58.  
  59. $cpzoneid = $cpcfg['zoneid'];
  60.  
  61. $orig_host = $_SERVER['HTTP_HOST'];
  62. /* NOTE: IE 8/9 is buggy and that is why this is needed */
  63. $orig_request = trim($_REQUEST['redirurl'], " /");
  64. $clientip = $_SERVER['REMOTE_ADDR'];
  65.  
  66. if (!$clientip) {
  67.         /* not good - bail out */
  68.         log_error("Zone: {$cpzone} - Captive portal could not determine client's IP address.");
  69.         $error_message = "An error occurred.  Please check the system logs for more information.";
  70.         portal_reply_page($redirurl, "error", $errormsg);
  71.         ob_flush();
  72.         return;
  73. }
  74.  
  75. $ourhostname = portal_hostname_from_client_ip($clientip);
  76. if ($orig_host != $ourhostname) {
  77.         /* the client thinks it's connected to the desired web server, but instead
  78.            it's connected to us. Issue a redirect... */
  79.         $protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
  80.         header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
  81.  
  82.         ob_flush();
  83.         return;
  84. }
  85.  
  86. if (!empty($cpcfg['redirurl']))
  87.         $redirurl = $cpcfg['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($cpcfg['nomacfilter']);
  94. $passthrumac = isset($cpcfg['passthrumacadd']);
  95.  
  96. /* find MAC address for client */
  97. if ($macfilter || $passthrumac) {
  98.         $tmpres = pfSense_ip_to_mac($clientip);
  99.         if (!is_array($tmpres)) {
  100.                 /* unable to find MAC address - shouldn't happen! - bail out */
  101.                 captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
  102.                 echo "An error occurred.  Please check the system logs for more information.";
  103.                 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.");
  104.                 ob_flush();
  105.                 return;
  106.         }
  107.         $clientmac = $tmpres['macaddr'];
  108.         unset($tmpres);
  109. }
  110.  
  111. /* find out if we need RADIUS + RADIUSMAC or not */
  112. if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
  113.         $radius_enable = TRUE;
  114.         if (isset($cpcfg['radmac_enable']))
  115.                 $radmac_enable = TRUE;
  116. }
  117.  
  118. /* find radius context */
  119. $radiusctx = 'first';
  120. if ($_POST['auth_user2'])
  121.         $radiusctx = 'second';
  122.  
  123. if ($_POST['logout_id']) {
  124.         if(isset($_COOKIE['cookie_portal'])) {
  125.                 unset($_COOKIE['cookie_portal']);
  126.                 setcookie('cookie_portal', '', time() - 3600);
  127.         }
  128.         echo <<<EOD
  129. <html>
  130. <head><title>Disconnecting...</title></head>
  131. <body bgcolor="#435370">
  132. <span style="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
  133. <b>You have been disconnected.</b>
  134. </span>
  135. <script type="text/javascript">
  136. <!--
  137. setTimeout('window.close();',5000) ;
  138. -->
  139. </script>
  140. </body>
  141. </html>
  142.  
  143. EOD;
  144.         captiveportal_disconnect_client($_POST['logout_id']);
  145.  
  146. } else if ($macfilter && $clientmac && captiveportal_blocked_mac($clientmac)) {
  147.         captiveportal_logportalauth($clientmac,$clientmac,$clientip,"Blocked MAC address");
  148.         if (!empty($cpcfg['blockedmacsurl']))
  149.                 portal_reply_page($cpcfg['blockedmacsurl'], "redir");
  150.         else
  151.                 portal_reply_page($redirurl, "error", "This MAC address has been blocked");
  152.  
  153. } else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
  154.         /* radius functions handle everything so we exit here since we're done */
  155.  
  156. } else if (portal_consume_passthrough_credit($clientmac)) {
  157.         /* allow the client through if it had a pass-through credit for its MAC */
  158.         captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
  159.         portal_allow($clientip, $clientmac, "unauthenticated");
  160.  
  161. } else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
  162.         $voucher = trim($_POST['auth_voucher']);
  163.         $timecredit = voucher_auth($voucher);
  164.         // $timecredit contains either a credit in minutes or an error message
  165.         if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
  166.                 // if multiple vouchers given, use the first as username
  167.                 $a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher);
  168.                 $voucher = $a_vouchers[0];
  169.                 $attr = array( 'voucher' => 1,
  170.                                 'session_timeout' => $timecredit*60,
  171.                                 'session_terminate_time' => 0);
  172.                 if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
  173.                         // YES: user is good for $timecredit minutes.
  174.                         captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
  175.                 } else {
  176.                         portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
  177.                 }
  178.         } else if (-1 == $timecredit) {  // valid but expired
  179.                 captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
  180.                 portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
  181.         } else {
  182.                 captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
  183.                 portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
  184.         }
  185.  
  186. } else if ($_POST['accept'] && $radius_enable) {
  187.         if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
  188.                 if (!empty($_POST['auth_user'])) {
  189.                         $user = $_POST['auth_user'];
  190.                         $paswd = $_POST['auth_pass'];
  191.                 } else if (!empty($_POST['auth_user2'])) {
  192.                         $user = $_POST['auth_user2'];
  193.                         $paswd = $_POST['auth_pass2'];
  194.                 }
  195.                 $auth_list = radius($user,$paswd,$clientip,$clientmac,"USER LOGIN", $radiusctx);
  196.                 $type = "error";
  197.                 if (!empty($auth_list['url_redirection'])) {
  198.                         $redirurl = $auth_list['url_redirection'];
  199.                         $type = "redir";
  200.                 }
  201.  
  202.                 if ($auth_list['auth_val'] == 1) {
  203.                         captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']);
  204.                         portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
  205.                 } else if ($auth_list['auth_val'] == 3) {
  206.                         captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
  207.                         portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
  208.                 }
  209.         } else {
  210.                 if (!empty($_POST['auth_user']))
  211.                         $user = $_POST['auth_user'];
  212.                 else if (!empty($_POST['auth_user2']))
  213.                         $user = $_POST['auth_user2'];
  214.                 else
  215.                         $user = 'unknown';
  216.                 captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR");
  217.                 portal_reply_page($redirurl, "error", $errormsg);
  218.         }
  219.  
  220. } else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
  221.         if ($_POST['auth_user'] && $_POST['auth_pass']) {
  222.                 //check against local user manager
  223.                 $loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
  224.  
  225.                 if ($loginok && isset($cpcfg['localauth_priv']))
  226.                         $loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
  227.  
  228.                 if ($loginok){
  229.                         captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
  230.                         portal_allow($clientip, $clientmac,$_POST['auth_user']);
  231.                 } else {
  232.                         captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
  233.                         portal_reply_page($redirurl, "error", $errormsg);
  234.                 }
  235.         } else
  236.                 portal_reply_page($redirurl, "error", $errormsg);
  237.  
  238. } else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
  239.         captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
  240.         portal_allow($clientip, $clientmac, "unauthenticated");
  241.  
  242. } else if ((isset($_COOKIE['cookie_portal']) && already_connected($_COOKIE['cookie_portal']))) {
  243.         portal_reply_page($redirurl, "already_connected",null,$clientmac,$clientip);
  244.        
  245. } else {
  246.         /* display captive portal page */
  247.         portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
  248. }
  249.  
  250. ob_flush();
  251.  
  252. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top