Code

Added multi actions
[gosa.git] / gosa-si / gosa-si-client
index c107dfc1e0a8b368c150cf77b94a8fc71f97535e..bde1ce2522f48dfc476788828ab0f8aa4da0b0d3 100755 (executable)
@@ -20,6 +20,7 @@
 
 use strict;
 use warnings;
+use utf8;
 use Getopt::Long;
 use Config::IniFiles;
 use POSIX;
@@ -201,6 +202,15 @@ sub check_pid {
 }
 
 
+sub sig_int_handler {
+    my ($signal) = @_;
+
+    daemon_log("shutting down gosa-si-server", 1);
+    exit(1);
+}
+$SIG{INT} = \&sig_int_handler;
+
+
 #===  FUNCTION  ================================================================
 #         NAME:  logging
 #   PARAMETERS:  level - string - default 'info' 
@@ -470,152 +480,6 @@ sub get_local_ip_for_remote_ip {
        return $result;
 }
 
-sub new_ldap_config {
-    my ($msg_hash) = @_ ;
-    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");
-    }
-
-    # 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);
-    }
-
-    
-
-    # 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 "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 generate_hw_digest {
        my $hw_data;
@@ -697,6 +561,8 @@ sub get_server_addresses {
     if( $error == 0 ) {
         foreach my $hit (@hits) {
             my ($hit_name, $hit_port) = split(/:/, $hit);
+                       chomp($hit_name);
+                       chomp($hit_port);
 
             my $address_query = $res->send($hit_name);
             if( 1 == length($address_query->answer) ) {
@@ -992,8 +858,6 @@ sub register_at_gosa_si_server {
 
 sub check_key_and_xml_validity {
     my ($crypted_msg, $module_key) = @_;
-#print STDERR "crypted_msg:$crypted_msg\n";
-#print STDERR "modul_key:$module_key\n";
 
     my $msg;
     my $msg_hash;
@@ -1283,6 +1147,30 @@ sub server_input {
     return;
 }
 
+sub get_dns_domains() {
+        my $line;
+        my @searches;
+        open(RESOLV, "</etc/resolv.conf");
+        while(<RESOLV>){
+                $line= $_;
+                chomp $line;
+                $line =~ s/^\s+//;
+                $line =~ s/\s+$//;
+                $line =~ s/\s+/ /;
+                if ($line =~ /^domain (.*)$/ ){
+                        push(@searches, $1);
+                } elsif ($line =~ /^search (.*)$/ ){
+                        push(@searches, split(/ /, $1));
+                }
+        }
+        close(RESOLV);
+
+        my %tmp = map { $_ => 1 } @searches;
+        @searches = sort keys %tmp;
+
+        return @searches;
+}
+
 #==== MAIN = main ==============================================================
 #  parse commandline options
 Getopt::Long::Configure( "bundling" );
@@ -1377,22 +1265,43 @@ $default_server_key = $server_key;
 
 
 # add gosa-si-server address from config file at first position of server list
-if (defined $server_address) {
-    unshift(@servers, $server_address);
-    my $servers_string = join(", ", @servers);
-    daemon_log("found servers in configuration file: $servers_string", 5);
-} 
-else {
-    if ( !$server_domain) {
-        daemon_log("ERROR: please specify a gosa-si-server address or a domain in config file", 1);
-        exit( 1 );
-    }
-    my @tmp_servers = &get_server_addresses($server_domain);
+my $server_check_cfg = Config::IniFiles->new( -file => $cfg_file );
+my $server_check = $server_check_cfg->val( "server", "ip");
+if( defined $server_check ) {
+       unshift(@servers, $server_address);
+       my $servers_string = join(", ", @servers);
+       daemon_log("INFO: found servers in configuration file: $servers_string", 5);
+} else {
+       my @tmp_servers;
+       if ( !$server_domain) {
+               # Try our DNS Searchlist
+               for my $domain(get_dns_domains()) {
+                       chomp($domain);
+                       my @tmp_domains= &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) {
+                       daemon_log("ERROR: please specify a gosa-si-server address or a domain in config file", 1);
+                       kill 2, $$;
+               }
+       } else {
+               @tmp_servers = &get_server_addresses($server_domain);
+               if( 0 == @tmp_servers ) {
+                       daemon_log("ERROR: no gosa-si-server found in DNS for domain '$server_domain'",1);
+                       daemon_log("ERROR: please specify a gosa-si-server address or a domain in config file", 1);
+                       kill 2, $$;
+               } 
+       }
+
     foreach my $server (@tmp_servers) { 
         unshift(@servers, $server); 
     }
     my $servers_string = join(", ", @servers);
-    daemon_log("found servers in DNS: $servers_string", 5);
+    daemon_log("INFO: found servers in DNS: $servers_string", 5);
 }