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 # load from config, store statically
50 if (isset($config->current['GOSA_SI'])){
52 if ($this->s_host == ""){
53 $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
54 $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
55 $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
56 }
58 $this->f_timeout = $timeout;
59 if($connect){
60 $this->connect();
61 }
62 }
63 }
66 /*! \brief Establish daemon connection.
67 @return boolean Returns true if the connection was succesfully established.
68 */
69 public function connect()
70 {
71 if(!empty($this->s_host) && !empty($this->i_port)){
72 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
73 if($this->o_sock->connected()){
74 $this->o_sock->setEncryptionKey($this->s_encryption_key);
75 $this->is_connected = TRUE;
76 }else{
77 $this->set_error($this->o_sock->get_error());
78 $this->disconnect();
79 new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
80 }
81 }else{
82 $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
83 }
84 return($this->is_connected);
85 }
88 /*! \brief Disconnect from gosa daemon.
89 */
90 public function disconnect()
91 {
92 $this->o_sock->close();
93 $this->is_connected = FALSE;
94 }
97 /*! \brief Sets an error message, which can be returned with get_error().
98 @param string The Error message,
99 */
100 private function set_error($str)
101 {
102 $this->b_error = TRUE;
103 $this->s_error = $str;
104 }
107 /*! \brief Sets an error message, which can be returned with get_error().
108 @param string The Error message,
109 */
110 private function reset_error()
111 {
112 $this->b_error = FALSE;
113 $this->s_error = "";
114 }
117 /*! \brief Checks if an error occured.
118 @return boolean returns TRUE or FALSE, whether there is an error or not.
119 */
120 public function is_error()
121 {
122 return($this->b_error);
123 }
126 /*! \brief Returns the last error.
127 @return Returns the last error.
128 */
129 public function get_error()
130 {
131 $str = $this->s_error;
132 $ret = "";
133 if(is_string($str)){
134 $ret = $str;
135 }else{
136 foreach($str as $msg){
137 $ret .= $msg." ";
138 }
139 }
140 $ret = preg_replace("/ /"," ",$ret);
141 return($ret);
142 }
145 public function FAI_get_kernels($release)
146 {
147 $xml_msg =
148 "<xml>".
149 "<header>gosa_get_available_kernel</header>".
150 "<source>GOSA</source>".
151 "<target>GOSA</target>".
152 "<release>".$release."</release>".
153 "</xml>";
155 $ret = array();
156 if($this->connect()){
157 $this->o_sock->write($xml_msg);
158 $str = trim($this->o_sock->read());
160 /* Check if something went wrong while reading */
161 if($this->o_sock->is_error()){
162 $this->set_error($this->o_sock->get_error());
163 return($ret);
164 }
166 $entries = $this->xml_to_array($str);
167 if(isset($entries['XML']) && is_array($entries['XML'])){
169 /* Check if returned values represent a valid answer */
170 if(isset($entries['XML'])){
171 if(isset($entries['XML']['ERROR_STRING'])) {
172 $this->set_error($entries['XML']['ERROR_STRING']);
173 new log("debug","GOsa-si",
174 get_class($this)."::".__FUNCTION__, array(),
175 "FAILED error was ".$this->get_error());
176 return($ret);
177 }
179 /* Unset header tags */
180 $ret = $entries['XML'];
181 foreach($ret as $key => $entry){
182 if(!preg_match("/^answer/i",$key)){
183 unset($ret[$key]);
184 }
185 }
186 }
187 }
188 }
189 return($ret);
190 }
193 public function FAI_get_package_sections($release)
194 {
195 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
196 "<select>distinct section</select>".
197 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
199 $ret = array();
200 if($this->connect()){
201 $this->o_sock->write($xml_msg);
202 $str = trim($this->o_sock->read());
204 /* Check if something went wrong while reading */
205 if($this->o_sock->is_error()){
206 $this->set_error($this->o_sock->get_error());
207 return($ret);
208 }
210 $entries = $this->xml_to_array($str);
211 if(isset($entries['XML']) && is_array($entries['XML'])){
213 /* Check if returned values represent a valid answer */
214 if(isset($entries['XML'])){
215 if(isset($entries['XML']['ERROR_STRING'])) {
216 $this->set_error($entries['XML']['ERROR_STRING']);
217 new log("debug","GOsa-si",
218 get_class($this)."::".__FUNCTION__, array(),
219 "FAILED error was ".$this->get_error());
220 return($ret);
221 }
223 /* Unset header tags */
224 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
225 if(isset($entries['XML'][$type])){
226 unset($entries['XML'][$type]);
227 }
228 }
229 $ret = $entries['XML'];
230 }
231 }
232 }
233 return($ret);
234 }
237 public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
238 {
239 $this->reset_error();
240 $ret = array();
242 /* Check Parameter */
243 if(!is_array($attrs) || !count($attrs)){
244 trigger_error("Second parameter must be an array. With at least one attribute name.");
245 return($ret);
246 }
248 /* Check Parameter */
249 if(!is_array($package)){
250 trigger_error("Third parameter must be an array. With at least one attribute name.");
251 return($ret);
252 }
254 /* Create list of attributes to fetch */
255 $attr = "";
256 foreach($attrs as $at){
257 $attr.= "<select>".$at."</select>";
258 }
260 /* If no package is given, search for all */
261 if(!count($package)) $package = array("%");
263 /* Create limit tag */
264 if($from == -1){
265 $limit ="";
266 }else{
267 $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
268 }
270 /* Create list of attributes to fetch */
271 $pkgs = "";
272 foreach($package as $pkg){
273 $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
274 }
276 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
277 $attr.
278 "<where>
279 <clause><phrase><distribution>".$release."</distribution></phrase></clause>
280 <clause><connector>OR</connector>
281 ".$pkgs."
282 </clause>
283 </where>".
284 $limit.
285 "</xml>";
287 if($this->connect()){
288 $this->o_sock->write($xml_msg);
289 $str = trim($this->o_sock->read());
291 /* Check if something went wrong while reading */
292 if($this->o_sock->is_error()){
293 $this->set_error($this->o_sock->get_error());
294 return($ret);
295 }
297 $entries = $this->xml_to_array($str);
298 if(isset($entries['XML']) && is_array($entries['XML'])){
300 /* Check if returned values represent a valid answer */
301 if(isset($entries['XML'])){
302 if(isset($entries['XML']['ERROR_STRING'])) {
303 $this->set_error($entries['XML']['ERROR_STRING']);
304 new log("debug","GOsa-si",
305 get_class($this)."::".__FUNCTION__, array(),
306 "FAILED error was ".$this->get_error());
307 return($ret);
308 }
310 /* Unset header tags */
311 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
312 if(isset($entries['XML'][$type])){
313 unset($entries['XML'][$type]);
314 }
315 }
316 $ret = $entries['XML'];
317 }
318 }
319 }
320 return($ret);
323 }
326 public function FAI_get_server($name = "")
327 {
328 $this->reset_error();
330 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
331 $ret = array();
332 if($this->connect()){
333 $this->o_sock->write($xml_msg);
334 $str = trim($this->o_sock->read());
336 /* Check if something went wrong while reading */
337 if($this->o_sock->is_error()){
338 $this->set_error($this->o_sock->get_error());
339 return($ret);
340 }
342 $entries = $this->xml_to_array($str);
343 if(isset($entries['XML']) && is_array($entries['XML'])){
345 /* Check if returned values represent a valid answer */
346 if(isset($entries['XML'])){
347 if(isset($entries['XML']['ERROR_STRING'])) {
348 $this->set_error($entries['XML']['ERROR_STRING']);
349 new log("debug","GOsa-si",
350 get_class($this)."::".__FUNCTION__, array(),
351 "FAILED error was ".$this->get_error());
352 return($ret);
353 }
355 /* Unset header tags */
356 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
357 if(isset($entries['XML'][$type])){
358 unset($entries['XML'][$type]);
359 }
360 }
361 $ret = $entries['XML'];
362 }
363 }
364 }
365 return($ret);
366 }
369 public function FAI_get_classes($name)
370 {
371 $this->reset_error();
372 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
373 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
374 $ret = array();
375 if($this->connect()){
376 $this->o_sock->write($xml_msg);
377 $str = trim($this->o_sock->read());
379 /* Check if something went wrong while reading */
380 if($this->o_sock->is_error()){
381 $this->set_error($this->o_sock->get_error());
382 return($ret);
383 }
385 $entries = $this->xml_to_array($str);
386 if(isset($entries['XML']) && is_array($entries['XML'])){
388 /* Check if returned values represent a valid answer */
389 if(isset($entries['XML'])){
390 if(isset($entries['XML']['ERROR_STRING'])) {
391 $this->set_error($entries['XML']['ERROR_STRING']);
392 new log("debug","GOsa-si",
393 get_class($this)."::".__FUNCTION__, array($name),
394 "FAILED error was ".$this->get_error());
395 return($ret);
396 }
398 /* Unset header tags */
399 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
400 if(isset($entries['XML'][$type])){
401 unset($entries['XML'][$type]);
402 }
403 }
404 $ret = $entries['XML'];
405 }
406 }
407 }
408 return($ret);
409 }
412 /*! \brief Returns an array containing all queued entries.
413 @return Array All queued entries as an array.
414 */
415 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
416 {
417 $this->reset_error();
418 $ret = array();
420 $tags = "";
421 foreach($event_types as $type){
422 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
423 }
424 if(count($event_types) > 1){
425 $tags = "<connector>or</connector>".$tags;
426 }
427 if(count($event_types)){
428 $tags = "<where><clause>".$tags."</clause></where>";
429 }
431 $xml_msg =
432 "<xml>
433 <header>gosa_query_jobdb</header>
434 <target>GOSA</target>
435 <source>GOSA</source>
436 ".$tags."
438 <orderby>".$sort."</orderby>";
439 if($from != -1 && $to != -1){
440 $xml_msg.= "
441 <limit>
442 <from>".$from."</from>
443 <to>".$to."</to>
444 </limit>";
445 }
446 $xml_msg.= "
447 </xml>";
449 if($this->connect()){
450 $this->o_sock->write($xml_msg);
451 $str = trim($this->o_sock->read());
453 /* Check if something went wrong while reading */
454 if($this->o_sock->is_error()){
455 $this->set_error($this->o_sock->get_error());
456 return($ret);
457 }
459 $entries = $this->xml_to_array($str);
460 if(isset($entries['XML']) && is_array($entries['XML'])){
462 /* Check if returned values represent a valid answer */
463 if(isset($entries['XML'])){
465 /* Unset header tags */
466 foreach(array("HEADER","SOURCE","TARGET") as $type){
467 unset($entries['XML'][$type]);
468 }
469 $ret = $entries['XML'];
470 }
471 }
472 }
474 /* Remove session ID. No one is interested in this... */
475 unset($ret['SESSION_ID']);
477 return($ret);
478 }
481 /*! \brief Checks if the given ids are used queue ids.
482 @param Array The ids we want to check..
483 @return Array An array containing all ids as index and TRUE/FALSE as value.
484 */
485 public function ids_exist($ids)
486 {
487 if(!is_array($ids)){
488 trigger_error("Requires an array as parameter.");
489 return;
490 }
491 $this->reset_error();
493 $ret = array();
495 $xml_msg = "<xml>
496 <header>gosa_query_jobdb</header>
497 <target>GOSA</target>
498 <source>GOSA</source>
499 <where>
500 <clause>
501 <connector>or</connector>";
502 foreach($ids as $id){
503 $xml_msg .= "<phrase>
504 <operator>eq</operator>
505 <id>".$id."</id>
506 </phrase>";
507 }
508 $xml_msg .= "</clause>
509 </where>
510 </xml>";
512 if($this->connect()){
513 $this->o_sock->write($xml_msg);
514 $str = trim($this->o_sock->read());
516 /* Check if something went wrong while reading */
517 if($this->o_sock->is_error()){
518 $this->set_error($this->o_sock->get_error());
519 return($ret);
520 }
522 $entries = $this->xml_to_array($str);
523 if(isset($entries['XML']) && is_array($entries['XML'])){
524 foreach($entries['XML'] as $entry){
525 if(is_array($entry) && array_key_exists("ID",$entry)){
526 $ret[] = $entry['ID'];
527 }
528 }
529 }
530 }
531 return($ret);
532 }
535 /*! \brief Returns an entry containing all requested ids.
536 @param Array The IDs of the entries we want to return.
537 @return Array Of the requested entries.
538 */
539 public function get_entries_by_mac($macs)
540 {
541 if(!is_array($macs)){
542 trigger_error("Requires an array as parameter.");
543 return;
544 }
545 $this->reset_error();
547 $ret = array();
549 $xml_msg = "<xml>
550 <header>gosa_query_jobdb</header>
551 <target>GOSA</target>
552 <source>GOSA</source>
553 <where>
554 <clause>
555 <connector>or</connector>";
556 foreach($macs as $mac){
557 $xml_msg .= "<phrase>
558 <operator>eq</operator>
559 <macaddress>".$mac."</macaddress>
560 </phrase>";
561 }
562 $xml_msg .= "</clause>
563 </where>
564 </xml>";
566 if($this->connect()){
567 $this->o_sock->write($xml_msg);
568 $str = trim($this->o_sock->read());
570 /* Check if something went wrong while reading */
571 if($this->o_sock->is_error()){
572 $this->set_error($this->o_sock->get_error());
573 return($ret);
574 }
576 $entries = $this->xml_to_array($str);
577 if(isset($entries['XML'])){
578 foreach($entries['XML'] as $name => $entry){
579 if(preg_match("/^ANSWER[0-9]*$/",$name)){
580 $ret[$name] = $entry;
581 }
582 }
583 }
584 }
585 return($ret);
586 }
589 /*! \brief Returns an entry containing all requested ids.
590 @param Array The IDs of the entries we want to return.
591 @return Array Of the requested entries.
592 */
593 public function get_entries_by_id($ids)
594 {
595 if(!is_array($ids)){
596 trigger_error("Requires an array as parameter.");
597 return;
598 }
599 $this->reset_error();
601 $ret = array();
603 $xml_msg = "<xml>
604 <header>gosa_query_jobdb</header>
605 <target>GOSA</target>
606 <source>GOSA</source>
607 <where>
608 <clause>
609 <connector>or</connector>";
610 foreach($ids as $id){
611 $xml_msg .= "<phrase>
612 <operator>eq</operator>
613 <id>".$id."</id>
614 </phrase>";
615 }
616 $xml_msg .= "</clause>
617 </where>
618 </xml>";
620 if($this->connect()){
621 $this->o_sock->write($xml_msg);
622 $str = trim($this->o_sock->read());
624 /* Check if something went wrong while reading */
625 if($this->o_sock->is_error()){
626 $this->set_error($this->o_sock->get_error());
627 return($ret);
628 }
630 $entries = $this->xml_to_array($str);
631 if(isset($entries['XML'])){
632 foreach($entries['XML'] as $name => $entry){
633 if(preg_match("/^ANSWER[0-9]*$/",$name)){
634 $ret[$name] = $entry;
635 }
636 }
637 }
638 }
639 return($ret);
640 }
643 /*! \brief Checks if the given id is in use.
644 @param Integer The ID of the entry.
645 @return Boolean TRUE if entry exists.
646 */
647 public function id_exists($id)
648 {
649 if(!is_numeric($id)){
650 trigger_error("Requires an integer as parameter.");
651 return;
652 }
654 $this->reset_error();
656 $xml_msg = "<xml>
657 <header>gosa_query_jobdb</header>
658 <target>GOSA</target>
659 <source>GOSA</source>
660 <where>
661 <clause>
662 <phrase>
663 <operator>eq</operator>
664 <id>".$id."</id>
665 </phrase>
666 </clause>
667 </where>
668 </xml>";
670 if($this->connect()){
671 $this->o_sock->write($xml_msg);
672 $str = trim($this->o_sock->read());
674 /* Check if something went wrong while reading */
675 if($this->o_sock->is_error()){
676 $this->set_error($this->o_sock->get_error());
677 return(FALSE);
678 }
680 $entries = $this->xml_to_array($str);
681 if( isset($entries['XML']['HEADER']) &&
682 $entries['XML']['HEADER']=="answer" &&
683 isset($entries['XML']['ANSWER1'])){
684 return(TRUE);
685 }
686 }
687 return(FALSE);
688 }
691 /*! \brief Returns an entry from the gosaSupportQueue
692 @param Integer The ID of the entry we want to return.
693 @return Array Of the requested entry.
694 */
695 public function get_entry_by_id($id)
696 {
697 if(!is_numeric($id)){
698 trigger_error("Requires an integer as parameter.");
699 return;
700 }
701 $this->reset_error();
703 $ret = array();
704 $xml_msg = "<xml>
705 <header>gosa_query_jobdb</header>
706 <target>GOSA</target>
707 <source>GOSA</source>
708 <where>
709 <clause>
710 <phrase>
711 <operator>eq</operator>
712 <id>".$id."</id>
713 </phrase>
714 </clause>
715 </where>
716 </xml>";
717 if($this->connect()){
718 $this->o_sock->write($xml_msg);
719 $str = trim($this->o_sock->read());
721 /* Check if something went wrong while reading */
722 if($this->o_sock->is_error()){
723 $this->set_error($this->o_sock->get_error());
724 return($ret);
725 }
727 $entries = $this->xml_to_array($str);
728 if( isset($entries['XML']['HEADER']) &&
729 $entries['XML']['HEADER']=="answer" &&
730 isset($entries['XML']['ANSWER1'])){
731 $ret = $entries['XML']['ANSWER1'];
732 }
733 }
734 return($ret);
735 }
738 /*! \brief Removes a set of entries from the GOsa support queue.
739 @param Array The IDs to remove.
740 @return Boolean True on success.
741 */
742 public function remove_entries($ids)
743 {
744 if(!is_array($ids)){
745 trigger_error("Requires an array as parameter.");
746 return;
747 }
749 $this->reset_error();
751 $ret = array();
753 $xml_msg = "<xml>
754 <header>gosa_delete_jobdb_entry</header>
755 <target>GOSA</target>
756 <source>GOSA</source>
757 <where>
758 <clause>
759 <connector>or</connector>";
760 foreach($ids as $id){
761 $xml_msg .= "<phrase>
762 <operator>eq</operator>
763 <id>".$id."</id>
764 </phrase>";
765 }
766 $xml_msg .= "</clause>
767 </where>
768 </xml>";
770 if($this->connect()){
771 $this->o_sock->write($xml_msg);
772 $str = $this->o_sock->read();
774 /* Check if something went wrong while reading */
775 if($this->o_sock->is_error()){
776 $this->set_error($this->o_sock->get_error());
777 return($ret);
778 }
780 $entries = $this->xml_to_array($str);
781 if(isset($entries['XML']) || isset($entries['COUNT'])){
782 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
783 return(TRUE);
784 }else{
785 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
786 }
787 }
788 return(FALSE);
789 }
793 /*! \brief Removes an entry from the GOsa support queue.
794 @param Integer The ID of the entry we want to remove.
795 @return Boolean True on success.
796 */
797 public function remove_entry($id)
798 {
799 return($this->remove_entries(array($id)));
800 }
803 /*! \brief Parses the given xml string into an array
804 @param String XML string
805 @return Array Returns an array containing the xml structure.
806 */
807 private function xml_to_array($xml)
808 {
809 $params = array();
810 $level = array();
811 $parser = xml_parser_create_ns();
812 xml_parse_into_struct($parser, $xml, $vals, $index);
814 $err_id = xml_get_error_code($parser);
815 if($err_id){
816 xml_parser_free($parser);
817 }else{
818 xml_parser_free($parser);
820 foreach ($vals as $xml_elem) {
821 if ($xml_elem['type'] == 'open') {
822 if (array_key_exists('attributes',$xml_elem)) {
823 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
824 } else {
825 $level[$xml_elem['level']] = $xml_elem['tag'];
826 }
827 }
828 if ($xml_elem['type'] == 'complete') {
829 $start_level = 1;
830 $test2 = &$params;
831 while($start_level < $xml_elem['level']) {
832 $test2 = &$test2[$level[$start_level]];
833 $start_level++;
834 }
835 if(!isset($test2[$xml_elem['tag']])){
836 if(isset($xml_elem['value'])){
837 $test2[$xml_elem['tag']] = $xml_elem['value'];
838 }
839 }else{
840 if(!is_array($test2[$xml_elem['tag']])){
841 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
842 }
843 $test2[$xml_elem['tag']][] = $xml_elem['value'];
844 }
845 }
846 }
847 }
849 if(!isset($params['XML'])){
850 if (!array_key_exists('XML', $params)){
851 $this->set_error(_("Cannot not parse XML!"));
852 }
853 $params = array("COUNT" => 0);
854 }
856 return($params);
857 }
860 /*! \brief Updates an entry with a set of new values,
861 @param Integer The ID of the entry, we want to update.
862 @param Array The variables to update.
863 @return Boolean Returns TRUE on success.
864 */
865 public function update_entries($ids,$data)
866 {
867 $this->reset_error();
868 if(!is_array($ids)){
869 trigger_error("Requires an array as first parameter.");
870 return;
871 }
873 if(!is_array($data)){
874 trigger_error("Requires an array as second parameter.");
875 return;
876 }
878 $attr = "";
879 foreach($data as $key => $value){
880 $key = strtolower($key);
881 if(is_array($value)){
882 foreach($value as $sub_value){
883 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
884 }
885 }else{
886 $attr.= "<$key>".strtolower($value)."</$key>\n";
887 }
888 }
890 $xml_msg = "<xml>
891 <header>gosa_update_status_jobdb_entry</header>
892 <target>GOSA</target>
893 <source>GOSA</source>
894 <where>
895 <clause>
896 <connector>or</connector>";
897 foreach($ids as $id){
898 $xml_msg .= "<phrase>
899 <operator>eq</operator>
900 <id>".$id."</id>
901 </phrase>";
902 }
903 $xml_msg .= "</clause>
904 </where>
905 <update>
906 ".$attr."
907 </update>
908 </xml>";
910 if($this->connect()){
912 $this->o_sock->write($xml_msg);
913 $str = trim($this->o_sock->read());
915 /* Check if something went wrong while reading */
916 if($this->o_sock->is_error()){
917 $this->set_error($this->o_sock->get_error());
918 return(FALSE);
919 }
921 $entries = $this->xml_to_array($str);
922 if(isset($entries['XML'])){
923 if(isset($entries['XML']['ERROR_STRING'])) {
924 $this->set_error($entries['XML']['ERROR_STRING']);
925 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
926 return(FALSE);
927 }
928 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
929 return(TRUE);
930 }
931 }
932 return(FALSE);
933 }
936 /*! \brief Returns the number of currently queued objects.
937 @return Integer
938 */
939 public function number_of_queued_entries($event_types)
940 {
941 $tags = "";
942 foreach($event_types as $type){
943 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
944 }
945 if(count($event_types) > 1){
946 $tags = "<connector>or</connector>".$tags;
947 }
948 if(count($event_types)){
949 $tags = "<where><clause>".$tags."</clause></where>";
950 }
953 $xml_msg =
954 "<xml>".
955 "<header>gosa_query_jobdb</header>".
956 "<target>GOSA</target>".
957 "<source>GOSA</source>".
958 "<select> count ID</select>".
959 $tags.
960 "</xml>";
962 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
963 $this->connect();
964 if($this->connect()){
965 $this->o_sock->write($xml_msg);
966 $str = trim($this->o_sock->read());
968 /* Check if something went wrong while reading */
969 if($this->o_sock->is_error()){
970 $this->set_error($this->o_sock->get_error());
971 return(0);
972 }
974 $entries = $this->xml_to_array($str);
975 if(isset($entries['XML'])){
976 return($entries['XML']['COUNT']);
977 }
978 }
979 return(-1);
980 }
983 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
984 {
985 $xml_message= "";
987 /* Prepare data */
988 foreach ($data as $key => $value){
989 if(is_array($value)){
990 foreach($value as $sub_value){
991 $xml_message.= "<$key>$sub_value</$key>";
992 }
993 }else{
994 $xml_message.= "<$key>$value</$key>";
995 }
996 }
998 /* Multiple targets? */
999 if (!is_array($to)){
1000 $to_targets= array($to);
1001 } else {
1002 $to_targets= $to;
1003 }
1005 /* Build target strings */
1006 $target ="";
1007 foreach($to_targets as $to){
1008 $target.= "<target>$to</target>";
1009 }
1011 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1012 }
1015 /* Allows simply appending a new DaemonEvent
1016 */
1017 public function append($event)
1018 {
1019 if(!($event instanceof DaemonEvent)){
1020 return(FALSE);
1021 }
1023 $this->reset_error();
1025 /* Add to queue if new
1026 */
1027 if($event->is_new()){
1029 $request_answer = FALSE;
1030 if($event->get_type() == SCHEDULED_EVENT){
1031 $action = $event->get_schedule_action();
1032 }elseif($event->get_type() == TRIGGERED_EVENT){
1033 $action = $event->get_trigger_action();
1034 }else{
1035 trigger_error("Unknown type of queue event given.");
1036 return(FALSE);
1037 }
1039 /* Get event informations, like targets..
1040 */
1041 $targets = $event->get_targets();
1042 $data = $event->save();
1044 /* Append an entry for each target
1045 */
1046 foreach($targets as $target){
1047 $data['macaddress'] = $target;
1048 $this->send_data($action,$target,$data,$request_answer);
1050 if($this->is_error()){
1051 return(FALSE);
1052 }
1053 }
1054 return(TRUE);
1055 }else{
1057 /* Updated edited entry.
1058 */
1059 $id = $event->get_id();
1060 $data = $event->save();
1061 return($this->update_entries(array($id),$data));
1062 }
1064 return(FALSE);
1065 }
1068 /*! \brief Returns an array containing all queued entries.
1069 @return Array All queued entries as an array.
1070 */
1071 public function _send($data, $answer_expected= FALSE)
1072 {
1073 $this->reset_error();
1074 $ret = array();
1076 if($this->connect()){
1077 $this->o_sock->write($data);
1078 if ($answer_expected){
1079 $str = trim($this->o_sock->read());
1081 /* Check if something went wrong while reading */
1082 if($this->o_sock->is_error()){
1083 $this->set_error($this->o_sock->get_error());
1084 return($ret);
1085 }
1087 $entries = $this->xml_to_array($str);
1088 if(isset($entries['XML']) && is_array($entries['XML'])){
1089 $ret = $entries;
1090 if(isset($entries['XML']['ERROR_STRING'])) {
1091 $this->set_error($entries['XML']['ERROR_STRING']);
1092 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1093 }elseif(isset($entries['XML']['ERROR'])){
1094 $this->set_error($entries['XML']['ERROR']);
1095 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1096 }else{
1097 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1098 }
1099 }
1100 }else{
1101 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1102 }
1103 }
1104 return($ret);
1105 }
1108 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1109 {
1110 $xml_message= "";
1112 /* Get communication object */
1113 $d= new gosaSupportDaemon(TRUE,10);
1115 /* Prepare data */
1116 foreach ($data as $key => $value){
1117 if(is_array($value)){
1118 foreach($value as $sub_val){
1119 $xml_message.= "<$key>$sub_val</$key>";
1120 }
1121 }else{
1122 $xml_message.= "<$key>$value</$key>";
1123 }
1124 }
1126 /* Multiple targets? */
1127 if (!is_array($to)){
1128 $to_targets= array($to);
1129 } else {
1130 $to_targets= $to;
1131 }
1133 /* Build target strings */
1134 $target ="";
1135 foreach($to_targets as $to){
1136 $target.= "<target>$to</target>";
1137 }
1139 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1140 }
1143 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1144 @param String $mac The mac address for which we want to remove all jobs.
1145 */
1146 function clean_queue_from_mac($mac)
1147 {
1148 global $config;
1150 /* First of all we have to check which jobs are startet
1151 * for $mac
1152 */
1153 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1155 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1157 $data = $this->_send($xml_msg,TRUE);
1158 if(is_array($data) && isset($data['XML'])){
1159 $already_aborted = FALSE;
1160 foreach($data['XML'] as $name => $entry){
1161 if(preg_match("/answer[0-9]*/i",$name)){
1162 $entry['STATUS'] = strtoupper($entry['STATUS']);
1163 switch($entry['STATUS']){
1165 case 'PROCESSING' :
1167 /* Send abort event, but only once
1168 */
1169 if($already_aborted){
1170 break;
1171 }elseif(class_available("DaemonEvent_faireboot")){
1172 $already_aborted = TRUE;
1173 $tmp = new DaemonEvent_faireboot($config);
1174 $tmp->add_targets(array($mac));
1175 $tmp->set_type(TRIGGERED_EVENT);
1176 if(!$this->append($tmp)){
1177 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1178 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1179 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1180 }else{
1181 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1182 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1183 }
1184 ;break;
1185 }else{
1186 /* Couldn't find abort event, just remove entry */
1187 }
1189 case 'WAITING':
1190 case 'ERROR':
1191 default :
1193 /* Simply remove entries from queue.
1194 * Failed or waiting events, can be removed without any trouble.
1195 */
1196 if(!$this->remove_entries(array($entry['ID']))){
1197 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1198 }
1199 ;break;
1200 }
1202 }
1203 }
1204 }
1205 }
1208 static function ping($target)
1209 {
1210 if (tests::is_mac($target)){
1211 /* Get communication object */
1212 $d= new gosaSupportDaemon(TRUE,0.5);
1213 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1214 return (count($answer) ? TRUE:FALSE);
1215 }
1216 return (FALSE);
1217 }
1221 /*! \brief Returns a list of all configured principals.
1222 (Uses the GOsa support daemon instead of the ldap database.)
1223 @return Array A list containing the names of all configured principals.
1224 */
1225 public function krb5_list_principals($server)
1226 {
1227 $res = array();
1229 /* Check if the given server is a valid mac address
1230 */
1231 if(!tests::is_mac($server)){
1232 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1233 return($ret);
1234 }
1236 /* Prepare request event
1237 */
1238 $xml_msg =
1239 "<xml>".
1240 "<header>gosa_krb5_list_principals</header>".
1241 "<source>GOSA</source>".
1242 "<target>".$server."</target>".
1243 "</xml>";
1245 $tmp = $this->_send($xml_msg,TRUE);
1246 if(isset($tmp['XML']['PRINCIPAL'])){
1247 return($tmp['XML']['PRINCIPAL']);
1248 }else{
1249 return($res);
1250 }
1251 }
1254 /*! \brief Returns the configuration settings for a given principal name.
1255 (Uses the GOsa support daemon instead of the ldap database.)
1256 @pram String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1257 @return Array A list containing the names of all configured principals.
1258 */
1259 public function krb5_get_principal($server,$name)
1260 {
1261 $ret = array();
1263 /* Check if the given name is a valid request value
1264 */
1265 if(!is_string($name) || empty($name)){
1266 trigger_error("The given principal name is not of type string or it is empty.");
1267 return($ret);
1268 }
1270 /* Check if the given server is a valid mac address
1271 */
1272 if(!tests::is_mac($server)){
1273 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1274 return($ret);
1275 }
1277 /* Prepare request event
1278 */
1279 $xml_msg =
1280 "<xml>".
1281 "<header>gosa_krb5_get_principal</header>".
1282 "<principal>".$name."</principal>".
1283 "<source>GOSA</source>".
1284 "<target>".$server."</target>".
1285 "</xml>";
1287 $res = $this->_send($xml_msg,TRUE);
1288 if(isset($res['XML'])){
1289 return($res['XML']);
1290 }else{
1291 return($ret);
1292 }
1293 }
1296 /*! \brief Creates a given principal with a set of configuration settings.
1297 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1298 (Uses the GOsa support daemon instead of the ldap database.)
1299 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1300 @return Boolean TRUE on success else FALSE.
1301 */
1302 public function krb5_add_principal($server,$name,$values)
1303 {
1304 $ret = FALSE;
1306 /* Check if the given name is a valid request value
1307 */
1308 if(!is_string($name) || empty($name)){
1309 trigger_error("The given principal name is not of type string or it is empty.");
1310 return($ret);
1311 }
1312 if(!is_array($values)){
1313 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1314 return($ret);
1315 }
1317 /* Check if the given server is a valid mac address
1318 */
1319 if(!tests::is_mac($server)){
1320 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1321 return($ret);
1322 }
1324 $attrs = "";
1325 foreach($values as $key => $value){
1326 if(empty($key) || is_numeric($key)){
1327 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1328 return($ret);
1329 }
1330 $key = strtolower($key);
1331 if(is_array($value)){
1332 foreach($value as $val){
1333 $attrs.= "<$key>$val</$key>\n";
1334 }
1335 }else{
1336 $attrs.= "<$key>$value</$key>\n";
1337 }
1338 }
1340 /* Prepare request event
1341 */
1342 $xml_msg =
1343 "<xml>".
1344 "<header>gosa_krb5_create_principal</header>".
1345 "<principal>".$name."</principal>".
1346 $attrs.
1347 "<source>GOSA</source>".
1348 "<target>".$server."</target>".
1349 "</xml>";
1351 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1352 }
1355 function krb5_ramdomize_key($server,$name)
1356 {
1357 /* Prepare request event
1358 */
1359 $xml_msg =
1360 "<xml>".
1361 "<header>gosa_krb5_randomize_key</header>".
1362 "<principal>".$name."</principal>".
1363 "<source>GOSA</source>".
1364 "<target>".$server."</target>".
1365 "</xml>";
1367 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1368 }
1372 /*! \brief Updates a given principal with a set of configuration settings.
1373 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1374 (Uses the GOsa support daemon instead of the ldap database.)
1375 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1376 @return Boolean TRUE on success else FALSE.
1377 */
1378 public function krb5_set_principal($server,$name,$values)
1379 {
1380 $ret = FALSE;
1382 /* Check if the given name is a valid request value
1383 */
1384 if(!is_string($name) || empty($name)){
1385 trigger_error("The given principal name is not of type string or it is empty.");
1386 return($ret);
1387 }
1388 if(!is_array($values) || !count($values)){
1389 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1390 return($ret);
1391 }
1393 /* Check if the given server is a valid mac address
1394 */
1395 if(!tests::is_mac($server)){
1396 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1397 return($ret);
1398 }
1400 $attrs = "";
1401 foreach($values as $key => $value){
1402 if(empty($key) || is_numeric($key)){
1403 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1404 return($ret);
1405 }
1406 $key = strtolower($key);
1407 if(is_array($value)){
1408 foreach($value as $val){
1409 $attrs.= "<$key>$val</$key>\n";
1410 }
1411 }else{
1412 $attrs.= "<$key>$value</$key>\n";
1413 }
1414 }
1416 /* Prepare request event
1417 */
1418 $xml_msg =
1419 "<xml>".
1420 "<header>gosa_krb5_modify_principal</header>".
1421 "<principal>".$name."</principal>".
1422 $attrs.
1423 "<source>GOSA</source>".
1424 "<target>".$server."</target>".
1425 "</xml>";
1427 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1428 }
1431 /*! \brief Removes the given principal.
1432 (Uses the GOsa support daemon instead of the ldap database.)
1433 @pram String The name of the principal. (e.g. peter@EXAMPLE.DE)
1434 @return Boollean TRUE on success else FALSE
1435 */
1436 public function krb5_del_principal($server,$name)
1437 {
1438 $ret = FALSE;
1440 /* Check if the given name is a valid request value
1441 */
1442 if(!is_string($name) || empty($name)){
1443 trigger_error("The given principal name is not of type string or it is empty.");
1444 return($ret);
1445 }
1447 /* Check if the given server is a valid mac address
1448 */
1449 if(!tests::is_mac($server)){
1450 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1451 return($ret);
1452 }
1454 /* Prepare request event
1455 */
1456 $xml_msg =
1457 "<xml>".
1458 "<header>gosa_krb5_del_principal</header>".
1459 "<principal>".$name."</principal>".
1460 "<source>GOSA</source>".
1461 "<target>".$server."</target>".
1462 "</xml>";
1464 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1465 }
1468 /*! \brief Returns a list of configured password policies.
1469 (Uses the GOsa support daemon instead of the ldap database.)
1470 @return Array A list of all configured password policies.
1471 */
1472 public function krb5_list_policies($server)
1473 {
1474 $res = array();
1476 /* Check if the given server is a valid mac address
1477 */
1478 if(!tests::is_mac($server)){
1479 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1480 return($ret);
1481 }
1483 /* Prepare request event
1484 */
1485 $xml_msg =
1486 "<xml>".
1487 "<header>gosa_krb5_list_policies</header>".
1488 "<source>GOSA</source>".
1489 "<target>".$server."</target>".
1490 "</xml>";
1492 $res = $this->_send($xml_msg,TRUE);
1494 /* Check if there are results for POLICY
1495 */
1496 if(isset($res['XML']['POLICY'])){
1498 /* Ensure that we return an array
1499 */
1500 $tmp = $res['XML']['POLICY'];
1501 if(!is_array($tmp)){
1502 $tmp = array($tmp);
1503 }
1504 return($tmp);
1505 }else{
1506 return(array());
1507 }
1508 }
1511 /*! \brief Returns a list of configured password policies.
1512 (Uses the GOsa support daemon instead of the ldap database.)
1513 @return Array The policy settings for the given policy name.
1514 */
1515 public function krb5_get_policy($server,$name)
1516 {
1517 $res = array();
1519 /* Check if the given name is a valid request value
1520 */
1521 if(!is_string($name) || empty($name)){
1522 trigger_error("The given policy name is not of type string or it is empty.");
1523 return($ret);
1524 }
1526 /* Check if the given server is a valid mac address
1527 */
1528 if(!tests::is_mac($server)){
1529 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1530 return($ret);
1531 }
1533 /* Prepare request event
1534 */
1535 $xml_msg =
1536 "<xml>".
1537 "<header>gosa_krb5_get_policy</header>".
1538 "<policy>".$name."</policy>".
1539 "<source>GOSA</source>".
1540 "<target>".$server."</target>".
1541 "</xml>";
1543 /* Possible attributes */
1544 $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1545 "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1548 $tmp = $this->_send($xml_msg,TRUE);
1549 if(isset($tmp['XML'])){
1550 foreach($attrs as $attr){
1551 if(isset($tmp['XML'][$attr])){
1552 $ret[$attr] = $tmp['XML'][$attr];
1553 }else{
1554 $ret[$attr] = "";
1555 }
1556 }
1557 }
1558 return($ret);
1559 }
1562 /*! \brief Creates a new policy with a given set of configuration settings.
1563 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1564 (Uses the GOsa support daemon instead of the ldap database.)
1565 @pram String The name of the policy to update.
1566 @pram Array The attributes to update
1567 @return Boolean TRUE on success else FALSE.
1568 */
1569 public function krb5_add_policy($server,$name,$values)
1570 {
1571 $ret = FALSE;
1573 /* Check if the given name is a valid request value
1574 */
1575 if(!is_string($name) || empty($name)){
1576 trigger_error("The given policy name is not of type string or it is empty.");
1577 return($ret);
1578 }
1579 if(!is_array($values) || !count($values)){
1580 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1581 return($ret);
1582 }
1584 /* Check if the given server is a valid mac address
1585 */
1586 if(!tests::is_mac($server)){
1587 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1588 return($ret);
1589 }
1592 /* Transform array into <xml>
1593 */
1594 $attrs = "";
1595 foreach($values as $id => $value){
1596 if(empty($id) || is_numeric($id)){
1597 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1598 return($ret);
1599 }
1600 $id = strtolower($id);
1601 $attrs.= "<$id>$value</$id>\n";
1602 }
1604 /* Prepare request event
1605 */
1606 $xml_msg =
1607 "<xml>".
1608 "<header>gosa_krb5_create_policy</header>".
1609 "<policy>".$name."</policy>".
1610 $attrs.
1611 "<source>GOSA</source>".
1612 "<target>".$server."</target>".
1613 "</xml>";
1615 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1616 }
1619 /*! \brief Updates a given policy with a set of configuration settings.
1620 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1621 (Uses the GOsa support daemon instead of the ldap database.)
1622 @pram String The name of the policy to update.
1623 @return Boolean TRUE on success else FALSE.
1624 */
1625 public function krb5_set_policy($server,$name,$values)
1626 {
1627 $ret = FALSE;
1629 /* Check if the given name is a valid request value
1630 */
1631 if(!is_string($name) || empty($name)){
1632 trigger_error("The given policy name is not of type string or it is empty.");
1633 return($ret);
1634 }
1635 if(!is_array($values) || !count($values)){
1636 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1637 return($ret);
1638 }
1640 /* Check if the given server is a valid mac address
1641 */
1642 if(!tests::is_mac($server)){
1643 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1644 return($ret);
1645 }
1647 /* Transform array into <xml>
1648 */
1649 $attrs = "";
1650 foreach($values as $id => $value){
1651 if(preg_match("/^policy$/i",$id)) continue;
1652 if(empty($id) || is_numeric($id)){
1653 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1654 return($ret);
1655 }
1656 $id = strtolower($id);
1657 $attrs.= "<$id>$value</$id>\n";
1658 }
1660 /* Prepare request event
1661 */
1662 $xml_msg =
1663 "<xml>".
1664 "<header>gosa_krb5_modify_policy</header>".
1665 "<policy>".$name."</policy>".
1666 $attrs.
1667 "<source>GOSA</source>".
1668 "<target>".$server."</target>".
1669 "</xml>";
1671 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1672 }
1675 /*! \brief Removes the given password policy.
1676 (Uses the GOsa support daemon instead of the ldap database.)
1677 @return Boolean TRUE on success else FALSE
1678 */
1679 public function krb5_del_policy($server,$name)
1680 {
1681 $ret = FALSE;
1683 /* Check if the given server is a valid mac address
1684 */
1685 if(!tests::is_mac($server)){
1686 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1687 return($ret);
1688 }
1690 /* Check if the given name is a valid request value
1691 */
1692 if(!is_string($name) || empty($name)){
1693 trigger_error("The given policy name is not of type string or it is empty.");
1694 return($ret);
1695 }
1697 /* Prepare request event
1698 */
1699 $xml_msg =
1700 "<xml>".
1701 "<header>gosa_krb5_del_policy</header>".
1702 "<policy>".$name."</policy>".
1703 "<source>GOSA</source>".
1704 "<target>".$server."</target>".
1705 "</xml>";
1706 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1707 }
1710 /*! \brief Sets the password of for the given principal.
1711 (Uses the GOsa support daemon instead of the ldap database.)
1712 @param String The servers mac
1713 @param String The principals name
1714 @param String $the new password.
1715 @return Boolean TRUE on success else FALSE
1716 */
1717 public function krb5_set_password($server,$name,$password)
1718 {
1719 $ret = FALSE;
1721 /* Check if the given server is a valid mac address
1722 */
1723 if(!tests::is_mac($server)){
1724 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1725 return($ret);
1726 }
1728 /* Check if the given name is a valid request value
1729 */
1730 if(!is_string($name) || empty($name)){
1731 trigger_error("The given principal name is not of type string or it is empty.");
1732 return($ret);
1733 }
1735 /* Prepare request event
1736 */
1737 $xml_msg =
1738 "<xml>".
1739 "<header>gosa_krb5_set_password</header>".
1740 "<principal>".$name."</principal>".
1741 "<password>".$password."</password>".
1742 "<source>GOSA</source>".
1743 "<target>".$server."</target>".
1744 "</xml>";
1745 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1746 }
1749 /*! \brief Returns log file informations for a given mac address
1750 @param $mac The mac address to fetch logs for.
1751 @retrun Array A Multidimensional array containing log infos.
1752 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1753 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1754 install_20080313_144450 ...
1755 */
1756 public function get_log_info_for_mac($mac)
1757 {
1758 $xml_msg = "
1759 <xml>
1760 <header>gosa_show_log_by_mac</header>
1761 <target>GOSA</target>
1762 <source>GOSA</source>
1763 <mac>".$mac."</mac>
1764 </xml>";
1766 $res = $this->_send($xml_msg,TRUE);
1767 $ret = array();
1768 if(isset($res['XML'])){
1770 /* Filter all entry that look like this
1771 MAC_00_01_6C_9D_B9_FA
1772 */
1773 foreach($res['XML'] as $name => $entry){
1774 if(preg_match("/^MAC/",$name)){
1776 /* Get list of available log files
1777 */
1778 foreach($entry as $log_date){
1779 $xml_msg2 = "<xml>
1780 <header>gosa_show_log_files_by_date_and_mac</header>
1781 <target>GOSA</target>
1782 <source>GOSA</source>
1783 <date>".$log_date."</date>
1784 <mac>".$mac."</mac>
1785 </xml>";
1787 $ret[$mac][$log_date] = array();
1788 $res = $this->_send($xml_msg2,TRUE);
1789 $ret[$mac][$log_date]['DATE_STR'] = $log_date;
1790 $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1791 if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1792 $ret[$mac][$log_date]['FILES'] = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1793 }
1794 }
1795 }
1796 }
1797 }
1798 return($ret);
1799 }
1801 public function get_log_file($mac,$date,$file)
1802 {
1803 $xml_msg ="
1804 <xml>
1805 <header>gosa_get_log_file_by_date_and_mac</header>
1806 <target>GOSA</target>
1807 <source>GOSA</source>
1808 <date>".$date."</date>
1809 <mac>".$mac."</mac>
1810 <log_file>".$file."</log_file>
1811 </xml>";
1813 $res = $this->_send($xml_msg,TRUE);
1814 if(isset($res['XML'][strtoupper($file)])){
1815 return(base64_decode($res['XML'][strtoupper($file)]));
1816 }
1817 return("");
1818 }
1819 }
1821 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1822 ?>