Code

Hard coded ldap-shell.conf. Need to place it somewhere else later.
[gosa.git] / gosa-si / client / events / corefunctions.pm
index b52677f4a6af7bb0cc6fcb747222995cd35d0084..70d3bff70d5ff16501bdb9ed69baa455d324ebfa 100644 (file)
@@ -1,7 +1,17 @@
 package corefunctions;
 use Exporter;
 @ISA = qw(Exporter);
-my @events = qw(get_events registered set_activated_for_installation new_ldap_config new_key generate_hw_digest detect_hardware reboot halt reinstall softupdate confirm_new_key);
+my @events = (
+    "get_events",
+    "registered",
+    "new_ldap_config",
+    "new_key",
+    "generate_hw_digest",
+    "detect_hardware",
+    "confirm_new_key",
+    "ping",
+    "import_events",
+    );
 @EXPORT = @events;
 
 use strict;
@@ -11,17 +21,16 @@ use Fcntl;
 use GOSA::GosaSupportDaemon;
 use File::Basename;
 
-
 my ($ldap_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
 
 
 my %cfg_defaults = (
     "client" => {
         "ldap" => [\$ldap_enabled, 1],
-        "ldap_config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
-        "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" => [\$ldap_config, "/etc/ldap/ldap.conf"],
+        "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"],
     },
 );
 
