diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server
index 777e5feee91739845c8e2f4a6ac725e2668ab4f0..91a627c56dd2aaf2f599ef3ced5fd7c152565fc6 100755 (executable)
--- a/gosa-si/gosa-si-server
+++ b/gosa-si/gosa-si-server
use Sys::Syslog qw( :DEFAULT setlogsock);
use Cwd;
use File::Spec;
+use File::Basename;
use GOSA::DBsqlite;
+use GOSA::GosaSupportDaemon;
use POE qw(Component::Server::TCP);
my $modules_path = "/usr/lib/gosa-si/modules";
use lib "/usr/lib/gosa-si/modules";
my (%cfg_defaults, $foreground, $verbose, $ping_timeout);
-my ($bus, $msg_to_bus, $bus_cipher);
-my ($server, $server_mac_address, $server_events);
+my ($bus_activ, $bus, $msg_to_bus, $bus_cipher);
+my ($server, $server_mac_address);
my ($gosa_server, $job_queue_timeout, $job_queue_table_name, $job_queue_file_name,$job_queue_loop_delay);
my ($known_modules, $known_clients_file_name, $known_server_file_name);
-my ($max_clients);
my ($pid_file, $procid, $pid, $log_file);
-my (%free_child, %busy_child, $child_max, $child_min, %child_alive_time, $child_timeout);
-my ($arp_activ, $arp_fifo, $arp_fifo_path);
+my ($arp_activ, $arp_fifo);
my ($xml);
# variables declared in config file are always set to 'our'
our (%cfg_defaults, $log_file, $pid_file,
- $bus_activ, $bus_key, $bus_ip, $bus_port,
- $server_activ, $server_ip, $server_port, $SIPackages_key, $max_clients,
- $arp_activ, $arp_fifo_path,
- $gosa_activ, $GosaPackages_key, $gosa_ip, $gosa_port, $gosa_timeout,
+ $server_ip, $server_port, $SIPackages_key,
+ $arp_activ, $gosa_unit_tag,
+ $GosaPackages_key, $gosa_ip, $gosa_port, $gosa_timeout,
);
# additional variable which should be globaly accessable
$ping_timeout = 5;
$no_bus = 0;
+$bus_activ = "true";
$no_arp = 0;
-# name of table for storing gosa jobs
-our $job_queue_table_name = 'jobs';
+# holds all gosa jobs
our $job_db;
+our $job_queue_table_name = 'jobs';
# holds all other gosa-sd as well as the gosa-sd-bus
our $known_server_db;
# holds all registrated clients
our $known_clients_db;
+our $prg= basename($0);
-%cfg_defaults =
-("general" =>
- {"log_file" => [\$log_file, "/var/run/".$0.".log"],
- "pid_file" => [\$pid_file, "/var/run/".$0.".pid"],
- "child_max" => [\$child_max, 10],
- "child_min" => [\$child_min, 3],
- "child_timeout" => [\$child_timeout, 180],
- "job_queue_timeout" => [\$job_queue_timeout, undef],
- "job_queue_file_name" => [\$job_queue_file_name, '/var/lib/gosa-si/gosa-si-server_jobs.db'],
- "job_queue_loop_delay" => [\$job_queue_loop_delay, 3],
- "known_clients_file_name" => [\$known_clients_file_name, '/var/lib/gosa-si/gosa-si-server_known_clients.db' ],
- "known_server_file_name" => [\$known_server_file_name, '/var/lib/gosa-si/gosa-si-server_known_server.db'],
- },
-"bus" =>
- {"bus_activ" => [\$bus_activ, "on"],
- "bus_passwd" => [\$bus_key, ""],
- "bus_ip" => [\$bus_ip, "0.0.0.0"],
- "bus_port" => [\$bus_port, "20080"],
+%cfg_defaults = (
+"general" => {
+ "log-file" => [\$log_file, "/var/run/".$prg.".log"],
+ "pid-file" => [\$pid_file, "/var/run/".$prg.".pid"],
},
-"server" =>
- {"server_activ" => [\$server_activ, "on"],
- "server_ip" => [\$server_ip, "0.0.0.0"],
- "server_port" => [\$server_port, "20081"],
- "SIPackages_key" => [\$SIPackages_key, "none"],
- "max_clients" => [\$max_clients, 100],
+"bus" => {
+ "activ" => [\$bus_activ, "true"],
},
-"arp" =>
- {"arp_activ" => [\$arp_activ, "on"],
- "arp_fifo_path" => [\$arp_fifo_path, "/var/run/gosa-si/arp-notify"],
+"server" => {
+# "ip" => [\$server_ip, "0.0.0.0"],
+ "port" => [\$server_port, "20081"],
+ "known-clients" => [\$known_clients_file_name, '/var/lib/gosa-si/clients.db' ],
+ "known-servers" => [\$known_server_file_name, '/var/lib/gosa-si/servers.db'],
+ "gosa-unit-tag" => [\$gosa_unit_tag, ""],
},
-"gosa" =>
- {"gosa_activ" => [\$gosa_activ, "on"],
- "gosa_ip" => [\$gosa_ip, "0.0.0.0"],
- "gosa_port" => [\$gosa_port, "20082"],
- "GosaPackages_key" => [\$GosaPackages_key, "none"],
+"GOsaPackages" => {
+ "ip" => [\$gosa_ip, "0.0.0.0"],
+ "port" => [\$gosa_port, "20082"],
+ "job-queue" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'],
+ "job-queue-loop-delay" => [\$job_queue_loop_delay, 3],
+ "key" => [\$GosaPackages_key, "none"],
},
- );
+"SIPackages" => {
+ "key" => [\$SIPackages_key, "none"],
+ },
+);
#=== FUNCTION ================================================================
#===============================================================================
sub usage {
print STDERR << "EOF" ;
-usage: $0 [-hvf] [-c config]
+usage: $prg [-hvf] [-c config]
-h : this (help) message
-c <file> : config file
-f : foreground, process will not be forked to background
-v : be verbose (multiple to increase verbosity)
- -no-bus : starts $0 without connection to bus
- -no-arp : starts $0 without connection to arp module
+ -no-bus : starts $prg without connection to bus
+ -no-arp : starts $prg without connection to arp module
EOF
print "\n" ;
$month = $monthnames[$month];
$monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
$year+=1900;
- my $name = $0;
- $name =~ s/\.\///;
+ my $name = $prg;
my $log_msg = "$month $monthday $hours:$minutes:$seconds $name $msg\n";
print LOG_HANDLE $log_msg;
}
-sub get_time {
- my ($seconds, $minutes, $hours, $monthday, $month,
- $year, $weekday, $yearday, $sommertime) = localtime(time);
- $hours = $hours < 10 ? $hours = "0".$hours : $hours;
- $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
- $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
- $month+=1;
- $month = $month < 10 ? $month = "0".$month : $month;
- $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
- $year+=1900;
- return "$year$month$monthday$hours$minutes$seconds";
-
-}
-
-
#=== FUNCTION ================================================================
# NAME: check_cmdline_param
# PARAMETERS: nothing
sub check_key_and_xml_validity {
my ($crypted_msg, $module_key) = @_;
-#print STDERR "crypted_msg:$crypted_msg\n";
-#print STDERR "modul_key:$module_key\n";
-
my $msg;
my $msg_hash;
+ my $error_string;
eval{
$msg = &decrypt_msg($crypted_msg, $module_key);
- &main::daemon_log("decrypted_msg: \n$msg", 8);
+ if ($msg =~ /<xml>/i){
+ &main::daemon_log("decrypted_msg: \n$msg", 8);
+ $msg_hash = $xml->XMLin($msg, ForceArray=>1);
+
+ ##############
+ # check header
+ if( not exists $msg_hash->{'header'} ) { die "no header specified"; }
+ my $header_l = $msg_hash->{'header'};
+ if( 1 > @{$header_l} ) { die 'empty header tag'; }
+ if( 1 < @{$header_l} ) { die 'more than one header specified'; }
+ my $header = @{$header_l}[0];
+ if( 0 == length $header) { die 'empty string in header tag'; }
+
+ ##############
+ # check source
+ if( not exists $msg_hash->{'source'} ) { die "no source specified"; }
+ my $source_l = $msg_hash->{'source'};
+ if( 1 > @{$source_l} ) { die 'empty source tag'; }
+ if( 1 < @{$source_l} ) { die 'more than one source specified'; }
+ my $source = @{$source_l}[0];
+ if( 0 == length $source) { die 'source error'; }
+
+ ##############
+ # check target
+ if( not exists $msg_hash->{'target'} ) { die "no target specified"; }
+ my $target_l = $msg_hash->{'target'};
+ if( 1 > @{$target_l} ) { die 'empty target tag'; }
+ }
+ };
+ if($@) {
+ &main::daemon_log("WARNING: do not understand the message", 5);
+ &main::daemon_log("$@", 8);
+ $msg = undef;
+ $msg_hash = undef;
+ }
+
+ return ($msg, $msg_hash);
+}
+
+
+sub check_outgoing_xml_validity {
+ my ($msg) = @_;
+
+ my $msg_hash;
+ eval{
$msg_hash = $xml->XMLin($msg, ForceArray=>1);
+ ##############
# check header
my $header_l = $msg_hash->{'header'};
if( 1 != @{$header_l} ) {
- die'header error';
+ die 'no or more than one headers specified';
}
my $header = @{$header_l}[0];
if( 0 == length $header) {
- die 'header error';
+ die 'header has length 0';
}
+ ##############
# check source
my $source_l = $msg_hash->{'source'};
if( 1 != @{$source_l} ) {
- die'source error';
+ die 'no or more than 1 sources specified';
}
my $source = @{$source_l}[0];
if( 0 == length $source) {
- die 'source error';
+ die 'source has length 0';
}
-
- # check target
+ unless( $source =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/ ||
+ $source =~ /^GOSA$/i ) {
+ die "source '$source' is neither a complete ip-address with port nor 'GOSA'";
+ }
+
+ ##############
+ # check target
my $target_l = $msg_hash->{'target'};
- if( 1 != @{$target_l} ) {
- die'target error';
+ if( 0 == @{$target_l} ) {
+ die "no targets specified";
}
- my $target = @{$target_l}[0];
- if( 0 == length $target) {
- die 'target error';
+ foreach my $target (@$target_l) {
+ if( 0 == length $target) {
+ die "target has length 0";
+ }
+ unless( $target =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$/ ||
+ $target =~ /^GOSA$/i ||
+ $target =~ /^\*$/ ||
+ $target =~ /KNOWN_SERVER/i ||
+ $target =~ /JOBDB/i ||
+ $target =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ){
+ die "target '$target' is not a complete ip-address with port or a valid target name or a mac-address";
+ }
}
-
};
if($@) {
- &main::daemon_log("WARNING: do not understand the message:", 5);
- &main::daemon_log("$@", 8);
+ daemon_log("WARNING: outgoing msg is not gosa-si envelope conform", 5);
+ daemon_log("$@ ".(defined($msg) && length($msg)>0)?$msg:"Empty Message", 8);
+ $msg_hash = undef;
}
- return ($msg, $msg_hash);
+ return ($msg_hash);
}
while( my ($hit_num, $hit) = each %{ $query_res } ) {
my $host_name = $hit->{hostname};
if( not $host_name =~ /^$remote_ip:\d*$/) {
- next;
+ next;
}
my $host_key = $hit->{hostkey};
&daemon_log("SIPackages: known_client host_name: $host_name", 7);
no strict "refs";
my ($input) = @_ ;
my ($msg, $msg_hash, $module);
+ my $error_string;
my %act_modules = %$known_modules;
while( my ($mod, $info) = each(%act_modules)) {
+
# check a key exists for this module
my $module_key = ${$mod."_key"};
- if( ! $module_key ) {
+ if( not defined $module_key ) {
+ if( $mod eq 'ArpHandler' ) {
+ next;
+ }
daemon_log("ERROR: no key specified in config file for $mod", 1);
next;
}
# check if module can open msg envelope with module key
($msg, $msg_hash) = &check_key_and_xml_validity($input, $module_key);
- if( (!$msg) || (!$msg_hash) ) {
- daemon_log("$mod: deciphering failed", 5);
+ if( (not defined $msg) || (not defined $msg_hash) ) {
next;
}
else {
}
if( (!$msg) || (!$msg_hash) || (!$module)) {
- daemon_log("Incoming message is not from a unknown host", 3);
+ daemon_log("Incoming message is not from an unknown host", 5);
}
return ($msg, $msg_hash, $module);
}
+
sub create_ciphering {
my ($passwd) = @_;
+ if((!defined($passwd)) || length($passwd)==0) {
+ $passwd = "";
+ }
$passwd = substr(md5_hex("$passwd") x 32, 0, 32);
my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
sub decrypt_msg {
+
my ($msg, $key) = @_ ;
$msg = &decode_base64($msg);
my $my_cipher = &create_ciphering($key);
my $error = 0;
# target can be in known_server
- if( !$encrypt_key ) {
+ if( not defined $encrypt_key ) {
my $sql_statement= "SELECT * FROM known_server WHERE hostname='$target'";
my $query_res = $known_server_db->select_dbentry( $sql_statement );
while( my ($hit_num, $hit) = each %{ $query_res } ) {
last;
}
}
-
# target can be in known_client
- if( !$encrypt_key ) {
+ if( not defined $encrypt_key ) {
my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$target'";
my $query_res = $known_clients_db->select_dbentry( $sql_statement );
while( my ($hit_num, $hit) = each %{ $query_res } ) {
}
+#=== FUNCTION ================================================================
+# NAME: get_ip
+# PARAMETERS: interface name (i.e. eth0)
+# RETURNS: (ip address)
+# DESCRIPTION: Uses ioctl to get ip address directly from system.
+#===============================================================================
+sub get_ip {
+ my $ifreq= shift;
+ my $result= "";
+ my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
+ my $proto= getprotobyname('ip');
+
+ socket SOCKET, PF_INET, SOCK_DGRAM, $proto
+ or die "socket: $!";
+
+ if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
+ my ($if, $sin) = unpack 'a16 a16', $ifreq;
+ my ($port, $addr) = sockaddr_in $sin;
+ my $ip = inet_ntoa $addr;
+
+ if ($ip && length($ip) > 0) {
+ $result = $ip;
+ }
+ }
+
+ return $result;
+}
+
+sub get_local_ip_for_remote_ip {
+ my $remote_ip= shift;
+ my $result="0.0.0.0";
+
+ if($remote_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
+ if($remote_ip eq "127.0.0.1") {
+ $result = "127.0.0.1";
+ } else {
+ my $PROC_NET_ROUTE= ('/proc/net/route');
+
+ open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
+ or die "Could not open $PROC_NET_ROUTE";
+
+ my @ifs = <PROC_NET_ROUTE>;
+
+ close(PROC_NET_ROUTE);
+
+ # Eat header line
+ shift @ifs;
+ chomp @ifs;
+ foreach my $line(@ifs) {
+ my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
+ my $destination;
+ my $mask;
+ my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
+ $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
+ ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
+ $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
+ if(new NetAddr::IP($remote_ip)->within(new NetAddr::IP($destination, $mask))) {
+ # destination matches route, save mac and exit
+ $result= &get_ip($Iface);
+ last;
+ }
+ }
+ }
+ } else {
+ daemon_log("get_local_ip_for_remote_ip was called with a non-ip parameter: $remote_ip", 1);
+ }
+ return $result;
+}
+
sub send_msg_to_target {
my ($msg, $address, $encrypt_key, $msg_header) = @_ ;
my $error = 0;
-
+ my $header;
+ my $new_status;
+ my $act_status;
+ my ($sql_statement, $res);
+
if( $msg_header ) {
- $msg_header = "'$msg_header'-";
+ $header = "'$msg_header'-";
}
else {
- $msg_header = "";
+ $header = "";
}
+ # Patch the source ip
+ if($msg =~ /<source>0\.0\.0\.0:\d*?<\/source>/) {
+ my $remote_ip = &get_local_ip_for_remote_ip(sprintf("%s", $address =~ /^([0-9\.]*?):.*$/));
+ $msg =~ s/<source>(0\.0\.0\.0):(\d*?)<\/source>/<source>$remote_ip:$2<\/source>/s;
+ }
+
# encrypt xml msg
my $crypted_msg = &encrypt_msg($msg, $encrypt_key);
# opensocket
my $socket = &open_socket($address);
if( !$socket ) {
- daemon_log("cannot send ".$msg_header."msg to $address , host not reachable", 1);
+ daemon_log("ERROR: cannot send ".$header."msg to $address , host not reachable", 1);
$error++;
}
# send xml msg
print $socket $crypted_msg."\n";
- daemon_log("send ".$msg_header."msg to $address", 1);
+ daemon_log("INFO: send ".$header."msg to $address", 5);
daemon_log("message:\n$msg", 8);
-
+
}
# close socket in any case
close $socket;
}
- return;
+ if( $error > 0 ) { $new_status = "down"; }
+ else { $new_status = $msg_header; }
+
+
+ # known_clients
+ $sql_statement = "SELECT * FROM known_clients WHERE hostname='$address'";
+ $res = $known_clients_db->select_dbentry($sql_statement);
+ if( keys(%$res) > 0) {
+ $act_status = $res->{1}->{'status'};
+ if( $act_status eq "down" ) {
+ $sql_statement = "DELETE FROM known_clients WHERE hostname='$address'";
+ $res = $known_clients_db->del_dbentry($sql_statement);
+ daemon_log("WARNING: failed 2x to send msg to host '$address', delete host from known_clients", 3);
+ }
+ else {
+ $sql_statement = "UPDATE known_clients SET status='$new_status' WHERE hostname='$address'";
+ $res = $known_clients_db->update_dbentry($sql_statement);
+ if($new_status eq "down"){
+ daemon_log("WARNING: set '$address' from status '$act_status' to '$new_status'", 3);
+ }
+ else {
+ daemon_log("INFO: set '$address' from status '$act_status' to '$new_status'", 5);
+ }
+ }
+ }
+
+ # known_server
+ $sql_statement = "SELECT * FROM known_server WHERE hostname='$address'";
+ $res = $known_server_db->select_dbentry($sql_statement);
+ if( keys(%$res) > 0 ) {
+ $act_status = $res->{1}->{'status'};
+ if( $act_status eq "down" ) {
+ $sql_statement = "DELETE FROM known_server WHERE hostname='$address'";
+ $res = $known_server_db->del_dbentry($sql_statement);
+ daemon_log("WARNING: failed 2x to a send msg to host '$address', delete host from known_server", 3);
+ }
+ else {
+ $sql_statement = "UPDATE known_server SET status='$new_status' WHERE hostname='$address'";
+ $res = $known_server_db->update_dbentry($sql_statement);
+ if($new_status eq "down"){
+ daemon_log("WARNING: set '$address' from status '$act_status' to '$new_status'", 3);
+ }
+ else {
+ daemon_log("INFO: set '$address' from status '$act_status' to '$new_status'", 5);
+ }
+ }
+ }
+ return $error;
}
sub client_input {
no strict "refs";
- my ($heap,$input,$wheel) = @_[HEAP, ARG0, ARG1];
+ my ($kernel, $session, $heap,$input,$wheel) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1];
+ my $session_id = $session->ID;
my ($msg, $msg_hash, $module);
my $error = 0;
my $answer_l;
my $client_answer;
daemon_log("Incoming msg from '".$heap->{'remote_ip'}."'", 7);
- daemon_log("\n$input", 8);
+ daemon_log("message:\n$input", 8);
+ ####################
+ # check incoming msg
# msg is from a new client or gosa
($msg, $msg_hash, $module) = &input_from_unknown_host($input);
-
# msg is from a gosa-si-server or gosa-si-bus
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
($msg, $msg_hash, $module) = &input_from_known_server($input, $heap->{'remote_ip'});
}
-
# msg is from a gosa-si-client
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
($msg, $msg_hash, $module) = &input_from_known_client($input, $heap->{'remote_ip'});
}
-
# an error occurred
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
$error++;
######################
# process incoming msg
if( $error == 0) {
- daemon_log("Processing module ".$module, 3);
- $answer_l = &{ $module."::process_incoming_msg" }($msg, $msg_hash, $heap->{'remote_ip'});
+ daemon_log("Processing module ".$module, 5);
+ $answer_l = &{ $module."::process_incoming_msg" }($msg, $msg_hash, $session_id);
if ( 0 > @{$answer_l} ) {
my $answer_str = join("\n", @{$answer_l});
# answer
if( $error == 0 ) {
- # for each answer in answer list
foreach my $answer ( @{$answer_l} ) {
-
- # check answer if gosa-si envelope conform
- if( $error == 0 ) {
- my $answer_hash = $xml->XMLin($answer, ForceArray=>1);
- $answer_header = @{$answer_hash->{'header'}}[0];
- @answer_target_l = @{$answer_hash->{'target'}};
- $answer_source = @{$answer_hash->{'source'}}[0];
- if( !$answer_header ) {
- daemon_log('ERROR: module answer is not gosa-si envelope conform: no header', 1);
- daemon_log("\n$answer", 8);
- $error++;
+ # for each answer in answer list
+
+ # check outgoing msg to xml validity
+ my $answer_hash = &check_outgoing_xml_validity($answer);
+ if( not defined $answer_hash ) {
+ next;
+ }
+
+ $answer_header = @{$answer_hash->{'header'}}[0];
+ @answer_target_l = @{$answer_hash->{'target'}};
+ $answer_source = @{$answer_hash->{'source'}}[0];
+
+ # deliver msg to all targets
+ foreach my $answer_target ( @answer_target_l ) {
+
+ # targets of msg are all gosa-si-clients in known_clients_db
+ if( $answer_target eq "*" ) {
+ # answer is for all clients
+ my $sql_statement= "SELECT * FROM known_clients";
+ my $query_res = $known_clients_db->select_dbentry( $sql_statement );
+ while( my ($hit_num, $hit) = each %{ $query_res } ) {
+ my $host_name = $hit->{hostname};
+ my $host_key = $hit->{hostkey};
+ &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ }
}
- if( 0 == length @answer_target_l ) {
- daemon_log('ERROR: module answer is not gosa-si envelope conform: no targets', 1);
- daemon_log("\n$answer", 8);
- $error++;
+
+ # targets of msg are all gosa-si-server in known_server_db
+ elsif( $answer_target eq "KNOWN_SERVER" ) {
+ # answer is for all server in known_server
+ my $sql_statement= "SELECT * FROM known_server";
+ my $query_res = $known_server_db->select_dbentry( $sql_statement );
+ while( my ($hit_num, $hit) = each %{ $query_res } ) {
+ my $host_name = $hit->{hostname};
+ my $host_key = $hit->{hostkey};
+ $answer =~ s/KNOWN_SERVER/$host_name/g;
+ &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ }
}
- if( !$answer_source ) {
- daemon_log('ERROR: module answer is not gosa-si envelope conform: no source', 1);
- daemon_log("\n$answer", 8);
- $error++;
+
+ # target of msg is GOsa
+ elsif( $answer_target eq "GOSA" ) {
+ $answer =~ /<session_id>(\d+)<\/session_id>/;
+ my $session_id = $1;
+ my $add_on = "";
+ if( defined $session_id ) {
+ $add_on = ".session_id=$session_id";
+ }
+ # answer is for GOSA and has to returned to connected client
+ my $gosa_answer = &encrypt_msg($answer, $GosaPackages_key);
+ $client_answer = $gosa_answer.$add_on;
}
- if( $error != 0 ) {
- next;
+ # target of msg is job queue at this host
+ elsif( $answer_target eq "JOBDB") {
+ $answer =~ /<header>(\S+)<\/header>/;
+ my $header;
+ if( defined $1 ) { $header = $1; }
+ &send_msg_to_target($answer, $server_address, $GosaPackages_key, $header);
}
- # deliver msg to all targets
- foreach my $answer_target ( @answer_target_l ) {
- if( $answer_target eq "*" ) {
- # answer is for all clients
- my $sql_statement= "SELECT * FROM known_clients";
- my $query_res = $known_clients_db->select_dbentry( $sql_statement );
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $host_name = $hit->{hostname};
- my $host_key = $hit->{hostkey};
- &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
- }
- }
- elsif( $answer_target eq "GOSA" ) {
- # answer is for GOSA and has to returned to connected client
- my $gosa_answer = &encrypt_msg($answer, $GosaPackages_key);
- $client_answer = $gosa_answer;
- }
- elsif( $answer_target eq "KNOWN_SERVER" ) {
- # answer is for all server in known_server
- my $sql_statement= "SELECT * FROM known_server";
- my $query_res = $known_server_db->select_dbentry( $sql_statement );
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $host_name = $hit->{hostname};
- my $host_key = $hit->{hostkey};
- $answer =~ s/KNOWN_SERVER/$host_name/g;
- &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ # target of msg is a mac address
+ elsif( $answer_target =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ) {
+ daemon_log("target is mac address '$answer_target', looking for host in known_clients", 3);
+ my $sql_statement = "SELECT * FROM known_clients WHERE macaddress='$answer_target'";
+ my $query_res = $known_clients_db->select_dbentry( $sql_statement );
+ my $found_ip_flag = 0;
+ while( my ($hit_num, $hit) = each %{ $query_res } ) {
+ my $host_name = $hit->{hostname};
+ my $host_key = $hit->{hostkey};
+ $answer =~ s/$answer_target/$host_name/g;
+ daemon_log("found host '$host_name', associated to '$answer_target'", 3);
+ &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ $found_ip_flag++ ;
+ }
+ if( $found_ip_flag == 0) {
+ daemon_log("WARNING: no host found in known_clients with mac address '$answer_target'", 3);
+ if( $bus_activ eq "true" ) {
+ daemon_log("INFO: try to forward msg '$answer_header' to bus '$bus_address'", 5);
+ my $sql_statement = "SELECT * FROM known_server WHERE hostname='$bus_address'";
+ my $query_res = $known_server_db->select_dbentry( $sql_statement );
+ while( my ($hit_num, $hit) = each %{ $query_res } ) {
+ my $bus_address = $hit->{hostname};
+ my $bus_key = $hit->{hostkey};
+ &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header);
+ last;
+ }
}
+
}
- else {
- # answer is for one specific host
- # get encrypt_key
- my $encrypt_key = &get_encrypt_key($answer_target);
- if( !$encrypt_key ) {
- daemon_log("ERROR: no encrypt key found for answer target '$answer_target'", 1);
- daemon_log("\n$answer", 8);
- next;
+
+ # answer is for one specific host
+ } else {
+ # get encrypt_key
+ my $encrypt_key = &get_encrypt_key($answer_target);
+ if( not defined $encrypt_key ) {
+ # unknown target, forward msg to bus
+ daemon_log("WARNING: unknown target '$answer_target'", 3);
+ if( $bus_activ eq "true" ) {
+ daemon_log("INFO: try to forward msg '$answer_header' to bus '$bus_address'", 5);
+ my $sql_statement = "SELECT * FROM known_server WHERE hostname='$bus_address'";
+ my $query_res = $known_server_db->select_dbentry( $sql_statement );
+ my $res_length = keys( %{$query_res} );
+ if( $res_length == 0 ){
+ daemon_log("WARNING: send '$answer_header' to '$bus_address' failed, no bus found in known_server", 3);
+ }
+ else {
+ while( my ($hit_num, $hit) = each %{ $query_res } ) {
+ my $bus_key = $hit->{hostkey};
+ &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header);
+ }
+ }
}
- # send_msg
- &send_msg_to_target($answer, $answer_target, $encrypt_key, $answer_header);
+ next;
}
+ # send_msg
+ &send_msg_to_target($answer, $answer_target, $encrypt_key, $answer_header);
}
}
}
}
if( $client_answer ) {
+ if( $client_answer =~ s/session_id=(\d+)$// ) {
+ my $session_id = $1;
+ if( defined $session_id ) {
+ my $session_reference = $kernel->ID_id_to_session($session_id);
+ $heap = $session_reference->get_heap();
+ }
+ }
$heap->{client}->put($client_answer);
}
sub trigger_db_loop {
# my ($kernel) = $_[KERNEL];
my ($kernel) = @_ ;
- $kernel->delay_set('watch_for_new_jobs',3);
+ $kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
}
# check gosa job queue for jobs with executable timestamp
my $timestamp = &get_time();
-
my $sql_statement = "SELECT * FROM ".$job_queue_table_name.
" WHERE status='waiting' AND timestamp<'$timestamp'";
-
my $res = $job_db->select_dbentry( $sql_statement );
while( my ($id, $hit) = each %{$res} ) {
-
my $jobdb_id = $hit->{id};
- my $macaddress = $hit->{macaddress};
- my $job_msg_hash = &transform_msg2hash($hit->{xmlmessage});
- my $out_msg_hash = $job_msg_hash;
+ my $macaddress = $hit->{'macaddress'};
+ my $job_msg = $hit->{'xmlmessage'};
+ my $header = $hit->{'headertag'};
my $sql_statement = "SELECT * FROM known_clients WHERE macaddress='$macaddress'";
my $res_hash = $known_clients_db->select_dbentry( $sql_statement );
# expect macaddress is unique!!!!!!
my $target = $res_hash->{1}->{hostname};
if (not defined $target) {
- &daemon_log("ERROR: no host found for mac address: $job_msg_hash->{mac}[0]", 1);
- &daemon_log("xml message: $hit->{xmlmessage}", 5);
+ &daemon_log("ERROR: no host found for mac address: $macaddress", 1);
+ &daemon_log("$hit->{xmlmessage}", 8);
my $sql_statement = "UPDATE $job_queue_table_name ".
"SET status='error', result='no host found for mac address' ".
"WHERE id='$jobdb_id'";
next;
}
- # add target
- &add_content2xml_hash($out_msg_hash, "target", $target);
-
- # add new header
- my $out_header = $job_msg_hash->{header}[0];
- $out_header =~ s/job_/gosa_/;
- delete $out_msg_hash->{header};
- &add_content2xml_hash($out_msg_hash, "header", $out_header);
+ # change header
+ $job_msg =~ s/<header>job_/<header>gosa_/;
# add sqlite_id
- &add_content2xml_hash($out_msg_hash, "jobdb_id", $jobdb_id);
-
- my $out_msg = &create_xml_string($out_msg_hash);
-
- # encrypt msg as a GosaPackage module
- my $cipher = &create_ciphering($GosaPackages_key);
- my $crypted_out_msg = &encrypt_msg($out_msg, $cipher);
+ $job_msg =~ s/<\/xml>$/<jobdb_id>$jobdb_id<\/jobdb_id><\/xml>/;
- my $error = &send_msg_hash2address($out_msg_hash, "$gosa_ip:$gosa_port", $GosaPackages_key);
+ my $func_error = &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header);
- if ($error == 0) {
- my $sql_statement = "UPDATE $job_queue_table_name ".
- "SET status='processing', targettag='$target' ".
- "WHERE id='$jobdb_id'";
- my $res = $job_db->update_dbentry($sql_statement);
- } else {
- my $sql_statement = "UPDATE $job_queue_table_name ".
- "SET status='error' ".
- "WHERE id='$jobdb_id'";
- my $res = $job_db->update_dbentry($sql_statement);
- }
- }
+# if ( $func_error ) {
+#print STDERR "========================================\n";
+#print STDERR "func_error: $func_error\n";
+#
+# my $sql_statement = "UPDATE $job_queue_table_name ".
+# "SET status='error', targettag='$target' ".
+# "WHERE id='$jobdb_id'";
+#print STDERR "sql: $sql_statement\n";
+# my $res = $job_db->update_dbentry($sql_statement);
+#print STDERR "res: ".Dumper($res);
+#
+# } else {
+#print STDERR "========================================\n";
+#print STDERR "func_error: $func_error\n";
+#
+# my $sql_statement = "UPDATE $job_queue_table_name ".
+# "SET status='done', targettag='$target' ".
+# "WHERE id='$jobdb_id'";
+#print STDERR "sql: $sql_statement\n";
+# my $res = $job_db->update_dbentry($sql_statement);
+#print STDERR "res: ".Dumper($res);
+# }
+ }
$kernel->delay_set('watch_for_new_jobs',3);
}
daemon_log(" ", 1);
daemon_log("$0 started!", 1);
+if ($no_bus > 0) {
+ $bus_activ = "false"
+}
+
+
+
# delete old DBsqlite lock files
-system('rm -f /tmp/gosa_si_lock*gosa-si-server*');
+#unlink('/tmp/gosa_si_lock*');
# connect to gosa-si job queue
-my @job_col_names = ("id", "timestamp", "status", "result", "headertag", "targettag", "xmlmessage", "macaddress");
+my @job_col_names = ("id INTEGER", "timestamp", "status", "result", "progress", "headertag", "targettag", "xmlmessage", "macaddress");
$job_db = GOSA::DBsqlite->new($job_queue_file_name);
$job_db->create_table('jobs', \@job_col_names);