Code

bea358f72903078d111d8c4650f8100d645fd409
[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'])){
110       foreach($res['XML'][0] as $name => $data){
111         if(preg_match("/^HOST[0-9]*$/",$name) && $name != "HOST"){
112           $hosts[] = $data[0]['MAC'][0]['VALUE'];
113         }
114       }
115     }
116     return($hosts);
117   }
120   /*! \brief  Disconnect from gosa daemon.
121    */
122   public function disconnect()
123   {
124     $this->o_sock->close();
125     $this->is_connected = FALSE;
126   }
129   /*! \brief  Sets an error message, which can be returned with get_error().
130     @param  string  The Error message,
131    */
132   private function set_error($str)
133   {
134     /******
135       Debug handling
136      ******/
137     $debug = debug_backtrace();
138     $file = __FILE__;
139     $function = __FUNCTION__;
140     $line = __LINE__;
141     $class = __CLASS__;
142     foreach($debug as $info){
143       if(!in_array($info['function'],array("send_data","_send","set_error"))){
144         $file = $info['file'];
145         $line = $info['line'];
146         $class = get_class($this);
147         $function = $info['function'];
148         break;
149       }
150     }
151     @DEBUG(GOSA_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
153     /******
154       Set error string.
155      ******/
156   
157     $this->b_error = TRUE;
158     $this->s_error = $str;
159   }
162   /*! \brief  Sets an error message, which can be returned with get_error().
163     @param  string  The Error message,
164    */
165   private function reset_error()
166   {
167     $this->b_error = FALSE;
168     $this->s_error = "";
169   }
172   /*! \brief  Checks if an error occured.
173     @return boolean returns TRUE or FALSE, whether there is an error or not.
174    */
175   public function is_error()
176   {
177     return($this->b_error);
178   }
181   /*! \brief  Returns the last error. 
182     @return Returns the last error.
183    */
184   public function get_error()
185   {
186     $str = $this->s_error;
187     $ret = "";
188     if(is_string($str)){
189       $ret = $str;
190     }else{
191       foreach($str as $msg){
192         $ret .= $msg." ";
193       }
194     }
195     $ret = preg_replace("/ /","&nbsp;",$ret);
196     return($ret);
197   }
200   public function FAI_get_kernels($release)
201   {
202     $xml_msg = 
203       "<xml>".
204       "<header>gosa_get_available_kernel</header>".
205       "<source>GOSA</source>".
206       "<target>GOSA</target>".
207       "<release>".$release."</release>".
208       "</xml>";
210     $ret = array();
211     if($this->connect()){
212       $entries = $this->_send($xml_msg,TRUE);
214       /* Check if returned values represent a valid answer */
215       if(isset($entries['XML']) && is_array($entries['XML'])){
216         if(isset($entries['XML'])){
217           $ret = $entries['XML'];
218           foreach($ret as $key => $entry){
219             if(!preg_match("/^answer/i",$key)){
220               unset($ret[$key]);
221             }
222           }
223         }
224       }
225     }
226     return($ret);
227   }
230   public function FAI_get_package_sections($release)
231   {
232     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
233       "<select>distinct section</select>".
234       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
236     $ret = array();
237     if($this->connect()){
238       $entries = $this->_send($xml_msg,TRUE);
239       if(isset($entries['XML']) && is_array($entries['XML'])){
241         /* Unset header tags */
242         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
243           if(isset($entries['XML'][$type])){
244             unset($entries['XML'][$type]);
245           }
246         }
247         $ret = $entries['XML'];
248       }
249     }
250     return($ret);
251   }
254   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
255   {
256     $ret = array();
258     /* Check Parameter */
259     if(!is_array($attrs) || !count($attrs)){
260       trigger_error("Second parameter must be an array. With at least one attribute name.");
261       return($ret);
262     }
264     /* Check Parameter */
265     if(!is_array($package)){
266       trigger_error("Third parameter must be an array. With at least one attribute name.");
267       return($ret);
268     }
270     /* Create list of attributes to fetch */
271     $attr = ""; 
272     foreach($attrs as $at){
273       $attr.= "<select>".$at."</select>";
274     }
276     /* If no package is given, search for all */
277     if(!count($package)) $package = array("%");
279     /* Create limit tag */
280     if($from == -1){
281       $limit =""; 
282     }else{
283       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
284     }
286     /* Create list of attributes to fetch */
287     $pkgs = ""; 
288     foreach($package as $pkg){
289       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
290     }
292     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
293       $attr.
294       "<where>
295       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
296       <clause><connector>OR</connector>
297       ".$pkgs."
298       </clause>
299       </where>".
300       $limit.
301       "</xml>";
303     if($this->connect()){
304       $entries = $this->_send($xml_msg,TRUE);
305       if(isset($entries['XML']) && is_array($entries['XML'])){
307         /* Check if returned values represent a valid answer */
308         if(isset($entries['XML'])){
310           /* Unset header tags */
311           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
312             if(isset($entries['XML'][$type])){
313               unset($entries['XML'][$type]);
314             }
315           }
316           $ret = $entries['XML'];
317         }
318       }
319     }
320     return($ret);
322     
323   }
326   public function FAI_get_server($name = "")
327   {
329     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
330     $ret = array();
331     if($this->connect()){
333       /* Check if returned values represent a valid answer */
334       $entries = $this->_send($xml_msg,TRUE);
335       if(isset($entries['XML']) && is_array($entries['XML'])){
337         /* Unset header tags */
338         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
339           if(isset($entries['XML'][$type])){
340             unset($entries['XML'][$type]);
341           }
342         }
343         $ret = $entries['XML']; 
344       }
345     }
346     return($ret);
347   }
350   public function FAI_get_classes($name)
351   {
352     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
353                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
354     $ret = array();
355     if($this->connect()){
357       $entries = $this->_send($xml_msg,TRUE);
358       if(isset($entries['XML']) && is_array($entries['XML'])){
360         /* Unset header tags */
361         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
362           if(isset($entries['XML'][$type])){
363             unset($entries['XML'][$type]);
364           }
365         }
366         $ret = $entries['XML']; 
367       }
368     }
369     return($ret);
370   }
373   /*! \brief  Returns an array containing all queued entries.
374     @return Array All queued entries as an array.
375    */
376   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
377   {
378     $ret = array();
380     $tags = "";
381     foreach($event_types as $type){
382       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
383     }
384     if(count($event_types) > 1){
385       $tags = "<connector>or</connector>".$tags;
386     }
387     if(count($event_types)){
388       $tags = "<where><clause>".$tags."</clause></where>";
389     }
391     $xml_msg = 
392       "<xml>
393       <header>gosa_query_jobdb</header>
394       <target>GOSA</target>
395       <source>GOSA</source>
396       ".$tags."
398       <orderby>".$sort."</orderby>";
399     if($from != -1 && $to != -1){
400       $xml_msg.= "
401         <limit>
402         <from>".$from."</from>
403         <to>".$to."</to>
404         </limit>";
405     }
406     $xml_msg.= "
407       </xml>";
409     if($this->connect()){
410       $entries = $this->_send($xml_msg,TRUE);
411       if(isset($entries['XML']) && is_array($entries['XML'])){
413         /* Unset header tags */
414         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
415           unset($entries['XML'][$type]);
416         }
417         $ret = $entries['XML']; 
418       }
419     }
420     return($ret);
421   }
424   /*! \brief  Checks if the given ids are used queue ids.
425     @param  Array   The ids we want to check..
426     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
427    */
428   public function ids_exist($ids)
429   {
430     if(!is_array($ids)){
431       trigger_error("Requires an array as parameter.");
432       return;
433     }
435     $ret = array();
437     $xml_msg = "<xml>
438       <header>gosa_query_jobdb</header>
439       <target>GOSA</target>
440       <source>GOSA</source>
441       <where>
442       <clause>
443       <connector>or</connector>";
444     foreach($ids as $id){
445       $xml_msg .= "<phrase>
446         <operator>eq</operator>
447         <id>".$id."</id>
448         </phrase>";
449     }
450     $xml_msg .= "</clause>
451       </where>
452       </xml>";
454     if($this->connect()){
455       $entries = $this->_send($xml_msg,TRUE);
456       if(isset($entries['XML']) && is_array($entries['XML'])){
457         foreach($entries['XML'] as $entry){
458           if(is_array($entry) && array_key_exists("ID",$entry)){
459             $ret[] = $entry['ID'];
460           }
461         }
462       }
463     }
464     return($ret);
465   }
468   /*! \brief  Returns an entry containing all requested ids.
469     @param  Array   The IDs of the entries we want to return.
470     @return Array   Of the requested entries. 
471    */
472   public function get_entries_by_mac($macs)
473   {
474     if(!is_array($macs)){
475       trigger_error("Requires an array as parameter.");
476       return;
477     }
479     $ret = array();
481     $xml_msg = "<xml>
482       <header>gosa_query_jobdb</header>
483       <target>GOSA</target>
484       <source>GOSA</source>
485       <where>
486       <clause>
487       <connector>or</connector>";
488     foreach($macs as $mac){
489       $xml_msg .= "<phrase>
490         <operator>eq</operator>
491         <macaddress>".$mac."</macaddress>
492         </phrase>";
493     }
494     $xml_msg .= "</clause>
495       </where>
496       </xml>";
498     if($this->connect()){
499       $entries = $this->_send($xml_msg,TRUE);
500       if(isset($entries['XML'])){
501         foreach($entries['XML'] as $name => $entry){
502           if(preg_match("/^ANSWER[0-9]*$/",$name)){
503             $ret[$name] = $entry;
504           }
505         }
506       }
507     }
508     return($ret);
509   }
512   /*! \brief  Returns an entry containing all requested ids.
513     @param  Array   The IDs of the entries we want to return.
514     @return Array   Of the requested entries. 
515    */
516   public function get_entries_by_id($ids)
517   {
518     if(!is_array($ids)){
519       trigger_error("Requires an array as parameter.");
520       return;
521     }
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       $entries = $this->_send($xml_msg,TRUE);
544       if(isset($entries['XML'])){
545         foreach($entries['XML'] as $name => $entry){
546           if(preg_match("/^ANSWER[0-9]*$/",$name)){
547             $ret[$name] = $entry;
548           }
549         }
550       }
551     }
552     return($ret);
553   }
556   /*! \brief  Checks if the given id is in use.
557     @param  Integer The ID of the entry.
558     @return Boolean TRUE if entry exists. 
559    */
560   public function id_exists($id)
561   {
562     if(!is_numeric($id)){
563       trigger_error("Requires an integer as parameter.");
564       return;
565     }
568     $xml_msg = "<xml>
569       <header>gosa_query_jobdb</header>
570       <target>GOSA</target>
571       <source>GOSA</source>
572       <where>
573       <clause>
574       <phrase>
575       <operator>eq</operator>
576       <id>".$id."</id>
577       </phrase>
578       </clause>
579       </where>
580       </xml>";
582     if($this->connect()){
583       $entries = $this->_send($xml_msg,TRUE);
584       if( isset($entries['XML']['HEADER']) && 
585           $entries['XML']['HEADER']=="answer" && 
586           isset($entries['XML']['ANSWER1'])){
587         return(TRUE);
588       }
589     }
590     return(FALSE);
591   }
594   /*! \brief  Returns an entry from the gosaSupportQueue
595     @param  Integer The ID of the entry we want to return.
596     @return Array   Of the requested entry. 
597    */
598   public function get_entry_by_id($id)
599   {
600     if(!is_numeric($id)){
601       trigger_error("Requires an integer as parameter.");
602       return;
603     }
604   
605     $ret = array();
606     $xml_msg = "<xml>
607       <header>gosa_query_jobdb</header>
608       <target>GOSA</target>
609       <source>GOSA</source>
610       <where>
611       <clause>
612       <phrase>
613       <operator>eq</operator>
614       <id>".$id."</id>
615       </phrase>
616       </clause>
617       </where>
618       </xml>";
619     if($this->connect()){
620       $entries = $this->_send($xml_msg,TRUE);
621       if( isset($entries['XML']['HEADER']) &&
622           $entries['XML']['HEADER']=="answer" &&
623           isset($entries['XML']['ANSWER1'])){
624         $ret = $entries['XML']['ANSWER1'];
625       }
626     }
627     return($ret);
628   }
631   /*! \brief  Removes a set of entries from the GOsa support queue. 
632     @param  Array The IDs to remove.
633     @return Boolean True on success.
634    */
635   public function remove_entries($ids)
636   {
637     if(!is_array($ids)){
638       trigger_error("Requires an array as parameter.");
639       return;
640     }
643     $ret = array();
645     $xml_msg = "<xml>
646       <header>gosa_delete_jobdb_entry</header>
647       <target>GOSA</target>
648       <source>GOSA</source>
649       <where>
650       <clause>
651       <connector>or</connector>";
652     foreach($ids as $id){
653       $xml_msg .= "<phrase>
654         <operator>eq</operator>
655         <id>".$id."</id>
656         </phrase>";
657     }
658     $xml_msg .= "</clause>
659       </where>
660       </xml>";
662     if($this->connect()){
663       $entries = $this->_send($xml_msg,TRUE);
664       if(isset($entries['XML']) || isset($entries['COUNT'])){
665         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
666         return(TRUE);
667       }else{
668         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
669       }
670     }
671     return(FALSE);
672   }
676   /*! \brief  Removes an entry from the GOsa support queue. 
677     @param  Integer The ID of the entry we want to remove.
678     @return Boolean True on success.
679    */
680   public function remove_entry($id)
681   {
682     return($this->remove_entries(array($id)));
683   }
686   /*! \brief  Parses the given xml string into an array 
687     @param  String XML string  
688     @return Array Returns an array containing the xml structure. 
689    */
690   private function xml_to_array($xml,$alternative_method = FALSE)
691   {
692     $params = array();
693     $level = array();
694     $parser  = xml_parser_create_ns();
695     xml_parse_into_struct($parser, $xml, $vals, $index);
697     $err_id = xml_get_error_code($parser);
698     if($err_id){
699       xml_parser_free($parser);
700     }else{
701       xml_parser_free($parser);
703       if($this->use_alternative_xml_parse_method) {
704         $params = $this->build_xml_array($vals);
705       } else {
707         foreach ($vals as $xml_elem) {
708           if ($xml_elem['type'] == 'open') {
709             if (array_key_exists('attributes',$xml_elem)) {
710               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
711             } else {
712               $level[$xml_elem['level']] = $xml_elem['tag'];
713             }
714           }
715           if ($xml_elem['type'] == 'complete') {
717             $start_level = 1;
718             $test2 = &$params;
719             while($start_level < $xml_elem['level']) {
720               $test2 = &$test2[$level[$start_level]];
721               $start_level++;
722             }
724             /* Save tag attributes too. 
725                e.g. <tag attr="val">
726              */
727             if(isset($xml_elem['attributes'])){
728               foreach($xml_elem['attributes'] as $name => $value){
729                 $test2['ATTRIBUTES'][$name] = $value;
730               }
731             }
733             if(!isset($test2[$xml_elem['tag']])){
734               if(isset($xml_elem['value'])){
735                 $test2[$xml_elem['tag']] = $xml_elem['value'];
736               }
737             }else{
738               if(!is_array($test2[$xml_elem['tag']])){
739                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
740               }
741               $test2[$xml_elem['tag']][] = $xml_elem['value'];
742             }
743           }
744         }
745       }
746     }
748     if(!isset($params['XML'])){
749       if (!array_key_exists('XML', $params)){
750         $this->set_error(_("Cannot not parse XML!"));
751       }
752       $params = array("COUNT" => 0);
753     }
755     return($params); 
756   }
759   function build_xml_array(&$vals)
760   {
761     $array = array();
762     while(count($vals)){
763       $key = key($vals);
764       $val = $vals[$key];
765       unset($vals[$key]);
766       if($val['type'] == "close"){
767         return($array);
768       }elseif($val['type']=="open"){
769         $array[$val['tag']][] = $this->build_xml_array($vals);
770       }elseif($val['type'] != "cdata"){
771         $data = array("VALUE" => "","ATTRIBUTES" => "");
772         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
773           if(isset($val[$name])){
774             $data[$attr] = $val[$name];
775           }
776         }
777         $array[$val['tag']][] = $data;
778       }else{
779 #print_a($val);
780       }
781     }
782     return($array);
783   }
790   /*! \brief  Updates an entry with a set of new values, 
791     @param  Integer The ID of the entry, we want to update.
792     @param  Array   The variables to update.   
793     @return Boolean Returns TRUE on success. 
794    */
795   public function update_entries($ids,$data)
796   {
797     if(!is_array($ids)){
798       trigger_error("Requires an array as first parameter.");
799       return;
800     }
802     if(!is_array($data)){
803       trigger_error("Requires an array as second parameter.");
804       return;
805     }
807     $attr = "";
808     foreach($data as $key => $value){
809       $key = strtolower($key);
810       if(is_array($value)){
811         foreach($value as $sub_value){
812           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
813         }
814       }else{
815         $attr.= "<$key>".strtolower($value)."</$key>\n";
816       }
817     }
819     $xml_msg = "<xml>
820       <header>gosa_update_status_jobdb_entry</header>
821       <target>GOSA</target>
822       <source>GOSA</source>
823       <where>
824       <clause>
825       <connector>or</connector>";
826     foreach($ids as $id){
827       $xml_msg .= "<phrase>
828         <operator>eq</operator>
829         <id>".$id."</id>
830         </phrase>";
831     }
832     $xml_msg .= "</clause>
833       </where>
834       <update>
835       ".$attr." 
836       </update>
837       </xml>";
839     if($this->connect()){
840       $entries = $this->_send($xml_msg,TRUE);
841       if(isset($entries['XML'])){
842         if(isset($entries['XML']['ERROR_STRING'])) {
843           $this->set_error($entries['XML']['ERROR_STRING']);
844           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
845           return(FALSE);
846         }
847         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
848         return(TRUE);
849       }
850     }
851     return(FALSE);
852   }
855   /*! \brief  Returns the number of currently queued objects.
856       @return Integer  
857    */
858   public function number_of_queued_entries($event_types)
859   {
860     $tags = "";
861     foreach($event_types as $type){
862       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
863     }
864     if(count($event_types) > 1){
865       $tags = "<connector>or</connector>".$tags;
866     }
867     if(count($event_types)){
868       $tags = "<where><clause>".$tags."</clause></where>";
869     }
872     $xml_msg =
873       "<xml>".
874       "<header>gosa_query_jobdb</header>".
875       "<target>GOSA</target>".
876       "<source>GOSA</source>".
877       "<select> count ID</select>".
878       $tags.
879       "</xml>";
881     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
882     $this->connect();
883     if($this->connect()){
884       $entries = $this->_send($xml_msg,TRUE);
885       if($this->o_sock->is_error()){
886         $this->set_error($this->o_sock->get_error());
887         return(0);
888       }
889       if(isset($entries['XML'])){
890         return($entries['XML']['COUNT']);
891       }
892     }
893     return(-1);
894   } 
897   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
898   {
899     $xml_message= "";
901     /* Prepare data */
902     foreach ($data as $key => $value){
903       if(is_array($value)){
904         foreach($value as $sub_value){
905           $xml_message.= "<$key>$sub_value</$key>";
906         }
907       }else{
908         $xml_message.= "<$key>$value</$key>";
909       }
910     }
912     /* Multiple targets? */
913     if (!is_array($to)){
914       $to_targets= array($to);
915     } else {
916       $to_targets= $to;
917     }
919     /* Build target strings */
920     $target ="";
921     foreach($to_targets as $to){
922       $target.= "<target>$to</target>";
923     }
925     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
926   }
929   /* Allows simply appending a new DaemonEvent 
930    */
931   public function append($event)
932   {
933     if(!($event instanceof DaemonEvent)){
934       return(FALSE);
935     }
936   
938     /* Add to queue if new 
939      */
940     if($event->is_new()){
942       $request_answer = FALSE;
943       if($event->get_type() == SCHEDULED_EVENT){
944         $action = $event->get_schedule_action();
945       }elseif($event->get_type() == TRIGGERED_EVENT){
946         $action = $event->get_trigger_action();
947       }else{
948         trigger_error("Unknown type of queue event given.");
949         return(FALSE);
950       }
952       /* Get event informations, like targets..
953        */
954       $targets    = $event->get_targets();
955       $data       = $event->save();
957       /* Append an entry for each target 
958        */
959       foreach($targets as $target){
960         $data['macaddress'] = $target;
961         $this->send_data($action,$target,$data,$request_answer);
963         if($this->is_error()){
964           return(FALSE);
965         }
966       }
967       return(TRUE);
968     }else{
970       /* Updated edited entry.
971        */
972       $id                 = $event->get_id();
973       $data               = $event->save();
974       return($this->update_entries(array($id),$data));
975     }
977     return(FALSE);
978   }
981   /*! \brief  Returns an array containing all queued entries.
982     @return Array All queued entries as an array.
983    */
984   public function _send($data, $answer_expected= FALSE)
985   {
986     $this->reset_error();
987     $ret = array();
989     /******
990       Debug handling
991      ******/
992     $debug = debug_backtrace();
993     $file = __FILE__;
994     $function = __FUNCTION__;
995     $line = __LINE__;
996     $class = __CLASS__;
997     foreach($debug as $info){
998       if(!in_array($info['function'],array("send_data","_send"))){
999         $file = $info['file'];
1000         $line = $info['line'];
1001         $class = get_class($this);
1002         $function = $info['function'];
1003         break;
1004       }
1005     }
1006     @DEBUG(GOSA_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1009     /*******
1010       Start sending data 
1011      *******/
1012     if($this->connect()){
1013       $this->o_sock->write($data);
1014       if ($answer_expected){
1015         $str = trim($this->o_sock->read());
1017         /* Check if something went wrong while reading */
1018         if($this->o_sock->is_error()){
1019           $this->set_error($this->o_sock->get_error());
1020           return($ret);
1021         }
1023         $entries = $this->xml_to_array($str);
1024         if(isset($entries['XML']) && is_array($entries['XML'])){
1025           $ret = $entries;
1026           if($this->use_alternative_xml_parse_method) {
1027             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1028               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1029               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1030                   array($data=>$data),"FAILED ".$this->get_error());
1031             }
1032           }else{
1033             if(isset($entries['XML']['ERROR_STRING'])) {
1034               $this->set_error($entries['XML']['ERROR_STRING']);
1035               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1036                   array($data=>$data),"FAILED ".$this->get_error());
1037             }elseif(isset($entries['XML']['ERROR'])){
1038               $this->set_error($entries['XML']['ERROR']);
1039               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1040                   array($data=>$data),"FAILED ".$this->get_error());
1041             }
1042           }
1043           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1044               array($data=>$data),"SUCCESS");
1045         }
1046       }else{
1047         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1048             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1049       }
1050     }
1051     return($ret);
1052   }
1055   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1056   {
1057     $xml_message= "";
1059     /* Get communication object */
1060     $d= new gosaSupportDaemon(TRUE,10);
1062     /* Prepare data */
1063     foreach ($data as $key => $value){
1064       if(is_array($value)){
1065         foreach($value as $sub_val){
1066           $xml_message.= "<$key>$sub_val</$key>";
1067         }
1068       }else{
1069         $xml_message.= "<$key>$value</$key>";
1070       }
1071     }
1073     /* Multiple targets? */
1074     if (!is_array($to)){
1075       $to_targets= array($to);
1076     } else {
1077       $to_targets= $to;
1078     }
1080     /* Build target strings */
1081     $target ="";
1082     foreach($to_targets as $to){
1083       $target.= "<target>$to</target>";
1084     }
1086     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1087   }
1090   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1091       @param  String  $mac  The mac address for which we want to remove all jobs.      
1092    */
1093   function clean_queue_from_mac($mac)
1094   {
1095     global $config;
1097     /* First of all we have to check which jobs are startet 
1098      *  for $mac 
1099      */
1100     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1101     
1102     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1103  
1104     $data = $this->_send($xml_msg,TRUE);
1105     if(is_array($data) && isset($data['XML'])){
1106       $already_aborted = FALSE;
1107       foreach($data['XML']  as $name => $entry){
1108         if(preg_match("/answer[0-9]*/i",$name)){
1109           $entry['STATUS'] = strtoupper($entry['STATUS']);
1110           switch($entry['STATUS']){
1112             case 'PROCESSING' :
1114               /* Send abort event, but only once 
1115                */
1116               if($already_aborted){
1117                 break;
1118               }elseif(class_available("DaemonEvent_faireboot")){
1119                 $already_aborted = TRUE;
1120                 $tmp = new DaemonEvent_faireboot($config);
1121                 $tmp->add_targets(array($mac));
1122                 $tmp->set_type(TRIGGERED_EVENT);
1123                 if(!$this->append($tmp)){
1124                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1125                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1126                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1127                 }else{
1128                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1129                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1130                 }
1131                 ;break;
1132               }else{
1133                 /* Couldn't find abort event, just remove entry */
1134               }
1136             case 'WAITING':
1137             case 'ERROR':
1138             default :
1139             
1140               /* Simply remove entries from queue. 
1141                *  Failed or waiting events, can be removed without any trouble.
1142                */ 
1143               if(!$this->remove_entries(array($entry['ID']))){
1144                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1145               }
1146               ;break;
1147           }
1148     
1149         }
1150       }
1151     }
1152   }
1155   static function ping($target)
1156   {
1157     if (tests::is_mac($target)){
1158       /* Get communication object */
1159       $d= new gosaSupportDaemon(TRUE,0.5);
1160       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1161       return (count($answer) ? TRUE:FALSE);
1162     }
1163     return (FALSE);
1164   }
1168   /*! \brief  Returns a list of all configured principals. 
1169               (Uses the GOsa support daemon instead of the ldap database.)
1170       @return Array  A list containing the names of all configured principals.
1171    */
1172   public function krb5_list_principals($server)
1173   {
1174     $res = array();  
1176     /* Check if the given server is a valid mac address
1177      */
1178     if(!tests::is_mac($server)){
1179       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1180       return($ret);
1181     }
1183     /* Prepare request event 
1184      */ 
1185     $xml_msg = 
1186       "<xml>".
1187       "<header>gosa_krb5_list_principals</header>".
1188       "<source>GOSA</source>".
1189       "<target>".$server."</target>".
1190       "</xml>";
1191     
1192     $tmp = $this->_send($xml_msg,TRUE);
1193     if(isset($tmp['XML']['PRINCIPAL'])){
1194       return($tmp['XML']['PRINCIPAL']);
1195     }else{
1196       return($res);
1197     }
1198   }
1201   /*! \brief  Returns the configuration settings for a given principal name. 
1202               (Uses the GOsa support daemon instead of the ldap database.)
1203       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1204       @return Array  A list containing the names of all configured principals.
1205    */
1206   public function krb5_get_principal($server,$name)
1207   {
1208     $ret = array();
1210     /* Check if the given name is a valid request value 
1211      */
1212     if(!is_string($name) || empty($name)){
1213       trigger_error("The given principal name is not of type string or it is empty.");
1214       return($ret);
1215     }
1217     /* Check if the given server is a valid mac address
1218      */
1219     if(!tests::is_mac($server)){
1220       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1221       return($ret);
1222     }
1224     /* Prepare request event 
1225      */ 
1226     $xml_msg = 
1227       "<xml>".
1228       "<header>gosa_krb5_get_principal</header>".
1229       "<principal>".$name."</principal>".
1230       "<source>GOSA</source>".
1231       "<target>".$server."</target>".
1232       "</xml>";
1234     $res = $this->_send($xml_msg,TRUE);
1235     if(isset($res['XML'])){
1236       return($res['XML']);
1237     }else{
1238       return($ret);
1239     }
1240   }
1243   /*! \brief  Creates a given principal with a set of configuration settings.
1244               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1245               (Uses the GOsa support daemon instead of the ldap database.)
1246       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1247       @return Boolean   TRUE on success else FALSE. 
1248    */
1249   public function krb5_add_principal($server,$name,$values)
1250   {
1251     $ret = FALSE;  
1253     /* Check if the given name is a valid request value 
1254      */
1255     if(!is_string($name) || empty($name)){
1256       trigger_error("The given principal name is not of type string or it is empty.");
1257       return($ret);
1258     }
1259     if(!is_array($values)){
1260       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1261       return($ret);
1262     }
1264     /* Check if the given server is a valid mac address
1265      */
1266     if(!tests::is_mac($server)){
1267       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1268       return($ret);
1269     }
1271     $attrs = "";
1272     foreach($values as $key => $value){
1273       if(empty($key) || is_numeric($key)){
1274         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1275         return($ret);
1276       }
1277       $key = strtolower($key);
1278       if(is_array($value)){
1279         foreach($value as $val){
1280           $attrs.= "<$key>$val</$key>\n";
1281         }
1282       }else{
1283         $attrs.= "<$key>$value</$key>\n";
1284       }
1285     }
1287     /* Prepare request event 
1288      */ 
1289     $xml_msg = 
1290       "<xml>".
1291       "<header>gosa_krb5_create_principal</header>".
1292       "<principal>".$name."</principal>".
1293       $attrs.
1294       "<source>GOSA</source>".
1295       "<target>".$server."</target>".
1296       "</xml>";
1298     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1299   }
1302   function krb5_ramdomize_key($server,$name)  
1303   {
1304     /* Prepare request event 
1305      */ 
1306     $xml_msg = 
1307       "<xml>".
1308       "<header>gosa_krb5_randomize_key</header>".
1309       "<principal>".$name."</principal>".
1310       "<source>GOSA</source>".
1311       "<target>".$server."</target>".
1312       "</xml>";
1314     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1315   }
1316   
1319   /*! \brief  Updates a given principal with a set of configuration settings.
1320               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1321               (Uses the GOsa support daemon instead of the ldap database.)
1322       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1323       @return Boolean   TRUE on success else FALSE. 
1324    */
1325   public function krb5_set_principal($server,$name,$values)
1326   {
1327     $ret = FALSE;  
1329     /* Check if the given name is a valid request value 
1330      */
1331     if(!is_string($name) || empty($name)){
1332       trigger_error("The given principal name is not of type string or it is empty.");
1333       return($ret);
1334     }
1335     if(!is_array($values) || !count($values)){
1336       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1337       return($ret);
1338     }
1340     /* Check if the given server is a valid mac address
1341      */
1342     if(!tests::is_mac($server)){
1343       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1344       return($ret);
1345     }
1347     $attrs = "";
1348     foreach($values as $key => $value){
1349       if(empty($key) || is_numeric($key)){
1350         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1351         return($ret);
1352       }
1353       $key = strtolower($key);
1354       if(is_array($value)){
1355         foreach($value as $val){
1356           $attrs.= "<$key>$val</$key>\n";
1357         }
1358       }else{
1359         $attrs.= "<$key>$value</$key>\n";
1360       }
1361     }
1363     /* Prepare request event 
1364      */ 
1365     $xml_msg = 
1366       "<xml>".
1367       "<header>gosa_krb5_modify_principal</header>".
1368       "<principal>".$name."</principal>".
1369       $attrs.
1370       "<source>GOSA</source>".
1371       "<target>".$server."</target>".
1372       "</xml>";
1374     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1375   }
1378   /*! \brief  Removes the given principal.
1379               (Uses the GOsa support daemon instead of the ldap database.)
1380       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1381       @return Boollean   TRUE on success else FALSE
1382    */
1383   public function krb5_del_principal($server,$name)
1384   {
1385     $ret = FALSE;  
1387     /* Check if the given name is a valid request value 
1388      */
1389     if(!is_string($name) || empty($name)){
1390       trigger_error("The given principal name is not of type string or it is empty.");
1391       return($ret);
1392     }
1394     /* Check if the given server is a valid mac address
1395      */
1396     if(!tests::is_mac($server)){
1397       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1398       return($ret);
1399     }
1401     /* Prepare request event 
1402      */ 
1403     $xml_msg = 
1404       "<xml>".
1405       "<header>gosa_krb5_del_principal</header>".
1406       "<principal>".$name."</principal>".
1407       "<source>GOSA</source>".
1408       "<target>".$server."</target>".
1409       "</xml>";
1410     
1411     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1412   }
1415   /*! \brief  Returns a list of configured password policies.
1416               (Uses the GOsa support daemon instead of the ldap database.)
1417       @return Array A list of all configured password policies.
1418    */
1419   public function krb5_list_policies($server)
1420   {
1421     $res = array();  
1423     /* Check if the given server is a valid mac address
1424      */
1425     if(!tests::is_mac($server)){
1426       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1427       return($ret);
1428     }
1430     /* Prepare request event 
1431      */ 
1432     $xml_msg = 
1433       "<xml>".
1434       "<header>gosa_krb5_list_policies</header>".
1435       "<source>GOSA</source>".
1436       "<target>".$server."</target>".
1437       "</xml>";
1438     
1439     $res = $this->_send($xml_msg,TRUE);
1440     
1441     /* Check if there are results for POLICY 
1442      */
1443     if(isset($res['XML']['POLICY'])){
1444       
1445       /* Ensure that we return an array 
1446        */
1447       $tmp = $res['XML']['POLICY'];
1448       if(!is_array($tmp)){
1449         $tmp = array($tmp);
1450       }
1451       return($tmp);
1452     }else{
1453       return(array());
1454     }
1455   }
1458   /*! \brief  Returns a list of configured password policies.
1459               (Uses the GOsa support daemon instead of the ldap database.)
1460       @return Array The policy settings for the given policy name.
1461    */
1462   public function krb5_get_policy($server,$name)
1463   {
1464     $res = array();  
1466     /* Check if the given name is a valid request value 
1467      */
1468     if(!is_string($name) || empty($name)){
1469       trigger_error("The given policy name is not of type string or it is empty.");
1470       return($ret);
1471     }
1473     /* Check if the given server is a valid mac address
1474      */
1475     if(!tests::is_mac($server)){
1476       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1477       return($ret);
1478     }
1480     /* Prepare request event 
1481      */ 
1482     $xml_msg = 
1483       "<xml>".
1484       "<header>gosa_krb5_get_policy</header>".
1485       "<policy>".$name."</policy>".
1486       "<source>GOSA</source>".
1487       "<target>".$server."</target>".
1488       "</xml>";
1490     /* Possible attributes */
1491     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1492         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1494   
1495     $tmp = $this->_send($xml_msg,TRUE);
1496     if(isset($tmp['XML'])){
1497       foreach($attrs as $attr){
1498         if(isset($tmp['XML'][$attr])){
1499           $ret[$attr] = $tmp['XML'][$attr];
1500         }else{
1501           $ret[$attr] = "";
1502         }
1503       }
1504     }
1505     return($ret);
1506   }
1508   
1509   /*! \brief  Creates a new policy with a given set of configuration settings.
1510               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1511               (Uses the GOsa support daemon instead of the ldap database.)
1512       @pram   String The name of the policy to update.
1513       @pram   Array  The attributes to update
1514       @return Boolean   TRUE on success else FALSE. 
1515    */
1516   public function krb5_add_policy($server,$name,$values)
1517   {
1518     $ret = FALSE;  
1520     /* Check if the given name is a valid request value 
1521      */
1522     if(!is_string($name) || empty($name)){
1523       trigger_error("The given policy name is not of type string or it is empty.");
1524       return($ret);
1525     }
1526     if(!is_array($values) || !count($values)){
1527       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1528       return($ret);
1529     }
1531     /* Check if the given server is a valid mac address
1532      */
1533     if(!tests::is_mac($server)){
1534       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1535       return($ret);
1536     }
1539     /* Transform array into <xml>
1540      */
1541     $attrs = "";
1542     foreach($values as $id => $value){
1543       if(empty($id) || is_numeric($id)){
1544         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1545         return($ret);
1546       }
1547       $id = strtolower($id);
1548       $attrs.= "<$id>$value</$id>\n";
1549     }
1551     /* Prepare request event 
1552      */ 
1553     $xml_msg = 
1554       "<xml>".
1555       "<header>gosa_krb5_create_policy</header>".
1556       "<policy>".$name."</policy>".
1557       $attrs.
1558       "<source>GOSA</source>".
1559       "<target>".$server."</target>".
1560       "</xml>";
1562     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1563   }
1566   /*! \brief  Updates a given policy with a set of configuration settings.
1567               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1568               (Uses the GOsa support daemon instead of the ldap database.)
1569       @pram   String The name of the policy to update.
1570       @return Boolean   TRUE on success else FALSE. 
1571    */
1572   public function krb5_set_policy($server,$name,$values)
1573   {
1574     $ret = FALSE;  
1576     /* Check if the given name is a valid request value 
1577      */
1578     if(!is_string($name) || empty($name)){
1579       trigger_error("The given policy name is not of type string or it is empty.");
1580       return($ret);
1581     }
1582     if(!is_array($values) || !count($values)){
1583       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1584       return($ret);
1585     }
1587     /* Check if the given server is a valid mac address
1588      */
1589     if(!tests::is_mac($server)){
1590       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1591       return($ret);
1592     }
1594     /* Transform array into <xml>
1595      */
1596     $attrs = "";
1597     foreach($values as $id => $value){
1598       if(preg_match("/^policy$/i",$id)) continue;
1599       if(empty($id) || is_numeric($id)){
1600         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1601         return($ret);
1602       }
1603       $id = strtolower($id);
1604       $attrs.= "<$id>$value</$id>\n";
1605     }
1607     /* Prepare request event 
1608      */ 
1609     $xml_msg = 
1610       "<xml>".
1611       "<header>gosa_krb5_modify_policy</header>".
1612       "<policy>".$name."</policy>".
1613       $attrs.
1614       "<source>GOSA</source>".
1615       "<target>".$server."</target>".
1616       "</xml>";
1618     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1619   }
1621   
1622   /*! \brief  Removes the given password policy. 
1623               (Uses the GOsa support daemon instead of the ldap database.)
1624       @return Boolean  TRUE on success else FALSE
1625    */
1626   public function krb5_del_policy($server,$name)
1627   {
1628     $ret = FALSE;
1630     /* Check if the given server is a valid mac address
1631      */
1632     if(!tests::is_mac($server)){
1633       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1634       return($ret);
1635     }
1637     /* Check if the given name is a valid request value 
1638      */
1639     if(!is_string($name) || empty($name)){
1640       trigger_error("The given policy name is not of type string or it is empty.");
1641       return($ret);
1642     }
1644     /* Prepare request event 
1645      */ 
1646     $xml_msg = 
1647       "<xml>".
1648       "<header>gosa_krb5_del_policy</header>".
1649       "<policy>".$name."</policy>".
1650       "<source>GOSA</source>".
1651       "<target>".$server."</target>".
1652       "</xml>";
1653     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1654   }
1657   /*! \brief  Sets the password of for the given principal.
1658               (Uses the GOsa support daemon instead of the ldap database.)
1659       @param  String  The servers mac
1660       @param  String  The principals name
1661       @param  String  $the new password.   
1662       @return Boolean  TRUE on success else FALSE
1663    */
1664   public function krb5_set_password($server,$name,$password)
1665   {
1666     $ret = FALSE;
1668     /* Check if the given server is a valid mac address
1669      */
1670     if(!tests::is_mac($server)){
1671       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1672       return($ret);
1673     }
1675     /* Check if the given name is a valid request value
1676      */
1677     if(!is_string($name) || empty($name)){
1678       trigger_error("The given principal name is not of type string or it is empty.");
1679       return($ret);
1680     }
1682     /* Prepare request event
1683      */
1684     $xml_msg =
1685       "<xml>".
1686       "<header>gosa_krb5_set_password</header>".
1687       "<principal>".$name."</principal>".
1688       "<password>".$password."</password>".
1689       "<source>GOSA</source>".
1690       "<target>".$server."</target>".
1691       "</xml>";
1692     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1693   }
1696   /*! \brief  Returns log file informations for a given mac address 
1697       @param  $mac The mac address to fetch logs for.
1698       @retrun Array A Multidimensional array containing log infos.
1699         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1700         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1701                                install_20080313_144450   ...
1702    */
1703   public function get_log_info_for_mac($mac)
1704   {
1705     $xml_msg = "
1706       <xml>
1707       <header>gosa_show_log_by_mac</header>
1708       <target>GOSA</target>
1709       <source>GOSA</source>
1710       <mac>".$mac."</mac>
1711       </xml>";
1713     $res = $this->_send($xml_msg,TRUE);
1714     $ret = array();
1715     if(isset($res['XML'])){
1717       /* Filter all entry that look like this 
1718           MAC_00_01_6C_9D_B9_FA
1719        */
1720       foreach($res['XML'] as $name => $entry){
1721         if(preg_match("/^MAC/",$name)){
1723           /* Get list of available log files 
1724            */
1725           foreach($entry as $log_date){
1726             $xml_msg2 = "<xml> 
1727               <header>gosa_show_log_files_by_date_and_mac</header> 
1728               <target>GOSA</target> 
1729               <source>GOSA</source>                        
1730               <date>".$log_date."</date> 
1731               <mac>".$mac."</mac> 
1732               </xml>";
1734             $ret[$mac][$log_date] = array();
1735             $res = $this->_send($xml_msg2,TRUE);
1736             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1737             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1738             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1739               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1740             }
1741           }
1742         }
1743       }
1744     }
1745     return($ret);
1746   }
1748   public function get_log_file($mac,$date,$file)
1749   {
1750     $xml_msg ="
1751       <xml> 
1752       <header>gosa_get_log_file_by_date_and_mac</header> 
1753       <target>GOSA</target> 
1754       <source>GOSA</source>
1755       <date>".$date."</date> 
1756       <mac>".$mac."</mac> 
1757       <log_file>".$file."</log_file>
1758       </xml>";
1760     $res = $this->_send($xml_msg,TRUE);
1761     if(isset($res['XML'][strtoupper($file)])){
1762       return(base64_decode($res['XML'][strtoupper($file)]));
1763     }
1764     return("");
1765   }
1771   /*****************
1772    * DAK - Functions 
1773    *****************/
1775   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1776       @param  ...
1777       @return Array   All queued entries.
1778    */
1779   public function DAK_keyring_entries($server)  
1780   {
1781     /* Ensure that we send the event to a valid mac address 
1782      */
1783     if(!is_string($server) || !tests::is_mac($server)){
1784       trigger_error("No valid mac address given '".$server."'.");
1785       return;
1786     }
1788     /* Create query
1789      */
1790     $xml_msg = "<xml> 
1791                   <header>gosa_get_dak_keyring</header> 
1792                   <target>".$server."</target> 
1793                   <source>GOSA</source>
1794                 </xml>";
1795         
1796     $res = $this->_send($xml_msg,TRUE);
1798     /* Check if there are results for POLICY
1799      */
1800     if(isset($res['XML'])){
1801       $ret = array();
1802       foreach($res['XML'] as $key => $entry){
1803         if(preg_match("/^ANSWER/",$key)){
1804           $ret[] = $entry;
1805         }
1806       }
1807       return($ret);
1808     }else{
1809       return(array());
1810     }
1811   }
1814   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1815       @param  String  The servers mac address 
1816       @param  String  The gpg key.
1817       @return Boolean TRUE on success else FALSE 
1818    */
1819   public function DAK_import_key($server,$key)  
1820   {
1821     /* Ensure that we send the event to a valid mac address 
1822      */
1823     if(!is_string($server) || !tests::is_mac($server)){
1824       trigger_error("No valid mac address given '".$server."'.");
1825       return;
1826     }
1828     /* Check if there is some cleanup required before importing the key.
1829         There may be some Header lines like:
1830         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1831      */
1832     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1834       /* Remove header */
1835       $key = preg_replace("/^.*\n\n/sim","",$key);
1836       /* Remove footer */
1837       $key = preg_replace("/-----.*$/sim","",$key);
1838     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1839       
1840       /* Encode key if it is raw.
1841        */
1842       $key = base64_encode($key);
1843     }
1845     /* Create query
1846      */
1847     $xml_msg = "<xml> 
1848                   <header>gosa_import_dak_key</header> 
1849                   <target>".$server."</target> 
1850                   <key>".$key."</key> 
1851                   <source>GOSA</source>
1852                 </xml>";
1853         
1854     $res = $this->_send($xml_msg,TRUE);
1855     return($this->is_error());
1856   }
1859   /*! \brief Removes a key from the keyring on the given server. 
1860       @param  String  The servers mac address 
1861       @param  String  The gpg key uid.
1862       @return Boolean TRUE on success else FALSE 
1863    */
1864   public function DAK_remove_key($server,$key)  
1865   {
1866     /* Ensure that we send the event to a valid mac address 
1867      */
1868     if(!is_string($server) || !tests::is_mac($server)){
1869       trigger_error("No valid mac address given '".$server."'.");
1870       return;
1871     }
1873     /* Create query
1874      */
1875     $xml_msg = "<xml> 
1876                   <header>gosa_remove_dak_key</header> 
1877                   <target>".$server."</target> 
1878                   <keyid>".$key."</keyid> 
1879                   <source>GOSA</source>
1880                 </xml>";
1881        
1882     $res = $this->_send($xml_msg,TRUE);
1883     return($this->is_error());
1884   }
1887 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1888 ?>