SHARE
TWEET

/usr/local/captiveportal/index.php

dpsapao Feb 8th, 2016 (edited) 210 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.     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. global $cpzone;
  48.  
  49. $cpzone = $_REQUEST['zone'];
  50. $cpcfg = $config['captiveportal'][$cpzone];
  51. if (empty($cpcfg)) {
  52.     log_error("Submission to captiveportal with unkown parameter zone: " . htmlspecialchars($cpzone));
  53.     portal_reply_page($redirurl, "error", $errormsg);
  54.     ob_flush();
  55.     return;
  56. }
  57.  
  58. $orig_host = $_ENV['HTTP_HOST'];
  59. /* NOTE: IE 8/9 is buggy and that is why this is needed */
  60. $orig_request = trim($_REQUEST['redirurl'], " /");
  61. $clientip = $_SERVER['REMOTE_ADDR'];
  62.  
  63. if (!$clientip) {
  64.     /* not good - bail out */
  65.     log_error("Captive portal could not determine client's IP address.");
  66.     $error_message = "An error occurred.  Please check the system logs for more information.";
  67.     portal_reply_page($redirurl, "error", $errormsg);
  68.     ob_flush();
  69.     return;
  70. }
  71.  
  72. $cpsession = captiveportal_isip_logged($clientip);
  73. $ourhostname = portal_hostname_from_client_ip($clientip);
  74. /* Automatically switching to the logout page requires a custom logout page to be present. */
  75. if ((!empty($cpsession)) && (! $_POST['logout_id']) && (!empty($cpcfg['page']['logouttext']))) {
  76.     /* if client already logged in so show logout page */
  77.     $protocol = (isset($config['captiveportal'][$cpzone]['httpslogin'])) ? 'https://' : 'http://';
  78.     $logouturl = "{$protocol}{$ourhostname}/";
  79.     $sessionid = $cpsession['sessionid'];
  80.     $attributes = array();
  81.     if (!empty($cpsession['session_timeout']))
  82.         $attributes['session_timeout'] = $cpsession['session_timeout'];
  83.     if (!empty($cpsession['session_terminate_time']))
  84.         $attributes['session_terminate_time'] = $cpsession['session_terminate_time'];
  85.     include("{$g['varetc_path']}/captiveportal-{$cpzone}-logout.html");
  86.     ob_flush();
  87.     return;
  88. } else if ($orig_host != $ourhostname) {
  89.     /* the client thinks it's connected to the desired web server, but instead
  90.        it's connected to us. Issue a redirect... */
  91.     $protocol = (isset($cpcfg['httpslogin'])) ? 'https://' : 'http://';
  92.     header("Location: {$protocol}{$ourhostname}/index.php?zone={$cpzone}&redirurl=" . urlencode("http://{$orig_host}/{$orig_request}"));
  93.  
  94.     ob_flush();
  95.     return;
  96. }
  97.  
  98. if (!empty($cpcfg['redirurl']))
  99.     $redirurl = $cpcfg['redirurl'];
  100. else if (preg_match("/redirurl=(.*)/", $orig_request, $matches))
  101.     $redirurl = urldecode($matches[1]);
  102. else if ($_REQUEST['redirurl'])
  103.     $redirurl = $_REQUEST['redirurl'];
  104.  
  105. $macfilter = !isset($cpcfg['nomacfilter']);
  106. $passthrumac = isset($cpcfg['passthrumacadd']);
  107.  
  108. /* find MAC address for client */
  109. if ($macfilter || $passthrumac) {
  110.     $tmpres = pfSense_ip_to_mac($clientip);
  111.     if (!is_array($tmpres)) {
  112.         /* unable to find MAC address - shouldn't happen! - bail out */
  113.         captiveportal_logportalauth("unauthenticated","noclientmac",$clientip,"ERROR");
  114.         echo "An error occurred.  Please check the system logs for more information.";
  115.         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.");
  116.         ob_flush();
  117.         return;
  118.     }
  119.     $clientmac = $tmpres['macaddr'];
  120.     unset($tmpres);
  121. }
  122.  
  123. /* find out if we need RADIUS + RADIUSMAC or not */
  124. if (file_exists("{$g['vardb_path']}/captiveportal_radius_{$cpzone}.db")) {
  125.     $radius_enable = TRUE;
  126.     if (isset($cpcfg['radmac_enable']))
  127.         $radmac_enable = TRUE;
  128. }
  129.  
  130. /* find radius context */
  131. $radiusctx = 'first';
  132. if ($_POST['auth_user2'])
  133.     $radiusctx = 'second';
  134.  
  135. if ($_POST['logout_id']) {
  136.     echo <<<EOD
  137. <HTML>
  138. <HEAD><TITLE>Disconnecting...</TITLE></HEAD>
  139. <BODY BGCOLOR="#3953A3">
  140. <SPAN STYLE="color: #ffffff; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">
  141. <B>You have been disconnected.</B>
  142. </SPAN>
  143. <SCRIPT LANGUAGE="JavaScript">
  144. <!--
  145. setTimeout('window.close();',5000) ;
  146. -->
  147. </SCRIPT>
  148. </BODY>
  149. </HTML>
  150.  
  151. EOD;
  152.     captiveportal_disconnect_client($_POST['logout_id']);
  153.  
  154. } else if ($clientmac && $radmac_enable && portal_mac_radius($clientmac,$clientip, $radiusctx)) {
  155.     /* radius functions handle everything so we exit here since we're done */
  156.  
  157. } else if (portal_consume_passthrough_credit($clientmac)) {
  158.     /* allow the client through if it had a pass-through credit for its MAC */
  159.     captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
  160.     portal_allow($clientip, $clientmac, "unauthenticated");
  161.  
  162. } else if (isset($config['voucher'][$cpzone]['enable']) && $_POST['accept'] && $_POST['auth_voucher']) {
  163.     $voucher = trim($_POST['auth_voucher']);
  164.     $timecredit = voucher_auth($voucher);
  165.     // $timecredit contains either a credit in minutes or an error message
  166.     if ($timecredit > 0) {  // voucher is valid. Remaining minutes returned
  167.         // if multiple vouchers given, use the first as username
  168.         $a_vouchers = preg_split("/[\t\n\r ]+/s",$voucher);
  169.         $voucher = $a_vouchers[0];
  170.         $attr = array( 'voucher' => 1,
  171.                 'session_timeout' => $timecredit*60,
  172.                 'session_terminate_time' => 0);
  173.         if (portal_allow($clientip, $clientmac,$voucher,null,$attr)) {
  174.             // YES: user is good for $timecredit minutes.
  175.             captiveportal_logportalauth($voucher,$clientmac,$clientip,"Voucher login good for $timecredit min.");
  176.         } else {
  177.             portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
  178.         }
  179.     } else if (-1 == $timecredit) {  // valid but expired
  180.         captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE","voucher expired");
  181.         portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgexpired'] ? $config['voucher'][$cpzone]['msgexpired']: $errormsg);
  182.     } else {
  183.         captiveportal_logportalauth($voucher,$clientmac,$clientip,"FAILURE");
  184.         portal_reply_page($redirurl, "error", $config['voucher'][$cpzone]['msgnoaccess'] ? $config['voucher'][$cpzone]['msgnoaccess'] : $errormsg);
  185.     }
  186.  
  187. } else if ($_POST['accept'] && $radius_enable) {
  188.     if (($_POST['auth_user'] && isset($_POST['auth_pass'])) || ($_POST['auth_user2'] && isset($_POST['auth_pass2']))) {
  189.         if (!empty($_POST['auth_user'])) {
  190.             $user = $_POST['auth_user'];
  191.             $paswd = $_POST['auth_pass'];
  192.         } else if (!empty($_POST['auth_user2'])) {
  193.             $user = $_POST['auth_user2'];
  194.             $paswd = $_POST['auth_pass2'];
  195.         }
  196.         $auth_list = radius($user,$paswd,$clientip,$clientmac,"USER LOGIN", $radiusctx);
  197.         $type = "error";
  198.         if (!empty($auth_list['url_redirection'])) {
  199.             $redirurl = $auth_list['url_redirection'];
  200.             $type = "redir";
  201.         }
  202.  
  203.         if ($auth_list['auth_val'] == 1) {
  204.             captiveportal_logportalauth($user,$clientmac,$clientip,"ERROR",$auth_list['error']);
  205.             portal_reply_page($redirurl, $type, $auth_list['error'] ? $auth_list['error'] : $errormsg);
  206.         } else if ($auth_list['auth_val'] == 3) {
  207.             captiveportal_logportalauth($user,$clientmac,$clientip,"FAILURE",$auth_list['reply_message']);
  208.             portal_reply_page($redirurl, $type, $auth_list['reply_message'] ? $auth_list['reply_message'] : $errormsg);
  209.         }
  210.     } else {
  211.         if (!empty($_POST['auth_user']))
  212.             $user = $_POST['auth_user'];
  213.         else if (!empty($_POST['auth_user2']))
  214.             $user = $_POST['auth_user2'];
  215.         else
  216.             $user = 'unknown';
  217.         captiveportal_logportalauth($user ,$clientmac,$clientip,"ERROR");
  218.         portal_reply_page($redirurl, "error", $errormsg);
  219.     }
  220.  
  221. } else if ($_POST['accept'] && $cpcfg['auth_method'] == "local") {
  222.     if ($_POST['auth_user'] && $_POST['auth_pass']) {
  223.         //check against local user manager
  224.         $loginok = local_backed($_POST['auth_user'], $_POST['auth_pass']);
  225.  
  226.         if ($loginok && isset($cpcfg['localauth_priv']))
  227.             $loginok = userHasPrivilege(getUserEntry($_POST['auth_user']), "user-services-captiveportal-login");
  228.  
  229.         if ($loginok){
  230.             captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"LOGIN");
  231.             portal_allow($clientip, $clientmac,$_POST['auth_user']);
  232.         } else {
  233.             captiveportal_logportalauth($_POST['auth_user'],$clientmac,$clientip,"FAILURE");
  234.             portal_reply_page($redirurl, "error", $errormsg);
  235.         }
  236.     } else
  237.         portal_reply_page($redirurl, "error", $errormsg);
  238.  
  239. } else if ($_POST['accept'] && $clientip && $cpcfg['auth_method'] == "none") {
  240.     captiveportal_logportalauth("unauthenticated",$clientmac,$clientip,"ACCEPT");
  241.     portal_allow($clientip, $clientmac, "unauthenticated");
  242.    
  243. } else if ((isset($_COOKIE['cookie_portal']) && already_connected($_COOKIE['cookie_portal']))){
  244.         /* if we have a valid session, display already connected page - offer logout */
  245.         portal_reply_page($redirurl, "already_connected",null,$clientmac,$clientip);
  246. } else {
  247.     /* display captive portal page */
  248.     portal_reply_page($redirurl, "login",null,$clientmac,$clientip);
  249. }
  250.  
  251. ob_flush();
  252.  
  253. ?>
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