2044f54c1f34f1f2a17790740165c67ae9ce8ea0
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 );
22 @EXPORT = @functions;
23 use strict;
24 use warnings;
25 use IO::Socket::INET;
26 use Crypt::Rijndael;
27 use Digest::MD5 qw(md5 md5_hex md5_base64);
28 use MIME::Base64;
29 use XML::Simple;
30 use utf8;
32 my $op_hash = {
33 'eq' => '=',
34 'ne' => '!=',
35 'ge' => '>=',
36 'gt' => '>',
37 'le' => '<=',
38 'lt' => '<',
39 };
42 BEGIN {}
44 END {}
46 ### Start ######################################################################
48 my $xml = new XML::Simple();
50 sub daemon_log {
51 my ($msg, $level) = @_ ;
52 &main::daemon_log($msg, $level);
53 return;
54 }
59 #=== FUNCTION ================================================================
60 # NAME: create_xml_hash
61 # PARAMETERS: header - string - message header (required)
62 # source - string - where the message come from (required)
63 # target - string - where the message should go to (required)
64 # [header_value] - string - something usefull (optional)
65 # RETURNS: hash - hash - nomen est omen
66 # DESCRIPTION: creates a key-value hash, all values are stored in a array
67 #===============================================================================
68 sub create_xml_hash {
69 my ($header, $source, $target, $header_value) = @_;
70 my $hash = {
71 header => [$header],
72 source => [$source],
73 target => [$target],
74 $header => [$header_value],
75 };
76 return $hash
77 }
80 #=== FUNCTION ================================================================
81 # NAME: create_xml_string
82 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
83 # RETURNS: xml_string - string - xml string representation of the hash
84 # DESCRIPTION: transform the hash to a string using XML::Simple module
85 #===============================================================================
86 sub create_xml_string {
87 my ($xml_hash) = @_ ;
88 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
89 #$xml_string =~ s/[\n]+//g;
90 #daemon_log("create_xml_string:",7);
91 #daemon_log("$xml_string\n", 7);
92 return $xml_string;
93 }
96 sub transform_msg2hash {
97 my ($msg) = @_ ;
98 my $hash = $xml->XMLin($msg, ForceArray=>1);
100 # xml tags without a content are created as an empty hash
101 # substitute it with an empty list
102 eval {
103 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
104 if( 1 == @{ $xml_content } ) {
105 # there is only one element in xml_content list ...
106 my $element = @{ $xml_content }[0];
107 if( ref($element) eq "HASH" ) {
108 # and this element is an hash ...
109 my $len_element = keys %{ $element };
110 if( $len_element == 0 ) {
111 # and this hash is empty, then substitute the xml_content
112 # with an empty string in list
113 $hash->{$xml_tag} = [ "none" ];
114 }
115 }
116 }
117 }
118 };
119 if( $@ ) {
120 $hash = undef;
121 }
123 return $hash;
124 }
127 #=== FUNCTION ================================================================
128 # NAME: add_content2xml_hash
129 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
130 # element - string - key for the hash
131 # content - string - value for the hash
132 # RETURNS: nothing
133 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
134 # then append value to list
135 #===============================================================================
136 sub add_content2xml_hash {
137 my ($xml_ref, $element, $content) = @_;
138 if(not exists $$xml_ref{$element} ) {
139 $$xml_ref{$element} = [];
140 }
141 my $tmp = $$xml_ref{$element};
142 push(@$tmp, $content);
143 return;
144 }
147 #=== FUNCTION ================================================================
148 # NAME: encrypt_msg
149 # PARAMETERS: msg - string - message to encrypt
150 # my_cipher - ref - reference to a Crypt::Rijndael object
151 # RETURNS: crypted_msg - string - crypted message
152 # DESCRIPTION: crypts the incoming message with the Crypt::Rijndael module
153 #===============================================================================
154 #sub encrypt_msg {
155 # my ($msg, $key) = @_;
156 # my $my_cipher = &create_ciphering($key);
157 # {
158 # use bytes;
159 # $msg = "\0"x(16-length($msg)%16).$msg;
160 # }
161 # $msg = $my_cipher->encrypt($msg);
162 # chomp($msg = &encode_base64($msg));
163 # # there are no newlines allowed inside msg
164 # $msg=~ s/\n//g;
165 # return $msg;
166 #}
169 #=== FUNCTION ================================================================
170 # NAME: decrypt_msg
171 # PARAMETERS: crypted_msg - string - message to decrypt
172 # my_cipher - ref - reference to a Crypt::Rijndael object
173 # RETURNS: msg - string - decrypted message
174 # DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
175 #===============================================================================
176 #sub decrypt_msg {
177 # my ($msg, $my_cipher) = @_ ;
178 #
179 # if(defined $msg && defined $my_cipher) {
180 # $msg = &decode_base64($msg);
181 # }
182 # $msg = $my_cipher->decrypt($msg);
183 # $msg =~ s/\0*//g;
184 # return $msg;
185 # my ($msg, $key) = @_ ;
186 # $msg = &decode_base64($msg);
187 # my $my_cipher = &create_ciphering($key);
188 # $msg = $my_cipher->decrypt($msg);
189 # $msg =~ s/\0*//g;
190 # return $msg;
191 #}
194 #=== FUNCTION ================================================================
195 # NAME: create_ciphering
196 # PARAMETERS: passwd - string - used to create ciphering
197 # RETURNS: cipher - object
198 # DESCRIPTION: creates a Crypt::Rijndael::MODE_CBC object with passwd as key
199 #===============================================================================
200 #sub create_ciphering {
201 # my ($passwd) = @_;
202 # $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
203 # my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
204 #
205 # #daemon_log("iv: $iv", 7);
206 # #daemon_log("key: $passwd", 7);
207 # my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
208 # $my_cipher->set_iv($iv);
209 # return $my_cipher;
210 #}
213 #=== FUNCTION ================================================================
214 # NAME: open_socket
215 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
216 # [PeerPort] string necessary if port not appended by PeerAddr
217 # RETURNS: socket IO::Socket::INET
218 # DESCRIPTION: open a socket to PeerAddr
219 #===============================================================================
220 #sub open_socket {
221 # my ($PeerAddr, $PeerPort) = @_ ;
222 # if(defined($PeerPort)){
223 # $PeerAddr = $PeerAddr.":".$PeerPort;
224 # }
225 # my $socket;
226 # $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
227 # Porto => "tcp",
228 # Type => SOCK_STREAM,
229 # Timeout => 5,
230 # );
231 # if(not defined $socket) {
232 # return;
233 # }
234 # &daemon_log("open_socket: $PeerAddr", 7);
235 # return $socket;
236 #}
239 sub get_time {
240 my ($seconds, $minutes, $hours, $monthday, $month,
241 $year, $weekday, $yearday, $sommertime) = localtime(time);
242 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
243 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
244 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
245 $month+=1;
246 $month = $month < 10 ? $month = "0".$month : $month;
247 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
248 $year+=1900;
249 return "$year$month$monthday$hours$minutes$seconds";
251 }
254 #=== FUNCTION ================================================================
255 # NAME: build_msg
256 # DESCRIPTION: Send a message to a destination
257 # PARAMETERS: [header] Name of the header
258 # [from] sender ip
259 # [to] recipient ip
260 # [data] Hash containing additional attributes for the xml
261 # package
262 # RETURNS: nothing
263 #===============================================================================
264 sub build_msg ($$$$) {
265 my ($header, $from, $to, $data) = @_;
267 my $out_hash = &create_xml_hash($header, $from, $to);
269 while ( my ($key, $value) = each(%$data) ) {
270 if(ref($value) eq 'ARRAY'){
271 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
272 } else {
273 &add_content2xml_hash($out_hash, $key, $value);
274 }
275 }
276 my $out_msg = &create_xml_string($out_hash);
277 return $out_msg;
278 }
281 sub db_res2xml {
282 my ($db_res) = @_ ;
283 my $xml = "";
285 my $len_db_res= keys %{$db_res};
286 for( my $i= 1; $i<= $len_db_res; $i++ ) {
287 $xml .= "<answer$i>";
288 my $hash= $db_res->{$i};
289 while ( my ($column_name, $column_value) = each %{$hash} ) {
290 $xml .= "<$column_name>";
291 my $xml_content;
292 if( $column_name eq "xmlmessage" ) {
293 $xml_content = &encode_base64($column_value);
294 } else {
295 $xml_content = $column_value;
296 }
297 $xml .= $xml_content;
298 $xml .= "</$column_name>";
299 }
300 $xml .= "</answer$i>";
302 }
304 return $xml;
305 }
308 sub db_res2si_msg {
309 my ($db_res, $header, $target, $source) = @_;
311 my $si_msg = "<xml>";
312 $si_msg .= "<header>$header</header>";
313 $si_msg .= "<source>$source</source>";
314 $si_msg .= "<target>$target</target>";
315 $si_msg .= &db_res2xml;
316 $si_msg .= "</xml>";
317 }
320 sub get_where_statement {
321 my ($msg, $msg_hash) = @_;
322 my $error= 0;
324 my $clause_str= "";
325 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
326 $error++;
327 }
329 if( $error == 0 ) {
330 my @clause_l;
331 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
332 foreach my $clause (@where) {
333 my $connector = $clause->{'connector'}[0];
334 if( not defined $connector ) { $connector = "AND"; }
335 $connector = uc($connector);
336 delete($clause->{'connector'});
338 my @phrase_l ;
339 foreach my $phrase (@{$clause->{'phrase'}}) {
340 my $operator = "=";
341 if( exists $phrase->{'operator'} ) {
342 my $op = $op_hash->{$phrase->{'operator'}[0]};
343 if( not defined $op ) {
344 &main::daemon_log("Can not translate operator '$operator' in where ".
345 "statement to sql valid syntax. Please use 'eq', ".
346 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
347 &main::daemon_log($msg, 8);
348 $op = "=";
349 }
350 $operator = $op;
351 delete($phrase->{'operator'});
352 }
354 my @xml_tags = keys %{$phrase};
355 my $tag = $xml_tags[0];
356 my $val = $phrase->{$tag}[0];
357 push(@phrase_l, "$tag$operator'$val'");
358 }
359 my $clause_str .= join(" $connector ", @phrase_l);
360 push(@clause_l, $clause_str);
361 }
363 if( not 0 == @clause_l ) {
364 $clause_str = join(" AND ", @clause_l);
365 $clause_str = "WHERE $clause_str ";
366 }
367 }
369 return $clause_str;
370 }
372 sub get_select_statement {
373 my ($msg, $msg_hash)= @_;
374 my $select = "*";
375 if( exists $msg_hash->{'select'} ) {
376 my $select_l = \@{$msg_hash->{'select'}};
377 $select = join(' AND ', @{$select_l});
378 }
379 return $select;
380 }
383 sub get_update_statement {
384 my ($msg, $msg_hash) = @_;
385 my $error= 0;
386 my $update_str= "";
387 my @update_l;
389 if( not exists $msg_hash->{'update'} ) { $error++; };
391 if( $error == 0 ) {
392 my $update= @{$msg_hash->{'update'}}[0];
393 while( my ($tag, $val) = each %{$update} ) {
394 my $val= @{$update->{$tag}}[0];
395 push(@update_l, "$tag='$val'");
396 }
397 if( 0 == @update_l ) { $error++; };
398 }
400 if( $error == 0 ) {
401 $update_str= join(', ', @update_l);
402 $update_str= "SET $update_str ";
403 }
405 return $update_str;
406 }
408 sub get_limit_statement {
409 my ($msg, $msg_hash)= @_;
410 my $error= 0;
411 my $limit_str = "";
412 my ($from, $to);
414 if( not exists $msg_hash->{'limit'} ) { $error++; };
416 if( $error == 0 ) {
417 eval {
418 my $limit= @{$msg_hash->{'limit'}}[0];
419 $from= @{$limit->{'from'}}[0];
420 $to= @{$limit->{'to'}}[0];
421 };
422 if( $@ ) {
423 $error++;
424 }
425 }
427 if( $error == 0 ) {
428 $limit_str= "LIMIT $from, $to";
429 }
431 return $limit_str;
432 }
434 sub get_orderby_statement {
435 my ($msg, $msg_hash)= @_;
436 my $error= 0;
437 my $order_str= "";
438 my $order;
440 if( not exists $msg_hash->{'orderby'} ) { $error++; };
442 if( $error == 0) {
443 eval {
444 $order= @{$msg_hash->{'orderby'}}[0];
445 };
446 if( $@ ) {
447 $error++;
448 }
449 }
451 if( $error == 0 ) {
452 $order_str= "ORDER BY $order";
453 }
455 return $order_str;
456 }
458 sub get_dns_domains() {
459 my $line;
460 my @searches;
461 open(RESOLV, "</etc/resolv.conf") or return @searches;
462 while(<RESOLV>){
463 $line= $_;
464 chomp $line;
465 $line =~ s/^\s+//;
466 $line =~ s/\s+$//;
467 $line =~ s/\s+/ /;
468 if ($line =~ /^domain (.*)$/ ){
469 push(@searches, $1);
470 } elsif ($line =~ /^search (.*)$/ ){
471 push(@searches, split(/ /, $1));
472 }
473 }
474 close(RESOLV);
476 my %tmp = map { $_ => 1 } @searches;
477 @searches = sort keys %tmp;
479 return @searches;
480 }
482 1;