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