Code

update: outdated logged in user are deleted from login_users_db
[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     my ($seconds, $minutes, $hours, $monthday, $month,
184             $year, $weekday, $yearday, $sommertime) = localtime(time);
185     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
186     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
187     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
188     $month+=1;
189     $month = $month < 10 ? $month = "0".$month : $month;
190     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
191     $year+=1900;
192     return "$year$month$monthday$hours$minutes$seconds";
197 #===  FUNCTION  ================================================================
198 #         NAME: build_msg
199 #  DESCRIPTION: Send a message to a destination
200 #   PARAMETERS: [header] Name of the header
201 #               [from]   sender ip
202 #               [to]     recipient ip
203 #               [data]   Hash containing additional attributes for the xml
204 #                        package
205 #      RETURNS:  nothing
206 #===============================================================================
207 sub build_msg ($$$$) {
208         my ($header, $from, $to, $data) = @_;
210     # data is of form, i.e.
211     # %data= ('ip' => $address, 'mac' => $mac);
213         my $out_hash = &create_xml_hash($header, $from, $to);
215         while ( my ($key, $value) = each(%$data) ) {
216                 if(ref($value) eq 'ARRAY'){
217                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
218                 } else {
219                         &add_content2xml_hash($out_hash, $key, $value);
220                 }
221         }
222     my $out_msg = &create_xml_string($out_hash);
223     return $out_msg;
227 sub db_res2xml {
228     my ($db_res) = @_ ;
229     my $xml = "";
231     my $len_db_res= keys %{$db_res};
232     for( my $i= 1; $i<= $len_db_res; $i++ ) {
233         $xml .= "\n<answer$i>";
234         my $hash= $db_res->{$i};
235         while ( my ($column_name, $column_value) = each %{$hash} ) {
236             $xml .= "<$column_name>";
237             my $xml_content;
238             if( $column_name eq "xmlmessage" ) {
239                 $xml_content = &encode_base64($column_value);
240             } else {
241                 $xml_content = $column_value;
242             }
243             $xml .= $xml_content;
244             $xml .= "</$column_name>"; 
245         }
246         $xml .= "</answer$i>";
248     }
250     return $xml;
254 sub db_res2si_msg {
255     my ($db_res, $header, $target, $source) = @_;
257     my $si_msg = "<xml>";
258     $si_msg .= "<header>$header</header>";
259     $si_msg .= "<source>$source</source>";
260     $si_msg .= "<target>$target</target>";
261     $si_msg .= &db_res2xml;
262     $si_msg .= "</xml>";
266 sub get_where_statement {
267     my ($msg, $msg_hash) = @_;
268     my $error= 0;
269     
270     my $clause_str= "";
271     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
272         $error++; 
273     }
275     if( $error == 0 ) {
276         my @clause_l;
277         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
278         foreach my $clause (@where) {
279             my $connector = $clause->{'connector'}[0];
280             if( not defined $connector ) { $connector = "AND"; }
281             $connector = uc($connector);
282             delete($clause->{'connector'});
284             my @phrase_l ;
285             foreach my $phrase (@{$clause->{'phrase'}}) {
286                 my $operator = "=";
287                 if( exists $phrase->{'operator'} ) {
288                     my $op = $op_hash->{$phrase->{'operator'}[0]};
289                     if( not defined $op ) {
290                         &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
291                                 "statement to sql valid syntax. Please use 'eq', ".
292                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
293                         &main::daemon_log($msg, 8);
294                         $op = "=";
295                     }
296                     $operator = $op;
297                     delete($phrase->{'operator'});
298                 }
300                 my @xml_tags = keys %{$phrase};
301                 my $tag = $xml_tags[0];
302                 my $val = $phrase->{$tag}[0];
303                 if( ref($val) eq "HASH" ) { next; }  # empty xml-tags should not appear in where statement
305                                 # integer columns do not have to have single quotes besides the value
306                                 if ($tag eq "id") {
307                                                 push(@phrase_l, "$tag$operator$val");
308                                 } else {
309                                                 push(@phrase_l, "$tag$operator'$val'");
310                                 }
311             }
313             if (not 0 == @phrase_l) {
314                 my $clause_str .= join(" $connector ", @phrase_l);
315                 push(@clause_l, "($clause_str)");
316             }
317         }
319         if( not 0 == @clause_l ) {
320             $clause_str = join(" AND ", @clause_l);
321             $clause_str = "WHERE ($clause_str) ";
322         }
323     }
325     return $clause_str;
328 sub get_select_statement {
329     my ($msg, $msg_hash)= @_;
330     my $select = "*";
331     if( exists $msg_hash->{'select'} ) {
332         my $select_l = \@{$msg_hash->{'select'}};
333         $select = join(', ', @{$select_l});
334     }
335     return $select;
339 sub get_update_statement {
340     my ($msg, $msg_hash) = @_;
341     my $error= 0;
342     my $update_str= "";
343     my @update_l; 
345     if( not exists $msg_hash->{'update'} ) { $error++; };
347     if( $error == 0 ) {
348         my $update= @{$msg_hash->{'update'}}[0];
349         while( my ($tag, $val) = each %{$update} ) {
350             my $val= @{$update->{$tag}}[0];
351             push(@update_l, "$tag='$val'");
352         }
353         if( 0 == @update_l ) { $error++; };   
354     }
356     if( $error == 0 ) { 
357         $update_str= join(', ', @update_l);
358         $update_str= "SET $update_str ";
359     }
361     return $update_str;
364 sub get_limit_statement {
365     my ($msg, $msg_hash)= @_; 
366     my $error= 0;
367     my $limit_str = "";
368     my ($from, $to);
370     if( not exists $msg_hash->{'limit'} ) { $error++; };
372     if( $error == 0 ) {
373         eval {
374             my $limit= @{$msg_hash->{'limit'}}[0];
375             $from= @{$limit->{'from'}}[0];
376             $to= @{$limit->{'to'}}[0];
377         };
378         if( $@ ) {
379             $error++;
380         }
381     }
383     if( $error == 0 ) {
384         $limit_str= "LIMIT $from, $to";
385     }   
386     
387     return $limit_str;
390 sub get_orderby_statement {
391     my ($msg, $msg_hash)= @_;
392     my $error= 0;
393     my $order_str= "";
394     my $order;
395     
396     if( not exists $msg_hash->{'orderby'} ) { $error++; };
398     if( $error == 0) {
399         eval {
400             $order= @{$msg_hash->{'orderby'}}[0];
401         };
402         if( $@ ) {
403             $error++;
404         }
405     }
407     if( $error == 0 ) {
408         $order_str= "ORDER BY $order";   
409     }
410     
411     return $order_str;
414 sub get_dns_domains() {
415         my $line;
416         my @searches;
417         open(RESOLV, "</etc/resolv.conf") or return @searches;
418         while(<RESOLV>){
419                 $line= $_;
420                 chomp $line;
421                 $line =~ s/^\s+//;
422                 $line =~ s/\s+$//;
423                 $line =~ s/\s+/ /;
424                 if ($line =~ /^domain (.*)$/ ){
425                         push(@searches, $1);
426                 } elsif ($line =~ /^search (.*)$/ ){
427                         push(@searches, split(/ /, $1));
428                 }
429         }
430         close(RESOLV);
432         my %tmp = map { $_ => 1 } @searches;
433         @searches = sort keys %tmp;
435         return @searches;
439 sub get_server_addresses {
440     my $domain= shift;
441     my @result;
442     my $error_string;
444     my $error = 0;
445     my $res   = Net::DNS::Resolver->new;
446     my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
447     my @hits;
449     if ($query) {
450         foreach my $rr ($query->answer) {
451             push(@hits, $rr->target.":".$rr->port);
452         }
453     }
454     else {
455         $error_string = "determination of '_gosa-si._tcp' server in domain '$domain' failed: ".$res->errorstring;
456         $error++;
457     }
459     if( $error == 0 ) {
460         foreach my $hit (@hits) {
461             my ($hit_name, $hit_port) = split(/:/, $hit);
462             chomp($hit_name);
463             chomp($hit_port);
465             my $address_query = $res->send($hit_name);
466             if( 1 == length($address_query->answer) ) {
467                 foreach my $rr ($address_query->answer) {
468                     push(@result, $rr->address.":".$hit_port);
469                 }
470             }
471         }
472     }
474     return \@result, $error_string;
478 sub get_logged_in_users {
479     my $result = qx(/usr/bin/w -hs);
480     my @res_lines;
482     if( defined $result ) { 
483         chomp($result);
484         @res_lines = split("\n", $result);
485     }
487     my @logged_in_user_list;
488     foreach my $line (@res_lines) {
489         chomp($line);
490         my @line_parts = split(/\s+/, $line); 
491         push(@logged_in_user_list, $line_parts[0]);
492     }
494     return @logged_in_user_list;
499 sub import_events {
500     my ($event_dir) = @_;
501     my $event_hash;
502     my $error = 0;
503     my @result = ();
504     if (not -e $event_dir) {
505         $error++;
506         push(@result, "cannot find directory or directory is not readable: $event_dir");   
507     }
509     my $DIR;
510     if ($error == 0) {
511         opendir ($DIR, $event_dir) or do { 
512             $error++;
513             push(@result, "cannot open directory '$event_dir' for reading: $!\n");
514         }
515     }
517     if ($error == 0) {
518         while (defined (my $event = readdir ($DIR))) {
519             if( $event eq "." || $event eq ".." ) { next; }  
521             # try to import event module
522             eval{ require $event; };
523             if( $@ ) {
524                 $error++;
525                 #push(@result, "import of event module '$event' failed: $@");
526                 #next;
527                 
528                 &main::daemon_log("ERROR: Import of event module '$event' failed: $@",1);
529                 exit(1);
530             }
532             # fetch all single events
533             $event =~ /(\S*?).pm$/;
534             my $event_module = $1;
535             my $events_l = eval( $1."::get_events()") ;
536             foreach my $event_name (@{$events_l}) {
537                 $event_hash->{$event_module}->{$event_name} = "";
538             }
539             my $events_string = join( ", ", @{$events_l});
540             push(@result, "import of event module '$event' succeed: $events_string");
541         }
542         
543         close $DIR;
544     }
546     return ($error, \@result, $event_hash);
551 #===  FUNCTION  ================================================================
552 #         NAME:  get_ip 
553 #   PARAMETERS:  interface name (i.e. eth0)
554 #      RETURNS:  (ip address) 
555 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
556 #===============================================================================
557 sub get_ip {
558         my $ifreq= shift;
559         my $result= "";
560         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
561         my $proto= getprotobyname('ip');
563         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
564                 or die "socket: $!";
566         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
567                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
568                 my ($port, $addr) = sockaddr_in $sin;
569                 my $ip            = inet_ntoa $addr;
571                 if ($ip && length($ip) > 0) {
572                         $result = $ip;
573                 }
574         }
576         return $result;
580 #===  FUNCTION  ================================================================
581 #         NAME:  get_interface_for_ip
582 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
583 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
584 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
585 #===============================================================================
586 sub get_interface_for_ip {
587         my $result;
588         my $ip= shift;
589         if ($ip && length($ip) > 0) {
590                 my @ifs= &get_interfaces();
591                 if($ip eq "0.0.0.0") {
592                         $result = "all";
593                 } else {
594                         foreach (@ifs) {
595                                 my $if=$_;
596                                 if(&get_ip($if) eq $ip) {
597                                         $result = $if;
598                                 }
599                         }       
600                 }
601         }       
602         return $result;
605 #===  FUNCTION  ================================================================
606 #         NAME:  get_interfaces 
607 #   PARAMETERS:  none
608 #      RETURNS:  (list of interfaces) 
609 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
610 #===============================================================================
611 sub get_interfaces {
612         my @result;
613         my $PROC_NET_DEV= ('/proc/net/dev');
615         open(PROC_NET_DEV, "<$PROC_NET_DEV")
616                 or die "Could not open $PROC_NET_DEV";
618         my @ifs = <PROC_NET_DEV>;
620         close(PROC_NET_DEV);
622         # Eat first two line
623         shift @ifs;
624         shift @ifs;
626         chomp @ifs;
627         foreach my $line(@ifs) {
628                 my $if= (split /:/, $line)[0];
629                 $if =~ s/^\s+//;
630                 push @result, $if;
631         }
633         return @result;
636 sub get_local_ip_for_remote_ip {
637         my $remote_ip= shift;
638         my $result="0.0.0.0";
640     if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
641         my $PROC_NET_ROUTE= ('/proc/net/route');
643         open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
644             or die "Could not open $PROC_NET_ROUTE";
646         my @ifs = <PROC_NET_ROUTE>;
648         close(PROC_NET_ROUTE);
650         # Eat header line
651         shift @ifs;
652         chomp @ifs;
653         foreach my $line(@ifs) {
654             my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
655             my $destination;
656             my $mask;
657             my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
658             $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
659             ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
660             $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
661             if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) {
662                 # destination matches route, save mac and exit
663                 $result= &get_ip($Iface);
664                 last;
665             }
666         }
667     } else {
668                 daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1);
669         }
670         return $result;
674 sub get_mac_for_interface {
675         my $ifreq= shift;
676         my $result;
677         if ($ifreq && length($ifreq) > 0) { 
678                 if($ifreq eq "all") {
679                         $result = "00:00:00:00:00:00";
680                 } else {
681                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
683                         # A configured MAC Address should always override a guessed value
684                         if ($main::server_mac_address and length($main::server_mac_address) > 0) {
685                                 $result= $main::server_mac_address;
686                         }
688                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
689                                 or die "socket: $!";
691                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
692                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
694                                 if (length($mac) > 0) {
695                                         $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])$/;
696                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
697                                         $result = $mac;
698                                 }
699                         }
700                 }
701         }
702         return $result;
706 #===  FUNCTION  ================================================================
707 #         NAME:  is_local
708 #   PARAMETERS:  Server Address
709 #      RETURNS:  true if Server Address is on this host, false otherwise
710 #  DESCRIPTION:  Checks all interface addresses, stops on first match
711 #===============================================================================
712 sub is_local {
713     my $server_address = shift || "";
714     my $result = 0;
716     my $server_ip = $1 if $server_address =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,6}$/;
718     if(defined($server_ip) && length($server_ip) > 0) {
719         foreach my $interface(&get_interfaces()) {
720             my $ip_address= &get_ip($interface);
721             if($ip_address eq $server_ip) {
722                 $result = 1;
723                 last;
724             }
725         }
726     }
728     return $result;
732 #===  FUNCTION  ================================================================
733 #         NAME:  run_as
734 #   PARAMETERS:  uid, command
735 #      RETURNS:  hash with keys 'resultCode' = resultCode of command and 
736 #                'output' = program output
737 #  DESCRIPTION:  Runs command as uid using the sudo utility.
738 #===============================================================================
739 sub run_as {
740         my ($uid, $command) = @_;
741         my $sudo_cmd = `which sudo`;
742         chomp($sudo_cmd);
743         if(! -x $sudo_cmd) {
744                 &main::daemon_log("ERROR: The sudo utility is not available! Please fix this!");
745         }
746         my $cmd_line= "$sudo_cmd su - $uid -c '$command'";
747         open(PIPE, "$cmd_line |");
748         my $result = {'resultCode' => $?};
749         $result->{'command'} = $cmd_line;
750         push @{$result->{'output'}}, <PIPE>;
751         return $result;
755 #===  FUNCTION  ================================================================
756 #         NAME:  inform_other_si_server
757 #   PARAMETERS:  message
758 #      RETURNS:  nothing
759 #  DESCRIPTION:  Sends message to all other SI-server found in known_server_db. 
760 #===============================================================================
761 sub inform_all_other_si_server {
762     my ($msg) = @_;
764     # determine all other si-server from known_server_db
765     my $sql_statement= "SELECT * FROM $main::known_server_tn";
766     my $res = $main::known_server_db->select_dbentry( $sql_statement ); 
768     while( my ($hit_num, $hit) = each %$res ) {    
769         my $act_target_address = $hit->{hostname};
770         my $act_target_key = $hit->{hostkey};
772         # determine the source address corresponding to the actual target address
773         my ($act_target_ip, $act_target_port) = split(/:/, $act_target_address);
774         my $act_source_address = &main::get_local_ip_for_remote_ip($act_target_ip).":$act_target_port";
776         # fill into message the correct target and source addresses
777         my $act_msg = $msg;
778         $act_msg =~ s/<target>\w*<\/target>/<target>$act_target_address<\/target>/g;
779         $act_msg =~ s/<source>\w*<\/source>/<source>$act_source_address<\/source>/g;
781         # send message to the target
782         &main::send_msg_to_target($act_msg, $act_target_address, $act_target_key, "foreign_job_updates" , "J");
783     }
785     return;
789 sub read_configfile {
790     my ($cfg_file, %cfg_defaults) = @_ ;
791     my $cfg;
792     if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
793         if( -r $cfg_file ) {
794             $cfg = Config::IniFiles->new( -file => $cfg_file );
795         } else {
796             print STDERR "Couldn't read config file!";
797         }
798     } else {
799         $cfg = Config::IniFiles->new() ;
800     }
801     foreach my $section (keys %cfg_defaults) {
802         foreach my $param (keys %{$cfg_defaults{ $section }}) {
803             my $pinfo = $cfg_defaults{ $section }{ $param };
804            ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
805         }
806     }
810 sub check_opsi_res {
811     my $res= shift;
813     if($res) {
814         if ($res->is_error) {
815             my $error_string;
816             if (ref $res->error_message eq "HASH") { 
817                 $error_string = $res->error_message->{'message'}; 
818             } else { 
819                 $error_string = $res->error_message; 
820             }
821             return 1, $error_string;
822         }
823     } else {
824         return 1, $main::opsi_client->status_line;
825     }
826     return 0;
829 sub calc_timestamp {
830     my ($timestamp, $operation, $value) = @_ ;
831     my $res_timestamp = 0;
832     
833     $value = int($value);
834     $timestamp = int($timestamp);
835     $timestamp =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
836     my $dt = DateTime->new( year   => $1,
837             month  => $2,
838             day    => $3,
839             hour   => $4,
840             minute => $5,
841             second => $6,
842             );
844     if ($operation eq "plus" || $operation eq "+") {
845         $dt->add( seconds => $value);
846         $res_timestamp = $dt->ymd('').$dt->hms('');
847     }
849     if ($operation eq "minus" || $operation eq "-") {
850         $dt->subtract(seconds => $value);
851         $res_timestamp = $dt->ymd('').$dt->hms('');
852     }
854     return $res_timestamp;
858 1;