Code

9e7062472370305a6a7a1342036a2be3372e0493
[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     "get_utc_time",
14     "build_msg",
15     "db_res2xml",
16     "db_res2si_msg",
17     "get_where_statement",
18     "get_select_statement",
19     "get_update_statement",
20     "get_limit_statement",
21     "get_orderby_statement",
22     "get_dns_domains",
23     "get_server_addresses",
24     "get_logged_in_users",
25     "import_events",
26     "del_doubles",
27     "get_ip",
28     "get_interface_for_ip",
29     "get_interfaces",
30     "get_mac_for_interface",
31     "get_local_ip_for_remote_ip",
32     "is_local",
33     "run_as",
34     "inform_all_other_si_server",
35     "read_configfile",
36     "check_opsi_res",
37     "calc_timestamp",
38     "opsi_callobj2string",
39     ); 
40 @EXPORT = @functions;
41 use strict;
42 use warnings;
43 use IO::Socket::INET;
44 use Crypt::Rijndael;
45 use Digest::MD5  qw(md5 md5_hex md5_base64);
46 use MIME::Base64;
47 use XML::Simple;
48 use Data::Dumper;
49 use Net::DNS;
50 use DateTime;
53 my $op_hash = {
54     'eq' => '=',
55     'ne' => '!=',
56     'ge' => '>=',
57     'gt' => '>',
58     'le' => '<=',
59     'lt' => '<',
60     'like' => ' LIKE ',
61 };
64 BEGIN {}
66 END {}
68 ### Start ######################################################################
70 my $xml = new XML::Simple();
72 sub daemon_log {
73     my ($msg, $level) = @_ ;
74     &main::daemon_log($msg, $level);
75     return;
76 }
79 sub create_passwd {
80     my $new_passwd = "";
81     for(my $i=0; $i<31; $i++) {
82         $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
83     }
85     return $new_passwd;
86 }
89 sub del_doubles { 
90     my %all; 
91     $all{$_}=0 for @_; 
92     return (keys %all); 
93 }
96 #===  FUNCTION  ================================================================
97 #         NAME:  create_xml_hash
98 #   PARAMETERS:  header - string - message header (required)
99 #                source - string - where the message come from (required)
100 #                target - string - where the message should go to (required)
101 #                [header_value] - string - something usefull (optional)
102 #      RETURNS:  hash - hash - nomen est omen
103 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
104 #===============================================================================
105 sub create_xml_hash {
106     my ($header, $source, $target, $header_value) = @_;
107     my $hash = {
108             header => [$header],
109             source => [$source],
110             target => [$target],
111             $header => [$header_value],
112     };
113     return $hash
117 #===  FUNCTION  ================================================================
118 #         NAME:  create_xml_string
119 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
120 #      RETURNS:  xml_string - string - xml string representation of the hash
121 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
122 #===============================================================================
123 sub create_xml_string {
124     my ($xml_hash) = @_ ;
125     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
126     #$xml_string =~ s/[\n]+//g;
127     #daemon_log("create_xml_string:",7);
128     #daemon_log("$xml_string\n", 7);
129     return $xml_string;
133 sub transform_msg2hash {
134     my ($msg) = @_ ;
135     my $hash = $xml->XMLin($msg, ForceArray=>1);
136     
137     # xml tags without a content are created as an empty hash
138     # substitute it with an empty list
139     eval {
140         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
141             if( 1 == @{ $xml_content } ) {
142                 # there is only one element in xml_content list ...
143                 my $element = @{ $xml_content }[0];
144                 if( ref($element) eq "HASH" ) {
145                     # and this element is an hash ...
146                     my $len_element = keys %{ $element };
147                     if( $len_element == 0 ) {
148                         # and this hash is empty, then substitute the xml_content
149                         # with an empty string in list
150                         $hash->{$xml_tag} = [ "none" ];
151                     }
152                 }
153             }
154         }
155     };
156     if( $@ ) {  
157         $hash = undef;
158     }
160     return $hash;
164 #===  FUNCTION  ================================================================
165 #         NAME:  add_content2xml_hash
166 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
167 #                element - string - key for the hash
168 #                content - string - value for the hash
169 #      RETURNS:  nothing
170 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
171 #                then append value to list
172 #===============================================================================
173 sub add_content2xml_hash {
174     my ($xml_ref, $element, $content) = @_;
175     if(not exists $$xml_ref{$element} ) {
176         $$xml_ref{$element} = [];
177     }
178     my $tmp = $$xml_ref{$element};
179     push(@$tmp, $content);
180     return;
184 sub get_time {
185         my ($seconds, $minutes, $hours, $monthday, $month,
186                 $year, $weekday, $yearday, $sommertime) = localtime;
187         $hours = $hours < 10 ? $hours = "0".$hours : $hours;
188         $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
189         $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
190         $month+=1;
191         $month = $month < 10 ? $month = "0".$month : $month;
192         $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
193         $year+=1900;
194         return "$year$month$monthday$hours$minutes$seconds";
198 sub get_utc_time {
199     my $utc_time = qx(date --utc +%Y%m%d%H%M%S);
200     $utc_time =~ s/\s$//;
201     return $utc_time;
205 #===  FUNCTION  ================================================================
206 #         NAME: build_msg
207 #  DESCRIPTION: Send a message to a destination
208 #   PARAMETERS: [header] Name of the header
209 #               [from]   sender ip
210 #               [to]     recipient ip
211 #               [data]   Hash containing additional attributes for the xml
212 #                        package
213 #      RETURNS:  nothing
214 #===============================================================================
215 sub build_msg ($$$$) {
216         my ($header, $from, $to, $data) = @_;
218     # data is of form, i.e.
219     # %data= ('ip' => $address, 'mac' => $mac);
221         my $out_hash = &create_xml_hash($header, $from, $to);
223         while ( my ($key, $value) = each(%$data) ) {
224                 if(ref($value) eq 'ARRAY'){
225                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
226                 } else {
227                         &add_content2xml_hash($out_hash, $key, $value);
228                 }
229         }
230     my $out_msg = &create_xml_string($out_hash);
231     return $out_msg;
235 sub db_res2xml {
236     my ($db_res) = @_ ;
237     my $xml = "";
239     my $len_db_res= keys %{$db_res};
240     for( my $i= 1; $i<= $len_db_res; $i++ ) {
241         $xml .= "\n<answer$i>";
242         my $hash= $db_res->{$i};
243         while ( my ($column_name, $column_value) = each %{$hash} ) {
244             $xml .= "<$column_name>";
245             my $xml_content;
246             if( $column_name eq "xmlmessage" ) {
247                 $xml_content = &encode_base64($column_value);
248             } else {
249                 $xml_content = defined $column_value ? $column_value : "";
250             }
251             $xml .= $xml_content;
252             $xml .= "</$column_name>"; 
253         }
254         $xml .= "</answer$i>";
256     }
258     return $xml;
262 sub db_res2si_msg {
263     my ($db_res, $header, $target, $source) = @_;
265     my $si_msg = "<xml>";
266     $si_msg .= "<header>$header</header>";
267     $si_msg .= "<source>$source</source>";
268     $si_msg .= "<target>$target</target>";
269     $si_msg .= &db_res2xml;
270     $si_msg .= "</xml>";
274 sub get_where_statement {
275     my ($msg, $msg_hash) = @_;
276     my $error= 0;
277     
278     my $clause_str= "";
279     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
280         $error++; 
281     }
283     if( $error == 0 ) {
284         my @clause_l;
285         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
286         foreach my $clause (@where) {
287             my $connector = $clause->{'connector'}[0];
288             if( not defined $connector ) { $connector = "AND"; }
289             $connector = uc($connector);
290             delete($clause->{'connector'});
292             my @phrase_l ;
293             foreach my $phrase (@{$clause->{'phrase'}}) {
294                 my $operator = "=";
295                 if( exists $phrase->{'operator'} ) {
296                     my $op = $op_hash->{$phrase->{'operator'}[0]};
297                     if( not defined $op ) {
298                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
299                                 "statement to sql valid syntax. Please use 'eq', ".
300                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
301                         &main::daemon_log($msg, 8);
302                         $op = "=";
303                     }
304                     $operator = $op;
305                     delete($phrase->{'operator'});
306                 }
308                 my @xml_tags = keys %{$phrase};
309                 my $tag = $xml_tags[0];
310                 my $val = $phrase->{$tag}[0];
311                 if( ref($val) eq "HASH" ) { next; }  # empty xml-tags should not appear in where statement
313                                 # integer columns do not have to have single quotes besides the value
314                                 if ($tag eq "id") {
315                                                 push(@phrase_l, "$tag$operator$val");
316                                 } else {
317                                                 push(@phrase_l, "$tag$operator'$val'");
318                                 }
319             }
321             if (not 0 == @phrase_l) {
322                 my $clause_str .= join(" $connector ", @phrase_l);
323                 push(@clause_l, "($clause_str)");
324             }
325         }
327         if( not 0 == @clause_l ) {
328             $clause_str = join(" AND ", @clause_l);
329             $clause_str = "WHERE ($clause_str) ";
330         }
331     }
333     return $clause_str;
336 sub get_select_statement {
337     my ($msg, $msg_hash)= @_;
338     my $select = "*";
339     if( exists $msg_hash->{'select'} ) {
340         my $select_l = \@{$msg_hash->{'select'}};
341         $select = join(', ', @{$select_l});
342     }
343     return $select;
347 sub get_update_statement {
348     my ($msg, $msg_hash) = @_;
349     my $error= 0;
350     my $update_str= "";
351     my @update_l; 
353     if( not exists $msg_hash->{'update'} ) { $error++; };
355     if( $error == 0 ) {
356         my $update= @{$msg_hash->{'update'}}[0];
357         while( my ($tag, $val) = each %{$update} ) {
358             my $val= @{$update->{$tag}}[0];
359             push(@update_l, "$tag='$val'");
360         }
361         if( 0 == @update_l ) { $error++; };   
362     }
364     if( $error == 0 ) { 
365         $update_str= join(', ', @update_l);
366         $update_str= "SET $update_str ";
367     }
369     return $update_str;
372 sub get_limit_statement {
373     my ($msg, $msg_hash)= @_; 
374     my $error= 0;
375     my $limit_str = "";
376     my ($from, $to);
378     if( not exists $msg_hash->{'limit'} ) { $error++; };
380     if( $error == 0 ) {
381         eval {
382             my $limit= @{$msg_hash->{'limit'}}[0];
383             $from= @{$limit->{'from'}}[0];
384             $to= @{$limit->{'to'}}[0];
385         };
386         if( $@ ) {
387             $error++;
388         }
389     }
391     if( $error == 0 ) {
392         $limit_str= "LIMIT $from, $to";
393     }   
394     
395     return $limit_str;
398 sub get_orderby_statement {
399     my ($msg, $msg_hash)= @_;
400     my $error= 0;
401     my $order_str= "";
402     my $order;
403     
404     if( not exists $msg_hash->{'orderby'} ) { $error++; };
406     if( $error == 0) {
407         eval {
408             $order= @{$msg_hash->{'orderby'}}[0];
409         };
410         if( $@ ) {
411             $error++;
412         }
413     }
415     if( $error == 0 ) {
416         $order_str= "ORDER BY $order";   
417     }
418     
419     return $order_str;
422 sub get_dns_domains() {
423         my $line;
424         my @searches;
425         open(RESOLV, "</etc/resolv.conf") or return @searches;
426         while(<RESOLV>){
427                 $line= $_;
428                 chomp $line;
429                 $line =~ s/^\s+//;
430                 $line =~ s/\s+$//;
431                 $line =~ s/\s+/ /;
432                 if ($line =~ /^domain (.*)$/ ){
433                         push(@searches, $1);
434                 } elsif ($line =~ /^search (.*)$/ ){
435                         push(@searches, split(/ /, $1));
436                 }
437         }
438         close(RESOLV);
440         my %tmp = map { $_ => 1 } @searches;
441         @searches = sort keys %tmp;
443         return @searches;
447 sub get_server_addresses {
448     my $domain= shift;
449     my @result;
450     my $error_string;
452     my $error = 0;
453     my $res   = Net::DNS::Resolver->new;
454     my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
455     my @hits;
457     if ($query) {
458         foreach my $rr ($query->answer) {
459             push(@hits, $rr->target.":".$rr->port);
460         }
461     }
462     else {
463         $error_string = "determination of '_gosa-si._tcp' server in domain '$domain' failed: ".$res->errorstring;
464         $error++;
465     }
467     if( $error == 0 ) {
468         foreach my $hit (@hits) {
469             my ($hit_name, $hit_port) = split(/:/, $hit);
470             chomp($hit_name);
471             chomp($hit_port);
473             my $address_query = $res->send($hit_name);
474             if( 1 == length($address_query->answer) ) {
475                 foreach my $rr ($address_query->answer) {
476                     push(@result, $rr->address.":".$hit_port);
477                 }
478             }
479         }
480     }
482     return \@result, $error_string;
486 sub get_logged_in_users {
487     my $result = qx(/usr/bin/w -hs);
488     my @res_lines;
490     if( defined $result ) { 
491         chomp($result);
492         @res_lines = split("\n", $result);
493     }
495     my @logged_in_user_list;
496     foreach my $line (@res_lines) {
497         chomp($line);
498         my @line_parts = split(/\s+/, $line); 
499         push(@logged_in_user_list, $line_parts[0]);
500     }
502     return @logged_in_user_list;
507 sub import_events {
508     my ($event_dir) = @_;
509     my $event_hash;
510     my $error = 0;
511     my @result = ();
512     if (not -e $event_dir) {
513         $error++;
514         push(@result, "cannot find directory or directory is not readable: $event_dir");   
515     }
517     my $DIR;
518     if ($error == 0) {
519         opendir ($DIR, $event_dir) or do { 
520             $error++;
521             push(@result, "cannot open directory '$event_dir' for reading: $!\n");
522         }
523     }
525     if ($error == 0) {
526         while (defined (my $event = readdir ($DIR))) {
527             if( $event eq "." || $event eq ".." ) { next; }  
529             # try to import event module
530             eval{ require $event; };
531             if( $@ ) {
532                 $error++;
533                 #push(@result, "import of event module '$event' failed: $@");
534                 #next;
535                 
536                 &main::daemon_log("ERROR: Import of event module '$event' failed: $@",1);
537                 exit(1);
538             }
540             # fetch all single events
541             $event =~ /(\S*?).pm$/;
542             my $event_module = $1;
543             my $events_l = eval( $1."::get_events()") ;
544             foreach my $event_name (@{$events_l}) {
545                 $event_hash->{$event_module}->{$event_name} = "";
546             }
547             my $events_string = join( ", ", @{$events_l});
548             push(@result, "import of event module '$event' succeed: $events_string");
549         }
550         
551         close $DIR;
552     }
554     return ($error, \@result, $event_hash);
559 #===  FUNCTION  ================================================================
560 #         NAME:  get_ip 
561 #   PARAMETERS:  interface name (i.e. eth0)
562 #      RETURNS:  (ip address) 
563 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
564 #===============================================================================
565 sub get_ip {
566         my $ifreq= shift;
567         my $result= "";
568         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
569         my $proto= getprotobyname('ip');
571         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
572                 or die "socket: $!";
574         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
575                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
576                 my ($port, $addr) = sockaddr_in $sin;
577                 my $ip            = inet_ntoa $addr;
579                 if ($ip && length($ip) > 0) {
580                         $result = $ip;
581                 }
582         }
584         return $result;
588 #===  FUNCTION  ================================================================
589 #         NAME:  get_interface_for_ip
590 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
591 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
592 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
593 #===============================================================================
594 sub get_interface_for_ip {
595         my $result;
596         my $ip= shift;
597         if ($ip && length($ip) > 0) {
598                 my @ifs= &get_interfaces();
599                 if($ip eq "0.0.0.0") {
600                         $result = "all";
601                 } else {
602                         foreach (@ifs) {
603                                 my $if=$_;
604                                 if(&get_ip($if) eq $ip) {
605                                         $result = $if;
606                                 }
607                         }       
608                 }
609         }       
610         return $result;
613 #===  FUNCTION  ================================================================
614 #         NAME:  get_interfaces 
615 #   PARAMETERS:  none
616 #      RETURNS:  (list of interfaces) 
617 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
618 #===============================================================================
619 sub get_interfaces {
620         my @result;
621         my $PROC_NET_DEV= ('/proc/net/dev');
623         open(PROC_NET_DEV, "<$PROC_NET_DEV")
624                 or die "Could not open $PROC_NET_DEV";
626         my @ifs = <PROC_NET_DEV>;
628         close(PROC_NET_DEV);
630         # Eat first two line
631         shift @ifs;
632         shift @ifs;
634         chomp @ifs;
635         foreach my $line(@ifs) {
636                 my $if= (split /:/, $line)[0];
637                 $if =~ s/^\s+//;
638                 push @result, $if;
639         }
641         return @result;
644 sub get_local_ip_for_remote_ip {
645         my $remote_ip= shift;
646         my $result="0.0.0.0";
648     if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
649         my $PROC_NET_ROUTE= ('/proc/net/route');
651         open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
652             or die "Could not open $PROC_NET_ROUTE";
654         my @ifs = <PROC_NET_ROUTE>;
656         close(PROC_NET_ROUTE);
658         # Eat header line
659         shift @ifs;
660         chomp @ifs;
661         foreach my $line(@ifs) {
662             my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
663             my $destination;
664             my $mask;
665             my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
666             $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
667             ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
668             $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
669             if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) {
670                 # destination matches route, save mac and exit
671                 $result= &get_ip($Iface);
672                 last;
673             }
674         }
675     } 
677         return $result;
681 sub get_mac_for_interface {
682         my $ifreq= shift;
683         my $result;
684         if ($ifreq && length($ifreq) > 0) { 
685                 if($ifreq eq "all") {
686                         $result = "00:00:00:00:00:00";
687                 } else {
688                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
690                         # A configured MAC Address should always override a guessed value
691                         if ($main::server_mac_address and length($main::server_mac_address) > 0) {
692                                 $result= $main::server_mac_address;
693                         }
695                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
696                                 or die "socket: $!";
698                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
699                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
701                                 if (length($mac) > 0) {
702                                         $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])$/;
703                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
704                                         $result = $mac;
705                                 }
706                         }
707                 }
708         }
709         return $result;
713 #===  FUNCTION  ================================================================
714 #         NAME:  is_local
715 #   PARAMETERS:  Server Address
716 #      RETURNS:  true if Server Address is on this host, false otherwise
717 #  DESCRIPTION:  Checks all interface addresses, stops on first match
718 #===============================================================================
719 sub is_local {
720     my $server_address = shift || "";
721     my $result = 0;
723     my $server_ip = $1 if $server_address =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,6}$/;
725     if(defined($server_ip) && length($server_ip) > 0) {
726         foreach my $interface(&get_interfaces()) {
727             my $ip_address= &get_ip($interface);
728             if($ip_address eq $server_ip) {
729                 $result = 1;
730                 last;
731             }
732         }
733     }
735     return $result;
739 #===  FUNCTION  ================================================================
740 #         NAME:  run_as
741 #   PARAMETERS:  uid, command
742 #      RETURNS:  hash with keys 'resultCode' = resultCode of command and 
743 #                'output' = program output
744 #  DESCRIPTION:  Runs command as uid using the sudo utility.
745 #===============================================================================
746 sub run_as {
747         my ($uid, $command) = @_;
748         my $sudo_cmd = `which sudo`;
749         chomp($sudo_cmd);
750         if(! -x $sudo_cmd) {
751                 &main::daemon_log("ERROR: The sudo utility is not available! Please fix this!");
752         }
753         my $cmd_line= "$sudo_cmd su - $uid -c '$command'";
754         open(PIPE, "$cmd_line |");
755         my $result = {'resultCode' => $?};
756         $result->{'command'} = $cmd_line;
757         push @{$result->{'output'}}, <PIPE>;
758         return $result;
762 #===  FUNCTION  ================================================================
763 #         NAME:  inform_other_si_server
764 #   PARAMETERS:  message
765 #      RETURNS:  nothing
766 #  DESCRIPTION:  Sends message to all other SI-server found in known_server_db. 
767 #===============================================================================
768 sub inform_all_other_si_server {
769     my ($msg) = @_;
771     # determine all other si-server from known_server_db
772     my $sql_statement= "SELECT * FROM $main::known_server_tn";
773     my $res = $main::known_server_db->select_dbentry( $sql_statement ); 
775     while( my ($hit_num, $hit) = each %$res ) {    
776         my $act_target_address = $hit->{hostname};
777         my $act_target_key = $hit->{hostkey};
779         # determine the source address corresponding to the actual target address
780         my ($act_target_ip, $act_target_port) = split(/:/, $act_target_address);
781         my $act_source_address = &main::get_local_ip_for_remote_ip($act_target_ip).":$act_target_port";
783         # fill into message the correct target and source addresses
784         my $act_msg = $msg;
785         $act_msg =~ s/<target>\w*<\/target>/<target>$act_target_address<\/target>/g;
786         $act_msg =~ s/<source>\w*<\/source>/<source>$act_source_address<\/source>/g;
788         # send message to the target
789         &main::send_msg_to_target($act_msg, $act_target_address, $act_target_key, "foreign_job_updates" , "J");
790     }
792     return;
796 sub read_configfile {
797     my ($cfg_file, %cfg_defaults) = @_ ;
798     my $cfg;
799     if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
800         if( -r $cfg_file ) {
801             $cfg = Config::IniFiles->new( -file => $cfg_file, -nocase => 1 );
802         } else {
803             print STDERR "Couldn't read config file!";
804         }
805     } else {
806         $cfg = Config::IniFiles->new() ;
807     }
808     foreach my $section (keys %cfg_defaults) {
809         foreach my $param (keys %{$cfg_defaults{ $section }}) {
810             my $pinfo = $cfg_defaults{ $section }{ $param };
811            ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
812         }
813     }
817 sub check_opsi_res {
818     my $res= shift;
820     if($res) {
821         if ($res->is_error) {
822             my $error_string;
823             if (ref $res->error_message eq "HASH") { 
824                 $error_string = $res->error_message->{'message'}; 
825             } else { 
826                 $error_string = $res->error_message; 
827             }
828             return 1, $error_string;
829         }
830     } else {
831         return 1, $main::opsi_client->status_line;
832     }
833     return 0;
836 sub calc_timestamp {
837     my ($timestamp, $operation, $value) = @_ ;
838     my $res_timestamp = 0;
839     
840     $value = int($value);
841     $timestamp = int($timestamp);
842     $timestamp =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
843     my $dt = DateTime->new( year   => $1,
844             month  => $2,
845             day    => $3,
846             hour   => $4,
847             minute => $5,
848             second => $6,
849             );
851     if ($operation eq "plus" || $operation eq "+") {
852         $dt->add( seconds => $value);
853         $res_timestamp = $dt->ymd('').$dt->hms('');
854     }
856     if ($operation eq "minus" || $operation eq "-") {
857         $dt->subtract(seconds => $value);
858         $res_timestamp = $dt->ymd('').$dt->hms('');
859     }
861     return $res_timestamp;
864 sub opsi_callobj2string {
865     my ($callobj) = @_;
866     my @callobj_string;
867     while(my ($key, $value) = each(%$callobj)) {
868         my $value_string = "";
869         if (ref($value) eq "ARRAY") {
870             $value_string = join(",", @$value);
871         } else {
872             $value_string = $value;
873         }
874         push(@callobj_string, "$key=$value_string")
875     }
876     return join(", ", @callobj_string);
879 1;