phpPoA2
[ class tree: phpPoA2 ] [ index: phpPoA2 ] [ all elements ]

Source for file SourceIPAddrAuthzEngine.php

Documentation is available at SourceIPAddrAuthzEngine.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * This hook is executed right after retrieving source address and
  28.  * the arrays of allowed and denied patterns that will be checked inmediately.
  29.  * It can be used to alter the source address, and also to configure the filters on runtime.
  30.  * The hook receives the source IP address, the allowed and the denied patterns.
  31.  * Functions for this hook must be defined like this:
  32.  *
  33.  * function ipBeforeFilterHook(&$ipaddr, &$allowed, &$denied);
  34.  *
  35.  * Please bear in mind that hooks must return TRUE or they'll keep other hooks from executing.
  36.  */
  37. define("SOURCEADDR_BEFORE_FILTERS""SOURCEADDR_BEFORE_FILTERS");
  38.  
  39. /**
  40.  * Authorization engine that works by checking the source IP address of the request.
  41.  * PLEASE NOTE THAT THIS ENGINE SILENTLY IGNORES BOTH USER AND ATTRIBUTES.
  42.  * PLEASE NOTE THAT THIS ENGINE WORKS ONLY FOR WEB-BASED APPLICATIONS.
  43.  * @package phpPoA2
  44.  * @subpackage SourceIPAddrAuthorizationEngine
  45.  */
  46.  
  47.     protected $valid_hooks = array(SOURCEADDR_BEFORE_FILTERS);
  48.  
  49.     /**
  50.      * PLEASE NOTE THAT THIS ENGINE SILENTLY IGNORES BOTH USER AND ATTRIBUTES.
  51.      */
  52.     public function isAuthorized($user$attrs{
  53.         $default $this->cfg->getDefaultBehaviour();
  54.  
  55.         // proxy support
  56.         $src_addr (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $_SERVER['HTTP_X_FORWARDED_FOR'$_SERVER['REMOTE_ADDR'];
  57.  
  58.         // check if there are IP filters
  59.         $allowed $this->cfg->getAllowed();
  60.         $denied $this->cfg->getDenied();
  61.  
  62.         // run hook before checking patterns
  63.         $args array($src_addr$allowed$denied);
  64.         $this->runHooks(SOURCEADDR_BEFORE_FILTERS$args);
  65.         $src_addr $args[0];
  66.         $allowed $args[1];
  67.         $denied $args[2];
  68.  
  69.         $allowed_match $this->matches($src_addr$allowed);
  70.         $denied_match  $this->matches($src_addr$denied);
  71.  
  72.         // check matches giving priority to the default setting
  73.         $order array($default!$default);
  74.         foreach ($order as $option{
  75.             if ($option// check allowed attributes
  76.                 if ($allowed_match{
  77.                     trigger_error(PoAUtils::msg('source-ip-allowed'array($src_addr$allowed_match))E_USER_WARNING);
  78.                     return true;
  79.                 }
  80.             else // check denied attributes
  81.                 if ($denied_match{
  82.                     trigger_error(PoAUtils::msg('source-ip-denied'array($src_addr$denied_match))E_USER_WARNING);
  83.                     return false;
  84.                 }
  85.             }
  86.         }
  87.  
  88.     // default response
  89.     trigger_error(PoAUtils::msg('authz-default-fallback')E_USER_NOTICE);
  90.         return $default;
  91.     }
  92.  
  93.  
  94.  
  95.     public function getAuthorizedList({
  96.         $this->registerHandler();
  97.         $list $this->cfg->getAllowed();
  98.         $this->clean();
  99.         return $list;
  100.     }
  101.  
  102.     public function authorize($user$attrs$ref$expires 0{
  103.         return false;
  104.     }
  105.  
  106.     public function revoke($mail{
  107.         return false;
  108.     }
  109.  
  110.     /**
  111.      * Check if an IP address matches the current allowed patterns.
  112.      * @param ip The IP address.
  113.      * @param patterns An array of patterns to be matched with.
  114.      * @return The matched pattern. False otherwise.
  115.      */
  116.     private function matches($addr$patterns{
  117.         // setup filtering criteria
  118.         $search array("/\./",
  119.                         "/\.0/",
  120.                         // IPv6 support
  121.                         "/(:0){1,7}/",
  122.                         "/^::/",
  123.                         "/::$/");
  124.         $replace array("\.",
  125.                          ".\d{1,3}",
  126.                          // IPv6 support
  127.                          "::",
  128.                          "(([0-9a-fA-F]{1,4})){1,7}\:",
  129.                          "(\:([0-9a-fA-F]{1,4})){1,7}");
  130.  
  131.         foreach ($patterns as $pattern{
  132.             if (is_array($pattern)) continue// arrays are not supported, just single strings
  133.  
  134.             // transform from network notation to regular expression
  135.             $mask preg_replace($search$replace$pattern);
  136.  
  137.             if (preg_match("/".$mask."/i"$addr)) {
  138.                 return $pattern;
  139.             }
  140.         }
  141.         return false;
  142.     }
  143.  
  144. }
  145.  
  146. ?>

Documentation generated on Tue, 25 Jan 2011 11:24:39 +0100 by phpDocumentor 1.4.3