Code

Updated ACL management.
[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') {
830           $start_level = 1;
831           $test2 = &$params;
832           while($start_level < $xml_elem['level']) {
833             $test2 = &$test2[$level[$start_level]];
834             $start_level++;
835           }
837           /* Save tag attributes too. 
838               e.g. <tag attr="val">
839            */
840           if(isset($xml_elem['attributes'])){
841             foreach($xml_elem['attributes'] as $name => $value){
842               $test2['ATTRIBUTES'][$name] = $value;
843             }
844           }
846           if(!isset($test2[$xml_elem['tag']])){
847             if(isset($xml_elem['value'])){
848               $test2[$xml_elem['tag']] = $xml_elem['value'];
849             }
850           }else{
851             if(!is_array($test2[$xml_elem['tag']])){
852               $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
853             }
854             $test2[$xml_elem['tag']][] = $xml_elem['value'];
855           }
856         }
857       }
858     }
860     if(!isset($params['XML'])){
861       if (!array_key_exists('XML', $params)){
862         $this->set_error(_("Cannot not parse XML!"));
863       }
864       $params = array("COUNT" => 0);
865     }
867     return($params); 
868   }
871   /*! \brief  Updates an entry with a set of new values, 
872     @param  Integer The ID of the entry, we want to update.
873     @param  Array   The variables to update.   
874     @return Boolean Returns TRUE on success. 
875    */
876   public function update_entries($ids,$data)
877   {
878     $this->reset_error();
879     if(!is_array($ids)){
880       trigger_error("Requires an array as first parameter.");
881       return;
882     }
884     if(!is_array($data)){
885       trigger_error("Requires an array as second parameter.");
886       return;
887     }
889     $attr = "";
890     foreach($data as $key => $value){
891       $key = strtolower($key);
892       if(is_array($value)){
893         foreach($value as $sub_value){
894           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
895         }
896       }else{
897         $attr.= "<$key>".strtolower($value)."</$key>\n";
898       }
899     }
901     $xml_msg = "<xml>
902       <header>gosa_update_status_jobdb_entry</header>
903       <target>GOSA</target>
904       <source>GOSA</source>
905       <where>
906       <clause>
907       <connector>or</connector>";
908     foreach($ids as $id){
909       $xml_msg .= "<phrase>
910         <operator>eq</operator>
911         <id>".$id."</id>
912         </phrase>";
913     }
914     $xml_msg .= "</clause>
915       </where>
916       <update>
917       ".$attr." 
918       </update>
919       </xml>";
921     if($this->connect()){
923       $this->o_sock->write($xml_msg);
924       $str      = trim($this->o_sock->read());
926       /* Check if something went wrong while reading */
927       if($this->o_sock->is_error()){
928         $this->set_error($this->o_sock->get_error());
929         return(FALSE);
930       }
932       $entries = $this->xml_to_array($str);
933       if(isset($entries['XML'])){
934         if(isset($entries['XML']['ERROR_STRING'])) {
935           $this->set_error($entries['XML']['ERROR_STRING']);
936           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
937           return(FALSE);
938         }
939         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
940         return(TRUE);
941       }
942     }
943     return(FALSE);
944   }
947   /*! \brief  Returns the number of currently queued objects.
948       @return Integer  
949    */
950   public function number_of_queued_entries($event_types)
951   {
952     $tags = "";
953     foreach($event_types as $type){
954       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
955     }
956     if(count($event_types) > 1){
957       $tags = "<connector>or</connector>".$tags;
958     }
959     if(count($event_types)){
960       $tags = "<where><clause>".$tags."</clause></where>";
961     }
964     $xml_msg =
965       "<xml>".
966       "<header>gosa_query_jobdb</header>".
967       "<target>GOSA</target>".
968       "<source>GOSA</source>".
969       "<select> count ID</select>".
970       $tags.
971       "</xml>";
973     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
974     $this->connect();
975     if($this->connect()){
976       $this->o_sock->write($xml_msg);
977       $str     = trim($this->o_sock->read());
979       /* Check if something went wrong while reading */
980       if($this->o_sock->is_error()){
981         $this->set_error($this->o_sock->get_error());
982         return(0);
983       }
985       $entries = $this->xml_to_array($str);
986       if(isset($entries['XML'])){
987         return($entries['XML']['COUNT']);
988       }
989     }
990     return(-1);
991   } 
994   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
995   {
996     $xml_message= "";
998     /* Prepare data */
999     foreach ($data as $key => $value){
1000       if(is_array($value)){
1001         foreach($value as $sub_value){
1002           $xml_message.= "<$key>$sub_value</$key>";
1003         }
1004       }else{
1005         $xml_message.= "<$key>$value</$key>";
1006       }
1007     }
1009     /* Multiple targets? */
1010     if (!is_array($to)){
1011       $to_targets= array($to);
1012     } else {
1013       $to_targets= $to;
1014     }
1016     /* Build target strings */
1017     $target ="";
1018     foreach($to_targets as $to){
1019       $target.= "<target>$to</target>";
1020     }
1022     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1023   }
1026   /* Allows simply appending a new DaemonEvent 
1027    */
1028   public function append($event)
1029   {
1030     if(!($event instanceof DaemonEvent)){
1031       return(FALSE);
1032     }
1033   
1034     $this->reset_error();
1036     /* Add to queue if new 
1037      */
1038     if($event->is_new()){
1040       $request_answer = FALSE;
1041       if($event->get_type() == SCHEDULED_EVENT){
1042         $action = $event->get_schedule_action();
1043       }elseif($event->get_type() == TRIGGERED_EVENT){
1044         $action = $event->get_trigger_action();
1045       }else{
1046         trigger_error("Unknown type of queue event given.");
1047         return(FALSE);
1048       }
1050       /* Get event informations, like targets..
1051        */
1052       $targets    = $event->get_targets();
1053       $data       = $event->save();
1055       /* Append an entry for each target 
1056        */
1057       foreach($targets as $target){
1058         $data['macaddress'] = $target;
1059         $this->send_data($action,$target,$data,$request_answer);
1061         if($this->is_error()){
1062           return(FALSE);
1063         }
1064       }
1065       return(TRUE);
1066     }else{
1068       /* Updated edited entry.
1069        */
1070       $id                 = $event->get_id();
1071       $data               = $event->save();
1072       return($this->update_entries(array($id),$data));
1073     }
1075     return(FALSE);
1076   }
1079 /*! \brief  Returns an array containing all queued entries.
1080     @return Array All queued entries as an array.
1081    */
1082   public function _send($data, $answer_expected= FALSE)
1083   {
1084     $this->reset_error();
1085     $ret = array();
1087     if($this->connect()){
1088       $this->o_sock->write($data);
1089       if ($answer_expected){
1090         $str = trim($this->o_sock->read());
1092         /* Check if something went wrong while reading */
1093         if($this->o_sock->is_error()){
1094           $this->set_error($this->o_sock->get_error());
1095           return($ret);
1096         }
1098         $entries = $this->xml_to_array($str);
1099         if(isset($entries['XML']) && is_array($entries['XML'])){
1100           $ret = $entries;
1101           if(isset($entries['XML']['ERROR_STRING'])) {
1102             $this->set_error($entries['XML']['ERROR_STRING']);
1103             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1104           }elseif(isset($entries['XML']['ERROR'])){
1105             $this->set_error($entries['XML']['ERROR']);
1106             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1107           }else{
1108             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1109           }
1110         }
1111       }else{
1112         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1113       }
1114     }
1115     return($ret);
1116   }
1119   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1120   {
1121     $xml_message= "";
1123     /* Get communication object */
1124     $d= new gosaSupportDaemon(TRUE,10);
1126     /* Prepare data */
1127     foreach ($data as $key => $value){
1128       if(is_array($value)){
1129         foreach($value as $sub_val){
1130           $xml_message.= "<$key>$sub_val</$key>";
1131         }
1132       }else{
1133         $xml_message.= "<$key>$value</$key>";
1134       }
1135     }
1137     /* Multiple targets? */
1138     if (!is_array($to)){
1139       $to_targets= array($to);
1140     } else {
1141       $to_targets= $to;
1142     }
1144     /* Build target strings */
1145     $target ="";
1146     foreach($to_targets as $to){
1147       $target.= "<target>$to</target>";
1148     }
1150     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1151   }
1154   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1155       @param  String  $mac  The mac address for which we want to remove all jobs.      
1156    */
1157   function clean_queue_from_mac($mac)
1158   {
1159     global $config;
1161     /* First of all we have to check which jobs are startet 
1162      *  for $mac 
1163      */
1164     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1165     
1166     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1167  
1168     $data = $this->_send($xml_msg,TRUE);
1169     if(is_array($data) && isset($data['XML'])){
1170       $already_aborted = FALSE;
1171       foreach($data['XML']  as $name => $entry){
1172         if(preg_match("/answer[0-9]*/i",$name)){
1173           $entry['STATUS'] = strtoupper($entry['STATUS']);
1174           switch($entry['STATUS']){
1176             case 'PROCESSING' :
1178               /* Send abort event, but only once 
1179                */
1180               if($already_aborted){
1181                 break;
1182               }elseif(class_available("DaemonEvent_faireboot")){
1183                 $already_aborted = TRUE;
1184                 $tmp = new DaemonEvent_faireboot($config);
1185                 $tmp->add_targets(array($mac));
1186                 $tmp->set_type(TRIGGERED_EVENT);
1187                 if(!$this->append($tmp)){
1188                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1189                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1190                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1191                 }else{
1192                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1193                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1194                 }
1195                 ;break;
1196               }else{
1197                 /* Couldn't find abort event, just remove entry */
1198               }
1200             case 'WAITING':
1201             case 'ERROR':
1202             default :
1203             
1204               /* Simply remove entries from queue. 
1205                *  Failed or waiting events, can be removed without any trouble.
1206                */ 
1207               if(!$this->remove_entries(array($entry['ID']))){
1208                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1209               }
1210               ;break;
1211           }
1212     
1213         }
1214       }
1215     }
1216   }
1219   static function ping($target)
1220   {
1221     if (tests::is_mac($target)){
1222       /* Get communication object */
1223       $d= new gosaSupportDaemon(TRUE,0.5);
1224       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1225       return (count($answer) ? TRUE:FALSE);
1226     }
1227     return (FALSE);
1228   }
1232   /*! \brief  Returns a list of all configured principals. 
1233               (Uses the GOsa support daemon instead of the ldap database.)
1234       @return Array  A list containing the names of all configured principals.
1235    */
1236   public function krb5_list_principals($server)
1237   {
1238     $res = array();  
1240     /* Check if the given server is a valid mac address
1241      */
1242     if(!tests::is_mac($server)){
1243       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1244       return($ret);
1245     }
1247     /* Prepare request event 
1248      */ 
1249     $xml_msg = 
1250       "<xml>".
1251       "<header>gosa_krb5_list_principals</header>".
1252       "<source>GOSA</source>".
1253       "<target>".$server."</target>".
1254       "</xml>";
1255     
1256     $tmp = $this->_send($xml_msg,TRUE);
1257     if(isset($tmp['XML']['PRINCIPAL'])){
1258       return($tmp['XML']['PRINCIPAL']);
1259     }else{
1260       return($res);
1261     }
1262   }
1265   /*! \brief  Returns the configuration settings for a given principal name. 
1266               (Uses the GOsa support daemon instead of the ldap database.)
1267       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1268       @return Array  A list containing the names of all configured principals.
1269    */
1270   public function krb5_get_principal($server,$name)
1271   {
1272     $ret = array();
1274     /* Check if the given name is a valid request value 
1275      */
1276     if(!is_string($name) || empty($name)){
1277       trigger_error("The given principal name is not of type string or it is empty.");
1278       return($ret);
1279     }
1281     /* Check if the given server is a valid mac address
1282      */
1283     if(!tests::is_mac($server)){
1284       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1285       return($ret);
1286     }
1288     /* Prepare request event 
1289      */ 
1290     $xml_msg = 
1291       "<xml>".
1292       "<header>gosa_krb5_get_principal</header>".
1293       "<principal>".$name."</principal>".
1294       "<source>GOSA</source>".
1295       "<target>".$server."</target>".
1296       "</xml>";
1298     $res = $this->_send($xml_msg,TRUE);
1299     if(isset($res['XML'])){
1300       return($res['XML']);
1301     }else{
1302       return($ret);
1303     }
1304   }
1307   /*! \brief  Creates a given principal with a set of configuration settings.
1308               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1309               (Uses the GOsa support daemon instead of the ldap database.)
1310       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1311       @return Boolean   TRUE on success else FALSE. 
1312    */
1313   public function krb5_add_principal($server,$name,$values)
1314   {
1315     $ret = FALSE;  
1317     /* Check if the given name is a valid request value 
1318      */
1319     if(!is_string($name) || empty($name)){
1320       trigger_error("The given principal name is not of type string or it is empty.");
1321       return($ret);
1322     }
1323     if(!is_array($values)){
1324       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1325       return($ret);
1326     }
1328     /* Check if the given server is a valid mac address
1329      */
1330     if(!tests::is_mac($server)){
1331       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1332       return($ret);
1333     }
1335     $attrs = "";
1336     foreach($values as $key => $value){
1337       if(empty($key) || is_numeric($key)){
1338         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1339         return($ret);
1340       }
1341       $key = strtolower($key);
1342       if(is_array($value)){
1343         foreach($value as $val){
1344           $attrs.= "<$key>$val</$key>\n";
1345         }
1346       }else{
1347         $attrs.= "<$key>$value</$key>\n";
1348       }
1349     }
1351     /* Prepare request event 
1352      */ 
1353     $xml_msg = 
1354       "<xml>".
1355       "<header>gosa_krb5_create_principal</header>".
1356       "<principal>".$name."</principal>".
1357       $attrs.
1358       "<source>GOSA</source>".
1359       "<target>".$server."</target>".
1360       "</xml>";
1362     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1363   }
1366   function krb5_ramdomize_key($server,$name)  
1367   {
1368     /* Prepare request event 
1369      */ 
1370     $xml_msg = 
1371       "<xml>".
1372       "<header>gosa_krb5_randomize_key</header>".
1373       "<principal>".$name."</principal>".
1374       "<source>GOSA</source>".
1375       "<target>".$server."</target>".
1376       "</xml>";
1378     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1379   }
1380   
1383   /*! \brief  Updates a given principal with a set of configuration settings.
1384               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1385               (Uses the GOsa support daemon instead of the ldap database.)
1386       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1387       @return Boolean   TRUE on success else FALSE. 
1388    */
1389   public function krb5_set_principal($server,$name,$values)
1390   {
1391     $ret = FALSE;  
1393     /* Check if the given name is a valid request value 
1394      */
1395     if(!is_string($name) || empty($name)){
1396       trigger_error("The given principal name is not of type string or it is empty.");
1397       return($ret);
1398     }
1399     if(!is_array($values) || !count($values)){
1400       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1401       return($ret);
1402     }
1404     /* Check if the given server is a valid mac address
1405      */
1406     if(!tests::is_mac($server)){
1407       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1408       return($ret);
1409     }
1411     $attrs = "";
1412     foreach($values as $key => $value){
1413       if(empty($key) || is_numeric($key)){
1414         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1415         return($ret);
1416       }
1417       $key = strtolower($key);
1418       if(is_array($value)){
1419         foreach($value as $val){
1420           $attrs.= "<$key>$val</$key>\n";
1421         }
1422       }else{
1423         $attrs.= "<$key>$value</$key>\n";
1424       }
1425     }
1427     /* Prepare request event 
1428      */ 
1429     $xml_msg = 
1430       "<xml>".
1431       "<header>gosa_krb5_modify_principal</header>".
1432       "<principal>".$name."</principal>".
1433       $attrs.
1434       "<source>GOSA</source>".
1435       "<target>".$server."</target>".
1436       "</xml>";
1438     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1439   }
1442   /*! \brief  Removes the given principal.
1443               (Uses the GOsa support daemon instead of the ldap database.)
1444       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1445       @return Boollean   TRUE on success else FALSE
1446    */
1447   public function krb5_del_principal($server,$name)
1448   {
1449     $ret = FALSE;  
1451     /* Check if the given name is a valid request value 
1452      */
1453     if(!is_string($name) || empty($name)){
1454       trigger_error("The given principal name is not of type string or it is empty.");
1455       return($ret);
1456     }
1458     /* Check if the given server is a valid mac address
1459      */
1460     if(!tests::is_mac($server)){
1461       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1462       return($ret);
1463     }
1465     /* Prepare request event 
1466      */ 
1467     $xml_msg = 
1468       "<xml>".
1469       "<header>gosa_krb5_del_principal</header>".
1470       "<principal>".$name."</principal>".
1471       "<source>GOSA</source>".
1472       "<target>".$server."</target>".
1473       "</xml>";
1474     
1475     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1476   }
1479   /*! \brief  Returns a list of configured password policies.
1480               (Uses the GOsa support daemon instead of the ldap database.)
1481       @return Array A list of all configured password policies.
1482    */
1483   public function krb5_list_policies($server)
1484   {
1485     $res = array();  
1487     /* Check if the given server is a valid mac address
1488      */
1489     if(!tests::is_mac($server)){
1490       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1491       return($ret);
1492     }
1494     /* Prepare request event 
1495      */ 
1496     $xml_msg = 
1497       "<xml>".
1498       "<header>gosa_krb5_list_policies</header>".
1499       "<source>GOSA</source>".
1500       "<target>".$server."</target>".
1501       "</xml>";
1502     
1503     $res = $this->_send($xml_msg,TRUE);
1504     
1505     /* Check if there are results for POLICY 
1506      */
1507     if(isset($res['XML']['POLICY'])){
1508       
1509       /* Ensure that we return an array 
1510        */
1511       $tmp = $res['XML']['POLICY'];
1512       if(!is_array($tmp)){
1513         $tmp = array($tmp);
1514       }
1515       return($tmp);
1516     }else{
1517       return(array());
1518     }
1519   }
1522   /*! \brief  Returns a list of configured password policies.
1523               (Uses the GOsa support daemon instead of the ldap database.)
1524       @return Array The policy settings for the given policy name.
1525    */
1526   public function krb5_get_policy($server,$name)
1527   {
1528     $res = array();  
1530     /* Check if the given name is a valid request value 
1531      */
1532     if(!is_string($name) || empty($name)){
1533       trigger_error("The given policy name is not of type string or it is empty.");
1534       return($ret);
1535     }
1537     /* Check if the given server is a valid mac address
1538      */
1539     if(!tests::is_mac($server)){
1540       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1541       return($ret);
1542     }
1544     /* Prepare request event 
1545      */ 
1546     $xml_msg = 
1547       "<xml>".
1548       "<header>gosa_krb5_get_policy</header>".
1549       "<policy>".$name."</policy>".
1550       "<source>GOSA</source>".
1551       "<target>".$server."</target>".
1552       "</xml>";
1554     /* Possible attributes */
1555     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1556         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1558   
1559     $tmp = $this->_send($xml_msg,TRUE);
1560     if(isset($tmp['XML'])){
1561       foreach($attrs as $attr){
1562         if(isset($tmp['XML'][$attr])){
1563           $ret[$attr] = $tmp['XML'][$attr];
1564         }else{
1565           $ret[$attr] = "";
1566         }
1567       }
1568     }
1569     return($ret);
1570   }
1572   
1573   /*! \brief  Creates a new policy with a given set of configuration settings.
1574               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1575               (Uses the GOsa support daemon instead of the ldap database.)
1576       @pram   String The name of the policy to update.
1577       @pram   Array  The attributes to update
1578       @return Boolean   TRUE on success else FALSE. 
1579    */
1580   public function krb5_add_policy($server,$name,$values)
1581   {
1582     $ret = FALSE;  
1584     /* Check if the given name is a valid request value 
1585      */
1586     if(!is_string($name) || empty($name)){
1587       trigger_error("The given policy name is not of type string or it is empty.");
1588       return($ret);
1589     }
1590     if(!is_array($values) || !count($values)){
1591       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1592       return($ret);
1593     }
1595     /* Check if the given server is a valid mac address
1596      */
1597     if(!tests::is_mac($server)){
1598       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1599       return($ret);
1600     }
1603     /* Transform array into <xml>
1604      */
1605     $attrs = "";
1606     foreach($values as $id => $value){
1607       if(empty($id) || is_numeric($id)){
1608         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1609         return($ret);
1610       }
1611       $id = strtolower($id);
1612       $attrs.= "<$id>$value</$id>\n";
1613     }
1615     /* Prepare request event 
1616      */ 
1617     $xml_msg = 
1618       "<xml>".
1619       "<header>gosa_krb5_create_policy</header>".
1620       "<policy>".$name."</policy>".
1621       $attrs.
1622       "<source>GOSA</source>".
1623       "<target>".$server."</target>".
1624       "</xml>";
1626     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1627   }
1630   /*! \brief  Updates a given policy with a set of configuration settings.
1631               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1632               (Uses the GOsa support daemon instead of the ldap database.)
1633       @pram   String The name of the policy to update.
1634       @return Boolean   TRUE on success else FALSE. 
1635    */
1636   public function krb5_set_policy($server,$name,$values)
1637   {
1638     $ret = FALSE;  
1640     /* Check if the given name is a valid request value 
1641      */
1642     if(!is_string($name) || empty($name)){
1643       trigger_error("The given policy name is not of type string or it is empty.");
1644       return($ret);
1645     }
1646     if(!is_array($values) || !count($values)){
1647       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1648       return($ret);
1649     }
1651     /* Check if the given server is a valid mac address
1652      */
1653     if(!tests::is_mac($server)){
1654       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1655       return($ret);
1656     }
1658     /* Transform array into <xml>
1659      */
1660     $attrs = "";
1661     foreach($values as $id => $value){
1662       if(preg_match("/^policy$/i",$id)) continue;
1663       if(empty($id) || is_numeric($id)){
1664         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1665         return($ret);
1666       }
1667       $id = strtolower($id);
1668       $attrs.= "<$id>$value</$id>\n";
1669     }
1671     /* Prepare request event 
1672      */ 
1673     $xml_msg = 
1674       "<xml>".
1675       "<header>gosa_krb5_modify_policy</header>".
1676       "<policy>".$name."</policy>".
1677       $attrs.
1678       "<source>GOSA</source>".
1679       "<target>".$server."</target>".
1680       "</xml>";
1682     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1683   }
1685   
1686   /*! \brief  Removes the given password policy. 
1687               (Uses the GOsa support daemon instead of the ldap database.)
1688       @return Boolean  TRUE on success else FALSE
1689    */
1690   public function krb5_del_policy($server,$name)
1691   {
1692     $ret = FALSE;
1694     /* Check if the given server is a valid mac address
1695      */
1696     if(!tests::is_mac($server)){
1697       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1698       return($ret);
1699     }
1701     /* Check if the given name is a valid request value 
1702      */
1703     if(!is_string($name) || empty($name)){
1704       trigger_error("The given policy name is not of type string or it is empty.");
1705       return($ret);
1706     }
1708     /* Prepare request event 
1709      */ 
1710     $xml_msg = 
1711       "<xml>".
1712       "<header>gosa_krb5_del_policy</header>".
1713       "<policy>".$name."</policy>".
1714       "<source>GOSA</source>".
1715       "<target>".$server."</target>".
1716       "</xml>";
1717     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1718   }
1721   /*! \brief  Sets the password of for the given principal.
1722               (Uses the GOsa support daemon instead of the ldap database.)
1723       @param  String  The servers mac
1724       @param  String  The principals name
1725       @param  String  $the new password.   
1726       @return Boolean  TRUE on success else FALSE
1727    */
1728   public function krb5_set_password($server,$name,$password)
1729   {
1730     $ret = FALSE;
1732     /* Check if the given server is a valid mac address
1733      */
1734     if(!tests::is_mac($server)){
1735       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1736       return($ret);
1737     }
1739     /* Check if the given name is a valid request value
1740      */
1741     if(!is_string($name) || empty($name)){
1742       trigger_error("The given principal name is not of type string or it is empty.");
1743       return($ret);
1744     }
1746     /* Prepare request event
1747      */
1748     $xml_msg =
1749       "<xml>".
1750       "<header>gosa_krb5_set_password</header>".
1751       "<principal>".$name."</principal>".
1752       "<password>".$password."</password>".
1753       "<source>GOSA</source>".
1754       "<target>".$server."</target>".
1755       "</xml>";
1756     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1757   }
1760   /*! \brief  Returns log file informations for a given mac address 
1761       @param  $mac The mac address to fetch logs for.
1762       @retrun Array A Multidimensional array containing log infos.
1763         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1764         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1765                                install_20080313_144450   ...
1766    */
1767   public function get_log_info_for_mac($mac)
1768   {
1769     $xml_msg = "
1770       <xml>
1771       <header>gosa_show_log_by_mac</header>
1772       <target>GOSA</target>
1773       <source>GOSA</source>
1774       <mac>".$mac."</mac>
1775       </xml>";
1777     $res = $this->_send($xml_msg,TRUE);
1778     $ret = array();
1779     if(isset($res['XML'])){
1781       /* Filter all entry that look like this 
1782           MAC_00_01_6C_9D_B9_FA
1783        */
1784       foreach($res['XML'] as $name => $entry){
1785         if(preg_match("/^MAC/",$name)){
1787           /* Get list of available log files 
1788            */
1789           foreach($entry as $log_date){
1790             $xml_msg2 = "<xml> 
1791               <header>gosa_show_log_files_by_date_and_mac</header> 
1792               <target>GOSA</target> 
1793               <source>GOSA</source>                        
1794               <date>".$log_date."</date> 
1795               <mac>".$mac."</mac> 
1796               </xml>";
1798             $ret[$mac][$log_date] = array();
1799             $res = $this->_send($xml_msg2,TRUE);
1800             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1801             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1802             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1803               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1804             }
1805           }
1806         }
1807       }
1808     }
1809     return($ret);
1810   }
1812   public function get_log_file($mac,$date,$file)
1813   {
1814     $xml_msg ="
1815       <xml> 
1816       <header>gosa_get_log_file_by_date_and_mac</header> 
1817       <target>GOSA</target> 
1818       <source>GOSA</source>
1819       <date>".$date."</date> 
1820       <mac>".$mac."</mac> 
1821       <log_file>".$file."</log_file>
1822       </xml>";
1824     $res = $this->_send($xml_msg,TRUE);
1825     if(isset($res['XML'][strtoupper($file)])){
1826       return(base64_decode($res['XML'][strtoupper($file)]));
1827     }
1828     return("");
1829   }
1835   /*****************
1836    * DAK - Functions 
1837    *****************/
1839   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1840       @param  ...
1841       @return Array   All queued entries.
1842    */
1843   public function DAK_keyring_entries($server)  
1844   {
1845     /* Ensure that we send the event to a valid mac address 
1846      */
1847     if(!is_string($server) || !tests::is_mac($server)){
1848       trigger_error("No valid mac address given '".$server."'.");
1849       return;
1850     }
1852     /* Create query
1853      */
1854     $xml_msg = "<xml> 
1855                   <header>gosa_get_dak_keyring</header> 
1856                   <target>".$server."</target> 
1857                   <source>GOSA</source>
1858                 </xml>";
1859         
1860     $res = $this->_send($xml_msg,TRUE);
1862     /* Check if there are results for POLICY
1863      */
1864     if(isset($res['XML'])){
1865       $ret = array();
1866       foreach($res['XML'] as $key => $entry){
1867         if(preg_match("/^ANSWER/",$key)){
1868           $ret[] = $entry;
1869         }
1870       }
1871       return($ret);
1872     }else{
1873       return(array());
1874     }
1875   }
1878   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1879       @param  String  The servers mac address 
1880       @param  String  The gpg key.
1881       @return Boolean TRUE on success else FALSE 
1882    */
1883   public function DAK_import_key($server,$key)  
1884   {
1885     /* Ensure that we send the event to a valid mac address 
1886      */
1887     if(!is_string($server) || !tests::is_mac($server)){
1888       trigger_error("No valid mac address given '".$server."'.");
1889       return;
1890     }
1892     /* Check if there is some cleanup required before importing the key.
1893         There may be some Header lines like:
1894         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1895      */
1896     if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1898       /* Remove header */
1899       $key = preg_replace("/^.*\n\n/sim","",$key);
1900       /* Remove footer */
1901       $key = preg_replace("/-----.*$/sim","",$key);
1902     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1903       
1904       /* Encode key if it is raw.
1905        */
1906       $key = base64_encode($key);
1907     }
1909     /* Create query
1910      */
1911     $xml_msg = "<xml> 
1912                   <header>gosa_import_dak_key</header> 
1913                   <target>".$server."</target> 
1914                   <key>".$key."</key> 
1915                   <source>GOSA</source>
1916                 </xml>";
1917         
1918     $res = $this->_send($xml_msg,TRUE);
1919     return($this->is_error());
1920   }
1923   /*! \brief Removes a key from the keyring on the given server. 
1924       @param  String  The servers mac address 
1925       @param  String  The gpg key uid.
1926       @return Boolean TRUE on success else FALSE 
1927    */
1928   public function DAK_remove_key($server,$key)  
1929   {
1930     /* Ensure that we send the event to a valid mac address 
1931      */
1932     if(!is_string($server) || !tests::is_mac($server)){
1933       trigger_error("No valid mac address given '".$server."'.");
1934       return;
1935     }
1937     /* Create query
1938      */
1939     $xml_msg = "<xml> 
1940                   <header>gosa_remove_dak_key</header> 
1941                   <target>".$server."</target> 
1942                   <keyid>".$key."</keyid> 
1943                   <source>GOSA</source>
1944                 </xml>";
1945        
1946     $res = $this->_send($xml_msg,TRUE);
1947     return($this->is_error());
1948   }
1951 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1952 ?>