Code

Updated a couple of values
[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   /*! \brief  Creates a new gosaSupportDaemon object.
39     @param string   Host    The Host where the daemon is running on.  
40     @param integer  Port    The port which the daemon use.
41     @param string   Key     The encryption string.
42     @param boolean  Connect Directly connect to daemon socket.
43     @param float    Timeout The timelimit for all socket actions.
44    */
45   public function __construct($connect=TRUE,$timeout=10)
46   {
47     #FIXME: bad idea about referencing global variables from within classes
48     global $config;
50     /* This should only be the case if we call this from setup.
51         __autoload() 
52      */
53     if(!is_object($config)) { return; }
55     # load from config, store statically
56     if ($config->get_cfg_value("gosa_si") != ""){
58       if ($this->s_host == ""){
59         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosa_si"));
60         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosa_si"));
61         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosa_si"));
62       }
64       $this->f_timeout = $timeout;
65       if($connect){
66         $this->connect();
67       }
68     }
69   }
72   /*! \brief  Establish daemon connection. 
73     @return boolean Returns true if the connection was succesfully established. 
74    */
75   public function connect()
76   {
77     if(!empty($this->s_host) && !empty($this->i_port)){
78       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
79       if($this->o_sock->connected()){ 
80         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
81         $this->is_connected = TRUE;
82       }else{
83         $this->set_error($this->o_sock->get_error());
84         $this->disconnect();
85         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
86       }
87     }else{
88       $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
89     }
90     return($this->is_connected);
91   }
93   
94   /*! \brief  Returns TRUE whether we are connected or not 
95       @return BOOLEAN  Returns TRUE when connected else FALSE
96    */
97   public function is_connected()
98   {
99     return($this->is_connected);
100   }
101  
103   /*! \brief  */
104   public function get_hosts_with_module($mod)
105   {
106     $data = array("module_name" => $mod);
107     $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
108     $hosts = array();
109     if(isset($res['XML'])){
110       foreach($res['XML'][0] as $name => $data){
111         if(preg_match("/^ANSWER[0-9]*$/",$name)){
112           if(isset($data[0]['MAC'][0]['VALUE'])){
113             $hosts[] = $data[0]['MAC'][0]['VALUE'];
114           }elseif(isset($data[0]['VALUE'])){
115             $hosts[] = $data[0]['VALUE'];
116           }
117         }
118       }
119     }
120     return($hosts);
121   }
124   /*! \brief  Disconnect from gosa daemon.
125    */
126   public function disconnect()
127   {
128     $this->o_sock->close();
129     $this->is_connected = FALSE;
130   }
133   /*! \brief  Sets an error message, which can be returned with get_error().
134     @param  string  The Error message,
135    */
136   private function set_error($str)
137   {
138     /******
139       Debug handling
140      ******/
141     $debug = debug_backtrace();
142     $file = __FILE__;
143     $function = __FUNCTION__;
144     $line = __LINE__;
145     $class = __CLASS__;
146     foreach($debug as $info){
147       if(!in_array($info['function'],array("send_data","_send","set_error","connect"))){
148         $file = $info['file'];
149         $line = $info['line'];
150         $class = get_class($this);
151         $function = $info['function'];
152         break;
153       }
154     }
155     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
157     /******
158       Set error string.
159      ******/
160   
161     $this->b_error = TRUE;
162     $this->s_error = $str;
163   }
166   /*! \brief  Sets an error message, which can be returned with get_error().
167     @param  string  The Error message,
168    */
169   private function reset_error()
170   {
171     $this->b_error = FALSE;
172     $this->s_error = "";
173   }
176   /*! \brief  Checks if an error occured.
177     @return boolean returns TRUE or FALSE, whether there is an error or not.
178    */
179   public function is_error()
180   {
181     return($this->b_error);
182   }
185   /*! \brief  Returns the last error. 
186     @return Returns the last error.
187    */
188   public function get_error()
189   {
190     $str = $this->s_error;
191     $ret = "";
192     if(is_string($str)){
193       $ret = $str;
194     }else{
195       foreach($str as $msg){
196         $ret .= $msg." ";
197       }
198     }
199     $ret = preg_replace("/ /","&nbsp;",$ret);
200     return($ret);
201   }
204   public function FAI_get_kernels($release)
205   {
206     $xml_msg = 
207       "<xml>".
208       "<header>gosa_get_available_kernel</header>".
209       "<source>GOSA</source>".
210       "<target>GOSA</target>".
211       "<release>".$release."</release>".
212       "</xml>";
214     $ret = array();
215     if($this->connect()){
216       $entries = $this->_send($xml_msg,TRUE);
218       /* Check if returned values represent a valid answer */
219       if(isset($entries['XML']) && is_array($entries['XML'])){
220         if(isset($entries['XML'])){
221           $ret = $entries['XML'];
222           foreach($ret as $key => $entry){
223             if(!preg_match("/^answer/i",$key)){
224               unset($ret[$key]);
225             }
226           }
227         }
228       }
229     }
230     return($ret);
231   }
234   public function FAI_get_package_sections($release)
235   {
236     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
237       "<select>distinct section</select>".
238       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
240     $ret = array();
241     if($this->connect()){
242       $entries = $this->_send($xml_msg,TRUE);
243       if(isset($entries['XML']) && is_array($entries['XML'])){
245         /* Unset header tags */
246         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
247           if(isset($entries['XML'][$type])){
248             unset($entries['XML'][$type]);
249           }
250         }
251         $ret = $entries['XML'];
252       }
253     }
254     return($ret);
255   }
258   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
259   {
260     $ret = array();
262     /* Check Parameter */
263     if(!is_array($attrs) || !count($attrs)){
264       trigger_error("Second parameter must be an array. With at least one attribute name.");
265       return($ret);
266     }
268     /* Check Parameter */
269     if(!is_array($package)){
270       trigger_error("Third parameter must be an array. With at least one attribute name.");
271       return($ret);
272     }
274     /* Create list of attributes to fetch */
275     $attr = ""; 
276     foreach($attrs as $at){
277       $attr.= "<select>".$at."</select>";
278     }
280     /* If no package is given, search for all */
281     if(!count($package)) $package = array("%");
283     /* Create limit tag */
284     if($from == -1){
285       $limit =""; 
286     }else{
287       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
288     }
290     /* Create list of attributes to fetch */
291     $pkgs = ""; 
292     foreach($package as $pkg){
293       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
294     }
296     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
297       $attr.
298       "<where>
299       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
300       <clause><connector>OR</connector>
301       ".$pkgs."
302       </clause>
303       </where>".
304       $limit.
305       "</xml>";
307     if($this->connect()){
308       $entries = $this->_send($xml_msg,TRUE);
309       if(isset($entries['XML']) && is_array($entries['XML'])){
311         /* Check if returned values represent a valid answer */
312         if(isset($entries['XML'])){
314           /* Unset header tags */
315           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
316             if(isset($entries['XML'][$type])){
317               unset($entries['XML'][$type]);
318             }
319           }
320           $ret = $entries['XML'];
321         }
322       }
323     }
324     return($ret);
326     
327   }
330   public function FAI_get_server($name = "")
331   {
333     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
334     $ret = array();
335     if($this->connect()){
337       /* Check if returned values represent a valid answer */
338       $entries = $this->_send($xml_msg,TRUE);
339       if(isset($entries['XML']) && is_array($entries['XML'])){
341         /* Unset header tags */
342         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
343           if(isset($entries['XML'][$type])){
344             unset($entries['XML'][$type]);
345           }
346         }
347         $ret = $entries['XML']; 
348       }
349     }
350     return($ret);
351   }
354   public function FAI_get_classes($name)
355   {
356     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
357                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
358     $ret = array();
359     if($this->connect()){
361       $entries = $this->_send($xml_msg,TRUE);
362       if(isset($entries['XML']) && is_array($entries['XML'])){
364         /* Unset header tags */
365         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
366           if(isset($entries['XML'][$type])){
367             unset($entries['XML'][$type]);
368           }
369         }
370         $ret = $entries['XML']; 
371       }
372     }
373     return($ret);
374   }
377   /*! \brief  Returns an array containing all queued entries.
378     @return Array All queued entries as an array.
379    */
380   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
381   {
382     $ret = array();
384     $tags = "";
385     foreach($event_types as $type){
386       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
387     }
388     if(count($event_types) > 1){
389       $tags = "<connector>or</connector>".$tags;
390     }
391     if(count($event_types)){
392       $tags = "<where><clause>".$tags."</clause></where>";
393     }
395     $xml_msg = 
396       "<xml>
397       <header>gosa_query_jobdb</header>
398       <target>GOSA</target>
399       <source>GOSA</source>
400       ".$tags."
402       <orderby>".$sort."</orderby>";
403     if($from != -1 && $to != -1){
404       $xml_msg.= "
405         <limit>
406         <from>".$from."</from>
407         <to>".$to."</to>
408         </limit>";
409     }
410     $xml_msg.= "
411       </xml>";
413     if($this->connect()){
414       $entries = $this->_send($xml_msg,TRUE);
415       if(isset($entries['XML']) && is_array($entries['XML'])){
417         /* Unset header tags */
418         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
419           unset($entries['XML'][$type]);
420         }
421         $ret = $entries['XML']; 
422       }
423     }
424     return($ret);
425   }
428   /*! \brief  Checks if the given ids are used queue ids.
429     @param  Array   The ids we want to check..
430     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
431    */
432   public function ids_exist($ids)
433   {
434     if(!is_array($ids)){
435       trigger_error("Requires an array as parameter.");
436       return;
437     }
439     $ret = array();
441     $xml_msg = "<xml>
442       <header>gosa_query_jobdb</header>
443       <target>GOSA</target>
444       <source>GOSA</source>
445       <where>
446       <clause>
447       <connector>or</connector>";
448     foreach($ids as $id){
449       $xml_msg .= "<phrase>
450         <operator>eq</operator>
451         <id>".$id."</id>
452         </phrase>";
453     }
454     $xml_msg .= "</clause>
455       </where>
456       </xml>";
458     if($this->connect()){
459       $entries = $this->_send($xml_msg,TRUE);
460       if(isset($entries['XML']) && is_array($entries['XML'])){
461         foreach($entries['XML'] as $entry){
462           if(is_array($entry) && array_key_exists("ID",$entry)){
463             $ret[] = $entry['ID'];
464           }
465         }
466       }
467     }
468     return($ret);
469   }
472   /*! \brief  Returns an entry containing all requested ids.
473     @param  Array   The IDs of the entries we want to return.
474     @return Array   Of the requested entries. 
475    */
476   public function get_entries_by_mac($macs)
477   {
478     if(!is_array($macs)){
479       trigger_error("Requires an array as parameter.");
480       return;
481     }
483     $ret = array();
485     $xml_msg = "<xml>
486       <header>gosa_query_jobdb</header>
487       <target>GOSA</target>
488       <source>GOSA</source>
489       <where>
490       <clause>
491       <connector>or</connector>";
492     foreach($macs as $mac){
493       $xml_msg .= "<phrase>
494         <operator>eq</operator>
495         <macaddress>".$mac."</macaddress>
496         </phrase>";
497     }
498     $xml_msg .= "</clause>
499       </where>
500       </xml>";
502     if($this->connect()){
503       $entries = $this->_send($xml_msg,TRUE);
504       if(isset($entries['XML'])){
505         foreach($entries['XML'] as $name => $entry){
506           if(preg_match("/^ANSWER[0-9]*$/",$name)){
507             $ret[$name] = $entry;
508           }
509         }
510       }
511     }
512     return($ret);
513   }
516   /*! \brief  Returns an entry containing all requested ids.
517     @param  Array   The IDs of the entries we want to return.
518     @return Array   Of the requested entries. 
519    */
520   public function get_entries_by_id($ids)
521   {
522     if(!is_array($ids)){
523       trigger_error("Requires an array as parameter.");
524       return;
525     }
527     $ret = array();
529     $xml_msg = "<xml>
530       <header>gosa_query_jobdb</header>
531       <target>GOSA</target>
532       <source>GOSA</source>
533       <where>
534       <clause>
535       <connector>or</connector>";
536     foreach($ids as $id){
537       $xml_msg .= "<phrase>
538         <operator>eq</operator>
539         <id>".$id."</id>
540         </phrase>";
541     }
542     $xml_msg .= "</clause>
543       </where>
544       </xml>";
546     if($this->connect()){
547       $entries = $this->_send($xml_msg,TRUE);
548       if(isset($entries['XML'])){
549         foreach($entries['XML'] as $name => $entry){
550           if(preg_match("/^ANSWER[0-9]*$/",$name)){
551             $ret[$name] = $entry;
552           }
553         }
554       }
555     }
556     return($ret);
557   }
560   /*! \brief  Checks if the given id is in use.
561     @param  Integer The ID of the entry.
562     @return Boolean TRUE if entry exists. 
563    */
564   public function id_exists($id)
565   {
566     if(!is_numeric($id)){
567       trigger_error("Requires an integer as parameter.");
568       return;
569     }
572     $xml_msg = "<xml>
573       <header>gosa_query_jobdb</header>
574       <target>GOSA</target>
575       <source>GOSA</source>
576       <where>
577       <clause>
578       <phrase>
579       <operator>eq</operator>
580       <id>".$id."</id>
581       </phrase>
582       </clause>
583       </where>
584       </xml>";
586     if($this->connect()){
587       $entries = $this->_send($xml_msg,TRUE);
588       if( isset($entries['XML']['HEADER']) && 
589           $entries['XML']['HEADER']=="answer" && 
590           isset($entries['XML']['ANSWER1'])){
591         return(TRUE);
592       }
593     }
594     return(FALSE);
595   }
598   /*! \brief  Returns an entry from the gosaSupportQueue
599     @param  Integer The ID of the entry we want to return.
600     @return Array   Of the requested entry. 
601    */
602   public function get_entry_by_id($id)
603   {
604     if(!is_numeric($id)){
605       trigger_error("Requires an integer as parameter.");
606       return;
607     }
608   
609     $ret = array();
610     $xml_msg = "<xml>
611       <header>gosa_query_jobdb</header>
612       <target>GOSA</target>
613       <source>GOSA</source>
614       <where>
615       <clause>
616       <phrase>
617       <operator>eq</operator>
618       <id>".$id."</id>
619       </phrase>
620       </clause>
621       </where>
622       </xml>";
623     if($this->connect()){
624       $entries = $this->_send($xml_msg,TRUE);
625       if( isset($entries['XML']['HEADER']) &&
626           $entries['XML']['HEADER']=="answer" &&
627           isset($entries['XML']['ANSWER1'])){
628         $ret = $entries['XML']['ANSWER1'];
629       }
630     }
631     return($ret);
632   }
635   /*! \brief  Removes a set of entries from the GOsa support queue. 
636     @param  Array The IDs to remove.
637     @return Boolean True on success.
638    */
639   public function remove_entries($ids)
640   {
641     if(!is_array($ids)){
642       trigger_error("Requires an array as parameter.");
643       return;
644     }
647     $ret = array();
649     $xml_msg = "<xml>
650       <header>gosa_delete_jobdb_entry</header>
651       <target>GOSA</target>
652       <source>GOSA</source>
653       <where>
654       <clause>
655       <connector>or</connector>";
656     foreach($ids as $id){
657       $xml_msg .= "<phrase>
658         <operator>eq</operator>
659         <id>".$id."</id>
660         </phrase>";
661     }
662     $xml_msg .= "</clause>
663       </where>
664       </xml>";
666     if($this->connect()){
667       $entries = $this->_send($xml_msg,TRUE);
668       if(isset($entries['XML']) || isset($entries['COUNT'])){
669         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
670         return(TRUE);
671       }else{
672         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
673       }
674     }
675     return(FALSE);
676   }
680   /*! \brief  Removes an entry from the GOsa support queue. 
681     @param  Integer The ID of the entry we want to remove.
682     @return Boolean True on success.
683    */
684   public function remove_entry($id)
685   {
686     return($this->remove_entries(array($id)));
687   }
690   /*! \brief  Parses the given xml string into an array 
691     @param  String XML string  
692     @return Array Returns an array containing the xml structure. 
693    */
694   private function xml_to_array($xml,$alternative_method = FALSE)
695   {
696     $params = array();
697     $level = array();
698     $parser  = xml_parser_create_ns();
699     xml_parse_into_struct($parser, $xml, $vals, $index);
701     $err_id = xml_get_error_code($parser);
702     if($err_id){
703       xml_parser_free($parser);
704     }else{
705       xml_parser_free($parser);
707       if($this->use_alternative_xml_parse_method) {
708         $params = $this->build_xml_array($vals);
709       } else {
711         foreach ($vals as $xml_elem) {
712           if ($xml_elem['type'] == 'open') {
713             if (array_key_exists('attributes',$xml_elem)) {
714               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
715             } else {
716               $level[$xml_elem['level']] = $xml_elem['tag'];
717             }
718           }
719           if ($xml_elem['type'] == 'complete') {
721             $start_level = 1;
722             $test2 = &$params;
723             while($start_level < $xml_elem['level']) {
724               $test2 = &$test2[$level[$start_level]];
725               $start_level++;
726             }
728             /* Save tag attributes too. 
729                e.g. <tag attr="val">
730              */
731             if(isset($xml_elem['attributes'])){
732               foreach($xml_elem['attributes'] as $name => $value){
733                 $test2['ATTRIBUTES'][$name] = $value;
734               }
735             }
737             if(!isset($test2[$xml_elem['tag']])){
738               if(isset($xml_elem['value'])){
739                 $test2[$xml_elem['tag']] = $xml_elem['value'];
740               }
741             }else{
742               if(!is_array($test2[$xml_elem['tag']])){
743                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
744               }
745               $test2[$xml_elem['tag']][] = $xml_elem['value'];
746             }
747           }
748         }
749       }
750     }
752     if(!isset($params['XML'])){
753       if (!array_key_exists('XML', $params)){
754         $this->set_error(_("Cannot not parse XML!"));
755       }
756       $params = array("COUNT" => 0);
757     }
759     return($params); 
760   }
763   function build_xml_array(&$vals)
764   {
765     $array = array();
766     while(count($vals)){
767       $key = key($vals);
768       $val = $vals[$key];
769       unset($vals[$key]);
770       if($val['type'] == "close"){
771         return($array);
772       }elseif($val['type']=="open"){
773         $array[$val['tag']][] = $this->build_xml_array($vals);
774       }elseif($val['type'] != "cdata"){
775         $data = array("VALUE" => "","ATTRIBUTES" => "");
776         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
777           if(isset($val[$name])){
778             $data[$attr] = $val[$name];
779           }
780         }
781         $array[$val['tag']][] = $data;
782       }else{
783 #print_a($val);
784       }
785     }
786     return($array);
787   }
794   /*! \brief  Updates an entry with a set of new values, 
795     @param  Integer The ID of the entry, we want to update.
796     @param  Array   The variables to update.   
797     @return Boolean Returns TRUE on success. 
798    */
799   public function update_entries($ids,$data)
800   {
801     if(!is_array($ids)){
802       trigger_error("Requires an array as first parameter.");
803       return;
804     }
806     if(!is_array($data)){
807       trigger_error("Requires an array as second parameter.");
808       return;
809     }
811     $attr = "";
812     foreach($data as $key => $value){
813       $key = strtolower($key);
814       if(is_array($value)){
815         foreach($value as $sub_value){
816           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
817         }
818       }else{
819         $attr.= "<$key>".strtolower($value)."</$key>\n";
820       }
821     }
823     $xml_msg = "<xml>
824       <header>gosa_update_status_jobdb_entry</header>
825       <target>GOSA</target>
826       <source>GOSA</source>
827       <where>
828       <clause>
829       <connector>or</connector>";
830     foreach($ids as $id){
831       $xml_msg .= "<phrase>
832         <operator>eq</operator>
833         <id>".$id."</id>
834         </phrase>";
835     }
836     $xml_msg .= "</clause>
837       </where>
838       <update>
839       ".$attr." 
840       </update>
841       </xml>";
843     if($this->connect()){
844       $entries = $this->_send($xml_msg,TRUE);
845       if(isset($entries['XML'])){
846         if(isset($entries['XML']['ERROR_STRING'])) {
847           $this->set_error($entries['XML']['ERROR_STRING']);
848           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
849           return(FALSE);
850         }
851         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
852         return(TRUE);
853       }
854     }
855     return(FALSE);
856   }
859   /*! \brief  Returns the number of currently queued objects.
860       @return Integer  
861    */
862   public function number_of_queued_entries($event_types)
863   {
864     $tags = "";
865     foreach($event_types as $type){
866       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
867     }
868     if(count($event_types) > 1){
869       $tags = "<connector>or</connector>".$tags;
870     }
871     if(count($event_types)){
872       $tags = "<where><clause>".$tags."</clause></where>";
873     }
876     $xml_msg =
877       "<xml>".
878       "<header>gosa_query_jobdb</header>".
879       "<target>GOSA</target>".
880       "<source>GOSA</source>".
881       "<select> count ID</select>".
882       $tags.
883       "</xml>";
885     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
886     $this->connect();
887     if($this->connect()){
888       $entries = $this->_send($xml_msg,TRUE);
889       if($this->o_sock->is_error()){
890         $this->set_error($this->o_sock->get_error());
891         return(0);
892       }
893       if(isset($entries['XML'])){
894         return($entries['XML']['COUNT']);
895       }
896     }
897     return(-1);
898   } 
901   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
902   {
903     $xml_message= "";
905     /* Prepare data */
906     foreach ($data as $key => $value){
907       if(is_array($value)){
908         foreach($value as $sub_value){
909           $xml_message.= "<$key>$sub_value</$key>";
910         }
911       }else{
912         $xml_message.= "<$key>$value</$key>";
913       }
914     }
916     /* Multiple targets? */
917     if (!is_array($to)){
918       $to_targets= array($to);
919     } else {
920       $to_targets= $to;
921     }
923     /* Build target strings */
924     $target ="";
925     foreach($to_targets as $to){
926       $target.= "<target>$to</target>";
927     }
929     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
930   }
933   /* Allows simply appending a new DaemonEvent 
934    */
935   public function append($event)
936   {
937     if(!($event instanceof DaemonEvent)){
938       return(FALSE);
939     }
940   
942     /* Add to queue if new 
943      */
944     if($event->is_new()){
946       $request_answer = FALSE;
947       if($event->get_type() == SCHEDULED_EVENT){
948         $action = $event->get_schedule_action();
949       }elseif($event->get_type() == TRIGGERED_EVENT){
950         $action = $event->get_trigger_action();
951       }else{
952         trigger_error("Unknown type of queue event given.");
953         return(FALSE);
954       }
956       /* Get event informations, like targets..
957        */
958       $targets    = $event->get_targets();
959       $data       = $event->save();
961       /* Append an entry for each target 
962        */
963       foreach($targets as $target){
964         $data['macaddress'] = $target;
965         $this->send_data($action,$target,$data,$request_answer);
967         if($this->is_error()){
968           return(FALSE);
969         }
970       }
971       return(TRUE);
972     }else{
974       /* Updated edited entry.
975        */
976       $id                 = $event->get_id();
977       $data               = $event->save();
978       return($this->update_entries(array($id),$data));
979     }
981     return(FALSE);
982   }
985   /*! \brief  Returns an array containing all queued entries.
986     @return Array All queued entries as an array.
987    */
988   public function _send($data, $answer_expected= FALSE)
989   {
991     $ret = array();
992     if(!$this->connect()){
993       return($ret);
994     }
995   
996     $this->reset_error();
998     /******
999       Debug handling
1000      ******/
1001     $debug = debug_backtrace();
1002     $file = __FILE__;
1003     $function = __FUNCTION__;
1004     $line = __LINE__;
1005     $class = __CLASS__;
1006     foreach($debug as $info){
1007       if(!in_array($info['function'],array("send_data","_send"))){
1008         $file = $info['file'];
1009         $line = $info['line'];
1010         $class = get_class($this);
1011         $function = $info['function'];
1012         break;
1013       }
1014     }
1015     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1018     /*******
1019       Start sending data 
1020      *******/
1021     if($this->connect()){
1022       $this->o_sock->write($data);
1023       if ($answer_expected){
1024         $str = trim($this->o_sock->read());
1026         /* Check if something went wrong while reading */
1027         if($this->o_sock->is_error()){
1028           $this->set_error($this->o_sock->get_error());
1029           return($ret);
1030         }
1032         $entries = $this->xml_to_array($str);
1033         if(isset($entries['XML']) && is_array($entries['XML'])){
1034           $ret = $entries;
1035           if($this->use_alternative_xml_parse_method) {
1036             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1037               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1038               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1039                   array($data=>$data),"FAILED ".$this->get_error());
1040             }
1041           }else{
1042             if(isset($entries['XML']['ERROR_STRING'])) {
1043               $this->set_error($entries['XML']['ERROR_STRING']);
1044               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1045                   array($data=>$data),"FAILED ".$this->get_error());
1046             }elseif(isset($entries['XML']['ERROR'])){
1047               $this->set_error($entries['XML']['ERROR']);
1048               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1049                   array($data=>$data),"FAILED ".$this->get_error());
1050             }
1051           }
1052           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1053               array($data=>$data),"SUCCESS");
1054         }
1055       }else{
1056         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1057             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1058       }
1059     }
1060     return($ret);
1061   }
1064   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1065   {
1066     $xml_message= "";
1068     /* Get communication object */
1069     $d= new gosaSupportDaemon(TRUE,10);
1071     /* Prepare data */
1072     foreach ($data as $key => $value){
1073       if(is_array($value)){
1074         foreach($value as $sub_val){
1075           $xml_message.= "<$key>$sub_val</$key>";
1076         }
1077       }else{
1078         $xml_message.= "<$key>$value</$key>";
1079       }
1080     }
1082     /* Multiple targets? */
1083     if (!is_array($to)){
1084       $to_targets= array($to);
1085     } else {
1086       $to_targets= $to;
1087     }
1089     /* Build target strings */
1090     $target ="";
1091     foreach($to_targets as $to){
1092       $target.= "<target>$to</target>";
1093     }
1095     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1096   }
1099   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1100       @param  String  $mac  The mac address for which we want to remove all jobs.      
1101    */
1102   function clean_queue_from_mac($mac)
1103   {
1104     global $config;
1106     /* First of all we have to check which jobs are startet 
1107      *  for $mac 
1108      */
1109     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1110     
1111     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1112  
1113     $data = $this->_send($xml_msg,TRUE);
1114     if(is_array($data) && isset($data['XML'])){
1115       $already_aborted = FALSE;
1116       foreach($data['XML']  as $name => $entry){
1117         if(preg_match("/answer[0-9]*/i",$name)){
1118           $entry['STATUS'] = strtoupper($entry['STATUS']);
1119           switch($entry['STATUS']){
1121             case 'PROCESSING' :
1123               /* Send abort event, but only once 
1124                */
1125               if($already_aborted){
1126                 break;
1127               }elseif(class_available("DaemonEvent_faireboot")){
1128                 $already_aborted = TRUE;
1129                 $tmp = new DaemonEvent_faireboot($config);
1130                 $tmp->add_targets(array($mac));
1131                 $tmp->set_type(TRIGGERED_EVENT);
1132                 if(!$this->append($tmp)){
1133                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1134                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1135                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1136                 }else{
1137                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1138                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1139                 }
1140                 ;break;
1141               }else{
1142                 /* Couldn't find abort event, just remove entry */
1143               }
1145             case 'WAITING':
1146             case 'ERROR':
1147             default :
1148             
1149               /* Simply remove entries from queue. 
1150                *  Failed or waiting events, can be removed without any trouble.
1151                */ 
1152               if(!$this->remove_entries(array($entry['ID']))){
1153                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1154               }
1155               ;break;
1156           }
1157     
1158         }
1159       }
1160     }
1161   }
1164   static function ping($target)
1165   {
1166     if (tests::is_mac($target)){
1167       /* Get communication object */
1168       $d= new gosaSupportDaemon(TRUE,0.5);
1169       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1170       return (count($answer) ? TRUE:FALSE);
1171     }
1172     return (FALSE);
1173   }
1177   /*! \brief  Returns a list of all configured principals. 
1178               (Uses the GOsa support daemon instead of the ldap database.)
1179       @return Array  A list containing the names of all configured principals.
1180    */
1181   public function krb5_list_principals($server)
1182   {
1183     $res = array();  
1185     /* Check if the given server is a valid mac address
1186      */
1187     if(!tests::is_mac($server)){
1188       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1189       return($ret);
1190     }
1192     /* Prepare request event 
1193      */ 
1194     $xml_msg = 
1195       "<xml>".
1196       "<header>gosa_krb5_list_principals</header>".
1197       "<source>GOSA</source>".
1198       "<target>".$server."</target>".
1199       "</xml>";
1200     
1201     $tmp = $this->_send($xml_msg,TRUE);
1202     if(isset($tmp['XML']['PRINCIPAL'])){
1203       return($tmp['XML']['PRINCIPAL']);
1204     }else{
1205       return($res);
1206     }
1207   }
1210   /*! \brief  Returns the configuration settings for a given principal name. 
1211               (Uses the GOsa support daemon instead of the ldap database.)
1212       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1213       @return Array  A list containing the names of all configured principals.
1214    */
1215   public function krb5_get_principal($server,$name)
1216   {
1217     $ret = array();
1219     /* Check if the given name is a valid request value 
1220      */
1221     if(!is_string($name) || empty($name)){
1222       trigger_error("The given principal name is not of type string or it is empty.");
1223       return($ret);
1224     }
1226     /* Check if the given server is a valid mac address
1227      */
1228     if(!tests::is_mac($server)){
1229       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1230       return($ret);
1231     }
1233     /* Prepare request event 
1234      */ 
1235     $xml_msg = 
1236       "<xml>".
1237       "<header>gosa_krb5_get_principal</header>".
1238       "<principal>".$name."</principal>".
1239       "<source>GOSA</source>".
1240       "<target>".$server."</target>".
1241       "</xml>";
1243     $res = $this->_send($xml_msg,TRUE);
1244     if(isset($res['XML'])){
1245       return($res['XML']);
1246     }else{
1247       return($ret);
1248     }
1249   }
1252   /*! \brief  Creates a given principal with a set of configuration settings.
1253               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1254               (Uses the GOsa support daemon instead of the ldap database.)
1255       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1256       @return Boolean   TRUE on success else FALSE. 
1257    */
1258   public function krb5_add_principal($server,$name,$values)
1259   {
1260     $ret = FALSE;  
1262     /* Check if the given name is a valid request value 
1263      */
1264     if(!is_string($name) || empty($name)){
1265       trigger_error("The given principal name is not of type string or it is empty.");
1266       return($ret);
1267     }
1268     if(!is_array($values)){
1269       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1270       return($ret);
1271     }
1273     /* Check if the given server is a valid mac address
1274      */
1275     if(!tests::is_mac($server)){
1276       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1277       return($ret);
1278     }
1280     $attrs = "";
1281     foreach($values as $key => $value){
1282       if(empty($key) || is_numeric($key)){
1283         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1284         return($ret);
1285       }
1286       $key = strtolower($key);
1287       if(is_array($value)){
1288         foreach($value as $val){
1289           $attrs.= "<$key>$val</$key>\n";
1290         }
1291       }else{
1292         $attrs.= "<$key>$value</$key>\n";
1293       }
1294     }
1296     /* Prepare request event 
1297      */ 
1298     $xml_msg = 
1299       "<xml>".
1300       "<header>gosa_krb5_create_principal</header>".
1301       "<principal>".$name."</principal>".
1302       $attrs.
1303       "<source>GOSA</source>".
1304       "<target>".$server."</target>".
1305       "</xml>";
1307     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1308   }
1311   function krb5_ramdomize_key($server,$name)  
1312   {
1313     /* Prepare request event 
1314      */ 
1315     $xml_msg = 
1316       "<xml>".
1317       "<header>gosa_krb5_randomize_key</header>".
1318       "<principal>".$name."</principal>".
1319       "<source>GOSA</source>".
1320       "<target>".$server."</target>".
1321       "</xml>";
1323     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1324   }
1325   
1328   /*! \brief  Updates a given principal with a set of configuration settings.
1329               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1330               (Uses the GOsa support daemon instead of the ldap database.)
1331       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1332       @return Boolean   TRUE on success else FALSE. 
1333    */
1334   public function krb5_set_principal($server,$name,$values)
1335   {
1336     $ret = FALSE;  
1338     /* Check if the given name is a valid request value 
1339      */
1340     if(!is_string($name) || empty($name)){
1341       trigger_error("The given principal name is not of type string or it is empty.");
1342       return($ret);
1343     }
1344     if(!is_array($values) || !count($values)){
1345       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1346       return($ret);
1347     }
1349     /* Check if the given server is a valid mac address
1350      */
1351     if(!tests::is_mac($server)){
1352       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1353       return($ret);
1354     }
1356     $attrs = "";
1357     foreach($values as $key => $value){
1358       if(empty($key) || is_numeric($key)){
1359         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1360         return($ret);
1361       }
1362       $key = strtolower($key);
1363       if(is_array($value)){
1364         foreach($value as $val){
1365           $attrs.= "<$key>$val</$key>\n";
1366         }
1367       }else{
1368         $attrs.= "<$key>$value</$key>\n";
1369       }
1370     }
1372     /* Prepare request event 
1373      */ 
1374     $xml_msg = 
1375       "<xml>".
1376       "<header>gosa_krb5_modify_principal</header>".
1377       "<principal>".$name."</principal>".
1378       $attrs.
1379       "<source>GOSA</source>".
1380       "<target>".$server."</target>".
1381       "</xml>";
1383     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1384   }
1387   /*! \brief  Removes the given principal.
1388               (Uses the GOsa support daemon instead of the ldap database.)
1389       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1390       @return Boollean   TRUE on success else FALSE
1391    */
1392   public function krb5_del_principal($server,$name)
1393   {
1394     $ret = FALSE;  
1396     /* Check if the given name is a valid request value 
1397      */
1398     if(!is_string($name) || empty($name)){
1399       trigger_error("The given principal name is not of type string or it is empty.");
1400       return($ret);
1401     }
1403     /* Check if the given server is a valid mac address
1404      */
1405     if(!tests::is_mac($server)){
1406       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1407       return($ret);
1408     }
1410     /* Prepare request event 
1411      */ 
1412     $xml_msg = 
1413       "<xml>".
1414       "<header>gosa_krb5_del_principal</header>".
1415       "<principal>".$name."</principal>".
1416       "<source>GOSA</source>".
1417       "<target>".$server."</target>".
1418       "</xml>";
1419     
1420     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1421   }
1424   /*! \brief  Returns a list of configured password policies.
1425               (Uses the GOsa support daemon instead of the ldap database.)
1426       @return Array A list of all configured password policies.
1427    */
1428   public function krb5_list_policies($server)
1429   {
1430     $res = array();  
1432     /* Check if the given server is a valid mac address
1433      */
1434     if(!tests::is_mac($server)){
1435       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1436       return($ret);
1437     }
1439     /* Prepare request event 
1440      */ 
1441     $xml_msg = 
1442       "<xml>".
1443       "<header>gosa_krb5_list_policies</header>".
1444       "<source>GOSA</source>".
1445       "<target>".$server."</target>".
1446       "</xml>";
1447     
1448     $res = $this->_send($xml_msg,TRUE);
1449     
1450     /* Check if there are results for POLICY 
1451      */
1452     if(isset($res['XML']['POLICY'])){
1453       
1454       /* Ensure that we return an array 
1455        */
1456       $tmp = $res['XML']['POLICY'];
1457       if(!is_array($tmp)){
1458         $tmp = array($tmp);
1459       }
1460       return($tmp);
1461     }else{
1462       return(array());
1463     }
1464   }
1467   /*! \brief  Returns a list of configured password policies.
1468               (Uses the GOsa support daemon instead of the ldap database.)
1469       @return Array The policy settings for the given policy name.
1470    */
1471   public function krb5_get_policy($server,$name)
1472   {
1473     $res = array();  
1475     /* Check if the given name is a valid request value 
1476      */
1477     if(!is_string($name) || empty($name)){
1478       trigger_error("The given policy name is not of type string or it is empty.");
1479       return($ret);
1480     }
1482     /* Check if the given server is a valid mac address
1483      */
1484     if(!tests::is_mac($server)){
1485       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1486       return($ret);
1487     }
1489     /* Prepare request event 
1490      */ 
1491     $xml_msg = 
1492       "<xml>".
1493       "<header>gosa_krb5_get_policy</header>".
1494       "<policy>".$name."</policy>".
1495       "<source>GOSA</source>".
1496       "<target>".$server."</target>".
1497       "</xml>";
1499     /* Possible attributes */
1500     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1501         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1503   
1504     $tmp = $this->_send($xml_msg,TRUE);
1505     if(isset($tmp['XML'])){
1506       foreach($attrs as $attr){
1507         if(isset($tmp['XML'][$attr])){
1508           $ret[$attr] = $tmp['XML'][$attr];
1509         }else{
1510           $ret[$attr] = "";
1511         }
1512       }
1513     }
1514     return($ret);
1515   }
1517   
1518   /*! \brief  Creates a new policy with a given set of configuration settings.
1519               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1520               (Uses the GOsa support daemon instead of the ldap database.)
1521       @pram   String The name of the policy to update.
1522       @pram   Array  The attributes to update
1523       @return Boolean   TRUE on success else FALSE. 
1524    */
1525   public function krb5_add_policy($server,$name,$values)
1526   {
1527     $ret = FALSE;  
1529     /* Check if the given name is a valid request value 
1530      */
1531     if(!is_string($name) || empty($name)){
1532       trigger_error("The given policy name is not of type string or it is empty.");
1533       return($ret);
1534     }
1535     if(!is_array($values) || !count($values)){
1536       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1537       return($ret);
1538     }
1540     /* Check if the given server is a valid mac address
1541      */
1542     if(!tests::is_mac($server)){
1543       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1544       return($ret);
1545     }
1548     /* Transform array into <xml>
1549      */
1550     $attrs = "";
1551     foreach($values as $id => $value){
1552       if(empty($id) || is_numeric($id)){
1553         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1554         return($ret);
1555       }
1556       $id = strtolower($id);
1557       $attrs.= "<$id>$value</$id>\n";
1558     }
1560     /* Prepare request event 
1561      */ 
1562     $xml_msg = 
1563       "<xml>".
1564       "<header>gosa_krb5_create_policy</header>".
1565       "<policy>".$name."</policy>".
1566       $attrs.
1567       "<source>GOSA</source>".
1568       "<target>".$server."</target>".
1569       "</xml>";
1571     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1572   }
1575   /*! \brief  Updates a given policy with a set of configuration settings.
1576               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1577               (Uses the GOsa support daemon instead of the ldap database.)
1578       @pram   String The name of the policy to update.
1579       @return Boolean   TRUE on success else FALSE. 
1580    */
1581   public function krb5_set_policy($server,$name,$values)
1582   {
1583     $ret = FALSE;  
1585     /* Check if the given name is a valid request value 
1586      */
1587     if(!is_string($name) || empty($name)){
1588       trigger_error("The given policy name is not of type string or it is empty.");
1589       return($ret);
1590     }
1591     if(!is_array($values) || !count($values)){
1592       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1593       return($ret);
1594     }
1596     /* Check if the given server is a valid mac address
1597      */
1598     if(!tests::is_mac($server)){
1599       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1600       return($ret);
1601     }
1603     /* Transform array into <xml>
1604      */
1605     $attrs = "";
1606     foreach($values as $id => $value){
1607       if(preg_match("/^policy$/i",$id)) continue;
1608       if(empty($id) || is_numeric($id)){
1609         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1610         return($ret);
1611       }
1612       $id = strtolower($id);
1613       $attrs.= "<$id>$value</$id>\n";
1614     }
1616     /* Prepare request event 
1617      */ 
1618     $xml_msg = 
1619       "<xml>".
1620       "<header>gosa_krb5_modify_policy</header>".
1621       "<policy>".$name."</policy>".
1622       $attrs.
1623       "<source>GOSA</source>".
1624       "<target>".$server."</target>".
1625       "</xml>";
1627     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1628   }
1630   
1631   /*! \brief  Removes the given password policy. 
1632               (Uses the GOsa support daemon instead of the ldap database.)
1633       @return Boolean  TRUE on success else FALSE
1634    */
1635   public function krb5_del_policy($server,$name)
1636   {
1637     $ret = FALSE;
1639     /* Check if the given server is a valid mac address
1640      */
1641     if(!tests::is_mac($server)){
1642       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1643       return($ret);
1644     }
1646     /* Check if the given name is a valid request value 
1647      */
1648     if(!is_string($name) || empty($name)){
1649       trigger_error("The given policy name is not of type string or it is empty.");
1650       return($ret);
1651     }
1653     /* Prepare request event 
1654      */ 
1655     $xml_msg = 
1656       "<xml>".
1657       "<header>gosa_krb5_del_policy</header>".
1658       "<policy>".$name."</policy>".
1659       "<source>GOSA</source>".
1660       "<target>".$server."</target>".
1661       "</xml>";
1662     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1663   }
1666   /*! \brief  Sets the password of for the given principal.
1667               (Uses the GOsa support daemon instead of the ldap database.)
1668       @param  String  The servers mac
1669       @param  String  The principals name
1670       @param  String  $the new password.   
1671       @return Boolean  TRUE on success else FALSE
1672    */
1673   public function krb5_set_password($server,$name,$password)
1674   {
1675     $ret = FALSE;
1677     /* Check if the given server is a valid mac address
1678      */
1679     if(!tests::is_mac($server)){
1680       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1681       return($ret);
1682     }
1684     /* Check if the given name is a valid request value
1685      */
1686     if(!is_string($name) || empty($name)){
1687       trigger_error("The given principal name is not of type string or it is empty.");
1688       return($ret);
1689     }
1691     /* Prepare request event
1692      */
1693     $xml_msg =
1694       "<xml>".
1695       "<header>gosa_krb5_set_password</header>".
1696       "<principal>".$name."</principal>".
1697       "<password>".$password."</password>".
1698       "<source>GOSA</source>".
1699       "<target>".$server."</target>".
1700       "</xml>";
1701     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1702   }
1705   /*! \brief  Returns log file informations for a given mac address 
1706       @param  $mac The mac address to fetch logs for.
1707       @retrun Array A Multidimensional array containing log infos.
1708         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1709         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1710                                install_20080313_144450   ...
1711    */
1712   public function get_log_info_for_mac($mac)
1713   {
1714     $xml_msg = "
1715       <xml>
1716       <header>gosa_show_log_by_mac</header>
1717       <target>GOSA</target>
1718       <source>GOSA</source>
1719       <mac>".$mac."</mac>
1720       </xml>";
1722     $res = $this->_send($xml_msg,TRUE);
1723     $ret = array();
1724     if(isset($res['XML'])){
1726       /* Filter all entry that look like this 
1727           MAC_00_01_6C_9D_B9_FA
1728        */
1729       foreach($res['XML'] as $name => $entry){
1730         if(preg_match("/^MAC/",$name)){
1732           /* Get list of available log files 
1733            */
1734           foreach($entry as $log_date){
1735             $xml_msg2 = "<xml> 
1736               <header>gosa_show_log_files_by_date_and_mac</header> 
1737               <target>GOSA</target> 
1738               <source>GOSA</source>                        
1739               <date>".$log_date."</date> 
1740               <mac>".$mac."</mac> 
1741               </xml>";
1743             $ret[$mac][$log_date] = array();
1744             $res = $this->_send($xml_msg2,TRUE);
1745             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1746             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1747             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1748               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1749             }
1750           }
1751         }
1752       }
1753     }
1754     return($ret);
1755   }
1757   public function get_log_file($mac,$date,$file)
1758   {
1759     $xml_msg ="
1760       <xml> 
1761       <header>gosa_get_log_file_by_date_and_mac</header> 
1762       <target>GOSA</target> 
1763       <source>GOSA</source>
1764       <date>".$date."</date> 
1765       <mac>".$mac."</mac> 
1766       <log_file>".$file."</log_file>
1767       </xml>";
1769     $res = $this->_send($xml_msg,TRUE);
1770     if(isset($res['XML'][strtoupper($file)])){
1771       return(base64_decode($res['XML'][strtoupper($file)]));
1772     }
1773     return("");
1774   }
1780   /*****************
1781    * DAK - Functions 
1782    *****************/
1784   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1785       @param  ...
1786       @return Array   All queued entries.
1787    */
1788   public function DAK_keyring_entries($server)  
1789   {
1790     /* Ensure that we send the event to a valid mac address 
1791      */
1792     if(!is_string($server) || !tests::is_mac($server)){
1793       trigger_error("No valid mac address given '".$server."'.");
1794       return;
1795     }
1797     /* Create query
1798      */
1799     $xml_msg = "<xml> 
1800                   <header>gosa_get_dak_keyring</header> 
1801                   <target>".$server."</target> 
1802                   <source>GOSA</source>
1803                 </xml>";
1804         
1805     $res = $this->_send($xml_msg,TRUE);
1807     /* Check if there are results for POLICY
1808      */
1809     if(isset($res['XML'])){
1810       $ret = array();
1811       foreach($res['XML'] as $key => $entry){
1812         if(preg_match("/^ANSWER/",$key)){
1813           $ret[] = $entry;
1814         }
1815       }
1816       return($ret);
1817     }else{
1818       return(array());
1819     }
1820   }
1823   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1824       @param  String  The servers mac address 
1825       @param  String  The gpg key.
1826       @return Boolean TRUE on success else FALSE 
1827    */
1828   public function DAK_import_key($server,$key)  
1829   {
1830     /* Ensure that we send the event to a valid mac address 
1831      */
1832     if(!is_string($server) || !tests::is_mac($server)){
1833       trigger_error("No valid mac address given '".$server."'.");
1834       return;
1835     }
1837     /* Check if there is some cleanup required before importing the key.
1838         There may be some Header lines like:
1839         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1840      */
1841     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1843       /* Remove header */
1844       $key = preg_replace("/^.*\n\n/sim","",$key);
1845       /* Remove footer */
1846       $key = preg_replace("/-----.*$/sim","",$key);
1847     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1848       
1849       /* Encode key if it is raw.
1850        */
1851       $key = base64_encode($key);
1852     }
1854     /* Create query
1855      */
1856     $xml_msg = "<xml> 
1857                   <header>gosa_import_dak_key</header> 
1858                   <target>".$server."</target> 
1859                   <key>".$key."</key> 
1860                   <source>GOSA</source>
1861                 </xml>";
1862         
1863     $res = $this->_send($xml_msg,TRUE);
1864     return($this->is_error());
1865   }
1868   /*! \brief Removes a key from the keyring on the given server. 
1869       @param  String  The servers mac address 
1870       @param  String  The gpg key uid.
1871       @return Boolean TRUE on success else FALSE 
1872    */
1873   public function DAK_remove_key($server,$key)  
1874   {
1875     /* Ensure that we send the event to a valid mac address 
1876      */
1877     if(!is_string($server) || !tests::is_mac($server)){
1878       trigger_error("No valid mac address given '".$server."'.");
1879       return;
1880     }
1882     /* Create query
1883      */
1884     $xml_msg = "<xml> 
1885                   <header>gosa_remove_dak_key</header> 
1886                   <target>".$server."</target> 
1887                   <keyid>".$key."</keyid> 
1888                   <source>GOSA</source>
1889                 </xml>";
1890        
1891     $res = $this->_send($xml_msg,TRUE);
1892     return($this->is_error());
1893   }
1896 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1897 ?>