Code

Make package filters for sections work (#6576)
[gosa.git] / trunk / 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;
35   private $is_configured  = FALSE;
36   protected $use_alternative_xml_parse_method = FALSE;
38   public function get_host()
39   {
40     return($this->s_host);
41   }
43   public function get_port()
44   {
45     return($this->i_port);
46   }
48   /*! \brief  Creates a new gosaSupportDaemon object.
49     @param string   Host    The Host where the daemon is running on.  
50     @param integer  Port    The port which the daemon use.
51     @param string   Key     The encryption string.
52     @param boolean  Connect Directly connect to daemon socket.
53     @param float    Timeout The timelimit for all socket actions.
54    */
55   public function __construct($connect=TRUE,$timeout=15)
56   {
57     #FIXME: bad idea about referencing global variables from within classes
58     global $config;
60     /* This should only be the case if we call this from setup.
61         __autoload() 
62      */
63     if(!is_object($config)) { return; }
65     # load from config, store statically
66     if ($config->get_cfg_value("gosaSupportURI") != ""){
68       if ($this->s_host == ""){
69         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
70         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosaSupportURI"));
71         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
72       }
73       $this->is_configured = TRUE;
74       $this->f_timeout = $timeout;
75       if($connect){
76         $this->connect();
77       }
78     }
79   }
82   public function is_configured()
83   {
84     return($this->is_configured);
85   }
88   /*! \brief  Establish daemon connection. 
89     @return boolean Returns true if the connection was succesfully established. 
90    */
91   public function connect()
92   {
93     if(!empty($this->s_host) && !empty($this->i_port)){
94       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
95       if($this->o_sock->connected()){ 
96         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
97         $this->is_connected = TRUE;
98       }else{
99         $this->set_error($this->o_sock->get_error());
100         $this->disconnect();
101         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
102       }
103     }else{
104       $this->set_error(msgPool::cmdnotfound("gosaSupportURI",_("GOsa support daemon")));
105     }
106     return($this->is_connected);
107   }
109   
110   /*! \brief  Returns TRUE whether we are connected or not 
111       @return BOOLEAN  Returns TRUE when connected else FALSE
112    */
113   public function is_connected()
114   {
115     return($this->is_connected);
116   }
117  
119   /*! \brief  */
120   public function get_hosts_with_module($mod)
121   {
122     $data = array("module_name" => $mod);
123     $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
124     $hosts = array();
125     if(isset($res['XML'][0])){
126       foreach($res['XML'][0] as $name => $data){
127         if(preg_match("/^ANSWER[0-9]*$/",$name)){
128           if(isset($data[0]['MAC'][0]['VALUE']) && $data[0]['MAC'][0]['VALUE'] != ""){
129             $hosts[] = $data[0]['MAC'][0]['VALUE'];
130           } elseif(isset($data[0]['IP'][0]['VALUE']) && $data[0]['IP'][0]['VALUE'] != "") {
131             $hosts[] = $data[0]['IP'][0]['VALUE'];
132           }
133         }
134       }
135     }
137     if(count($hosts) == 0){
138       @DEBUG(DEBUG_SI, __LINE__, "<b>".__CLASS__."::".__FUNCTION__."</b>" , 
139         __FILE__, "<font color='red'><i>Found: 0</i></font>", $info=$mod);
140     }else{
141       @DEBUG(DEBUG_SI, __LINE__, "<b>".__CLASS__."::".__FUNCTION__."</b>" , 
142         __FILE__, "<i>Found: ".count($hosts)."</i>", $info=$mod);
143     }
145     return($hosts);
146   }
149   /*! \brief  Disconnect from gosa daemon.
150    */
151   public function disconnect()
152   {
153     $this->o_sock->close();
154     $this->is_connected = FALSE;
155   }
158   /*! \brief  Sets an error message, which can be returned with get_error().
159     @param  string  The Error message,
160    */
161   private function set_error($str)
162   {
163     /******
164       Debug handling
165      ******/
166     $debug = debug_backtrace();
167     $file = __FILE__;
168     $function = __FUNCTION__;
169     $line = __LINE__;
170     $class = __CLASS__;
171     foreach($debug as $info){
172       if(!in_array($info['function'],array("send_data","_send","set_error","connect"))){
173         $file = $info['file'];
174         $line = $info['line'];
175         $class = get_class($this);
176         $function = $info['function'];
177         break;
178       }
179     }
180     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
182     /******
183       Set error string.
184      ******/
185   
186     $this->b_error = TRUE;
187     $this->s_error = $str;
188   }
191   /*! \brief  Sets an error message, which can be returned with get_error().
192     @param  string  The Error message,
193    */
194   private function reset_error()
195   {
196     $this->b_error = FALSE;
197     $this->s_error = "";
198   }
201   /*! \brief  Checks if an error occured.
202     @return boolean returns TRUE or FALSE, whether there is an error or not.
203    */
204   public function is_error()
205   {
206     return($this->b_error);
207   }
210   /*! \brief  Returns the last error. 
211     @return Returns the last error.
212    */
213   public function get_error()
214   {
215     $str = $this->s_error;
216     $ret = "";
217     if(is_string($str)){
218       $ret = $str;
219     }else{
220       foreach($str as $msg){
221         $ret .= $msg." ";
222       }
223     }
224     $ret = str_replace(" ","&nbsp;",$ret);
225     return($ret);
226   }
229   public function FAI_get_kernels($release)
230   {
231     $xml_msg = 
232       "<xml>".
233       "<header>gosa_get_available_kernel</header>".
234       "<source>GOSA</source>".
235       "<target>GOSA</target>".
236       "<fai_release>".$release."</fai_release>".
237       "</xml>";
239     $ret = array();
240     if($this->connect()){
241       $entries = $this->_send($xml_msg,TRUE);
243       /* Check if returned values represent a valid answer */
244       if(isset($entries['XML']) && is_array($entries['XML'])){
245         if(isset($entries['XML'])){
246           $ret = $entries['XML'];
247           foreach($ret as $key => $entry){
248             if(!preg_match("/^answer/i",$key)){
249               unset($ret[$key]);
250             }
251           }
252         }
253       }
254     }
255     return($ret);
256   }
259   public function FAI_get_package_sections($release)
260   {
261     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
262       "<select>distinct section</select>".
263       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
265     $ret = array();
266     if($this->connect()){
267       $entries = $this->_send($xml_msg,TRUE);
268       if(isset($entries['XML']) && is_array($entries['XML'])){
270         /* Unset header tags */
271         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
272           if(isset($entries['XML'][$type])){
273             unset($entries['XML'][$type]);
274           }
275         }
276         $ret = $entries['XML'];
277       }
278     }
279     return($ret);
280   }
283   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1, $exclude_sections=array())
284   {
285     $ret = array();
287     /* Check Parameter */
288     if(!is_array($attrs) || !count($attrs)){
289       trigger_error("Second parameter must be an array. With at least one attribute name.");
290       return($ret);
291     }
293     /* Check Parameter */
294     if(!is_array($package)){
295       trigger_error("Third parameter must be an array. With at least one attribute name.");
296       return($ret);
297     }
299     /* Create list of attributes to fetch */
300     $attr = ""; 
301     foreach($attrs as $at){
302       $attr.= "<select>".$at."</select>";
303     }
305     /* If no package is given, search for all */
306     if(!count($package)) $package = array("%");
308     /* If no section is given, search for all */
309     $section_filter = "";
310     if(count($exclude_sections)) {
311       $section_filter = '<clause><connector>AND</connector>';
312           foreach ($exclude_sections as $section => $value) {
313             $section_filter .= "<phrase><operator>ne</operator><section>"
314               .$section
315               ."</section></phrase>";
316           }
317         $section_filter .= "</clause>";
318     }
320     /* Create limit tag */
321     if($from == -1){
322       $limit =""; 
323     }else{
324       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
325     }
327     /* Create list of attributes to fetch */
328     $pkgs = ""; 
329     foreach($package as $pkg){
330       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
331     }
333     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
334       $attr.
335       "<where>
336       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
337       <clause><connector>OR</connector>
338       ".$pkgs."
339       </clause>
340       ".$section_filter."
341       </where>".
342       $limit.
343       "</xml>";
345     if($this->connect()){
346       $entries = $this->_send($xml_msg,TRUE);
347       if(isset($entries['XML']) && is_array($entries['XML'])){
349         /* Check if returned values represent a valid answer */
350         if(isset($entries['XML'])){
352           /* Unset header tags */
353           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
354             if(isset($entries['XML'][$type])){
355               unset($entries['XML'][$type]);
356             }
357           }
358           $ret = $entries['XML'];
359         }
360       }
361     }
362     return($ret);
364     
365   }
368   public function FAI_get_server($name = "")
369   {
371     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
372     $ret = array();
373     if($this->connect()){
375       /* Check if returned values represent a valid answer */
376       $entries = $this->_send($xml_msg,TRUE);
377       if(isset($entries['XML']) && is_array($entries['XML'])){
379         /* Unset header tags */
380         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
381           if(isset($entries['XML'][$type])){
382             unset($entries['XML'][$type]);
383           }
384         }
385         $ret = $entries['XML']; 
386       }
387     }
388     return($ret);
389   }
392   public function FAI_get_classes($name)
393   {
394     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
395                   "<where><clause><phrase><fai_release>".$name."</fai_release></phrase></clause></where></xml>";;
396     $ret = array();
397     if($this->connect()){
399       $entries = $this->_send($xml_msg,TRUE);
400       if(isset($entries['XML']) && is_array($entries['XML'])){
402         /* Unset header tags */
403         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
404           if(isset($entries['XML'][$type])){
405             unset($entries['XML'][$type]);
406           }
407         }
408         $ret = $entries['XML']; 
409       }
410     }
411     return($ret);
412   }
415   /*! \brief  Returns an array containing all queued entries.
416     @return Array All queued entries as an array.
417    */
418   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
419   {
420     $ret = array();
422     $tags = "";
423     foreach($event_types as $type){
424       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
425     }
426     if(count($event_types) > 1){
427       $tags = "<connector>or</connector>".$tags;
428     }
429     if(count($event_types)){
430       $tags = "<where><clause>".$tags."</clause></where>";
431     }
433     $xml_msg = 
434       "<xml>
435       <header>gosa_query_jobdb</header>
436       <target>GOSA</target>
437       <source>GOSA</source>
438       ".$tags."
440       <orderby>".$sort."</orderby>";
441     if($from != -1 && $to != -1){
442       $xml_msg.= "
443         <limit>
444         <from>".$from."</from>
445         <to>".$to."</to>
446         </limit>";
447     }
448     $xml_msg.= "
449       </xml>";
451     if($this->connect()){
452       $entries = $this->_send($xml_msg,TRUE);
453       if(isset($entries['XML']) && is_array($entries['XML'])){
455         /* Unset header tags */
456         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
457           unset($entries['XML'][$type]);
458         }
459         $ret = $entries['XML']; 
460       }
461     }
462     return($ret);
463   }
466   /*! \brief  Checks if the given ids are used queue ids.
467     @param  Array   The ids we want to check..
468     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
469    */
470   public function ids_exist($ids)
471   {
472     if(!is_array($ids)){
473       trigger_error("Requires an array as parameter.");
474       return;
475     }
477     $ret = array();
479     $xml_msg = "<xml>
480       <header>gosa_query_jobdb</header>
481       <target>GOSA</target>
482       <source>GOSA</source>
483       <where>
484       <clause>
485       <connector>or</connector>";
486     foreach($ids as $id){
487       $xml_msg .= "<phrase>
488         <operator>eq</operator>
489         <id>".$id."</id>
490         </phrase>";
491     }
492     $xml_msg .= "</clause>
493       </where>
494       </xml>";
496     if($this->connect()){
497       $entries = $this->_send($xml_msg,TRUE);
498       if(isset($entries['XML']) && is_array($entries['XML'])){
499         foreach($entries['XML'] as $entry){
500           if(is_array($entry) && array_key_exists("ID",$entry)){
501             $ret[] = $entry['ID'];
502           }
503         }
504       }
505     }
506     return($ret);
507   }
510   /*! \brief  Returns an entry containing all requested ids.
511     @param  Array   The IDs of the entries we want to return.
512     @return Array   Of the requested entries. 
513    */
514   public function get_entries_by_mac($macs)
515   {
516     if(!is_array($macs)){
517       trigger_error("Requires an array as parameter.");
518       return;
519     }
521     $ret = array();
523     $xml_msg = "<xml>
524       <header>gosa_query_jobdb</header>
525       <target>GOSA</target>
526       <source>GOSA</source>
527       <where>
528       <clause>
529       <connector>or</connector>";
530     foreach($macs as $mac){
531       $xml_msg .= "<phrase>
532         <operator>eq</operator>
533         <macaddress>".$mac."</macaddress>
534         </phrase>";
535     }
536     $xml_msg .= "</clause>
537       </where>
538       </xml>";
540     if($this->connect()){
541       $entries = $this->_send($xml_msg,TRUE);
542       if(isset($entries['XML'])){
543         foreach($entries['XML'] as $name => $entry){
544           if(preg_match("/^ANSWER[0-9]*$/",$name)){
545             $ret[$name] = $entry;
546           }
547         }
548       }
549     }
550     return($ret);
551   }
554   /*! \brief  Returns an entry containing all requested ids.
555     @param  Array   The IDs of the entries we want to return.
556     @return Array   Of the requested entries. 
557    */
558   public function get_entries_by_id($ids)
559   {
560     if(!is_array($ids)){
561       trigger_error("Requires an array as parameter.");
562       return;
563     }
565     $ret = array();
567     $xml_msg = "<xml>
568       <header>gosa_query_jobdb</header>
569       <target>GOSA</target>
570       <source>GOSA</source>
571       <where>
572       <clause>
573       <connector>or</connector>";
574     foreach($ids as $id){
575       $xml_msg .= "<phrase>
576         <operator>eq</operator>
577         <id>".$id."</id>
578         </phrase>";
579     }
580     $xml_msg .= "</clause>
581       </where>
582       </xml>";
584     if($this->connect()){
585       $entries = $this->_send($xml_msg,TRUE);
586       if(isset($entries['XML'])){
587         foreach($entries['XML'] as $name => $entry){
588           if(preg_match("/^ANSWER[0-9]*$/",$name)){
589             $ret[$name] = $entry;
590           }
591         }
592       }
593     }
594     return($ret);
595   }
598   /*! \brief  Checks if the given id is in use.
599     @param  Integer The ID of the entry.
600     @return Boolean TRUE if entry exists. 
601    */
602   public function id_exists($id)
603   {
604     if(!is_numeric($id)){
605       trigger_error("Requires an integer as parameter.");
606       return;
607     }
610     $xml_msg = "<xml>
611       <header>gosa_query_jobdb</header>
612       <target>GOSA</target>
613       <source>GOSA</source>
614       <where>
615       <clause>
616       <phrase>
617       <operator>eq</operator>
618       <id>".$id."</id>
619       </phrase>
620       </clause>
621       </where>
622       </xml>";
624     if($this->connect()){
625       $entries = $this->_send($xml_msg,TRUE);
626       if( isset($entries['XML']['HEADER']) && 
627           $entries['XML']['HEADER']=="answer" && 
628           isset($entries['XML']['ANSWER1'])){
629         return(TRUE);
630       }
631     }
632     return(FALSE);
633   }
636   /*! \brief  Returns an entry from the gosaSupportQueue
637     @param  Integer The ID of the entry we want to return.
638     @return Array   Of the requested entry. 
639    */
640   public function get_entry_by_id($id)
641   {
642     if(!is_numeric($id)){
643       trigger_error("Requires an integer as parameter.");
644       return;
645     }
646   
647     $ret = array();
648     $xml_msg = "<xml>
649       <header>gosa_query_jobdb</header>
650       <target>GOSA</target>
651       <source>GOSA</source>
652       <where>
653       <clause>
654       <phrase>
655       <operator>eq</operator>
656       <id>".$id."</id>
657       </phrase>
658       </clause>
659       </where>
660       </xml>";
661     if($this->connect()){
662       $entries = $this->_send($xml_msg,TRUE);
663       if( isset($entries['XML']['HEADER']) &&
664           $entries['XML']['HEADER']=="answer" &&
665           isset($entries['XML']['ANSWER1'])){
666         $ret = $entries['XML']['ANSWER1'];
667       }
668     }
669     return($ret);
670   }
673   /*! \brief  Removes a set of entries from the GOsa support queue. 
674     @param  Array The IDs to remove.
675     @return Boolean True on success.
676    */
677   public function remove_entries($ids)
678   {
679     if(!is_array($ids)){
680       trigger_error("Requires an array as parameter.");
681       return;
682     }
685     $ret = array();
687     $xml_msg = "<xml>
688       <header>gosa_delete_jobdb_entry</header>
689       <target>GOSA</target>
690       <source>GOSA</source>
691       <where>
692       <clause>
693       <connector>or</connector>";
694     foreach($ids as $id){
695       $xml_msg .= "<phrase>
696         <operator>eq</operator>
697         <id>".$id."</id>
698         </phrase>";
699     }
700     $xml_msg .= "</clause>
701       </where>
702       </xml>";
704     if($this->connect()){
705       $entries = $this->_send($xml_msg,TRUE);
706       if(isset($entries['XML']) || isset($entries['COUNT'])){
707         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
708         return(TRUE);
709       }else{
710         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
711       }
712     }
713     return(FALSE);
714   }
718   /*! \brief  Removes an entry from the GOsa support queue. 
719     @param  Integer The ID of the entry we want to remove.
720     @return Boolean True on success.
721    */
722   public function remove_entry($id)
723   {
724     return($this->remove_entries(array($id)));
725   }
728   /*! \brief  Parses the given xml string into an array 
729     @param  String XML string  
730     @return Array Returns an array containing the xml structure. 
731    */
732   private function xml_to_array($xml,$alternative_method = FALSE)
733   {
734     $params = array();
735     $level = array();
736     $parser  = xml_parser_create_ns();
737     xml_parse_into_struct($parser, $xml, $vals, $index);
739     $err_id = xml_get_error_code($parser);
740     if($err_id){
741       xml_parser_free($parser);
742     }else{
743       xml_parser_free($parser);
745       if($this->use_alternative_xml_parse_method) {
746         $params = $this->build_xml_array($vals);
747       } else {
749         foreach ($vals as $xml_elem) {
750           if ($xml_elem['type'] == 'open') {
751             if (array_key_exists('attributes',$xml_elem)) {
752               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
753             } else {
754               $level[$xml_elem['level']] = $xml_elem['tag'];
755             }
756           }
757           if ($xml_elem['type'] == 'complete') {
759             $start_level = 1;
760             $test2 = &$params;
761             while($start_level < $xml_elem['level']) {
762               $test2 = &$test2[$level[$start_level]];
763               $start_level++;
764             }
766             /* Save tag attributes too. 
767                e.g. <tag attr="val">
768              */
769             if(isset($xml_elem['attributes'])){
770               foreach($xml_elem['attributes'] as $name => $value){
771                 $test2['ATTRIBUTES'][$name] = $value;
772               }
773             }
775             if(!isset($test2[$xml_elem['tag']])){
776               if(isset($xml_elem['value'])){
777                 $test2[$xml_elem['tag']] = $xml_elem['value'];
778               }
779             }else{
780               if(!is_array($test2[$xml_elem['tag']])){
781                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
782               }
783               $test2[$xml_elem['tag']][] = $xml_elem['value'];
784             }
785           }
786         }
787       }
788     }
790     if(!isset($params['XML'])){
791       if (!array_key_exists('XML', $params)){
792         $this->set_error(_("Cannot not parse XML!"));
793       }
794       $params = array("COUNT" => 0);
795     }
797     return($params); 
798   }
801   function build_xml_array(&$vals)
802   {
803     $array = array();
804     while(count($vals)){
805       $key = key($vals);
806       $val = $vals[$key];
807       unset($vals[$key]);
808       if($val['type'] == "close"){
809         return($array);
810       }elseif($val['type']=="open"){
811         $array[$val['tag']][] = $this->build_xml_array($vals);
812       }elseif($val['type'] != "cdata"){
813         $data = array("VALUE" => "","ATTRIBUTES" => "");
814         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
815           if(isset($val[$name])){
816             $data[$attr] = $val[$name];
817           }
818         }
819         $array[$val['tag']][] = $data;
820       }else{
821 #print_a($val);
822       }
823     }
824     return($array);
825   }
832   /*! \brief  Updates an entry with a set of new values, 
833     @param  Integer The ID of the entry, we want to update.
834     @param  Array   The variables to update.   
835     @return Boolean Returns TRUE on success. 
836    */
837   public function update_entries($ids,$data)
838   {
839     if(!is_array($ids)){
840       trigger_error("Requires an array as first parameter.");
841       return;
842     }
844     if(!is_array($data)){
845       trigger_error("Requires an array as second parameter.");
846       return;
847     }
849     $attr = "";
850     foreach($data as $key => $value){
851       $key = strtolower($key);
852       if(is_array($value)){
853         foreach($value as $sub_value){
854           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
855         }
856       }else{
857         $attr.= "<$key>".strtolower($value)."</$key>\n";
858       }
859     }
861     $xml_msg = "<xml>
862       <header>gosa_update_status_jobdb_entry</header>
863       <target>GOSA</target>
864       <source>GOSA</source>
865       <where>
866       <clause>
867       <connector>or</connector>";
868     foreach($ids as $id){
869       $xml_msg .= "<phrase>
870         <operator>eq</operator>
871         <id>".$id."</id>
872         </phrase>";
873     }
874     $xml_msg .= "</clause>
875       </where>
876       <update>
877       ".$attr." 
878       </update>
879       </xml>";
881     if($this->connect()){
882       $entries = $this->_send($xml_msg,TRUE);
883       if(isset($entries['XML'])){
884         if(isset($entries['XML']['ERROR_STRING'])) {
885           $this->set_error($entries['XML']['ERROR_STRING']);
886           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
887           return(FALSE);
888         }
889         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
890         return(TRUE);
891       }
892     }
893     return(FALSE);
894   }
897   /*! \brief  Returns the number of currently queued objects.
898       @return Integer  
899    */
900   public function number_of_queued_entries($event_types)
901   {
902     $tags = "";
903     foreach($event_types as $type){
904       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
905     }
906     if(count($event_types) > 1){
907       $tags = "<connector>or</connector>".$tags;
908     }
909     if(count($event_types)){
910       $tags = "<where><clause>".$tags."</clause></where>";
911     }
914     $xml_msg =
915       "<xml>".
916       "<header>gosa_query_jobdb</header>".
917       "<target>GOSA</target>".
918       "<source>GOSA</source>".
919       "<select> count ID</select>".
920       $tags.
921       "</xml>";
923     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
924     $this->connect();
925     if($this->connect()){
926       $entries = $this->_send($xml_msg,TRUE);
927       if($this->o_sock->is_error()){
928         $this->set_error($this->o_sock->get_error());
929         return(0);
930       }
931       if(isset($entries['XML'])){
932         return($entries['XML']['COUNT']);
933       }
934     }
935     return(-1);
936   } 
939   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
940   {
941     $xml_message= "";
943     /* Prepare data */
944     foreach ($data as $key => $value){
945       if(is_array($value)){
946         foreach($value as $sub_value){
947           $xml_message.= "<$key>$sub_value</$key>";
948         }
949       }else{
950         $xml_message.= "<$key>$value</$key>";
951       }
952     }
954     /* Multiple targets? */
955     if (!is_array($to)){
956       $to_targets= array($to);
957     } else {
958       $to_targets= $to;
959     }
961     /* Build target strings */
962     $target ="";
963     foreach($to_targets as $to){
964       $target.= "<target>$to</target>";
965     }
967     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
968   }
971   /* Allows simply appending a new DaemonEvent 
972    */
973   public function append($event, $skip_add_mac = FALSE)
974   {
975     if(!($event instanceof DaemonEvent)){
976       return(FALSE);
977     }
978   
980     /* Add to queue if new 
981      */
982     if($event->is_new()){
984       $request_answer = FALSE;
985       if($event->get_type() == SCHEDULED_EVENT){
986         $action = $event->get_schedule_action();
987       }elseif($event->get_type() == TRIGGERED_EVENT){
988         $action = $event->get_trigger_action();
989       }else{
990         trigger_error("Unknown type of queue event given.");
991         return(FALSE);
992       }
994       /* Get event informations, like targets..
995        */
996       $targets    = $event->get_targets();
997       $data       = $event->save();
999       /* Append an entry for each target 
1000        */
1001       foreach($targets as $target){
1002         if (is_array($target)) {
1003             $target = $target['mac'];
1004         }
1006         if(!$skip_add_mac){
1007           $data['macaddress'] = $target;
1008         }
1009         $this->send_data($action,$target,$data,$request_answer);
1011         if($this->is_error()){
1012           return(FALSE);
1013         }
1014       }
1015       return(TRUE);
1016     }else{
1018       /* Updated edited entry.
1019        */
1020       $id                 = $event->get_id();
1021       $data               = $event->save();
1022       return($this->update_entries(array($id),$data));
1023     }
1025     return(FALSE);
1026   }
1029   /*! \brief  Returns an array containing all queued entries.
1030     @return Array All queued entries as an array.
1031    */
1032   public function _send($data, $answer_expected= FALSE)
1033   {
1035     $ret = array();
1036     if(!$this->connect()){
1037       return($ret);
1038     }
1039   
1040     $this->reset_error();
1042     /******
1043       Debug handling
1044      ******/
1045     $debug = debug_backtrace();
1046     $file = __FILE__;
1047     $function = __FUNCTION__;
1048     $line = __LINE__;
1049     $class = __CLASS__;
1050     foreach($debug as $info){
1051       if(!in_array($info['function'],array("send_data","_send"))){
1052         $file = $info['file'];
1053         $line = $info['line'];
1054         $class = get_class($this);
1055         $function = $info['function'];
1056         break;
1057       }
1058     }
1059     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1062     /*******
1063       Start sending data 
1064      *******/
1065     if($this->connect()){
1066       $this->o_sock->write($data);
1067       if ($answer_expected){
1068         $str = trim($this->o_sock->read());
1070         /* Check if something went wrong while reading */
1071         if($this->o_sock->is_error()){
1072           $this->set_error($this->o_sock->get_error());
1073           return($ret);
1074         }
1076         $entries = $this->xml_to_array($str);
1077         if(isset($entries['XML']) && is_array($entries['XML'])){
1078           $ret = $entries;
1079           if($this->use_alternative_xml_parse_method) {
1080             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1081               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1082               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1083                   array($data=>$data),"FAILED ".$this->get_error());
1084             }
1085           }else{
1086             if(isset($entries['XML']['ERROR_STRING'])) {
1087               $this->set_error($entries['XML']['ERROR_STRING']);
1088               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1089                   array($data=>$data),"FAILED ".$this->get_error());
1090             }elseif(isset($entries['XML']['ERROR'])){
1091               $this->set_error($entries['XML']['ERROR']);
1092               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1093                   array($data=>$data),"FAILED ".$this->get_error());
1094             }
1095           }
1096           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1097               array($data=>$data),"SUCCESS");
1098         }
1099       }else{
1100         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1101             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1102       }
1103     }
1104     return($ret);
1105   }
1108   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1109   {
1110     $xml_message= "";
1112     /* Get communication object */
1113     $d= new gosaSupportDaemon(TRUE,10);
1115     /* Prepare data */
1116     foreach ($data as $key => $value){
1117       if(is_array($value)){
1118         foreach($value as $sub_val){
1119           $xml_message.= "<$key>$sub_val</$key>";
1120         }
1121       }else{
1122         $xml_message.= "<$key>$value</$key>";
1123       }
1124     }
1126     /* Multiple targets? */
1127     if (!is_array($to)){
1128       $to_targets= array($to);
1129     } else {
1130       $to_targets= $to;
1131     }
1133     /* Build target strings */
1134     $target ="";
1135     foreach($to_targets as $to){
1136       $target.= "<target>$to</target>";
1137     }
1139     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1140   }
1143   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1144       @param  String  $mac  The mac address for which we want to remove all jobs.      
1145    */
1146   function clean_queue_from_mac($mac)
1147   {
1148     global $config;
1150     /* First of all we have to check which jobs are startet 
1151      *  for $mac 
1152      */
1153     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1154     
1155     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1156  
1157     $data = $this->_send($xml_msg,TRUE);
1158     if(is_array($data) && isset($data['XML'])){
1159       $already_aborted = FALSE;
1160       foreach($data['XML']  as $name => $entry){
1161         if(preg_match("/answer[0-9]*/i",$name)){
1162           $entry['STATUS'] = strtoupper($entry['STATUS']);
1163           switch($entry['STATUS']){
1165             case 'PROCESSING' :
1167               /* Send abort event, but only once 
1168                */
1169               if($already_aborted){
1170                 break;
1171               }elseif(class_available("DaemonEvent_faireboot")){
1172                 $already_aborted = TRUE;
1173                 $tmp = new DaemonEvent_faireboot($config);
1174                 $tmp->add_targets(array($mac));
1175                 $tmp->set_type(TRIGGERED_EVENT);
1176                 if(!$this->append($tmp)){
1177                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1178                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1179                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1180                 }else{
1181                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1182                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1183                 }
1184                 ;break;
1185               }else{
1186                 /* Couldn't find abort event, just remove entry */
1187               }
1189             case 'WAITING':
1190             case 'ERROR':
1191             default :
1192             
1193               /* Simply remove entries from queue. 
1194                *  Failed or waiting events, can be removed without any trouble.
1195                */ 
1196               if(!$this->remove_entries(array($entry['ID']))){
1197                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1198               }
1199               ;break;
1200           }
1201     
1202         }
1203       }
1204     }
1205   }
1208   static function ping($target)
1209   {
1210     if (tests::is_mac($target)){
1211       /* Get communication object */
1212       $d= new gosaSupportDaemon(TRUE,2);
1213       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1214       return (count($answer) ? TRUE:FALSE);
1215     }
1216     return (FALSE);
1217   }
1221   /*! \brief  Returns a list of all configured principals. 
1222               (Uses the GOsa support daemon instead of the ldap database.)
1223       @return Array  A list containing the names of all configured principals.
1224    */
1225   public function krb5_list_principals($server)
1226   {
1227     $res = array();  
1229     /* Check if the given server is a valid mac address
1230      */
1231     if(!tests::is_mac($server)){
1232       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1233       return($ret);
1234     }
1236     /* Prepare request event 
1237      */ 
1238     $xml_msg = 
1239       "<xml>".
1240       "<header>gosa_krb5_list_principals</header>".
1241       "<source>GOSA</source>".
1242       "<target>".$server."</target>".
1243       "</xml>";
1244     
1245     $tmp = $this->_send($xml_msg,TRUE);
1246     if(isset($tmp['XML']['PRINCIPAL'])){
1247       return($tmp['XML']['PRINCIPAL']);
1248     }else{
1249       return($res);
1250     }
1251   }
1254   /*! \brief  Returns the configuration settings for a given principal name. 
1255               (Uses the GOsa support daemon instead of the ldap database.)
1256       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1257       @return Array  A list containing the names of all configured principals.
1258    */
1259   public function krb5_get_principal($server,$name)
1260   {
1261     $ret = array();
1263     /* Check if the given name is a valid request value 
1264      */
1265     if(!is_string($name) || empty($name)){
1266       trigger_error("The given principal name is not of type string or it is empty.");
1267       return($ret);
1268     }
1270     /* Check if the given server is a valid mac address
1271      */
1272     if(!tests::is_mac($server)){
1273       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1274       return($ret);
1275     }
1277     /* Prepare request event 
1278      */ 
1279     $xml_msg = 
1280       "<xml>".
1281       "<header>gosa_krb5_get_principal</header>".
1282       "<principal>".$name."</principal>".
1283       "<source>GOSA</source>".
1284       "<target>".$server."</target>".
1285       "</xml>";
1287     $res = $this->_send($xml_msg,TRUE);
1288     if(isset($res['XML'])){
1289       return($res['XML']);
1290     }else{
1291       return($ret);
1292     }
1293   }
1296   /*! \brief  Creates a given principal with a set of configuration settings.
1297               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1298               (Uses the GOsa support daemon instead of the ldap database.)
1299       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1300       @return Boolean   TRUE on success else FALSE. 
1301    */
1302   public function krb5_add_principal($server,$name,$values)
1303   {
1304     $ret = FALSE;  
1306     /* Check if the given name is a valid request value 
1307      */
1308     if(!is_string($name) || empty($name)){
1309       trigger_error("The given principal name is not of type string or it is empty.");
1310       return($ret);
1311     }
1312     if(!is_array($values)){
1313       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1314       return($ret);
1315     }
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     $attrs = "";
1325     foreach($values as $key => $value){
1326       if(empty($key) || is_numeric($key)){
1327         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1328         return($ret);
1329       }
1330       $key = strtolower($key);
1331       if(is_array($value)){
1332         foreach($value as $val){
1333           $attrs.= "<$key>$val</$key>\n";
1334         }
1335       }else{
1336         $attrs.= "<$key>$value</$key>\n";
1337       }
1338     }
1340     /* Prepare request event 
1341      */ 
1342     $xml_msg = 
1343       "<xml>".
1344       "<header>gosa_krb5_create_principal</header>".
1345       "<principal>".$name."</principal>".
1346       $attrs.
1347       "<source>GOSA</source>".
1348       "<target>".$server."</target>".
1349       "</xml>";
1351     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1352   }
1355   function krb5_ramdomize_key($server,$name)  
1356   {
1357     /* Prepare request event 
1358      */ 
1359     $xml_msg = 
1360       "<xml>".
1361       "<header>gosa_krb5_randomize_key</header>".
1362       "<principal>".$name."</principal>".
1363       "<source>GOSA</source>".
1364       "<target>".$server."</target>".
1365       "</xml>";
1367     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1368   }
1369   
1372   /*! \brief  Updates a given principal with a set of configuration settings.
1373               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1374               (Uses the GOsa support daemon instead of the ldap database.)
1375       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1376       @return Boolean   TRUE on success else FALSE. 
1377    */
1378   public function krb5_set_principal($server,$name,$values)
1379   {
1380     $ret = FALSE;  
1382     /* Check if the given name is a valid request value 
1383      */
1384     if(!is_string($name) || empty($name)){
1385       trigger_error("The given principal name is not of type string or it is empty.");
1386       return($ret);
1387     }
1388     if(!is_array($values) || !count($values)){
1389       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1390       return($ret);
1391     }
1393     /* Check if the given server is a valid mac address
1394      */
1395     if(!tests::is_mac($server)){
1396       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1397       return($ret);
1398     }
1400     $attrs = "";
1401     foreach($values as $key => $value){
1402       if(empty($key) || is_numeric($key)){
1403         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1404         return($ret);
1405       }
1406       $key = strtolower($key);
1407       if(is_array($value)){
1408         foreach($value as $val){
1409           $attrs.= "<$key>$val</$key>\n";
1410         }
1411       }else{
1412         $attrs.= "<$key>$value</$key>\n";
1413       }
1414     }
1416     /* Prepare request event 
1417      */ 
1418     $xml_msg = 
1419       "<xml>".
1420       "<header>gosa_krb5_modify_principal</header>".
1421       "<principal>".$name."</principal>".
1422       $attrs.
1423       "<source>GOSA</source>".
1424       "<target>".$server."</target>".
1425       "</xml>";
1427     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1428   }
1431   /*! \brief  Removes the given principal.
1432               (Uses the GOsa support daemon instead of the ldap database.)
1433       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1434       @return Boollean   TRUE on success else FALSE
1435    */
1436   public function krb5_del_principal($server,$name)
1437   {
1438     $ret = FALSE;  
1440     /* Check if the given name is a valid request value 
1441      */
1442     if(!is_string($name) || empty($name)){
1443       trigger_error("The given principal name is not of type string or it is empty.");
1444       return($ret);
1445     }
1447     /* Check if the given server is a valid mac address
1448      */
1449     if(!tests::is_mac($server)){
1450       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1451       return($ret);
1452     }
1454     /* Prepare request event 
1455      */ 
1456     $xml_msg = 
1457       "<xml>".
1458       "<header>gosa_krb5_del_principal</header>".
1459       "<principal>".$name."</principal>".
1460       "<source>GOSA</source>".
1461       "<target>".$server."</target>".
1462       "</xml>";
1463     
1464     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1465   }
1468   /*! \brief  Returns a list of configured password policies.
1469               (Uses the GOsa support daemon instead of the ldap database.)
1470       @return Array A list of all configured password policies.
1471    */
1472   public function krb5_list_policies($server)
1473   {
1474     $res = array();  
1476     /* Check if the given server is a valid mac address
1477      */
1478     if(!tests::is_mac($server)){
1479       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1480       return($ret);
1481     }
1483     /* Prepare request event 
1484      */ 
1485     $xml_msg = 
1486       "<xml>".
1487       "<header>gosa_krb5_list_policies</header>".
1488       "<source>GOSA</source>".
1489       "<target>".$server."</target>".
1490       "</xml>";
1491     
1492     $res = $this->_send($xml_msg,TRUE);
1493     
1494     /* Check if there are results for POLICY 
1495      */
1496     if(isset($res['XML']['POLICY'])){
1497       
1498       /* Ensure that we return an array 
1499        */
1500       $tmp = $res['XML']['POLICY'];
1501       if(!is_array($tmp)){
1502         $tmp = array($tmp);
1503       }
1504       return($tmp);
1505     }else{
1506       return(array());
1507     }
1508   }
1511   /*! \brief  Returns a list of configured password policies.
1512               (Uses the GOsa support daemon instead of the ldap database.)
1513       @return Array The policy settings for the given policy name.
1514    */
1515   public function krb5_get_policy($server,$name)
1516   {
1517     $ret = array();  
1519     /* Check if the given name is a valid request value 
1520      */
1521     if(!is_string($name) || empty($name)){
1522       trigger_error("The given policy name is not of type string or it is empty.");
1523       return($ret);
1524     }
1526     /* Check if the given server is a valid mac address
1527      */
1528     if(!tests::is_mac($server)){
1529       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1530       return($ret);
1531     }
1533     /* Prepare request event 
1534      */ 
1535     $xml_msg = 
1536       "<xml>".
1537       "<header>gosa_krb5_get_policy</header>".
1538       "<policy>".$name."</policy>".
1539       "<source>GOSA</source>".
1540       "<target>".$server."</target>".
1541       "</xml>";
1543     /* Possible attributes */
1544     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1545         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1547   
1548     $tmp = $this->_send($xml_msg,TRUE);
1549     if(isset($tmp['XML'])){
1550       foreach($attrs as $attr){
1551         if(isset($tmp['XML'][$attr])){
1552           $ret[$attr] = $tmp['XML'][$attr];
1553         }else{
1554           $ret[$attr] = "";
1555         }
1556       }
1557     }
1558     return($ret);
1559   }
1561   
1562   /*! \brief  Creates a new policy with a given set of configuration settings.
1563               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1564               (Uses the GOsa support daemon instead of the ldap database.)
1565       @pram   String The name of the policy to update.
1566       @pram   Array  The attributes to update
1567       @return Boolean   TRUE on success else FALSE. 
1568    */
1569   public function krb5_add_policy($server,$name,$values)
1570   {
1571     $ret = FALSE;  
1573     /* Check if the given name is a valid request value 
1574      */
1575     if(!is_string($name) || empty($name)){
1576       trigger_error("The given policy name is not of type string or it is empty.");
1577       return($ret);
1578     }
1579     if(!is_array($values) || !count($values)){
1580       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1581       return($ret);
1582     }
1584     /* Check if the given server is a valid mac address
1585      */
1586     if(!tests::is_mac($server)){
1587       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1588       return($ret);
1589     }
1592     /* Transform array into <xml>
1593      */
1594     $attrs = "";
1595     foreach($values as $id => $value){
1596       if(empty($id) || is_numeric($id)){
1597         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1598         return($ret);
1599       }
1600       $id = strtolower($id);
1601       $attrs.= "<$id>$value</$id>\n";
1602     }
1604     /* Prepare request event 
1605      */ 
1606     $xml_msg = 
1607       "<xml>".
1608       "<header>gosa_krb5_create_policy</header>".
1609       "<policy>".$name."</policy>".
1610       $attrs.
1611       "<source>GOSA</source>".
1612       "<target>".$server."</target>".
1613       "</xml>";
1615     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1616   }
1619   /*! \brief  Updates a given policy with a set of configuration settings.
1620               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1621               (Uses the GOsa support daemon instead of the ldap database.)
1622       @pram   String The name of the policy to update.
1623       @return Boolean   TRUE on success else FALSE. 
1624    */
1625   public function krb5_set_policy($server,$name,$values)
1626   {
1627     $ret = FALSE;  
1629     /* Check if the given name is a valid request value 
1630      */
1631     if(!is_string($name) || empty($name)){
1632       trigger_error("The given policy name is not of type string or it is empty.");
1633       return($ret);
1634     }
1635     if(!is_array($values) || !count($values)){
1636       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1637       return($ret);
1638     }
1640     /* Check if the given server is a valid mac address
1641      */
1642     if(!tests::is_mac($server)){
1643       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1644       return($ret);
1645     }
1647     /* Transform array into <xml>
1648      */
1649     $attrs = "";
1650     foreach($values as $id => $value){
1651       if(preg_match("/^policy$/i",$id)) continue;
1652       if(empty($id) || is_numeric($id)){
1653         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1654         return($ret);
1655       }
1656       $id = strtolower($id);
1657       $attrs.= "<$id>$value</$id>\n";
1658     }
1660     /* Prepare request event 
1661      */ 
1662     $xml_msg = 
1663       "<xml>".
1664       "<header>gosa_krb5_modify_policy</header>".
1665       "<policy>".$name."</policy>".
1666       $attrs.
1667       "<source>GOSA</source>".
1668       "<target>".$server."</target>".
1669       "</xml>";
1671     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1672   }
1674   
1675   /*! \brief  Removes the given password policy. 
1676               (Uses the GOsa support daemon instead of the ldap database.)
1677       @return Boolean  TRUE on success else FALSE
1678    */
1679   public function krb5_del_policy($server,$name)
1680   {
1681     $ret = FALSE;
1683     /* Check if the given server is a valid mac address
1684      */
1685     if(!tests::is_mac($server)){
1686       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1687       return($ret);
1688     }
1690     /* Check if the given name is a valid request value 
1691      */
1692     if(!is_string($name) || empty($name)){
1693       trigger_error("The given policy name is not of type string or it is empty.");
1694       return($ret);
1695     }
1697     /* Prepare request event 
1698      */ 
1699     $xml_msg = 
1700       "<xml>".
1701       "<header>gosa_krb5_del_policy</header>".
1702       "<policy>".$name."</policy>".
1703       "<source>GOSA</source>".
1704       "<target>".$server."</target>".
1705       "</xml>";
1706     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1707   }
1710   /*! \brief  Sets the password of for the given principal.
1711               (Uses the GOsa support daemon instead of the ldap database.)
1712       @param  String  The servers mac
1713       @param  String  The principals name
1714       @param  String  $the new password.   
1715       @return Boolean  TRUE on success else FALSE
1716    */
1717   public function krb5_set_password($server,$name,$password)
1718   {
1719     $ret = FALSE;
1721     /* Check if the given server is a valid mac address
1722      */
1723     if(!tests::is_mac($server)){
1724       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1725       return($ret);
1726     }
1728     /* Check if the given name is a valid request value
1729      */
1730     if(!is_string($name) || empty($name)){
1731       trigger_error("The given principal name is not of type string or it is empty.");
1732       return($ret);
1733     }
1735     /* Prepare request event
1736      */
1737     $xml_msg =
1738       "<xml>".
1739       "<header>gosa_krb5_set_password</header>".
1740       "<principal>".$name."</principal>".
1741       "<password>".$password."</password>".
1742       "<source>GOSA</source>".
1743       "<target>".$server."</target>".
1744       "</xml>";
1745     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1746   }
1749   /*! \brief  Returns log file informations for a given mac address 
1750       @param  $mac The mac address to fetch logs for.
1751       @retrun Array A Multidimensional array containing log infos.
1752         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1753         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1754                                install_20080313_144450   ...
1755    */
1756   public function get_log_info_for_mac($mac)
1757   {
1758     $xml_msg = "
1759       <xml>
1760       <header>gosa_show_log_by_mac</header>
1761       <target>GOSA</target>
1762       <source>GOSA</source>
1763       <mac>".$mac."</mac>
1764       </xml>";
1766     $res = $this->_send($xml_msg,TRUE);
1767     $ret = array();
1768     if(isset($res['XML'])){
1770       /* Filter all entry that look like this 
1771           MAC_00_01_6C_9D_B9_FA
1772        */
1773       foreach($res['XML'] as $name => $entry){
1774         if(preg_match("/^MAC/",$name)){
1776           /* Get list of available log files 
1777            */
1778           if(!is_array($entry)){
1779             $entry = array($entry);
1780           }
1781           foreach($entry as $log_date){
1782             $xml_msg2 = "<xml> 
1783               <header>gosa_show_log_files_by_date_and_mac</header> 
1784               <target>GOSA</target> 
1785               <source>GOSA</source>                        
1786               <date>".$log_date."</date> 
1787               <mac>".$mac."</mac> 
1788               </xml>";
1789    
1790             $ret[$mac][$log_date] = array();
1791             $res = $this->_send($xml_msg2,TRUE);
1792             $ret[$mac][$log_date]['DATE_STR']  = $log_date;
1793             /* Determine FAI action */
1794             $fai_action = split('_', $ret[$mac][$log_date]['DATE_STR']);
1795             $fai_action = $fai_action[0];
1796             $ret[$mac][$log_date]['FAI_ACTION'] = $fai_action;
1797             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1798             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1799               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1800             }
1801           }
1802         }
1803       }
1804     }
1805     return($ret);
1806   }
1808   public function get_log_file($mac,$date,$file)
1809   {
1810     $xml_msg ="
1811       <xml> 
1812       <header>gosa_get_log_file_by_date_and_mac</header> 
1813       <target>GOSA</target> 
1814       <source>GOSA</source>
1815       <date>".$date."</date> 
1816       <mac>".$mac."</mac> 
1817       <log_file>".$file."</log_file>
1818       </xml>";
1820     $res = $this->_send($xml_msg,TRUE);
1821     if(isset($res['XML'][strtoupper($file)])){
1822       return(base64_decode($res['XML'][strtoupper($file)]));
1823     }
1824     return("");
1825   }
1831   /*****************
1832    * DAK - Functions 
1833    *****************/
1835   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1836       @param  ...
1837       @return Array   All queued entries.
1838    */
1839   public function DAK_keyring_entries($server)  
1840   {
1841     /* Ensure that we send the event to a valid mac address 
1842      */
1843     if(!is_string($server) || !tests::is_mac($server)){
1844       trigger_error("No valid mac address given '".$server."'.");
1845       return;
1846     }
1848     /* Create query
1849      */
1850     $xml_msg = "<xml> 
1851                   <header>gosa_get_dak_keyring</header> 
1852                   <target>".$server."</target> 
1853                   <source>GOSA</source>
1854                 </xml>";
1855         
1856     $res = $this->_send($xml_msg,TRUE);
1858     /* Check if there are results for POLICY
1859      */
1860     if(isset($res['XML'])){
1861       $ret = array();
1862       foreach($res['XML'] as $key => $entry){
1863         if(preg_match("/^ANSWER/",$key)){
1864           $ret[] = $entry;
1865         }
1866       }
1867       return($ret);
1868     }else{
1869       return(array());
1870     }
1871   }
1874   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1875       @param  String  The servers mac address 
1876       @param  String  The gpg key.
1877       @return Boolean TRUE on success else FALSE 
1878    */
1879   public function DAK_import_key($server,$key)  
1880   {
1881     /* Ensure that we send the event to a valid mac address 
1882      */
1883     if(!is_string($server) || !tests::is_mac($server)){
1884       trigger_error("No valid mac address given '".$server."'.");
1885       return;
1886     }
1888     /* Check if there is some cleanup required before importing the key.
1889         There may be some Header lines like:
1890         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1891      */
1892     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1894       /* Remove header */
1895       $key = preg_replace("/^.*\n\n/sim","",$key);
1896       /* Remove footer */
1897       $key = preg_replace("/-----.*$/sim","",$key);
1898     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1899       
1900       /* Encode key if it is raw.
1901        */
1902       $key = base64_encode($key);
1903     }
1905     /* Create query
1906      */
1907     $xml_msg = "<xml> 
1908                   <header>gosa_import_dak_key</header> 
1909                   <target>".$server."</target> 
1910                   <key>".$key."</key> 
1911                   <source>GOSA</source>
1912                 </xml>";
1913         
1914     $res = $this->_send($xml_msg,TRUE);
1915     return($this->is_error());
1916   }
1919   /*! \brief Removes a key from the keyring on the given server. 
1920       @param  String  The servers mac address 
1921       @param  String  The gpg key uid.
1922       @return Boolean TRUE on success else FALSE 
1923    */
1924   public function DAK_remove_key($server,$key)  
1925   {
1926     /* Ensure that we send the event to a valid mac address 
1927      */
1928     if(!is_string($server) || !tests::is_mac($server)){
1929       trigger_error("No valid mac address given '".$server."'.");
1930       return;
1931     }
1933     /* Create query
1934      */
1935     $xml_msg = "<xml> 
1936                   <header>gosa_remove_dak_key</header> 
1937                   <target>".$server."</target> 
1938                   <keyid>".$key."</keyid> 
1939                   <source>GOSA</source>
1940                 </xml>";
1941        
1942     $res = $this->_send($xml_msg,TRUE);
1943     return($this->is_error());
1944   }
1947 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1948 ?>