Code

Only write non-empty entries...
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 @EXPORT = qw(create_xml_hash 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 #===  FUNCTION  ================================================================
62 #         NAME:  create_xml_string
63 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
64 #      RETURNS:  xml_string - string - xml string representation of the hash
65 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
66 #===============================================================================
67 sub create_xml_string {
68     my ($xml_hash) = @_ ;
69     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
70     #$xml_string =~ s/[\n]+//g;
71     #daemon_log("create_xml_string:",7);
72     #daemon_log("$xml_string\n", 7);
73     return $xml_string;
74 }
77 sub transform_msg2hash {
78     my ($msg) = @_ ;
79     my $hash = $xml->XMLin($msg, ForceArray=>1);
80     
81     # xml tags without a content are created as an empty hash
82     # substitute it with an empty list
83     eval {
84         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
85             if( 1 == @{ $xml_content } ) {
86                 # there is only one element in xml_content list ...
87                 my $element = @{ $xml_content }[0];
88                 if( ref($element) eq "HASH" ) {
89                     # and this element is an hash ...
90                     my $len_element = keys %{ $element };
91                     if( $len_element == 0 ) {
92                         # and this hash is empty, then substitute the xml_content
93                         # with an empty string in list
94                         $hash->{$xml_tag} = [ "none" ];
95                     }
96                 }
97             }
98         }
99     };
100     if( $@ ) {  
101         $hash = undef;
102     }
104     return $hash;
108 #===  FUNCTION  ================================================================
109 #         NAME:  add_content2xml_hash
110 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
111 #                element - string - key for the hash
112 #                content - string - value for the hash
113 #      RETURNS:  nothing
114 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
115 #                then append value to list
116 #===============================================================================
117 sub add_content2xml_hash {
118     my ($xml_ref, $element, $content) = @_;
119     if(not exists $$xml_ref{$element} ) {
120         $$xml_ref{$element} = [];
121     }
122     my $tmp = $$xml_ref{$element};
123     push(@$tmp, $content);
124     return;
128 #===  FUNCTION  ================================================================
129 #         NAME:  encrypt_msg
130 #   PARAMETERS:  msg - string - message to encrypt
131 #                my_cipher - ref - reference to a Crypt::Rijndael object
132 #      RETURNS:  crypted_msg - string - crypted message
133 #  DESCRIPTION:  crypts the incoming message with the Crypt::Rijndael module
134 #===============================================================================
135 sub encrypt_msg {
136 #    my ($msg, $my_cipher) = @_;
137 #    if(not defined $my_cipher) { print "no cipher object\n"; }
138 #    {
139 #      use bytes;
140 #      $msg = "\0"x(16-length($msg)%16).$msg;
141 #    }
142 #    $msg = $my_cipher->encrypt($msg);
143 #    chomp($msg = &encode_base64($msg));
145 #    # there are no newlines allowed inside msg
146 #    $msg=~ s/\n//g;
148 #    return $msg;
149     my ($msg, $key) = @_;
150     my $my_cipher = &create_ciphering($key);
151     {
152       use bytes;
153       $msg = "\0"x(16-length($msg)%16).$msg;
154     }
155     $msg = $my_cipher->encrypt($msg);
156     chomp($msg = &encode_base64($msg));
157     # there are no newlines allowed inside msg
158     $msg=~ s/\n//g;
159     return $msg;
164 #===  FUNCTION  ================================================================
165 #         NAME:  decrypt_msg
166 #   PARAMETERS:  crypted_msg - string - message to decrypt
167 #                my_cipher - ref - reference to a Crypt::Rijndael object
168 #      RETURNS:  msg - string - decrypted message
169 #  DESCRIPTION:  decrypts the incoming message with the Crypt::Rijndael module
170 #===============================================================================
171 sub decrypt_msg {
172 #    my ($msg, $my_cipher) = @_ ;
173 #    
174 #    if(defined $msg && defined $my_cipher) {
175 #        $msg = &decode_base64($msg);
176 #    }
177 #    $msg = $my_cipher->decrypt($msg); 
178 #    $msg =~ s/\0*//g;
179 #    return $msg;
180     my ($msg, $key) = @_ ;
181     $msg = &decode_base64($msg);
182     my $my_cipher = &create_ciphering($key);
183     $msg = $my_cipher->decrypt($msg); 
184     $msg =~ s/\0*//g;
185     return $msg;
189 #===  FUNCTION  ================================================================
190 #         NAME:  create_ciphering
191 #   PARAMETERS:  passwd - string - used to create ciphering
192 #      RETURNS:  cipher - object
193 #  DESCRIPTION:  creates a Crypt::Rijndael::MODE_CBC object with passwd as key
194 #===============================================================================
195 sub create_ciphering {
196     my ($passwd) = @_;
197     $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
198     my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
200     #daemon_log("iv: $iv", 7);
201     #daemon_log("key: $passwd", 7);
202     my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
203     $my_cipher->set_iv($iv);
204     return $my_cipher;
208 #===  FUNCTION  ================================================================
209 #         NAME:  open_socket
210 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
211 #                [PeerPort] string necessary if port not appended by PeerAddr
212 #      RETURNS:  socket IO::Socket::INET
213 #  DESCRIPTION:  open a socket to PeerAddr
214 #===============================================================================
215 #sub open_socket {
216 #    my ($PeerAddr, $PeerPort) = @_ ;
217 #    if(defined($PeerPort)){
218 #        $PeerAddr = $PeerAddr.":".$PeerPort;
219 #    }
220 #    my $socket;
221 #    $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
222 #            Porto => "tcp",
223 #            Type => SOCK_STREAM,
224 #            Timeout => 5,
225 #            );
226 #    if(not defined $socket) {
227 #        return;
228 #    }
229 #    &daemon_log("open_socket: $PeerAddr", 7);
230 #    return $socket;
231 #}
234 sub get_time {
235     my ($seconds, $minutes, $hours, $monthday, $month,
236             $year, $weekday, $yearday, $sommertime) = localtime(time);
237     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
238     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
239     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
240     $month+=1;
241     $month = $month < 10 ? $month = "0".$month : $month;
242     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
243     $year+=1900;
244     return "$year$month$monthday$hours$minutes$seconds";
249 #===  FUNCTION  ================================================================
250 #         NAME: send_msg
251 #  DESCRIPTION: Send a message to a destination
252 #   PARAMETERS: [header] Name of the header
253 #               [from]   sender ip
254 #               [to]     recipient ip
255 #               [data]   Hash containing additional attributes for the xml
256 #                        package
257 #      RETURNS:  nothing
258 #===============================================================================
259 sub send_msg ($$$$) {
260         my ($header, $from, $to, $data) = @_;
262         my $out_hash = &create_xml_hash($header, $from, $to);
264         while ( my ($key, $value) = each(%$data) ) {
265                 if(ref($value) eq 'ARRAY'){
266                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
267                 } else {
268                         &add_content2xml_hash($out_hash, $key, $value);
269                 }
270         }
271     my $out_msg = &create_xml_string($out_hash);
272     return $out_msg;
276 sub get_where_statement {
277     my ($msg, $msg_hash) = @_;
278     my $error= 0;
279     
280     my $clause_str= "";
281     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
282         $error++; 
283     }
285     if( $error == 0 ) {
286         my @clause_l;
287         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
288         foreach my $clause (@where) {
289             my $connector = $clause->{'connector'}[0];
290             if( not defined $connector ) { $connector = "AND"; }
291             $connector = uc($connector);
292             delete($clause->{'connector'});
294             my @phrase_l ;
295             foreach my $phrase (@{$clause->{'phrase'}}) {
296                 my $operator = "=";
297                 if( exists $phrase->{'operator'} ) {
298                     my $op = $op_hash->{$phrase->{'operator'}[0]};
299                     if( not defined $op ) {
300                         &main::daemon_log("Can not translate operator '$operator' in where ".
301                                 "statement to sql valid syntax. Please use 'eq', ".
302                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
303                         &main::daemon_log($msg, 8);
304                         $op = "=";
305                     }
306                     $operator = $op;
307                     delete($phrase->{'operator'});
308                 }
310                 my @xml_tags = keys %{$phrase};
311                 my $tag = $xml_tags[0];
312                 my $val = $phrase->{$tag}[0];
313                 push(@phrase_l, "$tag$operator'$val'");
314             }
315             my $clause_str .= join(" $connector ", @phrase_l);
316             push(@clause_l, $clause_str);
317         }
319         if( not 0 == @clause_l ) {
320             $clause_str = join(" AND ", @clause_l);
321             $clause_str = "WHERE $clause_str ";
322         }
323     }
325     return $clause_str;
328 sub get_select_statement {
329     my ($msg, $msg_hash)= @_;
330     my $select = "*";
331     if( exists $msg_hash->{'select'} ) {
332         my $select_l = \@{$msg_hash->{'select'}};
333         $select = join(' AND ', @{$select_l});
334     }
335     return $select;
339 sub get_update_statement {
340     my ($msg, $msg_hash) = @_;
341     my $error= 0;
342     my $update_str= "";
343     my @update_l; 
345     if( not exists $msg_hash->{'update'} ) { $error++; };
347     if( $error == 0 ) {
348         my $update= @{$msg_hash->{'update'}}[0];
349         while( my ($tag, $val) = each %{$update} ) {
350             my $val= @{$update->{$tag}}[0];
351             push(@update_l, "$tag='$val'");
352         }
353         if( 0 == @update_l ) { $error++; };   
354     }
356     if( $error == 0 ) { 
357         $update_str= join(', ', @update_l);
358         $update_str= "SET $update_str ";
359     }
361     return $update_str;
364 sub get_limit_statement {
365     my ($msg, $msg_hash)= @_; 
366     my $error= 0;
367     my $limit_str = "";
368     my ($from, $to);
370     if( not exists $msg_hash->{'limit'} ) { $error++; };
372     if( $error == 0 ) {
373         eval {
374             my $limit= @{$msg_hash->{'limit'}}[0];
375             $from= @{$limit->{'from'}}[0];
376             $to= @{$limit->{'to'}}[0];
377         };
378         if( $@ ) {
379             $error++;
380         }
381     }
383     if( $error == 0 ) {
384         $limit_str= "LIMIT $from, $to";
385     }   
386     
387     return $limit_str;
390 sub get_orderby_statement {
391     my ($msg, $msg_hash)= @_;
392     my $error= 0;
393     my $order_str= "";
394     my $order;
395     
396     if( not exists $msg_hash->{'orderby'} ) { $error++; };
398     if( $error == 0) {
399         eval {
400             $order= @{$msg_hash->{'orderby'}}[0];
401         };
402         if( $@ ) {
403             $error++;
404         }
405     }
407     if( $error == 0 ) {
408         $order_str= "ORDER BY $order";   
409     }
410     
411     return $order_str;
414 1;