ae7feceed479d6ccac85ae24f16d8a4d8e3e5157
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;
36 protected $use_alternative_xml_parse_method = FALSE;
38 /*! \brief Creates a new gosaSupportDaemon object.
39 @param string Host The Host where the daemon is running on.
40 @param integer Port The port which the daemon use.
41 @param string Key The encryption string.
42 @param boolean Connect Directly connect to daemon socket.
43 @param float Timeout The timelimit for all socket actions.
44 */
45 public function __construct($connect=TRUE,$timeout=10)
46 {
47 #FIXME: bad idea about referencing global variables from within classes
48 global $config;
50 /* This should only be the case if we call this from setup.
51 __autoload()
52 */
53 if(!is_object($config)) { return; }
55 # load from config, store statically
56 if ($config->get_cfg_value("gosa_si") != ""){
58 if ($this->s_host == ""){
59 $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosa_si"));
60 $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosa_si"));
61 $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosa_si"));
62 }
64 $this->f_timeout = $timeout;
65 if($connect){
66 $this->connect();
67 }
68 }
69 }
72 /*! \brief Establish daemon connection.
73 @return boolean Returns true if the connection was succesfully established.
74 */
75 public function connect()
76 {
77 if(!empty($this->s_host) && !empty($this->i_port)){
78 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
79 if($this->o_sock->connected()){
80 $this->o_sock->setEncryptionKey($this->s_encryption_key);
81 $this->is_connected = TRUE;
82 }else{
83 $this->set_error($this->o_sock->get_error());
84 $this->disconnect();
85 new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
86 }
87 }else{
88 $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
89 }
90 return($this->is_connected);
91 }
94 /*! \brief Returns TRUE whether we are connected or not
95 @return BOOLEAN Returns TRUE when connected else FALSE
96 */
97 public function is_connected()
98 {
99 return($this->is_connected);
100 }
103 /*! \brief */
104 public function get_hosts_with_module($mod)
105 {
106 $data = array("module_name" => $mod);
107 $res = $this->send_data("gosa_get_hosts_with_module",$this->s_host.":".$this->i_port,$data,TRUE);
108 $hosts = array();
109 if(isset($res['XML'])){
110 foreach($res['XML'][0] as $name => $data){
111 if(preg_match("/^ANSWER[0-9]*$/",$name)){
112 if(isset($data[0]['MAC'][0]['VALUE'])){
113 $hosts[] = $data[0]['MAC'][0]['VALUE'];
114 }elseif(isset($data[0]['VALUE'])){
115 $hosts[] = $data[0]['VALUE'];
116 }
117 }
118 }
119 }
120 return($hosts);
121 }
124 /*! \brief Disconnect from gosa daemon.
125 */
126 public function disconnect()
127 {
128 $this->o_sock->close();
129 $this->is_connected = FALSE;
130 }
133 /*! \brief Sets an error message, which can be returned with get_error().
134 @param string The Error message,
135 */
136 private function set_error($str)
137 {
138 /******
139 Debug handling
140 ******/
141 $debug = debug_backtrace();
142 $file = __FILE__;
143 $function = __FUNCTION__;
144 $line = __LINE__;
145 $class = __CLASS__;
146 foreach($debug as $info){
147 if(!in_array($info['function'],array("send_data","_send","set_error"))){
148 $file = $info['file'];
149 $line = $info['line'];
150 $class = get_class($this);
151 $function = $info['function'];
152 break;
153 }
154 }
155 @DEBUG(GOSA_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<font color='red'><i>".htmlentities($str)."</i></font>", $info="");
157 /******
158 Set error string.
159 ******/
161 $this->b_error = TRUE;
162 $this->s_error = $str;
163 }
166 /*! \brief Sets an error message, which can be returned with get_error().
167 @param string The Error message,
168 */
169 private function reset_error()
170 {
171 $this->b_error = FALSE;
172 $this->s_error = "";
173 }
176 /*! \brief Checks if an error occured.
177 @return boolean returns TRUE or FALSE, whether there is an error or not.
178 */
179 public function is_error()
180 {
181 return($this->b_error);
182 }
185 /*! \brief Returns the last error.
186 @return Returns the last error.
187 */
188 public function get_error()
189 {
190 $str = $this->s_error;
191 $ret = "";
192 if(is_string($str)){
193 $ret = $str;
194 }else{
195 foreach($str as $msg){
196 $ret .= $msg." ";
197 }
198 }
199 $ret = preg_replace("/ /"," ",$ret);
200 return($ret);
201 }
204 public function FAI_get_kernels($release)
205 {
206 $xml_msg =
207 "<xml>".
208 "<header>gosa_get_available_kernel</header>".
209 "<source>GOSA</source>".
210 "<target>GOSA</target>".
211 "<release>".$release."</release>".
212 "</xml>";
214 $ret = array();
215 if($this->connect()){
216 $entries = $this->_send($xml_msg,TRUE);
218 /* Check if returned values represent a valid answer */
219 if(isset($entries['XML']) && is_array($entries['XML'])){
220 if(isset($entries['XML'])){
221 $ret = $entries['XML'];
222 foreach($ret as $key => $entry){
223 if(!preg_match("/^answer/i",$key)){
224 unset($ret[$key]);
225 }
226 }
227 }
228 }
229 }
230 return($ret);
231 }
234 public function FAI_get_package_sections($release)
235 {
236 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
237 "<select>distinct section</select>".
238 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
240 $ret = array();
241 if($this->connect()){
242 $entries = $this->_send($xml_msg,TRUE);
243 if(isset($entries['XML']) && is_array($entries['XML'])){
245 /* Unset header tags */
246 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
247 if(isset($entries['XML'][$type])){
248 unset($entries['XML'][$type]);
249 }
250 }
251 $ret = $entries['XML'];
252 }
253 }
254 return($ret);
255 }
258 public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
259 {
260 $ret = array();
262 /* Check Parameter */
263 if(!is_array($attrs) || !count($attrs)){
264 trigger_error("Second parameter must be an array. With at least one attribute name.");
265 return($ret);
266 }
268 /* Check Parameter */
269 if(!is_array($package)){
270 trigger_error("Third parameter must be an array. With at least one attribute name.");
271 return($ret);
272 }
274 /* Create list of attributes to fetch */
275 $attr = "";
276 foreach($attrs as $at){
277 $attr.= "<select>".$at."</select>";
278 }
280 /* If no package is given, search for all */
281 if(!count($package)) $package = array("%");
283 /* Create limit tag */
284 if($from == -1){
285 $limit ="";
286 }else{
287 $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
288 }
290 /* Create list of attributes to fetch */
291 $pkgs = "";
292 foreach($package as $pkg){
293 $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
294 }
296 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
297 $attr.
298 "<where>
299 <clause><phrase><distribution>".$release."</distribution></phrase></clause>
300 <clause><connector>OR</connector>
301 ".$pkgs."
302 </clause>
303 </where>".
304 $limit.
305 "</xml>";
307 if($this->connect()){
308 $entries = $this->_send($xml_msg,TRUE);
309 if(isset($entries['XML']) && is_array($entries['XML'])){
311 /* Check if returned values represent a valid answer */
312 if(isset($entries['XML'])){
314 /* Unset header tags */
315 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
316 if(isset($entries['XML'][$type])){
317 unset($entries['XML'][$type]);
318 }
319 }
320 $ret = $entries['XML'];
321 }
322 }
323 }
324 return($ret);
327 }
330 public function FAI_get_server($name = "")
331 {
333 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
334 $ret = array();
335 if($this->connect()){
337 /* Check if returned values represent a valid answer */
338 $entries = $this->_send($xml_msg,TRUE);
339 if(isset($entries['XML']) && is_array($entries['XML'])){
341 /* Unset header tags */
342 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
343 if(isset($entries['XML'][$type])){
344 unset($entries['XML'][$type]);
345 }
346 }
347 $ret = $entries['XML'];
348 }
349 }
350 return($ret);
351 }
354 public function FAI_get_classes($name)
355 {
356 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
357 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
358 $ret = array();
359 if($this->connect()){
361 $entries = $this->_send($xml_msg,TRUE);
362 if(isset($entries['XML']) && is_array($entries['XML'])){
364 /* Unset header tags */
365 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
366 if(isset($entries['XML'][$type])){
367 unset($entries['XML'][$type]);
368 }
369 }
370 $ret = $entries['XML'];
371 }
372 }
373 return($ret);
374 }
377 /*! \brief Returns an array containing all queued entries.
378 @return Array All queued entries as an array.
379 */
380 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
381 {
382 $ret = array();
384 $tags = "";
385 foreach($event_types as $type){
386 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
387 }
388 if(count($event_types) > 1){
389 $tags = "<connector>or</connector>".$tags;
390 }
391 if(count($event_types)){
392 $tags = "<where><clause>".$tags."</clause></where>";
393 }
395 $xml_msg =
396 "<xml>
397 <header>gosa_query_jobdb</header>
398 <target>GOSA</target>
399 <source>GOSA</source>
400 ".$tags."
402 <orderby>".$sort."</orderby>";
403 if($from != -1 && $to != -1){
404 $xml_msg.= "
405 <limit>
406 <from>".$from."</from>
407 <to>".$to."</to>
408 </limit>";
409 }
410 $xml_msg.= "
411 </xml>";
413 if($this->connect()){
414 $entries = $this->_send($xml_msg,TRUE);
415 if(isset($entries['XML']) && is_array($entries['XML'])){
417 /* Unset header tags */
418 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
419 unset($entries['XML'][$type]);
420 }
421 $ret = $entries['XML'];
422 }
423 }
424 return($ret);
425 }
428 /*! \brief Checks if the given ids are used queue ids.
429 @param Array The ids we want to check..
430 @return Array An array containing all ids as index and TRUE/FALSE as value.
431 */
432 public function ids_exist($ids)
433 {
434 if(!is_array($ids)){
435 trigger_error("Requires an array as parameter.");
436 return;
437 }
439 $ret = array();
441 $xml_msg = "<xml>
442 <header>gosa_query_jobdb</header>
443 <target>GOSA</target>
444 <source>GOSA</source>
445 <where>
446 <clause>
447 <connector>or</connector>";
448 foreach($ids as $id){
449 $xml_msg .= "<phrase>
450 <operator>eq</operator>
451 <id>".$id."</id>
452 </phrase>";
453 }
454 $xml_msg .= "</clause>
455 </where>
456 </xml>";
458 if($this->connect()){
459 $entries = $this->_send($xml_msg,TRUE);
460 if(isset($entries['XML']) && is_array($entries['XML'])){
461 foreach($entries['XML'] as $entry){
462 if(is_array($entry) && array_key_exists("ID",$entry)){
463 $ret[] = $entry['ID'];
464 }
465 }
466 }
467 }
468 return($ret);
469 }
472 /*! \brief Returns an entry containing all requested ids.
473 @param Array The IDs of the entries we want to return.
474 @return Array Of the requested entries.
475 */
476 public function get_entries_by_mac($macs)
477 {
478 if(!is_array($macs)){
479 trigger_error("Requires an array as parameter.");
480 return;
481 }
483 $ret = array();
485 $xml_msg = "<xml>
486 <header>gosa_query_jobdb</header>
487 <target>GOSA</target>
488 <source>GOSA</source>
489 <where>
490 <clause>
491 <connector>or</connector>";
492 foreach($macs as $mac){
493 $xml_msg .= "<phrase>
494 <operator>eq</operator>
495 <macaddress>".$mac."</macaddress>
496 </phrase>";
497 }
498 $xml_msg .= "</clause>
499 </where>
500 </xml>";
502 if($this->connect()){
503 $entries = $this->_send($xml_msg,TRUE);
504 if(isset($entries['XML'])){
505 foreach($entries['XML'] as $name => $entry){
506 if(preg_match("/^ANSWER[0-9]*$/",$name)){
507 $ret[$name] = $entry;
508 }
509 }
510 }
511 }
512 return($ret);
513 }
516 /*! \brief Returns an entry containing all requested ids.
517 @param Array The IDs of the entries we want to return.
518 @return Array Of the requested entries.
519 */
520 public function get_entries_by_id($ids)
521 {
522 if(!is_array($ids)){
523 trigger_error("Requires an array as parameter.");
524 return;
525 }
527 $ret = array();
529 $xml_msg = "<xml>
530 <header>gosa_query_jobdb</header>
531 <target>GOSA</target>
532 <source>GOSA</source>
533 <where>
534 <clause>
535 <connector>or</connector>";
536 foreach($ids as $id){
537 $xml_msg .= "<phrase>
538 <operator>eq</operator>
539 <id>".$id."</id>
540 </phrase>";
541 }
542 $xml_msg .= "</clause>
543 </where>
544 </xml>";
546 if($this->connect()){
547 $entries = $this->_send($xml_msg,TRUE);
548 if(isset($entries['XML'])){
549 foreach($entries['XML'] as $name => $entry){
550 if(preg_match("/^ANSWER[0-9]*$/",$name)){
551 $ret[$name] = $entry;
552 }
553 }
554 }
555 }
556 return($ret);
557 }
560 /*! \brief Checks if the given id is in use.
561 @param Integer The ID of the entry.
562 @return Boolean TRUE if entry exists.
563 */
564 public function id_exists($id)
565 {
566 if(!is_numeric($id)){
567 trigger_error("Requires an integer as parameter.");
568 return;
569 }
572 $xml_msg = "<xml>
573 <header>gosa_query_jobdb</header>
574 <target>GOSA</target>
575 <source>GOSA</source>
576 <where>
577 <clause>
578 <phrase>
579 <operator>eq</operator>
580 <id>".$id."</id>
581 </phrase>
582 </clause>
583 </where>
584 </xml>";
586 if($this->connect()){
587 $entries = $this->_send($xml_msg,TRUE);
588 if( isset($entries['XML']['HEADER']) &&
589 $entries['XML']['HEADER']=="answer" &&
590 isset($entries['XML']['ANSWER1'])){
591 return(TRUE);
592 }
593 }
594 return(FALSE);
595 }
598 /*! \brief Returns an entry from the gosaSupportQueue
599 @param Integer The ID of the entry we want to return.
600 @return Array Of the requested entry.
601 */
602 public function get_entry_by_id($id)
603 {
604 if(!is_numeric($id)){
605 trigger_error("Requires an integer as parameter.");
606 return;
607 }
609 $ret = array();
610 $xml_msg = "<xml>
611 <header>gosa_query_jobdb</header>
612 <target>GOSA</target>
613 <source>GOSA</source>
614 <where>
615 <clause>
616 <phrase>
617 <operator>eq</operator>
618 <id>".$id."</id>
619 </phrase>
620 </clause>
621 </where>
622 </xml>";
623 if($this->connect()){
624 $entries = $this->_send($xml_msg,TRUE);
625 if( isset($entries['XML']['HEADER']) &&
626 $entries['XML']['HEADER']=="answer" &&
627 isset($entries['XML']['ANSWER1'])){
628 $ret = $entries['XML']['ANSWER1'];
629 }
630 }
631 return($ret);
632 }
635 /*! \brief Removes a set of entries from the GOsa support queue.
636 @param Array The IDs to remove.
637 @return Boolean True on success.
638 */
639 public function remove_entries($ids)
640 {
641 if(!is_array($ids)){
642 trigger_error("Requires an array as parameter.");
643 return;
644 }
647 $ret = array();
649 $xml_msg = "<xml>
650 <header>gosa_delete_jobdb_entry</header>
651 <target>GOSA</target>
652 <source>GOSA</source>
653 <where>
654 <clause>
655 <connector>or</connector>";
656 foreach($ids as $id){
657 $xml_msg .= "<phrase>
658 <operator>eq</operator>
659 <id>".$id."</id>
660 </phrase>";
661 }
662 $xml_msg .= "</clause>
663 </where>
664 </xml>";
666 if($this->connect()){
667 $entries = $this->_send($xml_msg,TRUE);
668 if(isset($entries['XML']) || isset($entries['COUNT'])){
669 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
670 return(TRUE);
671 }else{
672 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
673 }
674 }
675 return(FALSE);
676 }
680 /*! \brief Removes an entry from the GOsa support queue.
681 @param Integer The ID of the entry we want to remove.
682 @return Boolean True on success.
683 */
684 public function remove_entry($id)
685 {
686 return($this->remove_entries(array($id)));
687 }
690 /*! \brief Parses the given xml string into an array
691 @param String XML string
692 @return Array Returns an array containing the xml structure.
693 */
694 private function xml_to_array($xml,$alternative_method = FALSE)
695 {
696 $params = array();
697 $level = array();
698 $parser = xml_parser_create_ns();
699 xml_parse_into_struct($parser, $xml, $vals, $index);
701 $err_id = xml_get_error_code($parser);
702 if($err_id){
703 xml_parser_free($parser);
704 }else{
705 xml_parser_free($parser);
707 if($this->use_alternative_xml_parse_method) {
708 $params = $this->build_xml_array($vals);
709 } else {
711 foreach ($vals as $xml_elem) {
712 if ($xml_elem['type'] == 'open') {
713 if (array_key_exists('attributes',$xml_elem)) {
714 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
715 } else {
716 $level[$xml_elem['level']] = $xml_elem['tag'];
717 }
718 }
719 if ($xml_elem['type'] == 'complete') {
721 $start_level = 1;
722 $test2 = &$params;
723 while($start_level < $xml_elem['level']) {
724 $test2 = &$test2[$level[$start_level]];
725 $start_level++;
726 }
728 /* Save tag attributes too.
729 e.g. <tag attr="val">
730 */
731 if(isset($xml_elem['attributes'])){
732 foreach($xml_elem['attributes'] as $name => $value){
733 $test2['ATTRIBUTES'][$name] = $value;
734 }
735 }
737 if(!isset($test2[$xml_elem['tag']])){
738 if(isset($xml_elem['value'])){
739 $test2[$xml_elem['tag']] = $xml_elem['value'];
740 }
741 }else{
742 if(!is_array($test2[$xml_elem['tag']])){
743 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
744 }
745 $test2[$xml_elem['tag']][] = $xml_elem['value'];
746 }
747 }
748 }
749 }
750 }
752 if(!isset($params['XML'])){
753 if (!array_key_exists('XML', $params)){
754 $this->set_error(_("Cannot not parse XML!"));
755 }
756 $params = array("COUNT" => 0);
757 }
759 return($params);
760 }
763 function build_xml_array(&$vals)
764 {
765 $array = array();
766 while(count($vals)){
767 $key = key($vals);
768 $val = $vals[$key];
769 unset($vals[$key]);
770 if($val['type'] == "close"){
771 return($array);
772 }elseif($val['type']=="open"){
773 $array[$val['tag']][] = $this->build_xml_array($vals);
774 }elseif($val['type'] != "cdata"){
775 $data = array("VALUE" => "","ATTRIBUTES" => "");
776 foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
777 if(isset($val[$name])){
778 $data[$attr] = $val[$name];
779 }
780 }
781 $array[$val['tag']][] = $data;
782 }else{
783 #print_a($val);
784 }
785 }
786 return($array);
787 }
794 /*! \brief Updates an entry with a set of new values,
795 @param Integer The ID of the entry, we want to update.
796 @param Array The variables to update.
797 @return Boolean Returns TRUE on success.
798 */
799 public function update_entries($ids,$data)
800 {
801 if(!is_array($ids)){
802 trigger_error("Requires an array as first parameter.");
803 return;
804 }
806 if(!is_array($data)){
807 trigger_error("Requires an array as second parameter.");
808 return;
809 }
811 $attr = "";
812 foreach($data as $key => $value){
813 $key = strtolower($key);
814 if(is_array($value)){
815 foreach($value as $sub_value){
816 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
817 }
818 }else{
819 $attr.= "<$key>".strtolower($value)."</$key>\n";
820 }
821 }
823 $xml_msg = "<xml>
824 <header>gosa_update_status_jobdb_entry</header>
825 <target>GOSA</target>
826 <source>GOSA</source>
827 <where>
828 <clause>
829 <connector>or</connector>";
830 foreach($ids as $id){
831 $xml_msg .= "<phrase>
832 <operator>eq</operator>
833 <id>".$id."</id>
834 </phrase>";
835 }
836 $xml_msg .= "</clause>
837 </where>
838 <update>
839 ".$attr."
840 </update>
841 </xml>";
843 if($this->connect()){
844 $entries = $this->_send($xml_msg,TRUE);
845 if(isset($entries['XML'])){
846 if(isset($entries['XML']['ERROR_STRING'])) {
847 $this->set_error($entries['XML']['ERROR_STRING']);
848 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
849 return(FALSE);
850 }
851 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
852 return(TRUE);
853 }
854 }
855 return(FALSE);
856 }
859 /*! \brief Returns the number of currently queued objects.
860 @return Integer
861 */
862 public function number_of_queued_entries($event_types)
863 {
864 $tags = "";
865 foreach($event_types as $type){
866 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
867 }
868 if(count($event_types) > 1){
869 $tags = "<connector>or</connector>".$tags;
870 }
871 if(count($event_types)){
872 $tags = "<where><clause>".$tags."</clause></where>";
873 }
876 $xml_msg =
877 "<xml>".
878 "<header>gosa_query_jobdb</header>".
879 "<target>GOSA</target>".
880 "<source>GOSA</source>".
881 "<select> count ID</select>".
882 $tags.
883 "</xml>";
885 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
886 $this->connect();
887 if($this->connect()){
888 $entries = $this->_send($xml_msg,TRUE);
889 if($this->o_sock->is_error()){
890 $this->set_error($this->o_sock->get_error());
891 return(0);
892 }
893 if(isset($entries['XML'])){
894 return($entries['XML']['COUNT']);
895 }
896 }
897 return(-1);
898 }
901 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
902 {
903 $xml_message= "";
905 /* Prepare data */
906 foreach ($data as $key => $value){
907 if(is_array($value)){
908 foreach($value as $sub_value){
909 $xml_message.= "<$key>$sub_value</$key>";
910 }
911 }else{
912 $xml_message.= "<$key>$value</$key>";
913 }
914 }
916 /* Multiple targets? */
917 if (!is_array($to)){
918 $to_targets= array($to);
919 } else {
920 $to_targets= $to;
921 }
923 /* Build target strings */
924 $target ="";
925 foreach($to_targets as $to){
926 $target.= "<target>$to</target>";
927 }
929 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
930 }
933 /* Allows simply appending a new DaemonEvent
934 */
935 public function append($event)
936 {
937 if(!($event instanceof DaemonEvent)){
938 return(FALSE);
939 }
942 /* Add to queue if new
943 */
944 if($event->is_new()){
946 $request_answer = FALSE;
947 if($event->get_type() == SCHEDULED_EVENT){
948 $action = $event->get_schedule_action();
949 }elseif($event->get_type() == TRIGGERED_EVENT){
950 $action = $event->get_trigger_action();
951 }else{
952 trigger_error("Unknown type of queue event given.");
953 return(FALSE);
954 }
956 /* Get event informations, like targets..
957 */
958 $targets = $event->get_targets();
959 $data = $event->save();
961 /* Append an entry for each target
962 */
963 foreach($targets as $target){
964 $data['macaddress'] = $target;
965 $this->send_data($action,$target,$data,$request_answer);
967 if($this->is_error()){
968 return(FALSE);
969 }
970 }
971 return(TRUE);
972 }else{
974 /* Updated edited entry.
975 */
976 $id = $event->get_id();
977 $data = $event->save();
978 return($this->update_entries(array($id),$data));
979 }
981 return(FALSE);
982 }
985 /*! \brief Returns an array containing all queued entries.
986 @return Array All queued entries as an array.
987 */
988 public function _send($data, $answer_expected= FALSE)
989 {
990 $this->reset_error();
991 $ret = array();
993 /******
994 Debug handling
995 ******/
996 $debug = debug_backtrace();
997 $file = __FILE__;
998 $function = __FUNCTION__;
999 $line = __LINE__;
1000 $class = __CLASS__;
1001 foreach($debug as $info){
1002 if(!in_array($info['function'],array("send_data","_send"))){
1003 $file = $info['file'];
1004 $line = $info['line'];
1005 $class = get_class($this);
1006 $function = $info['function'];
1007 break;
1008 }
1009 }
1010 @DEBUG(GOSA_SI, $line, "<b>".$class."::".$function."</b>" , $file, "<i>".htmlentities($data)."</i>", $info="");
1013 /*******
1014 Start sending data
1015 *******/
1016 if($this->connect()){
1017 $this->o_sock->write($data);
1018 if ($answer_expected){
1019 $str = trim($this->o_sock->read());
1021 /* Check if something went wrong while reading */
1022 if($this->o_sock->is_error()){
1023 $this->set_error($this->o_sock->get_error());
1024 return($ret);
1025 }
1027 $entries = $this->xml_to_array($str);
1028 if(isset($entries['XML']) && is_array($entries['XML'])){
1029 $ret = $entries;
1030 if($this->use_alternative_xml_parse_method) {
1031 if(isset($entries['XML'][0]['ERROR'][0]['VALUE']) && $entries['XML'][0]['ERROR'][0]['VALUE'] == "1"){
1032 $this->set_error($entries['XML'][0]['ERROR_STRING'][0]['VALUE']);
1033 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()",
1034 array($data=>$data),"FAILED ".$this->get_error());
1035 }
1036 }else{
1037 if(isset($entries['XML']['ERROR_STRING'])) {
1038 $this->set_error($entries['XML']['ERROR_STRING']);
1039 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()",
1040 array($data=>$data),"FAILED ".$this->get_error());
1041 }elseif(isset($entries['XML']['ERROR'])){
1042 $this->set_error($entries['XML']['ERROR']);
1043 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()",
1044 array($data=>$data),"FAILED ".$this->get_error());
1045 }
1046 }
1047 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()",
1048 array($data=>$data),"SUCCESS");
1049 }
1050 }else{
1051 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()",
1052 array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1053 }
1054 }
1055 return($ret);
1056 }
1059 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1060 {
1061 $xml_message= "";
1063 /* Get communication object */
1064 $d= new gosaSupportDaemon(TRUE,10);
1066 /* Prepare data */
1067 foreach ($data as $key => $value){
1068 if(is_array($value)){
1069 foreach($value as $sub_val){
1070 $xml_message.= "<$key>$sub_val</$key>";
1071 }
1072 }else{
1073 $xml_message.= "<$key>$value</$key>";
1074 }
1075 }
1077 /* Multiple targets? */
1078 if (!is_array($to)){
1079 $to_targets= array($to);
1080 } else {
1081 $to_targets= $to;
1082 }
1084 /* Build target strings */
1085 $target ="";
1086 foreach($to_targets as $to){
1087 $target.= "<target>$to</target>";
1088 }
1090 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1091 }
1094 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1095 @param String $mac The mac address for which we want to remove all jobs.
1096 */
1097 function clean_queue_from_mac($mac)
1098 {
1099 global $config;
1101 /* First of all we have to check which jobs are startet
1102 * for $mac
1103 */
1104 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1106 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1108 $data = $this->_send($xml_msg,TRUE);
1109 if(is_array($data) && isset($data['XML'])){
1110 $already_aborted = FALSE;
1111 foreach($data['XML'] as $name => $entry){
1112 if(preg_match("/answer[0-9]*/i",$name)){
1113 $entry['STATUS'] = strtoupper($entry['STATUS']);
1114 switch($entry['STATUS']){
1116 case 'PROCESSING' :
1118 /* Send abort event, but only once
1119 */
1120 if($already_aborted){
1121 break;
1122 }elseif(class_available("DaemonEvent_faireboot")){
1123 $already_aborted = TRUE;
1124 $tmp = new DaemonEvent_faireboot($config);
1125 $tmp->add_targets(array($mac));
1126 $tmp->set_type(TRIGGERED_EVENT);
1127 if(!$this->append($tmp)){
1128 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1129 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1130 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1131 }else{
1132 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1133 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1134 }
1135 ;break;
1136 }else{
1137 /* Couldn't find abort event, just remove entry */
1138 }
1140 case 'WAITING':
1141 case 'ERROR':
1142 default :
1144 /* Simply remove entries from queue.
1145 * Failed or waiting events, can be removed without any trouble.
1146 */
1147 if(!$this->remove_entries(array($entry['ID']))){
1148 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1149 }
1150 ;break;
1151 }
1153 }
1154 }
1155 }
1156 }
1159 static function ping($target)
1160 {
1161 if (tests::is_mac($target)){
1162 /* Get communication object */
1163 $d= new gosaSupportDaemon(TRUE,0.5);
1164 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1165 return (count($answer) ? TRUE:FALSE);
1166 }
1167 return (FALSE);
1168 }
1172 /*! \brief Returns a list of all configured principals.
1173 (Uses the GOsa support daemon instead of the ldap database.)
1174 @return Array A list containing the names of all configured principals.
1175 */
1176 public function krb5_list_principals($server)
1177 {
1178 $res = array();
1180 /* Check if the given server is a valid mac address
1181 */
1182 if(!tests::is_mac($server)){
1183 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1184 return($ret);
1185 }
1187 /* Prepare request event
1188 */
1189 $xml_msg =
1190 "<xml>".
1191 "<header>gosa_krb5_list_principals</header>".
1192 "<source>GOSA</source>".
1193 "<target>".$server."</target>".
1194 "</xml>";
1196 $tmp = $this->_send($xml_msg,TRUE);
1197 if(isset($tmp['XML']['PRINCIPAL'])){
1198 return($tmp['XML']['PRINCIPAL']);
1199 }else{
1200 return($res);
1201 }
1202 }
1205 /*! \brief Returns the configuration settings for a given principal name.
1206 (Uses the GOsa support daemon instead of the ldap database.)
1207 @pram String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1208 @return Array A list containing the names of all configured principals.
1209 */
1210 public function krb5_get_principal($server,$name)
1211 {
1212 $ret = array();
1214 /* Check if the given name is a valid request value
1215 */
1216 if(!is_string($name) || empty($name)){
1217 trigger_error("The given principal name is not of type string or it is empty.");
1218 return($ret);
1219 }
1221 /* Check if the given server is a valid mac address
1222 */
1223 if(!tests::is_mac($server)){
1224 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1225 return($ret);
1226 }
1228 /* Prepare request event
1229 */
1230 $xml_msg =
1231 "<xml>".
1232 "<header>gosa_krb5_get_principal</header>".
1233 "<principal>".$name."</principal>".
1234 "<source>GOSA</source>".
1235 "<target>".$server."</target>".
1236 "</xml>";
1238 $res = $this->_send($xml_msg,TRUE);
1239 if(isset($res['XML'])){
1240 return($res['XML']);
1241 }else{
1242 return($ret);
1243 }
1244 }
1247 /*! \brief Creates a given principal with a set of configuration settings.
1248 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1249 (Uses the GOsa support daemon instead of the ldap database.)
1250 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1251 @return Boolean TRUE on success else FALSE.
1252 */
1253 public function krb5_add_principal($server,$name,$values)
1254 {
1255 $ret = FALSE;
1257 /* Check if the given name is a valid request value
1258 */
1259 if(!is_string($name) || empty($name)){
1260 trigger_error("The given principal name is not of type string or it is empty.");
1261 return($ret);
1262 }
1263 if(!is_array($values)){
1264 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1265 return($ret);
1266 }
1268 /* Check if the given server is a valid mac address
1269 */
1270 if(!tests::is_mac($server)){
1271 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1272 return($ret);
1273 }
1275 $attrs = "";
1276 foreach($values as $key => $value){
1277 if(empty($key) || is_numeric($key)){
1278 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1279 return($ret);
1280 }
1281 $key = strtolower($key);
1282 if(is_array($value)){
1283 foreach($value as $val){
1284 $attrs.= "<$key>$val</$key>\n";
1285 }
1286 }else{
1287 $attrs.= "<$key>$value</$key>\n";
1288 }
1289 }
1291 /* Prepare request event
1292 */
1293 $xml_msg =
1294 "<xml>".
1295 "<header>gosa_krb5_create_principal</header>".
1296 "<principal>".$name."</principal>".
1297 $attrs.
1298 "<source>GOSA</source>".
1299 "<target>".$server."</target>".
1300 "</xml>";
1302 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1303 }
1306 function krb5_ramdomize_key($server,$name)
1307 {
1308 /* Prepare request event
1309 */
1310 $xml_msg =
1311 "<xml>".
1312 "<header>gosa_krb5_randomize_key</header>".
1313 "<principal>".$name."</principal>".
1314 "<source>GOSA</source>".
1315 "<target>".$server."</target>".
1316 "</xml>";
1318 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1319 }
1323 /*! \brief Updates a given principal with a set of configuration settings.
1324 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1325 (Uses the GOsa support daemon instead of the ldap database.)
1326 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1327 @return Boolean TRUE on success else FALSE.
1328 */
1329 public function krb5_set_principal($server,$name,$values)
1330 {
1331 $ret = FALSE;
1333 /* Check if the given name is a valid request value
1334 */
1335 if(!is_string($name) || empty($name)){
1336 trigger_error("The given principal name is not of type string or it is empty.");
1337 return($ret);
1338 }
1339 if(!is_array($values) || !count($values)){
1340 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1341 return($ret);
1342 }
1344 /* Check if the given server is a valid mac address
1345 */
1346 if(!tests::is_mac($server)){
1347 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1348 return($ret);
1349 }
1351 $attrs = "";
1352 foreach($values as $key => $value){
1353 if(empty($key) || is_numeric($key)){
1354 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1355 return($ret);
1356 }
1357 $key = strtolower($key);
1358 if(is_array($value)){
1359 foreach($value as $val){
1360 $attrs.= "<$key>$val</$key>\n";
1361 }
1362 }else{
1363 $attrs.= "<$key>$value</$key>\n";
1364 }
1365 }
1367 /* Prepare request event
1368 */
1369 $xml_msg =
1370 "<xml>".
1371 "<header>gosa_krb5_modify_principal</header>".
1372 "<principal>".$name."</principal>".
1373 $attrs.
1374 "<source>GOSA</source>".
1375 "<target>".$server."</target>".
1376 "</xml>";
1378 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1379 }
1382 /*! \brief Removes the given principal.
1383 (Uses the GOsa support daemon instead of the ldap database.)
1384 @pram String The name of the principal. (e.g. peter@EXAMPLE.DE)
1385 @return Boollean TRUE on success else FALSE
1386 */
1387 public function krb5_del_principal($server,$name)
1388 {
1389 $ret = FALSE;
1391 /* Check if the given name is a valid request value
1392 */
1393 if(!is_string($name) || empty($name)){
1394 trigger_error("The given principal name is not of type string or it is empty.");
1395 return($ret);
1396 }
1398 /* Check if the given server is a valid mac address
1399 */
1400 if(!tests::is_mac($server)){
1401 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1402 return($ret);
1403 }
1405 /* Prepare request event
1406 */
1407 $xml_msg =
1408 "<xml>".
1409 "<header>gosa_krb5_del_principal</header>".
1410 "<principal>".$name."</principal>".
1411 "<source>GOSA</source>".
1412 "<target>".$server."</target>".
1413 "</xml>";
1415 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1416 }
1419 /*! \brief Returns a list of configured password policies.
1420 (Uses the GOsa support daemon instead of the ldap database.)
1421 @return Array A list of all configured password policies.
1422 */
1423 public function krb5_list_policies($server)
1424 {
1425 $res = array();
1427 /* Check if the given server is a valid mac address
1428 */
1429 if(!tests::is_mac($server)){
1430 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1431 return($ret);
1432 }
1434 /* Prepare request event
1435 */
1436 $xml_msg =
1437 "<xml>".
1438 "<header>gosa_krb5_list_policies</header>".
1439 "<source>GOSA</source>".
1440 "<target>".$server."</target>".
1441 "</xml>";
1443 $res = $this->_send($xml_msg,TRUE);
1445 /* Check if there are results for POLICY
1446 */
1447 if(isset($res['XML']['POLICY'])){
1449 /* Ensure that we return an array
1450 */
1451 $tmp = $res['XML']['POLICY'];
1452 if(!is_array($tmp)){
1453 $tmp = array($tmp);
1454 }
1455 return($tmp);
1456 }else{
1457 return(array());
1458 }
1459 }
1462 /*! \brief Returns a list of configured password policies.
1463 (Uses the GOsa support daemon instead of the ldap database.)
1464 @return Array The policy settings for the given policy name.
1465 */
1466 public function krb5_get_policy($server,$name)
1467 {
1468 $res = array();
1470 /* Check if the given name is a valid request value
1471 */
1472 if(!is_string($name) || empty($name)){
1473 trigger_error("The given policy name is not of type string or it is empty.");
1474 return($ret);
1475 }
1477 /* Check if the given server is a valid mac address
1478 */
1479 if(!tests::is_mac($server)){
1480 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1481 return($ret);
1482 }
1484 /* Prepare request event
1485 */
1486 $xml_msg =
1487 "<xml>".
1488 "<header>gosa_krb5_get_policy</header>".
1489 "<policy>".$name."</policy>".
1490 "<source>GOSA</source>".
1491 "<target>".$server."</target>".
1492 "</xml>";
1494 /* Possible attributes */
1495 $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1496 "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1499 $tmp = $this->_send($xml_msg,TRUE);
1500 if(isset($tmp['XML'])){
1501 foreach($attrs as $attr){
1502 if(isset($tmp['XML'][$attr])){
1503 $ret[$attr] = $tmp['XML'][$attr];
1504 }else{
1505 $ret[$attr] = "";
1506 }
1507 }
1508 }
1509 return($ret);
1510 }
1513 /*! \brief Creates a new policy with a given set of configuration settings.
1514 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1515 (Uses the GOsa support daemon instead of the ldap database.)
1516 @pram String The name of the policy to update.
1517 @pram Array The attributes to update
1518 @return Boolean TRUE on success else FALSE.
1519 */
1520 public function krb5_add_policy($server,$name,$values)
1521 {
1522 $ret = FALSE;
1524 /* Check if the given name is a valid request value
1525 */
1526 if(!is_string($name) || empty($name)){
1527 trigger_error("The given policy name is not of type string or it is empty.");
1528 return($ret);
1529 }
1530 if(!is_array($values) || !count($values)){
1531 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1532 return($ret);
1533 }
1535 /* Check if the given server is a valid mac address
1536 */
1537 if(!tests::is_mac($server)){
1538 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1539 return($ret);
1540 }
1543 /* Transform array into <xml>
1544 */
1545 $attrs = "";
1546 foreach($values as $id => $value){
1547 if(empty($id) || is_numeric($id)){
1548 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1549 return($ret);
1550 }
1551 $id = strtolower($id);
1552 $attrs.= "<$id>$value</$id>\n";
1553 }
1555 /* Prepare request event
1556 */
1557 $xml_msg =
1558 "<xml>".
1559 "<header>gosa_krb5_create_policy</header>".
1560 "<policy>".$name."</policy>".
1561 $attrs.
1562 "<source>GOSA</source>".
1563 "<target>".$server."</target>".
1564 "</xml>";
1566 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1567 }
1570 /*! \brief Updates a given policy with a set of configuration settings.
1571 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1572 (Uses the GOsa support daemon instead of the ldap database.)
1573 @pram String The name of the policy to update.
1574 @return Boolean TRUE on success else FALSE.
1575 */
1576 public function krb5_set_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 }
1598 /* Transform array into <xml>
1599 */
1600 $attrs = "";
1601 foreach($values as $id => $value){
1602 if(preg_match("/^policy$/i",$id)) continue;
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_modify_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 Removes the given password policy.
1627 (Uses the GOsa support daemon instead of the ldap database.)
1628 @return Boolean TRUE on success else FALSE
1629 */
1630 public function krb5_del_policy($server,$name)
1631 {
1632 $ret = FALSE;
1634 /* Check if the given server is a valid mac address
1635 */
1636 if(!tests::is_mac($server)){
1637 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1638 return($ret);
1639 }
1641 /* Check if the given name is a valid request value
1642 */
1643 if(!is_string($name) || empty($name)){
1644 trigger_error("The given policy name is not of type string or it is empty.");
1645 return($ret);
1646 }
1648 /* Prepare request event
1649 */
1650 $xml_msg =
1651 "<xml>".
1652 "<header>gosa_krb5_del_policy</header>".
1653 "<policy>".$name."</policy>".
1654 "<source>GOSA</source>".
1655 "<target>".$server."</target>".
1656 "</xml>";
1657 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1658 }
1661 /*! \brief Sets the password of for the given principal.
1662 (Uses the GOsa support daemon instead of the ldap database.)
1663 @param String The servers mac
1664 @param String The principals name
1665 @param String $the new password.
1666 @return Boolean TRUE on success else FALSE
1667 */
1668 public function krb5_set_password($server,$name,$password)
1669 {
1670 $ret = FALSE;
1672 /* Check if the given server is a valid mac address
1673 */
1674 if(!tests::is_mac($server)){
1675 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1676 return($ret);
1677 }
1679 /* Check if the given name is a valid request value
1680 */
1681 if(!is_string($name) || empty($name)){
1682 trigger_error("The given principal name is not of type string or it is empty.");
1683 return($ret);
1684 }
1686 /* Prepare request event
1687 */
1688 $xml_msg =
1689 "<xml>".
1690 "<header>gosa_krb5_set_password</header>".
1691 "<principal>".$name."</principal>".
1692 "<password>".$password."</password>".
1693 "<source>GOSA</source>".
1694 "<target>".$server."</target>".
1695 "</xml>";
1696 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1697 }
1700 /*! \brief Returns log file informations for a given mac address
1701 @param $mac The mac address to fetch logs for.
1702 @retrun Array A Multidimensional array containing log infos.
1703 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1704 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1705 install_20080313_144450 ...
1706 */
1707 public function get_log_info_for_mac($mac)
1708 {
1709 $xml_msg = "
1710 <xml>
1711 <header>gosa_show_log_by_mac</header>
1712 <target>GOSA</target>
1713 <source>GOSA</source>
1714 <mac>".$mac."</mac>
1715 </xml>";
1717 $res = $this->_send($xml_msg,TRUE);
1718 $ret = array();
1719 if(isset($res['XML'])){
1721 /* Filter all entry that look like this
1722 MAC_00_01_6C_9D_B9_FA
1723 */
1724 foreach($res['XML'] as $name => $entry){
1725 if(preg_match("/^MAC/",$name)){
1727 /* Get list of available log files
1728 */
1729 foreach($entry as $log_date){
1730 $xml_msg2 = "<xml>
1731 <header>gosa_show_log_files_by_date_and_mac</header>
1732 <target>GOSA</target>
1733 <source>GOSA</source>
1734 <date>".$log_date."</date>
1735 <mac>".$mac."</mac>
1736 </xml>";
1738 $ret[$mac][$log_date] = array();
1739 $res = $this->_send($xml_msg2,TRUE);
1740 $ret[$mac][$log_date]['DATE_STR'] = $log_date;
1741 $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1742 if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1743 $ret[$mac][$log_date]['FILES'] = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1744 }
1745 }
1746 }
1747 }
1748 }
1749 return($ret);
1750 }
1752 public function get_log_file($mac,$date,$file)
1753 {
1754 $xml_msg ="
1755 <xml>
1756 <header>gosa_get_log_file_by_date_and_mac</header>
1757 <target>GOSA</target>
1758 <source>GOSA</source>
1759 <date>".$date."</date>
1760 <mac>".$mac."</mac>
1761 <log_file>".$file."</log_file>
1762 </xml>";
1764 $res = $this->_send($xml_msg,TRUE);
1765 if(isset($res['XML'][strtoupper($file)])){
1766 return(base64_decode($res['XML'][strtoupper($file)]));
1767 }
1768 return("");
1769 }
1775 /*****************
1776 * DAK - Functions
1777 *****************/
1779 /*! \brief Returns all currenlty queued entries for a given DAK repository
1780 @param ...
1781 @return Array All queued entries.
1782 */
1783 public function DAK_keyring_entries($server)
1784 {
1785 /* Ensure that we send the event to a valid mac address
1786 */
1787 if(!is_string($server) || !tests::is_mac($server)){
1788 trigger_error("No valid mac address given '".$server."'.");
1789 return;
1790 }
1792 /* Create query
1793 */
1794 $xml_msg = "<xml>
1795 <header>gosa_get_dak_keyring</header>
1796 <target>".$server."</target>
1797 <source>GOSA</source>
1798 </xml>";
1800 $res = $this->_send($xml_msg,TRUE);
1802 /* Check if there are results for POLICY
1803 */
1804 if(isset($res['XML'])){
1805 $ret = array();
1806 foreach($res['XML'] as $key => $entry){
1807 if(preg_match("/^ANSWER/",$key)){
1808 $ret[] = $entry;
1809 }
1810 }
1811 return($ret);
1812 }else{
1813 return(array());
1814 }
1815 }
1818 /*! \brief Imports the given key into the specified keyring (Servers mac address)
1819 @param String The servers mac address
1820 @param String The gpg key.
1821 @return Boolean TRUE on success else FALSE
1822 */
1823 public function DAK_import_key($server,$key)
1824 {
1825 /* Ensure that we send the event to a valid mac address
1826 */
1827 if(!is_string($server) || !tests::is_mac($server)){
1828 trigger_error("No valid mac address given '".$server."'.");
1829 return;
1830 }
1832 /* Check if there is some cleanup required before importing the key.
1833 There may be some Header lines like:
1834 -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.6 (GNU/Linux)
1835 */
1836 if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1838 /* Remove header */
1839 $key = preg_replace("/^.*\n\n/sim","",$key);
1840 /* Remove footer */
1841 $key = preg_replace("/-----.*$/sim","",$key);
1842 }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1844 /* Encode key if it is raw.
1845 */
1846 $key = base64_encode($key);
1847 }
1849 /* Create query
1850 */
1851 $xml_msg = "<xml>
1852 <header>gosa_import_dak_key</header>
1853 <target>".$server."</target>
1854 <key>".$key."</key>
1855 <source>GOSA</source>
1856 </xml>";
1858 $res = $this->_send($xml_msg,TRUE);
1859 return($this->is_error());
1860 }
1863 /*! \brief Removes a key from the keyring on the given server.
1864 @param String The servers mac address
1865 @param String The gpg key uid.
1866 @return Boolean TRUE on success else FALSE
1867 */
1868 public function DAK_remove_key($server,$key)
1869 {
1870 /* Ensure that we send the event to a valid mac address
1871 */
1872 if(!is_string($server) || !tests::is_mac($server)){
1873 trigger_error("No valid mac address given '".$server."'.");
1874 return;
1875 }
1877 /* Create query
1878 */
1879 $xml_msg = "<xml>
1880 <header>gosa_remove_dak_key</header>
1881 <target>".$server."</target>
1882 <keyid>".$key."</keyid>
1883 <source>GOSA</source>
1884 </xml>";
1886 $res = $this->_send($xml_msg,TRUE);
1887 return($this->is_error());
1888 }
1889 }
1891 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1892 ?>