Code

bugfix: writing ntp server file
[gosa.git] / gosa-si / client / events / corefunctions.pm
index 3d0311926bda160bfe7b8dc12eb02270a719f6f7..7e19a8b18dcdbd1f0e37d3e2f8dd2b0e94cddc56 100644 (file)
@@ -4,13 +4,15 @@ use Exporter;
 my @events = (
     "get_events",
     "registered",
+    'new_syslog_config',
+    "new_ntp_config",
     "new_ldap_config",
     "new_key",
-    "generate_hw_digest",
+    "generate_hw_digest",     # no implementations
     "detect_hardware",
     "confirm_new_key",
     "ping",
-    "import_events",
+    "import_events",    # no implementations
     );
 @EXPORT = @events;
 
@@ -20,14 +22,16 @@ use Data::Dumper;
 use Fcntl;
 use GOSA::GosaSupportDaemon;
 use File::Basename;
-use open ':utf8';
 
-my ($ldap_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
+my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
 
+my $chrony_file = "/etc/chrony/chrony.conf";
+my $syslog_file = "/etc/syslog.conf";
 
 my %cfg_defaults = (
     "client" => {
         "ldap" => [\$ldap_enabled, 1],
+        "offline-ldap" => [\$offline_enabled, 0],
         "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
         "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
         "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
@@ -94,7 +98,7 @@ sub registered {
     my $header = @{$msg_hash->{'header'}}[0];
     if( $header eq "registered" ) {
         my $source = @{$msg_hash->{'source'}}[0];
-        &main::daemon_log("registration at $source",1);
+        &main::daemon_log("INFO: registration at $source", 1);
         $main::server_address = $source;
     }
 
@@ -112,6 +116,7 @@ sub registered {
     my $opts_file_FH;
     my $hostname= $main::client_dnsname;
     $hostname =~ s/\..*$//;
+    $hostname =~ tr/A-Z/a-z/;
     open($opts_file_FH, ">$main::opts_file");
     print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
     print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
@@ -151,6 +156,124 @@ sub server_leaving {
 }
 
 
+## @method new_syslog_config
+# Update or add syslog messages forwarding to specified syslog server.
+# @param msg - STRING - xml message with tag server
+# @param msg_hash - HASHREF - message information parsed into a hash
+sub new_syslog_config {
+    my ($msg, $msg_hash) = @_ ;
+
+    # Sanity check of incoming message
+    if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
+        &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
+        return;
+    }
+
+    # Fetch the new syslog server from incoming message
+    my $syslog_server = @{$msg_hash->{'server'}}[0];
+    &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5); 
+    my $found_server_flag = 0;
+    
+    # Sanity check of /etc/syslog.conf
+    if (not -f $syslog_file) {
+        &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
+        return;
+    }
+    
+    # Substitute existing server with new syslog server
+    open (FILE, "<$syslog_file");
+    my @file = <FILE>;
+    close FILE;
+    my $syslog_server_line = "*.*\t@".$syslog_server."\n"; 
+    foreach my $line (@file) {
+        if ($line =~ /^\*\.\*\s+@/) {
+            $line = $syslog_server_line;
+            $found_server_flag++;
+        }
+    }
+    
+    # Append new server if no old server configuration found
+    if (not $found_server_flag) {
+        push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
+        push(@file, $syslog_server_line);
+    }
+    
+    # Write changes to file and close it
+    open (FILE, "+>$syslog_file");
+    print FILE join("", @file); 
+    close FILE;
+    &main::daemon_log("INFO: wrote new configuration file: $syslog_file", 5);
+
+    # Restart syslog deamon
+    my $res = qx(/etc/init.d/sysklogd restart);
+    &main::daemon_log("INFO: restart syslog daemon: $res", 5);
+
+    return;
+}
+
+
+## @method new_ntp_config
+# Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
+# @param msg - STRING - xml message with tag server
+# @param msg_hash - HASHREF - message information parsed into a hash
+sub new_ntp_config {
+    my ($msg, $msg_hash) = @_ ;
+
+    # Sanity check of incoming message
+    if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
+        &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
+        return;
+    }
+
+    # Fetch the new ntp server from incoming message
+    my $ntp_servers = $msg_hash->{'server'};
+    &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5); 
+    my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
+    my $found_server_flag = 0;
+
+    # Sanity check of /etc/chrony/chrony.conf
+    if (not -f $chrony_file) {
+        &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
+        return;
+    }
+
+    # Substitute existing server with new ntp server
+    open (FILE, "<$chrony_file");
+    my @file = <FILE>;
+    close FILE;
+       my @new_file;
+    foreach my $line (@file) {
+        if ($line =~ /^server\s+/) {
+            if ($found_server_flag) {  
+                $line =~ s/^server\s+[\S]+\s+$//;
+            } else {
+                $line =~ s/^server\s+[\S]+\s+$/$ntp_servers_string/;
+            }
+            $found_server_flag++;
+        }
+               push(@new_file, $line);
+    }
+
+    # Append new server if no old server configuration found
+    if (not $found_server_flag) {
+        push(@new_file, "\n# ntp server configuration written by GOsa-si\n");
+        push(@new_file, $ntp_servers_string);
+    }
+
+    # Write changes to file and close it
+    open (FILE, ">$chrony_file");
+    print FILE join("", @new_file); 
+    close FILE;
+    &main::daemon_log("INFO: wrote new configuration file: $chrony_file", 5);
+
+    # Restart chrony deamon
+    my $res = qx(/etc/init.d/chrony force-reload);
+    &main::daemon_log("INFO: restart chrony daemon: $res", 5);
+
+    return;
+}
+
+
 sub new_ldap_config {
     my ($msg, $msg_hash) = @_ ;
 
@@ -285,7 +408,7 @@ sub new_ldap_config {
     }
 
     # Write shell based config
-    my $cfg_name= dirname($ldap_config)."/ldap-shell.conf";
+    my $cfg_name= "/etc/ldap/ldap-shell.conf";
 
     # Get first LDAP server
     my $ldap_server= $ldap_uris[0];
@@ -294,6 +417,7 @@ sub new_ldap_config {
     open(file1, "> $cfg_name");
     print file1 "LDAP_BASE=\"$ldap_base\"\n";
     print file1 "LDAP_SERVER=\"$ldap_server\"\n";
+    print file1 "LDAP_URIS=\"@ldap_uris\"\n";
     print file1 "ADMIN_BASE=\"$admin_base\"\n";
     print file1 "DEPARTMENT=\"$department\"\n";
     print file1 "RELEASE=\"$release\"\n";
@@ -302,6 +426,24 @@ sub new_ldap_config {
     close(file1);
     daemon_log("wrote $cfg_name", 5);
 
+    # Write offline config
+    if ($offline_enabled){
+           $cfg_name= "/etc/ldap/ldap-offline.conf";
+
+           # Get first LDAP server
+           open(file1, "> $cfg_name");
+           print file1 "LDAP_BASE=\"$ldap_base\"\n";
+           print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
+           print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
+           print file1 "ADMIN_BASE=\"$admin_base\"\n";
+           print file1 "DEPARTMENT=\"$department\"\n";
+           print file1 "RELEASE=\"$release\"\n";
+           print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
+           print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
+           close(file1);
+           daemon_log("wrote $cfg_name", 5);
+    }
+
     return;
 }
 
@@ -335,7 +477,6 @@ sub confirm_new_key {
 
 sub detect_hardware {
 
-
     &write_to_file('goto-hardware-detection-start', $fai_logpath);
 
        my $hwinfo= `which hwinfo`;
@@ -460,12 +601,25 @@ sub ping {
     my $source = @{$msg_hash->{'source'}}[0];
     my $target = @{$msg_hash->{'target'}}[0];
     my $session_id = @{$msg_hash->{'session_id'}}[0];
+    my $out_msg;
+    my $out_hash;
+
+    # there is no session_id so send 'got_new_ping'-msg
+    if (not defined $session_id) {
+        $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
+
+    # there is a session_id so send 'answer_$session_id'-msg because there is 
+    # a process waiting for this message
+    } else {
+        $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
+        &add_content2xml_hash($out_hash, "session_id", $session_id);
+    }
 
-   
-    # switch target and source and send msg back
-    my $out_hash = &main::create_xml_hash("got_ping", $target, $source);
-    &add_content2xml_hash($out_hash, "session_id", $session_id);
-    my $out_msg = &main::create_xml_string($out_hash);
+    my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
+    if (defined $forward_to_gosa) {
+        &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
+    }
+    $out_msg = &main::create_xml_string($out_hash);
     return $out_msg;
 
 }