Code

90005169ae154e59a32420d38fb540a514b7f505
[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;
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=null)
56   {
57     #FIXME: bad idea about referencing global variables from within classes
58     global $config;
60     // Detect timeout 
61     if($timeout == null){
62       $timeout = $config->get_cfg_value("gosaSupportTimeout",15);
63     }
65     /* This should only be the case if we call this from setup.
66         __autoload() 
67      */
68     if(!is_object($config)) { return; }
70     # load from config, store statically
71     if ($config->get_cfg_value("gosaSupportURI") != ""){
73       if ($this->s_host == ""){
74         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
75         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosaSupportURI"));
76         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
77       }
78       $this->is_configured = TRUE;
79       $this->f_timeout = $timeout;
80       if($connect){
81         $this->connect();
82       }
83     }
84   }
87   public function is_configured()
88   {
89     return($this->is_configured);
90   }
93   /*! \brief  Establish daemon connection. 
94     @return boolean Returns true if the connection was succesfully established. 
95    */
96   public function connect()
97   {
98     if(!empty($this->s_host) && !empty($this->i_port)){
99       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
100       if($this->o_sock->connected()){ 
101         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
102         $this->is_connected = TRUE;
103       }else{
104         $this->set_error($this->o_sock->get_error());
105         $this->disconnect();
106         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
107       }
108     }else{
109       $this->set_error(msgPool::cmdnotfound("gosaSupportURI",_("GOsa support daemon")));
110     }
111     return($this->is_connected);
112   }
114   
115   /*! \brief  Returns TRUE whether we are connected or not 
116       @return BOOLEAN  Returns TRUE when connected else FALSE
117    */
118   public function is_connected()
119   {
120     return($this->is_connected);
121   }
122  
124   /*! \brief  */
125   public function get_hosts_with_module($mod)
126   {
127     $data = array("module_name" => $mod);
128     $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
129     $hosts = array();
130     if(isset($res['XML'][0])){
131       foreach($res['XML'][0] as $name => $data){
132         if(preg_match("/^ANSWER[0-9]*$/",$name)){
133           if(isset($data[0]['MAC'][0]['VALUE']) && $data[0]['MAC'][0]['VALUE'] != ""){
134             $hosts[] = $data[0]['MAC'][0]['VALUE'];
135           } elseif(isset($data[0]['IP'][0]['VALUE']) && $data[0]['IP'][0]['VALUE'] != "") {
136             $hosts[] = $data[0]['IP'][0]['VALUE'];
137           }
138         }
139       }
140     }
142     if(count($hosts) == 0){
143       @DEBUG(DEBUG_SI, __LINE__, "<b>".__CLASS__."::".__FUNCTION__."</b>" , 
144         __FILE__, "<font color='red'><i>Found: 0</i></font>", $info=$mod);
145     }else{
146       @DEBUG(DEBUG_SI, __LINE__, "<b>".__CLASS__."::".__FUNCTION__."</b>" , 
147         __FILE__, "<i>Found: ".count($hosts)."</i>", $info=$mod);
148     }
150     return($hosts);
151   }
154   /*! \brief  Disconnect from gosa daemon.
155    */
156   public function disconnect()
157   {
158     $this->o_sock->close();
159     $this->is_connected = FALSE;
160   }
163   /*! \brief  Sets an error message, which can be returned with get_error().
164     @param  string  The Error message,
165    */
166   private function set_error($str)
167   {
168     /******
169       Debug handling
170      ******/
171     $debug = debug_backtrace();
172     $file = __FILE__;
173     $function = __FUNCTION__;
174     $line = __LINE__;
175     $class = __CLASS__;
176     foreach($debug as $info){
177       if(!in_array($info['function'],array("send_data","_send","set_error","connect"))){
178         $file = $info['file'];
179         $line = $info['line'];
180         $class = get_class($this);
181         $function = $info['function'];
182         break;
183       }
184     }
185     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
187     /******
188       Set error string.
189      ******/
190   
191     $this->b_error = TRUE;
192     $this->s_error = $str;
193   }
196   /*! \brief  Sets an error message, which can be returned with get_error().
197     @param  string  The Error message,
198    */
199   private function reset_error()
200   {
201     $this->b_error = FALSE;
202     $this->s_error = "";
203   }
206   /*! \brief  Checks if an error occured.
207     @return boolean returns TRUE or FALSE, whether there is an error or not.
208    */
209   public function is_error()
210   {
211     return($this->b_error);
212   }
215   /*! \brief  Returns the last error. 
216     @return Returns the last error.
217    */
218   public function get_error()
219   {
220     $str = $this->s_error;
221     $ret = "";
222     if(is_string($str)){
223       $ret = $str;
224     }else{
225       foreach($str as $msg){
226         $ret .= $msg." ";
227       }
228     }
229     $ret = str_replace(" ","&nbsp;",$ret);
230     return($ret);
231   }
234   public function FAI_get_kernels($release)
235   {
236     $xml_msg = 
237       "<xml>".
238       "<header>gosa_get_available_kernel</header>".
239       "<source>GOSA</source>".
240       "<target>GOSA</target>".
241       "<fai_release>".$release."</fai_release>".
242       "</xml>";
244     $ret = array();
245     if($this->connect()){
246       $entries = $this->_send($xml_msg,TRUE);
248       /* Check if returned values represent a valid answer */
249       if(isset($entries['XML']) && is_array($entries['XML'])){
250         if(isset($entries['XML'])){
251           $ret = $entries['XML'];
252           foreach($ret as $key => $entry){
253             if(!preg_match("/^answer/i",$key)){
254               unset($ret[$key]);
255             }
256           }
257         }
258       }
259     }
260     return($ret);
261   }
264   public function FAI_get_package_sections($release)
265   {
266     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
267       "<select>distinct section</select>".
268       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
270     $ret = array();
271     if($this->connect()){
272       $entries = $this->_send($xml_msg,TRUE);
273       if(isset($entries['XML']) && is_array($entries['XML'])){
275         /* Unset header tags */
276         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
277           if(isset($entries['XML'][$type])){
278             unset($entries['XML'][$type]);
279           }
280         }
281         $ret = $entries['XML'];
282       }
283     }
284     return($ret);
285   }
288   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
289   {
290     $ret = array();
292     /* Check Parameter */
293     if(!is_array($attrs) || !count($attrs)){
294       trigger_error("Second parameter must be an array. With at least one attribute name.");
295       return($ret);
296     }
298     /* Check Parameter */
299     if(!is_array($package)){
300       trigger_error("Third parameter must be an array. With at least one attribute name.");
301       return($ret);
302     }
304     /* Create list of attributes to fetch */
305     $attr = ""; 
306     foreach($attrs as $at){
307       $attr.= "<select>".$at."</select>";
308     }
310     /* If no package is given, search for all */
311     if(!count($package)) $package = array("%");
313     /* Create limit tag */
314     if($from == -1){
315       $limit =""; 
316     }else{
317       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
318     }
320     /* Create list of attributes to fetch */
321     $pkgs = ""; 
322     foreach($package as $pkg){
323       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
324     }
326     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
327       $attr.
328       "<where>
329       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
330       <clause><connector>OR</connector>
331       ".$pkgs."
332       </clause>
333       </where>".
334       $limit.
335       "</xml>";
337     if($this->connect()){
338       $entries = $this->_send($xml_msg,TRUE);
339       if(isset($entries['XML']) && is_array($entries['XML'])){
341         /* Check if returned values represent a valid answer */
342         if(isset($entries['XML'])){
344           /* Unset header tags */
345           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
346             if(isset($entries['XML'][$type])){
347               unset($entries['XML'][$type]);
348             }
349           }
350           $ret = $entries['XML'];
351         }
352       }
353     }
354     return($ret);
356     
357   }
360   public function FAI_get_server($name = "")
361   {
363     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
364     $ret = array();
365     if($this->connect()){
367       /* Check if returned values represent a valid answer */
368       $entries = $this->_send($xml_msg,TRUE);
369       if(isset($entries['XML']) && is_array($entries['XML'])){
371         /* Unset header tags */
372         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
373           if(isset($entries['XML'][$type])){
374             unset($entries['XML'][$type]);
375           }
376         }
377         $ret = $entries['XML']; 
378       }
379     }
380     return($ret);
381   }
384   public function FAI_get_classes($name)
385   {
386     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
387                   "<where><clause><phrase><fai_release>".$name."</fai_release></phrase></clause></where></xml>";;
388     $ret = array();
389     if($this->connect()){
391       $entries = $this->_send($xml_msg,TRUE);
392       if(isset($entries['XML']) && is_array($entries['XML'])){
394         /* Unset header tags */
395         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
396           if(isset($entries['XML'][$type])){
397             unset($entries['XML'][$type]);
398           }
399         }
400         $ret = $entries['XML']; 
401       }
402     }
403     return($ret);
404   }
407   /*! \brief  Returns an array containing all queued entries.
408     @return Array All queued entries as an array.
409    */
410   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
411   {
412     $ret = array();
414     $tags = "";
415     foreach($event_types as $type){
416       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
417     }
418     if(count($event_types) > 1){
419       $tags = "<connector>or</connector>".$tags;
420     }
421     if(count($event_types)){
422       $tags = "<where><clause>".$tags."</clause></where>";
423     }
425     $xml_msg = 
426       "<xml>
427       <header>gosa_query_jobdb</header>
428       <target>GOSA</target>
429       <source>GOSA</source>
430       ".$tags."
432       <orderby>".$sort."</orderby>";
433     if($from != -1 && $to != -1){
434       $xml_msg.= "
435         <limit>
436         <from>".$from."</from>
437         <to>".$to."</to>
438         </limit>";
439     }
440     $xml_msg.= "
441       </xml>";
443     if($this->connect()){
444       $entries = $this->_send($xml_msg,TRUE);
445       if(isset($entries['XML']) && is_array($entries['XML'])){
447         /* Unset header tags */
448         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
449           unset($entries['XML'][$type]);
450         }
451         $ret = $entries['XML']; 
452       }
453     }
454     return($ret);
455   }
458   /*! \brief  Checks if the given ids are used queue ids.
459     @param  Array   The ids we want to check..
460     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
461    */
462   public function ids_exist($ids)
463   {
464     if(!is_array($ids)){
465       trigger_error("Requires an array as parameter.");
466       return;
467     }
469     $ret = array();
471     $xml_msg = "<xml>
472       <header>gosa_query_jobdb</header>
473       <target>GOSA</target>
474       <source>GOSA</source>
475       <where>
476       <clause>
477       <connector>or</connector>";
478     foreach($ids as $id){
479       $xml_msg .= "<phrase>
480         <operator>eq</operator>
481         <id>".$id."</id>
482         </phrase>";
483     }
484     $xml_msg .= "</clause>
485       </where>
486       </xml>";
488     if($this->connect()){
489       $entries = $this->_send($xml_msg,TRUE);
490       if(isset($entries['XML']) && is_array($entries['XML'])){
491         foreach($entries['XML'] as $entry){
492           if(is_array($entry) && array_key_exists("ID",$entry)){
493             $ret[] = $entry['ID'];
494           }
495         }
496       }
497     }
498     return($ret);
499   }
502   /*! \brief  Returns an entry containing all requested ids.
503     @param  Array   The IDs of the entries we want to return.
504     @return Array   Of the requested entries. 
505    */
506   public function get_entries_by_mac($macs)
507   {
508     if(!is_array($macs)){
509       trigger_error("Requires an array as parameter.");
510       return;
511     }
513     $ret = array();
515     $xml_msg = "<xml>
516       <header>gosa_query_jobdb</header>
517       <target>GOSA</target>
518       <source>GOSA</source>
519       <where>
520       <clause>
521       <connector>or</connector>";
522     foreach($macs as $mac){
523       $xml_msg .= "<phrase>
524         <operator>eq</operator>
525         <macaddress>".$mac."</macaddress>
526         </phrase>";
527     }
528     $xml_msg .= "</clause>
529       </where>
530       </xml>";
532     if($this->connect()){
533       $entries = $this->_send($xml_msg,TRUE);
534       if(isset($entries['XML'])){
535         foreach($entries['XML'] as $name => $entry){
536           if(preg_match("/^ANSWER[0-9]*$/",$name)){
537             $ret[$name] = $entry;
538           }
539         }
540       }
541     }
542     return($ret);
543   }
546   /*! \brief  Returns an entry containing all requested ids.
547     @param  Array   The IDs of the entries we want to return.
548     @return Array   Of the requested entries. 
549    */
550   public function get_entries_by_id($ids)
551   {
552     if(!is_array($ids)){
553       trigger_error("Requires an array as parameter.");
554       return;
555     }
557     $ret = array();
559     $xml_msg = "<xml>
560       <header>gosa_query_jobdb</header>
561       <target>GOSA</target>
562       <source>GOSA</source>
563       <where>
564       <clause>
565       <connector>or</connector>";
566     foreach($ids as $id){
567       $xml_msg .= "<phrase>
568         <operator>eq</operator>
569         <id>".$id."</id>
570         </phrase>";
571     }
572     $xml_msg .= "</clause>
573       </where>
574       </xml>";
576     if($this->connect()){
577       $entries = $this->_send($xml_msg,TRUE);
578       if(isset($entries['XML'])){
579         foreach($entries['XML'] as $name => $entry){
580           if(preg_match("/^ANSWER[0-9]*$/",$name)){
581             $ret[$name] = $entry;
582           }
583         }
584       }
585     }
586     return($ret);
587   }
590   /*! \brief  Checks if the given id is in use.
591     @param  Integer The ID of the entry.
592     @return Boolean TRUE if entry exists. 
593    */
594   public function id_exists($id)
595   {
596     if(!is_numeric($id)){
597       trigger_error("Requires an integer as parameter.");
598       return;
599     }
602     $xml_msg = "<xml>
603       <header>gosa_query_jobdb</header>
604       <target>GOSA</target>
605       <source>GOSA</source>
606       <where>
607       <clause>
608       <phrase>
609       <operator>eq</operator>
610       <id>".$id."</id>
611       </phrase>
612       </clause>
613       </where>
614       </xml>";
616     if($this->connect()){
617       $entries = $this->_send($xml_msg,TRUE);
618       if( isset($entries['XML']['HEADER']) && 
619           $entries['XML']['HEADER']=="answer" && 
620           isset($entries['XML']['ANSWER1'])){
621         return(TRUE);
622       }
623     }
624     return(FALSE);
625   }
628   /*! \brief  Returns an entry from the gosaSupportQueue
629     @param  Integer The ID of the entry we want to return.
630     @return Array   Of the requested entry. 
631    */
632   public function get_entry_by_id($id)
633   {
634     if(!is_numeric($id)){
635       trigger_error("Requires an integer as parameter.");
636       return;
637     }
638   
639     $ret = array();
640     $xml_msg = "<xml>
641       <header>gosa_query_jobdb</header>
642       <target>GOSA</target>
643       <source>GOSA</source>
644       <where>
645       <clause>
646       <phrase>
647       <operator>eq</operator>
648       <id>".$id."</id>
649       </phrase>
650       </clause>
651       </where>
652       </xml>";
653     if($this->connect()){
654       $entries = $this->_send($xml_msg,TRUE);
655       if( isset($entries['XML']['HEADER']) &&
656           $entries['XML']['HEADER']=="answer" &&
657           isset($entries['XML']['ANSWER1'])){
658         $ret = $entries['XML']['ANSWER1'];
659       }
660     }
661     return($ret);
662   }
665   /*! \brief  Removes a set of entries from the GOsa support queue. 
666     @param  Array The IDs to remove.
667     @return Boolean True on success.
668    */
669   public function remove_entries($ids)
670   {
671     if(!is_array($ids)){
672       trigger_error("Requires an array as parameter.");
673       return;
674     }
677     $ret = array();
679     $xml_msg = "<xml>
680       <header>gosa_delete_jobdb_entry</header>
681       <target>GOSA</target>
682       <source>GOSA</source>
683       <where>
684       <clause>
685       <connector>or</connector>";
686     foreach($ids as $id){
687       $xml_msg .= "<phrase>
688         <operator>eq</operator>
689         <id>".$id."</id>
690         </phrase>";
691     }
692     $xml_msg .= "</clause>
693       </where>
694       </xml>";
696     if($this->connect()){
697       $entries = $this->_send($xml_msg,TRUE);
698       if(isset($entries['XML']) || isset($entries['COUNT'])){
699         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
700         return(TRUE);
701       }else{
702         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
703       }
704     }
705     return(FALSE);
706   }
710   /*! \brief  Removes an entry from the GOsa support queue. 
711     @param  Integer The ID of the entry we want to remove.
712     @return Boolean True on success.
713    */
714   public function remove_entry($id)
715   {
716     return($this->remove_entries(array($id)));
717   }
720   /*! \brief  Parses the given xml string into an array 
721     @param  String XML string  
722     @return Array Returns an array containing the xml structure. 
723    */
724   private function xml_to_array($xml,$alternative_method = FALSE)
725   {
726     $params = array();
727     $level = array();
728     $parser  = xml_parser_create_ns();
729     xml_parse_into_struct($parser, $xml, $vals, $index);
731     $err_id = xml_get_error_code($parser);
732     if($err_id){
733       xml_parser_free($parser);
734     }else{
735       xml_parser_free($parser);
737       if($this->use_alternative_xml_parse_method) {
738         $params = $this->build_xml_array($vals);
739       } else {
741         foreach ($vals as $xml_elem) {
742           if ($xml_elem['type'] == 'open') {
743             if (array_key_exists('attributes',$xml_elem)) {
744               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
745             } else {
746               $level[$xml_elem['level']] = $xml_elem['tag'];
747             }
748           }
749           if ($xml_elem['type'] == 'complete') {
751             $start_level = 1;
752             $test2 = &$params;
753             while($start_level < $xml_elem['level']) {
754               $test2 = &$test2[$level[$start_level]];
755               $start_level++;
756             }
758             /* Save tag attributes too. 
759                e.g. <tag attr="val">
760              */
761             if(isset($xml_elem['attributes'])){
762               foreach($xml_elem['attributes'] as $name => $value){
763                 $test2['ATTRIBUTES'][$name] = $value;
764               }
765             }
767             if(!isset($test2[$xml_elem['tag']])){
768               if(isset($xml_elem['value'])){
769                 $test2[$xml_elem['tag']] = $xml_elem['value'];
770               }
771             }else{
772               if(!is_array($test2[$xml_elem['tag']])){
773                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
774               }
775               $test2[$xml_elem['tag']][] = $xml_elem['value'];
776             }
777           }
778         }
779       }
780     }
782     if(!isset($params['XML'])){
783       if (!array_key_exists('XML', $params)){
784         $this->set_error(_("Cannot not parse XML!"));
785       }
786       $params = array("COUNT" => 0);
787     }
789     return($params); 
790   }
793   function build_xml_array(&$vals)
794   {
795     $array = array();
796     while(count($vals)){
797       $key = key($vals);
798       $val = $vals[$key];
799       unset($vals[$key]);
800       if($val['type'] == "close"){
801         return($array);
802       }elseif($val['type']=="open"){
803         $array[$val['tag']][] = $this->build_xml_array($vals);
804       }elseif($val['type'] != "cdata"){
805         $data = array("VALUE" => "","ATTRIBUTES" => "");
806         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
807           if(isset($val[$name])){
808             $data[$attr] = $val[$name];
809           }
810         }
811         $array[$val['tag']][] = $data;
812       }else{
813 #print_a($val);
814       }
815     }
816     return($array);
817   }
824   /*! \brief  Updates an entry with a set of new values, 
825     @param  Integer The ID of the entry, we want to update.
826     @param  Array   The variables to update.   
827     @return Boolean Returns TRUE on success. 
828    */
829   public function update_entries($ids,$data)
830   {
831     if(!is_array($ids)){
832       trigger_error("Requires an array as first parameter.");
833       return;
834     }
836     if(!is_array($data)){
837       trigger_error("Requires an array as second parameter.");
838       return;
839     }
841     $attr = "";
842     foreach($data as $key => $value){
843       $key = strtolower($key);
844       if(is_array($value)){
845         if($key == "periodic"){
846           foreach($value as $sub_name => $sub_value){
847             $attr.= "<periodic>$sub_name</periodic>";
848             $attr.= "<$sub_name>$sub_value</$sub_name>";
849           }
850         }else{
851           foreach($value as $sub_value){
852             $attr.= "<$key>".strtolower($sub_value)."</$key>";
853           }
854         }
855       }else{
856         $attr.= "<$key>".strtolower($value)."</$key>\n";
857       }
858     }
860     $xml_msg = "<xml>
861       <header>gosa_update_status_jobdb_entry</header>
862       <target>GOSA</target>
863       <source>GOSA</source>
864       <where>
865       <clause>
866       <connector>or</connector>";
867     foreach($ids as $id){
868       $xml_msg .= "<phrase>
869         <operator>eq</operator>
870         <id>".$id."</id>
871         </phrase>";
872     }
873     $xml_msg .= "</clause>
874       </where>
875       <update>
876       ".$attr." 
877       </update>
878       </xml>";
880     if($this->connect()){
881       $entries = $this->_send($xml_msg,TRUE);
882       if(isset($entries['XML'])){
883         if(isset($entries['XML']['ERROR_STRING'])) {
884           $this->set_error($entries['XML']['ERROR_STRING']);
885           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
886           return(FALSE);
887         }
888         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
889         return(TRUE);
890       }
891     }
892     return(FALSE);
893   }
896   /*! \brief  Returns the number of currently queued objects.
897       @return Integer  
898    */
899   public function number_of_queued_entries($event_types)
900   {
901     $tags = "";
902     foreach($event_types as $type){
903       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
904     }
905     if(count($event_types) > 1){
906       $tags = "<connector>or</connector>".$tags;
907     }
908     if(count($event_types)){
909       $tags = "<where><clause>".$tags."</clause></where>";
910     }
913     $xml_msg =
914       "<xml>".
915       "<header>gosa_query_jobdb</header>".
916       "<target>GOSA</target>".
917       "<source>GOSA</source>".
918       "<select> count ID</select>".
919       $tags.
920       "</xml>";
922     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
923     $this->connect();
924     if($this->connect()){
925       $entries = $this->_send($xml_msg,TRUE);
926       if($this->o_sock->is_error()){
927         $this->set_error($this->o_sock->get_error());
928         return(0);
929       }
930       if(isset($entries['XML'])){
931         return($entries['XML']['COUNT']);
932       }
933     }
934     return(-1);
935   } 
938   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
939   {
940     $xml_message= "";
942     /* Prepare data */
943     foreach ($data as $key => $value){
944       if(is_array($value)){
945         if($key == "periodic"){
946           foreach($value as $sub_name => $sub_value){
947             $xml_message.= "<periodic>$sub_name</periodic>";
948             $xml_message.= "<$sub_name>$sub_value</$sub_name>";
949           }
950         }else{
951           foreach($value as $sub_value){
952             $xml_message.= "<$key>$sub_value</$key>";
953           }
954         }
955       }else{
956         $xml_message.= "<$key>$value</$key>";
957       }
958     }
960     /* Multiple targets? */
961     if (!is_array($to)){
962       $to_targets= array($to);
963     } else {
964       $to_targets= $to;
965     }
967     /* Build target strings */
968     $target ="";
969     foreach($to_targets as $to){
970       $target.= "<target>$to</target>";
971     }
973     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
974   }
977   /* Allows simply appending a new DaemonEvent 
978    */
979   public function append($event, $skip_add_mac = FALSE)
980   {
981     if(!($event instanceof DaemonEvent)){
982       return(FALSE);
983     }
984   
986     /* Add to queue if new 
987      */
988     if($event->is_new()){
990       $request_answer = FALSE;
991       if($event->get_type() == SCHEDULED_EVENT){
992         $action = $event->get_schedule_action();
993       }elseif($event->get_type() == TRIGGERED_EVENT){
994         $action = $event->get_trigger_action();
995       }else{
996         trigger_error("Unknown type of queue event given.");
997         return(FALSE);
998       }
1000       /* Get event informations, like targets..
1001        */
1002       $targets    = $event->get_targets();
1003       $data       = $event->save();
1005       /* Append an entry for each target 
1006        */
1007       foreach($targets as $target){
1008         if(!$skip_add_mac){
1009           $data['macaddress'] = $target;
1010         }
1011         $this->send_data($action,$target,$data,$request_answer);
1013         if($this->is_error()){
1014           return(FALSE);
1015         }
1016       }
1017       return(TRUE);
1018     }else{
1020       /* Updated edited entry.
1021        */
1022       $id                 = $event->get_id();
1023       $data               = $event->save();
1024       return($this->update_entries(array($id),$data));
1025     }
1027     return(FALSE);
1028   }
1031   /*! \brief  Returns an array containing all queued entries.
1032     @return Array All queued entries as an array.
1033    */
1034   public function _send($data, $answer_expected= FALSE)
1035   {
1037     $ret = array();
1038     if(!$this->connect()){
1039       return($ret);
1040     }
1041   
1042     $this->reset_error();
1044     /******
1045       Debug handling
1046      ******/
1047     $debug = debug_backtrace();
1048     $file = __FILE__;
1049     $function = __FUNCTION__;
1050     $line = __LINE__;
1051     $class = __CLASS__;
1052     foreach($debug as $info){
1053       if(!in_array($info['function'],array("send_data","_send"))){
1054         $file = $info['file'];
1055         $line = $info['line'];
1056         $class = get_class($this);
1057         $function = $info['function'];
1058         break;
1059       }
1060     }
1061     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1064     /*******
1065       Start sending data 
1066      *******/
1067     if($this->connect()){
1068       $this->o_sock->write($data);
1069       if ($answer_expected){
1070         $str = trim($this->o_sock->read());
1072         /* Check if something went wrong while reading */
1073         if($this->o_sock->is_error()){
1074           $this->set_error($this->o_sock->get_error());
1075           return($ret);
1076         }
1078         $entries = $this->xml_to_array($str);
1079         if(isset($entries['XML']) && is_array($entries['XML'])){
1080           $ret = $entries;
1081           if($this->use_alternative_xml_parse_method) {
1082             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1083               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1084               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1085                   array($data=>$data),"FAILED ".$this->get_error());
1086             }
1087           }else{
1088             if(isset($entries['XML']['ERROR_STRING'])) {
1089               $this->set_error($entries['XML']['ERROR_STRING']);
1090               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1091                   array($data=>$data),"FAILED ".$this->get_error());
1092             }elseif(isset($entries['XML']['ERROR'])){
1093               $this->set_error($entries['XML']['ERROR']);
1094               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1095                   array($data=>$data),"FAILED ".$this->get_error());
1096             }
1097           }
1098           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1099               array($data=>$data),"SUCCESS");
1100         }
1101       }else{
1102         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1103             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1104       }
1105     }
1106     return($ret);
1107   }
1110   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1111   {
1112     $xml_message= "";
1114     /* Get communication object */
1115     $d= new gosaSupportDaemon(TRUE,10);
1117     /* Prepare data */
1118     foreach ($data as $key => $value){
1119       if(is_array($value)){
1120         foreach($value as $sub_val){
1121           $xml_message.= "<$key>$sub_val</$key>";
1122         }
1123       }else{
1124         $xml_message.= "<$key>$value</$key>";
1125       }
1126     }
1128     /* Multiple targets? */
1129     if (!is_array($to)){
1130       $to_targets= array($to);
1131     } else {
1132       $to_targets= $to;
1133     }
1135     /* Build target strings */
1136     $target ="";
1137     foreach($to_targets as $to){
1138       $target.= "<target>$to</target>";
1139     }
1141     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1142   }
1145   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1146       @param  String  $mac  The mac address for which we want to remove all jobs.      
1147    */
1148   function clean_queue_from_mac($mac)
1149   {
1150     global $config;
1152     /* First of all we have to check which jobs are startet 
1153      *  for $mac 
1154      */
1155     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1156     
1157     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1158  
1159     $data = $this->_send($xml_msg,TRUE);
1160     if(is_array($data) && isset($data['XML'])){
1161       $already_aborted = FALSE;
1162       foreach($data['XML']  as $name => $entry){
1163         if(preg_match("/answer[0-9]*/i",$name)){
1164           $entry['STATUS'] = strtoupper($entry['STATUS']);
1165           switch($entry['STATUS']){
1167             case 'PROCESSING' :
1169               /* Send abort event, but only once 
1170                */
1171               if($already_aborted){
1172                 break;
1173               }elseif(class_available("DaemonEvent_faireboot")){
1174                 $already_aborted = TRUE;
1175                 $tmp = new DaemonEvent_faireboot($config);
1176                 $tmp->add_targets(array($mac));
1177                 $tmp->set_type(TRIGGERED_EVENT);
1178                 if(!$this->append($tmp)){
1179                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1180                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1181                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1182                 }else{
1183                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1184                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1185                 }
1186                 ;break;
1187               }else{
1188                 /* Couldn't find abort event, just remove entry */
1189               }
1191             case 'WAITING':
1192             case 'ERROR':
1193             default :
1194             
1195               /* Simply remove entries from queue. 
1196                *  Failed or waiting events, can be removed without any trouble.
1197                */ 
1198               if(!$this->remove_entries(array($entry['ID']))){
1199                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1200               }
1201               ;break;
1202           }
1203     
1204         }
1205       }
1206     }
1207   }
1210   static function ping($target)
1211   {
1212     if (tests::is_mac($target)){
1213       /* Get communication object */
1214       $d= new gosaSupportDaemon(TRUE,2);
1215       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1216       return (count($answer) ? TRUE:FALSE);
1217     }
1218     return (FALSE);
1219   }
1223   /*! \brief  Returns a list of all configured principals. 
1224               (Uses the GOsa support daemon instead of the ldap database.)
1225       @return Array  A list containing the names of all configured principals.
1226    */
1227   public function krb5_list_principals($server)
1228   {
1229     $res = array();  
1231     /* Check if the given server is a valid mac address
1232      */
1233     if(!tests::is_mac($server)){
1234       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1235       return($ret);
1236     }
1238     /* Prepare request event 
1239      */ 
1240     $xml_msg = 
1241       "<xml>".
1242       "<header>gosa_krb5_list_principals</header>".
1243       "<source>GOSA</source>".
1244       "<target>".$server."</target>".
1245       "</xml>";
1246     
1247     $tmp = $this->_send($xml_msg,TRUE);
1248     if(isset($tmp['XML']['PRINCIPAL'])){
1249       return($tmp['XML']['PRINCIPAL']);
1250     }else{
1251       return($res);
1252     }
1253   }
1256   /*! \brief  Returns the configuration settings for a given principal name. 
1257               (Uses the GOsa support daemon instead of the ldap database.)
1258       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1259       @return Array  A list containing the names of all configured principals.
1260    */
1261   public function krb5_get_principal($server,$name)
1262   {
1263     $ret = array();
1265     /* Check if the given name is a valid request value 
1266      */
1267     if(!is_string($name) || empty($name)){
1268       trigger_error("The given principal name is not of type string or it is empty.");
1269       return($ret);
1270     }
1272     /* Check if the given server is a valid mac address
1273      */
1274     if(!tests::is_mac($server)){
1275       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1276       return($ret);
1277     }
1279     /* Prepare request event 
1280      */ 
1281     $xml_msg = 
1282       "<xml>".
1283       "<header>gosa_krb5_get_principal</header>".
1284       "<principal>".$name."</principal>".
1285       "<source>GOSA</source>".
1286       "<target>".$server."</target>".
1287       "</xml>";
1289     $res = $this->_send($xml_msg,TRUE);
1290     if(isset($res['XML'])){
1291       return($res['XML']);
1292     }else{
1293       return($ret);
1294     }
1295   }
1298   /*! \brief  Creates a given principal with a set of configuration settings.
1299               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1300               (Uses the GOsa support daemon instead of the ldap database.)
1301       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1302       @return Boolean   TRUE on success else FALSE. 
1303    */
1304   public function krb5_add_principal($server,$name,$values)
1305   {
1306     $ret = FALSE;  
1308     /* Check if the given name is a valid request value 
1309      */
1310     if(!is_string($name) || empty($name)){
1311       trigger_error("The given principal name is not of type string or it is empty.");
1312       return($ret);
1313     }
1314     if(!is_array($values)){
1315       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1316       return($ret);
1317     }
1319     /* Check if the given server is a valid mac address
1320      */
1321     if(!tests::is_mac($server)){
1322       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1323       return($ret);
1324     }
1326     $attrs = "";
1327     foreach($values as $key => $value){
1328       if(empty($key) || is_numeric($key)){
1329         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1330         return($ret);
1331       }
1332       $key = strtolower($key);
1333       if(is_array($value)){
1334         foreach($value as $val){
1335           $attrs.= "<$key>$val</$key>\n";
1336         }
1337       }else{
1338         $attrs.= "<$key>$value</$key>\n";
1339       }
1340     }
1342     /* Prepare request event 
1343      */ 
1344     $xml_msg = 
1345       "<xml>".
1346       "<header>gosa_krb5_create_principal</header>".
1347       "<principal>".$name."</principal>".
1348       $attrs.
1349       "<source>GOSA</source>".
1350       "<target>".$server."</target>".
1351       "</xml>";
1353     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1354   }
1357   function krb5_ramdomize_key($server,$name)  
1358   {
1359     /* Prepare request event 
1360      */ 
1361     $xml_msg = 
1362       "<xml>".
1363       "<header>gosa_krb5_randomize_key</header>".
1364       "<principal>".$name."</principal>".
1365       "<source>GOSA</source>".
1366       "<target>".$server."</target>".
1367       "</xml>";
1369     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1370   }
1371   
1374   /*! \brief  Updates a given principal with a set of configuration settings.
1375               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1376               (Uses the GOsa support daemon instead of the ldap database.)
1377       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1378       @return Boolean   TRUE on success else FALSE. 
1379    */
1380   public function krb5_set_principal($server,$name,$values)
1381   {
1382     $ret = FALSE;  
1384     /* Check if the given name is a valid request value 
1385      */
1386     if(!is_string($name) || empty($name)){
1387       trigger_error("The given principal name is not of type string or it is empty.");
1388       return($ret);
1389     }
1390     if(!is_array($values) || !count($values)){
1391       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1392       return($ret);
1393     }
1395     /* Check if the given server is a valid mac address
1396      */
1397     if(!tests::is_mac($server)){
1398       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1399       return($ret);
1400     }
1402     $attrs = "";
1403     foreach($values as $key => $value){
1404       if(empty($key) || is_numeric($key)){
1405         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1406         return($ret);
1407       }
1408       $key = strtolower($key);
1409       if(is_array($value)){
1410         foreach($value as $val){
1411           $attrs.= "<$key>$val</$key>\n";
1412         }
1413       }else{
1414         $attrs.= "<$key>$value</$key>\n";
1415       }
1416     }
1418     /* Prepare request event 
1419      */ 
1420     $xml_msg = 
1421       "<xml>".
1422       "<header>gosa_krb5_modify_principal</header>".
1423       "<principal>".$name."</principal>".
1424       $attrs.
1425       "<source>GOSA</source>".
1426       "<target>".$server."</target>".
1427       "</xml>";
1429     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1430   }
1433   /*! \brief  Removes the given principal.
1434               (Uses the GOsa support daemon instead of the ldap database.)
1435       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1436       @return Boollean   TRUE on success else FALSE
1437    */
1438   public function krb5_del_principal($server,$name)
1439   {
1440     $ret = FALSE;  
1442     /* Check if the given name is a valid request value 
1443      */
1444     if(!is_string($name) || empty($name)){
1445       trigger_error("The given principal name is not of type string or it is empty.");
1446       return($ret);
1447     }
1449     /* Check if the given server is a valid mac address
1450      */
1451     if(!tests::is_mac($server)){
1452       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1453       return($ret);
1454     }
1456     /* Prepare request event 
1457      */ 
1458     $xml_msg = 
1459       "<xml>".
1460       "<header>gosa_krb5_del_principal</header>".
1461       "<principal>".$name."</principal>".
1462       "<source>GOSA</source>".
1463       "<target>".$server."</target>".
1464       "</xml>";
1465     
1466     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1467   }
1470   /*! \brief  Returns a list of configured password policies.
1471               (Uses the GOsa support daemon instead of the ldap database.)
1472       @return Array A list of all configured password policies.
1473    */
1474   public function krb5_list_policies($server)
1475   {
1476     $res = array();  
1478     /* Check if the given server is a valid mac address
1479      */
1480     if(!tests::is_mac($server)){
1481       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1482       return($ret);
1483     }
1485     /* Prepare request event 
1486      */ 
1487     $xml_msg = 
1488       "<xml>".
1489       "<header>gosa_krb5_list_policies</header>".
1490       "<source>GOSA</source>".
1491       "<target>".$server."</target>".
1492       "</xml>";
1493     
1494     $res = $this->_send($xml_msg,TRUE);
1495     
1496     /* Check if there are results for POLICY 
1497      */
1498     if(isset($res['XML']['POLICY'])){
1499       
1500       /* Ensure that we return an array 
1501        */
1502       $tmp = $res['XML']['POLICY'];
1503       if(!is_array($tmp)){
1504         $tmp = array($tmp);
1505       }
1506       return($tmp);
1507     }else{
1508       return(array());
1509     }
1510   }
1513   /*! \brief  Returns a list of configured password policies.
1514               (Uses the GOsa support daemon instead of the ldap database.)
1515       @return Array The policy settings for the given policy name.
1516    */
1517   public function krb5_get_policy($server,$name)
1518   {
1519     $ret = array();  
1521     /* Check if the given name is a valid request value 
1522      */
1523     if(!is_string($name) || empty($name)){
1524       trigger_error("The given policy name is not of type string or it is empty.");
1525       return($ret);
1526     }
1528     /* Check if the given server is a valid mac address
1529      */
1530     if(!tests::is_mac($server)){
1531       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1532       return($ret);
1533     }
1535     /* Prepare request event 
1536      */ 
1537     $xml_msg = 
1538       "<xml>".
1539       "<header>gosa_krb5_get_policy</header>".
1540       "<policy>".$name."</policy>".
1541       "<source>GOSA</source>".
1542       "<target>".$server."</target>".
1543       "</xml>";
1545     /* Possible attributes */
1546     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1547         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1549   
1550     $tmp = $this->_send($xml_msg,TRUE);
1551     if(isset($tmp['XML'])){
1552       foreach($attrs as $attr){
1553         if(isset($tmp['XML'][$attr])){
1554           $ret[$attr] = $tmp['XML'][$attr];
1555         }else{
1556           $ret[$attr] = "";
1557         }
1558       }
1559     }
1560     return($ret);
1561   }
1563   
1564   /*! \brief  Creates a new policy with a given set of configuration settings.
1565               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1566               (Uses the GOsa support daemon instead of the ldap database.)
1567       @pram   String The name of the policy to update.
1568       @pram   Array  The attributes to update
1569       @return Boolean   TRUE on success else FALSE. 
1570    */
1571   public function krb5_add_policy($server,$name,$values)
1572   {
1573     $ret = FALSE;  
1575     /* Check if the given name is a valid request value 
1576      */
1577     if(!is_string($name) || empty($name)){
1578       trigger_error("The given policy name is not of type string or it is empty.");
1579       return($ret);
1580     }
1581     if(!is_array($values) || !count($values)){
1582       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1583       return($ret);
1584     }
1586     /* Check if the given server is a valid mac address
1587      */
1588     if(!tests::is_mac($server)){
1589       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1590       return($ret);
1591     }
1594     /* Transform array into <xml>
1595      */
1596     $attrs = "";
1597     foreach($values as $id => $value){
1598       if(empty($id) || is_numeric($id)){
1599         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1600         return($ret);
1601       }
1602       $id = strtolower($id);
1603       $attrs.= "<$id>$value</$id>\n";
1604     }
1606     /* Prepare request event 
1607      */ 
1608     $xml_msg = 
1609       "<xml>".
1610       "<header>gosa_krb5_create_policy</header>".
1611       "<policy>".$name."</policy>".
1612       $attrs.
1613       "<source>GOSA</source>".
1614       "<target>".$server."</target>".
1615       "</xml>";
1617     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1618   }
1621   /*! \brief  Updates a given policy with a set of configuration settings.
1622               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1623               (Uses the GOsa support daemon instead of the ldap database.)
1624       @pram   String The name of the policy to update.
1625       @return Boolean   TRUE on success else FALSE. 
1626    */
1627   public function krb5_set_policy($server,$name,$values)
1628   {
1629     $ret = FALSE;  
1631     /* Check if the given name is a valid request value 
1632      */
1633     if(!is_string($name) || empty($name)){
1634       trigger_error("The given policy name is not of type string or it is empty.");
1635       return($ret);
1636     }
1637     if(!is_array($values) || !count($values)){
1638       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1639       return($ret);
1640     }
1642     /* Check if the given server is a valid mac address
1643      */
1644     if(!tests::is_mac($server)){
1645       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1646       return($ret);
1647     }
1649     /* Transform array into <xml>
1650      */
1651     $attrs = "";
1652     foreach($values as $id => $value){
1653       if(preg_match("/^policy$/i",$id)) continue;
1654       if(empty($id) || is_numeric($id)){
1655         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1656         return($ret);
1657       }
1658       $id = strtolower($id);
1659       $attrs.= "<$id>$value</$id>\n";
1660     }
1662     /* Prepare request event 
1663      */ 
1664     $xml_msg = 
1665       "<xml>".
1666       "<header>gosa_krb5_modify_policy</header>".
1667       "<policy>".$name."</policy>".
1668       $attrs.
1669       "<source>GOSA</source>".
1670       "<target>".$server."</target>".
1671       "</xml>";
1673     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1674   }
1676   
1677   /*! \brief  Removes the given password policy. 
1678               (Uses the GOsa support daemon instead of the ldap database.)
1679       @return Boolean  TRUE on success else FALSE
1680    */
1681   public function krb5_del_policy($server,$name)
1682   {
1683     $ret = FALSE;
1685     /* Check if the given server is a valid mac address
1686      */
1687     if(!tests::is_mac($server)){
1688       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1689       return($ret);
1690     }
1692     /* Check if the given name is a valid request value 
1693      */
1694     if(!is_string($name) || empty($name)){
1695       trigger_error("The given policy name is not of type string or it is empty.");
1696       return($ret);
1697     }
1699     /* Prepare request event 
1700      */ 
1701     $xml_msg = 
1702       "<xml>".
1703       "<header>gosa_krb5_del_policy</header>".
1704       "<policy>".$name."</policy>".
1705       "<source>GOSA</source>".
1706       "<target>".$server."</target>".
1707       "</xml>";
1708     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1709   }
1712   /*! \brief  Sets the password of for the given principal.
1713               (Uses the GOsa support daemon instead of the ldap database.)
1714       @param  String  The servers mac
1715       @param  String  The principals name
1716       @param  String  $the new password.   
1717       @return Boolean  TRUE on success else FALSE
1718    */
1719   public function krb5_set_password($server,$name,$password)
1720   {
1721     $ret = FALSE;
1723     /* Check if the given server is a valid mac address
1724      */
1725     if(!tests::is_mac($server)){
1726       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1727       return($ret);
1728     }
1730     /* Check if the given name is a valid request value
1731      */
1732     if(!is_string($name) || empty($name)){
1733       trigger_error("The given principal name is not of type string or it is empty.");
1734       return($ret);
1735     }
1737     /* Prepare request event
1738      */
1739     $xml_msg =
1740       "<xml>".
1741       "<header>gosa_krb5_set_password</header>".
1742       "<principal>".$name."</principal>".
1743       "<password>".$password."</password>".
1744       "<source>GOSA</source>".
1745       "<target>".$server."</target>".
1746       "</xml>";
1747     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1748   }
1751   /*! \brief  Returns log file informations for a given mac address 
1752       @param  $mac The mac address to fetch logs for.
1753       @retrun Array A Multidimensional array containing log infos.
1754         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1755         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1756                                install_20080313_144450   ...
1757    */
1758   public function get_log_info_for_mac($mac)
1759   {
1760     $xml_msg = "
1761       <xml>
1762       <header>gosa_show_log_by_mac</header>
1763       <target>GOSA</target>
1764       <source>GOSA</source>
1765       <mac>".$mac."</mac>
1766       </xml>";
1768     $res = $this->_send($xml_msg,TRUE);
1769     $ret = array();
1770     if(isset($res['XML'])){
1772       /* Filter all entry that look like this 
1773           MAC_00_01_6C_9D_B9_FA
1774        */
1775       foreach($res['XML'] as $name => $entry){
1776         if(preg_match("/^MAC/",$name)){
1778           /* Get list of available log files 
1779            */
1780           if(!is_array($entry)){
1781             $entry = array($entry);
1782           }
1783           foreach($entry as $log_date){
1784             $xml_msg2 = "<xml> 
1785               <header>gosa_show_log_files_by_date_and_mac</header> 
1786               <target>GOSA</target> 
1787               <source>GOSA</source>                        
1788               <date>".$log_date."</date> 
1789               <mac>".$mac."</mac> 
1790               </xml>";
1791    
1792             $ret[$mac][$log_date] = array();
1793             $res = $this->_send($xml_msg2,TRUE);
1794             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1795             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1796             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1797               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1798             }
1799           }
1800         }
1801       }
1802     }
1803     return($ret);
1804   }
1806   public function get_log_file($mac,$date,$file)
1807   {
1808     $xml_msg ="
1809       <xml> 
1810       <header>gosa_get_log_file_by_date_and_mac</header> 
1811       <target>GOSA</target> 
1812       <source>GOSA</source>
1813       <date>".$date."</date> 
1814       <mac>".$mac."</mac> 
1815       <log_file>".$file."</log_file>
1816       </xml>";
1818     $res = $this->_send($xml_msg,TRUE);
1819     if(isset($res['XML'][strtoupper($file)])){
1820       return(base64_decode($res['XML'][strtoupper($file)]));
1821     }
1822     return("");
1823   }
1829   /*****************
1830    * DAK - Functions 
1831    *****************/
1833   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1834       @param  ...
1835       @return Array   All queued entries.
1836    */
1837   public function DAK_keyring_entries($server)  
1838   {
1839     /* Ensure that we send the event to a valid mac address 
1840      */
1841     if(!is_string($server) || !tests::is_mac($server)){
1842       trigger_error("No valid mac address given '".$server."'.");
1843       return;
1844     }
1846     /* Create query
1847      */
1848     $xml_msg = "<xml> 
1849                   <header>gosa_get_dak_keyring</header> 
1850                   <target>".$server."</target> 
1851                   <source>GOSA</source>
1852                 </xml>";
1853         
1854     $res = $this->_send($xml_msg,TRUE);
1856     /* Check if there are results for POLICY
1857      */
1858     if(isset($res['XML'])){
1859       $ret = array();
1860       foreach($res['XML'] as $key => $entry){
1861         if(preg_match("/^ANSWER/",$key)){
1862           $ret[] = $entry;
1863         }
1864       }
1865       return($ret);
1866     }else{
1867       return(array());
1868     }
1869   }
1872   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1873       @param  String  The servers mac address 
1874       @param  String  The gpg key.
1875       @return Boolean TRUE on success else FALSE 
1876    */
1877   public function DAK_import_key($server,$key)  
1878   {
1879     /* Ensure that we send the event to a valid mac address 
1880      */
1881     if(!is_string($server) || !tests::is_mac($server)){
1882       trigger_error("No valid mac address given '".$server."'.");
1883       return;
1884     }
1886     /* Check if there is some cleanup required before importing the key.
1887         There may be some Header lines like:
1888         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1889      */
1890     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1892       /* Remove header */
1893       $key = preg_replace("/^.*\n\n/sim","",$key);
1894       /* Remove footer */
1895       $key = preg_replace("/-----.*$/sim","",$key);
1896     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1897       
1898       /* Encode key if it is raw.
1899        */
1900       $key = base64_encode($key);
1901     }
1903     /* Create query
1904      */
1905     $xml_msg = "<xml> 
1906                   <header>gosa_import_dak_key</header> 
1907                   <target>".$server."</target> 
1908                   <key>".$key."</key> 
1909                   <source>GOSA</source>
1910                 </xml>";
1911         
1912     $res = $this->_send($xml_msg,TRUE);
1913     return($this->is_error());
1914   }
1917   /*! \brief Removes a key from the keyring on the given server. 
1918       @param  String  The servers mac address 
1919       @param  String  The gpg key uid.
1920       @return Boolean TRUE on success else FALSE 
1921    */
1922   public function DAK_remove_key($server,$key)  
1923   {
1924     /* Ensure that we send the event to a valid mac address 
1925      */
1926     if(!is_string($server) || !tests::is_mac($server)){
1927       trigger_error("No valid mac address given '".$server."'.");
1928       return;
1929     }
1931     /* Create query
1932      */
1933     $xml_msg = "<xml> 
1934                   <header>gosa_remove_dak_key</header> 
1935                   <target>".$server."</target> 
1936                   <keyid>".$key."</keyid> 
1937                   <source>GOSA</source>
1938                 </xml>";
1939        
1940     $res = $this->_send($xml_msg,TRUE);
1941     return($this->is_error());
1942   }
1945 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1946 ?>