Code

0c99d158c203c0942d16894f7baeba9632978b05
[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("/^HOST[0-9]*$/",$name) && $name != "HOST"){
112           $hosts[] = $data[0]['MAC'][0]['VALUE'];
113         }
114       }
115     }
116     return($hosts);
117   }
120   /*! \brief  Disconnect from gosa daemon.
121    */
122   public function disconnect()
123   {
124     $this->o_sock->close();
125     $this->is_connected = FALSE;
126   }
129   /*! \brief  Sets an error message, which can be returned with get_error().
130     @param  string  The Error message,
131    */
132   private function set_error($str)
133   {
134     $this->b_error = TRUE;
135     $this->s_error = $str;
136   }
139   /*! \brief  Sets an error message, which can be returned with get_error().
140     @param  string  The Error message,
141    */
142   private function reset_error()
143   {
144     $this->b_error = FALSE;
145     $this->s_error = "";
146   }
149   /*! \brief  Checks if an error occured.
150     @return boolean returns TRUE or FALSE, whether there is an error or not.
151    */
152   public function is_error()
153   {
154     return($this->b_error);
155   }
158   /*! \brief  Returns the last error. 
159     @return Returns the last error.
160    */
161   public function get_error()
162   {
163     $str = $this->s_error;
164     $ret = "";
165     if(is_string($str)){
166       $ret = $str;
167     }else{
168       foreach($str as $msg){
169         $ret .= $msg." ";
170       }
171     }
172     $ret = preg_replace("/ /","&nbsp;",$ret);
173     return($ret);
174   }
177   public function FAI_get_kernels($release)
178   {
179     $xml_msg = 
180       "<xml>".
181       "<header>gosa_get_available_kernel</header>".
182       "<source>GOSA</source>".
183       "<target>GOSA</target>".
184       "<release>".$release."</release>".
185       "</xml>";
187     $ret = array();
188     if($this->connect()){
189       $this->o_sock->write($xml_msg);
190       $str = trim($this->o_sock->read());
192       /* Check if something went wrong while reading */
193       if($this->o_sock->is_error()){
194         $this->set_error($this->o_sock->get_error());
195         return($ret);
196       }
198       $entries = $this->xml_to_array($str);
199       if(isset($entries['XML']) && is_array($entries['XML'])){
201         /* Check if returned values represent a valid answer */
202         if(isset($entries['XML'])){
203           if(isset($entries['XML']['ERROR_STRING'])) {
204             $this->set_error($entries['XML']['ERROR_STRING']);
205             new log("debug","GOsa-si",
206                 get_class($this)."::".__FUNCTION__, array(),
207                 "FAILED error was ".$this->get_error());
208             return($ret);
209           }
211           /* Unset header tags */
212           $ret = $entries['XML'];
213           foreach($ret as $key => $entry){
214             if(!preg_match("/^answer/i",$key)){
215               unset($ret[$key]);
216             }
217           }
218         }
219       }
220     }
221     return($ret);
222   }
225   public function FAI_get_package_sections($release)
226   {
227     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
228       "<select>distinct section</select>".
229       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
231     $ret = array();
232     if($this->connect()){
233       $this->o_sock->write($xml_msg);
234       $str = trim($this->o_sock->read());
236       /* Check if something went wrong while reading */
237       if($this->o_sock->is_error()){
238         $this->set_error($this->o_sock->get_error());
239         return($ret);
240       }
242       $entries = $this->xml_to_array($str);
243       if(isset($entries['XML']) && is_array($entries['XML'])){
245         /* Check if returned values represent a valid answer */
246         if(isset($entries['XML'])){
247           if(isset($entries['XML']['ERROR_STRING'])) {
248             $this->set_error($entries['XML']['ERROR_STRING']);
249             new log("debug","GOsa-si",
250                 get_class($this)."::".__FUNCTION__, array(),
251                 "FAILED error was ".$this->get_error());
252             return($ret);
253           }
255           /* Unset header tags */
256           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
257             if(isset($entries['XML'][$type])){
258               unset($entries['XML'][$type]);
259             }
260           }
261           $ret = $entries['XML'];
262         }
263       }
264     }
265     return($ret);
266   }
269   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
270   {
271     $this->reset_error();
272     $ret = array();
274     /* Check Parameter */
275     if(!is_array($attrs) || !count($attrs)){
276       trigger_error("Second parameter must be an array. With at least one attribute name.");
277       return($ret);
278     }
280     /* Check Parameter */
281     if(!is_array($package)){
282       trigger_error("Third parameter must be an array. With at least one attribute name.");
283       return($ret);
284     }
286     /* Create list of attributes to fetch */
287     $attr = ""; 
288     foreach($attrs as $at){
289       $attr.= "<select>".$at."</select>";
290     }
292     /* If no package is given, search for all */
293     if(!count($package)) $package = array("%");
295     /* Create limit tag */
296     if($from == -1){
297       $limit =""; 
298     }else{
299       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
300     }
302     /* Create list of attributes to fetch */
303     $pkgs = ""; 
304     foreach($package as $pkg){
305       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
306     }
308     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
309       $attr.
310       "<where>
311       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
312       <clause><connector>OR</connector>
313       ".$pkgs."
314       </clause>
315       </where>".
316       $limit.
317       "</xml>";
319     if($this->connect()){
320       $this->o_sock->write($xml_msg);
321       $str = trim($this->o_sock->read());
323       /* Check if something went wrong while reading */
324       if($this->o_sock->is_error()){
325         $this->set_error($this->o_sock->get_error());
326         return($ret);
327       }
329       $entries = $this->xml_to_array($str);
330       if(isset($entries['XML']) && is_array($entries['XML'])){
332         /* Check if returned values represent a valid answer */
333         if(isset($entries['XML'])){
334           if(isset($entries['XML']['ERROR_STRING'])) {
335             $this->set_error($entries['XML']['ERROR_STRING']);
336             new log("debug","GOsa-si",
337                 get_class($this)."::".__FUNCTION__, array(),
338                 "FAILED error was ".$this->get_error());
339             return($ret);
340           }
342           /* Unset header tags */
343           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
344             if(isset($entries['XML'][$type])){
345               unset($entries['XML'][$type]);
346             }
347           }
348           $ret = $entries['XML'];
349         }
350       }
351     }
352     return($ret);
354     
355   }
358   public function FAI_get_server($name = "")
359   {
360     $this->reset_error();
362     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
363     $ret = array();
364     if($this->connect()){
365       $this->o_sock->write($xml_msg);
366       $str = trim($this->o_sock->read());
368       /* Check if something went wrong while reading */
369       if($this->o_sock->is_error()){
370         $this->set_error($this->o_sock->get_error());
371         return($ret);
372       }
374       $entries = $this->xml_to_array($str);
375       if(isset($entries['XML']) && is_array($entries['XML'])){
377         /* Check if returned values represent a valid answer */
378         if(isset($entries['XML'])){
379           if(isset($entries['XML']['ERROR_STRING'])) {
380             $this->set_error($entries['XML']['ERROR_STRING']);
381             new log("debug","GOsa-si", 
382               get_class($this)."::".__FUNCTION__, array(),
383               "FAILED error was ".$this->get_error());
384             return($ret);
385           }
387           /* Unset header tags */
388           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
389             if(isset($entries['XML'][$type])){
390               unset($entries['XML'][$type]);
391             }
392           }
393           $ret = $entries['XML']; 
394         }
395       }
396     }
397     return($ret);
398   }
401   public function FAI_get_classes($name)
402   {
403     $this->reset_error();
404     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
405                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
406     $ret = array();
407     if($this->connect()){
408       $this->o_sock->write($xml_msg);
409       $str = trim($this->o_sock->read());
411       /* Check if something went wrong while reading */
412       if($this->o_sock->is_error()){
413         $this->set_error($this->o_sock->get_error());
414         return($ret);
415       }
417       $entries = $this->xml_to_array($str);
418       if(isset($entries['XML']) && is_array($entries['XML'])){
420         /* Check if returned values represent a valid answer */
421         if(isset($entries['XML'])){
422           if(isset($entries['XML']['ERROR_STRING'])) {
423             $this->set_error($entries['XML']['ERROR_STRING']);
424             new log("debug","GOsa-si", 
425               get_class($this)."::".__FUNCTION__, array($name),
426               "FAILED error was ".$this->get_error());
427             return($ret);
428           }
430           /* Unset header tags */
431           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
432             if(isset($entries['XML'][$type])){
433               unset($entries['XML'][$type]);
434             }
435           }
436           $ret = $entries['XML']; 
437         }
438       }
439     }
440     return($ret);
441   }
444   /*! \brief  Returns an array containing all queued entries.
445     @return Array All queued entries as an array.
446    */
447   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
448   {
449     $this->reset_error();
450     $ret = array();
452     $tags = "";
453     foreach($event_types as $type){
454       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
455     }
456     if(count($event_types) > 1){
457       $tags = "<connector>or</connector>".$tags;
458     }
459     if(count($event_types)){
460       $tags = "<where><clause>".$tags."</clause></where>";
461     }
463     $xml_msg = 
464       "<xml>
465       <header>gosa_query_jobdb</header>
466       <target>GOSA</target>
467       <source>GOSA</source>
468       ".$tags."
470       <orderby>".$sort."</orderby>";
471     if($from != -1 && $to != -1){
472       $xml_msg.= "
473         <limit>
474         <from>".$from."</from>
475         <to>".$to."</to>
476         </limit>";
477     }
478     $xml_msg.= "
479       </xml>";
481     if($this->connect()){
482       $this->o_sock->write($xml_msg);
483       $str = trim($this->o_sock->read());
485       /* Check if something went wrong while reading */
486       if($this->o_sock->is_error()){
487         $this->set_error($this->o_sock->get_error());
488         return($ret);
489       }
491       $entries = $this->xml_to_array($str);
492       if(isset($entries['XML']) && is_array($entries['XML'])){
494         /* Check if returned values represent a valid answer */
495         if(isset($entries['XML'])){
496           
497           /* Unset header tags */
498           foreach(array("HEADER","SOURCE","TARGET") as $type){
499             unset($entries['XML'][$type]);
500           }
501           $ret = $entries['XML']; 
502         }
503       }
504     }
505     
506     /* Remove session ID. No one is interested in this... */
507     unset($ret['SESSION_ID']);
509     return($ret);
510   }
513   /*! \brief  Checks if the given ids are used queue ids.
514     @param  Array   The ids we want to check..
515     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
516    */
517   public function ids_exist($ids)
518   {
519     if(!is_array($ids)){
520       trigger_error("Requires an array as parameter.");
521       return;
522     }
523     $this->reset_error();
525     $ret = array();
527     $xml_msg = "<xml>
528       <header>gosa_query_jobdb</header>
529       <target>GOSA</target>
530       <source>GOSA</source>
531       <where>
532       <clause>
533       <connector>or</connector>";
534     foreach($ids as $id){
535       $xml_msg .= "<phrase>
536         <operator>eq</operator>
537         <id>".$id."</id>
538         </phrase>";
539     }
540     $xml_msg .= "</clause>
541       </where>
542       </xml>";
544     if($this->connect()){
545       $this->o_sock->write($xml_msg);
546       $str = trim($this->o_sock->read());
548       /* Check if something went wrong while reading */
549       if($this->o_sock->is_error()){
550         $this->set_error($this->o_sock->get_error());
551         return($ret);
552       }
554       $entries = $this->xml_to_array($str);
555       if(isset($entries['XML']) && is_array($entries['XML'])){
556         foreach($entries['XML'] as $entry){
557           if(is_array($entry) && array_key_exists("ID",$entry)){
558             $ret[] = $entry['ID'];
559           }
560         }
561       }
562     }
563     return($ret);
564   }
567   /*! \brief  Returns an entry containing all requested ids.
568     @param  Array   The IDs of the entries we want to return.
569     @return Array   Of the requested entries. 
570    */
571   public function get_entries_by_mac($macs)
572   {
573     if(!is_array($macs)){
574       trigger_error("Requires an array as parameter.");
575       return;
576     }
577     $this->reset_error();
579     $ret = array();
581     $xml_msg = "<xml>
582       <header>gosa_query_jobdb</header>
583       <target>GOSA</target>
584       <source>GOSA</source>
585       <where>
586       <clause>
587       <connector>or</connector>";
588     foreach($macs as $mac){
589       $xml_msg .= "<phrase>
590         <operator>eq</operator>
591         <macaddress>".$mac."</macaddress>
592         </phrase>";
593     }
594     $xml_msg .= "</clause>
595       </where>
596       </xml>";
598     if($this->connect()){
599       $this->o_sock->write($xml_msg);
600       $str = trim($this->o_sock->read());
602       /* Check if something went wrong while reading */
603       if($this->o_sock->is_error()){
604         $this->set_error($this->o_sock->get_error());
605         return($ret);
606       }
608       $entries = $this->xml_to_array($str); 
609       if(isset($entries['XML'])){
610         foreach($entries['XML'] as $name => $entry){
611           if(preg_match("/^ANSWER[0-9]*$/",$name)){
612             $ret[$name] = $entry;
613           }
614         }
615       }
616     }
617     return($ret);
618   }
621   /*! \brief  Returns an entry containing all requested ids.
622     @param  Array   The IDs of the entries we want to return.
623     @return Array   Of the requested entries. 
624    */
625   public function get_entries_by_id($ids)
626   {
627     if(!is_array($ids)){
628       trigger_error("Requires an array as parameter.");
629       return;
630     }
631     $this->reset_error();
633     $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       <connector>or</connector>";
642     foreach($ids as $id){
643       $xml_msg .= "<phrase>
644         <operator>eq</operator>
645         <id>".$id."</id>
646         </phrase>";
647     }
648     $xml_msg .= "</clause>
649       </where>
650       </xml>";
652     if($this->connect()){
653       $this->o_sock->write($xml_msg);
654       $str = trim($this->o_sock->read());
656       /* Check if something went wrong while reading */
657       if($this->o_sock->is_error()){
658         $this->set_error($this->o_sock->get_error());
659         return($ret);
660       }
662       $entries = $this->xml_to_array($str); 
663       if(isset($entries['XML'])){
664         foreach($entries['XML'] as $name => $entry){
665           if(preg_match("/^ANSWER[0-9]*$/",$name)){
666             $ret[$name] = $entry;
667           }
668         }
669       }
670     }
671     return($ret);
672   }
675   /*! \brief  Checks if the given id is in use.
676     @param  Integer The ID of the entry.
677     @return Boolean TRUE if entry exists. 
678    */
679   public function id_exists($id)
680   {
681     if(!is_numeric($id)){
682       trigger_error("Requires an integer as parameter.");
683       return;
684     }
686     $this->reset_error();
688     $xml_msg = "<xml>
689       <header>gosa_query_jobdb</header>
690       <target>GOSA</target>
691       <source>GOSA</source>
692       <where>
693       <clause>
694       <phrase>
695       <operator>eq</operator>
696       <id>".$id."</id>
697       </phrase>
698       </clause>
699       </where>
700       </xml>";
702     if($this->connect()){
703       $this->o_sock->write($xml_msg);
704       $str = trim($this->o_sock->read());
706       /* Check if something went wrong while reading */
707       if($this->o_sock->is_error()){
708         $this->set_error($this->o_sock->get_error());
709         return(FALSE);
710       }
712       $entries = $this->xml_to_array($str); 
713       if( isset($entries['XML']['HEADER']) && 
714           $entries['XML']['HEADER']=="answer" && 
715           isset($entries['XML']['ANSWER1'])){
716         return(TRUE);
717       }
718     }
719     return(FALSE);
720   }
723   /*! \brief  Returns an entry from the gosaSupportQueue
724     @param  Integer The ID of the entry we want to return.
725     @return Array   Of the requested entry. 
726    */
727   public function get_entry_by_id($id)
728   {
729     if(!is_numeric($id)){
730       trigger_error("Requires an integer as parameter.");
731       return;
732     }
733     $this->reset_error();
734   
735     $ret = array();
736     $xml_msg = "<xml>
737       <header>gosa_query_jobdb</header>
738       <target>GOSA</target>
739       <source>GOSA</source>
740       <where>
741       <clause>
742       <phrase>
743       <operator>eq</operator>
744       <id>".$id."</id>
745       </phrase>
746       </clause>
747       </where>
748       </xml>";
749     if($this->connect()){
750       $this->o_sock->write($xml_msg);
751       $str = trim($this->o_sock->read());
753       /* Check if something went wrong while reading */
754       if($this->o_sock->is_error()){
755         $this->set_error($this->o_sock->get_error());
756         return($ret);
757       }
759       $entries = $this->xml_to_array($str); 
760       if( isset($entries['XML']['HEADER']) &&
761           $entries['XML']['HEADER']=="answer" &&
762           isset($entries['XML']['ANSWER1'])){
763         $ret = $entries['XML']['ANSWER1'];
764       }
765     }
766     return($ret);
767   }
770   /*! \brief  Removes a set of entries from the GOsa support queue. 
771     @param  Array The IDs to remove.
772     @return Boolean True on success.
773    */
774   public function remove_entries($ids)
775   {
776     if(!is_array($ids)){
777       trigger_error("Requires an array as parameter.");
778       return;
779     }
781     $this->reset_error();
783     $ret = array();
785     $xml_msg = "<xml>
786       <header>gosa_delete_jobdb_entry</header>
787       <target>GOSA</target>
788       <source>GOSA</source>
789       <where>
790       <clause>
791       <connector>or</connector>";
792     foreach($ids as $id){
793       $xml_msg .= "<phrase>
794         <operator>eq</operator>
795         <id>".$id."</id>
796         </phrase>";
797     }
798     $xml_msg .= "</clause>
799       </where>
800       </xml>";
802     if($this->connect()){
803       $this->o_sock->write($xml_msg);
804       $str = $this->o_sock->read();
806       /* Check if something went wrong while reading */
807       if($this->o_sock->is_error()){
808         $this->set_error($this->o_sock->get_error());
809         return($ret);
810       }
812       $entries = $this->xml_to_array($str);
813       if(isset($entries['XML']) || isset($entries['COUNT'])){
814         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
815         return(TRUE);
816       }else{
817         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
818       }
819     }
820     return(FALSE);
821   }
825   /*! \brief  Removes an entry from the GOsa support queue. 
826     @param  Integer The ID of the entry we want to remove.
827     @return Boolean True on success.
828    */
829   public function remove_entry($id)
830   {
831     return($this->remove_entries(array($id)));
832   }
835   /*! \brief  Parses the given xml string into an array 
836     @param  String XML string  
837     @return Array Returns an array containing the xml structure. 
838    */
839   private function xml_to_array($xml,$alternative_method = FALSE)
840   {
841     $params = array();
842     $level = array();
843     $parser  = xml_parser_create_ns();
844     xml_parse_into_struct($parser, $xml, $vals, $index);
846     $err_id = xml_get_error_code($parser);
847     if($err_id){
848       xml_parser_free($parser);
849     }else{
850       xml_parser_free($parser);
852       if($this->use_alternative_xml_parse_method) {
853         $params = $this->build_xml_array($vals);
854       } else {
856         foreach ($vals as $xml_elem) {
857           if ($xml_elem['type'] == 'open') {
858             if (array_key_exists('attributes',$xml_elem)) {
859               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
860             } else {
861               $level[$xml_elem['level']] = $xml_elem['tag'];
862             }
863           }
864           if ($xml_elem['type'] == 'complete') {
866             $start_level = 1;
867             $test2 = &$params;
868             while($start_level < $xml_elem['level']) {
869               $test2 = &$test2[$level[$start_level]];
870               $start_level++;
871             }
873             /* Save tag attributes too. 
874                e.g. <tag attr="val">
875              */
876             if(isset($xml_elem['attributes'])){
877               foreach($xml_elem['attributes'] as $name => $value){
878                 $test2['ATTRIBUTES'][$name] = $value;
879               }
880             }
882             if(!isset($test2[$xml_elem['tag']])){
883               if(isset($xml_elem['value'])){
884                 $test2[$xml_elem['tag']] = $xml_elem['value'];
885               }
886             }else{
887               if(!is_array($test2[$xml_elem['tag']])){
888                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
889               }
890               $test2[$xml_elem['tag']][] = $xml_elem['value'];
891             }
892           }
893         }
894       }
895     }
897     if(!isset($params['XML'])){
898       if (!array_key_exists('XML', $params)){
899         $this->set_error(_("Cannot not parse XML!"));
900       }
901       $params = array("COUNT" => 0);
902     }
904     return($params); 
905   }
908   function build_xml_array(&$vals)
909   {
910     $array = array();
911     while(count($vals)){
912       $key = key($vals);
913       $val = $vals[$key];
914       unset($vals[$key]);
915       if($val['type'] == "close"){
916         return($array);
917       }elseif($val['type']=="open"){
918         $array[$val['tag']][] = $this->build_xml_array($vals);
919       }elseif($val['type'] != "cdata"){
920         $data = array("VALUE" => "","ATTRIBUTES" => "");
921         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
922           if(isset($val[$name])){
923             $data[$attr] = $val[$name];
924           }
925         }
926         $array[$val['tag']][] = $data;
927       }else{
928 #print_a($val);
929       }
930     }
931     return($array);
932   }
939   /*! \brief  Updates an entry with a set of new values, 
940     @param  Integer The ID of the entry, we want to update.
941     @param  Array   The variables to update.   
942     @return Boolean Returns TRUE on success. 
943    */
944   public function update_entries($ids,$data)
945   {
946     $this->reset_error();
947     if(!is_array($ids)){
948       trigger_error("Requires an array as first parameter.");
949       return;
950     }
952     if(!is_array($data)){
953       trigger_error("Requires an array as second parameter.");
954       return;
955     }
957     $attr = "";
958     foreach($data as $key => $value){
959       $key = strtolower($key);
960       if(is_array($value)){
961         foreach($value as $sub_value){
962           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
963         }
964       }else{
965         $attr.= "<$key>".strtolower($value)."</$key>\n";
966       }
967     }
969     $xml_msg = "<xml>
970       <header>gosa_update_status_jobdb_entry</header>
971       <target>GOSA</target>
972       <source>GOSA</source>
973       <where>
974       <clause>
975       <connector>or</connector>";
976     foreach($ids as $id){
977       $xml_msg .= "<phrase>
978         <operator>eq</operator>
979         <id>".$id."</id>
980         </phrase>";
981     }
982     $xml_msg .= "</clause>
983       </where>
984       <update>
985       ".$attr." 
986       </update>
987       </xml>";
989     if($this->connect()){
991       $this->o_sock->write($xml_msg);
992       $str      = trim($this->o_sock->read());
994       /* Check if something went wrong while reading */
995       if($this->o_sock->is_error()){
996         $this->set_error($this->o_sock->get_error());
997         return(FALSE);
998       }
1000       $entries = $this->xml_to_array($str);
1001       if(isset($entries['XML'])){
1002         if(isset($entries['XML']['ERROR_STRING'])) {
1003           $this->set_error($entries['XML']['ERROR_STRING']);
1004           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
1005           return(FALSE);
1006         }
1007         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
1008         return(TRUE);
1009       }
1010     }
1011     return(FALSE);
1012   }
1015   /*! \brief  Returns the number of currently queued objects.
1016       @return Integer  
1017    */
1018   public function number_of_queued_entries($event_types)
1019   {
1020     $tags = "";
1021     foreach($event_types as $type){
1022       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
1023     }
1024     if(count($event_types) > 1){
1025       $tags = "<connector>or</connector>".$tags;
1026     }
1027     if(count($event_types)){
1028       $tags = "<where><clause>".$tags."</clause></where>";
1029     }
1032     $xml_msg =
1033       "<xml>".
1034       "<header>gosa_query_jobdb</header>".
1035       "<target>GOSA</target>".
1036       "<source>GOSA</source>".
1037       "<select> count ID</select>".
1038       $tags.
1039       "</xml>";
1041     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
1042     $this->connect();
1043     if($this->connect()){
1044       $this->o_sock->write($xml_msg);
1045       $str     = trim($this->o_sock->read());
1047       /* Check if something went wrong while reading */
1048       if($this->o_sock->is_error()){
1049         $this->set_error($this->o_sock->get_error());
1050         return(0);
1051       }
1053       $entries = $this->xml_to_array($str);
1054       if(isset($entries['XML'])){
1055         return($entries['XML']['COUNT']);
1056       }
1057     }
1058     return(-1);
1059   } 
1062   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
1063   {
1064     $xml_message= "";
1066     /* Prepare data */
1067     foreach ($data as $key => $value){
1068       if(is_array($value)){
1069         foreach($value as $sub_value){
1070           $xml_message.= "<$key>$sub_value</$key>";
1071         }
1072       }else{
1073         $xml_message.= "<$key>$value</$key>";
1074       }
1075     }
1077     /* Multiple targets? */
1078     if (!is_array($to)){
1079       $to_targets= array($to);
1080     } else {
1081       $to_targets= $to;
1082     }
1084     /* Build target strings */
1085     $target ="";
1086     foreach($to_targets as $to){
1087       $target.= "<target>$to</target>";
1088     }
1090     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1091   }
1094   /* Allows simply appending a new DaemonEvent 
1095    */
1096   public function append($event)
1097   {
1098     if(!($event instanceof DaemonEvent)){
1099       return(FALSE);
1100     }
1101   
1102     $this->reset_error();
1104     /* Add to queue if new 
1105      */
1106     if($event->is_new()){
1108       $request_answer = FALSE;
1109       if($event->get_type() == SCHEDULED_EVENT){
1110         $action = $event->get_schedule_action();
1111       }elseif($event->get_type() == TRIGGERED_EVENT){
1112         $action = $event->get_trigger_action();
1113       }else{
1114         trigger_error("Unknown type of queue event given.");
1115         return(FALSE);
1116       }
1118       /* Get event informations, like targets..
1119        */
1120       $targets    = $event->get_targets();
1121       $data       = $event->save();
1123       /* Append an entry for each target 
1124        */
1125       foreach($targets as $target){
1126         $data['macaddress'] = $target;
1127         $this->send_data($action,$target,$data,$request_answer);
1129         if($this->is_error()){
1130           return(FALSE);
1131         }
1132       }
1133       return(TRUE);
1134     }else{
1136       /* Updated edited entry.
1137        */
1138       $id                 = $event->get_id();
1139       $data               = $event->save();
1140       return($this->update_entries(array($id),$data));
1141     }
1143     return(FALSE);
1144   }
1147 /*! \brief  Returns an array containing all queued entries.
1148     @return Array All queued entries as an array.
1149    */
1150   public function _send($data, $answer_expected= FALSE)
1151   {
1152     $this->reset_error();
1153     $ret = array();
1155     if($this->connect()){
1156       $this->o_sock->write($data);
1157       if ($answer_expected){
1158         $str = trim($this->o_sock->read());
1160         /* Check if something went wrong while reading */
1161         if($this->o_sock->is_error()){
1162           $this->set_error($this->o_sock->get_error());
1163           return($ret);
1164         }
1166         $entries = $this->xml_to_array($str);
1167         if(isset($entries['XML']) && is_array($entries['XML'])){
1168           $ret = $entries;
1169           if($this->use_alternative_xml_parse_method) {
1170             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1171               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1172               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1173                   array($data=>$data),"FAILED ".$this->get_error());
1174             }
1175           }else{
1176             if(isset($entries['XML']['ERROR_STRING'])) {
1177               $this->set_error($entries['XML']['ERROR_STRING']);
1178               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1179                   array($data=>$data),"FAILED ".$this->get_error());
1180             }elseif(isset($entries['XML']['ERROR'])){
1181               $this->set_error($entries['XML']['ERROR']);
1182               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1183                   array($data=>$data),"FAILED ".$this->get_error());
1184             }
1185           }
1186           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1187               array($data=>$data),"SUCCESS");
1188         }
1189       }else{
1190         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1191             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1192       }
1193     }
1194     return($ret);
1195   }
1198   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1199   {
1200     $xml_message= "";
1202     /* Get communication object */
1203     $d= new gosaSupportDaemon(TRUE,10);
1205     /* Prepare data */
1206     foreach ($data as $key => $value){
1207       if(is_array($value)){
1208         foreach($value as $sub_val){
1209           $xml_message.= "<$key>$sub_val</$key>";
1210         }
1211       }else{
1212         $xml_message.= "<$key>$value</$key>";
1213       }
1214     }
1216     /* Multiple targets? */
1217     if (!is_array($to)){
1218       $to_targets= array($to);
1219     } else {
1220       $to_targets= $to;
1221     }
1223     /* Build target strings */
1224     $target ="";
1225     foreach($to_targets as $to){
1226       $target.= "<target>$to</target>";
1227     }
1229     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1230   }
1233   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1234       @param  String  $mac  The mac address for which we want to remove all jobs.      
1235    */
1236   function clean_queue_from_mac($mac)
1237   {
1238     global $config;
1240     /* First of all we have to check which jobs are startet 
1241      *  for $mac 
1242      */
1243     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1244     
1245     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1246  
1247     $data = $this->_send($xml_msg,TRUE);
1248     if(is_array($data) && isset($data['XML'])){
1249       $already_aborted = FALSE;
1250       foreach($data['XML']  as $name => $entry){
1251         if(preg_match("/answer[0-9]*/i",$name)){
1252           $entry['STATUS'] = strtoupper($entry['STATUS']);
1253           switch($entry['STATUS']){
1255             case 'PROCESSING' :
1257               /* Send abort event, but only once 
1258                */
1259               if($already_aborted){
1260                 break;
1261               }elseif(class_available("DaemonEvent_faireboot")){
1262                 $already_aborted = TRUE;
1263                 $tmp = new DaemonEvent_faireboot($config);
1264                 $tmp->add_targets(array($mac));
1265                 $tmp->set_type(TRIGGERED_EVENT);
1266                 if(!$this->append($tmp)){
1267                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1268                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1269                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1270                 }else{
1271                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1272                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1273                 }
1274                 ;break;
1275               }else{
1276                 /* Couldn't find abort event, just remove entry */
1277               }
1279             case 'WAITING':
1280             case 'ERROR':
1281             default :
1282             
1283               /* Simply remove entries from queue. 
1284                *  Failed or waiting events, can be removed without any trouble.
1285                */ 
1286               if(!$this->remove_entries(array($entry['ID']))){
1287                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1288               }
1289               ;break;
1290           }
1291     
1292         }
1293       }
1294     }
1295   }
1298   static function ping($target)
1299   {
1300     if (tests::is_mac($target)){
1301       /* Get communication object */
1302       $d= new gosaSupportDaemon(TRUE,0.5);
1303       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1304       return (count($answer) ? TRUE:FALSE);
1305     }
1306     return (FALSE);
1307   }
1311   /*! \brief  Returns a list of all configured principals. 
1312               (Uses the GOsa support daemon instead of the ldap database.)
1313       @return Array  A list containing the names of all configured principals.
1314    */
1315   public function krb5_list_principals($server)
1316   {
1317     $res = array();  
1319     /* Check if the given server is a valid mac address
1320      */
1321     if(!tests::is_mac($server)){
1322       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1323       return($ret);
1324     }
1326     /* Prepare request event 
1327      */ 
1328     $xml_msg = 
1329       "<xml>".
1330       "<header>gosa_krb5_list_principals</header>".
1331       "<source>GOSA</source>".
1332       "<target>".$server."</target>".
1333       "</xml>";
1334     
1335     $tmp = $this->_send($xml_msg,TRUE);
1336     if(isset($tmp['XML']['PRINCIPAL'])){
1337       return($tmp['XML']['PRINCIPAL']);
1338     }else{
1339       return($res);
1340     }
1341   }
1344   /*! \brief  Returns the configuration settings for a given principal name. 
1345               (Uses the GOsa support daemon instead of the ldap database.)
1346       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1347       @return Array  A list containing the names of all configured principals.
1348    */
1349   public function krb5_get_principal($server,$name)
1350   {
1351     $ret = array();
1353     /* Check if the given name is a valid request value 
1354      */
1355     if(!is_string($name) || empty($name)){
1356       trigger_error("The given principal name is not of type string or it is empty.");
1357       return($ret);
1358     }
1360     /* Check if the given server is a valid mac address
1361      */
1362     if(!tests::is_mac($server)){
1363       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1364       return($ret);
1365     }
1367     /* Prepare request event 
1368      */ 
1369     $xml_msg = 
1370       "<xml>".
1371       "<header>gosa_krb5_get_principal</header>".
1372       "<principal>".$name."</principal>".
1373       "<source>GOSA</source>".
1374       "<target>".$server."</target>".
1375       "</xml>";
1377     $res = $this->_send($xml_msg,TRUE);
1378     if(isset($res['XML'])){
1379       return($res['XML']);
1380     }else{
1381       return($ret);
1382     }
1383   }
1386   /*! \brief  Creates a given principal with a set of configuration settings.
1387               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1388               (Uses the GOsa support daemon instead of the ldap database.)
1389       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1390       @return Boolean   TRUE on success else FALSE. 
1391    */
1392   public function krb5_add_principal($server,$name,$values)
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     }
1402     if(!is_array($values)){
1403       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1404       return($ret);
1405     }
1407     /* Check if the given server is a valid mac address
1408      */
1409     if(!tests::is_mac($server)){
1410       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1411       return($ret);
1412     }
1414     $attrs = "";
1415     foreach($values as $key => $value){
1416       if(empty($key) || is_numeric($key)){
1417         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1418         return($ret);
1419       }
1420       $key = strtolower($key);
1421       if(is_array($value)){
1422         foreach($value as $val){
1423           $attrs.= "<$key>$val</$key>\n";
1424         }
1425       }else{
1426         $attrs.= "<$key>$value</$key>\n";
1427       }
1428     }
1430     /* Prepare request event 
1431      */ 
1432     $xml_msg = 
1433       "<xml>".
1434       "<header>gosa_krb5_create_principal</header>".
1435       "<principal>".$name."</principal>".
1436       $attrs.
1437       "<source>GOSA</source>".
1438       "<target>".$server."</target>".
1439       "</xml>";
1441     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1442   }
1445   function krb5_ramdomize_key($server,$name)  
1446   {
1447     /* Prepare request event 
1448      */ 
1449     $xml_msg = 
1450       "<xml>".
1451       "<header>gosa_krb5_randomize_key</header>".
1452       "<principal>".$name."</principal>".
1453       "<source>GOSA</source>".
1454       "<target>".$server."</target>".
1455       "</xml>";
1457     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1458   }
1459   
1462   /*! \brief  Updates a given principal with a set of configuration settings.
1463               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1464               (Uses the GOsa support daemon instead of the ldap database.)
1465       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1466       @return Boolean   TRUE on success else FALSE. 
1467    */
1468   public function krb5_set_principal($server,$name,$values)
1469   {
1470     $ret = FALSE;  
1472     /* Check if the given name is a valid request value 
1473      */
1474     if(!is_string($name) || empty($name)){
1475       trigger_error("The given principal name is not of type string or it is empty.");
1476       return($ret);
1477     }
1478     if(!is_array($values) || !count($values)){
1479       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1480       return($ret);
1481     }
1483     /* Check if the given server is a valid mac address
1484      */
1485     if(!tests::is_mac($server)){
1486       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1487       return($ret);
1488     }
1490     $attrs = "";
1491     foreach($values as $key => $value){
1492       if(empty($key) || is_numeric($key)){
1493         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1494         return($ret);
1495       }
1496       $key = strtolower($key);
1497       if(is_array($value)){
1498         foreach($value as $val){
1499           $attrs.= "<$key>$val</$key>\n";
1500         }
1501       }else{
1502         $attrs.= "<$key>$value</$key>\n";
1503       }
1504     }
1506     /* Prepare request event 
1507      */ 
1508     $xml_msg = 
1509       "<xml>".
1510       "<header>gosa_krb5_modify_principal</header>".
1511       "<principal>".$name."</principal>".
1512       $attrs.
1513       "<source>GOSA</source>".
1514       "<target>".$server."</target>".
1515       "</xml>";
1517     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1518   }
1521   /*! \brief  Removes the given principal.
1522               (Uses the GOsa support daemon instead of the ldap database.)
1523       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1524       @return Boollean   TRUE on success else FALSE
1525    */
1526   public function krb5_del_principal($server,$name)
1527   {
1528     $ret = FALSE;  
1530     /* Check if the given name is a valid request value 
1531      */
1532     if(!is_string($name) || empty($name)){
1533       trigger_error("The given principal name is not of type string or it is empty.");
1534       return($ret);
1535     }
1537     /* Check if the given server is a valid mac address
1538      */
1539     if(!tests::is_mac($server)){
1540       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1541       return($ret);
1542     }
1544     /* Prepare request event 
1545      */ 
1546     $xml_msg = 
1547       "<xml>".
1548       "<header>gosa_krb5_del_principal</header>".
1549       "<principal>".$name."</principal>".
1550       "<source>GOSA</source>".
1551       "<target>".$server."</target>".
1552       "</xml>";
1553     
1554     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1555   }
1558   /*! \brief  Returns a list of configured password policies.
1559               (Uses the GOsa support daemon instead of the ldap database.)
1560       @return Array A list of all configured password policies.
1561    */
1562   public function krb5_list_policies($server)
1563   {
1564     $res = array();  
1566     /* Check if the given server is a valid mac address
1567      */
1568     if(!tests::is_mac($server)){
1569       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1570       return($ret);
1571     }
1573     /* Prepare request event 
1574      */ 
1575     $xml_msg = 
1576       "<xml>".
1577       "<header>gosa_krb5_list_policies</header>".
1578       "<source>GOSA</source>".
1579       "<target>".$server."</target>".
1580       "</xml>";
1581     
1582     $res = $this->_send($xml_msg,TRUE);
1583     
1584     /* Check if there are results for POLICY 
1585      */
1586     if(isset($res['XML']['POLICY'])){
1587       
1588       /* Ensure that we return an array 
1589        */
1590       $tmp = $res['XML']['POLICY'];
1591       if(!is_array($tmp)){
1592         $tmp = array($tmp);
1593       }
1594       return($tmp);
1595     }else{
1596       return(array());
1597     }
1598   }
1601   /*! \brief  Returns a list of configured password policies.
1602               (Uses the GOsa support daemon instead of the ldap database.)
1603       @return Array The policy settings for the given policy name.
1604    */
1605   public function krb5_get_policy($server,$name)
1606   {
1607     $res = array();  
1609     /* Check if the given name is a valid request value 
1610      */
1611     if(!is_string($name) || empty($name)){
1612       trigger_error("The given policy name is not of type string or it is empty.");
1613       return($ret);
1614     }
1616     /* Check if the given server is a valid mac address
1617      */
1618     if(!tests::is_mac($server)){
1619       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1620       return($ret);
1621     }
1623     /* Prepare request event 
1624      */ 
1625     $xml_msg = 
1626       "<xml>".
1627       "<header>gosa_krb5_get_policy</header>".
1628       "<policy>".$name."</policy>".
1629       "<source>GOSA</source>".
1630       "<target>".$server."</target>".
1631       "</xml>";
1633     /* Possible attributes */
1634     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1635         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1637   
1638     $tmp = $this->_send($xml_msg,TRUE);
1639     if(isset($tmp['XML'])){
1640       foreach($attrs as $attr){
1641         if(isset($tmp['XML'][$attr])){
1642           $ret[$attr] = $tmp['XML'][$attr];
1643         }else{
1644           $ret[$attr] = "";
1645         }
1646       }
1647     }
1648     return($ret);
1649   }
1651   
1652   /*! \brief  Creates a new policy with a given set of configuration settings.
1653               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1654               (Uses the GOsa support daemon instead of the ldap database.)
1655       @pram   String The name of the policy to update.
1656       @pram   Array  The attributes to update
1657       @return Boolean   TRUE on success else FALSE. 
1658    */
1659   public function krb5_add_policy($server,$name,$values)
1660   {
1661     $ret = FALSE;  
1663     /* Check if the given name is a valid request value 
1664      */
1665     if(!is_string($name) || empty($name)){
1666       trigger_error("The given policy name is not of type string or it is empty.");
1667       return($ret);
1668     }
1669     if(!is_array($values) || !count($values)){
1670       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1671       return($ret);
1672     }
1674     /* Check if the given server is a valid mac address
1675      */
1676     if(!tests::is_mac($server)){
1677       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1678       return($ret);
1679     }
1682     /* Transform array into <xml>
1683      */
1684     $attrs = "";
1685     foreach($values as $id => $value){
1686       if(empty($id) || is_numeric($id)){
1687         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1688         return($ret);
1689       }
1690       $id = strtolower($id);
1691       $attrs.= "<$id>$value</$id>\n";
1692     }
1694     /* Prepare request event 
1695      */ 
1696     $xml_msg = 
1697       "<xml>".
1698       "<header>gosa_krb5_create_policy</header>".
1699       "<policy>".$name."</policy>".
1700       $attrs.
1701       "<source>GOSA</source>".
1702       "<target>".$server."</target>".
1703       "</xml>";
1705     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1706   }
1709   /*! \brief  Updates a given policy with a set of configuration settings.
1710               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1711               (Uses the GOsa support daemon instead of the ldap database.)
1712       @pram   String The name of the policy to update.
1713       @return Boolean   TRUE on success else FALSE. 
1714    */
1715   public function krb5_set_policy($server,$name,$values)
1716   {
1717     $ret = FALSE;  
1719     /* Check if the given name is a valid request value 
1720      */
1721     if(!is_string($name) || empty($name)){
1722       trigger_error("The given policy name is not of type string or it is empty.");
1723       return($ret);
1724     }
1725     if(!is_array($values) || !count($values)){
1726       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1727       return($ret);
1728     }
1730     /* Check if the given server is a valid mac address
1731      */
1732     if(!tests::is_mac($server)){
1733       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1734       return($ret);
1735     }
1737     /* Transform array into <xml>
1738      */
1739     $attrs = "";
1740     foreach($values as $id => $value){
1741       if(preg_match("/^policy$/i",$id)) continue;
1742       if(empty($id) || is_numeric($id)){
1743         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1744         return($ret);
1745       }
1746       $id = strtolower($id);
1747       $attrs.= "<$id>$value</$id>\n";
1748     }
1750     /* Prepare request event 
1751      */ 
1752     $xml_msg = 
1753       "<xml>".
1754       "<header>gosa_krb5_modify_policy</header>".
1755       "<policy>".$name."</policy>".
1756       $attrs.
1757       "<source>GOSA</source>".
1758       "<target>".$server."</target>".
1759       "</xml>";
1761     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1762   }
1764   
1765   /*! \brief  Removes the given password policy. 
1766               (Uses the GOsa support daemon instead of the ldap database.)
1767       @return Boolean  TRUE on success else FALSE
1768    */
1769   public function krb5_del_policy($server,$name)
1770   {
1771     $ret = FALSE;
1773     /* Check if the given server is a valid mac address
1774      */
1775     if(!tests::is_mac($server)){
1776       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1777       return($ret);
1778     }
1780     /* Check if the given name is a valid request value 
1781      */
1782     if(!is_string($name) || empty($name)){
1783       trigger_error("The given policy name is not of type string or it is empty.");
1784       return($ret);
1785     }
1787     /* Prepare request event 
1788      */ 
1789     $xml_msg = 
1790       "<xml>".
1791       "<header>gosa_krb5_del_policy</header>".
1792       "<policy>".$name."</policy>".
1793       "<source>GOSA</source>".
1794       "<target>".$server."</target>".
1795       "</xml>";
1796     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1797   }
1800   /*! \brief  Sets the password of for the given principal.
1801               (Uses the GOsa support daemon instead of the ldap database.)
1802       @param  String  The servers mac
1803       @param  String  The principals name
1804       @param  String  $the new password.   
1805       @return Boolean  TRUE on success else FALSE
1806    */
1807   public function krb5_set_password($server,$name,$password)
1808   {
1809     $ret = FALSE;
1811     /* Check if the given server is a valid mac address
1812      */
1813     if(!tests::is_mac($server)){
1814       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1815       return($ret);
1816     }
1818     /* Check if the given name is a valid request value
1819      */
1820     if(!is_string($name) || empty($name)){
1821       trigger_error("The given principal name is not of type string or it is empty.");
1822       return($ret);
1823     }
1825     /* Prepare request event
1826      */
1827     $xml_msg =
1828       "<xml>".
1829       "<header>gosa_krb5_set_password</header>".
1830       "<principal>".$name."</principal>".
1831       "<password>".$password."</password>".
1832       "<source>GOSA</source>".
1833       "<target>".$server."</target>".
1834       "</xml>";
1835     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1836   }
1839   /*! \brief  Returns log file informations for a given mac address 
1840       @param  $mac The mac address to fetch logs for.
1841       @retrun Array A Multidimensional array containing log infos.
1842         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1843         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1844                                install_20080313_144450   ...
1845    */
1846   public function get_log_info_for_mac($mac)
1847   {
1848     $xml_msg = "
1849       <xml>
1850       <header>gosa_show_log_by_mac</header>
1851       <target>GOSA</target>
1852       <source>GOSA</source>
1853       <mac>".$mac."</mac>
1854       </xml>";
1856     $res = $this->_send($xml_msg,TRUE);
1857     $ret = array();
1858     if(isset($res['XML'])){
1860       /* Filter all entry that look like this 
1861           MAC_00_01_6C_9D_B9_FA
1862        */
1863       foreach($res['XML'] as $name => $entry){
1864         if(preg_match("/^MAC/",$name)){
1866           /* Get list of available log files 
1867            */
1868           foreach($entry as $log_date){
1869             $xml_msg2 = "<xml> 
1870               <header>gosa_show_log_files_by_date_and_mac</header> 
1871               <target>GOSA</target> 
1872               <source>GOSA</source>                        
1873               <date>".$log_date."</date> 
1874               <mac>".$mac."</mac> 
1875               </xml>";
1877             $ret[$mac][$log_date] = array();
1878             $res = $this->_send($xml_msg2,TRUE);
1879             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1880             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1881             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1882               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1883             }
1884           }
1885         }
1886       }
1887     }
1888     return($ret);
1889   }
1891   public function get_log_file($mac,$date,$file)
1892   {
1893     $xml_msg ="
1894       <xml> 
1895       <header>gosa_get_log_file_by_date_and_mac</header> 
1896       <target>GOSA</target> 
1897       <source>GOSA</source>
1898       <date>".$date."</date> 
1899       <mac>".$mac."</mac> 
1900       <log_file>".$file."</log_file>
1901       </xml>";
1903     $res = $this->_send($xml_msg,TRUE);
1904     if(isset($res['XML'][strtoupper($file)])){
1905       return(base64_decode($res['XML'][strtoupper($file)]));
1906     }
1907     return("");
1908   }
1914   /*****************
1915    * DAK - Functions 
1916    *****************/
1918   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1919       @param  ...
1920       @return Array   All queued entries.
1921    */
1922   public function DAK_keyring_entries($server)  
1923   {
1924     /* Ensure that we send the event to a valid mac address 
1925      */
1926     if(!is_string($server) || !tests::is_mac($server)){
1927       trigger_error("No valid mac address given '".$server."'.");
1928       return;
1929     }
1931     /* Create query
1932      */
1933     $xml_msg = "<xml> 
1934                   <header>gosa_get_dak_keyring</header> 
1935                   <target>".$server."</target> 
1936                   <source>GOSA</source>
1937                 </xml>";
1938         
1939     $res = $this->_send($xml_msg,TRUE);
1941     /* Check if there are results for POLICY
1942      */
1943     if(isset($res['XML'])){
1944       $ret = array();
1945       foreach($res['XML'] as $key => $entry){
1946         if(preg_match("/^ANSWER/",$key)){
1947           $ret[] = $entry;
1948         }
1949       }
1950       return($ret);
1951     }else{
1952       return(array());
1953     }
1954   }
1957   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1958       @param  String  The servers mac address 
1959       @param  String  The gpg key.
1960       @return Boolean TRUE on success else FALSE 
1961    */
1962   public function DAK_import_key($server,$key)  
1963   {
1964     /* Ensure that we send the event to a valid mac address 
1965      */
1966     if(!is_string($server) || !tests::is_mac($server)){
1967       trigger_error("No valid mac address given '".$server."'.");
1968       return;
1969     }
1971     /* Check if there is some cleanup required before importing the key.
1972         There may be some Header lines like:
1973         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1974      */
1975     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1977       /* Remove header */
1978       $key = preg_replace("/^.*\n\n/sim","",$key);
1979       /* Remove footer */
1980       $key = preg_replace("/-----.*$/sim","",$key);
1981     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1982       
1983       /* Encode key if it is raw.
1984        */
1985       $key = base64_encode($key);
1986     }
1988     /* Create query
1989      */
1990     $xml_msg = "<xml> 
1991                   <header>gosa_import_dak_key</header> 
1992                   <target>".$server."</target> 
1993                   <key>".$key."</key> 
1994                   <source>GOSA</source>
1995                 </xml>";
1996         
1997     $res = $this->_send($xml_msg,TRUE);
1998     return($this->is_error());
1999   }
2002   /*! \brief Removes a key from the keyring on the given server. 
2003       @param  String  The servers mac address 
2004       @param  String  The gpg key uid.
2005       @return Boolean TRUE on success else FALSE 
2006    */
2007   public function DAK_remove_key($server,$key)  
2008   {
2009     /* Ensure that we send the event to a valid mac address 
2010      */
2011     if(!is_string($server) || !tests::is_mac($server)){
2012       trigger_error("No valid mac address given '".$server."'.");
2013       return;
2014     }
2016     /* Create query
2017      */
2018     $xml_msg = "<xml> 
2019                   <header>gosa_remove_dak_key</header> 
2020                   <target>".$server."</target> 
2021                   <keyid>".$key."</keyid> 
2022                   <source>GOSA</source>
2023                 </xml>";
2024        
2025     $res = $this->_send($xml_msg,TRUE);
2026     return($this->is_error());
2027   }
2030 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
2031 ?>