Code

Fix encoding of base
[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;
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=null)
56   {
57     #FIXME: bad idea about referencing global variables from within classes
58     global $config;
59     if(!isset($config) || !$config){
60       $config = session::global_get('config');
61     }
63     // Detect timeout 
64     if($timeout == null){
65       $timeout = $config->get_cfg_value("gosaSupportTimeout",15);
66     }
68     /* This should only be the case if we call this from setup.
69         __autoload() 
70      */
71     if(!is_object($config)) { return; }
73     # load from config, store statically
74     if ($config->get_cfg_value("gosaSupportURI") != ""){
76       if ($this->s_host == ""){
77         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
78         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosaSupportURI"));
79         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosaSupportURI"));
80       }
81       $this->is_configured = TRUE;
82       $this->f_timeout = $timeout;
83       if($connect){
84         $this->connect();
85       }
86     }
87   }
90   public function is_configured()
91   {
92     return($this->is_configured);
93   }
96   /*! \brief  Establish daemon connection. 
97     @return boolean Returns true if the connection was succesfully established. 
98    */
99   public function connect()
100   {
101     if(!empty($this->s_host) && !empty($this->i_port)){
102       $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
103       if($this->o_sock->connected()){ 
104         $this->o_sock->setEncryptionKey($this->s_encryption_key); 
105         $this->is_connected = TRUE;
106       }else{
107         $this->set_error($this->o_sock->get_error());
108         $this->disconnect();
109         new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
110       }
111     }else{
112       $this->set_error(msgPool::cmdnotfound("gosaSupportURI",_("GOsa support daemon")));
113     }
114     return($this->is_connected);
115   }
117   
118   /*! \brief  Returns TRUE whether we are connected or not 
119       @return BOOLEAN  Returns TRUE when connected else FALSE
120    */
121   public function is_connected()
122   {
123     return($this->is_connected);
124   }
125  
127   /*! \brief  */
128   public function get_hosts_with_module($mod)
129   {
130     $data = array("module_name" => $mod);
131     $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
132     $hosts = array();
133     if(isset($res['XML'][0])){
134       foreach($res['XML'][0] as $name => $data){
135         if(preg_match("/^ANSWER[0-9]*$/",$name)){
136           if(isset($data[0]['MAC'][0]['VALUE']) && $data[0]['MAC'][0]['VALUE'] != ""){
137             $hosts[] = $data[0]['MAC'][0]['VALUE'];
138           } elseif(isset($data[0]['IP'][0]['VALUE']) && $data[0]['IP'][0]['VALUE'] != "") {
139             $hosts[] = $data[0]['IP'][0]['VALUE'];
140           }
141         }
142       }
143     }
145     if(count($hosts) == 0){
146       @DEBUG(DEBUG_SI, __LINE__, "<b>".__CLASS__."::".__FUNCTION__."</b>" , 
147         __FILE__, "<font color='red'><i>Found: 0</i></font>", $info=$mod);
148     }else{
149       @DEBUG(DEBUG_SI, __LINE__, "<b>".__CLASS__."::".__FUNCTION__."</b>" , 
150         __FILE__, "<i>Found: ".count($hosts)."</i>", $info=$mod);
151     }
153     return($hosts);
154   }
157   /*! \brief  Disconnect from gosa daemon.
158    */
159   public function disconnect()
160   {
161     $this->o_sock->close();
162     $this->is_connected = FALSE;
163   }
166   /*! \brief  Sets an error message, which can be returned with get_error().
167     @param  string  The Error message,
168    */
169   private function set_error($str)
170   {
171     /******
172       Debug handling
173      ******/
174     $debug = debug_backtrace();
175     $file = __FILE__;
176     $function = __FUNCTION__;
177     $line = __LINE__;
178     $class = __CLASS__;
179     foreach($debug as $info){
180       if(!in_array($info['function'],array("send_data","_send","set_error","connect"))){
181         $file = $info['file'];
182         $line = $info['line'];
183         $class = get_class($this);
184         $function = $info['function'];
185         break;
186       }
187     }
188     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
190     /******
191       Set error string.
192      ******/
193   
194     $this->b_error = TRUE;
195     $this->s_error = $str;
196   }
199   /*! \brief  Sets an error message, which can be returned with get_error().
200     @param  string  The Error message,
201    */
202   private function reset_error()
203   {
204     $this->b_error = FALSE;
205     $this->s_error = "";
206   }
209   /*! \brief  Checks if an error occured.
210     @return boolean returns TRUE or FALSE, whether there is an error or not.
211    */
212   public function is_error()
213   {
214     return($this->b_error);
215   }
218   /*! \brief  Returns the last error. 
219     @return Returns the last error.
220    */
221   public function get_error()
222   {
223     $str = $this->s_error;
224     $ret = "";
225     if(is_string($str)){
226       $ret = $str;
227     }else{
228       foreach($str as $msg){
229         $ret .= $msg." ";
230       }
231     }
232     $ret = str_replace(" ","&nbsp;",$ret);
233     return($ret);
234   }
237   public function FAI_get_kernels($release)
238   {
239     $xml_msg = 
240       "<xml>".
241       "<header>gosa_get_available_kernel</header>".
242       "<source>GOSA</source>".
243       "<target>GOSA</target>".
244       "<fai_release>".$release."</fai_release>".
245       "</xml>";
247     $ret = array();
248     if($this->connect()){
249       $entries = $this->_send($xml_msg,TRUE);
251       /* Check if returned values represent a valid answer */
252       if(isset($entries['XML']) && is_array($entries['XML'])){
253         if(isset($entries['XML'])){
254           $ret = $entries['XML'];
255           foreach($ret as $key => $entry){
256             if(!preg_match("/^answer/i",$key)){
257               unset($ret[$key]);
258             }
259           }
260         }
261       }
262     }
263     return($ret);
264   }
267   public function FAI_get_package_sections($release)
268   {
269     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
270       "<select>distinct section</select>".
271       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
273     $ret = array();
274     if($this->connect()){
275       $entries = $this->_send($xml_msg,TRUE);
276       if(isset($entries['XML']) && is_array($entries['XML'])){
278         /* Unset header tags */
279         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
280           if(isset($entries['XML'][$type])){
281             unset($entries['XML'][$type]);
282           }
283         }
284         $ret = $entries['XML'];
285       }
286     }
287     return($ret);
288   }
291   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
292   {
293     $ret = array();
295     /* Check Parameter */
296     if(!is_array($attrs) || !count($attrs)){
297       trigger_error("Second parameter must be an array. With at least one attribute name.");
298       return($ret);
299     }
301     /* Check Parameter */
302     if(!is_array($package)){
303       trigger_error("Third parameter must be an array. With at least one attribute name.");
304       return($ret);
305     }
307     /* Create list of attributes to fetch */
308     $attr = ""; 
309     foreach($attrs as $at){
310       $attr.= "<select>".$at."</select>";
311     }
313     /* If no package is given, search for all */
314     if(!count($package)) $package = array("%");
316     /* Create limit tag */
317     if($from == -1){
318       $limit =""; 
319     }else{
320       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
321     }
323     /* Create list of attributes to fetch */
324     $pkgs = ""; 
325     foreach($package as $pkg){
326       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
327     }
329     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
330       $attr.
331       "<where>
332       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
333       <clause><connector>OR</connector>
334       ".$pkgs."
335       </clause>
336       </where>".
337       $limit.
338       "</xml>";
340     if($this->connect()){
341       $entries = $this->_send($xml_msg,TRUE);
342       if(isset($entries['XML']) && is_array($entries['XML'])){
344         /* Check if returned values represent a valid answer */
345         if(isset($entries['XML'])){
347           /* Unset header tags */
348           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
349             if(isset($entries['XML'][$type])){
350               unset($entries['XML'][$type]);
351             }
352           }
353           $ret = $entries['XML'];
354         }
355       }
356     }
357     return($ret);
359     
360   }
363   public function FAI_get_server($name = "")
364   {
366     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
367     $ret = array();
368     if($this->connect()){
370       /* Check if returned values represent a valid answer */
371       $entries = $this->_send($xml_msg,TRUE);
372       if(isset($entries['XML']) && is_array($entries['XML'])){
374         /* Unset header tags */
375         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
376           if(isset($entries['XML'][$type])){
377             unset($entries['XML'][$type]);
378           }
379         }
380         $ret = $entries['XML']; 
381       }
382     }
383     return($ret);
384   }
387   public function FAI_get_classes($name)
388   {
389     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
390                   "<where><clause><phrase><fai_release>".$name."</fai_release></phrase></clause></where></xml>";;
391     $ret = array();
392     if($this->connect()){
394       $entries = $this->_send($xml_msg,TRUE);
395       if(isset($entries['XML']) && is_array($entries['XML'])){
397         /* Unset header tags */
398         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
399           if(isset($entries['XML'][$type])){
400             unset($entries['XML'][$type]);
401           }
402         }
403         $ret = $entries['XML']; 
404       }
405     }
406     return($ret);
407   }
410   /*! \brief  Returns an array containing all queued entries.
411     @return Array All queued entries as an array.
412    */
413   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
414   {
415     $ret = array();
417     $tags = "";
418     foreach($event_types as $type){
419       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
420     }
421     if(count($event_types) > 1){
422       $tags = "<connector>or</connector>".$tags;
423     }
424     if(count($event_types)){
425       $tags = "<where><clause>".$tags."</clause></where>";
426     }
428     $xml_msg = 
429       "<xml>
430       <header>gosa_query_jobdb</header>
431       <target>GOSA</target>
432       <source>GOSA</source>
433       ".$tags."
435       <orderby>".$sort."</orderby>";
436     if($from != -1 && $to != -1){
437       $xml_msg.= "
438         <limit>
439         <from>".$from."</from>
440         <to>".$to."</to>
441         </limit>";
442     }
443     $xml_msg.= "
444       </xml>";
446     if($this->connect()){
447       $entries = $this->_send($xml_msg,TRUE);
448       if(isset($entries['XML']) && is_array($entries['XML'])){
450         /* Unset header tags */
451         foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
452           unset($entries['XML'][$type]);
453         }
454         $ret = $entries['XML']; 
455       }
456     }
457     return($ret);
458   }
461   /*! \brief  Checks if the given ids are used queue ids.
462     @param  Array   The ids we want to check..
463     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
464    */
465   public function ids_exist($ids)
466   {
467     if(!is_array($ids)){
468       trigger_error("Requires an array as parameter.");
469       return;
470     }
472     $ret = array();
474     $xml_msg = "<xml>
475       <header>gosa_query_jobdb</header>
476       <target>GOSA</target>
477       <source>GOSA</source>
478       <where>
479       <clause>
480       <connector>or</connector>";
481     foreach($ids as $id){
482       $xml_msg .= "<phrase>
483         <operator>eq</operator>
484         <id>".$id."</id>
485         </phrase>";
486     }
487     $xml_msg .= "</clause>
488       </where>
489       </xml>";
491     if($this->connect()){
492       $entries = $this->_send($xml_msg,TRUE);
493       if(isset($entries['XML']) && is_array($entries['XML'])){
494         foreach($entries['XML'] as $entry){
495           if(is_array($entry) && array_key_exists("ID",$entry)){
496             $ret[] = $entry['ID'];
497           }
498         }
499       }
500     }
501     return($ret);
502   }
505   /*! \brief  Returns an entry containing all requested ids.
506     @param  Array   The IDs of the entries we want to return.
507     @return Array   Of the requested entries. 
508    */
509   public function get_entries_by_mac($macs)
510   {
511     if(!is_array($macs)){
512       trigger_error("Requires an array as parameter.");
513       return;
514     }
516     $ret = array();
518     $xml_msg = "<xml>
519       <header>gosa_query_jobdb</header>
520       <target>GOSA</target>
521       <source>GOSA</source>
522       <where>
523       <clause>
524       <connector>or</connector>";
525     foreach($macs as $mac){
526       $xml_msg .= "<phrase>
527         <operator>eq</operator>
528         <macaddress>".$mac."</macaddress>
529         </phrase>";
530     }
531     $xml_msg .= "</clause>
532       </where>
533       </xml>";
535     if($this->connect()){
536       $entries = $this->_send($xml_msg,TRUE);
537       if(isset($entries['XML'])){
538         foreach($entries['XML'] as $name => $entry){
539           if(preg_match("/^ANSWER[0-9]*$/",$name)){
540             $ret[$name] = $entry;
541           }
542         }
543       }
544     }
545     return($ret);
546   }
549   /*! \brief  Returns an entry containing all requested ids.
550     @param  Array   The IDs of the entries we want to return.
551     @return Array   Of the requested entries. 
552    */
553   public function get_entries_by_id($ids)
554   {
555     if(!is_array($ids)){
556       trigger_error("Requires an array as parameter.");
557       return;
558     }
560     $ret = array();
562     $xml_msg = "<xml>
563       <header>gosa_query_jobdb</header>
564       <target>GOSA</target>
565       <source>GOSA</source>
566       <where>
567       <clause>
568       <connector>or</connector>";
569     foreach($ids as $id){
570       $xml_msg .= "<phrase>
571         <operator>eq</operator>
572         <id>".$id."</id>
573         </phrase>";
574     }
575     $xml_msg .= "</clause>
576       </where>
577       </xml>";
579     if($this->connect()){
580       $entries = $this->_send($xml_msg,TRUE);
581       if(isset($entries['XML'])){
582         foreach($entries['XML'] as $name => $entry){
583           if(preg_match("/^ANSWER[0-9]*$/",$name)){
584             $ret[$name] = $entry;
585           }
586         }
587       }
588     }
589     return($ret);
590   }
593   /*! \brief  Checks if the given id is in use.
594     @param  Integer The ID of the entry.
595     @return Boolean TRUE if entry exists. 
596    */
597   public function id_exists($id)
598   {
599     if(!is_numeric($id)){
600       trigger_error("Requires an integer as parameter.");
601       return;
602     }
605     $xml_msg = "<xml>
606       <header>gosa_query_jobdb</header>
607       <target>GOSA</target>
608       <source>GOSA</source>
609       <where>
610       <clause>
611       <phrase>
612       <operator>eq</operator>
613       <id>".$id."</id>
614       </phrase>
615       </clause>
616       </where>
617       </xml>";
619     if($this->connect()){
620       $entries = $this->_send($xml_msg,TRUE);
621       if( isset($entries['XML']['HEADER']) && 
622           $entries['XML']['HEADER']=="answer" && 
623           isset($entries['XML']['ANSWER1'])){
624         return(TRUE);
625       }
626     }
627     return(FALSE);
628   }
631   /*! \brief  Returns an entry from the gosaSupportQueue
632     @param  Integer The ID of the entry we want to return.
633     @return Array   Of the requested entry. 
634    */
635   public function get_entry_by_id($id)
636   {
637     if(!is_numeric($id)){
638       trigger_error("Requires an integer as parameter.");
639       return;
640     }
641   
642     $ret = array();
643     $xml_msg = "<xml>
644       <header>gosa_query_jobdb</header>
645       <target>GOSA</target>
646       <source>GOSA</source>
647       <where>
648       <clause>
649       <phrase>
650       <operator>eq</operator>
651       <id>".$id."</id>
652       </phrase>
653       </clause>
654       </where>
655       </xml>";
656     if($this->connect()){
657       $entries = $this->_send($xml_msg,TRUE);
658       if( isset($entries['XML']['HEADER']) &&
659           $entries['XML']['HEADER']=="answer" &&
660           isset($entries['XML']['ANSWER1'])){
661         $ret = $entries['XML']['ANSWER1'];
662       }
663     }
664     return($ret);
665   }
668   /*! \brief  Removes a set of entries from the GOsa support queue. 
669     @param  Array The IDs to remove.
670     @return Boolean True on success.
671    */
672   public function remove_entries($ids)
673   {
674     if(!is_array($ids)){
675       trigger_error("Requires an array as parameter.");
676       return;
677     }
680     $ret = array();
682     $xml_msg = "<xml>
683       <header>gosa_delete_jobdb_entry</header>
684       <target>GOSA</target>
685       <source>GOSA</source>
686       <where>
687       <clause>
688       <connector>or</connector>";
689     foreach($ids as $id){
690       $xml_msg .= "<phrase>
691         <operator>eq</operator>
692         <id>".$id."</id>
693         </phrase>";
694     }
695     $xml_msg .= "</clause>
696       </where>
697       </xml>";
699     if($this->connect()){
700       $entries = $this->_send($xml_msg,TRUE);
701       if(isset($entries['XML']) || isset($entries['COUNT'])){
702         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
703         return(TRUE);
704       }else{
705         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
706       }
707     }
708     return(FALSE);
709   }
713   /*! \brief  Removes an entry from the GOsa support queue. 
714     @param  Integer The ID of the entry we want to remove.
715     @return Boolean True on success.
716    */
717   public function remove_entry($id)
718   {
719     return($this->remove_entries(array($id)));
720   }
723   /*! \brief  Parses the given xml string into an array 
724     @param  String XML string  
725     @return Array Returns an array containing the xml structure. 
726    */
727   private function xml_to_array($xml,$alternative_method = FALSE)
728   {
729     $params = array();
730     $level = array();
731     $parser  = xml_parser_create_ns();
732     xml_parse_into_struct($parser, $xml, $vals, $index);
734     $err_id = xml_get_error_code($parser);
735     if($err_id){
736       xml_parser_free($parser);
737     }else{
738       xml_parser_free($parser);
740       if($this->use_alternative_xml_parse_method) {
741         $params = $this->build_xml_array($vals);
742       } else {
744         foreach ($vals as $xml_elem) {
745           if ($xml_elem['type'] == 'open') {
746             if (array_key_exists('attributes',$xml_elem)) {
747               list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
748             } else {
749               $level[$xml_elem['level']] = $xml_elem['tag'];
750             }
751           }
752           if ($xml_elem['type'] == 'complete') {
754             $start_level = 1;
755             $test2 = &$params;
756             while($start_level < $xml_elem['level']) {
757               $test2 = &$test2[$level[$start_level]];
758               $start_level++;
759             }
761             /* Save tag attributes too. 
762                e.g. <tag attr="val">
763              */
764             if(isset($xml_elem['attributes'])){
765               foreach($xml_elem['attributes'] as $name => $value){
766                 $test2['ATTRIBUTES'][$name] = $value;
767               }
768             }
770             if(!isset($test2[$xml_elem['tag']])){
771               if(isset($xml_elem['value'])){
772                 $test2[$xml_elem['tag']] = $xml_elem['value'];
773               }
774             }else{
775               if(!is_array($test2[$xml_elem['tag']])){
776                 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
777               }
778               $test2[$xml_elem['tag']][] = $xml_elem['value'];
779             }
780           }
781         }
782       }
783     }
785     if(!isset($params['XML'])){
786       if (!array_key_exists('XML', $params)){
787         $this->set_error(_("Cannot not parse XML!"));
788       }
789       $params = array("COUNT" => 0);
790     }
792     return($params); 
793   }
796   function build_xml_array(&$vals)
797   {
798     $array = array();
799     while(count($vals)){
800       $key = key($vals);
801       $val = $vals[$key];
802       unset($vals[$key]);
803       if($val['type'] == "close"){
804         return($array);
805       }elseif($val['type']=="open"){
806         $array[$val['tag']][] = $this->build_xml_array($vals);
807       }elseif($val['type'] != "cdata"){
808         $data = array("VALUE" => "","ATTRIBUTES" => "");
809         foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
810           if(isset($val[$name])){
811             $data[$attr] = $val[$name];
812           }
813         }
814         $array[$val['tag']][] = $data;
815       }else{
816 #print_a($val);
817       }
818     }
819     return($array);
820   }
827   /*! \brief  Updates an entry with a set of new values, 
828     @param  Integer The ID of the entry, we want to update.
829     @param  Array   The variables to update.   
830     @return Boolean Returns TRUE on success. 
831    */
832   public function update_entries($ids,$data)
833   {
834     if(!is_array($ids)){
835       trigger_error("Requires an array as first parameter.");
836       return;
837     }
839     if(!is_array($data)){
840       trigger_error("Requires an array as second parameter.");
841       return;
842     }
844     $attr = "";
845     foreach($data as $key => $value){
846       $key = strtolower($key);
847       if(is_array($value)){
848         foreach($value as $sub_value){
849           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
850         }
851       }else{
852         $attr.= "<$key>".strtolower($value)."</$key>\n";
853       }
854     }
856     $xml_msg = "<xml>
857       <header>gosa_update_status_jobdb_entry</header>
858       <target>GOSA</target>
859       <source>GOSA</source>
860       <where>
861       <clause>
862       <connector>or</connector>";
863     foreach($ids as $id){
864       $xml_msg .= "<phrase>
865         <operator>eq</operator>
866         <id>".$id."</id>
867         </phrase>";
868     }
869     $xml_msg .= "</clause>
870       </where>
871       <update>
872       ".$attr." 
873       </update>
874       </xml>";
876     if($this->connect()){
877       $entries = $this->_send($xml_msg,TRUE);
878       if(isset($entries['XML'])){
879         if(isset($entries['XML']['ERROR_STRING'])) {
880           $this->set_error($entries['XML']['ERROR_STRING']);
881           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
882           return(FALSE);
883         }
884         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
885         return(TRUE);
886       }
887     }
888     return(FALSE);
889   }
892   /*! \brief  Returns the number of currently queued objects.
893       @return Integer  
894    */
895   public function number_of_queued_entries($event_types)
896   {
897     $tags = "";
898     foreach($event_types as $type){
899       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
900     }
901     if(count($event_types) > 1){
902       $tags = "<connector>or</connector>".$tags;
903     }
904     if(count($event_types)){
905       $tags = "<where><clause>".$tags."</clause></where>";
906     }
909     $xml_msg =
910       "<xml>".
911       "<header>gosa_query_jobdb</header>".
912       "<target>GOSA</target>".
913       "<source>GOSA</source>".
914       "<select> count ID</select>".
915       $tags.
916       "</xml>";
918     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
919     $this->connect();
920     if($this->connect()){
921       $entries = $this->_send($xml_msg,TRUE);
922       if($this->o_sock->is_error()){
923         $this->set_error($this->o_sock->get_error());
924         return(0);
925       }
926       if(isset($entries['XML'])){
927         return($entries['XML']['COUNT']);
928       }
929     }
930     return(-1);
931   } 
934   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
935   {
936     $xml_message= "";
938     /* Prepare data */
939     foreach ($data as $key => $value){
940       if(is_array($value)){
941         foreach($value as $sub_value){
942           $xml_message.= "<$key>$sub_value</$key>";
943         }
944       }else{
945         $xml_message.= "<$key>$value</$key>";
946       }
947     }
949     /* Multiple targets? */
950     if (!is_array($to)){
951       $to_targets= array($to);
952     } else {
953       $to_targets= $to;
954     }
956     /* Build target strings */
957     $target ="";
958     foreach($to_targets as $to){
959       $target.= "<target>$to</target>";
960     }
962     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
963   }
966   /* Allows simply appending a new DaemonEvent 
967    */
968   public function append($event, $skip_add_mac = FALSE)
969   {
970     if(!($event instanceof DaemonEvent)){
971       return(FALSE);
972     }
973   
975     /* Add to queue if new 
976      */
977     if($event->is_new()){
979       $request_answer = FALSE;
980       if($event->get_type() == SCHEDULED_EVENT){
981         $action = $event->get_schedule_action();
982       }elseif($event->get_type() == TRIGGERED_EVENT){
983         $action = $event->get_trigger_action();
984       }else{
985         trigger_error("Unknown type of queue event given.");
986         return(FALSE);
987       }
989       /* Get event informations, like targets..
990        */
991       $targets    = $event->get_targets();
992       $data       = $event->save();
994       /* Append an entry for each target 
995        */
996       foreach($targets as $target){
997         if(!$skip_add_mac){
998           $data['macaddress'] = $target;
999         }
1000         $this->send_data($action,$target,$data,$request_answer);
1002         if($this->is_error()){
1003           return(FALSE);
1004         }
1005       }
1006       return(TRUE);
1007     }else{
1009       /* Updated edited entry.
1010        */
1011       $id                 = $event->get_id();
1012       $data               = $event->save();
1013       return($this->update_entries(array($id),$data));
1014     }
1016     return(FALSE);
1017   }
1020   /*! \brief  Returns an array containing all queued entries.
1021     @return Array All queued entries as an array.
1022    */
1023   public function _send($data, $answer_expected= FALSE)
1024   {
1026     $ret = array();
1027     if(!$this->connect()){
1028       return($ret);
1029     }
1030   
1031     $this->reset_error();
1033     /******
1034       Debug handling
1035      ******/
1036     $debug = debug_backtrace();
1037     $file = __FILE__;
1038     $function = __FUNCTION__;
1039     $line = __LINE__;
1040     $class = __CLASS__;
1041     foreach($debug as $info){
1042       if(!in_array($info['function'],array("send_data","_send"))){
1043         $file = $info['file'];
1044         $line = $info['line'];
1045         $class = get_class($this);
1046         $function = $info['function'];
1047         break;
1048       }
1049     }
1050     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1053     /*******
1054       Start sending data 
1055      *******/
1056     if($this->connect()){
1057       $this->o_sock->write($data);
1058       if ($answer_expected){
1059         $str = trim($this->o_sock->read());
1061         /* Check if something went wrong while reading */
1062         if($this->o_sock->is_error()){
1063           $this->set_error($this->o_sock->get_error());
1064           return($ret);
1065         }
1067         $entries = $this->xml_to_array($str);
1068         if(isset($entries['XML']) && is_array($entries['XML'])){
1069           $ret = $entries;
1070           if($this->use_alternative_xml_parse_method) {
1072             // --------- Seems broken, check for 'ERROR' but using 'ERROR_STRING'
1073             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1074               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1075               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1076                   array($data=>$data),"FAILED ".$this->get_error());
1077             // ---------
1078       
1079           
1080             }elseif(isset($entries['XML'][0]['ERROR'][0]['VALUE'])){
1081               $this->set_error($entries['XML'][0]['ERROR'][0]['VALUE']);
1082               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1083                   array($data=>$data),"FAILED ".$this->get_error());
1084             }
1085           }else{
1086             if(isset($entries['XML']['ERROR_STRING'])) {
1087               $this->set_error($entries['XML']['ERROR_STRING']);
1088               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1089                   array($data=>$data),"FAILED ".$this->get_error());
1090             }elseif(isset($entries['XML']['ERROR'])){
1091               $this->set_error($entries['XML']['ERROR']);
1092               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1093                   array($data=>$data),"FAILED ".$this->get_error());
1094             }
1095           }
1096           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1097               array($data=>$data),"SUCCESS");
1098         }
1099       }else{
1100         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1101             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1102       }
1103     }
1104     return($ret);
1105   }
1108   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1109   {
1110     $xml_message= "";
1112     /* Get communication object */
1113     $d= new gosaSupportDaemon(TRUE,10);
1115     /* Prepare data */
1116     foreach ($data as $key => $value){
1117       if(is_array($value)){
1118         foreach($value as $sub_val){
1119           $xml_message.= "<$key>$sub_val</$key>";
1120         }
1121       }else{
1122         $xml_message.= "<$key>$value</$key>";
1123       }
1124     }
1126     /* Multiple targets? */
1127     if (!is_array($to)){
1128       $to_targets= array($to);
1129     } else {
1130       $to_targets= $to;
1131     }
1133     /* Build target strings */
1134     $target ="";
1135     foreach($to_targets as $to){
1136       $target.= "<target>$to</target>";
1137     }
1139     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1140   }
1143   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1144       @param  String  $mac  The mac address for which we want to remove all jobs.      
1145    */
1146   function clean_queue_from_mac($mac)
1147   {
1148     global $config;
1149     if(!isset($config) || !$config){
1150       $config = session::global_get('config');
1151     }
1153     /* First of all we have to check which jobs are startet 
1154      *  for $mac 
1155      */
1156     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1157     
1158     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1159  
1160     $data = $this->_send($xml_msg,TRUE);
1161     if(is_array($data) && isset($data['XML'])){
1162       $already_aborted = FALSE;
1163       foreach($data['XML']  as $name => $entry){
1164         if(preg_match("/answer[0-9]*/i",$name)){
1165           $entry['STATUS'] = strtoupper($entry['STATUS']);
1166           switch($entry['STATUS']){
1168             case 'PROCESSING' :
1170               /* Send abort event, but only once 
1171                */
1172               if($already_aborted){
1173                 break;
1174               }elseif(class_available("DaemonEvent_faireboot")){
1175                 $already_aborted = TRUE;
1176                 $tmp = new DaemonEvent_faireboot($config);
1177                 $tmp->add_targets(array($mac));
1178                 $tmp->set_type(TRIGGERED_EVENT);
1179                 if(!$this->append($tmp)){
1180                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1181                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1182                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1183                 }else{
1184                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1185                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1186                 }
1187                 ;break;
1188               }else{
1189                 /* Couldn't find abort event, just remove entry */
1190               }
1192             case 'WAITING':
1193             case 'ERROR':
1194             default :
1195             
1196               /* Simply remove entries from queue. 
1197                *  Failed or waiting events, can be removed without any trouble.
1198                */ 
1199               if(!$this->remove_entries(array($entry['ID']))){
1200                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1201               }
1202               ;break;
1203           }
1204     
1205         }
1206       }
1207     }
1208   }
1211   static function ping($target)
1212   {
1213     if (tests::is_mac($target)){
1214       /* Get communication object */
1215       $d= new gosaSupportDaemon(TRUE,2);
1216       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1217       return (count($answer) ? TRUE:FALSE);
1218     }
1219     return (FALSE);
1220   }
1224   /*! \brief  Returns a list of all configured principals. 
1225               (Uses the GOsa support daemon instead of the ldap database.)
1226       @return Array  A list containing the names of all configured principals.
1227    */
1228   public function krb5_list_principals($server)
1229   {
1230     $res = array();  
1232     /* Check if the given server is a valid mac address
1233      */
1234     if(!tests::is_mac($server)){
1235       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1236       return($ret);
1237     }
1239     /* Prepare request event 
1240      */ 
1241     $xml_msg = 
1242       "<xml>".
1243       "<header>gosa_krb5_list_principals</header>".
1244       "<source>GOSA</source>".
1245       "<target>".$server."</target>".
1246       "</xml>";
1247     
1248     $tmp = $this->_send($xml_msg,TRUE);
1249     if(isset($tmp['XML']['PRINCIPAL'])){
1250       return($tmp['XML']['PRINCIPAL']);
1251     }else{
1252       return($res);
1253     }
1254   }
1257   /*! \brief  Returns the configuration settings for a given principal name. 
1258               (Uses the GOsa support daemon instead of the ldap database.)
1259       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1260       @return Array  A list containing the names of all configured principals.
1261    */
1262   public function krb5_get_principal($server,$name)
1263   {
1264     $ret = array();
1266     /* Check if the given name is a valid request value 
1267      */
1268     if(!is_string($name) || empty($name)){
1269       trigger_error("The given principal name is not of type string or it is empty.");
1270       return($ret);
1271     }
1273     /* Check if the given server is a valid mac address
1274      */
1275     if(!tests::is_mac($server)){
1276       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1277       return($ret);
1278     }
1280     /* Prepare request event 
1281      */ 
1282     $xml_msg = 
1283       "<xml>".
1284       "<header>gosa_krb5_get_principal</header>".
1285       "<principal>".$name."</principal>".
1286       "<source>GOSA</source>".
1287       "<target>".$server."</target>".
1288       "</xml>";
1290     $res = $this->_send($xml_msg,TRUE);
1291     if(isset($res['XML'])){
1292       return($res['XML']);
1293     }else{
1294       return($ret);
1295     }
1296   }
1299   /*! \brief  Creates a given principal with a set of configuration settings.
1300               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1301               (Uses the GOsa support daemon instead of the ldap database.)
1302       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1303       @return Boolean   TRUE on success else FALSE. 
1304    */
1305   public function krb5_add_principal($server,$name,$values)
1306   {
1307     $ret = FALSE;  
1309     /* Check if the given name is a valid request value 
1310      */
1311     if(!is_string($name) || empty($name)){
1312       trigger_error("The given principal name is not of type string or it is empty.");
1313       return($ret);
1314     }
1315     if(!is_array($values)){
1316       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1317       return($ret);
1318     }
1320     /* Check if the given server is a valid mac address
1321      */
1322     if(!tests::is_mac($server)){
1323       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1324       return($ret);
1325     }
1327     $attrs = "";
1328     foreach($values as $key => $value){
1329       if(empty($key) || is_numeric($key)){
1330         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1331         return($ret);
1332       }
1333       $key = strtolower($key);
1334       if(is_array($value)){
1335         foreach($value as $val){
1336           $attrs.= "<$key>$val</$key>\n";
1337         }
1338       }else{
1339         $attrs.= "<$key>$value</$key>\n";
1340       }
1341     }
1343     /* Prepare request event 
1344      */ 
1345     $xml_msg = 
1346       "<xml>".
1347       "<header>gosa_krb5_create_principal</header>".
1348       "<principal>".$name."</principal>".
1349       $attrs.
1350       "<source>GOSA</source>".
1351       "<target>".$server."</target>".
1352       "</xml>";
1354     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1355   }
1358   function krb5_ramdomize_key($server,$name)  
1359   {
1360     /* Prepare request event 
1361      */ 
1362     $xml_msg = 
1363       "<xml>".
1364       "<header>gosa_krb5_randomize_key</header>".
1365       "<principal>".$name."</principal>".
1366       "<source>GOSA</source>".
1367       "<target>".$server."</target>".
1368       "</xml>";
1370     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1371   }
1372   
1375   /*! \brief  Updates a given principal with a set of configuration settings.
1376               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1377               (Uses the GOsa support daemon instead of the ldap database.)
1378       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1379       @return Boolean   TRUE on success else FALSE. 
1380    */
1381   public function krb5_set_principal($server,$name,$values)
1382   {
1383     $ret = FALSE;  
1385     /* Check if the given name is a valid request value 
1386      */
1387     if(!is_string($name) || empty($name)){
1388       trigger_error("The given principal name is not of type string or it is empty.");
1389       return($ret);
1390     }
1391     if(!is_array($values) || !count($values)){
1392       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1393       return($ret);
1394     }
1396     /* Check if the given server is a valid mac address
1397      */
1398     if(!tests::is_mac($server)){
1399       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1400       return($ret);
1401     }
1403     $attrs = "";
1404     foreach($values as $key => $value){
1405       if(empty($key) || is_numeric($key)){
1406         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1407         return($ret);
1408       }
1409       $key = strtolower($key);
1410       if(is_array($value)){
1411         foreach($value as $val){
1412           $attrs.= "<$key>$val</$key>\n";
1413         }
1414       }else{
1415         $attrs.= "<$key>$value</$key>\n";
1416       }
1417     }
1419     /* Prepare request event 
1420      */ 
1421     $xml_msg = 
1422       "<xml>".
1423       "<header>gosa_krb5_modify_principal</header>".
1424       "<principal>".$name."</principal>".
1425       $attrs.
1426       "<source>GOSA</source>".
1427       "<target>".$server."</target>".
1428       "</xml>";
1430     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1431   }
1434   /*! \brief  Removes the given principal.
1435               (Uses the GOsa support daemon instead of the ldap database.)
1436       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1437       @return Boollean   TRUE on success else FALSE
1438    */
1439   public function krb5_del_principal($server,$name)
1440   {
1441     $ret = FALSE;  
1443     /* Check if the given name is a valid request value 
1444      */
1445     if(!is_string($name) || empty($name)){
1446       trigger_error("The given principal name is not of type string or it is empty.");
1447       return($ret);
1448     }
1450     /* Check if the given server is a valid mac address
1451      */
1452     if(!tests::is_mac($server)){
1453       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1454       return($ret);
1455     }
1457     /* Prepare request event 
1458      */ 
1459     $xml_msg = 
1460       "<xml>".
1461       "<header>gosa_krb5_del_principal</header>".
1462       "<principal>".$name."</principal>".
1463       "<source>GOSA</source>".
1464       "<target>".$server."</target>".
1465       "</xml>";
1466     
1467     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1468   }
1471   /*! \brief  Returns a list of configured password policies.
1472               (Uses the GOsa support daemon instead of the ldap database.)
1473       @return Array A list of all configured password policies.
1474    */
1475   public function krb5_list_policies($server)
1476   {
1477     $res = array();  
1479     /* Check if the given server is a valid mac address
1480      */
1481     if(!tests::is_mac($server)){
1482       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1483       return($ret);
1484     }
1486     /* Prepare request event 
1487      */ 
1488     $xml_msg = 
1489       "<xml>".
1490       "<header>gosa_krb5_list_policies</header>".
1491       "<source>GOSA</source>".
1492       "<target>".$server."</target>".
1493       "</xml>";
1494     
1495     $res = $this->_send($xml_msg,TRUE);
1496     
1497     /* Check if there are results for POLICY 
1498      */
1499     if(isset($res['XML']['POLICY'])){
1500       
1501       /* Ensure that we return an array 
1502        */
1503       $tmp = $res['XML']['POLICY'];
1504       if(!is_array($tmp)){
1505         $tmp = array($tmp);
1506       }
1507       return($tmp);
1508     }else{
1509       return(array());
1510     }
1511   }
1514   /*! \brief  Returns a list of configured password policies.
1515               (Uses the GOsa support daemon instead of the ldap database.)
1516       @return Array The policy settings for the given policy name.
1517    */
1518   public function krb5_get_policy($server,$name)
1519   {
1520     $ret = array();  
1522     /* Check if the given name is a valid request value 
1523      */
1524     if(!is_string($name) || empty($name)){
1525       trigger_error("The given policy name is not of type string or it is empty.");
1526       return($ret);
1527     }
1529     /* Check if the given server is a valid mac address
1530      */
1531     if(!tests::is_mac($server)){
1532       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1533       return($ret);
1534     }
1536     /* Prepare request event 
1537      */ 
1538     $xml_msg = 
1539       "<xml>".
1540       "<header>gosa_krb5_get_policy</header>".
1541       "<policy>".$name."</policy>".
1542       "<source>GOSA</source>".
1543       "<target>".$server."</target>".
1544       "</xml>";
1546     /* Possible attributes */
1547     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1548         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1550   
1551     $tmp = $this->_send($xml_msg,TRUE);
1552     if(isset($tmp['XML'])){
1553       foreach($attrs as $attr){
1554         if(isset($tmp['XML'][$attr])){
1555           $ret[$attr] = $tmp['XML'][$attr];
1556         }else{
1557           $ret[$attr] = "";
1558         }
1559       }
1560     }
1561     return($ret);
1562   }
1564   
1565   /*! \brief  Creates a new policy with a given set of configuration settings.
1566               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1567               (Uses the GOsa support daemon instead of the ldap database.)
1568       @pram   String The name of the policy to update.
1569       @pram   Array  The attributes to update
1570       @return Boolean   TRUE on success else FALSE. 
1571    */
1572   public function krb5_add_policy($server,$name,$values)
1573   {
1574     $ret = FALSE;  
1576     /* Check if the given name is a valid request value 
1577      */
1578     if(!is_string($name) || empty($name)){
1579       trigger_error("The given policy name is not of type string or it is empty.");
1580       return($ret);
1581     }
1582     if(!is_array($values) || !count($values)){
1583       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1584       return($ret);
1585     }
1587     /* Check if the given server is a valid mac address
1588      */
1589     if(!tests::is_mac($server)){
1590       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1591       return($ret);
1592     }
1595     /* Transform array into <xml>
1596      */
1597     $attrs = "";
1598     foreach($values as $id => $value){
1599       if(empty($id) || is_numeric($id)){
1600         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1601         return($ret);
1602       }
1603       $id = strtolower($id);
1604       $attrs.= "<$id>$value</$id>\n";
1605     }
1607     /* Prepare request event 
1608      */ 
1609     $xml_msg = 
1610       "<xml>".
1611       "<header>gosa_krb5_create_policy</header>".
1612       "<policy>".$name."</policy>".
1613       $attrs.
1614       "<source>GOSA</source>".
1615       "<target>".$server."</target>".
1616       "</xml>";
1618     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1619   }
1622   /*! \brief  Updates a given policy with a set of configuration settings.
1623               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1624               (Uses the GOsa support daemon instead of the ldap database.)
1625       @pram   String The name of the policy to update.
1626       @return Boolean   TRUE on success else FALSE. 
1627    */
1628   public function krb5_set_policy($server,$name,$values)
1629   {
1630     $ret = FALSE;  
1632     /* Check if the given name is a valid request value 
1633      */
1634     if(!is_string($name) || empty($name)){
1635       trigger_error("The given policy name is not of type string or it is empty.");
1636       return($ret);
1637     }
1638     if(!is_array($values) || !count($values)){
1639       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1640       return($ret);
1641     }
1643     /* Check if the given server is a valid mac address
1644      */
1645     if(!tests::is_mac($server)){
1646       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1647       return($ret);
1648     }
1650     /* Transform array into <xml>
1651      */
1652     $attrs = "";
1653     foreach($values as $id => $value){
1654       if(preg_match("/^policy$/i",$id)) continue;
1655       if(empty($id) || is_numeric($id)){
1656         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1657         return($ret);
1658       }
1659       $id = strtolower($id);
1660       $attrs.= "<$id>$value</$id>\n";
1661     }
1663     /* Prepare request event 
1664      */ 
1665     $xml_msg = 
1666       "<xml>".
1667       "<header>gosa_krb5_modify_policy</header>".
1668       "<policy>".$name."</policy>".
1669       $attrs.
1670       "<source>GOSA</source>".
1671       "<target>".$server."</target>".
1672       "</xml>";
1674     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1675   }
1677   
1678   /*! \brief  Removes the given password policy. 
1679               (Uses the GOsa support daemon instead of the ldap database.)
1680       @return Boolean  TRUE on success else FALSE
1681    */
1682   public function krb5_del_policy($server,$name)
1683   {
1684     $ret = FALSE;
1686     /* Check if the given server is a valid mac address
1687      */
1688     if(!tests::is_mac($server)){
1689       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1690       return($ret);
1691     }
1693     /* Check if the given name is a valid request value 
1694      */
1695     if(!is_string($name) || empty($name)){
1696       trigger_error("The given policy name is not of type string or it is empty.");
1697       return($ret);
1698     }
1700     /* Prepare request event 
1701      */ 
1702     $xml_msg = 
1703       "<xml>".
1704       "<header>gosa_krb5_del_policy</header>".
1705       "<policy>".$name."</policy>".
1706       "<source>GOSA</source>".
1707       "<target>".$server."</target>".
1708       "</xml>";
1709     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1710   }
1713   /*! \brief  Sets the password of for the given principal.
1714               (Uses the GOsa support daemon instead of the ldap database.)
1715       @param  String  The servers mac
1716       @param  String  The principals name
1717       @param  String  $the new password.   
1718       @return Boolean  TRUE on success else FALSE
1719    */
1720   public function krb5_set_password($server,$name,$password)
1721   {
1722     $ret = FALSE;
1724     /* Check if the given server is a valid mac address
1725      */
1726     if(!tests::is_mac($server)){
1727       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1728       return($ret);
1729     }
1731     /* Check if the given name is a valid request value
1732      */
1733     if(!is_string($name) || empty($name)){
1734       trigger_error("The given principal name is not of type string or it is empty.");
1735       return($ret);
1736     }
1738     /* Prepare request event
1739      */
1740     $xml_msg =
1741       "<xml>".
1742       "<header>gosa_krb5_set_password</header>".
1743       "<principal>".$name."</principal>".
1744       "<password>".$password."</password>".
1745       "<source>GOSA</source>".
1746       "<target>".$server."</target>".
1747       "</xml>";
1748     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1749   }
1752   /*! \brief  Returns log file informations for a given mac address 
1753       @param  $mac The mac address to fetch logs for.
1754       @retrun Array A Multidimensional array containing log infos.
1755         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1756         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1757                                install_20080313_144450   ...
1758    */
1759   public function get_log_info_for_mac($mac)
1760   {
1761     $xml_msg = "
1762       <xml>
1763       <header>gosa_show_log_by_mac</header>
1764       <target>GOSA</target>
1765       <source>GOSA</source>
1766       <mac>".$mac."</mac>
1767       </xml>";
1769     $res = $this->_send($xml_msg,TRUE);
1770     $ret = array();
1771     if(isset($res['XML'])){
1773       /* Filter all entry that look like this 
1774           MAC_00_01_6C_9D_B9_FA
1775        */
1776       foreach($res['XML'] as $name => $entry){
1777         if(preg_match("/^MAC/",$name)){
1779           /* Get list of available log files 
1780            */
1781           if(!is_array($entry)){
1782             $entry = array($entry);
1783           }
1784           foreach($entry as $log_date){
1785             $xml_msg2 = "<xml> 
1786               <header>gosa_show_log_files_by_date_and_mac</header> 
1787               <target>GOSA</target> 
1788               <source>GOSA</source>                        
1789               <date>".$log_date."</date> 
1790               <mac>".$mac."</mac> 
1791               </xml>";
1792    
1793             $ret[$mac][$log_date] = array();
1794             $res = $this->_send($xml_msg2,TRUE);
1795             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1796             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1797             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1798               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1799             }
1800           }
1801         }
1802       }
1803     }
1804     return($ret);
1805   }
1807   public function get_log_file($mac,$date,$file)
1808   {
1809     $xml_msg ="
1810       <xml> 
1811       <header>gosa_get_log_file_by_date_and_mac</header> 
1812       <target>GOSA</target> 
1813       <source>GOSA</source>
1814       <date>".$date."</date> 
1815       <mac>".$mac."</mac> 
1816       <log_file>".$file."</log_file>
1817       </xml>";
1819     $res = $this->_send($xml_msg,TRUE);
1820     if(isset($res['XML'][strtoupper($file)])){
1821       return(base64_decode($res['XML'][strtoupper($file)]));
1822     }
1823     return("");
1824   }
1830   /*****************
1831    * DAK - Functions 
1832    *****************/
1834   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1835       @param  ...
1836       @return Array   All queued entries.
1837    */
1838   public function DAK_keyring_entries($server)  
1839   {
1840     /* Ensure that we send the event to a valid mac address 
1841      */
1842     if(!is_string($server) || !tests::is_mac($server)){
1843       trigger_error("No valid mac address given '".$server."'.");
1844       return;
1845     }
1847     /* Create query
1848      */
1849     $xml_msg = "<xml> 
1850                   <header>gosa_get_dak_keyring</header> 
1851                   <target>".$server."</target> 
1852                   <source>GOSA</source>
1853                 </xml>";
1854         
1855     $res = $this->_send($xml_msg,TRUE);
1857     /* Check if there are results for POLICY
1858      */
1859     if(isset($res['XML'])){
1860       $ret = array();
1861       foreach($res['XML'] as $key => $entry){
1862         if(preg_match("/^ANSWER/",$key)){
1863           $ret[] = $entry;
1864         }
1865       }
1866       return($ret);
1867     }else{
1868       return(array());
1869     }
1870   }
1873   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1874       @param  String  The servers mac address 
1875       @param  String  The gpg key.
1876       @return Boolean TRUE on success else FALSE 
1877    */
1878   public function DAK_import_key($server,$key)  
1879   {
1880     /* Ensure that we send the event to a valid mac address 
1881      */
1882     if(!is_string($server) || !tests::is_mac($server)){
1883       trigger_error("No valid mac address given '".$server."'.");
1884       return;
1885     }
1887     /* Check if there is some cleanup required before importing the key.
1888         There may be some Header lines like:
1889         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1890      */
1891     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1893       /* Remove header */
1894       $key = preg_replace("/^.*\n\n/sim","",$key);
1895       /* Remove footer */
1896       $key = preg_replace("/-----.*$/sim","",$key);
1897     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1898       
1899       /* Encode key if it is raw.
1900        */
1901       $key = base64_encode($key);
1902     }
1904     /* Create query
1905      */
1906     $xml_msg = "<xml> 
1907                   <header>gosa_import_dak_key</header> 
1908                   <target>".$server."</target> 
1909                   <key>".$key."</key> 
1910                   <source>GOSA</source>
1911                 </xml>";
1912         
1913     $res = $this->_send($xml_msg,TRUE);
1914     return($this->is_error());
1915   }
1918   /*! \brief Removes a key from the keyring on the given server. 
1919       @param  String  The servers mac address 
1920       @param  String  The gpg key uid.
1921       @return Boolean TRUE on success else FALSE 
1922    */
1923   public function DAK_remove_key($server,$key)  
1924   {
1925     /* Ensure that we send the event to a valid mac address 
1926      */
1927     if(!is_string($server) || !tests::is_mac($server)){
1928       trigger_error("No valid mac address given '".$server."'.");
1929       return;
1930     }
1932     /* Create query
1933      */
1934     $xml_msg = "<xml> 
1935                   <header>gosa_remove_dak_key</header> 
1936                   <target>".$server."</target> 
1937                   <keyid>".$key."</keyid> 
1938                   <source>GOSA</source>
1939                 </xml>";
1940        
1941     $res = $this->_send($xml_msg,TRUE);
1942     return($this->is_error());
1943   }
1946 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1947 ?>