Code

Moved more values
[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     "is_local",
32     "run_as",
33     "inform_all_other_si_server",
34     "read_configfile",
35     "check_opsi_res",
36     "calc_timestamp",
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;
48 use DateTime;
51 my $op_hash = {
52     'eq' => '=',
53     'ne' => '!=',
54     'ge' => '>=',
55     'gt' => '>',
56     'le' => '<=',
57     'lt' => '<',
58     'like' => ' LIKE ',
59 };
62 BEGIN {}
64 END {}
66 ### Start ######################################################################
68 my $xml = new XML::Simple();
70 sub daemon_log {
71     my ($msg, $level) = @_ ;
72     &main::daemon_log($msg, $level);
73     return;
74 }
77 sub create_passwd {
78     my $new_passwd = "";
79     for(my $i=0; $i<31; $i++) {
80         $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
81     }
83     return $new_passwd;
84 }
87 sub del_doubles { 
88     my %all; 
89     $all{$_}=0 for @_; 
90     return (keys %all); 
91 }
94 #===  FUNCTION  ================================================================
95 #         NAME:  create_xml_hash
96 #   PARAMETERS:  header - string - message header (required)
97 #                source - string - where the message come from (required)
98 #                target - string - where the message should go to (required)
99 #                [header_value] - string - something usefull (optional)
100 #      RETURNS:  hash - hash - nomen est omen
101 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
102 #===============================================================================
103 sub create_xml_hash {
104     my ($header, $source, $target, $header_value) = @_;
105     my $hash = {
106             header => [$header],
107             source => [$source],
108             target => [$target],
109             $header => [$header_value],
110     };
111     return $hash
115 #===  FUNCTION  ================================================================
116 #         NAME:  create_xml_string
117 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
118 #      RETURNS:  xml_string - string - xml string representation of the hash
119 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
120 #===============================================================================
121 sub create_xml_string {
122     my ($xml_hash) = @_ ;
123     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
124     #$xml_string =~ s/[\n]+//g;
125     #daemon_log("create_xml_string:",7);
126     #daemon_log("$xml_string\n", 7);
127     return $xml_string;
131 sub transform_msg2hash {
132     my ($msg) = @_ ;
133     my $hash = $xml->XMLin($msg, ForceArray=>1);
134     
135     # xml tags without a content are created as an empty hash
136     # substitute it with an empty list
137     eval {
138         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
139             if( 1 == @{ $xml_content } ) {
140                 # there is only one element in xml_content list ...
141                 my $element = @{ $xml_content }[0];
142                 if( ref($element) eq "HASH" ) {
143                     # and this element is an hash ...
144                     my $len_element = keys %{ $element };
145                     if( $len_element == 0 ) {
146                         # and this hash is empty, then substitute the xml_content
147                         # with an empty string in list
148                         $hash->{$xml_tag} = [ "none" ];
149                     }
150                 }
151             }
152         }
153     };
154     if( $@ ) {  
155         $hash = undef;
156     }
158     return $hash;
162 #===  FUNCTION  ================================================================
163 #         NAME:  add_content2xml_hash
164 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
165 #                element - string - key for the hash
166 #                content - string - value for the hash
167 #      RETURNS:  nothing
168 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
169 #                then append value to list
170 #===============================================================================
171 sub add_content2xml_hash {
172     my ($xml_ref, $element, $content) = @_;
173     if(not exists $$xml_ref{$element} ) {
174         $$xml_ref{$element} = [];
175     }
176     my $tmp = $$xml_ref{$element};
177     push(@$tmp, $content);
178     return;
182 sub get_time {
183           # Add an optional offset in seconds
184                 my $offset = shift || 0;
185     my ($seconds, $minutes, $hours, $monthday, $month,
186             $year, $weekday, $yearday, $sommertime) = localtime(time+$offset);
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";
199 #===  FUNCTION  ================================================================
200 #         NAME: build_msg
201 #  DESCRIPTION: Send a message to a destination
202 #   PARAMETERS: [header] Name of the header
203 #               [from]   sender ip
204 #               [to]     recipient ip
205 #               [data]   Hash containing additional attributes for the xml
206 #                        package
207 #      RETURNS:  nothing
208 #===============================================================================
209 sub build_msg ($$$$) {
210         my ($header, $from, $to, $data) = @_;
212     # data is of form, i.e.
213     # %data= ('ip' => $address, 'mac' => $mac);
215         my $out_hash = &create_xml_hash($header, $from, $to);
217         while ( my ($key, $value) = each(%$data) ) {
218                 if(ref($value) eq 'ARRAY'){
219                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
220                 } else {
221                         &add_content2xml_hash($out_hash, $key, $value);
222                 }
223         }
224     my $out_msg = &create_xml_string($out_hash);
225     return $out_msg;
229 sub db_res2xml {
230     my ($db_res) = @_ ;
231     my $xml = "";
233     my $len_db_res= keys %{$db_res};
234     for( my $i= 1; $i<= $len_db_res; $i++ ) {
235         $xml .= "\n<answer$i>";
236         my $hash= $db_res->{$i};
237         while ( my ($column_name, $column_value) = each %{$hash} ) {
238             $xml .= "<$column_name>";
239             my $xml_content;
240             if( $column_name eq "xmlmessage" ) {
241                 $xml_content = &encode_base64($column_value);
242             } else {
243                 $xml_content = defined $column_value ? $column_value : "";
244             }
245             $xml .= $xml_content;
246             $xml .= "</$column_name>"; 
247         }
248         $xml .= "</answer$i>";
250     }
252     return $xml;
256 sub db_res2si_msg {
257     my ($db_res, $header, $target, $source) = @_;
259     my $si_msg = "<xml>";
260     $si_msg .= "<header>$header</header>";
261     $si_msg .= "<source>$source</source>";
262     $si_msg .= "<target>$target</target>";
263     $si_msg .= &db_res2xml;
264     $si_msg .= "</xml>";
268 sub get_where_statement {
269     my ($msg, $msg_hash) = @_;
270     my $error= 0;
271     
272     my $clause_str= "";
273     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
274         $error++; 
275     }
277     if( $error == 0 ) {
278         my @clause_l;
279         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
280         foreach my $clause (@where) {
281             my $connector = $clause->{'connector'}[0];
282             if( not defined $connector ) { $connector = "AND"; }
283             $connector = uc($connector);
284             delete($clause->{'connector'});
286             my @phrase_l ;
287             foreach my $phrase (@{$clause->{'phrase'}}) {
288                 my $operator = "=";
289                 if( exists $phrase->{'operator'} ) {
290                     my $op = $op_hash->{$phrase->{'operator'}[0]};
291                     if( not defined $op ) {
292                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
293                                 "statement to sql valid syntax. Please use 'eq', ".
294                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
295                         &main::daemon_log($msg, 8);
296                         $op = "=";
297                     }
298                     $operator = $op;
299                     delete($phrase->{'operator'});
300                 }
302                 my @xml_tags = keys %{$phrase};
303                 my $tag = $xml_tags[0];
304                 my $val = $phrase->{$tag}[0];
305                 if( ref($val) eq "HASH" ) { next; }  # empty xml-tags should not appear in where statement
307                                 # integer columns do not have to have single quotes besides the value
308                                 if ($tag eq "id") {
309                                                 push(@phrase_l, "$tag$operator$val");
310                                 } else {
311                                                 push(@phrase_l, "$tag$operator'$val'");
312                                 }
313             }
315             if (not 0 == @phrase_l) {
316                 my $clause_str .= join(" $connector ", @phrase_l);
317                 push(@clause_l, "($clause_str)");
318             }
319         }
321         if( not 0 == @clause_l ) {
322             $clause_str = join(" AND ", @clause_l);
323             $clause_str = "WHERE ($clause_str) ";
324         }
325     }
327     return $clause_str;
330 sub get_select_statement {
331     my ($msg, $msg_hash)= @_;
332     my $select = "*";
333     if( exists $msg_hash->{'select'} ) {
334         my $select_l = \@{$msg_hash->{'select'}};
335         $select = join(', ', @{$select_l});
336     }
337     return $select;
341 sub get_update_statement {
342     my ($msg, $msg_hash) = @_;
343     my $error= 0;
344     my $update_str= "";
345     my @update_l; 
347     if( not exists $msg_hash->{'update'} ) { $error++; };
349     if( $error == 0 ) {
350         my $update= @{$msg_hash->{'update'}}[0];
351         while( my ($tag, $val) = each %{$update} ) {
352             my $val= @{$update->{$tag}}[0];
353             push(@update_l, "$tag='$val'");
354         }
355         if( 0 == @update_l ) { $error++; };   
356     }
358     if( $error == 0 ) { 
359         $update_str= join(', ', @update_l);
360         $update_str= "SET $update_str ";
361     }
363     return $update_str;
366 sub get_limit_statement {
367     my ($msg, $msg_hash)= @_; 
368     my $error= 0;
369     my $limit_str = "";
370     my ($from, $to);
372     if( not exists $msg_hash->{'limit'} ) { $error++; };
374     if( $error == 0 ) {
375         eval {
376             my $limit= @{$msg_hash->{'limit'}}[0];
377             $from= @{$limit->{'from'}}[0];
378             $to= @{$limit->{'to'}}[0];
379         };
380         if( $@ ) {
381             $error++;
382         }
383     }
385     if( $error == 0 ) {
386         $limit_str= "LIMIT $from, $to";
387     }   
388     
389     return $limit_str;
392 sub get_orderby_statement {
393     my ($msg, $msg_hash)= @_;
394     my $error= 0;
395     my $order_str= "";
396     my $order;
397     
398     if( not exists $msg_hash->{'orderby'} ) { $error++; };
400     if( $error == 0) {
401         eval {
402             $order= @{$msg_hash->{'orderby'}}[0];
403         };
404         if( $@ ) {
405             $error++;
406         }
407     }
409     if( $error == 0 ) {
410         $order_str= "ORDER BY $order";   
411     }
412     
413     return $order_str;
416 sub get_dns_domains() {
417         my $line;
418         my @searches;
419         open(RESOLV, "</etc/resolv.conf") or return @searches;
420         while(<RESOLV>){
421                 $line= $_;
422                 chomp $line;
423                 $line =~ s/^\s+//;
424                 $line =~ s/\s+$//;
425                 $line =~ s/\s+/ /;
426                 if ($line =~ /^domain (.*)$/ ){
427                         push(@searches, $1);
428                 } elsif ($line =~ /^search (.*)$/ ){
429                         push(@searches, split(/ /, $1));
430                 }
431         }
432         close(RESOLV);
434         my %tmp = map { $_ => 1 } @searches;
435         @searches = sort keys %tmp;
437         return @searches;
441 sub get_server_addresses {
442     my $domain= shift;
443     my @result;
444     my $error_string;
446     my $error = 0;
447     my $res   = Net::DNS::Resolver->new;
448     my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
449     my @hits;
451     if ($query) {
452         foreach my $rr ($query->answer) {
453             push(@hits, $rr->target.":".$rr->port);
454         }
455     }
456     else {
457         $error_string = "determination of '_gosa-si._tcp' server in domain '$domain' failed: ".$res->errorstring;
458         $error++;
459     }
461     if( $error == 0 ) {
462         foreach my $hit (@hits) {
463             my ($hit_name, $hit_port) = split(/:/, $hit);
464             chomp($hit_name);
465             chomp($hit_port);
467             my $address_query = $res->send($hit_name);
468             if( 1 == length($address_query->answer) ) {
469                 foreach my $rr ($address_query->answer) {
470                     push(@result, $rr->address.":".$hit_port);
471                 }
472             }
473         }
474     }
476     return \@result, $error_string;
480 sub get_logged_in_users {
481     my $result = qx(/usr/bin/w -hs);
482     my @res_lines;
484     if( defined $result ) { 
485         chomp($result);
486         @res_lines = split("\n", $result);
487     }
489     my @logged_in_user_list;
490     foreach my $line (@res_lines) {
491         chomp($line);
492         my @line_parts = split(/\s+/, $line); 
493         push(@logged_in_user_list, $line_parts[0]);
494     }
496     return @logged_in_user_list;
501 sub import_events {
502     my ($event_dir) = @_;
503     my $event_hash;
504     my $error = 0;
505     my @result = ();
506     if (not -e $event_dir) {
507         $error++;
508         push(@result, "cannot find directory or directory is not readable: $event_dir");   
509     }
511     my $DIR;
512     if ($error == 0) {
513         opendir ($DIR, $event_dir) or do { 
514             $error++;
515             push(@result, "cannot open directory '$event_dir' for reading: $!\n");
516         }
517     }
519     if ($error == 0) {
520         while (defined (my $event = readdir ($DIR))) {
521             if( $event eq "." || $event eq ".." ) { next; }  
523             # try to import event module
524             eval{ require $event; };
525             if( $@ ) {
526                 $error++;
527                 #push(@result, "import of event module '$event' failed: $@");
528                 #next;
529                 
530                 &main::daemon_log("ERROR: Import of event module '$event' failed: $@",1);
531                 exit(1);
532             }
534             # fetch all single events
535             $event =~ /(\S*?).pm$/;
536             my $event_module = $1;
537             my $events_l = eval( $1."::get_events()") ;
538             foreach my $event_name (@{$events_l}) {
539                 $event_hash->{$event_module}->{$event_name} = "";
540             }
541             my $events_string = join( ", ", @{$events_l});
542             push(@result, "import of event module '$event' succeed: $events_string");
543         }
544         
545         close $DIR;
546     }
548     return ($error, \@result, $event_hash);
553 #===  FUNCTION  ================================================================
554 #         NAME:  get_ip 
555 #   PARAMETERS:  interface name (i.e. eth0)
556 #      RETURNS:  (ip address) 
557 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
558 #===============================================================================
559 sub get_ip {
560         my $ifreq= shift;
561         my $result= "";
562         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
563         my $proto= getprotobyname('ip');
565         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
566                 or die "socket: $!";
568         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
569                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
570                 my ($port, $addr) = sockaddr_in $sin;
571                 my $ip            = inet_ntoa $addr;
573                 if ($ip && length($ip) > 0) {
574                         $result = $ip;
575                 }
576         }
578         return $result;
582 #===  FUNCTION  ================================================================
583 #         NAME:  get_interface_for_ip
584 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
585 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
586 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
587 #===============================================================================
588 sub get_interface_for_ip {
589         my $result;
590         my $ip= shift;
591         if ($ip && length($ip) > 0) {
592                 my @ifs= &get_interfaces();
593                 if($ip eq "0.0.0.0") {
594                         $result = "all";
595                 } else {
596                         foreach (@ifs) {
597                                 my $if=$_;
598                                 if(&get_ip($if) eq $ip) {
599                                         $result = $if;
600                                 }
601                         }       
602                 }
603         }       
604         return $result;
607 #===  FUNCTION  ================================================================
608 #         NAME:  get_interfaces 
609 #   PARAMETERS:  none
610 #      RETURNS:  (list of interfaces) 
611 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
612 #===============================================================================
613 sub get_interfaces {
614         my @result;
615         my $PROC_NET_DEV= ('/proc/net/dev');
617         open(PROC_NET_DEV, "<$PROC_NET_DEV")
618                 or die "Could not open $PROC_NET_DEV";
620         my @ifs = <PROC_NET_DEV>;
622         close(PROC_NET_DEV);
624         # Eat first two line
625         shift @ifs;
626         shift @ifs;
628         chomp @ifs;
629         foreach my $line(@ifs) {
630                 my $if= (split /:/, $line)[0];
631                 $if =~ s/^\s+//;
632                 push @result, $if;
633         }
635         return @result;
638 sub get_local_ip_for_remote_ip {
639         my $remote_ip= shift;
640         my $result="0.0.0.0";
642     if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
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     } else {
670                 daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1);
671         }
672         return $result;
676 sub get_mac_for_interface {
677         my $ifreq= shift;
678         my $result;
679         if ($ifreq && length($ifreq) > 0) { 
680                 if($ifreq eq "all") {
681                         $result = "00:00:00:00:00:00";
682                 } else {
683                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
685                         # A configured MAC Address should always override a guessed value
686                         if ($main::server_mac_address and length($main::server_mac_address) > 0) {
687                                 $result= $main::server_mac_address;
688                         }
690                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
691                                 or die "socket: $!";
693                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
694                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
696                                 if (length($mac) > 0) {
697                                         $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])$/;
698                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
699                                         $result = $mac;
700                                 }
701                         }
702                 }
703         }
704         return $result;
708 #===  FUNCTION  ================================================================
709 #         NAME:  is_local
710 #   PARAMETERS:  Server Address
711 #      RETURNS:  true if Server Address is on this host, false otherwise
712 #  DESCRIPTION:  Checks all interface addresses, stops on first match
713 #===============================================================================
714 sub is_local {
715     my $server_address = shift || "";
716     my $result = 0;
718     my $server_ip = $1 if $server_address =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,6}$/;
720     if(defined($server_ip) && length($server_ip) > 0) {
721         foreach my $interface(&get_interfaces()) {
722             my $ip_address= &get_ip($interface);
723             if($ip_address eq $server_ip) {
724                 $result = 1;
725                 last;
726             }
727         }
728     }
730     return $result;
734 #===  FUNCTION  ================================================================
735 #         NAME:  run_as
736 #   PARAMETERS:  uid, command
737 #      RETURNS:  hash with keys 'resultCode' = resultCode of command and 
738 #                'output' = program output
739 #  DESCRIPTION:  Runs command as uid using the sudo utility.
740 #===============================================================================
741 sub run_as {
742         my ($uid, $command) = @_;
743         my $sudo_cmd = `which sudo`;
744         chomp($sudo_cmd);
745         if(! -x $sudo_cmd) {
746                 &main::daemon_log("ERROR: The sudo utility is not available! Please fix this!");
747         }
748         my $cmd_line= "$sudo_cmd su - $uid -c '$command'";
749         open(PIPE, "$cmd_line |");
750         my $result = {'resultCode' => $?};
751         $result->{'command'} = $cmd_line;
752         push @{$result->{'output'}}, <PIPE>;
753         return $result;
757 #===  FUNCTION  ================================================================
758 #         NAME:  inform_other_si_server
759 #   PARAMETERS:  message
760 #      RETURNS:  nothing
761 #  DESCRIPTION:  Sends message to all other SI-server found in known_server_db. 
762 #===============================================================================
763 sub inform_all_other_si_server {
764     my ($msg) = @_;
766     # determine all other si-server from known_server_db
767     my $sql_statement= "SELECT * FROM $main::known_server_tn";
768     my $res = $main::known_server_db->select_dbentry( $sql_statement ); 
770     while( my ($hit_num, $hit) = each %$res ) {    
771         my $act_target_address = $hit->{hostname};
772         my $act_target_key = $hit->{hostkey};
774         # determine the source address corresponding to the actual target address
775         my ($act_target_ip, $act_target_port) = split(/:/, $act_target_address);
776         my $act_source_address = &main::get_local_ip_for_remote_ip($act_target_ip).":$act_target_port";
778         # fill into message the correct target and source addresses
779         my $act_msg = $msg;
780         $act_msg =~ s/<target>\w*<\/target>/<target>$act_target_address<\/target>/g;
781         $act_msg =~ s/<source>\w*<\/source>/<source>$act_source_address<\/source>/g;
783         # send message to the target
784         &main::send_msg_to_target($act_msg, $act_target_address, $act_target_key, "foreign_job_updates" , "J");
785     }
787     return;
791 sub read_configfile {
792     my ($cfg_file, %cfg_defaults) = @_ ;
793     my $cfg;
794     if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
795         if( -r $cfg_file ) {
796             $cfg = Config::IniFiles->new( -file => $cfg_file );
797         } else {
798             print STDERR "Couldn't read config file!";
799         }
800     } else {
801         $cfg = Config::IniFiles->new() ;
802     }
803     foreach my $section (keys %cfg_defaults) {
804         foreach my $param (keys %{$cfg_defaults{ $section }}) {
805             my $pinfo = $cfg_defaults{ $section }{ $param };
806            ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
807         }
808     }
812 sub check_opsi_res {
813     my $res= shift;
815     if($res) {
816         if ($res->is_error) {
817             my $error_string;
818             if (ref $res->error_message eq "HASH") { 
819                 $error_string = $res->error_message->{'message'}; 
820             } else { 
821                 $error_string = $res->error_message; 
822             }
823             return 1, $error_string;
824         }
825     } else {
826         return 1, $main::opsi_client->status_line;
827     }
828     return 0;
831 sub calc_timestamp {
832     my ($timestamp, $operation, $value) = @_ ;
833     my $res_timestamp = 0;
834     
835     $value = int($value);
836     $timestamp = int($timestamp);
837     $timestamp =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
838     my $dt = DateTime->new( year   => $1,
839             month  => $2,
840             day    => $3,
841             hour   => $4,
842             minute => $5,
843             second => $6,
844             );
846     if ($operation eq "plus" || $operation eq "+") {
847         $dt->add( seconds => $value);
848         $res_timestamp = $dt->ymd('').$dt->hms('');
849     }
851     if ($operation eq "minus" || $operation eq "-") {
852         $dt->subtract(seconds => $value);
853         $res_timestamp = $dt->ymd('').$dt->hms('');
854     }
856     return $res_timestamp;
860 1;