@@ -54,8 +63,12 @@ sub write_to_file {
     if( $error == 0 ) {
 
         chomp($string);
-    
-        open(FILE, ">> $file");
+            
+        if( not -f $file ) {
+            open (FILE, "$file");
+            close(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);
     }
@@ -74,38 +87,6 @@ sub daemon_log {
     return;
 }
 
-
-sub reboot {
-    my ($msg, $msg_hash) = @_ ;
-    &main::daemon_log("got reboot-msg: $msg", 5);
-    return;
-}
-
-
-sub halt {
-    my ($msg, $msg_hash) = @_ ;
-
-    &main::daemon_log("got halt-msg: $msg", 5);
-    return;
-}
-
-
-sub reinstall {
-    my ($msg, $msg_hash) = @_ ;
-    &main::daemon_log("got reinstall-msg: $msg", 5);
-
-    return;
-}
-
-
-sub softupdate {
-    my ($msg, $msg_hash) = @_ ;
-    &main::daemon_log("got softupdate-msg: $msg", 5);
-
-    return;
-}
-
-
 sub registered {
     my ($msg, $msg_hash) = @_ ;
 
@@ -113,6 +94,7 @@ sub registered {
     if( $header eq "registered" ) {
         my $source = @{$msg_hash->{'source'}}[0];
         &main::daemon_log("registration at $source",1);
+        $main::server_address = $source;
     }
 
     # set globaly variable client_address
@@ -121,27 +103,35 @@ sub registered {
 
     # set registration_flag to true 
     my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
+     # Write the MAC address to file
+    if(stat($main::opts_file)) { 
+            unlink($main::opts_file);
+    }
+
+    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";
+    print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
+    print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
+    if(defined(@{$msg_hash->{'ldap_available'}}) &&
+                  @{$msg_hash->{'ldap_available'}}[0] eq "true") {
+       print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
+       }
+    if(defined(@{$msg_hash->{'error'}})) {
+               my $errormsg= @{$msg_hash->{'error'}}[0];
+       print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
+               &write_to_file($errormsg, $fai_logpath);
+       }
+    close($opts_file_FH);
+     
     my $out_msg = &create_xml_string($out_hash);
     return $out_msg;
-
 }
 
-
-sub set_activated_for_installation {
-    my ($msg, $msg_hash) = @_ ;
-    my $header = @{$msg_hash->{'header'}}[0];
-    my $target = @{$msg_hash->{'target'}}[0];
-    my $source = @{$msg_hash->{'source'}}[0];
-
-    my $Datei = "/tmp/set_activated_for_installation";
-    open(DATEI, ">$Datei");
-    print DATEI "set_activated_for_installation\n";
-    close DATEI;
-    
-    return;
-}
-
-
 sub server_leaving {
     my ($msg_hash) = @_ ;
     my $source = @{$msg_hash->{'source'}}[0]; 
@@ -162,162 +152,173 @@ sub server_leaving {
 
 
 sub new_ldap_config {
-       my ($msg, $msg_hash) = @_ ;
-
-       if( $ldap_enabled != 1 ) {
-               return;
-       }
-
-       my $element;
-       my @ldap_uris;
-       my $ldap_base;
-       my @ldap_options;
-       my @pam_options;
-       my @nss_options;
-       my $goto_admin;
-       my $goto_secret;
-       my $admin_base= "";
-       my $department= "";
-       my $unit_tag;
-
-       # Transform input into array
-       while ( my ($key, $value) = each(%$msg_hash) ) {
-               if ($key =~ /^(source|target|header)$/) {
-                       next;
-               }
-
-               foreach $element (@$value) {
-                       if ($key =~ /^ldap_uri$/) {
-                               push (@ldap_uris, $element);
-                               next;
-                       }
-                       if ($key =~ /^ldap_base$/) {
-                               $ldap_base= $element;
-                               next;
-                       }
-                       if ($key =~ /^goto_admin$/) {
-                               $goto_admin= $element;
-                               next;
-                       }
-                       if ($key =~ /^goto_secret$/) {
-                               $goto_secret= $element;
-                               next;
-                       }
-                       if ($key =~ /^ldap_cfg$/) {
-                               push (@ldap_options, "$element");
-                               next;
-                       }
-                       if ($key =~ /^pam_cfg$/) {
-                               push (@pam_options, "$element");
-                               next;
-                       }
-                       if ($key =~ /^nss_cfg$/) {
-                               push (@nss_options, "$element");
-                               next;
-                       }
-                       if ($key =~ /^admin_base$/) {
-                               $admin_base= $element;
-                               next;
-                       }
-                       if ($key =~ /^department$/) {
-                               $department= $element;
-                               next;
-                       }
-                       if ($key =~ /^unit_tag$/) {
-                               $unit_tag= $element;
-                               next;
-                       }
-               }
-       }
-
-       # Unit tagging enabled?
-       if (defined $unit_tag){
-               push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
-               push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
-               push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
-       }
-
-       # Sanity check
-       if(! ref($ldap_base) eq '') {
-               daemon_log("Received empty LDAP config. Don't write anything.", 1);
-               return;
-       }
+    my ($msg, $msg_hash) = @_ ;
 
-       # 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";
-       foreach $element (@ldap_uris) {
-               print file1 " $element";
-       }
-       print file1 "\nBASE $ldap_base\n";
-       foreach $element (@ldap_options) {
-               print file1 "$element\n";
-       }
-       close (file1);
-       daemon_log("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";
-       foreach $element (@ldap_uris) {
-               print file1 " $element";
-               print file2 " $element";
-       }
-       print file1 "\nbase $ldap_base\n";
-       print file2 "\nbase $ldap_base\n";
-       foreach $element (@pam_options) {
-               print file1 "$element\n";
-       }
-       foreach $element (@nss_options) {
-               print file2 "$element\n";
-       }
-       close (file2);
-       daemon_log("wrote $nss_config", 5);
-       close (file1);
-       daemon_log("wrote $pam_config", 5);
-
-       # Create goto.secrets if told so - for compatibility reasons
-       if (defined $goto_admin){
-               open(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);
-       }
+    if( $ldap_enabled != 1 ) {
+           return;
+    }
 
+    my $element;
+    my @ldap_uris;
+    my $ldap_base;
+    my @ldap_options;
+    my @pam_options;
+    my @nss_options;
+    my $goto_admin;
+    my $goto_secret;
+    my $admin_base= "";
+    my $department= "";
+    my $release= "";
+    my $unit_tag;
+
+    # Transform input into array
+    while ( my ($key, $value) = each(%$msg_hash) ) {
+        if ($key =~ /^(source|target|header)$/) {
+                next;
+        }
+
+        foreach $element (@$value) {
+                if ($key =~ /^ldap_uri$/) {
+                        push (@ldap_uris, $element);
+                        next;
+                }
+                if ($key =~ /^ldap_base$/) {
+                        $ldap_base= $element;
+                        next;
+                }
+                if ($key =~ /^goto_admin$/) {
+                        $goto_admin= $element;
+                        next;
+                }
+                if ($key =~ /^goto_secret$/) {
+                        $goto_secret= $element;
+                        next;
+                }
+                if ($key =~ /^ldap_cfg$/) {
+                        push (@ldap_options, "$element");
+                        next;
+                }
+                if ($key =~ /^pam_cfg$/) {
+                        push (@pam_options, "$element");
+                        next;
+                }
+                if ($key =~ /^nss_cfg$/) {
+                        push (@nss_options, "$element");
+                        next;
+                }
+                if ($key =~ /^admin_base$/) {
+                        $admin_base= $element;
+                        next;
+                }
+                if ($key =~ /^department$/) {
+                        $department= $element;
+                        next;
+                }
+                if ($key =~ /^unit_tag$/) {
+                        $unit_tag= $element;
+                        next;
+                }
+                if ($key =~ /^release$/) {
+                        $release= $element;
+                        next;
+                }
+        }
+    }
 
+    # Unit tagging enabled?
+    if (defined $unit_tag){
+            push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
+            push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
+            push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
+    }
 
-       # Write shell based config
-       my $cfg_name= dirname($ldap_config)."/ldap-shell.conf";
-       open(file1, "> $cfg_name");
-       print file1 "LDAP_BASE=\"$ldap_base\"\n";
-       print file1 "ADMIN_BASE=\"$admin_base\"\n";
-       print file1 "DEPARTMENT=\"$department\"\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);
+    # Setup ldap.conf
+    my $file1;
+    my $file2;
+    open(file1, "> $ldap_config");
+#print STDERR Dumper(PerlIO::get_layers(file1, output => 1));
+    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 "\nBASE $ldap_base\n";
+    foreach $element (@ldap_options) {
+        print file1 "$element\n";
+    }
+    close (file1);
+    daemon_log("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";
+    foreach $element (@ldap_uris) {
+        print file1 " $element";
+        print file2 " $element";
+    }
+    print file1 "\nbase $ldap_base\n";
+    print file2 "\nbase $ldap_base\n";
+    foreach $element (@pam_options) {
+        print file1 "$element\n";
+    }
+    foreach $element (@nss_options) {
+        print file2 "$element\n";
+    }
+    close (file2);
+    daemon_log("wrote $nss_config", 5);
+    close (file1);
+    daemon_log("wrote $pam_config", 5);
+
+    # Create goto.secrets if told so - for compatibility reasons
+    if (defined $goto_admin){
+           open(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);
+    }
 
-       return;
+    # Write shell based config
+    my $cfg_name= "/etc/ldap/ldap-shell.conf";
+
+    # Get first LDAP server
+    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);
 
+    return;
 }
 
+
 sub new_key {
     # my ($msg_hash) = @_ ;
     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);
+
+    # 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
+    
+
     return $out_msg; 
 }
 
@@ -460,13 +461,15 @@ sub ping {
     my $header = @{$msg_hash->{'header'}}[0];
     my $source = @{$msg_hash->{'source'}}[0];
     my $target = @{$msg_hash->{'target'}}[0];
+    my $session_id = @{$msg_hash->{'session_id'}}[0];
+
    
     # 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);
     return $out_msg;
 
 }
 
-
 1;