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