From f7a7fb50c3607559597d53e012b445c69fab114f Mon Sep 17 00:00:00 2001 From: janw Date: Thu, 18 Sep 2008 11:50:18 +0000 Subject: [PATCH] Moved new MySQL Backend to trunk. git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6-lhm@12503 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/debian/gosa-si-server.postinst | 29 - gosa-si/debian/rules | 6 +- gosa-si/debian/templates | 28 - gosa-si/gosa-si-server | 664 ++++++++++---------- gosa-si/modules/{DBmysql.pm => DBsqlite.pm} | 221 ++++--- gosa-si/modules/GosaPackages.pm | 2 +- gosa-si/server/events/gosaTriggered.pm | 52 +- 7 files changed, 489 insertions(+), 513 deletions(-) delete mode 100644 gosa-si/debian/templates rename gosa-si/modules/{DBmysql.pm => DBsqlite.pm} (58%) diff --git a/gosa-si/debian/gosa-si-server.postinst b/gosa-si/debian/gosa-si-server.postinst index e20a28fc9..ccbf8c3c9 100644 --- a/gosa-si/debian/gosa-si-server.postinst +++ b/gosa-si/debian/gosa-si-server.postinst @@ -1,18 +1,5 @@ #!/bin/sh -set -e - -. /usr/share/debconf/confmodule - -db_version 2.0 - -for i in hostname database username password rootpw; do - db_get gosa-si/$i || true - eval $i'="$RET"' -done - -host="127.0.0.1" - #DEBHELPER# # We exit unless the package is being configured @@ -24,26 +11,10 @@ case "$1" in *) exit 0; esac -# Setup database -/usr/bin/mysqladmin -u root --password=$rootpw create "$database" &> /dev/null || true -/usr/bin/mysql -u root --password=$rootpw -e "GRANT ALL PRIVILEGES ON $database.* to '$username'@'127.0.0.1' identified by '$password'" - # Fix permission [ -f /etc/gosa-si/server.conf ] && chmod go-rwx /etc/gosa-si/server.conf -# Apply DB settings to config file -if [ -w /etc/gosa-si/server.conf ]; then - for i in database username password host; do - eval 'val=$'$i - grep -E "mysql-$i" /etc/gosa-si/server.conf | grep -vE '^ *#' && sed -i "/\[server\]/,/\[/s/mysql-$i\s*=\s*\(.*\)$/mysql-$i = $val/g" /etc/gosa-si/server.conf || sed -i " -/\[server\]/ a\ -mysql-$i = $val" /etc/gosa-si/server.conf - done -fi - # Restart daemon invoke-rc.d gosa-si restart -db_stop - exit 0 diff --git a/gosa-si/debian/rules b/gosa-si/debian/rules index 0b12205ef..892de833c 100755 --- a/gosa-si/debian/rules +++ b/gosa-si/debian/rules @@ -2,7 +2,7 @@ # Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess. # Uncomment this to turn on verbose mode. -# export DH_VERBOSE=1 +#export DH_VERBOSE=1 build: patch #******************************************************** @@ -37,7 +37,7 @@ install-stamp: done -find debian/tmp -name '*.svn' -type d -exec rm -rf {} \; 2> /dev/null - -for i in gosa-si-client gosa-si-server modules/GosaPackages.pm modules/ClientPackages.pm modules/ServerPackages.pm server/events/*; do sed -i 's/use GosaSupportDaemon;/use GOSA::GosaSupportDaemon;/g;s/use DBmysql;/use GOSA::DBmysql;/g' debian/tmp/$$i; done + -for i in gosa-si-client gosa-si-server modules/GosaPackages.pm modules/ClientPackages.pm modules/ServerPackages.pm server/events/*; do sed -i 's/use GosaSupportDaemon;/use GOSA::GosaSupportDaemon;/g;s/use DBsqlite;/use GOSA::DBsqlite;/g' debian/tmp/$$i; done -sed -i 's!"/etc/gosa-si/modules";!use lib "/usr/lib/gosa-si/modules";!g' debian/tmp/gosa-si-server @@ -65,7 +65,7 @@ binary-indep: install dh_installchangelogs dh_installman dh_installlogrotate - dh_installdebconf + #dh_installdebconf dh_installinit --init-script=gosa-si -- start 00 2 3 4 5 . stop 29 1 . chmod 640 debian/gosa-si-server/etc/gosa-si/server.conf debian/gosa-si-client/etc/gosa-si/client.conf diff --git a/gosa-si/debian/templates b/gosa-si/debian/templates deleted file mode 100644 index 119524367..000000000 --- a/gosa-si/debian/templates +++ /dev/null @@ -1,28 +0,0 @@ -Template: gosa-si/hostname -Type: string -Default: 127.0.0.1 -Description: Hostname or IP of the MySQL-Server - Please enter the full-qualified hostname or IP Address of the desired MySQL Host. - -Template: gosa-si/database -Type: string -Default: gosa_si -Description: Name of the GOsa-SI Database - Please choose a name for the gosa-si database. - -Template: gosa-si/username -Type: string -Default: gosa_si -Description: Name of the GOsa-SI Database User - Please choose a name for the gosa-si database user. - -Template: gosa-si/password -Type: password -Description: Password for the GOsa-SI Database User - Please enter a password for the gosa-si database user. - -Template: gosa-si/rootpw -Type: password -Description: Password for the Database Administrator 'root' - In order to create the privileged account, setup needs the password of the - database administrator. Please enter the password of the database user 'root'. diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index ca826949b..0ed129cb4 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -48,7 +48,7 @@ use File::Basename; use File::Find; use File::Copy; use File::Path; -use GOSA::DBmysql; +use GOSA::DBsqlite; use GOSA::GosaSupportDaemon; use POE qw(Component::Server::TCP Wheel::Run Filter::Reference); use Net::LDAP; @@ -100,7 +100,6 @@ our $verbose; our $forground; our $cfg_file; our ($ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $ldap_server_dn); -our ($mysql_username, $mysql_password, $mysql_database, $mysql_host); our $known_modules; our $root_uid; our $adm_gid; @@ -124,81 +123,81 @@ my $watch_for_new_jobs_in_progress = 0; our $incoming_db; our $incoming_tn = 'incoming'; my $incoming_file_name; -my @incoming_col_names = ("id INTEGER PRIMARY KEY auto_increment", - "timestamp VARCHAR(14) DEFAULT 'none'", - "headertag VARCHAR(255) DEFAULT 'none'", - "targettag VARCHAR(255) DEFAULT 'none'", - "xmlmessage TEXT", - "module VARCHAR(255) DEFAULT 'none'", - "sessionid VARCHAR(255) DEFAULT '0'", -); +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 PRIMARY KEY auto_increment", - "timestamp VARCHAR(14) DEFAULT 'none'", - "status VARCHAR(255) DEFAULT 'none'", - "result TEXT", - "progress VARCHAR(255) DEFAULT 'none'", - "headertag VARCHAR(255) DEFAULT 'none'", - "targettag VARCHAR(255) DEFAULT 'none'", - "xmlmessage TEXT", - "macaddress VARCHAR(17) DEFAULT 'none'", - "plainname VARCHAR(255) DEFAULT 'none'", - "siserver VARCHAR(255) DEFAULT 'none'", - "modified INTEGER DEFAULT '0'", -); +my @job_queue_col_names = ("id INTEGER PRIMARY KEY", + "timestamp DEFAULT 'none'", + "status DEFAULT 'none'", + "result DEFAULT 'none'", + "progress DEFAULT 'none'", + "headertag DEFAULT 'none'", + "targettag DEFAULT 'none'", + "xmlmessage DEFAULT 'none'", + "macaddress DEFAULT 'none'", + "plainname DEFAULT 'none'", + "siserver DEFAULT 'none'", + "modified DEFAULT '0'", + ); # 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 VARCHAR(255)", "macaddress VARCHAR(17)", "status VARCHAR(255)", "hostkey VARCHAR(255)", "loaded_modules TEXT", "timestamp VARCHAR(14)"); +my @known_server_col_names = ("hostname", "macaddress", "status", "hostkey", "loaded_modules", "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 VARCHAR(255)", "status VARCHAR(255)", "hostkey VARCHAR(255)", "timestamp VARCHAR(14)", "macaddress VARCHAR(17)", "events TEXT", "keylifetime VARCHAR(255)"); +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 VARCHAR(255)", "macaddress VARCHAR(17)", "regserver VARCHAR(255)", "timestamp VARCHAR(14)"); +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 VARCHAR(255)", "user VARCHAR(255)", "timestamp VARCHAR(14)"); +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 VARCHAR(14)", "server VARCHAR(255)", "fai_release VARCHAR(255)", "sections VARCHAR(255)", "tag VARCHAR(255)"); +our @fai_server_col_names = ("timestamp", "server", "release", "sections", "tag"); our $fai_release_db; our $fai_release_tn = "fai_release"; my $fai_release_file_name; -our @fai_release_col_names = ("timestamp VARCHAR(14)", "fai_release VARCHAR(255)", "class VARCHAR(255)", "type VARCHAR(255)", "state VARCHAR(255)"); +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 VARCHAR(255)", "package VARCHAR(255)", "version VARCHAR(255)", "section VARCHAR(255)", "description TEXT", "template LONGBLOB", "timestamp VARCHAR(14)"); +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 = ("id INTEGER", "subject TEXT", "message_from VARCHAR(255)", "message_to VARCHAR(255)", - "flag VARCHAR(255)", "direction VARCHAR(255)", "delivery_time VARCHAR(255)", "message TEXT", "timestamp VARCHAR(14)" ); +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 @@ -247,10 +246,6 @@ our $logged_in_user_date_of_expiry = 600; "gosa-unit-tag" => [\$gosa_unit_tag, ""], "max-clients" => [\$max_clients, 10], "wol-password" => [\$wake_on_lan_passwd, ""], - "mysql-username" => [\$mysql_username, "gosa_si"], - "mysql-password" => [\$mysql_password, ""], - "mysql-database" => [\$mysql_database, "gosa_si"], - "mysql-host" => [\$mysql_host, "127.0.0.1"], }, "GOsaPackages" => { "job-queue" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'], @@ -1016,264 +1011,263 @@ sub sig_handler { sub msg_to_decrypt { - my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; - my $session_id = $session->ID; - my ($msg, $msg_hash, $module); - my $error = 0; - - # hole neue msg aus @msgs_to_decrypt - my $next_msg = shift @msgs_to_decrypt; - - # entschlüssle sie + my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; + my $session_id = $session->ID; + my ($msg, $msg_hash, $module); + my $error = 0; - # 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 WARNING 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", 3); - 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 = "
gosa_ping
$server_address $host_name
"; - 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++; - } + # hole neue msg aus @msgs_to_decrypt + my $next_msg = shift @msgs_to_decrypt; + + # entschlüssle sie + # 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 WARNING 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", 3); + 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 = "
gosa_ping
$server_address $host_name
"; + 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 $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 $local_mac; - my ($target_ip, $target_port) = split(':', $target); - - # Determine the local ip address if target is an ip address + my $local_address; + my $local_mac; + my ($target_ip, $target_port) = split(':', $target); + + # Determine the local ip address if target is an ip address if ($target =~ /^\d+\.\d+\.\d+\.\d+:\d+$/) { $local_address = &get_local_ip_for_remote_ip($target_ip).":$server_port"; } else { - $local_address = $server_address; - } + $local_address = $server_address; + } - # Determine the local mac address if target is a mac address - if ($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) { - my $loc_ip = &get_local_ip_for_remote_ip($heap->{'remote_ip'}); - my $network_interface= &get_interface_for_ip($loc_ip); - $local_mac = &get_mac_for_interface($network_interface); - } else { - $local_mac = $server_mac_address; - } + # Determine the local mac address if target is a mac address + if ($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) { + my $loc_ip = &get_local_ip_for_remote_ip($heap->{'remote_ip'}); + my $network_interface= &get_interface_for_ip($loc_ip); + $local_mac = &get_mac_for_interface($network_interface); + } else { + $local_mac = $server_mac_address; + } - # target and source is equal to GOSA -> process here - if (not $done) { - if ($target eq "GOSA" && $source eq "GOSA") { - $done = 1; - &daemon_log("$session_id DEBUG: target and source is 'GOSA' -> process here", 7); - } - } + # target and source is equal to GOSA -> process here + if (not $done) { + if ($target eq "GOSA" && $source eq "GOSA") { + $done = 1; + &daemon_log("$session_id DEBUG: target and source is 'GOSA' -> process here", 7); + } + } - # target is own address without forward_to_gosa-tag -> process here - if (not $done) { - #if ((($target eq $local_address) || ($target eq $local_mac) ) && (not exists $msg_hash->{'forward_to_gosa'})) { - if (($target eq $local_address) && (not exists $msg_hash->{'forward_to_gosa'})) { - $done = 1; - if ($source eq "GOSA") { - $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; - } - &daemon_log("$session_id DEBUG: target is own address without forward_to_gosa-tag -> process here", 7); - } - } + # target is own address without forward_to_gosa-tag -> process here + if (not $done) { + #if ((($target eq $local_address) || ($target eq $local_mac) ) && (not exists $msg_hash->{'forward_to_gosa'})) { + if (($target eq $local_address) && (not exists $msg_hash->{'forward_to_gosa'})) { + $done = 1; + if ($source eq "GOSA") { + $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; + } + &daemon_log("$session_id DEBUG: target is own address without forward_to_gosa-tag -> process here", 7); + } + } - # target is a client address in known_clients -> process here + # 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>/$hostname<\/target>/; - my $local_address = &get_local_ip_for_remote_ip($target_ip).":$server_port"; - if ($source eq "GOSA") { - $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; - } - &daemon_log("$session_id DEBUG: target is a client address in known_clients -> process here", 7); - - } else { - $not_found_in_known_clients_db = 1; - } - } + $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>/$hostname<\/target>/; + my $local_address = &get_local_ip_for_remote_ip($target_ip).":$server_port"; + if ($source eq "GOSA") { + $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; + } + &daemon_log("$session_id DEBUG: target is a client address in known_clients -> process here", 7); - # 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; - &daemon_log("$session_id DEBUG: target is own address with forward_to_gosa-tag not pointing to myself -> process here", 7); + } 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; + &daemon_log("$session_id DEBUG: target is own address with forward_to_gosa-tag not pointing to myself -> process here", 7); + } + } + } - # if message should be processed here -> add message to incoming_db + # 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, - } ); + # 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); - &daemon_log("$session_id INFO: incoming '$header' message forwarded to GOsa", 5); - } - $done = 1; - &daemon_log("$session_id DEBUG: target is own address with forward_to_gosa-tag pointing at myself -> forward to gosa", 7); - } - } + # 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); + &daemon_log("$session_id INFO: incoming '$header' message forwarded to GOsa", 5); + } + $done = 1; + &daemon_log("$session_id DEBUG: target is own address with forward_to_gosa-tag pointing at myself -> forward to gosa", 7); + } + } - } + } - # 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 ($host_ip, $host_port) = split(/:/, $hostname); - my $local_address = &get_local_ip_for_remote_ip($host_ip).":$server_port"; - 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/GOSA<\/source>/$local_address<\/source>/; - $msg =~ s/$target<\/target>/$hostname<\/target>/; - if ($source eq "GOSA") { - $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; - } - &send_msg_to_target($msg, $regserver, $regserver_key, $header, $session_id); - } - $done = 1; - &daemon_log("$session_id DEBUG: target is a client address in foreign_clients -> forward to registration server", 7); - } else { + # 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 ($host_ip, $host_port) = split(/:/, $hostname); + my $local_address = &get_local_ip_for_remote_ip($host_ip).":$server_port"; + 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/GOSA<\/source>/$local_address<\/source>/; + $msg =~ s/$target<\/target>/$hostname<\/target>/; + if ($source eq "GOSA") { + $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; + } + &send_msg_to_target($msg, $regserver, $regserver_key, $header, $session_id); + } + $done = 1; + &daemon_log("$session_id DEBUG: target is a client address in foreign_clients -> forward to registration server", 7); + } 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' OR macaddress LIKE '$target')"; - $res = $known_server_db->select_dbentry($sql); - if (keys(%$res) > 0) { - my $hostkey = $res->{1}->{'hostkey'}; + # target is a server address -> forward to server + if (not $done) { + $sql = "SELECT * FROM $known_server_tn WHERE (hostname='$target' OR macaddress LIKE '$target')"; + $res = $known_server_db->select_dbentry($sql); + if (keys(%$res) > 0) { + my $hostkey = $res->{1}->{'hostkey'}; - if ($source eq "GOSA") { - $msg =~ s/GOSA<\/source>/$local_address<\/source>/; - $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; + if ($source eq "GOSA") { + $msg =~ s/GOSA<\/source>/$local_address<\/source>/; + $msg =~ s/<\/xml>/$local_address,$session_id<\/forward_to_gosa><\/xml>/; - } + } - &send_msg_to_target($msg, $target, $hostkey, $header, $session_id); - $done = 1; - &daemon_log("$session_id DEBUG: target is a server address -> forward to server", 7); - } else { + &send_msg_to_target($msg, $target, $hostkey, $header, $session_id); + $done = 1; + &daemon_log("$session_id DEBUG: target is a server address -> forward to server", 7); + } 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; - &daemon_log("$session_id DEBUG: target is not in foreign_clients_db, known_server_db or known_clients_db, maybe it is a complete new one -> process here", 7); + && $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; + &daemon_log("$session_id DEBUG: target is not in foreign_clients_db, known_server_db or known_clients_db, maybe it is a complete new one -> process here", 7); } - 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); - } - } - } + 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; + } + + return; } @@ -1334,11 +1328,7 @@ sub process_task { my $module = $task->{'module'}; my $header = $task->{'headertag'}; my $session_id = $task->{'sessionid'}; - my $msg_hash; - eval { - $msg_hash = $xml->XMLin($msg, ForceArray=>1); - }; - daemon_log("ERROR: XML failure '$@'") if ($@); + my $msg_hash = $xml->XMLin($msg, ForceArray=>1); my $source = @{$msg_hash->{'source'}}[0]; # set timestamp of incoming client uptodate, so client will not @@ -1686,7 +1676,7 @@ sub watch_for_new_jobs { # check gosa job quaeue for jobs with executable timestamp my $timestamp = &get_time(); - my $sql_statement = "SELECT * FROM $job_queue_tn WHERE status='waiting' AND (CAST(timestamp AS UNSIGNED)) < $timestamp ORDER BY timestamp"; + 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 @@ -1792,7 +1782,7 @@ sub watch_for_new_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 UNSIGNED))<$timestamp AND flag='n' AND direction='in' )"; + 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}) { @@ -2057,7 +2047,7 @@ sub watch_for_next_tasks { my $res = $incoming_db->exec_statement($sql); } - $kernel->delay_set('watch_for_next_tasks', 1); + $kernel->delay_set('watch_for_next_tasks', 0.1); } @@ -2275,11 +2265,11 @@ sub run_create_fai_server_db { sub create_fai_server_db { - my ($table_name, $kernel, $dont_create_packages_list, $session_id) = @_; + 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(); + my $ldap_handle = &get_ldap_handle(); if(defined($ldap_handle)) { daemon_log("$session_id INFO: create_fai_server_db: start", 5); my $mesg= $ldap_handle->search( @@ -2289,25 +2279,25 @@ sub create_fai_server_db { filter => "(&(FAIrepository=*)(objectClass=FAIrepositoryServer))", ); if($mesg->{'resultCode'} == 0 && - $mesg->count != 0) { - foreach my $entry (@{$mesg->{entries}}) { - if($entry->exists('FAIrepository')) { - # Add an entry for each Repository configured for server - foreach my $repo(@{$entry->get_value('FAIrepository', asref => 1)}) { - my($tmp_url,$tmp_server,$tmp_release,$tmp_sections) = split(/\|/, $repo); - my $tmp_tag= $entry->get_value('gosaUnitTag') || ""; - $result= $fai_server_db->add_dbentry( { - table => $table_name, - primkey => ['server', 'fai_release', 'tag'], - server => $tmp_url, - fai_release => $tmp_release, - sections => $tmp_sections, - tag => (length($tmp_tag)>0)?$tmp_tag:"", - } ); - } - } - } - } + $mesg->count != 0) { + foreach my $entry (@{$mesg->{entries}}) { + if($entry->exists('FAIrepository')) { + # Add an entry for each Repository configured for server + foreach my $repo(@{$entry->get_value('FAIrepository', asref => 1)}) { + my($tmp_url,$tmp_server,$tmp_release,$tmp_sections) = split(/\|/, $repo); + my $tmp_tag= $entry->get_value('gosaUnitTag') || ""; + $result= $fai_server_db->add_dbentry( { + table => $table_name, + primkey => ['server', 'release', 'tag'], + server => $tmp_url, + release => $tmp_release, + sections => $tmp_sections, + tag => (length($tmp_tag)>0)?$tmp_tag:"", + } ); + } + } + } + } daemon_log("$session_id INFO: create_fai_server_db: finished", 5); # TODO: Find a way to post the 'create_packages_list_db' event @@ -2315,24 +2305,24 @@ sub create_fai_server_db { &create_packages_list_db(undef, undef, $session_id); } } - - $ldap_handle->disconnect; - return $result; + + $ldap_handle->disconnect; + return $result; } sub run_create_fai_release_db { - my ($session, $heap, $table_name) = @_[SESSION, HEAP, ARG0]; + my ($session, $heap, $table_name) = @_[SESSION, HEAP, ARG0]; my $session_id = $session->ID; - my $task = POE::Wheel::Run->new( - Program => sub { &create_fai_release_db($table_name, $session_id) }, - StdoutEvent => "session_run_result", - StderrEvent => "session_run_debug", - CloseEvent => "session_run_done", - ); + my $task = POE::Wheel::Run->new( + Program => sub { &create_fai_release_db($table_name, $session_id) }, + StdoutEvent => "session_run_result", + StderrEvent => "session_run_debug", + CloseEvent => "session_run_done", + ); - $heap->{task}->{ $task->ID } = $task; - return; + $heap->{task}->{ $task->ID } = $task; + return; } @@ -2340,10 +2330,10 @@ sub create_fai_release_db { my ($table_name, $session_id) = @_; my $result; - # used for logging - if (not defined $session_id) { $session_id = 0; } + # used for logging + if (not defined $session_id) { $session_id = 0; } - my $ldap_handle = &get_ldap_handle(); + my $ldap_handle = &get_ldap_handle(); if(defined($ldap_handle)) { daemon_log("$session_id INFO: create_fai_release_db: start",5); my $mesg= $ldap_handle->search( @@ -2366,7 +2356,7 @@ sub create_fai_release_db { if(defined($entry) && ref($entry) eq 'HASH') { my $sql= "INSERT INTO $table_name " - ."(timestamp, fai_release, class, type, state) VALUES (" + ."(timestamp, release, class, type, state) VALUES (" .$timestamp."," ."'".$entry->{'release'}."'," ."'".$entry->{'class'}."'," @@ -2381,6 +2371,7 @@ sub create_fai_release_db { 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_release_db->exec_statementlist(\@sql_list); } @@ -2388,7 +2379,7 @@ sub create_fai_release_db { } daemon_log("$session_id INFO: create_fai_release_db: finished",5); } - $ldap_handle->disconnect; + $ldap_handle->disconnect; return $result; } @@ -2771,6 +2762,7 @@ sub create_packages_list_db { if(keys(%repo_dirs)) { 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); @@ -3003,22 +2995,24 @@ sub store_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" ) { daemon_log("DEBUG: Found debconf templates in '$package' - $newver", 7); - my $tmpl= ""; { + my $tmpl= ""; + { local $/=undef; open FILE, "$dir/DEBIAN/templates"; $tmpl = &encode_base64(); @@ -3027,11 +3021,11 @@ sub cleanup_and_extract { rmtree("$dir/DEBIAN/templates"); $sql= "update $main::packages_list_tn set template = '$tmpl' where package = '$package' and version = '$newver';"; - push @packages_list_statements, $sql; + push @packages_list_statements, $sql; } - } + } - return; + return; } @@ -3159,54 +3153,54 @@ daemon_log($server_status_hash->{$server_status}.": $server_revision", 1); # connect to incoming_db unlink($incoming_file_name); -$incoming_db = GOSA::DBmysql->new($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 unlink($job_queue_file_name); ## just for debugging -$job_db = GOSA::DBmysql->new($job_queue_file_name); +$job_db = GOSA::DBsqlite->new($job_queue_file_name); $job_db->create_table($job_queue_tn, \@job_queue_col_names); chmod(0660, $job_queue_file_name); chown($root_uid, $adm_gid, $job_queue_file_name); # connect to known_clients_db unlink($known_clients_file_name); ## just for debugging -$known_clients_db = GOSA::DBmysql->new($known_clients_file_name); +$known_clients_db = GOSA::DBsqlite->new($known_clients_file_name); $known_clients_db->create_table($known_clients_tn, \@known_clients_col_names); chmod(0660, $known_clients_file_name); chown($root_uid, $adm_gid, $known_clients_file_name); # connect to foreign_clients_db unlink($foreign_clients_file_name); -$foreign_clients_db = GOSA::DBmysql->new($foreign_clients_file_name); +$foreign_clients_db = GOSA::DBsqlite->new($foreign_clients_file_name); $foreign_clients_db->create_table($foreign_clients_tn, \@foreign_clients_col_names); chmod(0660, $foreign_clients_file_name); chown($root_uid, $adm_gid, $foreign_clients_file_name); # connect to known_server_db unlink($known_server_file_name); -$known_server_db = GOSA::DBmysql->new($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); chmod(0660, $known_server_file_name); chown($root_uid, $adm_gid, $known_server_file_name); # connect to login_usr_db unlink($login_users_file_name); -$login_users_db = GOSA::DBmysql->new($login_users_file_name); +$login_users_db = GOSA::DBsqlite->new($login_users_file_name); $login_users_db->create_table($login_users_tn, \@login_users_col_names); chmod(0660, $login_users_file_name); chown($root_uid, $adm_gid, $login_users_file_name); # connect to fai_server_db unlink($fai_server_file_name); -$fai_server_db = GOSA::DBmysql->new($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); chmod(0660, $fai_server_file_name); chown($root_uid, $adm_gid, $fai_server_file_name); # connect to fai_release_db unlink($fai_release_file_name); -$fai_release_db = GOSA::DBmysql->new($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); chmod(0660, $fai_release_file_name); chown($root_uid, $adm_gid, $fai_release_file_name); @@ -3214,14 +3208,14 @@ chown($root_uid, $adm_gid, $fai_release_file_name); # connect to packages_list_db #unlink($packages_list_file_name); unlink($packages_list_under_construction); -$packages_list_db = GOSA::DBmysql->new($packages_list_file_name); +$packages_list_db = GOSA::DBsqlite->new($packages_list_file_name); $packages_list_db->create_table($packages_list_tn, \@packages_list_col_names); chmod(0660, $packages_list_file_name); chown($root_uid, $adm_gid, $packages_list_file_name); # connect to messaging_db unlink($messaging_file_name); -$messaging_db = GOSA::DBmysql->new($messaging_file_name); +$messaging_db = GOSA::DBsqlite->new($messaging_file_name); $messaging_db->create_table($messaging_tn, \@messaging_col_names); chmod(0660, $messaging_file_name); chown($root_uid, $adm_gid, $messaging_file_name); @@ -3320,22 +3314,22 @@ if ($opsi_enabled eq "true") { POE::Component::Server::TCP->new( - Alias => "TCP_SERVER", + Alias => "TCP_SERVER", Port => $server_port, ClientInput => sub { - my ($kernel, $input) = @_[KERNEL, ARG0]; - push(@tasks, $input); - 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, - task_debug => \&handle_task_debug, - child_reap => sub { "Do nothing special. I'm just a comment, but i'm necessary!" }, - } + my ($kernel, $input) = @_[KERNEL, ARG0]; + push(@tasks, $input); + 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, + task_debug => \&handle_task_debug, + child_reap => sub { "Do nothing special. I'm just a comment, but i'm necessary!" }, + } ); daemon_log("start socket for incoming xml messages at port '$server_port' ", 1); diff --git a/gosa-si/modules/DBmysql.pm b/gosa-si/modules/DBsqlite.pm similarity index 58% rename from gosa-si/modules/DBmysql.pm rename to gosa-si/modules/DBsqlite.pm index 6d5648d9e..a571a7bb6 100644 --- a/gosa-si/modules/DBmysql.pm +++ b/gosa-si/modules/DBsqlite.pm @@ -1,4 +1,5 @@ -package GOSA::DBmysql; +package GOSA::DBsqlite; + use strict; use warnings; @@ -7,42 +8,58 @@ use Data::Dumper; use GOSA::GosaSupportDaemon; use Time::HiRes qw(usleep); + my $col_names = {}; sub new { my $class = shift; + my $db_name = shift; - my $self = {dbh=>undef}; - my $dbh = DBI->connect("dbi:mysql:database=$main::mysql_database;host=$main::mysql_host", $main::mysql_username, $main::mysql_password,{ RaiseError => 1, AutoCommit => 1 }); - $dbh->{mysql_auto_reconnect} = 1; + my $lock = $db_name.".si.lock"; + # delete existing lock - instance should be running only once + if(stat($lock)) { + &main::daemon_log("DEBUG: Removed existing lock $lock.", 7); + unlink($lock); + } + my $self = {dbh=>undef,db_name=>undef,db_lock=>undef,db_lock_handle=>undef}; + my $dbh = DBI->connect("dbi:SQLite:dbname=$db_name", "", "", {RaiseError => 1, AutoCommit => 1}); $self->{dbh} = $dbh; - bless($self,$class); + $self->{db_name} = $db_name; + $self->{db_lock} = $lock; + bless($self,$class); return($self); } sub create_table { - my $self = shift; - my $table_name = shift; - my $col_names_ref = shift; - my $recreate_table = shift || 0; - my @col_names; - my $col_names_string = join(", ", @$col_names_ref); - - if($recreate_table) { - $self->{dbh}->do("DROP TABLE $table_name"); + my $self = shift; + my $table_name = shift; + my $col_names_ref = shift; + my @col_names; + foreach my $col_name (@$col_names_ref) { + my @t = split(" ", $col_name); + $col_name = $t[0]; + push(@col_names, $col_name); + } + + $col_names->{ $table_name } = $col_names_ref; + my $col_names_string = join("', '", @col_names); + my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( '$col_names_string' )"; + eval { + my $res = $self->{dbh}->do($sql_statement); + }; + if($@) { + $self->{dbh}->do("ANALYZE"); } - my $sql_statement = "CREATE TABLE IF NOT EXISTS $table_name ( $col_names_string )"; - # &main::daemon_log("DEBUG: $sql_statement"); eval { - $self->{dbh}->do($sql_statement); + my $res = $self->{dbh}->do($sql_statement); }; if($@) { &main::daemon_log("ERROR: $sql_statement failed with $@", 1); } - return 0; + return 0; } @@ -78,14 +95,16 @@ sub add_dbentry { # check wether primkey is unique in table, otherwise return errorflag my $sql_statement = "SELECT * FROM $table $prim_statement"; eval { - # &main::daemon_log("DEBUG: $sql_statement"); - my $sth = $self->{dbh}->prepare($sql_statement); - $sth->execute; - $res = @{ $sth->fetchall_arrayref() }; - $sth->finish; + $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) }; }; if($@) { - &main::daemon_log("ERROR: $sql_statement failed with $@", 1); + $self->{dbh}->do("ANALYZE"); + eval { + $res = @{ $self->{dbh}->selectall_arrayref($sql_statement) }; + }; + if($@) { + &main::daemon_log("ERROR: $sql_statement failed with $@", 1); + } } } @@ -95,13 +114,13 @@ sub add_dbentry { # fetch column names of table my $col_names = &get_table_columns($self, $table); - #my $create_id=0; - #foreach my $col_name (@{$col_names}) { - # #if($col_name eq "id" && (! exists $arg->{$col_name})) { - # #&main::daemon_log("0 DEBUG: id field found without value! Creating autoincrement statement!", 7); - # $create_id=1; - # } - #} + my $create_id=0; + foreach my $col_name (@{$col_names}) { + if($col_name eq "id" && (! exists $arg->{$col_name})) { + #&main::daemon_log("0 DEBUG: id field found without value! Creating autoincrement statement!", 7); + $create_id=1; + } + } # assign values to column name variables my @col_list; @@ -109,24 +128,29 @@ sub add_dbentry { foreach my $col_name (@{$col_names}) { # use function parameter for column values if (exists $arg->{$col_name}) { - push(@col_list, $col_name); + push(@col_list, "'".$col_name."'"); push(@val_list, "'".$arg->{$col_name}."'"); } } my $sql_statement; - #if($create_id==1) { - # $sql_statement = "INSERT INTO $table (id, ".join(", ", @col_list).") VALUES ((select coalesce(max(id),0)+1), ".join(", ", @val_list).")"; - #} else { + if($create_id==1) { + $sql_statement = "INSERT INTO $table ('id', ".join(", ", @col_list).") VALUES ((select coalesce(max(id), 0)+1 from $table), ".join(", ", @val_list).")"; + } else { $sql_statement = "INSERT INTO $table (".join(", ", @col_list).") VALUES (".join(", ", @val_list).")"; - #} + } my $db_res; - # &main::daemon_log("DEBUG: $sql_statement",1); eval { $db_res = $self->{dbh}->do($sql_statement); }; if($@) { - &main::daemon_log("ERROR: $sql_statement failed with $@", 1); + $self->{dbh}->do("ANALYZE"); + eval { + $db_res = $self->{dbh}->do($sql_statement); + }; + if($@) { + &main::daemon_log("ERROR: $sql_statement failed with $@", 1); + } } if( $db_res != 1 ) { @@ -160,7 +184,7 @@ sub update_dbentry { sub del_dbentry { - my ($self, $sql)= @_; + my ($self, $sql)= @_;; my $db_res= &exec_statement($self, $sql); return $db_res; } @@ -169,24 +193,31 @@ sub del_dbentry { sub get_table_columns { my $self = shift; my $table = shift; - my @column_names; - - my @res; - eval { - my $sth = $self->{dbh}->prepare("describe $table") or &main::daemon_log("ERROR: Preparation of statement 'describe $table' failed!", 1); - $sth->execute or &main::daemon_log("ERROR: Execution of statement 'describe $table' failed!", 1); - @res = @{ $sth->fetchall_arrayref() }; - $sth->finish or &main::daemon_log("ERROR: Finishing the statement handle failed!", 1); - }; - if($@) { - &main::daemon_log("ERROR: describe ('$table') failed with $@", 1); - } + my @column_names; + + if(exists $col_names->{$table}) { + @column_names = @{$col_names->{$table}}; + } else { + my @res; + eval { + @res = @{$self->{dbh}->selectall_arrayref("pragma table_info('$table')")}; + }; + if($@) { + $self->{dbh}->do("ANALYZE"); + eval { + @res = @{$self->{dbh}->selectall_arrayref("pragma table_info('$table')")}; + }; + if($@) { + &main::daemon_log("ERROR: pragma table_info('$table') failed with $@", 1); + } + } - foreach my $column (@res) { - push(@column_names, @$column[0]); + foreach my $column (@res) { + push(@column_names, @$column[1]); + } } + return \@column_names; - return \@column_names; } @@ -243,35 +274,29 @@ sub show_table { sub exec_statement { - my $self = shift; - my $sql_statement = shift; - my $sth; - my @db_answer; + my $self = shift; + my $sql_statement = shift; + my @db_answer; - # print STDERR Dumper($sql_statement); eval { - if($sql_statement =~ /^SELECT/i) { - $sth = $self->{dbh}->prepare($sql_statement) or &main::daemon_log("ERROR: Preparation of statement '$sql_statement' failed!", 1); - $sth->execute or &main::daemon_log("ERROR: Execution of statement '$sql_statement' failed!", 1); - if($sth->rows > 0) { - @db_answer = @{ $sth->fetchall_arrayref() } or &main::daemon_log("ERROR: Fetch() failed!", 1); - # print STDERR Dumper(@db_answer); - } - $sth->finish or &main::daemon_log("ERROR: Finishing the statement handle failed!", 1); - } else { - $self->{dbh}->do($sql_statement); - } + @db_answer = @{$self->{dbh}->selectall_arrayref($sql_statement)}; }; if($@) { - &main::daemon_log("ERROR: $sql_statement failed with '$@'", 1); + $self->{dbh}->do("ANALYZE"); + eval { + @db_answer = @{$self->{dbh}->selectall_arrayref($sql_statement)}; + }; + if($@) { + &main::daemon_log("ERROR: $sql_statement failed with $@", 1); + } } # TODO : maybe an error handling and an erro feedback to invoking function - my $error = $self->{dbh}->err; - if ($error) { - &main::daemon_log("ERROR: ".@$self->{dbh}->errstr, 1); - } + #my $error = @$self->{dbh}->err; + #if ($error) { + # my $error_string = @$self->{dbh}->errstr; + #} - return \@db_answer; + return \@db_answer; } @@ -282,22 +307,19 @@ sub exec_statementlist { foreach my $sql (@$sql_list) { if(defined($sql) && length($sql) > 0) { - # &main::daemon_log("DEBUG: $sql"); eval { - if($sql =~ /^SELECT/i) { - my $sth = $self->{dbh}->prepare($sql); - # &main::daemon_log("DEBUG: ".$sth->execute); - if($sth->rows > 0) { - my @answer = @{$sth->fetchall_arrayref()}; - push @db_answer, @answer; - } - $sth->finish; - } else { - $self->{dbh}->do($sql); - } + my @answer = @{$self->{dbh}->selectall_arrayref($sql)}; + push @db_answer, @answer; }; if($@) { - &main::daemon_log("ERROR: $sql failed with $@", 1); + $self->{dbh}->do("ANALYZE"); + eval { + my @answer = @{$self->{dbh}->selectall_arrayref($sql)}; + push @db_answer, @answer; + }; + if($@) { + &main::daemon_log("ERROR: $sql failed with $@", 1); + } } } else { next; @@ -329,11 +351,28 @@ sub move_table { eval { $self->{dbh}->do($sql_statement_drop); - $self->{dbh}->do($sql_statement_alter); }; + if($@) { + $self->{dbh}->do("ANALYZE"); + eval { + $self->{dbh}->do($sql_statement_drop); + }; + if($@) { + &main::daemon_log("ERROR: $sql_statement_drop failed with $@", 1); + } + } + eval { + $self->{dbh}->do($sql_statement_alter); + }; if($@) { - &main::daemon_log("ERROR: $sql_statement_drop failed with $@", 1); + $self->{dbh}->do("ANALYZE"); + eval { + $self->{dbh}->do($sql_statement_alter); + }; + if($@) { + &main::daemon_log("ERROR: $sql_statement_alter failed with $@", 1); + } } return; diff --git a/gosa-si/modules/GosaPackages.pm b/gosa-si/modules/GosaPackages.pm index 17a731bab..a2cf9f040 100644 --- a/gosa-si/modules/GosaPackages.pm +++ b/gosa-si/modules/GosaPackages.pm @@ -11,7 +11,7 @@ use Socket; use XML::Simple; use File::Spec; use Data::Dumper; -use GOSA::DBmysql; +use GOSA::DBsqlite; use MIME::Base64; my $event_dir = "/usr/lib/gosa-si/server/GosaPackages"; diff --git a/gosa-si/server/events/gosaTriggered.pm b/gosa-si/server/events/gosaTriggered.pm index e91aa4639..d49a4a381 100644 --- a/gosa-si/server/events/gosaTriggered.pm +++ b/gosa-si/server/events/gosaTriggered.pm @@ -109,7 +109,7 @@ sub send_user_msg { # determine new message id my $new_msg_id = 1; - my $new_msg_id_sql = "SELECT MAX(id) FROM $main::messaging_tn"; + my $new_msg_id_sql = "SELECT MAX(CAST(id AS INTEGER)) FROM $main::messaging_tn"; my $new_msg_id_res = $main::messaging_db->exec_statement($new_msg_id_sql); if (defined @{@{$new_msg_id_res}[0]}[0] ) { $new_msg_id = int(@{@{$new_msg_id_res}[0]}[0]); @@ -755,31 +755,31 @@ sub trigger_action_wake { sub get_available_kernel { - my ($msg, $msg_hash, $session_id) = @_; - - my $source = @{$msg_hash->{'source'}}[0]; - my $target = @{$msg_hash->{'target'}}[0]; - my $fai_release= @{$msg_hash->{'fai_release'}}[0]; - - my @kernel; - # Get Kernel packages for release - my $sql_statement = "SELECT * FROM $main::packages_list_tn WHERE distribution='$fai_release' AND package LIKE 'linux\-image\-%'"; - my $res_hash = $main::packages_list_db->select_dbentry($sql_statement); - my %data; - my $i=1; - - foreach my $package (keys %{$res_hash}) { - $data{"answer".$i++}= $data{"answer".$i++}= ${$res_hash}{$package}->{'package'}; - } - $data{"answer".$i++}= "default"; - - my $out_msg = &build_msg("get_available_kernel", $target, $source, \%data); - my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; - if (defined $forward_to_gosa) { - $out_msg =~s/<\/xml>/$forward_to_gosa<\/forward_to_gosa><\/xml>/; - } - - return ( $out_msg ); + my ($msg, $msg_hash, $session_id) = @_; + + my $source = @{$msg_hash->{'source'}}[0]; + my $target = @{$msg_hash->{'target'}}[0]; + my $release= @{$msg_hash->{'release'}}[0]; + + my @kernel; + # Get Kernel packages for release + my $sql_statement = "SELECT * FROM $main::packages_list_tn WHERE distribution='$release' AND package LIKE 'linux\-image\-%'"; + my $res_hash = $main::packages_list_db->select_dbentry($sql_statement); + my %data; + my $i=1; + + foreach my $package (keys %{$res_hash}) { + $data{"answer".$i++}= $data{"answer".$i++}= ${$res_hash}{$package}->{'package'}; + } + $data{"answer".$i++}= "default"; + + my $out_msg = &build_msg("get_available_kernel", $target, $source, \%data); + my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0]; + if (defined $forward_to_gosa) { + $out_msg =~s/<\/xml>/$forward_to_gosa<\/forward_to_gosa><\/xml>/; + } + + return ( $out_msg ); } -- 2.30.2