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') {
830 /* Save tag attributes too.
831 e.g. <tag attr="val">
832 */
833 if(isset($xml_elem['attributes'])){
834 foreach($xml_elem['attributes'] as $name => $value){
835 $test2['ATTRIBUTES'][$name] = $value;
836 }
837 }
839 $start_level = 1;
840 $test2 = &$params;
841 while($start_level < $xml_elem['level']) {
842 $test2 = &$test2[$level[$start_level]];
843 $start_level++;
844 }
845 if(!isset($test2[$xml_elem['tag']])){
846 if(isset($xml_elem['value'])){
847 $test2[$xml_elem['tag']] = $xml_elem['value'];
848 }
849 }else{
850 if(!is_array($test2[$xml_elem['tag']])){
851 $test2[$xml_elem['tag']] = array($test2[$xml_elem['tag']]);
852 }
853 $test2[$xml_elem['tag']][] = $xml_elem['value'];
854 }
855 }
856 }
857 }
859 if(!isset($params['XML'])){
860 if (!array_key_exists('XML', $params)){
861 $this->set_error(_("Cannot not parse XML!"));
862 }
863 $params = array("COUNT" => 0);
864 }
866 return($params);
867 }
870 /*! \brief Updates an entry with a set of new values,
871 @param Integer The ID of the entry, we want to update.
872 @param Array The variables to update.
873 @return Boolean Returns TRUE on success.
874 */
875 public function update_entries($ids,$data)
876 {
877 $this->reset_error();
878 if(!is_array($ids)){
879 trigger_error("Requires an array as first parameter.");
880 return;
881 }
883 if(!is_array($data)){
884 trigger_error("Requires an array as second parameter.");
885 return;
886 }
888 $attr = "";
889 foreach($data as $key => $value){
890 $key = strtolower($key);
891 if(is_array($value)){
892 foreach($value as $sub_value){
893 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
894 }
895 }else{
896 $attr.= "<$key>".strtolower($value)."</$key>\n";
897 }
898 }
900 $xml_msg = "<xml>
901 <header>gosa_update_status_jobdb_entry</header>
902 <target>GOSA</target>
903 <source>GOSA</source>
904 <where>
905 <clause>
906 <connector>or</connector>";
907 foreach($ids as $id){
908 $xml_msg .= "<phrase>
909 <operator>eq</operator>
910 <id>".$id."</id>
911 </phrase>";
912 }
913 $xml_msg .= "</clause>
914 </where>
915 <update>
916 ".$attr."
917 </update>
918 </xml>";
920 if($this->connect()){
922 $this->o_sock->write($xml_msg);
923 $str = trim($this->o_sock->read());
925 /* Check if something went wrong while reading */
926 if($this->o_sock->is_error()){
927 $this->set_error($this->o_sock->get_error());
928 return(FALSE);
929 }
931 $entries = $this->xml_to_array($str);
932 if(isset($entries['XML'])){
933 if(isset($entries['XML']['ERROR_STRING'])) {
934 $this->set_error($entries['XML']['ERROR_STRING']);
935 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
936 return(FALSE);
937 }
938 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
939 return(TRUE);
940 }
941 }
942 return(FALSE);
943 }
946 /*! \brief Returns the number of currently queued objects.
947 @return Integer
948 */
949 public function number_of_queued_entries($event_types)
950 {
951 $tags = "";
952 foreach($event_types as $type){
953 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
954 }
955 if(count($event_types) > 1){
956 $tags = "<connector>or</connector>".$tags;
957 }
958 if(count($event_types)){
959 $tags = "<where><clause>".$tags."</clause></where>";
960 }
963 $xml_msg =
964 "<xml>".
965 "<header>gosa_query_jobdb</header>".
966 "<target>GOSA</target>".
967 "<source>GOSA</source>".
968 "<select> count ID</select>".
969 $tags.
970 "</xml>";
972 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
973 $this->connect();
974 if($this->connect()){
975 $this->o_sock->write($xml_msg);
976 $str = trim($this->o_sock->read());
978 /* Check if something went wrong while reading */
979 if($this->o_sock->is_error()){
980 $this->set_error($this->o_sock->get_error());
981 return(0);
982 }
984 $entries = $this->xml_to_array($str);
985 if(isset($entries['XML'])){
986 return($entries['XML']['COUNT']);
987 }
988 }
989 return(-1);
990 }
993 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
994 {
995 $xml_message= "";
997 /* Prepare data */
998 foreach ($data as $key => $value){
999 if(is_array($value)){
1000 foreach($value as $sub_value){
1001 $xml_message.= "<$key>$sub_value</$key>";
1002 }
1003 }else{
1004 $xml_message.= "<$key>$value</$key>";
1005 }
1006 }
1008 /* Multiple targets? */
1009 if (!is_array($to)){
1010 $to_targets= array($to);
1011 } else {
1012 $to_targets= $to;
1013 }
1015 /* Build target strings */
1016 $target ="";
1017 foreach($to_targets as $to){
1018 $target.= "<target>$to</target>";
1019 }
1021 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1022 }
1025 /* Allows simply appending a new DaemonEvent
1026 */
1027 public function append($event)
1028 {
1029 if(!($event instanceof DaemonEvent)){
1030 return(FALSE);
1031 }
1033 $this->reset_error();
1035 /* Add to queue if new
1036 */
1037 if($event->is_new()){
1039 $request_answer = FALSE;
1040 if($event->get_type() == SCHEDULED_EVENT){
1041 $action = $event->get_schedule_action();
1042 }elseif($event->get_type() == TRIGGERED_EVENT){
1043 $action = $event->get_trigger_action();
1044 }else{
1045 trigger_error("Unknown type of queue event given.");
1046 return(FALSE);
1047 }
1049 /* Get event informations, like targets..
1050 */
1051 $targets = $event->get_targets();
1052 $data = $event->save();
1054 /* Append an entry for each target
1055 */
1056 foreach($targets as $target){
1057 $data['macaddress'] = $target;
1058 $this->send_data($action,$target,$data,$request_answer);
1060 if($this->is_error()){
1061 return(FALSE);
1062 }
1063 }
1064 return(TRUE);
1065 }else{
1067 /* Updated edited entry.
1068 */
1069 $id = $event->get_id();
1070 $data = $event->save();
1071 return($this->update_entries(array($id),$data));
1072 }
1074 return(FALSE);
1075 }
1078 /*! \brief Returns an array containing all queued entries.
1079 @return Array All queued entries as an array.
1080 */
1081 public function _send($data, $answer_expected= FALSE)
1082 {
1083 $this->reset_error();
1084 $ret = array();
1086 if($this->connect()){
1087 $this->o_sock->write($data);
1088 if ($answer_expected){
1089 $str = trim($this->o_sock->read());
1091 /* Check if something went wrong while reading */
1092 if($this->o_sock->is_error()){
1093 $this->set_error($this->o_sock->get_error());
1094 return($ret);
1095 }
1097 $entries = $this->xml_to_array($str);
1098 if(isset($entries['XML']) && is_array($entries['XML'])){
1099 $ret = $entries;
1100 if(isset($entries['XML']['ERROR_STRING'])) {
1101 $this->set_error($entries['XML']['ERROR_STRING']);
1102 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1103 }elseif(isset($entries['XML']['ERROR'])){
1104 $this->set_error($entries['XML']['ERROR']);
1105 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1106 }else{
1107 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1108 }
1109 }
1110 }else{
1111 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1112 }
1113 }
1114 return($ret);
1115 }
1118 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1119 {
1120 $xml_message= "";
1122 /* Get communication object */
1123 $d= new gosaSupportDaemon(TRUE,10);
1125 /* Prepare data */
1126 foreach ($data as $key => $value){
1127 if(is_array($value)){
1128 foreach($value as $sub_val){
1129 $xml_message.= "<$key>$sub_val</$key>";
1130 }
1131 }else{
1132 $xml_message.= "<$key>$value</$key>";
1133 }
1134 }
1136 /* Multiple targets? */
1137 if (!is_array($to)){
1138 $to_targets= array($to);
1139 } else {
1140 $to_targets= $to;
1141 }
1143 /* Build target strings */
1144 $target ="";
1145 foreach($to_targets as $to){
1146 $target.= "<target>$to</target>";
1147 }
1149 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1150 }
1153 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1154 @param String $mac The mac address for which we want to remove all jobs.
1155 */
1156 function clean_queue_from_mac($mac)
1157 {
1158 global $config;
1160 /* First of all we have to check which jobs are startet
1161 * for $mac
1162 */
1163 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1165 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1167 $data = $this->_send($xml_msg,TRUE);
1168 if(is_array($data) && isset($data['XML'])){
1169 $already_aborted = FALSE;
1170 foreach($data['XML'] as $name => $entry){
1171 if(preg_match("/answer[0-9]*/i",$name)){
1172 $entry['STATUS'] = strtoupper($entry['STATUS']);
1173 switch($entry['STATUS']){
1175 case 'PROCESSING' :
1177 /* Send abort event, but only once
1178 */
1179 if($already_aborted){
1180 break;
1181 }elseif(class_available("DaemonEvent_faireboot")){
1182 $already_aborted = TRUE;
1183 $tmp = new DaemonEvent_faireboot($config);
1184 $tmp->add_targets(array($mac));
1185 $tmp->set_type(TRIGGERED_EVENT);
1186 if(!$this->append($tmp)){
1187 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1188 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1189 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1190 }else{
1191 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1192 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1193 }
1194 ;break;
1195 }else{
1196 /* Couldn't find abort event, just remove entry */
1197 }
1199 case 'WAITING':
1200 case 'ERROR':
1201 default :
1203 /* Simply remove entries from queue.
1204 * Failed or waiting events, can be removed without any trouble.
1205 */
1206 if(!$this->remove_entries(array($entry['ID']))){
1207 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1208 }
1209 ;break;
1210 }
1212 }
1213 }
1214 }
1215 }
1218 static function ping($target)
1219 {
1220 if (tests::is_mac($target)){
1221 /* Get communication object */
1222 $d= new gosaSupportDaemon(TRUE,0.5);
1223 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1224 return (count($answer) ? TRUE:FALSE);
1225 }
1226 return (FALSE);
1227 }
1231 /*! \brief Returns a list of all configured principals.
1232 (Uses the GOsa support daemon instead of the ldap database.)
1233 @return Array A list containing the names of all configured principals.
1234 */
1235 public function krb5_list_principals($server)
1236 {
1237 $res = array();
1239 /* Check if the given server is a valid mac address
1240 */
1241 if(!tests::is_mac($server)){
1242 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1243 return($ret);
1244 }
1246 /* Prepare request event
1247 */
1248 $xml_msg =
1249 "<xml>".
1250 "<header>gosa_krb5_list_principals</header>".
1251 "<source>GOSA</source>".
1252 "<target>".$server."</target>".
1253 "</xml>";
1255 $tmp = $this->_send($xml_msg,TRUE);
1256 if(isset($tmp['XML']['PRINCIPAL'])){
1257 return($tmp['XML']['PRINCIPAL']);
1258 }else{
1259 return($res);
1260 }
1261 }
1264 /*! \brief Returns the configuration settings for a given principal name.
1265 (Uses the GOsa support daemon instead of the ldap database.)
1266 @pram String The name of the requested principal. (e.g. peter@EXAMPLE.DE)
1267 @return Array A list containing the names of all configured principals.
1268 */
1269 public function krb5_get_principal($server,$name)
1270 {
1271 $ret = array();
1273 /* Check if the given name is a valid request value
1274 */
1275 if(!is_string($name) || empty($name)){
1276 trigger_error("The given principal name is not of type string or it is empty.");
1277 return($ret);
1278 }
1280 /* Check if the given server is a valid mac address
1281 */
1282 if(!tests::is_mac($server)){
1283 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1284 return($ret);
1285 }
1287 /* Prepare request event
1288 */
1289 $xml_msg =
1290 "<xml>".
1291 "<header>gosa_krb5_get_principal</header>".
1292 "<principal>".$name."</principal>".
1293 "<source>GOSA</source>".
1294 "<target>".$server."</target>".
1295 "</xml>";
1297 $res = $this->_send($xml_msg,TRUE);
1298 if(isset($res['XML'])){
1299 return($res['XML']);
1300 }else{
1301 return($ret);
1302 }
1303 }
1306 /*! \brief Creates a given principal with a set of configuration settings.
1307 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1308 (Uses the GOsa support daemon instead of the ldap database.)
1309 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1310 @return Boolean TRUE on success else FALSE.
1311 */
1312 public function krb5_add_principal($server,$name,$values)
1313 {
1314 $ret = FALSE;
1316 /* Check if the given name is a valid request value
1317 */
1318 if(!is_string($name) || empty($name)){
1319 trigger_error("The given principal name is not of type string or it is empty.");
1320 return($ret);
1321 }
1322 if(!is_array($values)){
1323 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1324 return($ret);
1325 }
1327 /* Check if the given server is a valid mac address
1328 */
1329 if(!tests::is_mac($server)){
1330 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1331 return($ret);
1332 }
1334 $attrs = "";
1335 foreach($values as $key => $value){
1336 if(empty($key) || is_numeric($key)){
1337 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1338 return($ret);
1339 }
1340 $key = strtolower($key);
1341 if(is_array($value)){
1342 foreach($value as $val){
1343 $attrs.= "<$key>$val</$key>\n";
1344 }
1345 }else{
1346 $attrs.= "<$key>$value</$key>\n";
1347 }
1348 }
1350 /* Prepare request event
1351 */
1352 $xml_msg =
1353 "<xml>".
1354 "<header>gosa_krb5_create_principal</header>".
1355 "<principal>".$name."</principal>".
1356 $attrs.
1357 "<source>GOSA</source>".
1358 "<target>".$server."</target>".
1359 "</xml>";
1361 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1362 }
1365 function krb5_ramdomize_key($server,$name)
1366 {
1367 /* Prepare request event
1368 */
1369 $xml_msg =
1370 "<xml>".
1371 "<header>gosa_krb5_randomize_key</header>".
1372 "<principal>".$name."</principal>".
1373 "<source>GOSA</source>".
1374 "<target>".$server."</target>".
1375 "</xml>";
1377 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1378 }
1382 /*! \brief Updates a given principal with a set of configuration settings.
1383 For a list of configurable attributes have a look at 'krb5_get_principal()'.
1384 (Uses the GOsa support daemon instead of the ldap database.)
1385 @pram String The name of the principal to update. (e.g. peter@EXAMPLE.DE)
1386 @return Boolean TRUE on success else FALSE.
1387 */
1388 public function krb5_set_principal($server,$name,$values)
1389 {
1390 $ret = FALSE;
1392 /* Check if the given name is a valid request value
1393 */
1394 if(!is_string($name) || empty($name)){
1395 trigger_error("The given principal name is not of type string or it is empty.");
1396 return($ret);
1397 }
1398 if(!is_array($values) || !count($values)){
1399 trigger_error("No valid update settings given. The parameter must be of type array and must contain at least one entry");
1400 return($ret);
1401 }
1403 /* Check if the given server is a valid mac address
1404 */
1405 if(!tests::is_mac($server)){
1406 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1407 return($ret);
1408 }
1410 $attrs = "";
1411 foreach($values as $key => $value){
1412 if(empty($key) || is_numeric($key)){
1413 trigger_error("Invalid configuration attribute given '".$key."=".$value."'.");
1414 return($ret);
1415 }
1416 $key = strtolower($key);
1417 if(is_array($value)){
1418 foreach($value as $val){
1419 $attrs.= "<$key>$val</$key>\n";
1420 }
1421 }else{
1422 $attrs.= "<$key>$value</$key>\n";
1423 }
1424 }
1426 /* Prepare request event
1427 */
1428 $xml_msg =
1429 "<xml>".
1430 "<header>gosa_krb5_modify_principal</header>".
1431 "<principal>".$name."</principal>".
1432 $attrs.
1433 "<source>GOSA</source>".
1434 "<target>".$server."</target>".
1435 "</xml>";
1437 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1438 }
1441 /*! \brief Removes the given principal.
1442 (Uses the GOsa support daemon instead of the ldap database.)
1443 @pram String The name of the principal. (e.g. peter@EXAMPLE.DE)
1444 @return Boollean TRUE on success else FALSE
1445 */
1446 public function krb5_del_principal($server,$name)
1447 {
1448 $ret = FALSE;
1450 /* Check if the given name is a valid request value
1451 */
1452 if(!is_string($name) || empty($name)){
1453 trigger_error("The given principal name is not of type string or it is empty.");
1454 return($ret);
1455 }
1457 /* Check if the given server is a valid mac address
1458 */
1459 if(!tests::is_mac($server)){
1460 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1461 return($ret);
1462 }
1464 /* Prepare request event
1465 */
1466 $xml_msg =
1467 "<xml>".
1468 "<header>gosa_krb5_del_principal</header>".
1469 "<principal>".$name."</principal>".
1470 "<source>GOSA</source>".
1471 "<target>".$server."</target>".
1472 "</xml>";
1474 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1475 }
1478 /*! \brief Returns a list of configured password policies.
1479 (Uses the GOsa support daemon instead of the ldap database.)
1480 @return Array A list of all configured password policies.
1481 */
1482 public function krb5_list_policies($server)
1483 {
1484 $res = array();
1486 /* Check if the given server is a valid mac address
1487 */
1488 if(!tests::is_mac($server)){
1489 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1490 return($ret);
1491 }
1493 /* Prepare request event
1494 */
1495 $xml_msg =
1496 "<xml>".
1497 "<header>gosa_krb5_list_policies</header>".
1498 "<source>GOSA</source>".
1499 "<target>".$server."</target>".
1500 "</xml>";
1502 $res = $this->_send($xml_msg,TRUE);
1504 /* Check if there are results for POLICY
1505 */
1506 if(isset($res['XML']['POLICY'])){
1508 /* Ensure that we return an array
1509 */
1510 $tmp = $res['XML']['POLICY'];
1511 if(!is_array($tmp)){
1512 $tmp = array($tmp);
1513 }
1514 return($tmp);
1515 }else{
1516 return(array());
1517 }
1518 }
1521 /*! \brief Returns a list of configured password policies.
1522 (Uses the GOsa support daemon instead of the ldap database.)
1523 @return Array The policy settings for the given policy name.
1524 */
1525 public function krb5_get_policy($server,$name)
1526 {
1527 $res = array();
1529 /* Check if the given name is a valid request value
1530 */
1531 if(!is_string($name) || empty($name)){
1532 trigger_error("The given policy name is not of type string or it is empty.");
1533 return($ret);
1534 }
1536 /* Check if the given server is a valid mac address
1537 */
1538 if(!tests::is_mac($server)){
1539 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1540 return($ret);
1541 }
1543 /* Prepare request event
1544 */
1545 $xml_msg =
1546 "<xml>".
1547 "<header>gosa_krb5_get_policy</header>".
1548 "<policy>".$name."</policy>".
1549 "<source>GOSA</source>".
1550 "<target>".$server."</target>".
1551 "</xml>";
1553 /* Possible attributes */
1554 $attrs = array("MASK","POLICY","PW_HISTORY_NUM","PW_MAX_LIFE",
1555 "PW_MIN_CLASSES","PW_MIN_LENGTH","PW_MIN_LIFE","POLICY_REFCNT");
1558 $tmp = $this->_send($xml_msg,TRUE);
1559 if(isset($tmp['XML'])){
1560 foreach($attrs as $attr){
1561 if(isset($tmp['XML'][$attr])){
1562 $ret[$attr] = $tmp['XML'][$attr];
1563 }else{
1564 $ret[$attr] = "";
1565 }
1566 }
1567 }
1568 return($ret);
1569 }
1572 /*! \brief Creates a new policy with a given set of configuration settings.
1573 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1574 (Uses the GOsa support daemon instead of the ldap database.)
1575 @pram String The name of the policy to update.
1576 @pram Array The attributes to update
1577 @return Boolean TRUE on success else FALSE.
1578 */
1579 public function krb5_add_policy($server,$name,$values)
1580 {
1581 $ret = FALSE;
1583 /* Check if the given name is a valid request value
1584 */
1585 if(!is_string($name) || empty($name)){
1586 trigger_error("The given policy name is not of type string or it is empty.");
1587 return($ret);
1588 }
1589 if(!is_array($values) || !count($values)){
1590 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1591 return($ret);
1592 }
1594 /* Check if the given server is a valid mac address
1595 */
1596 if(!tests::is_mac($server)){
1597 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1598 return($ret);
1599 }
1602 /* Transform array into <xml>
1603 */
1604 $attrs = "";
1605 foreach($values as $id => $value){
1606 if(empty($id) || is_numeric($id)){
1607 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1608 return($ret);
1609 }
1610 $id = strtolower($id);
1611 $attrs.= "<$id>$value</$id>\n";
1612 }
1614 /* Prepare request event
1615 */
1616 $xml_msg =
1617 "<xml>".
1618 "<header>gosa_krb5_create_policy</header>".
1619 "<policy>".$name."</policy>".
1620 $attrs.
1621 "<source>GOSA</source>".
1622 "<target>".$server."</target>".
1623 "</xml>";
1625 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1626 }
1629 /*! \brief Updates a given policy with a set of configuration settings.
1630 For a list of configurable attributes have a look at 'krb5_get_policy()'.
1631 (Uses the GOsa support daemon instead of the ldap database.)
1632 @pram String The name of the policy to update.
1633 @return Boolean TRUE on success else FALSE.
1634 */
1635 public function krb5_set_policy($server,$name,$values)
1636 {
1637 $ret = FALSE;
1639 /* Check if the given name is a valid request value
1640 */
1641 if(!is_string($name) || empty($name)){
1642 trigger_error("The given policy name is not of type string or it is empty.");
1643 return($ret);
1644 }
1645 if(!is_array($values) || !count($values)){
1646 trigger_error("No valid policy settings given. The parameter must be of type array and must contain at least one entry");
1647 return($ret);
1648 }
1650 /* Check if the given server is a valid mac address
1651 */
1652 if(!tests::is_mac($server)){
1653 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1654 return($ret);
1655 }
1657 /* Transform array into <xml>
1658 */
1659 $attrs = "";
1660 foreach($values as $id => $value){
1661 if(preg_match("/^policy$/i",$id)) continue;
1662 if(empty($id) || is_numeric($id)){
1663 trigger_error("Invalid policy configuration attribute given '".$id."=".$value."'.");
1664 return($ret);
1665 }
1666 $id = strtolower($id);
1667 $attrs.= "<$id>$value</$id>\n";
1668 }
1670 /* Prepare request event
1671 */
1672 $xml_msg =
1673 "<xml>".
1674 "<header>gosa_krb5_modify_policy</header>".
1675 "<policy>".$name."</policy>".
1676 $attrs.
1677 "<source>GOSA</source>".
1678 "<target>".$server."</target>".
1679 "</xml>";
1681 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1682 }
1685 /*! \brief Removes the given password policy.
1686 (Uses the GOsa support daemon instead of the ldap database.)
1687 @return Boolean TRUE on success else FALSE
1688 */
1689 public function krb5_del_policy($server,$name)
1690 {
1691 $ret = FALSE;
1693 /* Check if the given server is a valid mac address
1694 */
1695 if(!tests::is_mac($server)){
1696 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1697 return($ret);
1698 }
1700 /* Check if the given name is a valid request value
1701 */
1702 if(!is_string($name) || empty($name)){
1703 trigger_error("The given policy name is not of type string or it is empty.");
1704 return($ret);
1705 }
1707 /* Prepare request event
1708 */
1709 $xml_msg =
1710 "<xml>".
1711 "<header>gosa_krb5_del_policy</header>".
1712 "<policy>".$name."</policy>".
1713 "<source>GOSA</source>".
1714 "<target>".$server."</target>".
1715 "</xml>";
1716 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1717 }
1720 /*! \brief Sets the password of for the given principal.
1721 (Uses the GOsa support daemon instead of the ldap database.)
1722 @param String The servers mac
1723 @param String The principals name
1724 @param String $the new password.
1725 @return Boolean TRUE on success else FALSE
1726 */
1727 public function krb5_set_password($server,$name,$password)
1728 {
1729 $ret = FALSE;
1731 /* Check if the given server is a valid mac address
1732 */
1733 if(!tests::is_mac($server)){
1734 trigger_error("The given server address '".$server."' is invalid, it must be a valid mac address");
1735 return($ret);
1736 }
1738 /* Check if the given name is a valid request value
1739 */
1740 if(!is_string($name) || empty($name)){
1741 trigger_error("The given principal name is not of type string or it is empty.");
1742 return($ret);
1743 }
1745 /* Prepare request event
1746 */
1747 $xml_msg =
1748 "<xml>".
1749 "<header>gosa_krb5_set_password</header>".
1750 "<principal>".$name."</principal>".
1751 "<password>".$password."</password>".
1752 "<source>GOSA</source>".
1753 "<target>".$server."</target>".
1754 "</xml>";
1755 return($this->_send($xml_msg,TRUE) == TRUE && !$this->is_error());
1756 }
1759 /*! \brief Returns log file informations for a given mac address
1760 @param $mac The mac address to fetch logs for.
1761 @retrun Array A Multidimensional array containing log infos.
1762 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][0]=debconf.log
1763 MAC_00_01_6C_9D_B9_FA['install_20080311_090900'][1]=syslog.log
1764 install_20080313_144450 ...
1765 */
1766 public function get_log_info_for_mac($mac)
1767 {
1768 $xml_msg = "
1769 <xml>
1770 <header>gosa_show_log_by_mac</header>
1771 <target>GOSA</target>
1772 <source>GOSA</source>
1773 <mac>".$mac."</mac>
1774 </xml>";
1776 $res = $this->_send($xml_msg,TRUE);
1777 $ret = array();
1778 if(isset($res['XML'])){
1780 /* Filter all entry that look like this
1781 MAC_00_01_6C_9D_B9_FA
1782 */
1783 foreach($res['XML'] as $name => $entry){
1784 if(preg_match("/^MAC/",$name)){
1786 /* Get list of available log files
1787 */
1788 foreach($entry as $log_date){
1789 $xml_msg2 = "<xml>
1790 <header>gosa_show_log_files_by_date_and_mac</header>
1791 <target>GOSA</target>
1792 <source>GOSA</source>
1793 <date>".$log_date."</date>
1794 <mac>".$mac."</mac>
1795 </xml>";
1797 $ret[$mac][$log_date] = array();
1798 $res = $this->_send($xml_msg2,TRUE);
1799 $ret[$mac][$log_date]['DATE_STR'] = $log_date;
1800 $ret[$mac][$log_date]['REAL_DATE'] = strtotime(preg_replace("/[^0-9]*/","",$log_date));
1801 if(isset($res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'])){
1802 $ret[$mac][$log_date]['FILES'] = $res['XML']['SHOW_LOG_FILES_BY_DATE_AND_MAC'];
1803 }
1804 }
1805 }
1806 }
1807 }
1808 return($ret);
1809 }
1811 public function get_log_file($mac,$date,$file)
1812 {
1813 $xml_msg ="
1814 <xml>
1815 <header>gosa_get_log_file_by_date_and_mac</header>
1816 <target>GOSA</target>
1817 <source>GOSA</source>
1818 <date>".$date."</date>
1819 <mac>".$mac."</mac>
1820 <log_file>".$file."</log_file>
1821 </xml>";
1823 $res = $this->_send($xml_msg,TRUE);
1824 if(isset($res['XML'][strtoupper($file)])){
1825 return(base64_decode($res['XML'][strtoupper($file)]));
1826 }
1827 return("");
1828 }
1834 /*****************
1835 * DAK - Functions
1836 *****************/
1838 /*! \brief Returns all currenlty queued entries for a given DAK repository
1839 @param ...
1840 @return Array All queued entries.
1841 */
1842 public function DAK_get_queue_entries($server)
1843 {
1844 /* Ensure that we send the event to a valid mac address
1845 */
1846 if(!is_string($server) || !tests::is_mac($server)){
1847 trigger_error("No valid mac address given '".$server."'.");
1848 return;
1849 }
1851 /* Create query
1852 */
1853 $xml_msg = "<xml>
1854 <header>gosa_get_dak_keyring</header>
1855 <target>".$server."</target>
1856 <source>GOSA</source>
1857 </xml>";
1859 $res = $this->_send($xml_msg,TRUE);
1861 /* Check if there are results for POLICY
1862 */
1863 if(isset($res['XML'])){
1864 $ret = array();
1865 foreach($res['XML'] as $key => $entry){
1866 if(preg_match("/^ANSWER/",$key)){
1867 $ret[] = $entry;
1868 }
1869 }
1870 return($ret);
1871 }else{
1872 return(array());
1873 }
1874 }
1875 }
1877 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1878 ?>