Code

cc58491b87c6d3cb465b942dc4c98278f948a429
[gosa.git] / gosa-si / modules / GosaSupportDaemon.pm
1 package GOSA::GosaSupportDaemon;
3 use Exporter;
4 @ISA = qw(Exporter);
5 my @functions = (
6     "create_xml_hash",
7     "get_content_from_xml_hash",
8     "add_content2xml_hash",
9     "create_xml_string",
10     "transform_msg2hash",
11     "get_time",
12     "build_msg",
13     "db_res2xml",
14     "db_res2si_msg",
15     "get_where_statement",
16     "get_select_statement",
17     "get_update_statement",
18     "get_limit_statement",
19     "get_orderby_statement",
20     "get_dns_domains",
21     ); 
22 @EXPORT = @functions;
23 use strict;
24 use warnings;
25 use IO::Socket::INET;
26 use Crypt::Rijndael;
27 use Digest::MD5  qw(md5 md5_hex md5_base64);
28 use MIME::Base64;
29 use XML::Simple;
31 my $op_hash = {
32     'eq' => '=',
33     'ne' => '!=',
34     'ge' => '>=',
35     'gt' => '>',
36     'le' => '<=',
37     'lt' => '<',
38 };
41 BEGIN {}
43 END {}
45 ### Start ######################################################################
47 my $xml = new XML::Simple();
49 sub daemon_log {
50     my ($msg, $level) = @_ ;
51     &main::daemon_log($msg, $level);
52     return;
53 }
58 #===  FUNCTION  ================================================================
59 #         NAME:  create_xml_hash
60 #   PARAMETERS:  header - string - message header (required)
61 #                source - string - where the message come from (required)
62 #                target - string - where the message should go to (required)
63 #                [header_value] - string - something usefull (optional)
64 #      RETURNS:  hash - hash - nomen est omen
65 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
66 #===============================================================================
67 sub create_xml_hash {
68     my ($header, $source, $target, $header_value) = @_;
69     my $hash = {
70             header => [$header],
71             source => [$source],
72             target => [$target],
73             $header => [$header_value],
74     };
75     return $hash
76 }
79 #===  FUNCTION  ================================================================
80 #         NAME:  create_xml_string
81 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
82 #      RETURNS:  xml_string - string - xml string representation of the hash
83 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
84 #===============================================================================
85 sub create_xml_string {
86     my ($xml_hash) = @_ ;
87     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
88     #$xml_string =~ s/[\n]+//g;
89     #daemon_log("create_xml_string:",7);
90     #daemon_log("$xml_string\n", 7);
91     return $xml_string;
92 }
95 sub transform_msg2hash {
96     my ($msg) = @_ ;
97     my $hash = $xml->XMLin($msg, ForceArray=>1);
98     
99     # xml tags without a content are created as an empty hash
100     # substitute it with an empty list
101     eval {
102         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
103             if( 1 == @{ $xml_content } ) {
104                 # there is only one element in xml_content list ...
105                 my $element = @{ $xml_content }[0];
106                 if( ref($element) eq "HASH" ) {
107                     # and this element is an hash ...
108                     my $len_element = keys %{ $element };
109                     if( $len_element == 0 ) {
110                         # and this hash is empty, then substitute the xml_content
111                         # with an empty string in list
112                         $hash->{$xml_tag} = [ "none" ];
113                     }
114                 }
115             }
116         }
117     };
118     if( $@ ) {  
119         $hash = undef;
120     }
122     return $hash;
126 #===  FUNCTION  ================================================================
127 #         NAME:  add_content2xml_hash
128 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
129 #                element - string - key for the hash
130 #                content - string - value for the hash
131 #      RETURNS:  nothing
132 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
133 #                then append value to list
134 #===============================================================================
135 sub add_content2xml_hash {
136     my ($xml_ref, $element, $content) = @_;
137     if(not exists $$xml_ref{$element} ) {
138         $$xml_ref{$element} = [];
139     }
140     my $tmp = $$xml_ref{$element};
141     push(@$tmp, $content);
142     return;
146 #===  FUNCTION  ================================================================
147 #         NAME:  encrypt_msg
148 #   PARAMETERS:  msg - string - message to encrypt
149 #                my_cipher - ref - reference to a Crypt::Rijndael object
150 #      RETURNS:  crypted_msg - string - crypted message
151 #  DESCRIPTION:  crypts the incoming message with the Crypt::Rijndael module
152 #===============================================================================
153 #sub encrypt_msg {
154 #    my ($msg, $key) = @_;
155 #    my $my_cipher = &create_ciphering($key);
156 #    {
157 #      use bytes;
158 #      $msg = "\0"x(16-length($msg)%16).$msg;
159 #    }
160 #    $msg = $my_cipher->encrypt($msg);
161 #    chomp($msg = &encode_base64($msg));
162 #    # there are no newlines allowed inside msg
163 #    $msg=~ s/\n//g;
164 #    return $msg;
165 #}
168 #===  FUNCTION  ================================================================
169 #         NAME:  decrypt_msg
170 #   PARAMETERS:  crypted_msg - string - message to decrypt
171 #                my_cipher - ref - reference to a Crypt::Rijndael object
172 #      RETURNS:  msg - string - decrypted message
173 #  DESCRIPTION:  decrypts the incoming message with the Crypt::Rijndael module
174 #===============================================================================
175 #sub decrypt_msg {
176 #    my ($msg, $my_cipher) = @_ ;
177 #    
178 #    if(defined $msg && defined $my_cipher) {
179 #        $msg = &decode_base64($msg);
180 #    }
181 #    $msg = $my_cipher->decrypt($msg); 
182 #    $msg =~ s/\0*//g;
183 #    return $msg;
184 #    my ($msg, $key) = @_ ;
185 #    $msg = &decode_base64($msg);
186 #    my $my_cipher = &create_ciphering($key);
187 #    $msg = $my_cipher->decrypt($msg); 
188 #    $msg =~ s/\0*//g;
189 #    return $msg;
190 #}
193 #===  FUNCTION  ================================================================
194 #         NAME:  create_ciphering
195 #   PARAMETERS:  passwd - string - used to create ciphering
196 #      RETURNS:  cipher - object
197 #  DESCRIPTION:  creates a Crypt::Rijndael::MODE_CBC object with passwd as key
198 #===============================================================================
199 #sub create_ciphering {
200 #    my ($passwd) = @_;
201 #    $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
202 #    my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
204 #    #daemon_log("iv: $iv", 7);
205 #    #daemon_log("key: $passwd", 7);
206 #    my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
207 #    $my_cipher->set_iv($iv);
208 #    return $my_cipher;
209 #}
212 #===  FUNCTION  ================================================================
213 #         NAME:  open_socket
214 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
215 #                [PeerPort] string necessary if port not appended by PeerAddr
216 #      RETURNS:  socket IO::Socket::INET
217 #  DESCRIPTION:  open a socket to PeerAddr
218 #===============================================================================
219 #sub open_socket {
220 #    my ($PeerAddr, $PeerPort) = @_ ;
221 #    if(defined($PeerPort)){
222 #        $PeerAddr = $PeerAddr.":".$PeerPort;
223 #    }
224 #    my $socket;
225 #    $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
226 #            Porto => "tcp",
227 #            Type => SOCK_STREAM,
228 #            Timeout => 5,
229 #            );
230 #    if(not defined $socket) {
231 #        return;
232 #    }
233 #    &daemon_log("open_socket: $PeerAddr", 7);
234 #    return $socket;
235 #}
238 sub get_time {
239     my ($seconds, $minutes, $hours, $monthday, $month,
240             $year, $weekday, $yearday, $sommertime) = localtime(time);
241     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
242     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
243     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
244     $month+=1;
245     $month = $month < 10 ? $month = "0".$month : $month;
246     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
247     $year+=1900;
248     return "$year$month$monthday$hours$minutes$seconds";
253 #===  FUNCTION  ================================================================
254 #         NAME: build_msg
255 #  DESCRIPTION: Send a message to a destination
256 #   PARAMETERS: [header] Name of the header
257 #               [from]   sender ip
258 #               [to]     recipient ip
259 #               [data]   Hash containing additional attributes for the xml
260 #                        package
261 #      RETURNS:  nothing
262 #===============================================================================
263 sub build_msg ($$$$) {
264         my ($header, $from, $to, $data) = @_;
266         my $out_hash = &create_xml_hash($header, $from, $to);
268         while ( my ($key, $value) = each(%$data) ) {
269                 if(ref($value) eq 'ARRAY'){
270                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
271                 } else {
272                         &add_content2xml_hash($out_hash, $key, $value);
273                 }
274         }
275     my $out_msg = &create_xml_string($out_hash);
276     return $out_msg;
280 sub db_res2xml {
281     my ($db_res) = @_ ;
282     my $xml = "";
284     my $len_db_res= keys %{$db_res};
285     for( my $i= 1; $i<= $len_db_res; $i++ ) {
286         $xml .= "\n<answer$i>";
287         my $hash= $db_res->{$i};
288         while ( my ($column_name, $column_value) = each %{$hash} ) {
289             $xml .= "<$column_name>";
290             my $xml_content;
291             if( $column_name eq "xmlmessage" ) {
292                 $xml_content = &encode_base64($column_value);
293             } else {
294                 $xml_content = $column_value;
295             }
296             $xml .= $xml_content;
297             $xml .= "</$column_name>"; 
298         }
299         $xml .= "</answer$i>";
301     }
303     return $xml;
307 sub db_res2si_msg {
308     my ($db_res, $header, $target, $source) = @_;
310     my $si_msg = "<xml>";
311     $si_msg .= "<header>$header</header>";
312     $si_msg .= "<source>$source</source>";
313     $si_msg .= "<target>$target</target>";
314     $si_msg .= &db_res2xml;
315     $si_msg .= "</xml>";
319 sub get_where_statement {
320     my ($msg, $msg_hash) = @_;
321     my $error= 0;
322     
323     my $clause_str= "";
324     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
325         $error++; 
326     }
328     if( $error == 0 ) {
329         my @clause_l;
330         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
331         foreach my $clause (@where) {
332             my $connector = $clause->{'connector'}[0];
333             if( not defined $connector ) { $connector = "AND"; }
334             $connector = uc($connector);
335             delete($clause->{'connector'});
337             my @phrase_l ;
338             foreach my $phrase (@{$clause->{'phrase'}}) {
339                 my $operator = "=";
340                 if( exists $phrase->{'operator'} ) {
341                     my $op = $op_hash->{$phrase->{'operator'}[0]};
342                     if( not defined $op ) {
343                         &main::daemon_log("Can not translate operator '$operator' in where ".
344                                 "statement to sql valid syntax. Please use 'eq', ".
345                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
346                         &main::daemon_log($msg, 8);
347                         $op = "=";
348                     }
349                     $operator = $op;
350                     delete($phrase->{'operator'});
351                 }
353                 my @xml_tags = keys %{$phrase};
354                 my $tag = $xml_tags[0];
355                 my $val = $phrase->{$tag}[0];
356                 push(@phrase_l, "$tag$operator'$val'");
357             }
358             my $clause_str .= join(" $connector ", @phrase_l);
359             push(@clause_l, $clause_str);
360         }
362         if( not 0 == @clause_l ) {
363             $clause_str = join(" AND ", @clause_l);
364             $clause_str = "WHERE $clause_str ";
365         }
366     }
368     return $clause_str;
371 sub get_select_statement {
372     my ($msg, $msg_hash)= @_;
373     my $select = "*";
374     if( exists $msg_hash->{'select'} ) {
375         my $select_l = \@{$msg_hash->{'select'}};
376         $select = join(' AND ', @{$select_l});
377     }
378     return $select;
382 sub get_update_statement {
383     my ($msg, $msg_hash) = @_;
384     my $error= 0;
385     my $update_str= "";
386     my @update_l; 
388     if( not exists $msg_hash->{'update'} ) { $error++; };
390     if( $error == 0 ) {
391         my $update= @{$msg_hash->{'update'}}[0];
392         while( my ($tag, $val) = each %{$update} ) {
393             my $val= @{$update->{$tag}}[0];
394             push(@update_l, "$tag='$val'");
395         }
396         if( 0 == @update_l ) { $error++; };   
397     }
399     if( $error == 0 ) { 
400         $update_str= join(', ', @update_l);
401         $update_str= "SET $update_str ";
402     }
404     return $update_str;
407 sub get_limit_statement {
408     my ($msg, $msg_hash)= @_; 
409     my $error= 0;
410     my $limit_str = "";
411     my ($from, $to);
413     if( not exists $msg_hash->{'limit'} ) { $error++; };
415     if( $error == 0 ) {
416         eval {
417             my $limit= @{$msg_hash->{'limit'}}[0];
418             $from= @{$limit->{'from'}}[0];
419             $to= @{$limit->{'to'}}[0];
420         };
421         if( $@ ) {
422             $error++;
423         }
424     }
426     if( $error == 0 ) {
427         $limit_str= "LIMIT $from, $to";
428     }   
429     
430     return $limit_str;
433 sub get_orderby_statement {
434     my ($msg, $msg_hash)= @_;
435     my $error= 0;
436     my $order_str= "";
437     my $order;
438     
439     if( not exists $msg_hash->{'orderby'} ) { $error++; };
441     if( $error == 0) {
442         eval {
443             $order= @{$msg_hash->{'orderby'}}[0];
444         };
445         if( $@ ) {
446             $error++;
447         }
448     }
450     if( $error == 0 ) {
451         $order_str= "ORDER BY $order";   
452     }
453     
454     return $order_str;
457 sub get_dns_domains() {
458         my $line;
459         my @searches;
460         open(RESOLV, "</etc/resolv.conf") or return @searches;
461         while(<RESOLV>){
462                 $line= $_;
463                 chomp $line;
464                 $line =~ s/^\s+//;
465                 $line =~ s/\s+$//;
466                 $line =~ s/\s+/ /;
467                 if ($line =~ /^domain (.*)$/ ){
468                         push(@searches, $1);
469                 } elsif ($line =~ /^search (.*)$/ ){
470                         push(@searches, split(/ /, $1));
471                 }
472         }
473         close(RESOLV);
475         my %tmp = map { $_ => 1 } @searches;
476         @searches = sort keys %tmp;
478         return @searches;
481 1;