Code

Fixed PHP Warnings when creating a new FAIrelease
[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) {
1069             // --------- Seems broken, check for 'ERROR' but using 'ERROR_STRING'
1070             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1071               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1072               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1073                   array($data=>$data),"FAILED ".$this->get_error());
1074             // ---------
1075       
1076           
1077             }elseif(isset($entries['XML'][0]['ERROR'][0]['VALUE'])){
1078               $this->set_error($entries['XML'][0]['ERROR'][0]['VALUE']);
1079               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1080                   array($data=>$data),"FAILED ".$this->get_error());
1081             }
1082           }else{
1083             if(isset($entries['XML']['ERROR_STRING'])) {
1084               $this->set_error($entries['XML']['ERROR_STRING']);
1085               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1086                   array($data=>$data),"FAILED ".$this->get_error());
1087             }elseif(isset($entries['XML']['ERROR'])){
1088               $this->set_error($entries['XML']['ERROR']);
1089               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1090                   array($data=>$data),"FAILED ".$this->get_error());
1091             }
1092           }
1093           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1094               array($data=>$data),"SUCCESS");
1095         }
1096       }else{
1097         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1098             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1099       }
1100     }
1101     return($ret);
1102   }
1105   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1106   {
1107     $xml_message= "";
1109     /* Get communication object */
1110     $d= new gosaSupportDaemon(TRUE,10);
1112     /* Prepare data */
1113     foreach ($data as $key => $value){
1114       if(is_array($value)){
1115         foreach($value as $sub_val){
1116           $xml_message.= "<$key>$sub_val</$key>";
1117         }
1118       }else{
1119         $xml_message.= "<$key>$value</$key>";
1120       }
1121     }
1123     /* Multiple targets? */
1124     if (!is_array($to)){
1125       $to_targets= array($to);
1126     } else {
1127       $to_targets= $to;
1128     }
1130     /* Build target strings */
1131     $target ="";
1132     foreach($to_targets as $to){
1133       $target.= "<target>$to</target>";
1134     }
1136     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1137   }
1140   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1141       @param  String  $mac  The mac address for which we want to remove all jobs.      
1142    */
1143   function clean_queue_from_mac($mac)
1144   {
1145     global $config;
1147     /* First of all we have to check which jobs are startet 
1148      *  for $mac 
1149      */
1150     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1151     
1152     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1153  
1154     $data = $this->_send($xml_msg,TRUE);
1155     if(is_array($data) && isset($data['XML'])){
1156       $already_aborted = FALSE;
1157       foreach($data['XML']  as $name => $entry){
1158         if(preg_match("/answer[0-9]*/i",$name)){
1159           $entry['STATUS'] = strtoupper($entry['STATUS']);
1160           switch($entry['STATUS']){
1162             case 'PROCESSING' :
1164               /* Send abort event, but only once 
1165                */
1166               if($already_aborted){
1167                 break;
1168               }elseif(class_available("DaemonEvent_faireboot")){
1169                 $already_aborted = TRUE;
1170                 $tmp = new DaemonEvent_faireboot($config);
1171                 $tmp->add_targets(array($mac));
1172                 $tmp->set_type(TRIGGERED_EVENT);
1173                 if(!$this->append($tmp)){
1174                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1175                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1176                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1177                 }else{
1178                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1179                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1180                 }
1181                 ;break;
1182               }else{
1183                 /* Couldn't find abort event, just remove entry */
1184               }
1186             case 'WAITING':
1187             case 'ERROR':
1188             default :
1189             
1190               /* Simply remove entries from queue. 
1191                *  Failed or waiting events, can be removed without any trouble.
1192                */ 
1193               if(!$this->remove_entries(array($entry['ID']))){
1194                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1195               }
1196               ;break;
1197           }
1198     
1199         }
1200       }
1201     }
1202   }
1205   static function ping($target)
1206   {
1207     if (tests::is_mac($target)){
1208       /* Get communication object */
1209       $d= new gosaSupportDaemon(TRUE,2);
1210       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1211       return (count($answer) ? TRUE:FALSE);
1212     }
1213     return (FALSE);
1214   }
1218   /*! \brief  Returns a list of all configured principals. 
1219               (Uses the GOsa support daemon instead of the ldap database.)
1220       @return Array  A list containing the names of all configured principals.
1221    */
1222   public function krb5_list_principals($server)
1223   {
1224     $res = array();  
1226     /* Check if the given server is a valid mac address
1227      */
1228     if(!tests::is_mac($server)){
1229       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1230       return($ret);
1231     }
1233     /* Prepare request event 
1234      */ 
1235     $xml_msg = 
1236       "<xml>".
1237       "<header>gosa_krb5_list_principals</header>".
1238       "<source>GOSA</source>".
1239       "<target>".$server."</target>".
1240       "</xml>";
1241     
1242     $tmp = $this->_send($xml_msg,TRUE);
1243     if(isset($tmp['XML']['PRINCIPAL'])){
1244       return($tmp['XML']['PRINCIPAL']);
1245     }else{
1246       return($res);
1247     }
1248   }
1251   /*! \brief  Returns the configuration settings for a given principal name. 
1252               (Uses the GOsa support daemon instead of the ldap database.)
1253       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1254       @return Array  A list containing the names of all configured principals.
1255    */
1256   public function krb5_get_principal($server,$name)
1257   {
1258     $ret = array();
1260     /* Check if the given name is a valid request value 
1261      */
1262     if(!is_string($name) || empty($name)){
1263       trigger_error("The given principal name is not of type string or it is empty.");
1264       return($ret);
1265     }
1267     /* Check if the given server is a valid mac address
1268      */
1269     if(!tests::is_mac($server)){
1270       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1271       return($ret);
1272     }
1274     /* Prepare request event 
1275      */ 
1276     $xml_msg = 
1277       "<xml>".
1278       "<header>gosa_krb5_get_principal</header>".
1279       "<principal>".$name."</principal>".
1280       "<source>GOSA</source>".
1281       "<target>".$server."</target>".
1282       "</xml>";
1284     $res = $this->_send($xml_msg,TRUE);
1285     if(isset($res['XML'])){
1286       return($res['XML']);
1287     }else{
1288       return($ret);
1289     }
1290   }
1293   /*! \brief  Creates a given principal with a set of configuration settings.
1294               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1295               (Uses the GOsa support daemon instead of the ldap database.)
1296       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1297       @return Boolean   TRUE on success else FALSE. 
1298    */
1299   public function krb5_add_principal($server,$name,$values)
1300   {
1301     $ret = FALSE;  
1303     /* Check if the given name is a valid request value 
1304      */
1305     if(!is_string($name) || empty($name)){
1306       trigger_error("The given principal name is not of type string or it is empty.");
1307       return($ret);
1308     }
1309     if(!is_array($values)){
1310       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1311       return($ret);
1312     }
1314     /* Check if the given server is a valid mac address
1315      */
1316     if(!tests::is_mac($server)){
1317       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1318       return($ret);
1319     }
1321     $attrs = "";
1322     foreach($values as $key => $value){
1323       if(empty($key) || is_numeric($key)){
1324         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1325         return($ret);
1326       }
1327       $key = strtolower($key);
1328       if(is_array($value)){
1329         foreach($value as $val){
1330           $attrs.= "<$key>$val</$key>\n";
1331         }
1332       }else{
1333         $attrs.= "<$key>$value</$key>\n";
1334       }
1335     }
1337     /* Prepare request event 
1338      */ 
1339     $xml_msg = 
1340       "<xml>".
1341       "<header>gosa_krb5_create_principal</header>".
1342       "<principal>".$name."</principal>".
1343       $attrs.
1344       "<source>GOSA</source>".
1345       "<target>".$server."</target>".
1346       "</xml>";
1348     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1349   }
1352   function krb5_ramdomize_key($server,$name)  
1353   {
1354     /* Prepare request event 
1355      */ 
1356     $xml_msg = 
1357       "<xml>".
1358       "<header>gosa_krb5_randomize_key</header>".
1359       "<principal>".$name."</principal>".
1360       "<source>GOSA</source>".
1361       "<target>".$server."</target>".
1362       "</xml>";
1364     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1365   }
1366   
1369   /*! \brief  Updates a given principal with a set of configuration settings.
1370               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1371               (Uses the GOsa support daemon instead of the ldap database.)
1372       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1373       @return Boolean   TRUE on success else FALSE. 
1374    */
1375   public function krb5_set_principal($server,$name,$values)
1376   {
1377     $ret = FALSE;  
1379     /* Check if the given name is a valid request value 
1380      */
1381     if(!is_string($name) || empty($name)){
1382       trigger_error("The given principal name is not of type string or it is empty.");
1383       return($ret);
1384     }
1385     if(!is_array($values) || !count($values)){
1386       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1387       return($ret);
1388     }
1390     /* Check if the given server is a valid mac address
1391      */
1392     if(!tests::is_mac($server)){
1393       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1394       return($ret);
1395     }
1397     $attrs = "";
1398     foreach($values as $key => $value){
1399       if(empty($key) || is_numeric($key)){
1400         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1401         return($ret);
1402       }
1403       $key = strtolower($key);
1404       if(is_array($value)){
1405         foreach($value as $val){
1406           $attrs.= "<$key>$val</$key>\n";
1407         }
1408       }else{
1409         $attrs.= "<$key>$value</$key>\n";
1410       }
1411     }
1413     /* Prepare request event 
1414      */ 
1415     $xml_msg = 
1416       "<xml>".
1417       "<header>gosa_krb5_modify_principal</header>".
1418       "<principal>".$name."</principal>".
1419       $attrs.
1420       "<source>GOSA</source>".
1421       "<target>".$server."</target>".
1422       "</xml>";
1424     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1425   }
1428   /*! \brief  Removes the given principal.
1429               (Uses the GOsa support daemon instead of the ldap database.)
1430       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1431       @return Boollean   TRUE on success else FALSE
1432    */
1433   public function krb5_del_principal($server,$name)
1434   {
1435     $ret = FALSE;  
1437     /* Check if the given name is a valid request value 
1438      */
1439     if(!is_string($name) || empty($name)){
1440       trigger_error("The given principal name is not of type string or it is empty.");
1441       return($ret);
1442     }
1444     /* Check if the given server is a valid mac address
1445      */
1446     if(!tests::is_mac($server)){
1447       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1448       return($ret);
1449     }
1451     /* Prepare request event 
1452      */ 
1453     $xml_msg = 
1454       "<xml>".
1455       "<header>gosa_krb5_del_principal</header>".
1456       "<principal>".$name."</principal>".
1457       "<source>GOSA</source>".
1458       "<target>".$server."</target>".
1459       "</xml>";
1460     
1461     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1462   }
1465   /*! \brief  Returns a list of configured password policies.
1466               (Uses the GOsa support daemon instead of the ldap database.)
1467       @return Array A list of all configured password policies.
1468    */
1469   public function krb5_list_policies($server)
1470   {
1471     $res = array();  
1473     /* Check if the given server is a valid mac address
1474      */
1475     if(!tests::is_mac($server)){
1476       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1477       return($ret);
1478     }
1480     /* Prepare request event 
1481      */ 
1482     $xml_msg = 
1483       "<xml>".
1484       "<header>gosa_krb5_list_policies</header>".
1485       "<source>GOSA</source>".
1486       "<target>".$server."</target>".
1487       "</xml>";
1488     
1489     $res = $this->_send($xml_msg,TRUE);
1490     
1491     /* Check if there are results for POLICY 
1492      */
1493     if(isset($res['XML']['POLICY'])){
1494       
1495       /* Ensure that we return an array 
1496        */
1497       $tmp = $res['XML']['POLICY'];
1498       if(!is_array($tmp)){
1499         $tmp = array($tmp);
1500       }
1501       return($tmp);
1502     }else{
1503       return(array());
1504     }
1505   }
1508   /*! \brief  Returns a list of configured password policies.
1509               (Uses the GOsa support daemon instead of the ldap database.)
1510       @return Array The policy settings for the given policy name.
1511    */
1512   public function krb5_get_policy($server,$name)
1513   {
1514     $ret = array();  
1516     /* Check if the given name is a valid request value 
1517      */
1518     if(!is_string($name) || empty($name)){
1519       trigger_error("The given policy name is not of type string or it is empty.");
1520       return($ret);
1521     }
1523     /* Check if the given server is a valid mac address
1524      */
1525     if(!tests::is_mac($server)){
1526       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1527       return($ret);
1528     }
1530     /* Prepare request event 
1531      */ 
1532     $xml_msg = 
1533       "<xml>".
1534       "<header>gosa_krb5_get_policy</header>".
1535       "<policy>".$name."</policy>".
1536       "<source>GOSA</source>".
1537       "<target>".$server."</target>".
1538       "</xml>";
1540     /* Possible attributes */
1541     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1542         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1544   
1545     $tmp = $this->_send($xml_msg,TRUE);
1546     if(isset($tmp['XML'])){
1547       foreach($attrs as $attr){
1548         if(isset($tmp['XML'][$attr])){
1549           $ret[$attr] = $tmp['XML'][$attr];
1550         }else{
1551           $ret[$attr] = "";
1552         }
1553       }
1554     }
1555     return($ret);
1556   }
1558   
1559   /*! \brief  Creates a new policy with a given set of configuration settings.
1560               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1561               (Uses the GOsa support daemon instead of the ldap database.)
1562       @pram   String The name of the policy to update.
1563       @pram   Array  The attributes to update
1564       @return Boolean   TRUE on success else FALSE. 
1565    */
1566   public function krb5_add_policy($server,$name,$values)
1567   {
1568     $ret = FALSE;  
1570     /* Check if the given name is a valid request value 
1571      */
1572     if(!is_string($name) || empty($name)){
1573       trigger_error("The given policy name is not of type string or it is empty.");
1574       return($ret);
1575     }
1576     if(!is_array($values) || !count($values)){
1577       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1578       return($ret);
1579     }
1581     /* Check if the given server is a valid mac address
1582      */
1583     if(!tests::is_mac($server)){
1584       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1585       return($ret);
1586     }
1589     /* Transform array into <xml>
1590      */
1591     $attrs = "";
1592     foreach($values as $id => $value){
1593       if(empty($id) || is_numeric($id)){
1594         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1595         return($ret);
1596       }
1597       $id = strtolower($id);
1598       $attrs.= "<$id>$value</$id>\n";
1599     }
1601     /* Prepare request event 
1602      */ 
1603     $xml_msg = 
1604       "<xml>".
1605       "<header>gosa_krb5_create_policy</header>".
1606       "<policy>".$name."</policy>".
1607       $attrs.
1608       "<source>GOSA</source>".
1609       "<target>".$server."</target>".
1610       "</xml>";
1612     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1613   }
1616   /*! \brief  Updates a given policy with a set of configuration settings.
1617               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1618               (Uses the GOsa support daemon instead of the ldap database.)
1619       @pram   String The name of the policy to update.
1620       @return Boolean   TRUE on success else FALSE. 
1621    */
1622   public function krb5_set_policy($server,$name,$values)
1623   {
1624     $ret = FALSE;  
1626     /* Check if the given name is a valid request value 
1627      */
1628     if(!is_string($name) || empty($name)){
1629       trigger_error("The given policy name is not of type string or it is empty.");
1630       return($ret);
1631     }
1632     if(!is_array($values) || !count($values)){
1633       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1634       return($ret);
1635     }
1637     /* Check if the given server is a valid mac address
1638      */
1639     if(!tests::is_mac($server)){
1640       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1641       return($ret);
1642     }
1644     /* Transform array into <xml>
1645      */
1646     $attrs = "";
1647     foreach($values as $id => $value){
1648       if(preg_match("/^policy$/i",$id)) continue;
1649       if(empty($id) || is_numeric($id)){
1650         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1651         return($ret);
1652       }
1653       $id = strtolower($id);
1654       $attrs.= "<$id>$value</$id>\n";
1655     }
1657     /* Prepare request event 
1658      */ 
1659     $xml_msg = 
1660       "<xml>".
1661       "<header>gosa_krb5_modify_policy</header>".
1662       "<policy>".$name."</policy>".
1663       $attrs.
1664       "<source>GOSA</source>".
1665       "<target>".$server."</target>".
1666       "</xml>";
1668     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1669   }
1671   
1672   /*! \brief  Removes the given password policy. 
1673               (Uses the GOsa support daemon instead of the ldap database.)
1674       @return Boolean  TRUE on success else FALSE
1675    */
1676   public function krb5_del_policy($server,$name)
1677   {
1678     $ret = FALSE;
1680     /* Check if the given server is a valid mac address
1681      */
1682     if(!tests::is_mac($server)){
1683       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1684       return($ret);
1685     }
1687     /* Check if the given name is a valid request value 
1688      */
1689     if(!is_string($name) || empty($name)){
1690       trigger_error("The given policy name is not of type string or it is empty.");
1691       return($ret);
1692     }
1694     /* Prepare request event 
1695      */ 
1696     $xml_msg = 
1697       "<xml>".
1698       "<header>gosa_krb5_del_policy</header>".
1699       "<policy>".$name."</policy>".
1700       "<source>GOSA</source>".
1701       "<target>".$server."</target>".
1702       "</xml>";
1703     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1704   }
1707   /*! \brief  Sets the password of for the given principal.
1708               (Uses the GOsa support daemon instead of the ldap database.)
1709       @param  String  The servers mac
1710       @param  String  The principals name
1711       @param  String  $the new password.   
1712       @return Boolean  TRUE on success else FALSE
1713    */
1714   public function krb5_set_password($server,$name,$password)
1715   {
1716     $ret = FALSE;
1718     /* Check if the given server is a valid mac address
1719      */
1720     if(!tests::is_mac($server)){
1721       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1722       return($ret);
1723     }
1725     /* Check if the given name is a valid request value
1726      */
1727     if(!is_string($name) || empty($name)){
1728       trigger_error("The given principal name is not of type string or it is empty.");
1729       return($ret);
1730     }
1732     /* Prepare request event
1733      */
1734     $xml_msg =
1735       "<xml>".
1736       "<header>gosa_krb5_set_password</header>".
1737       "<principal>".$name."</principal>".
1738       "<password>".$password."</password>".
1739       "<source>GOSA</source>".
1740       "<target>".$server."</target>".
1741       "</xml>";
1742     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1743   }
1746   /*! \brief  Returns log file informations for a given mac address 
1747       @param  $mac The mac address to fetch logs for.
1748       @retrun Array A Multidimensional array containing log infos.
1749         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1750         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1751                                install_20080313_144450   ...
1752    */
1753   public function get_log_info_for_mac($mac)
1754   {
1755     $xml_msg = "
1756       <xml>
1757       <header>gosa_show_log_by_mac</header>
1758       <target>GOSA</target>
1759       <source>GOSA</source>
1760       <mac>".$mac."</mac>
1761       </xml>";
1763     $res = $this->_send($xml_msg,TRUE);
1764     $ret = array();
1765     if(isset($res['XML'])){
1767       /* Filter all entry that look like this 
1768           MAC_00_01_6C_9D_B9_FA
1769        */
1770       foreach($res['XML'] as $name => $entry){
1771         if(preg_match("/^MAC/",$name)){
1773           /* Get list of available log files 
1774            */
1775           if(!is_array($entry)){
1776             $entry = array($entry);
1777           }
1778           foreach($entry as $log_date){
1779             $xml_msg2 = "<xml> 
1780               <header>gosa_show_log_files_by_date_and_mac</header> 
1781               <target>GOSA</target> 
1782               <source>GOSA</source>                        
1783               <date>".$log_date."</date> 
1784               <mac>".$mac."</mac> 
1785               </xml>";
1786    
1787             $ret[$mac][$log_date] = array();
1788             $res = $this->_send($xml_msg2,TRUE);
1789             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1790             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1791             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1792               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1793             }
1794           }
1795         }
1796       }
1797     }
1798     return($ret);
1799   }
1801   public function get_log_file($mac,$date,$file)
1802   {
1803     $xml_msg ="
1804       <xml> 
1805       <header>gosa_get_log_file_by_date_and_mac</header> 
1806       <target>GOSA</target> 
1807       <source>GOSA</source>
1808       <date>".$date."</date> 
1809       <mac>".$mac."</mac> 
1810       <log_file>".$file."</log_file>
1811       </xml>";
1813     $res = $this->_send($xml_msg,TRUE);
1814     if(isset($res['XML'][strtoupper($file)])){
1815       return(base64_decode($res['XML'][strtoupper($file)]));
1816     }
1817     return("");
1818   }
1824   /*****************
1825    * DAK - Functions 
1826    *****************/
1828   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1829       @param  ...
1830       @return Array   All queued entries.
1831    */
1832   public function DAK_keyring_entries($server)  
1833   {
1834     /* Ensure that we send the event to a valid mac address 
1835      */
1836     if(!is_string($server) || !tests::is_mac($server)){
1837       trigger_error("No valid mac address given '".$server."'.");
1838       return;
1839     }
1841     /* Create query
1842      */
1843     $xml_msg = "<xml> 
1844                   <header>gosa_get_dak_keyring</header> 
1845                   <target>".$server."</target> 
1846                   <source>GOSA</source>
1847                 </xml>";
1848         
1849     $res = $this->_send($xml_msg,TRUE);
1851     /* Check if there are results for POLICY
1852      */
1853     if(isset($res['XML'])){
1854       $ret = array();
1855       foreach($res['XML'] as $key => $entry){
1856         if(preg_match("/^ANSWER/",$key)){
1857           $ret[] = $entry;
1858         }
1859       }
1860       return($ret);
1861     }else{
1862       return(array());
1863     }
1864   }
1867   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1868       @param  String  The servers mac address 
1869       @param  String  The gpg key.
1870       @return Boolean TRUE on success else FALSE 
1871    */
1872   public function DAK_import_key($server,$key)  
1873   {
1874     /* Ensure that we send the event to a valid mac address 
1875      */
1876     if(!is_string($server) || !tests::is_mac($server)){
1877       trigger_error("No valid mac address given '".$server."'.");
1878       return;
1879     }
1881     /* Check if there is some cleanup required before importing the key.
1882         There may be some Header lines like:
1883         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1884      */
1885     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1887       /* Remove header */
1888       $key = preg_replace("/^.*\n\n/sim","",$key);
1889       /* Remove footer */
1890       $key = preg_replace("/-----.*$/sim","",$key);
1891     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1892       
1893       /* Encode key if it is raw.
1894        */
1895       $key = base64_encode($key);
1896     }
1898     /* Create query
1899      */
1900     $xml_msg = "<xml> 
1901                   <header>gosa_import_dak_key</header> 
1902                   <target>".$server."</target> 
1903                   <key>".$key."</key> 
1904                   <source>GOSA</source>
1905                 </xml>";
1906         
1907     $res = $this->_send($xml_msg,TRUE);
1908     return($this->is_error());
1909   }
1912   /*! \brief Removes a key from the keyring on the given server. 
1913       @param  String  The servers mac address 
1914       @param  String  The gpg key uid.
1915       @return Boolean TRUE on success else FALSE 
1916    */
1917   public function DAK_remove_key($server,$key)  
1918   {
1919     /* Ensure that we send the event to a valid mac address 
1920      */
1921     if(!is_string($server) || !tests::is_mac($server)){
1922       trigger_error("No valid mac address given '".$server."'.");
1923       return;
1924     }
1926     /* Create query
1927      */
1928     $xml_msg = "<xml> 
1929                   <header>gosa_remove_dak_key</header> 
1930                   <target>".$server."</target> 
1931                   <keyid>".$key."</keyid> 
1932                   <source>GOSA</source>
1933                 </xml>";
1934        
1935     $res = $this->_send($xml_msg,TRUE);
1936     return($this->is_error());
1937   }
1940 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1941 ?>