From: rettenbe Date: Fri, 8 Feb 2008 16:40:11 +0000 (+0000) Subject: server and clients which are down, are deleted from the dbs X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ad24b928485503b4eb0814994d56e8f308750195;p=gosa.git server and clients which are down, are deleted from the dbs git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8789 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-si/client/events/corefunctions.pm b/gosa-si/client/events/corefunctions.pm index de0539aa8..d68f98160 100644 --- a/gosa-si/client/events/corefunctions.pm +++ b/gosa-si/client/events/corefunctions.pm @@ -1,7 +1,7 @@ package corefunctions; use Exporter; @ISA = qw(Exporter); -my @events = qw(get_events registered set_activated_for_installation new_ldap_config new_key generate_hw_digest detect_hardware reboot halt reinstall softupdate); +my @events = qw(get_events registered set_activated_for_installation new_ldap_config new_key generate_hw_digest detect_hardware reboot halt reinstall softupdate confirm_new_key); @EXPORT = @events; use strict; @@ -324,6 +324,17 @@ sub new_key { } +sub confirm_new_key { + my ($msg, $msg_hash) = @_ ; + my $header = @{$msg_hash->{'header'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $source = @{$msg_hash->{'source'}}[0]; + + &main::daemon_log("confirm new key from $source", 5); + return; + +} + sub detect_hardware { diff --git a/gosa-si/gosa-si-bus b/gosa-si/gosa-si-bus index 29e31ff37..8f3935256 100755 --- a/gosa-si/gosa-si-bus +++ b/gosa-si/gosa-si-bus @@ -470,12 +470,16 @@ sub send_msg_hash2address { 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 = ""; } # encrypt xml msg @@ -484,7 +488,7 @@ sub send_msg_to_target { # opensocket my $socket = &open_socket($address); if( !$socket ) { - daemon_log("cannot send ".$msg_header."msg to $address , host not reachable", 1); + daemon_log("cannot send ".$header."msg to $address , host not reachable", 1); $error++; } @@ -492,7 +496,7 @@ sub send_msg_to_target { # send xml msg print $socket $crypted_msg."\n"; - daemon_log("send ".$msg_header."msg to $address", 1); + daemon_log("send ".$header."msg to $address", 1); daemon_log("message:\n$msg", 8); } @@ -502,6 +506,44 @@ sub send_msg_to_target { close $socket; } + if( $error > 0 ) { $new_status = "down"; } + else { $new_status = $msg_header; } + + + # known_clients + $sql_statement = "SELECT * FROM bus_known_clients WHERE hostname='$address'"; + $res = $bus_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 bus_known_clients WHERE hostname='$address'"; + $res = $bus_known_clients_db->del_dbentry($sql_statement); + daemon_log("WARNING: failed 2x to send msg to host '$address', delete host from bus_known_clients", 3); + } + else { + $sql_statement = "UPDATE bus_known_clients SET status='$new_status' WHERE hostname='$address'"; + $res = $bus_known_clients_db->update_dbentry($sql_statement); + daemon_log("INFO: set '$address' from status '$act_status' to '$new_status'", 5); + } + } + + # known_server + $sql_statement = "SELECT * FROM bus_known_server WHERE hostname='$address'"; + $res = $bus_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 bus_known_clients WHERE hostname='$address'"; + $res = $bus_known_clients_db->del_dbentry($sql_statement); + daemon_log("WARNING: failed 2x to a send msg to host '$address', delete host from bus_known_server", 3); + } + else { + $sql_statement = "UPDATE bus_known_server SET status='$new_status' WHERE hostname='$address'"; + $res = $bus_known_server_db->update_dbentry($sql_statement); + daemon_log("INFO: set '$address' from status '$act_status' to '$new_status'", 5) + } + } + return; } @@ -900,7 +942,7 @@ POE::Session->create( _start => \&_start, _default => \&_default, here_i_am => \&here_i_am, - confirm_new_passwd => \&confirm_new_key, + confirm_new_key => \&confirm_new_key, new_client => \&new_client, } ); diff --git a/gosa-si/gosa-si-client b/gosa-si/gosa-si-client index d4e17c29f..84396db07 100755 --- a/gosa-si/gosa-si-client +++ b/gosa-si/gosa-si-client @@ -848,7 +848,6 @@ sub register_at_gosa_si_server { if( $REGISTERED_FLAG == 1 ) { - # create new passwd and ciphering object for client-server communication $server_key = &create_passwd(); @@ -858,7 +857,12 @@ sub register_at_gosa_si_server { # fetch first gosa-si-server from @servers my $server = shift(@servers); - + if( !$server ) { + daemon_log("no gosa-si-server left in list of servers", 1); + daemon_log("unable to register at a gosa-si-server, force shutdown", 1); + exit(1); + } + # Check if our ip is resolvable - if not: don't try to register my $ip= &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/)); my $resolver= Net::DNS::Resolver->new; @@ -868,16 +872,12 @@ sub register_at_gosa_si_server { exit(1); } - if( !$server ) { - daemon_log("no gosa-si-server left in list of servers", 1); - daemon_log("unable to register at a gosa-si-server, force shutdown", 1); - exit(1); - } - # create registration msg - my $register_hash = &create_xml_hash("here_i_am", &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/)).":".$client_port, $server); + my $local_ip = &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/)); + my $local_mac = &get_local_mac_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/)); + my $register_hash = &create_xml_hash("here_i_am", $local_ip.":".$client_port, $server); &add_content2xml_hash($register_hash, "new_passwd", $server_key); - &add_content2xml_hash($register_hash, "mac_address", &get_local_mac_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/))); + &add_content2xml_hash($register_hash, "mac_address", $local_mac); &add_content2xml_hash($register_hash, "events", $events); &add_content2xml_hash($register_hash, "gotoHardwareChecksum", $gotoHardwareChecksum); @@ -893,7 +893,8 @@ sub register_at_gosa_si_server { } } daemon_log("waiting for msg 'register_at_gosa_si_server'",1); - $kernel->delay_set('register_at_gosa_si_server',180); +# $kernel->delay_set('register_at_gosa_si_server', 180); + $kernel->delay_set('register_at_gosa_si_server', 5); # clear old settings and set it again $kernel->delay_set('trigger_new_key', $server_key_lifetime); } @@ -1012,6 +1013,7 @@ sub server_input { $error++; } + ###################### # process incoming msg if( $error == 0 ) { diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index bc7d4f52f..8482e8d72 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -431,7 +431,7 @@ sub check_key_and_xml_validity { }; if($@) { - &main::daemon_log("WARNING: do not understand the message:", 5); + &main::daemon_log("WARNING: do not understand the message", 5); &main::daemon_log("$@", 8); } @@ -534,7 +534,7 @@ sub input_from_unknown_host { # 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); + #daemon_log("$mod: deciphering failed", 5); next; } else { @@ -652,12 +652,16 @@ sub open_socket { 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 = ""; } # encrypt xml msg @@ -666,7 +670,7 @@ sub send_msg_to_target { # opensocket my $socket = &open_socket($address); if( !$socket ) { - daemon_log("cannot send ".$msg_header."msg to $address , host not reachable", 1); + daemon_log("cannot send ".$header."msg to $address , host not reachable", 1); $error++; } @@ -674,9 +678,9 @@ sub send_msg_to_target { # send xml msg print $socket $crypted_msg."\n"; - daemon_log("send ".$msg_header."msg to $address", 1); + daemon_log("send ".$header."msg to $address", 1); daemon_log("message:\n$msg", 8); - + } # close socket in any case @@ -684,7 +688,55 @@ sub send_msg_to_target { 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_clients_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; } @@ -782,7 +834,7 @@ sub client_input { 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 @@ -815,7 +867,7 @@ sub client_input { } if( $found_ip_flag == 0) { daemon_log("WARNING: no host found in known_clients with mac address '$answer_target', forward msg to bus", 1); - my $sql_statement = "SELECT * FROM known_server WHERE status='bus'"; + 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}; @@ -833,9 +885,8 @@ sub client_input { if( !$encrypt_key ) { # unknown target, forward msg to bus daemon_log("WARNING: unknown target '$answer_target', forward msg to bus", 3); - my $sql_statement = "SELECT * FROM known_server WHERE status='bus'"; + my $sql_statement = "SELECT * FROM known_server WHERE hostname='$bus_address'"; my $query_res = $known_server_db->select_dbentry( $sql_statement ); - my $bus_address = $query_res->{1}->{hostname}; my $bus_key = $query_res->{1}->{hostkey}; &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header); next; diff --git a/gosa-si/modules/ArpHandler.pm b/gosa-si/modules/ArpHandler.pm index bb2809fdb..44a74978b 100644 --- a/gosa-si/modules/ArpHandler.pm +++ b/gosa-si/modules/ArpHandler.pm @@ -200,7 +200,7 @@ sub got_packet { } } $hosts_database->{$packet->{source_haddr}}->{dnsname}=$dnsname; - &main::daemon_log("Host was found in LDAP as ".$ldap_result->{dn}, 6); + &main::daemon_log("Host was found in LDAP as ".$ldap_result->{dn}, 8); } else { $hosts_database->{$packet->{source_haddr}}={ macAddress => $packet->{source_haddr}, @@ -240,7 +240,7 @@ sub got_packet { ); } - &main::daemon_log("Host already in cache (".($hosts_database->{$packet->{source_haddr}}->{device})."->".($hosts_database->{$packet->{source_haddr}}->{dnsname}).")",6); + &main::daemon_log("Host already in cache (".($hosts_database->{$packet->{source_haddr}}->{device})."->".($hosts_database->{$packet->{source_haddr}}->{dnsname}).")",8); } } diff --git a/gosa-si/modules/SIPackages.pm b/gosa-si/modules/SIPackages.pm index 96a6edc15..6494c4f7d 100644 --- a/gosa-si/modules/SIPackages.pm +++ b/gosa-si/modules/SIPackages.pm @@ -12,6 +12,7 @@ use GOSA::GosaSupportDaemon; use IO::Socket::INET; use XML::Simple; use Data::Dumper; +use NetAddr::IP; use Net::LDAP; use Socket; use Net::hostent; @@ -21,7 +22,7 @@ BEGIN{} END {} my ($known_clients_file_name); -my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password); +my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface); my ($bus_activ, $bus_key, $bus_ip, $bus_port); my $server; my $network_interface; @@ -34,7 +35,7 @@ my %cfg_defaults = "server" => {"server_activ" => [\$server_activ, "on"], "server_ip" => [\$server_ip, "0.0.0.0"], - "server_mac_address" => [\$server_mac_address, ""], + "server_mac_address" => [\$server_mac_address, "00:00:00:00:00"], "server_port" => [\$server_port, "20081"], "SIPackages_key" => [\$SIPackages_key, ""], "max_clients" => [\$max_clients, 100], @@ -56,16 +57,19 @@ my %cfg_defaults = # read configfile and import variables &read_configfile(); -# detect interfaces and mac address +$server_ip = &get_local_ip_for_remote_ip($server_ip); + $network_interface= &get_interface_for_ip($server_ip); -$server_mac_address= &get_mac($network_interface); +$server_mac_address= &get_mac($network_interface); # complete addresses if( $server_ip eq "0.0.0.0" ) { $server_ip = "127.0.0.1"; } my $server_address = "$server_ip:$server_port"; +$main::server_address = $server_address; my $bus_address = "$bus_ip:$bus_port"; +$main::bus_address = $bus_address; # create general settings for this module my $xml = new XML::Simple(); @@ -325,6 +329,48 @@ sub get_ip { } +sub get_local_ip_for_remote_ip { + my $server_ip= shift; + my $result="0.0.0.0"; + + if($server_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) { + if($server_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 = ; + + 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($server_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: $server_ip", 1); + } + return $result; +} + + #=== FUNCTION ================================================================ # NAME: register_at_bus # PARAMETERS: nothing @@ -346,14 +392,6 @@ sub register_at_bus { &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am"); return $msg; -# my $answer = ""; -# $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd); -# if ($answer == 0) { -# &main::daemon_log("register at bus: $bus_address", 1); -# } else { -# &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1); -# } -# return; } @@ -473,7 +511,7 @@ sub new_key { "SET hostkey='$source_key', timestamp='$act_time' ". "WHERE hostname='$source_name'"; my $res = $main::known_clients_db->update_dbentry( $sql_statement ); - my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name); + my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name); my $out_msg = &create_xml_string($hash); push(@out_msg_l, $out_msg); } @@ -490,7 +528,7 @@ sub new_key { "WHERE hostname='$source_name'"; my $res = $main::known_server_db->update_dbentry( $sql_statement ); - my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name); + my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name); my $out_msg = &create_xml_string($hash); push(@out_msg_l, $out_msg); } @@ -864,7 +902,7 @@ sub process_detected_hardware { filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))" ); } else { - &main::daemon_log("There was a problem adding the entry", 1); + &main::daemon_log("ERROR: There was a problem adding the entry", 1); } } diff --git a/gosa-si/server.conf b/gosa-si/server.conf index 22b42fb80..5ae3e923d 100644 --- a/gosa-si/server.conf +++ b/gosa-si/server.conf @@ -1,10 +1,6 @@ [general] log_file = /var/log/gosa-si-server.log pid_file = /var/run/gosa-si-server.pid -child_max = 10 -child_min = 2 -child_timeout = 10 -job_queue_timeout = 5 [bus] bus_activ = on @@ -14,7 +10,6 @@ bus_port = 20080 [server] server_activ = on -server_ip = 10.89.1.31 server_port = 20081 SIPackages_key = secret-server-password max_clients = 5 diff --git a/gosa-si/tests/client.php b/gosa-si/tests/client.php index f2a6ed4ca..fd2f55668 100755 --- a/gosa-si/tests/client.php +++ b/gosa-si/tests/client.php @@ -48,7 +48,8 @@ if($sock->connected()){ #$data = "
gosa_halt
10.89.1.31:20083 10.89.1.31:20081
"; #$data = "
gosa_new_key_for_client
00:01:6c:9d:b9:fa 10.89.1.31:20081
"; #$data = "
gosa_new_key_for_client
00:0c:29:bd:7b:e7 10.89.1.31:20081
"; -$data = "
gosa_new_key_for_client
10.89.1.197:20083 10.89.1.31:20081
"; +#$data = "
gosa_new_key_for_client
10.89.1.197:20083 10.89.1.31:20081
"; +$data = "
gosa_new_key_for_client
10.89.1.31:20083 10.89.1.31:20081
"; $sock->write($data);