"Fossies" - the Fresh Open Source Software Archive

Member "icinga-web-1.14.1/app/modules/Api/lib/config/AccessConfigHandler.class.php" (19 Dec 2017, 9862 Bytes) of package /linux/misc/old/icinga-web-1.14.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 // {{{ICINGA_LICENSE_CODE}}}
    3 // -----------------------------------------------------------------------------
    4 // This file is part of icinga-web.
    5 //
    6 // Copyright (c) 2009-2015 Icinga Developer Team.
    7 // All rights reserved.
    8 //
    9 // icinga-web is free software: you can redistribute it and/or modify
   10 // it under the terms of the GNU General Public License as published by
   11 // the Free Software Foundation, either version 3 of the License, or
   12 // (at your option) any later version.
   13 //
   14 // icinga-web is distributed in the hope that it will be useful,
   15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 // GNU General Public License for more details.
   18 //
   19 // You should have received a copy of the GNU General Public License
   20 // along with icinga-web.  If not, see <http://www.gnu.org/licenses/>.
   21 // -----------------------------------------------------------------------------
   22 // {{{ICINGA_LICENSE_CODE}}}
   23 
   24 
   25 
   26 
   27 class AccessConfigHandler extends AgaviXmlConfigHandler {
   28     protected $document = null;
   29     private $parseModules = true;
   30     protected $xpath = null;
   31 
   32     private $instances = array();
   33     private $hosts = array();
   34     private $defaults = array(
   35         "r" => array(),
   36         "w" => array(),
   37         "rw" => array(),
   38         "x" => array()
   39     );
   40     private $defaultHost;
   41     private $importModules = true;
   42 
   43     const XML_NAMESPACE = 'http://icinga.org/api/config/parts/access/1.0';
   44 
   45     public function setImportModuleConfigurations($bool) {
   46         $this->importModules = $bool;
   47     }
   48 
   49     public function execute(AgaviXmlConfigDomDocument $document) {
   50         $this->document = $document;
   51 
   52         $this->setupXPath();
   53         $this->fetchDefaults();
   54         if($this->importModules)
   55             $this->importModuleConfigurations();
   56 
   57         $this->fetchHosts();
   58         $this->mapInstances();
   59         return $this->generate("return ".var_export(array(
   60             "instances" => $this->instances,
   61             "hosts" => $this->hosts,
   62             "defaults" => $this->defaults,
   63             "defaultHost" => $this->defaultHost
   64         ),true));
   65     }
   66 
   67     protected function setupXPath() {
   68         $this->xpath = new DOMXPath($this->document);
   69         $this->xpath->registerNamespace("ac",self::XML_NAMESPACE);
   70         $this->xpath->registerNamespace("ae","http://agavi.org/agavi/config/global/envelope/1.0");
   71     }
   72 
   73     private function importModuleConfigurations() {
   74         $moduleDir = AgaviToolkit::literalize("%core.module_dir%");
   75         $modules = scandir($moduleDir);
   76         foreach($modules as $folder) {
   77             $dir = $moduleDir;
   78             if($folder == ".." || $folder == "." || $folder == "Api")
   79                 continue;
   80             $dir = $dir."/".$folder."/";
   81 
   82             if(!is_dir($dir) || !is_readable($dir))
   83                 continue;
   84             $accessLocation = $dir."config/access.xml";
   85             if(file_exists($accessLocation) && is_readable($accessLocation))
   86                 $this->importModuleXML($accessLocation);
   87 
   88         }
   89     }
   90 
   91     private function importModuleXML($accessLocation) {
   92         $config = include AgaviConfigCache::checkConfig(AgaviToolkit::expandDirectives($accessLocation));
   93         $this->instances = array_merge_recursive($this->instances,$config["instances"]);
   94         $this->defaults = array_merge_recursive($this->defaults,$config["defaults"]);
   95         $this->hosts = array_merge_recursive($this->hosts,$config["hosts"]);
   96     }
   97 
   98     private function fetchDefaults() {
   99         $defaultNodes = $this->xpath->query('//ac:defaults/node()');
  100         foreach($defaultNodes as $node) {
  101             if($node->nodeType != XML_ELEMENT_NODE)
  102                 continue;
  103             $this->registerDefaults($node);
  104         }
  105     }
  106 
  107     private function fetchHosts() {
  108         $defaultNodes = $this->xpath->query('//ac:hosts/node()');
  109         foreach($defaultNodes as $node) {
  110             if($node->nodeType != XML_ELEMENT_NODE)
  111                 continue;
  112             $this->registerHost($node);
  113         }
  114     }
  115 
  116     private function mapInstances() {
  117         $defaultNodes = $this->xpath->query('//ac:instances/node()');
  118         foreach($defaultNodes as $node) {
  119             if($node->nodeType != XML_ELEMENT_NODE)
  120                 continue;
  121             $instance = $node->getAttribute("name");
  122             $hosts = explode(";",$node->nodeValue);
  123 
  124             foreach($hosts as $host) {
  125                 if(!isset($this->hosts[$host]))
  126                     throw new AppKitException("Instance ".$instance." is mapped to unknown host ".$host);
  127             }
  128             $this->instances[$instance] = $hosts;
  129 
  130         }
  131     }
  132 
  133     private function registerDefaults(DOMNode $node) {
  134 
  135         foreach($node->childNodes as $child) {
  136             if($node->nodeType != XML_ELEMENT_NODE)
  137                 continue;
  138             if($node->nodeName == 'defaultHost') {
  139                 $this->defaultHost = $node->nodeValue;
  140                 continue;
  141             }
  142             if($node->nodeName != 'access')
  143                 continue; // shouldn't happen, we have our dtd
  144             foreach($node->childNodes as $accessDefinition) {
  145                 if($accessDefinition->nodeType != XML_ELEMENT_NODE)
  146                     continue;
  147 
  148                 $this->parseAccessDefinition($accessDefinition);
  149             }
  150         }
  151     }
  152 
  153     private function registerHost(DOMNode $node) {
  154         $hostname = $node->getAttribute("name");
  155         if(!isset($this->hosts[$hostname])) {
  156             $this->hosts[$hostname] = array(
  157                 "auth" => array(),
  158                 "r" => array(),
  159                 "w" => array(),
  160                 "rw" => array(),
  161                 "x" => array()
  162             );
  163         }
  164         $auth = &$this->hosts[$hostname]["auth"];
  165         foreach($node->childNodes as $hostinfo) {
  166             switch($hostinfo->nodeName) {
  167                 case 'type':
  168                     $auth["type"] = $hostinfo->nodeValue;
  169                     break;
  170                 case 'access':
  171                     // apply default rules to hosts if necessary
  172                     if($hostinfo->getAttribute("useDefaults") == "true")
  173                          $this->applyDefaultsToHost($hostname);
  174                     // apply host specific rules
  175                     if(!$hostinfo->hasChildNodes())
  176                         break;
  177                     foreach($hostinfo->childNodes as $accessNode)
  178                         $this->parseAccessDefinition($accessNode,$hostname);
  179                     break;
  180                 case 'ssh-config':
  181                     $this->applySSHConfig($hostinfo,$hostname);
  182             }
  183         }
  184     }
  185 
  186     private function applyDefaultsToHost($hostname) {
  187         if(!isset($this->hosts[$hostname]))
  188             return;
  189         $host = &$this->hosts[$hostname];
  190         foreach($this->defaults as $type=>$value) {
  191             foreach($value as $symbol=>$path)
  192                 if(!isset($host[$type][$symbol]))
  193                     $host[$type][$symbol] = $path;
  194         }
  195     }
  196 
  197     private function applySSHConfig(DOMNode $sshNode, $host) {
  198         $auth = &$this->hosts[$host]["auth"];
  199         foreach($sshNode->childNodes as $authInfo) {
  200             if($authInfo->nodeType != XML_ELEMENT_NODE)
  201                 continue;
  202 
  203             switch($authInfo->nodeName) {
  204                 case 'type':
  205                     $auth["method"] = $authInfo->nodeValue;
  206                 case 'auth':
  207                     $this->applySSHConfig($authInfo,$host);
  208                     break;
  209                 default:
  210                     $auth[$authInfo->nodeName] = $authInfo->nodeValue;
  211             }
  212         }
  213     }
  214 
  215     private function parseAccessDefinition(DOMNode $accessNode,$host = null) {
  216         $type;
  217 
  218         switch($accessNode->nodeName) {
  219             case 'readwrite':
  220                 $type = 'rw';
  221                 break;
  222             case 'read':
  223                 $type = 'r';
  224                 break;
  225             case 'write':
  226                 $type = 'w';
  227                 break;
  228             case 'execute':
  229                 $type = 'x';
  230                 break;
  231             default:
  232                 continue;
  233         }
  234         // defaults are not additive
  235         if($host == null)
  236             $this->defaults[$type] = array();
  237         if(!$accessNode->hasChildNodes())
  238             return;
  239         foreach($accessNode->childNodes as $resourceCollection) {
  240             if($resourceCollection->nodeName == 'files' ||
  241                     $resourceCollection->nodeName == 'folders')
  242                 $this->addResource($resourceCollection,$resourceCollection->nodeName, $type,$host);
  243 
  244         }
  245     }
  246 
  247     private function addResource(DOMNode $node,$resourceType,$accesstype, $host = null) {
  248 
  249         foreach($node->childNodes as $resource) {
  250             if($resource->nodeName != 'resource')
  251                 continue;
  252             $symbol = $resource->getAttribute("name");
  253             $target = $resource->nodeValue;
  254             if($resourceType == "folders")
  255                 $target = dirname($target).'/'.basename($target).'/*';
  256             if(!$host)
  257                 $this->defaults[$accesstype][$symbol] = $target;
  258             else if(isset($this->hosts[$host]))
  259                 $this->hosts[$host][$accesstype][$symbol] = $target;
  260         }
  261 
  262     }
  263 }
  264 
  265 class AccessConfigModuleHandler extends AccessConfigHandler {
  266 
  267     const XML_NAMESPACE = 'http://icinga.org/api/config/parts/access/module/1.0';
  268     public function execute(AgaviXmlConfigDomDocument $dom) {
  269         $this->setImportModuleConfigurations(false);
  270         return parent::execute($dom);
  271     }
  272     protected function setupXPath() {
  273         $this->xpath = new DOMXPath($this->document);
  274         $this->xpath->registerNamespace("ac",self::XML_NAMESPACE);
  275         $this->xpath->registerNamespace("ae","http://agavi.org/agavi/config/global/envelope/1.0");
  276     }
  277 }