86895caeb87fec22785faef0ee863a23322b0af9
1 package GosaPackages;
3 use Exporter;
4 @ISA = ("Exporter");
6 use strict;
7 use warnings;
8 use GOSA::GosaSupportDaemon;
9 use IO::Socket::INET;
10 use XML::Simple;
11 use File::Spec;
12 use Data::Dumper;
13 use GOSA::DBsqlite;
14 use MIME::Base64;
16 BEGIN{}
17 END{}
19 my ($server_activ, $server_ip, $server_mac_address, $server_port, $server_passwd, $max_clients, $server_event_dir);
20 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
21 my ($gosa_activ, $gosa_ip, $gosa_mac_address, $gosa_port, $gosa_passwd, $network_interface);
22 my ($job_queue_timeout, $job_queue_file_name);
24 my $gosa_server;
26 my %cfg_defaults =
27 ("general" =>
28 {"job_queue_file_name" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'],
29 },
30 "server" =>
31 {"server_activ" => [\$server_activ, "on"],
32 "server_ip" => [\$server_ip, "0.0.0.0"],
33 "server_port" => [\$server_port, "20081"],
34 "server_passwd" => [\$server_passwd, ""],
35 "max_clients" => [\$max_clients, 100],
36 "server_event_dir" => [\$server_event_dir, '/usr/lib/gosa-si/server/events'],
37 },
38 "bus" =>
39 {"bus_activ" => [\$bus_activ, "on"],
40 "bus_passwd" => [\$bus_passwd, ""],
41 "bus_ip" => [\$bus_ip, "0.0.0.0"],
42 "bus_port" => [\$bus_port, "20080"],
43 },
44 "gosa" =>
45 {"gosa_activ" => [\$gosa_activ, "on"],
46 "gosa_ip" => [\$gosa_ip, "0.0.0.0"],
47 "gosa_port" => [\$gosa_port, "20082"],
48 "gosa_passwd" => [\$gosa_passwd, "none"],
49 },
50 );
53 ## START ##########################
55 # read configfile and import variables
56 &read_configfile();
57 $network_interface= &get_interface_for_ip($server_ip);
58 $gosa_mac_address= &get_mac($network_interface);
60 # complete addresses
61 my $server_address = "$server_ip:$server_port";
62 my $bus_address = "$bus_ip:$bus_port";
63 my $gosa_address = "$gosa_ip:$gosa_port";
65 # create general settings for this module
66 my $gosa_cipher = &create_ciphering($gosa_passwd);
67 my $xml = new XML::Simple();
69 # open gosa socket
70 if ($gosa_activ eq "on") {
71 &main::daemon_log(" ",1);
72 $gosa_server = IO::Socket::INET->new(LocalPort => $gosa_port,
73 Type => SOCK_STREAM,
74 Reuse => 1,
75 Listen => 1,
76 );
77 if (not defined $gosa_server) {
78 &main::daemon_log("cannot start tcp server at $gosa_port for communication to gosa: $@", 1);
79 die;
80 } else {
81 &main::daemon_log("start server for communication to gosa: $gosa_address", 1);
83 }
84 }
86 # create gosa job queue as a SQLite DB
87 my $table_name = "jobs";
88 my $sqlite = GOSA::DBsqlite->new($job_queue_file_name);
93 ## FUNCTIONS #################################################################
95 sub get_module_info {
96 my @info = ($gosa_address,
97 $gosa_passwd,
98 $gosa_server,
99 $gosa_activ,
100 "socket",
101 );
102 return \@info;
103 }
106 #=== FUNCTION ================================================================
107 # NAME: read_configfile
108 # PARAMETERS: cfg_file - string -
109 # RETURNS: nothing
110 # DESCRIPTION: read cfg_file and set variables
111 #===============================================================================
112 sub read_configfile {
113 my $cfg;
114 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
115 if( -r $main::cfg_file ) {
116 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
117 } else {
118 print STDERR "Couldn't read config file!";
119 }
120 } else {
121 $cfg = Config::IniFiles->new() ;
122 }
123 foreach my $section (keys %cfg_defaults) {
124 foreach my $param (keys %{$cfg_defaults{ $section }}) {
125 my $pinfo = $cfg_defaults{ $section }{ $param };
126 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
127 }
128 }
129 }
131 #=== FUNCTION ================================================================
132 # NAME: get_interface_for_ip
133 # PARAMETERS: ip address (i.e. 192.168.0.1)
134 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
135 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
136 #===============================================================================
137 sub get_interface_for_ip {
138 my $result;
139 my $ip= shift;
140 if ($ip && length($ip) > 0) {
141 my @ifs= &get_interfaces();
142 if($ip eq "0.0.0.0") {
143 $result = "all";
144 } else {
145 foreach (@ifs) {
146 my $if=$_;
147 if(get_ip($if) eq $ip) {
148 $result = $if;
149 }
150 }
151 }
152 }
153 return $result;
154 }
156 #=== FUNCTION ================================================================
157 # NAME: get_interfaces
158 # PARAMETERS: none
159 # RETURNS: (list of interfaces)
160 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
161 #===============================================================================
162 sub get_interfaces {
163 my @result;
164 my $PROC_NET_DEV= ('/proc/net/dev');
166 open(PROC_NET_DEV, "<$PROC_NET_DEV")
167 or die "Could not open $PROC_NET_DEV";
169 my @ifs = <PROC_NET_DEV>;
171 close(PROC_NET_DEV);
173 # Eat first two line
174 shift @ifs;
175 shift @ifs;
177 chomp @ifs;
178 foreach my $line(@ifs) {
179 my $if= (split /:/, $line)[0];
180 $if =~ s/^\s+//;
181 push @result, $if;
182 }
184 return @result;
185 }
187 #=== FUNCTION ================================================================
188 # NAME: get_mac
189 # PARAMETERS: interface name (i.e. eth0)
190 # RETURNS: (mac address)
191 # DESCRIPTION: Uses ioctl to get mac address directly from system.
192 #===============================================================================
193 sub get_mac {
194 my $ifreq= shift;
195 my $result;
196 if ($ifreq && length($ifreq) > 0) {
197 if($ifreq eq "all") {
198 $result = "00:00:00:00:00:00";
199 } else {
200 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
202 # A configured MAC Address should always override a guessed value
203 if ($gosa_mac_address and length($gosa_mac_address) > 0) {
204 $result= $gosa_mac_address;
205 }
207 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
208 or die "socket: $!";
210 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
211 my ($if, $mac)= unpack 'h36 H12', $ifreq;
213 if (length($mac) > 0) {
214 $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
215 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
216 $result = $mac;
217 }
218 }
219 }
220 }
221 return $result;
222 }
224 #=== FUNCTION ================================================================
225 # NAME: get_ip
226 # PARAMETERS: interface name (i.e. eth0)
227 # RETURNS: (ip address)
228 # DESCRIPTION: Uses ioctl to get ip address directly from system.
229 #===============================================================================
230 sub get_ip {
231 my $ifreq= shift;
232 my $result= "";
233 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
234 my $proto= getprotobyname('ip');
236 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
237 or die "socket: $!";
239 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
240 my ($if, $sin) = unpack 'a16 a16', $ifreq;
241 my ($port, $addr) = sockaddr_in $sin;
242 my $ip = inet_ntoa $addr;
244 if ($ip && length($ip) > 0) {
245 $result = $ip;
246 }
247 }
249 return $result;
250 }
252 #=== FUNCTION ================================================================
253 # NAME: open_socket
254 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
255 # [PeerPort] string necessary if port not appended by PeerAddr
256 # RETURNS: socket IO::Socket::INET
257 # DESCRIPTION: open a socket to PeerAddr
258 #===============================================================================
259 sub open_socket {
260 my ($PeerAddr, $PeerPort) = @_ ;
261 if(defined($PeerPort)){
262 $PeerAddr = $PeerAddr.":".$PeerPort;
263 }
264 my $socket;
265 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
266 Porto => "tcp" ,
267 Type => SOCK_STREAM,
268 Timeout => 5,
269 );
270 if(not defined $socket) {
271 return;
272 }
273 &main::daemon_log("open_socket to: $PeerAddr", 7);
274 return $socket;
275 }
278 #=== FUNCTION ================================================================
279 # NAME: process_incoming_msg
280 # PARAMETERS: crypted_msg - string - incoming crypted message
281 # RETURNS: nothing
282 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
283 #===============================================================================
284 sub process_incoming_msg {
285 my ($crypted_msg) = @_ ;
286 &main::daemon_log("Got message $crypted_msg", 5);
287 if( (not(defined($crypted_msg))) || (length($crypted_msg) <= 0)) {
288 &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
289 return;
290 }
292 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
293 $crypted_msg = $1;
294 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
296 # collect addresses from possible incoming clients
297 # only gosa is allowd as incoming client
298 &main::daemon_log("GosaPackages: host_key: $host", 7);
299 &main::daemon_log("GosaPackages: key_passwd: $gosa_passwd", 7);
301 $gosa_cipher = &create_ciphering($gosa_passwd);
303 # determine the correct passwd for deciphering of the incoming msgs
304 my $msg = "";
305 my $msg_hash;
306 eval{
307 $msg = &decrypt_msg($crypted_msg, $gosa_cipher);
308 &main::daemon_log("GosaPackages: decrypted_msg: \n$msg", 7);
310 $msg_hash = $xml->XMLin($msg, ForceArray=>1);
311 };
312 if($@) {
313 &main::daemon_log("WARNING: GosaPackages do not understand the message:", 5);
314 &main::daemon_log("$@", 7);
315 return;
316 }
318 my $header = @{$msg_hash->{header}}[0];
320 &main::daemon_log("GosaPackages: receive '$header' from $host", 1);
322 my $out_msg;
323 if ($header =~ /^job_/) {
324 $out_msg = &process_job_msg($msg, $msg_hash);
325 } elsif ($header =~ /^gosa_/) {
326 $out_msg = &process_gosa_msg($msg, $header);
327 } else {
328 &main::daemon_log("ERROR: $header is not a valid GosaPackage-header, need a 'job_' or a 'gosa_' prefix");
329 }
331 if (not defined $out_msg) {
332 return;
333 }
335 if ($out_msg =~ /<jobdb_id>(\d*?)<\/jobdb_id>/) {
336 my $job_id = $1;
337 my $sql = "BEGIN TRANSATION; UPDATE '".$main::job_queue_table_name.
338 "' SET status='done', result='".$out_msg.
339 "' WHERE id='$job_id'; COMMIT;";
340 my $res = $main::job_db->exec_statement($sql);
341 return;
343 } else {
345 my $out_cipher = &create_ciphering($gosa_passwd);
346 $out_msg = &encrypt_msg($out_msg, $out_cipher);
347 return $out_msg;
348 }
350 }
352 sub process_gosa_msg {
353 my ($msg, $header) = @_ ;
354 my $out_msg;
355 $header =~ s/gosa_//;
357 # decide wether msg is a core function or a event handler
358 if ( $header eq 'query_jobdb') { $out_msg = &query_jobdb }
359 elsif ($header eq 'delete_jobdb_entry') { $out_msg = &delete_jobdb_entry }
360 elsif ($header eq 'clear_jobdb') { $out_msg = &clear_jobdb }
361 elsif ($header eq 'update_status_jobdb_entry' ) { $out_msg = &update_status_jobdb_entry }
362 elsif ($header eq 'update_timestamp_jobdb_entry' ) { $out_msg = &update_timestamp_jobdb_entry }
363 else {
364 # msg could not be assigned to core function
365 # fetch all available eventhandler under $server_event_dir
366 opendir (DIR, $server_event_dir) or &main::daemon_log("ERROR cannot open $server_event_dir: $!\n", 1) and return;
367 while (defined (my $file = readdir (DIR))) {
368 if (not $file eq $header) {
369 next;
370 }
371 # try to deliver incoming msg to eventhandler
372 my $cmd = File::Spec->join($server_event_dir, $header)." '$msg'";
373 &main::daemon_log("GosaPackages: execute event_handler $header", 3);
374 &main::daemon_log("GosaPackages: cmd: $cmd", 7);
376 $out_msg = "";
377 open(PIPE, "$cmd 2>&1 |");
378 while(<PIPE>) {
379 $out_msg.=$_;
380 }
381 close(PIPE);
382 &main::daemon_log("GosaPackages: answer of cmd: $out_msg", 5);
383 last;
384 }
385 }
387 # if delivery not possible raise error and return
388 if (not defined $out_msg) {
389 &main::daemon_log("ERROR: GosaPackages: no event handler or core function defined for $header", 1);
390 } elsif ($out_msg eq "") {
391 &main::daemon_log("ERROR: GosaPackages got not answer from event_handler $header", 1);
392 }
393 return $out_msg;
395 }
398 sub process_job_msg {
399 my ($msg, $msg_hash)= @_ ;
401 my $header = @{$msg_hash->{header}}[0];
402 $header =~ s/job_//;
404 # check wether mac address is already known in known_daemons or known_clients
405 my $target = 'none';
407 # add job to job queue
408 my $func_dic = {table=>$main::job_queue_table_name,
409 primkey=>'id',
410 timestamp=>@{$msg_hash->{timestamp}}[0],
411 status=>'waiting',
412 result=>'none',
413 headertag=>$header,
414 targettag=>$target,
415 xmlmessage=>$msg,
416 macaddress=>@{$msg_hash->{mac}}[0],
417 };
418 my $res = $main::job_db->add_dbentry($func_dic);
419 if (not $res == 0) {
420 &main::daemon_log("ERROR: GosaPackages: process_job_msg: $res", 1);
421 }
423 &main::daemon_log("GosaPackages: $header job successfully added to job queue", 3);
424 return "<xml><1>$res</1></xml>";
426 }
429 sub db_res_2_xml {
430 my ($db_res) = @_ ;
432 my $xml = "<xml>";
434 while ( my ($hit, $hash) = each %{ $db_res } ) {
435 $xml .= "\n<answer$hit>";
437 while ( my ($column_name, $column_value) = each %{$hash} ) {
438 $xml .= "<$column_name>";
439 my $xml_content;
440 if( $column_name eq "xmlmessage" ) {
441 $xml_content = &encode_base64($column_value);
442 } else {
443 $xml_content = $column_value;
444 }
445 $xml .= $xml_content;
446 $xml .= "</$column_name>";
447 }
449 $xml .= "</answer$hit>";
450 }
452 $xml .= "</xml>";
453 return $xml;
454 }
457 ## CORE FUNCTIONS ############################################################
459 sub query_jobdb {
460 my ($msg) = @_;
461 my $msg_hash = &transform_msg2hash($msg);
463 # prepare query sql statement
464 my @where = @{$msg_hash->{where}};
465 my $where_hash = {table=>$main::job_queue_table_name };
466 foreach my $where_pram (@where) {
467 my $where_val = @{$msg_hash->{$where_pram}}[0];
468 if (defined $where_val) {
469 $where_hash->{$where_pram} = $where_val;
470 }
471 }
473 # execute db query
474 my $res_hash = $main::job_db->select_dbentry($where_hash);
476 my $out_xml = &db_res_2_xml($res_hash);
477 return $out_xml;
478 }
480 sub delete_jobdb_entry {
481 my ($msg) = @_ ;
482 my $msg_hash = &transform_msg2hash($msg);
484 # prepare query sql statement
485 my @where = @{$msg_hash->{where}};
486 my $where_hash = {table=>$main::job_queue_table_name };
487 foreach my $where_pram (@where) {
488 my $where_val = @{$msg_hash->{$where_pram}}[0];
489 if (defined $where_val) {
490 $where_hash->{$where_pram} = $where_val;
491 }
492 }
494 # execute db query
495 my $db_res = $main::job_db->del_dbentry($where_hash);
497 my $res;
498 if( $db_res > 0 ) {
499 $res = 0 ;
500 } else {
501 $res = 1;
502 }
504 # prepare xml answer
505 my $out_xml = "<xml><answer1>$res</answer1></xml>";
506 return $out_xml;
508 }
510 sub clear_jobdb {
511 my ($msg) = @_ ;
512 my $msg_hash = &transform_msg2hash($msg);
514 my $where_hash = {table=>$main::job_queue_table_name };
516 # execute db query
517 my $db_res = $main::job_db->del_dbentry($where_hash);
518 print STDERR "db_res=$db_res\n";
519 my $res;
520 if( $db_res eq '0E0' ) {
521 $res = 0 ;
522 } else {
523 $res = 1;
524 }
526 # prepare xml answer
527 my $out_xml = "<xml><answer1>$res</answer1></xml>";
528 return $out_xml;
529 }
531 sub update_status_jobdb_entry {
532 my ($msg) = @_ ;
533 my $msg_hash = &transform_msg2hash($msg);
535 # prepare query sql statement
536 my $update_hash = {table=>$main::job_queue_table_name };
537 if( exists $msg_hash->{where} ) {
538 $update_hash->{where} = $msg_hash->{where};
539 } else {
540 $update_hash->{where} = [];
541 }
543 if( not exists $msg_hash->{update}[0]->{status} ) {
544 return "<xml><answer1>1</answer1></xml>";
545 }
546 $update_hash->{update} = [ { status=>$msg_hash->{update}[0]->{status} } ];
548 # execute db query
549 my $db_res = $main::job_db->update_dbentry($update_hash);
551 # transform db answer to error returnment
552 my $res;
553 if( $db_res > 0 ) {
554 $res = 0 ;
555 } else {
556 $res = 1;
557 }
559 # prepare xml answer
560 my $out_xml = "<xml><answer1>$res</answer1></xml>";
561 return $out_xml;
562 }
564 sub update_timestamp_jobdb_entry {
565 my ($msg) = @_ ;
566 my $msg_hash = &transform_msg2hash($msg);
568 # prepare query sql statement
569 my $update_hash = {table=>$main::job_queue_table_name };
570 if( exists $msg_hash->{where} ) {
571 $update_hash->{where} = $msg_hash->{where};
572 } else {
573 $update_hash->{where} = [];
574 }
576 if( not exists $msg_hash->{update}[0]->{timestamp} ) {
577 return "<xml><answer1>1</answer1></xml>";
578 }
580 $update_hash->{update} = [ { timestamp=>$msg_hash->{update}[0]->{timestamp} } ];
582 # execute db query
583 my $db_res = $main::job_db->update_dbentry($update_hash);
585 # transform db answer to error returnment
586 my $res;
587 if( $db_res > 0 ) {
588 $res = 0 ;
589 } else {
590 $res = 1;
591 }
593 # prepare xml answer
594 my $out_xml = "<xml><answer1>$res</answer1></xml>";
595 return $out_xml;
597 }
600 1;