Code

Applied in_array strict patches from trunk
[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("core","gosaSupportTimeout");
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("core","gosaSupportURI") != ""){
76       if ($this->s_host == ""){
77         $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("core","gosaSupportURI"));
78         $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("core","gosaSupportURI"));
79         $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("core","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_strict($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_strict($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="");
1052     $start = microtime(1);
1054     /*******
1055       Start sending data 
1056      *******/
1057     if($this->connect()){
1058       $this->o_sock->write($data);
1059       if ($answer_expected){
1060         $str = trim($this->o_sock->read());
1062         /* Check if something went wrong while reading */
1063         if($this->o_sock->is_error()){
1064           $this->set_error($this->o_sock->get_error());
1065           @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, sprintf('%.7f', microtime(1) - $start) , "<b>FAILED Duration:</b>");
1066           return($ret);
1067         }
1069         $entries = $this->xml_to_array($str);
1070         if(isset($entries['XML']) && is_array($entries['XML'])){
1071           $ret = $entries;
1072           if($this->use_alternative_xml_parse_method) {
1074             // --------- Seems broken, check for 'ERROR' but using 'ERROR_STRING'
1075             if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1076               $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1077               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1078                   array($data=>$data),"FAILED ".$this->get_error());
1079             // ---------
1080       
1081           
1082             }elseif(isset($entries['XML'][0]['ERROR'][0]['VALUE'])){
1083               $this->set_error($entries['XML'][0]['ERROR'][0]['VALUE']);
1084               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1085                   array($data=>$data),"FAILED ".$this->get_error());
1086             }
1087           }else{
1088             if(isset($entries['XML']['ERROR_STRING'])) {
1089               $this->set_error($entries['XML']['ERROR_STRING']);
1090               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1091                   array($data=>$data),"FAILED ".$this->get_error());
1092             }elseif(isset($entries['XML']['ERROR'])){
1093               $this->set_error($entries['XML']['ERROR']);
1094               new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1095                   array($data=>$data),"FAILED ".$this->get_error());
1096             }
1097           }
1098           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1099               array($data=>$data),"SUCCESS");
1100         }
1101       }else{
1102         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", 
1103             array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1104       }
1105     }
1107     @DEBUG(DEBUG_SI, $line, "<b>".$class."::".$function."</b>" , $file, sprintf('%.7f', microtime(1) - $start) , "<b>Duration:</b>");
1108     return($ret);
1109   }
1112   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1113   {
1114     $xml_message= "";
1116     /* Get communication object */
1117     $d= new gosaSupportDaemon(TRUE,10);
1119     /* Prepare data */
1120     foreach ($data as $key => $value){
1121       if(is_array($value)){
1122         foreach($value as $sub_val){
1123           $xml_message.= "<$key>$sub_val</$key>";
1124         }
1125       }else{
1126         $xml_message.= "<$key>$value</$key>";
1127       }
1128     }
1130     /* Multiple targets? */
1131     if (!is_array($to)){
1132       $to_targets= array($to);
1133     } else {
1134       $to_targets= $to;
1135     }
1137     /* Build target strings */
1138     $target ="";
1139     foreach($to_targets as $to){
1140       $target.= "<target>$to</target>";
1141     }
1143     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1144   }
1147   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1148       @param  String  $mac  The mac address for which we want to remove all jobs.      
1149    */
1150   function clean_queue_from_mac($mac)
1151   {
1152     global $config;
1153     if(!isset($config) || !$config){
1154       $config = session::global_get('config');
1155     }
1157     /* First of all we have to check which jobs are startet 
1158      *  for $mac 
1159      */
1160     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1161     
1162     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1163  
1164     $data = $this->_send($xml_msg,TRUE);
1165     if(is_array($data) && isset($data['XML'])){
1166       $already_aborted = FALSE;
1167       foreach($data['XML']  as $name => $entry){
1168         if(preg_match("/answer[0-9]*/i",$name)){
1169           $entry['STATUS'] = strtoupper($entry['STATUS']);
1170           switch($entry['STATUS']){
1172             case 'PROCESSING' :
1174               /* Send abort event, but only once 
1175                */
1176               if($already_aborted){
1177                 break;
1178               }elseif(class_available("DaemonEvent_faireboot")){
1179                 $already_aborted = TRUE;
1180                 $tmp = new DaemonEvent_faireboot($config);
1181                 $tmp->add_targets(array($mac));
1182                 $tmp->set_type(TRIGGERED_EVENT);
1183                 if(!$this->append($tmp)){
1184                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"), bold($entry['ID'])) , ERROR_DIALOG);
1185                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1186                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1187                 }else{
1188                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1189                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1190                 }
1191                 ;break;
1192               }else{
1193                 /* Couldn't find abort event, just remove entry */
1194               }
1196             case 'WAITING':
1197             case 'ERROR':
1198             default :
1199             
1200               /* Simply remove entries from queue. 
1201                *  Failed or waiting events, can be removed without any trouble.
1202                */ 
1203               if(!$this->remove_entries(array($entry['ID']))){
1204                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"), bold($entry['ID'])) , ERROR_DIALOG);
1205               }
1206               ;break;
1207           }
1208     
1209         }
1210       }
1211     }
1212   }
1215   static function ping($target)
1216   {
1217     if (tests::is_mac($target)){
1218       /* Get communication object */
1219       $d= new gosaSupportDaemon(TRUE,2);
1220       $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1221       return (count($answer) ? TRUE:FALSE);
1222     }
1223     return (FALSE);
1224   }
1228   /*! \brief  Returns a list of all configured principals. 
1229               (Uses the GOsa support daemon instead of the ldap database.)
1230       @return Array  A list containing the names of all configured principals.
1231    */
1232   public function krb5_list_principals($server)
1233   {
1234     $res = array();  
1236     /* Check if the given server is a valid mac address
1237      */
1238     if(!tests::is_mac($server)){
1239       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1240       return($ret);
1241     }
1243     /* Prepare request event 
1244      */ 
1245     $xml_msg = 
1246       "<xml>".
1247       "<header>gosa_krb5_list_principals</header>".
1248       "<source>GOSA</source>".
1249       "<target>".$server."</target>".
1250       "</xml>";
1251     
1252     $tmp = $this->_send($xml_msg,TRUE);
1253     if(isset($tmp['XML']['PRINCIPAL'])){
1254       return($tmp['XML']['PRINCIPAL']);
1255     }else{
1256       return($res);
1257     }
1258   }
1261   /*! \brief  Returns the configuration settings for a given principal name. 
1262               (Uses the GOsa support daemon instead of the ldap database.)
1263       @pram   String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1264       @return Array  A list containing the names of all configured principals.
1265    */
1266   public function krb5_get_principal($server,$name)
1267   {
1268     $ret = array();
1270     /* Check if the given name is a valid request value 
1271      */
1272     if(!is_string($name) || empty($name)){
1273       trigger_error("The given principal name is not of type string or it is empty.");
1274       return($ret);
1275     }
1277     /* Check if the given server is a valid mac address
1278      */
1279     if(!tests::is_mac($server)){
1280       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1281       return($ret);
1282     }
1284     /* Prepare request event 
1285      */ 
1286     $xml_msg = 
1287       "<xml>".
1288       "<header>gosa_krb5_get_principal</header>".
1289       "<principal>".$name."</principal>".
1290       "<source>GOSA</source>".
1291       "<target>".$server."</target>".
1292       "</xml>";
1294     $res = $this->_send($xml_msg,TRUE);
1295     if(isset($res['XML'])){
1296       return($res['XML']);
1297     }else{
1298       return($ret);
1299     }
1300   }
1303   /*! \brief  Creates a given principal with a set of configuration settings.
1304               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1305               (Uses the GOsa support daemon instead of the ldap database.)
1306       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1307       @return Boolean   TRUE on success else FALSE. 
1308    */
1309   public function krb5_add_principal($server,$name,$values)
1310   {
1311     $ret = FALSE;  
1313     /* Check if the given name is a valid request value 
1314      */
1315     if(!is_string($name) || empty($name)){
1316       trigger_error("The given principal name is not of type string or it is empty.");
1317       return($ret);
1318     }
1319     if(!is_array($values)){
1320       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1321       return($ret);
1322     }
1324     /* Check if the given server is a valid mac address
1325      */
1326     if(!tests::is_mac($server)){
1327       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1328       return($ret);
1329     }
1331     $attrs = "";
1332     foreach($values as $key => $value){
1333       if(empty($key) || is_numeric($key)){
1334         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1335         return($ret);
1336       }
1337       $key = strtolower($key);
1338       if(is_array($value)){
1339         foreach($value as $val){
1340           $attrs.= "<$key>$val</$key>\n";
1341         }
1342       }else{
1343         $attrs.= "<$key>$value</$key>\n";
1344       }
1345     }
1347     /* Prepare request event 
1348      */ 
1349     $xml_msg = 
1350       "<xml>".
1351       "<header>gosa_krb5_create_principal</header>".
1352       "<principal>".$name."</principal>".
1353       $attrs.
1354       "<source>GOSA</source>".
1355       "<target>".$server."</target>".
1356       "</xml>";
1358     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1359   }
1362   function krb5_ramdomize_key($server,$name)  
1363   {
1364     /* Prepare request event 
1365      */ 
1366     $xml_msg = 
1367       "<xml>".
1368       "<header>gosa_krb5_randomize_key</header>".
1369       "<principal>".$name."</principal>".
1370       "<source>GOSA</source>".
1371       "<target>".$server."</target>".
1372       "</xml>";
1374     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1375   }
1376   
1379   /*! \brief  Updates a given principal with a set of configuration settings.
1380               For a list of configurable attributes have a look at 'krb5_get_principal()'.
1381               (Uses the GOsa support daemon instead of the ldap database.)
1382       @pram   String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1383       @return Boolean   TRUE on success else FALSE. 
1384    */
1385   public function krb5_set_principal($server,$name,$values)
1386   {
1387     $ret = FALSE;  
1389     /* Check if the given name is a valid request value 
1390      */
1391     if(!is_string($name) || empty($name)){
1392       trigger_error("The given principal name is not of type string or it is empty.");
1393       return($ret);
1394     }
1395     if(!is_array($values) || !count($values)){
1396       trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1397       return($ret);
1398     }
1400     /* Check if the given server is a valid mac address
1401      */
1402     if(!tests::is_mac($server)){
1403       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1404       return($ret);
1405     }
1407     $attrs = "";
1408     foreach($values as $key => $value){
1409       if(empty($key) || is_numeric($key)){
1410         trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1411         return($ret);
1412       }
1413       $key = strtolower($key);
1414       if(is_array($value)){
1415         foreach($value as $val){
1416           $attrs.= "<$key>$val</$key>\n";
1417         }
1418       }else{
1419         $attrs.= "<$key>$value</$key>\n";
1420       }
1421     }
1423     /* Prepare request event 
1424      */ 
1425     $xml_msg = 
1426       "<xml>".
1427       "<header>gosa_krb5_modify_principal</header>".
1428       "<principal>".$name."</principal>".
1429       $attrs.
1430       "<source>GOSA</source>".
1431       "<target>".$server."</target>".
1432       "</xml>";
1434     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1435   }
1438   /*! \brief  Removes the given principal.
1439               (Uses the GOsa support daemon instead of the ldap database.)
1440       @pram   String The name of the principal. (e.g. peter@EXAMPLE.DE)
1441       @return Boollean   TRUE on success else FALSE
1442    */
1443   public function krb5_del_principal($server,$name)
1444   {
1445     $ret = FALSE;  
1447     /* Check if the given name is a valid request value 
1448      */
1449     if(!is_string($name) || empty($name)){
1450       trigger_error("The given principal name is not of type string or it is empty.");
1451       return($ret);
1452     }
1454     /* Check if the given server is a valid mac address
1455      */
1456     if(!tests::is_mac($server)){
1457       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1458       return($ret);
1459     }
1461     /* Prepare request event 
1462      */ 
1463     $xml_msg = 
1464       "<xml>".
1465       "<header>gosa_krb5_del_principal</header>".
1466       "<principal>".$name."</principal>".
1467       "<source>GOSA</source>".
1468       "<target>".$server."</target>".
1469       "</xml>";
1470     
1471     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1472   }
1475   /*! \brief  Returns a list of configured password policies.
1476               (Uses the GOsa support daemon instead of the ldap database.)
1477       @return Array A list of all configured password policies.
1478    */
1479   public function krb5_list_policies($server)
1480   {
1481     $res = array();  
1483     /* Check if the given server is a valid mac address
1484      */
1485     if(!tests::is_mac($server)){
1486       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1487       return($ret);
1488     }
1490     /* Prepare request event 
1491      */ 
1492     $xml_msg = 
1493       "<xml>".
1494       "<header>gosa_krb5_list_policies</header>".
1495       "<source>GOSA</source>".
1496       "<target>".$server."</target>".
1497       "</xml>";
1498     
1499     $res = $this->_send($xml_msg,TRUE);
1500     
1501     /* Check if there are results for POLICY 
1502      */
1503     if(isset($res['XML']['POLICY'])){
1504       
1505       /* Ensure that we return an array 
1506        */
1507       $tmp = $res['XML']['POLICY'];
1508       if(!is_array($tmp)){
1509         $tmp = array($tmp);
1510       }
1511       return($tmp);
1512     }else{
1513       return(array());
1514     }
1515   }
1518   /*! \brief  Returns a list of configured password policies.
1519               (Uses the GOsa support daemon instead of the ldap database.)
1520       @return Array The policy settings for the given policy name.
1521    */
1522   public function krb5_get_policy($server,$name)
1523   {
1524     $ret = array();  
1526     /* Check if the given name is a valid request value 
1527      */
1528     if(!is_string($name) || empty($name)){
1529       trigger_error("The given policy name is not of type string or it is empty.");
1530       return($ret);
1531     }
1533     /* Check if the given server is a valid mac address
1534      */
1535     if(!tests::is_mac($server)){
1536       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1537       return($ret);
1538     }
1540     /* Prepare request event 
1541      */ 
1542     $xml_msg = 
1543       "<xml>".
1544       "<header>gosa_krb5_get_policy</header>".
1545       "<policy>".$name."</policy>".
1546       "<source>GOSA</source>".
1547       "<target>".$server."</target>".
1548       "</xml>";
1550     /* Possible attributes */
1551     $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1552         "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1554   
1555     $tmp = $this->_send($xml_msg,TRUE);
1556     if(isset($tmp['XML'])){
1557       foreach($attrs as $attr){
1558         if(isset($tmp['XML'][$attr])){
1559           $ret[$attr] = $tmp['XML'][$attr];
1560         }else{
1561           $ret[$attr] = "";
1562         }
1563       }
1564     }
1565     return($ret);
1566   }
1568   
1569   /*! \brief  Creates a new policy with a given set of configuration settings.
1570               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1571               (Uses the GOsa support daemon instead of the ldap database.)
1572       @pram   String The name of the policy to update.
1573       @pram   Array  The attributes to update
1574       @return Boolean   TRUE on success else FALSE. 
1575    */
1576   public function krb5_add_policy($server,$name,$values)
1577   {
1578     $ret = FALSE;  
1580     /* Check if the given name is a valid request value 
1581      */
1582     if(!is_string($name) || empty($name)){
1583       trigger_error("The given policy name is not of type string or it is empty.");
1584       return($ret);
1585     }
1586     if(!is_array($values) || !count($values)){
1587       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1588       return($ret);
1589     }
1591     /* Check if the given server is a valid mac address
1592      */
1593     if(!tests::is_mac($server)){
1594       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1595       return($ret);
1596     }
1599     /* Transform array into <xml>
1600      */
1601     $attrs = "";
1602     foreach($values as $id => $value){
1603       if(empty($id) || is_numeric($id)){
1604         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1605         return($ret);
1606       }
1607       $id = strtolower($id);
1608       $attrs.= "<$id>$value</$id>\n";
1609     }
1611     /* Prepare request event 
1612      */ 
1613     $xml_msg = 
1614       "<xml>".
1615       "<header>gosa_krb5_create_policy</header>".
1616       "<policy>".$name."</policy>".
1617       $attrs.
1618       "<source>GOSA</source>".
1619       "<target>".$server."</target>".
1620       "</xml>";
1622     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1623   }
1626   /*! \brief  Updates a given policy with a set of configuration settings.
1627               For a list of configurable attributes have a look at 'krb5_get_policy()'.
1628               (Uses the GOsa support daemon instead of the ldap database.)
1629       @pram   String The name of the policy to update.
1630       @return Boolean   TRUE on success else FALSE. 
1631    */
1632   public function krb5_set_policy($server,$name,$values)
1633   {
1634     $ret = FALSE;  
1636     /* Check if the given name is a valid request value 
1637      */
1638     if(!is_string($name) || empty($name)){
1639       trigger_error("The given policy name is not of type string or it is empty.");
1640       return($ret);
1641     }
1642     if(!is_array($values) || !count($values)){
1643       trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1644       return($ret);
1645     }
1647     /* Check if the given server is a valid mac address
1648      */
1649     if(!tests::is_mac($server)){
1650       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1651       return($ret);
1652     }
1654     /* Transform array into <xml>
1655      */
1656     $attrs = "";
1657     foreach($values as $id => $value){
1658       if(preg_match("/^policy$/i",$id)) continue;
1659       if(empty($id) || is_numeric($id)){
1660         trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1661         return($ret);
1662       }
1663       $id = strtolower($id);
1664       $attrs.= "<$id>$value</$id>\n";
1665     }
1667     /* Prepare request event 
1668      */ 
1669     $xml_msg = 
1670       "<xml>".
1671       "<header>gosa_krb5_modify_policy</header>".
1672       "<policy>".$name."</policy>".
1673       $attrs.
1674       "<source>GOSA</source>".
1675       "<target>".$server."</target>".
1676       "</xml>";
1678     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1679   }
1681   
1682   /*! \brief  Removes the given password policy. 
1683               (Uses the GOsa support daemon instead of the ldap database.)
1684       @return Boolean  TRUE on success else FALSE
1685    */
1686   public function krb5_del_policy($server,$name)
1687   {
1688     $ret = FALSE;
1690     /* Check if the given server is a valid mac address
1691      */
1692     if(!tests::is_mac($server)){
1693       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1694       return($ret);
1695     }
1697     /* Check if the given name is a valid request value 
1698      */
1699     if(!is_string($name) || empty($name)){
1700       trigger_error("The given policy name is not of type string or it is empty.");
1701       return($ret);
1702     }
1704     /* Prepare request event 
1705      */ 
1706     $xml_msg = 
1707       "<xml>".
1708       "<header>gosa_krb5_del_policy</header>".
1709       "<policy>".$name."</policy>".
1710       "<source>GOSA</source>".
1711       "<target>".$server."</target>".
1712       "</xml>";
1713     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1714   }
1717   /*! \brief  Sets the password of for the given principal.
1718               (Uses the GOsa support daemon instead of the ldap database.)
1719       @param  String  The servers mac
1720       @param  String  The principals name
1721       @param  String  $the new password.   
1722       @return Boolean  TRUE on success else FALSE
1723    */
1724   public function krb5_set_password($server,$name,$password)
1725   {
1726     $ret = FALSE;
1728     /* Check if the given server is a valid mac address
1729      */
1730     if(!tests::is_mac($server)){
1731       trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1732       return($ret);
1733     }
1735     /* Check if the given name is a valid request value
1736      */
1737     if(!is_string($name) || empty($name)){
1738       trigger_error("The given principal name is not of type string or it is empty.");
1739       return($ret);
1740     }
1742     /* Prepare request event
1743      */
1744     $xml_msg =
1745       "<xml>".
1746       "<header>gosa_krb5_set_password</header>".
1747       "<principal>".$name."</principal>".
1748       "<password>".$password."</password>".
1749       "<source>GOSA</source>".
1750       "<target>".$server."</target>".
1751       "</xml>";
1752     return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1753   }
1756   /*! \brief  Returns log file informations for a given mac address 
1757       @param  $mac The mac address to fetch logs for.
1758       @retrun Array A Multidimensional array containing log infos.
1759         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1760         MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1761                                install_20080313_144450   ...
1762    */
1763   public function get_log_info_for_mac($mac)
1764   {
1765     $xml_msg = "
1766       <xml>
1767       <header>gosa_show_log_by_mac</header>
1768       <target>GOSA</target>
1769       <source>GOSA</source>
1770       <mac>".$mac."</mac>
1771       </xml>";
1773     $res = $this->_send($xml_msg,TRUE);
1774     $ret = array();
1775     if(isset($res['XML'])){
1777       /* Filter all entry that look like this 
1778           MAC_00_01_6C_9D_B9_FA
1779        */
1780       foreach($res['XML'] as $name => $entry){
1781         if(preg_match("/^MAC/",$name)){
1783           /* Get list of available log files 
1784            */
1785           if(!is_array($entry)){
1786             $entry = array($entry);
1787           }
1788           foreach($entry as $log_date){
1789             $xml_msg2 = "<xml> 
1790               <header>gosa_show_log_files_by_date_and_mac</header> 
1791               <target>GOSA</target> 
1792               <source>GOSA</source>                        
1793               <date>".$log_date."</date> 
1794               <mac>".$mac."</mac> 
1795               </xml>";
1796    
1797             $ret[$mac][$log_date] = array();
1798             $res = $this->_send($xml_msg2,TRUE);
1799             $ret[$mac][$log_date]['DATE_STR']  = $log_date; 
1800             $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1801             if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1802               $ret[$mac][$log_date]['FILES']     = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1803             }
1804           }
1805         }
1806       }
1807     }
1808     return($ret);
1809   }
1811   public function get_log_file($mac,$date,$file)
1812   {
1813     $xml_msg ="
1814       <xml> 
1815       <header>gosa_get_log_file_by_date_and_mac</header> 
1816       <target>GOSA</target> 
1817       <source>GOSA</source>
1818       <date>".$date."</date> 
1819       <mac>".$mac."</mac> 
1820       <log_file>".$file."</log_file>
1821       </xml>";
1823     $res = $this->_send($xml_msg,TRUE);
1824     if(isset($res['XML'][strtoupper($file)])){
1825       return(base64_decode($res['XML'][strtoupper($file)]));
1826     }
1827     return("");
1828   }
1834   /*****************
1835    * DAK - Functions 
1836    *****************/
1838   /*! \brief  Returns all currenlty queued entries for a given DAK repository 
1839       @param  ...
1840       @return Array   All queued entries.
1841    */
1842   public function DAK_keyring_entries($server)  
1843   {
1844     /* Ensure that we send the event to a valid mac address 
1845      */
1846     if(!is_string($server) || !tests::is_mac($server)){
1847       trigger_error("No valid mac address given '".$server."'.");
1848       return;
1849     }
1851     /* Create query
1852      */
1853     $xml_msg = "<xml> 
1854                   <header>gosa_get_dak_keyring</header> 
1855                   <target>".$server."</target> 
1856                   <source>GOSA</source>
1857                 </xml>";
1858         
1859     $res = $this->_send($xml_msg,TRUE);
1861     /* Check if there are results for POLICY
1862      */
1863     if(isset($res['XML'])){
1864       $ret = array();
1865       foreach($res['XML'] as $key => $entry){
1866         if(preg_match("/^ANSWER/",$key)){
1867           $ret[] = $entry;
1868         }
1869       }
1870       return($ret);
1871     }else{
1872       return(array());
1873     }
1874   }
1877   /*! \brief  Imports the given key into the specified keyring (Servers mac address)
1878       @param  String  The servers mac address 
1879       @param  String  The gpg key.
1880       @return Boolean TRUE on success else FALSE 
1881    */
1882   public function DAK_import_key($server,$key)  
1883   {
1884     /* Ensure that we send the event to a valid mac address 
1885      */
1886     if(!is_string($server) || !tests::is_mac($server)){
1887       trigger_error("No valid mac address given '".$server."'.");
1888       return;
1889     }
1891     /* Check if there is some cleanup required before importing the key.
1892         There may be some Header lines like:
1893         -----BEGIN PGP PUBLIC KEY BLOCK-----   Version: GnuPG v1.4.6 (GNU/Linux)
1894      */
1895     if(preg_match("/BEGIN PGP PUBLIC KEY BLOCK/",$key)){
1897       /* Remove header */
1898       $key = preg_replace("/^.*\n\n/sim","",$key);
1899       /* Remove footer */
1900       $key = preg_replace("/-----.*$/sim","",$key);
1901     }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1902       
1903       /* Encode key if it is raw.
1904        */
1905       $key = base64_encode($key);
1906     }
1908     /* Create query
1909      */
1910     $xml_msg = "<xml> 
1911                   <header>gosa_import_dak_key</header> 
1912                   <target>".$server."</target> 
1913                   <key>".$key."</key> 
1914                   <source>GOSA</source>
1915                 </xml>";
1916         
1917     $res = $this->_send($xml_msg,TRUE);
1918     return($this->is_error());
1919   }
1922   /*! \brief Removes a key from the keyring on the given server. 
1923       @param  String  The servers mac address 
1924       @param  String  The gpg key uid.
1925       @return Boolean TRUE on success else FALSE 
1926    */
1927   public function DAK_remove_key($server,$key)  
1928   {
1929     /* Ensure that we send the event to a valid mac address 
1930      */
1931     if(!is_string($server) || !tests::is_mac($server)){
1932       trigger_error("No valid mac address given '".$server."'.");
1933       return;
1934     }
1936     /* Create query
1937      */
1938     $xml_msg = "<xml> 
1939                   <header>gosa_remove_dak_key</header> 
1940                   <target>".$server."</target> 
1941                   <keyid>".$key."</keyid> 
1942                   <source>GOSA</source>
1943                 </xml>";
1944        
1945     $res = $this->_send($xml_msg,TRUE);
1946     return($this->is_error());
1947   }
1950 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1951 ?>