Code

Updated Group Application Handling
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class gosaSupportDaemon
24 {
25   private $s_host       = "";
26   private $i_port       = 0;
27   private $s_encryption_key = "";
29   private $o_sock       = NULL;
30   private $f_timeout    = 2;
31   private $s_error      = "";
32   private $b_error      = FALSE;
34   private $is_connected     = FALSE;
36   protected $use_alternative_xml_parse_method = FALSE;
38   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=10)
56   {
57     #FIXME: bad idea about referencing global variables from within classes
58     global $config;
60     /* This should only be the case if we call this from setup.
61         __autoload() 
62      */
63     if(!is_object($config)) { return; }
65     # load from config, store statically
66     if ($config->get_cfg_value("gosaSupportURI") != ""){
68       if ($this->s_host == ""){
69         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
70         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosaSupportURI"));
71         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
72       }
74       $this->f_timeout = $timeout;
75       if($connect){
76         $this->connect();
77       }
78     }
79   }
82   /*! \brief  Establish daemon connection. 
83     @return boolean Returns true if the connection was succesfully established. 
84    */
85   public function connect()
86   {
87     if(!empty($this->s_host) && !empty($this->i_port)){
88       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
89       if($this->o_sock->connected()){ 
90         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
91         $this->is_connected = TRUE;
92       }else{
93         $this->set_error($this->o_sock->get_error());
94         $this->disconnect();
95         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
96       }
97     }else{
98       $this->set_error(msgPool::cmdnotfound("gosaSupportURI",_("GOsa support daemon")));
99     }
100     return($this->is_connected);
101   }
103   
104   /*! \brief  Returns TRUE whether we are connected or not 
105       @return BOOLEAN  Returns TRUE when connected else FALSE
106    */
107   public function is_connected()
108   {
109     return($this->is_connected);
110   }
111  
113   /*! \brief  */
114   public function get_hosts_with_module($mod)
115   {
116     $data = array("module_name" => $mod);
117     $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
118     $hosts = array();
119     if(isset($res['XML'][0])){
120       foreach($res['XML'][0] as $name => $data){
121         if(preg_match("/^ANSWER[0-9]*$/",$name)){
122           if(isset($data[0]['MAC'][0]['VALUE']) && $data[0]['MAC'][0]['VALUE'] != ""){
123             $hosts[] = $data[0]['MAC'][0]['VALUE'];
124           } elseif(isset($data[0]['IP'][0]['VALUE']) && $data[0]['IP'][0]['VALUE'] != "") {
125             $hosts[] = $data[0]['IP'][0]['VALUE'];
126           }
127         }
128       }
129     }
131     return($hosts);
132   }
135   /*! \brief  Disconnect from gosa daemon.
136    */
137   public function disconnect()
138   {
139     $this->o_sock->close();
140     $this->is_connected = FALSE;
141   }
144   /*! \brief  Sets an error message, which can be returned with get_error().
145     @param  string  The Error message,
146    */
147   private function set_error($str)
148   {
149     /******
150       Debug handling
151      ******/
152     $debug = debug_backtrace();
153     $file = __FILE__;
154     $function = __FUNCTION__;
155     $line = __LINE__;
156     $class = __CLASS__;
157     foreach($debug as $info){
158       if(!in_array($info['function'],array("send_data","_send","set_error","connect"))){
159         $file = $info['file'];
160         $line = $info['line'];
161         $class = get_class($this);
162         $function = $info['function'];
163         break;
164       }
165     }
166     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
168     /******
169       Set error string.
170      ******/
171   
172     $this->b_error = TRUE;
173     $this->s_error = $str;
174   }
177   /*! \brief  Sets an error message, which can be returned with get_error().
178     @param  string  The Error message,
179    */
180   private function reset_error()
181   {
182     $this->b_error = FALSE;
183     $this->s_error = "";
184   }
187   /*! \brief  Checks if an error occured.
188     @return boolean returns TRUE or FALSE, whether there is an error or not.
189    */
190   public function is_error()
191   {
192     return($this->b_error);
193   }
196   /*! \brief  Returns the last error. 
197     @return Returns the last error.
198    */
199   public function get_error()
200   {
201     $str = $this->s_error;
202     $ret = "";
203     if(is_string($str)){
204       $ret = $str;
205     }else{
206       foreach($str as $msg){
207         $ret .= $msg." ";
208       }
209     }
210     $ret = preg_replace("/ /","&nbsp;",$ret);
211     return($ret);
212   }
215   public function FAI_get_kernels($release)
216   {
217     $xml_msg = 
218       "<xml>".
219       "<header>gosa_get_available_kernel</header>".
220       "<source>GOSA</source>".
221       "<target>GOSA</target>".
222       "<fai_release>".$release."</fai_release>".
223       "</xml>";
225     $ret = array();
226     if($this->connect()){
227       $entries = $this->_send($xml_msg,TRUE);
229       /* Check if returned values represent a valid answer */
230       if(isset($entries['XML']) && is_array($entries['XML'])){
231         if(isset($entries['XML'])){
232           $ret = $entries['XML'];
233           foreach($ret as $key => $entry){
234             if(!preg_match("/^answer/i",$key)){
235               unset($ret[$key]);
236             }
237           }
238         }
239       }
240     }
241     return($ret);
242   }
245   public function FAI_get_package_sections($release)
246   {
247     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
248       "<select>distinct section</select>".
249       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
251     $ret = array();
252     if($this->connect()){
253       $entries = $this->_send($xml_msg,TRUE);
254       if(isset($entries['XML']) && is_array($entries['XML'])){
256         /* Unset header tags */
257         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
258           if(isset($entries['XML'][$type])){
259             unset($entries['XML'][$type]);
260           }
261         }
262         $ret = $entries['XML'];
263       }
264     }
265     return($ret);
266   }
269   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
270   {
271     $ret = array();
273     /* Check Parameter */
274     if(!is_array($attrs) || !count($attrs)){
275       trigger_error("Second parameter must be an array. With at least one attribute name.");
276       return($ret);
277     }
279     /* Check Parameter */
280     if(!is_array($package)){
281       trigger_error("Third parameter must be an array. With at least one attribute name.");
282       return($ret);
283     }
285     /* Create list of attributes to fetch */
286     $attr = ""; 
287     foreach($attrs as $at){
288       $attr.= "<select>".$at."</select>";
289     }
291     /* If no package is given, search for all */
292     if(!count($package)) $package = array("%");
294     /* Create limit tag */
295     if($from == -1){
296       $limit =""; 
297     }else{
298       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
299     }
301     /* Create list of attributes to fetch */
302     $pkgs = ""; 
303     foreach($package as $pkg){
304       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
305     }
307     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
308       $attr.
309       "<where>
310       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
311       <clause><connector>OR</connector>
312       ".$pkgs."
313       </clause>
314       </where>".
315       $limit.
316       "</xml>";
318     if($this->connect()){
319       $entries = $this->_send($xml_msg,TRUE);
320       if(isset($entries['XML']) && is_array($entries['XML'])){
322         /* Check if returned values represent a valid answer */
323         if(isset($entries['XML'])){
325           /* Unset header tags */
326           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
327             if(isset($entries['XML'][$type])){
328               unset($entries['XML'][$type]);
329             }
330           }
331           $ret = $entries['XML'];
332         }
333       }
334     }
335     return($ret);
337     
338   }
341   public function FAI_get_server($name = "")
342   {
344     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
345     $ret = array();
346     if($this->connect()){
348       /* Check if returned values represent a valid answer */
349       $entries = $this->_send($xml_msg,TRUE);
350       if(isset($entries['XML']) && is_array($entries['XML'])){
352         /* Unset header tags */
353         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
354           if(isset($entries['XML'][$type])){
355             unset($entries['XML'][$type]);
356           }
357         }
358         $ret = $entries['XML']; 
359       }
360     }
361     return($ret);
362   }
365   public function FAI_get_classes($name)
366   {
367     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
368                   "<where><clause><phrase><fai_release>".$name."</fai_release></phrase></clause></where></xml>";;
369     $ret = array();
370     if($this->connect()){
372       $entries = $this->_send($xml_msg,TRUE);
373       if(isset($entries['XML']) && is_array($entries['XML'])){
375         /* Unset header tags */
376         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
377           if(isset($entries['XML'][$type])){
378             unset($entries['XML'][$type]);
379           }
380         }
381         $ret = $entries['XML']; 
382       }
383     }
384     return($ret);
385   }
388   /*! \brief  Returns an array containing all queued entries.
389     @return Array All queued entries as an array.
390    */
391   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
392   {
393     $ret = array();
395     $tags = "";
396     foreach($event_types as $type){
397       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
398     }
399     if(count($event_types) > 1){
400       $tags = "<connector>or</connector>".$tags;
401     }
402     if(count($event_types)){
403       $tags = "<where><clause>".$tags."</clause></where>";
404     }
406     $xml_msg = 
407       "<xml>
408       <header>gosa_query_jobdb</header>
409       <target>GOSA</target>
410       <source>GOSA</source>
411       ".$tags."
413       <orderby>".$sort."</orderby>";
414     if($from != -1 && $to != -1){
415       $xml_msg.= "
416         <limit>
417         <from>".$from."</from>
418         <to>".$to."</to>
419         </limit>";
420     }
421     $xml_msg.= "
422       </xml>";
424     if($this->connect()){
425       $entries = $this->_send($xml_msg,TRUE);
426       if(isset($entries['XML']) && is_array($entries['XML'])){
428         /* Unset header tags */
429         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
430           unset($entries['XML'][$type]);
431         }
432         $ret = $entries['XML']; 
433       }
434     }
435     return($ret);
436   }
439   /*! \brief  Checks if the given ids are used queue ids.
440     @param  Array   The ids we want to check..
441     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
442    */
443   public function ids_exist($ids)
444   {
445     if(!is_array($ids)){
446       trigger_error("Requires an array as parameter.");
447       return;
448     }
450     $ret = array();
452     $xml_msg = "<xml>
453       <header>gosa_query_jobdb</header>
454       <target>GOSA</target>
455       <source>GOSA</source>
456       <where>
457       <clause>
458       <connector>or</connector>";
459     foreach($ids as $id){
460       $xml_msg .= "<phrase>
461         <operator>eq</operator>
462         <id>".$id."</id>
463         </phrase>";
464     }
465     $xml_msg .= "</clause>
466       </where>
467       </xml>";
469     if($this->connect()){
470       $entries = $this->_send($xml_msg,TRUE);
471       if(isset($entries['XML']) && is_array($entries['XML'])){
472         foreach($entries['XML'] as $entry){
473           if(is_array($entry) && array_key_exists("ID",$entry)){
474             $ret[] = $entry['ID'];
475           }
476         }
477       }
478     }
479     return($ret);
480   }
483   /*! \brief  Returns an entry containing all requested ids.
484     @param  Array   The IDs of the entries we want to return.
485     @return Array   Of the requested entries. 
486    */
487   public function get_entries_by_mac($macs)
488   {
489     if(!is_array($macs)){
490       trigger_error("Requires an array as parameter.");
491       return;
492     }
494     $ret = array();
496     $xml_msg = "<xml>
497       <header>gosa_query_jobdb</header>
498       <target>GOSA</target>
499       <source>GOSA</source>
500       <where>
501       <clause>
502       <connector>or</connector>";
503     foreach($macs as $mac){
504       $xml_msg .= "<phrase>
505         <operator>eq</operator>
506         <macaddress>".$mac."</macaddress>
507         </phrase>";
508     }
509     $xml_msg .= "</clause>
510       </where>
511       </xml>";
513     if($this->connect()){
514       $entries = $this->_send($xml_msg,TRUE);
515       if(isset($entries['XML'])){
516         foreach($entries['XML'] as $name => $entry){
517           if(preg_match("/^ANSWER[0-9]*$/",$name)){
518             $ret[$name] = $entry;
519           }
520         }
521       }
522     }
523     return($ret);
524   }
527   /*! \brief  Returns an entry containing all requested ids.
528     @param  Array   The IDs of the entries we want to return.
529     @return Array   Of the requested entries. 
530    */
531   public function get_entries_by_id($ids)
532   {
533     if(!is_array($ids)){
534       trigger_error("Requires an array as parameter.");
535       return;
536     }
538     $ret = array();
540     $xml_msg = "<xml>
541       <header>gosa_query_jobdb</header>
542       <target>GOSA</target>
543       <source>GOSA</source>
544       <where>
545       <clause>
546       <connector>or</connector>";
547     foreach($ids as $id){
548       $xml_msg .= "<phrase>
549         <operator>eq</operator>
550         <id>".$id."</id>
551         </phrase>";
552     }
553     $xml_msg .= "</clause>
554       </where>
555       </xml>";
557     if($this->connect()){
558       $entries = $this->_send($xml_msg,TRUE);
559       if(isset($entries['XML'])){
560         foreach($entries['XML'] as $name => $entry){
561           if(preg_match("/^ANSWER[0-9]*$/",$name)){
562             $ret[$name] = $entry;
563           }
564         }
565       }
566     }
567     return($ret);
568   }
571   /*! \brief  Checks if the given id is in use.
572     @param  Integer The ID of the entry.
573     @return Boolean TRUE if entry exists. 
574    */
575   public function id_exists($id)
576   {
577     if(!is_numeric($id)){
578       trigger_error("Requires an integer as parameter.");
579       return;
580     }
583     $xml_msg = "<xml>
584       <header>gosa_query_jobdb</header>
585       <target>GOSA</target>
586       <source>GOSA</source>
587       <where>
588       <clause>
589       <phrase>
590       <operator>eq</operator>
591       <id>".$id."</id>
592       </phrase>
593       </clause>
594       </where>
595       </xml>";
597     if($this->connect()){
598       $entries = $this->_send($xml_msg,TRUE);
599       if( isset($entries['XML']['HEADER']) && 
600           $entries['XML']['HEADER']=="answer" && 
601           isset($entries['XML']['ANSWER1'])){
602         return(TRUE);
603       }
604     }
605     return(FALSE);
606   }
609   /*! \brief  Returns an entry from the gosaSupportQueue
610     @param  Integer The ID of the entry we want to return.
611     @return Array   Of the requested entry. 
612    */
613   public function get_entry_by_id($id)
614   {
615     if(!is_numeric($id)){
616       trigger_error("Requires an integer as parameter.");
617       return;
618     }
619   
620     $ret = array();
621     $xml_msg = "<xml>
622       <header>gosa_query_jobdb</header>
623       <target>GOSA</target>
624       <source>GOSA</source>
625       <where>
626       <clause>
627       <phrase>
628       <operator>eq</operator>
629       <id>".$id."</id>
630       </phrase>
631       </clause>
632       </where>
633       </xml>";
634     if($this->connect()){
635       $entries = $this->_send($xml_msg,TRUE);
636       if( isset($entries['XML']['HEADER']) &&
637           $entries['XML']['HEADER']=="answer" &&
638           isset($entries['XML']['ANSWER1'])){
639         $ret = $entries['XML']['ANSWER1'];
640       }
641     }
642     return($ret);
643   }
646   /*! \brief  Removes a set of entries from the GOsa support queue. 
647     @param  Array The IDs to remove.
648     @return Boolean True on success.
649    */
650   public function remove_entries($ids)
651   {
652     if(!is_array($ids)){
653       trigger_error("Requires an array as parameter.");
654       return;
655     }
658     $ret = array();
660     $xml_msg = "<xml>
661       <header>gosa_delete_jobdb_entry</header>
662       <target>GOSA</target>
663       <source>GOSA</source>
664       <where>
665       <clause>
666       <connector>or</connector>";
667     foreach($ids as $id){
668       $xml_msg .= "<phrase>
669         <operator>eq</operator>
670         <id>".$id."</id>
671         </phrase>";
672     }
673     $xml_msg .= "</clause>
674       </where>
675       </xml>";
677     if($this->connect()){
678       $entries = $this->_send($xml_msg,TRUE);
679       if(isset($entries['XML']) || isset($entries['COUNT'])){
680         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
681         return(TRUE);
682       }else{
683         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
684       }
685     }
686     return(FALSE);
687   }
691   /*! \brief  Removes an entry from the GOsa support queue. 
692     @param  Integer The ID of the entry we want to remove.
693     @return Boolean True on success.
694    */
695   public function remove_entry($id)
696   {
697     return($this->remove_entries(array($id)));
698   }
701   /*! \brief  Parses the given xml string into an array 
702     @param  String XML string  
703     @return Array Returns an array containing the xml structure. 
704    */
705   private function xml_to_array($xml,$alternative_method = FALSE)
706   {
707     $params = array();
708     $level = array();
709     $parser  = xml_parser_create_ns();
710     xml_parse_into_struct($parser, $xml, $vals, $index);
712     $err_id = xml_get_error_code($parser);
713     if($err_id){
714       xml_parser_free($parser);
715     }else{
716       xml_parser_free($parser);
718       if($this->use_alternative_xml_parse_method) {
719         $params = $this->build_xml_array($vals);
720       } else {
722         foreach ($vals as $xml_elem) {
723           if ($xml_elem['type'] == 'open') {
724             if (array_key_exists('attributes',$xml_elem)) {
725               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
726             } else {
727               $level[$xml_elem['level']] = $xml_elem['tag'];
728             }
729           }
730           if ($xml_elem['type'] == 'complete') {
732             $start_level = 1;
733             $test2 = &$params;
734             while($start_level < $xml_elem['level']) {
735               $test2 = &$test2[$level[$start_level]];
736               $start_level++;
737             }
739             /* Save tag attributes too. 
740                e.g. <tag attr="val">
741              */
742             if(isset($xml_elem['attributes'])){
743               foreach($xml_elem['attributes'] as $name => $value){
744                 $test2['ATTRIBUTES'][$name] = $value;
745               }
746             }
748             if(!isset($test2[$xml_elem['tag']])){
749               if(isset($xml_elem['value'])){
750                 $test2[$xml_elem['tag']] = $xml_elem['value'];
751               }
752             }else{
753               if(!is_array($test2[$xml_elem['tag']])){
754                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
755               }
756               $test2[$xml_elem['tag']][] = $xml_elem['value'];
757             }
758           }
759         }
760       }
761     }
763     if(!isset($params['XML'])){
764       if (!array_key_exists('XML', $params)){
765         $this->set_error(_("Cannot not parse XML!"));
766       }
767       $params = array("COUNT" => 0);
768     }
770     return($params); 
771   }
774   function build_xml_array(&$vals)
775   {
776     $array = array();
777     while(count($vals)){
778       $key = key($vals);
779       $val = $vals[$key];
780       unset($vals[$key]);
781       if($val['type'] == "close"){
782         return($array);
783       }elseif($val['type']=="open"){
784         $array[$val['tag']][] = $this->build_xml_array($vals);
785       }elseif($val['type'] != "cdata"){
786         $data = array("VALUE" => "","ATTRIBUTES" => "");
787         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
788           if(isset($val[$name])){
789             $data[$attr] = $val[$name];
790           }
791         }
792         $array[$val['tag']][] = $data;
793       }else{
794 #print_a($val);
795       }
796     }
797     return($array);
798   }
805   /*! \brief  Updates an entry with a set of new values, 
806     @param  Integer The ID of the entry, we want to update.
807     @param  Array   The variables to update.   
808     @return Boolean Returns TRUE on success. 
809    */
810   public function update_entries($ids,$data)
811   {
812     if(!is_array($ids)){
813       trigger_error("Requires an array as first parameter.");
814       return;
815     }
817     if(!is_array($data)){
818       trigger_error("Requires an array as second parameter.");
819       return;
820     }
822     $attr = "";
823     foreach($data as $key => $value){
824       $key = strtolower($key);
825       if(is_array($value)){
826         foreach($value as $sub_value){
827           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
828         }
829       }else{
830         $attr.= "<$key>".strtolower($value)."</$key>\n";
831       }
832     }
834     $xml_msg = "<xml>
835       <header>gosa_update_status_jobdb_entry</header>
836       <target>GOSA</target>
837       <source>GOSA</source>
838       <where>
839       <clause>
840       <connector>or</connector>";
841     foreach($ids as $id){
842       $xml_msg .= "<phrase>
843         <operator>eq</operator>
844         <id>".$id."</id>
845         </phrase>";
846     }
847     $xml_msg .= "</clause>
848       </where>
849       <update>
850       ".$attr." 
851       </update>
852       </xml>";
854     if($this->connect()){
855       $entries = $this->_send($xml_msg,TRUE);
856       if(isset($entries['XML'])){
857         if(isset($entries['XML']['ERROR_STRING'])) {
858           $this->set_error($entries['XML']['ERROR_STRING']);
859           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
860           return(FALSE);
861         }
862         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
863         return(TRUE);
864       }
865     }
866     return(FALSE);
867   }
870   /*! \brief  Returns the number of currently queued objects.
871       @return Integer  
872    */
873   public function number_of_queued_entries($event_types)
874   {
875     $tags = "";
876     foreach($event_types as $type){
877       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
878     }
879     if(count($event_types) > 1){
880       $tags = "<connector>or</connector>".$tags;
881     }
882     if(count($event_types)){
883       $tags = "<where><clause>".$tags."</clause></where>";
884     }
887     $xml_msg =
888       "<xml>".
889       "<header>gosa_query_jobdb</header>".
890       "<target>GOSA</target>".
891       "<source>GOSA</source>".
892       "<select> count ID</select>".
893       $tags.
894       "</xml>";
896     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
897     $this->connect();
898     if($this->connect()){
899       $entries = $this->_send($xml_msg,TRUE);
900       if($this->o_sock->is_error()){
901         $this->set_error($this->o_sock->get_error());
902         return(0);
903       }
904       if(isset($entries['XML'])){
905         return($entries['XML']['COUNT']);
906       }
907     }
908     return(-1);
909   } 
912   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
913   {
914     $xml_message= "";
916     /* Prepare data */
917     foreach ($data as $key => $value){
918       if(is_array($value)){
919         foreach($value as $sub_value){
920           $xml_message.= "<$key>$sub_value</$key>";
921         }
922       }else{
923         $xml_message.= "<$key>$value</$key>";
924       }
925     }
927     /* Multiple targets? */
928     if (!is_array($to)){
929       $to_targets= array($to);
930     } else {
931       $to_targets= $to;
932     }
934     /* Build target strings */
935     $target ="";
936     foreach($to_targets as $to){
937       $target.= "<target>$to</target>";
938     }
940     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
941   }
944   /* Allows simply appending a new DaemonEvent 
945    */
946   public function append($event, $skip_add_mac = FALSE)
947   {
948     if(!($event instanceof DaemonEvent)){
949       return(FALSE);
950     }
951   
953     /* Add to queue if new 
954      */
955     if($event->is_new()){
957       $request_answer = FALSE;
958       if($event->get_type() == SCHEDULED_EVENT){
959         $action = $event->get_schedule_action();
960       }elseif($event->get_type() == TRIGGERED_EVENT){
961         $action = $event->get_trigger_action();
962       }else{
963         trigger_error("Unknown type of queue event given.");
964         return(FALSE);
965       }
967       /* Get event informations, like targets..
968        */
969       $targets    = $event->get_targets();
970       $data       = $event->save();
972       /* Append an entry for each target 
973        */
974       foreach($targets as $target){
975         if(!$skip_add_mac){
976           $data['macaddress'] = $target;
977         }
978         $this->send_data($action,$target,$data,$request_answer);
980         if($this->is_error()){
981           return(FALSE);
982         }
983       }
984       return(TRUE);
985     }else{
987       /* Updated edited entry.
988        */
989       $id                 = $event->get_id();
990       $data               = $event->save();
991       return($this->update_entries(array($id),$data));
992     }
994     return(FALSE);
995   }
998   /*! \brief  Returns an array containing all queued entries.
999     @return Array All queued entries as an array.
1000    */
1001   public function _send($data, $answer_expected= FALSE)
1002   {
1004     $ret = array();
1005     if(!$this->connect()){
1006       return($ret);
1007     }
1008   
1009     $this->reset_error();
1011     /******
1012       Debug handling
1013      ******/
1014     $debug = debug_backtrace();
1015     $file = __FILE__;
1016     $function = __FUNCTION__;
1017     $line = __LINE__;
1018     $class = __CLASS__;
1019     foreach($debug as $info){
1020       if(!in_array($info['function'],array("send_data","_send"))){
1021         $file = $info['file'];
1022         $line = $info['line'];
1023         $class = get_class($this);
1024         $function = $info['function'];
1025         break;
1026       }
1027     }
1028     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1031     /*******
1032       Start sending data 
1033      *******/
1034     if($this->connect()){
1035       $this->o_sock->write($data);
1036       if ($answer_expected){
1037         $str = trim($this->o_sock->read());
1039         /* Check if something went wrong while reading */
1040         if($this->o_sock->is_error()){
1041           $this->set_error($this->o_sock->get_error());
1042           return($ret);
1043         }
1045         $entries = $this->xml_to_array($str);
1046         if(isset($entries['XML']) && is_array($entries['XML'])){
1047           $ret = $entries;
1048           if($this->use_alternative_xml_parse_method) {
1049             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1050               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1051               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1052                   array($data=>$data),"FAILED ".$this->get_error());
1053             }
1054           }else{
1055             if(isset($entries['XML']['ERROR_STRING'])) {
1056               $this->set_error($entries['XML']['ERROR_STRING']);
1057               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1058                   array($data=>$data),"FAILED ".$this->get_error());
1059             }elseif(isset($entries['XML']['ERROR'])){
1060               $this->set_error($entries['XML']['ERROR']);
1061               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1062                   array($data=>$data),"FAILED ".$this->get_error());
1063             }
1064           }
1065           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1066               array($data=>$data),"SUCCESS");
1067         }
1068       }else{
1069         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1070             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1071       }
1072     }
1073     return($ret);
1074   }
1077   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1078   {
1079     $xml_message= "";
1081     /* Get communication object */
1082     $d= new gosaSupportDaemon(TRUE,10);
1084     /* Prepare data */
1085     foreach ($data as $key => $value){
1086       if(is_array($value)){
1087         foreach($value as $sub_val){
1088           $xml_message.= "<$key>$sub_val</$key>";
1089         }
1090       }else{
1091         $xml_message.= "<$key>$value</$key>";
1092       }
1093     }
1095     /* Multiple targets? */
1096     if (!is_array($to)){
1097       $to_targets= array($to);
1098     } else {
1099       $to_targets= $to;
1100     }
1102     /* Build target strings */
1103     $target ="";
1104     foreach($to_targets as $to){
1105       $target.= "<target>$to</target>";
1106     }
1108     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1109   }
1112   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1113       @param  String  $mac  The mac address for which we want to remove all jobs.      
1114    */
1115   function clean_queue_from_mac($mac)
1116   {
1117     global $config;
1119     /* First of all we have to check which jobs are startet 
1120      *  for $mac 
1121      */
1122     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1123     
1124     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1125  
1126     $data = $this->_send($xml_msg,TRUE);
1127     if(is_array($data) && isset($data['XML'])){
1128       $already_aborted = FALSE;
1129       foreach($data['XML']  as $name => $entry){
1130         if(preg_match("/answer[0-9]*/i",$name)){
1131           $entry['STATUS'] = strtoupper($entry['STATUS']);
1132           switch($entry['STATUS']){
1134             case 'PROCESSING' :
1136               /* Send abort event, but only once 
1137                */
1138               if($already_aborted){
1139                 break;
1140               }elseif(class_available("DaemonEvent_faireboot")){
1141                 $already_aborted = TRUE;
1142                 $tmp = new DaemonEvent_faireboot($config);
1143                 $tmp->add_targets(array($mac));
1144                 $tmp->set_type(TRIGGERED_EVENT);
1145                 if(!$this->append($tmp)){
1146                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1147                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1148                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1149                 }else{
1150                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1151                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1152                 }
1153                 ;break;
1154               }else{
1155                 /* Couldn't find abort event, just remove entry */
1156               }
1158             case 'WAITING':
1159             case 'ERROR':
1160             default :
1161             
1162               /* Simply remove entries from queue. 
1163                *  Failed or waiting events, can be removed without any trouble.
1164                */ 
1165               if(!$this->remove_entries(array($entry['ID']))){
1166                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1167               }
1168               ;break;
1169           }
1170     
1171         }
1172       }
1173     }
1174   }
1177   static function ping($target)
1178   {
1179     if (tests::is_mac($target)){
1180       /* Get communication object */
1181       $d= new gosaSupportDaemon(TRUE,0.5);
1182       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1183       return (count($answer) ? TRUE:FALSE);
1184     }
1185     return (FALSE);
1186   }
1190   /*! \brief  Returns a list of all configured principals. 
1191               (Uses the GOsa support daemon instead of the ldap database.)
1192       @return Array  A list containing the names of all configured principals.
1193    */
1194   public function krb5_list_principals($server)
1195   {
1196     $res = array();  
1198     /* Check if the given server is a valid mac address
1199      */
1200     if(!tests::is_mac($server)){
1201       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1202       return($ret);
1203     }
1205     /* Prepare request event 
1206      */ 
1207     $xml_msg = 
1208       "<xml>".
1209       "<header>gosa_krb5_list_principals</header>".
1210       "<source>GOSA</source>".
1211       "<target>".$server."</target>".
1212       "</xml>";
1213     
1214     $tmp = $this->_send($xml_msg,TRUE);
1215     if(isset($tmp['XML']['PRINCIPAL'])){
1216       return($tmp['XML']['PRINCIPAL']);
1217     }else{
1218       return($res);
1219     }
1220   }
1223   /*! \brief  Returns the configuration settings for a given principal name. 
1224               (Uses the GOsa support daemon instead of the ldap database.)
1225       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1226       @return Array  A list containing the names of all configured principals.
1227    */
1228   public function krb5_get_principal($server,$name)
1229   {
1230     $ret = array();
1232     /* Check if the given name is a valid request value 
1233      */
1234     if(!is_string($name) || empty($name)){
1235       trigger_error("The given principal name is not of type string or it is empty.");
1236       return($ret);
1237     }
1239     /* Check if the given server is a valid mac address
1240      */
1241     if(!tests::is_mac($server)){
1242       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1243       return($ret);
1244     }
1246     /* Prepare request event 
1247      */ 
1248     $xml_msg = 
1249       "<xml>".
1250       "<header>gosa_krb5_get_principal</header>".
1251       "<principal>".$name."</principal>".
1252       "<source>GOSA</source>".
1253       "<target>".$server."</target>".
1254       "</xml>";
1256     $res = $this->_send($xml_msg,TRUE);
1257     if(isset($res['XML'])){
1258       return($res['XML']);
1259     }else{
1260       return($ret);
1261     }
1262   }
1265   /*! \brief  Creates a given principal with a set of configuration settings.
1266               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1267               (Uses the GOsa support daemon instead of the ldap database.)
1268       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1269       @return Boolean   TRUE on success else FALSE. 
1270    */
1271   public function krb5_add_principal($server,$name,$values)
1272   {
1273     $ret = FALSE;  
1275     /* Check if the given name is a valid request value 
1276      */
1277     if(!is_string($name) || empty($name)){
1278       trigger_error("The given principal name is not of type string or it is empty.");
1279       return($ret);
1280     }
1281     if(!is_array($values)){
1282       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1283       return($ret);
1284     }
1286     /* Check if the given server is a valid mac address
1287      */
1288     if(!tests::is_mac($server)){
1289       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1290       return($ret);
1291     }
1293     $attrs = "";
1294     foreach($values as $key => $value){
1295       if(empty($key) || is_numeric($key)){
1296         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1297         return($ret);
1298       }
1299       $key = strtolower($key);
1300       if(is_array($value)){
1301         foreach($value as $val){
1302           $attrs.= "<$key>$val</$key>\n";
1303         }
1304       }else{
1305         $attrs.= "<$key>$value</$key>\n";
1306       }
1307     }
1309     /* Prepare request event 
1310      */ 
1311     $xml_msg = 
1312       "<xml>".
1313       "<header>gosa_krb5_create_principal</header>".
1314       "<principal>".$name."</principal>".
1315       $attrs.
1316       "<source>GOSA</source>".
1317       "<target>".$server."</target>".
1318       "</xml>";
1320     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1321   }
1324   function krb5_ramdomize_key($server,$name)  
1325   {
1326     /* Prepare request event 
1327      */ 
1328     $xml_msg = 
1329       "<xml>".
1330       "<header>gosa_krb5_randomize_key</header>".
1331       "<principal>".$name."</principal>".
1332       "<source>GOSA</source>".
1333       "<target>".$server."</target>".
1334       "</xml>";
1336     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1337   }
1338   
1341   /*! \brief  Updates a given principal with a set of configuration settings.
1342               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1343               (Uses the GOsa support daemon instead of the ldap database.)
1344       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1345       @return Boolean   TRUE on success else FALSE. 
1346    */
1347   public function krb5_set_principal($server,$name,$values)
1348   {
1349     $ret = FALSE;  
1351     /* Check if the given name is a valid request value 
1352      */
1353     if(!is_string($name) || empty($name)){
1354       trigger_error("The given principal name is not of type string or it is empty.");
1355       return($ret);
1356     }
1357     if(!is_array($values) || !count($values)){
1358       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1359       return($ret);
1360     }
1362     /* Check if the given server is a valid mac address
1363      */
1364     if(!tests::is_mac($server)){
1365       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1366       return($ret);
1367     }
1369     $attrs = "";
1370     foreach($values as $key => $value){
1371       if(empty($key) || is_numeric($key)){
1372         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1373         return($ret);
1374       }
1375       $key = strtolower($key);
1376       if(is_array($value)){
1377         foreach($value as $val){
1378           $attrs.= "<$key>$val</$key>\n";
1379         }
1380       }else{
1381         $attrs.= "<$key>$value</$key>\n";
1382       }
1383     }
1385     /* Prepare request event 
1386      */ 
1387     $xml_msg = 
1388       "<xml>".
1389       "<header>gosa_krb5_modify_principal</header>".
1390       "<principal>".$name."</principal>".
1391       $attrs.
1392       "<source>GOSA</source>".
1393       "<target>".$server."</target>".
1394       "</xml>";
1396     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1397   }
1400   /*! \brief  Removes the given principal.
1401               (Uses the GOsa support daemon instead of the ldap database.)
1402       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1403       @return Boollean   TRUE on success else FALSE
1404    */
1405   public function krb5_del_principal($server,$name)
1406   {
1407     $ret = FALSE;  
1409     /* Check if the given name is a valid request value 
1410      */
1411     if(!is_string($name) || empty($name)){
1412       trigger_error("The given principal name is not of type string or it is empty.");
1413       return($ret);
1414     }
1416     /* Check if the given server is a valid mac address
1417      */
1418     if(!tests::is_mac($server)){
1419       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1420       return($ret);
1421     }
1423     /* Prepare request event 
1424      */ 
1425     $xml_msg = 
1426       "<xml>".
1427       "<header>gosa_krb5_del_principal</header>".
1428       "<principal>".$name."</principal>".
1429       "<source>GOSA</source>".
1430       "<target>".$server."</target>".
1431       "</xml>";
1432     
1433     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1434   }
1437   /*! \brief  Returns a list of configured password policies.
1438               (Uses the GOsa support daemon instead of the ldap database.)
1439       @return Array A list of all configured password policies.
1440    */
1441   public function krb5_list_policies($server)
1442   {
1443     $res = array();  
1445     /* Check if the given server is a valid mac address
1446      */
1447     if(!tests::is_mac($server)){
1448       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1449       return($ret);
1450     }
1452     /* Prepare request event 
1453      */ 
1454     $xml_msg = 
1455       "<xml>".
1456       "<header>gosa_krb5_list_policies</header>".
1457       "<source>GOSA</source>".
1458       "<target>".$server."</target>".
1459       "</xml>";
1460     
1461     $res = $this->_send($xml_msg,TRUE);
1462     
1463     /* Check if there are results for POLICY 
1464      */
1465     if(isset($res['XML']['POLICY'])){
1466       
1467       /* Ensure that we return an array 
1468        */
1469       $tmp = $res['XML']['POLICY'];
1470       if(!is_array($tmp)){
1471         $tmp = array($tmp);
1472       }
1473       return($tmp);
1474     }else{
1475       return(array());
1476     }
1477   }
1480   /*! \brief  Returns a list of configured password policies.
1481               (Uses the GOsa support daemon instead of the ldap database.)
1482       @return Array The policy settings for the given policy name.
1483    */
1484   public function krb5_get_policy($server,$name)
1485   {
1486     $res = array();  
1488     /* Check if the given name is a valid request value 
1489      */
1490     if(!is_string($name) || empty($name)){
1491       trigger_error("The given policy name is not of type string or it is empty.");
1492       return($ret);
1493     }
1495     /* Check if the given server is a valid mac address
1496      */
1497     if(!tests::is_mac($server)){
1498       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1499       return($ret);
1500     }
1502     /* Prepare request event 
1503      */ 
1504     $xml_msg = 
1505       "<xml>".
1506       "<header>gosa_krb5_get_policy</header>".
1507       "<policy>".$name."</policy>".
1508       "<source>GOSA</source>".
1509       "<target>".$server."</target>".
1510       "</xml>";
1512     /* Possible attributes */
1513     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1514         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1516   
1517     $tmp = $this->_send($xml_msg,TRUE);
1518     if(isset($tmp['XML'])){
1519       foreach($attrs as $attr){
1520         if(isset($tmp['XML'][$attr])){
1521           $ret[$attr] = $tmp['XML'][$attr];
1522         }else{
1523           $ret[$attr] = "";
1524         }
1525       }
1526     }
1527     return($ret);
1528   }
1530   
1531   /*! \brief  Creates a new policy with a given set of configuration settings.
1532               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1533               (Uses the GOsa support daemon instead of the ldap database.)
1534       @pram   String The name of the policy to update.
1535       @pram   Array  The attributes to update
1536       @return Boolean   TRUE on success else FALSE. 
1537    */
1538   public function krb5_add_policy($server,$name,$values)
1539   {
1540     $ret = FALSE;  
1542     /* Check if the given name is a valid request value 
1543      */
1544     if(!is_string($name) || empty($name)){
1545       trigger_error("The given policy name is not of type string or it is empty.");
1546       return($ret);
1547     }
1548     if(!is_array($values) || !count($values)){
1549       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1550       return($ret);
1551     }
1553     /* Check if the given server is a valid mac address
1554      */
1555     if(!tests::is_mac($server)){
1556       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1557       return($ret);
1558     }
1561     /* Transform array into <xml>
1562      */
1563     $attrs = "";
1564     foreach($values as $id => $value){
1565       if(empty($id) || is_numeric($id)){
1566         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1567         return($ret);
1568       }
1569       $id = strtolower($id);
1570       $attrs.= "<$id>$value</$id>\n";
1571     }
1573     /* Prepare request event 
1574      */ 
1575     $xml_msg = 
1576       "<xml>".
1577       "<header>gosa_krb5_create_policy</header>".
1578       "<policy>".$name."</policy>".
1579       $attrs.
1580       "<source>GOSA</source>".
1581       "<target>".$server."</target>".
1582       "</xml>";
1584     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1585   }
1588   /*! \brief  Updates a given policy with a set of configuration settings.
1589               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1590               (Uses the GOsa support daemon instead of the ldap database.)
1591       @pram   String The name of the policy to update.
1592       @return Boolean   TRUE on success else FALSE. 
1593    */
1594   public function krb5_set_policy($server,$name,$values)
1595   {
1596     $ret = FALSE;  
1598     /* Check if the given name is a valid request value 
1599      */
1600     if(!is_string($name) || empty($name)){
1601       trigger_error("The given policy name is not of type string or it is empty.");
1602       return($ret);
1603     }
1604     if(!is_array($values) || !count($values)){
1605       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1606       return($ret);
1607     }
1609     /* Check if the given server is a valid mac address
1610      */
1611     if(!tests::is_mac($server)){
1612       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1613       return($ret);
1614     }
1616     /* Transform array into <xml>
1617      */
1618     $attrs = "";
1619     foreach($values as $id => $value){
1620       if(preg_match("/^policy$/i",$id)) continue;
1621       if(empty($id) || is_numeric($id)){
1622         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1623         return($ret);
1624       }
1625       $id = strtolower($id);
1626       $attrs.= "<$id>$value</$id>\n";
1627     }
1629     /* Prepare request event 
1630      */ 
1631     $xml_msg = 
1632       "<xml>".
1633       "<header>gosa_krb5_modify_policy</header>".
1634       "<policy>".$name."</policy>".
1635       $attrs.
1636       "<source>GOSA</source>".
1637       "<target>".$server."</target>".
1638       "</xml>";
1640     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1641   }
1643   
1644   /*! \brief  Removes the given password policy. 
1645               (Uses the GOsa support daemon instead of the ldap database.)
1646       @return Boolean  TRUE on success else FALSE
1647    */
1648   public function krb5_del_policy($server,$name)
1649   {
1650     $ret = FALSE;
1652     /* Check if the given server is a valid mac address
1653      */
1654     if(!tests::is_mac($server)){
1655       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1656       return($ret);
1657     }
1659     /* Check if the given name is a valid request value 
1660      */
1661     if(!is_string($name) || empty($name)){
1662       trigger_error("The given policy name is not of type string or it is empty.");
1663       return($ret);
1664     }
1666     /* Prepare request event 
1667      */ 
1668     $xml_msg = 
1669       "<xml>".
1670       "<header>gosa_krb5_del_policy</header>".
1671       "<policy>".$name."</policy>".
1672       "<source>GOSA</source>".
1673       "<target>".$server."</target>".
1674       "</xml>";
1675     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1676   }
1679   /*! \brief  Sets the password of for the given principal.
1680               (Uses the GOsa support daemon instead of the ldap database.)
1681       @param  String  The servers mac
1682       @param  String  The principals name
1683       @param  String  $the new password.   
1684       @return Boolean  TRUE on success else FALSE
1685    */
1686   public function krb5_set_password($server,$name,$password)
1687   {
1688     $ret = FALSE;
1690     /* Check if the given server is a valid mac address
1691      */
1692     if(!tests::is_mac($server)){
1693       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1694       return($ret);
1695     }
1697     /* Check if the given name is a valid request value
1698      */
1699     if(!is_string($name) || empty($name)){
1700       trigger_error("The given principal name is not of type string or it is empty.");
1701       return($ret);
1702     }
1704     /* Prepare request event
1705      */
1706     $xml_msg =
1707       "<xml>".
1708       "<header>gosa_krb5_set_password</header>".
1709       "<principal>".$name."</principal>".
1710       "<password>".$password."</password>".
1711       "<source>GOSA</source>".
1712       "<target>".$server."</target>".
1713       "</xml>";
1714     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1715   }
1718   /*! \brief  Returns log file informations for a given mac address 
1719       @param  $mac The mac address to fetch logs for.
1720       @retrun Array A Multidimensional array containing log infos.
1721         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1722         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1723                                install_20080313_144450   ...
1724    */
1725   public function get_log_info_for_mac($mac)
1726   {
1727     $xml_msg = "
1728       <xml>
1729       <header>gosa_show_log_by_mac</header>
1730       <target>GOSA</target>
1731       <source>GOSA</source>
1732       <mac>".$mac."</mac>
1733       </xml>";
1735     $res = $this->_send($xml_msg,TRUE);
1736     $ret = array();
1737     if(isset($res['XML'])){
1739       /* Filter all entry that look like this 
1740           MAC_00_01_6C_9D_B9_FA
1741        */
1742       foreach($res['XML'] as $name => $entry){
1743         if(preg_match("/^MAC/",$name)){
1745           /* Get list of available log files 
1746            */
1747           foreach($entry as $log_date){
1748             $xml_msg2 = "<xml> 
1749               <header>gosa_show_log_files_by_date_and_mac</header> 
1750               <target>GOSA</target> 
1751               <source>GOSA</source>                        
1752               <date>".$log_date."</date> 
1753               <mac>".$mac."</mac> 
1754               </xml>";
1756             $ret[$mac][$log_date] = array();
1757             $res = $this->_send($xml_msg2,TRUE);
1758             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1759             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1760             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1761               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1762             }
1763           }
1764         }
1765       }
1766     }
1767     return($ret);
1768   }
1770   public function get_log_file($mac,$date,$file)
1771   {
1772     $xml_msg ="
1773       <xml> 
1774       <header>gosa_get_log_file_by_date_and_mac</header> 
1775       <target>GOSA</target> 
1776       <source>GOSA</source>
1777       <date>".$date."</date> 
1778       <mac>".$mac."</mac> 
1779       <log_file>".$file."</log_file>
1780       </xml>";
1782     $res = $this->_send($xml_msg,TRUE);
1783     if(isset($res['XML'][strtoupper($file)])){
1784       return(base64_decode($res['XML'][strtoupper($file)]));
1785     }
1786     return("");
1787   }
1793   /*****************
1794    * DAK - Functions 
1795    *****************/
1797   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1798       @param  ...
1799       @return Array   All queued entries.
1800    */
1801   public function DAK_keyring_entries($server)  
1802   {
1803     /* Ensure that we send the event to a valid mac address 
1804      */
1805     if(!is_string($server) || !tests::is_mac($server)){
1806       trigger_error("No valid mac address given '".$server."'.");
1807       return;
1808     }
1810     /* Create query
1811      */
1812     $xml_msg = "<xml> 
1813                   <header>gosa_get_dak_keyring</header> 
1814                   <target>".$server."</target> 
1815                   <source>GOSA</source>
1816                 </xml>";
1817         
1818     $res = $this->_send($xml_msg,TRUE);
1820     /* Check if there are results for POLICY
1821      */
1822     if(isset($res['XML'])){
1823       $ret = array();
1824       foreach($res['XML'] as $key => $entry){
1825         if(preg_match("/^ANSWER/",$key)){
1826           $ret[] = $entry;
1827         }
1828       }
1829       return($ret);
1830     }else{
1831       return(array());
1832     }
1833   }
1836   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1837       @param  String  The servers mac address 
1838       @param  String  The gpg key.
1839       @return Boolean TRUE on success else FALSE 
1840    */
1841   public function DAK_import_key($server,$key)  
1842   {
1843     /* Ensure that we send the event to a valid mac address 
1844      */
1845     if(!is_string($server) || !tests::is_mac($server)){
1846       trigger_error("No valid mac address given '".$server."'.");
1847       return;
1848     }
1850     /* Check if there is some cleanup required before importing the key.
1851         There may be some Header lines like:
1852         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1853      */
1854     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1856       /* Remove header */
1857       $key = preg_replace("/^.*\n\n/sim","",$key);
1858       /* Remove footer */
1859       $key = preg_replace("/-----.*$/sim","",$key);
1860     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1861       
1862       /* Encode key if it is raw.
1863        */
1864       $key = base64_encode($key);
1865     }
1867     /* Create query
1868      */
1869     $xml_msg = "<xml> 
1870                   <header>gosa_import_dak_key</header> 
1871                   <target>".$server."</target> 
1872                   <key>".$key."</key> 
1873                   <source>GOSA</source>
1874                 </xml>";
1875         
1876     $res = $this->_send($xml_msg,TRUE);
1877     return($this->is_error());
1878   }
1881   /*! \brief Removes a key from the keyring on the given server. 
1882       @param  String  The servers mac address 
1883       @param  String  The gpg key uid.
1884       @return Boolean TRUE on success else FALSE 
1885    */
1886   public function DAK_remove_key($server,$key)  
1887   {
1888     /* Ensure that we send the event to a valid mac address 
1889      */
1890     if(!is_string($server) || !tests::is_mac($server)){
1891       trigger_error("No valid mac address given '".$server."'.");
1892       return;
1893     }
1895     /* Create query
1896      */
1897     $xml_msg = "<xml> 
1898                   <header>gosa_remove_dak_key</header> 
1899                   <target>".$server."</target> 
1900                   <keyid>".$key."</keyid> 
1901                   <source>GOSA</source>
1902                 </xml>";
1903        
1904     $res = $this->_send($xml_msg,TRUE);
1905     return($this->is_error());
1906   }
1909 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1910 ?>