Code

219a2fde5193f0bda81941d569453c231bd2e41e
[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     "get_where_statement",
14     "get_select_statement",
15     "get_update_statement",
16     "get_limit_statement",
17     "get_orderby_statement",
18     "get_dns_domains",
19     ); 
20 @EXPORT = @functions;
21 use strict;
22 use warnings;
23 use IO::Socket::INET;
24 use Crypt::Rijndael;
25 use Digest::MD5  qw(md5 md5_hex md5_base64);
26 use MIME::Base64;
27 use XML::Simple;
29 my $op_hash = {
30     'eq' => '=',
31     'ne' => '!=',
32     'ge' => '>=',
33     'gt' => '>',
34     'le' => '<=',
35     'lt' => '<',
36 };
39 BEGIN {}
41 END {}
43 ### Start ######################################################################
45 my $xml = new XML::Simple();
47 sub daemon_log {
48     my ($msg, $level) = @_ ;
49     &main::daemon_log($msg, $level);
50     return;
51 }
56 #===  FUNCTION  ================================================================
57 #         NAME:  create_xml_hash
58 #   PARAMETERS:  header - string - message header (required)
59 #                source - string - where the message come from (required)
60 #                target - string - where the message should go to (required)
61 #                [header_value] - string - something usefull (optional)
62 #      RETURNS:  hash - hash - nomen est omen
63 #  DESCRIPTION:  creates a key-value hash, all values are stored in a array
64 #===============================================================================
65 sub create_xml_hash {
66     my ($header, $source, $target, $header_value) = @_;
67     my $hash = {
68             header => [$header],
69             source => [$source],
70             target => [$target],
71             $header => [$header_value],
72     };
73     return $hash
74 }
77 #===  FUNCTION  ================================================================
78 #         NAME:  create_xml_string
79 #   PARAMETERS:  xml_hash - hash - hash from function create_xml_hash
80 #      RETURNS:  xml_string - string - xml string representation of the hash
81 #  DESCRIPTION:  transform the hash to a string using XML::Simple module
82 #===============================================================================
83 sub create_xml_string {
84     my ($xml_hash) = @_ ;
85     my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
86     #$xml_string =~ s/[\n]+//g;
87     #daemon_log("create_xml_string:",7);
88     #daemon_log("$xml_string\n", 7);
89     return $xml_string;
90 }
93 sub transform_msg2hash {
94     my ($msg) = @_ ;
95     my $hash = $xml->XMLin($msg, ForceArray=>1);
96     
97     # xml tags without a content are created as an empty hash
98     # substitute it with an empty list
99     eval {
100         while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
101             if( 1 == @{ $xml_content } ) {
102                 # there is only one element in xml_content list ...
103                 my $element = @{ $xml_content }[0];
104                 if( ref($element) eq "HASH" ) {
105                     # and this element is an hash ...
106                     my $len_element = keys %{ $element };
107                     if( $len_element == 0 ) {
108                         # and this hash is empty, then substitute the xml_content
109                         # with an empty string in list
110                         $hash->{$xml_tag} = [ "none" ];
111                     }
112                 }
113             }
114         }
115     };
116     if( $@ ) {  
117         $hash = undef;
118     }
120     return $hash;
124 #===  FUNCTION  ================================================================
125 #         NAME:  add_content2xml_hash
126 #   PARAMETERS:  xml_ref - ref - reference to a hash from function create_xml_hash
127 #                element - string - key for the hash
128 #                content - string - value for the hash
129 #      RETURNS:  nothing
130 #  DESCRIPTION:  add key-value pair to xml_ref, if key alread exists, 
131 #                then append value to list
132 #===============================================================================
133 sub add_content2xml_hash {
134     my ($xml_ref, $element, $content) = @_;
135     if(not exists $$xml_ref{$element} ) {
136         $$xml_ref{$element} = [];
137     }
138     my $tmp = $$xml_ref{$element};
139     push(@$tmp, $content);
140     return;
144 #===  FUNCTION  ================================================================
145 #         NAME:  encrypt_msg
146 #   PARAMETERS:  msg - string - message to encrypt
147 #                my_cipher - ref - reference to a Crypt::Rijndael object
148 #      RETURNS:  crypted_msg - string - crypted message
149 #  DESCRIPTION:  crypts the incoming message with the Crypt::Rijndael module
150 #===============================================================================
151 #sub encrypt_msg {
152 #    my ($msg, $key) = @_;
153 #    my $my_cipher = &create_ciphering($key);
154 #    {
155 #      use bytes;
156 #      $msg = "\0"x(16-length($msg)%16).$msg;
157 #    }
158 #    $msg = $my_cipher->encrypt($msg);
159 #    chomp($msg = &encode_base64($msg));
160 #    # there are no newlines allowed inside msg
161 #    $msg=~ s/\n//g;
162 #    return $msg;
163 #}
166 #===  FUNCTION  ================================================================
167 #         NAME:  decrypt_msg
168 #   PARAMETERS:  crypted_msg - string - message to decrypt
169 #                my_cipher - ref - reference to a Crypt::Rijndael object
170 #      RETURNS:  msg - string - decrypted message
171 #  DESCRIPTION:  decrypts the incoming message with the Crypt::Rijndael module
172 #===============================================================================
173 #sub decrypt_msg {
174 #    my ($msg, $my_cipher) = @_ ;
175 #    
176 #    if(defined $msg && defined $my_cipher) {
177 #        $msg = &decode_base64($msg);
178 #    }
179 #    $msg = $my_cipher->decrypt($msg); 
180 #    $msg =~ s/\0*//g;
181 #    return $msg;
182 #    my ($msg, $key) = @_ ;
183 #    $msg = &decode_base64($msg);
184 #    my $my_cipher = &create_ciphering($key);
185 #    $msg = $my_cipher->decrypt($msg); 
186 #    $msg =~ s/\0*//g;
187 #    return $msg;
188 #}
191 #===  FUNCTION  ================================================================
192 #         NAME:  create_ciphering
193 #   PARAMETERS:  passwd - string - used to create ciphering
194 #      RETURNS:  cipher - object
195 #  DESCRIPTION:  creates a Crypt::Rijndael::MODE_CBC object with passwd as key
196 #===============================================================================
197 #sub create_ciphering {
198 #    my ($passwd) = @_;
199 #    $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
200 #    my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
202 #    #daemon_log("iv: $iv", 7);
203 #    #daemon_log("key: $passwd", 7);
204 #    my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
205 #    $my_cipher->set_iv($iv);
206 #    return $my_cipher;
207 #}
210 #===  FUNCTION  ================================================================
211 #         NAME:  open_socket
212 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
213 #                [PeerPort] string necessary if port not appended by PeerAddr
214 #      RETURNS:  socket IO::Socket::INET
215 #  DESCRIPTION:  open a socket to PeerAddr
216 #===============================================================================
217 #sub open_socket {
218 #    my ($PeerAddr, $PeerPort) = @_ ;
219 #    if(defined($PeerPort)){
220 #        $PeerAddr = $PeerAddr.":".$PeerPort;
221 #    }
222 #    my $socket;
223 #    $socket = new IO::Socket::INET(PeerAddr => $PeerAddr,
224 #            Porto => "tcp",
225 #            Type => SOCK_STREAM,
226 #            Timeout => 5,
227 #            );
228 #    if(not defined $socket) {
229 #        return;
230 #    }
231 #    &daemon_log("open_socket: $PeerAddr", 7);
232 #    return $socket;
233 #}
236 sub get_time {
237     my ($seconds, $minutes, $hours, $monthday, $month,
238             $year, $weekday, $yearday, $sommertime) = localtime(time);
239     $hours = $hours < 10 ? $hours = "0".$hours : $hours;
240     $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
241     $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
242     $month+=1;
243     $month = $month < 10 ? $month = "0".$month : $month;
244     $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
245     $year+=1900;
246     return "$year$month$monthday$hours$minutes$seconds";
251 #===  FUNCTION  ================================================================
252 #         NAME: build_msg
253 #  DESCRIPTION: Send a message to a destination
254 #   PARAMETERS: [header] Name of the header
255 #               [from]   sender ip
256 #               [to]     recipient ip
257 #               [data]   Hash containing additional attributes for the xml
258 #                        package
259 #      RETURNS:  nothing
260 #===============================================================================
261 sub build_msg ($$$$) {
262         my ($header, $from, $to, $data) = @_;
264         my $out_hash = &create_xml_hash($header, $from, $to);
266         while ( my ($key, $value) = each(%$data) ) {
267                 if(ref($value) eq 'ARRAY'){
268                         map(&add_content2xml_hash($out_hash, $key, $_), @$value);
269                 } else {
270                         &add_content2xml_hash($out_hash, $key, $value);
271                 }
272         }
273     my $out_msg = &create_xml_string($out_hash);
274     return $out_msg;
278 sub get_where_statement {
279     my ($msg, $msg_hash) = @_;
280     my $error= 0;
281     
282     my $clause_str= "";
283     if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) { 
284         $error++; 
285     }
287     if( $error == 0 ) {
288         my @clause_l;
289         my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
290         foreach my $clause (@where) {
291             my $connector = $clause->{'connector'}[0];
292             if( not defined $connector ) { $connector = "AND"; }
293             $connector = uc($connector);
294             delete($clause->{'connector'});
296             my @phrase_l ;
297             foreach my $phrase (@{$clause->{'phrase'}}) {
298                 my $operator = "=";
299                 if( exists $phrase->{'operator'} ) {
300                     my $op = $op_hash->{$phrase->{'operator'}[0]};
301                     if( not defined $op ) {
302                         &main::daemon_log("Can not translate operator '$operator' in where ".
303                                 "statement to sql valid syntax. Please use 'eq', ".
304                                 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
305                         &main::daemon_log($msg, 8);
306                         $op = "=";
307                     }
308                     $operator = $op;
309                     delete($phrase->{'operator'});
310                 }
312                 my @xml_tags = keys %{$phrase};
313                 my $tag = $xml_tags[0];
314                 my $val = $phrase->{$tag}[0];
315                 push(@phrase_l, "$tag$operator'$val'");
316             }
317             my $clause_str .= join(" $connector ", @phrase_l);
318             push(@clause_l, $clause_str);
319         }
321         if( not 0 == @clause_l ) {
322             $clause_str = join(" AND ", @clause_l);
323             $clause_str = "WHERE $clause_str ";
324         }
325     }
327     return $clause_str;
330 sub get_select_statement {
331     my ($msg, $msg_hash)= @_;
332     my $select = "*";
333     if( exists $msg_hash->{'select'} ) {
334         my $select_l = \@{$msg_hash->{'select'}};
335         $select = join(' AND ', @{$select_l});
336     }
337     return $select;
341 sub get_update_statement {
342     my ($msg, $msg_hash) = @_;
343     my $error= 0;
344     my $update_str= "";
345     my @update_l; 
347     if( not exists $msg_hash->{'update'} ) { $error++; };
349     if( $error == 0 ) {
350         my $update= @{$msg_hash->{'update'}}[0];
351         while( my ($tag, $val) = each %{$update} ) {
352             my $val= @{$update->{$tag}}[0];
353             push(@update_l, "$tag='$val'");
354         }
355         if( 0 == @update_l ) { $error++; };   
356     }
358     if( $error == 0 ) { 
359         $update_str= join(', ', @update_l);
360         $update_str= "SET $update_str ";
361     }
363     return $update_str;
366 sub get_limit_statement {
367     my ($msg, $msg_hash)= @_; 
368     my $error= 0;
369     my $limit_str = "";
370     my ($from, $to);
372     if( not exists $msg_hash->{'limit'} ) { $error++; };
374     if( $error == 0 ) {
375         eval {
376             my $limit= @{$msg_hash->{'limit'}}[0];
377             $from= @{$limit->{'from'}}[0];
378             $to= @{$limit->{'to'}}[0];
379         };
380         if( $@ ) {
381             $error++;
382         }
383     }
385     if( $error == 0 ) {
386         $limit_str= "LIMIT $from, $to";
387     }   
388     
389     return $limit_str;
392 sub get_orderby_statement {
393     my ($msg, $msg_hash)= @_;
394     my $error= 0;
395     my $order_str= "";
396     my $order;
397     
398     if( not exists $msg_hash->{'orderby'} ) { $error++; };
400     if( $error == 0) {
401         eval {
402             $order= @{$msg_hash->{'orderby'}}[0];
403         };
404         if( $@ ) {
405             $error++;
406         }
407     }
409     if( $error == 0 ) {
410         $order_str= "ORDER BY $order";   
411     }
412     
413     return $order_str;
416 sub get_dns_domains() {
417         my $line;
418         my @searches;
419         open(RESOLV, "</etc/resolv.conf") or return @searches;
420         while(<RESOLV>){
421                 $line= $_;
422                 chomp $line;
423                 $line =~ s/^\s+//;
424                 $line =~ s/\s+$//;
425                 $line =~ s/\s+/ /;
426                 if ($line =~ /^domain (.*)$/ ){
427                         push(@searches, $1);
428                 } elsif ($line =~ /^search (.*)$/ ){
429                         push(@searches, split(/ /, $1));
430                 }
431         }
432         close(RESOLV);
434         my %tmp = map { $_ => 1 } @searches;
435         @searches = sort keys %tmp;
437         return @searches;
440 1;