diff --git a/gosa-si/gosa-si-client b/gosa-si/gosa-si-client
index 1b6fa0cebb2a88436e113b79477f58a8bc6bd592..d1d62dff398c670b322f2f8c859db2338eb917a7 100755 (executable)
--- a/gosa-si/gosa-si-client
+++ b/gosa-si/gosa-si-client
#!/usr/bin/perl
-#===============================================================================
-#
-# FILE: gosa-server
+#*********************************************************************
#
-# USAGE: gosa-si-client
+# gosa-si-client -- client for the gosa-si-server
#
-# DESCRIPTION:
+# (c) 2007-2009 by Andreas Rettenberger <rettenberger@gonicus.de>
+# (c) 2008-2010 by Cajus Pollmeier <pollmeier@gonicus.de>
+# (c) 2008-2009 by Jan Wenzel <wenzel@gonicus.de>
+# (c) 2010 by Benoit Mortier <benoit.mortier@opensides.be>
#
-# OPTIONS: ---
-# REQUIREMENTS: libnetaddr-ip-perl
-# BUGS: ---
-# NOTES:
-# AUTHOR: (Andreas Rettenberger), <rettenberger@gonicus.de>
-# COMPANY:
-# VERSION: 1.0
-# CREATED: 12.09.2007 08:54:41 CEST
-# REVISION: ---
-#===============================================================================
+#*********************************************************************
+
+=head1 NAME
+
+gosa-si-client - Client for GOsa support infrastructure
+
+=head1 SYNOPSIS
+
+gosa-si-client [-hvf] [-c config]
+
+=head1 OPTIONS
+
+B<-h>, B<--help>
+ print out this help message
+
+B<-v>, B<--verbose>
+ be verbose (multiple v's will increase verbosity)
+ -v ERROR level
+ -vvv WARNING + ERROR level
+ -vvvvv INFO + WARNING level
+ -vvvvvvv DEBUG + INFO level
+ -vvvvvvvvv in and out going xml messages will be displayed
+
+B<-f>, B<--foreground>
+ foregroud, process will not be forked to background
+
+B<-c> I<file>, B<--config=>I<file>
+ configuration file, default F</etc/gosa-si/client.conf>
+
+B<-x> <dump>
+ dump configuration to stdout
+ ( 1 = current, 2 = default )
+
+=head1 DESCRIPTION
+
+B<gosa-si-client> belongs to the support infrastructure of GOsa.
+The client registrates at a GOsa-SI-server and accepts messages from it.
+Each message is related to a working instruction which will be executed on the client.
+Depending on the message an answer can be send back to the server.
+
+=head1 BUGS
+
+Please report any bugs, or post any suggestions, to the GOsa mailing list <gosa-devel@oss.gonicus.de> or to <https://oss.gonicus.de/labs/gosa>
+
+
+=head1 LICENCE AND COPYRIGHT
+
+This code is part of GOsa (L<http://www.gosa-project.org>)
+
+Copyright (C) 2003-2010 GONICUS GmbH
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+=cut
use strict;
use warnings;
+
use Getopt::Long;
use Config::IniFiles;
-use POSIX;
use Time::HiRes qw( gettimeofday );
-
-use POE qw(Component::Server::TCP Wheel::FollowTail Wheel::Run);
use IO::Socket::INET;
use NetAddr::IP;
-use Data::Dumper;
use Crypt::Rijndael;
-use GOSA::GosaSupportDaemon;
+use GOsaSI::GosaSupportDaemon;
use Digest::MD5 qw(md5_hex md5 md5_base64);
use MIME::Base64;
use XML::Simple;
use File::Basename;
use File::Spec;
+use File::Pid;
+use Net::ARP;
+
+use POE qw(Component::Server::TCP Wheel::FollowTail Wheel::Run);
+use Fcntl;
+use POSIX;
# Workaround: need pure perl to make it work with UTF-8 :-(
$XML::Simple::PREFERRED_PARSER= "XML::SAX::PurePerl";
my $client_version = '$HeadURL$:$Rev$';
+
my $client_headURL;
my $client_revision;
my $client_status;
my (%cfg_defaults, $foreground, $verbose, $pid_file, $procid, $pid, $log_file, $fai_logpath);
my ($server_ip, $server_port, $server_timeout, $server_domain, $server_key_lifetime);
my ($client_port, $ldap_enabled, $ldap_config, $pam_config, $nss_config);
+my ($root_uid, $adm_gid);
my $xml;
my $event_hash;
my $default_server_key;
my @servers;
my $gotoHardwareChecksum;
-my $gosa_si_client_fifo;
-my %files_to_watch;
+my $system_com;
+my $servers_string;
$verbose= 1;
# globalise variables which are used in imported events
our $client_dnsname;
our $client_force_hostname;
our $server_key;
+our $terminal_server_hash;
+our $opts_dnslookup;
+
+# where is the config stored by default and his name
+our $config = '/etc/gosa-si/client.conf';
+
+# by default dumping of config is undefined
+our $dump_config = undef;
# default variables
-our $REGISTERED = 0;
+my $REGISTERED = 0;
# path to fifo for non-gosa-si-client messages to gosa-si-server
-$gosa_si_client_fifo = "/var/run/gosa-si-client.socket";
-%files_to_watch = (fifo => $gosa_si_client_fifo);
+my $fai_com_fifo = "/var/run/gosa-si/gosa-si-client.socket";
+my $system_com_fifo = "/var/run/gosa-si/gosa-si-client-system-com.socket";
+my %files_to_watch = (fai_fifo => $fai_com_fifo, system_fifo => $system_com_fifo);
-# in function register_at_gosa_si_server, after which period of seconds a new registration should be tried if a registration was
-# not successful until now
+# in function register_at_gosa_si_server, after which period of seconds a new registration should be tried if a registration was not successful until now
my $delay_set_time = 10;
-our $prg= basename($0);
+#our $prg= basename($0);
-# all x seconds the client reports logged_in users to gosa-si-server
+# all n seconds the client reports logged_in users to gosa-si-server
my $trigger_logged_in_users_report_delay = 600;
+# all n seconds the client reports messages seen by user
+my $trigger_seen_messages_delay = 30;
+
# directory where all log files from installation are stored
my $fai_log_dir = "/var/log/fai";
%cfg_defaults = (
-"general" =>
- {"log-file" => [\$log_file, "/var/run/".$prg.".log"],
- "pid-file" => [\$pid_file, "/var/run/".$prg.".pid"],
- "opts-file" => [\$opts_file, "/var/run/".$prg.".opts"],
+"General" =>
+ {"log-file" => [\$log_file, "/var/log/gosa-si/gosa-si.log"],
+ "pid-file" => [\$pid_file, "/var/run/gosa-si/gosa-si-client.pid"],
+ "opts-file" => [\$opts_file, "/var/run/gosa-si/gosa-si-client.opts"],
},
-"client" =>
+"Client" =>
{"port" => [\$client_port, "20083"],
"ip" => [\$client_ip, "0.0.0.0"],
"mac-address" => [\$client_mac_address, "00:00:00:00:00:00"],
"nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
"fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
"force-hostname" => [\$client_force_hostname, "false"],
+ "system-com" => [\$system_com, "disabled"],
},
-"server" => {
- "ip" => [\$server_ip, "127.0.0.1"],
+"Server" => {
+ "ip" => [\$servers_string, "127.0.0.1"],
"port" => [\$server_port, "20081"],
"key" => [\$server_key, ""],
"timeout" => [\$server_timeout, 10],
- "key-lifetime" => [\$server_key_lifetime, 600],
+ "key-lifetime" => [\$server_key_lifetime, 600],
+ "dns-lookup" => [\$opts_dnslookup, "true"],
},
);
#=== FUNCTIONS = functions =====================================================
-sub usage {
- print STDERR << "EOF" ;
-usage: $prg [-hvf] [-c config]
-
- -h : this (help) message
- -c <file> : config file
- -f : foreground, process will not be forked to background
- -v : be verbose (multiple to increase verbosity)
-
+#############################
+#
+# @brief Display error message and/or help text.
+#
+# In correspondence to previous GetOptions
+#
+# @param $text - string to print as error message
+# @param $help - set true, if you want to show usage help
+#
+sub usage
+{
+ my( $text, $help ) = @_;
+
+ $text = undef if( 'h' eq $text );
+ (defined $text) && print STDERR "\n$text\n";
+
+ if( (defined $help && $help)
+ || (!defined $help && !defined $text) )
+{
+ print STDERR << "EOF";
+
+ usage: $0 [-hvf] [-c config]
+
+ -h : this (help) message
+ -c <file> : config file (default: ${config})
+ -x <cfg> : dump configuration to stdout
+ ( 1 = current, 2 = default )
+ -f : foreground (don't fork)
+ -v : be verbose (multiple to increase verbosity)
EOF
- print "\n" ;
}
+ print( "\n" );
+
+ exit( -1 );
+}
+
+#############################
+#
+# @brief Manage gosa-si-client configuration.
+#
+# Will exit after successfull dump to stdout (type = 1 | 2)
+#
+# Dump type can be:
+# 1: Current gosa-si-client configuration in config file (exit)
+# 2: Default gosa-si-client configuration (exit)
+# 3: Dump to logfile (no exit)
+#
+# @param int config type
+#
+sub dump_configuration {
+
+ my( $cfg_type ) = @_;
+
+ return if( ! defined $cfg_type );
+
+ if(1==$cfg_type ) {
+ print( "# Current gosa-si-client configuration\n" );
+ } elsif (2==$cfg_type) {
+ print( "# Default gosa-si-client configuration\n" );
+ } elsif (3==$cfg_type) {
+ daemon_log( "Dumping gosa-si-client configuration\n", 2 );
+ } else {
+ return;
+ }
+
+ foreach my $section (keys %cfg_defaults) {
+ if( 3 != $cfg_type ) {
+ print( "\n[${section}]\n" );
+ } else {
+ daemon_log( "\n [${section}]\n", 3 );
+ }
+
+ foreach my $param (sort( keys %{$cfg_defaults{ $section }})) {
+ my $pinfo = $cfg_defaults{ $section }{ $param };
+ my $value;
+ if (1==$cfg_type) {
+ if( defined( ${@$pinfo[ 0 ]} ) ) {
+ $value = ${@$pinfo[ 0 ]};
+ print( "$param=$value\n" );
+ } else {
+ print( "#${param}=\n" );
+ }
+ } elsif (2==$cfg_type) {
+ $value = @{$pinfo}[ 1 ];
+ if( defined( @$pinfo[ 1 ] ) ) {
+ $value = @{$pinfo}[ 1 ];
+ print( "$param=$value\n" );
+ } else {
+ print( "#${param}=\n" );
+ }
+ } elsif (3==$cfg_type) {
+ if( defined( ${@$pinfo[ 0 ]} ) ) {
+ $value = ${@$pinfo[ 0 ]};
+ daemon_log( " $param=$value\n", 3 )
+ }
+ }
+ }
+ }
+
+
+# We just exit at stdout dump
+ if( 3 == $cfg_type ) {
+ daemon_log( "\n", 3 );
+ } else {
+ exit( 0 );
+ }
+}
+
#=== FUNCTION ================================================================
# NAME: check_cmdline_param
sub check_pid {
$pid = -1;
# Check, if we are already running
- if( open(LOCK_FILE, "<$pid_file") ) {
- $pid = <LOCK_FILE>;
+ if( open(my $LOCK_FILE, "<","$pid_file") ) {
+ $pid = <$LOCK_FILE>;
if( defined $pid ) {
chomp( $pid );
if( -f "/proc/$pid/stat" ) {
my($stat) = `cat /proc/$pid/stat` =~ m/$pid \((.+)\).*/;
if( $0 eq $stat ) {
- close( LOCK_FILE );
+ close($LOCK_FILE);
exit -1;
}
}
}
- close( LOCK_FILE );
+ close( $LOCK_FILE );
unlink( $pid_file );
}
+ my $LOCK_FILE2;
# create a syslog msg if it is not to possible to open PID file
if (not sysopen(LOCK_FILE, $pid_file, O_WRONLY|O_CREAT|O_EXCL, 0644)) {
- my($msg) = "Couldn't obtain lockfile '$pid_file' ";
- if (open(LOCK_FILE, '<', $pid_file)
- && ($pid = <LOCK_FILE>))
+ my($msg) = "Couldn't obtain lockfile '$pid_file' ";
+ if (open($LOCK_FILE2, '<', "$pid_file")
+ && ($pid = <$LOCK_FILE2>))
{
chomp($pid);
$msg .= "(PID $pid)\n";
}
}
-
+#############################
+#
+# @brief When caching an interrupt remove file pid and stopping gosa-si
+#
sub sig_int_handler {
- my ($signal) = @_;
+ my ($signal) = @_;
+
+ daemon_log("shutting down gosa-si-client", 1);
+
+# to be changed crude !!
+# system("kill `ps -C gosa-si-server -o pid=`");
+ $global_kernel->yield('shutdown');
- daemon_log("shutting down gosa-si-client", 1);
- system("kill `ps -C gosa-si-client -o pid=`");
+ $pid->remove or warn "Could not remove $pid_file\n";
+
+ exit(0);
}
$SIG{INT} = \&sig_int_handler;
-
#=== FUNCTION ================================================================
# NAME: logging
# PARAMETERS: level - string - default 'info'
if(not defined $msg) { return }
if(not defined $level) { $level = 1 }
if(defined $log_file){
- open(LOG_HANDLE, ">>$log_file");
- chmod 0600, $log_file;
- if(not defined open( LOG_HANDLE, ">>$log_file" )) {
- print STDERR "cannot open $log_file: $!";
- return
- }
+ my $open_log_fh = sysopen(LOG_HANDLE, $log_file, O_WRONLY | O_CREAT | O_APPEND , 0440);
+ if(not $open_log_fh) {
+ print STDERR "cannot open $log_file: $!";
+ return;
+ }
+ # check owner and group of log_file and update settings if necessary
+ my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($log_file);
+ if((not $uid eq $root_uid) || (not $gid eq $adm_gid)) {
+ chown($root_uid, $adm_gid, $log_file);
+ }
+
chomp($msg);
if($level <= $verbose){
my ($seconds, $minutes, $hours, $monthday, $month,
$monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
$year+=1900;
- my $log_msg = "$month $monthday $hours:$minutes:$seconds $prg $msg\n";
+ # Microseconds since epoch
+ my $microSeconds = sprintf("%.2f", &Time::HiRes::clock_gettime());
+ $microSeconds =~ s/^\d*(.\d\d)$/$1/;
+
+
+ my $log_msg = "$month $monthday $hours:$minutes:$seconds$microSeconds gosa-si-client $msg\n";
print LOG_HANDLE $log_msg;
if( $foreground ) {
print STDERR $log_msg;
$result = "00:00:00:00:00:00";
}
} else {
- my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
-
# A configured MAC Address should always override a guessed value
if ($client_mac_address and length($client_mac_address) > 0 and not($client_mac_address eq "00:00:00:00:00:00")) {
$result= $client_mac_address;
}
else {
- socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
- or die "socket: $!";
-
- if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
- my ($if, $mac)= unpack 'h36 H12', $ifreq;
-
- if (length($mac) > 0) {
- $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
- $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
- $result = $mac;
- }
- }
+ $result = Net::ARP::get_mac($ifreq);
}
}
}
my $server_ip= shift;
my $result= "00:00:00:00:00:00";
+ if($server_ip =~ /^[a-z][a-z0-9\.]+/i) {
+ my $ip_address = inet_ntoa(scalar gethostbyname($server_ip));
+ if(defined($ip_address) && $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
+ # Write ip address to $server_ip variable
+ $server_ip = $ip_address;
+ }
+ }
+
if($server_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
my $PROC_NET_ROUTE= ('/proc/net/route');
- open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
+ open(my $PROC_NET, "<","$PROC_NET_ROUTE")
or die "Could not open $PROC_NET_ROUTE";
- my @ifs = <PROC_NET_ROUTE>;
+ my @ifs = <$PROC_NET>;
- close(PROC_NET_ROUTE);
+ close($PROC_NET);
# Eat header line
shift @ifs;
foreach my $line (split /\n/, `cat /proc/bus/pci/devices`) {
$hw_data.= sprintf "%s", $line =~ /[^\s]+\s([^\s]+)\s.*/;
}
+ # linux-vserver has no /proc/bus/pci/devices
+ if ( !$hw_data ) {
+ foreach my $line (split /\n/, `ifconfig | grep HWaddr`) {
+ $hw_data.= sprintf "%s", $line =~ /[^\s]+\s([^\s]+)\s.*/;
+ }
+ }
return(md5_base64($hw_data));
}
return $error;
}
+sub _setREGISTERED {
+ my $newStatus = shift;
+ &daemon_log("DEBUG: set REGISTERED flag from '$REGISTERED' to '$newStatus'", 7);
+ $REGISTERED = $newStatus;
+ return;
+}
sub send_msg_to_target {
my ($msg, $address, $encrypt_key, $msg_header) = @_ ;
if( $msg_header ) { $msg_header = "'$msg_header'-"; }
else { $msg_header = ""; }
+ # Memorize own source address
+ $msg =~ /<source>(\S+)<\/source>/;
+ my $own_source_address = $1;
+
# encrypt xml msg
my $crypted_msg = &encrypt_msg($msg, $encrypt_key);
# opensocket
my $socket = &open_socket($address);
if( !$socket ) {
- daemon_log("WARNING: cannot send ".$msg_header."msg to $address , host not reachable", 3);
+ daemon_log("WARNING: cannot send ".$msg_header."msg to '$address', host not reachable", 3);
if ($REGISTERED == 1) {
- $REGISTERED = 0; # if server is not available, cause reregistering
+ &_setREGISTERED(0); # if server is not available, cause reregistering
daemon_log("INFO: cause reregistering at gosa-si-server", 5);
- $global_kernel->yield('register_at_gosa_si_server');
+ $global_kernel->post('client_session', 'register_at_gosa_si_server');
}
$error++;
# send xml msg
if( $error == 0 ) {
- print $socket $crypted_msg."\n";
+ print $socket $crypted_msg.";$own_source_address\n";
daemon_log("INFO: send ".$msg_header."msg to $address", 5);
daemon_log("DEBUG: message:\n$msg", 9);
}
my $error = 0;
if( not defined $file || not -f $file ) {
- &main::daemon_log("ERROR: $prg: check '-f file' failed: $file", 1);
+ &main::daemon_log("ERROR: gosa-si-client : check '-f file' failed: $file", 1);
$error++;
}
if( not defined $string || 0 == length($string)) {
- &main::daemon_log("ERROR: $prg: empty string to write to file '$file'", 1);
+ &main::daemon_log("ERROR: gosa-si-client : empty string to write to file '$file'", 1);
$error++;
}
chomp($string);
- if (open(FILE, ">> $file")){
- print FILE $string."\n";
- close(FILE);
+ if (open(my $FILE, ">>", "$file")){
+ print $FILE $string."\n";
+ close($FILE);
}
}
- return;
+ return;
}
# DESCRIPTION:
#===============================================================================
sub register_at_gosa_si_server {
- my ($kernel) = $_[KERNEL];
- my $try_to_register = 0;
-
- # if client is already registered, stop registration process
- if ($REGISTERED) {
- $kernel->delay('register_at_gosa_si_server');
+ my ($kernel) = $_[KERNEL];
+ my $try_to_register = 0;
- # client is not registered, start registration process
+ if ($REGISTERED) {
+ # Client is already registered, clear all other registration events
+ $kernel->delay('register_at_gosa_si_server');
} else {
- # clear all other triggered events and wait till registration was successful
- $kernel->delay('trigger_new_key');
+ # Client is not registered, start registration process
+ # Clear all other events and wait till registration was successful
+ $kernel->delay('register_at_gosa_si_server');
+ $kernel->delay('trigger_new_key');
- # create new passwd and ciphering object for client-server communication
- $server_key = &create_passwd();
+ # Create new passwd and ciphering object for client-server communication
+ $server_key = &create_passwd();
- my $events = join( ",", keys %{$event_hash} );
- while(1) {
- $try_to_register++;
+ my $events = join( ",", keys %{$event_hash} );
+ while(1) {
+ $try_to_register++;
- # after one complete round through all server, stop trying to register
- if( $try_to_register > @servers ) { last; }
+ # after one complete round through all server, stop trying to register
+ if( $try_to_register > @servers ) { last; }
- # fetch first gosa-si-server from @servers
- # append shifted gosa-si-server at the end of @servers, so looking for servers never stop if
- # a registration never occured
- my $server = shift(@servers);
- push( @servers, $server );
+ # fetch first gosa-si-server from @servers
+ # append shifted gosa-si-server at the end of @servers, so looking for servers never stop if
+ # a registration never occured
+ my $server = shift(@servers);
+ push( @servers, $server );
- # Check if our ip is resolvable - if not: don't try to register
+ # Check if $server is a valid server address
if(!(defined($server) && $server =~ m/^[0-9\.]*?:.*$/)) {
&main::daemon_log("ERROR: Server with address '".defined($server)?$server:""."' is invalid!", 1);
if (length(@servers) == 1) {
}
}
- # Check if our ip is resolvable - if not: don't try to register
- my $ip= &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/));
- my $dnsname= gethostbyaddr(inet_aton($ip), AF_INET);
- if(!defined($dnsname)) {
- if( defined($client_force_hostname) && $client_force_hostname eq "true") {
- $dnsname = `hostname`;
- } else {
- &write_to_file("goto-error-dns:$ip", $fai_logpath);
- exit(1);
+ # Check if our ip is resolvable - if not: don't try to register
+ my $ip= &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/));
+ my $dnsname= gethostbyaddr(inet_aton($ip), AF_INET);
+ if(!defined($dnsname)) {
+ if( defined($client_force_hostname) && $client_force_hostname eq "true") {
+ $dnsname = `hostname`;
+ } else {
+ &write_to_file("goto-error-dns:$ip", $fai_logpath);
+ &main::daemon_log("ERROR: ip is not resolvable, no registration possible. Write 'goto-error-dns:$ip' to $fai_logpath", 1);
+ exit(1);
+ }
}
- }
- # create registration msg
+ # create registration msg
my $local_ip = &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/));
my $local_mac = &get_local_mac_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/));
my $register_hash = &create_xml_hash("here_i_am", $local_ip.":".$client_port, $server);
&add_content2xml_hash($register_hash, "new_passwd", $server_key);
&add_content2xml_hash($register_hash, "mac_address", $local_mac);
&add_content2xml_hash($register_hash, "events", $events);
- &add_content2xml_hash($register_hash, "client_status", $client_status);
- &add_content2xml_hash($register_hash, "client_revision", $client_revision);
+ &add_content2xml_hash($register_hash, "client_status", $client_status);
+ &add_content2xml_hash($register_hash, "client_revision", $client_revision);
&add_content2xml_hash($register_hash, "gotoHardwareChecksum", $gotoHardwareChecksum);
- &add_content2xml_hash($register_hash, "key_lifetime", $server_key_lifetime);
+ &add_content2xml_hash($register_hash, "key_lifetime", $server_key_lifetime);
# Add $HOSTNAME from ENV if force-hostname is set
if( defined($client_force_hostname) && $client_force_hostname eq "true") {
}
}
- # send xml hash to server with general server passwd
+ # send xml hash to server with general server passwd
my $res = &send_msg_hash_to_target($register_hash, $server, $default_server_key);
- # if delivery of registration msg succeed
+ # if delivery of registration msg succeed
if($res eq "0") {
- # reset try_to_register
+ # reset try_to_register
$try_to_register = 0;
- # Set fixed client address and mac address
+ # Set fixed client address and mac address
$client_ip= &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/));
$client_address= "$client_ip:$client_port";
$client_mac_address = $local_mac;
$client_dnsname = $dnsname;
- last;
+ last;
- # delivery of registration msg failed
+ # delivery of registration msg failed
} else {
- # wait 1 sec until trying to register again
+ # wait 1 sec until trying to register again
sleep(1);
next;
}
- } # end of while
- # one circle through all servers finished and no registration succeed
- if ( $try_to_register >= @servers ) {
+ } # end of while
+
+ if ( $try_to_register >= @servers ) {
+ # One circle through all servers finished and no registration succeed
&write_to_file("gosa-si-no-server-available", $fai_logpath);
- $kernel->delay_set('register_at_gosa_si_server', $delay_set_time);
-
- # delivery of registraion msg succeed, waiting for server response
- } else {
+ } else {
+ # Delivery of registraion msg succeed, waiting for server response
daemon_log("INFO: waiting for msg 'register_at_gosa_si_server'",5);
- $kernel->delay_set('register_at_gosa_si_server', $delay_set_time);
- # clear old settings and set it again
+ # Clear old settings and set it again
$kernel->delay('trigger_new_key');
$kernel->delay_set('trigger_new_key', $server_key_lifetime);
- }
+ }
- }
- return;
+ # Set waiting time for 'register_at_gosa_si_server'-event
+ $kernel->delay_set('register_at_gosa_si_server', $delay_set_time);
+ }
+ return;
}
@logged_in_user_list = split(/\s/, $result);
}
- system("echo 'CURRENTLY_LOGGED_IN ".join(" ", @logged_in_user_list)."' > /var/run/gosa-si-client.socket");
+ system("echo 'CURRENTLY_LOGGED_IN ".join(" ", @logged_in_user_list)."' > /var/run/gosa-si/gosa-si-client.socket");
$kernel->delay_set('trigger_logged_in_users_report', $trigger_logged_in_users_report_delay);
} else {
# try it in 10 sec again
}
}
+sub trigger_set_terminal_server
+{
+ # Function is defined in load_reporter.pm
+ if (exists $event_hash->{set_terminal_server})
+ {
+ no strict 'refs';
+ &{$event_hash->{set_terminal_server}."::set_terminal_server"};
+ }
+ else
+ {
+ &daemon_log("0 ERROR: load_reporter.pm needs function set_terminal_server to write terminal server load information to client. Processing aborted!", 1);
+ }
+ return;
+}
+
+sub trigger_seen_messages {
+ my ($kernel) = $_[KERNEL] ;
+
+ # Select all files under /tmp with prefix 'goto_notify'
+ my $goto_dir = "/tmp";
+ opendir(DIR, $goto_dir);
+ my @goto_files = grep { /.+\.goto_notify$/ && -f "$goto_dir/$_" } readdir(DIR);
+ closedir DIR;
+
+ # Check if file has 'seen' tag
+ foreach my $goto_file (@goto_files) {
+ open(my $FILE, "$goto_dir/$goto_file");
+ my @lines = <$FILE>;
+ close($FILE);
+
+ my $source;
+ my $target;
+ my $usr;
+ my $subject;
+ my $message;
+ my $seen = 0;
+ chomp(@lines);
+ foreach my $line (@lines) {
+ if ($line =~ /^source:([\S\s]*)$/) {
+ $source = $1
+ }
+ if ($line =~ /^target:([\S\s]*)$/) {
+ $target = $1;
+ }
+ if ($line =~ /^usr:([\S\s]*)$/) {
+ $usr = $1;
+ }
+ if ($line =~ /^subject:([\S\s]*)$/) {
+ $subject = $1;
+ }
+ if ($line =~ /^message:([\S\s]*)$/) {
+ $message = $1;
+ }
+ if ($line =~ /^seen$/) {
+ $seen++;
+ }
+ }
+
+ # Send 'confirm_usr_msg' back to msg-hosting server
+ if ($seen) {
+ my %data = ('usr'=>$usr, 'subject'=>$subject, 'message'=>$message);
+ my $confirm_msg = &build_msg("confirm_usr_msg", $target, $source, \%data);
+ my $send_error = &send_msg_to_target($confirm_msg, $server_address, $server_key);
+
+ # Delete file
+ if (not $send_error) {
+ system("rm $goto_dir/$goto_file");
+ }
+ }
+ }
+
+ $kernel->delay_set('trigger_seen_messages', $trigger_seen_messages_delay);
+
+ return;
+}
+
sub generic_file_error {
my ( $heap, $operation, $errno, $error_string, $wheel_id ) =
}
-sub fifo_got_record {
+sub fai_fifo_got_record {
my $file_record = $_[ARG0];
my $header;
my $content = "";
my $clmsg_hash = &create_xml_hash("CLMSG_$header", $client_address, $server_address, $content);
&add_content2xml_hash($clmsg_hash, "macaddress", $client_mac_address);
+ my $utc_ts = &main::get_utc_time();
+ &add_content2xml_hash($clmsg_hash, "timestamp", $utc_ts);
my $clmsg = &create_xml_string($clmsg_hash);
&send_msg_to_target($clmsg, $server_address, $server_key, "CLMSG_$header");
}
+sub system_fifo_got_record {
+ my ($kernel, $socket_com_record) = @_[KERNEL, ARG0];
+ $socket_com_record =~ /^(\S+)\s?([\s\S]*)$/;
+ my $function = $1;
+ my $content = $2;
+
+ if( exists $event_hash->{$function} )
+ {
+ # a event exists with the header as name
+ daemon_log("INFO: found event '$function' at event-module '".$event_hash->{$function}."'", 5);
+ no strict 'refs';
+ my $answer = &{$event_hash->{$function}."::$function"}($content, $kernel);
+ &post_processing_and_sending($answer)
+ }
+ else
+ {
+ daemon_log("ERROR: Got input '$socket_com_record' from socket '$system_com_fifo'", 1);
+ daemon_log("ERROR: No event '$function' found in event modules under '$event_dir'.", 1);
+ }
+
+# # Sanity check of fifo record
+#
+# # Create record message
+# my $lr_msg_hash = &create_xml_hash("load_report", $client_address, $server_address, $file_record);
+# &add_content2xml_hash($lr_msg_hash, "macaddress", $client_mac_address);
+# my $utc_ts = &main::get_utc_time();
+# &add_content2xml_hash($lr_msg_hash, "timestamp", $utc_ts);
+# my $lr_msg = &create_xml_string($lr_msg_hash);
+#
+# # Report message to si-server
+# &send_msg_to_target($lr_msg, $server_address, $server_key, "load_report");
+
+ return;
+}
+
+
sub save_fai_log {
my ($fai_log_dir) = @_ ;
my $FAI_DIR;
- opendir($FAI_DIR, $fai_log_dir);
- if (not defined $FAI_DIR) {
- daemon_log("ERROR: can not open directory $fai_log_dir: $!", 1);
- return;
+ # Directory for log files after a softupdate
+ my $log_dir = File::Spec->catdir($fai_log_dir, "localhost/last");
+
+ if (not -d $log_dir) {
+ # Directory for log files after a new installation
+ $log_dir = "/tmp/fai";
+ if (not -d $log_dir) {
+ daemon_log("ERROR: cannot open directory $log_dir", 1);
+ return;
+ }
}
+ opendir($FAI_DIR, "$log_dir");
my @log_files = readdir($FAI_DIR);
closedir($FAI_DIR);
- my @log_list;
+ my @log_list;
foreach my $log_file (@log_files) {
if( $log_file eq "." || $log_file eq ".." ) { next; }
+
my $log = "log_file:$log_file:";
- $log_file = File::Spec->catfile( $fai_log_dir, $log_file );
- open(my $FILE, "<$log_file");
- if (not defined $FILE ) {
- daemon_log("ERROR: can not open '$log_file': $!", 1);
+ $log_file = File::Spec->catfile( $log_dir, $log_file );
+
+ if (not -f $log_file) {
+ daemon_log("ERROR: cannot read $log_file", 1);
next;
}
- my @lines = <$FILE>;
+
+ open(my $FILE, "<","$log_file");
+ my @lines = <$FILE> ;
+ close ($FILE);
my $log_string = join("", @lines);
$log .= &encode_base64($log_string);
push(@log_list, $log);
- close ($FILE);
}
my $all_log_string = join("\n", @log_list);
my $watcher = POE::Wheel::FollowTail->new(
Filename => $file,
InputEvent => $name."_record",
- # ResetEvent => "file_reset",
ErrorEvent => "file_error",
);
-# $heap->{tail} = POE::Wheel::Run->new(
-# Program => [ "/usr/bin/tail", "-f", $file ],
-# StdoutEvent => $file_name."_record",
-# );
$heap->{services}->{ $watcher->ID } = $name;
$heap->{watchers}->{ $watcher->ID } = $watcher;
}
$kernel->yield('trigger_logged_in_users_report');
+ $kernel->yield('trigger_seen_messages');
}
my $error = 0;
my $answer;
-
daemon_log("INFO: Incoming msg from '$remote_ip'", 5);
daemon_log("DEBUG: Incoming msg:\n$input\n", 9);
- my ($msg, $msg_hash) = &check_key_and_xml_validity($input, $server_key);
+ # Cut of ip and port from msgs tail
+ my ($encrypted_msg, $msg_source) = split(/;/, $input);
+
+ my ($msg, $msg_hash) = &check_key_and_xml_validity($encrypted_msg, $server_key);
if( (!$msg) || (!$msg_hash) ) {
daemon_log("WARNING: Deciphering of incoming msg failed", 3);
if($server_address =~ /$remote_ip/) {
- # got a msg from gosa-si-server which can not be decrypted, may the secrete not up-to-date
+ # Got a msg from gosa-si-server which can not be decrypted, the secrete is not up-to-date
# cause a reregistering with a new secrete handshake
- daemon_log("WARNING: Message from gosa-si-server could not be understood, cause reregistering at server", 3);
-
- # if client is alread in a registration process, that means not registered, do nothing
- # if not, cause re-registration
- if (not $REGISTERED) {
- &daemon_log("WARNING: gosa-si-client is already in a registration process so ignore this message", 3);
- } else {
- $REGISTERED = 0;
+ daemon_log("ERROR: Message from gosa-si-server could not be understood, cause reregistering at server", 1);
+ if ($REGISTERED) {
+ daemon_log("INFO: Client has currently status registered so throw new registering event", 5);
+ &_setREGISTERED(0);
$kernel->post('client_session', 'register_at_gosa_si_server');
+ } else {
+ daemon_log("INFO: Client is currently within a registration process so wait for the next registration itteration", 5);
}
}
$error++;
########
# answer
- if( $answer ) {
+ &post_processing_and_sending($answer);
- #check gosa-si envelope validity
- my $answer_hash = &check_outgoing_xml_validity($answer);
+ return;
+}
- if( $answer_hash ) {
- # answer is valid
+sub post_processing_and_sending
+{
+ my ($answer) = @_ ;
- # preprocessing
- if( $answer =~ "<header>registered</header>") {
- # set registered flag to true to stop sending further registered msgs
- $REGISTERED = 1;
- }
+ # Do nothing if answer does not exists
+ if (not defined $answer) { return; }
+
+ # Check gosa-si envelope validity
+ my $answer_hash = &check_outgoing_xml_validity($answer);
+
+ # If answer is valid go ahead
+ if( not defined $answer_hash ) { return; }
+
+ # Sending
+ $answer =~ /<header>(\S+)<\/header>/;
+ &send_msg_to_target($answer, $server_address, $server_key, $1);
+
+ return;
+}
+
+sub find_servers {
+ # add gosa-si-server address from config file at first position of server list
+ #my $server_check_cfg = Config::IniFiles->new( -file => $cfg_file );
+ my $server_check_cfg = Config::IniFiles->new( -file => $config );
+
+ # Parse servers string
+ my @conf_servers = split(',', $servers_string);
+
+ # Now search for fallback servers in the configuration
+ foreach my $cur_server (@conf_servers) {
+ # Remove spaces from the IP
+ $cur_server =~ s/\s//g;
+
+ my $ip = $cur_server;
+ if(not $cur_server =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
+ my $ip_address = inet_ntoa(scalar gethostbyname($ip));
+ if(defined($ip_address) && $ip_address =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
+ # Write ip address to $server_ip variable
+ $ip = $ip_address;
+ }
+ }
+
+ my $server_addr = sprintf("%s:%s", $ip, $server_port);
+ if (not grep { $_ eq $server_addr } @servers) {
+ push(@servers, $server_addr);
+ }
+ }
+
+ my $servers_string = join(", ", @servers);
+ daemon_log("INFO: found servers in configuration file: $servers_string", 1);
+
+ # Last but not least search for fallback servers in the DNS
+ if (defined($opts_dnslookup) and $opts_dnslookup eq "true") {
+ my @tmp_servers;
+ if ( !$server_domain) {
+ # Try our DNS Searchlist
+ my @domain_list = &get_dns_domains();
+ my $tmp_domains;
+ my $error_string;
+ for my $domain (@domain_list) {
+ chomp($domain);
+ ($tmp_domains, $error_string) = &get_server_addresses($domain);
+ if(@$tmp_domains) {
+ for my $tmp_server(@$tmp_domains) {
+ push @tmp_servers, $tmp_server;
+ }
+ }
+ }
+
+ if (0 == @tmp_servers) {
+ daemon_log("INFO: No servers found in DNS.", 1);
+ }
else {
- $answer =~ /<header>(\S+)<\/header>/;
- &send_msg_to_target($answer, $server_address, $server_key, $1);
+ my $servers_string = join(", ", @tmp_servers);
+ daemon_log("INFO: found servers in DNS: $servers_string", 1);
}
+ } else {
+ @tmp_servers = &get_server_addresses($server_domain);
+ if( 0 == @tmp_servers ) {
+ daemon_log("INFO: No servers found in DNS for domain '$server_domain'",1);
+ }
+ }
- # postprocessing
- if( $answer =~ "<header>new_key</header>") {
- # set new key to global variable
- $answer =~ /<new_key>(\S*?)<\/new_key>/;
- my $new_key = $1;
- $server_key = $new_key;
+ if ( 0 != @tmp_servers ) {
+ foreach my $server_addr (@tmp_servers) {
+ if (not grep { $_ eq $server_addr } @servers) {
+ push(@servers, $server_addr);
+ }
}
}
+ }
+ if (0 == scalar(@servers)) {
+ daemon_log("ERROR: No servers found in the configuration or DNS.", 1);
+ exit(1);
}
- return;
-}
+ # Define first server as server_ip
+ $server_ip = $servers[0];
+ # prepare variables
+ if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
+ if (defined $server_ip && defined $server_port) {
+ $server_address = $server_ip.":".$server_port;
+ }
+}
+#
#==== MAIN = main ==============================================================
-# parse commandline options
-Getopt::Long::Configure( "bundling" );
-GetOptions("h|help" => \&usage,
- "c|config=s" => \$cfg_file,
- "f|foreground" => \$foreground,
- "v|verbose+" => \$verbose,
- );
+#
+# Parse options and allow '-vvv'
+Getopt::Long::Configure( 'bundling' );
+GetOptions( 'v|verbose+' => \$verbose,
+ 'h|help' => \&usage,
+ 'c|config=s' => \$config,
+ 'x|dump-config=i' => \$dump_config,
+ 'f|foreground' => \$foreground)
+ or usage( '', 1 );
+
+# We may want to dump the default configuration
+if( defined $dump_config ) {
+ if($dump_config==1) {
+ } elsif ($dump_config==2) {
+ dump_configuration( $dump_config );
+ } else {
+ usage( "Dump configuration value has to be 1 or 2" );
+ }
+}
# read and set config parameters
-&check_cmdline_param ;
-&read_configfile($cfg_file, %cfg_defaults);
-&check_pid;
+&read_configfile($config, %cfg_defaults);
+#&check_pid;
+# Create the PID object
+# Ensure you put a name that won't clobber
+# another program's PID file
+$pid = File::Pid->new({
+ file => $pid_file,
+});
+
+# Write the PID file
+$pid->write;
# forward error messages to logfile
if ( ! $foreground ) {
$pid = fork;
setsid or die "Can't start a new session: $!";
umask 0;
-} else {
- $pid = $$;
+#} else {
+# $pid = $$;
}
# Do something useful - put our PID into the pid_file
-if( 0 != $pid ) {
- open( LOCK_FILE, ">$pid_file" );
- print LOCK_FILE "$pid\n";
- close( LOCK_FILE );
- if( !$foreground ) {
- exit( 0 )
- };
-}
+#if( 0 != $pid ) {
+# open( my $LOCK_FILE, ">", "$pid_file" );
+# print $LOCK_FILE "$pid\n";
+# close( $LOCK_FILE );
+# if( !$foreground ) {
+# exit( 0 )
+# };
+#}
# parse head url and revision from svn
my $client_status_hash = { 'developmental'=>'revision', 'stable'=>'release'};
$client_status = "developmental" ;
}
-# Prepare log file
-my $root_uid = getpwnam('root');
-my $adm_gid = getgrnam('adm');
-chmod(0640, $log_file);
-chown($root_uid, $adm_gid, $log_file);
-
+# Determine root uid and adm gid, used for creating log files
+$root_uid = getpwnam('root');
+$adm_gid = getgrnam('adm');
+if(not defined $adm_gid){
+ $adm_gid = getgrnam('root');
+}
daemon_log(" ", 1);
-daemon_log("$prg started!", 1);
+daemon_log("gosa-si-client started!", 1);
daemon_log("INFO: status: $client_status", 1);
daemon_log("INFO: ".$client_status_hash->{$client_status}.": $client_revision", 1);
-# delete old DBsqlite lock files
+# to be changed maybe not trace of it when running
system('rm -f /tmp/gosa_si_lock*gosa-si-client*');
# detect ip and mac address and complete host address
$gotoHardwareChecksum= &generate_hw_digest();
daemon_log("INFO: gotoHardwareChecksum detected: $gotoHardwareChecksum", 1);
-
# create socket for incoming xml messages
POE::Component::Server::TCP->new(
Alias => 'gosa-si-client',
);
daemon_log("INFO: start socket for incoming xml messages at port '$client_port' ", 1);
-
-# prepare variables
-if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
-if (defined $server_ip && defined $server_port) {
- $server_address = $server_ip.":".$server_port;
-}
$xml = new XML::Simple();
$default_server_key = $server_key;
+# Open a new fifo for FAI messages to gosa-si-server
+if (-p $fai_com_fifo) { unlink $fai_com_fifo }
+POSIX::mkfifo("$fai_com_fifo", 0600);
-# add gosa-si-server address from config file at first position of server list
-my $server_check_cfg = Config::IniFiles->new( -file => $cfg_file );
-my $server_check = (defined($server_check_cfg))?$server_check_cfg->val( "server", "ip"):undef;
-if( defined $server_check ) {
- unshift(@servers, $server_address);
- my $servers_string = join(", ", @servers);
- daemon_log("INFO: found servers in configuration file: $servers_string", 1);
-} else {
- my @tmp_servers;
- if ( !$server_domain) {
- # Try our DNS Searchlist
- my @domain_list = &get_dns_domains();
- my $tmp_domains;
- my $error_string;
- for my $domain (@domain_list) {
- chomp($domain);
- ($tmp_domains, $error_string) = &get_server_addresses($domain);
- if(@$tmp_domains) {
- for my $tmp_server(@$tmp_domains) {
- push @tmp_servers, $tmp_server;
- }
- }
- }
- if (0 == @tmp_servers) {
- my $log_string = "no gosa-si-server found!";
- $log_string .= "\n\tdetermined domains out of /etc/resolv.conf: ".join(", ", @domain_list);
- $log_string .= "\n\tdetermined server addresses in domains: ".join(", ",@$tmp_domains);
- daemon_log("ERROR: $log_string", 1);
- daemon_log("ERROR: $error_string", 1);
- daemon_log("ERROR: please specify a gosa-si-server address or a domain in config file", 1);
- kill 2, $$;
- }
- } else {
- @tmp_servers = &get_server_addresses($server_domain);
- if( 0 == @tmp_servers ) {
- daemon_log("ERROR: no gosa-si-server found in DNS for domain '$server_domain'",1);
- daemon_log("ERROR: please specify a gosa-si-server address or a domain in config file", 1);
- kill 2, $$;
- }
- }
+# Find servers from config and DNS
+&find_servers;
- foreach my $server (@tmp_servers) {
- unshift(@servers, $server);
- }
- my $servers_string = join(", ", @servers);
- daemon_log("INFO: found servers in DNS: $servers_string", 1);
+# Open a new fifo for system communication,
+if (-p $system_com_fifo) { unlink $system_com_fifo }
+if ($system_com eq "enabled")
+{
+ POSIX::mkfifo("$system_com_fifo", 0600);
}
-
-# open fifo for non-gosa-si-client-msgs to gosa-si-server
-POSIX::mkfifo("$gosa_si_client_fifo", "0600");
-
-
POE::Session->create(
inline_states => {
- _start => \&_start,
- _default => \&_default,
- sig_handler => \&sig_handler,
- register_at_gosa_si_server => \®ister_at_gosa_si_server,
-
- # trigger periodical tasks
- trigger_new_key => \&trigger_new_key,
- trigger_logged_in_users_report => \&trigger_logged_in_users_report,
-
- # handle records from each defined file differently
- fifo_record => \&fifo_got_record,
-
- # handle file resets and errors the same way for each file
- file_reset => \&generic_file_reset,
- file_error => \&generic_file_error,
+ _start => \&_start,
+ _default => \&_default,
+ sig_handler => \&sig_handler,
+ register_at_gosa_si_server => \®ister_at_gosa_si_server,
+
+ # trigger periodical tasks
+ trigger_new_key => \&trigger_new_key,
+ trigger_logged_in_users_report => \&trigger_logged_in_users_report,
+ trigger_seen_messages => \&trigger_seen_messages,
+
+ # trigger non periodical tasks
+ trigger_set_terminal_server => \&trigger_set_terminal_server,
+
+ # handle records from each defined file differently
+ fai_fifo_record => \&fai_fifo_got_record,
+ system_fifo_record => \&system_fifo_got_record,
+
+ # handle file resets and errors the same way for each file
+ file_reset => \&generic_file_reset,
+ file_error => \&generic_file_error,
}
);