diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server
index 41dd27a5c9cb646c50f8e9f3a4c0a785545ec1a3..6d2c866d81e8a6128347383392f89760202761a3 100755 (executable)
--- a/gosa-si/gosa-si-server
+++ b/gosa-si/gosa-si-server
#===============================================================================
+# TODO
+#
+# max_children wird momentan nicht mehr verwendet, jede eingehende nachricht bekommt ein eigenes POE child
+
use strict;
use warnings;
use Getopt::Long;
use POE qw(Component::Server::TCP Wheel::Run Filter::Reference);
use Net::LDAP;
use Net::LDAP::Util qw(:escape);
+use Time::HiRes qw( usleep);
+use DateTime;
my $modules_path = "/usr/lib/gosa-si/modules";
use lib "/usr/lib/gosa-si/modules";
-# TODO es gibt eine globale funktion get_ldap_handle
-# - ist in einer session dieses ldap handle schon vorhanden, wird es zurückgegeben
-# - ist es nicht vorhanden, wird es erzeugt, im heap für spätere ldap anfragen gespeichert und zurückgegeben
-# - sessions die kein ldap handle brauchen, sollen auch keins haben
-# - wird eine session geschlossen, muss das ldap verbindung vorher beendet werden
-our $global_kernel;
+# revision number of server and program name
+my $server_version = '$HeadURL: https://oss.gonicus.de/repositories/gosa/trunk/gosa-si/gosa-si-server $:$Rev: 10826 $';
+my $server_headURL;
+my $server_revision;
+my $server_status;
+our $prg= basename($0);
-my (%cfg_defaults, $foreground, $verbose, $ping_timeout);
-my ($bus_activ, $bus, $msg_to_bus, $bus_cipher);
+our $global_kernel;
+my ($foreground, $ping_timeout);
my ($server);
my ($gosa_server, $job_queue_timeout, $job_queue_loop_delay);
+my ($messaging_db_loop_delay);
my ($known_modules);
-my ($pid_file, $procid, $pid, $log_file);
-my ($arp_activ, $arp_fifo);
+my ($procid, $pid);
+my ($arp_fifo);
my ($xml);
my $sources_list;
my $max_clients;
my %repo_dirs=();
# variables declared in config file are always set to 'our'
our (%cfg_defaults, $log_file, $pid_file,
- $server_ip, $server_port, $SIPackages_key,
+ $server_ip, $server_port, $ClientPackages_key,
$arp_activ, $gosa_unit_tag,
$GosaPackages_key, $gosa_ip, $gosa_port, $gosa_timeout,
+ $foreign_server_string, $server_domain, $ServerPackages_key, $foreign_servers_register_delay,
);
# additional variable which should be globaly accessable
our $server_address;
our $server_mac_address;
-our $bus_address;
our $gosa_address;
-our $no_bus;
our $no_arp;
our $verbose;
our $forground;
our $cfg_file;
-#our ($ldap_handle, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $ldap_server_dn);
our ($ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $ldap_server_dn);
+# dak variables
+our $dak_base_directory;
+our $dak_signing_keys_directory;
+our $dak_queue_directory;
+our $dak_user;
# specifies the verbosity of the daemon_log
$verbose = 0 ;
# specifies the timeout seconds while checking the online status of a registrating client
$ping_timeout = 5;
-$no_bus = 0;
-$bus_activ = "true";
-
$no_arp = 0;
-
-our $prg= basename($0);
+my $packages_list_under_construction = "/tmp/packages_list_creation_in_progress";
+my @packages_list_statements;
+my $watch_for_new_jobs_in_progress = 0;
+
+# holds all incoming decrypted messages
+our $incoming_db;
+our $incoming_tn = 'incoming';
+my $incoming_file_name;
+my @incoming_col_names = ("id INTEGER PRIMARY KEY",
+ "timestamp DEFAULT 'none'",
+ "headertag DEFAULT 'none'",
+ "targettag DEFAULT 'none'",
+ "xmlmessage DEFAULT 'none'",
+ "module DEFAULT 'none'",
+ "sessionid DEFAULT '0'",
+ );
# holds all gosa jobs
our $job_db;
our $job_queue_tn = 'jobs';
my $job_queue_file_name;
-my @job_queue_col_names = ("id INTEGER",
- "timestamp",
+my @job_queue_col_names = ("id INTEGER PRIMARY KEY",
+ "timestamp DEFAULT 'none'",
"status DEFAULT 'none'",
"result DEFAULT 'none'",
"progress DEFAULT 'none'",
- "headertag DEFAULT 'none'",
+ "headertag DEFAULT 'none'",
"targettag DEFAULT 'none'",
"xmlmessage DEFAULT 'none'",
"macaddress DEFAULT 'none'",
+ "plainname DEFAULT 'none'",
);
-# holds all other gosa-sd as well as the gosa-sd-bus
+# holds all other gosa-si-server
our $known_server_db;
our $known_server_tn = "known_server";
my $known_server_file_name;
-my @known_server_col_names = ('hostname', 'status', 'hostkey', 'timestamp');
+my @known_server_col_names = ("hostname", "status", "hostkey", "timestamp");
# holds all registrated clients
our $known_clients_db;
our $known_clients_tn = "known_clients";
my $known_clients_file_name;
-my @known_clients_col_names = ('hostname', 'status', 'hostkey', 'timestamp', 'macaddress', 'events');
+my @known_clients_col_names = ("hostname", "status", "hostkey", "timestamp", "macaddress", "events", "keylifetime");
+
+# holds all registered clients at a foreign server
+our $foreign_clients_db;
+our $foreign_clients_tn = "foreign_clients";
+my $foreign_clients_file_name;
+my @foreign_clients_col_names = ("hostname", "macaddress", "regserver", "timestamp");
# holds all logged in user at each client
our $login_users_db;
our $login_users_tn = "login_users";
my $login_users_file_name;
-my @login_users_col_names = ('client', 'user', 'timestamp');
+my @login_users_col_names = ("client", "user", "timestamp");
# holds all fai server, the debian release and tag
our $fai_server_db;
our $fai_server_tn = "fai_server";
my $fai_server_file_name;
-our @fai_server_col_names = ('timestamp', 'server', 'release', 'sections', 'tag');
+our @fai_server_col_names = ("timestamp", "server", "release", "sections", "tag");
+
+our $fai_release_db;
our $fai_release_tn = "fai_release";
-our @fai_release_col_names = ('timestamp', 'release', 'class', 'type', 'state');
+my $fai_release_file_name;
+our @fai_release_col_names = ("timestamp", "release", "class", "type", "state");
# holds all packages available from different repositories
our $packages_list_db;
our $packages_list_tn = "packages_list";
my $packages_list_file_name;
-our @packages_list_col_names = ('distribution', 'package', 'version', 'section', 'description', 'template', 'timestamp');
+our @packages_list_col_names = ("distribution", "package", "version", "section", "description", "template", "timestamp");
my $outdir = "/tmp/packages_list_db";
my $arch = "i386";
# holds all messages which should be delivered to a user
our $messaging_db;
our $messaging_tn = "messaging";
-our @messaging_col_names = ('subject', 'from', 'to', 'flag', 'direction', 'delivery_time', 'message', 'timestamp', 'id INTEGER', );
+our @messaging_col_names = ("id INTEGER", "subject", "message_from", "message_to",
+ "flag", "direction", "delivery_time", "message", "timestamp" );
my $messaging_file_name;
# path to directory to store client install log files
# queue which stores taskes until one of the $max_children children are ready to process the task
my @tasks = qw();
+my @msgs_to_decrypt = qw();
my $max_children = 2;
"log-file" => [\$log_file, "/var/run/".$prg.".log"],
"pid-file" => [\$pid_file, "/var/run/".$prg.".pid"],
},
-"bus" => {
- "activ" => [\$bus_activ, "true"],
- },
"server" => {
"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'],
- "login-users" => [\$login_users_file_name, '/var/lib/gosa-si/users.db'],
- "fai-server" => [\$fai_server_file_name, '/var/lib/gosa-si/fai.db'],
- "packages-list" => [\$packages_list_file_name, '/var/lib/gosa-si/packages.db'],
- "messaging" => [\$messaging_file_name, '/var/lib/gosa-si/messaging.db'],
- "source-list" => [\$sources_list, '/etc/apt/sources.list'],
- "repo-path" => [\$repo_path, '/srv/www/repository'],
- "ldap-uri" => [\$ldap_uri, ""],
- "ldap-base" => [\$ldap_base, ""],
- "ldap-admin-dn" => [\$ldap_admin_dn, ""],
- "ldap-admin-password" => [\$ldap_admin_password, ""],
- "gosa-unit-tag" => [\$gosa_unit_tag, ""],
- "max-clients" => [\$max_clients, 10],
+ "known-clients" => [\$known_clients_file_name, '/var/lib/gosa-si/clients.db' ],
+ "known-servers" => [\$known_server_file_name, '/var/lib/gosa-si/servers.db'],
+ "incoming" => [\$incoming_file_name, '/var/lib/gosa-si/incoming.db'],
+ "login-users" => [\$login_users_file_name, '/var/lib/gosa-si/users.db'],
+ "fai-server" => [\$fai_server_file_name, '/var/lib/gosa-si/fai_server.db'],
+ "fai-release" => [\$fai_release_file_name, '/var/lib/gosa-si/fai_release.db'],
+ "packages-list" => [\$packages_list_file_name, '/var/lib/gosa-si/packages.db'],
+ "messaging" => [\$messaging_file_name, '/var/lib/gosa-si/messaging.db'],
+ "foreign-clients" => [\$foreign_clients_file_name, '/var/lib/gosa-si/foreign_clients.db'],
+ "source-list" => [\$sources_list, '/etc/apt/sources.list'],
+ "repo-path" => [\$repo_path, '/srv/www/repository'],
+ "ldap-uri" => [\$ldap_uri, ""],
+ "ldap-base" => [\$ldap_base, ""],
+ "ldap-admin-dn" => [\$ldap_admin_dn, ""],
+ "ldap-admin-password" => [\$ldap_admin_password, ""],
+ "gosa-unit-tag" => [\$gosa_unit_tag, ""],
+ "max-clients" => [\$max_clients, 10],
},
"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],
+ "messaging-db-loop-delay" => [\$messaging_db_loop_delay, 3],
"key" => [\$GosaPackages_key, "none"],
+ "dak-base" => [\$dak_base_directory, "/srv/archive"],
+ "dak-keyring" => [\$dak_signing_keys_directory, "/srv/archive/keyrings"],
+ "dak-queue" => [\$dak_queue_directory, "/srv/archive/queue"],
+ "dak-user" => [\$dak_user, "deb-dak"],
},
-"SIPackages" => {
- "key" => [\$SIPackages_key, "none"],
+"ClientPackages" => {
+ "key" => [\$ClientPackages_key, "none"],
},
+"ServerPackages"=> {
+ "address" => [\$foreign_server_string, ""],
+ "domain" => [\$server_domain, ""],
+ "key" => [\$ServerPackages_key, "none"],
+ "key-lifetime" => [\$foreign_servers_register_delay, 120],
+}
);
-c <file> : config file
-f : foreground, process will not be forked to background
-v : be verbose (multiple to increase verbosity)
- -no-bus : starts $prg without connection to bus
-no-arp : starts $prg without connection to arp module
EOF
if(not defined $level) { $level = 1 }
if(defined $log_file){
open(LOG_HANDLE, ">>$log_file");
+ chmod 0600, $log_file;
if(not defined open( LOG_HANDLE, ">>$log_file" )) {
print STDERR "cannot open $log_file: $!";
- return }
- chomp($msg);
- if($level <= $verbose){
- 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;
- my @monthnames = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
- $month = $monthnames[$month];
- $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
- $year+=1900;
- my $name = $prg;
-
- my $log_msg = "$month $monthday $hours:$minutes:$seconds $name $msg\n";
- print LOG_HANDLE $log_msg;
- if( $foreground ) {
- print STDERR $log_msg;
- }
+ return
+ }
+ chomp($msg);
+ $msg =~s/\n//g; # no newlines are allowed in log messages, this is important for later log parsing
+ if($level <= $verbose){
+ 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;
+ my @monthnames = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
+ $month = $monthnames[$month];
+ $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
+ $year+=1900;
+ my $name = $prg;
+
+ my $log_msg = "$month $monthday $hours:$minutes:$seconds $name $msg\n";
+ print LOG_HANDLE $log_msg;
+ if( $foreground ) {
+ print STDERR $log_msg;
}
+ }
close( LOG_HANDLE );
}
}
daemon_log(" ", 1);
if (not -e $modules_path) {
- daemon_log("ERROR: cannot find directory or directory is not readable: $modules_path", 1);
+ daemon_log("0 ERROR: cannot find directory or directory is not readable: $modules_path", 1);
}
opendir (DIR, $modules_path) or die "ERROR while loading modules from directory $modules_path : $!\n";
eval { require $file; };
if ($@) {
- daemon_log("ERROR: gosa-si-server could not load module $file", 1);
+ daemon_log("0 ERROR: gosa-si-server could not load module $file", 1);
daemon_log("$@", 5);
} else {
my $info = eval($mod_name.'::get_module_info()');
if( $info ) {
my ($input_address, $input_key, $input, $input_active, $input_type) = @{$info};
$known_modules->{$mod_name} = $info;
- daemon_log("INFO: module $mod_name loaded", 5);
+ daemon_log("0 INFO: module $mod_name loaded", 5);
}
}
}
}
my $host_key = $hit->{hostkey};
daemon_log("$session_id DEBUG: input_from_known_server: host_name: $host_name", 7);
- daemon_log("DEBUG: input_from_known_server: host_key: $host_key", 7);
+ daemon_log("$session_id DEBUG: input_from_known_server: host_key: $host_key", 7);
# check if module can open msg envelope with module key
my ($tmp_msg, $tmp_msg_hash) = &check_key_and_xml_validity($input, $host_key, $session_id);
else {
$msg = $tmp_msg;
$msg_hash = $tmp_msg_hash;
- $module = "SIPackages";
+ $module = "ServerPackages";
last;
}
}
next;
}
else {
- $module = "SIPackages";
+ $module = "ClientPackages";
last;
}
}
my $error_string;
my %act_modules = %$known_modules;
-
- while( my ($mod, $info) = each(%act_modules)) {
+
+ while( my ($mod, $info) = each(%act_modules)) {
# check a key exists for this module
my $module_key = ${$mod."_key"};
}
+# moved to GosaSupportDaemon: 03-06-2008: rettenbe
#=== 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_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 ($msg, $address, $encrypt_key, $msg_header, $session_id) = @_ ;
my $error = 0;
my $header;
+ my $timestamp = &get_time();
my $new_status;
my $act_status;
my ($sql_statement, $res);
print $socket $crypted_msg."\n";
daemon_log("$session_id INFO: send ".$header."msg to $address", 5);
- #daemon_log("DEBUG: message:\n$msg", 9);
+ daemon_log("$session_id DEBUG: message:\n$msg", 9);
}
# known_clients
- $sql_statement = "SELECT * FROM known_clients WHERE hostname='$address'";
+ $sql_statement = "SELECT * FROM $known_clients_tn 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" ) {
+ if( keys(%$res) == 1) {
+ $act_status = exists $res->{1}->{'status'} ? $res->{1}->{'status'} : "";
+ if ($act_status eq "down" && $new_status eq "down") {
$sql_statement = "DELETE FROM known_clients WHERE hostname='$address'";
$res = $known_clients_db->del_dbentry($sql_statement);
daemon_log("$session_id 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'";
+ $sql_statement = "UPDATE known_clients SET status='$new_status', timestamp='$timestamp' WHERE hostname='$address'";
$res = $known_clients_db->update_dbentry($sql_statement);
if($new_status eq "down"){
daemon_log("$session_id WARNING: set '$address' from status '$act_status' to '$new_status'", 3);
}
# known_server
- $sql_statement = "SELECT * FROM known_server WHERE hostname='$address'";
+ $sql_statement = "SELECT * FROM $known_server_tn 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" ) {
+ if( keys(%$res) == 1) {
+ $act_status = exists $res->{1}->{'status'} ? $res->{1}->{'status'} : "";
+ if ($act_status eq "down" && $new_status eq "down") {
$sql_statement = "DELETE FROM known_server WHERE hostname='$address'";
$res = $known_server_db->del_dbentry($sql_statement);
- daemon_log("$session_id WARNING: failed 2x to a send msg to host '$address', delete host from known_server", 3);
+ daemon_log("$session_id WARNING: failed 2x to send a message to host '$address', delete host from known_server", 3);
}
else {
- $sql_statement = "UPDATE known_server SET status='$new_status' WHERE hostname='$address'";
+ $sql_statement = "UPDATE known_server SET status='$new_status', timestamp='$timestamp' WHERE hostname='$address'";
$res = $known_server_db->update_dbentry($sql_statement);
if($new_status eq "down"){
daemon_log("$session_id WARNING: set '$address' from status '$act_status' to '$new_status'", 3);
- }
- else {
+ } else {
daemon_log("$session_id INFO: set '$address' from status '$act_status' to '$new_status'", 5);
}
}
if (not $answer =~ /<header>trigger_action_reinstall<\/header>/) {
my $sql_statement = "UPDATE $job_queue_tn ".
"SET status='error', result='can not deliver msg, please consult log file' ".
- "WHERE id='$jobdb_id'";
+ "WHERE id=$jobdb_id";
my $res = $job_db->update_dbentry($sql_statement);
}
} else {
my $sql_statement = "UPDATE $job_queue_tn ".
"SET status='done' ".
- "WHERE id='$jobdb_id' AND status='processed'";
+ "WHERE id=$jobdb_id AND status='processed'";
my $res = $job_db->update_dbentry($sql_statement);
}
}
}
-sub _start {
- my ($kernel) = $_[KERNEL];
- &trigger_db_loop($kernel);
- $global_kernel = $kernel;
- $kernel->yield('create_fai_server_db', $fai_server_tn );
- $kernel->yield('create_fai_release_db', $fai_release_tn );
- $kernel->sig(USR1 => "sig_handler");
- $kernel->sig(USR2 => "create_packages_list_db");
-}
sub sig_handler {
my ($kernel, $signal) = @_[KERNEL, ARG0] ;
return;
}
-sub next_task {
- my ($session, $heap) = @_[SESSION, HEAP];
- while ( keys( %{ $heap->{task} } ) < $max_children ) {
- my $next_task = shift @tasks;
- last unless defined $next_task;
+sub msg_to_decrypt {
+ my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
+ my $session_id = $session->ID;
+ my ($msg, $msg_hash, $module);
+ my $error = 0;
- my $task = POE::Wheel::Run->new(
- Program => sub { process_task($session, $heap, $next_task) },
- StdioFilter => POE::Filter::Reference->new(),
- StdoutEvent => "task_result",
- StderrEvent => "task_debug",
- CloseEvent => "task_done",
- );
+ # hole neue msg aus @msgs_to_decrypt
+ my $next_msg = shift @msgs_to_decrypt;
+
+ # entschlüssle sie
- $heap->{task}->{ $task->ID } = $task;
+ # msg is from a new client or gosa
+ ($msg, $msg_hash, $module) = &input_from_unknown_host($next_msg, $session_id);
+ # msg is from a gosa-si-server
+ if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
+ ($msg, $msg_hash, $module) = &input_from_known_server($next_msg, $heap->{'remote_ip'}, $session_id);
+ }
+ # msg is from a gosa-si-client
+ if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
+ ($msg, $msg_hash, $module) = &input_from_known_client($next_msg, $heap->{'remote_ip'}, $session_id);
}
+ # an error occurred
+ if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
+ # if an incoming msg could not be decrypted (maybe a wrong key), send client a ping. If the client
+ # could not understand a msg from its server the client cause a re-registering process
+ daemon_log("$session_id INFO cannot understand incoming msg, send 'ping'-msg to all host with ip '".$heap->{remote_ip}.
+ "' to cause a re-registering of the client if necessary", 5);
+ my $sql_statement = "SELECT * FROM $main::known_clients_tn WHERE (hostname LIKE '".$heap->{'remote_ip'}."%')";
+ 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'};
+ my $ping_msg = "<xml> <header>gosa_ping</header> <source>$server_address</source> <target>$host_name</target></xml>";
+ my $error = &send_msg_to_target($ping_msg, $host_name, $host_key, "gosa_ping", $session_id);
+ &update_jobdb_status_for_send_msgs($ping_msg, $error);
+ }
+ $error++;
+ }
+
+
+ my $header;
+ my $target;
+ my $source;
+ my $done = 0;
+ my $sql;
+ my $res;
+
+ # check whether this message should be processed here
+ if ($error == 0) {
+ $header = @{$msg_hash->{'header'}}[0];
+ $target = @{$msg_hash->{'target'}}[0];
+ $source = @{$msg_hash->{'source'}}[0];
+ my $not_found_in_known_clients_db = 0;
+ my $not_found_in_known_server_db = 0;
+ my $not_found_in_foreign_clients_db = 0;
+ my $local_address;
+ my ($target_ip, $target_port) = split(':', $target);
+ if ($target =~ /^\d+\.\d+\.\d+\.\d+:\d+$/) {
+ $local_address = &get_local_ip_for_remote_ip($target_ip).":$server_port";
+ } else {
+ $local_address = $server_address;
+ }
+
+ # target and source is equal to GOSA -> process here
+ if (not $done) {
+ if ($target eq "GOSA" && $source eq "GOSA") {
+ $done = 1;
+ }
+ }
+
+ # target is own address without forward_to_gosa-tag -> process here
+ if (not $done) {
+ if (($target eq $local_address) && (not exists $msg_hash->{'forward_to_gosa'})) {
+ $done = 1;
+ if ($source eq "GOSA") {
+ $msg =~ s/<\/xml>/<forward_to_gosa>$local_address,$session_id<\/forward_to_gosa><\/xml>/;
+ }
+ #print STDERR "target is own address without forward_to_gosa-tag -> process here\n";
+ }
+ }
+
+ # target is a client address in known_clients -> process here
+ if (not $done) {
+ $sql = "SELECT * FROM $known_clients_tn WHERE (hostname='$target' OR macaddress LIKE '$target')";
+ $res = $known_clients_db->select_dbentry($sql);
+ if (keys(%$res) > 0) {
+ $done = 1;
+ my $hostname = $res->{1}->{'hostname'};
+ $msg =~ s/<target>$target<\/target>/<target>$hostname<\/target>/;
+ #print STDERR "target is a client address in known_clients -> process here\n";
+ } else {
+ $not_found_in_known_clients_db = 1;
+ }
+ }
+
+ # target ist own address with forward_to_gosa-tag not pointing to myself -> process here
+ if (not $done) {
+ my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
+ my $gosa_at;
+ my $gosa_session_id;
+ if (($target eq $local_address) && (defined $forward_to_gosa)){
+ my ($gosa_at, $gosa_session_id) = split(/,/, $forward_to_gosa);
+ if ($gosa_at ne $local_address) {
+ $done = 1;
+ #print STDERR "target is own address with forward_to_gosa-tag not pointing to myself -> process here\n";
+ }
+ }
+ }
+
+ # if message should be processed here -> add message to incoming_db
+ if ($done) {
+ # if a job or a gosa message comes from a foreign server, fake module to GosaPackages
+ # so gosa-si-server knows how to process this kind of messages
+ if ($header =~ /^gosa_/ || $header =~ /job_/) {
+ $module = "GosaPackages";
+ }
+
+ my $res = $incoming_db->add_dbentry( {table=>$incoming_tn,
+ primkey=>[],
+ headertag=>$header,
+ targettag=>$target,
+ xmlmessage=>&encode_base64($msg),
+ timestamp=>&get_time,
+ module=>$module,
+ sessionid=>$session_id,
+ } );
+ }
+
+ # target is own address with forward_to_gosa-tag pointing at myself -> forward to gosa
+ if (not $done) {
+ my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
+ my $gosa_at;
+ my $gosa_session_id;
+ if (($target eq $local_address) && (defined $forward_to_gosa)){
+ my ($gosa_at, $gosa_session_id) = split(/,/, $forward_to_gosa);
+ if ($gosa_at eq $local_address) {
+ my $session_reference = $kernel->ID_id_to_session($gosa_session_id);
+ if( defined $session_reference ) {
+ $heap = $session_reference->get_heap();
+ }
+ if(exists $heap->{'client'}) {
+ $msg = &encrypt_msg($msg, $GosaPackages_key);
+ $heap->{'client'}->put($msg);
+ }
+ $done = 1;
+ #print STDERR "target is own address with forward_to_gosa-tag pointing at myself -> forward to gosa\n";
+ }
+ }
+
+ }
+
+ # target is a client address in foreign_clients -> forward to registration server
+ if (not $done) {
+ $sql = "SELECT * FROM $foreign_clients_tn WHERE (hostname='$target' OR macaddress LIKE '$target')";
+ $res = $foreign_clients_db->select_dbentry($sql);
+ if (keys(%$res) > 0) {
+ my $hostname = $res->{1}->{'hostname'};
+ my $regserver = $res->{1}->{'regserver'};
+ my $sql = "SELECT * FROM $known_server_tn WHERE hostname='$regserver'";
+ my $res = $known_server_db->select_dbentry($sql);
+ if (keys(%$res) > 0) {
+ my $regserver_key = $res->{1}->{'hostkey'};
+ $msg =~ s/<source>GOSA<\/source>/<source>$local_address<\/source>/;
+ $msg =~ s/<target>$target<\/target>/<target>$hostname<\/target>/;
+ if ($source eq "GOSA") {
+ $msg =~ s/<\/xml>/<forward_to_gosa>$local_address,$session_id<\/forward_to_gosa><\/xml>/;
+ }
+ &send_msg_to_target($msg, $regserver, $regserver_key, $header, $session_id);
+ }
+ $done = 1;
+ #print STDERR "target is a client address in foreign_clients -> forward to registration server\n";
+ } else {
+ $not_found_in_foreign_clients_db = 1;
+ }
+ }
+
+ # target is a server address -> forward to server
+ if (not $done) {
+ $sql = "SELECT * FROM $known_server_tn WHERE hostname='$target'";
+ $res = $known_server_db->select_dbentry($sql);
+ if (keys(%$res) > 0) {
+ my $hostkey = $res->{1}->{'hostkey'};
+
+ if ($source eq "GOSA") {
+ $msg =~ s/<source>GOSA<\/source>/<source>$local_address<\/source>/;
+ $msg =~ s/<\/xml>/<forward_to_gosa>$local_address,$session_id<\/forward_to_gosa><\/xml>/;
+
+ }
+
+ &send_msg_to_target($msg, $target, $hostkey, $header, $session_id);
+ $done = 1;
+ #print STDERR "target is a server address -> forward to server\n";
+ } else {
+ $not_found_in_known_server_db = 1;
+ }
+ }
+
+
+ # target is not in foreign_clients_db, known_server_db or known_clients_db, maybe it is a complete new one -> process here
+ if ( $not_found_in_foreign_clients_db
+ && $not_found_in_known_server_db
+ && $not_found_in_known_clients_db) {
+ my $res = $incoming_db->add_dbentry( {table=>$incoming_tn,
+ primkey=>[],
+ headertag=>$header,
+ targettag=>$target,
+ xmlmessage=>&encode_base64($msg),
+ timestamp=>&get_time,
+ module=>$module,
+ sessionid=>$session_id,
+ } );
+ $done = 1;
+ }
+
+
+ if (not $done) {
+ daemon_log("$session_id ERROR: do not know what to do with this message: $msg", 1);
+ if ($source eq "GOSA") {
+ my %data = ('error_msg' => &encode_base64($msg), 'error_string' => "Do not know what to do with this message!");
+ my $error_msg = &build_msg("error", $local_address, "GOSA", \%data );
+
+ my $session_reference = $kernel->ID_id_to_session($session_id);
+ if( defined $session_reference ) {
+ $heap = $session_reference->get_heap();
+ }
+ if(exists $heap->{'client'}) {
+ $error_msg = &encrypt_msg($error_msg, $GosaPackages_key);
+ $heap->{'client'}->put($error_msg);
+ }
+ }
+ }
+
+ }
+
+ return;
+}
+
+
+sub next_task {
+ my ($session, $heap, $task) = @_[SESSION, HEAP, ARG0];
+ my $running_task = POE::Wheel::Run->new(
+ Program => sub { process_task($session, $heap, $task) },
+ StdioFilter => POE::Filter::Reference->new(),
+ StdoutEvent => "task_result",
+ StderrEvent => "task_debug",
+ CloseEvent => "task_done",
+ );
+ $heap->{task}->{ $running_task->ID } = $running_task;
}
sub handle_task_result {
sub handle_task_done {
my ( $kernel, $heap, $task_id ) = @_[ KERNEL, HEAP, ARG0 ];
delete $heap->{task}->{$task_id};
- $kernel->yield("next_task");
}
sub process_task {
no strict "refs";
- my ($session, $heap, $input) = @_;
- my $session_id = $session->ID;
- my ($msg, $msg_hash, $module);
+ my ($session, $heap, $task) = @_;
my $error = 0;
my $answer_l;
my ($answer_header, @answer_target_l, $answer_source);
my $client_answer = "";
- daemon_log("", 5);
- daemon_log("$session_id INFO: Incoming msg with session ID $session_id from '".$heap->{'remote_ip'}."'", 5);
- daemon_log("$session_id DEBUG: Incoming msg:\n$input", 9);
-
- ####################
- # check incoming msg
- # msg is from a new client or gosa
- ($msg, $msg_hash, $module) = &input_from_unknown_host($input, $session_id);
- # 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'}, $session_id);
- }
- # msg is from a gosa-si-client
- if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- ($msg, $msg_hash, $module) = &input_from_known_client($input, $heap->{'remote_ip'}, $session_id);
- }
- # an error occurred
- if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- # if an incoming msg could not be decrypted (maybe a wrong key), send client a ping. If the client
- # could not understand a msg from its server the client cause a re-registering process
- daemon_log("$session_id INFO cannot understand incoming msg, send 'ping'-msg to all host with ip '".$heap->{remote_ip}."' to cause a re-registering of the client if necessary", 5);
- my $sql_statement = "SELECT * FROM $main::known_clients_tn WHERE (hostname LIKE '".$heap->{'remote_ip'}."%')";
- 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'};
- my $ping_msg = "<xml> <header>gosa_ping</header> <source>$server_address</source> <target>$host_name</target></xml>";
- my $error = &send_msg_to_target($ping_msg, $host_name, $host_key, "gosa_ping", $session_id);
- &update_jobdb_status_for_send_msgs($ping_msg, $error);
- }
- $error++;
- }
+ # prepare all variables needed to process message
+ #my $msg = $task->{'xmlmessage'};
+ my $msg = &decode_base64($task->{'xmlmessage'});
+ my $incoming_id = $task->{'id'};
+ my $module = $task->{'module'};
+ my $header = $task->{'headertag'};
+ my $session_id = $task->{'sessionid'};
+ my $msg_hash = $xml->XMLin($msg, ForceArray=>1);
+ my $source = @{$msg_hash->{'source'}}[0];
+
+ # set timestamp of incoming client uptodate, so client will not
+ # be deleted from known_clients because of expiration
+ my $act_time = &get_time();
+ my $sql = "UPDATE $known_clients_tn SET timestamp='$act_time' WHERE hostname='$source'";
+ my $res = $known_clients_db->exec_statement($sql);
######################
# process incoming msg
if( $error == 0) {
- daemon_log("$session_id INFO: Incoming msg with header '".@{$msg_hash->{'header'}}[0].
- "' from '".$heap->{'remote_ip'}."'", 5);
+ daemon_log("$session_id INFO: Incoming msg (session_id=$session_id) with header '".@{$msg_hash->{'header'}}[0]."'", 5);
daemon_log("$session_id DEBUG: Processing module ".$module, 7);
$answer_l = &{ $module."::process_incoming_msg" }($msg, $msg_hash, $session_id);
if ( 0 < @{$answer_l} ) {
my $answer_str = join("\n", @{$answer_l});
- daemon_log("$session_id DEBUG: $module: Got answer from module: \n".$answer_str,8);
+ while ($answer_str =~ /<header>(\w+)<\/header>/g) {
+ daemon_log("$session_id INFO: got answer message with header '$1'", 5);
+ }
+ daemon_log("$session_id DEBUG: $module: got answer from module: \n".$answer_str,8);
+ } else {
+ daemon_log("$session_id DEBUG: $module: got no answer from module!" ,8);
}
+
}
if( !$answer_l ) { $error++ };
if( $error == 0 ) {
foreach my $answer ( @{$answer_l} ) {
- # 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;
- }
+ if( not defined $answer_hash ) { next; }
$answer_header = @{$answer_hash->{'header'}}[0];
@answer_target_l = @{$answer_hash->{'target'}};
# 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 $sql_statement= "SELECT * FROM $known_server_tn";
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;
+ $answer =~ s/<target>\S+<\/target>/<target>$host_name<\/target>/g;
my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header, $session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
if( $found_ip_flag == 0) {
daemon_log("$session_id WARNING: no host found in known_clients with mac address '$answer_target'", 3);
- if( $bus_activ eq "true" ) {
- daemon_log("$session_id 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};
- my $error = &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header, $session_id);
- &update_jobdb_status_for_send_msgs($answer, $error);
- last;
- }
- }
-
}
# answer is for one specific host
# get encrypt_key
my $encrypt_key = &get_encrypt_key($answer_target);
if( not defined $encrypt_key ) {
- # unknown target, forward msg to bus
+ # unknown target
daemon_log("$session_id WARNING: unknown target '$answer_target'", 3);
- if( $bus_activ eq "true" ) {
- daemon_log("$session_id 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("$session_id 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};
- my $error = &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header,$session_id );
- &update_jobdb_status_for_send_msgs($answer, $error);
- }
- }
- }
next;
}
my $error = &send_msg_to_target($answer, $answer_target, $encrypt_key, $answer_header,$session_id);
}
-
-sub trigger_db_loop {
- my ($kernel) = @_ ;
+sub session_start {
+ my ($kernel) = $_[KERNEL];
+ $global_kernel = $kernel;
+ $kernel->yield('register_at_foreign_servers');
+ $kernel->yield('create_fai_server_db', $fai_server_tn );
+ $kernel->yield('create_fai_release_db', $fai_release_tn );
+ $kernel->yield('watch_for_next_tasks');
+ $kernel->sig(USR1 => "sig_handler");
+ $kernel->sig(USR2 => "recreate_packages_db");
$kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
$kernel->delay_set('watch_for_done_jobs', $job_queue_loop_delay);
+ $kernel->delay_set('watch_for_new_messages', $messaging_db_loop_delay);
+ $kernel->delay_set('watch_for_delivery_messages', $messaging_db_loop_delay);
+ $kernel->delay_set('watch_for_done_messages', $messaging_db_loop_delay);
+ $kernel->delay_set('watch_for_old_known_clients', $job_queue_loop_delay);
+
}
+
sub watch_for_done_jobs {
my ($kernel,$heap) = @_[KERNEL, HEAP];
- my $sql_statement = "SELECT * FROM ".$job_queue_tn.
- " WHERE status='done'";
+ my $sql_statement = "SELECT * FROM ".$job_queue_tn." WHERE status='done'";
my $res = $job_db->select_dbentry( $sql_statement );
while( my ($id, $hit) = each %{$res} ) {
my $jobdb_id = $hit->{id};
- my $sql_statement = "DELETE FROM $job_queue_tn WHERE id='$jobdb_id'";
- my $res = $job_db->del_dbentry($sql_statement);
+ my $sql_statement = "DELETE FROM $job_queue_tn WHERE id=$jobdb_id";
+ my $res = $job_db->del_dbentry($sql_statement);
}
$kernel->delay_set('watch_for_done_jobs',$job_queue_loop_delay);
}
+
sub watch_for_new_jobs {
- my ($kernel,$heap) = @_[KERNEL, HEAP];
-
- # check gosa job queue for jobs with executable timestamp
- my $timestamp = &get_time();
- my $sql_statement = "SELECT * FROM $job_queue_tn WHERE status='waiting' AND (CAST (timestamp AS INTEGER) + 120) < $timestamp ORDER BY timestamp";
- my $res = $job_db->exec_statement( $sql_statement );
-
- # Merge all new jobs that would do the same actions
- my @drops;
- my $hits;
- foreach my $hit (reverse @{$res} ) {
- my $macaddress= lc @{$hit}[8];
- my $headertag= @{$hit}[5];
- if(defined($hits->{$macaddress}->{$headertag})) {
- push @drops, "DELETE FROM $job_queue_tn WHERE id = '$hits->{$macaddress}->{$headertag}[0]'";
+ if($watch_for_new_jobs_in_progress == 0) {
+ $watch_for_new_jobs_in_progress = 1;
+ my ($kernel,$heap) = @_[KERNEL, HEAP];
+
+ # check gosa job queue for jobs with executable timestamp
+ my $timestamp = &get_time();
+ my $sql_statement = "SELECT * FROM $job_queue_tn WHERE status='waiting' AND (CAST (timestamp AS INTEGER)) < $timestamp ORDER BY timestamp";
+ my $res = $job_db->exec_statement( $sql_statement );
+
+ # Merge all new jobs that would do the same actions
+ my @drops;
+ my $hits;
+ foreach my $hit (reverse @{$res} ) {
+ my $macaddress= lc @{$hit}[8];
+ my $headertag= @{$hit}[5];
+ if(
+ defined($hits->{$macaddress}) &&
+ defined($hits->{$macaddress}->{$headertag}) &&
+ defined($hits->{$macaddress}->{$headertag}[0])
+ ) {
+ push @drops, "DELETE FROM $job_queue_tn WHERE id = $hits->{$macaddress}->{$headertag}[0]";
+ }
+ $hits->{$macaddress}->{$headertag}= $hit;
}
- $hits->{$macaddress}->{$headertag}= $hit;
- }
- # Delete new jobs with a matching job in state 'processing'
- foreach my $macaddress (keys %{$hits}) {
- foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) {
- my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0];
- my $sql_statement = "SELECT * FROM $job_queue_tn WHERE macaddress LIKE '$macaddress' AND headertag='$jobdb_headertag' AND status='processing'";
- my $res = $job_db->exec_statement( $sql_statement );
- foreach my $hit (@{$res}) {
- push @drops, "DELETE FROM $job_queue_tn WHERE id = '$jobdb_id'";
+ # Delete new jobs with a matching job in state 'processing'
+ foreach my $macaddress (keys %{$hits}) {
+ foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) {
+ my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0];
+ if(defined($jobdb_id)) {
+ my $sql_statement = "SELECT * FROM $job_queue_tn WHERE macaddress LIKE '$macaddress' AND headertag='$jobdb_headertag' AND status='processing'";
+ my $res = $job_db->exec_statement( $sql_statement );
+ foreach my $hit (@{$res}) {
+ push @drops, "DELETE FROM $job_queue_tn WHERE id=$jobdb_id";
+ }
+ } else {
+ daemon_log("J ERROR: Job without id exists for macaddress $macaddress!", 1);
+ }
}
}
- }
- # Commit deletion
- $job_db->exec_statementlist(\@drops);
+ # Commit deletion
+ $job_db->exec_statementlist(\@drops);
- # Look for new jobs that could be executed
- foreach my $macaddress (keys %{$hits}) {
+ # Look for new jobs that could be executed
+ foreach my $macaddress (keys %{$hits}) {
- # Look if there is an executing job
- my $sql_statement = "SELECT * FROM $job_queue_tn WHERE macaddress LIKE '$macaddress' AND status='processing'";
- my $res = $job_db->exec_statement( $sql_statement );
+ # Look if there is an executing job
+ my $sql_statement = "SELECT * FROM $job_queue_tn WHERE macaddress LIKE '$macaddress' AND status='processing'";
+ my $res = $job_db->exec_statement( $sql_statement );
- # Skip new jobs for host if there is a processing job
- if(defined($res) and defined @{$res}[0]) {
- next;
+ # Skip new jobs for host if there is a processing job
+ if(defined($res) and defined @{$res}[0]) {
+ next;
+ }
+
+ foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) {
+ my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0];
+ if(defined($jobdb_id)) {
+ my $job_msg = @{$hits->{$macaddress}->{$jobdb_headertag}}[7];
+
+ daemon_log("J DEBUG: its time to execute $job_msg", 7);
+ my $sql_statement = "SELECT * FROM known_clients WHERE macaddress LIKE '$macaddress'";
+ my $res_hash = $known_clients_db->select_dbentry( $sql_statement );
+
+ # expect macaddress is unique!!!!!!
+ my $target = $res_hash->{1}->{hostname};
+
+ # change header
+ $job_msg =~ s/<header>job_/<header>gosa_/;
+
+ # add sqlite_id
+ $job_msg =~ s/<\/xml>$/<jobdb_id>$jobdb_id<\/jobdb_id><\/xml>/;
+
+ $job_msg =~ /<header>(\S+)<\/header>/;
+ my $header = $1 ;
+ my $func_error = &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header, "J");
+
+ # update status in job queue to 'processing'
+ $sql_statement = "UPDATE $job_queue_tn SET status='processing' WHERE id=$jobdb_id";
+ my $res = $job_db->update_dbentry($sql_statement);
+# TODO: abfangen ob alles in ordnung ist oder nicht, wenn nicht error schmeißen
+
+ # We don't want parallel processing
+ last;
+ }
+ }
}
- foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) {
- my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0];
- my $job_msg = @{$hits->{$macaddress}->{$jobdb_headertag}}[7];
+ $watch_for_new_jobs_in_progress = 0;
+ $kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
+ }
+}
- daemon_log("J DEBUG: its time to execute $job_msg", 7);
- my $sql_statement = "SELECT * FROM known_clients WHERE macaddress LIKE '$macaddress'";
- my $res_hash = $known_clients_db->select_dbentry( $sql_statement );
- # expect macaddress is unique!!!!!!
- my $target = $res_hash->{1}->{hostname};
+sub watch_for_new_messages {
+ my ($kernel,$heap) = @_[KERNEL, HEAP];
+ my @coll_user_msg; # collection list of outgoing messages
+
+ # check messaging_db for new incoming messages with executable timestamp
+ my $timestamp = &get_time();
+ my $sql_statement = "SELECT * FROM $messaging_tn WHERE ( (CAST(timestamp AS INTEGER))<$timestamp AND flag='n' AND direction='in' )";
+ my $res = $messaging_db->exec_statement( $sql_statement );
+ foreach my $hit (@{$res}) {
+
+ # create outgoing messages
+ my $message_to = @{$hit}[3];
+ # translate message_to to plain login name
+ my @message_to_l = split(/,/, $message_to);
+ my %receiver_h;
+ foreach my $receiver (@message_to_l) {
+ if ($receiver =~ /^u_([\s\S]*)$/) {
+ $receiver_h{$1} = 0;
+ } elsif ($receiver =~ /^g_([\s\S]*)$/) {
+ my $group_name = $1;
+ # fetch all group members from ldap and add them to receiver hash
+ my $ldap_handle = &get_ldap_handle();
+ if (defined $ldap_handle) {
+ my $mesg = $ldap_handle->search(
+ base => $ldap_base,
+ scope => 'sub',
+ attrs => ['memberUid'],
+ filter => "cn=$group_name",
+ );
+ if ($mesg->count) {
+ my @entries = $mesg->entries;
+ foreach my $entry (@entries) {
+ my @receivers= $entry->get_value("memberUid");
+ foreach my $receiver (@receivers) {
+ $receiver_h{$1} = 0;
+ }
+ }
+ }
+ # translating errors ?
+ if ($mesg->code) {
+ daemon_log("M ERROR: unable to translate group '$group_name' to user list for message delivery: $mesg->error", 1);
+ }
+ # ldap handle error ?
+ } else {
+ daemon_log("M ERROR: unable to translate group '$group_name' to user list for message delivery: no ldap handle available", 1);
+ }
+ } else {
+ my $sbjct = &encode_base64(@{$hit}[1]);
+ my $msg = &encode_base64(@{$hit}[7]);
+ &daemon_log("M WARNING: unknown receiver '$receiver' for a user-message '$sbjct - $msg'", 3);
+ }
+ }
+ my @receiver_l = keys(%receiver_h);
+
+ my $message_id = @{$hit}[0];
+
+ #add each outgoing msg to messaging_db
+ my $receiver;
+ foreach $receiver (@receiver_l) {
+ my $sql_statement = "INSERT INTO $messaging_tn (id, subject, message_from, message_to, flag, direction, delivery_time, message, timestamp) ".
+ "VALUES ('".
+ $message_id."', '". # id
+ @{$hit}[1]."', '". # subject
+ @{$hit}[2]."', '". # message_from
+ $receiver."', '". # message_to
+ "none"."', '". # flag
+ "out"."', '". # direction
+ @{$hit}[6]."', '". # delivery_time
+ @{$hit}[7]."', '". # message
+ $timestamp."'". # timestamp
+ ")";
+ &daemon_log("M DEBUG: $sql_statement", 1);
+ my $res = $messaging_db->exec_statement($sql_statement);
+ &daemon_log("M INFO: message '".@{$hit}[0]."' is prepared for delivery to receiver '$receiver'", 5);
+ }
- # change header
- $job_msg =~ s/<header>job_/<header>gosa_/;
+ # set incoming message to flag d=deliverd
+ $sql_statement = "UPDATE $messaging_tn SET flag='p' WHERE id='$message_id'";
+ &daemon_log("M DEBUG: $sql_statement", 7);
+ $res = $messaging_db->update_dbentry($sql_statement);
+ &daemon_log("M INFO: message '$message_id' is set to flag 'p' (processed)", 5);
+ }
- # add sqlite_id
- $job_msg =~ s/<\/xml>$/<jobdb_id>$jobdb_id<\/jobdb_id><\/xml>/;
+ $kernel->delay_set('watch_for_new_messages', $messaging_db_loop_delay);
+ return;
+}
- $job_msg =~ /<header>(\S+)<\/header>/;
- my $header = $1 ;
- my $func_error = &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header, "J");
+sub watch_for_delivery_messages {
+ my ($kernel, $heap) = @_[KERNEL, HEAP];
- # update status in job queue to 'processing'
- $sql_statement = "UPDATE $job_queue_tn SET status='processing' WHERE id='$jobdb_id'";
- my $res = $job_db->update_dbentry($sql_statement);
+ # select outgoing messages
+ my $sql_statement = "SELECT * FROM $messaging_tn WHERE ( flag='p' AND direction='out' )";
+ #&daemon_log("0 DEBUG: $sql", 7);
+ my $res = $messaging_db->exec_statement( $sql_statement );
+
+ # build out msg for each usr
+ foreach my $hit (@{$res}) {
+ my $receiver = @{$hit}[3];
+ my $msg_id = @{$hit}[0];
+ my $subject = @{$hit}[1];
+ my $message = @{$hit}[7];
+
+ # resolve usr -> host where usr is logged in
+ my $sql = "SELECT * FROM $login_users_tn WHERE (user='$receiver')";
+ #&daemon_log("0 DEBUG: $sql", 7);
+ my $res = $login_users_db->exec_statement($sql);
+
+ # reciver is logged in nowhere
+ if (not ref(@$res[0]) eq "ARRAY") { next; }
+
+ my $send_succeed = 0;
+ foreach my $hit (@$res) {
+ my $receiver_host = @$hit[0];
+ &daemon_log("M DEBUG: user '$receiver' is logged in at host '$receiver_host'", 7);
+
+ # fetch key to encrypt msg propperly for usr/host
+ my $sql = "SELECT * FROM $known_clients_tn WHERE (hostname='$receiver_host')";
+ &daemon_log("0 DEBUG: $sql", 7);
+ my $res = $known_clients_db->exec_statement($sql);
+
+ # host is already down
+ if (not ref(@$res[0]) eq "ARRAY") { next; }
+
+ # host is on
+ my $receiver_key = @{@{$res}[0]}[2];
+ my %data = ('subject' => $subject, 'message' => $message, 'usr' => $receiver);
+ my $out_msg = &build_msg("usr_msg", $server_address, $receiver_host, \%data );
+ my $error = &send_msg_to_target($out_msg, $receiver_host, $receiver_key, "usr_msg", 0);
+ if ($error == 0 ) {
+ $send_succeed++ ;
+ }
+ }
- # We don't want parallel processing
- last;
+ if ($send_succeed) {
+ # set outgoing msg at db to deliverd
+ my $sql = "UPDATE $messaging_tn SET flag='d' WHERE (id='$msg_id' AND direction='out' AND message_to='$receiver')";
+ &daemon_log("0 DEBUG: $sql", 7);
+ my $res = $messaging_db->exec_statement($sql);
}
}
- $kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
+ $kernel->delay_set('watch_for_delivery_messages', $messaging_db_loop_delay);
+ return;
+}
+
+
+sub watch_for_done_messages {
+ my ($kernel,$heap) = @_[KERNEL, HEAP];
+
+ my $sql = "SELECT * FROM $messaging_tn WHERE (flag='p' AND direction='in')";
+ #&daemon_log("0 DEBUG: $sql", 7);
+ my $res = $messaging_db->exec_statement($sql);
+
+ foreach my $hit (@{$res}) {
+ my $msg_id = @{$hit}[0];
+
+ my $sql = "SELECT * FROM $messaging_tn WHERE (id='$msg_id' AND direction='out' AND (NOT flag='s'))";
+ #&daemon_log("0 DEBUG: $sql", 7);
+ my $res = $messaging_db->exec_statement($sql);
+
+ # not all usr msgs have been seen till now
+ if ( ref(@$res[0]) eq "ARRAY") { next; }
+
+ $sql = "DELETE FROM $messaging_tn WHERE (id='$msg_id')";
+ #&daemon_log("0 DEBUG: $sql", 7);
+ $res = $messaging_db->exec_statement($sql);
+
+ }
+
+ $kernel->delay_set('watch_for_done_messages', $messaging_db_loop_delay);
+ return;
+}
+
+
+sub watch_for_old_known_clients {
+ my ($kernel,$heap) = @_[KERNEL, HEAP];
+
+ my $sql_statement = "SELECT * FROM $known_clients_tn";
+ my $res = $known_clients_db->select_dbentry( $sql_statement );
+
+ my $act_time = int(&get_time());
+
+ while ( my ($hit_num, $hit) = each %$res) {
+ my $expired_timestamp = int($hit->{'timestamp'});
+ $expired_timestamp =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/;
+ my $dt = DateTime->new( year => $1,
+ month => $2,
+ day => $3,
+ hour => $4,
+ minute => $5,
+ second => $6,
+ );
+
+ $dt->add( seconds => 2 * int($hit->{'keylifetime'}) );
+ $expired_timestamp = $dt->ymd('').$dt->hms('')."\n";
+ if ($act_time > $expired_timestamp) {
+ my $hostname = $hit->{'hostname'};
+ my $del_sql = "DELETE FROM $known_clients_tn WHERE hostname='$hostname'";
+ my $del_res = $known_clients_db->exec_statement($del_sql);
+
+ &main::daemon_log("0 INFO: timestamp '".$hit->{'timestamp'}."' of client '$hostname' is expired('$expired_timestamp'), client will be deleted from known_clients_db", 5);
+ }
+
+ }
+
+ $kernel->delay_set('watch_for_old_known_clients', $job_queue_loop_delay);
+}
+
+
+sub watch_for_next_tasks {
+ my ($kernel,$heap) = @_[KERNEL, HEAP];
+
+ my $sql = "SELECT * FROM $incoming_tn";
+ my $res = $incoming_db->select_dbentry($sql);
+
+ while ( my ($hit_num, $hit) = each %$res) {
+ my $headertag = $hit->{'headertag'};
+ if ($headertag =~ /^answer_(\d+)/) {
+ # do not start processing, this message is for a still running POE::Wheel
+ next;
+ }
+ my $message_id = $hit->{'id'};
+ $kernel->yield('next_task', $hit);
+
+ my $sql = "DELETE FROM $incoming_tn WHERE id=$message_id";
+ my $res = $incoming_db->exec_statement($sql);
+ }
+
+ $kernel->delay_set('watch_for_next_tasks', 0.1);
}
my $ldap_handle;
if (not defined $session_id ) { $session_id = 0 };
+ if ($session_id =~ /[^0-9]*/) { $session_id = 0 };
if ($session_id == 0) {
daemon_log("$session_id DEBUG: get_ldap_handle invoked without a session_id, create a new ldap_handle", 7);
$ldap_handle = Net::LDAP->new( $ldap_uri );
- $ldap_handle->bind($ldap_admin_dn, password => $ldap_admin_password);
+ $ldap_handle->bind($ldap_admin_dn, password => $ldap_admin_password) or daemon_log("$session_id ERROR: Bind to LDAP $ldap_uri as $ldap_admin_dn failed!");
} else {
my $session_reference = $global_kernel->ID_id_to_session($session_id);
# used handle is still valid - or if we've to reconnect...
#if (not exists $heap->{ldap_handle}) {
$ldap_handle = Net::LDAP->new( $ldap_uri );
- $ldap_handle->bind($ldap_admin_dn, password => $ldap_admin_password);
+ $ldap_handle->bind($ldap_admin_dn, password => $ldap_admin_password) or daemon_log("$session_id ERROR: Bind to LDAP $ldap_uri as $ldap_admin_dn failed!");
$heap->{ldap_handle} = $ldap_handle;
#}
}
if ($mesg->count) {
my @entries = $mesg->entries;
+ if (0 == @entries) {
+ daemon_log("$session_id ERROR: ldap search failed: ldap_base=$ldap_base, filter=$search", 1);
+ }
+
foreach my $entry (@entries) {
# Only modify entry if it is not set to '$state'
if ($entry->get_value("FAIstate") ne "$state"){
daemon_log("$session_id DEBUG FAIstate at host '".$entry->dn."' already at state '$st'", 7);
}
}
+ } else {
+ daemon_log("$session_id ERROR: LDAP search failed: ldap_base=$ldap_base, filter=$search", 1);
}
+
# if no ldap handle defined
} else {
daemon_log("$session_id ERROR: no LDAP handle defined for update FAIstate", 1);
}
+ return;
}
}
}
- }
+ } else {
+ daemon_log("$session_id ERROR: LDAP search failed in function change_goto_state: ldap_base=$ldap_base, filter=$search", 1);
+ }
}
}
+sub run_recreate_packages_db {
+ my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
+ my $session_id = $session->ID;
+ &main::daemon_log("$session_id INFO: Recreating FAI Packages DB ('$fai_release_tn', '$fai_server_tn', '$packages_list_tn')", 4);
+ $kernel->yield('create_fai_release_db');
+ $kernel->yield('create_fai_server_db');
+ return;
+}
+
+
+sub run_create_fai_server_db {
+ my ($kernel, $session, $heap, $table_name) = @_[KERNEL, SESSION, HEAP, ARG0];
+ my $session_id = $session->ID;
+ my $task = POE::Wheel::Run->new(
+ Program => sub { &create_fai_server_db($table_name,$kernel, undef, $session_id) },
+ StdoutEvent => "session_run_result",
+ StderrEvent => "session_run_debug",
+ CloseEvent => "session_run_done",
+ );
+
+ $heap->{task}->{ $task->ID } = $task;
+ return;
+}
+
+
sub create_fai_server_db {
- my ($table_name, $kernel) = @_;
+ my ($table_name, $kernel, $dont_create_packages_list, $session_id) = @_;
my $result;
+
+ if (not defined $session_id) { $session_id = 0; }
my $ldap_handle = &get_ldap_handle();
if(defined($ldap_handle)) {
- daemon_log("INFO: create_fai_server_db: start", 5);
+ daemon_log("$session_id INFO: create_fai_server_db: start", 5);
my $mesg= $ldap_handle->search(
base => $ldap_base,
scope => 'sub',
}
}
}
- daemon_log("INFO: create_fai_server_db: finished", 5);
+ daemon_log("$session_id INFO: create_fai_server_db: finished", 5);
# TODO: Find a way to post the 'create_packages_list_db' event
- &create_packages_list_db($ldap_handle);
+ if(not defined($dont_create_packages_list)) {
+ &create_packages_list_db(undef, undef, $session_id);
+ }
}
$ldap_handle->disconnect;
return $result;
}
-sub run_create_fai_server_db {
- my ($kernel, $session, $heap, $table_name) = @_[KERNEL, SESSION, HEAP, ARG0];
+
+sub run_create_fai_release_db {
+ my ($session, $heap, $table_name) = @_[SESSION, HEAP, ARG0];
+ my $session_id = $session->ID;
my $task = POE::Wheel::Run->new(
- Program => sub { &create_fai_server_db($table_name,$kernel) },
+ Program => sub { &create_fai_release_db($table_name, $session_id) },
StdoutEvent => "session_run_result",
StderrEvent => "session_run_debug",
CloseEvent => "session_run_done",
sub create_fai_release_db {
- my ($table_name) = @_;
+ my ($table_name, $session_id) = @_;
my $result;
+ # used for logging
+ if (not defined $session_id) { $session_id = 0; }
+
my $ldap_handle = &get_ldap_handle();
if(defined($ldap_handle)) {
- daemon_log("INFO: create_fai_release_db: start",5);
+ daemon_log("$session_id INFO: create_fai_release_db: start",5);
my $mesg= $ldap_handle->search(
base => $ldap_base,
scope => 'sub',
my @sql_list;
my $timestamp= &get_time();
foreach my $ou (@{$mesg->{entries}}) {
- my $tmp_classes= resolve_fai_classes($ou->dn, $ldap_handle);
+ my $tmp_classes= resolve_fai_classes($ou->dn, $ldap_handle, $session_id);
if(defined($tmp_classes) && ref($tmp_classes) eq 'HASH') {
my @tmp_array=get_fai_release_entries($tmp_classes);
if(@tmp_array) {
}
}
}
- daemon_log("DEBUG: Inserting ".scalar @sql_list." entries to DB",6);
+
+ daemon_log("$session_id DEBUG: Inserting ".scalar @sql_list." entries to DB",8);
if(@sql_list) {
+ unshift @sql_list, "VACUUM";
unshift @sql_list, "DELETE FROM $table_name";
- $fai_server_db->exec_statementlist(\@sql_list);
+ $fai_release_db->exec_statementlist(\@sql_list);
}
- daemon_log("DEBUG: Done with inserting",6);
+ daemon_log("$session_id DEBUG: Done with inserting",7);
}
- daemon_log("INFO: create_fai_release_db: finished",5);
+ daemon_log("$session_id INFO: create_fai_release_db: finished",5);
}
$ldap_handle->disconnect;
return $result;
}
-sub run_create_fai_release_db {
- my ($session, $heap, $table_name) = @_[SESSION, HEAP, ARG0];
- my $task = POE::Wheel::Run->new(
- Program => sub { &create_fai_release_db($table_name) },
- StdoutEvent => "session_run_result",
- StderrEvent => "session_run_debug",
- CloseEvent => "session_run_done",
- );
-
- $heap->{task}->{ $task->ID } = $task;
- return;
-}
sub get_fai_types {
my $tmp_classes = shift || return undef;
}
sub resolve_fai_classes {
- my ($fai_base, $ldap_handle) = @_;
+ my ($fai_base, $ldap_handle, $session_id) = @_;
+ if (not defined $session_id) { $session_id = 0; }
my $result;
my @possible_fai_classes= ("FAIscript", "FAIhook", "FAIpartitionTable", "FAItemplate", "FAIvariable", "FAIprofile", "FAIpackageList");
my $fai_filter= "(|(&(objectClass=FAIclass)(|(objectClass=".join(")(objectClass=", @possible_fai_classes).")))(objectClass=FAIbranch))";
my $fai_classes;
- daemon_log("DEBUG: Searching for FAI entries in base $fai_base",6);
+ daemon_log("$session_id DEBUG: Searching for FAI entries in base $fai_base",7);
my $mesg= $ldap_handle->search(
base => $fai_base,
scope => 'sub',
attrs => ['cn','objectClass','FAIstate'],
filter => $fai_filter,
);
- daemon_log("DEBUG: Found ".$mesg->count()." FAI entries",6);
+ daemon_log("$session_id DEBUG: Found ".$mesg->count()." FAI entries",7);
if($mesg->{'resultCode'} == 0 &&
$mesg->count != 0) {
delete $heap->{task}->{$task_id};
}
+
sub create_sources_list {
- my ($ldap_handle) = @_;
+ my $session_id = shift;
+ my $ldap_handle = &main::get_ldap_handle;
my $result="/tmp/gosa_si_tmp_sources_list";
# Remove old file
if(stat($result)) {
unlink($result);
+ &main::daemon_log("$session_id DEBUG: remove an old version of '$result'", 7);
}
my $fh;
- open($fh, ">$result") or return undef;
- if(defined($ldap_server_dn) and length($ldap_server_dn) > 0) {
+ open($fh, ">$result");
+ if (not defined $fh) {
+ &main::daemon_log("$session_id DEBUG: cannot open '$result' for writing", 7);
+ return undef;
+ }
+ if(defined($main::ldap_server_dn) and length($main::ldap_server_dn) > 0) {
my $mesg=$ldap_handle->search(
- base => $ldap_server_dn,
- scope => 'base',
- attrs => 'FAIrepository',
- filter => 'objectClass=FAIrepositoryServer'
- );
+ base => $main::ldap_server_dn,
+ scope => 'base',
+ attrs => 'FAIrepository',
+ filter => 'objectClass=FAIrepositoryServer'
+ );
if($mesg->count) {
foreach my $entry(@{$mesg->{'entries'}}) {
foreach my $value(@{$entry->get_value('FAIrepository', asref => 1)}) {
}
}
}
+ } else {
+ if (defined $main::ldap_server_dn){
+ &main::daemon_log("$session_id ERROR: something wrong with ldap_server_dn '$main::ldap_server_dn', abort create_sources_list", 1);
+ } else {
+ &main::daemon_log("$session_id ERROR: no ldap_server_dn found, abort create_sources_list", 1);
+ }
}
close($fh);
return $result;
}
+
+sub run_create_packages_list_db {
+ my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
+ my $session_id = $session->ID;
+
+ my $task = POE::Wheel::Run->new(
+ Priority => +20,
+ Program => sub {&create_packages_list_db(undef, undef, $session_id)},
+ StdoutEvent => "session_run_result",
+ StderrEvent => "session_run_debug",
+ CloseEvent => "session_run_done",
+ );
+ $heap->{task}->{ $task->ID } = $task;
+}
+
+
sub create_packages_list_db {
- my ($ldap_handle, $sources_file) = @_ ;
+ my ($ldap_handle, $sources_file, $session_id) = @_;
+
+ # it should not be possible to trigger a recreation of packages_list_db
+ # while packages_list_db is under construction, so set flag packages_list_under_construction
+ # which is tested befor recreation can be started
+ if (-r $packages_list_under_construction) {
+ daemon_log("$session_id WARNING: packages_list_db is right now under construction, please wait until this process is finished", 3);
+ return;
+ } else {
+ daemon_log("$session_id INFO: create_packages_list_db: start", 5);
+ # set packages_list_under_construction to true
+ system("touch $packages_list_under_construction");
+ @packages_list_statements=();
+ }
+ if (not defined $session_id) { $session_id = 0; }
if (not defined $ldap_handle) {
$ldap_handle= &get_ldap_handle();
if (not defined $ldap_handle) {
- daemon_log("0 ERROR: no ldap_handle available to create_packages_list_db", 1);
+ daemon_log("$session_id ERROR: no ldap_handle available to create_packages_list_db", 1);
+ unlink($packages_list_under_construction);
return;
}
}
+ if (not defined $sources_file) {
+ &main::daemon_log("$session_id INFO: no sources_file given for creating packages list so trigger creation of it", 5);
+ $sources_file = &create_sources_list($session_id);
+ }
- if (not defined $sources_file) {
- $sources_file = &create_sources_list($ldap_handle);
- }
+ if (not defined $sources_file) {
+ &main::daemon_log("$session_id ERROR: no sources_file given under '$sources_file', skip create_packages_list_db", 1);
+ unlink($packages_list_under_construction);
+ return;
+ }
- my $line;
- daemon_log("INFO: create_packages_list_db: start", 5);
+ my $line;
- open(CONFIG, "<$sources_file") or do {
- daemon_log( "ERROR: create_packages_list_db: Failed to open '$sources_file'", 1);
- return;
- };
-
- # Read lines
- while ($line = <CONFIG>){
- # Unify
- chop($line);
- $line =~ s/^\s+//;
- $line =~ s/^\s+/ /;
+ open(CONFIG, "<$sources_file") or do {
+ daemon_log( "$session_id ERROR: create_packages_list_db: Failed to open '$sources_file'", 1);
+ unlink($packages_list_under_construction);
+ return;
+ };
- # Strip comments
- $line =~ s/#.*$//g;
+ # Read lines
+ while ($line = <CONFIG>){
+ # Unify
+ chop($line);
+ $line =~ s/^\s+//;
+ $line =~ s/^\s+/ /;
- # Skip empty lines
- if ($line =~ /^\s*$/){
- next;
- }
+ # Strip comments
+ $line =~ s/#.*$//g;
- # Interpret deb line
- if ($line =~ /^deb [^\s]+\s[^\s]+\s[^\s]+/){
- my( $baseurl, $dist, $sections ) = ($line =~ /^deb\s([^\s]+)\s+([^\s]+)\s+(.*)$/);
- my $section;
- foreach $section (split(' ', $sections)){
- &parse_package_info( $baseurl, $dist, $section );
- }
- }
- }
+ # Skip empty lines
+ if ($line =~ /^\s*$/){
+ next;
+ }
- close (CONFIG);
+ # Interpret deb line
+ if ($line =~ /^deb [^\s]+\s[^\s]+\s[^\s]+/){
+ my( $baseurl, $dist, $sections ) = ($line =~ /^deb\s([^\s]+)\s+([^\s]+)\s+(.*)$/);
+ my $section;
+ foreach $section (split(' ', $sections)){
+ &parse_package_info( $baseurl, $dist, $section, $session_id );
+ }
+ }
+ }
- daemon_log("INFO: create_packages_list_db: finished", 5);
- return;
-}
+ close (CONFIG);
-sub run_create_packages_list_db {
- my ($session, $heap) = @_[SESSION, HEAP];
- my $task = POE::Wheel::Run->new(
- Program => sub {&create_packages_list_db},
- StdoutEvent => "session_run_result",
- StderrEvent => "session_run_debug",
- CloseEvent => "session_run_done",
- );
- $heap->{task}->{ $task->ID } = $task;
+ find(\&cleanup_and_extract, keys( %repo_dirs ));
+ &main::strip_packages_list_statements();
+ unshift @packages_list_statements, "VACUUM";
+ $packages_list_db->exec_statementlist(\@packages_list_statements);
+ unlink($packages_list_under_construction);
+ daemon_log("$session_id INFO: create_packages_list_db: finished", 5);
+ return;
}
-sub parse_package_info {
- my ($baseurl, $dist, $section)= @_;
- my ($package);
+# This function should do some intensive task to minimize the db-traffic
+sub strip_packages_list_statements {
+ my @existing_entries= @{$packages_list_db->exec_statement("SELECT * FROM $main::packages_list_tn")};
+ my @new_statement_list=();
+ my $hash;
+ my $insert_hash;
+ my $update_hash;
+ my $delete_hash;
+ my $local_timestamp=get_time();
+
+ foreach my $existing_entry (@existing_entries) {
+ $hash->{@{$existing_entry}[0]}->{@{$existing_entry}[1]}->{@{$existing_entry}[2]}= $existing_entry;
+ }
+
+ foreach my $statement (@packages_list_statements) {
+ if($statement =~ /^INSERT/i) {
+ # Assign the values from the insert statement
+ my ($distribution,$package,$version,$section,$description,$template,$timestamp) = ($1,$2,$3,$4,$5,$6,$7) if $statement =~
+ /^INSERT\s+?INTO\s+?$main::packages_list_tn\s+?VALUES\s*?\('(.*?)',\s*?'(.*?)',\s*?'(.*?)',\s*?'(.*?)',\s*?'(.*?)',\s*?'(.*?)',\s*?'(.*?)'\s*?\)$/si;
+ if(exists($hash->{$distribution}->{$package}->{$version})) {
+ # If section or description has changed, update the DB
+ if(
+ (! (@{$hash->{$distribution}->{$package}->{$version}}[3] eq $section)) or
+ (! (@{$hash->{$distribution}->{$package}->{$version}}[4] eq $description))
+ ) {
+ @{$update_hash->{$distribution}->{$package}->{$version}} = ($distribution,$package,$version,$section,$description,undef);
+ }
+ } else {
+ # Insert a non-existing entry to db
+ @{$insert_hash->{$distribution}->{$package}->{$version}} = ($distribution,$package,$version,$section,$description,$template);
+ }
+ } elsif ($statement =~ /^UPDATE/i) {
+ my ($template,$package,$version) = ($1,$2,$3) if $statement =~
+ /^update\s+?$main::packages_list_tn\s+?set\s+?template\s*?=\s*?'(.*?)'\s+?where\s+?package\s*?=\s*?'(.*?)'\s+?and\s+?version\s*?=\s*?'(.*?)'\s*?;$/si;
+ foreach my $distribution (keys %{$hash}) {
+ if(exists($insert_hash->{$distribution}->{$package}->{$version})) {
+ # update the insertion hash to execute only one query per package (insert instead insert+update)
+ @{$insert_hash->{$distribution}->{$package}->{$version}}[5]= $template;
+ } elsif(exists($hash->{$distribution}->{$package}->{$version})) {
+ if( ! (@{$hash->{$distribution}->{$package}->{$version}}[5] eq $template)) {
+ my $section;
+ my $description;
+ if(defined(@{$update_hash->{$distribution}->{$package}->{$version}}[3]) and
+ length(@{$update_hash->{$distribution}->{$package}->{$version}}[3]) > 0 ) {
+ $section= @{$update_hash->{$distribution}->{$package}->{$version}}[3];
+ }
+ if(defined(@{$update_hash->{$distribution}->{$package}->{$version}}[4])) {
+ $description= @{$update_hash->{$distribution}->{$package}->{$version}}[4];
+ }
+ @{$update_hash->{$distribution}->{$package}->{$version}} = ($distribution,$package,$version,$section,$description,$template);
+ }
+ }
+ }
+ }
+ }
+
+ # TODO: Check for orphaned entries
+
+ # unroll the insert_hash
+ foreach my $distribution (keys %{$insert_hash}) {
+ foreach my $package (keys %{$insert_hash->{$distribution}}) {
+ foreach my $version (keys %{$insert_hash->{$distribution}->{$package}}) {
+ push @new_statement_list, "INSERT INTO $main::packages_list_tn VALUES ('$distribution','$package','$version',"
+ ."'@{$insert_hash->{$distribution}->{$package}->{$version}}[3]',"
+ ."'@{$insert_hash->{$distribution}->{$package}->{$version}}[4]',"
+ ."'@{$insert_hash->{$distribution}->{$package}->{$version}}[5]',"
+ ."'$local_timestamp')";
+ }
+ }
+ }
- my ($path) = ($baseurl =~ m%://[^/]*(.*)$%);
- $repo_dirs{ "${repo_path}/pool" } = 1;
+ # unroll the update hash
+ foreach my $distribution (keys %{$update_hash}) {
+ foreach my $package (keys %{$update_hash->{$distribution}}) {
+ foreach my $version (keys %{$update_hash->{$distribution}->{$package}}) {
+ my $set = "";
+ if(defined(@{$update_hash->{$distribution}->{$package}->{$version}}[3])) {
+ $set .= "section = '@{$update_hash->{$distribution}->{$package}->{$version}}[3]', ";
+ }
+ if(defined(@{$update_hash->{$distribution}->{$package}->{$version}}[4])) {
+ $set .= "description = '@{$update_hash->{$distribution}->{$package}->{$version}}[4]', ";
+ }
+ if(defined(@{$update_hash->{$distribution}->{$package}->{$version}}[5])) {
+ $set .= "template = '@{$update_hash->{$distribution}->{$package}->{$version}}[5]', ";
+ }
+ if(defined($set) and length($set) > 0) {
+ $set .= "timestamp = '$local_timestamp'";
+ } else {
+ next;
+ }
+ push @new_statement_list,
+ "UPDATE $main::packages_list_tn SET $set WHERE"
+ ." distribution = '$distribution'"
+ ." AND package = '$package'"
+ ." AND version = '$version'";
+ }
+ }
+ }
- foreach $package ("Packages.gz"){
- daemon_log("DEBUG: create_packages_list: fetch $baseurl, $dist, $section", 7);
- get_package( "$baseurl/dists/$dist/$section/binary-$arch/$package", "$outdir/$dist/$section" );
- parse_package( "$outdir/$dist/$section", $dist, $path );
- }
- find(\&cleanup_and_extract, keys( %repo_dirs ) );
+ @packages_list_statements = @new_statement_list;
}
-sub get_package {
- my ($url, $dest)= @_;
- my $tpath = dirname($dest);
- -d "$tpath" || mkpath "$tpath";
+sub parse_package_info {
+ my ($baseurl, $dist, $section, $session_id)= @_;
+ my ($package);
+ if (not defined $session_id) { $session_id = 0; }
+ my ($path) = ($baseurl =~ m%://[^/]*(.*)$%);
+ $repo_dirs{ "${repo_path}/pool" } = 1;
+
+ foreach $package ("Packages.gz"){
+ daemon_log("$session_id DEBUG: create_packages_list: fetch $baseurl, $dist, $section", 7);
+ get_package( "$baseurl/dists/$dist/$section/binary-$arch/$package", "$outdir/$dist/$section", $session_id );
+ parse_package( "$outdir/$dist/$section", $dist, $path, $session_id );
+ }
+
+}
+
- # This is ugly, but I've no time to take a look at "how it works in perl"
- if(0 == system("wget '$url' -O '$dest' 2>/dev/null") ) {
- system("gunzip -cd '$dest' > '$dest.in'");
- unlink($dest);
- } else {
- daemon_log("ERROR: create_packages_list_db: get_packages: fetching '$url' failed!", 1);
- }
- return 0;
+sub get_package {
+ my ($url, $dest, $session_id)= @_;
+ if (not defined $session_id) { $session_id = 0; }
+
+ my $tpath = dirname($dest);
+ -d "$tpath" || mkpath "$tpath";
+
+ # This is ugly, but I've no time to take a look at "how it works in perl"
+ if(0 == system("wget '$url' -O '$dest' 2>/dev/null") ) {
+ system("gunzip -cd '$dest' > '$dest.in'");
+ daemon_log("$session_id DEBUG: run command: gunzip -cd '$dest' > '$dest.in'", 5);
+ unlink($dest);
+ daemon_log("$session_id DEBUG: delete file '$dest'", 5);
+ } else {
+ daemon_log("$session_id ERROR: create_packages_list_db: get_packages: fetching '$url' failed!", 1);
+ }
+ return 0;
}
+
sub parse_package {
- my ($path, $dist, $srv_path)= @_;
+ my ($path, $dist, $srv_path, $session_id)= @_;
+ if (not defined $session_id) { $session_id = 0;}
my ($package, $version, $section, $description);
- my @sql_list;
my $PACKAGES;
+ my $timestamp = &get_time();
if(not stat("$path.in")) {
- daemon_log("ERROR: create_packages_list: parse_package: file '$path.in' is not readable",1);
+ daemon_log("$session_id ERROR: create_packages_list: parse_package: file '$path.in' is not readable",1);
return;
}
open($PACKAGES, "<$path.in");
- if(not defined($PACKAGES)) {
- daemon_log("ERROR: create_packages_list_db: parse_package: can not open '$path.in'",1);
+ if(not defined($PACKAGES)) {
+ daemon_log("$session_id ERROR: create_packages_list_db: parse_package: cannot open '$path.in'",1);
return;
}
# Use empty lines as a trigger
if ($line =~ /^\s*$/){
- my $sql = "INSERT INTO packages_list VALUES ('$dist', '$package', '$version', '$section', '', 'none', '0')";
- push(@sql_list, $sql);
+ my $sql = "INSERT INTO packages_list VALUES ('$dist', '$package', '$version', '$section', '$description', '', '$timestamp')";
+ push(@packages_list_statements, $sql);
$package = "none";
$version = "none";
$section = "none";
# Trigger for description
if ($line =~ /^Description:\s/){
- ($description)= ($line =~ /^Description: (.*)$/);
+ ($description)= &encode_base64(($line =~ /^Description: (.*)$/));
next;
}
next;
}
- # Trigger for filename
- if ($line =~ /^Filename:\s/){
- my ($filename) = ($line =~ /^Filename: (.*)$/);
- store_fileinfo( $package, $filename, $dist, $srv_path, $version, $repo_path );
- next;
- }
+ # Trigger for filename
+ if ($line =~ /^Filename:\s/){
+ my ($filename) = ($line =~ /^Filename: (.*)$/);
+ store_fileinfo( $package, $filename, $dist, $srv_path, $version, $repo_path );
+ next;
+ }
}
close( $PACKAGES );
unlink( "$path.in" );
-
- $packages_list_db->exec_statementlist(\@sql_list);
}
+
sub store_fileinfo {
- my( $package, $file, $dist, $path, $vers, $srvdir) = @_;
+ my( $package, $file, $dist, $path, $vers, $srvdir) = @_;
- my %fileinfo = (
- 'package' => $package,
- 'dist' => $dist,
- 'version' => $vers,
- );
+ my %fileinfo = (
+ 'package' => $package,
+ 'dist' => $dist,
+ 'version' => $vers,
+ );
- $repo_files{ "${srvdir}/$file" } = \%fileinfo;
+ $repo_files{ "${srvdir}/$file" } = \%fileinfo;
}
+
sub cleanup_and_extract {
- my $fileinfo = $repo_files{ $File::Find::name };
+ my $fileinfo = $repo_files{ $File::Find::name };
- if( defined $fileinfo ) {
+ if( defined $fileinfo ) {
- my $dir = "$outdir/$fileinfo->{ 'dist' }/debconf.d";
- my $sql;
- my $package = $fileinfo->{ 'package' };
- my $newver = $fileinfo->{ 'version' };
+ my $dir = "$outdir/$fileinfo->{ 'dist' }/debconf.d";
+ my $sql;
+ my $package = $fileinfo->{ 'package' };
+ my $newver = $fileinfo->{ 'version' };
- mkpath($dir);
- system( "dpkg -e '$File::Find::name' '$dir/DEBIAN'" );
+ mkpath($dir);
+ system( "dpkg -e '$File::Find::name' '$dir/DEBIAN'" );
- if( -f "$dir/DEBIAN/templates" ) {
+ if( -f "$dir/DEBIAN/templates" ) {
- daemon_log("DEBUG: Found debconf templates in '$package' - $newver", 5);
+ daemon_log("DEBUG: Found debconf templates in '$package' - $newver", 5);
- my $tmpl= "";
- {
- local $/=undef;
- open FILE, "$dir/DEBIAN/templates";
- $tmpl = &encode_base64(<FILE>);
- close FILE;
- }
- rmtree("$dir/DEBIAN/templates");
+ my $tmpl= "";
+ {
+ local $/=undef;
+ open FILE, "$dir/DEBIAN/templates";
+ $tmpl = &encode_base64(<FILE>);
+ close FILE;
+ }
+ rmtree("$dir/DEBIAN/templates");
- $sql= "update $main::packages_list_tn set template = '$tmpl' where package = '$package' and version = '$newver';";
+ $sql= "update $main::packages_list_tn set template = '$tmpl' where package = '$package' and version = '$newver';";
+ push @packages_list_statements, $sql;
+ }
+ }
- } else {
- $sql= "update $main::packages_list_tn set template = '' where package = '$package' and version = '$newver';";
+ return;
+}
+
+
+sub register_at_foreign_servers {
+ my ($kernel) = $_[KERNEL];
+
+ # hole alle bekannten server aus known_server_db
+ my $server_sql = "SELECT * FROM $known_server_tn";
+ my $server_res = $known_server_db->exec_statement($server_sql);
+
+ # no entries in known_server_db
+ if (not ref(@$server_res[0]) eq "ARRAY") {
+ # TODO
}
- my $res= $main::packages_list_db->update_dbentry($sql);
- }
+ # detect already connected clients
+ my $client_sql = "SELECT * FROM $known_clients_tn";
+ my $client_res = $known_clients_db->exec_statement($client_sql);
+
+ # send my server details to all other gosa-si-server within the network
+ foreach my $hit (@$server_res) {
+ my $hostname = @$hit[0];
+ my $hostkey = &create_passwd;
+
+ # add already connected clients to registration message
+ my $myhash = &create_xml_hash('new_server', $server_address, $hostname);
+ &add_content2xml_hash($myhash, 'key', $hostkey);
+ map(&add_content2xml_hash($myhash, 'client', @{$_}[0].",".@{$_}[4]), @$client_res);
+
+ # build registration message and send it
+ my $foreign_server_msg = &create_xml_string($myhash);
+ my $error = &send_msg_to_target($foreign_server_msg, $hostname, $ServerPackages_key, "new_server", 0);
+ }
+
+ $kernel->delay_set("register_at_foreign_servers", $foreign_servers_register_delay);
+ return;
}
"c|config=s" => \$cfg_file,
"f|foreground" => \$foreground,
"v|verbose+" => \$verbose,
- "no-bus+" => \$no_bus,
"no-arp+" => \$no_arp,
);
};
}
+# parse head url and revision from svn
+my $server_status_hash = { 'developmental'=>'revision', 'stable'=>'release'};
+$server_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/;
+$server_headURL = defined $1 ? $1 : 'unknown' ;
+$server_revision = defined $2 ? $2 : 'unknown' ;
+if ($server_headURL =~ /\/tag\// ||
+ $server_headURL =~ /\/branches\// ) {
+ $server_status = "stable";
+} else {
+ $server_status = "developmental" ;
+}
+
+
daemon_log(" ", 1);
daemon_log("$0 started!", 1);
+daemon_log("status: $server_status", 1);
+daemon_log($server_status_hash->{$server_status}.": $server_revision", 1);
-if ($no_bus > 0) {
- $bus_activ = "false"
-}
+# connect to incoming_db
+unlink($incoming_file_name);
+$incoming_db = GOSA::DBsqlite->new($incoming_file_name);
+$incoming_db->create_table($incoming_tn, \@incoming_col_names);
# connect to gosa-si job queue
$job_db = GOSA::DBsqlite->new($job_queue_file_name);
$known_clients_db = GOSA::DBsqlite->new($known_clients_file_name);
$known_clients_db->create_table($known_clients_tn, \@known_clients_col_names);
+# connect to foreign_clients_db
+$foreign_clients_db = GOSA::DBsqlite->new($foreign_clients_file_name);
+$foreign_clients_db->create_table($foreign_clients_tn, \@foreign_clients_col_names);
+
# connect to known_server_db
+unlink($known_server_file_name);
$known_server_db = GOSA::DBsqlite->new($known_server_file_name);
$known_server_db->create_table($known_server_tn, \@known_server_col_names);
$login_users_db->create_table($login_users_tn, \@login_users_col_names);
# connect to fai_server_db and fai_release_db
+unlink($fai_server_file_name);
$fai_server_db = GOSA::DBsqlite->new($fai_server_file_name);
$fai_server_db->create_table($fai_server_tn, \@fai_server_col_names);
-$fai_server_db->create_table($fai_release_tn, \@fai_release_col_names);
+
+unlink($fai_release_file_name);
+$fai_release_db = GOSA::DBsqlite->new($fai_release_file_name);
+$fai_release_db->create_table($fai_release_tn, \@fai_release_col_names);
# connect to packages_list_db
-unlink($packages_list_file_name);
+#unlink($packages_list_file_name);
+unlink($packages_list_under_construction);
$packages_list_db = GOSA::DBsqlite->new($packages_list_file_name);
$packages_list_db->create_table($packages_list_tn, \@packages_list_col_names);
# create xml object used for en/decrypting
$xml = new XML::Simple();
-# create socket for incoming xml messages
+
+# foreign servers
+my @foreign_server_list;
+
+# add foreign server from cfg file
+if ($foreign_server_string ne "") {
+ my @cfg_foreign_server_list = split(",", $foreign_server_string);
+ foreach my $foreign_server (@cfg_foreign_server_list) {
+ push(@foreign_server_list, $foreign_server);
+ }
+}
+
+# add foreign server from dns
+my @tmp_servers;
+if ( !$server_domain) {
+ # Try our DNS Searchlist
+ for my $domain(get_dns_domains()) {
+ chomp($domain);
+ my @tmp_domains= &get_server_addresses($domain);
+ if(@tmp_domains) {
+ for my $tmp_server(@tmp_domains) {
+ push @tmp_servers, $tmp_server;
+ }
+ }
+ }
+ if(@tmp_servers && length(@tmp_servers)==0) {
+ daemon_log("0 WARNING: no foreign gosa-si-server found in DNS for domain '$server_domain'", 3);
+ }
+} else {
+ @tmp_servers = &get_server_addresses($server_domain);
+ if( 0 == @tmp_servers ) {
+ daemon_log("0 WARNING: no foreign gosa-si-server found in DNS for domain '$server_domain'", 3);
+ }
+}
+foreach my $server (@tmp_servers) {
+ unshift(@foreign_server_list, $server);
+}
+# eliminate duplicate entries
+@foreign_server_list = &del_doubles(@foreign_server_list);
+my $all_foreign_server = join(", ", @foreign_server_list);
+daemon_log("0 INFO: found foreign server in config file and DNS: $all_foreign_server", 5);
+
+# add all found foreign servers to known_server
+my $act_timestamp = &get_time();
+foreach my $foreign_server (@foreign_server_list) {
+
+ #######################################
+ # TODO for jan
+ # do not add myself to known_server_db
+ # work around!!!
+ if ($foreign_server eq '172.16.2.89:20081') { next; }
+ ######################################
+
+ my $res = $known_server_db->add_dbentry( {table=>$known_server_tn,
+ primkey=>['hostname'],
+ hostname=>$foreign_server,
+ status=>'not_jet_registered',
+ hostkey=>"none",
+ timestamp=>$act_timestamp,
+ } );
+}
+
POE::Component::Server::TCP->new(
+ Alias => "TCP_SERVER",
Port => $server_port,
ClientInput => sub {
my ($kernel, $input) = @_[KERNEL, ARG0];
push(@tasks, $input);
- $kernel->yield("next_task");
+ push(@msgs_to_decrypt, $input);
+ $kernel->yield("msg_to_decrypt");
},
InlineStates => {
+ msg_to_decrypt => \&msg_to_decrypt,
next_task => \&next_task,
task_result => \&handle_task_result,
task_done => \&handle_task_done,
@@ -2231,13 +3023,24 @@ daemon_log("start socket for incoming xml messages at port '$server_port' ", 1);
# create session for repeatedly checking the job queue for jobs
POE::Session->create(
inline_states => {
- _start => \&_start,
- sig_handler => \&sig_handler,
+ _start => \&session_start,
+ register_at_foreign_servers => \®ister_at_foreign_servers,
+ sig_handler => \&sig_handler,
+ next_task => \&next_task,
+ task_result => \&handle_task_result,
+ task_done => \&handle_task_done,
+ task_debug => \&handle_task_debug,
+ watch_for_next_tasks => \&watch_for_next_tasks,
+ watch_for_new_messages => \&watch_for_new_messages,
+ watch_for_delivery_messages => \&watch_for_delivery_messages,
+ watch_for_done_messages => \&watch_for_done_messages,
watch_for_new_jobs => \&watch_for_new_jobs,
watch_for_done_jobs => \&watch_for_done_jobs,
+ watch_for_old_known_clients => \&watch_for_old_known_clients,
create_packages_list_db => \&run_create_packages_list_db,
create_fai_server_db => \&run_create_fai_server_db,
create_fai_release_db => \&run_create_fai_release_db,
+ recreate_packages_db => \&run_recreate_packages_db,
session_run_result => \&session_run_result,
session_run_debug => \&session_run_debug,
session_run_done => \&session_run_done,
# import all modules
&import_modules;
+# TODO
# check wether all modules are gosa-si valid passwd check
+
+
POE::Kernel->run();
exit;