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