1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 my @functions = (
6 "create_passwd",
7 "create_xml_hash",
8 "get_content_from_xml_hash",
9 "add_content2xml_hash",
10 "create_xml_string",
11 "transform_msg2hash",
12 "get_time",
13 "get_utc_time",
14 "build_msg",
15 "db_res2xml",
16 "db_res2si_msg",
17 "get_where_statement",
18 "get_select_statement",
19 "get_update_statement",
20 "get_limit_statement",
21 "get_orderby_statement",
22 "get_dns_domains",
23 "get_server_addresses",
24 "get_logged_in_users",
25 "import_events",
26 "del_doubles",
27 "get_ip",
28 "get_interface_for_ip",
29 "get_interfaces",
30 "get_mac_for_interface",
31 "get_local_ip_for_remote_ip",
32 "is_local",
33 "run_as",
34 "inform_all_other_si_server",
35 "read_configfile",
36 "check_opsi_res",
37 "calc_timestamp",
38 "opsi_callobj2string",
39 );
40 @EXPORT = @functions;
41 use strict;
42 use warnings;
43 use IO::Socket::INET;
44 use Crypt::Rijndael;
45 use Digest::MD5 qw(md5 md5_hex md5_base64);
46 use MIME::Base64;
47 use XML::Simple;
48 use Data::Dumper;
49 use Net::DNS;
50 use DateTime;
53 my $op_hash = {
54 'eq' => '=',
55 'ne' => '!=',
56 'ge' => '>=',
57 'gt' => '>',
58 'le' => '<=',
59 'lt' => '<',
60 'like' => ' LIKE ',
61 };
64 BEGIN {}
66 END {}
68 ### Start ######################################################################
70 my $xml = new XML::Simple();
72 sub daemon_log {
73 my ($msg, $level) = @_ ;
74 &main::daemon_log($msg, $level);
75 return;
76 }
79 sub create_passwd {
80 my $new_passwd = "";
81 for(my $i=0; $i<31; $i++) {
82 $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
83 }
85 return $new_passwd;
86 }
89 sub del_doubles {
90 my %all;
91 $all{$_}=0 for @_;
92 return (keys %all);
93 }
96 #=== FUNCTION ================================================================
97 # NAME: create_xml_hash
98 # PARAMETERS: header - string - message header (required)
99 # source - string - where the message come from (required)
100 # target - string - where the message should go to (required)
101 # [header_value] - string - something usefull (optional)
102 # RETURNS: hash - hash - nomen est omen
103 # DESCRIPTION: creates a key-value hash, all values are stored in a array
104 #===============================================================================
105 sub create_xml_hash {
106 my ($header, $source, $target, $header_value) = @_;
107 my $hash = {
108 header => [$header],
109 source => [$source],
110 target => [$target],
111 $header => [$header_value],
112 };
113 return $hash
114 }
117 #=== FUNCTION ================================================================
118 # NAME: create_xml_string
119 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
120 # RETURNS: xml_string - string - xml string representation of the hash
121 # DESCRIPTION: transform the hash to a string using XML::Simple module
122 #===============================================================================
123 sub create_xml_string {
124 my ($xml_hash) = @_ ;
125 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
126 #$xml_string =~ s/[\n]+//g;
127 #daemon_log("create_xml_string:",7);
128 #daemon_log("$xml_string\n", 7);
129 return $xml_string;
130 }
133 sub transform_msg2hash {
134 my ($msg) = @_ ;
135 my $hash = $xml->XMLin($msg, ForceArray=>1);
137 # xml tags without a content are created as an empty hash
138 # substitute it with an empty list
139 eval {
140 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
141 if( 1 == @{ $xml_content } ) {
142 # there is only one element in xml_content list ...
143 my $element = @{ $xml_content }[0];
144 if( ref($element) eq "HASH" ) {
145 # and this element is an hash ...
146 my $len_element = keys %{ $element };
147 if( $len_element == 0 ) {
148 # and this hash is empty, then substitute the xml_content
149 # with an empty string in list
150 $hash->{$xml_tag} = [ "none" ];
151 }
152 }
153 }
154 }
155 };
156 if( $@ ) {
157 $hash = undef;
158 }
160 return $hash;
161 }
164 #=== FUNCTION ================================================================
165 # NAME: add_content2xml_hash
166 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
167 # element - string - key for the hash
168 # content - string - value for the hash
169 # RETURNS: nothing
170 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
171 # then append value to list
172 #===============================================================================
173 sub add_content2xml_hash {
174 my ($xml_ref, $element, $content) = @_;
175 if(not exists $$xml_ref{$element} ) {
176 $$xml_ref{$element} = [];
177 }
178 my $tmp = $$xml_ref{$element};
179 push(@$tmp, $content);
180 return;
181 }
184 sub get_time {
185 my ($seconds, $minutes, $hours, $monthday, $month,
186 $year, $weekday, $yearday, $sommertime) = localtime;
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";
195 }
198 sub get_utc_time {
199 my $utc_time = qx(date --utc +%Y%m%d%H%M%S);
200 $utc_time =~ s/\s$//;
201 return $utc_time;
202 }
205 #=== FUNCTION ================================================================
206 # NAME: build_msg
207 # DESCRIPTION: Send a message to a destination
208 # PARAMETERS: [header] Name of the header
209 # [from] sender ip
210 # [to] recipient ip
211 # [data] Hash containing additional attributes for the xml
212 # package
213 # RETURNS: nothing
214 #===============================================================================
215 sub build_msg ($$$$) {
216 my ($header, $from, $to, $data) = @_;
218 # data is of form, i.e.
219 # %data= ('ip' => $address, 'mac' => $mac);
221 my $out_hash = &create_xml_hash($header, $from, $to);
223 while ( my ($key, $value) = each(%$data) ) {
224 if(ref($value) eq 'ARRAY'){
225 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
226 } else {
227 &add_content2xml_hash($out_hash, $key, $value);
228 }
229 }
230 my $out_msg = &create_xml_string($out_hash);
231 return $out_msg;
232 }
235 sub db_res2xml {
236 my ($db_res) = @_ ;
237 my $xml = "";
239 my $len_db_res= keys %{$db_res};
240 for( my $i= 1; $i<= $len_db_res; $i++ ) {
241 $xml .= "\n<answer$i>";
242 my $hash= $db_res->{$i};
243 while ( my ($column_name, $column_value) = each %{$hash} ) {
244 $xml .= "<$column_name>";
245 my $xml_content;
246 if( $column_name eq "xmlmessage" ) {
247 $xml_content = &encode_base64($column_value);
248 } else {
249 $xml_content = defined $column_value ? $column_value : "";
250 }
251 $xml .= $xml_content;
252 $xml .= "</$column_name>";
253 }
254 $xml .= "</answer$i>";
256 }
258 return $xml;
259 }
262 sub db_res2si_msg {
263 my ($db_res, $header, $target, $source) = @_;
265 my $si_msg = "<xml>";
266 $si_msg .= "<header>$header</header>";
267 $si_msg .= "<source>$source</source>";
268 $si_msg .= "<target>$target</target>";
269 $si_msg .= &db_res2xml;
270 $si_msg .= "</xml>";
271 }
274 sub get_where_statement {
275 my ($msg, $msg_hash) = @_;
276 my $error= 0;
278 my $clause_str= "";
279 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
280 $error++;
281 }
283 if( $error == 0 ) {
284 my @clause_l;
285 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
286 foreach my $clause (@where) {
287 my $connector = $clause->{'connector'}[0];
288 if( not defined $connector ) { $connector = "AND"; }
289 $connector = uc($connector);
290 delete($clause->{'connector'});
292 my @phrase_l ;
293 foreach my $phrase (@{$clause->{'phrase'}}) {
294 my $operator = "=";
295 if( exists $phrase->{'operator'} ) {
296 my $op = $op_hash->{$phrase->{'operator'}[0]};
297 if( not defined $op ) {
298 &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
299 "statement to sql valid syntax. Please use 'eq', ".
300 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
301 &main::daemon_log($msg, 8);
302 $op = "=";
303 }
304 $operator = $op;
305 delete($phrase->{'operator'});
306 }
308 my @xml_tags = keys %{$phrase};
309 my $tag = $xml_tags[0];
310 my $val = $phrase->{$tag}[0];
311 if( ref($val) eq "HASH" ) { next; } # empty xml-tags should not appear in where statement
313 # integer columns do not have to have single quotes besides the value
314 if ($tag eq "id") {
315 push(@phrase_l, "$tag$operator$val");
316 } else {
317 push(@phrase_l, "$tag$operator'$val'");
318 }
319 }
321 if (not 0 == @phrase_l) {
322 my $clause_str .= join(" $connector ", @phrase_l);
323 push(@clause_l, "($clause_str)");
324 }
325 }
327 if( not 0 == @clause_l ) {
328 $clause_str = join(" AND ", @clause_l);
329 $clause_str = "WHERE ($clause_str) ";
330 }
331 }
333 return $clause_str;
334 }
336 sub get_select_statement {
337 my ($msg, $msg_hash)= @_;
338 my $select = "*";
339 if( exists $msg_hash->{'select'} ) {
340 my $select_l = \@{$msg_hash->{'select'}};
341 $select = join(', ', @{$select_l});
342 }
343 return $select;
344 }
347 sub get_update_statement {
348 my ($msg, $msg_hash) = @_;
349 my $error= 0;
350 my $update_str= "";
351 my @update_l;
353 if( not exists $msg_hash->{'update'} ) { $error++; };
355 if( $error == 0 ) {
356 my $update= @{$msg_hash->{'update'}}[0];
357 while( my ($tag, $val) = each %{$update} ) {
358 my $val= @{$update->{$tag}}[0];
359 push(@update_l, "$tag='$val'");
360 }
361 if( 0 == @update_l ) { $error++; };
362 }
364 if( $error == 0 ) {
365 $update_str= join(', ', @update_l);
366 $update_str= "SET $update_str ";
367 }
369 return $update_str;
370 }
372 sub get_limit_statement {
373 my ($msg, $msg_hash)= @_;
374 my $error= 0;
375 my $limit_str = "";
376 my ($from, $to);
378 if( not exists $msg_hash->{'limit'} ) { $error++; };
380 if( $error == 0 ) {
381 eval {
382 my $limit= @{$msg_hash->{'limit'}}[0];
383 $from= @{$limit->{'from'}}[0];
384 $to= @{$limit->{'to'}}[0];
385 };
386 if( $@ ) {
387 $error++;
388 }
389 }
391 if( $error == 0 ) {
392 $limit_str= "LIMIT $from, $to";
393 }
395 return $limit_str;
396 }
398 sub get_orderby_statement {
399 my ($msg, $msg_hash)= @_;
400 my $error= 0;
401 my $order_str= "";
402 my $order;
404 if( not exists $msg_hash->{'orderby'} ) { $error++; };
406 if( $error == 0) {
407 eval {
408 $order= @{$msg_hash->{'orderby'}}[0];
409 };
410 if( $@ ) {
411 $error++;
412 }
413 }
415 if( $error == 0 ) {
416 $order_str= "ORDER BY $order";
417 }
419 return $order_str;
420 }
422 sub get_dns_domains() {
423 my $line;
424 my @searches;
425 open(RESOLV, "</etc/resolv.conf") or return @searches;
426 while(<RESOLV>){
427 $line= $_;
428 chomp $line;
429 $line =~ s/^\s+//;
430 $line =~ s/\s+$//;
431 $line =~ s/\s+/ /;
432 if ($line =~ /^domain (.*)$/ ){
433 push(@searches, $1);
434 } elsif ($line =~ /^search (.*)$/ ){
435 push(@searches, split(/ /, $1));
436 }
437 }
438 close(RESOLV);
440 my %tmp = map { $_ => 1 } @searches;
441 @searches = sort keys %tmp;
443 return @searches;
444 }
447 sub get_server_addresses {
448 my $domain= shift;
449 my @result;
450 my $error_string;
452 my $error = 0;
453 my $res = Net::DNS::Resolver->new;
454 my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
455 my @hits;
457 if ($query) {
458 foreach my $rr ($query->answer) {
459 push(@hits, $rr->target.":".$rr->port);
460 }
461 }
462 else {
463 $error_string = "determination of '_gosa-si._tcp' server in domain '$domain' failed: ".$res->errorstring;
464 $error++;
465 }
467 if( $error == 0 ) {
468 foreach my $hit (@hits) {
469 my ($hit_name, $hit_port) = split(/:/, $hit);
470 chomp($hit_name);
471 chomp($hit_port);
473 my $address_query = $res->send($hit_name);
474 if( 1 == length($address_query->answer) ) {
475 foreach my $rr ($address_query->answer) {
476 push(@result, $rr->address.":".$hit_port);
477 }
478 }
479 }
480 }
482 return \@result, $error_string;
483 }
486 sub get_logged_in_users {
487 my $result = qx(/usr/bin/w -hs);
488 my @res_lines;
490 if( defined $result ) {
491 chomp($result);
492 @res_lines = split("\n", $result);
493 }
495 my @logged_in_user_list;
496 foreach my $line (@res_lines) {
497 chomp($line);
498 my @line_parts = split(/\s+/, $line);
499 push(@logged_in_user_list, $line_parts[0]);
500 }
502 return @logged_in_user_list;
504 }
507 sub import_events {
508 my ($event_dir) = @_;
509 my $event_hash;
510 my $error = 0;
511 my @result = ();
512 if (not -e $event_dir) {
513 $error++;
514 push(@result, "cannot find directory or directory is not readable: $event_dir");
515 }
517 my $DIR;
518 if ($error == 0) {
519 opendir ($DIR, $event_dir) or do {
520 $error++;
521 push(@result, "cannot open directory '$event_dir' for reading: $!\n");
522 }
523 }
525 if ($error == 0) {
526 while (defined (my $event = readdir ($DIR))) {
527 if( $event eq "." || $event eq ".." ) { next; }
529 # try to import event module
530 eval{ require $event; };
531 if( $@ ) {
532 $error++;
533 #push(@result, "import of event module '$event' failed: $@");
534 #next;
536 &main::daemon_log("ERROR: Import of event module '$event' failed: $@",1);
537 exit(1);
538 }
540 # fetch all single events
541 $event =~ /(\S*?).pm$/;
542 my $event_module = $1;
543 my $events_l = eval( $1."::get_events()") ;
544 foreach my $event_name (@{$events_l}) {
545 $event_hash->{$event_module}->{$event_name} = "";
546 }
547 my $events_string = join( ", ", @{$events_l});
548 push(@result, "import of event module '$event' succeed: $events_string");
549 }
551 close $DIR;
552 }
554 return ($error, \@result, $event_hash);
556 }
559 #=== FUNCTION ================================================================
560 # NAME: get_ip
561 # PARAMETERS: interface name (i.e. eth0)
562 # RETURNS: (ip address)
563 # DESCRIPTION: Uses ioctl to get ip address directly from system.
564 #===============================================================================
565 sub get_ip {
566 my $ifreq= shift;
567 my $result= "";
568 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
569 my $proto= getprotobyname('ip');
571 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
572 or die "socket: $!";
574 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
575 my ($if, $sin) = unpack 'a16 a16', $ifreq;
576 my ($port, $addr) = sockaddr_in $sin;
577 my $ip = inet_ntoa $addr;
579 if ($ip && length($ip) > 0) {
580 $result = $ip;
581 }
582 }
584 return $result;
585 }
588 #=== FUNCTION ================================================================
589 # NAME: get_interface_for_ip
590 # PARAMETERS: ip address (i.e. 192.168.0.1)
591 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
592 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
593 #===============================================================================
594 sub get_interface_for_ip {
595 my $result;
596 my $ip= shift;
597 if ($ip && length($ip) > 0) {
598 my @ifs= &get_interfaces();
599 if($ip eq "0.0.0.0") {
600 $result = "all";
601 } else {
602 foreach (@ifs) {
603 my $if=$_;
604 if(&get_ip($if) eq $ip) {
605 $result = $if;
606 }
607 }
608 }
609 }
610 return $result;
611 }
613 #=== FUNCTION ================================================================
614 # NAME: get_interfaces
615 # PARAMETERS: none
616 # RETURNS: (list of interfaces)
617 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
618 #===============================================================================
619 sub get_interfaces {
620 my @result;
621 my $PROC_NET_DEV= ('/proc/net/dev');
623 open(PROC_NET_DEV, "<$PROC_NET_DEV")
624 or die "Could not open $PROC_NET_DEV";
626 my @ifs = <PROC_NET_DEV>;
628 close(PROC_NET_DEV);
630 # Eat first two line
631 shift @ifs;
632 shift @ifs;
634 chomp @ifs;
635 foreach my $line(@ifs) {
636 my $if= (split /:/, $line)[0];
637 $if =~ s/^\s+//;
638 push @result, $if;
639 }
641 return @result;
642 }
644 sub get_local_ip_for_remote_ip {
645 my $remote_ip= shift;
646 my $result="0.0.0.0";
648 if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
649 my $PROC_NET_ROUTE= ('/proc/net/route');
651 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
652 or die "Could not open $PROC_NET_ROUTE";
654 my @ifs = <PROC_NET_ROUTE>;
656 close(PROC_NET_ROUTE);
658 # Eat header line
659 shift @ifs;
660 chomp @ifs;
661 foreach my $line(@ifs) {
662 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
663 my $destination;
664 my $mask;
665 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
666 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
667 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
668 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
669 if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) {
670 # destination matches route, save mac and exit
671 $result= &get_ip($Iface);
672 last;
673 }
674 }
675 } else {
676 daemon_log("0 WARNING: get_local_ip_for_remote_ip() was called with a non-ip parameter: '$remote_ip'", 1);
677 }
678 return $result;
679 }
682 sub get_mac_for_interface {
683 my $ifreq= shift;
684 my $result;
685 if ($ifreq && length($ifreq) > 0) {
686 if($ifreq eq "all") {
687 $result = "00:00:00:00:00:00";
688 } else {
689 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
691 # A configured MAC Address should always override a guessed value
692 if ($main::server_mac_address and length($main::server_mac_address) > 0) {
693 $result= $main::server_mac_address;
694 }
696 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
697 or die "socket: $!";
699 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
700 my ($if, $mac)= unpack 'h36 H12', $ifreq;
702 if (length($mac) > 0) {
703 $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])$/;
704 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
705 $result = $mac;
706 }
707 }
708 }
709 }
710 return $result;
711 }
714 #=== FUNCTION ================================================================
715 # NAME: is_local
716 # PARAMETERS: Server Address
717 # RETURNS: true if Server Address is on this host, false otherwise
718 # DESCRIPTION: Checks all interface addresses, stops on first match
719 #===============================================================================
720 sub is_local {
721 my $server_address = shift || "";
722 my $result = 0;
724 my $server_ip = $1 if $server_address =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,6}$/;
726 if(defined($server_ip) && length($server_ip) > 0) {
727 foreach my $interface(&get_interfaces()) {
728 my $ip_address= &get_ip($interface);
729 if($ip_address eq $server_ip) {
730 $result = 1;
731 last;
732 }
733 }
734 }
736 return $result;
737 }
740 #=== FUNCTION ================================================================
741 # NAME: run_as
742 # PARAMETERS: uid, command
743 # RETURNS: hash with keys 'resultCode' = resultCode of command and
744 # 'output' = program output
745 # DESCRIPTION: Runs command as uid using the sudo utility.
746 #===============================================================================
747 sub run_as {
748 my ($uid, $command) = @_;
749 my $sudo_cmd = `which sudo`;
750 chomp($sudo_cmd);
751 if(! -x $sudo_cmd) {
752 &main::daemon_log("ERROR: The sudo utility is not available! Please fix this!");
753 }
754 my $cmd_line= "$sudo_cmd su - $uid -c '$command'";
755 open(PIPE, "$cmd_line |");
756 my $result = {'resultCode' => $?};
757 $result->{'command'} = $cmd_line;
758 push @{$result->{'output'}}, <PIPE>;
759 return $result;
760 }
763 #=== FUNCTION ================================================================
764 # NAME: inform_other_si_server
765 # PARAMETERS: message
766 # RETURNS: nothing
767 # DESCRIPTION: Sends message to all other SI-server found in known_server_db.
768 #===============================================================================
769 sub inform_all_other_si_server {
770 my ($msg) = @_;
772 # determine all other si-server from known_server_db
773 my $sql_statement= "SELECT * FROM $main::known_server_tn";
774 my $res = $main::known_server_db->select_dbentry( $sql_statement );
776 while( my ($hit_num, $hit) = each %$res ) {
777 my $act_target_address = $hit->{hostname};
778 my $act_target_key = $hit->{hostkey};
780 # determine the source address corresponding to the actual target address
781 my ($act_target_ip, $act_target_port) = split(/:/, $act_target_address);
782 my $act_source_address = &main::get_local_ip_for_remote_ip($act_target_ip).":$act_target_port";
784 # fill into message the correct target and source addresses
785 my $act_msg = $msg;
786 $act_msg =~ s/<target>\w*<\/target>/<target>$act_target_address<\/target>/g;
787 $act_msg =~ s/<source>\w*<\/source>/<source>$act_source_address<\/source>/g;
789 # send message to the target
790 &main::send_msg_to_target($act_msg, $act_target_address, $act_target_key, "foreign_job_updates" , "J");
791 }
793 return;
794 }
797 sub read_configfile {
798 my ($cfg_file, %cfg_defaults) = @_ ;
799 my $cfg;
800 if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
801 if( -r $cfg_file ) {
802 $cfg = Config::IniFiles->new( -file => $cfg_file, -nocase => 1 );
803 } else {
804 print STDERR "Couldn't read config file!";
805 }
806 } else {
807 $cfg = Config::IniFiles->new() ;
808 }
809 foreach my $section (keys %cfg_defaults) {
810 foreach my $param (keys %{$cfg_defaults{ $section }}) {
811 my $pinfo = $cfg_defaults{ $section }{ $param };
812 ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
813 }
814 }
815 }
818 sub check_opsi_res {
819 my $res= shift;
821 if($res) {
822 if ($res->is_error) {
823 my $error_string;
824 if (ref $res->error_message eq "HASH") {
825 $error_string = $res->error_message->{'message'};
826 } else {
827 $error_string = $res->error_message;
828 }
829 return 1, $error_string;
830 }
831 } else {
832 return 1, $main::opsi_client->status_line;
833 }
834 return 0;
835 }
837 sub calc_timestamp {
838 my ($timestamp, $operation, $value) = @_ ;
839 my $res_timestamp = 0;
841 $value = int($value);
842 $timestamp = int($timestamp);
843 $timestamp =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
844 my $dt = DateTime->new( year => $1,
845 month => $2,
846 day => $3,
847 hour => $4,
848 minute => $5,
849 second => $6,
850 );
852 if ($operation eq "plus" || $operation eq "+") {
853 $dt->add( seconds => $value);
854 $res_timestamp = $dt->ymd('').$dt->hms('');
855 }
857 if ($operation eq "minus" || $operation eq "-") {
858 $dt->subtract(seconds => $value);
859 $res_timestamp = $dt->ymd('').$dt->hms('');
860 }
862 return $res_timestamp;
863 }
865 sub opsi_callobj2string {
866 my ($callobj) = @_;
867 my @callobj_string;
868 while(my ($key, $value) = each(%$callobj)) {
869 my $value_string = "";
870 if (ref($value) eq "ARRAY") {
871 $value_string = join(",", @$value);
872 } else {
873 $value_string = $value;
874 }
875 push(@callobj_string, "$key=$value_string")
876 }
877 return join(", ", @callobj_string);
878 }
880 1;