1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 @EXPORT = qw(create_xml_hash send_msg_hash2address get_content_from_xml_hash add_content2xml_hash create_xml_string encrypt_msg decrypt_msg create_ciphering transform_msg2hash get_time send_msg get_where_statement get_select_statement get_update_statement get_limit_statement get_orderby_statement);
7 use strict;
8 use warnings;
9 use IO::Socket::INET;
10 use Crypt::Rijndael;
11 use Digest::MD5 qw(md5 md5_hex md5_base64);
12 use MIME::Base64;
13 use XML::Simple;
15 my $op_hash = {
16 'eq' => '=',
17 'ne' => '!=',
18 'ge' => '>=',
19 'gt' => '>',
20 'le' => '<=',
21 'lt' => '<',
22 };
25 BEGIN {}
27 END {}
29 ### Start ######################################################################
31 my $xml = new XML::Simple();
33 sub daemon_log {
34 my ($msg, $level) = @_ ;
35 &main::daemon_log($msg, $level);
36 return;
37 }
40 #=== FUNCTION ================================================================
41 # NAME: create_xml_hash
42 # PARAMETERS: header - string - message header (required)
43 # source - string - where the message come from (required)
44 # target - string - where the message should go to (required)
45 # [header_value] - string - something usefull (optional)
46 # RETURNS: hash - hash - nomen est omen
47 # DESCRIPTION: creates a key-value hash, all values are stored in a array
48 #===============================================================================
49 sub create_xml_hash {
50 my ($header, $source, $target, $header_value) = @_;
51 my $hash = {
52 header => [$header],
53 source => [$source],
54 target => [$target],
55 $header => [$header_value],
56 };
57 return $hash
58 }
61 sub transform_msg2hash {
62 my ($msg) = @_ ;
63 my $hash = $xml->XMLin($msg, ForceArray=>1);
65 # xml tags without a content are created as an empty hash
66 # substitute it with an empty list
67 eval {
68 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
69 if( 1 == @{ $xml_content } ) {
70 # there is only one element in xml_content list ...
71 my $element = @{ $xml_content }[0];
72 if( ref($element) eq "HASH" ) {
73 # and this element is an hash ...
74 my $len_element = keys %{ $element };
75 if( $len_element == 0 ) {
76 # and this hash is empty, then substitute the xml_content
77 # with an empty string in list
78 $hash->{$xml_tag} = [ "none" ];
79 }
80 }
81 }
82 }
83 };
84 if( $@ ) {
85 $hash = undef;
86 }
88 return $hash;
89 }
92 #=== FUNCTION ================================================================
93 # NAME: send_msg_hash2address
94 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
95 # PeerAddr string - socket address to send msg
96 # PeerPort string - socket port, if not included in socket address
97 # RETURNS: nothing
98 # DESCRIPTION: ????
99 #===============================================================================
100 #sub send_msg_hash2address ($$$){
101 # my ($msg_hash, $address, $passwd) = @_ ;
102 #
103 # # fetch header for logging
104 # my $header = @{$msg_hash->{header}}[0];
105 #
106 # # generate xml string
107 # my $msg_xml = &create_xml_string($msg_hash);
108 #
109 # # create ciphering object
110 # my $act_cipher = &create_ciphering($passwd);
111 #
112 # # encrypt xml msg
113 # my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
114 #
115 # # opensocket
116 # my $socket = &open_socket($address);
117 # if(not defined $socket){
118 # daemon_log("cannot send '$header'-msg to $address , server not reachable", 5);
119 # return 1;
120 # }
121 #
122 # # send xml msg
123 # print $socket $crypted_msg."\n";
124 #
125 # close $socket;
126 #
127 # daemon_log("send '$header'-msg to $address", 1);
128 # daemon_log("message:\n$msg_xml", 8);
129 # return 0;
130 #}
133 #=== FUNCTION ================================================================
134 # NAME: get_content_from_xml_hash
135 # PARAMETERS: xml_ref - ref - reference of the xml hash
136 # element - string - key of the value you want
137 # RETURNS: value - string - if key is either header, target or source
138 # value - list - for all other keys in xml hash
139 # DESCRIPTION:
140 #===============================================================================
141 #sub get_content_from_xml_hash {
142 # my ($xml_ref, $element) = @_ ;
143 # #my $result = $main::xml_ref->{$element};
144 # #if( $element eq "header" || $element eq "target" || $element eq "source") {
145 # # return @$result[0];
146 # #}
147 # my @result = $xml_ref->{$element};
148 # return \@result;
149 #}
152 #=== FUNCTION ================================================================
153 # NAME: add_content2xml_hash
154 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
155 # element - string - key for the hash
156 # content - string - value for the hash
157 # RETURNS: nothing
158 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
159 # then append value to list
160 #===============================================================================
161 sub add_content2xml_hash {
162 my ($xml_ref, $element, $content) = @_;
163 if(not exists $$xml_ref{$element} ) {
164 $$xml_ref{$element} = [];
165 }
166 my $tmp = $$xml_ref{$element};
167 push(@$tmp, $content);
168 return;
169 }
172 #=== FUNCTION ================================================================
173 # NAME: create_xml_string
174 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
175 # RETURNS: xml_string - string - xml string representation of the hash
176 # DESCRIPTION: transform the hash to a string using XML::Simple module
177 #===============================================================================
178 sub create_xml_string {
179 my ($xml_hash) = @_ ;
180 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
181 #$xml_string =~ s/[\n]+//g;
182 #daemon_log("create_xml_string:",7);
183 #daemon_log("$xml_string\n", 7);
184 return $xml_string;
185 }
188 #=== FUNCTION ================================================================
189 # NAME: encrypt_msg
190 # PARAMETERS: msg - string - message to encrypt
191 # my_cipher - ref - reference to a Crypt::Rijndael object
192 # RETURNS: crypted_msg - string - crypted message
193 # DESCRIPTION: crypts the incoming message with the Crypt::Rijndael module
194 #===============================================================================
195 sub encrypt_msg {
196 # my ($msg, $my_cipher) = @_;
197 # if(not defined $my_cipher) { print "no cipher object\n"; }
198 # {
199 # use bytes;
200 # $msg = "\0"x(16-length($msg)%16).$msg;
201 # }
202 # $msg = $my_cipher->encrypt($msg);
203 # chomp($msg = &encode_base64($msg));
204 #
205 # # there are no newlines allowed inside msg
206 # $msg=~ s/\n//g;
207 #
208 # return $msg;
209 my ($msg, $key) = @_;
210 my $my_cipher = &create_ciphering($key);
211 {
212 use bytes;
213 $msg = "\0"x(16-length($msg)%16).$msg;
214 }
215 $msg = $my_cipher->encrypt($msg);
216 chomp($msg = &encode_base64($msg));
217 # there are no newlines allowed inside msg
218 $msg=~ s/\n//g;
219 return $msg;
221 }
224 #=== FUNCTION ================================================================
225 # NAME: decrypt_msg
226 # PARAMETERS: crypted_msg - string - message to decrypt
227 # my_cipher - ref - reference to a Crypt::Rijndael object
228 # RETURNS: msg - string - decrypted message
229 # DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
230 #===============================================================================
231 sub decrypt_msg {
232 # my ($msg, $my_cipher) = @_ ;
233 #
234 # if(defined $msg && defined $my_cipher) {
235 # $msg = &decode_base64($msg);
236 # }
237 # $msg = $my_cipher->decrypt($msg);
238 # $msg =~ s/\0*//g;
239 # return $msg;
240 my ($msg, $key) = @_ ;
241 $msg = &decode_base64($msg);
242 my $my_cipher = &create_ciphering($key);
243 $msg = $my_cipher->decrypt($msg);
244 $msg =~ s/\0*//g;
245 return $msg;
246 }
249 #=== FUNCTION ================================================================
250 # NAME: create_ciphering
251 # PARAMETERS: passwd - string - used to create ciphering
252 # RETURNS: cipher - object
253 # DESCRIPTION: creates a Crypt::Rijndael::MODE_CBC object with passwd as key
254 #===============================================================================
255 sub create_ciphering {
256 my ($passwd) = @_;
257 $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
258 my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
260 #daemon_log("iv: $iv", 7);
261 #daemon_log("key: $passwd", 7);
262 my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
263 $my_cipher->set_iv($iv);
264 return $my_cipher;
265 }
268 #=== FUNCTION ================================================================
269 # NAME: open_socket
270 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
271 # [PeerPort] string necessary if port not appended by PeerAddr
272 # RETURNS: socket IO::Socket::INET
273 # DESCRIPTION: open a socket to PeerAddr
274 #===============================================================================
275 #sub open_socket {
276 # my ($PeerAddr, $PeerPort) = @_ ;
277 # if(defined($PeerPort)){
278 # $PeerAddr = $PeerAddr.":".$PeerPort;
279 # }
280 # my $socket;
281 # $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
282 # Porto => "tcp",
283 # Type => SOCK_STREAM,
284 # Timeout => 5,
285 # );
286 # if(not defined $socket) {
287 # return;
288 # }
289 # &daemon_log("open_socket: $PeerAddr", 7);
290 # return $socket;
291 #}
294 sub get_time {
295 my ($seconds, $minutes, $hours, $monthday, $month,
296 $year, $weekday, $yearday, $sommertime) = localtime(time);
297 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
298 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
299 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
300 $month+=1;
301 $month = $month < 10 ? $month = "0".$month : $month;
302 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
303 $year+=1900;
304 return "$year$month$monthday$hours$minutes$seconds";
306 }
309 #=== FUNCTION ================================================================
310 # NAME: send_msg
311 # DESCRIPTION: Send a message to a destination
312 # PARAMETERS: [header] Name of the header
313 # [from] sender ip
314 # [to] recipient ip
315 # [data] Hash containing additional attributes for the xml
316 # package
317 # RETURNS: nothing
318 #===============================================================================
319 sub send_msg ($$$$) {
320 my ($header, $from, $to, $data) = @_;
322 my $out_hash = &create_xml_hash($header, $from, $to);
324 while ( my ($key, $value) = each(%$data) ) {
325 if(ref($value) eq 'ARRAY'){
326 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
327 } else {
328 &add_content2xml_hash($out_hash, $key, $value);
329 }
330 }
331 my $out_msg = &create_xml_string($out_hash);
332 return $out_msg;
333 }
336 sub get_where_statement {
337 my ($msg, $msg_hash) = @_;
338 my $error= 0;
340 my $clause_str= "";
341 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
342 $error++;
343 }
345 if( $error == 0 ) {
346 my @clause_l;
347 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
348 foreach my $clause (@where) {
349 my $connector = $clause->{'connector'}[0];
350 if( not defined $connector ) { $connector = "AND"; }
351 $connector = uc($connector);
352 delete($clause->{'connector'});
354 my @phrase_l ;
355 foreach my $phrase (@{$clause->{'phrase'}}) {
356 my $operator = "=";
357 if( exists $phrase->{'operator'} ) {
358 my $op = $op_hash->{$phrase->{'operator'}[0]};
359 if( not defined $op ) {
360 &main::daemon_log("Can not translate operator '$operator' in where ".
361 "statement to sql valid syntax. Please use 'eq', ".
362 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
363 &main::daemon_log($msg, 8);
364 $op = "=";
365 }
366 $operator = $op;
367 delete($phrase->{'operator'});
368 }
370 my @xml_tags = keys %{$phrase};
371 my $tag = $xml_tags[0];
372 my $val = $phrase->{$tag}[0];
373 push(@phrase_l, "$tag$operator'$val'");
374 }
375 my $clause_str .= join(" $connector ", @phrase_l);
376 push(@clause_l, $clause_str);
377 }
379 if( not 0 == @clause_l ) {
380 $clause_str = join(" AND ", @clause_l);
381 $clause_str = "WHERE $clause_str ";
382 }
383 }
385 return $clause_str;
386 }
388 sub get_select_statement {
389 my ($msg, $msg_hash)= @_;
390 my $select = "*";
391 if( exists $msg_hash->{'select'} ) {
392 my $select_l = \@{$msg_hash->{'select'}};
393 $select = join(' AND ', @{$select_l});
394 }
395 return $select;
396 }
399 sub get_update_statement {
400 my ($msg, $msg_hash) = @_;
401 my $error= 0;
402 my $update_str= "";
403 my @update_l;
405 if( not exists $msg_hash->{'update'} ) { $error++; };
407 if( $error == 0 ) {
408 my $update= @{$msg_hash->{'update'}}[0];
409 while( my ($tag, $val) = each %{$update} ) {
410 my $val= @{$update->{$tag}}[0];
411 push(@update_l, "$tag='$val'");
412 }
413 if( 0 == @update_l ) { $error++; };
414 }
416 if( $error == 0 ) {
417 $update_str= join(', ', @update_l);
418 $update_str= "SET $update_str ";
419 }
421 return $update_str;
422 }
424 sub get_limit_statement {
425 my ($msg, $msg_hash)= @_;
426 my $error= 0;
427 my $limit_str = "";
428 my ($from, $to);
430 if( not exists $msg_hash->{'limit'} ) { $error++; };
432 if( $error == 0 ) {
433 eval {
434 my $limit= @{$msg_hash->{'limit'}}[0];
435 $from= @{$limit->{'from'}}[0];
436 $to= @{$limit->{'to'}}[0];
437 };
438 if( $@ ) {
439 $error++;
440 }
441 }
443 if( $error == 0 ) {
444 $limit_str= "LIMIT $from, $to";
445 }
447 return $limit_str;
448 }
450 sub get_orderby_statement {
451 my ($msg, $msg_hash)= @_;
452 my $error= 0;
453 my $order_str= "";
454 my $order;
456 if( not exists $msg_hash->{'orderby'} ) { $error++; };
458 if( $error == 0) {
459 eval {
460 $order= @{$msg_hash->{'orderby'}}[0];
461 };
462 if( $@ ) {
463 $error++;
464 }
465 }
467 if( $error == 0 ) {
468 $order_str= "ORDER BY $order";
469 }
471 return $order_str;
472 }
474 1;