b3430e89d2046ed5160e24d0a3471cfc65b184a1
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 }
64 sub create_passwd {
65 my $new_passwd = "";
66 for(my $i=0; $i<31; $i++) {
67 $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
68 }
70 return $new_passwd;
71 }
74 sub del_doubles {
75 my %all;
76 $all{$_}=0 for @_;
77 return (keys %all);
78 }
81 #=== FUNCTION ================================================================
82 # NAME: create_xml_hash
83 # PARAMETERS: header - string - message header (required)
84 # source - string - where the message come from (required)
85 # target - string - where the message should go to (required)
86 # [header_value] - string - something usefull (optional)
87 # RETURNS: hash - hash - nomen est omen
88 # DESCRIPTION: creates a key-value hash, all values are stored in a array
89 #===============================================================================
90 sub create_xml_hash {
91 my ($header, $source, $target, $header_value) = @_;
92 my $hash = {
93 header => [$header],
94 source => [$source],
95 target => [$target],
96 $header => [$header_value],
97 };
98 return $hash
99 }
102 #=== FUNCTION ================================================================
103 # NAME: create_xml_string
104 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
105 # RETURNS: xml_string - string - xml string representation of the hash
106 # DESCRIPTION: transform the hash to a string using XML::Simple module
107 #===============================================================================
108 sub create_xml_string {
109 my ($xml_hash) = @_ ;
110 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
111 #$xml_string =~ s/[\n]+//g;
112 #daemon_log("create_xml_string:",7);
113 #daemon_log("$xml_string\n", 7);
114 return $xml_string;
115 }
118 sub transform_msg2hash {
119 my ($msg) = @_ ;
120 my $hash = $xml->XMLin($msg, ForceArray=>1);
122 # xml tags without a content are created as an empty hash
123 # substitute it with an empty list
124 eval {
125 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
126 if( 1 == @{ $xml_content } ) {
127 # there is only one element in xml_content list ...
128 my $element = @{ $xml_content }[0];
129 if( ref($element) eq "HASH" ) {
130 # and this element is an hash ...
131 my $len_element = keys %{ $element };
132 if( $len_element == 0 ) {
133 # and this hash is empty, then substitute the xml_content
134 # with an empty string in list
135 $hash->{$xml_tag} = [ "none" ];
136 }
137 }
138 }
139 }
140 };
141 if( $@ ) {
142 $hash = undef;
143 }
145 return $hash;
146 }
149 #=== FUNCTION ================================================================
150 # NAME: add_content2xml_hash
151 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
152 # element - string - key for the hash
153 # content - string - value for the hash
154 # RETURNS: nothing
155 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
156 # then append value to list
157 #===============================================================================
158 sub add_content2xml_hash {
159 my ($xml_ref, $element, $content) = @_;
160 if(not exists $$xml_ref{$element} ) {
161 $$xml_ref{$element} = [];
162 }
163 my $tmp = $$xml_ref{$element};
164 push(@$tmp, $content);
165 return;
166 }
169 sub get_time {
170 my ($seconds, $minutes, $hours, $monthday, $month,
171 $year, $weekday, $yearday, $sommertime) = localtime(time);
172 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
173 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
174 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
175 $month+=1;
176 $month = $month < 10 ? $month = "0".$month : $month;
177 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
178 $year+=1900;
179 return "$year$month$monthday$hours$minutes$seconds";
181 }
184 #=== FUNCTION ================================================================
185 # NAME: build_msg
186 # DESCRIPTION: Send a message to a destination
187 # PARAMETERS: [header] Name of the header
188 # [from] sender ip
189 # [to] recipient ip
190 # [data] Hash containing additional attributes for the xml
191 # package
192 # RETURNS: nothing
193 #===============================================================================
194 sub build_msg ($$$$) {
195 my ($header, $from, $to, $data) = @_;
197 # data is of form, i.e.
198 # %data= ('ip' => $address, 'mac' => $mac);
200 my $out_hash = &create_xml_hash($header, $from, $to);
202 while ( my ($key, $value) = each(%$data) ) {
203 if(ref($value) eq 'ARRAY'){
204 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
205 } else {
206 &add_content2xml_hash($out_hash, $key, $value);
207 }
208 }
209 my $out_msg = &create_xml_string($out_hash);
210 return $out_msg;
211 }
214 sub db_res2xml {
215 my ($db_res) = @_ ;
216 my $xml = "";
218 my $len_db_res= keys %{$db_res};
219 for( my $i= 1; $i<= $len_db_res; $i++ ) {
220 $xml .= "\n<answer$i>";
221 my $hash= $db_res->{$i};
222 while ( my ($column_name, $column_value) = each %{$hash} ) {
223 $xml .= "<$column_name>";
224 my $xml_content;
225 if( $column_name eq "xmlmessage" ) {
226 $xml_content = &encode_base64($column_value);
227 } else {
228 $xml_content = $column_value;
229 }
230 $xml .= $xml_content;
231 $xml .= "</$column_name>";
232 }
233 $xml .= "</answer$i>";
235 }
237 return $xml;
238 }
241 sub db_res2si_msg {
242 my ($db_res, $header, $target, $source) = @_;
244 my $si_msg = "<xml>";
245 $si_msg .= "<header>$header</header>";
246 $si_msg .= "<source>$source</source>";
247 $si_msg .= "<target>$target</target>";
248 $si_msg .= &db_res2xml;
249 $si_msg .= "</xml>";
250 }
253 sub get_where_statement {
254 my ($msg, $msg_hash) = @_;
255 my $error= 0;
257 my $clause_str= "";
258 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
259 $error++;
260 }
262 if( $error == 0 ) {
263 my @clause_l;
264 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
265 foreach my $clause (@where) {
266 my $connector = $clause->{'connector'}[0];
267 if( not defined $connector ) { $connector = "AND"; }
268 $connector = uc($connector);
269 delete($clause->{'connector'});
271 my @phrase_l ;
272 foreach my $phrase (@{$clause->{'phrase'}}) {
273 my $operator = "=";
274 if( exists $phrase->{'operator'} ) {
275 my $op = $op_hash->{$phrase->{'operator'}[0]};
276 if( not defined $op ) {
277 &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
278 "statement to sql valid syntax. Please use 'eq', ".
279 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
280 &main::daemon_log($msg, 8);
281 $op = "=";
282 }
283 $operator = $op;
284 delete($phrase->{'operator'});
285 }
287 my @xml_tags = keys %{$phrase};
288 my $tag = $xml_tags[0];
289 my $val = $phrase->{$tag}[0];
290 # integer columns do not have to have single quotes besides the value
291 if ($tag eq "id") {
292 push(@phrase_l, "$tag$operator$val");
293 } else {
294 push(@phrase_l, "$tag$operator'$val'");
295 }
296 }
297 my $clause_str .= join(" $connector ", @phrase_l);
298 push(@clause_l, "($clause_str)");
299 }
301 if( not 0 == @clause_l ) {
302 $clause_str = join(" AND ", @clause_l);
303 $clause_str = "WHERE ($clause_str) ";
304 }
305 }
307 return $clause_str;
308 }
310 sub get_select_statement {
311 my ($msg, $msg_hash)= @_;
312 my $select = "*";
313 if( exists $msg_hash->{'select'} ) {
314 my $select_l = \@{$msg_hash->{'select'}};
315 $select = join(', ', @{$select_l});
316 }
317 return $select;
318 }
321 sub get_update_statement {
322 my ($msg, $msg_hash) = @_;
323 my $error= 0;
324 my $update_str= "";
325 my @update_l;
327 if( not exists $msg_hash->{'update'} ) { $error++; };
329 if( $error == 0 ) {
330 my $update= @{$msg_hash->{'update'}}[0];
331 while( my ($tag, $val) = each %{$update} ) {
332 my $val= @{$update->{$tag}}[0];
333 push(@update_l, "$tag='$val'");
334 }
335 if( 0 == @update_l ) { $error++; };
336 }
338 if( $error == 0 ) {
339 $update_str= join(', ', @update_l);
340 $update_str= "SET $update_str ";
341 }
343 return $update_str;
344 }
346 sub get_limit_statement {
347 my ($msg, $msg_hash)= @_;
348 my $error= 0;
349 my $limit_str = "";
350 my ($from, $to);
352 if( not exists $msg_hash->{'limit'} ) { $error++; };
354 if( $error == 0 ) {
355 eval {
356 my $limit= @{$msg_hash->{'limit'}}[0];
357 $from= @{$limit->{'from'}}[0];
358 $to= @{$limit->{'to'}}[0];
359 };
360 if( $@ ) {
361 $error++;
362 }
363 }
365 if( $error == 0 ) {
366 $limit_str= "LIMIT $from, $to";
367 }
369 return $limit_str;
370 }
372 sub get_orderby_statement {
373 my ($msg, $msg_hash)= @_;
374 my $error= 0;
375 my $order_str= "";
376 my $order;
378 if( not exists $msg_hash->{'orderby'} ) { $error++; };
380 if( $error == 0) {
381 eval {
382 $order= @{$msg_hash->{'orderby'}}[0];
383 };
384 if( $@ ) {
385 $error++;
386 }
387 }
389 if( $error == 0 ) {
390 $order_str= "ORDER BY $order";
391 }
393 return $order_str;
394 }
396 sub get_dns_domains() {
397 my $line;
398 my @searches;
399 open(RESOLV, "</etc/resolv.conf") or return @searches;
400 while(<RESOLV>){
401 $line= $_;
402 chomp $line;
403 $line =~ s/^\s+//;
404 $line =~ s/\s+$//;
405 $line =~ s/\s+/ /;
406 if ($line =~ /^domain (.*)$/ ){
407 push(@searches, $1);
408 } elsif ($line =~ /^search (.*)$/ ){
409 push(@searches, split(/ /, $1));
410 }
411 }
412 close(RESOLV);
414 my %tmp = map { $_ => 1 } @searches;
415 @searches = sort keys %tmp;
417 return @searches;
418 }
421 #############################################
422 # moved from gosa-si-client: rettenbe, 16.05.2008
423 # outcommented at gosa-si-client
424 sub get_server_addresses {
425 my $domain= shift;
426 my @result;
428 my $error = 0;
429 my $res = Net::DNS::Resolver->new;
430 my $query = $res->send("_gosa-si._tcp.".$domain, "SRV");
431 my @hits;
433 if ($query) {
434 foreach my $rr ($query->answer) {
435 push(@hits, $rr->target.":".$rr->port);
436 }
437 }
438 else {
439 #warn "query failed: ", $res->errorstring, "\n";
440 $error++;
441 }
443 if( $error == 0 ) {
444 foreach my $hit (@hits) {
445 my ($hit_name, $hit_port) = split(/:/, $hit);
446 chomp($hit_name);
447 chomp($hit_port);
449 my $address_query = $res->send($hit_name);
450 if( 1 == length($address_query->answer) ) {
451 foreach my $rr ($address_query->answer) {
452 push(@result, $rr->address.":".$hit_port);
453 }
454 }
455 }
456 }
458 return @result;
459 }
462 sub get_logged_in_users {
463 my $result = qx(/usr/bin/w -hs);
464 my @res_lines;
466 if( defined $result ) {
467 chomp($result);
468 @res_lines = split("\n", $result);
469 }
471 my @logged_in_user_list;
472 foreach my $line (@res_lines) {
473 chomp($line);
474 my @line_parts = split(/\s+/, $line);
475 push(@logged_in_user_list, $line_parts[0]);
476 }
478 return @logged_in_user_list;
480 }
483 sub import_events {
484 my ($event_dir) = @_;
485 my $event_hash;
486 my $error = 0;
487 my @result = ();
488 if (not -e $event_dir) {
489 $error++;
490 push(@result, "cannot find directory or directory is not readable: $event_dir");
491 }
493 my $DIR;
494 if ($error == 0) {
495 opendir (DIR, $event_dir) or sub {
496 $error++;
497 push(@result, "cannot open directory '$event_dir' for reading: $!\n");
498 }
499 }
501 if ($error == 0) {
502 while (defined (my $event = readdir (DIR))) {
503 if( $event eq "." || $event eq ".." ) { next; }
505 # try to import event module
506 eval{ require $event; };
507 if( $@ ) {
508 $error++;
509 push(@result, "import of event module '$event' failed: $@");
510 next;
511 }
513 # fetch all single events
514 $event =~ /(\S*?).pm$/;
515 my $event_module = $1;
516 my $events_l = eval( $1."::get_events()") ;
517 foreach my $event_name (@{$events_l}) {
518 $event_hash->{$event_name} = $event_module;
519 }
520 my $events_string = join( ", ", @{$events_l});
521 push(@result, "import of event module '$event' succeed: $events_string");
522 }
523 }
525 return ($error, \@result, $event_hash);
527 }
530 1;