Code

Added DBsqlite to sed rewrite.
[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     ); 
39 @EXPORT = @functions;
40 use strict;
41 use warnings;
42 use IO::Socket::INET;
43 use Crypt::Rijndael;
44 use Digest::MD5  qw(md5 md5_hex md5_base64);
45 use MIME::Base64;
46 use XML::Simple;
47 use Data::Dumper;
48 use Net::DNS;
49 use DateTime;
52 my $op_hash = {
53     'eq' => '=',
54     'ne' => '!=',
55     'ge' => '>=',
56     'gt' => '>',
57     'le' => '<=',
58     'lt' => '<',
59     'like' => ' LIKE ',
60 };
63 BEGIN {}
65 END {}
67 ### Start ######################################################################
69 my $xml = new XML::Simple();
71 sub daemon_log {
72     my ($msg, $level) = @_ ;
73     &main::daemon_log($msg, $level);
74     return;
75 }
78 sub create_passwd {
79     my $new_passwd = "";
80     for(my $i=0; $i<31; $i++) {
81         $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
82     }
84     return $new_passwd;
85 }
88 sub del_doubles { 
89     my %all; 
90     $all{$_}=0 for @_; 
91     return (keys %all); 
92 }
95 #===  FUNCTION  ================================================================
96 #         NAME:  create_xml_hash
97 #   PARAMETERS:  header - string - message header (required)
98 #                source - string - where the message come from (required)
99 #                target - string - where the message should go to (required)
100 #                [header_value] - string - something usefull (optional)
101 #      RETURNS:  hash - hash - nomen est omen
102 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
103 #===============================================================================
104 sub create_xml_hash {
105     my ($header, $source, $target, $header_value) = @_;
106     my $hash = {
107             header => [$header],
108             source => [$source],
109             target => [$target],
110             $header => [$header_value],
111     };
112     return $hash
116 #===  FUNCTION  ================================================================
117 #         NAME:  create_xml_string
118 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
119 #      RETURNS:  xml_string - string - xml string representation of the hash
120 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
121 #===============================================================================
122 sub create_xml_string {
123     my ($xml_hash) = @_ ;
124     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
125     #$xml_string =~ s/[\n]+//g;
126     #daemon_log("create_xml_string:",7);
127     #daemon_log("$xml_string\n", 7);
128     return $xml_string;
132 sub transform_msg2hash {
133     my ($msg) = @_ ;
134     my $hash = $xml->XMLin($msg, ForceArray=>1);
135     
136     # xml tags without a content are created as an empty hash
137     # substitute it with an empty list
138     eval {
139         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
140             if( 1 == @{ $xml_content } ) {
141                 # there is only one element in xml_content list ...
142                 my $element = @{ $xml_content }[0];
143                 if( ref($element) eq "HASH" ) {
144                     # and this element is an hash ...
145                     my $len_element = keys %{ $element };
146                     if( $len_element == 0 ) {
147                         # and this hash is empty, then substitute the xml_content
148                         # with an empty string in list
149                         $hash->{$xml_tag} = [ "none" ];
150                     }
151                 }
152             }
153         }
154     };
155     if( $@ ) {  
156         $hash = undef;
157     }
159     return $hash;
163 #===  FUNCTION  ================================================================
164 #         NAME:  add_content2xml_hash
165 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
166 #                element - string - key for the hash
167 #                content - string - value for the hash
168 #      RETURNS:  nothing
169 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
170 #                then append value to list
171 #===============================================================================
172 sub add_content2xml_hash {
173     my ($xml_ref, $element, $content) = @_;
174     if(not exists $$xml_ref{$element} ) {
175         $$xml_ref{$element} = [];
176     }
177     my $tmp = $$xml_ref{$element};
178     push(@$tmp, $content);
179     return;
183 sub get_time {
184         my ($seconds, $minutes, $hours, $monthday, $month,
185                 $year, $weekday, $yearday, $sommertime) = localtime;
186         $hours = $hours < 10 ? $hours = "0".$hours : $hours;
187         $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
188         $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
189         $month+=1;
190         $month = $month < 10 ? $month = "0".$month : $month;
191         $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
192         $year+=1900;
193         return "$year$month$monthday$hours$minutes$seconds";
197 sub get_utc_time {
198     my $utc_time = qx(date --utc +%Y%m%d%H%M%S);
199     $utc_time =~ s/\s$//;
200     return $utc_time;
204 #===  FUNCTION  ================================================================
205 #         NAME: build_msg
206 #  DESCRIPTION: Send a message to a destination
207 #   PARAMETERS: [header] Name of the header
208 #               [from]   sender ip
209 #               [to]     recipient ip
210 #               [data]   Hash containing additional attributes for the xml
211 #                        package
212 #      RETURNS:  nothing
213 #===============================================================================
214 sub build_msg ($$$$) {
215         my ($header, $from, $to, $data) = @_;
217     # data is of form, i.e.
218     # %data= ('ip' => $address, 'mac' => $mac);
220         my $out_hash = &create_xml_hash($header, $from, $to);
222         while ( my ($key, $value) = each(%$data) ) {
223                 if(ref($value) eq 'ARRAY'){
224                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
225                 } else {
226                         &add_content2xml_hash($out_hash, $key, $value);
227                 }
228         }
229     my $out_msg = &create_xml_string($out_hash);
230     return $out_msg;
234 sub db_res2xml {
235     my ($db_res) = @_ ;
236     my $xml = "";
238     my $len_db_res= keys %{$db_res};
239     for( my $i= 1; $i<= $len_db_res; $i++ ) {
240         $xml .= "\n<answer$i>";
241         my $hash= $db_res->{$i};
242         while ( my ($column_name, $column_value) = each %{$hash} ) {
243             $xml .= "<$column_name>";
244             my $xml_content;
245             if( $column_name eq "xmlmessage" ) {
246                 $xml_content = &encode_base64($column_value);
247             } else {
248                 $xml_content = defined $column_value ? $column_value : "";
249             }
250             $xml .= $xml_content;
251             $xml .= "</$column_name>"; 
252         }
253         $xml .= "</answer$i>";
255     }
257     return $xml;
261 sub db_res2si_msg {
262     my ($db_res, $header, $target, $source) = @_;
264     my $si_msg = "<xml>";
265     $si_msg .= "<header>$header</header>";
266     $si_msg .= "<source>$source</source>";
267     $si_msg .= "<target>$target</target>";
268     $si_msg .= &db_res2xml;
269     $si_msg .= "</xml>";
273 sub get_where_statement {
274     my ($msg, $msg_hash) = @_;
275     my $error= 0;
276     
277     my $clause_str= "";
278     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
279         $error++; 
280     }
282     if( $error == 0 ) {
283         my @clause_l;
284         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
285         foreach my $clause (@where) {
286             my $connector = $clause->{'connector'}[0];
287             if( not defined $connector ) { $connector = "AND"; }
288             $connector = uc($connector);
289             delete($clause->{'connector'});
291             my @phrase_l ;
292             foreach my $phrase (@{$clause->{'phrase'}}) {
293                 my $operator = "=";
294                 if( exists $phrase->{'operator'} ) {
295                     my $op = $op_hash->{$phrase->{'operator'}[0]};
296                     if( not defined $op ) {
297                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
298                                 "statement to sql valid syntax. Please use 'eq', ".
299                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
300                         &main::daemon_log($msg, 8);
301                         $op = "=";
302                     }
303                     $operator = $op;
304                     delete($phrase->{'operator'});
305                 }
307                 my @xml_tags = keys %{$phrase};
308                 my $tag = $xml_tags[0];
309                 my $val = $phrase->{$tag}[0];
310                 if( ref($val) eq "HASH" ) { next; }  # empty xml-tags should not appear in where statement
312                                 # integer columns do not have to have single quotes besides the value
313                                 if ($tag eq "id") {
314                                                 push(@phrase_l, "$tag$operator$val");
315                                 } else {
316                                                 push(@phrase_l, "$tag$operator'$val'");
317                                 }
318             }
320             if (not 0 == @phrase_l) {
321                 my $clause_str .= join(" $connector ", @phrase_l);
322                 push(@clause_l, "($clause_str)");
323             }
324         }
326         if( not 0 == @clause_l ) {
327             $clause_str = join(" AND ", @clause_l);
328             $clause_str = "WHERE ($clause_str) ";
329         }
330     }
332     return $clause_str;
335 sub get_select_statement {
336     my ($msg, $msg_hash)= @_;
337     my $select = "*";
338     if( exists $msg_hash->{'select'} ) {
339         my $select_l = \@{$msg_hash->{'select'}};
340         $select = join(', ', @{$select_l});
341     }
342     return $select;
346 sub get_update_statement {
347     my ($msg, $msg_hash) = @_;
348     my $error= 0;
349     my $update_str= "";
350     my @update_l; 
352     if( not exists $msg_hash->{'update'} ) { $error++; };
354     if( $error == 0 ) {
355         my $update= @{$msg_hash->{'update'}}[0];
356         while( my ($tag, $val) = each %{$update} ) {
357             my $val= @{$update->{$tag}}[0];
358             push(@update_l, "$tag='$val'");
359         }
360         if( 0 == @update_l ) { $error++; };   
361     }
363     if( $error == 0 ) { 
364         $update_str= join(', ', @update_l);
365         $update_str= "SET $update_str ";
366     }
368     return $update_str;
371 sub get_limit_statement {
372     my ($msg, $msg_hash)= @_; 
373     my $error= 0;
374     my $limit_str = "";
375     my ($from, $to);
377     if( not exists $msg_hash->{'limit'} ) { $error++; };
379     if( $error == 0 ) {
380         eval {
381             my $limit= @{$msg_hash->{'limit'}}[0];
382             $from= @{$limit->{'from'}}[0];
383             $to= @{$limit->{'to'}}[0];
384         };
385         if( $@ ) {
386             $error++;
387         }
388     }
390     if( $error == 0 ) {
391         $limit_str= "LIMIT $from, $to";
392     }   
393     
394     return $limit_str;
397 sub get_orderby_statement {
398     my ($msg, $msg_hash)= @_;
399     my $error= 0;
400     my $order_str= "";
401     my $order;
402     
403     if( not exists $msg_hash->{'orderby'} ) { $error++; };
405     if( $error == 0) {
406         eval {
407             $order= @{$msg_hash->{'orderby'}}[0];
408         };
409         if( $@ ) {
410             $error++;
411         }
412     }
414     if( $error == 0 ) {
415         $order_str= "ORDER BY $order";   
416     }
417     
418     return $order_str;
421 sub get_dns_domains() {
422         my $line;
423         my @searches;
424         open(RESOLV, "</etc/resolv.conf") or return @searches;
425         while(<RESOLV>){
426                 $line= $_;
427                 chomp $line;
428                 $line =~ s/^\s+//;
429                 $line =~ s/\s+$//;
430                 $line =~ s/\s+/ /;
431                 if ($line =~ /^domain (.*)$/ ){
432                         push(@searches, $1);
433                 } elsif ($line =~ /^search (.*)$/ ){
434                         push(@searches, split(/ /, $1));
435                 }
436         }
437         close(RESOLV);
439         my %tmp = map { $_ => 1 } @searches;
440         @searches = sort keys %tmp;
442         return @searches;
446 sub get_server_addresses {
447     my $domain= shift;
448     my @result;
449     my $error_string;
451     my $error = 0;
452     my $res   = Net::DNS::Resolver->new;
453     my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
454     my @hits;
456     if ($query) {
457         foreach my $rr ($query->answer) {
458             push(@hits, $rr->target.":".$rr->port);
459         }
460     }
461     else {
462         $error_string = "determination of '_gosa-si._tcp' server in domain '$domain' failed: ".$res->errorstring;
463         $error++;
464     }
466     if( $error == 0 ) {
467         foreach my $hit (@hits) {
468             my ($hit_name, $hit_port) = split(/:/, $hit);
469             chomp($hit_name);
470             chomp($hit_port);
472             my $address_query = $res->send($hit_name);
473             if( 1 == length($address_query->answer) ) {
474                 foreach my $rr ($address_query->answer) {
475                     push(@result, $rr->address.":".$hit_port);
476                 }
477             }
478         }
479     }
481     return \@result, $error_string;
485 sub get_logged_in_users {
486     my $result = qx(/usr/bin/w -hs);
487     my @res_lines;
489     if( defined $result ) { 
490         chomp($result);
491         @res_lines = split("\n", $result);
492     }
494     my @logged_in_user_list;
495     foreach my $line (@res_lines) {
496         chomp($line);
497         my @line_parts = split(/\s+/, $line); 
498         push(@logged_in_user_list, $line_parts[0]);
499     }
501     return @logged_in_user_list;
506 sub import_events {
507     my ($event_dir) = @_;
508     my $event_hash;
509     my $error = 0;
510     my @result = ();
511     if (not -e $event_dir) {
512         $error++;
513         push(@result, "cannot find directory or directory is not readable: $event_dir");   
514     }
516     my $DIR;
517     if ($error == 0) {
518         opendir ($DIR, $event_dir) or do { 
519             $error++;
520             push(@result, "cannot open directory '$event_dir' for reading: $!\n");
521         }
522     }
524     if ($error == 0) {
525         while (defined (my $event = readdir ($DIR))) {
526             if( $event eq "." || $event eq ".." ) { next; }  
528             # try to import event module
529             eval{ require $event; };
530             if( $@ ) {
531                 $error++;
532                 #push(@result, "import of event module '$event' failed: $@");
533                 #next;
534                 
535                 &main::daemon_log("ERROR: Import of event module '$event' failed: $@",1);
536                 exit(1);
537             }
539             # fetch all single events
540             $event =~ /(\S*?).pm$/;
541             my $event_module = $1;
542             my $events_l = eval( $1."::get_events()") ;
543             foreach my $event_name (@{$events_l}) {
544                 $event_hash->{$event_module}->{$event_name} = "";
545             }
546             my $events_string = join( ", ", @{$events_l});
547             push(@result, "import of event module '$event' succeed: $events_string");
548         }
549         
550         close $DIR;
551     }
553     return ($error, \@result, $event_hash);
558 #===  FUNCTION  ================================================================
559 #         NAME:  get_ip 
560 #   PARAMETERS:  interface name (i.e. eth0)
561 #      RETURNS:  (ip address) 
562 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
563 #===============================================================================
564 sub get_ip {
565         my $ifreq= shift;
566         my $result= "";
567         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
568         my $proto= getprotobyname('ip');
570         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
571                 or die "socket: $!";
573         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
574                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
575                 my ($port, $addr) = sockaddr_in $sin;
576                 my $ip            = inet_ntoa $addr;
578                 if ($ip && length($ip) > 0) {
579                         $result = $ip;
580                 }
581         }
583         return $result;
587 #===  FUNCTION  ================================================================
588 #         NAME:  get_interface_for_ip
589 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
590 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
591 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
592 #===============================================================================
593 sub get_interface_for_ip {
594         my $result;
595         my $ip= shift;
596         if ($ip && length($ip) > 0) {
597                 my @ifs= &get_interfaces();
598                 if($ip eq "0.0.0.0") {
599                         $result = "all";
600                 } else {
601                         foreach (@ifs) {
602                                 my $if=$_;
603                                 if(&get_ip($if) eq $ip) {
604                                         $result = $if;
605                                 }
606                         }       
607                 }
608         }       
609         return $result;
612 #===  FUNCTION  ================================================================
613 #         NAME:  get_interfaces 
614 #   PARAMETERS:  none
615 #      RETURNS:  (list of interfaces) 
616 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
617 #===============================================================================
618 sub get_interfaces {
619         my @result;
620         my $PROC_NET_DEV= ('/proc/net/dev');
622         open(PROC_NET_DEV, "<$PROC_NET_DEV")
623                 or die "Could not open $PROC_NET_DEV";
625         my @ifs = <PROC_NET_DEV>;
627         close(PROC_NET_DEV);
629         # Eat first two line
630         shift @ifs;
631         shift @ifs;
633         chomp @ifs;
634         foreach my $line(@ifs) {
635                 my $if= (split /:/, $line)[0];
636                 $if =~ s/^\s+//;
637                 push @result, $if;
638         }
640         return @result;
643 sub get_local_ip_for_remote_ip {
644         my $remote_ip= shift;
645         my $result="0.0.0.0";
647     if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
648         my $PROC_NET_ROUTE= ('/proc/net/route');
650         open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
651             or die "Could not open $PROC_NET_ROUTE";
653         my @ifs = <PROC_NET_ROUTE>;
655         close(PROC_NET_ROUTE);
657         # Eat header line
658         shift @ifs;
659         chomp @ifs;
660         foreach my $line(@ifs) {
661             my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
662             my $destination;
663             my $mask;
664             my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
665             $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
666             ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
667             $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
668             if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) {
669                 # destination matches route, save mac and exit
670                 $result= &get_ip($Iface);
671                 last;
672             }
673         }
674     } else {
675                 daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1);
676         }
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 );
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;
865 1;