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