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 "get_logged_in_users",
22 );
23 @EXPORT = @functions;
24 use strict;
25 use warnings;
26 use IO::Socket::INET;
27 use Crypt::Rijndael;
28 use Digest::MD5 qw(md5 md5_hex md5_base64);
29 use MIME::Base64;
30 use XML::Simple;
32 my $op_hash = {
33 'eq' => '=',
34 'ne' => '!=',
35 'ge' => '>=',
36 'gt' => '>',
37 'le' => '<=',
38 'lt' => '<',
39 };
42 BEGIN {}
44 END {}
46 ### Start ######################################################################
48 my $xml = new XML::Simple();
50 sub daemon_log {
51 my ($msg, $level) = @_ ;
52 &main::daemon_log($msg, $level);
53 return;
54 }
59 #=== FUNCTION ================================================================
60 # NAME: create_xml_hash
61 # PARAMETERS: header - string - message header (required)
62 # source - string - where the message come from (required)
63 # target - string - where the message should go to (required)
64 # [header_value] - string - something usefull (optional)
65 # RETURNS: hash - hash - nomen est omen
66 # DESCRIPTION: creates a key-value hash, all values are stored in a array
67 #===============================================================================
68 sub create_xml_hash {
69 my ($header, $source, $target, $header_value) = @_;
70 my $hash = {
71 header => [$header],
72 source => [$source],
73 target => [$target],
74 $header => [$header_value],
75 };
76 return $hash
77 }
80 #=== FUNCTION ================================================================
81 # NAME: create_xml_string
82 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
83 # RETURNS: xml_string - string - xml string representation of the hash
84 # DESCRIPTION: transform the hash to a string using XML::Simple module
85 #===============================================================================
86 sub create_xml_string {
87 my ($xml_hash) = @_ ;
88 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
89 #$xml_string =~ s/[\n]+//g;
90 #daemon_log("create_xml_string:",7);
91 #daemon_log("$xml_string\n", 7);
92 return $xml_string;
93 }
96 sub transform_msg2hash {
97 my ($msg) = @_ ;
98 my $hash = $xml->XMLin($msg, ForceArray=>1);
100 # xml tags without a content are created as an empty hash
101 # substitute it with an empty list
102 eval {
103 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
104 if( 1 == @{ $xml_content } ) {
105 # there is only one element in xml_content list ...
106 my $element = @{ $xml_content }[0];
107 if( ref($element) eq "HASH" ) {
108 # and this element is an hash ...
109 my $len_element = keys %{ $element };
110 if( $len_element == 0 ) {
111 # and this hash is empty, then substitute the xml_content
112 # with an empty string in list
113 $hash->{$xml_tag} = [ "none" ];
114 }
115 }
116 }
117 }
118 };
119 if( $@ ) {
120 $hash = undef;
121 }
123 return $hash;
124 }
127 #=== FUNCTION ================================================================
128 # NAME: add_content2xml_hash
129 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
130 # element - string - key for the hash
131 # content - string - value for the hash
132 # RETURNS: nothing
133 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
134 # then append value to list
135 #===============================================================================
136 sub add_content2xml_hash {
137 my ($xml_ref, $element, $content) = @_;
138 if(not exists $$xml_ref{$element} ) {
139 $$xml_ref{$element} = [];
140 }
141 my $tmp = $$xml_ref{$element};
142 push(@$tmp, $content);
143 return;
144 }
147 sub get_time {
148 my ($seconds, $minutes, $hours, $monthday, $month,
149 $year, $weekday, $yearday, $sommertime) = localtime(time);
150 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
151 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
152 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
153 $month+=1;
154 $month = $month < 10 ? $month = "0".$month : $month;
155 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
156 $year+=1900;
157 return "$year$month$monthday$hours$minutes$seconds";
159 }
162 #=== FUNCTION ================================================================
163 # NAME: build_msg
164 # DESCRIPTION: Send a message to a destination
165 # PARAMETERS: [header] Name of the header
166 # [from] sender ip
167 # [to] recipient ip
168 # [data] Hash containing additional attributes for the xml
169 # package
170 # RETURNS: nothing
171 #===============================================================================
172 sub build_msg ($$$$) {
173 my ($header, $from, $to, $data) = @_;
175 my $out_hash = &create_xml_hash($header, $from, $to);
177 while ( my ($key, $value) = each(%$data) ) {
178 if(ref($value) eq 'ARRAY'){
179 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
180 } else {
181 &add_content2xml_hash($out_hash, $key, $value);
182 }
183 }
184 my $out_msg = &create_xml_string($out_hash);
185 return $out_msg;
186 }
189 sub db_res2xml {
190 my ($db_res) = @_ ;
191 my $xml = "";
193 my $len_db_res= keys %{$db_res};
194 for( my $i= 1; $i<= $len_db_res; $i++ ) {
195 $xml .= "\n<answer$i>";
196 my $hash= $db_res->{$i};
197 while ( my ($column_name, $column_value) = each %{$hash} ) {
198 $xml .= "<$column_name>";
199 my $xml_content;
200 if( $column_name eq "xmlmessage" ) {
201 $xml_content = &encode_base64($column_value);
202 } else {
203 $xml_content = $column_value;
204 }
205 $xml .= $xml_content;
206 $xml .= "</$column_name>";
207 }
208 $xml .= "</answer$i>";
210 }
212 return $xml;
213 }
216 sub db_res2si_msg {
217 my ($db_res, $header, $target, $source) = @_;
219 my $si_msg = "<xml>";
220 $si_msg .= "<header>$header</header>";
221 $si_msg .= "<source>$source</source>";
222 $si_msg .= "<target>$target</target>";
223 $si_msg .= &db_res2xml;
224 $si_msg .= "</xml>";
225 }
228 sub get_where_statement {
229 my ($msg, $msg_hash) = @_;
230 my $error= 0;
232 my $clause_str= "";
233 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
234 $error++;
235 }
237 if( $error == 0 ) {
238 my @clause_l;
239 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
240 foreach my $clause (@where) {
241 my $connector = $clause->{'connector'}[0];
242 if( not defined $connector ) { $connector = "AND"; }
243 $connector = uc($connector);
244 delete($clause->{'connector'});
246 my @phrase_l ;
247 foreach my $phrase (@{$clause->{'phrase'}}) {
248 my $operator = "=";
249 if( exists $phrase->{'operator'} ) {
250 my $op = $op_hash->{$phrase->{'operator'}[0]};
251 if( not defined $op ) {
252 &main::daemon_log("ERROR: Can not translate operator '$operator' in where ".
253 "statement to sql valid syntax. Please use 'eq', ".
254 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
255 &main::daemon_log($msg, 8);
256 $op = "=";
257 }
258 $operator = $op;
259 delete($phrase->{'operator'});
260 }
262 my @xml_tags = keys %{$phrase};
263 my $tag = $xml_tags[0];
264 my $val = $phrase->{$tag}[0];
265 push(@phrase_l, "$tag$operator'$val'");
266 }
267 my $clause_str .= join(" $connector ", @phrase_l);
268 push(@clause_l, $clause_str);
269 }
271 if( not 0 == @clause_l ) {
272 $clause_str = join(" AND ", @clause_l);
273 $clause_str = "WHERE ($clause_str) ";
274 }
275 }
277 return $clause_str;
278 }
280 sub get_select_statement {
281 my ($msg, $msg_hash)= @_;
282 my $select = "*";
283 if( exists $msg_hash->{'select'} ) {
284 my $select_l = \@{$msg_hash->{'select'}};
285 $select = join(' AND ', @{$select_l});
286 }
287 return $select;
288 }
291 sub get_update_statement {
292 my ($msg, $msg_hash) = @_;
293 my $error= 0;
294 my $update_str= "";
295 my @update_l;
297 if( not exists $msg_hash->{'update'} ) { $error++; };
299 if( $error == 0 ) {
300 my $update= @{$msg_hash->{'update'}}[0];
301 while( my ($tag, $val) = each %{$update} ) {
302 my $val= @{$update->{$tag}}[0];
303 push(@update_l, "$tag='$val'");
304 }
305 if( 0 == @update_l ) { $error++; };
306 }
308 if( $error == 0 ) {
309 $update_str= join(', ', @update_l);
310 $update_str= "SET $update_str ";
311 }
313 return $update_str;
314 }
316 sub get_limit_statement {
317 my ($msg, $msg_hash)= @_;
318 my $error= 0;
319 my $limit_str = "";
320 my ($from, $to);
322 if( not exists $msg_hash->{'limit'} ) { $error++; };
324 if( $error == 0 ) {
325 eval {
326 my $limit= @{$msg_hash->{'limit'}}[0];
327 $from= @{$limit->{'from'}}[0];
328 $to= @{$limit->{'to'}}[0];
329 };
330 if( $@ ) {
331 $error++;
332 }
333 }
335 if( $error == 0 ) {
336 $limit_str= "LIMIT $from, $to";
337 }
339 return $limit_str;
340 }
342 sub get_orderby_statement {
343 my ($msg, $msg_hash)= @_;
344 my $error= 0;
345 my $order_str= "";
346 my $order;
348 if( not exists $msg_hash->{'orderby'} ) { $error++; };
350 if( $error == 0) {
351 eval {
352 $order= @{$msg_hash->{'orderby'}}[0];
353 };
354 if( $@ ) {
355 $error++;
356 }
357 }
359 if( $error == 0 ) {
360 $order_str= "ORDER BY $order";
361 }
363 return $order_str;
364 }
366 sub get_dns_domains() {
367 my $line;
368 my @searches;
369 open(RESOLV, "</etc/resolv.conf") or return @searches;
370 while(<RESOLV>){
371 $line= $_;
372 chomp $line;
373 $line =~ s/^\s+//;
374 $line =~ s/\s+$//;
375 $line =~ s/\s+/ /;
376 if ($line =~ /^domain (.*)$/ ){
377 push(@searches, $1);
378 } elsif ($line =~ /^search (.*)$/ ){
379 push(@searches, split(/ /, $1));
380 }
381 }
382 close(RESOLV);
384 my %tmp = map { $_ => 1 } @searches;
385 @searches = sort keys %tmp;
387 return @searches;
388 }
391 sub get_logged_in_users {
392 my $result = qx(/usr/bin/w -hs);
393 my @res_lines;
395 if( defined $result ) {
396 chomp($result);
397 @res_lines = split("\n", $result);
398 }
400 my @logged_in_user_list;
401 foreach my $line (@res_lines) {
402 chomp($line);
403 my @line_parts = split(/\s+/, $line);
404 push(@logged_in_user_list, $line_parts[0]);
405 }
407 return @logged_in_user_list;
409 }
411 1;