Code

Added robustness to tabs::save()
[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;
37   /*! \brief  Creates a new gosaSupportDaemon object.
38     @param string   Host    The Host where the daemon is running on.  
39     @param integer  Port    The port which the daemon use.
40     @param string   Key     The encryption string.
41     @param boolean  Connect Directly connect to daemon socket.
42     @param float    Timeout The timelimit for all socket actions.
43    */
44   public function __construct($connect=TRUE,$timeout=10)
45   {
46     #FIXME: bad idea about referencing global variables from within classes
47     global $config;
49     # load from config, store statically
50     if (isset($config->current['GOSA_SI'])){
52       if ($this->s_host == ""){
53         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
54         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
55         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
56       }
58       $this->f_timeout = $timeout;
59       if($connect){
60         $this->connect();
61       }
62     }
63   }
66   /*! \brief  Establish daemon connection. 
67     @return boolean Returns true if the connection was succesfully established. 
68    */
69   public function connect()
70   {
71     if(!empty($this->s_host) && !empty($this->i_port)){
72       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
73       if($this->o_sock->connected()){ 
74         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
75         $this->is_connected = TRUE;
76       }else{
77         $this->set_error($this->o_sock->get_error());
78         $this->disconnect();
79         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
80       }
81     }else{
82       $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
83     }
84     return($this->is_connected);
85   }
88   /*! \brief  Disconnect from gosa daemon.
89    */
90   public function disconnect()
91   {
92     $this->o_sock->close();
93     $this->is_connected = FALSE;
94   }
97   /*! \brief  Sets an error message, which can be returned with get_error().
98     @param  string  The Error message,
99    */
100   private function set_error($str)
101   {
102     $this->b_error = TRUE;
103     $this->s_error = $str;
104   }
107   /*! \brief  Sets an error message, which can be returned with get_error().
108     @param  string  The Error message,
109    */
110   private function reset_error()
111   {
112     $this->b_error = FALSE;
113     $this->s_error = "";
114   }
117   /*! \brief  Checks if an error occured.
118     @return boolean returns TRUE or FALSE, whether there is an error or not.
119    */
120   public function is_error()
121   {
122     return($this->b_error);
123   }
126   /*! \brief  Returns the last error. 
127     @return Returns the last error.
128    */
129   public function get_error()
130   {
131     $str = $this->s_error;
132     $ret = "";
133     if(is_string($str)){
134       $ret = $str;
135     }else{
136       foreach($str as $msg){
137         $ret .= $msg." ";
138       }
139     }
140     $ret = preg_replace("/ /","&nbsp;",$ret);
141     return($ret);
142   }
145   public function FAI_get_kernels($release)
146   {
147     $xml_msg = 
148       "<xml>".
149       "<header>gosa_get_available_kernel</header>".
150       "<source>GOSA</source>".
151       "<target>GOSA</target>".
152       "<release>".$release."</release>".
153       "</xml>";
155     $ret = array();
156     if($this->connect()){
157       $this->o_sock->write($xml_msg);
158       $str = trim($this->o_sock->read());
160       /* Check if something went wrong while reading */
161       if($this->o_sock->is_error()){
162         $this->set_error($this->o_sock->get_error());
163         return($ret);
164       }
166       $entries = $this->xml_to_array($str);
167       if(isset($entries['XML']) && is_array($entries['XML'])){
169         /* Check if returned values represent a valid answer */
170         if(isset($entries['XML'])){
171           if(isset($entries['XML']['ERROR_STRING'])) {
172             $this->set_error($entries['XML']['ERROR_STRING']);
173             new log("debug","GOsa-si",
174                 get_class($this)."::".__FUNCTION__, array(),
175                 "FAILED error was ".$this->get_error());
176             return($ret);
177           }
179           /* Unset header tags */
180           $ret = $entries['XML'];
181           foreach($ret as $key => $entry){
182             if(!preg_match("/^answer/i",$key)){
183               unset($ret[$key]);
184             }
185           }
186         }
187       }
188     }
189     return($ret);
190   }
193   public function FAI_get_package_sections($release)
194   {
195     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
196       "<select>distinct section</select>".
197       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
199     $ret = array();
200     if($this->connect()){
201       $this->o_sock->write($xml_msg);
202       $str = trim($this->o_sock->read());
204       /* Check if something went wrong while reading */
205       if($this->o_sock->is_error()){
206         $this->set_error($this->o_sock->get_error());
207         return($ret);
208       }
210       $entries = $this->xml_to_array($str);
211       if(isset($entries['XML']) && is_array($entries['XML'])){
213         /* Check if returned values represent a valid answer */
214         if(isset($entries['XML'])){
215           if(isset($entries['XML']['ERROR_STRING'])) {
216             $this->set_error($entries['XML']['ERROR_STRING']);
217             new log("debug","GOsa-si",
218                 get_class($this)."::".__FUNCTION__, array(),
219                 "FAILED error was ".$this->get_error());
220             return($ret);
221           }
223           /* Unset header tags */
224           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
225             if(isset($entries['XML'][$type])){
226               unset($entries['XML'][$type]);
227             }
228           }
229           $ret = $entries['XML'];
230         }
231       }
232     }
233     return($ret);
234   }
237   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
238   {
239     $this->reset_error();
240     $ret = array();
242     /* Check Parameter */
243     if(!is_array($attrs) || !count($attrs)){
244       trigger_error("Second parameter must be an array. With at least one attribute name.");
245       return($ret);
246     }
248     /* Check Parameter */
249     if(!is_array($package)){
250       trigger_error("Third parameter must be an array. With at least one attribute name.");
251       return($ret);
252     }
254     /* Create list of attributes to fetch */
255     $attr = ""; 
256     foreach($attrs as $at){
257       $attr.= "<select>".$at."</select>";
258     }
260     /* If no package is given, search for all */
261     if(!count($package)) $package = array("%");
263     /* Create limit tag */
264     if($from == -1){
265       $limit =""; 
266     }else{
267       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
268     }
270     /* Create list of attributes to fetch */
271     $pkgs = ""; 
272     foreach($package as $pkg){
273       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
274     }
276     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
277       $attr.
278       "<where>
279       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
280       <clause><connector>OR</connector>
281       ".$pkgs."
282       </clause>
283       </where>".
284       $limit.
285       "</xml>";
287     if($this->connect()){
288       $this->o_sock->write($xml_msg);
289       $str = trim($this->o_sock->read());
291       /* Check if something went wrong while reading */
292       if($this->o_sock->is_error()){
293         $this->set_error($this->o_sock->get_error());
294         return($ret);
295       }
297       $entries = $this->xml_to_array($str);
298       if(isset($entries['XML']) && is_array($entries['XML'])){
300         /* Check if returned values represent a valid answer */
301         if(isset($entries['XML'])){
302           if(isset($entries['XML']['ERROR_STRING'])) {
303             $this->set_error($entries['XML']['ERROR_STRING']);
304             new log("debug","GOsa-si",
305                 get_class($this)."::".__FUNCTION__, array(),
306                 "FAILED error was ".$this->get_error());
307             return($ret);
308           }
310           /* Unset header tags */
311           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
312             if(isset($entries['XML'][$type])){
313               unset($entries['XML'][$type]);
314             }
315           }
316           $ret = $entries['XML'];
317         }
318       }
319     }
320     return($ret);
322     
323   }
326   public function FAI_get_server($name = "")
327   {
328     $this->reset_error();
330     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
331     $ret = array();
332     if($this->connect()){
333       $this->o_sock->write($xml_msg);
334       $str = trim($this->o_sock->read());
336       /* Check if something went wrong while reading */
337       if($this->o_sock->is_error()){
338         $this->set_error($this->o_sock->get_error());
339         return($ret);
340       }
342       $entries = $this->xml_to_array($str);
343       if(isset($entries['XML']) && is_array($entries['XML'])){
345         /* Check if returned values represent a valid answer */
346         if(isset($entries['XML'])){
347           if(isset($entries['XML']['ERROR_STRING'])) {
348             $this->set_error($entries['XML']['ERROR_STRING']);
349             new log("debug","GOsa-si", 
350               get_class($this)."::".__FUNCTION__, array(),
351               "FAILED error was ".$this->get_error());
352             return($ret);
353           }
355           /* Unset header tags */
356           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
357             if(isset($entries['XML'][$type])){
358               unset($entries['XML'][$type]);
359             }
360           }
361           $ret = $entries['XML']; 
362         }
363       }
364     }
365     return($ret);
366   }
369   public function FAI_get_classes($name)
370   {
371     $this->reset_error();
372     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
373                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
374     $ret = array();
375     if($this->connect()){
376       $this->o_sock->write($xml_msg);
377       $str = trim($this->o_sock->read());
379       /* Check if something went wrong while reading */
380       if($this->o_sock->is_error()){
381         $this->set_error($this->o_sock->get_error());
382         return($ret);
383       }
385       $entries = $this->xml_to_array($str);
386       if(isset($entries['XML']) && is_array($entries['XML'])){
388         /* Check if returned values represent a valid answer */
389         if(isset($entries['XML'])){
390           if(isset($entries['XML']['ERROR_STRING'])) {
391             $this->set_error($entries['XML']['ERROR_STRING']);
392             new log("debug","GOsa-si", 
393               get_class($this)."::".__FUNCTION__, array($name),
394               "FAILED error was ".$this->get_error());
395             return($ret);
396           }
398           /* Unset header tags */
399           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
400             if(isset($entries['XML'][$type])){
401               unset($entries['XML'][$type]);
402             }
403           }
404           $ret = $entries['XML']; 
405         }
406       }
407     }
408     return($ret);
409   }
412   /*! \brief  Returns an array containing all queued entries.
413     @return Array All queued entries as an array.
414    */
415   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
416   {
417     $this->reset_error();
418     $ret = array();
420     $tags = "";
421     foreach($event_types as $type){
422       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
423     }
424     if(count($event_types) > 1){
425       $tags = "<connector>or</connector>".$tags;
426     }
427     if(count($event_types)){
428       $tags = "<where><clause>".$tags."</clause></where>";
429     }
431     $xml_msg = 
432       "<xml>
433       <header>gosa_query_jobdb</header>
434       <target>GOSA</target>
435       <source>GOSA</source>
436       ".$tags."
438       <orderby>".$sort."</orderby>";
439     if($from != -1 && $to != -1){
440       $xml_msg.= "
441         <limit>
442         <from>".$from."</from>
443         <to>".$to."</to>
444         </limit>";
445     }
446     $xml_msg.= "
447       </xml>";
449     if($this->connect()){
450       $this->o_sock->write($xml_msg);
451       $str = trim($this->o_sock->read());
453       /* Check if something went wrong while reading */
454       if($this->o_sock->is_error()){
455         $this->set_error($this->o_sock->get_error());
456         return($ret);
457       }
459       $entries = $this->xml_to_array($str);
460       if(isset($entries['XML']) && is_array($entries['XML'])){
462         /* Check if returned values represent a valid answer */
463         if(isset($entries['XML'])){
464           
465           /* Unset header tags */
466           foreach(array("HEADER","SOURCE","TARGET") as $type){
467             unset($entries['XML'][$type]);
468           }
469           $ret = $entries['XML']; 
470         }
471       }
472     }
473     
474     /* Remove session ID. No one is interested in this... */
475     unset($ret['SESSION_ID']);
477     return($ret);
478   }
481   /*! \brief  Checks if the given ids are used queue ids.
482     @param  Array   The ids we want to check..
483     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
484    */
485   public function ids_exist($ids)
486   {
487     if(!is_array($ids)){
488       trigger_error("Requires an array as parameter.");
489       return;
490     }
491     $this->reset_error();
493     $ret = array();
495     $xml_msg = "<xml>
496       <header>gosa_query_jobdb</header>
497       <target>GOSA</target>
498       <source>GOSA</source>
499       <where>
500       <clause>
501       <connector>or</connector>";
502     foreach($ids as $id){
503       $xml_msg .= "<phrase>
504         <operator>eq</operator>
505         <id>".$id."</id>
506         </phrase>";
507     }
508     $xml_msg .= "</clause>
509       </where>
510       </xml>";
512     if($this->connect()){
513       $this->o_sock->write($xml_msg);
514       $str = trim($this->o_sock->read());
516       /* Check if something went wrong while reading */
517       if($this->o_sock->is_error()){
518         $this->set_error($this->o_sock->get_error());
519         return($ret);
520       }
522       $entries = $this->xml_to_array($str);
523       if(isset($entries['XML']) && is_array($entries['XML'])){
524         foreach($entries['XML'] as $entry){
525           if(is_array($entry) && array_key_exists("ID",$entry)){
526             $ret[] = $entry['ID'];
527           }
528         }
529       }
530     }
531     return($ret);
532   }
535   /*! \brief  Returns an entry containing all requested ids.
536     @param  Array   The IDs of the entries we want to return.
537     @return Array   Of the requested entries. 
538    */
539   public function get_entries_by_mac($macs)
540   {
541     if(!is_array($macs)){
542       trigger_error("Requires an array as parameter.");
543       return;
544     }
545     $this->reset_error();
547     $ret = array();
549     $xml_msg = "<xml>
550       <header>gosa_query_jobdb</header>
551       <target>GOSA</target>
552       <source>GOSA</source>
553       <where>
554       <clause>
555       <connector>or</connector>";
556     foreach($macs as $mac){
557       $xml_msg .= "<phrase>
558         <operator>eq</operator>
559         <macaddress>".$mac."</macaddress>
560         </phrase>";
561     }
562     $xml_msg .= "</clause>
563       </where>
564       </xml>";
566     if($this->connect()){
567       $this->o_sock->write($xml_msg);
568       $str = trim($this->o_sock->read());
570       /* Check if something went wrong while reading */
571       if($this->o_sock->is_error()){
572         $this->set_error($this->o_sock->get_error());
573         return($ret);
574       }
576       $entries = $this->xml_to_array($str); 
577       if(isset($entries['XML'])){
578         foreach($entries['XML'] as $name => $entry){
579           if(preg_match("/^ANSWER[0-9]*$/",$name)){
580             $ret[$name] = $entry;
581           }
582         }
583       }
584     }
585     return($ret);
586   }
589   /*! \brief  Returns an entry containing all requested ids.
590     @param  Array   The IDs of the entries we want to return.
591     @return Array   Of the requested entries. 
592    */
593   public function get_entries_by_id($ids)
594   {
595     if(!is_array($ids)){
596       trigger_error("Requires an array as parameter.");
597       return;
598     }
599     $this->reset_error();
601     $ret = array();
603     $xml_msg = "<xml>
604       <header>gosa_query_jobdb</header>
605       <target>GOSA</target>
606       <source>GOSA</source>
607       <where>
608       <clause>
609       <connector>or</connector>";
610     foreach($ids as $id){
611       $xml_msg .= "<phrase>
612         <operator>eq</operator>
613         <id>".$id."</id>
614         </phrase>";
615     }
616     $xml_msg .= "</clause>
617       </where>
618       </xml>";
620     if($this->connect()){
621       $this->o_sock->write($xml_msg);
622       $str = trim($this->o_sock->read());
624       /* Check if something went wrong while reading */
625       if($this->o_sock->is_error()){
626         $this->set_error($this->o_sock->get_error());
627         return($ret);
628       }
630       $entries = $this->xml_to_array($str); 
631       if(isset($entries['XML'])){
632         foreach($entries['XML'] as $name => $entry){
633           if(preg_match("/^ANSWER[0-9]*$/",$name)){
634             $ret[$name] = $entry;
635           }
636         }
637       }
638     }
639     return($ret);
640   }
643   /*! \brief  Checks if the given id is in use.
644     @param  Integer The ID of the entry.
645     @return Boolean TRUE if entry exists. 
646    */
647   public function id_exists($id)
648   {
649     if(!is_numeric($id)){
650       trigger_error("Requires an integer as parameter.");
651       return;
652     }
654     $this->reset_error();
656     $xml_msg = "<xml>
657       <header>gosa_query_jobdb</header>
658       <target>GOSA</target>
659       <source>GOSA</source>
660       <where>
661       <clause>
662       <phrase>
663       <operator>eq</operator>
664       <id>".$id."</id>
665       </phrase>
666       </clause>
667       </where>
668       </xml>";
670     if($this->connect()){
671       $this->o_sock->write($xml_msg);
672       $str = trim($this->o_sock->read());
674       /* Check if something went wrong while reading */
675       if($this->o_sock->is_error()){
676         $this->set_error($this->o_sock->get_error());
677         return(FALSE);
678       }
680       $entries = $this->xml_to_array($str); 
681       if( isset($entries['XML']['HEADER']) && 
682           $entries['XML']['HEADER']=="answer" && 
683           isset($entries['XML']['ANSWER1'])){
684         return(TRUE);
685       }
686     }
687     return(FALSE);
688   }
691   /*! \brief  Returns an entry from the gosaSupportQueue
692     @param  Integer The ID of the entry we want to return.
693     @return Array   Of the requested entry. 
694    */
695   public function get_entry_by_id($id)
696   {
697     if(!is_numeric($id)){
698       trigger_error("Requires an integer as parameter.");
699       return;
700     }
701     $this->reset_error();
702   
703     $ret = array();
704     $xml_msg = "<xml>
705       <header>gosa_query_jobdb</header>
706       <target>GOSA</target>
707       <source>GOSA</source>
708       <where>
709       <clause>
710       <phrase>
711       <operator>eq</operator>
712       <id>".$id."</id>
713       </phrase>
714       </clause>
715       </where>
716       </xml>";
717     if($this->connect()){
718       $this->o_sock->write($xml_msg);
719       $str = trim($this->o_sock->read());
721       /* Check if something went wrong while reading */
722       if($this->o_sock->is_error()){
723         $this->set_error($this->o_sock->get_error());
724         return($ret);
725       }
727       $entries = $this->xml_to_array($str); 
728       if( isset($entries['XML']['HEADER']) &&
729           $entries['XML']['HEADER']=="answer" &&
730           isset($entries['XML']['ANSWER1'])){
731         $ret = $entries['XML']['ANSWER1'];
732       }
733     }
734     return($ret);
735   }
738   /*! \brief  Removes a set of entries from the GOsa support queue. 
739     @param  Array The IDs to remove.
740     @return Boolean True on success.
741    */
742   public function remove_entries($ids)
743   {
744     if(!is_array($ids)){
745       trigger_error("Requires an array as parameter.");
746       return;
747     }
749     $this->reset_error();
751     $ret = array();
753     $xml_msg = "<xml>
754       <header>gosa_delete_jobdb_entry</header>
755       <target>GOSA</target>
756       <source>GOSA</source>
757       <where>
758       <clause>
759       <connector>or</connector>";
760     foreach($ids as $id){
761       $xml_msg .= "<phrase>
762         <operator>eq</operator>
763         <id>".$id."</id>
764         </phrase>";
765     }
766     $xml_msg .= "</clause>
767       </where>
768       </xml>";
770     if($this->connect()){
771       $this->o_sock->write($xml_msg);
772       $str = $this->o_sock->read();
774       /* Check if something went wrong while reading */
775       if($this->o_sock->is_error()){
776         $this->set_error($this->o_sock->get_error());
777         return($ret);
778       }
780       $entries = $this->xml_to_array($str);
781       if(isset($entries['XML']) || isset($entries['COUNT'])){
782         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
783         return(TRUE);
784       }else{
785         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
786       }
787     }
788     return(FALSE);
789   }
793   /*! \brief  Removes an entry from the GOsa support queue. 
794     @param  Integer The ID of the entry we want to remove.
795     @return Boolean True on success.
796    */
797   public function remove_entry($id)
798   {
799     return($this->remove_entries(array($id)));
800   }
803   /*! \brief  Parses the given xml string into an array 
804     @param  String XML string  
805     @return Array Returns an array containing the xml structure. 
806    */
807   private function xml_to_array($xml)
808   {
809     $params = array();
810     $level = array();
811     $parser  = xml_parser_create_ns();
812     xml_parse_into_struct($parser, $xml, $vals, $index);
814     $err_id = xml_get_error_code($parser);
815     if($err_id){
816       xml_parser_free($parser);
817     }else{
818       xml_parser_free($parser);
820       foreach ($vals as $xml_elem) {
821         if ($xml_elem['type'] == 'open') {
822           if (array_key_exists('attributes',$xml_elem)) {
823             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
824           } else {
825             $level[$xml_elem['level']] = $xml_elem['tag'];
826           }
827         }
828         if ($xml_elem['type'] == 'complete') {
829           $start_level = 1;
830           $test2 = &$params;
831           while($start_level < $xml_elem['level']) {
832             $test2 = &$test2[$level[$start_level]];
833             $start_level++;
834           }
835           if(!isset($test2[$xml_elem['tag']])){
836             if(isset($xml_elem['value'])){
837               $test2[$xml_elem['tag']] = $xml_elem['value'];
838             }
839           }else{
840             if(!is_array($test2[$xml_elem['tag']])){
841               $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
842             }
843             $test2[$xml_elem['tag']][] = $xml_elem['value'];
844           }
845         }
846       }
847     }
849     if(!isset($params['XML'])){
850       if (!array_key_exists('XML', $params)){
851         $this->set_error(_("Cannot not parse XML!"));
852       }
853       $params = array("COUNT" => 0);
854     }
856     return($params); 
857   }
860   /*! \brief  Updates an entry with a set of new values, 
861     @param  Integer The ID of the entry, we want to update.
862     @param  Array   The variables to update.   
863     @return Boolean Returns TRUE on success. 
864    */
865   public function update_entries($ids,$data)
866   {
867     $this->reset_error();
868     if(!is_array($ids)){
869       trigger_error("Requires an array as first parameter.");
870       return;
871     }
873     if(!is_array($data)){
874       trigger_error("Requires an array as second parameter.");
875       return;
876     }
878     $attr = "";
879     foreach($data as $key => $value){
880       $key = strtolower($key);
881       if(is_array($value)){
882         foreach($value as $sub_value){
883           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
884         }
885       }else{
886         $attr.= "<$key>".strtolower($value)."</$key>\n";
887       }
888     }
890     $xml_msg = "<xml>
891       <header>gosa_update_status_jobdb_entry</header>
892       <target>GOSA</target>
893       <source>GOSA</source>
894       <where>
895       <clause>
896       <connector>or</connector>";
897     foreach($ids as $id){
898       $xml_msg .= "<phrase>
899         <operator>eq</operator>
900         <id>".$id."</id>
901         </phrase>";
902     }
903     $xml_msg .= "</clause>
904       </where>
905       <update>
906       ".$attr." 
907       </update>
908       </xml>";
910     if($this->connect()){
912       $this->o_sock->write($xml_msg);
913       $str      = trim($this->o_sock->read());
915       /* Check if something went wrong while reading */
916       if($this->o_sock->is_error()){
917         $this->set_error($this->o_sock->get_error());
918         return(FALSE);
919       }
921       $entries = $this->xml_to_array($str);
922       if(isset($entries['XML'])){
923         if(isset($entries['XML']['ERROR_STRING'])) {
924           $this->set_error($entries['XML']['ERROR_STRING']);
925           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
926           return(FALSE);
927         }
928         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
929         return(TRUE);
930       }
931     }
932     return(FALSE);
933   }
936   /*! \brief  Returns the number of currently queued objects.
937       @return Integer  
938    */
939   public function number_of_queued_entries($event_types)
940   {
941     $tags = "";
942     foreach($event_types as $type){
943       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
944     }
945     if(count($event_types) > 1){
946       $tags = "<connector>or</connector>".$tags;
947     }
948     if(count($event_types)){
949       $tags = "<where><clause>".$tags."</clause></where>";
950     }
953     $xml_msg =
954       "<xml>".
955       "<header>gosa_query_jobdb</header>".
956       "<target>GOSA</target>".
957       "<source>GOSA</source>".
958       "<select> count ID</select>".
959       $tags.
960       "</xml>";
962     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
963     $this->connect();
964     if($this->connect()){
965       $this->o_sock->write($xml_msg);
966       $str     = trim($this->o_sock->read());
968       /* Check if something went wrong while reading */
969       if($this->o_sock->is_error()){
970         $this->set_error($this->o_sock->get_error());
971         return(0);
972       }
974       $entries = $this->xml_to_array($str);
975       if(isset($entries['XML'])){
976         return($entries['XML']['COUNT']);
977       }
978     }
979     return(-1);
980   } 
983   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
984   {
985     $xml_message= "";
987     /* Prepare data */
988     foreach ($data as $key => $value){
989       if(is_array($value)){
990         foreach($value as $sub_value){
991           $xml_message.= "<$key>$sub_value</$key>";
992         }
993       }else{
994         $xml_message.= "<$key>$value</$key>";
995       }
996     }
998     /* Multiple targets? */
999     if (!is_array($to)){
1000       $to_targets= array($to);
1001     } else {
1002       $to_targets= $to;
1003     }
1005     /* Build target strings */
1006     $target ="";
1007     foreach($to_targets as $to){
1008       $target.= "<target>$to</target>";
1009     }
1011     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1012   }
1015   /* Allows simply appending a new DaemonEvent 
1016    */
1017   public function append($event)
1018   {
1019     if(!($event instanceof DaemonEvent)){
1020       return(FALSE);
1021     }
1022   
1023     $this->reset_error();
1025     /* Add to queue if new 
1026      */
1027     if($event->is_new()){
1029       $request_answer = FALSE;
1030       if($event->get_type() == SCHEDULED_EVENT){
1031         $action = $event->get_schedule_action();
1032       }elseif($event->get_type() == TRIGGERED_EVENT){
1033         $action = $event->get_trigger_action();
1034       }else{
1035         trigger_error("Unknown type of queue event given.");
1036         return(FALSE);
1037       }
1039       /* Get event informations, like targets..
1040        */
1041       $targets    = $event->get_targets();
1042       $data       = $event->save();
1044       /* Append an entry for each target 
1045        */
1046       foreach($targets as $target){
1047         $data['macaddress'] = $target;
1048         $this->send_data($action,$target,$data,$request_answer);
1050         if($this->is_error()){
1051           return(FALSE);
1052         }
1053       }
1054       return(TRUE);
1055     }else{
1057       /* Updated edited entry.
1058        */
1059       $id                 = $event->get_id();
1060       $data               = $event->save();
1061       return($this->update_entries(array($id),$data));
1062     }
1064     return(FALSE);
1065   }
1068 /*! \brief  Returns an array containing all queued entries.
1069     @return Array All queued entries as an array.
1070    */
1071   public function _send($data, $answer_expected= FALSE)
1072   {
1073     $this->reset_error();
1074     $ret = array();
1076     if($this->connect()){
1077       $this->o_sock->write($data);
1078       if ($answer_expected){
1079         $str = trim($this->o_sock->read());
1081         /* Check if something went wrong while reading */
1082         if($this->o_sock->is_error()){
1083           $this->set_error($this->o_sock->get_error());
1084           return($ret);
1085         }
1087         $entries = $this->xml_to_array($str);
1088         if(isset($entries['XML']) && is_array($entries['XML'])){
1089           $ret = $entries;
1090           if(isset($entries['XML']['ERROR_STRING'])) {
1091             $this->set_error($entries['XML']['ERROR_STRING']);
1092             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1093           }elseif(isset($entries['XML']['ERROR'])){
1094             $this->set_error($entries['XML']['ERROR']);
1095             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1096           }else{
1097             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1098           }
1099         }
1100       }else{
1101         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1102       }
1103     }
1104     return($ret);
1105   }
1108   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1109   {
1110     $xml_message= "";
1112     /* Get communication object */
1113     $d= new gosaSupportDaemon(TRUE,10);
1115     /* Prepare data */
1116     foreach ($data as $key => $value){
1117       if(is_array($value)){
1118         foreach($value as $sub_val){
1119           $xml_message.= "<$key>$sub_value</$key>";
1120         }
1121       }else{
1122         $xml_message.= "<$key>$value</$key>";
1123       }
1124     }
1126     /* Multiple targets? */
1127     if (!is_array($to)){
1128       $to_targets= array($to);
1129     } else {
1130       $to_targets= $to;
1131     }
1133     /* Build target strings */
1134     $target ="";
1135     foreach($to_targets as $to){
1136       $target.= "<target>$to</target>";
1137     }
1139     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1140   }
1143   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1144       @param  String  $mac  The mac address for which we want to remove all jobs.      
1145    */
1146   function clean_queue_from_mac($mac)
1147   {
1148     global $config;
1150     /* First of all we have to check which jobs are startet 
1151      *  for $mac 
1152      */
1153     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1154     
1155     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1156  
1157     $data = $this->_send($xml_msg,TRUE);
1158     if(is_array($data) && isset($data['XML'])){
1159       $already_aborted = FALSE;
1160       foreach($data['XML']  as $name => $entry){
1161         if(preg_match("/answer[0-9]*/i",$name)){
1162           $entry['STATUS'] = strtoupper($entry['STATUS']);
1163           switch($entry['STATUS']){
1165             case 'PROCESSING' :
1167               /* Send abort event, but only once 
1168                */
1169               if($already_aborted){
1170                 break;
1171               }elseif(class_available("DaemonEvent_faireboot")){
1172                 $already_aborted = TRUE;
1173                 $tmp = new DaemonEvent_faireboot($config);
1174                 $tmp->add_targets(array($mac));
1175                 $tmp->set_type(TRIGGERED_EVENT);
1176                 if(!$this->append($tmp)){
1177                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1178                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1179                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1180                 }else{
1181                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1182                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1183                 }
1184                 ;break;
1185               }else{
1186                 /* Couldn't find abort event, just remove entry */
1187               }
1189             case 'WAITING':
1190             case 'ERROR':
1191             default :
1192             
1193               /* Simply remove entries from queue. 
1194                *  Failed or waiting events, can be removed without any trouble.
1195                */ 
1196               if(!$this->remove_entries(array($entry['ID']))){
1197                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1198               }
1199               ;break;
1200           }
1201     
1202         }
1203       }
1204     }
1205   }
1208   static function ping($target)
1209   {
1210     if (tests::is_mac($target)){
1211       /* Get communication object */
1212       $d= new gosaSupportDaemon(TRUE,0.5);
1213       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1214       return (count($answer) ? TRUE:FALSE);
1215     }
1216     return (FALSE);
1217   }
1221   /*! \brief  Returns a list of all configured principals. 
1222               (Uses the GOsa support daemon instead of the ldap database.)
1223       @return Array  A list containing the names of all configured principals.
1224    */
1225   public function krb5_list_principals($server)
1226   {
1227     $res = array();  
1229     /* Check if the given server is a valid mac address
1230      */
1231     if(!tests::is_mac($server)){
1232       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1233       return($ret);
1234     }
1236     /* Prepare request event 
1237      */ 
1238     $xml_msg = 
1239       "<xml>".
1240       "<header>gosa_krb5_list_principals</header>".
1241       "<source>GOSA</source>".
1242       "<target>".$server."</target>".
1243       "</xml>";
1244     
1245     $tmp = $this->_send($xml_msg,TRUE);
1246     if(isset($tmp['XML']['PRINCIPAL'])){
1247       return($tmp['XML']['PRINCIPAL']);
1248     }else{
1249       return($res);
1250     }
1251   }
1254   /*! \brief  Returns the configuration settings for a given principal name. 
1255               (Uses the GOsa support daemon instead of the ldap database.)
1256       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1257       @return Array  A list containing the names of all configured principals.
1258    */
1259   public function krb5_get_principal($server,$name)
1260   {
1261     $ret = array();
1263     /* Check if the given name is a valid request value 
1264      */
1265     if(!is_string($name) || empty($name)){
1266       trigger_error("The given principal name is not of type string or it is empty.");
1267       return($ret);
1268     }
1270     /* Check if the given server is a valid mac address
1271      */
1272     if(!tests::is_mac($server)){
1273       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1274       return($ret);
1275     }
1277     /* Prepare request event 
1278      */ 
1279     $xml_msg = 
1280       "<xml>".
1281       "<header>gosa_krb5_get_principal</header>".
1282       "<principal>".$name."</principal>".
1283       "<source>GOSA</source>".
1284       "<target>".$server."</target>".
1285       "</xml>";
1287     $res = $this->_send($xml_msg,TRUE);
1288     if(isset($res['XML'])){
1289       return($res['XML']);
1290     }else{
1291       return($ret);
1292     }
1293   }
1296   /*! \brief  Creates a given principal with a set of configuration settings.
1297               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1298               (Uses the GOsa support daemon instead of the ldap database.)
1299       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1300       @return Boolean   TRUE on success else FALSE. 
1301    */
1302   public function krb5_add_principal($server,$name,$values)
1303   {
1304     $ret = FALSE;  
1306     /* Check if the given name is a valid request value 
1307      */
1308     if(!is_string($name) || empty($name)){
1309       trigger_error("The given principal name is not of type string or it is empty.");
1310       return($ret);
1311     }
1312     if(!is_array($values) || !count($values)){
1313       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1314       return($ret);
1315     }
1317     /* Check if the given server is a valid mac address
1318      */
1319     if(!tests::is_mac($server)){
1320       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1321       return($ret);
1322     }
1324     $attrs = "";
1325     foreach($values as $key => $value){
1326       if(empty($key) || is_numeric($key)){
1327         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1328         return($ret);
1329       }
1330       $key = strtolower($key);
1331       if(is_array($value)){
1332         foreach($value as $val){
1333           $attrs.= "<$key>$val</$key>\n";
1334         }
1335       }else{
1336         $attrs.= "<$key>$value</$key>\n";
1337       }
1338     }
1340     /* Prepare request event 
1341      */ 
1342     $xml_msg = 
1343       "<xml>".
1344       "<header>gosa_krb5_create_principal</header>".
1345       "<principal>".$name."</principal>".
1346       $attrs.
1347       "<source>GOSA</source>".
1348       "<target>".$server."</target>".
1349       "</xml>";
1351     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1352   }
1355   /*! \brief  Updates a given principal with a set of configuration settings.
1356               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1357               (Uses the GOsa support daemon instead of the ldap database.)
1358       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1359       @return Boolean   TRUE on success else FALSE. 
1360    */
1361   public function krb5_set_principal($server,$name,$values)
1362   {
1363     $ret = FALSE;  
1365     /* Check if the given name is a valid request value 
1366      */
1367     if(!is_string($name) || empty($name)){
1368       trigger_error("The given principal name is not of type string or it is empty.");
1369       return($ret);
1370     }
1371     if(!is_array($values) || !count($values)){
1372       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1373       return($ret);
1374     }
1376     /* Check if the given server is a valid mac address
1377      */
1378     if(!tests::is_mac($server)){
1379       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1380       return($ret);
1381     }
1383     $attrs = "";
1384     foreach($values as $key => $value){
1385       if(empty($key) || is_numeric($key)){
1386         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1387         return($ret);
1388       }
1389       $key = strtolower($key);
1390       if(is_array($value)){
1391         foreach($value as $val){
1392           $attrs.= "<$key>$val</$key>\n";
1393         }
1394       }else{
1395         $attrs.= "<$key>$value</$key>\n";
1396       }
1397     }
1399     /* Prepare request event 
1400      */ 
1401     $xml_msg = 
1402       "<xml>".
1403       "<header>gosa_krb5_modify_principal</header>".
1404       "<principal>".$name."</principal>".
1405       $attrs.
1406       "<source>GOSA</source>".
1407       "<target>".$server."</target>".
1408       "</xml>";
1410     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1411   }
1414   /*! \brief  Removes the given principal.
1415               (Uses the GOsa support daemon instead of the ldap database.)
1416       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1417       @return Boollean   TRUE on success else FALSE
1418    */
1419   public function krb5_del_principal($server,$name)
1420   {
1421     $ret = FALSE;  
1423     /* Check if the given name is a valid request value 
1424      */
1425     if(!is_string($name) || empty($name)){
1426       trigger_error("The given principal name is not of type string or it is empty.");
1427       return($ret);
1428     }
1430     /* Check if the given server is a valid mac address
1431      */
1432     if(!tests::is_mac($server)){
1433       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1434       return($ret);
1435     }
1437     /* Prepare request event 
1438      */ 
1439     $xml_msg = 
1440       "<xml>".
1441       "<header>gosa_krb5_del_principal</header>".
1442       "<principal>".$name."</principal>".
1443       "<source>GOSA</source>".
1444       "<target>".$server."</target>".
1445       "</xml>";
1446     
1447     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1448   }
1451   /*! \brief  Returns a list of configured password policies.
1452               (Uses the GOsa support daemon instead of the ldap database.)
1453       @return Array A list of all configured password policies.
1454    */
1455   public function krb5_list_policies($server)
1456   {
1457     $res = array();  
1459     /* Check if the given server is a valid mac address
1460      */
1461     if(!tests::is_mac($server)){
1462       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1463       return($ret);
1464     }
1466     /* Prepare request event 
1467      */ 
1468     $xml_msg = 
1469       "<xml>".
1470       "<header>gosa_krb5_list_policies</header>".
1471       "<source>GOSA</source>".
1472       "<target>".$server."</target>".
1473       "</xml>";
1474     
1475     $res = $this->_send($xml_msg,TRUE);
1476     
1477     /* Check if there are results for POLICY 
1478      */
1479     if(isset($res['XML']['POLICY'])){
1480       
1481       /* Ensure that we return an array 
1482        */
1483       $tmp = $res['XML']['POLICY'];
1484       if(!is_array($tmp)){
1485         $tmp = array($tmp);
1486       }
1487       return($tmp);
1488     }else{
1489       return(array());
1490     }
1491   }
1494   /*! \brief  Returns a list of configured password policies.
1495               (Uses the GOsa support daemon instead of the ldap database.)
1496       @return Array The policy settings for the given policy name.
1497    */
1498   public function krb5_get_policy($server,$name)
1499   {
1500     $res = array();  
1502     /* Check if the given name is a valid request value 
1503      */
1504     if(!is_string($name) || empty($name)){
1505       trigger_error("The given policy name is not of type string or it is empty.");
1506       return($ret);
1507     }
1509     /* Check if the given server is a valid mac address
1510      */
1511     if(!tests::is_mac($server)){
1512       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1513       return($ret);
1514     }
1516     /* Prepare request event 
1517      */ 
1518     $xml_msg = 
1519       "<xml>".
1520       "<header>gosa_krb5_get_policy</header>".
1521       "<policy>".$name."</policy>".
1522       "<source>GOSA</source>".
1523       "<target>".$server."</target>".
1524       "</xml>";
1526     /* Possible attributes */
1527     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1528         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1530   
1531     $tmp = $this->_send($xml_msg,TRUE);
1532     if(isset($tmp['XML'])){
1533       foreach($attrs as $attr){
1534         if(isset($tmp['XML'][$attr])){
1535           $ret[$attr] = $tmp['XML'][$attr];
1536         }else{
1537           $ret[$attr] = "";
1538         }
1539       }
1540     }
1541     return($ret);
1542   }
1544   
1545   /*! \brief  Creates a new policy with a given set of configuration settings.
1546               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1547               (Uses the GOsa support daemon instead of the ldap database.)
1548       @pram   String The name of the policy to update.
1549       @pram   Array  The attributes to update
1550       @return Boolean   TRUE on success else FALSE. 
1551    */
1552   public function krb5_add_policy($server,$name,$values)
1553   {
1554     $ret = FALSE;  
1556     /* Check if the given name is a valid request value 
1557      */
1558     if(!is_string($name) || empty($name)){
1559       trigger_error("The given policy name is not of type string or it is empty.");
1560       return($ret);
1561     }
1562     if(!is_array($values) || !count($values)){
1563       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1564       return($ret);
1565     }
1567     /* Check if the given server is a valid mac address
1568      */
1569     if(!tests::is_mac($server)){
1570       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1571       return($ret);
1572     }
1575     /* Transform array into <xml>
1576      */
1577     $attrs = "";
1578     foreach($values as $id => $value){
1579       if(empty($id) || is_numeric($id)){
1580         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1581         return($ret);
1582       }
1583       $id = strtolower($id);
1584       $attrs.= "<$id>$value</$id>\n";
1585     }
1587     /* Prepare request event 
1588      */ 
1589     $xml_msg = 
1590       "<xml>".
1591       "<header>gosa_krb5_create_policy</header>".
1592       "<policy>".$name."</policy>".
1593       $attrs.
1594       "<source>GOSA</source>".
1595       "<target>".$server."</target>".
1596       "</xml>";
1598     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1599   }
1602   /*! \brief  Updates a given policy with a set of configuration settings.
1603               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1604               (Uses the GOsa support daemon instead of the ldap database.)
1605       @pram   String The name of the policy to update.
1606       @return Boolean   TRUE on success else FALSE. 
1607    */
1608   public function krb5_set_policy($server,$name,$values)
1609   {
1610     $ret = FALSE;  
1612     /* Check if the given name is a valid request value 
1613      */
1614     if(!is_string($name) || empty($name)){
1615       trigger_error("The given policy name is not of type string or it is empty.");
1616       return($ret);
1617     }
1618     if(!is_array($values) || !count($values)){
1619       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1620       return($ret);
1621     }
1623     /* Check if the given server is a valid mac address
1624      */
1625     if(!tests::is_mac($server)){
1626       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1627       return($ret);
1628     }
1630     /* Transform array into <xml>
1631      */
1632     $attrs = "";
1633     foreach($values as $id => $value){
1634       if(preg_match("/^policy$/i",$id)) continue;
1635       if(empty($id) || is_numeric($id)){
1636         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1637         return($ret);
1638       }
1639       $id = strtolower($id);
1640       $attrs.= "<$id>$value</$id>\n";
1641     }
1643     /* Prepare request event 
1644      */ 
1645     $xml_msg = 
1646       "<xml>".
1647       "<header>gosa_krb5_modify_policy</header>".
1648       "<policy>".$name."</policy>".
1649       $attrs.
1650       "<source>GOSA</source>".
1651       "<target>".$server."</target>".
1652       "</xml>";
1654     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1655   }
1657   
1658   /*! \brief  Removes the given password policy. 
1659               (Uses the GOsa support daemon instead of the ldap database.)
1660       @return Boolean  TRUE on success else FALSE
1661    */
1662   public function krb5_del_policy($server,$name)
1663   {
1664     $ret = FALSE;
1666     /* Check if the given server is a valid mac address
1667      */
1668     if(!tests::is_mac($server)){
1669       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1670       return($ret);
1671     }
1673     /* Check if the given name is a valid request value 
1674      */
1675     if(!is_string($name) || empty($name)){
1676       trigger_error("The given policy name is not of type string or it is empty.");
1677       return($ret);
1678     }
1680     /* Prepare request event 
1681      */ 
1682     $xml_msg = 
1683       "<xml>".
1684       "<header>gosa_krb5_del_policy</header>".
1685       "<policy>".$name."</policy>".
1686       "<source>GOSA</source>".
1687       "<target>".$server."</target>".
1688       "</xml>";
1689     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1690   }
1693   /*! \brief  Sets the password of for the given principal.
1694               (Uses the GOsa support daemon instead of the ldap database.)
1695       @param  String  The servers mac
1696       @param  String  The principals name
1697       @param  String  $the new password.   
1698       @return Boolean  TRUE on success else FALSE
1699    */
1700   public function krb5_set_password($server,$name,$password)
1701   {
1702     $ret = FALSE;
1704     /* Check if the given server is a valid mac address
1705      */
1706     if(!tests::is_mac($server)){
1707       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1708       return($ret);
1709     }
1711     /* Check if the given name is a valid request value
1712      */
1713     if(!is_string($name) || empty($name)){
1714       trigger_error("The given principal name is not of type string or it is empty.");
1715       return($ret);
1716     }
1718     /* Prepare request event
1719      */
1720     $xml_msg =
1721       "<xml>".
1722       "<header>gosa_krb5_set_password</header>".
1723       "<principal>".$name."</principal>".
1724       "<password>".$password."</password>".
1725       "<source>GOSA</source>".
1726       "<target>".$server."</target>".
1727       "</xml>";
1728     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1729   }
1732   /*! \brief  Returns log file informations for a given mac address 
1733       @param  $mac The mac address to fetch logs for.
1734       @retrun Array A Multidimensional array containing log infos.
1735         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1736         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1737                                install_20080313_144450   ...
1738    */
1739   public function get_log_info_for_mac($mac)
1740   {
1741     $xml_msg = "
1742       <xml>
1743       <header>gosa_show_log_by_mac</header>
1744       <target>GOSA</target>
1745       <source>GOSA</source>
1746       <mac>".$mac."</mac>
1747       </xml>";
1749     $res = $this->_send($xml_msg,TRUE);
1750     $ret = array();
1751     if(isset($res['XML'])){
1753       /* Filter all entry that look like this 
1754           MAC_00_01_6C_9D_B9_FA
1755        */
1756       foreach($res['XML'] as $name => $entry){
1757         if(preg_match("/^MAC/",$name)){
1759           /* Get list of available log files 
1760            */
1761           foreach($entry as $log_date){
1762             $xml_msg2 = "<xml> 
1763               <header>gosa_show_log_files_by_date_and_mac</header> 
1764               <target>GOSA</target> 
1765               <source>GOSA</source>                        
1766               <date>".$log_date."</date> 
1767               <mac>".$mac."</mac> 
1768               </xml>";
1770             $ret[$mac][$log_date] = array();
1771             $res = $this->_send($xml_msg2,TRUE);
1772             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1773             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1774             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1775               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1776             }
1777           }
1778         }
1779       }
1780     }
1781     return($ret);
1782   }
1784   public function get_log_file($mac,$date,$file)
1785   {
1786     $xml_msg ="
1787       <xml> 
1788       <header>gosa_get_log_file_by_date_and_mac</header> 
1789       <target>GOSA</target> 
1790       <source>GOSA</source>
1791       <date>".$date."</date> 
1792       <mac>".$mac."</mac> 
1793       <log_file>".$file."</log_file>
1794       </xml>";
1796     $res = $this->_send($xml_msg,TRUE);
1797     if(isset($res['XML'][strtoupper($file)])){
1798       return(base64_decode($res['XML'][strtoupper($file)]));
1799     }
1800     return("");
1801   }
1804 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1805 ?>