Code

Updated stype to fix layout errors
[gosa.git] / gosa-si / gosa-si-client
index 76eb6718bd7e01d9929bcc43812caa25df72ca46..a3987e70ea920ee83c64028be8bc654ee7441741 100755 (executable)
@@ -87,7 +87,7 @@ our $prg= basename($0);
 my $trigger_logged_in_users_report_delay = 600;
 
 # directory where all log files from installation are stored
-my $fai_log_dir = "/tmp/fai"; 
+my $fai_log_dir = "/var/log/fai"; 
 
 %cfg_defaults = (
 "general" =>
@@ -120,6 +120,19 @@ my $fai_log_dir = "/tmp/fai";
 
 #=== 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)
+EOF
+    print "\n" ;
+}
+
 #===  FUNCTION  ================================================================
 #         NAME: check_cmdline_param
 #   PARAMETERS: 
@@ -145,33 +158,6 @@ sub check_cmdline_param () {
 }
 
 
-#===  FUNCTION  ================================================================
-#         NAME:  read_configfile
-#   PARAMETERS:  cfg_file - string - 
-#      RETURNS:  
-#  DESCRIPTION: 
-#===============================================================================
-sub read_configfile {
-    my ($cfg_file, %cfg_defaults) = @_ ;
-    my $cfg;
-    if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
-        if( -r $cfg_file ) {
-            $cfg = Config::IniFiles->new( -file => $cfg_file );
-        } else {
-            print STDERR "Couldn't read config file!";
-        }
-    } else {
-        $cfg = Config::IniFiles->new() ;
-    }
-    foreach my $section (keys %cfg_defaults) {
-        foreach my $param (keys %{$cfg_defaults{ $section }}) {
-            my $pinfo = $cfg_defaults{ $section }{ $param };
-           ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
-        }
-    }
-}
-
-
 #===  FUNCTION  ================================================================
 #         NAME: check_pid
 #   PARAMETERS:
@@ -245,7 +231,6 @@ sub daemon_log {
     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 
@@ -365,49 +350,6 @@ sub get_local_mac_for_remote_ip {
        return $result;
 }
 
-sub get_local_ip_for_remote_ip {
-       my $server_ip= shift;
-       my $result="0.0.0.0";
-
-       if($server_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
-        # client should always have a 'valid' ip-address, which is available from other hosts too,
-        # 127.0.0.1 says nothing to foreign host
-               #if($server_ip eq "127.0.0.1") {
-               #       $result="127.0.0.1";
-               #} else {
-                       my $PROC_NET_ROUTE= ('/proc/net/route');
-
-                       open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
-                               or die "Could not open $PROC_NET_ROUTE";
-
-                       my @ifs = <PROC_NET_ROUTE>;
-
-                       close(PROC_NET_ROUTE);
-
-                       # Eat header line
-                       shift @ifs;
-                       chomp @ifs;
-                       foreach my $line(@ifs) {
-                               my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
-                               my $destination;
-                               my $mask;
-                               my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
-                               $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
-                               ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
-                               $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
-                               if(new NetAddr::IP($server_ip)->within(new NetAddr::IP($destination, $mask))) {
-                                       # destination matches route, save mac and exit
-                                       $result= &get_ip($Iface);
-                                       last;
-                               }
-                       }
-               #}
-       } else {
-               daemon_log("get_local_ip_for_remote_ip was called with a non-ip parameter: $server_ip", 1);
-       }
-       return $result;
-}
-
 
 sub generate_hw_digest {
        my $hw_data;
@@ -609,14 +551,15 @@ sub register_at_gosa_si_server {
                # 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);
-                       }
-               }
+            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
                        my $local_ip = &get_local_ip_for_remote_ip(sprintf("%s", $server =~ /^([0-9\.]*?):.*$/));
@@ -863,6 +806,8 @@ sub fifo_got_record {
 
     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");
 
@@ -1095,6 +1040,13 @@ if ($client_headURL =~ /\/tag\// ||
     $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);
+
+
 daemon_log(" ", 1);
 daemon_log("$prg started!", 1);
 daemon_log("INFO: status: $client_status", 1);
@@ -1113,7 +1065,11 @@ daemon_log("INFO: gosa-si-client mac address detected: $client_mac_address", 1);
 
 # import events
 my ($error, $result, $tmp_hash) = &import_events($event_dir);
-$event_hash = $tmp_hash;
+while (my ($module, $mod_event_hash) = each %$tmp_hash) {
+    while (my ($event_name, $nothing) = each %$mod_event_hash) {
+        $event_hash->{$event_name} = $module;
+    }
+}
 
 foreach my $log_line (@$result) {
     if ($log_line =~ / succeed: /) {
@@ -1153,21 +1109,29 @@ my $server_check = (defined($server_check_cfg))?$server_check_cfg->val( "server"
 if( defined $server_check ) {
        unshift(@servers, $server_address);
        my $servers_string = join(", ", @servers);
-       daemon_log("INFO: found servers in configuration file: $servers_string", 5);
+       daemon_log("INFO: found servers in configuration file: $servers_string", 1);
 } else {
        my @tmp_servers;
        if ( !$server_domain) {
                # Try our DNS Searchlist
-               for my $domain(get_dns_domains()) {
+        my @domain_list = &get_dns_domains();
+        my $tmp_domains;
+        my $error_string;
+               for my $domain (@domain_list) {
                        chomp($domain);
-                       my @tmp_domains= &get_server_addresses($domain);
-                       if(@tmp_domains) {
-                               for my $tmp_server(@tmp_domains) {
+            ($tmp_domains, $error_string) = &get_server_addresses($domain);
+                       if(@$tmp_domains) {
+                               for my $tmp_server(@$tmp_domains) {
                                        push @tmp_servers, $tmp_server;
                                }
                        }
                }
-               if(@tmp_servers && length(@tmp_servers)==0) {
+               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, $$;
                }
@@ -1184,7 +1148,7 @@ if( defined $server_check ) {
         unshift(@servers, $server); 
     }
     my $servers_string = join(", ", @servers);
-    daemon_log("INFO: found servers in DNS: $servers_string", 5);
+    daemon_log("INFO: found servers in DNS: $servers_string", 1);
 }