Code

Added activation signals
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
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);
68     
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);
112     
113     # create ciphering object
114     my $act_cipher = &create_ciphering($passwd);
115     
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     }
125     
126     # send xml msg
127     print $socket $crypted_msg."\n";
128     
129     close $socket;
131     daemon_log("send '$header'-msg to $address", 1);
132     daemon_log("message:\n$msg_xml", 8);
133     return 0;
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;
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;
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;
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     $msg = "\0"x(16-length($msg)%16).$msg;
203     $msg = $my_cipher->encrypt($msg);
204     chomp($msg = &encode_base64($msg));
206     # there are no newlines allowed inside msg
207     $msg=~ s/\n//g;
209     return $msg;
213 #===  FUNCTION  ================================================================
214 #         NAME:  decrypt_msg
215 #   PARAMETERS:  crypted_msg - string - message to decrypt
216 #                my_cipher - ref - reference to a Crypt::Rijndael object
217 #      RETURNS:  msg - string - decrypted message
218 #  DESCRIPTION:  decrypts the incoming message with the Crypt::Rijndael module
219 #===============================================================================
220 sub decrypt_msg {
221     my ($msg, $my_cipher) = @_ ;
222     if(defined $msg && defined $my_cipher) {
223         $msg = &decode_base64($msg);
224     }
225     $msg = $my_cipher->decrypt($msg); 
226     $msg =~ s/\0*//g;
227     return $msg;
231 #===  FUNCTION  ================================================================
232 #         NAME:  create_ciphering
233 #   PARAMETERS:  passwd - string - used to create ciphering
234 #      RETURNS:  cipher - object
235 #  DESCRIPTION:  creates a Crypt::Rijndael::MODE_CBC object with passwd as key
236 #===============================================================================
237 sub create_ciphering {
238     my ($passwd) = @_;
239     $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
240     my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
242     #daemon_log("iv: $iv", 7);
243     #daemon_log("key: $passwd", 7);
244     my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
245     $my_cipher->set_iv($iv);
246     return $my_cipher;
250 #===  FUNCTION  ================================================================
251 #         NAME:  open_socket
252 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
253 #                [PeerPort] string necessary if port not appended by PeerAddr
254 #      RETURNS:  socket IO::Socket::INET
255 #  DESCRIPTION:  open a socket to PeerAddr
256 #===============================================================================
257 sub open_socket {
258     my ($PeerAddr, $PeerPort) = @_ ;
259     if(defined($PeerPort)){
260         $PeerAddr = $PeerAddr.":".$PeerPort;
261     }
262     my $socket;
263     $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
264             Porto => "tcp",
265             Type => SOCK_STREAM,
266             Timeout => 5,
267             );
268     if(not defined $socket) {
269         return;
270     }
271     &daemon_log("open_socket: $PeerAddr", 7);
272     return $socket;
276 sub get_time {
277     my ($seconds, $minutes, $hours, $monthday, $month,
278             $year, $weekday, $yearday, $sommertime) = localtime(time);
279     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
280     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
281     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
282     $month+=1;
283     $month = $month < 10 ? $month = "0".$month : $month;
284     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
285     $year+=1900;
286     return "$year$month$monthday$hours$minutes$seconds";
291 #===  FUNCTION  ================================================================
292 #         NAME: send_msg
293 #  DESCRIPTION: Send a message to a destination
294 #   PARAMETERS: [header] Name of the header
295 #               [from]   sender ip
296 #               [to]     recipient ip
297 #               [data]   Hash containing additional attributes for the xml
298 #                        package
299 #      RETURNS:  nothing
300 #===============================================================================
301 sub send_msg ($$$$$) {
302         my ($header, $from, $to, $data, $hostkey) = @_;
304         my $out_hash = &create_xml_hash($header, $from, $to);
306         while ( my ($key, $value) = each(%$data) ) {
307                 if(ref($value) eq 'ARRAY'){
308                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
309                 } else {
310                         &add_content2xml_hash($out_hash, $key, $value);
311                 }
312         }
314         &send_msg_hash2address($out_hash, $to, $hostkey);
318 sub get_where_statement {
319     my ($msg, $msg_hash)= @_;
320     my $error= 0;
321     
322     my $clause_str= "";
323     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { $error++; };
324     if( $error == 0 ) {
325         my @clause_l;
326         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
327         foreach my $clause (@where) {
328             my $connector = $clause->{'connector'}[0];
329             if( not defined $connector ) { $connector = "AND"; }
330             $connector = uc($connector);
331             delete($clause->{'connector'});
333             my @phrase_l ;
334             foreach my $phrase (@{$clause->{'phrase'}}) {
335                 my $operator = "=";
336                 if( exists $phrase->{'operator'} ) {
337                     my $op = $op_hash->{$phrase->{'operator'}[0]};
338                     if( not defined $op ) {
339                         &main::daemon_log("Can not translate operator '$operator' in where ".
340                                 "statement to sql valid syntax. Please use 'eq', ".
341                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
342                         &main::daemon_log($msg, 8);
343                         $op = "=";
344                     }
345                     $operator = $op;
346                     delete($phrase->{'operator'});
347                 }
349                 my @xml_tags = keys %{$phrase};
350                 my $tag = $xml_tags[0];
351                 my $val = $phrase->{$tag}[0];
352                 push(@phrase_l, "$tag$operator'$val'");
353             }
354             my $clause_str .= join(" $connector ", @phrase_l);
355             push(@clause_l, $clause_str);
356         }
358         if( not 0 == @clause_l ) {
359             $clause_str = join(" AND ", @clause_l);
360             $clause_str = "WHERE $clause_str ";
361         }
362     }
364     return $clause_str;
367 sub get_select_statement {
368     my ($msg, $msg_hash)= @_;
369     my $select = "*";
370     if( exists $msg_hash->{'select'} ) {
371         my $select_l = \@{$msg_hash->{'select'}};
372         $select = join(' AND ', @{$select_l});
373     }
374     return $select;
378 sub get_update_statement {
379     my ($msg, $msg_hash) = @_;
380     my $error= 0;
381     my $update_str= "";
382     my @update_l; 
384     if( not exists $msg_hash->{'update'} ) { $error++; };
386     if( $error == 0 ) {
387         my $update= @{$msg_hash->{'update'}}[0];
388         while( my ($tag, $val) = each %{$update} ) {
389             my $val= @{$update->{$tag}}[0];
390             push(@update_l, "$tag='$val'");
391         }
392         if( 0 == @update_l ) { $error++; };   
393     }
395     if( $error == 0 ) { 
396         $update_str= join(', ', @update_l);
397         $update_str= "SET $update_str ";
398     }
400     return $update_str;
403 sub get_limit_statement {
404     my ($msg, $msg_hash)= @_; 
405     my $error= 0;
406     my $limit_str = "";
407     my ($from, $to);
409     if( not exists $msg_hash->{'limit'} ) { $error++; };
411     if( $error == 0 ) {
412         eval {
413             my $limit= @{$msg_hash->{'limit'}}[0];
414             $from= @{$limit->{'from'}}[0];
415             $to= @{$limit->{'to'}}[0];
416         };
417         if( $@ ) {
418             $error++;
419         }
420     }
422     if( $error == 0 ) {
423         $limit_str= "LIMIT $from, $to";
424     }   
425     
426     return $limit_str;
429 sub get_orderby_statement {
430     my ($msg, $msg_hash)= @_;
431     my $error= 0;
432     my $order_str= "";
433     my $order;
434     
435     if( not exists $msg_hash->{'orderby'} ) { $error++; };
437     if( $error == 0) {
438         eval {
439             $order= @{$msg_hash->{'orderby'}}[0];
440         };
441         if( $@ ) {
442             $error++;
443         }
444     }
446     if( $error == 0 ) {
447         $order_str= "ORDER BY $order";   
448     }
449     
450     return $order_str;
453 1;