Code

Made support daemon timeout configurable.
[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         foreach($value as $sub_value){
846           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
847         }
848       }else{
849         $attr.= "<$key>".strtolower($value)."</$key>\n";
850       }
851     }
853     $xml_msg = "<xml>
854       <header>gosa_update_status_jobdb_entry</header>
855       <target>GOSA</target>
856       <source>GOSA</source>
857       <where>
858       <clause>
859       <connector>or</connector>";
860     foreach($ids as $id){
861       $xml_msg .= "<phrase>
862         <operator>eq</operator>
863         <id>".$id."</id>
864         </phrase>";
865     }
866     $xml_msg .= "</clause>
867       </where>
868       <update>
869       ".$attr." 
870       </update>
871       </xml>";
873     if($this->connect()){
874       $entries = $this->_send($xml_msg,TRUE);
875       if(isset($entries['XML'])){
876         if(isset($entries['XML']['ERROR_STRING'])) {
877           $this->set_error($entries['XML']['ERROR_STRING']);
878           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
879           return(FALSE);
880         }
881         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
882         return(TRUE);
883       }
884     }
885     return(FALSE);
886   }
889   /*! \brief  Returns the number of currently queued objects.
890       @return Integer  
891    */
892   public function number_of_queued_entries($event_types)
893   {
894     $tags = "";
895     foreach($event_types as $type){
896       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
897     }
898     if(count($event_types) > 1){
899       $tags = "<connector>or</connector>".$tags;
900     }
901     if(count($event_types)){
902       $tags = "<where><clause>".$tags."</clause></where>";
903     }
906     $xml_msg =
907       "<xml>".
908       "<header>gosa_query_jobdb</header>".
909       "<target>GOSA</target>".
910       "<source>GOSA</source>".
911       "<select> count ID</select>".
912       $tags.
913       "</xml>";
915     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
916     $this->connect();
917     if($this->connect()){
918       $entries = $this->_send($xml_msg,TRUE);
919       if($this->o_sock->is_error()){
920         $this->set_error($this->o_sock->get_error());
921         return(0);
922       }
923       if(isset($entries['XML'])){
924         return($entries['XML']['COUNT']);
925       }
926     }
927     return(-1);
928   } 
931   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
932   {
933     $xml_message= "";
935     /* Prepare data */
936     foreach ($data as $key => $value){
937       if(is_array($value)){
938         foreach($value as $sub_value){
939           $xml_message.= "<$key>$sub_value</$key>";
940         }
941       }else{
942         $xml_message.= "<$key>$value</$key>";
943       }
944     }
946     /* Multiple targets? */
947     if (!is_array($to)){
948       $to_targets= array($to);
949     } else {
950       $to_targets= $to;
951     }
953     /* Build target strings */
954     $target ="";
955     foreach($to_targets as $to){
956       $target.= "<target>$to</target>";
957     }
959     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
960   }
963   /* Allows simply appending a new DaemonEvent 
964    */
965   public function append($event, $skip_add_mac = FALSE)
966   {
967     if(!($event instanceof DaemonEvent)){
968       return(FALSE);
969     }
970   
972     /* Add to queue if new 
973      */
974     if($event->is_new()){
976       $request_answer = FALSE;
977       if($event->get_type() == SCHEDULED_EVENT){
978         $action = $event->get_schedule_action();
979       }elseif($event->get_type() == TRIGGERED_EVENT){
980         $action = $event->get_trigger_action();
981       }else{
982         trigger_error("Unknown type of queue event given.");
983         return(FALSE);
984       }
986       /* Get event informations, like targets..
987        */
988       $targets    = $event->get_targets();
989       $data       = $event->save();
991       /* Append an entry for each target 
992        */
993       foreach($targets as $target){
994         if(!$skip_add_mac){
995           $data['macaddress'] = $target;
996         }
997         $this->send_data($action,$target,$data,$request_answer);
999         if($this->is_error()){
1000           return(FALSE);
1001         }
1002       }
1003       return(TRUE);
1004     }else{
1006       /* Updated edited entry.
1007        */
1008       $id                 = $event->get_id();
1009       $data               = $event->save();
1010       return($this->update_entries(array($id),$data));
1011     }
1013     return(FALSE);
1014   }
1017   /*! \brief  Returns an array containing all queued entries.
1018     @return Array All queued entries as an array.
1019    */
1020   public function _send($data, $answer_expected= FALSE)
1021   {
1023     $ret = array();
1024     if(!$this->connect()){
1025       return($ret);
1026     }
1027   
1028     $this->reset_error();
1030     /******
1031       Debug handling
1032      ******/
1033     $debug = debug_backtrace();
1034     $file = __FILE__;
1035     $function = __FUNCTION__;
1036     $line = __LINE__;
1037     $class = __CLASS__;
1038     foreach($debug as $info){
1039       if(!in_array($info['function'],array("send_data","_send"))){
1040         $file = $info['file'];
1041         $line = $info['line'];
1042         $class = get_class($this);
1043         $function = $info['function'];
1044         break;
1045       }
1046     }
1047     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1050     /*******
1051       Start sending data 
1052      *******/
1053     if($this->connect()){
1054       $this->o_sock->write($data);
1055       if ($answer_expected){
1056         $str = trim($this->o_sock->read());
1058         /* Check if something went wrong while reading */
1059         if($this->o_sock->is_error()){
1060           $this->set_error($this->o_sock->get_error());
1061           return($ret);
1062         }
1064         $entries = $this->xml_to_array($str);
1065         if(isset($entries['XML']) && is_array($entries['XML'])){
1066           $ret = $entries;
1067           if($this->use_alternative_xml_parse_method) {
1068             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1069               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1070               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1071                   array($data=>$data),"FAILED ".$this->get_error());
1072             }
1073           }else{
1074             if(isset($entries['XML']['ERROR_STRING'])) {
1075               $this->set_error($entries['XML']['ERROR_STRING']);
1076               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1077                   array($data=>$data),"FAILED ".$this->get_error());
1078             }elseif(isset($entries['XML']['ERROR'])){
1079               $this->set_error($entries['XML']['ERROR']);
1080               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1081                   array($data=>$data),"FAILED ".$this->get_error());
1082             }
1083           }
1084           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1085               array($data=>$data),"SUCCESS");
1086         }
1087       }else{
1088         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1089             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1090       }
1091     }
1092     return($ret);
1093   }
1096   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1097   {
1098     $xml_message= "";
1100     /* Get communication object */
1101     $d= new gosaSupportDaemon(TRUE,10);
1103     /* Prepare data */
1104     foreach ($data as $key => $value){
1105       if(is_array($value)){
1106         foreach($value as $sub_val){
1107           $xml_message.= "<$key>$sub_val</$key>";
1108         }
1109       }else{
1110         $xml_message.= "<$key>$value</$key>";
1111       }
1112     }
1114     /* Multiple targets? */
1115     if (!is_array($to)){
1116       $to_targets= array($to);
1117     } else {
1118       $to_targets= $to;
1119     }
1121     /* Build target strings */
1122     $target ="";
1123     foreach($to_targets as $to){
1124       $target.= "<target>$to</target>";
1125     }
1127     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1128   }
1131   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1132       @param  String  $mac  The mac address for which we want to remove all jobs.      
1133    */
1134   function clean_queue_from_mac($mac)
1135   {
1136     global $config;
1138     /* First of all we have to check which jobs are startet 
1139      *  for $mac 
1140      */
1141     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1142     
1143     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1144  
1145     $data = $this->_send($xml_msg,TRUE);
1146     if(is_array($data) && isset($data['XML'])){
1147       $already_aborted = FALSE;
1148       foreach($data['XML']  as $name => $entry){
1149         if(preg_match("/answer[0-9]*/i",$name)){
1150           $entry['STATUS'] = strtoupper($entry['STATUS']);
1151           switch($entry['STATUS']){
1153             case 'PROCESSING' :
1155               /* Send abort event, but only once 
1156                */
1157               if($already_aborted){
1158                 break;
1159               }elseif(class_available("DaemonEvent_faireboot")){
1160                 $already_aborted = TRUE;
1161                 $tmp = new DaemonEvent_faireboot($config);
1162                 $tmp->add_targets(array($mac));
1163                 $tmp->set_type(TRIGGERED_EVENT);
1164                 if(!$this->append($tmp)){
1165                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1166                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1167                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1168                 }else{
1169                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1170                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1171                 }
1172                 ;break;
1173               }else{
1174                 /* Couldn't find abort event, just remove entry */
1175               }
1177             case 'WAITING':
1178             case 'ERROR':
1179             default :
1180             
1181               /* Simply remove entries from queue. 
1182                *  Failed or waiting events, can be removed without any trouble.
1183                */ 
1184               if(!$this->remove_entries(array($entry['ID']))){
1185                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1186               }
1187               ;break;
1188           }
1189     
1190         }
1191       }
1192     }
1193   }
1196   static function ping($target)
1197   {
1198     if (tests::is_mac($target)){
1199       /* Get communication object */
1200       $d= new gosaSupportDaemon(TRUE,2);
1201       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1202       return (count($answer) ? TRUE:FALSE);
1203     }
1204     return (FALSE);
1205   }
1209   /*! \brief  Returns a list of all configured principals. 
1210               (Uses the GOsa support daemon instead of the ldap database.)
1211       @return Array  A list containing the names of all configured principals.
1212    */
1213   public function krb5_list_principals($server)
1214   {
1215     $res = array();  
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_list_principals</header>".
1229       "<source>GOSA</source>".
1230       "<target>".$server."</target>".
1231       "</xml>";
1232     
1233     $tmp = $this->_send($xml_msg,TRUE);
1234     if(isset($tmp['XML']['PRINCIPAL'])){
1235       return($tmp['XML']['PRINCIPAL']);
1236     }else{
1237       return($res);
1238     }
1239   }
1242   /*! \brief  Returns the configuration settings for a given principal name. 
1243               (Uses the GOsa support daemon instead of the ldap database.)
1244       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1245       @return Array  A list containing the names of all configured principals.
1246    */
1247   public function krb5_get_principal($server,$name)
1248   {
1249     $ret = array();
1251     /* Check if the given name is a valid request value 
1252      */
1253     if(!is_string($name) || empty($name)){
1254       trigger_error("The given principal name is not of type string or it is empty.");
1255       return($ret);
1256     }
1258     /* Check if the given server is a valid mac address
1259      */
1260     if(!tests::is_mac($server)){
1261       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1262       return($ret);
1263     }
1265     /* Prepare request event 
1266      */ 
1267     $xml_msg = 
1268       "<xml>".
1269       "<header>gosa_krb5_get_principal</header>".
1270       "<principal>".$name."</principal>".
1271       "<source>GOSA</source>".
1272       "<target>".$server."</target>".
1273       "</xml>";
1275     $res = $this->_send($xml_msg,TRUE);
1276     if(isset($res['XML'])){
1277       return($res['XML']);
1278     }else{
1279       return($ret);
1280     }
1281   }
1284   /*! \brief  Creates a given principal with a set of configuration settings.
1285               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1286               (Uses the GOsa support daemon instead of the ldap database.)
1287       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1288       @return Boolean   TRUE on success else FALSE. 
1289    */
1290   public function krb5_add_principal($server,$name,$values)
1291   {
1292     $ret = FALSE;  
1294     /* Check if the given name is a valid request value 
1295      */
1296     if(!is_string($name) || empty($name)){
1297       trigger_error("The given principal name is not of type string or it is empty.");
1298       return($ret);
1299     }
1300     if(!is_array($values)){
1301       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1302       return($ret);
1303     }
1305     /* Check if the given server is a valid mac address
1306      */
1307     if(!tests::is_mac($server)){
1308       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1309       return($ret);
1310     }
1312     $attrs = "";
1313     foreach($values as $key => $value){
1314       if(empty($key) || is_numeric($key)){
1315         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1316         return($ret);
1317       }
1318       $key = strtolower($key);
1319       if(is_array($value)){
1320         foreach($value as $val){
1321           $attrs.= "<$key>$val</$key>\n";
1322         }
1323       }else{
1324         $attrs.= "<$key>$value</$key>\n";
1325       }
1326     }
1328     /* Prepare request event 
1329      */ 
1330     $xml_msg = 
1331       "<xml>".
1332       "<header>gosa_krb5_create_principal</header>".
1333       "<principal>".$name."</principal>".
1334       $attrs.
1335       "<source>GOSA</source>".
1336       "<target>".$server."</target>".
1337       "</xml>";
1339     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1340   }
1343   function krb5_ramdomize_key($server,$name)  
1344   {
1345     /* Prepare request event 
1346      */ 
1347     $xml_msg = 
1348       "<xml>".
1349       "<header>gosa_krb5_randomize_key</header>".
1350       "<principal>".$name."</principal>".
1351       "<source>GOSA</source>".
1352       "<target>".$server."</target>".
1353       "</xml>";
1355     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1356   }
1357   
1360   /*! \brief  Updates a given principal with a set of configuration settings.
1361               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1362               (Uses the GOsa support daemon instead of the ldap database.)
1363       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1364       @return Boolean   TRUE on success else FALSE. 
1365    */
1366   public function krb5_set_principal($server,$name,$values)
1367   {
1368     $ret = FALSE;  
1370     /* Check if the given name is a valid request value 
1371      */
1372     if(!is_string($name) || empty($name)){
1373       trigger_error("The given principal name is not of type string or it is empty.");
1374       return($ret);
1375     }
1376     if(!is_array($values) || !count($values)){
1377       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1378       return($ret);
1379     }
1381     /* Check if the given server is a valid mac address
1382      */
1383     if(!tests::is_mac($server)){
1384       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1385       return($ret);
1386     }
1388     $attrs = "";
1389     foreach($values as $key => $value){
1390       if(empty($key) || is_numeric($key)){
1391         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1392         return($ret);
1393       }
1394       $key = strtolower($key);
1395       if(is_array($value)){
1396         foreach($value as $val){
1397           $attrs.= "<$key>$val</$key>\n";
1398         }
1399       }else{
1400         $attrs.= "<$key>$value</$key>\n";
1401       }
1402     }
1404     /* Prepare request event 
1405      */ 
1406     $xml_msg = 
1407       "<xml>".
1408       "<header>gosa_krb5_modify_principal</header>".
1409       "<principal>".$name."</principal>".
1410       $attrs.
1411       "<source>GOSA</source>".
1412       "<target>".$server."</target>".
1413       "</xml>";
1415     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1416   }
1419   /*! \brief  Removes the given principal.
1420               (Uses the GOsa support daemon instead of the ldap database.)
1421       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1422       @return Boollean   TRUE on success else FALSE
1423    */
1424   public function krb5_del_principal($server,$name)
1425   {
1426     $ret = FALSE;  
1428     /* Check if the given name is a valid request value 
1429      */
1430     if(!is_string($name) || empty($name)){
1431       trigger_error("The given principal name is not of type string or it is empty.");
1432       return($ret);
1433     }
1435     /* Check if the given server is a valid mac address
1436      */
1437     if(!tests::is_mac($server)){
1438       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1439       return($ret);
1440     }
1442     /* Prepare request event 
1443      */ 
1444     $xml_msg = 
1445       "<xml>".
1446       "<header>gosa_krb5_del_principal</header>".
1447       "<principal>".$name."</principal>".
1448       "<source>GOSA</source>".
1449       "<target>".$server."</target>".
1450       "</xml>";
1451     
1452     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1453   }
1456   /*! \brief  Returns a list of configured password policies.
1457               (Uses the GOsa support daemon instead of the ldap database.)
1458       @return Array A list of all configured password policies.
1459    */
1460   public function krb5_list_policies($server)
1461   {
1462     $res = array();  
1464     /* Check if the given server is a valid mac address
1465      */
1466     if(!tests::is_mac($server)){
1467       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1468       return($ret);
1469     }
1471     /* Prepare request event 
1472      */ 
1473     $xml_msg = 
1474       "<xml>".
1475       "<header>gosa_krb5_list_policies</header>".
1476       "<source>GOSA</source>".
1477       "<target>".$server."</target>".
1478       "</xml>";
1479     
1480     $res = $this->_send($xml_msg,TRUE);
1481     
1482     /* Check if there are results for POLICY 
1483      */
1484     if(isset($res['XML']['POLICY'])){
1485       
1486       /* Ensure that we return an array 
1487        */
1488       $tmp = $res['XML']['POLICY'];
1489       if(!is_array($tmp)){
1490         $tmp = array($tmp);
1491       }
1492       return($tmp);
1493     }else{
1494       return(array());
1495     }
1496   }
1499   /*! \brief  Returns a list of configured password policies.
1500               (Uses the GOsa support daemon instead of the ldap database.)
1501       @return Array The policy settings for the given policy name.
1502    */
1503   public function krb5_get_policy($server,$name)
1504   {
1505     $ret = array();  
1507     /* Check if the given name is a valid request value 
1508      */
1509     if(!is_string($name) || empty($name)){
1510       trigger_error("The given policy name is not of type string or it is empty.");
1511       return($ret);
1512     }
1514     /* Check if the given server is a valid mac address
1515      */
1516     if(!tests::is_mac($server)){
1517       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1518       return($ret);
1519     }
1521     /* Prepare request event 
1522      */ 
1523     $xml_msg = 
1524       "<xml>".
1525       "<header>gosa_krb5_get_policy</header>".
1526       "<policy>".$name."</policy>".
1527       "<source>GOSA</source>".
1528       "<target>".$server."</target>".
1529       "</xml>";
1531     /* Possible attributes */
1532     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1533         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1535   
1536     $tmp = $this->_send($xml_msg,TRUE);
1537     if(isset($tmp['XML'])){
1538       foreach($attrs as $attr){
1539         if(isset($tmp['XML'][$attr])){
1540           $ret[$attr] = $tmp['XML'][$attr];
1541         }else{
1542           $ret[$attr] = "";
1543         }
1544       }
1545     }
1546     return($ret);
1547   }
1549   
1550   /*! \brief  Creates a new policy with a given set of configuration settings.
1551               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1552               (Uses the GOsa support daemon instead of the ldap database.)
1553       @pram   String The name of the policy to update.
1554       @pram   Array  The attributes to update
1555       @return Boolean   TRUE on success else FALSE. 
1556    */
1557   public function krb5_add_policy($server,$name,$values)
1558   {
1559     $ret = FALSE;  
1561     /* Check if the given name is a valid request value 
1562      */
1563     if(!is_string($name) || empty($name)){
1564       trigger_error("The given policy name is not of type string or it is empty.");
1565       return($ret);
1566     }
1567     if(!is_array($values) || !count($values)){
1568       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1569       return($ret);
1570     }
1572     /* Check if the given server is a valid mac address
1573      */
1574     if(!tests::is_mac($server)){
1575       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1576       return($ret);
1577     }
1580     /* Transform array into <xml>
1581      */
1582     $attrs = "";
1583     foreach($values as $id => $value){
1584       if(empty($id) || is_numeric($id)){
1585         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1586         return($ret);
1587       }
1588       $id = strtolower($id);
1589       $attrs.= "<$id>$value</$id>\n";
1590     }
1592     /* Prepare request event 
1593      */ 
1594     $xml_msg = 
1595       "<xml>".
1596       "<header>gosa_krb5_create_policy</header>".
1597       "<policy>".$name."</policy>".
1598       $attrs.
1599       "<source>GOSA</source>".
1600       "<target>".$server."</target>".
1601       "</xml>";
1603     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1604   }
1607   /*! \brief  Updates a given policy with a set of configuration settings.
1608               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1609               (Uses the GOsa support daemon instead of the ldap database.)
1610       @pram   String The name of the policy to update.
1611       @return Boolean   TRUE on success else FALSE. 
1612    */
1613   public function krb5_set_policy($server,$name,$values)
1614   {
1615     $ret = FALSE;  
1617     /* Check if the given name is a valid request value 
1618      */
1619     if(!is_string($name) || empty($name)){
1620       trigger_error("The given policy name is not of type string or it is empty.");
1621       return($ret);
1622     }
1623     if(!is_array($values) || !count($values)){
1624       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1625       return($ret);
1626     }
1628     /* Check if the given server is a valid mac address
1629      */
1630     if(!tests::is_mac($server)){
1631       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1632       return($ret);
1633     }
1635     /* Transform array into <xml>
1636      */
1637     $attrs = "";
1638     foreach($values as $id => $value){
1639       if(preg_match("/^policy$/i",$id)) continue;
1640       if(empty($id) || is_numeric($id)){
1641         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1642         return($ret);
1643       }
1644       $id = strtolower($id);
1645       $attrs.= "<$id>$value</$id>\n";
1646     }
1648     /* Prepare request event 
1649      */ 
1650     $xml_msg = 
1651       "<xml>".
1652       "<header>gosa_krb5_modify_policy</header>".
1653       "<policy>".$name."</policy>".
1654       $attrs.
1655       "<source>GOSA</source>".
1656       "<target>".$server."</target>".
1657       "</xml>";
1659     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1660   }
1662   
1663   /*! \brief  Removes the given password policy. 
1664               (Uses the GOsa support daemon instead of the ldap database.)
1665       @return Boolean  TRUE on success else FALSE
1666    */
1667   public function krb5_del_policy($server,$name)
1668   {
1669     $ret = FALSE;
1671     /* Check if the given server is a valid mac address
1672      */
1673     if(!tests::is_mac($server)){
1674       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1675       return($ret);
1676     }
1678     /* Check if the given name is a valid request value 
1679      */
1680     if(!is_string($name) || empty($name)){
1681       trigger_error("The given policy name is not of type string or it is empty.");
1682       return($ret);
1683     }
1685     /* Prepare request event 
1686      */ 
1687     $xml_msg = 
1688       "<xml>".
1689       "<header>gosa_krb5_del_policy</header>".
1690       "<policy>".$name."</policy>".
1691       "<source>GOSA</source>".
1692       "<target>".$server."</target>".
1693       "</xml>";
1694     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1695   }
1698   /*! \brief  Sets the password of for the given principal.
1699               (Uses the GOsa support daemon instead of the ldap database.)
1700       @param  String  The servers mac
1701       @param  String  The principals name
1702       @param  String  $the new password.   
1703       @return Boolean  TRUE on success else FALSE
1704    */
1705   public function krb5_set_password($server,$name,$password)
1706   {
1707     $ret = FALSE;
1709     /* Check if the given server is a valid mac address
1710      */
1711     if(!tests::is_mac($server)){
1712       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1713       return($ret);
1714     }
1716     /* Check if the given name is a valid request value
1717      */
1718     if(!is_string($name) || empty($name)){
1719       trigger_error("The given principal name is not of type string or it is empty.");
1720       return($ret);
1721     }
1723     /* Prepare request event
1724      */
1725     $xml_msg =
1726       "<xml>".
1727       "<header>gosa_krb5_set_password</header>".
1728       "<principal>".$name."</principal>".
1729       "<password>".$password."</password>".
1730       "<source>GOSA</source>".
1731       "<target>".$server."</target>".
1732       "</xml>";
1733     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1734   }
1737   /*! \brief  Returns log file informations for a given mac address 
1738       @param  $mac The mac address to fetch logs for.
1739       @retrun Array A Multidimensional array containing log infos.
1740         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1741         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1742                                install_20080313_144450   ...
1743    */
1744   public function get_log_info_for_mac($mac)
1745   {
1746     $xml_msg = "
1747       <xml>
1748       <header>gosa_show_log_by_mac</header>
1749       <target>GOSA</target>
1750       <source>GOSA</source>
1751       <mac>".$mac."</mac>
1752       </xml>";
1754     $res = $this->_send($xml_msg,TRUE);
1755     $ret = array();
1756     if(isset($res['XML'])){
1758       /* Filter all entry that look like this 
1759           MAC_00_01_6C_9D_B9_FA
1760        */
1761       foreach($res['XML'] as $name => $entry){
1762         if(preg_match("/^MAC/",$name)){
1764           /* Get list of available log files 
1765            */
1766           if(!is_array($entry)){
1767             $entry = array($entry);
1768           }
1769           foreach($entry as $log_date){
1770             $xml_msg2 = "<xml> 
1771               <header>gosa_show_log_files_by_date_and_mac</header> 
1772               <target>GOSA</target> 
1773               <source>GOSA</source>                        
1774               <date>".$log_date."</date> 
1775               <mac>".$mac."</mac> 
1776               </xml>";
1777    
1778             $ret[$mac][$log_date] = array();
1779             $res = $this->_send($xml_msg2,TRUE);
1780             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1781             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1782             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1783               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1784             }
1785           }
1786         }
1787       }
1788     }
1789     return($ret);
1790   }
1792   public function get_log_file($mac,$date,$file)
1793   {
1794     $xml_msg ="
1795       <xml> 
1796       <header>gosa_get_log_file_by_date_and_mac</header> 
1797       <target>GOSA</target> 
1798       <source>GOSA</source>
1799       <date>".$date."</date> 
1800       <mac>".$mac."</mac> 
1801       <log_file>".$file."</log_file>
1802       </xml>";
1804     $res = $this->_send($xml_msg,TRUE);
1805     if(isset($res['XML'][strtoupper($file)])){
1806       return(base64_decode($res['XML'][strtoupper($file)]));
1807     }
1808     return("");
1809   }
1815   /*****************
1816    * DAK - Functions 
1817    *****************/
1819   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1820       @param  ...
1821       @return Array   All queued entries.
1822    */
1823   public function DAK_keyring_entries($server)  
1824   {
1825     /* Ensure that we send the event to a valid mac address 
1826      */
1827     if(!is_string($server) || !tests::is_mac($server)){
1828       trigger_error("No valid mac address given '".$server."'.");
1829       return;
1830     }
1832     /* Create query
1833      */
1834     $xml_msg = "<xml> 
1835                   <header>gosa_get_dak_keyring</header> 
1836                   <target>".$server."</target> 
1837                   <source>GOSA</source>
1838                 </xml>";
1839         
1840     $res = $this->_send($xml_msg,TRUE);
1842     /* Check if there are results for POLICY
1843      */
1844     if(isset($res['XML'])){
1845       $ret = array();
1846       foreach($res['XML'] as $key => $entry){
1847         if(preg_match("/^ANSWER/",$key)){
1848           $ret[] = $entry;
1849         }
1850       }
1851       return($ret);
1852     }else{
1853       return(array());
1854     }
1855   }
1858   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1859       @param  String  The servers mac address 
1860       @param  String  The gpg key.
1861       @return Boolean TRUE on success else FALSE 
1862    */
1863   public function DAK_import_key($server,$key)  
1864   {
1865     /* Ensure that we send the event to a valid mac address 
1866      */
1867     if(!is_string($server) || !tests::is_mac($server)){
1868       trigger_error("No valid mac address given '".$server."'.");
1869       return;
1870     }
1872     /* Check if there is some cleanup required before importing the key.
1873         There may be some Header lines like:
1874         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1875      */
1876     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1878       /* Remove header */
1879       $key = preg_replace("/^.*\n\n/sim","",$key);
1880       /* Remove footer */
1881       $key = preg_replace("/-----.*$/sim","",$key);
1882     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1883       
1884       /* Encode key if it is raw.
1885        */
1886       $key = base64_encode($key);
1887     }
1889     /* Create query
1890      */
1891     $xml_msg = "<xml> 
1892                   <header>gosa_import_dak_key</header> 
1893                   <target>".$server."</target> 
1894                   <key>".$key."</key> 
1895                   <source>GOSA</source>
1896                 </xml>";
1897         
1898     $res = $this->_send($xml_msg,TRUE);
1899     return($this->is_error());
1900   }
1903   /*! \brief Removes a key from the keyring on the given server. 
1904       @param  String  The servers mac address 
1905       @param  String  The gpg key uid.
1906       @return Boolean TRUE on success else FALSE 
1907    */
1908   public function DAK_remove_key($server,$key)  
1909   {
1910     /* Ensure that we send the event to a valid mac address 
1911      */
1912     if(!is_string($server) || !tests::is_mac($server)){
1913       trigger_error("No valid mac address given '".$server."'.");
1914       return;
1915     }
1917     /* Create query
1918      */
1919     $xml_msg = "<xml> 
1920                   <header>gosa_remove_dak_key</header> 
1921                   <target>".$server."</target> 
1922                   <keyid>".$key."</keyid> 
1923                   <source>GOSA</source>
1924                 </xml>";
1925        
1926     $res = $this->_send($xml_msg,TRUE);
1927     return($this->is_error());
1928   }
1931 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1932 ?>