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