Code

- More open fixes, add missing my in front of filehandle variable
[gosa.git] / gosa-si / client / events / corefunctions.pm
index 04cb8a8bfe32549c5167e28d2c311842dc149029..a25aa98cb89f3ed1fbffa3cf949979c768ab1ad1 100644 (file)
@@ -1,28 +1,29 @@
 package corefunctions;
-use Exporter;
-@ISA = qw(Exporter);
-my @events = (
-       "get_events",
-       "registered",
-       'new_syslog_config',
-       "new_ntp_config",
-       "new_ldap_config",
-       "new_key",
-       "generate_hw_digest",     # no implementations
-       "detect_hardware",
-       "confirm_new_key",
-       "ping",
-       "import_events",    # no implementations
-       );
-@EXPORT = @events;
 
 use strict;
 use warnings;
+use Exporter;
 use Fcntl;
 use GOSA::GosaSupportDaemon;
 use File::Basename;
 
-my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
+@ISA = qw(Exporter);
+my @events = (
+  "get_events",
+  "registered",
+  "new_syslog_config",
+  "new_ntp_config",
+  "new_ldap_config",
+  "new_key",
+  "generate_hw_digest",     # no implementations
+  "detect_hardware",
+  "confirm_new_key",
+  "ping",
+  "import_events",    # no implementations
+  );
+@EXPORT = @events;
+
+my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath, $ldap_config_exit_hook);
 
 my $chrony_file = "/etc/chrony/chrony.conf";
 my $syslog_file = "/etc/syslog.conf";
@@ -35,6 +36,7 @@ my %cfg_defaults = (
                "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
                "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
                "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
+               "ldap-config-exit-hook" => [\$ldap_config_exit_hook, undef],
        },
 );
 
