Code

af77b7f4add9a5ea4bf193c3a67f4fa8cf1577a9
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 my @functions = (
6     "create_passwd",
7     "create_xml_hash",
8     "get_content_from_xml_hash",
9     "add_content2xml_hash",
10     "create_xml_string",
11     "transform_msg2hash",
12     "get_time",
13     "build_msg",
14     "db_res2xml",
15     "db_res2si_msg",
16     "get_where_statement",
17     "get_select_statement",
18     "get_update_statement",
19     "get_limit_statement",
20     "get_orderby_statement",
21     "get_dns_domains",
22     "get_server_addresses",
23     "get_logged_in_users",
24     "import_events",
25     "del_doubles",
26     "get_ip",
27     "get_interface_for_ip",
28     "get_interfaces",
29     "get_mac_for_interface",
30     "get_local_ip_for_remote_ip",
31     "get_local_mac_for_remote_ip",
32     "is_local",
33     "run_as",
34     "inform_all_other_si_server",
35     "read_configfile",
36     "check_opsi_res",
37     ); 
38 @EXPORT = @functions;
39 use strict;
40 use warnings;
41 use IO::Socket::INET;
42 use Crypt::Rijndael;
43 use Digest::MD5  qw(md5 md5_hex md5_base64);
44 use MIME::Base64;
45 use XML::Simple;
46 use Data::Dumper;
47 use Net::DNS;
50 my $op_hash = {
51     'eq' => '=',
52     'ne' => '!=',
53     'ge' => '>=',
54     'gt' => '>',
55     'le' => '<=',
56     'lt' => '<',
57     'like' => ' LIKE ',
58 };
61 BEGIN {}
63 END {}
65 ### Start ######################################################################
67 my $xml = new XML::Simple();
69 sub daemon_log {
70     my ($msg, $level) = @_ ;
71     &main::daemon_log($msg, $level);
72     return;
73 }
76 sub create_passwd {
77     my $new_passwd = "";
78     for(my $i=0; $i<31; $i++) {
79         $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
80     }
82     return $new_passwd;
83 }
86 sub del_doubles { 
87     my %all; 
88     $all{$_}=0 for @_; 
89     return (keys %all); 
90 }
93 #===  FUNCTION  ================================================================
94 #         NAME:  create_xml_hash
95 #   PARAMETERS:  header - string - message header (required)
96 #                source - string - where the message come from (required)
97 #                target - string - where the message should go to (required)
98 #                [header_value] - string - something usefull (optional)
99 #      RETURNS:  hash - hash - nomen est omen
100 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
101 #===============================================================================
102 sub create_xml_hash {
103     my ($header, $source, $target, $header_value) = @_;
104     my $hash = {
105             header => [$header],
106             source => [$source],
107             target => [$target],
108             $header => [$header_value],
109     };
110     return $hash
114 #===  FUNCTION  ================================================================
115 #         NAME:  create_xml_string
116 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
117 #      RETURNS:  xml_string - string - xml string representation of the hash
118 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
119 #===============================================================================
120 sub create_xml_string {
121     my ($xml_hash) = @_ ;
122     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
123     #$xml_string =~ s/[\n]+//g;
124     #daemon_log("create_xml_string:",7);
125     #daemon_log("$xml_string\n", 7);
126     return $xml_string;
130 sub transform_msg2hash {
131     my ($msg) = @_ ;
132     my $hash = $xml->XMLin($msg, ForceArray=>1);
133     
134     # xml tags without a content are created as an empty hash
135     # substitute it with an empty list
136     eval {
137         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
138             if( 1 == @{ $xml_content } ) {
139                 # there is only one element in xml_content list ...
140                 my $element = @{ $xml_content }[0];
141                 if( ref($element) eq "HASH" ) {
142                     # and this element is an hash ...
143                     my $len_element = keys %{ $element };
144                     if( $len_element == 0 ) {
145                         # and this hash is empty, then substitute the xml_content
146                         # with an empty string in list
147                         $hash->{$xml_tag} = [ "none" ];
148                     }
149                 }
150             }
151         }
152     };
153     if( $@ ) {  
154         $hash = undef;
155     }
157     return $hash;
161 #===  FUNCTION  ================================================================
162 #         NAME:  add_content2xml_hash
163 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
164 #                element - string - key for the hash
165 #                content - string - value for the hash
166 #      RETURNS:  nothing
167 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
168 #                then append value to list
169 #===============================================================================
170 sub add_content2xml_hash {
171     my ($xml_ref, $element, $content) = @_;
172     if(not exists $$xml_ref{$element} ) {
173         $$xml_ref{$element} = [];
174     }
175     my $tmp = $$xml_ref{$element};
176     push(@$tmp, $content);
177     return;
181 sub get_time {
182     my ($seconds, $minutes, $hours, $monthday, $month,
183             $year, $weekday, $yearday, $sommertime) = localtime(time);
184     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
185     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
186     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
187     $month+=1;
188     $month = $month < 10 ? $month = "0".$month : $month;
189     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
190     $year+=1900;
191     return "$year$month$monthday$hours$minutes$seconds";
196 #===  FUNCTION  ================================================================
197 #         NAME: build_msg
198 #  DESCRIPTION: Send a message to a destination
199 #   PARAMETERS: [header] Name of the header
200 #               [from]   sender ip
201 #               [to]     recipient ip
202 #               [data]   Hash containing additional attributes for the xml
203 #                        package
204 #      RETURNS:  nothing
205 #===============================================================================
206 sub build_msg ($$$$) {
207         my ($header, $from, $to, $data) = @_;
209     # data is of form, i.e.
210     # %data= ('ip' => $address, 'mac' => $mac);
212         my $out_hash = &create_xml_hash($header, $from, $to);
214         while ( my ($key, $value) = each(%$data) ) {
215                 if(ref($value) eq 'ARRAY'){
216                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
217                 } else {
218                         &add_content2xml_hash($out_hash, $key, $value);
219                 }
220         }
221     my $out_msg = &create_xml_string($out_hash);
222     return $out_msg;
226 sub db_res2xml {
227     my ($db_res) = @_ ;
228     my $xml = "";
230     my $len_db_res= keys %{$db_res};
231     for( my $i= 1; $i<= $len_db_res; $i++ ) {
232         $xml .= "\n<answer>";
233         my $hash= $db_res->{$i};
234         while ( my ($column_name, $column_value) = each %{$hash} ) {
235             $xml .= "<$column_name>";
236             my $xml_content;
237             if( $column_name eq "xmlmessage" ) {
238                 $xml_content = &encode_base64($column_value);
239             } else {
240                 $xml_content = $column_value;
241             }
242             $xml .= $xml_content;
243             $xml .= "</$column_name>"; 
244         }
245         $xml .= "</answer$i>";
247     }
249     return $xml;
253 sub db_res2si_msg {
254     my ($db_res, $header, $target, $source) = @_;
256     my $si_msg = "<xml>";
257     $si_msg .= "<header>$header</header>";
258     $si_msg .= "<source>$source</source>";
259     $si_msg .= "<target>$target</target>";
260     $si_msg .= &db_res2xml;
261     $si_msg .= "</xml>";
265 sub get_where_statement {
266     my ($msg, $msg_hash) = @_;
267     my $error= 0;
268     
269     my $clause_str= "";
270     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
271         $error++; 
272     }
274     if( $error == 0 ) {
275         my @clause_l;
276         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
277         foreach my $clause (@where) {
278             my $connector = $clause->{'connector'}[0];
279             if( not defined $connector ) { $connector = "AND"; }
280             $connector = uc($connector);
281             delete($clause->{'connector'});
283             my @phrase_l ;
284             foreach my $phrase (@{$clause->{'phrase'}}) {
285                 my $operator = "=";
286                 if( exists $phrase->{'operator'} ) {
287                     my $op = $op_hash->{$phrase->{'operator'}[0]};
288                     if( not defined $op ) {
289                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
290                                 "statement to sql valid syntax. Please use 'eq', ".
291                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
292                         &main::daemon_log($msg, 8);
293                         $op = "=";
294                     }
295                     $operator = $op;
296                     delete($phrase->{'operator'});
297                 }
299                 my @xml_tags = keys %{$phrase};
300                 my $tag = $xml_tags[0];
301                 my $val = $phrase->{$tag}[0];
302                 if( ref($val) eq "HASH" ) { next; }  # empty xml-tags should not appear in where statement
304                                 # integer columns do not have to have single quotes besides the value
305                                 if ($tag eq "id") {
306                                                 push(@phrase_l, "$tag$operator$val");
307                                 } else {
308                                                 push(@phrase_l, "$tag$operator'$val'");
309                                 }
310             }
312             if (not 0 == @phrase_l) {
313                 my $clause_str .= join(" $connector ", @phrase_l);
314                 push(@clause_l, "($clause_str)");
315             }
316         }
318         if( not 0 == @clause_l ) {
319             $clause_str = join(" AND ", @clause_l);
320             $clause_str = "WHERE ($clause_str) ";
321         }
322     }
324     return $clause_str;
327 sub get_select_statement {
328     my ($msg, $msg_hash)= @_;
329     my $select = "*";
330     if( exists $msg_hash->{'select'} ) {
331         my $select_l = \@{$msg_hash->{'select'}};
332         $select = join(', ', @{$select_l});
333     }
334     return $select;
338 sub get_update_statement {
339     my ($msg, $msg_hash) = @_;
340     my $error= 0;
341     my $update_str= "";
342     my @update_l; 
344     if( not exists $msg_hash->{'update'} ) { $error++; };
346     if( $error == 0 ) {
347         my $update= @{$msg_hash->{'update'}}[0];
348         while( my ($tag, $val) = each %{$update} ) {
349             my $val= @{$update->{$tag}}[0];
350             push(@update_l, "$tag='$val'");
351         }
352         if( 0 == @update_l ) { $error++; };   
353     }
355     if( $error == 0 ) { 
356         $update_str= join(', ', @update_l);
357         $update_str= "SET $update_str ";
358     }
360     return $update_str;
363 sub get_limit_statement {
364     my ($msg, $msg_hash)= @_; 
365     my $error= 0;
366     my $limit_str = "";
367     my ($from, $to);
369     if( not exists $msg_hash->{'limit'} ) { $error++; };
371     if( $error == 0 ) {
372         eval {
373             my $limit= @{$msg_hash->{'limit'}}[0];
374             $from= @{$limit->{'from'}}[0];
375             $to= @{$limit->{'to'}}[0];
376         };
377         if( $@ ) {
378             $error++;
379         }
380     }
382     if( $error == 0 ) {
383         $limit_str= "LIMIT $from, $to";
384     }   
385     
386     return $limit_str;
389 sub get_orderby_statement {
390     my ($msg, $msg_hash)= @_;
391     my $error= 0;
392     my $order_str= "";
393     my $order;
394     
395     if( not exists $msg_hash->{'orderby'} ) { $error++; };
397     if( $error == 0) {
398         eval {
399             $order= @{$msg_hash->{'orderby'}}[0];
400         };
401         if( $@ ) {
402             $error++;
403         }
404     }
406     if( $error == 0 ) {
407         $order_str= "ORDER BY $order";   
408     }
409     
410     return $order_str;
413 sub get_dns_domains() {
414         my $line;
415         my @searches;
416         open(RESOLV, "</etc/resolv.conf") or return @searches;
417         while(<RESOLV>){
418                 $line= $_;
419                 chomp $line;
420                 $line =~ s/^\s+//;
421                 $line =~ s/\s+$//;
422                 $line =~ s/\s+/ /;
423                 if ($line =~ /^domain (.*)$/ ){
424                         push(@searches, $1);
425                 } elsif ($line =~ /^search (.*)$/ ){
426                         push(@searches, split(/ /, $1));
427                 }
428         }
429         close(RESOLV);
431         my %tmp = map { $_ => 1 } @searches;
432         @searches = sort keys %tmp;
434         return @searches;
438 sub get_server_addresses {
439     my $domain= shift;
440     my @result;
441     my $error_string;
443     my $error = 0;
444     my $res   = Net::DNS::Resolver->new;
445     my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
446     my @hits;
448     if ($query) {
449         foreach my $rr ($query->answer) {
450             push(@hits, $rr->target.":".$rr->port);
451         }
452     }
453     else {
454         $error_string = "determination of '_gosa-si._tcp' server in domain '$domain' failed: ".$res->errorstring;
455         $error++;
456     }
458     if( $error == 0 ) {
459         foreach my $hit (@hits) {
460             my ($hit_name, $hit_port) = split(/:/, $hit);
461             chomp($hit_name);
462             chomp($hit_port);
464             my $address_query = $res->send($hit_name);
465             if( 1 == length($address_query->answer) ) {
466                 foreach my $rr ($address_query->answer) {
467                     push(@result, $rr->address.":".$hit_port);
468                 }
469             }
470         }
471     }
473     return \@result, $error_string;
477 sub get_logged_in_users {
478     my $result = qx(/usr/bin/w -hs);
479     my @res_lines;
481     if( defined $result ) { 
482         chomp($result);
483         @res_lines = split("\n", $result);
484     }
486     my @logged_in_user_list;
487     foreach my $line (@res_lines) {
488         chomp($line);
489         my @line_parts = split(/\s+/, $line); 
490         push(@logged_in_user_list, $line_parts[0]);
491     }
493     return @logged_in_user_list;
498 sub import_events {
499     my ($event_dir) = @_;
500     my $event_hash;
501     my $error = 0;
502     my @result = ();
503     if (not -e $event_dir) {
504         $error++;
505         push(@result, "cannot find directory or directory is not readable: $event_dir");   
506     }
508     my $DIR;
509     if ($error == 0) {
510         opendir ($DIR, $event_dir) or do { 
511             $error++;
512             push(@result, "cannot open directory '$event_dir' for reading: $!\n");
513         }
514     }
516     if ($error == 0) {
517         while (defined (my $event = readdir ($DIR))) {
518             if( $event eq "." || $event eq ".." ) { next; }  
520             # try to import event module
521             eval{ require $event; };
522             if( $@ ) {
523                 $error++;
524                 #push(@result, "import of event module '$event' failed: $@");
525                 #next;
526                 
527                 &main::daemon_log("ERROR: Import of event module '$event' failed: $@",1);
528                 exit(1);
529             }
531             # fetch all single events
532             $event =~ /(\S*?).pm$/;
533             my $event_module = $1;
534             my $events_l = eval( $1."::get_events()") ;
535             foreach my $event_name (@{$events_l}) {
536                 $event_hash->{$event_module}->{$event_name} = "";
537             }
538             my $events_string = join( ", ", @{$events_l});
539             push(@result, "import of event module '$event' succeed: $events_string");
540         }
541         
542         close $DIR;
543     }
545     return ($error, \@result, $event_hash);
550 #===  FUNCTION  ================================================================
551 #         NAME:  get_ip 
552 #   PARAMETERS:  interface name (i.e. eth0)
553 #      RETURNS:  (ip address) 
554 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
555 #===============================================================================
556 sub get_ip {
557         my $ifreq= shift;
558         my $result= "";
559         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
560         my $proto= getprotobyname('ip');
562         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
563                 or die "socket: $!";
565         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
566                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
567                 my ($port, $addr) = sockaddr_in $sin;
568                 my $ip            = inet_ntoa $addr;
570                 if ($ip && length($ip) > 0) {
571                         $result = $ip;
572                 }
573         }
575         return $result;
579 #===  FUNCTION  ================================================================
580 #         NAME:  get_interface_for_ip
581 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
582 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
583 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
584 #===============================================================================
585 sub get_interface_for_ip {
586         my $result;
587         my $ip= shift;
588         if ($ip && length($ip) > 0) {
589                 my @ifs= &get_interfaces();
590                 if($ip eq "0.0.0.0") {
591                         $result = "all";
592                 } else {
593                         foreach (@ifs) {
594                                 my $if=$_;
595                                 if(&get_ip($if) eq $ip) {
596                                         $result = $if;
597                                 }
598                         }       
599                 }
600         }       
601         return $result;
604 #===  FUNCTION  ================================================================
605 #         NAME:  get_interfaces 
606 #   PARAMETERS:  none
607 #      RETURNS:  (list of interfaces) 
608 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
609 #===============================================================================
610 sub get_interfaces {
611         my @result;
612         my $PROC_NET_DEV= ('/proc/net/dev');
614         open(PROC_NET_DEV, "<$PROC_NET_DEV")
615                 or die "Could not open $PROC_NET_DEV";
617         my @ifs = <PROC_NET_DEV>;
619         close(PROC_NET_DEV);
621         # Eat first two line
622         shift @ifs;
623         shift @ifs;
625         chomp @ifs;
626         foreach my $line(@ifs) {
627                 my $if= (split /:/, $line)[0];
628                 $if =~ s/^\s+//;
629                 push @result, $if;
630         }
632         return @result;
635 sub get_local_ip_for_remote_ip {
636         my $remote_ip= shift;
637         my $result="0.0.0.0";
639         if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
640                 if($remote_ip eq "127.0.0.1") {
641                         $result = "127.0.0.1";
642                 } else {
643                         my $PROC_NET_ROUTE= ('/proc/net/route');
645                         open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
646                                 or die "Could not open $PROC_NET_ROUTE";
648                         my @ifs = <PROC_NET_ROUTE>;
650                         close(PROC_NET_ROUTE);
652                         # Eat header line
653                         shift @ifs;
654                         chomp @ifs;
655                         foreach my $line(@ifs) {
656                                 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
657                                 my $destination;
658                                 my $mask;
659                                 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
660                                 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
661                                 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
662                                 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
663                                 if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) {
664                                         # destination matches route, save mac and exit
665                                         $result= &get_ip($Iface);
666                                         last;
667                                 }
668                         }
669                 }
670         } else {
671                 daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1);
672         }
673         return $result;
677 sub get_mac_for_interface {
678         my $ifreq= shift;
679         my $result;
680         if ($ifreq && length($ifreq) > 0) { 
681                 if($ifreq eq "all") {
682                         $result = "00:00:00:00:00:00";
683                 } else {
684                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
686                         # A configured MAC Address should always override a guessed value
687                         if ($main::server_mac_address and length($main::server_mac_address) > 0) {
688                                 $result= $main::server_mac_address;
689                         }
691                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
692                                 or die "socket: $!";
694                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
695                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
697                                 if (length($mac) > 0) {
698                                         $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
699                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
700                                         $result = $mac;
701                                 }
702                         }
703                 }
704         }
705         return $result;
709 sub get_local_mac_for_remote_ip {
710     my $ip = shift;
712     my $local_ip = &get_local_ip_for_remote_ip($ip);
713     my $network_interface= &get_interface_for_ip($local_ip);
714     my $mac = &get_mac_for_interface($network_interface);
716     return $mac
720 #===  FUNCTION  ================================================================
721 #         NAME:  is_local
722 #   PARAMETERS:  Server Address
723 #      RETURNS:  true if Server Address is on this host, false otherwise
724 #  DESCRIPTION:  Checks all interface addresses, stops on first match
725 #===============================================================================
726 sub is_local {
727     my $server_address = shift || "";
728     my $result = 0;
730     my $server_ip = $1 if $server_address =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,6}$/;
732     if(defined($server_ip) && length($server_ip) > 0) {
733         foreach my $interface(&get_interfaces()) {
734             my $ip_address= &get_ip($interface);
735             if($ip_address eq $server_ip) {
736                 $result = 1;
737                 last;
738             }
739         }
740     }
742     return $result;
746 #===  FUNCTION  ================================================================
747 #         NAME:  run_as
748 #   PARAMETERS:  uid, command
749 #      RETURNS:  hash with keys 'resultCode' = resultCode of command and 
750 #                'output' = program output
751 #  DESCRIPTION:  Runs command as uid using the sudo utility.
752 #===============================================================================
753 sub run_as {
754         my ($uid, $command) = @_;
755         my $sudo_cmd = `which sudo`;
756         chomp($sudo_cmd);
757         if(! -x $sudo_cmd) {
758                 &main::daemon_log("ERROR: The sudo utility is not available! Please fix this!");
759         }
760         my $cmd_line= "$sudo_cmd su - $uid -c '$command'";
761         open(PIPE, "$cmd_line |");
762         my $result = {'resultCode' => $?};
763         $result->{'command'} = $cmd_line;
764         push @{$result->{'output'}}, <PIPE>;
765         return $result;
769 #===  FUNCTION  ================================================================
770 #         NAME:  inform_other_si_server
771 #   PARAMETERS:  message
772 #      RETURNS:  nothing
773 #  DESCRIPTION:  Sends message to all other SI-server found in known_server_db. 
774 #===============================================================================
775 sub inform_all_other_si_server {
776     my ($msg) = @_;
778     # determine all other si-server from known_server_db
779     my $sql_statement= "SELECT * FROM $main::known_server_tn";
780     my $res = $main::known_server_db->select_dbentry( $sql_statement ); 
782     while( my ($hit_num, $hit) = each %$res ) {    
783         my $act_target_address = $hit->{hostname};
784         my $act_target_key = $hit->{hostkey};
786         # determine the source address corresponding to the actual target address
787         my ($act_target_ip, $act_target_port) = split(/:/, $act_target_address);
788         my $act_source_address = &main::get_local_ip_for_remote_ip($act_target_ip).":$act_target_port";
790         # fill into message the correct target and source addresses
791         my $act_msg = $msg;
792         $act_msg =~ s/<target>\w*<\/target>/<target>$act_target_address<\/target>/g;
793         $act_msg =~ s/<source>\w*<\/source>/<source>$act_source_address<\/source>/g;
795         # send message to the target
796         &main::send_msg_to_target($act_msg, $act_target_address, $act_target_key, "foreign_job_updates" , "J");
797     }
799     return;
803 sub read_configfile {
804     my ($cfg_file, %cfg_defaults) = @_ ;
805     my $cfg;
806     if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
807         if( -r $cfg_file ) {
808             $cfg = Config::IniFiles->new( -file => $cfg_file );
809         } else {
810             print STDERR "Couldn't read config file!";
811         }
812     } else {
813         $cfg = Config::IniFiles->new() ;
814     }
815     foreach my $section (keys %cfg_defaults) {
816         foreach my $param (keys %{$cfg_defaults{ $section }}) {
817             my $pinfo = $cfg_defaults{ $section }{ $param };
818            ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
819         }
820     }
824 sub check_opsi_res {
825     my $res= shift;
827     if($res) {
828         if ($res->is_error) {
829             my $error_string;
830             if (ref $res->error_message eq "HASH") { 
831                 $error_string = $res->error_message->{'message'}; 
832             } else { 
833                 $error_string = $res->error_message; 
834             }
835             return 1, $error_string;
836         }
837     } else {
838         return 1, $main::opsi_client->status_line;
839     }
840     return 0;
844 1;