Code

Moved from schema_check to schemaCheck
[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($this->use_alternative_xml_parse_method) {
1168             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1169               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1170               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1171                   array($data=>$data),"FAILED ".$this->get_error());
1172             }
1173           }else{
1174             if(isset($entries['XML']['ERROR_STRING'])) {
1175               $this->set_error($entries['XML']['ERROR_STRING']);
1176               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1177                   array($data=>$data),"FAILED ".$this->get_error());
1178             }elseif(isset($entries['XML']['ERROR'])){
1179               $this->set_error($entries['XML']['ERROR']);
1180               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1181                   array($data=>$data),"FAILED ".$this->get_error());
1182             }
1183           }
1184           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1185               array($data=>$data),"SUCCESS");
1186         }
1187       }else{
1188         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1189             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1190       }
1191     }
1192     return($ret);
1193   }
1196   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1197   {
1198     $xml_message= "";
1200     /* Get communication object */
1201     $d= new gosaSupportDaemon(TRUE,10);
1203     /* Prepare data */
1204     foreach ($data as $key => $value){
1205       if(is_array($value)){
1206         foreach($value as $sub_val){
1207           $xml_message.= "<$key>$sub_val</$key>";
1208         }
1209       }else{
1210         $xml_message.= "<$key>$value</$key>";
1211       }
1212     }
1214     /* Multiple targets? */
1215     if (!is_array($to)){
1216       $to_targets= array($to);
1217     } else {
1218       $to_targets= $to;
1219     }
1221     /* Build target strings */
1222     $target ="";
1223     foreach($to_targets as $to){
1224       $target.= "<target>$to</target>";
1225     }
1227     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1228   }
1231   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1232       @param  String  $mac  The mac address for which we want to remove all jobs.      
1233    */
1234   function clean_queue_from_mac($mac)
1235   {
1236     global $config;
1238     /* First of all we have to check which jobs are startet 
1239      *  for $mac 
1240      */
1241     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1242     
1243     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1244  
1245     $data = $this->_send($xml_msg,TRUE);
1246     if(is_array($data) && isset($data['XML'])){
1247       $already_aborted = FALSE;
1248       foreach($data['XML']  as $name => $entry){
1249         if(preg_match("/answer[0-9]*/i",$name)){
1250           $entry['STATUS'] = strtoupper($entry['STATUS']);
1251           switch($entry['STATUS']){
1253             case 'PROCESSING' :
1255               /* Send abort event, but only once 
1256                */
1257               if($already_aborted){
1258                 break;
1259               }elseif(class_available("DaemonEvent_faireboot")){
1260                 $already_aborted = TRUE;
1261                 $tmp = new DaemonEvent_faireboot($config);
1262                 $tmp->add_targets(array($mac));
1263                 $tmp->set_type(TRIGGERED_EVENT);
1264                 if(!$this->append($tmp)){
1265                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1266                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1267                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1268                 }else{
1269                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1270                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1271                 }
1272                 ;break;
1273               }else{
1274                 /* Couldn't find abort event, just remove entry */
1275               }
1277             case 'WAITING':
1278             case 'ERROR':
1279             default :
1280             
1281               /* Simply remove entries from queue. 
1282                *  Failed or waiting events, can be removed without any trouble.
1283                */ 
1284               if(!$this->remove_entries(array($entry['ID']))){
1285                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1286               }
1287               ;break;
1288           }
1289     
1290         }
1291       }
1292     }
1293   }
1296   static function ping($target)
1297   {
1298     if (tests::is_mac($target)){
1299       /* Get communication object */
1300       $d= new gosaSupportDaemon(TRUE,0.5);
1301       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1302       return (count($answer) ? TRUE:FALSE);
1303     }
1304     return (FALSE);
1305   }
1309   /*! \brief  Returns a list of all configured principals. 
1310               (Uses the GOsa support daemon instead of the ldap database.)
1311       @return Array  A list containing the names of all configured principals.
1312    */
1313   public function krb5_list_principals($server)
1314   {
1315     $res = array();  
1317     /* Check if the given server is a valid mac address
1318      */
1319     if(!tests::is_mac($server)){
1320       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1321       return($ret);
1322     }
1324     /* Prepare request event 
1325      */ 
1326     $xml_msg = 
1327       "<xml>".
1328       "<header>gosa_krb5_list_principals</header>".
1329       "<source>GOSA</source>".
1330       "<target>".$server."</target>".
1331       "</xml>";
1332     
1333     $tmp = $this->_send($xml_msg,TRUE);
1334     if(isset($tmp['XML']['PRINCIPAL'])){
1335       return($tmp['XML']['PRINCIPAL']);
1336     }else{
1337       return($res);
1338     }
1339   }
1342   /*! \brief  Returns the configuration settings for a given principal name. 
1343               (Uses the GOsa support daemon instead of the ldap database.)
1344       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1345       @return Array  A list containing the names of all configured principals.
1346    */
1347   public function krb5_get_principal($server,$name)
1348   {
1349     $ret = array();
1351     /* Check if the given name is a valid request value 
1352      */
1353     if(!is_string($name) || empty($name)){
1354       trigger_error("The given principal name is not of type string or it is empty.");
1355       return($ret);
1356     }
1358     /* Check if the given server is a valid mac address
1359      */
1360     if(!tests::is_mac($server)){
1361       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1362       return($ret);
1363     }
1365     /* Prepare request event 
1366      */ 
1367     $xml_msg = 
1368       "<xml>".
1369       "<header>gosa_krb5_get_principal</header>".
1370       "<principal>".$name."</principal>".
1371       "<source>GOSA</source>".
1372       "<target>".$server."</target>".
1373       "</xml>";
1375     $res = $this->_send($xml_msg,TRUE);
1376     if(isset($res['XML'])){
1377       return($res['XML']);
1378     }else{
1379       return($ret);
1380     }
1381   }
1384   /*! \brief  Creates a given principal with a set of configuration settings.
1385               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1386               (Uses the GOsa support daemon instead of the ldap database.)
1387       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1388       @return Boolean   TRUE on success else FALSE. 
1389    */
1390   public function krb5_add_principal($server,$name,$values)
1391   {
1392     $ret = FALSE;  
1394     /* Check if the given name is a valid request value 
1395      */
1396     if(!is_string($name) || empty($name)){
1397       trigger_error("The given principal name is not of type string or it is empty.");
1398       return($ret);
1399     }
1400     if(!is_array($values)){
1401       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1402       return($ret);
1403     }
1405     /* Check if the given server is a valid mac address
1406      */
1407     if(!tests::is_mac($server)){
1408       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1409       return($ret);
1410     }
1412     $attrs = "";
1413     foreach($values as $key => $value){
1414       if(empty($key) || is_numeric($key)){
1415         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1416         return($ret);
1417       }
1418       $key = strtolower($key);
1419       if(is_array($value)){
1420         foreach($value as $val){
1421           $attrs.= "<$key>$val</$key>\n";
1422         }
1423       }else{
1424         $attrs.= "<$key>$value</$key>\n";
1425       }
1426     }
1428     /* Prepare request event 
1429      */ 
1430     $xml_msg = 
1431       "<xml>".
1432       "<header>gosa_krb5_create_principal</header>".
1433       "<principal>".$name."</principal>".
1434       $attrs.
1435       "<source>GOSA</source>".
1436       "<target>".$server."</target>".
1437       "</xml>";
1439     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1440   }
1443   function krb5_ramdomize_key($server,$name)  
1444   {
1445     /* Prepare request event 
1446      */ 
1447     $xml_msg = 
1448       "<xml>".
1449       "<header>gosa_krb5_randomize_key</header>".
1450       "<principal>".$name."</principal>".
1451       "<source>GOSA</source>".
1452       "<target>".$server."</target>".
1453       "</xml>";
1455     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1456   }
1457   
1460   /*! \brief  Updates a given principal with a set of configuration settings.
1461               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1462               (Uses the GOsa support daemon instead of the ldap database.)
1463       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1464       @return Boolean   TRUE on success else FALSE. 
1465    */
1466   public function krb5_set_principal($server,$name,$values)
1467   {
1468     $ret = FALSE;  
1470     /* Check if the given name is a valid request value 
1471      */
1472     if(!is_string($name) || empty($name)){
1473       trigger_error("The given principal name is not of type string or it is empty.");
1474       return($ret);
1475     }
1476     if(!is_array($values) || !count($values)){
1477       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1478       return($ret);
1479     }
1481     /* Check if the given server is a valid mac address
1482      */
1483     if(!tests::is_mac($server)){
1484       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1485       return($ret);
1486     }
1488     $attrs = "";
1489     foreach($values as $key => $value){
1490       if(empty($key) || is_numeric($key)){
1491         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1492         return($ret);
1493       }
1494       $key = strtolower($key);
1495       if(is_array($value)){
1496         foreach($value as $val){
1497           $attrs.= "<$key>$val</$key>\n";
1498         }
1499       }else{
1500         $attrs.= "<$key>$value</$key>\n";
1501       }
1502     }
1504     /* Prepare request event 
1505      */ 
1506     $xml_msg = 
1507       "<xml>".
1508       "<header>gosa_krb5_modify_principal</header>".
1509       "<principal>".$name."</principal>".
1510       $attrs.
1511       "<source>GOSA</source>".
1512       "<target>".$server."</target>".
1513       "</xml>";
1515     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1516   }
1519   /*! \brief  Removes the given principal.
1520               (Uses the GOsa support daemon instead of the ldap database.)
1521       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1522       @return Boollean   TRUE on success else FALSE
1523    */
1524   public function krb5_del_principal($server,$name)
1525   {
1526     $ret = FALSE;  
1528     /* Check if the given name is a valid request value 
1529      */
1530     if(!is_string($name) || empty($name)){
1531       trigger_error("The given principal name is not of type string or it is empty.");
1532       return($ret);
1533     }
1535     /* Check if the given server is a valid mac address
1536      */
1537     if(!tests::is_mac($server)){
1538       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1539       return($ret);
1540     }
1542     /* Prepare request event 
1543      */ 
1544     $xml_msg = 
1545       "<xml>".
1546       "<header>gosa_krb5_del_principal</header>".
1547       "<principal>".$name."</principal>".
1548       "<source>GOSA</source>".
1549       "<target>".$server."</target>".
1550       "</xml>";
1551     
1552     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1553   }
1556   /*! \brief  Returns a list of configured password policies.
1557               (Uses the GOsa support daemon instead of the ldap database.)
1558       @return Array A list of all configured password policies.
1559    */
1560   public function krb5_list_policies($server)
1561   {
1562     $res = array();  
1564     /* Check if the given server is a valid mac address
1565      */
1566     if(!tests::is_mac($server)){
1567       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1568       return($ret);
1569     }
1571     /* Prepare request event 
1572      */ 
1573     $xml_msg = 
1574       "<xml>".
1575       "<header>gosa_krb5_list_policies</header>".
1576       "<source>GOSA</source>".
1577       "<target>".$server."</target>".
1578       "</xml>";
1579     
1580     $res = $this->_send($xml_msg,TRUE);
1581     
1582     /* Check if there are results for POLICY 
1583      */
1584     if(isset($res['XML']['POLICY'])){
1585       
1586       /* Ensure that we return an array 
1587        */
1588       $tmp = $res['XML']['POLICY'];
1589       if(!is_array($tmp)){
1590         $tmp = array($tmp);
1591       }
1592       return($tmp);
1593     }else{
1594       return(array());
1595     }
1596   }
1599   /*! \brief  Returns a list of configured password policies.
1600               (Uses the GOsa support daemon instead of the ldap database.)
1601       @return Array The policy settings for the given policy name.
1602    */
1603   public function krb5_get_policy($server,$name)
1604   {
1605     $res = array();  
1607     /* Check if the given name is a valid request value 
1608      */
1609     if(!is_string($name) || empty($name)){
1610       trigger_error("The given policy name is not of type string or it is empty.");
1611       return($ret);
1612     }
1614     /* Check if the given server is a valid mac address
1615      */
1616     if(!tests::is_mac($server)){
1617       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1618       return($ret);
1619     }
1621     /* Prepare request event 
1622      */ 
1623     $xml_msg = 
1624       "<xml>".
1625       "<header>gosa_krb5_get_policy</header>".
1626       "<policy>".$name."</policy>".
1627       "<source>GOSA</source>".
1628       "<target>".$server."</target>".
1629       "</xml>";
1631     /* Possible attributes */
1632     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1633         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1635   
1636     $tmp = $this->_send($xml_msg,TRUE);
1637     if(isset($tmp['XML'])){
1638       foreach($attrs as $attr){
1639         if(isset($tmp['XML'][$attr])){
1640           $ret[$attr] = $tmp['XML'][$attr];
1641         }else{
1642           $ret[$attr] = "";
1643         }
1644       }
1645     }
1646     return($ret);
1647   }
1649   
1650   /*! \brief  Creates a new policy with a given set of configuration settings.
1651               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1652               (Uses the GOsa support daemon instead of the ldap database.)
1653       @pram   String The name of the policy to update.
1654       @pram   Array  The attributes to update
1655       @return Boolean   TRUE on success else FALSE. 
1656    */
1657   public function krb5_add_policy($server,$name,$values)
1658   {
1659     $ret = FALSE;  
1661     /* Check if the given name is a valid request value 
1662      */
1663     if(!is_string($name) || empty($name)){
1664       trigger_error("The given policy name is not of type string or it is empty.");
1665       return($ret);
1666     }
1667     if(!is_array($values) || !count($values)){
1668       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1669       return($ret);
1670     }
1672     /* Check if the given server is a valid mac address
1673      */
1674     if(!tests::is_mac($server)){
1675       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1676       return($ret);
1677     }
1680     /* Transform array into <xml>
1681      */
1682     $attrs = "";
1683     foreach($values as $id => $value){
1684       if(empty($id) || is_numeric($id)){
1685         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1686         return($ret);
1687       }
1688       $id = strtolower($id);
1689       $attrs.= "<$id>$value</$id>\n";
1690     }
1692     /* Prepare request event 
1693      */ 
1694     $xml_msg = 
1695       "<xml>".
1696       "<header>gosa_krb5_create_policy</header>".
1697       "<policy>".$name."</policy>".
1698       $attrs.
1699       "<source>GOSA</source>".
1700       "<target>".$server."</target>".
1701       "</xml>";
1703     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1704   }
1707   /*! \brief  Updates a given policy with a set of configuration settings.
1708               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1709               (Uses the GOsa support daemon instead of the ldap database.)
1710       @pram   String The name of the policy to update.
1711       @return Boolean   TRUE on success else FALSE. 
1712    */
1713   public function krb5_set_policy($server,$name,$values)
1714   {
1715     $ret = FALSE;  
1717     /* Check if the given name is a valid request value 
1718      */
1719     if(!is_string($name) || empty($name)){
1720       trigger_error("The given policy name is not of type string or it is empty.");
1721       return($ret);
1722     }
1723     if(!is_array($values) || !count($values)){
1724       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1725       return($ret);
1726     }
1728     /* Check if the given server is a valid mac address
1729      */
1730     if(!tests::is_mac($server)){
1731       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1732       return($ret);
1733     }
1735     /* Transform array into <xml>
1736      */
1737     $attrs = "";
1738     foreach($values as $id => $value){
1739       if(preg_match("/^policy$/i",$id)) continue;
1740       if(empty($id) || is_numeric($id)){
1741         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1742         return($ret);
1743       }
1744       $id = strtolower($id);
1745       $attrs.= "<$id>$value</$id>\n";
1746     }
1748     /* Prepare request event 
1749      */ 
1750     $xml_msg = 
1751       "<xml>".
1752       "<header>gosa_krb5_modify_policy</header>".
1753       "<policy>".$name."</policy>".
1754       $attrs.
1755       "<source>GOSA</source>".
1756       "<target>".$server."</target>".
1757       "</xml>";
1759     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1760   }
1762   
1763   /*! \brief  Removes the given password policy. 
1764               (Uses the GOsa support daemon instead of the ldap database.)
1765       @return Boolean  TRUE on success else FALSE
1766    */
1767   public function krb5_del_policy($server,$name)
1768   {
1769     $ret = FALSE;
1771     /* Check if the given server is a valid mac address
1772      */
1773     if(!tests::is_mac($server)){
1774       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1775       return($ret);
1776     }
1778     /* Check if the given name is a valid request value 
1779      */
1780     if(!is_string($name) || empty($name)){
1781       trigger_error("The given policy name is not of type string or it is empty.");
1782       return($ret);
1783     }
1785     /* Prepare request event 
1786      */ 
1787     $xml_msg = 
1788       "<xml>".
1789       "<header>gosa_krb5_del_policy</header>".
1790       "<policy>".$name."</policy>".
1791       "<source>GOSA</source>".
1792       "<target>".$server."</target>".
1793       "</xml>";
1794     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1795   }
1798   /*! \brief  Sets the password of for the given principal.
1799               (Uses the GOsa support daemon instead of the ldap database.)
1800       @param  String  The servers mac
1801       @param  String  The principals name
1802       @param  String  $the new password.   
1803       @return Boolean  TRUE on success else FALSE
1804    */
1805   public function krb5_set_password($server,$name,$password)
1806   {
1807     $ret = FALSE;
1809     /* Check if the given server is a valid mac address
1810      */
1811     if(!tests::is_mac($server)){
1812       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1813       return($ret);
1814     }
1816     /* Check if the given name is a valid request value
1817      */
1818     if(!is_string($name) || empty($name)){
1819       trigger_error("The given principal name is not of type string or it is empty.");
1820       return($ret);
1821     }
1823     /* Prepare request event
1824      */
1825     $xml_msg =
1826       "<xml>".
1827       "<header>gosa_krb5_set_password</header>".
1828       "<principal>".$name."</principal>".
1829       "<password>".$password."</password>".
1830       "<source>GOSA</source>".
1831       "<target>".$server."</target>".
1832       "</xml>";
1833     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1834   }
1837   /*! \brief  Returns log file informations for a given mac address 
1838       @param  $mac The mac address to fetch logs for.
1839       @retrun Array A Multidimensional array containing log infos.
1840         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1841         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1842                                install_20080313_144450   ...
1843    */
1844   public function get_log_info_for_mac($mac)
1845   {
1846     $xml_msg = "
1847       <xml>
1848       <header>gosa_show_log_by_mac</header>
1849       <target>GOSA</target>
1850       <source>GOSA</source>
1851       <mac>".$mac."</mac>
1852       </xml>";
1854     $res = $this->_send($xml_msg,TRUE);
1855     $ret = array();
1856     if(isset($res['XML'])){
1858       /* Filter all entry that look like this 
1859           MAC_00_01_6C_9D_B9_FA
1860        */
1861       foreach($res['XML'] as $name => $entry){
1862         if(preg_match("/^MAC/",$name)){
1864           /* Get list of available log files 
1865            */
1866           foreach($entry as $log_date){
1867             $xml_msg2 = "<xml> 
1868               <header>gosa_show_log_files_by_date_and_mac</header> 
1869               <target>GOSA</target> 
1870               <source>GOSA</source>                        
1871               <date>".$log_date."</date> 
1872               <mac>".$mac."</mac> 
1873               </xml>";
1875             $ret[$mac][$log_date] = array();
1876             $res = $this->_send($xml_msg2,TRUE);
1877             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1878             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1879             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1880               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1881             }
1882           }
1883         }
1884       }
1885     }
1886     return($ret);
1887   }
1889   public function get_log_file($mac,$date,$file)
1890   {
1891     $xml_msg ="
1892       <xml> 
1893       <header>gosa_get_log_file_by_date_and_mac</header> 
1894       <target>GOSA</target> 
1895       <source>GOSA</source>
1896       <date>".$date."</date> 
1897       <mac>".$mac."</mac> 
1898       <log_file>".$file."</log_file>
1899       </xml>";
1901     $res = $this->_send($xml_msg,TRUE);
1902     if(isset($res['XML'][strtoupper($file)])){
1903       return(base64_decode($res['XML'][strtoupper($file)]));
1904     }
1905     return("");
1906   }
1912   /*****************
1913    * DAK - Functions 
1914    *****************/
1916   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1917       @param  ...
1918       @return Array   All queued entries.
1919    */
1920   public function DAK_keyring_entries($server)  
1921   {
1922     /* Ensure that we send the event to a valid mac address 
1923      */
1924     if(!is_string($server) || !tests::is_mac($server)){
1925       trigger_error("No valid mac address given '".$server."'.");
1926       return;
1927     }
1929     /* Create query
1930      */
1931     $xml_msg = "<xml> 
1932                   <header>gosa_get_dak_keyring</header> 
1933                   <target>".$server."</target> 
1934                   <source>GOSA</source>
1935                 </xml>";
1936         
1937     $res = $this->_send($xml_msg,TRUE);
1939     /* Check if there are results for POLICY
1940      */
1941     if(isset($res['XML'])){
1942       $ret = array();
1943       foreach($res['XML'] as $key => $entry){
1944         if(preg_match("/^ANSWER/",$key)){
1945           $ret[] = $entry;
1946         }
1947       }
1948       return($ret);
1949     }else{
1950       return(array());
1951     }
1952   }
1955   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1956       @param  String  The servers mac address 
1957       @param  String  The gpg key.
1958       @return Boolean TRUE on success else FALSE 
1959    */
1960   public function DAK_import_key($server,$key)  
1961   {
1962     /* Ensure that we send the event to a valid mac address 
1963      */
1964     if(!is_string($server) || !tests::is_mac($server)){
1965       trigger_error("No valid mac address given '".$server."'.");
1966       return;
1967     }
1969     /* Check if there is some cleanup required before importing the key.
1970         There may be some Header lines like:
1971         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1972      */
1973     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1975       /* Remove header */
1976       $key = preg_replace("/^.*\n\n/sim","",$key);
1977       /* Remove footer */
1978       $key = preg_replace("/-----.*$/sim","",$key);
1979     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1980       
1981       /* Encode key if it is raw.
1982        */
1983       $key = base64_encode($key);
1984     }
1986     /* Create query
1987      */
1988     $xml_msg = "<xml> 
1989                   <header>gosa_import_dak_key</header> 
1990                   <target>".$server."</target> 
1991                   <key>".$key."</key> 
1992                   <source>GOSA</source>
1993                 </xml>";
1994         
1995     $res = $this->_send($xml_msg,TRUE);
1996     return($this->is_error());
1997   }
2000   /*! \brief Removes a key from the keyring on the given server. 
2001       @param  String  The servers mac address 
2002       @param  String  The gpg key uid.
2003       @return Boolean TRUE on success else FALSE 
2004    */
2005   public function DAK_remove_key($server,$key)  
2006   {
2007     /* Ensure that we send the event to a valid mac address 
2008      */
2009     if(!is_string($server) || !tests::is_mac($server)){
2010       trigger_error("No valid mac address given '".$server."'.");
2011       return;
2012     }
2014     /* Create query
2015      */
2016     $xml_msg = "<xml> 
2017                   <header>gosa_remove_dak_key</header> 
2018                   <target>".$server."</target> 
2019                   <keyid>".$key."</keyid> 
2020                   <source>GOSA</source>
2021                 </xml>";
2022        
2023     $res = $this->_send($xml_msg,TRUE);
2024     return($this->is_error());
2025   }
2028 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
2029 ?>