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;
31 use Data::Dumper;
33 my $op_hash = {
34 'eq' => '=',
35 'ne' => '!=',
36 'ge' => '>=',
37 'gt' => '>',
38 'le' => '<=',
39 'lt' => '<',
40 'like' => ' LIKE ',
41 };
44 BEGIN {}
46 END {}
48 ### Start ######################################################################
50 my $xml = new XML::Simple();
52 sub daemon_log {
53 my ($msg, $level) = @_ ;
54 &main::daemon_log($msg, $level);
55 return;
56 }
61 #=== FUNCTION ================================================================
62 # NAME: create_xml_hash
63 # PARAMETERS: header - string - message header (required)
64 # source - string - where the message come from (required)
65 # target - string - where the message should go to (required)
66 # [header_value] - string - something usefull (optional)
67 # RETURNS: hash - hash - nomen est omen
68 # DESCRIPTION: creates a key-value hash, all values are stored in a array
69 #===============================================================================
70 sub create_xml_hash {
71 my ($header, $source, $target, $header_value) = @_;
72 my $hash = {
73 header => [$header],
74 source => [$source],
75 target => [$target],
76 $header => [$header_value],
77 };
78 return $hash
79 }
82 #=== FUNCTION ================================================================
83 # NAME: create_xml_string
84 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
85 # RETURNS: xml_string - string - xml string representation of the hash
86 # DESCRIPTION: transform the hash to a string using XML::Simple module
87 #===============================================================================
88 sub create_xml_string {
89 my ($xml_hash) = @_ ;
90 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
91 #$xml_string =~ s/[\n]+//g;
92 #daemon_log("create_xml_string:",7);
93 #daemon_log("$xml_string\n", 7);
94 return $xml_string;
95 }
98 sub transform_msg2hash {
99 my ($msg) = @_ ;
100 my $hash = $xml->XMLin($msg, ForceArray=>1);
102 # xml tags without a content are created as an empty hash
103 # substitute it with an empty list
104 eval {
105 while( my ($xml_tag, $xml_content) = each %{ $hash } ) {
106 if( 1 == @{ $xml_content } ) {
107 # there is only one element in xml_content list ...
108 my $element = @{ $xml_content }[0];
109 if( ref($element) eq "HASH" ) {
110 # and this element is an hash ...
111 my $len_element = keys %{ $element };
112 if( $len_element == 0 ) {
113 # and this hash is empty, then substitute the xml_content
114 # with an empty string in list
115 $hash->{$xml_tag} = [ "none" ];
116 }
117 }
118 }
119 }
120 };
121 if( $@ ) {
122 $hash = undef;
123 }
125 return $hash;
126 }
129 #=== FUNCTION ================================================================
130 # NAME: add_content2xml_hash
131 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
132 # element - string - key for the hash
133 # content - string - value for the hash
134 # RETURNS: nothing
135 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists,
136 # then append value to list
137 #===============================================================================
138 sub add_content2xml_hash {
139 my ($xml_ref, $element, $content) = @_;
140 if(not exists $$xml_ref{$element} ) {
141 $$xml_ref{$element} = [];
142 }
143 my $tmp = $$xml_ref{$element};
144 push(@$tmp, $content);
145 return;
146 }
149 sub get_time {
150 my ($seconds, $minutes, $hours, $monthday, $month,
151 $year, $weekday, $yearday, $sommertime) = localtime(time);
152 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
153 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
154 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
155 $month+=1;
156 $month = $month < 10 ? $month = "0".$month : $month;
157 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
158 $year+=1900;
159 return "$year$month$monthday$hours$minutes$seconds";
161 }
164 #=== FUNCTION ================================================================
165 # NAME: build_msg
166 # DESCRIPTION: Send a message to a destination
167 # PARAMETERS: [header] Name of the header
168 # [from] sender ip
169 # [to] recipient ip
170 # [data] Hash containing additional attributes for the xml
171 # package
172 # RETURNS: nothing
173 #===============================================================================
174 sub build_msg ($$$$) {
175 my ($header, $from, $to, $data) = @_;
177 my $out_hash = &create_xml_hash($header, $from, $to);
179 while ( my ($key, $value) = each(%$data) ) {
180 if(ref($value) eq 'ARRAY'){
181 map(&add_content2xml_hash($out_hash, $key, $_), @$value);
182 } else {
183 &add_content2xml_hash($out_hash, $key, $value);
184 }
185 }
186 my $out_msg = &create_xml_string($out_hash);
187 return $out_msg;
188 }
191 sub db_res2xml {
192 my ($db_res) = @_ ;
193 my $xml = "";
195 my $len_db_res= keys %{$db_res};
196 for( my $i= 1; $i<= $len_db_res; $i++ ) {
197 $xml .= "\n<answer$i>";
198 my $hash= $db_res->{$i};
199 while ( my ($column_name, $column_value) = each %{$hash} ) {
200 $xml .= "<$column_name>";
201 my $xml_content;
202 if( $column_name eq "xmlmessage" ) {
203 $xml_content = &encode_base64($column_value);
204 } else {
205 $xml_content = $column_value;
206 }
207 $xml .= $xml_content;
208 $xml .= "</$column_name>";
209 }
210 $xml .= "</answer$i>";
212 }
214 return $xml;
215 }
218 sub db_res2si_msg {
219 my ($db_res, $header, $target, $source) = @_;
221 my $si_msg = "<xml>";
222 $si_msg .= "<header>$header</header>";
223 $si_msg .= "<source>$source</source>";
224 $si_msg .= "<target>$target</target>";
225 $si_msg .= &db_res2xml;
226 $si_msg .= "</xml>";
227 }
230 sub get_where_statement {
231 my ($msg, $msg_hash) = @_;
232 my $error= 0;
234 my $clause_str= "";
235 if( (not exists $msg_hash->{'where'}) || (not exists @{$msg_hash->{'where'}}[0]->{'clause'}) ) {
236 $error++;
237 }
239 if( $error == 0 ) {
240 my @clause_l;
241 my @where = @{@{$msg_hash->{'where'}}[0]->{'clause'}};
242 foreach my $clause (@where) {
243 my $connector = $clause->{'connector'}[0];
244 if( not defined $connector ) { $connector = "AND"; }
245 $connector = uc($connector);
246 delete($clause->{'connector'});
248 my @phrase_l ;
249 foreach my $phrase (@{$clause->{'phrase'}}) {
250 my $operator = "=";
251 if( exists $phrase->{'operator'} ) {
252 my $op = $op_hash->{$phrase->{'operator'}[0]};
253 if( not defined $op ) {
254 &main::daemon_log("ERROR: Can not translate operator '$operator' in where-".
255 "statement to sql valid syntax. Please use 'eq', ".
256 "'ne', 'ge', 'gt', 'le', 'lt' in xml message\n", 1);
257 &main::daemon_log($msg, 8);
258 $op = "=";
259 }
260 $operator = $op;
261 delete($phrase->{'operator'});
262 }
264 my @xml_tags = keys %{$phrase};
265 my $tag = $xml_tags[0];
266 my $val = $phrase->{$tag}[0];
267 # integer columns do not have to have single quotes besides the value
268 if ($tag eq "id") {
269 push(@phrase_l, "$tag$operator$val");
270 } else {
271 push(@phrase_l, "$tag$operator'$val'");
272 }
273 }
274 my $clause_str .= join(" $connector ", @phrase_l);
275 push(@clause_l, "($clause_str)");
276 }
278 if( not 0 == @clause_l ) {
279 $clause_str = join(" AND ", @clause_l);
280 $clause_str = "WHERE ($clause_str) ";
281 }
282 }
284 return $clause_str;
285 }
287 sub get_select_statement {
288 my ($msg, $msg_hash)= @_;
289 my $select = "*";
290 if( exists $msg_hash->{'select'} ) {
291 my $select_l = \@{$msg_hash->{'select'}};
292 $select = join(', ', @{$select_l});
293 }
294 return $select;
295 }
298 sub get_update_statement {
299 my ($msg, $msg_hash) = @_;
300 my $error= 0;
301 my $update_str= "";
302 my @update_l;
304 if( not exists $msg_hash->{'update'} ) { $error++; };
306 if( $error == 0 ) {
307 my $update= @{$msg_hash->{'update'}}[0];
308 while( my ($tag, $val) = each %{$update} ) {
309 my $val= @{$update->{$tag}}[0];
310 push(@update_l, "$tag='$val'");
311 }
312 if( 0 == @update_l ) { $error++; };
313 }
315 if( $error == 0 ) {
316 $update_str= join(', ', @update_l);
317 $update_str= "SET $update_str ";
318 }
320 return $update_str;
321 }
323 sub get_limit_statement {
324 my ($msg, $msg_hash)= @_;
325 my $error= 0;
326 my $limit_str = "";
327 my ($from, $to);
329 if( not exists $msg_hash->{'limit'} ) { $error++; };
331 if( $error == 0 ) {
332 eval {
333 my $limit= @{$msg_hash->{'limit'}}[0];
334 $from= @{$limit->{'from'}}[0];
335 $to= @{$limit->{'to'}}[0];
336 };
337 if( $@ ) {
338 $error++;
339 }
340 }
342 if( $error == 0 ) {
343 $limit_str= "LIMIT $from, $to";
344 }
346 return $limit_str;
347 }
349 sub get_orderby_statement {
350 my ($msg, $msg_hash)= @_;
351 my $error= 0;
352 my $order_str= "";
353 my $order;
355 if( not exists $msg_hash->{'orderby'} ) { $error++; };
357 if( $error == 0) {
358 eval {
359 $order= @{$msg_hash->{'orderby'}}[0];
360 };
361 if( $@ ) {
362 $error++;
363 }
364 }
366 if( $error == 0 ) {
367 $order_str= "ORDER BY $order";
368 }
370 return $order_str;
371 }
373 sub get_dns_domains() {
374 my $line;
375 my @searches;
376 open(RESOLV, "</etc/resolv.conf") or return @searches;
377 while(<RESOLV>){
378 $line= $_;
379 chomp $line;
380 $line =~ s/^\s+//;
381 $line =~ s/\s+$//;
382 $line =~ s/\s+/ /;
383 if ($line =~ /^domain (.*)$/ ){
384 push(@searches, $1);
385 } elsif ($line =~ /^search (.*)$/ ){
386 push(@searches, split(/ /, $1));
387 }
388 }
389 close(RESOLV);
391 my %tmp = map { $_ => 1 } @searches;
392 @searches = sort keys %tmp;
394 return @searches;
395 }
398 sub get_logged_in_users {
399 my $result = qx(/usr/bin/w -hs);
400 my @res_lines;
402 if( defined $result ) {
403 chomp($result);
404 @res_lines = split("\n", $result);
405 }
407 my @logged_in_user_list;
408 foreach my $line (@res_lines) {
409 chomp($line);
410 my @line_parts = split(/\s+/, $line);
411 push(@logged_in_user_list, $line_parts[0]);
412 }
414 return @logged_in_user_list;
416 }
418 1;