Code

Property filter is no longer case sensitive
[gosa.git] / gosa-si / gosa-si-client
index d9c62845ca4afbf27314fa74bf567f016bddd6ef..56200d9964557078bc8530c094f7acf8de4b250f 100755 (executable)
@@ -1,42 +1,92 @@
 #!/usr/bin/perl
-#===============================================================================
-#
-#         FILE:  gosa-si-client
+#*********************************************************************
 #
-#        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 Crypt::Rijndael;
-use GOSA::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 GOsaSI::GosaSupportDaemon;
+
+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";
@@ -49,7 +99,7 @@ my $client_status;
 my $event_dir = "/usr/lib/gosa-si/client/events";
 use lib "/usr/lib/gosa-si/client/events";
 
-my (%cfg_defaults, $foreground, $verbose, $pid_file, $procid, $pid, $log_file, $fai_logpath);
+my (%cfg_defaults, $foreground, $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);
@@ -60,7 +110,8 @@ my @servers;
 my $gotoHardwareChecksum;
 my $system_com;
 my $servers_string;
-$verbose= 1;
+
+my $client_status_hash = { 'developmental'=>'revision', 'stable'=>'release'};
 
 # globalise variables which are used in imported events
 our $global_kernel;
@@ -75,20 +126,25 @@ our $client_force_hostname;
 our $server_key;
 our $terminal_server_hash;
 our $opts_dnslookup;
+our $verbose= 0;
 
+# 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
 my $REGISTERED = 0;
 
 # path to fifo for non-gosa-si-client messages to gosa-si-server
-my $fai_com_fifo = "/var/run/gosa-si-client.socket";
-my $system_com_fifo = "/var/run/gosa-si-client-system-com.socket";
+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 n seconds the client reports logged_in users to gosa-si-server
 my $trigger_logged_in_users_report_delay = 600;
