Code

979a545503407dc7325043901b852e83fc26071a
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class gosaSupportDaemon
24 {
25   private $s_host       = "";
26   private $i_port       = 0;
27   private $s_encryption_key = "";
29   private $o_sock       = NULL;
30   private $f_timeout    = 2;
31   private $s_error      = "";
32   private $b_error      = FALSE;
34   private $is_connected     = FALSE;
36   protected $use_alternative_xml_parse_method = FALSE;
38   /*! \brief  Creates a new gosaSupportDaemon object.
39     @param string   Host    The Host where the daemon is running on.  
40     @param integer  Port    The port which the daemon use.
41     @param string   Key     The encryption string.
42     @param boolean  Connect Directly connect to daemon socket.
43     @param float    Timeout The timelimit for all socket actions.
44    */
45   public function __construct($connect=TRUE,$timeout=10)
46   {
47     #FIXME: bad idea about referencing global variables from within classes
48     global $config;
50     /* This should only be the case if we call this from setup.
51         __autoload() 
52      */
53     if(!is_object($config)) { return; }
55     # load from config, store statically
56     if ($config->get_cfg_value("gosa_si") != ""){
58       if ($this->s_host == ""){
59         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosa_si"));
60         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosa_si"));
61         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosa_si"));
62       }
64       $this->f_timeout = $timeout;
65       if($connect){
66         $this->connect();
67       }
68     }
69   }
72   /*! \brief  Establish daemon connection. 
73     @return boolean Returns true if the connection was succesfully established. 
74    */
75   public function connect()
76   {
77     if(!empty($this->s_host) && !empty($this->i_port)){
78       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
79       if($this->o_sock->connected()){ 
80         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
81         $this->is_connected = TRUE;
82       }else{
83         $this->set_error($this->o_sock->get_error());
84         $this->disconnect();
85         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
86       }
87     }else{
88       $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
89     }
90     return($this->is_connected);
91   }
93   
94   /*! \brief  Returns TRUE whether we are connected or not 
95       @return BOOLEAN  Returns TRUE when connected else FALSE
96    */
97   public function is_connected()
98   {
99     return($this->is_connected);
100   }
101  
103   /*! \brief  */
104   public function get_hosts_with_module($mod)
105   {
106     $data = array("module_name" => $mod);
107     $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
108     $hosts = array();
109     if(isset($res['XML'][0]['HOST'])){
110       foreach($res['XML'][0]['HOST'] as $entry){
111         $hosts [] = $entry['VALUE'];
112       }
113     }
114     return($hosts);
115   }
118   /*! \brief  Disconnect from gosa daemon.
119    */
120   public function disconnect()
121   {
122     $this->o_sock->close();
123     $this->is_connected = FALSE;
124   }
127   /*! \brief  Sets an error message, which can be returned with get_error().
128     @param  string  The Error message,
129    */
130   private function set_error($str)
131   {
132     $this->b_error = TRUE;
133     $this->s_error = $str;
134   }
137   /*! \brief  Sets an error message, which can be returned with get_error().
138     @param  string  The Error message,
139    */
140   private function reset_error()
141   {
142     $this->b_error = FALSE;
143     $this->s_error = "";
144   }
147   /*! \brief  Checks if an error occured.
148     @return boolean returns TRUE or FALSE, whether there is an error or not.
149    */
150   public function is_error()
151   {
152     return($this->b_error);
153   }
156   /*! \brief  Returns the last error. 
157     @return Returns the last error.
158    */
159   public function get_error()
160   {
161     $str = $this->s_error;
162     $ret = "";
163     if(is_string($str)){
164       $ret = $str;
165     }else{
166       foreach($str as $msg){
167         $ret .= $msg." ";
168       }
169     }
170     $ret = preg_replace("/ /","&nbsp;",$ret);
171     return($ret);
172   }
175   public function FAI_get_kernels($release)
176   {
177     $xml_msg = 
178       "<xml>".
179       "<header>gosa_get_available_kernel</header>".
180       "<source>GOSA</source>".
181       "<target>GOSA</target>".
182       "<release>".$release."</release>".
183       "</xml>";
185     $ret = array();
186     if($this->connect()){
187       $this->o_sock->write($xml_msg);
188       $str = trim($this->o_sock->read());
190       /* Check if something went wrong while reading */
191       if($this->o_sock->is_error()){
192         $this->set_error($this->o_sock->get_error());
193         return($ret);
194       }
196       $entries = $this->xml_to_array($str);
197       if(isset($entries['XML']) && is_array($entries['XML'])){
199         /* Check if returned values represent a valid answer */
200         if(isset($entries['XML'])){
201           if(isset($entries['XML']['ERROR_STRING'])) {
202             $this->set_error($entries['XML']['ERROR_STRING']);
203             new log("debug","GOsa-si",
204                 get_class($this)."::".__FUNCTION__, array(),
205                 "FAILED error was ".$this->get_error());
206             return($ret);
207           }
209           /* Unset header tags */
210           $ret = $entries['XML'];
211           foreach($ret as $key => $entry){
212             if(!preg_match("/^answer/i",$key)){
213               unset($ret[$key]);
214             }
215           }
216         }
217       }
218     }
219     return($ret);
220   }
223   public function FAI_get_package_sections($release)
224   {
225     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
226       "<select>distinct section</select>".
227       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
229     $ret = array();
230     if($this->connect()){
231       $this->o_sock->write($xml_msg);
232       $str = trim($this->o_sock->read());
234       /* Check if something went wrong while reading */
235       if($this->o_sock->is_error()){
236         $this->set_error($this->o_sock->get_error());
237         return($ret);
238       }
240       $entries = $this->xml_to_array($str);
241       if(isset($entries['XML']) && is_array($entries['XML'])){
243         /* Check if returned values represent a valid answer */
244         if(isset($entries['XML'])){
245           if(isset($entries['XML']['ERROR_STRING'])) {
246             $this->set_error($entries['XML']['ERROR_STRING']);
247             new log("debug","GOsa-si",
248                 get_class($this)."::".__FUNCTION__, array(),
249                 "FAILED error was ".$this->get_error());
250             return($ret);
251           }
253           /* Unset header tags */
254           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
255             if(isset($entries['XML'][$type])){
256               unset($entries['XML'][$type]);
257             }
258           }
259           $ret = $entries['XML'];
260         }
261       }
262     }
263     return($ret);
264   }
267   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
268   {
269     $this->reset_error();
270     $ret = array();
272     /* Check Parameter */
273     if(!is_array($attrs) || !count($attrs)){
274       trigger_error("Second parameter must be an array. With at least one attribute name.");
275       return($ret);
276     }
278     /* Check Parameter */
279     if(!is_array($package)){
280       trigger_error("Third parameter must be an array. With at least one attribute name.");
281       return($ret);
282     }
284     /* Create list of attributes to fetch */
285     $attr = ""; 
286     foreach($attrs as $at){
287       $attr.= "<select>".$at."</select>";
288     }
290     /* If no package is given, search for all */
291     if(!count($package)) $package = array("%");
293     /* Create limit tag */
294     if($from == -1){
295       $limit =""; 
296     }else{
297       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
298     }
300     /* Create list of attributes to fetch */
301     $pkgs = ""; 
302     foreach($package as $pkg){
303       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
304     }
306     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
307       $attr.
308       "<where>
309       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
310       <clause><connector>OR</connector>
311       ".$pkgs."
312       </clause>
313       </where>".
314       $limit.
315       "</xml>";
317     if($this->connect()){
318       $this->o_sock->write($xml_msg);
319       $str = trim($this->o_sock->read());
321       /* Check if something went wrong while reading */
322       if($this->o_sock->is_error()){
323         $this->set_error($this->o_sock->get_error());
324         return($ret);
325       }
327       $entries = $this->xml_to_array($str);
328       if(isset($entries['XML']) && is_array($entries['XML'])){
330         /* Check if returned values represent a valid answer */
331         if(isset($entries['XML'])){
332           if(isset($entries['XML']['ERROR_STRING'])) {
333             $this->set_error($entries['XML']['ERROR_STRING']);
334             new log("debug","GOsa-si",
335                 get_class($this)."::".__FUNCTION__, array(),
336                 "FAILED error was ".$this->get_error());
337             return($ret);
338           }
340           /* Unset header tags */
341           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
342             if(isset($entries['XML'][$type])){
343               unset($entries['XML'][$type]);
344             }
345           }
346           $ret = $entries['XML'];
347         }
348       }
349     }
350     return($ret);
352     
353   }
356   public function FAI_get_server($name = "")
357   {
358     $this->reset_error();
360     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
361     $ret = array();
362     if($this->connect()){
363       $this->o_sock->write($xml_msg);
364       $str = trim($this->o_sock->read());
366       /* Check if something went wrong while reading */
367       if($this->o_sock->is_error()){
368         $this->set_error($this->o_sock->get_error());
369         return($ret);
370       }
372       $entries = $this->xml_to_array($str);
373       if(isset($entries['XML']) && is_array($entries['XML'])){
375         /* Check if returned values represent a valid answer */
376         if(isset($entries['XML'])){
377           if(isset($entries['XML']['ERROR_STRING'])) {
378             $this->set_error($entries['XML']['ERROR_STRING']);
379             new log("debug","GOsa-si", 
380               get_class($this)."::".__FUNCTION__, array(),
381               "FAILED error was ".$this->get_error());
382             return($ret);
383           }
385           /* Unset header tags */
386           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
387             if(isset($entries['XML'][$type])){
388               unset($entries['XML'][$type]);
389             }
390           }
391           $ret = $entries['XML']; 
392         }
393       }
394     }
395     return($ret);
396   }
399   public function FAI_get_classes($name)
400   {
401     $this->reset_error();
402     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
403                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
404     $ret = array();
405     if($this->connect()){
406       $this->o_sock->write($xml_msg);
407       $str = trim($this->o_sock->read());
409       /* Check if something went wrong while reading */
410       if($this->o_sock->is_error()){
411         $this->set_error($this->o_sock->get_error());
412         return($ret);
413       }
415       $entries = $this->xml_to_array($str);
416       if(isset($entries['XML']) && is_array($entries['XML'])){
418         /* Check if returned values represent a valid answer */
419         if(isset($entries['XML'])){
420           if(isset($entries['XML']['ERROR_STRING'])) {
421             $this->set_error($entries['XML']['ERROR_STRING']);
422             new log("debug","GOsa-si", 
423               get_class($this)."::".__FUNCTION__, array($name),
424               "FAILED error was ".$this->get_error());
425             return($ret);
426           }
428           /* Unset header tags */
429           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
430             if(isset($entries['XML'][$type])){
431               unset($entries['XML'][$type]);
432             }
433           }
434           $ret = $entries['XML']; 
435         }
436       }
437     }
438     return($ret);
439   }
442   /*! \brief  Returns an array containing all queued entries.
443     @return Array All queued entries as an array.
444    */
445   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
446   {
447     $this->reset_error();
448     $ret = array();
450     $tags = "";
451     foreach($event_types as $type){
452       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
453     }
454     if(count($event_types) > 1){
455       $tags = "<connector>or</connector>".$tags;
456     }
457     if(count($event_types)){
458       $tags = "<where><clause>".$tags."</clause></where>";
459     }
461     $xml_msg = 
462       "<xml>
463       <header>gosa_query_jobdb</header>
464       <target>GOSA</target>
465       <source>GOSA</source>
466       ".$tags."
468       <orderby>".$sort."</orderby>";
469     if($from != -1 && $to != -1){
470       $xml_msg.= "
471         <limit>
472         <from>".$from."</from>
473         <to>".$to."</to>
474         </limit>";
475     }
476     $xml_msg.= "
477       </xml>";
479     if($this->connect()){
480       $this->o_sock->write($xml_msg);
481       $str = trim($this->o_sock->read());
483       /* Check if something went wrong while reading */
484       if($this->o_sock->is_error()){
485         $this->set_error($this->o_sock->get_error());
486         return($ret);
487       }
489       $entries = $this->xml_to_array($str);
490       if(isset($entries['XML']) && is_array($entries['XML'])){
492         /* Check if returned values represent a valid answer */
493         if(isset($entries['XML'])){
494           
495           /* Unset header tags */
496           foreach(array("HEADER","SOURCE","TARGET") as $type){
497             unset($entries['XML'][$type]);
498           }
499           $ret = $entries['XML']; 
500         }
501       }
502     }
503     
504     /* Remove session ID. No one is interested in this... */
505     unset($ret['SESSION_ID']);
507     return($ret);
508   }
511   /*! \brief  Checks if the given ids are used queue ids.
512     @param  Array   The ids we want to check..
513     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
514    */
515   public function ids_exist($ids)
516   {
517     if(!is_array($ids)){
518       trigger_error("Requires an array as parameter.");
519       return;
520     }
521     $this->reset_error();
523     $ret = array();
525     $xml_msg = "<xml>
526       <header>gosa_query_jobdb</header>
527       <target>GOSA</target>
528       <source>GOSA</source>
529       <where>
530       <clause>
531       <connector>or</connector>";
532     foreach($ids as $id){
533       $xml_msg .= "<phrase>
534         <operator>eq</operator>
535         <id>".$id."</id>
536         </phrase>";
537     }
538     $xml_msg .= "</clause>
539       </where>
540       </xml>";
542     if($this->connect()){
543       $this->o_sock->write($xml_msg);
544       $str = trim($this->o_sock->read());
546       /* Check if something went wrong while reading */
547       if($this->o_sock->is_error()){
548         $this->set_error($this->o_sock->get_error());
549         return($ret);
550       }
552       $entries = $this->xml_to_array($str);
553       if(isset($entries['XML']) && is_array($entries['XML'])){
554         foreach($entries['XML'] as $entry){
555           if(is_array($entry) && array_key_exists("ID",$entry)){
556             $ret[] = $entry['ID'];
557           }
558         }
559       }
560     }
561     return($ret);
562   }
565   /*! \brief  Returns an entry containing all requested ids.
566     @param  Array   The IDs of the entries we want to return.
567     @return Array   Of the requested entries. 
568    */
569   public function get_entries_by_mac($macs)
570   {
571     if(!is_array($macs)){
572       trigger_error("Requires an array as parameter.");
573       return;
574     }
575     $this->reset_error();
577     $ret = array();
579     $xml_msg = "<xml>
580       <header>gosa_query_jobdb</header>
581       <target>GOSA</target>
582       <source>GOSA</source>
583       <where>
584       <clause>
585       <connector>or</connector>";
586     foreach($macs as $mac){
587       $xml_msg .= "<phrase>
588         <operator>eq</operator>
589         <macaddress>".$mac."</macaddress>
590         </phrase>";
591     }
592     $xml_msg .= "</clause>
593       </where>
594       </xml>";
596     if($this->connect()){
597       $this->o_sock->write($xml_msg);
598       $str = trim($this->o_sock->read());
600       /* Check if something went wrong while reading */
601       if($this->o_sock->is_error()){
602         $this->set_error($this->o_sock->get_error());
603         return($ret);
604       }
606       $entries = $this->xml_to_array($str); 
607       if(isset($entries['XML'])){
608         foreach($entries['XML'] as $name => $entry){
609           if(preg_match("/^ANSWER[0-9]*$/",$name)){
610             $ret[$name] = $entry;
611           }
612         }
613       }
614     }
615     return($ret);
616   }
619   /*! \brief  Returns an entry containing all requested ids.
620     @param  Array   The IDs of the entries we want to return.
621     @return Array   Of the requested entries. 
622    */
623   public function get_entries_by_id($ids)
624   {
625     if(!is_array($ids)){
626       trigger_error("Requires an array as parameter.");
627       return;
628     }
629     $this->reset_error();
631     $ret = array();
633     $xml_msg = "<xml>
634       <header>gosa_query_jobdb</header>
635       <target>GOSA</target>
636       <source>GOSA</source>
637       <where>
638       <clause>
639       <connector>or</connector>";
640     foreach($ids as $id){
641       $xml_msg .= "<phrase>
642         <operator>eq</operator>
643         <id>".$id."</id>
644         </phrase>";
645     }
646     $xml_msg .= "</clause>
647       </where>
648       </xml>";
650     if($this->connect()){
651       $this->o_sock->write($xml_msg);
652       $str = trim($this->o_sock->read());
654       /* Check if something went wrong while reading */
655       if($this->o_sock->is_error()){
656         $this->set_error($this->o_sock->get_error());
657         return($ret);
658       }
660       $entries = $this->xml_to_array($str); 
661       if(isset($entries['XML'])){
662         foreach($entries['XML'] as $name => $entry){
663           if(preg_match("/^ANSWER[0-9]*$/",$name)){
664             $ret[$name] = $entry;
665           }
666         }
667       }
668     }
669     return($ret);
670   }
673   /*! \brief  Checks if the given id is in use.
674     @param  Integer The ID of the entry.
675     @return Boolean TRUE if entry exists. 
676    */
677   public function id_exists($id)
678   {
679     if(!is_numeric($id)){
680       trigger_error("Requires an integer as parameter.");
681       return;
682     }
684     $this->reset_error();
686     $xml_msg = "<xml>
687       <header>gosa_query_jobdb</header>
688       <target>GOSA</target>
689       <source>GOSA</source>
690       <where>
691       <clause>
692       <phrase>
693       <operator>eq</operator>
694       <id>".$id."</id>
695       </phrase>
696       </clause>
697       </where>
698       </xml>";
700     if($this->connect()){
701       $this->o_sock->write($xml_msg);
702       $str = trim($this->o_sock->read());
704       /* Check if something went wrong while reading */
705       if($this->o_sock->is_error()){
706         $this->set_error($this->o_sock->get_error());
707         return(FALSE);
708       }
710       $entries = $this->xml_to_array($str); 
711       if( isset($entries['XML']['HEADER']) && 
712           $entries['XML']['HEADER']=="answer" && 
713           isset($entries['XML']['ANSWER1'])){
714         return(TRUE);
715       }
716     }
717     return(FALSE);
718   }
721   /*! \brief  Returns an entry from the gosaSupportQueue
722     @param  Integer The ID of the entry we want to return.
723     @return Array   Of the requested entry. 
724    */
725   public function get_entry_by_id($id)
726   {
727     if(!is_numeric($id)){
728       trigger_error("Requires an integer as parameter.");
729       return;
730     }
731     $this->reset_error();
732   
733     $ret = array();
734     $xml_msg = "<xml>
735       <header>gosa_query_jobdb</header>
736       <target>GOSA</target>
737       <source>GOSA</source>
738       <where>
739       <clause>
740       <phrase>
741       <operator>eq</operator>
742       <id>".$id."</id>
743       </phrase>
744       </clause>
745       </where>
746       </xml>";
747     if($this->connect()){
748       $this->o_sock->write($xml_msg);
749       $str = trim($this->o_sock->read());
751       /* Check if something went wrong while reading */
752       if($this->o_sock->is_error()){
753         $this->set_error($this->o_sock->get_error());
754         return($ret);
755       }
757       $entries = $this->xml_to_array($str); 
758       if( isset($entries['XML']['HEADER']) &&
759           $entries['XML']['HEADER']=="answer" &&
760           isset($entries['XML']['ANSWER1'])){
761         $ret = $entries['XML']['ANSWER1'];
762       }
763     }
764     return($ret);
765   }
768   /*! \brief  Removes a set of entries from the GOsa support queue. 
769     @param  Array The IDs to remove.
770     @return Boolean True on success.
771    */
772   public function remove_entries($ids)
773   {
774     if(!is_array($ids)){
775       trigger_error("Requires an array as parameter.");
776       return;
777     }
779     $this->reset_error();
781     $ret = array();
783     $xml_msg = "<xml>
784       <header>gosa_delete_jobdb_entry</header>
785       <target>GOSA</target>
786       <source>GOSA</source>
787       <where>
788       <clause>
789       <connector>or</connector>";
790     foreach($ids as $id){
791       $xml_msg .= "<phrase>
792         <operator>eq</operator>
793         <id>".$id."</id>
794         </phrase>";
795     }
796     $xml_msg .= "</clause>
797       </where>
798       </xml>";
800     if($this->connect()){
801       $this->o_sock->write($xml_msg);
802       $str = $this->o_sock->read();
804       /* Check if something went wrong while reading */
805       if($this->o_sock->is_error()){
806         $this->set_error($this->o_sock->get_error());
807         return($ret);
808       }
810       $entries = $this->xml_to_array($str);
811       if(isset($entries['XML']) || isset($entries['COUNT'])){
812         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
813         return(TRUE);
814       }else{
815         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
816       }
817     }
818     return(FALSE);
819   }
823   /*! \brief  Removes an entry from the GOsa support queue. 
824     @param  Integer The ID of the entry we want to remove.
825     @return Boolean True on success.
826    */
827   public function remove_entry($id)
828   {
829     return($this->remove_entries(array($id)));
830   }
833   /*! \brief  Parses the given xml string into an array 
834     @param  String XML string  
835     @return Array Returns an array containing the xml structure. 
836    */
837   private function xml_to_array($xml,$alternative_method = FALSE)
838   {
839     $params = array();
840     $level = array();
841     $parser  = xml_parser_create_ns();
842     xml_parse_into_struct($parser, $xml, $vals, $index);
844     $err_id = xml_get_error_code($parser);
845     if($err_id){
846       xml_parser_free($parser);
847     }else{
848       xml_parser_free($parser);
850       if($this->use_alternative_xml_parse_method) {
851         $params = $this->build_xml_array($vals);
852       } else {
854         foreach ($vals as $xml_elem) {
855           if ($xml_elem['type'] == 'open') {
856             if (array_key_exists('attributes',$xml_elem)) {
857               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
858             } else {
859               $level[$xml_elem['level']] = $xml_elem['tag'];
860             }
861           }
862           if ($xml_elem['type'] == 'complete') {
864             $start_level = 1;
865             $test2 = &$params;
866             while($start_level < $xml_elem['level']) {
867               $test2 = &$test2[$level[$start_level]];
868               $start_level++;
869             }
871             /* Save tag attributes too. 
872                e.g. <tag attr="val">
873              */
874             if(isset($xml_elem['attributes'])){
875               foreach($xml_elem['attributes'] as $name => $value){
876                 $test2['ATTRIBUTES'][$name] = $value;
877               }
878             }
880             if(!isset($test2[$xml_elem['tag']])){
881               if(isset($xml_elem['value'])){
882                 $test2[$xml_elem['tag']] = $xml_elem['value'];
883               }
884             }else{
885               if(!is_array($test2[$xml_elem['tag']])){
886                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
887               }
888               $test2[$xml_elem['tag']][] = $xml_elem['value'];
889             }
890           }
891         }
892       }
893     }
895     if(!isset($params['XML'])){
896       if (!array_key_exists('XML', $params)){
897         $this->set_error(_("Cannot not parse XML!"));
898       }
899       $params = array("COUNT" => 0);
900     }
902     return($params); 
903   }
906   function build_xml_array(&$vals)
907   {
908     $array = array();
909     while(count($vals)){
910       $key = key($vals);
911       $val = $vals[$key];
912       unset($vals[$key]);
913       if($val['type'] == "close"){
914         return($array);
915       }elseif($val['type']=="open"){
916         $array[$val['tag']][] = $this->build_xml_array($vals);
917       }elseif($val['type'] != "cdata"){
918         $data = array("VALUE" => "","ATTRIBUTES" => "");
919         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
920           if(isset($val[$name])){
921             $data[$attr] = $val[$name];
922           }
923         }
924         $array[$val['tag']][] = $data;
925       }else{
926 #print_a($val);
927       }
928     }
929     return($array);
930   }
937   /*! \brief  Updates an entry with a set of new values, 
938     @param  Integer The ID of the entry, we want to update.
939     @param  Array   The variables to update.   
940     @return Boolean Returns TRUE on success. 
941    */
942   public function update_entries($ids,$data)
943   {
944     $this->reset_error();
945     if(!is_array($ids)){
946       trigger_error("Requires an array as first parameter.");
947       return;
948     }
950     if(!is_array($data)){
951       trigger_error("Requires an array as second parameter.");
952       return;
953     }
955     $attr = "";
956     foreach($data as $key => $value){
957       $key = strtolower($key);
958       if(is_array($value)){
959         foreach($value as $sub_value){
960           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
961         }
962       }else{
963         $attr.= "<$key>".strtolower($value)."</$key>\n";
964       }
965     }
967     $xml_msg = "<xml>
968       <header>gosa_update_status_jobdb_entry</header>
969       <target>GOSA</target>
970       <source>GOSA</source>
971       <where>
972       <clause>
973       <connector>or</connector>";
974     foreach($ids as $id){
975       $xml_msg .= "<phrase>
976         <operator>eq</operator>
977         <id>".$id."</id>
978         </phrase>";
979     }
980     $xml_msg .= "</clause>
981       </where>
982       <update>
983       ".$attr." 
984       </update>
985       </xml>";
987     if($this->connect()){
989       $this->o_sock->write($xml_msg);
990       $str      = trim($this->o_sock->read());
992       /* Check if something went wrong while reading */
993       if($this->o_sock->is_error()){
994         $this->set_error($this->o_sock->get_error());
995         return(FALSE);
996       }
998       $entries = $this->xml_to_array($str);
999       if(isset($entries['XML'])){
1000         if(isset($entries['XML']['ERROR_STRING'])) {
1001           $this->set_error($entries['XML']['ERROR_STRING']);
1002           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
1003           return(FALSE);
1004         }
1005         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
1006         return(TRUE);
1007       }
1008     }
1009     return(FALSE);
1010   }
1013   /*! \brief  Returns the number of currently queued objects.
1014       @return Integer  
1015    */
1016   public function number_of_queued_entries($event_types)
1017   {
1018     $tags = "";
1019     foreach($event_types as $type){
1020       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
1021     }
1022     if(count($event_types) > 1){
1023       $tags = "<connector>or</connector>".$tags;
1024     }
1025     if(count($event_types)){
1026       $tags = "<where><clause>".$tags."</clause></where>";
1027     }
1030     $xml_msg =
1031       "<xml>".
1032       "<header>gosa_query_jobdb</header>".
1033       "<target>GOSA</target>".
1034       "<source>GOSA</source>".
1035       "<select> count ID</select>".
1036       $tags.
1037       "</xml>";
1039     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
1040     $this->connect();
1041     if($this->connect()){
1042       $this->o_sock->write($xml_msg);
1043       $str     = trim($this->o_sock->read());
1045       /* Check if something went wrong while reading */
1046       if($this->o_sock->is_error()){
1047         $this->set_error($this->o_sock->get_error());
1048         return(0);
1049       }
1051       $entries = $this->xml_to_array($str);
1052       if(isset($entries['XML'])){
1053         return($entries['XML']['COUNT']);
1054       }
1055     }
1056     return(-1);
1057   } 
1060   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
1061   {
1062     $xml_message= "";
1064     /* Prepare data */
1065     foreach ($data as $key => $value){
1066       if(is_array($value)){
1067         foreach($value as $sub_value){
1068           $xml_message.= "<$key>$sub_value</$key>";
1069         }
1070       }else{
1071         $xml_message.= "<$key>$value</$key>";
1072       }
1073     }
1075     /* Multiple targets? */
1076     if (!is_array($to)){
1077       $to_targets= array($to);
1078     } else {
1079       $to_targets= $to;
1080     }
1082     /* Build target strings */
1083     $target ="";
1084     foreach($to_targets as $to){
1085       $target.= "<target>$to</target>";
1086     }
1088     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1089   }
1092   /* Allows simply appending a new DaemonEvent 
1093    */
1094   public function append($event)
1095   {
1096     if(!($event instanceof DaemonEvent)){
1097       return(FALSE);
1098     }
1099   
1100     $this->reset_error();
1102     /* Add to queue if new 
1103      */
1104     if($event->is_new()){
1106       $request_answer = FALSE;
1107       if($event->get_type() == SCHEDULED_EVENT){
1108         $action = $event->get_schedule_action();
1109       }elseif($event->get_type() == TRIGGERED_EVENT){
1110         $action = $event->get_trigger_action();
1111       }else{
1112         trigger_error("Unknown type of queue event given.");
1113         return(FALSE);
1114       }
1116       /* Get event informations, like targets..
1117        */
1118       $targets    = $event->get_targets();
1119       $data       = $event->save();
1121       /* Append an entry for each target 
1122        */
1123       foreach($targets as $target){
1124         $data['macaddress'] = $target;
1125         $this->send_data($action,$target,$data,$request_answer);
1127         if($this->is_error()){
1128           return(FALSE);
1129         }
1130       }
1131       return(TRUE);
1132     }else{
1134       /* Updated edited entry.
1135        */
1136       $id                 = $event->get_id();
1137       $data               = $event->save();
1138       return($this->update_entries(array($id),$data));
1139     }
1141     return(FALSE);
1142   }
1145 /*! \brief  Returns an array containing all queued entries.
1146     @return Array All queued entries as an array.
1147    */
1148   public function _send($data, $answer_expected= FALSE)
1149   {
1150     $this->reset_error();
1151     $ret = array();
1153     if($this->connect()){
1154       $this->o_sock->write($data);
1155       if ($answer_expected){
1156         $str = trim($this->o_sock->read());
1158         /* Check if something went wrong while reading */
1159         if($this->o_sock->is_error()){
1160           $this->set_error($this->o_sock->get_error());
1161           return($ret);
1162         }
1164         $entries = $this->xml_to_array($str);
1165         if(isset($entries['XML']) && is_array($entries['XML'])){
1166           $ret = $entries;
1167           if(isset($entries['XML']['ERROR_STRING'])) {
1168             $this->set_error($entries['XML']['ERROR_STRING']);
1169             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1170           }elseif(isset($entries['XML']['ERROR'])){
1171             $this->set_error($entries['XML']['ERROR']);
1172             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1173           }else{
1174             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1175           }
1176         }
1177       }else{
1178         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1179       }
1180     }
1181     return($ret);
1182   }
1185   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1186   {
1187     $xml_message= "";
1189     /* Get communication object */
1190     $d= new gosaSupportDaemon(TRUE,10);
1192     /* Prepare data */
1193     foreach ($data as $key => $value){
1194       if(is_array($value)){
1195         foreach($value as $sub_val){
1196           $xml_message.= "<$key>$sub_val</$key>";
1197         }
1198       }else{
1199         $xml_message.= "<$key>$value</$key>";
1200       }
1201     }
1203     /* Multiple targets? */
1204     if (!is_array($to)){
1205       $to_targets= array($to);
1206     } else {
1207       $to_targets= $to;
1208     }
1210     /* Build target strings */
1211     $target ="";
1212     foreach($to_targets as $to){
1213       $target.= "<target>$to</target>";
1214     }
1216     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1217   }
1220   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1221       @param  String  $mac  The mac address for which we want to remove all jobs.      
1222    */
1223   function clean_queue_from_mac($mac)
1224   {
1225     global $config;
1227     /* First of all we have to check which jobs are startet 
1228      *  for $mac 
1229      */
1230     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1231     
1232     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1233  
1234     $data = $this->_send($xml_msg,TRUE);
1235     if(is_array($data) && isset($data['XML'])){
1236       $already_aborted = FALSE;
1237       foreach($data['XML']  as $name => $entry){
1238         if(preg_match("/answer[0-9]*/i",$name)){
1239           $entry['STATUS'] = strtoupper($entry['STATUS']);
1240           switch($entry['STATUS']){
1242             case 'PROCESSING' :
1244               /* Send abort event, but only once 
1245                */
1246               if($already_aborted){
1247                 break;
1248               }elseif(class_available("DaemonEvent_faireboot")){
1249                 $already_aborted = TRUE;
1250                 $tmp = new DaemonEvent_faireboot($config);
1251                 $tmp->add_targets(array($mac));
1252                 $tmp->set_type(TRIGGERED_EVENT);
1253                 if(!$this->append($tmp)){
1254                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1255                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1256                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1257                 }else{
1258                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1259                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1260                 }
1261                 ;break;
1262               }else{
1263                 /* Couldn't find abort event, just remove entry */
1264               }
1266             case 'WAITING':
1267             case 'ERROR':
1268             default :
1269             
1270               /* Simply remove entries from queue. 
1271                *  Failed or waiting events, can be removed without any trouble.
1272                */ 
1273               if(!$this->remove_entries(array($entry['ID']))){
1274                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1275               }
1276               ;break;
1277           }
1278     
1279         }
1280       }
1281     }
1282   }
1285   static function ping($target)
1286   {
1287     if (tests::is_mac($target)){
1288       /* Get communication object */
1289       $d= new gosaSupportDaemon(TRUE,0.5);
1290       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1291       return (count($answer) ? TRUE:FALSE);
1292     }
1293     return (FALSE);
1294   }
1298   /*! \brief  Returns a list of all configured principals. 
1299               (Uses the GOsa support daemon instead of the ldap database.)
1300       @return Array  A list containing the names of all configured principals.
1301    */
1302   public function krb5_list_principals($server)
1303   {
1304     $res = array();  
1306     /* Check if the given server is a valid mac address
1307      */
1308     if(!tests::is_mac($server)){
1309       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1310       return($ret);
1311     }
1313     /* Prepare request event 
1314      */ 
1315     $xml_msg = 
1316       "<xml>".
1317       "<header>gosa_krb5_list_principals</header>".
1318       "<source>GOSA</source>".
1319       "<target>".$server."</target>".
1320       "</xml>";
1321     
1322     $tmp = $this->_send($xml_msg,TRUE);
1323     if(isset($tmp['XML']['PRINCIPAL'])){
1324       return($tmp['XML']['PRINCIPAL']);
1325     }else{
1326       return($res);
1327     }
1328   }
1331   /*! \brief  Returns the configuration settings for a given principal name. 
1332               (Uses the GOsa support daemon instead of the ldap database.)
1333       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1334       @return Array  A list containing the names of all configured principals.
1335    */
1336   public function krb5_get_principal($server,$name)
1337   {
1338     $ret = array();
1340     /* Check if the given name is a valid request value 
1341      */
1342     if(!is_string($name) || empty($name)){
1343       trigger_error("The given principal name is not of type string or it is empty.");
1344       return($ret);
1345     }
1347     /* Check if the given server is a valid mac address
1348      */
1349     if(!tests::is_mac($server)){
1350       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1351       return($ret);
1352     }
1354     /* Prepare request event 
1355      */ 
1356     $xml_msg = 
1357       "<xml>".
1358       "<header>gosa_krb5_get_principal</header>".
1359       "<principal>".$name."</principal>".
1360       "<source>GOSA</source>".
1361       "<target>".$server."</target>".
1362       "</xml>";
1364     $res = $this->_send($xml_msg,TRUE);
1365     if(isset($res['XML'])){
1366       return($res['XML']);
1367     }else{
1368       return($ret);
1369     }
1370   }
1373   /*! \brief  Creates a given principal with a set of configuration settings.
1374               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1375               (Uses the GOsa support daemon instead of the ldap database.)
1376       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1377       @return Boolean   TRUE on success else FALSE. 
1378    */
1379   public function krb5_add_principal($server,$name,$values)
1380   {
1381     $ret = FALSE;  
1383     /* Check if the given name is a valid request value 
1384      */
1385     if(!is_string($name) || empty($name)){
1386       trigger_error("The given principal name is not of type string or it is empty.");
1387       return($ret);
1388     }
1389     if(!is_array($values)){
1390       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1391       return($ret);
1392     }
1394     /* Check if the given server is a valid mac address
1395      */
1396     if(!tests::is_mac($server)){
1397       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1398       return($ret);
1399     }
1401     $attrs = "";
1402     foreach($values as $key => $value){
1403       if(empty($key) || is_numeric($key)){
1404         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1405         return($ret);
1406       }
1407       $key = strtolower($key);
1408       if(is_array($value)){
1409         foreach($value as $val){
1410           $attrs.= "<$key>$val</$key>\n";
1411         }
1412       }else{
1413         $attrs.= "<$key>$value</$key>\n";
1414       }
1415     }
1417     /* Prepare request event 
1418      */ 
1419     $xml_msg = 
1420       "<xml>".
1421       "<header>gosa_krb5_create_principal</header>".
1422       "<principal>".$name."</principal>".
1423       $attrs.
1424       "<source>GOSA</source>".
1425       "<target>".$server."</target>".
1426       "</xml>";
1428     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1429   }
1432   function krb5_ramdomize_key($server,$name)  
1433   {
1434     /* Prepare request event 
1435      */ 
1436     $xml_msg = 
1437       "<xml>".
1438       "<header>gosa_krb5_randomize_key</header>".
1439       "<principal>".$name."</principal>".
1440       "<source>GOSA</source>".
1441       "<target>".$server."</target>".
1442       "</xml>";
1444     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1445   }
1446   
1449   /*! \brief  Updates a given principal with a set of configuration settings.
1450               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1451               (Uses the GOsa support daemon instead of the ldap database.)
1452       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1453       @return Boolean   TRUE on success else FALSE. 
1454    */
1455   public function krb5_set_principal($server,$name,$values)
1456   {
1457     $ret = FALSE;  
1459     /* Check if the given name is a valid request value 
1460      */
1461     if(!is_string($name) || empty($name)){
1462       trigger_error("The given principal name is not of type string or it is empty.");
1463       return($ret);
1464     }
1465     if(!is_array($values) || !count($values)){
1466       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1467       return($ret);
1468     }
1470     /* Check if the given server is a valid mac address
1471      */
1472     if(!tests::is_mac($server)){
1473       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1474       return($ret);
1475     }
1477     $attrs = "";
1478     foreach($values as $key => $value){
1479       if(empty($key) || is_numeric($key)){
1480         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1481         return($ret);
1482       }
1483       $key = strtolower($key);
1484       if(is_array($value)){
1485         foreach($value as $val){
1486           $attrs.= "<$key>$val</$key>\n";
1487         }
1488       }else{
1489         $attrs.= "<$key>$value</$key>\n";
1490       }
1491     }
1493     /* Prepare request event 
1494      */ 
1495     $xml_msg = 
1496       "<xml>".
1497       "<header>gosa_krb5_modify_principal</header>".
1498       "<principal>".$name."</principal>".
1499       $attrs.
1500       "<source>GOSA</source>".
1501       "<target>".$server."</target>".
1502       "</xml>";
1504     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1505   }
1508   /*! \brief  Removes the given principal.
1509               (Uses the GOsa support daemon instead of the ldap database.)
1510       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1511       @return Boollean   TRUE on success else FALSE
1512    */
1513   public function krb5_del_principal($server,$name)
1514   {
1515     $ret = FALSE;  
1517     /* Check if the given name is a valid request value 
1518      */
1519     if(!is_string($name) || empty($name)){
1520       trigger_error("The given principal name is not of type string or it is empty.");
1521       return($ret);
1522     }
1524     /* Check if the given server is a valid mac address
1525      */
1526     if(!tests::is_mac($server)){
1527       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1528       return($ret);
1529     }
1531     /* Prepare request event 
1532      */ 
1533     $xml_msg = 
1534       "<xml>".
1535       "<header>gosa_krb5_del_principal</header>".
1536       "<principal>".$name."</principal>".
1537       "<source>GOSA</source>".
1538       "<target>".$server."</target>".
1539       "</xml>";
1540     
1541     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1542   }
1545   /*! \brief  Returns a list of configured password policies.
1546               (Uses the GOsa support daemon instead of the ldap database.)
1547       @return Array A list of all configured password policies.
1548    */
1549   public function krb5_list_policies($server)
1550   {
1551     $res = array();  
1553     /* Check if the given server is a valid mac address
1554      */
1555     if(!tests::is_mac($server)){
1556       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1557       return($ret);
1558     }
1560     /* Prepare request event 
1561      */ 
1562     $xml_msg = 
1563       "<xml>".
1564       "<header>gosa_krb5_list_policies</header>".
1565       "<source>GOSA</source>".
1566       "<target>".$server."</target>".
1567       "</xml>";
1568     
1569     $res = $this->_send($xml_msg,TRUE);
1570     
1571     /* Check if there are results for POLICY 
1572      */
1573     if(isset($res['XML']['POLICY'])){
1574       
1575       /* Ensure that we return an array 
1576        */
1577       $tmp = $res['XML']['POLICY'];
1578       if(!is_array($tmp)){
1579         $tmp = array($tmp);
1580       }
1581       return($tmp);
1582     }else{
1583       return(array());
1584     }
1585   }
1588   /*! \brief  Returns a list of configured password policies.
1589               (Uses the GOsa support daemon instead of the ldap database.)
1590       @return Array The policy settings for the given policy name.
1591    */
1592   public function krb5_get_policy($server,$name)
1593   {
1594     $res = array();  
1596     /* Check if the given name is a valid request value 
1597      */
1598     if(!is_string($name) || empty($name)){
1599       trigger_error("The given policy name is not of type string or it is empty.");
1600       return($ret);
1601     }
1603     /* Check if the given server is a valid mac address
1604      */
1605     if(!tests::is_mac($server)){
1606       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1607       return($ret);
1608     }
1610     /* Prepare request event 
1611      */ 
1612     $xml_msg = 
1613       "<xml>".
1614       "<header>gosa_krb5_get_policy</header>".
1615       "<policy>".$name."</policy>".
1616       "<source>GOSA</source>".
1617       "<target>".$server."</target>".
1618       "</xml>";
1620     /* Possible attributes */
1621     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1622         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1624   
1625     $tmp = $this->_send($xml_msg,TRUE);
1626     if(isset($tmp['XML'])){
1627       foreach($attrs as $attr){
1628         if(isset($tmp['XML'][$attr])){
1629           $ret[$attr] = $tmp['XML'][$attr];
1630         }else{
1631           $ret[$attr] = "";
1632         }
1633       }
1634     }
1635     return($ret);
1636   }
1638   
1639   /*! \brief  Creates a new policy with a given set of configuration settings.
1640               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1641               (Uses the GOsa support daemon instead of the ldap database.)
1642       @pram   String The name of the policy to update.
1643       @pram   Array  The attributes to update
1644       @return Boolean   TRUE on success else FALSE. 
1645    */
1646   public function krb5_add_policy($server,$name,$values)
1647   {
1648     $ret = FALSE;  
1650     /* Check if the given name is a valid request value 
1651      */
1652     if(!is_string($name) || empty($name)){
1653       trigger_error("The given policy name is not of type string or it is empty.");
1654       return($ret);
1655     }
1656     if(!is_array($values) || !count($values)){
1657       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1658       return($ret);
1659     }
1661     /* Check if the given server is a valid mac address
1662      */
1663     if(!tests::is_mac($server)){
1664       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1665       return($ret);
1666     }
1669     /* Transform array into <xml>
1670      */
1671     $attrs = "";
1672     foreach($values as $id => $value){
1673       if(empty($id) || is_numeric($id)){
1674         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1675         return($ret);
1676       }
1677       $id = strtolower($id);
1678       $attrs.= "<$id>$value</$id>\n";
1679     }
1681     /* Prepare request event 
1682      */ 
1683     $xml_msg = 
1684       "<xml>".
1685       "<header>gosa_krb5_create_policy</header>".
1686       "<policy>".$name."</policy>".
1687       $attrs.
1688       "<source>GOSA</source>".
1689       "<target>".$server."</target>".
1690       "</xml>";
1692     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1693   }
1696   /*! \brief  Updates a given policy with a set of configuration settings.
1697               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1698               (Uses the GOsa support daemon instead of the ldap database.)
1699       @pram   String The name of the policy to update.
1700       @return Boolean   TRUE on success else FALSE. 
1701    */
1702   public function krb5_set_policy($server,$name,$values)
1703   {
1704     $ret = FALSE;  
1706     /* Check if the given name is a valid request value 
1707      */
1708     if(!is_string($name) || empty($name)){
1709       trigger_error("The given policy name is not of type string or it is empty.");
1710       return($ret);
1711     }
1712     if(!is_array($values) || !count($values)){
1713       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1714       return($ret);
1715     }
1717     /* Check if the given server is a valid mac address
1718      */
1719     if(!tests::is_mac($server)){
1720       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1721       return($ret);
1722     }
1724     /* Transform array into <xml>
1725      */
1726     $attrs = "";
1727     foreach($values as $id => $value){
1728       if(preg_match("/^policy$/i",$id)) continue;
1729       if(empty($id) || is_numeric($id)){
1730         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1731         return($ret);
1732       }
1733       $id = strtolower($id);
1734       $attrs.= "<$id>$value</$id>\n";
1735     }
1737     /* Prepare request event 
1738      */ 
1739     $xml_msg = 
1740       "<xml>".
1741       "<header>gosa_krb5_modify_policy</header>".
1742       "<policy>".$name."</policy>".
1743       $attrs.
1744       "<source>GOSA</source>".
1745       "<target>".$server."</target>".
1746       "</xml>";
1748     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1749   }
1751   
1752   /*! \brief  Removes the given password policy. 
1753               (Uses the GOsa support daemon instead of the ldap database.)
1754       @return Boolean  TRUE on success else FALSE
1755    */
1756   public function krb5_del_policy($server,$name)
1757   {
1758     $ret = FALSE;
1760     /* Check if the given server is a valid mac address
1761      */
1762     if(!tests::is_mac($server)){
1763       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1764       return($ret);
1765     }
1767     /* Check if the given name is a valid request value 
1768      */
1769     if(!is_string($name) || empty($name)){
1770       trigger_error("The given policy name is not of type string or it is empty.");
1771       return($ret);
1772     }
1774     /* Prepare request event 
1775      */ 
1776     $xml_msg = 
1777       "<xml>".
1778       "<header>gosa_krb5_del_policy</header>".
1779       "<policy>".$name."</policy>".
1780       "<source>GOSA</source>".
1781       "<target>".$server."</target>".
1782       "</xml>";
1783     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1784   }
1787   /*! \brief  Sets the password of for the given principal.
1788               (Uses the GOsa support daemon instead of the ldap database.)
1789       @param  String  The servers mac
1790       @param  String  The principals name
1791       @param  String  $the new password.   
1792       @return Boolean  TRUE on success else FALSE
1793    */
1794   public function krb5_set_password($server,$name,$password)
1795   {
1796     $ret = FALSE;
1798     /* Check if the given server is a valid mac address
1799      */
1800     if(!tests::is_mac($server)){
1801       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1802       return($ret);
1803     }
1805     /* Check if the given name is a valid request value
1806      */
1807     if(!is_string($name) || empty($name)){
1808       trigger_error("The given principal name is not of type string or it is empty.");
1809       return($ret);
1810     }
1812     /* Prepare request event
1813      */
1814     $xml_msg =
1815       "<xml>".
1816       "<header>gosa_krb5_set_password</header>".
1817       "<principal>".$name."</principal>".
1818       "<password>".$password."</password>".
1819       "<source>GOSA</source>".
1820       "<target>".$server."</target>".
1821       "</xml>";
1822     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1823   }
1826   /*! \brief  Returns log file informations for a given mac address 
1827       @param  $mac The mac address to fetch logs for.
1828       @retrun Array A Multidimensional array containing log infos.
1829         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1830         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1831                                install_20080313_144450   ...
1832    */
1833   public function get_log_info_for_mac($mac)
1834   {
1835     $xml_msg = "
1836       <xml>
1837       <header>gosa_show_log_by_mac</header>
1838       <target>GOSA</target>
1839       <source>GOSA</source>
1840       <mac>".$mac."</mac>
1841       </xml>";
1843     $res = $this->_send($xml_msg,TRUE);
1844     $ret = array();
1845     if(isset($res['XML'])){
1847       /* Filter all entry that look like this 
1848           MAC_00_01_6C_9D_B9_FA
1849        */
1850       foreach($res['XML'] as $name => $entry){
1851         if(preg_match("/^MAC/",$name)){
1853           /* Get list of available log files 
1854            */
1855           foreach($entry as $log_date){
1856             $xml_msg2 = "<xml> 
1857               <header>gosa_show_log_files_by_date_and_mac</header> 
1858               <target>GOSA</target> 
1859               <source>GOSA</source>                        
1860               <date>".$log_date."</date> 
1861               <mac>".$mac."</mac> 
1862               </xml>";
1864             $ret[$mac][$log_date] = array();
1865             $res = $this->_send($xml_msg2,TRUE);
1866             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1867             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1868             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1869               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1870             }
1871           }
1872         }
1873       }
1874     }
1875     return($ret);
1876   }
1878   public function get_log_file($mac,$date,$file)
1879   {
1880     $xml_msg ="
1881       <xml> 
1882       <header>gosa_get_log_file_by_date_and_mac</header> 
1883       <target>GOSA</target> 
1884       <source>GOSA</source>
1885       <date>".$date."</date> 
1886       <mac>".$mac."</mac> 
1887       <log_file>".$file."</log_file>
1888       </xml>";
1890     $res = $this->_send($xml_msg,TRUE);
1891     if(isset($res['XML'][strtoupper($file)])){
1892       return(base64_decode($res['XML'][strtoupper($file)]));
1893     }
1894     return("");
1895   }
1901   /*****************
1902    * DAK - Functions 
1903    *****************/
1905   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1906       @param  ...
1907       @return Array   All queued entries.
1908    */
1909   public function DAK_keyring_entries($server)  
1910   {
1911     /* Ensure that we send the event to a valid mac address 
1912      */
1913     if(!is_string($server) || !tests::is_mac($server)){
1914       trigger_error("No valid mac address given '".$server."'.");
1915       return;
1916     }
1918     /* Create query
1919      */
1920     $xml_msg = "<xml> 
1921                   <header>gosa_get_dak_keyring</header> 
1922                   <target>".$server."</target> 
1923                   <source>GOSA</source>
1924                 </xml>";
1925         
1926     $res = $this->_send($xml_msg,TRUE);
1928     /* Check if there are results for POLICY
1929      */
1930     if(isset($res['XML'])){
1931       $ret = array();
1932       foreach($res['XML'] as $key => $entry){
1933         if(preg_match("/^ANSWER/",$key)){
1934           $ret[] = $entry;
1935         }
1936       }
1937       return($ret);
1938     }else{
1939       return(array());
1940     }
1941   }
1944   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1945       @param  String  The servers mac address 
1946       @param  String  The gpg key.
1947       @return Boolean TRUE on success else FALSE 
1948    */
1949   public function DAK_import_key($server,$key)  
1950   {
1951     /* Ensure that we send the event to a valid mac address 
1952      */
1953     if(!is_string($server) || !tests::is_mac($server)){
1954       trigger_error("No valid mac address given '".$server."'.");
1955       return;
1956     }
1958     /* Check if there is some cleanup required before importing the key.
1959         There may be some Header lines like:
1960         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1961      */
1962     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1964       /* Remove header */
1965       $key = preg_replace("/^.*\n\n/sim","",$key);
1966       /* Remove footer */
1967       $key = preg_replace("/-----.*$/sim","",$key);
1968     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1969       
1970       /* Encode key if it is raw.
1971        */
1972       $key = base64_encode($key);
1973     }
1975     /* Create query
1976      */
1977     $xml_msg = "<xml> 
1978                   <header>gosa_import_dak_key</header> 
1979                   <target>".$server."</target> 
1980                   <key>".$key."</key> 
1981                   <source>GOSA</source>
1982                 </xml>";
1983         
1984     $res = $this->_send($xml_msg,TRUE);
1985     return($this->is_error());
1986   }
1989   /*! \brief Removes a key from the keyring on the given server. 
1990       @param  String  The servers mac address 
1991       @param  String  The gpg key uid.
1992       @return Boolean TRUE on success else FALSE 
1993    */
1994   public function DAK_remove_key($server,$key)  
1995   {
1996     /* Ensure that we send the event to a valid mac address 
1997      */
1998     if(!is_string($server) || !tests::is_mac($server)){
1999       trigger_error("No valid mac address given '".$server."'.");
2000       return;
2001     }
2003     /* Create query
2004      */
2005     $xml_msg = "<xml> 
2006                   <header>gosa_remove_dak_key</header> 
2007                   <target>".$server."</target> 
2008                   <keyid>".$key."</keyid> 
2009                   <source>GOSA</source>
2010                 </xml>";
2011        
2012     $res = $this->_send($xml_msg,TRUE);
2013     return($this->is_error());
2014   }
2017 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
2018 ?>