Code

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