571d5008b3ff2999a5b0987a73cd7d683825d697
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, $hostkey) = @_;
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 }
317 &send_msg_hash2address($out_hash, $to, $hostkey);
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'}) ) { $error++; };
327 if( $error == 0 ) {
328 my @clause_l;
329 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
330 foreach my $clause (@where) {
331 my $connector = $clause->{'connector'}[0];
332 if( not defined $connector ) { $connector = "AND"; }
333 $connector = uc($connector);
334 delete($clause->{'connector'});
336 my @phrase_l ;
337 foreach my $phrase (@{$clause->{'phrase'}}) {
338 my $operator = "=";
339 if( exists $phrase->{'operator'} ) {
340 my $op = $op_hash->{$phrase->{'operator'}[0]};
341 if( not defined $op ) {
342 &main::daemon_log("Can not translate operator '$operator' in where ".
343 "statement to sql valid syntax. Please use 'eq', ".
344 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
345 &main::daemon_log($msg, 8);
346 $op = "=";
347 }
348 $operator = $op;
349 delete($phrase->{'operator'});
350 }
352 my @xml_tags = keys %{$phrase};
353 my $tag = $xml_tags[0];
354 my $val = $phrase->{$tag}[0];
355 push(@phrase_l, "$tag$operator'$val'");
356 }
357 my $clause_str .= join(" $connector ", @phrase_l);
358 push(@clause_l, $clause_str);
359 }
361 if( not 0 == @clause_l ) {
362 $clause_str = join(" AND ", @clause_l);
363 $clause_str = "WHERE $clause_str ";
364 }
365 }
367 return $clause_str;
368 }
370 sub get_select_statement {
371 my ($msg, $msg_hash)= @_;
372 my $select = "*";
373 if( exists $msg_hash->{'select'} ) {
374 my $select_l = \@{$msg_hash->{'select'}};
375 $select = join(' AND ', @{$select_l});
376 }
377 return $select;
378 }
381 sub get_update_statement {
382 my ($msg, $msg_hash) = @_;
383 my $error= 0;
384 my $update_str= "";
385 my @update_l;
387 if( not exists $msg_hash->{'update'} ) { $error++; };
389 if( $error == 0 ) {
390 my $update= @{$msg_hash->{'update'}}[0];
391 while( my ($tag, $val) = each %{$update} ) {
392 my $val= @{$update->{$tag}}[0];
393 push(@update_l, "$tag='$val'");
394 }
395 if( 0 == @update_l ) { $error++; };
396 }
398 if( $error == 0 ) {
399 $update_str= join(', ', @update_l);
400 $update_str= "SET $update_str ";
401 }
403 return $update_str;
404 }
406 sub get_limit_statement {
407 my ($msg, $msg_hash)= @_;
408 my $error= 0;
409 my $limit_str = "";
410 my ($from, $to);
412 if( not exists $msg_hash->{'limit'} ) { $error++; };
414 if( $error == 0 ) {
415 eval {
416 my $limit= @{$msg_hash->{'limit'}}[0];
417 $from= @{$limit->{'from'}}[0];
418 $to= @{$limit->{'to'}}[0];
419 };
420 if( $@ ) {
421 $error++;
422 }
423 }
425 if( $error == 0 ) {
426 $limit_str= "LIMIT $from, $to";
427 }
429 return $limit_str;
430 }
432 sub get_orderby_statement {
433 my ($msg, $msg_hash)= @_;
434 my $error= 0;
435 my $order_str= "";
436 my $order;
438 if( not exists $msg_hash->{'orderby'} ) { $error++; };
440 if( $error == 0) {
441 eval {
442 $order= @{$msg_hash->{'orderby'}}[0];
443 };
444 if( $@ ) {
445 $error++;
446 }
447 }
449 if( $error == 0 ) {
450 $order_str= "ORDER BY $order";
451 }
453 return $order_str;
454 }
456 1;