Code

Updated share list acls.
[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   }
87   
88   /*! \brief  Returns TRUE whether we are connected or not 
89       @return BOOLEAN  Returns TRUE when connected else FALSE
90    */
91   public function is_connected()
92   {
93     return($this->is_connected);
94   }
95   
98   /*! \brief  Disconnect from gosa daemon.
99    */
100   public function disconnect()
101   {
102     $this->o_sock->close();
103     $this->is_connected = FALSE;
104   }
107   /*! \brief  Sets an error message, which can be returned with get_error().
108     @param  string  The Error message,
109    */
110   private function set_error($str)
111   {
112     $this->b_error = TRUE;
113     $this->s_error = $str;
114   }
117   /*! \brief  Sets an error message, which can be returned with get_error().
118     @param  string  The Error message,
119    */
120   private function reset_error()
121   {
122     $this->b_error = FALSE;
123     $this->s_error = "";
124   }
127   /*! \brief  Checks if an error occured.
128     @return boolean returns TRUE or FALSE, whether there is an error or not.
129    */
130   public function is_error()
131   {
132     return($this->b_error);
133   }
136   /*! \brief  Returns the last error. 
137     @return Returns the last error.
138    */
139   public function get_error()
140   {
141     $str = $this->s_error;
142     $ret = "";
143     if(is_string($str)){
144       $ret = $str;
145     }else{
146       foreach($str as $msg){
147         $ret .= $msg." ";
148       }
149     }
150     $ret = preg_replace("/ /","&nbsp;",$ret);
151     return($ret);
152   }
155   public function FAI_get_kernels($release)
156   {
157     $xml_msg = 
158       "<xml>".
159       "<header>gosa_get_available_kernel</header>".
160       "<source>GOSA</source>".
161       "<target>GOSA</target>".
162       "<release>".$release."</release>".
163       "</xml>";
165     $ret = array();
166     if($this->connect()){
167       $this->o_sock->write($xml_msg);
168       $str = trim($this->o_sock->read());
170       /* Check if something went wrong while reading */
171       if($this->o_sock->is_error()){
172         $this->set_error($this->o_sock->get_error());
173         return($ret);
174       }
176       $entries = $this->xml_to_array($str);
177       if(isset($entries['XML']) && is_array($entries['XML'])){
179         /* Check if returned values represent a valid answer */
180         if(isset($entries['XML'])){
181           if(isset($entries['XML']['ERROR_STRING'])) {
182             $this->set_error($entries['XML']['ERROR_STRING']);
183             new log("debug","GOsa-si",
184                 get_class($this)."::".__FUNCTION__, array(),
185                 "FAILED error was ".$this->get_error());
186             return($ret);
187           }
189           /* Unset header tags */
190           $ret = $entries['XML'];
191           foreach($ret as $key => $entry){
192             if(!preg_match("/^answer/i",$key)){
193               unset($ret[$key]);
194             }
195           }
196         }
197       }
198     }
199     return($ret);
200   }
203   public function FAI_get_package_sections($release)
204   {
205     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
206       "<select>distinct section</select>".
207       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
209     $ret = array();
210     if($this->connect()){
211       $this->o_sock->write($xml_msg);
212       $str = trim($this->o_sock->read());
214       /* Check if something went wrong while reading */
215       if($this->o_sock->is_error()){
216         $this->set_error($this->o_sock->get_error());
217         return($ret);
218       }
220       $entries = $this->xml_to_array($str);
221       if(isset($entries['XML']) && is_array($entries['XML'])){
223         /* Check if returned values represent a valid answer */
224         if(isset($entries['XML'])){
225           if(isset($entries['XML']['ERROR_STRING'])) {
226             $this->set_error($entries['XML']['ERROR_STRING']);
227             new log("debug","GOsa-si",
228                 get_class($this)."::".__FUNCTION__, array(),
229                 "FAILED error was ".$this->get_error());
230             return($ret);
231           }
233           /* Unset header tags */
234           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
235             if(isset($entries['XML'][$type])){
236               unset($entries['XML'][$type]);
237             }
238           }
239           $ret = $entries['XML'];
240         }
241       }
242     }
243     return($ret);
244   }
247   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
248   {
249     $this->reset_error();
250     $ret = array();
252     /* Check Parameter */
253     if(!is_array($attrs) || !count($attrs)){
254       trigger_error("Second parameter must be an array. With at least one attribute name.");
255       return($ret);
256     }
258     /* Check Parameter */
259     if(!is_array($package)){
260       trigger_error("Third parameter must be an array. With at least one attribute name.");
261       return($ret);
262     }
264     /* Create list of attributes to fetch */
265     $attr = ""; 
266     foreach($attrs as $at){
267       $attr.= "<select>".$at."</select>";
268     }
270     /* If no package is given, search for all */
271     if(!count($package)) $package = array("%");
273     /* Create limit tag */
274     if($from == -1){
275       $limit =""; 
276     }else{
277       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
278     }
280     /* Create list of attributes to fetch */
281     $pkgs = ""; 
282     foreach($package as $pkg){
283       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
284     }
286     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
287       $attr.
288       "<where>
289       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
290       <clause><connector>OR</connector>
291       ".$pkgs."
292       </clause>
293       </where>".
294       $limit.
295       "</xml>";
297     if($this->connect()){
298       $this->o_sock->write($xml_msg);
299       $str = trim($this->o_sock->read());
301       /* Check if something went wrong while reading */
302       if($this->o_sock->is_error()){
303         $this->set_error($this->o_sock->get_error());
304         return($ret);
305       }
307       $entries = $this->xml_to_array($str);
308       if(isset($entries['XML']) && is_array($entries['XML'])){
310         /* Check if returned values represent a valid answer */
311         if(isset($entries['XML'])){
312           if(isset($entries['XML']['ERROR_STRING'])) {
313             $this->set_error($entries['XML']['ERROR_STRING']);
314             new log("debug","GOsa-si",
315                 get_class($this)."::".__FUNCTION__, array(),
316                 "FAILED error was ".$this->get_error());
317             return($ret);
318           }
320           /* Unset header tags */
321           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
322             if(isset($entries['XML'][$type])){
323               unset($entries['XML'][$type]);
324             }
325           }
326           $ret = $entries['XML'];
327         }
328       }
329     }
330     return($ret);
332     
333   }
336   public function FAI_get_server($name = "")
337   {
338     $this->reset_error();
340     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
341     $ret = array();
342     if($this->connect()){
343       $this->o_sock->write($xml_msg);
344       $str = trim($this->o_sock->read());
346       /* Check if something went wrong while reading */
347       if($this->o_sock->is_error()){
348         $this->set_error($this->o_sock->get_error());
349         return($ret);
350       }
352       $entries = $this->xml_to_array($str);
353       if(isset($entries['XML']) && is_array($entries['XML'])){
355         /* Check if returned values represent a valid answer */
356         if(isset($entries['XML'])){
357           if(isset($entries['XML']['ERROR_STRING'])) {
358             $this->set_error($entries['XML']['ERROR_STRING']);
359             new log("debug","GOsa-si", 
360               get_class($this)."::".__FUNCTION__, array(),
361               "FAILED error was ".$this->get_error());
362             return($ret);
363           }
365           /* Unset header tags */
366           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
367             if(isset($entries['XML'][$type])){
368               unset($entries['XML'][$type]);
369             }
370           }
371           $ret = $entries['XML']; 
372         }
373       }
374     }
375     return($ret);
376   }
379   public function FAI_get_classes($name)
380   {
381     $this->reset_error();
382     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
383                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
384     $ret = array();
385     if($this->connect()){
386       $this->o_sock->write($xml_msg);
387       $str = trim($this->o_sock->read());
389       /* Check if something went wrong while reading */
390       if($this->o_sock->is_error()){
391         $this->set_error($this->o_sock->get_error());
392         return($ret);
393       }
395       $entries = $this->xml_to_array($str);
396       if(isset($entries['XML']) && is_array($entries['XML'])){
398         /* Check if returned values represent a valid answer */
399         if(isset($entries['XML'])){
400           if(isset($entries['XML']['ERROR_STRING'])) {
401             $this->set_error($entries['XML']['ERROR_STRING']);
402             new log("debug","GOsa-si", 
403               get_class($this)."::".__FUNCTION__, array($name),
404               "FAILED error was ".$this->get_error());
405             return($ret);
406           }
408           /* Unset header tags */
409           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
410             if(isset($entries['XML'][$type])){
411               unset($entries['XML'][$type]);
412             }
413           }
414           $ret = $entries['XML']; 
415         }
416       }
417     }
418     return($ret);
419   }
422   /*! \brief  Returns an array containing all queued entries.
423     @return Array All queued entries as an array.
424    */
425   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
426   {
427     $this->reset_error();
428     $ret = array();
430     $tags = "";
431     foreach($event_types as $type){
432       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
433     }
434     if(count($event_types) > 1){
435       $tags = "<connector>or</connector>".$tags;
436     }
437     if(count($event_types)){
438       $tags = "<where><clause>".$tags."</clause></where>";
439     }
441     $xml_msg = 
442       "<xml>
443       <header>gosa_query_jobdb</header>
444       <target>GOSA</target>
445       <source>GOSA</source>
446       ".$tags."
448       <orderby>".$sort."</orderby>";
449     if($from != -1 && $to != -1){
450       $xml_msg.= "
451         <limit>
452         <from>".$from."</from>
453         <to>".$to."</to>
454         </limit>";
455     }
456     $xml_msg.= "
457       </xml>";
459     if($this->connect()){
460       $this->o_sock->write($xml_msg);
461       $str = trim($this->o_sock->read());
463       /* Check if something went wrong while reading */
464       if($this->o_sock->is_error()){
465         $this->set_error($this->o_sock->get_error());
466         return($ret);
467       }
469       $entries = $this->xml_to_array($str);
470       if(isset($entries['XML']) && is_array($entries['XML'])){
472         /* Check if returned values represent a valid answer */
473         if(isset($entries['XML'])){
474           
475           /* Unset header tags */
476           foreach(array("HEADER","SOURCE","TARGET") as $type){
477             unset($entries['XML'][$type]);
478           }
479           $ret = $entries['XML']; 
480         }
481       }
482     }
483     
484     /* Remove session ID. No one is interested in this... */
485     unset($ret['SESSION_ID']);
487     return($ret);
488   }
491   /*! \brief  Checks if the given ids are used queue ids.
492     @param  Array   The ids we want to check..
493     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
494    */
495   public function ids_exist($ids)
496   {
497     if(!is_array($ids)){
498       trigger_error("Requires an array as parameter.");
499       return;
500     }
501     $this->reset_error();
503     $ret = array();
505     $xml_msg = "<xml>
506       <header>gosa_query_jobdb</header>
507       <target>GOSA</target>
508       <source>GOSA</source>
509       <where>
510       <clause>
511       <connector>or</connector>";
512     foreach($ids as $id){
513       $xml_msg .= "<phrase>
514         <operator>eq</operator>
515         <id>".$id."</id>
516         </phrase>";
517     }
518     $xml_msg .= "</clause>
519       </where>
520       </xml>";
522     if($this->connect()){
523       $this->o_sock->write($xml_msg);
524       $str = trim($this->o_sock->read());
526       /* Check if something went wrong while reading */
527       if($this->o_sock->is_error()){
528         $this->set_error($this->o_sock->get_error());
529         return($ret);
530       }
532       $entries = $this->xml_to_array($str);
533       if(isset($entries['XML']) && is_array($entries['XML'])){
534         foreach($entries['XML'] as $entry){
535           if(is_array($entry) && array_key_exists("ID",$entry)){
536             $ret[] = $entry['ID'];
537           }
538         }
539       }
540     }
541     return($ret);
542   }
545   /*! \brief  Returns an entry containing all requested ids.
546     @param  Array   The IDs of the entries we want to return.
547     @return Array   Of the requested entries. 
548    */
549   public function get_entries_by_mac($macs)
550   {
551     if(!is_array($macs)){
552       trigger_error("Requires an array as parameter.");
553       return;
554     }
555     $this->reset_error();
557     $ret = array();
559     $xml_msg = "<xml>
560       <header>gosa_query_jobdb</header>
561       <target>GOSA</target>
562       <source>GOSA</source>
563       <where>
564       <clause>
565       <connector>or</connector>";
566     foreach($macs as $mac){
567       $xml_msg .= "<phrase>
568         <operator>eq</operator>
569         <macaddress>".$mac."</macaddress>
570         </phrase>";
571     }
572     $xml_msg .= "</clause>
573       </where>
574       </xml>";
576     if($this->connect()){
577       $this->o_sock->write($xml_msg);
578       $str = trim($this->o_sock->read());
580       /* Check if something went wrong while reading */
581       if($this->o_sock->is_error()){
582         $this->set_error($this->o_sock->get_error());
583         return($ret);
584       }
586       $entries = $this->xml_to_array($str); 
587       if(isset($entries['XML'])){
588         foreach($entries['XML'] as $name => $entry){
589           if(preg_match("/^ANSWER[0-9]*$/",$name)){
590             $ret[$name] = $entry;
591           }
592         }
593       }
594     }
595     return($ret);
596   }
599   /*! \brief  Returns an entry containing all requested ids.
600     @param  Array   The IDs of the entries we want to return.
601     @return Array   Of the requested entries. 
602    */
603   public function get_entries_by_id($ids)
604   {
605     if(!is_array($ids)){
606       trigger_error("Requires an array as parameter.");
607       return;
608     }
609     $this->reset_error();
611     $ret = array();
613     $xml_msg = "<xml>
614       <header>gosa_query_jobdb</header>
615       <target>GOSA</target>
616       <source>GOSA</source>
617       <where>
618       <clause>
619       <connector>or</connector>";
620     foreach($ids as $id){
621       $xml_msg .= "<phrase>
622         <operator>eq</operator>
623         <id>".$id."</id>
624         </phrase>";
625     }
626     $xml_msg .= "</clause>
627       </where>
628       </xml>";
630     if($this->connect()){
631       $this->o_sock->write($xml_msg);
632       $str = trim($this->o_sock->read());
634       /* Check if something went wrong while reading */
635       if($this->o_sock->is_error()){
636         $this->set_error($this->o_sock->get_error());
637         return($ret);
638       }
640       $entries = $this->xml_to_array($str); 
641       if(isset($entries['XML'])){
642         foreach($entries['XML'] as $name => $entry){
643           if(preg_match("/^ANSWER[0-9]*$/",$name)){
644             $ret[$name] = $entry;
645           }
646         }
647       }
648     }
649     return($ret);
650   }
653   /*! \brief  Checks if the given id is in use.
654     @param  Integer The ID of the entry.
655     @return Boolean TRUE if entry exists. 
656    */
657   public function id_exists($id)
658   {
659     if(!is_numeric($id)){
660       trigger_error("Requires an integer as parameter.");
661       return;
662     }
664     $this->reset_error();
666     $xml_msg = "<xml>
667       <header>gosa_query_jobdb</header>
668       <target>GOSA</target>
669       <source>GOSA</source>
670       <where>
671       <clause>
672       <phrase>
673       <operator>eq</operator>
674       <id>".$id."</id>
675       </phrase>
676       </clause>
677       </where>
678       </xml>";
680     if($this->connect()){
681       $this->o_sock->write($xml_msg);
682       $str = trim($this->o_sock->read());
684       /* Check if something went wrong while reading */
685       if($this->o_sock->is_error()){
686         $this->set_error($this->o_sock->get_error());
687         return(FALSE);
688       }
690       $entries = $this->xml_to_array($str); 
691       if( isset($entries['XML']['HEADER']) && 
692           $entries['XML']['HEADER']=="answer" && 
693           isset($entries['XML']['ANSWER1'])){
694         return(TRUE);
695       }
696     }
697     return(FALSE);
698   }
701   /*! \brief  Returns an entry from the gosaSupportQueue
702     @param  Integer The ID of the entry we want to return.
703     @return Array   Of the requested entry. 
704    */
705   public function get_entry_by_id($id)
706   {
707     if(!is_numeric($id)){
708       trigger_error("Requires an integer as parameter.");
709       return;
710     }
711     $this->reset_error();
712   
713     $ret = array();
714     $xml_msg = "<xml>
715       <header>gosa_query_jobdb</header>
716       <target>GOSA</target>
717       <source>GOSA</source>
718       <where>
719       <clause>
720       <phrase>
721       <operator>eq</operator>
722       <id>".$id."</id>
723       </phrase>
724       </clause>
725       </where>
726       </xml>";
727     if($this->connect()){
728       $this->o_sock->write($xml_msg);
729       $str = trim($this->o_sock->read());
731       /* Check if something went wrong while reading */
732       if($this->o_sock->is_error()){
733         $this->set_error($this->o_sock->get_error());
734         return($ret);
735       }
737       $entries = $this->xml_to_array($str); 
738       if( isset($entries['XML']['HEADER']) &&
739           $entries['XML']['HEADER']=="answer" &&
740           isset($entries['XML']['ANSWER1'])){
741         $ret = $entries['XML']['ANSWER1'];
742       }
743     }
744     return($ret);
745   }
748   /*! \brief  Removes a set of entries from the GOsa support queue. 
749     @param  Array The IDs to remove.
750     @return Boolean True on success.
751    */
752   public function remove_entries($ids)
753   {
754     if(!is_array($ids)){
755       trigger_error("Requires an array as parameter.");
756       return;
757     }
759     $this->reset_error();
761     $ret = array();
763     $xml_msg = "<xml>
764       <header>gosa_delete_jobdb_entry</header>
765       <target>GOSA</target>
766       <source>GOSA</source>
767       <where>
768       <clause>
769       <connector>or</connector>";
770     foreach($ids as $id){
771       $xml_msg .= "<phrase>
772         <operator>eq</operator>
773         <id>".$id."</id>
774         </phrase>";
775     }
776     $xml_msg .= "</clause>
777       </where>
778       </xml>";
780     if($this->connect()){
781       $this->o_sock->write($xml_msg);
782       $str = $this->o_sock->read();
784       /* Check if something went wrong while reading */
785       if($this->o_sock->is_error()){
786         $this->set_error($this->o_sock->get_error());
787         return($ret);
788       }
790       $entries = $this->xml_to_array($str);
791       if(isset($entries['XML']) || isset($entries['COUNT'])){
792         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
793         return(TRUE);
794       }else{
795         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
796       }
797     }
798     return(FALSE);
799   }
803   /*! \brief  Removes an entry from the GOsa support queue. 
804     @param  Integer The ID of the entry we want to remove.
805     @return Boolean True on success.
806    */
807   public function remove_entry($id)
808   {
809     return($this->remove_entries(array($id)));
810   }
813   /*! \brief  Parses the given xml string into an array 
814     @param  String XML string  
815     @return Array Returns an array containing the xml structure. 
816    */
817   private function xml_to_array($xml)
818   {
819     $params = array();
820     $level = array();
821     $parser  = xml_parser_create_ns();
822     xml_parse_into_struct($parser, $xml, $vals, $index);
824     $err_id = xml_get_error_code($parser);
825     if($err_id){
826       xml_parser_free($parser);
827     }else{
828       xml_parser_free($parser);
830       foreach ($vals as $xml_elem) {
831         if ($xml_elem['type'] == 'open') {
832           if (array_key_exists('attributes',$xml_elem)) {
833             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
834           } else {
835             $level[$xml_elem['level']] = $xml_elem['tag'];
836           }
837         }
838         if ($xml_elem['type'] == 'complete') {
840           $start_level = 1;
841           $test2 = &$params;
842           while($start_level < $xml_elem['level']) {
843             $test2 = &$test2[$level[$start_level]];
844             $start_level++;
845           }
847           /* Save tag attributes too. 
848               e.g. <tag attr="val">
849            */
850           if(isset($xml_elem['attributes'])){
851             foreach($xml_elem['attributes'] as $name => $value){
852               $test2['ATTRIBUTES'][$name] = $value;
853             }
854           }
856           if(!isset($test2[$xml_elem['tag']])){
857             if(isset($xml_elem['value'])){
858               $test2[$xml_elem['tag']] = $xml_elem['value'];
859             }
860           }else{
861             if(!is_array($test2[$xml_elem['tag']])){
862               $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
863             }
864             $test2[$xml_elem['tag']][] = $xml_elem['value'];
865           }
866         }
867       }
868     }
870     if(!isset($params['XML'])){
871       if (!array_key_exists('XML', $params)){
872         $this->set_error(_("Cannot not parse XML!"));
873       }
874       $params = array("COUNT" => 0);
875     }
877     return($params); 
878   }
881   /*! \brief  Updates an entry with a set of new values, 
882     @param  Integer The ID of the entry, we want to update.
883     @param  Array   The variables to update.   
884     @return Boolean Returns TRUE on success. 
885    */
886   public function update_entries($ids,$data)
887   {
888     $this->reset_error();
889     if(!is_array($ids)){
890       trigger_error("Requires an array as first parameter.");
891       return;
892     }
894     if(!is_array($data)){
895       trigger_error("Requires an array as second parameter.");
896       return;
897     }
899     $attr = "";
900     foreach($data as $key => $value){
901       $key = strtolower($key);
902       if(is_array($value)){
903         foreach($value as $sub_value){
904           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
905         }
906       }else{
907         $attr.= "<$key>".strtolower($value)."</$key>\n";
908       }
909     }
911     $xml_msg = "<xml>
912       <header>gosa_update_status_jobdb_entry</header>
913       <target>GOSA</target>
914       <source>GOSA</source>
915       <where>
916       <clause>
917       <connector>or</connector>";
918     foreach($ids as $id){
919       $xml_msg .= "<phrase>
920         <operator>eq</operator>
921         <id>".$id."</id>
922         </phrase>";
923     }
924     $xml_msg .= "</clause>
925       </where>
926       <update>
927       ".$attr." 
928       </update>
929       </xml>";
931     if($this->connect()){
933       $this->o_sock->write($xml_msg);
934       $str      = trim($this->o_sock->read());
936       /* Check if something went wrong while reading */
937       if($this->o_sock->is_error()){
938         $this->set_error($this->o_sock->get_error());
939         return(FALSE);
940       }
942       $entries = $this->xml_to_array($str);
943       if(isset($entries['XML'])){
944         if(isset($entries['XML']['ERROR_STRING'])) {
945           $this->set_error($entries['XML']['ERROR_STRING']);
946           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
947           return(FALSE);
948         }
949         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
950         return(TRUE);
951       }
952     }
953     return(FALSE);
954   }
957   /*! \brief  Returns the number of currently queued objects.
958       @return Integer  
959    */
960   public function number_of_queued_entries($event_types)
961   {
962     $tags = "";
963     foreach($event_types as $type){
964       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
965     }
966     if(count($event_types) > 1){
967       $tags = "<connector>or</connector>".$tags;
968     }
969     if(count($event_types)){
970       $tags = "<where><clause>".$tags."</clause></where>";
971     }
974     $xml_msg =
975       "<xml>".
976       "<header>gosa_query_jobdb</header>".
977       "<target>GOSA</target>".
978       "<source>GOSA</source>".
979       "<select> count ID</select>".
980       $tags.
981       "</xml>";
983     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
984     $this->connect();
985     if($this->connect()){
986       $this->o_sock->write($xml_msg);
987       $str     = trim($this->o_sock->read());
989       /* Check if something went wrong while reading */
990       if($this->o_sock->is_error()){
991         $this->set_error($this->o_sock->get_error());
992         return(0);
993       }
995       $entries = $this->xml_to_array($str);
996       if(isset($entries['XML'])){
997         return($entries['XML']['COUNT']);
998       }
999     }
1000     return(-1);
1001   } 
1004   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
1005   {
1006     $xml_message= "";
1008     /* Prepare data */
1009     foreach ($data as $key => $value){
1010       if(is_array($value)){
1011         foreach($value as $sub_value){
1012           $xml_message.= "<$key>$sub_value</$key>";
1013         }
1014       }else{
1015         $xml_message.= "<$key>$value</$key>";
1016       }
1017     }
1019     /* Multiple targets? */
1020     if (!is_array($to)){
1021       $to_targets= array($to);
1022     } else {
1023       $to_targets= $to;
1024     }
1026     /* Build target strings */
1027     $target ="";
1028     foreach($to_targets as $to){
1029       $target.= "<target>$to</target>";
1030     }
1032     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1033   }
1036   /* Allows simply appending a new DaemonEvent 
1037    */
1038   public function append($event)
1039   {
1040     if(!($event instanceof DaemonEvent)){
1041       return(FALSE);
1042     }
1043   
1044     $this->reset_error();
1046     /* Add to queue if new 
1047      */
1048     if($event->is_new()){
1050       $request_answer = FALSE;
1051       if($event->get_type() == SCHEDULED_EVENT){
1052         $action = $event->get_schedule_action();
1053       }elseif($event->get_type() == TRIGGERED_EVENT){
1054         $action = $event->get_trigger_action();
1055       }else{
1056         trigger_error("Unknown type of queue event given.");
1057         return(FALSE);
1058       }
1060       /* Get event informations, like targets..
1061        */
1062       $targets    = $event->get_targets();
1063       $data       = $event->save();
1065       /* Append an entry for each target 
1066        */
1067       foreach($targets as $target){
1068         $data['macaddress'] = $target;
1069         $this->send_data($action,$target,$data,$request_answer);
1071         if($this->is_error()){
1072           return(FALSE);
1073         }
1074       }
1075       return(TRUE);
1076     }else{
1078       /* Updated edited entry.
1079        */
1080       $id                 = $event->get_id();
1081       $data               = $event->save();
1082       return($this->update_entries(array($id),$data));
1083     }
1085     return(FALSE);
1086   }
1089 /*! \brief  Returns an array containing all queued entries.
1090     @return Array All queued entries as an array.
1091    */
1092   public function _send($data, $answer_expected= FALSE)
1093   {
1094     $this->reset_error();
1095     $ret = array();
1097     if($this->connect()){
1098       $this->o_sock->write($data);
1099       if ($answer_expected){
1100         $str = trim($this->o_sock->read());
1102         /* Check if something went wrong while reading */
1103         if($this->o_sock->is_error()){
1104           $this->set_error($this->o_sock->get_error());
1105           return($ret);
1106         }
1108         $entries = $this->xml_to_array($str);
1109         if(isset($entries['XML']) && is_array($entries['XML'])){
1110           $ret = $entries;
1111           if(isset($entries['XML']['ERROR_STRING'])) {
1112             $this->set_error($entries['XML']['ERROR_STRING']);
1113             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1114           }elseif(isset($entries['XML']['ERROR'])){
1115             $this->set_error($entries['XML']['ERROR']);
1116             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1117           }else{
1118             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1119           }
1120         }
1121       }else{
1122         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1123       }
1124     }
1125     return($ret);
1126   }
1129   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1130   {
1131     $xml_message= "";
1133     /* Get communication object */
1134     $d= new gosaSupportDaemon(TRUE,10);
1136     /* Prepare data */
1137     foreach ($data as $key => $value){
1138       if(is_array($value)){
1139         foreach($value as $sub_val){
1140           $xml_message.= "<$key>$sub_val</$key>";
1141         }
1142       }else{
1143         $xml_message.= "<$key>$value</$key>";
1144       }
1145     }
1147     /* Multiple targets? */
1148     if (!is_array($to)){
1149       $to_targets= array($to);
1150     } else {
1151       $to_targets= $to;
1152     }
1154     /* Build target strings */
1155     $target ="";
1156     foreach($to_targets as $to){
1157       $target.= "<target>$to</target>";
1158     }
1160     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1161   }
1164   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1165       @param  String  $mac  The mac address for which we want to remove all jobs.      
1166    */
1167   function clean_queue_from_mac($mac)
1168   {
1169     global $config;
1171     /* First of all we have to check which jobs are startet 
1172      *  for $mac 
1173      */
1174     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1175     
1176     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1177  
1178     $data = $this->_send($xml_msg,TRUE);
1179     if(is_array($data) && isset($data['XML'])){
1180       $already_aborted = FALSE;
1181       foreach($data['XML']  as $name => $entry){
1182         if(preg_match("/answer[0-9]*/i",$name)){
1183           $entry['STATUS'] = strtoupper($entry['STATUS']);
1184           switch($entry['STATUS']){
1186             case 'PROCESSING' :
1188               /* Send abort event, but only once 
1189                */
1190               if($already_aborted){
1191                 break;
1192               }elseif(class_available("DaemonEvent_faireboot")){
1193                 $already_aborted = TRUE;
1194                 $tmp = new DaemonEvent_faireboot($config);
1195                 $tmp->add_targets(array($mac));
1196                 $tmp->set_type(TRIGGERED_EVENT);
1197                 if(!$this->append($tmp)){
1198                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1199                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1200                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1201                 }else{
1202                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1203                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1204                 }
1205                 ;break;
1206               }else{
1207                 /* Couldn't find abort event, just remove entry */
1208               }
1210             case 'WAITING':
1211             case 'ERROR':
1212             default :
1213             
1214               /* Simply remove entries from queue. 
1215                *  Failed or waiting events, can be removed without any trouble.
1216                */ 
1217               if(!$this->remove_entries(array($entry['ID']))){
1218                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1219               }
1220               ;break;
1221           }
1222     
1223         }
1224       }
1225     }
1226   }
1229   static function ping($target)
1230   {
1231     if (tests::is_mac($target)){
1232       /* Get communication object */
1233       $d= new gosaSupportDaemon(TRUE,0.5);
1234       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1235       return (count($answer) ? TRUE:FALSE);
1236     }
1237     return (FALSE);
1238   }
1242   /*! \brief  Returns a list of all configured principals. 
1243               (Uses the GOsa support daemon instead of the ldap database.)
1244       @return Array  A list containing the names of all configured principals.
1245    */
1246   public function krb5_list_principals($server)
1247   {
1248     $res = array();  
1250     /* Check if the given server is a valid mac address
1251      */
1252     if(!tests::is_mac($server)){
1253       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1254       return($ret);
1255     }
1257     /* Prepare request event 
1258      */ 
1259     $xml_msg = 
1260       "<xml>".
1261       "<header>gosa_krb5_list_principals</header>".
1262       "<source>GOSA</source>".
1263       "<target>".$server."</target>".
1264       "</xml>";
1265     
1266     $tmp = $this->_send($xml_msg,TRUE);
1267     if(isset($tmp['XML']['PRINCIPAL'])){
1268       return($tmp['XML']['PRINCIPAL']);
1269     }else{
1270       return($res);
1271     }
1272   }
1275   /*! \brief  Returns the configuration settings for a given principal name. 
1276               (Uses the GOsa support daemon instead of the ldap database.)
1277       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1278       @return Array  A list containing the names of all configured principals.
1279    */
1280   public function krb5_get_principal($server,$name)
1281   {
1282     $ret = array();
1284     /* Check if the given name is a valid request value 
1285      */
1286     if(!is_string($name) || empty($name)){
1287       trigger_error("The given principal name is not of type string or it is empty.");
1288       return($ret);
1289     }
1291     /* Check if the given server is a valid mac address
1292      */
1293     if(!tests::is_mac($server)){
1294       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1295       return($ret);
1296     }
1298     /* Prepare request event 
1299      */ 
1300     $xml_msg = 
1301       "<xml>".
1302       "<header>gosa_krb5_get_principal</header>".
1303       "<principal>".$name."</principal>".
1304       "<source>GOSA</source>".
1305       "<target>".$server."</target>".
1306       "</xml>";
1308     $res = $this->_send($xml_msg,TRUE);
1309     if(isset($res['XML'])){
1310       return($res['XML']);
1311     }else{
1312       return($ret);
1313     }
1314   }
1317   /*! \brief  Creates a given principal with a set of configuration settings.
1318               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1319               (Uses the GOsa support daemon instead of the ldap database.)
1320       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1321       @return Boolean   TRUE on success else FALSE. 
1322    */
1323   public function krb5_add_principal($server,$name,$values)
1324   {
1325     $ret = FALSE;  
1327     /* Check if the given name is a valid request value 
1328      */
1329     if(!is_string($name) || empty($name)){
1330       trigger_error("The given principal name is not of type string or it is empty.");
1331       return($ret);
1332     }
1333     if(!is_array($values)){
1334       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1335       return($ret);
1336     }
1338     /* Check if the given server is a valid mac address
1339      */
1340     if(!tests::is_mac($server)){
1341       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1342       return($ret);
1343     }
1345     $attrs = "";
1346     foreach($values as $key => $value){
1347       if(empty($key) || is_numeric($key)){
1348         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1349         return($ret);
1350       }
1351       $key = strtolower($key);
1352       if(is_array($value)){
1353         foreach($value as $val){
1354           $attrs.= "<$key>$val</$key>\n";
1355         }
1356       }else{
1357         $attrs.= "<$key>$value</$key>\n";
1358       }
1359     }
1361     /* Prepare request event 
1362      */ 
1363     $xml_msg = 
1364       "<xml>".
1365       "<header>gosa_krb5_create_principal</header>".
1366       "<principal>".$name."</principal>".
1367       $attrs.
1368       "<source>GOSA</source>".
1369       "<target>".$server."</target>".
1370       "</xml>";
1372     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1373   }
1376   function krb5_ramdomize_key($server,$name)  
1377   {
1378     /* Prepare request event 
1379      */ 
1380     $xml_msg = 
1381       "<xml>".
1382       "<header>gosa_krb5_randomize_key</header>".
1383       "<principal>".$name."</principal>".
1384       "<source>GOSA</source>".
1385       "<target>".$server."</target>".
1386       "</xml>";
1388     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1389   }
1390   
1393   /*! \brief  Updates a given principal with a set of configuration settings.
1394               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1395               (Uses the GOsa support daemon instead of the ldap database.)
1396       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1397       @return Boolean   TRUE on success else FALSE. 
1398    */
1399   public function krb5_set_principal($server,$name,$values)
1400   {
1401     $ret = FALSE;  
1403     /* Check if the given name is a valid request value 
1404      */
1405     if(!is_string($name) || empty($name)){
1406       trigger_error("The given principal name is not of type string or it is empty.");
1407       return($ret);
1408     }
1409     if(!is_array($values) || !count($values)){
1410       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1411       return($ret);
1412     }
1414     /* Check if the given server is a valid mac address
1415      */
1416     if(!tests::is_mac($server)){
1417       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1418       return($ret);
1419     }
1421     $attrs = "";
1422     foreach($values as $key => $value){
1423       if(empty($key) || is_numeric($key)){
1424         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1425         return($ret);
1426       }
1427       $key = strtolower($key);
1428       if(is_array($value)){
1429         foreach($value as $val){
1430           $attrs.= "<$key>$val</$key>\n";
1431         }
1432       }else{
1433         $attrs.= "<$key>$value</$key>\n";
1434       }
1435     }
1437     /* Prepare request event 
1438      */ 
1439     $xml_msg = 
1440       "<xml>".
1441       "<header>gosa_krb5_modify_principal</header>".
1442       "<principal>".$name."</principal>".
1443       $attrs.
1444       "<source>GOSA</source>".
1445       "<target>".$server."</target>".
1446       "</xml>";
1448     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1449   }
1452   /*! \brief  Removes the given principal.
1453               (Uses the GOsa support daemon instead of the ldap database.)
1454       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1455       @return Boollean   TRUE on success else FALSE
1456    */
1457   public function krb5_del_principal($server,$name)
1458   {
1459     $ret = FALSE;  
1461     /* Check if the given name is a valid request value 
1462      */
1463     if(!is_string($name) || empty($name)){
1464       trigger_error("The given principal name is not of type string or it is empty.");
1465       return($ret);
1466     }
1468     /* Check if the given server is a valid mac address
1469      */
1470     if(!tests::is_mac($server)){
1471       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1472       return($ret);
1473     }
1475     /* Prepare request event 
1476      */ 
1477     $xml_msg = 
1478       "<xml>".
1479       "<header>gosa_krb5_del_principal</header>".
1480       "<principal>".$name."</principal>".
1481       "<source>GOSA</source>".
1482       "<target>".$server."</target>".
1483       "</xml>";
1484     
1485     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1486   }
1489   /*! \brief  Returns a list of configured password policies.
1490               (Uses the GOsa support daemon instead of the ldap database.)
1491       @return Array A list of all configured password policies.
1492    */
1493   public function krb5_list_policies($server)
1494   {
1495     $res = array();  
1497     /* Check if the given server is a valid mac address
1498      */
1499     if(!tests::is_mac($server)){
1500       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1501       return($ret);
1502     }
1504     /* Prepare request event 
1505      */ 
1506     $xml_msg = 
1507       "<xml>".
1508       "<header>gosa_krb5_list_policies</header>".
1509       "<source>GOSA</source>".
1510       "<target>".$server."</target>".
1511       "</xml>";
1512     
1513     $res = $this->_send($xml_msg,TRUE);
1514     
1515     /* Check if there are results for POLICY 
1516      */
1517     if(isset($res['XML']['POLICY'])){
1518       
1519       /* Ensure that we return an array 
1520        */
1521       $tmp = $res['XML']['POLICY'];
1522       if(!is_array($tmp)){
1523         $tmp = array($tmp);
1524       }
1525       return($tmp);
1526     }else{
1527       return(array());
1528     }
1529   }
1532   /*! \brief  Returns a list of configured password policies.
1533               (Uses the GOsa support daemon instead of the ldap database.)
1534       @return Array The policy settings for the given policy name.
1535    */
1536   public function krb5_get_policy($server,$name)
1537   {
1538     $res = array();  
1540     /* Check if the given name is a valid request value 
1541      */
1542     if(!is_string($name) || empty($name)){
1543       trigger_error("The given policy name is not of type string or it is empty.");
1544       return($ret);
1545     }
1547     /* Check if the given server is a valid mac address
1548      */
1549     if(!tests::is_mac($server)){
1550       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1551       return($ret);
1552     }
1554     /* Prepare request event 
1555      */ 
1556     $xml_msg = 
1557       "<xml>".
1558       "<header>gosa_krb5_get_policy</header>".
1559       "<policy>".$name."</policy>".
1560       "<source>GOSA</source>".
1561       "<target>".$server."</target>".
1562       "</xml>";
1564     /* Possible attributes */
1565     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1566         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1568   
1569     $tmp = $this->_send($xml_msg,TRUE);
1570     if(isset($tmp['XML'])){
1571       foreach($attrs as $attr){
1572         if(isset($tmp['XML'][$attr])){
1573           $ret[$attr] = $tmp['XML'][$attr];
1574         }else{
1575           $ret[$attr] = "";
1576         }
1577       }
1578     }
1579     return($ret);
1580   }
1582   
1583   /*! \brief  Creates a new policy with a given set of configuration settings.
1584               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1585               (Uses the GOsa support daemon instead of the ldap database.)
1586       @pram   String The name of the policy to update.
1587       @pram   Array  The attributes to update
1588       @return Boolean   TRUE on success else FALSE. 
1589    */
1590   public function krb5_add_policy($server,$name,$values)
1591   {
1592     $ret = FALSE;  
1594     /* Check if the given name is a valid request value 
1595      */
1596     if(!is_string($name) || empty($name)){
1597       trigger_error("The given policy name is not of type string or it is empty.");
1598       return($ret);
1599     }
1600     if(!is_array($values) || !count($values)){
1601       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1602       return($ret);
1603     }
1605     /* Check if the given server is a valid mac address
1606      */
1607     if(!tests::is_mac($server)){
1608       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1609       return($ret);
1610     }
1613     /* Transform array into <xml>
1614      */
1615     $attrs = "";
1616     foreach($values as $id => $value){
1617       if(empty($id) || is_numeric($id)){
1618         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1619         return($ret);
1620       }
1621       $id = strtolower($id);
1622       $attrs.= "<$id>$value</$id>\n";
1623     }
1625     /* Prepare request event 
1626      */ 
1627     $xml_msg = 
1628       "<xml>".
1629       "<header>gosa_krb5_create_policy</header>".
1630       "<policy>".$name."</policy>".
1631       $attrs.
1632       "<source>GOSA</source>".
1633       "<target>".$server."</target>".
1634       "</xml>";
1636     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1637   }
1640   /*! \brief  Updates a given policy with a set of configuration settings.
1641               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1642               (Uses the GOsa support daemon instead of the ldap database.)
1643       @pram   String The name of the policy to update.
1644       @return Boolean   TRUE on success else FALSE. 
1645    */
1646   public function krb5_set_policy($server,$name,$values)
1647   {
1648     $ret = FALSE;  
1650     /* Check if the given name is a valid request value 
1651      */
1652     if(!is_string($name) || empty($name)){
1653       trigger_error("The given policy name is not of type string or it is empty.");
1654       return($ret);
1655     }
1656     if(!is_array($values) || !count($values)){
1657       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1658       return($ret);
1659     }
1661     /* Check if the given server is a valid mac address
1662      */
1663     if(!tests::is_mac($server)){
1664       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1665       return($ret);
1666     }
1668     /* Transform array into <xml>
1669      */
1670     $attrs = "";
1671     foreach($values as $id => $value){
1672       if(preg_match("/^policy$/i",$id)) continue;
1673       if(empty($id) || is_numeric($id)){
1674         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1675         return($ret);
1676       }
1677       $id = strtolower($id);
1678       $attrs.= "<$id>$value</$id>\n";
1679     }
1681     /* Prepare request event 
1682      */ 
1683     $xml_msg = 
1684       "<xml>".
1685       "<header>gosa_krb5_modify_policy</header>".
1686       "<policy>".$name."</policy>".
1687       $attrs.
1688       "<source>GOSA</source>".
1689       "<target>".$server."</target>".
1690       "</xml>";
1692     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1693   }
1695   
1696   /*! \brief  Removes the given password policy. 
1697               (Uses the GOsa support daemon instead of the ldap database.)
1698       @return Boolean  TRUE on success else FALSE
1699    */
1700   public function krb5_del_policy($server,$name)
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 policy 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_del_policy</header>".
1723       "<policy>".$name."</policy>".
1724       "<source>GOSA</source>".
1725       "<target>".$server."</target>".
1726       "</xml>";
1727     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1728   }
1731   /*! \brief  Sets the password of for the given principal.
1732               (Uses the GOsa support daemon instead of the ldap database.)
1733       @param  String  The servers mac
1734       @param  String  The principals name
1735       @param  String  $the new password.   
1736       @return Boolean  TRUE on success else FALSE
1737    */
1738   public function krb5_set_password($server,$name,$password)
1739   {
1740     $ret = FALSE;
1742     /* Check if the given server is a valid mac address
1743      */
1744     if(!tests::is_mac($server)){
1745       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1746       return($ret);
1747     }
1749     /* Check if the given name is a valid request value
1750      */
1751     if(!is_string($name) || empty($name)){
1752       trigger_error("The given principal name is not of type string or it is empty.");
1753       return($ret);
1754     }
1756     /* Prepare request event
1757      */
1758     $xml_msg =
1759       "<xml>".
1760       "<header>gosa_krb5_set_password</header>".
1761       "<principal>".$name."</principal>".
1762       "<password>".$password."</password>".
1763       "<source>GOSA</source>".
1764       "<target>".$server."</target>".
1765       "</xml>";
1766     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1767   }
1770   /*! \brief  Returns log file informations for a given mac address 
1771       @param  $mac The mac address to fetch logs for.
1772       @retrun Array A Multidimensional array containing log infos.
1773         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1774         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1775                                install_20080313_144450   ...
1776    */
1777   public function get_log_info_for_mac($mac)
1778   {
1779     $xml_msg = "
1780       <xml>
1781       <header>gosa_show_log_by_mac</header>
1782       <target>GOSA</target>
1783       <source>GOSA</source>
1784       <mac>".$mac."</mac>
1785       </xml>";
1787     $res = $this->_send($xml_msg,TRUE);
1788     $ret = array();
1789     if(isset($res['XML'])){
1791       /* Filter all entry that look like this 
1792           MAC_00_01_6C_9D_B9_FA
1793        */
1794       foreach($res['XML'] as $name => $entry){
1795         if(preg_match("/^MAC/",$name)){
1797           /* Get list of available log files 
1798            */
1799           foreach($entry as $log_date){
1800             $xml_msg2 = "<xml> 
1801               <header>gosa_show_log_files_by_date_and_mac</header> 
1802               <target>GOSA</target> 
1803               <source>GOSA</source>                        
1804               <date>".$log_date."</date> 
1805               <mac>".$mac."</mac> 
1806               </xml>";
1808             $ret[$mac][$log_date] = array();
1809             $res = $this->_send($xml_msg2,TRUE);
1810             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1811             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1812             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1813               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1814             }
1815           }
1816         }
1817       }
1818     }
1819     return($ret);
1820   }
1822   public function get_log_file($mac,$date,$file)
1823   {
1824     $xml_msg ="
1825       <xml> 
1826       <header>gosa_get_log_file_by_date_and_mac</header> 
1827       <target>GOSA</target> 
1828       <source>GOSA</source>
1829       <date>".$date."</date> 
1830       <mac>".$mac."</mac> 
1831       <log_file>".$file."</log_file>
1832       </xml>";
1834     $res = $this->_send($xml_msg,TRUE);
1835     if(isset($res['XML'][strtoupper($file)])){
1836       return(base64_decode($res['XML'][strtoupper($file)]));
1837     }
1838     return("");
1839   }
1845   /*****************
1846    * DAK - Functions 
1847    *****************/
1849   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1850       @param  ...
1851       @return Array   All queued entries.
1852    */
1853   public function DAK_keyring_entries($server)  
1854   {
1855     /* Ensure that we send the event to a valid mac address 
1856      */
1857     if(!is_string($server) || !tests::is_mac($server)){
1858       trigger_error("No valid mac address given '".$server."'.");
1859       return;
1860     }
1862     /* Create query
1863      */
1864     $xml_msg = "<xml> 
1865                   <header>gosa_get_dak_keyring</header> 
1866                   <target>".$server."</target> 
1867                   <source>GOSA</source>
1868                 </xml>";
1869         
1870     $res = $this->_send($xml_msg,TRUE);
1872     /* Check if there are results for POLICY
1873      */
1874     if(isset($res['XML'])){
1875       $ret = array();
1876       foreach($res['XML'] as $key => $entry){
1877         if(preg_match("/^ANSWER/",$key)){
1878           $ret[] = $entry;
1879         }
1880       }
1881       return($ret);
1882     }else{
1883       return(array());
1884     }
1885   }
1888   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1889       @param  String  The servers mac address 
1890       @param  String  The gpg key.
1891       @return Boolean TRUE on success else FALSE 
1892    */
1893   public function DAK_import_key($server,$key)  
1894   {
1895     /* Ensure that we send the event to a valid mac address 
1896      */
1897     if(!is_string($server) || !tests::is_mac($server)){
1898       trigger_error("No valid mac address given '".$server."'.");
1899       return;
1900     }
1902     /* Check if there is some cleanup required before importing the key.
1903         There may be some Header lines like:
1904         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1905      */
1906     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1908       /* Remove header */
1909       $key = preg_replace("/^.*\n\n/sim","",$key);
1910       /* Remove footer */
1911       $key = preg_replace("/-----.*$/sim","",$key);
1912     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1913       
1914       /* Encode key if it is raw.
1915        */
1916       $key = base64_encode($key);
1917     }
1919     /* Create query
1920      */
1921     $xml_msg = "<xml> 
1922                   <header>gosa_import_dak_key</header> 
1923                   <target>".$server."</target> 
1924                   <key>".$key."</key> 
1925                   <source>GOSA</source>
1926                 </xml>";
1927         
1928     $res = $this->_send($xml_msg,TRUE);
1929     return($this->is_error());
1930   }
1933   /*! \brief Removes a key from the keyring on the given server. 
1934       @param  String  The servers mac address 
1935       @param  String  The gpg key uid.
1936       @return Boolean TRUE on success else FALSE 
1937    */
1938   public function DAK_remove_key($server,$key)  
1939   {
1940     /* Ensure that we send the event to a valid mac address 
1941      */
1942     if(!is_string($server) || !tests::is_mac($server)){
1943       trigger_error("No valid mac address given '".$server."'.");
1944       return;
1945     }
1947     /* Create query
1948      */
1949     $xml_msg = "<xml> 
1950                   <header>gosa_remove_dak_key</header> 
1951                   <target>".$server."</target> 
1952                   <keyid>".$key."</keyid> 
1953                   <source>GOSA</source>
1954                 </xml>";
1955        
1956     $res = $this->_send($xml_msg,TRUE);
1957     return($this->is_error());
1958   }
1961 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1962 ?>