41e02ac61853196e222d68f8f73b97799e1bc314
1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 my @functions = (
6 "create_xml_hash",
7 "get_content_from_xml_hash",
8 "add_content2xml_hash",
9 "create_xml_string",
10 "transform_msg2hash",
11 "get_time",
12 "build_msg",
13 "db_res2xml",
14 "db_res2si_msg",
15 "get_where_statement",
16 "get_select_statement",
17 "get_update_statement",
18 "get_limit_statement",
19 "get_orderby_statement",
20 "get_dns_domains",
21 "get_server_addresses",
22 "get_logged_in_users",
23 "import_events",
24 "del_doubles",
25 );
26 @EXPORT = @functions;
27 use strict;
28 use warnings;
29 use IO::Socket::INET;
30 use Crypt::Rijndael;
31 use Digest::MD5 qw(md5 md5_hex md5_base64);
32 use MIME::Base64;
33 use XML::Simple;
34 use Data::Dumper;
35 use Net::DNS;
38 my $op_hash = {
39 'eq' => '=',
40 'ne' => '!=',
41 'ge' => '>=',
42 'gt' => '>',
43 'le' => '<=',
44 'lt' => '<',
45 'like' => ' LIKE ',
46 };
49 BEGIN {}
51 END {}
53 ### Start ######################################################################
55 my $xml = new XML::Simple();
57 sub daemon_log {
58 my ($msg, $level) = @_ ;
59 &main::daemon_log($msg, $level);
60 return;
61 }
63 sub del_doubles {
64 my %all;
65 $all{$_}=0 for @_;
66 return (keys %all);
67 }
70 #=== FUNCTION ================================================================
71 # NAME: create_xml_hash
72 # PARAMETERS: header - string - message header (required)
73 # source - string - where the message come from (required)
74 # target - string - where the message should go to (required)
75 # [header_value] - string - something usefull (optional)
76 # RETURNS: hash - hash - nomen est omen
77 # DESCRIPTION: creates a key-value hash, all values are stored in a array
78 #===============================================================================
79 sub create_xml_hash {
80 my ($header, $source, $target, $header_value) = @_;
81 my $hash = {
82 header => [$header],
83 source => [$source],
84 target => [$target],
85 $header => [$header_value],
86 };
87 return $hash
88 }
91 #=== FUNCTION ================================================================
92 # NAME: create_xml_string
93 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
94 # RETURNS: xml_string - string - xml string representation of the hash
95 # DESCRIPTION: transform the hash to a string using XML::Simple module
96 #===============================================================================
97 sub create_xml_string {
98 my ($xml_hash) = @_ ;
99 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
100 #$xml_string =~ s/[\n]+//g;
101 #daemon_log("create_xml_string:",7);
102 #daemon_log("$xml_string\n", 7);
103 return $xml_string;
104 }
107 sub transform_msg2hash {
108 my ($msg) = @_ ;
109 my $hash = $xml->XMLin($msg, ForceArray=>1);
111 # xml tags without a content are created as an empty hash
112 # substitute it with an empty list
113 eval {
114 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
115 if( 1 == @{ $xml_content } ) {
116 # there is only one element in xml_content list ...
117 my $element = @{ $xml_content }[0];
118 if( ref($element) eq "HASH" ) {
119 # and this element is an hash ...
120 my $len_element = keys %{ $element };
121 if( $len_element == 0 ) {
122 # and this hash is empty, then substitute the xml_content
123 # with an empty string in list
124 $hash->{$xml_tag} = [ "none" ];
125 }
126 }
127 }
128 }
129 };
130 if( $@ ) {
131 $hash = undef;
132 }
134 return $hash;
135 }
138 #=== FUNCTION ================================================================
139 # NAME: add_content2xml_hash
140 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
141 # element - string - key for the hash
142 # content - string - value for the hash
143 # RETURNS: nothing
144 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
145 # then append value to list
146 #===============================================================================
147 sub add_content2xml_hash {
148 my ($xml_ref, $element, $content) = @_;
149 if(not exists $$xml_ref{$element} ) {
150 $$xml_ref{$element} = [];
151 }
152 my $tmp = $$xml_ref{$element};
153 push(@$tmp, $content);
154 return;
155 }
158 sub get_time {
159 my ($seconds, $minutes, $hours, $monthday, $month,
160 $year, $weekday, $yearday, $sommertime) = localtime(time);
161 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
162 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
163 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
164 $month+=1;
165 $month = $month < 10 ? $month = "0".$month : $month;
166 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
167 $year+=1900;
168 return "$year$month$monthday$hours$minutes$seconds";
170 }
173 #=== FUNCTION ================================================================
174 # NAME: build_msg
175 # DESCRIPTION: Send a message to a destination
176 # PARAMETERS: [header] Name of the header
177 # [from] sender ip
178 # [to] recipient ip
179 # [data] Hash containing additional attributes for the xml
180 # package
181 # RETURNS: nothing
182 #===============================================================================
183 sub build_msg ($$$$) {
184 my ($header, $from, $to, $data) = @_;
186 # data is of form, i.e.
187 # %data= ('ip' => $address, 'mac' => $mac);
189 my $out_hash = &create_xml_hash($header, $from, $to);
191 while ( my ($key, $value) = each(%$data) ) {
192 if(ref($value) eq 'ARRAY'){
193 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
194 } else {
195 &add_content2xml_hash($out_hash, $key, $value);
196 }
197 }
198 my $out_msg = &create_xml_string($out_hash);
199 return $out_msg;
200 }
203 sub db_res2xml {
204 my ($db_res) = @_ ;
205 my $xml = "";
207 my $len_db_res= keys %{$db_res};
208 for( my $i= 1; $i<= $len_db_res; $i++ ) {
209 $xml .= "\n<answer$i>";
210 my $hash= $db_res->{$i};
211 while ( my ($column_name, $column_value) = each %{$hash} ) {
212 $xml .= "<$column_name>";
213 my $xml_content;
214 if( $column_name eq "xmlmessage" ) {
215 $xml_content = &encode_base64($column_value);
216 } else {
217 $xml_content = $column_value;
218 }
219 $xml .= $xml_content;
220 $xml .= "</$column_name>";
221 }
222 $xml .= "</answer$i>";
224 }
226 return $xml;
227 }
230 sub db_res2si_msg {
231 my ($db_res, $header, $target, $source) = @_;
233 my $si_msg = "<xml>";
234 $si_msg .= "<header>$header</header>";
235 $si_msg .= "<source>$source</source>";
236 $si_msg .= "<target>$target</target>";
237 $si_msg .= &db_res2xml;
238 $si_msg .= "</xml>";
239 }
242 sub get_where_statement {
243 my ($msg, $msg_hash) = @_;
244 my $error= 0;
246 my $clause_str= "";
247 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
248 $error++;
249 }
251 if( $error == 0 ) {
252 my @clause_l;
253 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
254 foreach my $clause (@where) {
255 my $connector = $clause->{'connector'}[0];
256 if( not defined $connector ) { $connector = "AND"; }
257 $connector = uc($connector);
258 delete($clause->{'connector'});
260 my @phrase_l ;
261 foreach my $phrase (@{$clause->{'phrase'}}) {
262 my $operator = "=";
263 if( exists $phrase->{'operator'} ) {
264 my $op = $op_hash->{$phrase->{'operator'}[0]};
265 if( not defined $op ) {
266 &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
267 "statement to sql valid syntax. Please use 'eq', ".
268 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
269 &main::daemon_log($msg, 8);
270 $op = "=";
271 }
272 $operator = $op;
273 delete($phrase->{'operator'});
274 }
276 my @xml_tags = keys %{$phrase};
277 my $tag = $xml_tags[0];
278 my $val = $phrase->{$tag}[0];
279 # integer columns do not have to have single quotes besides the value
280 if ($tag eq "id") {
281 push(@phrase_l, "$tag$operator$val");
282 } else {
283 push(@phrase_l, "$tag$operator'$val'");
284 }
285 }
286 my $clause_str .= join(" $connector ", @phrase_l);
287 push(@clause_l, "($clause_str)");
288 }
290 if( not 0 == @clause_l ) {
291 $clause_str = join(" AND ", @clause_l);
292 $clause_str = "WHERE ($clause_str) ";
293 }
294 }
296 return $clause_str;
297 }
299 sub get_select_statement {
300 my ($msg, $msg_hash)= @_;
301 my $select = "*";
302 if( exists $msg_hash->{'select'} ) {
303 my $select_l = \@{$msg_hash->{'select'}};
304 $select = join(', ', @{$select_l});
305 }
306 return $select;
307 }
310 sub get_update_statement {
311 my ($msg, $msg_hash) = @_;
312 my $error= 0;
313 my $update_str= "";
314 my @update_l;
316 if( not exists $msg_hash->{'update'} ) { $error++; };
318 if( $error == 0 ) {
319 my $update= @{$msg_hash->{'update'}}[0];
320 while( my ($tag, $val) = each %{$update} ) {
321 my $val= @{$update->{$tag}}[0];
322 push(@update_l, "$tag='$val'");
323 }
324 if( 0 == @update_l ) { $error++; };
325 }
327 if( $error == 0 ) {
328 $update_str= join(', ', @update_l);
329 $update_str= "SET $update_str ";
330 }
332 return $update_str;
333 }
335 sub get_limit_statement {
336 my ($msg, $msg_hash)= @_;
337 my $error= 0;
338 my $limit_str = "";
339 my ($from, $to);
341 if( not exists $msg_hash->{'limit'} ) { $error++; };
343 if( $error == 0 ) {
344 eval {
345 my $limit= @{$msg_hash->{'limit'}}[0];
346 $from= @{$limit->{'from'}}[0];
347 $to= @{$limit->{'to'}}[0];
348 };
349 if( $@ ) {
350 $error++;
351 }
352 }
354 if( $error == 0 ) {
355 $limit_str= "LIMIT $from, $to";
356 }
358 return $limit_str;
359 }
361 sub get_orderby_statement {
362 my ($msg, $msg_hash)= @_;
363 my $error= 0;
364 my $order_str= "";
365 my $order;
367 if( not exists $msg_hash->{'orderby'} ) { $error++; };
369 if( $error == 0) {
370 eval {
371 $order= @{$msg_hash->{'orderby'}}[0];
372 };
373 if( $@ ) {
374 $error++;
375 }
376 }
378 if( $error == 0 ) {
379 $order_str= "ORDER BY $order";
380 }
382 return $order_str;
383 }
385 sub get_dns_domains() {
386 my $line;
387 my @searches;
388 open(RESOLV, "</etc/resolv.conf") or return @searches;
389 while(<RESOLV>){
390 $line= $_;
391 chomp $line;
392 $line =~ s/^\s+//;
393 $line =~ s/\s+$//;
394 $line =~ s/\s+/ /;
395 if ($line =~ /^domain (.*)$/ ){
396 push(@searches, $1);
397 } elsif ($line =~ /^search (.*)$/ ){
398 push(@searches, split(/ /, $1));
399 }
400 }
401 close(RESOLV);
403 my %tmp = map { $_ => 1 } @searches;
404 @searches = sort keys %tmp;
406 return @searches;
407 }
410 #############################################
411 # moved from gosa-si-client: rettenbe, 16.05.2008
412 # outcommented at gosa-si-client
413 sub get_server_addresses {
414 my $domain= shift;
415 my @result;
417 my $error = 0;
418 my $res = Net::DNS::Resolver->new;
419 my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
420 my @hits;
422 if ($query) {
423 foreach my $rr ($query->answer) {
424 push(@hits, $rr->target.":".$rr->port);
425 }
426 }
427 else {
428 #warn "query failed: ", $res->errorstring, "\n";
429 $error++;
430 }
432 if( $error == 0 ) {
433 foreach my $hit (@hits) {
434 my ($hit_name, $hit_port) = split(/:/, $hit);
435 chomp($hit_name);
436 chomp($hit_port);
438 my $address_query = $res->send($hit_name);
439 if( 1 == length($address_query->answer) ) {
440 foreach my $rr ($address_query->answer) {
441 push(@result, $rr->address.":".$hit_port);
442 }
443 }
444 }
445 }
447 return @result;
448 }
451 sub get_logged_in_users {
452 my $result = qx(/usr/bin/w -hs);
453 my @res_lines;
455 if( defined $result ) {
456 chomp($result);
457 @res_lines = split("\n", $result);
458 }
460 my @logged_in_user_list;
461 foreach my $line (@res_lines) {
462 chomp($line);
463 my @line_parts = split(/\s+/, $line);
464 push(@logged_in_user_list, $line_parts[0]);
465 }
467 return @logged_in_user_list;
469 }
472 sub import_events {
473 my ($event_dir) = @_;
474 my $event_hash;
475 my $error = 0;
476 my @result = ();
477 if (not -e $event_dir) {
478 $error++;
479 push(@result, "cannot find directory or directory is not readable: $event_dir");
480 }
482 my $DIR;
483 if ($error == 0) {
484 opendir (DIR, $event_dir) or sub {
485 $error++;
486 push(@result, "cannot open directory '$event_dir' for reading: $!\n");
487 }
488 }
490 if ($error == 0) {
491 while (defined (my $event = readdir (DIR))) {
492 if( $event eq "." || $event eq ".." ) { next; }
494 # try to import event module
495 eval{ require $event; };
496 if( $@ ) {
497 $error++;
498 push(@result, "import of event module '$event' failed: $@");
499 next;
500 }
502 # fetch all single events
503 $event =~ /(\S*?).pm$/;
504 my $event_module = $1;
505 my $events_l = eval( $1."::get_events()") ;
506 foreach my $event_name (@{$events_l}) {
507 $event_hash->{$event_name} = $event_module;
508 }
509 my $events_string = join( ", ", @{$events_l});
510 push(@result, "import of event module '$event' succeed: $events_string");
511 }
512 }
514 return ($error, \@result, $event_hash);
516 }
519 1;