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;
37 /*! \brief Creates a new gosaSupportDaemon object.
38 @param string Host The Host where the daemon is running on.
39 @param integer Port The port which the daemon use.
40 @param string Key The encryption string.
41 @param boolean Connect Directly connect to daemon socket.
42 @param float Timeout The timelimit for all socket actions.
43 */
44 public function __construct($connect=TRUE,$timeout=10)
45 {
46 #FIXME: bad idea about referencing global variables from within classes
47 global $config;
49 /* This should only be the case if we call this from setup.
50 __autoload()
51 */
52 if(!is_object($config)) { return; }
54 # load from config, store statically
55 if ($config->get_cfg_value("gosa_si") != ""){
57 if ($this->s_host == ""){
58 $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->get_cfg_value("gosa_si"));
59 $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->get_cfg_value("gosa_si"));
60 $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->get_cfg_value("gosa_si"));
61 }
63 $this->f_timeout = $timeout;
64 if($connect){
65 $this->connect();
66 }
67 }
68 }
71 /*! \brief Establish daemon connection.
72 @return boolean Returns true if the connection was succesfully established.
73 */
74 public function connect()
75 {
76 if(!empty($this->s_host) && !empty($this->i_port)){
77 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
78 if($this->o_sock->connected()){
79 $this->o_sock->setEncryptionKey($this->s_encryption_key);
80 $this->is_connected = TRUE;
81 }else{
82 $this->set_error($this->o_sock->get_error());
83 $this->disconnect();
84 new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
85 }
86 }else{
87 $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
88 }
89 return($this->is_connected);
90 }
93 /*! \brief Returns TRUE whether we are connected or not
94 @return BOOLEAN Returns TRUE when connected else FALSE
95 */
96 public function is_connected()
97 {
98 return($this->is_connected);
99 }
103 /*! \brief Disconnect from gosa daemon.
104 */
105 public function disconnect()
106 {
107 $this->o_sock->close();
108 $this->is_connected = FALSE;
109 }
112 /*! \brief Sets an error message, which can be returned with get_error().
113 @param string The Error message,
114 */
115 private function set_error($str)
116 {
117 $this->b_error = TRUE;
118 $this->s_error = $str;
119 }
122 /*! \brief Sets an error message, which can be returned with get_error().
123 @param string The Error message,
124 */
125 private function reset_error()
126 {
127 $this->b_error = FALSE;
128 $this->s_error = "";
129 }
132 /*! \brief Checks if an error occured.
133 @return boolean returns TRUE or FALSE, whether there is an error or not.
134 */
135 public function is_error()
136 {
137 return($this->b_error);
138 }
141 /*! \brief Returns the last error.
142 @return Returns the last error.
143 */
144 public function get_error()
145 {
146 $str = $this->s_error;
147 $ret = "";
148 if(is_string($str)){
149 $ret = $str;
150 }else{
151 foreach($str as $msg){
152 $ret .= $msg." ";
153 }
154 }
155 $ret = preg_replace("/ /"," ",$ret);
156 return($ret);
157 }
160 public function FAI_get_kernels($release)
161 {
162 $xml_msg =
163 "<xml>".
164 "<header>gosa_get_available_kernel</header>".
165 "<source>GOSA</source>".
166 "<target>GOSA</target>".
167 "<release>".$release."</release>".
168 "</xml>";
170 $ret = array();
171 if($this->connect()){
172 $this->o_sock->write($xml_msg);
173 $str = trim($this->o_sock->read());
175 /* Check if something went wrong while reading */
176 if($this->o_sock->is_error()){
177 $this->set_error($this->o_sock->get_error());
178 return($ret);
179 }
181 $entries = $this->xml_to_array($str);
182 if(isset($entries['XML']) && is_array($entries['XML'])){
184 /* Check if returned values represent a valid answer */
185 if(isset($entries['XML'])){
186 if(isset($entries['XML']['ERROR_STRING'])) {
187 $this->set_error($entries['XML']['ERROR_STRING']);
188 new log("debug","GOsa-si",
189 get_class($this)."::".__FUNCTION__, array(),
190 "FAILED error was ".$this->get_error());
191 return($ret);
192 }
194 /* Unset header tags */
195 $ret = $entries['XML'];
196 foreach($ret as $key => $entry){
197 if(!preg_match("/^answer/i",$key)){
198 unset($ret[$key]);
199 }
200 }
201 }
202 }
203 }
204 return($ret);
205 }
208 public function FAI_get_package_sections($release)
209 {
210 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
211 "<select>distinct section</select>".
212 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
214 $ret = array();
215 if($this->connect()){
216 $this->o_sock->write($xml_msg);
217 $str = trim($this->o_sock->read());
219 /* Check if something went wrong while reading */
220 if($this->o_sock->is_error()){
221 $this->set_error($this->o_sock->get_error());
222 return($ret);
223 }
225 $entries = $this->xml_to_array($str);
226 if(isset($entries['XML']) && is_array($entries['XML'])){
228 /* Check if returned values represent a valid answer */
229 if(isset($entries['XML'])){
230 if(isset($entries['XML']['ERROR_STRING'])) {
231 $this->set_error($entries['XML']['ERROR_STRING']);
232 new log("debug","GOsa-si",
233 get_class($this)."::".__FUNCTION__, array(),
234 "FAILED error was ".$this->get_error());
235 return($ret);
236 }
238 /* Unset header tags */
239 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
240 if(isset($entries['XML'][$type])){
241 unset($entries['XML'][$type]);
242 }
243 }
244 $ret = $entries['XML'];
245 }
246 }
247 }
248 return($ret);
249 }
252 public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
253 {
254 $this->reset_error();
255 $ret = array();
257 /* Check Parameter */
258 if(!is_array($attrs) || !count($attrs)){
259 trigger_error("Second parameter must be an array. With at least one attribute name.");
260 return($ret);
261 }
263 /* Check Parameter */
264 if(!is_array($package)){
265 trigger_error("Third parameter must be an array. With at least one attribute name.");
266 return($ret);
267 }
269 /* Create list of attributes to fetch */
270 $attr = "";
271 foreach($attrs as $at){
272 $attr.= "<select>".$at."</select>";
273 }
275 /* If no package is given, search for all */
276 if(!count($package)) $package = array("%");
278 /* Create limit tag */
279 if($from == -1){
280 $limit ="";
281 }else{
282 $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
283 }
285 /* Create list of attributes to fetch */
286 $pkgs = "";
287 foreach($package as $pkg){
288 $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
289 }
291 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
292 $attr.
293 "<where>
294 <clause><phrase><distribution>".$release."</distribution></phrase></clause>
295 <clause><connector>OR</connector>
296 ".$pkgs."
297 </clause>
298 </where>".
299 $limit.
300 "</xml>";
302 if($this->connect()){
303 $this->o_sock->write($xml_msg);
304 $str = trim($this->o_sock->read());
306 /* Check if something went wrong while reading */
307 if($this->o_sock->is_error()){
308 $this->set_error($this->o_sock->get_error());
309 return($ret);
310 }
312 $entries = $this->xml_to_array($str);
313 if(isset($entries['XML']) && is_array($entries['XML'])){
315 /* Check if returned values represent a valid answer */
316 if(isset($entries['XML'])){
317 if(isset($entries['XML']['ERROR_STRING'])) {
318 $this->set_error($entries['XML']['ERROR_STRING']);
319 new log("debug","GOsa-si",
320 get_class($this)."::".__FUNCTION__, array(),
321 "FAILED error was ".$this->get_error());
322 return($ret);
323 }
325 /* Unset header tags */
326 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
327 if(isset($entries['XML'][$type])){
328 unset($entries['XML'][$type]);
329 }
330 }
331 $ret = $entries['XML'];
332 }
333 }
334 }
335 return($ret);
338 }
341 public function FAI_get_server($name = "")
342 {
343 $this->reset_error();
345 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
346 $ret = array();
347 if($this->connect()){
348 $this->o_sock->write($xml_msg);
349 $str = trim($this->o_sock->read());
351 /* Check if something went wrong while reading */
352 if($this->o_sock->is_error()){
353 $this->set_error($this->o_sock->get_error());
354 return($ret);
355 }
357 $entries = $this->xml_to_array($str);
358 if(isset($entries['XML']) && is_array($entries['XML'])){
360 /* Check if returned values represent a valid answer */
361 if(isset($entries['XML'])){
362 if(isset($entries['XML']['ERROR_STRING'])) {
363 $this->set_error($entries['XML']['ERROR_STRING']);
364 new log("debug","GOsa-si",
365 get_class($this)."::".__FUNCTION__, array(),
366 "FAILED error was ".$this->get_error());
367 return($ret);
368 }
370 /* Unset header tags */
371 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
372 if(isset($entries['XML'][$type])){
373 unset($entries['XML'][$type]);
374 }
375 }
376 $ret = $entries['XML'];
377 }
378 }
379 }
380 return($ret);
381 }
384 public function FAI_get_classes($name)
385 {
386 $this->reset_error();
387 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
388 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
389 $ret = array();
390 if($this->connect()){
391 $this->o_sock->write($xml_msg);
392 $str = trim($this->o_sock->read());
394 /* Check if something went wrong while reading */
395 if($this->o_sock->is_error()){
396 $this->set_error($this->o_sock->get_error());
397 return($ret);
398 }
400 $entries = $this->xml_to_array($str);
401 if(isset($entries['XML']) && is_array($entries['XML'])){
403 /* Check if returned values represent a valid answer */
404 if(isset($entries['XML'])){
405 if(isset($entries['XML']['ERROR_STRING'])) {
406 $this->set_error($entries['XML']['ERROR_STRING']);
407 new log("debug","GOsa-si",
408 get_class($this)."::".__FUNCTION__, array($name),
409 "FAILED error was ".$this->get_error());
410 return($ret);
411 }
413 /* Unset header tags */
414 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
415 if(isset($entries['XML'][$type])){
416 unset($entries['XML'][$type]);
417 }
418 }
419 $ret = $entries['XML'];
420 }
421 }
422 }
423 return($ret);
424 }
427 /*! \brief Returns an array containing all queued entries.
428 @return Array All queued entries as an array.
429 */
430 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
431 {
432 $this->reset_error();
433 $ret = array();
435 $tags = "";
436 foreach($event_types as $type){
437 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
438 }
439 if(count($event_types) > 1){
440 $tags = "<connector>or</connector>".$tags;
441 }
442 if(count($event_types)){
443 $tags = "<where><clause>".$tags."</clause></where>";
444 }
446 $xml_msg =
447 "<xml>
448 <header>gosa_query_jobdb</header>
449 <target>GOSA</target>
450 <source>GOSA</source>
451 ".$tags."
453 <orderby>".$sort."</orderby>";
454 if($from != -1 && $to != -1){
455 $xml_msg.= "
456 <limit>
457 <from>".$from."</from>
458 <to>".$to."</to>
459 </limit>";
460 }
461 $xml_msg.= "
462 </xml>";
464 if($this->connect()){
465 $this->o_sock->write($xml_msg);
466 $str = trim($this->o_sock->read());
468 /* Check if something went wrong while reading */
469 if($this->o_sock->is_error()){
470 $this->set_error($this->o_sock->get_error());
471 return($ret);
472 }
474 $entries = $this->xml_to_array($str);
475 if(isset($entries['XML']) && is_array($entries['XML'])){
477 /* Check if returned values represent a valid answer */
478 if(isset($entries['XML'])){
480 /* Unset header tags */
481 foreach(array("HEADER","SOURCE","TARGET") as $type){
482 unset($entries['XML'][$type]);
483 }
484 $ret = $entries['XML'];
485 }
486 }
487 }
489 /* Remove session ID. No one is interested in this... */
490 unset($ret['SESSION_ID']);
492 return($ret);
493 }
496 /*! \brief Checks if the given ids are used queue ids.
497 @param Array The ids we want to check..
498 @return Array An array containing all ids as index and TRUE/FALSE as value.
499 */
500 public function ids_exist($ids)
501 {
502 if(!is_array($ids)){
503 trigger_error("Requires an array as parameter.");
504 return;
505 }
506 $this->reset_error();
508 $ret = array();
510 $xml_msg = "<xml>
511 <header>gosa_query_jobdb</header>
512 <target>GOSA</target>
513 <source>GOSA</source>
514 <where>
515 <clause>
516 <connector>or</connector>";
517 foreach($ids as $id){
518 $xml_msg .= "<phrase>
519 <operator>eq</operator>
520 <id>".$id."</id>
521 </phrase>";
522 }
523 $xml_msg .= "</clause>
524 </where>
525 </xml>";
527 if($this->connect()){
528 $this->o_sock->write($xml_msg);
529 $str = trim($this->o_sock->read());
531 /* Check if something went wrong while reading */
532 if($this->o_sock->is_error()){
533 $this->set_error($this->o_sock->get_error());
534 return($ret);
535 }
537 $entries = $this->xml_to_array($str);
538 if(isset($entries['XML']) && is_array($entries['XML'])){
539 foreach($entries['XML'] as $entry){
540 if(is_array($entry) && array_key_exists("ID",$entry)){
541 $ret[] = $entry['ID'];
542 }
543 }
544 }
545 }
546 return($ret);
547 }
550 /*! \brief Returns an entry containing all requested ids.
551 @param Array The IDs of the entries we want to return.
552 @return Array Of the requested entries.
553 */
554 public function get_entries_by_mac($macs)
555 {
556 if(!is_array($macs)){
557 trigger_error("Requires an array as parameter.");
558 return;
559 }
560 $this->reset_error();
562 $ret = array();
564 $xml_msg = "<xml>
565 <header>gosa_query_jobdb</header>
566 <target>GOSA</target>
567 <source>GOSA</source>
568 <where>
569 <clause>
570 <connector>or</connector>";
571 foreach($macs as $mac){
572 $xml_msg .= "<phrase>
573 <operator>eq</operator>
574 <macaddress>".$mac."</macaddress>
575 </phrase>";
576 }
577 $xml_msg .= "</clause>
578 </where>
579 </xml>";
581 if($this->connect()){
582 $this->o_sock->write($xml_msg);
583 $str = trim($this->o_sock->read());
585 /* Check if something went wrong while reading */
586 if($this->o_sock->is_error()){
587 $this->set_error($this->o_sock->get_error());
588 return($ret);
589 }
591 $entries = $this->xml_to_array($str);
592 if(isset($entries['XML'])){
593 foreach($entries['XML'] as $name => $entry){
594 if(preg_match("/^ANSWER[0-9]*$/",$name)){
595 $ret[$name] = $entry;
596 }
597 }
598 }
599 }
600 return($ret);
601 }
604 /*! \brief Returns an entry containing all requested ids.
605 @param Array The IDs of the entries we want to return.
606 @return Array Of the requested entries.
607 */
608 public function get_entries_by_id($ids)
609 {
610 if(!is_array($ids)){
611 trigger_error("Requires an array as parameter.");
612 return;
613 }
614 $this->reset_error();
616 $ret = array();
618 $xml_msg = "<xml>
619 <header>gosa_query_jobdb</header>
620 <target>GOSA</target>
621 <source>GOSA</source>
622 <where>
623 <clause>
624 <connector>or</connector>";
625 foreach($ids as $id){
626 $xml_msg .= "<phrase>
627 <operator>eq</operator>
628 <id>".$id."</id>
629 </phrase>";
630 }
631 $xml_msg .= "</clause>
632 </where>
633 </xml>";
635 if($this->connect()){
636 $this->o_sock->write($xml_msg);
637 $str = trim($this->o_sock->read());
639 /* Check if something went wrong while reading */
640 if($this->o_sock->is_error()){
641 $this->set_error($this->o_sock->get_error());
642 return($ret);
643 }
645 $entries = $this->xml_to_array($str);
646 if(isset($entries['XML'])){
647 foreach($entries['XML'] as $name => $entry){
648 if(preg_match("/^ANSWER[0-9]*$/",$name)){
649 $ret[$name] = $entry;
650 }
651 }
652 }
653 }
654 return($ret);
655 }
658 /*! \brief Checks if the given id is in use.
659 @param Integer The ID of the entry.
660 @return Boolean TRUE if entry exists.
661 */
662 public function id_exists($id)
663 {
664 if(!is_numeric($id)){
665 trigger_error("Requires an integer as parameter.");
666 return;
667 }
669 $this->reset_error();
671 $xml_msg = "<xml>
672 <header>gosa_query_jobdb</header>
673 <target>GOSA</target>
674 <source>GOSA</source>
675 <where>
676 <clause>
677 <phrase>
678 <operator>eq</operator>
679 <id>".$id."</id>
680 </phrase>
681 </clause>
682 </where>
683 </xml>";
685 if($this->connect()){
686 $this->o_sock->write($xml_msg);
687 $str = trim($this->o_sock->read());
689 /* Check if something went wrong while reading */
690 if($this->o_sock->is_error()){
691 $this->set_error($this->o_sock->get_error());
692 return(FALSE);
693 }
695 $entries = $this->xml_to_array($str);
696 if( isset($entries['XML']['HEADER']) &&
697 $entries['XML']['HEADER']=="answer" &&
698 isset($entries['XML']['ANSWER1'])){
699 return(TRUE);
700 }
701 }
702 return(FALSE);
703 }
706 /*! \brief Returns an entry from the gosaSupportQueue
707 @param Integer The ID of the entry we want to return.
708 @return Array Of the requested entry.
709 */
710 public function get_entry_by_id($id)
711 {
712 if(!is_numeric($id)){
713 trigger_error("Requires an integer as parameter.");
714 return;
715 }
716 $this->reset_error();
718 $ret = array();
719 $xml_msg = "<xml>
720 <header>gosa_query_jobdb</header>
721 <target>GOSA</target>
722 <source>GOSA</source>
723 <where>
724 <clause>
725 <phrase>
726 <operator>eq</operator>
727 <id>".$id."</id>
728 </phrase>
729 </clause>
730 </where>
731 </xml>";
732 if($this->connect()){
733 $this->o_sock->write($xml_msg);
734 $str = trim($this->o_sock->read());
736 /* Check if something went wrong while reading */
737 if($this->o_sock->is_error()){
738 $this->set_error($this->o_sock->get_error());
739 return($ret);
740 }
742 $entries = $this->xml_to_array($str);
743 if( isset($entries['XML']['HEADER']) &&
744 $entries['XML']['HEADER']=="answer" &&
745 isset($entries['XML']['ANSWER1'])){
746 $ret = $entries['XML']['ANSWER1'];
747 }
748 }
749 return($ret);
750 }
753 /*! \brief Removes a set of entries from the GOsa support queue.
754 @param Array The IDs to remove.
755 @return Boolean True on success.
756 */
757 public function remove_entries($ids)
758 {
759 if(!is_array($ids)){
760 trigger_error("Requires an array as parameter.");
761 return;
762 }
764 $this->reset_error();
766 $ret = array();
768 $xml_msg = "<xml>
769 <header>gosa_delete_jobdb_entry</header>
770 <target>GOSA</target>
771 <source>GOSA</source>
772 <where>
773 <clause>
774 <connector>or</connector>";
775 foreach($ids as $id){
776 $xml_msg .= "<phrase>
777 <operator>eq</operator>
778 <id>".$id."</id>
779 </phrase>";
780 }
781 $xml_msg .= "</clause>
782 </where>
783 </xml>";
785 if($this->connect()){
786 $this->o_sock->write($xml_msg);
787 $str = $this->o_sock->read();
789 /* Check if something went wrong while reading */
790 if($this->o_sock->is_error()){
791 $this->set_error($this->o_sock->get_error());
792 return($ret);
793 }
795 $entries = $this->xml_to_array($str);
796 if(isset($entries['XML']) || isset($entries['COUNT'])){
797 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
798 return(TRUE);
799 }else{
800 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
801 }
802 }
803 return(FALSE);
804 }
808 /*! \brief Removes an entry from the GOsa support queue.
809 @param Integer The ID of the entry we want to remove.
810 @return Boolean True on success.
811 */
812 public function remove_entry($id)
813 {
814 return($this->remove_entries(array($id)));
815 }
818 /*! \brief Parses the given xml string into an array
819 @param String XML string
820 @return Array Returns an array containing the xml structure.
821 */
822 private function xml_to_array($xml)
823 {
824 $params = array();
825 $level = array();
826 $parser = xml_parser_create_ns();
827 xml_parse_into_struct($parser, $xml, $vals, $index);
829 $err_id = xml_get_error_code($parser);
830 if($err_id){
831 xml_parser_free($parser);
832 }else{
833 xml_parser_free($parser);
835 foreach ($vals as $xml_elem) {
836 if ($xml_elem['type'] == 'open') {
837 if (array_key_exists('attributes',$xml_elem)) {
838 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
839 } else {
840 $level[$xml_elem['level']] = $xml_elem['tag'];
841 }
842 }
843 if ($xml_elem['type'] == 'complete') {
845 $start_level = 1;
846 $test2 = &$params;
847 while($start_level < $xml_elem['level']) {
848 $test2 = &$test2[$level[$start_level]];
849 $start_level++;
850 }
852 /* Save tag attributes too.
853 e.g. <tag attr="val">
854 */
855 if(isset($xml_elem['attributes'])){
856 foreach($xml_elem['attributes'] as $name => $value){
857 $test2['ATTRIBUTES'][$name] = $value;
858 }
859 }
861 if(!isset($test2[$xml_elem['tag']])){
862 if(isset($xml_elem['value'])){
863 $test2[$xml_elem['tag']] = $xml_elem['value'];
864 }
865 }else{
866 if(!is_array($test2[$xml_elem['tag']])){
867 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
868 }
869 $test2[$xml_elem['tag']][] = $xml_elem['value'];
870 }
871 }
872 }
873 }
875 if(!isset($params['XML'])){
876 if (!array_key_exists('XML', $params)){
877 $this->set_error(_("Cannot not parse XML!"));
878 }
879 $params = array("COUNT" => 0);
880 }
882 return($params);
883 }
886 /*! \brief Updates an entry with a set of new values,
887 @param Integer The ID of the entry, we want to update.
888 @param Array The variables to update.
889 @return Boolean Returns TRUE on success.
890 */
891 public function update_entries($ids,$data)
892 {
893 $this->reset_error();
894 if(!is_array($ids)){
895 trigger_error("Requires an array as first parameter.");
896 return;
897 }
899 if(!is_array($data)){
900 trigger_error("Requires an array as second parameter.");
901 return;
902 }
904 $attr = "";
905 foreach($data as $key => $value){
906 $key = strtolower($key);
907 if(is_array($value)){
908 foreach($value as $sub_value){
909 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
910 }
911 }else{
912 $attr.= "<$key>".strtolower($value)."</$key>\n";
913 }
914 }
916 $xml_msg = "<xml>
917 <header>gosa_update_status_jobdb_entry</header>
918 <target>GOSA</target>
919 <source>GOSA</source>
920 <where>
921 <clause>
922 <connector>or</connector>";
923 foreach($ids as $id){
924 $xml_msg .= "<phrase>
925 <operator>eq</operator>
926 <id>".$id."</id>
927 </phrase>";
928 }
929 $xml_msg .= "</clause>
930 </where>
931 <update>
932 ".$attr."
933 </update>
934 </xml>";
936 if($this->connect()){
938 $this->o_sock->write($xml_msg);
939 $str = trim($this->o_sock->read());
941 /* Check if something went wrong while reading */
942 if($this->o_sock->is_error()){
943 $this->set_error($this->o_sock->get_error());
944 return(FALSE);
945 }
947 $entries = $this->xml_to_array($str);
948 if(isset($entries['XML'])){
949 if(isset($entries['XML']['ERROR_STRING'])) {
950 $this->set_error($entries['XML']['ERROR_STRING']);
951 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
952 return(FALSE);
953 }
954 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
955 return(TRUE);
956 }
957 }
958 return(FALSE);
959 }
962 /*! \brief Returns the number of currently queued objects.
963 @return Integer
964 */
965 public function number_of_queued_entries($event_types)
966 {
967 $tags = "";
968 foreach($event_types as $type){
969 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
970 }
971 if(count($event_types) > 1){
972 $tags = "<connector>or</connector>".$tags;
973 }
974 if(count($event_types)){
975 $tags = "<where><clause>".$tags."</clause></where>";
976 }
979 $xml_msg =
980 "<xml>".
981 "<header>gosa_query_jobdb</header>".
982 "<target>GOSA</target>".
983 "<source>GOSA</source>".
984 "<select> count ID</select>".
985 $tags.
986 "</xml>";
988 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
989 $this->connect();
990 if($this->connect()){
991 $this->o_sock->write($xml_msg);
992 $str = trim($this->o_sock->read());
994 /* Check if something went wrong while reading */
995 if($this->o_sock->is_error()){
996 $this->set_error($this->o_sock->get_error());
997 return(0);
998 }
1000 $entries = $this->xml_to_array($str);
1001 if(isset($entries['XML'])){
1002 return($entries['XML']['COUNT']);
1003 }
1004 }
1005 return(-1);
1006 }
1009 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
1010 {
1011 $xml_message= "";
1013 /* Prepare data */
1014 foreach ($data as $key => $value){
1015 if(is_array($value)){
1016 foreach($value as $sub_value){
1017 $xml_message.= "<$key>$sub_value</$key>";
1018 }
1019 }else{
1020 $xml_message.= "<$key>$value</$key>";
1021 }
1022 }
1024 /* Multiple targets? */
1025 if (!is_array($to)){
1026 $to_targets= array($to);
1027 } else {
1028 $to_targets= $to;
1029 }
1031 /* Build target strings */
1032 $target ="";
1033 foreach($to_targets as $to){
1034 $target.= "<target>$to</target>";
1035 }
1037 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1038 }
1041 /* Allows simply appending a new DaemonEvent
1042 */
1043 public function append($event)
1044 {
1045 if(!($event instanceof DaemonEvent)){
1046 return(FALSE);
1047 }
1049 $this->reset_error();
1051 /* Add to queue if new
1052 */
1053 if($event->is_new()){
1055 $request_answer = FALSE;
1056 if($event->get_type() == SCHEDULED_EVENT){
1057 $action = $event->get_schedule_action();
1058 }elseif($event->get_type() == TRIGGERED_EVENT){
1059 $action = $event->get_trigger_action();
1060 }else{
1061 trigger_error("Unknown type of queue event given.");
1062 return(FALSE);
1063 }
1065 /* Get event informations, like targets..
1066 */
1067 $targets = $event->get_targets();
1068 $data = $event->save();
1070 /* Append an entry for each target
1071 */
1072 foreach($targets as $target){
1073 $data['macaddress'] = $target;
1074 $this->send_data($action,$target,$data,$request_answer);
1076 if($this->is_error()){
1077 return(FALSE);
1078 }
1079 }
1080 return(TRUE);
1081 }else{
1083 /* Updated edited entry.
1084 */
1085 $id = $event->get_id();
1086 $data = $event->save();
1087 return($this->update_entries(array($id),$data));
1088 }
1090 return(FALSE);
1091 }
1094 /*! \brief Returns an array containing all queued entries.
1095 @return Array All queued entries as an array.
1096 */
1097 public function _send($data, $answer_expected= FALSE)
1098 {
1099 $this->reset_error();
1100 $ret = array();
1102 if($this->connect()){
1103 $this->o_sock->write($data);
1104 if ($answer_expected){
1105 $str = trim($this->o_sock->read());
1107 /* Check if something went wrong while reading */
1108 if($this->o_sock->is_error()){
1109 $this->set_error($this->o_sock->get_error());
1110 return($ret);
1111 }
1113 $entries = $this->xml_to_array($str);
1114 if(isset($entries['XML']) && is_array($entries['XML'])){
1115 $ret = $entries;
1116 if(isset($entries['XML']['ERROR_STRING'])) {
1117 $this->set_error($entries['XML']['ERROR_STRING']);
1118 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1119 }elseif(isset($entries['XML']['ERROR'])){
1120 $this->set_error($entries['XML']['ERROR']);
1121 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1122 }else{
1123 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1124 }
1125 }
1126 }else{
1127 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1128 }
1129 }
1130 return($ret);
1131 }
1134 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1135 {
1136 $xml_message= "";
1138 /* Get communication object */
1139 $d= new gosaSupportDaemon(TRUE,10);
1141 /* Prepare data */
1142 foreach ($data as $key => $value){
1143 if(is_array($value)){
1144 foreach($value as $sub_val){
1145 $xml_message.= "<$key>$sub_val</$key>";
1146 }
1147 }else{
1148 $xml_message.= "<$key>$value</$key>";
1149 }
1150 }
1152 /* Multiple targets? */
1153 if (!is_array($to)){
1154 $to_targets= array($to);
1155 } else {
1156 $to_targets= $to;
1157 }
1159 /* Build target strings */
1160 $target ="";
1161 foreach($to_targets as $to){
1162 $target.= "<target>$to</target>";
1163 }
1165 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1166 }
1169 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1170 @param String $mac The mac address for which we want to remove all jobs.
1171 */
1172 function clean_queue_from_mac($mac)
1173 {
1174 global $config;
1176 /* First of all we have to check which jobs are startet
1177 * for $mac
1178 */
1179 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1181 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1183 $data = $this->_send($xml_msg,TRUE);
1184 if(is_array($data) && isset($data['XML'])){
1185 $already_aborted = FALSE;
1186 foreach($data['XML'] as $name => $entry){
1187 if(preg_match("/answer[0-9]*/i",$name)){
1188 $entry['STATUS'] = strtoupper($entry['STATUS']);
1189 switch($entry['STATUS']){
1191 case 'PROCESSING' :
1193 /* Send abort event, but only once
1194 */
1195 if($already_aborted){
1196 break;
1197 }elseif(class_available("DaemonEvent_faireboot")){
1198 $already_aborted = TRUE;
1199 $tmp = new DaemonEvent_faireboot($config);
1200 $tmp->add_targets(array($mac));
1201 $tmp->set_type(TRIGGERED_EVENT);
1202 if(!$this->append($tmp)){
1203 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1204 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1205 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1206 }else{
1207 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1208 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1209 }
1210 ;break;
1211 }else{
1212 /* Couldn't find abort event, just remove entry */
1213 }
1215 case 'WAITING':
1216 case 'ERROR':
1217 default :
1219 /* Simply remove entries from queue.
1220 * Failed or waiting events, can be removed without any trouble.
1221 */
1222 if(!$this->remove_entries(array($entry['ID']))){
1223 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1224 }
1225 ;break;
1226 }
1228 }
1229 }
1230 }
1231 }
1234 static function ping($target)
1235 {
1236 if (tests::is_mac($target)){
1237 /* Get communication object */
1238 $d= new gosaSupportDaemon(TRUE,0.5);
1239 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1240 return (count($answer) ? TRUE:FALSE);
1241 }
1242 return (FALSE);
1243 }
1247 /*! \brief Returns a list of all configured principals.
1248 (Uses the GOsa support daemon instead of the ldap database.)
1249 @return Array A list containing the names of all configured principals.
1250 */
1251 public function krb5_list_principals($server)
1252 {
1253 $res = array();
1255 /* Check if the given server is a valid mac address
1256 */
1257 if(!tests::is_mac($server)){
1258 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1259 return($ret);
1260 }
1262 /* Prepare request event
1263 */
1264 $xml_msg =
1265 "<xml>".
1266 "<header>gosa_krb5_list_principals</header>".
1267 "<source>GOSA</source>".
1268 "<target>".$server."</target>".
1269 "</xml>";
1271 $tmp = $this->_send($xml_msg,TRUE);
1272 if(isset($tmp['XML']['PRINCIPAL'])){
1273 return($tmp['XML']['PRINCIPAL']);
1274 }else{
1275 return($res);
1276 }
1277 }
1280 /*! \brief Returns the configuration settings for a given principal name.
1281 (Uses the GOsa support daemon instead of the ldap database.)
1282 @pram String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1283 @return Array A list containing the names of all configured principals.
1284 */
1285 public function krb5_get_principal($server,$name)
1286 {
1287 $ret = array();
1289 /* Check if the given name is a valid request value
1290 */
1291 if(!is_string($name) || empty($name)){
1292 trigger_error("The given principal name is not of type string or it is empty.");
1293 return($ret);
1294 }
1296 /* Check if the given server is a valid mac address
1297 */
1298 if(!tests::is_mac($server)){
1299 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1300 return($ret);
1301 }
1303 /* Prepare request event
1304 */
1305 $xml_msg =
1306 "<xml>".
1307 "<header>gosa_krb5_get_principal</header>".
1308 "<principal>".$name."</principal>".
1309 "<source>GOSA</source>".
1310 "<target>".$server."</target>".
1311 "</xml>";
1313 $res = $this->_send($xml_msg,TRUE);
1314 if(isset($res['XML'])){
1315 return($res['XML']);
1316 }else{
1317 return($ret);
1318 }
1319 }
1322 /*! \brief Creates a given principal with a set of configuration settings.
1323 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1324 (Uses the GOsa support daemon instead of the ldap database.)
1325 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1326 @return Boolean TRUE on success else FALSE.
1327 */
1328 public function krb5_add_principal($server,$name,$values)
1329 {
1330 $ret = FALSE;
1332 /* Check if the given name is a valid request value
1333 */
1334 if(!is_string($name) || empty($name)){
1335 trigger_error("The given principal name is not of type string or it is empty.");
1336 return($ret);
1337 }
1338 if(!is_array($values)){
1339 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1340 return($ret);
1341 }
1343 /* Check if the given server is a valid mac address
1344 */
1345 if(!tests::is_mac($server)){
1346 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1347 return($ret);
1348 }
1350 $attrs = "";
1351 foreach($values as $key => $value){
1352 if(empty($key) || is_numeric($key)){
1353 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1354 return($ret);
1355 }
1356 $key = strtolower($key);
1357 if(is_array($value)){
1358 foreach($value as $val){
1359 $attrs.= "<$key>$val</$key>\n";
1360 }
1361 }else{
1362 $attrs.= "<$key>$value</$key>\n";
1363 }
1364 }
1366 /* Prepare request event
1367 */
1368 $xml_msg =
1369 "<xml>".
1370 "<header>gosa_krb5_create_principal</header>".
1371 "<principal>".$name."</principal>".
1372 $attrs.
1373 "<source>GOSA</source>".
1374 "<target>".$server."</target>".
1375 "</xml>";
1377 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1378 }
1381 function krb5_ramdomize_key($server,$name)
1382 {
1383 /* Prepare request event
1384 */
1385 $xml_msg =
1386 "<xml>".
1387 "<header>gosa_krb5_randomize_key</header>".
1388 "<principal>".$name."</principal>".
1389 "<source>GOSA</source>".
1390 "<target>".$server."</target>".
1391 "</xml>";
1393 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1394 }
1398 /*! \brief Updates a given principal with a set of configuration settings.
1399 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1400 (Uses the GOsa support daemon instead of the ldap database.)
1401 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1402 @return Boolean TRUE on success else FALSE.
1403 */
1404 public function krb5_set_principal($server,$name,$values)
1405 {
1406 $ret = FALSE;
1408 /* Check if the given name is a valid request value
1409 */
1410 if(!is_string($name) || empty($name)){
1411 trigger_error("The given principal name is not of type string or it is empty.");
1412 return($ret);
1413 }
1414 if(!is_array($values) || !count($values)){
1415 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1416 return($ret);
1417 }
1419 /* Check if the given server is a valid mac address
1420 */
1421 if(!tests::is_mac($server)){
1422 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1423 return($ret);
1424 }
1426 $attrs = "";
1427 foreach($values as $key => $value){
1428 if(empty($key) || is_numeric($key)){
1429 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1430 return($ret);
1431 }
1432 $key = strtolower($key);
1433 if(is_array($value)){
1434 foreach($value as $val){
1435 $attrs.= "<$key>$val</$key>\n";
1436 }
1437 }else{
1438 $attrs.= "<$key>$value</$key>\n";
1439 }
1440 }
1442 /* Prepare request event
1443 */
1444 $xml_msg =
1445 "<xml>".
1446 "<header>gosa_krb5_modify_principal</header>".
1447 "<principal>".$name."</principal>".
1448 $attrs.
1449 "<source>GOSA</source>".
1450 "<target>".$server."</target>".
1451 "</xml>";
1453 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1454 }
1457 /*! \brief Removes the given principal.
1458 (Uses the GOsa support daemon instead of the ldap database.)
1459 @pram String The name of the principal. (e.g. peter@EXAMPLE.DE)
1460 @return Boollean TRUE on success else FALSE
1461 */
1462 public function krb5_del_principal($server,$name)
1463 {
1464 $ret = FALSE;
1466 /* Check if the given name is a valid request value
1467 */
1468 if(!is_string($name) || empty($name)){
1469 trigger_error("The given principal name is not of type string or it is empty.");
1470 return($ret);
1471 }
1473 /* Check if the given server is a valid mac address
1474 */
1475 if(!tests::is_mac($server)){
1476 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1477 return($ret);
1478 }
1480 /* Prepare request event
1481 */
1482 $xml_msg =
1483 "<xml>".
1484 "<header>gosa_krb5_del_principal</header>".
1485 "<principal>".$name."</principal>".
1486 "<source>GOSA</source>".
1487 "<target>".$server."</target>".
1488 "</xml>";
1490 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1491 }
1494 /*! \brief Returns a list of configured password policies.
1495 (Uses the GOsa support daemon instead of the ldap database.)
1496 @return Array A list of all configured password policies.
1497 */
1498 public function krb5_list_policies($server)
1499 {
1500 $res = array();
1502 /* Check if the given server is a valid mac address
1503 */
1504 if(!tests::is_mac($server)){
1505 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1506 return($ret);
1507 }
1509 /* Prepare request event
1510 */
1511 $xml_msg =
1512 "<xml>".
1513 "<header>gosa_krb5_list_policies</header>".
1514 "<source>GOSA</source>".
1515 "<target>".$server."</target>".
1516 "</xml>";
1518 $res = $this->_send($xml_msg,TRUE);
1520 /* Check if there are results for POLICY
1521 */
1522 if(isset($res['XML']['POLICY'])){
1524 /* Ensure that we return an array
1525 */
1526 $tmp = $res['XML']['POLICY'];
1527 if(!is_array($tmp)){
1528 $tmp = array($tmp);
1529 }
1530 return($tmp);
1531 }else{
1532 return(array());
1533 }
1534 }
1537 /*! \brief Returns a list of configured password policies.
1538 (Uses the GOsa support daemon instead of the ldap database.)
1539 @return Array The policy settings for the given policy name.
1540 */
1541 public function krb5_get_policy($server,$name)
1542 {
1543 $res = array();
1545 /* Check if the given name is a valid request value
1546 */
1547 if(!is_string($name) || empty($name)){
1548 trigger_error("The given policy name is not of type string or it is empty.");
1549 return($ret);
1550 }
1552 /* Check if the given server is a valid mac address
1553 */
1554 if(!tests::is_mac($server)){
1555 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1556 return($ret);
1557 }
1559 /* Prepare request event
1560 */
1561 $xml_msg =
1562 "<xml>".
1563 "<header>gosa_krb5_get_policy</header>".
1564 "<policy>".$name."</policy>".
1565 "<source>GOSA</source>".
1566 "<target>".$server."</target>".
1567 "</xml>";
1569 /* Possible attributes */
1570 $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1571 "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1574 $tmp = $this->_send($xml_msg,TRUE);
1575 if(isset($tmp['XML'])){
1576 foreach($attrs as $attr){
1577 if(isset($tmp['XML'][$attr])){
1578 $ret[$attr] = $tmp['XML'][$attr];
1579 }else{
1580 $ret[$attr] = "";
1581 }
1582 }
1583 }
1584 return($ret);
1585 }
1588 /*! \brief Creates a new policy with a given set of configuration settings.
1589 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1590 (Uses the GOsa support daemon instead of the ldap database.)
1591 @pram String The name of the policy to update.
1592 @pram Array The attributes to update
1593 @return Boolean TRUE on success else FALSE.
1594 */
1595 public function krb5_add_policy($server,$name,$values)
1596 {
1597 $ret = FALSE;
1599 /* Check if the given name is a valid request value
1600 */
1601 if(!is_string($name) || empty($name)){
1602 trigger_error("The given policy name is not of type string or it is empty.");
1603 return($ret);
1604 }
1605 if(!is_array($values) || !count($values)){
1606 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1607 return($ret);
1608 }
1610 /* Check if the given server is a valid mac address
1611 */
1612 if(!tests::is_mac($server)){
1613 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1614 return($ret);
1615 }
1618 /* Transform array into <xml>
1619 */
1620 $attrs = "";
1621 foreach($values as $id => $value){
1622 if(empty($id) || is_numeric($id)){
1623 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1624 return($ret);
1625 }
1626 $id = strtolower($id);
1627 $attrs.= "<$id>$value</$id>\n";
1628 }
1630 /* Prepare request event
1631 */
1632 $xml_msg =
1633 "<xml>".
1634 "<header>gosa_krb5_create_policy</header>".
1635 "<policy>".$name."</policy>".
1636 $attrs.
1637 "<source>GOSA</source>".
1638 "<target>".$server."</target>".
1639 "</xml>";
1641 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1642 }
1645 /*! \brief Updates a given policy with a set of configuration settings.
1646 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1647 (Uses the GOsa support daemon instead of the ldap database.)
1648 @pram String The name of the policy to update.
1649 @return Boolean TRUE on success else FALSE.
1650 */
1651 public function krb5_set_policy($server,$name,$values)
1652 {
1653 $ret = FALSE;
1655 /* Check if the given name is a valid request value
1656 */
1657 if(!is_string($name) || empty($name)){
1658 trigger_error("The given policy name is not of type string or it is empty.");
1659 return($ret);
1660 }
1661 if(!is_array($values) || !count($values)){
1662 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1663 return($ret);
1664 }
1666 /* Check if the given server is a valid mac address
1667 */
1668 if(!tests::is_mac($server)){
1669 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1670 return($ret);
1671 }
1673 /* Transform array into <xml>
1674 */
1675 $attrs = "";
1676 foreach($values as $id => $value){
1677 if(preg_match("/^policy$/i",$id)) continue;
1678 if(empty($id) || is_numeric($id)){
1679 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1680 return($ret);
1681 }
1682 $id = strtolower($id);
1683 $attrs.= "<$id>$value</$id>\n";
1684 }
1686 /* Prepare request event
1687 */
1688 $xml_msg =
1689 "<xml>".
1690 "<header>gosa_krb5_modify_policy</header>".
1691 "<policy>".$name."</policy>".
1692 $attrs.
1693 "<source>GOSA</source>".
1694 "<target>".$server."</target>".
1695 "</xml>";
1697 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1698 }
1701 /*! \brief Removes the given password policy.
1702 (Uses the GOsa support daemon instead of the ldap database.)
1703 @return Boolean TRUE on success else FALSE
1704 */
1705 public function krb5_del_policy($server,$name)
1706 {
1707 $ret = FALSE;
1709 /* Check if the given server is a valid mac address
1710 */
1711 if(!tests::is_mac($server)){
1712 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1713 return($ret);
1714 }
1716 /* Check if the given name is a valid request value
1717 */
1718 if(!is_string($name) || empty($name)){
1719 trigger_error("The given policy name is not of type string or it is empty.");
1720 return($ret);
1721 }
1723 /* Prepare request event
1724 */
1725 $xml_msg =
1726 "<xml>".
1727 "<header>gosa_krb5_del_policy</header>".
1728 "<policy>".$name."</policy>".
1729 "<source>GOSA</source>".
1730 "<target>".$server."</target>".
1731 "</xml>";
1732 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1733 }
1736 /*! \brief Sets the password of for the given principal.
1737 (Uses the GOsa support daemon instead of the ldap database.)
1738 @param String The servers mac
1739 @param String The principals name
1740 @param String $the new password.
1741 @return Boolean TRUE on success else FALSE
1742 */
1743 public function krb5_set_password($server,$name,$password)
1744 {
1745 $ret = FALSE;
1747 /* Check if the given server is a valid mac address
1748 */
1749 if(!tests::is_mac($server)){
1750 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1751 return($ret);
1752 }
1754 /* Check if the given name is a valid request value
1755 */
1756 if(!is_string($name) || empty($name)){
1757 trigger_error("The given principal name is not of type string or it is empty.");
1758 return($ret);
1759 }
1761 /* Prepare request event
1762 */
1763 $xml_msg =
1764 "<xml>".
1765 "<header>gosa_krb5_set_password</header>".
1766 "<principal>".$name."</principal>".
1767 "<password>".$password."</password>".
1768 "<source>GOSA</source>".
1769 "<target>".$server."</target>".
1770 "</xml>";
1771 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1772 }
1775 /*! \brief Returns log file informations for a given mac address
1776 @param $mac The mac address to fetch logs for.
1777 @retrun Array A Multidimensional array containing log infos.
1778 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1779 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1780 install_20080313_144450 ...
1781 */
1782 public function get_log_info_for_mac($mac)
1783 {
1784 $xml_msg = "
1785 <xml>
1786 <header>gosa_show_log_by_mac</header>
1787 <target>GOSA</target>
1788 <source>GOSA</source>
1789 <mac>".$mac."</mac>
1790 </xml>";
1792 $res = $this->_send($xml_msg,TRUE);
1793 $ret = array();
1794 if(isset($res['XML'])){
1796 /* Filter all entry that look like this
1797 MAC_00_01_6C_9D_B9_FA
1798 */
1799 foreach($res['XML'] as $name => $entry){
1800 if(preg_match("/^MAC/",$name)){
1802 /* Get list of available log files
1803 */
1804 foreach($entry as $log_date){
1805 $xml_msg2 = "<xml>
1806 <header>gosa_show_log_files_by_date_and_mac</header>
1807 <target>GOSA</target>
1808 <source>GOSA</source>
1809 <date>".$log_date."</date>
1810 <mac>".$mac."</mac>
1811 </xml>";
1813 $ret[$mac][$log_date] = array();
1814 $res = $this->_send($xml_msg2,TRUE);
1815 $ret[$mac][$log_date]['DATE_STR'] = $log_date;
1816 $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1817 if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1818 $ret[$mac][$log_date]['FILES'] = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1819 }
1820 }
1821 }
1822 }
1823 }
1824 return($ret);
1825 }
1827 public function get_log_file($mac,$date,$file)
1828 {
1829 $xml_msg ="
1830 <xml>
1831 <header>gosa_get_log_file_by_date_and_mac</header>
1832 <target>GOSA</target>
1833 <source>GOSA</source>
1834 <date>".$date."</date>
1835 <mac>".$mac."</mac>
1836 <log_file>".$file."</log_file>
1837 </xml>";
1839 $res = $this->_send($xml_msg,TRUE);
1840 if(isset($res['XML'][strtoupper($file)])){
1841 return(base64_decode($res['XML'][strtoupper($file)]));
1842 }
1843 return("");
1844 }
1850 /*****************
1851 * DAK - Functions
1852 *****************/
1854 /*! \brief Returns all currenlty queued entries for a given DAK repository
1855 @param ...
1856 @return Array All queued entries.
1857 */
1858 public function DAK_keyring_entries($server)
1859 {
1860 /* Ensure that we send the event to a valid mac address
1861 */
1862 if(!is_string($server) || !tests::is_mac($server)){
1863 trigger_error("No valid mac address given '".$server."'.");
1864 return;
1865 }
1867 /* Create query
1868 */
1869 $xml_msg = "<xml>
1870 <header>gosa_get_dak_keyring</header>
1871 <target>".$server."</target>
1872 <source>GOSA</source>
1873 </xml>";
1875 $res = $this->_send($xml_msg,TRUE);
1877 /* Check if there are results for POLICY
1878 */
1879 if(isset($res['XML'])){
1880 $ret = array();
1881 foreach($res['XML'] as $key => $entry){
1882 if(preg_match("/^ANSWER/",$key)){
1883 $ret[] = $entry;
1884 }
1885 }
1886 return($ret);
1887 }else{
1888 return(array());
1889 }
1890 }
1893 /*! \brief Imports the given key into the specified keyring (Servers mac address)
1894 @param String The servers mac address
1895 @param String The gpg key.
1896 @return Boolean TRUE on success else FALSE
1897 */
1898 public function DAK_import_key($server,$key)
1899 {
1900 /* Ensure that we send the event to a valid mac address
1901 */
1902 if(!is_string($server) || !tests::is_mac($server)){
1903 trigger_error("No valid mac address given '".$server."'.");
1904 return;
1905 }
1907 /* Check if there is some cleanup required before importing the key.
1908 There may be some Header lines like:
1909 -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.6 (GNU/Linux)
1910 */
1911 if(preg_match("/".normalizePreg("BEGIN PGP PUBLIC KEY BLOCK")."/",$key)){
1913 /* Remove header */
1914 $key = preg_replace("/^.*\n\n/sim","",$key);
1915 /* Remove footer */
1916 $key = preg_replace("/-----.*$/sim","",$key);
1917 }elseif (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $key)) {
1919 /* Encode key if it is raw.
1920 */
1921 $key = base64_encode($key);
1922 }
1924 /* Create query
1925 */
1926 $xml_msg = "<xml>
1927 <header>gosa_import_dak_key</header>
1928 <target>".$server."</target>
1929 <key>".$key."</key>
1930 <source>GOSA</source>
1931 </xml>";
1933 $res = $this->_send($xml_msg,TRUE);
1934 return($this->is_error());
1935 }
1938 /*! \brief Removes a key from the keyring on the given server.
1939 @param String The servers mac address
1940 @param String The gpg key uid.
1941 @return Boolean TRUE on success else FALSE
1942 */
1943 public function DAK_remove_key($server,$key)
1944 {
1945 /* Ensure that we send the event to a valid mac address
1946 */
1947 if(!is_string($server) || !tests::is_mac($server)){
1948 trigger_error("No valid mac address given '".$server."'.");
1949 return;
1950 }
1952 /* Create query
1953 */
1954 $xml_msg = "<xml>
1955 <header>gosa_remove_dak_key</header>
1956 <target>".$server."</target>
1957 <keyid>".$key."</keyid>
1958 <source>GOSA</source>
1959 </xml>";
1961 $res = $this->_send($xml_msg,TRUE);
1962 return($this->is_error());
1963 }
1964 }
1966 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1967 ?>