Code

7f21eed9245f1a63705786425590700df9426da2
[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         if($key == "periodic"){
939           foreach($value as $sub_name => $sub_value){
940             $xml_message.= "<periodic>$sub_name</periodic>";
941             $xml_message.= "<$sub_name>$sub_value</$sub_name>";
942           }
943         }else{
944           foreach($value as $sub_value){
945             $xml_message.= "<$key>$sub_value</$key>";
946           }
947         }
948       }else{
949         $xml_message.= "<$key>$value</$key>";
950       }
951     }
953     /* Multiple targets? */
954     if (!is_array($to)){
955       $to_targets= array($to);
956     } else {
957       $to_targets= $to;
958     }
960     /* Build target strings */
961     $target ="";
962     foreach($to_targets as $to){
963       $target.= "<target>$to</target>";
964     }
966     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
967   }
970   /* Allows simply appending a new DaemonEvent 
971    */
972   public function append($event, $skip_add_mac = FALSE)
973   {
974     if(!($event instanceof DaemonEvent)){
975       return(FALSE);
976     }
977   
979     /* Add to queue if new 
980      */
981     if($event->is_new()){
983       $request_answer = FALSE;
984       if($event->get_type() == SCHEDULED_EVENT){
985         $action = $event->get_schedule_action();
986       }elseif($event->get_type() == TRIGGERED_EVENT){
987         $action = $event->get_trigger_action();
988       }else{
989         trigger_error("Unknown type of queue event given.");
990         return(FALSE);
991       }
993       /* Get event informations, like targets..
994        */
995       $targets    = $event->get_targets();
996       $data       = $event->save();
998       /* Append an entry for each target 
999        */
1000       foreach($targets as $target){
1001         if(!$skip_add_mac){
1002           $data['macaddress'] = $target;
1003         }
1004         $this->send_data($action,$target,$data,$request_answer);
1006         if($this->is_error()){
1007           return(FALSE);
1008         }
1009       }
1010       return(TRUE);
1011     }else{
1013       /* Updated edited entry.
1014        */
1015       $id                 = $event->get_id();
1016       $data               = $event->save();
1017       return($this->update_entries(array($id),$data));
1018     }
1020     return(FALSE);
1021   }
1024   /*! \brief  Returns an array containing all queued entries.
1025     @return Array All queued entries as an array.
1026    */
1027   public function _send($data, $answer_expected= FALSE)
1028   {
1030     $ret = array();
1031     if(!$this->connect()){
1032       return($ret);
1033     }
1034   
1035     $this->reset_error();
1037     /******
1038       Debug handling
1039      ******/
1040     $debug = debug_backtrace();
1041     $file = __FILE__;
1042     $function = __FUNCTION__;
1043     $line = __LINE__;
1044     $class = __CLASS__;
1045     foreach($debug as $info){
1046       if(!in_array($info['function'],array("send_data","_send"))){
1047         $file = $info['file'];
1048         $line = $info['line'];
1049         $class = get_class($this);
1050         $function = $info['function'];
1051         break;
1052       }
1053     }
1054     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1057     /*******
1058       Start sending data 
1059      *******/
1060     if($this->connect()){
1061       $this->o_sock->write($data);
1062       if ($answer_expected){
1063         $str = trim($this->o_sock->read());
1065         /* Check if something went wrong while reading */
1066         if($this->o_sock->is_error()){
1067           $this->set_error($this->o_sock->get_error());
1068           return($ret);
1069         }
1071         $entries = $this->xml_to_array($str);
1072         if(isset($entries['XML']) && is_array($entries['XML'])){
1073           $ret = $entries;
1074           if($this->use_alternative_xml_parse_method) {
1075             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1076               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1077               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1078                   array($data=>$data),"FAILED ".$this->get_error());
1079             }
1080           }else{
1081             if(isset($entries['XML']['ERROR_STRING'])) {
1082               $this->set_error($entries['XML']['ERROR_STRING']);
1083               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1084                   array($data=>$data),"FAILED ".$this->get_error());
1085             }elseif(isset($entries['XML']['ERROR'])){
1086               $this->set_error($entries['XML']['ERROR']);
1087               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1088                   array($data=>$data),"FAILED ".$this->get_error());
1089             }
1090           }
1091           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1092               array($data=>$data),"SUCCESS");
1093         }
1094       }else{
1095         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1096             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1097       }
1098     }
1099     return($ret);
1100   }
1103   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1104   {
1105     $xml_message= "";
1107     /* Get communication object */
1108     $d= new gosaSupportDaemon(TRUE,10);
1110     /* Prepare data */
1111     foreach ($data as $key => $value){
1112       if(is_array($value)){
1113         foreach($value as $sub_val){
1114           $xml_message.= "<$key>$sub_val</$key>";
1115         }
1116       }else{
1117         $xml_message.= "<$key>$value</$key>";
1118       }
1119     }
1121     /* Multiple targets? */
1122     if (!is_array($to)){
1123       $to_targets= array($to);
1124     } else {
1125       $to_targets= $to;
1126     }
1128     /* Build target strings */
1129     $target ="";
1130     foreach($to_targets as $to){
1131       $target.= "<target>$to</target>";
1132     }
1134     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1135   }
1138   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1139       @param  String  $mac  The mac address for which we want to remove all jobs.      
1140    */
1141   function clean_queue_from_mac($mac)
1142   {
1143     global $config;
1145     /* First of all we have to check which jobs are startet 
1146      *  for $mac 
1147      */
1148     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1149     
1150     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1151  
1152     $data = $this->_send($xml_msg,TRUE);
1153     if(is_array($data) && isset($data['XML'])){
1154       $already_aborted = FALSE;
1155       foreach($data['XML']  as $name => $entry){
1156         if(preg_match("/answer[0-9]*/i",$name)){
1157           $entry['STATUS'] = strtoupper($entry['STATUS']);
1158           switch($entry['STATUS']){
1160             case 'PROCESSING' :
1162               /* Send abort event, but only once 
1163                */
1164               if($already_aborted){
1165                 break;
1166               }elseif(class_available("DaemonEvent_faireboot")){
1167                 $already_aborted = TRUE;
1168                 $tmp = new DaemonEvent_faireboot($config);
1169                 $tmp->add_targets(array($mac));
1170                 $tmp->set_type(TRIGGERED_EVENT);
1171                 if(!$this->append($tmp)){
1172                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1173                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1174                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1175                 }else{
1176                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1177                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1178                 }
1179                 ;break;
1180               }else{
1181                 /* Couldn't find abort event, just remove entry */
1182               }
1184             case 'WAITING':
1185             case 'ERROR':
1186             default :
1187             
1188               /* Simply remove entries from queue. 
1189                *  Failed or waiting events, can be removed without any trouble.
1190                */ 
1191               if(!$this->remove_entries(array($entry['ID']))){
1192                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1193               }
1194               ;break;
1195           }
1196     
1197         }
1198       }
1199     }
1200   }
1203   static function ping($target)
1204   {
1205     if (tests::is_mac($target)){
1206       /* Get communication object */
1207       $d= new gosaSupportDaemon(TRUE,2);
1208       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1209       return (count($answer) ? TRUE:FALSE);
1210     }
1211     return (FALSE);
1212   }
1216   /*! \brief  Returns a list of all configured principals. 
1217               (Uses the GOsa support daemon instead of the ldap database.)
1218       @return Array  A list containing the names of all configured principals.
1219    */
1220   public function krb5_list_principals($server)
1221   {
1222     $res = array();  
1224     /* Check if the given server is a valid mac address
1225      */
1226     if(!tests::is_mac($server)){
1227       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1228       return($ret);
1229     }
1231     /* Prepare request event 
1232      */ 
1233     $xml_msg = 
1234       "<xml>".
1235       "<header>gosa_krb5_list_principals</header>".
1236       "<source>GOSA</source>".
1237       "<target>".$server."</target>".
1238       "</xml>";
1239     
1240     $tmp = $this->_send($xml_msg,TRUE);
1241     if(isset($tmp['XML']['PRINCIPAL'])){
1242       return($tmp['XML']['PRINCIPAL']);
1243     }else{
1244       return($res);
1245     }
1246   }
1249   /*! \brief  Returns the configuration settings for a given principal name. 
1250               (Uses the GOsa support daemon instead of the ldap database.)
1251       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1252       @return Array  A list containing the names of all configured principals.
1253    */
1254   public function krb5_get_principal($server,$name)
1255   {
1256     $ret = array();
1258     /* Check if the given name is a valid request value 
1259      */
1260     if(!is_string($name) || empty($name)){
1261       trigger_error("The given principal name is not of type string or it is empty.");
1262       return($ret);
1263     }
1265     /* Check if the given server is a valid mac address
1266      */
1267     if(!tests::is_mac($server)){
1268       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1269       return($ret);
1270     }
1272     /* Prepare request event 
1273      */ 
1274     $xml_msg = 
1275       "<xml>".
1276       "<header>gosa_krb5_get_principal</header>".
1277       "<principal>".$name."</principal>".
1278       "<source>GOSA</source>".
1279       "<target>".$server."</target>".
1280       "</xml>";
1282     $res = $this->_send($xml_msg,TRUE);
1283     if(isset($res['XML'])){
1284       return($res['XML']);
1285     }else{
1286       return($ret);
1287     }
1288   }
1291   /*! \brief  Creates a given principal with a set of configuration settings.
1292               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1293               (Uses the GOsa support daemon instead of the ldap database.)
1294       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1295       @return Boolean   TRUE on success else FALSE. 
1296    */
1297   public function krb5_add_principal($server,$name,$values)
1298   {
1299     $ret = FALSE;  
1301     /* Check if the given name is a valid request value 
1302      */
1303     if(!is_string($name) || empty($name)){
1304       trigger_error("The given principal name is not of type string or it is empty.");
1305       return($ret);
1306     }
1307     if(!is_array($values)){
1308       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1309       return($ret);
1310     }
1312     /* Check if the given server is a valid mac address
1313      */
1314     if(!tests::is_mac($server)){
1315       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1316       return($ret);
1317     }
1319     $attrs = "";
1320     foreach($values as $key => $value){
1321       if(empty($key) || is_numeric($key)){
1322         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1323         return($ret);
1324       }
1325       $key = strtolower($key);
1326       if(is_array($value)){
1327         foreach($value as $val){
1328           $attrs.= "<$key>$val</$key>\n";
1329         }
1330       }else{
1331         $attrs.= "<$key>$value</$key>\n";
1332       }
1333     }
1335     /* Prepare request event 
1336      */ 
1337     $xml_msg = 
1338       "<xml>".
1339       "<header>gosa_krb5_create_principal</header>".
1340       "<principal>".$name."</principal>".
1341       $attrs.
1342       "<source>GOSA</source>".
1343       "<target>".$server."</target>".
1344       "</xml>";
1346     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1347   }
1350   function krb5_ramdomize_key($server,$name)  
1351   {
1352     /* Prepare request event 
1353      */ 
1354     $xml_msg = 
1355       "<xml>".
1356       "<header>gosa_krb5_randomize_key</header>".
1357       "<principal>".$name."</principal>".
1358       "<source>GOSA</source>".
1359       "<target>".$server."</target>".
1360       "</xml>";
1362     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1363   }
1364   
1367   /*! \brief  Updates a given principal with a set of configuration settings.
1368               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1369               (Uses the GOsa support daemon instead of the ldap database.)
1370       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1371       @return Boolean   TRUE on success else FALSE. 
1372    */
1373   public function krb5_set_principal($server,$name,$values)
1374   {
1375     $ret = FALSE;  
1377     /* Check if the given name is a valid request value 
1378      */
1379     if(!is_string($name) || empty($name)){
1380       trigger_error("The given principal name is not of type string or it is empty.");
1381       return($ret);
1382     }
1383     if(!is_array($values) || !count($values)){
1384       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1385       return($ret);
1386     }
1388     /* Check if the given server is a valid mac address
1389      */
1390     if(!tests::is_mac($server)){
1391       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1392       return($ret);
1393     }
1395     $attrs = "";
1396     foreach($values as $key => $value){
1397       if(empty($key) || is_numeric($key)){
1398         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1399         return($ret);
1400       }
1401       $key = strtolower($key);
1402       if(is_array($value)){
1403         foreach($value as $val){
1404           $attrs.= "<$key>$val</$key>\n";
1405         }
1406       }else{
1407         $attrs.= "<$key>$value</$key>\n";
1408       }
1409     }
1411     /* Prepare request event 
1412      */ 
1413     $xml_msg = 
1414       "<xml>".
1415       "<header>gosa_krb5_modify_principal</header>".
1416       "<principal>".$name."</principal>".
1417       $attrs.
1418       "<source>GOSA</source>".
1419       "<target>".$server."</target>".
1420       "</xml>";
1422     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1423   }
1426   /*! \brief  Removes the given principal.
1427               (Uses the GOsa support daemon instead of the ldap database.)
1428       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1429       @return Boollean   TRUE on success else FALSE
1430    */
1431   public function krb5_del_principal($server,$name)
1432   {
1433     $ret = FALSE;  
1435     /* Check if the given name is a valid request value 
1436      */
1437     if(!is_string($name) || empty($name)){
1438       trigger_error("The given principal name is not of type string or it is empty.");
1439       return($ret);
1440     }
1442     /* Check if the given server is a valid mac address
1443      */
1444     if(!tests::is_mac($server)){
1445       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1446       return($ret);
1447     }
1449     /* Prepare request event 
1450      */ 
1451     $xml_msg = 
1452       "<xml>".
1453       "<header>gosa_krb5_del_principal</header>".
1454       "<principal>".$name."</principal>".
1455       "<source>GOSA</source>".
1456       "<target>".$server."</target>".
1457       "</xml>";
1458     
1459     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1460   }
1463   /*! \brief  Returns a list of configured password policies.
1464               (Uses the GOsa support daemon instead of the ldap database.)
1465       @return Array A list of all configured password policies.
1466    */
1467   public function krb5_list_policies($server)
1468   {
1469     $res = array();  
1471     /* Check if the given server is a valid mac address
1472      */
1473     if(!tests::is_mac($server)){
1474       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1475       return($ret);
1476     }
1478     /* Prepare request event 
1479      */ 
1480     $xml_msg = 
1481       "<xml>".
1482       "<header>gosa_krb5_list_policies</header>".
1483       "<source>GOSA</source>".
1484       "<target>".$server."</target>".
1485       "</xml>";
1486     
1487     $res = $this->_send($xml_msg,TRUE);
1488     
1489     /* Check if there are results for POLICY 
1490      */
1491     if(isset($res['XML']['POLICY'])){
1492       
1493       /* Ensure that we return an array 
1494        */
1495       $tmp = $res['XML']['POLICY'];
1496       if(!is_array($tmp)){
1497         $tmp = array($tmp);
1498       }
1499       return($tmp);
1500     }else{
1501       return(array());
1502     }
1503   }
1506   /*! \brief  Returns a list of configured password policies.
1507               (Uses the GOsa support daemon instead of the ldap database.)
1508       @return Array The policy settings for the given policy name.
1509    */
1510   public function krb5_get_policy($server,$name)
1511   {
1512     $ret = array();  
1514     /* Check if the given name is a valid request value 
1515      */
1516     if(!is_string($name) || empty($name)){
1517       trigger_error("The given policy name is not of type string or it is empty.");
1518       return($ret);
1519     }
1521     /* Check if the given server is a valid mac address
1522      */
1523     if(!tests::is_mac($server)){
1524       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1525       return($ret);
1526     }
1528     /* Prepare request event 
1529      */ 
1530     $xml_msg = 
1531       "<xml>".
1532       "<header>gosa_krb5_get_policy</header>".
1533       "<policy>".$name."</policy>".
1534       "<source>GOSA</source>".
1535       "<target>".$server."</target>".
1536       "</xml>";
1538     /* Possible attributes */
1539     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1540         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1542   
1543     $tmp = $this->_send($xml_msg,TRUE);
1544     if(isset($tmp['XML'])){
1545       foreach($attrs as $attr){
1546         if(isset($tmp['XML'][$attr])){
1547           $ret[$attr] = $tmp['XML'][$attr];
1548         }else{
1549           $ret[$attr] = "";
1550         }
1551       }
1552     }
1553     return($ret);
1554   }
1556   
1557   /*! \brief  Creates a new policy with a given set of configuration settings.
1558               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1559               (Uses the GOsa support daemon instead of the ldap database.)
1560       @pram   String The name of the policy to update.
1561       @pram   Array  The attributes to update
1562       @return Boolean   TRUE on success else FALSE. 
1563    */
1564   public function krb5_add_policy($server,$name,$values)
1565   {
1566     $ret = FALSE;  
1568     /* Check if the given name is a valid request value 
1569      */
1570     if(!is_string($name) || empty($name)){
1571       trigger_error("The given policy name is not of type string or it is empty.");
1572       return($ret);
1573     }
1574     if(!is_array($values) || !count($values)){
1575       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1576       return($ret);
1577     }
1579     /* Check if the given server is a valid mac address
1580      */
1581     if(!tests::is_mac($server)){
1582       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1583       return($ret);
1584     }
1587     /* Transform array into <xml>
1588      */
1589     $attrs = "";
1590     foreach($values as $id => $value){
1591       if(empty($id) || is_numeric($id)){
1592         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1593         return($ret);
1594       }
1595       $id = strtolower($id);
1596       $attrs.= "<$id>$value</$id>\n";
1597     }
1599     /* Prepare request event 
1600      */ 
1601     $xml_msg = 
1602       "<xml>".
1603       "<header>gosa_krb5_create_policy</header>".
1604       "<policy>".$name."</policy>".
1605       $attrs.
1606       "<source>GOSA</source>".
1607       "<target>".$server."</target>".
1608       "</xml>";
1610     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1611   }
1614   /*! \brief  Updates a given policy with a set of configuration settings.
1615               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1616               (Uses the GOsa support daemon instead of the ldap database.)
1617       @pram   String The name of the policy to update.
1618       @return Boolean   TRUE on success else FALSE. 
1619    */
1620   public function krb5_set_policy($server,$name,$values)
1621   {
1622     $ret = FALSE;  
1624     /* Check if the given name is a valid request value 
1625      */
1626     if(!is_string($name) || empty($name)){
1627       trigger_error("The given policy name is not of type string or it is empty.");
1628       return($ret);
1629     }
1630     if(!is_array($values) || !count($values)){
1631       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1632       return($ret);
1633     }
1635     /* Check if the given server is a valid mac address
1636      */
1637     if(!tests::is_mac($server)){
1638       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1639       return($ret);
1640     }
1642     /* Transform array into <xml>
1643      */
1644     $attrs = "";
1645     foreach($values as $id => $value){
1646       if(preg_match("/^policy$/i",$id)) continue;
1647       if(empty($id) || is_numeric($id)){
1648         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1649         return($ret);
1650       }
1651       $id = strtolower($id);
1652       $attrs.= "<$id>$value</$id>\n";
1653     }
1655     /* Prepare request event 
1656      */ 
1657     $xml_msg = 
1658       "<xml>".
1659       "<header>gosa_krb5_modify_policy</header>".
1660       "<policy>".$name."</policy>".
1661       $attrs.
1662       "<source>GOSA</source>".
1663       "<target>".$server."</target>".
1664       "</xml>";
1666     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1667   }
1669   
1670   /*! \brief  Removes the given password policy. 
1671               (Uses the GOsa support daemon instead of the ldap database.)
1672       @return Boolean  TRUE on success else FALSE
1673    */
1674   public function krb5_del_policy($server,$name)
1675   {
1676     $ret = FALSE;
1678     /* Check if the given server is a valid mac address
1679      */
1680     if(!tests::is_mac($server)){
1681       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1682       return($ret);
1683     }
1685     /* Check if the given name is a valid request value 
1686      */
1687     if(!is_string($name) || empty($name)){
1688       trigger_error("The given policy name is not of type string or it is empty.");
1689       return($ret);
1690     }
1692     /* Prepare request event 
1693      */ 
1694     $xml_msg = 
1695       "<xml>".
1696       "<header>gosa_krb5_del_policy</header>".
1697       "<policy>".$name."</policy>".
1698       "<source>GOSA</source>".
1699       "<target>".$server."</target>".
1700       "</xml>";
1701     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1702   }
1705   /*! \brief  Sets the password of for the given principal.
1706               (Uses the GOsa support daemon instead of the ldap database.)
1707       @param  String  The servers mac
1708       @param  String  The principals name
1709       @param  String  $the new password.   
1710       @return Boolean  TRUE on success else FALSE
1711    */
1712   public function krb5_set_password($server,$name,$password)
1713   {
1714     $ret = FALSE;
1716     /* Check if the given server is a valid mac address
1717      */
1718     if(!tests::is_mac($server)){
1719       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1720       return($ret);
1721     }
1723     /* Check if the given name is a valid request value
1724      */
1725     if(!is_string($name) || empty($name)){
1726       trigger_error("The given principal name is not of type string or it is empty.");
1727       return($ret);
1728     }
1730     /* Prepare request event
1731      */
1732     $xml_msg =
1733       "<xml>".
1734       "<header>gosa_krb5_set_password</header>".
1735       "<principal>".$name."</principal>".
1736       "<password>".$password."</password>".
1737       "<source>GOSA</source>".
1738       "<target>".$server."</target>".
1739       "</xml>";
1740     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1741   }
1744   /*! \brief  Returns log file informations for a given mac address 
1745       @param  $mac The mac address to fetch logs for.
1746       @retrun Array A Multidimensional array containing log infos.
1747         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1748         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1749                                install_20080313_144450   ...
1750    */
1751   public function get_log_info_for_mac($mac)
1752   {
1753     $xml_msg = "
1754       <xml>
1755       <header>gosa_show_log_by_mac</header>
1756       <target>GOSA</target>
1757       <source>GOSA</source>
1758       <mac>".$mac."</mac>
1759       </xml>";
1761     $res = $this->_send($xml_msg,TRUE);
1762     $ret = array();
1763     if(isset($res['XML'])){
1765       /* Filter all entry that look like this 
1766           MAC_00_01_6C_9D_B9_FA
1767        */
1768       foreach($res['XML'] as $name => $entry){
1769         if(preg_match("/^MAC/",$name)){
1771           /* Get list of available log files 
1772            */
1773           if(!is_array($entry)){
1774             $entry = array($entry);
1775           }
1776           foreach($entry as $log_date){
1777             $xml_msg2 = "<xml> 
1778               <header>gosa_show_log_files_by_date_and_mac</header> 
1779               <target>GOSA</target> 
1780               <source>GOSA</source>                        
1781               <date>".$log_date."</date> 
1782               <mac>".$mac."</mac> 
1783               </xml>";
1784    
1785             $ret[$mac][$log_date] = array();
1786             $res = $this->_send($xml_msg2,TRUE);
1787             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1788             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1789             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1790               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1791             }
1792           }
1793         }
1794       }
1795     }
1796     return($ret);
1797   }
1799   public function get_log_file($mac,$date,$file)
1800   {
1801     $xml_msg ="
1802       <xml> 
1803       <header>gosa_get_log_file_by_date_and_mac</header> 
1804       <target>GOSA</target> 
1805       <source>GOSA</source>
1806       <date>".$date."</date> 
1807       <mac>".$mac."</mac> 
1808       <log_file>".$file."</log_file>
1809       </xml>";
1811     $res = $this->_send($xml_msg,TRUE);
1812     if(isset($res['XML'][strtoupper($file)])){
1813       return(base64_decode($res['XML'][strtoupper($file)]));
1814     }
1815     return("");
1816   }
1822   /*****************
1823    * DAK - Functions 
1824    *****************/
1826   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1827       @param  ...
1828       @return Array   All queued entries.
1829    */
1830   public function DAK_keyring_entries($server)  
1831   {
1832     /* Ensure that we send the event to a valid mac address 
1833      */
1834     if(!is_string($server) || !tests::is_mac($server)){
1835       trigger_error("No valid mac address given '".$server."'.");
1836       return;
1837     }
1839     /* Create query
1840      */
1841     $xml_msg = "<xml> 
1842                   <header>gosa_get_dak_keyring</header> 
1843                   <target>".$server."</target> 
1844                   <source>GOSA</source>
1845                 </xml>";
1846         
1847     $res = $this->_send($xml_msg,TRUE);
1849     /* Check if there are results for POLICY
1850      */
1851     if(isset($res['XML'])){
1852       $ret = array();
1853       foreach($res['XML'] as $key => $entry){
1854         if(preg_match("/^ANSWER/",$key)){
1855           $ret[] = $entry;
1856         }
1857       }
1858       return($ret);
1859     }else{
1860       return(array());
1861     }
1862   }
1865   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1866       @param  String  The servers mac address 
1867       @param  String  The gpg key.
1868       @return Boolean TRUE on success else FALSE 
1869    */
1870   public function DAK_import_key($server,$key)  
1871   {
1872     /* Ensure that we send the event to a valid mac address 
1873      */
1874     if(!is_string($server) || !tests::is_mac($server)){
1875       trigger_error("No valid mac address given '".$server."'.");
1876       return;
1877     }
1879     /* Check if there is some cleanup required before importing the key.
1880         There may be some Header lines like:
1881         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1882      */
1883     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1885       /* Remove header */
1886       $key = preg_replace("/^.*\n\n/sim","",$key);
1887       /* Remove footer */
1888       $key = preg_replace("/-----.*$/sim","",$key);
1889     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1890       
1891       /* Encode key if it is raw.
1892        */
1893       $key = base64_encode($key);
1894     }
1896     /* Create query
1897      */
1898     $xml_msg = "<xml> 
1899                   <header>gosa_import_dak_key</header> 
1900                   <target>".$server."</target> 
1901                   <key>".$key."</key> 
1902                   <source>GOSA</source>
1903                 </xml>";
1904         
1905     $res = $this->_send($xml_msg,TRUE);
1906     return($this->is_error());
1907   }
1910   /*! \brief Removes a key from the keyring on the given server. 
1911       @param  String  The servers mac address 
1912       @param  String  The gpg key uid.
1913       @return Boolean TRUE on success else FALSE 
1914    */
1915   public function DAK_remove_key($server,$key)  
1916   {
1917     /* Ensure that we send the event to a valid mac address 
1918      */
1919     if(!is_string($server) || !tests::is_mac($server)){
1920       trigger_error("No valid mac address given '".$server."'.");
1921       return;
1922     }
1924     /* Create query
1925      */
1926     $xml_msg = "<xml> 
1927                   <header>gosa_remove_dak_key</header> 
1928                   <target>".$server."</target> 
1929                   <keyid>".$key."</keyid> 
1930                   <source>GOSA</source>
1931                 </xml>";
1932        
1933     $res = $this->_send($xml_msg,TRUE);
1934     return($this->is_error());
1935   }
1938 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1939 ?>