Code

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