diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server
index c279c7943032a6ed60a22692b090b771be8ac7b6..b93f07cc7a58c9c717e13303941b4b720f0f92a0 100755 (executable)
--- a/gosa-si/gosa-si-server
+++ b/gosa-si/gosa-si-server
# REVISION: ---
#===============================================================================
# REVISION: ---
#===============================================================================
+
use strict;
use warnings;
use Getopt::Long;
use Config::IniFiles;
use POSIX;
use strict;
use warnings;
use Getopt::Long;
use Config::IniFiles;
use POSIX;
-use utf8;
use Fcntl;
use IO::Socket::INET;
use Fcntl;
use IO::Socket::INET;
use Cwd;
use File::Spec;
use File::Basename;
use Cwd;
use File::Spec;
use File::Basename;
+use File::Find;
+use File::Copy;
use File::Path;
use GOSA::DBsqlite;
use GOSA::GosaSupportDaemon;
use File::Path;
use GOSA::DBsqlite;
use GOSA::GosaSupportDaemon;
my $modules_path = "/usr/lib/gosa-si/modules";
use lib "/usr/lib/gosa-si/modules";
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;
+
my (%cfg_defaults, $foreground, $verbose, $ping_timeout);
my ($bus_activ, $bus, $msg_to_bus, $bus_cipher);
my (%cfg_defaults, $foreground, $verbose, $ping_timeout);
my ($bus_activ, $bus, $msg_to_bus, $bus_cipher);
-my ($server, $server_mac_address);
+my ($server);
my ($gosa_server, $job_queue_timeout, $job_queue_loop_delay);
my ($known_modules);
my ($pid_file, $procid, $pid, $log_file);
my ($gosa_server, $job_queue_timeout, $job_queue_loop_delay);
my ($known_modules);
my ($pid_file, $procid, $pid, $log_file);
my ($xml);
my $sources_list;
my $max_clients;
my ($xml);
my $sources_list;
my $max_clients;
+my %repo_files=();
+my $repo_path;
+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,
# variables declared in config file are always set to 'our'
our (%cfg_defaults, $log_file, $pid_file,
$server_ip, $server_port, $SIPackages_key,
# additional variable which should be globaly accessable
our $server_address;
# additional variable which should be globaly accessable
our $server_address;
+our $server_mac_address;
our $bus_address;
our $gosa_address;
our $no_bus;
our $bus_address;
our $gosa_address;
our $no_bus;
our $verbose;
our $forground;
our $cfg_file;
our $verbose;
our $forground;
our $cfg_file;
-our ($ldap_handle, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password );
+#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);
# specifies the verbosity of the daemon_log
# specifies the verbosity of the daemon_log
our $job_db;
our $job_queue_tn = 'jobs';
my $job_queue_file_name;
our $job_db;
our $job_queue_tn = 'jobs';
my $job_queue_file_name;
-my @job_queue_col_names = ("id INTEGER", "timestamp", "status", "result", "progress INTEGER",
- "headertag", "targettag", "xmlmessage", "macaddress");
+my @job_queue_col_names = ("id INTEGER",
+ "timestamp",
+ "status DEFAULT 'none'",
+ "result DEFAULT 'none'",
+ "progress DEFAULT 'none'",
+ "headertag DEFAULT 'none'",
+ "targettag DEFAULT 'none'",
+ "xmlmessage DEFAULT 'none'",
+ "macaddress DEFAULT 'none'",
+ );
# holds all other gosa-sd as well as the gosa-sd-bus
our $known_server_db;
# holds all other gosa-sd as well as the gosa-sd-bus
our $known_server_db;
our $fai_server_db;
our $fai_server_tn = "fai_server";
my $fai_server_file_name;
our $fai_server_db;
our $fai_server_tn = "fai_server";
my $fai_server_file_name;
-our @fai_server_col_names = ('timestamp', 'server', 'release', 'tag');
+our @fai_server_col_names = ('timestamp', 'server', 'release', 'sections', 'tag');
our $fai_release_tn = "fai_release";
our @fai_release_col_names = ('timestamp', 'release', 'class', 'type', 'state');
our $fai_release_tn = "fai_release";
our @fai_release_col_names = ('timestamp', 'release', 'class', 'type', 'state');
# path to directory to store client install log files
our $client_fai_log_dir = "/var/log/fai";
# path to directory to store client install log files
our $client_fai_log_dir = "/var/log/fai";
-# queue which stores taskes while max_clients are reached
+# queue which stores taskes until one of the $max_children children are ready to process the task
my @tasks = qw();
my @tasks = qw();
+my $max_children = 2;
+
%cfg_defaults = (
"general" => {
%cfg_defaults = (
"general" => {
"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'],
"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-uri" => [\$ldap_uri, ""],
"ldap-base" => [\$ldap_base, ""],
"ldap-admin-dn" => [\$ldap_admin_dn, ""],
#===============================================================================
sub read_configfile {
my $cfg;
#===============================================================================
sub read_configfile {
my $cfg;
- if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
+ if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
if( -r $cfg_file ) {
$cfg = Config::IniFiles->new( -file => $cfg_file );
} else {
if( -r $cfg_file ) {
$cfg = Config::IniFiles->new( -file => $cfg_file );
} else {
sub sig_int_handler {
my ($signal) = @_;
sub sig_int_handler {
my ($signal) = @_;
- if(defined($ldap_handle)) {
- $ldap_handle->disconnect;
- }
+# if (defined($ldap_handle)) {
+# $ldap_handle->disconnect;
+# }
+ # TODO alle verbliebenden ldap verbindungen aus allen heaps beenden
+
daemon_log("shutting down gosa-si-server", 1);
daemon_log("shutting down gosa-si-server", 1);
- exit(0);
+ system("kill `ps -C gosa-si-server -o pid=`");
}
$SIG{INT} = \&sig_int_handler;
}
$SIG{INT} = \&sig_int_handler;
-
sub check_key_and_xml_validity {
sub check_key_and_xml_validity {
- my ($crypted_msg, $module_key) = @_;
+ my ($crypted_msg, $module_key, $session_id) = @_;
my $msg;
my $msg_hash;
my $error_string;
my $msg;
my $msg_hash;
my $error_string;
$msg = &decrypt_msg($crypted_msg, $module_key);
if ($msg =~ /<xml>/i){
$msg = &decrypt_msg($crypted_msg, $module_key);
if ($msg =~ /<xml>/i){
- daemon_log("decrypted_msg: \n$msg", 8);
+ $msg =~ s/\s+/ /g; # just for better daemon_log
+ daemon_log("$session_id DEBUG: decrypted_msg: \n$msg", 8);
$msg_hash = $xml->XMLin($msg, ForceArray=>1);
##############
$msg_hash = $xml->XMLin($msg, ForceArray=>1);
##############
}
};
if($@) {
}
};
if($@) {
- daemon_log("WARNING: do not understand the message: $@", 5);
+ daemon_log("$session_id DEBUG: do not understand the message: $@", 7);
$msg = undef;
$msg_hash = undef;
}
$msg = undef;
$msg_hash = undef;
}
sub input_from_known_server {
sub input_from_known_server {
- my ($input, $remote_ip) = @_ ;
+ my ($input, $remote_ip, $session_id) = @_ ;
my ($msg, $msg_hash, $module);
my $sql_statement= "SELECT * FROM known_server";
my ($msg, $msg_hash, $module);
my $sql_statement= "SELECT * FROM known_server";
next;
}
my $host_key = $hit->{hostkey};
next;
}
my $host_key = $hit->{hostkey};
- daemon_log("DEBUG: input_from_known_server: host_name: $host_name", 7);
+ 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);
# check if module can open msg envelope with module key
daemon_log("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);
+ my ($tmp_msg, $tmp_msg_hash) = &check_key_and_xml_validity($input, $host_key, $session_id);
if( (!$tmp_msg) || (!$tmp_msg_hash) ) {
if( (!$tmp_msg) || (!$tmp_msg_hash) ) {
- daemon_log("DEBUG: input_from_known_server: deciphering raise error", 7);
+ daemon_log("$session_id DEBUG: input_from_known_server: deciphering raise error", 7);
daemon_log("$@", 8);
next;
}
daemon_log("$@", 8);
next;
}
}
if( (!$msg) || (!$msg_hash) || (!$module) ) {
}
if( (!$msg) || (!$msg_hash) || (!$module) ) {
- daemon_log("DEBUG: Incoming message is not from a known server", 7);
+ daemon_log("$session_id DEBUG: Incoming message is not from a known server", 7);
}
return ($msg, $msg_hash, $module);
}
return ($msg, $msg_hash, $module);
sub input_from_known_client {
sub input_from_known_client {
- my ($input, $remote_ip) = @_ ;
+ my ($input, $remote_ip, $session_id) = @_ ;
my ($msg, $msg_hash, $module);
my $sql_statement= "SELECT * FROM known_clients";
my ($msg, $msg_hash, $module);
my $sql_statement= "SELECT * FROM known_clients";
next;
}
my $host_key = $hit->{hostkey};
next;
}
my $host_key = $hit->{hostkey};
- &daemon_log("DEBUG: input_from_known_client: host_name: $host_name", 7);
- &daemon_log("DEBUG: input_from_known_client: host_key: $host_key", 7);
+ &daemon_log("$session_id DEBUG: input_from_known_client: host_name: $host_name", 7);
+ &daemon_log("$session_id DEBUG: input_from_known_client: host_key: $host_key", 7);
# check if module can open msg envelope with module key
# check if module can open msg envelope with module key
- ($msg, $msg_hash) = &check_key_and_xml_validity($input, $host_key);
+ ($msg, $msg_hash) = &check_key_and_xml_validity($input, $host_key, $session_id);
if( (!$msg) || (!$msg_hash) ) {
if( (!$msg) || (!$msg_hash) ) {
- &daemon_log("DEGUG: input_from_known_client: deciphering raise error", 7);
+ &daemon_log("$session_id DEGUG: input_from_known_client: deciphering raise error", 7);
&daemon_log("$@", 8);
next;
}
&daemon_log("$@", 8);
next;
}
}
if( (!$msg) || (!$msg_hash) || (!$module) ) {
}
if( (!$msg) || (!$msg_hash) || (!$module) ) {
- &daemon_log("DEBUG: Incoming message is not from a known client", 7);
+ &daemon_log("$session_id DEBUG: Incoming message is not from a known client", 7);
}
return ($msg, $msg_hash, $module);
}
return ($msg, $msg_hash, $module);
sub input_from_unknown_host {
no strict "refs";
sub input_from_unknown_host {
no strict "refs";
- my ($input) = @_ ;
+ my ($input, $session_id) = @_ ;
my ($msg, $msg_hash, $module);
my $error_string;
my ($msg, $msg_hash, $module);
my $error_string;
if( $mod eq 'ArpHandler' ) {
next;
}
if( $mod eq 'ArpHandler' ) {
next;
}
- daemon_log("ERROR: no key specified in config file for $mod", 1);
+ daemon_log("$session_id ERROR: no key specified in config file for $mod", 1);
next;
}
next;
}
- daemon_log("DEBUG: $mod: $module_key", 7);
+ daemon_log("$session_id DEBUG: $mod: $module_key", 7);
# check if module can open msg envelope with module key
# check if module can open msg envelope with module key
- ($msg, $msg_hash) = &check_key_and_xml_validity($input, $module_key);
+ ($msg, $msg_hash) = &check_key_and_xml_validity($input, $module_key, $session_id);
if( (not defined $msg) || (not defined $msg_hash) ) {
next;
}
if( (not defined $msg) || (not defined $msg_hash) ) {
next;
}
}
if( (!$msg) || (!$msg_hash) || (!$module)) {
}
if( (!$msg) || (!$msg_hash) || (!$module)) {
- daemon_log("DEBUG: Incoming message is not from an unknown host", 7);
+ daemon_log("$session_id DEBUG: Incoming message is not from an unknown host", 7);
}
return ($msg, $msg_hash, $module);
}
return ($msg, $msg_hash, $module);
sub send_msg_to_target {
sub send_msg_to_target {
- my ($msg, $address, $encrypt_key, $msg_header) = @_ ;
+ my ($msg, $address, $encrypt_key, $msg_header, $session_id) = @_ ;
my $error = 0;
my $header;
my $new_status;
my $error = 0;
my $header;
my $new_status;
# opensocket
my $socket = &open_socket($address);
if( !$socket ) {
# opensocket
my $socket = &open_socket($address);
if( !$socket ) {
- daemon_log("ERROR: cannot send ".$header."msg to $address , host not reachable", 1);
+ daemon_log("$session_id ERROR: cannot send ".$header."msg to $address , host not reachable", 1);
$error++;
}
$error++;
}
# send xml msg
print $socket $crypted_msg."\n";
# send xml msg
print $socket $crypted_msg."\n";
- daemon_log("INFO: send ".$header."msg to $address", 5);
- daemon_log("message:\n$msg", 8);
+ daemon_log("$session_id INFO: send ".$header."msg to $address", 5);
+ #daemon_log("DEBUG: message:\n$msg", 9);
}
}
if( $act_status eq "down" ) {
$sql_statement = "DELETE FROM known_clients WHERE hostname='$address'";
$res = $known_clients_db->del_dbentry($sql_statement);
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);
+ 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'";
$res = $known_clients_db->update_dbentry($sql_statement);
if($new_status eq "down"){
} 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);
+ daemon_log("$session_id WARNING: set '$address' from status '$act_status' to '$new_status'", 3);
} else {
} else {
- daemon_log("INFO: set '$address' from status '$act_status' to '$new_status'", 5);
+ daemon_log("$session_id INFO: set '$address' from status '$act_status' to '$new_status'", 5);
}
}
}
}
}
}
if( $act_status eq "down" ) {
$sql_statement = "DELETE FROM known_server WHERE hostname='$address'";
$res = $known_server_db->del_dbentry($sql_statement);
if( $act_status eq "down" ) {
$sql_statement = "DELETE FROM known_server WHERE hostname='$address'";
$res = $known_server_db->del_dbentry($sql_statement);
- daemon_log("WARNING: failed 2x to a send msg to host '$address', delete host from known_server", 3);
+ daemon_log("$session_id 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"){
}
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);
+ daemon_log("$session_id WARNING: set '$address' from status '$act_status' to '$new_status'", 3);
}
else {
}
else {
- daemon_log("INFO: set '$address' from status '$act_status' to '$new_status'", 5);
+ daemon_log("$session_id INFO: set '$address' from status '$act_status' to '$new_status'", 5);
}
}
}
}
}
}
my ($answer, $error) = @_;
if( $answer =~ /<jobdb_id>(\d+)<\/jobdb_id>/ ) {
my $jobdb_id = $1;
my ($answer, $error) = @_;
if( $answer =~ /<jobdb_id>(\d+)<\/jobdb_id>/ ) {
my $jobdb_id = $1;
+
+ # sending msg faild
if( $error ) {
if (not $answer =~ /<header>trigger_action_reinstall<\/header>/) {
if( $error ) {
if (not $answer =~ /<header>trigger_action_reinstall<\/header>/) {
- # sending msg faild
my $sql_statement = "UPDATE $job_queue_tn ".
"SET status='error', result='can not deliver msg, please consult log file' ".
"WHERE id='$jobdb_id'";
my $res = $job_db->update_dbentry($sql_statement);
}
my $sql_statement = "UPDATE $job_queue_tn ".
"SET status='error', result='can not deliver msg, please consult log file' ".
"WHERE id='$jobdb_id'";
my $res = $job_db->update_dbentry($sql_statement);
}
+
+ # sending msg was successful
} else {
} else {
- # sending msg was successful
my $sql_statement = "UPDATE $job_queue_tn ".
"SET status='done' ".
"WHERE id='$jobdb_id' AND status='processed'";
my $sql_statement = "UPDATE $job_queue_tn ".
"SET status='done' ".
"WHERE id='$jobdb_id' AND status='processed'";
}
}
}
}
-
sub _start {
my ($kernel) = $_[KERNEL];
&trigger_db_loop($kernel);
sub _start {
my ($kernel) = $_[KERNEL];
&trigger_db_loop($kernel);
- $kernel->yield('create_fai_server_db', $fai_server_tn );
+ $global_kernel = $kernel;
+ $kernel->yield('create_fai_server_db', $fai_server_tn );
$kernel->yield('create_fai_release_db', $fai_release_tn );
$kernel->yield('create_fai_release_db', $fai_release_tn );
- $kernel->yield('create_packages_list_db', $sources_list );
+ $kernel->sig(USR1 => "sig_handler");
+ $kernel->sig(USR2 => "create_packages_list_db");
}
}
+sub sig_handler {
+ my ($kernel, $signal) = @_[KERNEL, ARG0] ;
+ daemon_log("0 INFO got signal '$signal'", 1);
+ $kernel->sig_handled();
+ return;
+}
-sub next_tasks {
+sub next_task {
my ($session, $heap) = @_[SESSION, HEAP];
my ($session, $heap) = @_[SESSION, HEAP];
- while ( keys( %{ $heap->{task} } ) < $max_clients ) {
+ while ( keys( %{ $heap->{task} } ) < $max_children ) {
my $next_task = shift @tasks;
last unless defined $next_task;
my $task = POE::Wheel::Run->new(
Program => sub { process_task($session, $heap, $next_task) },
my $next_task = shift @tasks;
last unless defined $next_task;
my $task = POE::Wheel::Run->new(
Program => sub { process_task($session, $heap, $next_task) },
- StdoutFilter => POE::Filter::Reference->new(),
+ StdioFilter => POE::Filter::Reference->new(),
StdoutEvent => "task_result",
StderrEvent => "task_debug",
CloseEvent => "task_done",
StdoutEvent => "task_result",
StderrEvent => "task_debug",
CloseEvent => "task_done",
- );
+ );
$heap->{task}->{ $task->ID } = $task;
}
}
$heap->{task}->{ $task->ID } = $task;
}
}
+
sub handle_task_result {
sub handle_task_result {
- my ($kernel, $heap, $client_answer) = @_[KERNEL, HEAP, ARG0];
+ my ($kernel, $heap, $result) = @_[KERNEL, HEAP, ARG0];
+ my $client_answer = $result->{'answer'};
if( $client_answer =~ s/session_id=(\d+)$// ) {
my $session_id = $1;
if( defined $session_id ) {
if( $client_answer =~ s/session_id=(\d+)$// ) {
my $session_id = $1;
if( defined $session_id ) {
if( defined $session_reference ) {
$heap = $session_reference->get_heap();
}
if( defined $session_reference ) {
$heap = $session_reference->get_heap();
}
- }
+ }
+
+ if(exists $heap->{'client'}) {
+ $heap->{'client'}->put($client_answer);
+ }
}
}
- $heap->{client}->put($client_answer);
$kernel->sig(CHLD => "child_reap");
}
$kernel->sig(CHLD => "child_reap");
}
+
sub handle_task_debug {
my $result = $_[ARG0];
print STDERR "$result\n";
sub handle_task_debug {
my $result = $_[ARG0];
print STDERR "$result\n";
delete $heap->{task}->{$task_id};
$kernel->yield("next_task");
}
delete $heap->{task}->{$task_id};
$kernel->yield("next_task");
}
+
sub process_task {
no strict "refs";
my ($session, $heap, $input) = @_;
sub process_task {
no strict "refs";
my ($session, $heap, $input) = @_;
my $client_answer = "";
daemon_log("", 5);
my $client_answer = "";
daemon_log("", 5);
- daemon_log("INFO: Incoming msg from '".$heap->{'remote_ip'}."'", 5);
- daemon_log("DEBUG: Incoming msg:\n$input", 9);
+ 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
####################
# check incoming msg
# msg is from a new client or gosa
- ($msg, $msg_hash, $module) = &input_from_unknown_host($input);
+ ($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 is from a gosa-si-server or gosa-si-bus
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- ($msg, $msg_hash, $module) = &input_from_known_server($input, $heap->{'remote_ip'});
+ ($msg, $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 is from a gosa-si-client
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- ($msg, $msg_hash, $module) = &input_from_known_client($input, $heap->{'remote_ip'});
+ ($msg, $msg_hash, $module) = &input_from_known_client($input, $heap->{'remote_ip'}, $session_id);
}
# an error occurred
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
}
# an error occurred
if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- $error++;
- }
-
- ######################
- # process incoming msg
- if( $error == 0) {
- daemon_log("INFO: Incoming msg with header '".@{$msg_hash->{'header'}}[0]."'", 5);
- daemon_log("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("DEGUB: $module: Got answer from module: \n".$answer_str,8);
- }
- }
- if( !$answer_l ) { $error++ };
-
- ########
- # answer
- 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;
- }
-
- $answer_header = @{$answer_hash->{'header'}}[0];
- @answer_target_l = @{$answer_hash->{'target'}};
- $answer_source = @{$answer_hash->{'source'}}[0];
-
- # deliver msg to all targets
- foreach my $answer_target ( @answer_target_l ) {
-
- # targets of msg are all gosa-si-clients in known_clients_db
- if( $answer_target eq "*" ) {
- # answer is for all clients
- my $sql_statement= "SELECT * FROM known_clients";
- my $query_res = $known_clients_db->select_dbentry( $sql_statement );
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $host_name = $hit->{hostname};
- my $host_key = $hit->{hostkey};
- my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- }
- }
-
- # targets of msg are all gosa-si-server in known_server_db
- elsif( $answer_target eq "KNOWN_SERVER" ) {
- # answer is for all server in known_server
- my $sql_statement= "SELECT * FROM known_server";
- my $query_res = $known_server_db->select_dbentry( $sql_statement );
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $host_name = $hit->{hostname};
- my $host_key = $hit->{hostkey};
- $answer =~ s/KNOWN_SERVER/$host_name/g;
- my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- }
- }
-
- # target of msg is GOsa
- elsif( $answer_target eq "GOSA" ) {
- $answer =~ /<session_id>(\d+)<\/session_id>/;
- my $session_id = $1;
- my $add_on = "";
- if( defined $session_id ) {
- $add_on = ".session_id=$session_id";
- }
- # answer is for GOSA and has to returned to connected client
- my $gosa_answer = &encrypt_msg($answer, $GosaPackages_key);
- $client_answer = $gosa_answer.$add_on;
- }
-
- # target of msg is job queue at this host
- elsif( $answer_target eq "JOBDB") {
- $answer =~ /<header>(\S+)<\/header>/;
- my $header;
- if( defined $1 ) { $header = $1; }
- my $error = &send_msg_to_target($answer, $server_address, $GosaPackages_key, $header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- }
-
- # target of msg is a mac address
- elsif( $answer_target =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ) {
- daemon_log("INFO: target is mac address '$answer_target', looking for host in known_clients", 5);
- my $sql_statement = "SELECT * FROM known_clients WHERE macaddress='$answer_target'";
- my $query_res = $known_clients_db->select_dbentry( $sql_statement );
- my $found_ip_flag = 0;
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $host_name = $hit->{hostname};
- my $host_key = $hit->{hostkey};
- $answer =~ s/$answer_target/$host_name/g;
- daemon_log("INFO: found host '$host_name', associated to '$answer_target'", 5);
- my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- $found_ip_flag++ ;
- }
- if( $found_ip_flag == 0) {
- daemon_log("WARNING: no host found in known_clients with mac address '$answer_target'", 3);
- if( $bus_activ eq "true" ) {
- daemon_log("INFO: try to forward msg '$answer_header' to bus '$bus_address'", 5);
- my $sql_statement = "SELECT * FROM known_server WHERE hostname='$bus_address'";
- my $query_res = $known_server_db->select_dbentry( $sql_statement );
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $bus_address = $hit->{hostname};
- my $bus_key = $hit->{hostkey};
- my $error = &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- last;
- }
- }
-
- }
-
- # answer is for one specific host
- } else {
- # get encrypt_key
- my $encrypt_key = &get_encrypt_key($answer_target);
- if( not defined $encrypt_key ) {
- # unknown target, forward msg to bus
- daemon_log("WARNING: unknown target '$answer_target'", 3);
- if( $bus_activ eq "true" ) {
- daemon_log("INFO: try to forward msg '$answer_header' to bus '$bus_address'", 5);
- my $sql_statement = "SELECT * FROM known_server WHERE hostname='$bus_address'";
- my $query_res = $known_server_db->select_dbentry( $sql_statement );
- my $res_length = keys( %{$query_res} );
- if( $res_length == 0 ){
- daemon_log("WARNING: send '$answer_header' to '$bus_address' failed, ".
- "no bus found in known_server", 3);
- }
- else {
- while( my ($hit_num, $hit) = each %{ $query_res } ) {
- my $bus_key = $hit->{hostkey};
- my $error = &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- }
- }
- }
- next;
- }
- my $error = &send_msg_to_target($answer, $answer_target, $encrypt_key, $answer_header);
- &update_jobdb_status_for_send_msgs($answer, $error);
- }
- }
+ # 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);
}
}
- }
-
- print $client_answer;
- return;
-}
-
-
-sub client_input {
- no strict "refs";
- my ($kernel, $session, $heap, $input, $wheel) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1];
- my $session_id = $session->ID;
- my ($msg, $msg_hash, $module);
- my $error = 0;
- my $answer_l;
- my ($answer_header, @answer_target_l, $answer_source);
- my $client_answer;
-
- daemon_log("", 5);
- daemon_log("INFO: Incoming msg from '".$heap->{'remote_ip'}."'", 5);
- daemon_log("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);
- # msg is from a gosa-si-server or gosa-si-bus
- if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- ($msg, $msg_hash, $module) = &input_from_known_server($input, $heap->{'remote_ip'});
- }
- # msg is from a gosa-si-client
- if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
- ($msg, $msg_hash, $module) = &input_from_known_client($input, $heap->{'remote_ip'});
- }
- # an error occurred
- if(( !$msg ) || ( !$msg_hash ) || ( !$module )){
$error++;
}
######################
# process incoming msg
if( $error == 0) {
$error++;
}
######################
# process incoming msg
if( $error == 0) {
- daemon_log("INFO: Incoming msg with header '".@{$msg_hash->{'header'}}[0]."'", 5);
- daemon_log("DEBUG: Processing module ".$module, 7);
+ daemon_log("$session_id INFO: Incoming msg with header '".@{$msg_hash->{'header'}}[0].
+ "' from '".$heap->{'remote_ip'}."'", 5);
+ daemon_log("$session_id DEBUG: Processing module ".$module, 7);
$answer_l = &{ $module."::process_incoming_msg" }($msg, $msg_hash, $session_id);
$answer_l = &{ $module."::process_incoming_msg" }($msg, $msg_hash, $session_id);
- if ( 0 > @{$answer_l} ) {
+ if ( 0 < @{$answer_l} ) {
my $answer_str = join("\n", @{$answer_l});
my $answer_str = join("\n", @{$answer_l});
- daemon_log("DEGUB: $module: Got answer from module: \n".$answer_str,8);
+ daemon_log("$session_id DEBUG: $module: Got answer from module: \n".$answer_str,8);
}
}
if( !$answer_l ) { $error++ };
}
}
if( !$answer_l ) { $error++ };
while( my ($hit_num, $hit) = each %{ $query_res } ) {
my $host_name = $hit->{hostname};
my $host_key = $hit->{hostkey};
while( my ($hit_num, $hit) = each %{ $query_res } ) {
my $host_name = $hit->{hostname};
my $host_key = $hit->{hostkey};
- my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header, $session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
my $host_name = $hit->{hostname};
my $host_key = $hit->{hostkey};
$answer =~ s/KNOWN_SERVER/$host_name/g;
my $host_name = $hit->{hostname};
my $host_key = $hit->{hostkey};
$answer =~ s/KNOWN_SERVER/$host_name/g;
- my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header, $session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
# target of msg is GOsa
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
# target of msg is GOsa
- elsif( $answer_target eq "GOSA" ) {
- $answer =~ /<session_id>(\d+)<\/session_id>/;
- my $session_id = $1;
- my $add_on = "";
+ elsif( $answer_target eq "GOSA" ) {
+ my $session_id = ($1) if $answer =~ /<session_id>(\d+?)<\/session_id>/;
+ my $add_on = "";
if( defined $session_id ) {
$add_on = ".session_id=$session_id";
}
if( defined $session_id ) {
$add_on = ".session_id=$session_id";
}
$answer =~ /<header>(\S+)<\/header>/;
my $header;
if( defined $1 ) { $header = $1; }
$answer =~ /<header>(\S+)<\/header>/;
my $header;
if( defined $1 ) { $header = $1; }
- my $error = &send_msg_to_target($answer, $server_address, $GosaPackages_key, $header);
+ my $error = &send_msg_to_target($answer, $server_address, $GosaPackages_key, $header, $session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
}
# target of msg is a mac address
elsif( $answer_target =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ) {
&update_jobdb_status_for_send_msgs($answer, $error);
}
# target of msg is a mac address
elsif( $answer_target =~ /^([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})$/i ) {
- daemon_log("INFO: target is mac address '$answer_target', looking for host in known_clients", 5);
+ daemon_log("$session_id INFO: target is mac address '$answer_target', looking for host in known_clients", 5);
my $sql_statement = "SELECT * FROM known_clients WHERE macaddress LIKE '$answer_target'";
my $query_res = $known_clients_db->select_dbentry( $sql_statement );
my $found_ip_flag = 0;
my $sql_statement = "SELECT * FROM known_clients WHERE macaddress LIKE '$answer_target'";
my $query_res = $known_clients_db->select_dbentry( $sql_statement );
my $found_ip_flag = 0;
my $host_name = $hit->{hostname};
my $host_key = $hit->{hostkey};
$answer =~ s/$answer_target/$host_name/g;
my $host_name = $hit->{hostname};
my $host_key = $hit->{hostkey};
$answer =~ s/$answer_target/$host_name/g;
- daemon_log("INFO: found host '$host_name', associated to '$answer_target'", 5);
- my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header);
+ daemon_log("$session_id INFO: found host '$host_name', associated to '$answer_target'", 5);
+ my $error = &send_msg_to_target($answer, $host_name, $host_key, $answer_header, $session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
$found_ip_flag++ ;
}
if( $found_ip_flag == 0) {
&update_jobdb_status_for_send_msgs($answer, $error);
$found_ip_flag++ ;
}
if( $found_ip_flag == 0) {
- daemon_log("WARNING: no host found in known_clients with mac address '$answer_target'", 3);
+ daemon_log("$session_id WARNING: no host found in known_clients with mac address '$answer_target'", 3);
if( $bus_activ eq "true" ) {
if( $bus_activ eq "true" ) {
- daemon_log("INFO: try to forward msg '$answer_header' to bus '$bus_address'", 5);
+ 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 $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);
+ my $error = &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header, $session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
last;
}
&update_jobdb_status_for_send_msgs($answer, $error);
last;
}
my $encrypt_key = &get_encrypt_key($answer_target);
if( not defined $encrypt_key ) {
# unknown target, forward msg to bus
my $encrypt_key = &get_encrypt_key($answer_target);
if( not defined $encrypt_key ) {
# unknown target, forward msg to bus
- daemon_log("WARNING: unknown target '$answer_target'", 3);
+ daemon_log("$session_id WARNING: unknown target '$answer_target'", 3);
if( $bus_activ eq "true" ) {
if( $bus_activ eq "true" ) {
- daemon_log("INFO: try to forward msg '$answer_header' to bus '$bus_address'", 5);
+ 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 ){
my $sql_statement = "SELECT * FROM known_server WHERE hostname='$bus_address'";
my $query_res = $known_server_db->select_dbentry( $sql_statement );
my $res_length = keys( %{$query_res} );
if( $res_length == 0 ){
- daemon_log("WARNING: send '$answer_header' to '$bus_address' failed, ".
+ 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};
"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);
+ my $error = &send_msg_to_target($answer, $bus_address, $bus_key, $answer_header,$session_id );
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
}
next;
}
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
}
next;
}
- my $error = &send_msg_to_target($answer, $answer_target, $encrypt_key, $answer_header);
+ my $error = &send_msg_to_target($answer, $answer_target, $encrypt_key, $answer_header,$session_id);
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
}
}
&update_jobdb_status_for_send_msgs($answer, $error);
}
}
}
}
- if( $client_answer ) {
- if( $client_answer =~ s/session_id=(\d+)$// ) {
- my $session_id = $1;
- if( defined $session_id ) {
- my $session_reference = $kernel->ID_id_to_session($session_id);
- if( defined $session_reference ) {
- $heap = $session_reference->get_heap();
- }
- }
- }
- $heap->{client}->put($client_answer);
- }
+ my $filter = POE::Filter::Reference->new();
+ my %result = (
+ status => "seems ok to me",
+ answer => $client_answer,
+ );
+
+ my $output = $filter->put( [ \%result ] );
+ print @$output;
- return;
-}
+}
sub trigger_db_loop {
my ($kernel) = @_ ;
$kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
sub trigger_db_loop {
my ($kernel) = @_ ;
$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_done_jobs', $job_queue_loop_delay);
}
sub watch_for_done_jobs {
}
sub watch_for_done_jobs {
my ($kernel,$heap) = @_[KERNEL, HEAP];
# check gosa job queue for jobs with executable timestamp
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 timestamp<'$timestamp'";
- my $res = $job_db->select_dbentry( $sql_statement );
+ 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]'";
+ }
+ $hits->{$macaddress}->{$headertag}= $hit;
+ }
- while( my ($id, $hit) = each %{$res} ) {
- my $jobdb_id = $hit->{id};
- my $macaddress = $hit->{'macaddress'};
- my $job_msg = $hit->{'xmlmessage'};
- daemon_log("DEBUG: its time to execute $job_msg", 7);
- my $header = $hit->{'headertag'};
- 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};
-
- if (not defined $target) {
- &daemon_log("ERROR: no host found for mac address: $macaddress", 1);
- &daemon_log("$hit->{xmlmessage}", 8);
- my $sql_statement = "UPDATE $job_queue_tn ".
- "SET status='error', result='no host found for mac address' ".
- "WHERE id='$jobdb_id'";
- my $res = $job_db->update_dbentry($sql_statement);
+ # 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'";
+ }
+ }
+ }
+
+ # Commit deletion
+ $job_db->exec_statementlist(\@drops);
+
+ # 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 );
+
+ # Skip new jobs for host if there is a processing job
+ if(defined($res) and defined @{$res}[0]) {
next;
}
next;
}
- # change header
- $job_msg =~ s/<header>job_/<header>gosa_/;
+ foreach my $jobdb_headertag (keys %{$hits->{$macaddress}}) {
+ my $jobdb_id = @{$hits->{$macaddress}->{$jobdb_headertag}}[0];
+ my $job_msg = @{$hits->{$macaddress}->{$jobdb_headertag}}[7];
- # add sqlite_id
- $job_msg =~ s/<\/xml>$/<jobdb_id>$jobdb_id<\/jobdb_id><\/xml>/;
+ 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 );
- my $func_error = &send_msg_to_target($job_msg, $server_address, $GosaPackages_key, $header);
+ # expect macaddress is unique!!!!!!
+ my $target = $res_hash->{1}->{hostname};
- # 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);
- }
+ # 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);
+
+ # We don't want parallel processing
+ last;
+ }
+ }
$kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
}
$kernel->delay_set('watch_for_new_jobs', $job_queue_loop_delay);
}
-sub refresh_ldap_handle {
- my $mesg;
+sub get_ldap_handle {
+ my ($session_id) = @_;
+ my $heap;
+ my $ldap_handle;
- # Get an ldap handle, if we don't have one
- if( ! defined $ldap_handle ){
- $ldap_handle = Net::LDAP->new( $ldap_uri );
- }
- # Still not defined?
- if( ! defined $ldap_handle ) {
- daemon_log( "ch $$: Net::LDAP constructor failed: $!\n" );
- return 0;
- }
+ if (not defined $session_id ) { $session_id = 0 };
- # Bind to ldap server - eventually authenticate
- if( defined $ldap_admin_dn ) {
- if( defined $ldap_admin_password ) {
- $mesg = $ldap_handle->bind( $ldap_admin_dn, password => $ldap_admin_password );
- } else {
- $mesg = $ldap_handle->bind( $ldap_admin_dn );
- }
- } else {
- $mesg = $ldap_handle->bind();
- }
+ 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);
- if( 0 != $mesg->code ) {
- undef( $ldap_handle ) if( 81 == $mesg->code );
- daemon_log( "ch $$: LDAP bind: error (". $mesg->code . ') - ' . $mesg->error . "\n", 1);
- return 0;
- }
+ } else {
+ my $session_reference = $global_kernel->ID_id_to_session($session_id);
+ if( defined $session_reference ) {
+ $heap = $session_reference->get_heap();
+ }
+
+ if (not defined $heap) {
+ daemon_log("$session_id DEBUG: cannot get heap for session_id '$session_id'", 7);
+ return;
+ }
- return 1;
+ # TODO: This "if" is nonsense, because it doesn't prove that the
+ # 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);
+ $heap->{ldap_handle} = $ldap_handle;
+ #}
+ }
+ return $ldap_handle;
}
sub change_fai_state {
}
sub change_fai_state {
- my ($st, $targets) = @_;
-
+ my ($st, $targets, $session_id) = @_;
+ $session_id = 0 if not defined $session_id;
# Set FAI state to localboot
my %mapActions= (
reboot => '',
# Set FAI state to localboot
my %mapActions= (
reboot => '',
wake => '',
memcheck => 'memcheck',
sysinfo => 'sysinfo',
wake => '',
memcheck => 'memcheck',
sysinfo => 'sysinfo',
+ install => 'install',
);
# Return if this is unknown
if (!exists $mapActions{ $st }){
);
# Return if this is unknown
if (!exists $mapActions{ $st }){
+ daemon_log("$session_id ERROR: unknown action '$st', can not translate ot FAIstate", 1);
return;
}
my $state= $mapActions{ $st };
return;
}
my $state= $mapActions{ $st };
- &refresh_ldap_handle();
+ my $ldap_handle = &get_ldap_handle($session_id);
if( defined($ldap_handle) ) {
# Build search filter for hosts
if( defined($ldap_handle) ) {
# Build search filter for hosts
- my $search= "(&(objectClass=GOhard)";
- foreach (@{$targets}){
- $search.= "(macAddress=$_)";
- }
- $search.= ")";
+ my $search= "(&(objectClass=GOhard)";
+ foreach (@{$targets}){
+ $search.= "(macAddress=$_)";
+ }
+ $search.= ")";
# If there's any host inside of the search string, procress them
# If there's any host inside of the search string, procress them
- if (!($search =~ /macAddress/)){
- return;
- }
+ if (!($search =~ /macAddress/)){
+ daemon_log("$session_id ERROR: no macAddress found in filter statement for LDAP search: '$search'", 1);
+ return;
+ }
# Perform search for Unit Tag
my $mesg = $ldap_handle->search(
# Perform search for Unit Tag
my $mesg = $ldap_handle->search(
filter => "$search"
);
filter => "$search"
);
- if ($mesg->count) {
- my @entries = $mesg->entries;
- foreach my $entry (@entries) {
-
- # Only modify entry if it is not set to '$state'
- if ($entry->get_value("FAIstate") ne "$state"){
-
- daemon_log("INFO: Setting FAIstate to '$state' for ".$entry->dn, 5);
- my $result;
- my %tmp = map { $_ => 1 } $entry->get_value("objectClass");
- if (exists $tmp{'FAIobject'}){
- if ($state eq ''){
- $result= $ldap_handle->modify($entry->dn, changes => [
- delete => [ FAIstate => [] ] ]);
- } else {
- $result= $ldap_handle->modify($entry->dn, changes => [
- replace => [ FAIstate => $state ] ]);
- }
- } elsif ($state ne ''){
- $result= $ldap_handle->modify($entry->dn, changes => [
- add => [ objectClass => 'FAIobject' ],
- add => [ FAIstate => $state ] ]);
- }
-
- # Errors?
- if ($result->code){
- daemon_log("Error: Setting FAIstate to '$state' for ".$entry->dn. "failed: ".$result->error, 1);
- }
-
- }
- }
- }
+ if ($mesg->count) {
+ my @entries = $mesg->entries;
+ 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 INFO: Setting FAIstate to '$state' for ".$entry->dn, 5);
+ my $result;
+ my %tmp = map { $_ => 1 } $entry->get_value("objectClass");
+ if (exists $tmp{'FAIobject'}){
+ if ($state eq ''){
+ $result= $ldap_handle->modify($entry->dn, changes => [
+ delete => [ FAIstate => [] ] ]);
+ } else {
+ $result= $ldap_handle->modify($entry->dn, changes => [
+ replace => [ FAIstate => $state ] ]);
+ }
+ } elsif ($state ne ''){
+ $result= $ldap_handle->modify($entry->dn, changes => [
+ add => [ objectClass => 'FAIobject' ],
+ add => [ FAIstate => $state ] ]);
+ }
+
+ # Errors?
+ if ($result->code){
+ daemon_log("$session_id Error: Setting FAIstate to '$state' for ".$entry->dn. "failed: ".$result->error, 1);
+ }
+ } else {
+ daemon_log("$session_id DEBUG FAIstate at host '".$entry->dn."' already at state '$st'", 7);
+ }
+ }
+ }
+ # if no ldap handle defined
+ } else {
+ daemon_log("$session_id ERROR: no LDAP handle defined for update FAIstate", 1);
}
}
+
}
sub change_goto_state {
}
sub change_goto_state {
- my ($st, $targets) = @_;
+ my ($st, $targets, $session_id) = @_;
+ $session_id = 0 if not defined $session_id;
# Switch on or off?
my $state= $st eq 'active' ? 'active': 'locked';
# Switch on or off?
my $state= $st eq 'active' ? 'active': 'locked';
- &refresh_ldap_handle();
+ my $ldap_handle = &get_ldap_handle($session_id);
if( defined($ldap_handle) ) {
# Build search filter for hosts
if( defined($ldap_handle) ) {
# Build search filter for hosts
# Only modify entry if it is not set to '$state'
if ($entry->get_value("gotoMode") ne $state){
# Only modify entry if it is not set to '$state'
if ($entry->get_value("gotoMode") ne $state){
- daemon_log("INFO: Setting gotoMode to '$state' for ".$entry->dn, 5);
+ daemon_log("$session_id INFO: Setting gotoMode to '$state' for ".$entry->dn, 5);
my $result;
$result= $ldap_handle->modify($entry->dn, changes => [
replace => [ gotoMode => $state ] ]);
# Errors?
if ($result->code){
my $result;
$result= $ldap_handle->modify($entry->dn, changes => [
replace => [ gotoMode => $state ] ]);
# Errors?
if ($result->code){
- &daemon_log("Error: Setting gotoMode to '$state' for ".$entry->dn. "failed: ".$result->error, 1);
+ &daemon_log("$session_id Error: Setting gotoMode to '$state' for ".$entry->dn. "failed: ".$result->error, 1);
}
}
}
}
sub create_fai_server_db {
sub create_fai_server_db {
- my ($table_name) = @_;
+ my ($table_name, $kernel) = @_;
my $result;
my $result;
-
+ my $ldap_handle = &get_ldap_handle();
if(defined($ldap_handle)) {
daemon_log("INFO: create_fai_server_db: start", 5);
my $mesg= $ldap_handle->search(
if(defined($ldap_handle)) {
daemon_log("INFO: create_fai_server_db: start", 5);
my $mesg= $ldap_handle->search(
primkey => ['server', 'release', 'tag'],
server => $tmp_url,
release => $tmp_release,
primkey => ['server', 'release', 'tag'],
server => $tmp_url,
release => $tmp_release,
+ sections => $tmp_sections,
tag => (length($tmp_tag)>0)?$tmp_tag:"",
} );
}
tag => (length($tmp_tag)>0)?$tmp_tag:"",
} );
}
}
}
daemon_log("INFO: create_fai_server_db: finished", 5);
}
}
daemon_log("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);
}
}
+
+ $ldap_handle->disconnect;
return $result;
}
return $result;
}
+
sub run_create_fai_server_db {
sub run_create_fai_server_db {
- my ($session, $heap, $table_name) = @_[SESSION, HEAP, ARG0];
+ my ($kernel, $session, $heap, $table_name) = @_[KERNEL, SESSION, HEAP, ARG0];
my $task = POE::Wheel::Run->new(
my $task = POE::Wheel::Run->new(
- Program => sub { &create_fai_server_db($table_name) },
+ Program => sub { &create_fai_server_db($table_name,$kernel) },
StdoutEvent => "session_run_result",
StderrEvent => "session_run_debug",
CloseEvent => "session_run_done",
StdoutEvent => "session_run_result",
StderrEvent => "session_run_debug",
CloseEvent => "session_run_done",
my ($table_name) = @_;
my $result;
my ($table_name) = @_;
my $result;
+ my $ldap_handle = &get_ldap_handle();
if(defined($ldap_handle)) {
daemon_log("INFO: create_fai_release_db: start",5);
my $mesg= $ldap_handle->search(
if(defined($ldap_handle)) {
daemon_log("INFO: create_fai_release_db: start",5);
my $mesg= $ldap_handle->search(
my @sql_list;
my $timestamp= &get_time();
foreach my $ou (@{$mesg->{entries}}) {
my @sql_list;
my $timestamp= &get_time();
foreach my $ou (@{$mesg->{entries}}) {
- my $tmp_classes= resolve_fai_classes($ou->dn);
+ my $tmp_classes= resolve_fai_classes($ou->dn, $ldap_handle);
if(defined($tmp_classes) && ref($tmp_classes) eq 'HASH') {
my @tmp_array=get_fai_release_entries($tmp_classes);
if(@tmp_array) {
if(defined($tmp_classes) && ref($tmp_classes) eq 'HASH') {
my @tmp_array=get_fai_release_entries($tmp_classes);
if(@tmp_array) {
}
daemon_log("INFO: create_fai_release_db: finished",5);
}
}
daemon_log("INFO: create_fai_release_db: finished",5);
}
-
+ $ldap_handle->disconnect;
return $result;
}
sub run_create_fai_release_db {
return $result;
}
sub run_create_fai_release_db {
}
sub resolve_fai_classes {
}
sub resolve_fai_classes {
+ my ($fai_base, $ldap_handle) = @_;
my $result;
my $result;
- my $fai_base= shift;
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;
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;
delete $heap->{task}->{$task_id};
}
delete $heap->{task}->{$task_id};
}
+sub create_sources_list {
+ my ($ldap_handle) = @_;
+ my $result="/tmp/gosa_si_tmp_sources_list";
+
+ # Remove old file
+ if(stat($result)) {
+ unlink($result);
+ }
+
+ my $fh;
+ open($fh, ">$result") or return undef;
+ if(defined($ldap_server_dn) and length($ldap_server_dn) > 0) {
+ my $mesg=$ldap_handle->search(
+ base => $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)}) {
+ my ($server, $tag, $release, $sections)= split /\|/, $value;
+ my $line = "deb $server $release";
+ $sections =~ s/,/ /g;
+ $line.= " $sections";
+ print $fh $line."\n";
+ }
+ }
+ }
+ }
+ close($fh);
+
+ return $result;
+}
sub create_packages_list_db {
sub create_packages_list_db {
- my ($sources_file) = @_ ;
+ my ($ldap_handle, $sources_file) = @_ ;
+
+ 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);
+ return;
+ }
+ }
+
+ if (not defined $sources_file) {
+ $sources_file = &create_sources_list($ldap_handle);
+ }
+
my $line;
daemon_log("INFO: create_packages_list_db: start", 5);
my $line;
daemon_log("INFO: create_packages_list_db: start", 5);
daemon_log("INFO: create_packages_list_db: finished", 5);
return;
}
daemon_log("INFO: create_packages_list_db: finished", 5);
return;
}
+
sub run_create_packages_list_db {
sub run_create_packages_list_db {
- my ($session, $heap, $sources_list) = @_[SESSION, HEAP, ARG0];
+ my ($session, $heap) = @_[SESSION, HEAP];
my $task = POE::Wheel::Run->new(
my $task = POE::Wheel::Run->new(
- Program => sub { &create_packages_list_db($sources_list) },
+ Program => sub {&create_packages_list_db},
StdoutEvent => "session_run_result",
StderrEvent => "session_run_debug",
CloseEvent => "session_run_done",
);
StdoutEvent => "session_run_result",
StderrEvent => "session_run_debug",
CloseEvent => "session_run_done",
);
-
$heap->{task}->{ $task->ID } = $task;
}
$heap->{task}->{ $task->ID } = $task;
}
+
sub parse_package_info {
my ($baseurl, $dist, $section)= @_;
my ($package);
my ($path) = ($baseurl =~ m%://[^/]*(.*)$%);
sub parse_package_info {
my ($baseurl, $dist, $section)= @_;
my ($package);
my ($path) = ($baseurl =~ m%://[^/]*(.*)$%);
+ $repo_dirs{ "${repo_path}/pool" } = 1;
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 );
}
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 ) );
}
}
+
sub get_package {
my ($url, $dest)= @_;
sub get_package {
my ($url, $dest)= @_;
# 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'");
# 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'");
- system("rm -f '$dest'");
+ unlink($dest);
} else {
daemon_log("ERROR: create_packages_list_db: get_packages: fetching '$url' failed!", 1);
}
return 0;
}
} else {
daemon_log("ERROR: create_packages_list_db: get_packages: fetching '$url' failed!", 1);
}
return 0;
}
+
sub parse_package {
sub parse_package {
- my ($path, $dist, $srv_path )= @_;
+ my ($path, $dist, $srv_path)= @_;
my ($package, $version, $section, $description);
my @sql_list;
my $PACKAGES;
my ($package, $version, $section, $description);
my @sql_list;
my $PACKAGES;
# Use empty lines as a trigger
if ($line =~ /^\s*$/){
# Use empty lines as a trigger
if ($line =~ /^\s*$/){
- my $sql = "INSERT INTO packages_list VALUES ('$dist', '$package', '$version', '$section', 'xxx', 'none', '0')";
+ my $sql = "INSERT INTO packages_list VALUES ('$dist', '$package', '$version', '$section', '', 'none', '0')";
push(@sql_list, $sql);
$package = "none";
$version = "none";
push(@sql_list, $sql);
$package = "none";
$version = "none";
next;
}
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 );
}
close( $PACKAGES );
$packages_list_db->exec_statementlist(\@sql_list);
}
$packages_list_db->exec_statementlist(\@sql_list);
}
-#
-#sub store_fileinfo {
-# my( $package, $file, $dist, $path, $vers ) = @_;
-#
-# my %fileinfo = (
-# 'package' => $package,
-# 'dist' => $dist,
-# 'version' => $vers
-# );
-#
-# $repo_files{ "${srvdir}${path}/$file" } = \%fileinfo;
-#}
+sub store_fileinfo {
+ my( $package, $file, $dist, $path, $vers, $srvdir) = @_;
+
+ my %fileinfo = (
+ 'package' => $package,
+ 'dist' => $dist,
+ 'version' => $vers,
+ );
+
+ $repo_files{ "${srvdir}/$file" } = \%fileinfo;
+}
+
+sub cleanup_and_extract {
+ my $fileinfo = $repo_files{ $File::Find::name };
+
+ if( defined $fileinfo ) {
+
+ 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'" );
+
+ if( -f "$dir/DEBIAN/templates" ) {
+
+ 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");
+
+ $sql= "update $main::packages_list_tn set template = '$tmpl' where package = '$package' and version = '$newver';";
+
+ } else {
+ $sql= "update $main::packages_list_tn set template = '' where package = '$package' and version = '$newver';";
+ }
+
+ my $res= $main::packages_list_db->update_dbentry($sql);
+ }
+}
#==== MAIN = main ==============================================================
#==== MAIN = main ==============================================================
$bus_activ = "false"
}
$bus_activ = "false"
}
-
-
-# delete old DBsqlite lock files
-#unlink('/tmp/gosa_si_lock*');
-
# connect to gosa-si job queue
$job_db = GOSA::DBsqlite->new($job_queue_file_name);
$job_db->create_table($job_queue_tn, \@job_queue_col_names);
# connect to gosa-si job queue
$job_db = GOSA::DBsqlite->new($job_queue_file_name);
$job_db->create_table($job_queue_tn, \@job_queue_col_names);
$login_users_db->create_table($login_users_tn, \@login_users_col_names);
# connect to fai_server_db and fai_release_db
$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);
$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);
$kernel->yield("next_task");
},
InlineStates => {
$kernel->yield("next_task");
},
InlineStates => {
- next_task => \&next_tasks,
+ next_task => \&next_task,
task_result => \&handle_task_result,
task_done => \&handle_task_done,
task_debug => \&handle_task_debug,
task_result => \&handle_task_result,
task_done => \&handle_task_done,
task_debug => \&handle_task_debug,
@@ -2227,6 +2233,7 @@ daemon_log("start socket for incoming xml messages at port '$server_port' ", 1);
POE::Session->create(
inline_states => {
_start => \&_start,
POE::Session->create(
inline_states => {
_start => \&_start,
+ sig_handler => \&sig_handler,
watch_for_new_jobs => \&watch_for_new_jobs,
watch_for_done_jobs => \&watch_for_done_jobs,
create_packages_list_db => \&run_create_packages_list_db,
watch_for_new_jobs => \&watch_for_new_jobs,
watch_for_done_jobs => \&watch_for_done_jobs,
create_packages_list_db => \&run_create_packages_list_db,