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