@@ -69,12 +71,12 @@ sub write_to_file {
                chomp($string);
                        
                if( not -f $file ) {
-                       open (FILE, "$file");
-                       close(FILE);
+                       open (my $FD_FILE, "$file");
+                       close($FD_FILE);
                }
-               open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
-               print FILE $string."\n";
-               close(FILE);
+               open(my $FD_FILE, ">>", "$file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
+               print $FD_FILE $string."\n";
+               close($FD_FILE);
        }
 
        return;    
@@ -106,10 +108,11 @@ sub registered {
        $main::client_address = $target;
 
        # set registration_flag to true 
-       my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
-        # Write the MAC address to file
+       &main::_setREGISTERED(1);
+
+       # Write the MAC address to file
        if(stat($main::opts_file)) { 
-                       unlink($main::opts_file);
+               unlink($main::opts_file);
        }
 
        my $opts_file_FH;
@@ -132,8 +135,7 @@ sub registered {
        }
        close($opts_file_FH);
         
-       my $out_msg = &create_xml_string($out_hash);
-       return $out_msg;
+       return;
 }
 
 sub server_leaving {
@@ -180,9 +182,9 @@ sub new_syslog_config {
        }
        
        # Substitute existing server with new syslog server
-       open (FILE, "<$syslog_file");
-       my @file = <FILE>;
-       close FILE;
+       open (my $FILE, "<","$syslog_file");
+       my @file = <$FILE>;
+       close($FILE);
        my $syslog_server_line = "*.*\t@".$syslog_server."\n"; 
        foreach my $line (@file) {
                if ($line =~ /^\*\.\*\s+@/) {
@@ -198,10 +200,10 @@ sub new_syslog_config {
        }
        
        # 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);
+       open (my $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);
@@ -237,9 +239,9 @@ sub new_ntp_config {
        }
 
        # Substitute existing server with new ntp server
-       open (FILE, "<$chrony_file");
-       my @file = <FILE>;
-       close FILE;
+       open (my $FILE, "<","$chrony_file");
+       my @file = <$FILE>;
+       close($FILE);
        my @new_file;
        foreach my $line (@file) {
                if ($line =~ /^server\s+/) {
@@ -260,10 +262,10 @@ sub new_ntp_config {
        }
 
        # 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);
+       open (my $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);
@@ -357,53 +359,53 @@ sub new_ldap_config {
        # Setup ldap.conf
        my $file1;
        my $file2;
-       open(file1, "> $ldap_config");
-       print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
-       print file1 "URI";
+       open(my $file1, ">","$ldap_config");
+       print $file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
+       print $file1 "URI";
        foreach $element (@ldap_uris) {
-               print file1 " $element";
+               print $file1 " $element";
        }
-       print file1 "\nBASE $ldap_base\n";
+       print $file1 "\nBASE $ldap_base\n";
        foreach $element (@ldap_options) {
-               print file1 "$element\n";
+               print $file1 "$element\n";
        }
-       close (file1);
-       daemon_log("wrote $ldap_config", 5);
+       close ($file1);
+       daemon_log("INFO: Wrote $ldap_config", 5);
 
        # Setup pam_ldap.conf / libnss-ldap.conf
-       open(file1, "> $pam_config");
-       open(file2, "> $nss_config");
-       print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
-       print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
-       print file1 "uri";
-       print file2 "uri";
+       open(my $file1, ">","$pam_config");
+       open(my $file2, ">","$nss_config");
+       print $file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
+       print $file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
+       print $file1 "uri";
+       print $file2 "uri";
        foreach $element (@ldap_uris) {
-               print file1 " $element";
-               print file2 " $element";
+               print $file1 " $element";
+               print $file2 " $element";
        }
-       print file1 "\nbase $ldap_base\n";
-       print file2 "\nbase $ldap_base\n";
+       print $file1 "\nbase $ldap_base\n";
+       print $file2 "\nbase $ldap_base\n";
        foreach $element (@pam_options) {
-               print file1 "$element\n";
+               print $file1 "$element\n";
        }
        foreach $element (@nss_options) {
-               print file2 "$element\n";
+               print $file2 "$element\n";
        }
-       close (file2);
-       daemon_log("wrote $nss_config", 5);
-       close (file1);
-       daemon_log("wrote $pam_config", 5);
+       close ($file2);
+       daemon_log("INFO: Wrote $nss_config", 5);
+       close ($file1);
+       daemon_log("INFO: Wrote $pam_config", 5);
 
        # Create goto.secrets if told so - for compatibility reasons
        if (defined $goto_admin){
-               open(file1, "> /etc/goto/secret");
-                       close(file1);
+               open(my $file1, ">","/etc/goto/secret");
+                       close($file1);
                        chown(0,0, "/etc/goto/secret");
                        chmod(0600, "/etc/goto/secret");
-               open(file1, "> /etc/goto/secret");
-                       print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
-                       close(file1);
-                       daemon_log("wrote /etc/goto/secret", 5);
+               open(my $file1, ">","/etc/goto/secret");
+                       print $file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
+                       close($file1);
+                       daemon_log("INFO: Wrote /etc/goto/secret", 5);
        }
 
        # Write shell based config
@@ -413,63 +415,72 @@ sub new_ldap_config {
     my $ldap_server= $ldap_uris[0];
     $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
 
-    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";
-    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);
+    open(my $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";
+    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("INFO: 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);
+           open(my $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("INFO: Wrote $cfg_name", 5);
     }
 
        # Set permissions and ownership structure of 
        chown(0, 0, $cfg_name);
        chmod(0644, $cfg_name);
 
+    # Allow custom scripts to be executed
+    if (defined $ldap_config_exit_hook) {
+        system($ldap_config_exit_hook);
+        daemon_log("Hook $ldap_config_exit_hook returned with code ".($? >> 8), 5);
+    }
+
     return;
 }
 
 
 sub new_key {
-    # my ($msg_hash) = @_ ;
+       # Create new key
     my $new_server_key = &main::create_passwd();
 
-    my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
-    my $out_msg = &create_xml_string($out_hash);
+       # Send new_key message to server
+    my $errSend = &main::send_msg_hash_to_target(
+               &main::create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key),
+               $main::server_address, 
+               $main::server_key,
+       );
 
-    # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg 
-    # comes from gosa-si-server within a given time
-    
+       # Set global key
+       if (not $errSend) {
+               $main::server_key = $new_server_key;
+       }
 
-    return $out_msg
+    return; 
 }
 
 
 sub confirm_new_key {
     my ($msg, $msg_hash) = @_ ;
-    my $header = @{$msg_hash->{'header'}}[0];
-    my $target = @{$msg_hash->{'target'}}[0];
     my $source = @{$msg_hash->{'source'}}[0];
 
     &main::daemon_log("confirm new key from $source", 5);