e33c83eaa5305d9ad72c3a07931ebb2d36fb77db
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);
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;
123 }
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;
143 }
146 sub get_time {
147 my ($seconds, $minutes, $hours, $monthday, $month,
148 $year, $weekday, $yearday, $sommertime) = localtime(time);
149 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
150 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
151 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
152 $month+=1;
153 $month = $month < 10 ? $month = "0".$month : $month;
154 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
155 $year+=1900;
156 return "$year$month$monthday$hours$minutes$seconds";
158 }
161 #=== FUNCTION ================================================================
162 # NAME: build_msg
163 # DESCRIPTION: Send a message to a destination
164 # PARAMETERS: [header] Name of the header
165 # [from] sender ip
166 # [to] recipient ip
167 # [data] Hash containing additional attributes for the xml
168 # package
169 # RETURNS: nothing
170 #===============================================================================
171 sub build_msg ($$$$) {
172 my ($header, $from, $to, $data) = @_;
174 my $out_hash = &create_xml_hash($header, $from, $to);
176 while ( my ($key, $value) = each(%$data) ) {
177 if(ref($value) eq 'ARRAY'){
178 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
179 } else {
180 &add_content2xml_hash($out_hash, $key, $value);
181 }
182 }
183 my $out_msg = &create_xml_string($out_hash);
184 return $out_msg;
185 }
188 sub db_res2xml {
189 my ($db_res) = @_ ;
190 my $xml = "";
192 my $len_db_res= keys %{$db_res};
193 for( my $i= 1; $i<= $len_db_res; $i++ ) {
194 $xml .= "\n<answer$i>";
195 my $hash= $db_res->{$i};
196 while ( my ($column_name, $column_value) = each %{$hash} ) {
197 $xml .= "<$column_name>";
198 my $xml_content;
199 if( $column_name eq "xmlmessage" ) {
200 $xml_content = &encode_base64($column_value);
201 } else {
202 $xml_content = $column_value;
203 }
204 $xml .= $xml_content;
205 $xml .= "</$column_name>";
206 }
207 $xml .= "</answer$i>";
209 }
211 return $xml;
212 }
215 sub db_res2si_msg {
216 my ($db_res, $header, $target, $source) = @_;
218 my $si_msg = "<xml>";
219 $si_msg .= "<header>$header</header>";
220 $si_msg .= "<source>$source</source>";
221 $si_msg .= "<target>$target</target>";
222 $si_msg .= &db_res2xml;
223 $si_msg .= "</xml>";
224 }
227 sub get_where_statement {
228 my ($msg, $msg_hash) = @_;
229 my $error= 0;
231 my $clause_str= "";
232 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
233 $error++;
234 }
236 if( $error == 0 ) {
237 my @clause_l;
238 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
239 foreach my $clause (@where) {
240 my $connector = $clause->{'connector'}[0];
241 if( not defined $connector ) { $connector = "AND"; }
242 $connector = uc($connector);
243 delete($clause->{'connector'});
245 my @phrase_l ;
246 foreach my $phrase (@{$clause->{'phrase'}}) {
247 my $operator = "=";
248 if( exists $phrase->{'operator'} ) {
249 my $op = $op_hash->{$phrase->{'operator'}[0]};
250 if( not defined $op ) {
251 &main::daemon_log("ERROR: Can not translate operator '$operator' in where ".
252 "statement to sql valid syntax. Please use 'eq', ".
253 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
254 &main::daemon_log($msg, 8);
255 $op = "=";
256 }
257 $operator = $op;
258 delete($phrase->{'operator'});
259 }
261 my @xml_tags = keys %{$phrase};
262 my $tag = $xml_tags[0];
263 my $val = $phrase->{$tag}[0];
264 push(@phrase_l, "$tag$operator'$val'");
265 }
266 my $clause_str .= join(" $connector ", @phrase_l);
267 push(@clause_l, $clause_str);
268 }
270 if( not 0 == @clause_l ) {
271 $clause_str = join(" AND ", @clause_l);
272 $clause_str = "WHERE ($clause_str) ";
273 }
274 }
276 return $clause_str;
277 }
279 sub get_select_statement {
280 my ($msg, $msg_hash)= @_;
281 my $select = "*";
282 if( exists $msg_hash->{'select'} ) {
283 my $select_l = \@{$msg_hash->{'select'}};
284 $select = join(' AND ', @{$select_l});
285 }
286 return $select;
287 }
290 sub get_update_statement {
291 my ($msg, $msg_hash) = @_;
292 my $error= 0;
293 my $update_str= "";
294 my @update_l;
296 if( not exists $msg_hash->{'update'} ) { $error++; };
298 if( $error == 0 ) {
299 my $update= @{$msg_hash->{'update'}}[0];
300 while( my ($tag, $val) = each %{$update} ) {
301 my $val= @{$update->{$tag}}[0];
302 push(@update_l, "$tag='$val'");
303 }
304 if( 0 == @update_l ) { $error++; };
305 }
307 if( $error == 0 ) {
308 $update_str= join(', ', @update_l);
309 $update_str= "SET $update_str ";
310 }
312 return $update_str;
313 }
315 sub get_limit_statement {
316 my ($msg, $msg_hash)= @_;
317 my $error= 0;
318 my $limit_str = "";
319 my ($from, $to);
321 if( not exists $msg_hash->{'limit'} ) { $error++; };
323 if( $error == 0 ) {
324 eval {
325 my $limit= @{$msg_hash->{'limit'}}[0];
326 $from= @{$limit->{'from'}}[0];
327 $to= @{$limit->{'to'}}[0];
328 };
329 if( $@ ) {
330 $error++;
331 }
332 }
334 if( $error == 0 ) {
335 $limit_str= "LIMIT $from, $to";
336 }
338 return $limit_str;
339 }
341 sub get_orderby_statement {
342 my ($msg, $msg_hash)= @_;
343 my $error= 0;
344 my $order_str= "";
345 my $order;
347 if( not exists $msg_hash->{'orderby'} ) { $error++; };
349 if( $error == 0) {
350 eval {
351 $order= @{$msg_hash->{'orderby'}}[0];
352 };
353 if( $@ ) {
354 $error++;
355 }
356 }
358 if( $error == 0 ) {
359 $order_str= "ORDER BY $order";
360 }
362 return $order_str;
363 }
365 sub get_dns_domains() {
366 my $line;
367 my @searches;
368 open(RESOLV, "</etc/resolv.conf") or return @searches;
369 while(<RESOLV>){
370 $line= $_;
371 chomp $line;
372 $line =~ s/^\s+//;
373 $line =~ s/\s+$//;
374 $line =~ s/\s+/ /;
375 if ($line =~ /^domain (.*)$/ ){
376 push(@searches, $1);
377 } elsif ($line =~ /^search (.*)$/ ){
378 push(@searches, split(/ /, $1));
379 }
380 }
381 close(RESOLV);
383 my %tmp = map { $_ => 1 } @searches;
384 @searches = sort keys %tmp;
386 return @searches;
387 }
389 1;