@@ -101,9 +157,9 @@ 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"],
+    {"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" => 
     {"port"        => [\$client_port, "20083"],
@@ -132,102 +188,139 @@ my $fai_log_dir = "/var/log/fai";
 
 #=== FUNCTIONS = functions =====================================================
 
-sub usage {
-    print STDERR << "EOF" ;
-usage: $prg [-hvf] [-c config]
+#############################
+#
+# @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 ) = @_;
 
-           -h        : this (help) message
-           -c <file> : config file
-           -f        : foreground, process will not be forked to background
-           -v        : be verbose (multiple to increase verbosity)
+  $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" );
 
-#===  FUNCTION  ================================================================
-#         NAME: check_cmdline_param
-#   PARAMETERS: 
-#      RETURNS:  
-#  DESCRIPTION: 
-#===============================================================================
-sub check_cmdline_param () {
-    my $err_config;
-    my $err_counter = 0;
-       if(not defined($cfg_file)) {
-               $cfg_file = "/etc/gosa-si/client.conf";
-               if(! -r $cfg_file) {
-                       $err_config = "please specify a config file";
-                       $err_counter += 1;
-               }
-    }
-    if( $err_counter > 0 ) {
-        &usage( "", 1 );
-        if( defined( $err_config)) { print STDERR "$err_config\n"}
-        print STDERR "\n";
-        exit( -1 );
-    }
+  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 {
 
-#===  FUNCTION  ================================================================
-#         NAME: check_pid
-#   PARAMETERS:
-#      RETURNS:
-#  DESCRIPTION:
-#===============================================================================
-sub check_pid {
-    $pid = -1;
-    # Check, if we are already running
-    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);
-                    exit -1;
-                }
-            }
-        }
-        close( $LOCK_FILE );
-        unlink( $pid_file );
-    }
+  my( $cfg_type ) = @_;
 
-    # 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(my $LOCK_FILE2, '<', $pid_file)
-                && ($pid = <$LOCK_FILE2>))
-        {
-            chomp($pid);
-            $msg .= "(PID $pid)\n";
-        } else {
-            $msg .= "(unable to read PID)\n";
-        }
-        if( ! ($foreground) ) {
-            openlog( $0, "cons,pid", "daemon" );
-            syslog( "warning", $msg );
-            closelog();
-        }
-        else {
-            print( STDERR " $msg " );
-        }
-        exit( -1 );
-    }
-}
+  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 );
+       }
+}
+
+#############################
+#
+# @brief When caching an interrupt remove file pid and stopping gosa-si
+#
 sub sig_int_handler {
-    my ($signal) = @_;
-       
-    daemon_log("shutting down gosa-si-client", 1);
-    system("kill `ps -C gosa-si-client -o pid=`");
+       my ($signal) = @_;
+
+       daemon_log("shutting down gosa-si-client", 1);
+
+       # shtudown the client by telling the poe kernel
+       $global_kernel->yield('shutdown');
+
+       #removing the opts file
+       unlink ('/var/run/gosa-si/gosa-si-client.opts');
+
+       #removing the fifo for communication 
+       unlink ($fai_com_fifo);
+
+       #removing the 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' 
@@ -270,7 +363,7 @@ sub daemon_log {
                        $microSeconds =~ s/^\d*(.\d\d)$/$1/;
 
 
-            my $log_msg = "$month $monthday $hours:$minutes:$seconds$microSeconds $prg $msg\n";
+            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;
@@ -497,11 +590,11 @@ sub write_to_file {
     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++;
     }
     
@@ -798,7 +891,7 @@ sub trigger_logged_in_users_report {
                        @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
@@ -1132,7 +1225,7 @@ sub post_processing_and_sending
 
 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);
@@ -1216,81 +1309,109 @@ sub find_servers {
     }
 }
 
-#==== MAIN = main ==============================================================
-#  parse commandline options
-Getopt::Long::Configure( "bundling" );
-GetOptions("h|help" => \&usage,
-           "c|config=s" => \$cfg_file,
-           "f|foreground" => \$foreground,
-           "v|verbose+" => \$verbose,
-           );
 
-#  read and set config parameters
-&check_cmdline_param ;
-&read_configfile($cfg_file, %cfg_defaults);
-&check_pid;
+sub start_daemon {
 
+       if( ! $foreground ) {
+               chdir '/'                 or die "Can't chdir to /: $!";
+               umask 0;
+               open STDIN, '+>/dev/null'   or die "Can't read /dev/null: $!";
+               open STDOUT, '+>&STDIN' or die "Can't write to /dev/null: $!";
+               open STDERR, '+>&STDIN' or die "Can't write to /dev/null: $!";
+               defined($pid = fork)   or die "Can't fork: $!";
+               exit if $pid;
+               setsid                    or die "Can't start a new session: $!";
+       }
+       return;
+}
+
+sub put_version {
 
-# forward error messages to logfile
-if ( ! $foreground ) {
-  open( STDIN,  '+>/dev/null' );
-  open( STDOUT, '+>&STDIN'    );
-  open( STDERR, '+>&STDIN'    );
+       # parse head url and revision from svn
+       $client_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/;
+       $client_headURL = defined $1 ? $1 : 'unknown' ;
+       $client_revision = defined $2 ? $2 : 'unknown' ;
+       if ($client_headURL =~ /\/tag\// || 
+        $client_headURL =~ /\/branches\// ) {
+    $client_status = "stable"; 
+       } else {
+    $client_status = "developmental" ;
+       }
+       return;
 }
 
-# Just fork, if we are not in foreground mode
-if( ! $foreground ) { 
-    chdir '/'                 or die "Can't chdir to /: $!";
-    $pid = fork;
-    setsid                    or die "Can't start a new session: $!";
-    umask 0;
-} else { 
-    $pid = $$; 
+sub get_perms_admin {
+       # 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');
+       }
+       return;
 }
 
-# Do something useful - put our PID into the pid_file
-if( 0 != $pid ) {
-    open( my $LOCK_FILE, ">", "$pid_file" );
-    print $LOCK_FILE "$pid\n";
-    close( $LOCK_FILE );
-    if( !$foreground ) { 
-        exit( 0 ) 
-    };
+sub create_pid {
+       # Create the PID object
+       $pid = File::Pid->new({ file  => $pid_file });
+
+       # Write the PID file
+       $pid->write;
+
+       return;
 }
 
-# parse head url and revision from svn
-my $client_status_hash = { 'developmental'=>'revision', 'stable'=>'release'};
-$client_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/;
-$client_headURL = defined $1 ? $1 : 'unknown' ;
-$client_revision = defined $2 ? $2 : 'unknown' ;
-if ($client_headURL =~ /\/tag\// || 
-        $client_headURL =~ /\/branches\// ) {
-    $client_status = "stable"; 
-} else {
-    $client_status = "developmental" ;
+#
+#==== MAIN = main ==============================================================
+#
+# 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
+&read_configfile($config, %cfg_defaults);
+
+# daemonize the program
+&start_daemon($foreground);
+
+# create pid file
+&create_pid($pid, $pid_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');
-}
+&get_perms_admin($root_uid, $adm_gid);
+
+# put version
+&put_version($client_status_hash, $client_version, $client_version, $client_headURL, $client_status);
 
 daemon_log(" ", 1);
-daemon_log("$prg started!", 1);
+daemon_log("$0 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
-system('rm -f /tmp/gosa_si_lock*gosa-si-client*');
+# 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
 $client_address = $client_ip.":".$client_port;
 my $network_interface= &get_interface_for_ip($client_ip);
 $client_mac_address= &get_mac($network_interface);
+
 daemon_log("INFO: ip address detected: $client_ip", 1);
-daemon_log("INFO: gosa-si-client mac address detected: $client_mac_address", 1);
+daemon_log("INFO: $0 mac address detected: $client_mac_address", 1);
 
 
 # import events
@@ -1341,26 +1462,26 @@ if ($system_com eq "enabled")
 
 POE::Session->create(
        inline_states => {
-               _start => \&_start, 
-        _default => \&_default,
-        sig_handler => \&sig_handler,
-        register_at_gosa_si_server => \&register_at_gosa_si_server,
+               _start => \&_start,
+               _default => \&_default,
+               sig_handler => \&sig_handler,
+               register_at_gosa_si_server => \&register_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 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,
+
+               # 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,
+               # handle file resets and errors the same way for each file
+               file_reset => \&generic_file_reset,
+               file_error => \&generic_file_error,
        }
 );