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