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 }
104 /*! \brief Disconnect from gosa daemon.
105 */
106 public function disconnect()
107 {
108 $this->o_sock->close();
109 $this->is_connected = FALSE;
110 }
113 /*! \brief Sets an error message, which can be returned with get_error().
114 @param string The Error message,
115 */
116 private function set_error($str)
117 {
118 $this->b_error = TRUE;
119 $this->s_error = $str;
120 }
123 /*! \brief Sets an error message, which can be returned with get_error().
124 @param string The Error message,
125 */
126 private function reset_error()
127 {
128 $this->b_error = FALSE;
129 $this->s_error = "";
130 }
133 /*! \brief Checks if an error occured.
134 @return boolean returns TRUE or FALSE, whether there is an error or not.
135 */
136 public function is_error()
137 {
138 return($this->b_error);
139 }
142 /*! \brief Returns the last error.
143 @return Returns the last error.
144 */
145 public function get_error()
146 {
147 $str = $this->s_error;
148 $ret = "";
149 if(is_string($str)){
150 $ret = $str;
151 }else{
152 foreach($str as $msg){
153 $ret .= $msg." ";
154 }
155 }
156 $ret = preg_replace("/ /"," ",$ret);
157 return($ret);
158 }
161 public function FAI_get_kernels($release)
162 {
163 $xml_msg =
164 "<xml>".
165 "<header>gosa_get_available_kernel</header>".
166 "<source>GOSA</source>".
167 "<target>GOSA</target>".
168 "<release>".$release."</release>".
169 "</xml>";
171 $ret = array();
172 if($this->connect()){
173 $this->o_sock->write($xml_msg);
174 $str = trim($this->o_sock->read());
176 /* Check if something went wrong while reading */
177 if($this->o_sock->is_error()){
178 $this->set_error($this->o_sock->get_error());
179 return($ret);
180 }
182 $entries = $this->xml_to_array($str);
183 if(isset($entries['XML']) && is_array($entries['XML'])){
185 /* Check if returned values represent a valid answer */
186 if(isset($entries['XML'])){
187 if(isset($entries['XML']['ERROR_STRING'])) {
188 $this->set_error($entries['XML']['ERROR_STRING']);
189 new log("debug","GOsa-si",
190 get_class($this)."::".__FUNCTION__, array(),
191 "FAILED error was ".$this->get_error());
192 return($ret);
193 }
195 /* Unset header tags */
196 $ret = $entries['XML'];
197 foreach($ret as $key => $entry){
198 if(!preg_match("/^answer/i",$key)){
199 unset($ret[$key]);
200 }
201 }
202 }
203 }
204 }
205 return($ret);
206 }
209 public function FAI_get_package_sections($release)
210 {
211 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
212 "<select>distinct section</select>".
213 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
215 $ret = array();
216 if($this->connect()){
217 $this->o_sock->write($xml_msg);
218 $str = trim($this->o_sock->read());
220 /* Check if something went wrong while reading */
221 if($this->o_sock->is_error()){
222 $this->set_error($this->o_sock->get_error());
223 return($ret);
224 }
226 $entries = $this->xml_to_array($str);
227 if(isset($entries['XML']) && is_array($entries['XML'])){
229 /* Check if returned values represent a valid answer */
230 if(isset($entries['XML'])){
231 if(isset($entries['XML']['ERROR_STRING'])) {
232 $this->set_error($entries['XML']['ERROR_STRING']);
233 new log("debug","GOsa-si",
234 get_class($this)."::".__FUNCTION__, array(),
235 "FAILED error was ".$this->get_error());
236 return($ret);
237 }
239 /* Unset header tags */
240 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
241 if(isset($entries['XML'][$type])){
242 unset($entries['XML'][$type]);
243 }
244 }
245 $ret = $entries['XML'];
246 }
247 }
248 }
249 return($ret);
250 }
253 public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
254 {
255 $this->reset_error();
256 $ret = array();
258 /* Check Parameter */
259 if(!is_array($attrs) || !count($attrs)){
260 trigger_error("Second parameter must be an array. With at least one attribute name.");
261 return($ret);
262 }
264 /* Check Parameter */
265 if(!is_array($package)){
266 trigger_error("Third parameter must be an array. With at least one attribute name.");
267 return($ret);
268 }
270 /* Create list of attributes to fetch */
271 $attr = "";
272 foreach($attrs as $at){
273 $attr.= "<select>".$at."</select>";
274 }
276 /* If no package is given, search for all */
277 if(!count($package)) $package = array("%");
279 /* Create limit tag */
280 if($from == -1){
281 $limit ="";
282 }else{
283 $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
284 }
286 /* Create list of attributes to fetch */
287 $pkgs = "";
288 foreach($package as $pkg){
289 $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
290 }
292 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
293 $attr.
294 "<where>
295 <clause><phrase><distribution>".$release."</distribution></phrase></clause>
296 <clause><connector>OR</connector>
297 ".$pkgs."
298 </clause>
299 </where>".
300 $limit.
301 "</xml>";
303 if($this->connect()){
304 $this->o_sock->write($xml_msg);
305 $str = trim($this->o_sock->read());
307 /* Check if something went wrong while reading */
308 if($this->o_sock->is_error()){
309 $this->set_error($this->o_sock->get_error());
310 return($ret);
311 }
313 $entries = $this->xml_to_array($str);
314 if(isset($entries['XML']) && is_array($entries['XML'])){
316 /* Check if returned values represent a valid answer */
317 if(isset($entries['XML'])){
318 if(isset($entries['XML']['ERROR_STRING'])) {
319 $this->set_error($entries['XML']['ERROR_STRING']);
320 new log("debug","GOsa-si",
321 get_class($this)."::".__FUNCTION__, array(),
322 "FAILED error was ".$this->get_error());
323 return($ret);
324 }
326 /* Unset header tags */
327 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
328 if(isset($entries['XML'][$type])){
329 unset($entries['XML'][$type]);
330 }
331 }
332 $ret = $entries['XML'];
333 }
334 }
335 }
336 return($ret);
339 }
342 public function FAI_get_server($name = "")
343 {
344 $this->reset_error();
346 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
347 $ret = array();
348 if($this->connect()){
349 $this->o_sock->write($xml_msg);
350 $str = trim($this->o_sock->read());
352 /* Check if something went wrong while reading */
353 if($this->o_sock->is_error()){
354 $this->set_error($this->o_sock->get_error());
355 return($ret);
356 }
358 $entries = $this->xml_to_array($str);
359 if(isset($entries['XML']) && is_array($entries['XML'])){
361 /* Check if returned values represent a valid answer */
362 if(isset($entries['XML'])){
363 if(isset($entries['XML']['ERROR_STRING'])) {
364 $this->set_error($entries['XML']['ERROR_STRING']);
365 new log("debug","GOsa-si",
366 get_class($this)."::".__FUNCTION__, array(),
367 "FAILED error was ".$this->get_error());
368 return($ret);
369 }
371 /* Unset header tags */
372 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
373 if(isset($entries['XML'][$type])){
374 unset($entries['XML'][$type]);
375 }
376 }
377 $ret = $entries['XML'];
378 }
379 }
380 }
381 return($ret);
382 }
385 public function FAI_get_classes($name)
386 {
387 $this->reset_error();
388 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
389 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
390 $ret = array();
391 if($this->connect()){
392 $this->o_sock->write($xml_msg);
393 $str = trim($this->o_sock->read());
395 /* Check if something went wrong while reading */
396 if($this->o_sock->is_error()){
397 $this->set_error($this->o_sock->get_error());
398 return($ret);
399 }
401 $entries = $this->xml_to_array($str);
402 if(isset($entries['XML']) && is_array($entries['XML'])){
404 /* Check if returned values represent a valid answer */
405 if(isset($entries['XML'])){
406 if(isset($entries['XML']['ERROR_STRING'])) {
407 $this->set_error($entries['XML']['ERROR_STRING']);
408 new log("debug","GOsa-si",
409 get_class($this)."::".__FUNCTION__, array($name),
410 "FAILED error was ".$this->get_error());
411 return($ret);
412 }
414 /* Unset header tags */
415 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
416 if(isset($entries['XML'][$type])){
417 unset($entries['XML'][$type]);
418 }
419 }
420 $ret = $entries['XML'];
421 }
422 }
423 }
424 return($ret);
425 }
428 /*! \brief Returns an array containing all queued entries.
429 @return Array All queued entries as an array.
430 */
431 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
432 {
433 $this->reset_error();
434 $ret = array();
436 $tags = "";
437 foreach($event_types as $type){
438 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
439 }
440 if(count($event_types) > 1){
441 $tags = "<connector>or</connector>".$tags;
442 }
443 if(count($event_types)){
444 $tags = "<where><clause>".$tags."</clause></where>";
445 }
447 $xml_msg =
448 "<xml>
449 <header>gosa_query_jobdb</header>
450 <target>GOSA</target>
451 <source>GOSA</source>
452 ".$tags."
454 <orderby>".$sort."</orderby>";
455 if($from != -1 && $to != -1){
456 $xml_msg.= "
457 <limit>
458 <from>".$from."</from>
459 <to>".$to."</to>
460 </limit>";
461 }
462 $xml_msg.= "
463 </xml>";
465 if($this->connect()){
466 $this->o_sock->write($xml_msg);
467 $str = trim($this->o_sock->read());
469 /* Check if something went wrong while reading */
470 if($this->o_sock->is_error()){
471 $this->set_error($this->o_sock->get_error());
472 return($ret);
473 }
475 $entries = $this->xml_to_array($str);
476 if(isset($entries['XML']) && is_array($entries['XML'])){
478 /* Check if returned values represent a valid answer */
479 if(isset($entries['XML'])){
481 /* Unset header tags */
482 foreach(array("HEADER","SOURCE","TARGET") as $type){
483 unset($entries['XML'][$type]);
484 }
485 $ret = $entries['XML'];
486 }
487 }
488 }
490 /* Remove session ID. No one is interested in this... */
491 unset($ret['SESSION_ID']);
493 return($ret);
494 }
497 /*! \brief Checks if the given ids are used queue ids.
498 @param Array The ids we want to check..
499 @return Array An array containing all ids as index and TRUE/FALSE as value.
500 */
501 public function ids_exist($ids)
502 {
503 if(!is_array($ids)){
504 trigger_error("Requires an array as parameter.");
505 return;
506 }
507 $this->reset_error();
509 $ret = array();
511 $xml_msg = "<xml>
512 <header>gosa_query_jobdb</header>
513 <target>GOSA</target>
514 <source>GOSA</source>
515 <where>
516 <clause>
517 <connector>or</connector>";
518 foreach($ids as $id){
519 $xml_msg .= "<phrase>
520 <operator>eq</operator>
521 <id>".$id."</id>
522 </phrase>";
523 }
524 $xml_msg .= "</clause>
525 </where>
526 </xml>";
528 if($this->connect()){
529 $this->o_sock->write($xml_msg);
530 $str = trim($this->o_sock->read());
532 /* Check if something went wrong while reading */
533 if($this->o_sock->is_error()){
534 $this->set_error($this->o_sock->get_error());
535 return($ret);
536 }
538 $entries = $this->xml_to_array($str);
539 if(isset($entries['XML']) && is_array($entries['XML'])){
540 foreach($entries['XML'] as $entry){
541 if(is_array($entry) && array_key_exists("ID",$entry)){
542 $ret[] = $entry['ID'];
543 }
544 }
545 }
546 }
547 return($ret);
548 }
551 /*! \brief Returns an entry containing all requested ids.
552 @param Array The IDs of the entries we want to return.
553 @return Array Of the requested entries.
554 */
555 public function get_entries_by_mac($macs)
556 {
557 if(!is_array($macs)){
558 trigger_error("Requires an array as parameter.");
559 return;
560 }
561 $this->reset_error();
563 $ret = array();
565 $xml_msg = "<xml>
566 <header>gosa_query_jobdb</header>
567 <target>GOSA</target>
568 <source>GOSA</source>
569 <where>
570 <clause>
571 <connector>or</connector>";
572 foreach($macs as $mac){
573 $xml_msg .= "<phrase>
574 <operator>eq</operator>
575 <macaddress>".$mac."</macaddress>
576 </phrase>";
577 }
578 $xml_msg .= "</clause>
579 </where>
580 </xml>";
582 if($this->connect()){
583 $this->o_sock->write($xml_msg);
584 $str = trim($this->o_sock->read());
586 /* Check if something went wrong while reading */
587 if($this->o_sock->is_error()){
588 $this->set_error($this->o_sock->get_error());
589 return($ret);
590 }
592 $entries = $this->xml_to_array($str);
593 if(isset($entries['XML'])){
594 foreach($entries['XML'] as $name => $entry){
595 if(preg_match("/^ANSWER[0-9]*$/",$name)){
596 $ret[$name] = $entry;
597 }
598 }
599 }
600 }
601 return($ret);
602 }
605 /*! \brief Returns an entry containing all requested ids.
606 @param Array The IDs of the entries we want to return.
607 @return Array Of the requested entries.
608 */
609 public function get_entries_by_id($ids)
610 {
611 if(!is_array($ids)){
612 trigger_error("Requires an array as parameter.");
613 return;
614 }
615 $this->reset_error();
617 $ret = array();
619 $xml_msg = "<xml>
620 <header>gosa_query_jobdb</header>
621 <target>GOSA</target>
622 <source>GOSA</source>
623 <where>
624 <clause>
625 <connector>or</connector>";
626 foreach($ids as $id){
627 $xml_msg .= "<phrase>
628 <operator>eq</operator>
629 <id>".$id."</id>
630 </phrase>";
631 }
632 $xml_msg .= "</clause>
633 </where>
634 </xml>";
636 if($this->connect()){
637 $this->o_sock->write($xml_msg);
638 $str = trim($this->o_sock->read());
640 /* Check if something went wrong while reading */
641 if($this->o_sock->is_error()){
642 $this->set_error($this->o_sock->get_error());
643 return($ret);
644 }
646 $entries = $this->xml_to_array($str);
647 if(isset($entries['XML'])){
648 foreach($entries['XML'] as $name => $entry){
649 if(preg_match("/^ANSWER[0-9]*$/",$name)){
650 $ret[$name] = $entry;
651 }
652 }
653 }
654 }
655 return($ret);
656 }
659 /*! \brief Checks if the given id is in use.
660 @param Integer The ID of the entry.
661 @return Boolean TRUE if entry exists.
662 */
663 public function id_exists($id)
664 {
665 if(!is_numeric($id)){
666 trigger_error("Requires an integer as parameter.");
667 return;
668 }
670 $this->reset_error();
672 $xml_msg = "<xml>
673 <header>gosa_query_jobdb</header>
674 <target>GOSA</target>
675 <source>GOSA</source>
676 <where>
677 <clause>
678 <phrase>
679 <operator>eq</operator>
680 <id>".$id."</id>
681 </phrase>
682 </clause>
683 </where>
684 </xml>";
686 if($this->connect()){
687 $this->o_sock->write($xml_msg);
688 $str = trim($this->o_sock->read());
690 /* Check if something went wrong while reading */
691 if($this->o_sock->is_error()){
692 $this->set_error($this->o_sock->get_error());
693 return(FALSE);
694 }
696 $entries = $this->xml_to_array($str);
697 if( isset($entries['XML']['HEADER']) &&
698 $entries['XML']['HEADER']=="answer" &&
699 isset($entries['XML']['ANSWER1'])){
700 return(TRUE);
701 }
702 }
703 return(FALSE);
704 }
707 /*! \brief Returns an entry from the gosaSupportQueue
708 @param Integer The ID of the entry we want to return.
709 @return Array Of the requested entry.
710 */
711 public function get_entry_by_id($id)
712 {
713 if(!is_numeric($id)){
714 trigger_error("Requires an integer as parameter.");
715 return;
716 }
717 $this->reset_error();
719 $ret = array();
720 $xml_msg = "<xml>
721 <header>gosa_query_jobdb</header>
722 <target>GOSA</target>
723 <source>GOSA</source>
724 <where>
725 <clause>
726 <phrase>
727 <operator>eq</operator>
728 <id>".$id."</id>
729 </phrase>
730 </clause>
731 </where>
732 </xml>";
733 if($this->connect()){
734 $this->o_sock->write($xml_msg);
735 $str = trim($this->o_sock->read());
737 /* Check if something went wrong while reading */
738 if($this->o_sock->is_error()){
739 $this->set_error($this->o_sock->get_error());
740 return($ret);
741 }
743 $entries = $this->xml_to_array($str);
744 if( isset($entries['XML']['HEADER']) &&
745 $entries['XML']['HEADER']=="answer" &&
746 isset($entries['XML']['ANSWER1'])){
747 $ret = $entries['XML']['ANSWER1'];
748 }
749 }
750 return($ret);
751 }
754 /*! \brief Removes a set of entries from the GOsa support queue.
755 @param Array The IDs to remove.
756 @return Boolean True on success.
757 */
758 public function remove_entries($ids)
759 {
760 if(!is_array($ids)){
761 trigger_error("Requires an array as parameter.");
762 return;
763 }
765 $this->reset_error();
767 $ret = array();
769 $xml_msg = "<xml>
770 <header>gosa_delete_jobdb_entry</header>
771 <target>GOSA</target>
772 <source>GOSA</source>
773 <where>
774 <clause>
775 <connector>or</connector>";
776 foreach($ids as $id){
777 $xml_msg .= "<phrase>
778 <operator>eq</operator>
779 <id>".$id."</id>
780 </phrase>";
781 }
782 $xml_msg .= "</clause>
783 </where>
784 </xml>";
786 if($this->connect()){
787 $this->o_sock->write($xml_msg);
788 $str = $this->o_sock->read();
790 /* Check if something went wrong while reading */
791 if($this->o_sock->is_error()){
792 $this->set_error($this->o_sock->get_error());
793 return($ret);
794 }
796 $entries = $this->xml_to_array($str);
797 if(isset($entries['XML']) || isset($entries['COUNT'])){
798 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
799 return(TRUE);
800 }else{
801 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
802 }
803 }
804 return(FALSE);
805 }
809 /*! \brief Removes an entry from the GOsa support queue.
810 @param Integer The ID of the entry we want to remove.
811 @return Boolean True on success.
812 */
813 public function remove_entry($id)
814 {
815 return($this->remove_entries(array($id)));
816 }
819 /*! \brief Parses the given xml string into an array
820 @param String XML string
821 @return Array Returns an array containing the xml structure.
822 */
823 private function xml_to_array($xml,$alternative_method = FALSE)
824 {
825 $params = array();
826 $level = array();
827 $parser = xml_parser_create_ns();
828 xml_parse_into_struct($parser, $xml, $vals, $index);
830 $err_id = xml_get_error_code($parser);
831 if($err_id){
832 xml_parser_free($parser);
833 }else{
834 xml_parser_free($parser);
836 if($this->use_alternative_xml_parse_method) {
837 $params = $this->build_xml_array($vals);
838 } else {
840 foreach ($vals as $xml_elem) {
841 if ($xml_elem['type'] == 'open') {
842 if (array_key_exists('attributes',$xml_elem)) {
843 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
844 } else {
845 $level[$xml_elem['level']] = $xml_elem['tag'];
846 }
847 }
848 if ($xml_elem['type'] == 'complete') {
850 $start_level = 1;
851 $test2 = &$params;
852 while($start_level < $xml_elem['level']) {
853 $test2 = &$test2[$level[$start_level]];
854 $start_level++;
855 }
857 /* Save tag attributes too.
858 e.g. <tag attr="val">
859 */
860 if(isset($xml_elem['attributes'])){
861 foreach($xml_elem['attributes'] as $name => $value){
862 $test2['ATTRIBUTES'][$name] = $value;
863 }
864 }
866 if(!isset($test2[$xml_elem['tag']])){
867 if(isset($xml_elem['value'])){
868 $test2[$xml_elem['tag']] = $xml_elem['value'];
869 }
870 }else{
871 if(!is_array($test2[$xml_elem['tag']])){
872 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
873 }
874 $test2[$xml_elem['tag']][] = $xml_elem['value'];
875 }
876 }
877 }
878 }
879 }
881 if(!isset($params['XML'])){
882 if (!array_key_exists('XML', $params)){
883 $this->set_error(_("Cannot not parse XML!"));
884 }
885 $params = array("COUNT" => 0);
886 }
888 return($params);
889 }
892 function build_xml_array(&$vals)
893 {
894 $array = array();
895 while(count($vals)){
896 $key = key($vals);
897 $val = $vals[$key];
898 unset($vals[$key]);
899 if($val['type'] == "close"){
900 return($array);
901 }elseif($val['type']=="open"){
902 $array[$val['tag']][] = $this->build_xml_array($vals);
903 }elseif($val['type'] != "cdata"){
904 $data = array("VALUE" => "","ATTRIBUTES" => "");
905 foreach(array("value" => "VALUE", "attributes" => "ATTRIBUTES") as $name => $attr){
906 if(isset($val[$name])){
907 $data[$attr] = $val[$name];
908 }
909 }
910 $array[$val['tag']][] = $data;
911 }else{
912 #print_a($val);
913 }
914 }
915 return($array);
916 }
923 /*! \brief Updates an entry with a set of new values,
924 @param Integer The ID of the entry, we want to update.
925 @param Array The variables to update.
926 @return Boolean Returns TRUE on success.
927 */
928 public function update_entries($ids,$data)
929 {
930 $this->reset_error();
931 if(!is_array($ids)){
932 trigger_error("Requires an array as first parameter.");
933 return;
934 }
936 if(!is_array($data)){
937 trigger_error("Requires an array as second parameter.");
938 return;
939 }
941 $attr = "";
942 foreach($data as $key => $value){
943 $key = strtolower($key);
944 if(is_array($value)){
945 foreach($value as $sub_value){
946 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
947 }
948 }else{
949 $attr.= "<$key>".strtolower($value)."</$key>\n";
950 }
951 }
953 $xml_msg = "<xml>
954 <header>gosa_update_status_jobdb_entry</header>
955 <target>GOSA</target>
956 <source>GOSA</source>
957 <where>
958 <clause>
959 <connector>or</connector>";
960 foreach($ids as $id){
961 $xml_msg .= "<phrase>
962 <operator>eq</operator>
963 <id>".$id."</id>
964 </phrase>";
965 }
966 $xml_msg .= "</clause>
967 </where>
968 <update>
969 ".$attr."
970 </update>
971 </xml>";
973 if($this->connect()){
975 $this->o_sock->write($xml_msg);
976 $str = trim($this->o_sock->read());
978 /* Check if something went wrong while reading */
979 if($this->o_sock->is_error()){
980 $this->set_error($this->o_sock->get_error());
981 return(FALSE);
982 }
984 $entries = $this->xml_to_array($str);
985 if(isset($entries['XML'])){
986 if(isset($entries['XML']['ERROR_STRING'])) {
987 $this->set_error($entries['XML']['ERROR_STRING']);
988 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
989 return(FALSE);
990 }
991 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
992 return(TRUE);
993 }
994 }
995 return(FALSE);
996 }
999 /*! \brief Returns the number of currently queued objects.
1000 @return Integer
1001 */
1002 public function number_of_queued_entries($event_types)
1003 {
1004 $tags = "";
1005 foreach($event_types as $type){
1006 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
1007 }
1008 if(count($event_types) > 1){
1009 $tags = "<connector>or</connector>".$tags;
1010 }
1011 if(count($event_types)){
1012 $tags = "<where><clause>".$tags."</clause></where>";
1013 }
1016 $xml_msg =
1017 "<xml>".
1018 "<header>gosa_query_jobdb</header>".
1019 "<target>GOSA</target>".
1020 "<source>GOSA</source>".
1021 "<select> count ID</select>".
1022 $tags.
1023 "</xml>";
1025 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
1026 $this->connect();
1027 if($this->connect()){
1028 $this->o_sock->write($xml_msg);
1029 $str = trim($this->o_sock->read());
1031 /* Check if something went wrong while reading */
1032 if($this->o_sock->is_error()){
1033 $this->set_error($this->o_sock->get_error());
1034 return(0);
1035 }
1037 $entries = $this->xml_to_array($str);
1038 if(isset($entries['XML'])){
1039 return($entries['XML']['COUNT']);
1040 }
1041 }
1042 return(-1);
1043 }
1046 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
1047 {
1048 $xml_message= "";
1050 /* Prepare data */
1051 foreach ($data as $key => $value){
1052 if(is_array($value)){
1053 foreach($value as $sub_value){
1054 $xml_message.= "<$key>$sub_value</$key>";
1055 }
1056 }else{
1057 $xml_message.= "<$key>$value</$key>";
1058 }
1059 }
1061 /* Multiple targets? */
1062 if (!is_array($to)){
1063 $to_targets= array($to);
1064 } else {
1065 $to_targets= $to;
1066 }
1068 /* Build target strings */
1069 $target ="";
1070 foreach($to_targets as $to){
1071 $target.= "<target>$to</target>";
1072 }
1074 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1075 }
1078 /* Allows simply appending a new DaemonEvent
1079 */
1080 public function append($event)
1081 {
1082 if(!($event instanceof DaemonEvent)){
1083 return(FALSE);
1084 }
1086 $this->reset_error();
1088 /* Add to queue if new
1089 */
1090 if($event->is_new()){
1092 $request_answer = FALSE;
1093 if($event->get_type() == SCHEDULED_EVENT){
1094 $action = $event->get_schedule_action();
1095 }elseif($event->get_type() == TRIGGERED_EVENT){
1096 $action = $event->get_trigger_action();
1097 }else{
1098 trigger_error("Unknown type of queue event given.");
1099 return(FALSE);
1100 }
1102 /* Get event informations, like targets..
1103 */
1104 $targets = $event->get_targets();
1105 $data = $event->save();
1107 /* Append an entry for each target
1108 */
1109 foreach($targets as $target){
1110 $data['macaddress'] = $target;
1111 $this->send_data($action,$target,$data,$request_answer);
1113 if($this->is_error()){
1114 return(FALSE);
1115 }
1116 }
1117 return(TRUE);
1118 }else{
1120 /* Updated edited entry.
1121 */
1122 $id = $event->get_id();
1123 $data = $event->save();
1124 return($this->update_entries(array($id),$data));
1125 }
1127 return(FALSE);
1128 }
1131 /*! \brief Returns an array containing all queued entries.
1132 @return Array All queued entries as an array.
1133 */
1134 public function _send($data, $answer_expected= FALSE)
1135 {
1136 $this->reset_error();
1137 $ret = array();
1139 if($this->connect()){
1140 $this->o_sock->write($data);
1141 if ($answer_expected){
1142 $str = trim($this->o_sock->read());
1144 /* Check if something went wrong while reading */
1145 if($this->o_sock->is_error()){
1146 $this->set_error($this->o_sock->get_error());
1147 return($ret);
1148 }
1150 $entries = $this->xml_to_array($str);
1151 if(isset($entries['XML']) && is_array($entries['XML'])){
1152 $ret = $entries;
1153 if(isset($entries['XML']['ERROR_STRING'])) {
1154 $this->set_error($entries['XML']['ERROR_STRING']);
1155 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1156 }elseif(isset($entries['XML']['ERROR'])){
1157 $this->set_error($entries['XML']['ERROR']);
1158 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1159 }else{
1160 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1161 }
1162 }
1163 }else{
1164 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1165 }
1166 }
1167 return($ret);
1168 }
1171 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1172 {
1173 $xml_message= "";
1175 /* Get communication object */
1176 $d= new gosaSupportDaemon(TRUE,10);
1178 /* Prepare data */
1179 foreach ($data as $key => $value){
1180 if(is_array($value)){
1181 foreach($value as $sub_val){
1182 $xml_message.= "<$key>$sub_val</$key>";
1183 }
1184 }else{
1185 $xml_message.= "<$key>$value</$key>";
1186 }
1187 }
1189 /* Multiple targets? */
1190 if (!is_array($to)){
1191 $to_targets= array($to);
1192 } else {
1193 $to_targets= $to;
1194 }
1196 /* Build target strings */
1197 $target ="";
1198 foreach($to_targets as $to){
1199 $target.= "<target>$to</target>";
1200 }
1202 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1203 }
1206 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1207 @param String $mac The mac address for which we want to remove all jobs.
1208 */
1209 function clean_queue_from_mac($mac)
1210 {
1211 global $config;
1213 /* First of all we have to check which jobs are startet
1214 * for $mac
1215 */
1216 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1218 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1220 $data = $this->_send($xml_msg,TRUE);
1221 if(is_array($data) && isset($data['XML'])){
1222 $already_aborted = FALSE;
1223 foreach($data['XML'] as $name => $entry){
1224 if(preg_match("/answer[0-9]*/i",$name)){
1225 $entry['STATUS'] = strtoupper($entry['STATUS']);
1226 switch($entry['STATUS']){
1228 case 'PROCESSING' :
1230 /* Send abort event, but only once
1231 */
1232 if($already_aborted){
1233 break;
1234 }elseif(class_available("DaemonEvent_faireboot")){
1235 $already_aborted = TRUE;
1236 $tmp = new DaemonEvent_faireboot($config);
1237 $tmp->add_targets(array($mac));
1238 $tmp->set_type(TRIGGERED_EVENT);
1239 if(!$this->append($tmp)){
1240 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1241 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1242 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1243 }else{
1244 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1245 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1246 }
1247 ;break;
1248 }else{
1249 /* Couldn't find abort event, just remove entry */
1250 }
1252 case 'WAITING':
1253 case 'ERROR':
1254 default :
1256 /* Simply remove entries from queue.
1257 * Failed or waiting events, can be removed without any trouble.
1258 */
1259 if(!$this->remove_entries(array($entry['ID']))){
1260 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1261 }
1262 ;break;
1263 }
1265 }
1266 }
1267 }
1268 }
1271 static function ping($target)
1272 {
1273 if (tests::is_mac($target)){
1274 /* Get communication object */
1275 $d= new gosaSupportDaemon(TRUE,0.5);
1276 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1277 return (count($answer) ? TRUE:FALSE);
1278 }
1279 return (FALSE);
1280 }
1284 /*! \brief Returns a list of all configured principals.
1285 (Uses the GOsa support daemon instead of the ldap database.)
1286 @return Array A list containing the names of all configured principals.
1287 */
1288 public function krb5_list_principals($server)
1289 {
1290 $res = array();
1292 /* Check if the given server is a valid mac address
1293 */
1294 if(!tests::is_mac($server)){
1295 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1296 return($ret);
1297 }
1299 /* Prepare request event
1300 */
1301 $xml_msg =
1302 "<xml>".
1303 "<header>gosa_krb5_list_principals</header>".
1304 "<source>GOSA</source>".
1305 "<target>".$server."</target>".
1306 "</xml>";
1308 $tmp = $this->_send($xml_msg,TRUE);
1309 if(isset($tmp['XML']['PRINCIPAL'])){
1310 return($tmp['XML']['PRINCIPAL']);
1311 }else{
1312 return($res);
1313 }
1314 }
1317 /*! \brief Returns the configuration settings for a given principal name.
1318 (Uses the GOsa support daemon instead of the ldap database.)
1319 @pram String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1320 @return Array A list containing the names of all configured principals.
1321 */
1322 public function krb5_get_principal($server,$name)
1323 {
1324 $ret = array();
1326 /* Check if the given name is a valid request value
1327 */
1328 if(!is_string($name) || empty($name)){
1329 trigger_error("The given principal name is not of type string or it is empty.");
1330 return($ret);
1331 }
1333 /* Check if the given server is a valid mac address
1334 */
1335 if(!tests::is_mac($server)){
1336 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1337 return($ret);
1338 }
1340 /* Prepare request event
1341 */
1342 $xml_msg =
1343 "<xml>".
1344 "<header>gosa_krb5_get_principal</header>".
1345 "<principal>".$name."</principal>".
1346 "<source>GOSA</source>".
1347 "<target>".$server."</target>".
1348 "</xml>";
1350 $res = $this->_send($xml_msg,TRUE);
1351 if(isset($res['XML'])){
1352 return($res['XML']);
1353 }else{
1354 return($ret);
1355 }
1356 }
1359 /*! \brief Creates a given principal with a set of configuration settings.
1360 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1361 (Uses the GOsa support daemon instead of the ldap database.)
1362 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1363 @return Boolean TRUE on success else FALSE.
1364 */
1365 public function krb5_add_principal($server,$name,$values)
1366 {
1367 $ret = FALSE;
1369 /* Check if the given name is a valid request value
1370 */
1371 if(!is_string($name) || empty($name)){
1372 trigger_error("The given principal name is not of type string or it is empty.");
1373 return($ret);
1374 }
1375 if(!is_array($values)){
1376 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1377 return($ret);
1378 }
1380 /* Check if the given server is a valid mac address
1381 */
1382 if(!tests::is_mac($server)){
1383 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1384 return($ret);
1385 }
1387 $attrs = "";
1388 foreach($values as $key => $value){
1389 if(empty($key) || is_numeric($key)){
1390 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1391 return($ret);
1392 }
1393 $key = strtolower($key);
1394 if(is_array($value)){
1395 foreach($value as $val){
1396 $attrs.= "<$key>$val</$key>\n";
1397 }
1398 }else{
1399 $attrs.= "<$key>$value</$key>\n";
1400 }
1401 }
1403 /* Prepare request event
1404 */
1405 $xml_msg =
1406 "<xml>".
1407 "<header>gosa_krb5_create_principal</header>".
1408 "<principal>".$name."</principal>".
1409 $attrs.
1410 "<source>GOSA</source>".
1411 "<target>".$server."</target>".
1412 "</xml>";
1414 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1415 }
1418 function krb5_ramdomize_key($server,$name)
1419 {
1420 /* Prepare request event
1421 */
1422 $xml_msg =
1423 "<xml>".
1424 "<header>gosa_krb5_randomize_key</header>".
1425 "<principal>".$name."</principal>".
1426 "<source>GOSA</source>".
1427 "<target>".$server."</target>".
1428 "</xml>";
1430 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1431 }
1435 /*! \brief Updates a given principal with a set of configuration settings.
1436 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1437 (Uses the GOsa support daemon instead of the ldap database.)
1438 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1439 @return Boolean TRUE on success else FALSE.
1440 */
1441 public function krb5_set_principal($server,$name,$values)
1442 {
1443 $ret = FALSE;
1445 /* Check if the given name is a valid request value
1446 */
1447 if(!is_string($name) || empty($name)){
1448 trigger_error("The given principal name is not of type string or it is empty.");
1449 return($ret);
1450 }
1451 if(!is_array($values) || !count($values)){
1452 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1453 return($ret);
1454 }
1456 /* Check if the given server is a valid mac address
1457 */
1458 if(!tests::is_mac($server)){
1459 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1460 return($ret);
1461 }
1463 $attrs = "";
1464 foreach($values as $key => $value){
1465 if(empty($key) || is_numeric($key)){
1466 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1467 return($ret);
1468 }
1469 $key = strtolower($key);
1470 if(is_array($value)){
1471 foreach($value as $val){
1472 $attrs.= "<$key>$val</$key>\n";
1473 }
1474 }else{
1475 $attrs.= "<$key>$value</$key>\n";
1476 }
1477 }
1479 /* Prepare request event
1480 */
1481 $xml_msg =
1482 "<xml>".
1483 "<header>gosa_krb5_modify_principal</header>".
1484 "<principal>".$name."</principal>".
1485 $attrs.
1486 "<source>GOSA</source>".
1487 "<target>".$server."</target>".
1488 "</xml>";
1490 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1491 }
1494 /*! \brief Removes the given principal.
1495 (Uses the GOsa support daemon instead of the ldap database.)
1496 @pram String The name of the principal. (e.g. peter@EXAMPLE.DE)
1497 @return Boollean TRUE on success else FALSE
1498 */
1499 public function krb5_del_principal($server,$name)
1500 {
1501 $ret = FALSE;
1503 /* Check if the given name is a valid request value
1504 */
1505 if(!is_string($name) || empty($name)){
1506 trigger_error("The given principal name is not of type string or it is empty.");
1507 return($ret);
1508 }
1510 /* Check if the given server is a valid mac address
1511 */
1512 if(!tests::is_mac($server)){
1513 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1514 return($ret);
1515 }
1517 /* Prepare request event
1518 */
1519 $xml_msg =
1520 "<xml>".
1521 "<header>gosa_krb5_del_principal</header>".
1522 "<principal>".$name."</principal>".
1523 "<source>GOSA</source>".
1524 "<target>".$server."</target>".
1525 "</xml>";
1527 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1528 }
1531 /*! \brief Returns a list of configured password policies.
1532 (Uses the GOsa support daemon instead of the ldap database.)
1533 @return Array A list of all configured password policies.
1534 */
1535 public function krb5_list_policies($server)
1536 {
1537 $res = array();
1539 /* Check if the given server is a valid mac address
1540 */
1541 if(!tests::is_mac($server)){
1542 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1543 return($ret);
1544 }
1546 /* Prepare request event
1547 */
1548 $xml_msg =
1549 "<xml>".
1550 "<header>gosa_krb5_list_policies</header>".
1551 "<source>GOSA</source>".
1552 "<target>".$server."</target>".
1553 "</xml>";
1555 $res = $this->_send($xml_msg,TRUE);
1557 /* Check if there are results for POLICY
1558 */
1559 if(isset($res['XML']['POLICY'])){
1561 /* Ensure that we return an array
1562 */
1563 $tmp = $res['XML']['POLICY'];
1564 if(!is_array($tmp)){
1565 $tmp = array($tmp);
1566 }
1567 return($tmp);
1568 }else{
1569 return(array());
1570 }
1571 }
1574 /*! \brief Returns a list of configured password policies.
1575 (Uses the GOsa support daemon instead of the ldap database.)
1576 @return Array The policy settings for the given policy name.
1577 */
1578 public function krb5_get_policy($server,$name)
1579 {
1580 $res = array();
1582 /* Check if the given name is a valid request value
1583 */
1584 if(!is_string($name) || empty($name)){
1585 trigger_error("The given policy name is not of type string or it is empty.");
1586 return($ret);
1587 }
1589 /* Check if the given server is a valid mac address
1590 */
1591 if(!tests::is_mac($server)){
1592 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1593 return($ret);
1594 }
1596 /* Prepare request event
1597 */
1598 $xml_msg =
1599 "<xml>".
1600 "<header>gosa_krb5_get_policy</header>".
1601 "<policy>".$name."</policy>".
1602 "<source>GOSA</source>".
1603 "<target>".$server."</target>".
1604 "</xml>";
1606 /* Possible attributes */
1607 $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1608 "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1611 $tmp = $this->_send($xml_msg,TRUE);
1612 if(isset($tmp['XML'])){
1613 foreach($attrs as $attr){
1614 if(isset($tmp['XML'][$attr])){
1615 $ret[$attr] = $tmp['XML'][$attr];
1616 }else{
1617 $ret[$attr] = "";
1618 }
1619 }
1620 }
1621 return($ret);
1622 }
1625 /*! \brief Creates a new policy with a given set of configuration settings.
1626 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1627 (Uses the GOsa support daemon instead of the ldap database.)
1628 @pram String The name of the policy to update.
1629 @pram Array The attributes to update
1630 @return Boolean TRUE on success else FALSE.
1631 */
1632 public function krb5_add_policy($server,$name,$values)
1633 {
1634 $ret = FALSE;
1636 /* Check if the given name is a valid request value
1637 */
1638 if(!is_string($name) || empty($name)){
1639 trigger_error("The given policy name is not of type string or it is empty.");
1640 return($ret);
1641 }
1642 if(!is_array($values) || !count($values)){
1643 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1644 return($ret);
1645 }
1647 /* Check if the given server is a valid mac address
1648 */
1649 if(!tests::is_mac($server)){
1650 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1651 return($ret);
1652 }
1655 /* Transform array into <xml>
1656 */
1657 $attrs = "";
1658 foreach($values as $id => $value){
1659 if(empty($id) || is_numeric($id)){
1660 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1661 return($ret);
1662 }
1663 $id = strtolower($id);
1664 $attrs.= "<$id>$value</$id>\n";
1665 }
1667 /* Prepare request event
1668 */
1669 $xml_msg =
1670 "<xml>".
1671 "<header>gosa_krb5_create_policy</header>".
1672 "<policy>".$name."</policy>".
1673 $attrs.
1674 "<source>GOSA</source>".
1675 "<target>".$server."</target>".
1676 "</xml>";
1678 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1679 }
1682 /*! \brief Updates a given policy with a set of configuration settings.
1683 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1684 (Uses the GOsa support daemon instead of the ldap database.)
1685 @pram String The name of the policy to update.
1686 @return Boolean TRUE on success else FALSE.
1687 */
1688 public function krb5_set_policy($server,$name,$values)
1689 {
1690 $ret = FALSE;
1692 /* Check if the given name is a valid request value
1693 */
1694 if(!is_string($name) || empty($name)){
1695 trigger_error("The given policy name is not of type string or it is empty.");
1696 return($ret);
1697 }
1698 if(!is_array($values) || !count($values)){
1699 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1700 return($ret);
1701 }
1703 /* Check if the given server is a valid mac address
1704 */
1705 if(!tests::is_mac($server)){
1706 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1707 return($ret);
1708 }
1710 /* Transform array into <xml>
1711 */
1712 $attrs = "";
1713 foreach($values as $id => $value){
1714 if(preg_match("/^policy$/i",$id)) continue;
1715 if(empty($id) || is_numeric($id)){
1716 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1717 return($ret);
1718 }
1719 $id = strtolower($id);
1720 $attrs.= "<$id>$value</$id>\n";
1721 }
1723 /* Prepare request event
1724 */
1725 $xml_msg =
1726 "<xml>".
1727 "<header>gosa_krb5_modify_policy</header>".
1728 "<policy>".$name."</policy>".
1729 $attrs.
1730 "<source>GOSA</source>".
1731 "<target>".$server."</target>".
1732 "</xml>";
1734 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1735 }
1738 /*! \brief Removes the given password policy.
1739 (Uses the GOsa support daemon instead of the ldap database.)
1740 @return Boolean TRUE on success else FALSE
1741 */
1742 public function krb5_del_policy($server,$name)
1743 {
1744 $ret = FALSE;
1746 /* Check if the given server is a valid mac address
1747 */
1748 if(!tests::is_mac($server)){
1749 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1750 return($ret);
1751 }
1753 /* Check if the given name is a valid request value
1754 */
1755 if(!is_string($name) || empty($name)){
1756 trigger_error("The given policy name is not of type string or it is empty.");
1757 return($ret);
1758 }
1760 /* Prepare request event
1761 */
1762 $xml_msg =
1763 "<xml>".
1764 "<header>gosa_krb5_del_policy</header>".
1765 "<policy>".$name."</policy>".
1766 "<source>GOSA</source>".
1767 "<target>".$server."</target>".
1768 "</xml>";
1769 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1770 }
1773 /*! \brief Sets the password of for the given principal.
1774 (Uses the GOsa support daemon instead of the ldap database.)
1775 @param String The servers mac
1776 @param String The principals name
1777 @param String $the new password.
1778 @return Boolean TRUE on success else FALSE
1779 */
1780 public function krb5_set_password($server,$name,$password)
1781 {
1782 $ret = FALSE;
1784 /* Check if the given server is a valid mac address
1785 */
1786 if(!tests::is_mac($server)){
1787 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1788 return($ret);
1789 }
1791 /* Check if the given name is a valid request value
1792 */
1793 if(!is_string($name) || empty($name)){
1794 trigger_error("The given principal name is not of type string or it is empty.");
1795 return($ret);
1796 }
1798 /* Prepare request event
1799 */
1800 $xml_msg =
1801 "<xml>".
1802 "<header>gosa_krb5_set_password</header>".
1803 "<principal>".$name."</principal>".
1804 "<password>".$password."</password>".
1805 "<source>GOSA</source>".
1806 "<target>".$server."</target>".
1807 "</xml>";
1808 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1809 }
1812 /*! \brief Returns log file informations for a given mac address
1813 @param $mac The mac address to fetch logs for.
1814 @retrun Array A Multidimensional array containing log infos.
1815 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1816 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1817 install_20080313_144450 ...
1818 */
1819 public function get_log_info_for_mac($mac)
1820 {
1821 $xml_msg = "
1822 <xml>
1823 <header>gosa_show_log_by_mac</header>
1824 <target>GOSA</target>
1825 <source>GOSA</source>
1826 <mac>".$mac."</mac>
1827 </xml>";
1829 $res = $this->_send($xml_msg,TRUE);
1830 $ret = array();
1831 if(isset($res['XML'])){
1833 /* Filter all entry that look like this
1834 MAC_00_01_6C_9D_B9_FA
1835 */
1836 foreach($res['XML'] as $name => $entry){
1837 if(preg_match("/^MAC/",$name)){
1839 /* Get list of available log files
1840 */
1841 foreach($entry as $log_date){
1842 $xml_msg2 = "<xml>
1843 <header>gosa_show_log_files_by_date_and_mac</header>
1844 <target>GOSA</target>
1845 <source>GOSA</source>
1846 <date>".$log_date."</date>
1847 <mac>".$mac."</mac>
1848 </xml>";
1850 $ret[$mac][$log_date] = array();
1851 $res = $this->_send($xml_msg2,TRUE);
1852 $ret[$mac][$log_date]['DATE_STR'] = $log_date;
1853 $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1854 if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1855 $ret[$mac][$log_date]['FILES'] = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1856 }
1857 }
1858 }
1859 }
1860 }
1861 return($ret);
1862 }
1864 public function get_log_file($mac,$date,$file)
1865 {
1866 $xml_msg ="
1867 <xml>
1868 <header>gosa_get_log_file_by_date_and_mac</header>
1869 <target>GOSA</target>
1870 <source>GOSA</source>
1871 <date>".$date."</date>
1872 <mac>".$mac."</mac>
1873 <log_file>".$file."</log_file>
1874 </xml>";
1876 $res = $this->_send($xml_msg,TRUE);
1877 if(isset($res['XML'][strtoupper($file)])){
1878 return(base64_decode($res['XML'][strtoupper($file)]));
1879 }
1880 return("");
1881 }
1887 /*****************
1888 * DAK - Functions
1889 *****************/
1891 /*! \brief Returns all currenlty queued entries for a given DAK repository
1892 @param ...
1893 @return Array All queued entries.
1894 */
1895 public function DAK_keyring_entries($server)
1896 {
1897 /* Ensure that we send the event to a valid mac address
1898 */
1899 if(!is_string($server) || !tests::is_mac($server)){
1900 trigger_error("No valid mac address given '".$server."'.");
1901 return;
1902 }
1904 /* Create query
1905 */
1906 $xml_msg = "<xml>
1907 <header>gosa_get_dak_keyring</header>
1908 <target>".$server."</target>
1909 <source>GOSA</source>
1910 </xml>";
1912 $res = $this->_send($xml_msg,TRUE);
1914 /* Check if there are results for POLICY
1915 */
1916 if(isset($res['XML'])){
1917 $ret = array();
1918 foreach($res['XML'] as $key => $entry){
1919 if(preg_match("/^ANSWER/",$key)){
1920 $ret[] = $entry;
1921 }
1922 }
1923 return($ret);
1924 }else{
1925 return(array());
1926 }
1927 }
1930 /*! \brief Imports the given key into the specified keyring (Servers mac address)
1931 @param String The servers mac address
1932 @param String The gpg key.
1933 @return Boolean TRUE on success else FALSE
1934 */
1935 public function DAK_import_key($server,$key)
1936 {
1937 /* Ensure that we send the event to a valid mac address
1938 */
1939 if(!is_string($server) || !tests::is_mac($server)){
1940 trigger_error("No valid mac address given '".$server."'.");
1941 return;
1942 }
1944 /* Check if there is some cleanup required before importing the key.
1945 There may be some Header lines like:
1946 -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.6 (GNU/Linux)
1947 */
1948 if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1950 /* Remove header */
1951 $key = preg_replace("/^.*\n\n/sim","",$key);
1952 /* Remove footer */
1953 $key = preg_replace("/-----.*$/sim","",$key);
1954 }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1956 /* Encode key if it is raw.
1957 */
1958 $key = base64_encode($key);
1959 }
1961 /* Create query
1962 */
1963 $xml_msg = "<xml>
1964 <header>gosa_import_dak_key</header>
1965 <target>".$server."</target>
1966 <key>".$key."</key>
1967 <source>GOSA</source>
1968 </xml>";
1970 $res = $this->_send($xml_msg,TRUE);
1971 return($this->is_error());
1972 }
1975 /*! \brief Removes a key from the keyring on the given server.
1976 @param String The servers mac address
1977 @param String The gpg key uid.
1978 @return Boolean TRUE on success else FALSE
1979 */
1980 public function DAK_remove_key($server,$key)
1981 {
1982 /* Ensure that we send the event to a valid mac address
1983 */
1984 if(!is_string($server) || !tests::is_mac($server)){
1985 trigger_error("No valid mac address given '".$server."'.");
1986 return;
1987 }
1989 /* Create query
1990 */
1991 $xml_msg = "<xml>
1992 <header>gosa_remove_dak_key</header>
1993 <target>".$server."</target>
1994 <keyid>".$key."</keyid>
1995 <source>GOSA</source>
1996 </xml>";
1998 $res = $this->_send($xml_msg,TRUE);
1999 return($this->is_error());
2000 }
2001 }
2003 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
2004 ?>