Code

wrong if-clause
[gosa.git] / gosa-si / server / events / siTriggered.pm
index 88cc39f1b1ce1605278dc00fa9a64140aa6a9ce8..544e2e6ab9c96ab9ac2c0b604781c585976c65fa 100644 (file)
@@ -5,6 +5,7 @@ my @events = (
     "got_ping",
     "detected_hardware",
     "trigger_wake",
+    "reload_ldap_config",
     );
 @EXPORT = @events;
 
@@ -24,7 +25,7 @@ my $ldap_uri;
 my $ldap_base;
 my $ldap_admin_dn;
 my $ldap_admin_password;
-my $gosa_unit_tag;
+my $mesg;
 
 my %cfg_defaults = (
 "server" => {
@@ -32,7 +33,6 @@ my %cfg_defaults = (
     "ldap-base" => [\$ldap_base, ""],
     "ldap-admin-dn" => [\$ldap_admin_dn, ""],
     "ldap-admin-password" => [\$ldap_admin_password, ""],
-       "gosa-unit-tag" => [\$gosa_unit_tag, ""],
     },
 );
 &read_configfile($main::cfg_file, %cfg_defaults);
@@ -47,7 +47,7 @@ sub read_configfile {
     my ($cfg_file, %cfg_defaults) = @_;
     my $cfg;
 
-    if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
+    if( defined( $cfg_file) && ( (-s $cfg_file) > 0 )) {
         if( -r $cfg_file ) {
             $cfg = Config::IniFiles->new( -file => $cfg_file );
         } else {
@@ -64,6 +64,18 @@ sub read_configfile {
     }
 }
 
+
+sub reload_ldap_config {
+    my ($msg, $msg_hash, $session_id) = @_;
+    my $header = @{$msg_hash->{header}}[0];
+    my $target = @{$msg_hash->{$header}}[0];
+
+    my $out_msg = &ClientPackages::new_ldap_config($target, $session_id);
+    my @out_msg_l = ( $out_msg );
+    return @out_msg_l;
+}
+
+
 sub got_ping {
     my ($msg, $msg_hash, $session_id) = @_;
 
@@ -107,39 +119,36 @@ sub got_ping {
 
 
 sub detected_hardware {
-    my ($msg, $msg_hash, $session_id) = @_ ;
+       my ($msg, $msg_hash, $session_id) = @_ ;
        my $address = $msg_hash->{source}[0];
        my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
 
-    my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
-    my $res = $main::known_clients_db->select_dbentry( $sql_statement );
+       my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
+       my $res = $main::known_clients_db->select_dbentry( $sql_statement );
 
-    # check hit
-    my $hit_counter = keys %{$res};
-    if( not $hit_counter == 1 ) {
-        &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
+       # check hit
+       my $hit_counter = keys %{$res};
+       if( not $hit_counter == 1 ) {
+               &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
                return;
-    }
-
-    my $macaddress = $res->{1}->{macaddress};
-    my $hostkey = $res->{1}->{hostkey};
+       }
 
-    if (not defined $macaddress) {
-        &main::daemon_log("ERROR: no mac address found for client $address", 1);
-        return;
-    }
-    # Build LDAP connection
-    my $ldap = Net::LDAP->new($ldap_uri);
-    if( not defined $ldap ) {
-        &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
-        return;
-    } 
+       my $macaddress = $res->{1}->{macaddress};
+       my $hostkey = $res->{1}->{hostkey};
 
-    # Bind to a directory with dn and password
-    my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
+       if (not defined $macaddress) {
+               &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
+               return;
+       }
+       # Build LDAP connection
+    my $ldap_handle = &main::get_ldap_handle($session_id);
+       if( not defined $ldap_handle ) {
+               &main::daemon_log("$session_id ERROR: cannot connect to ldap: $ldap_uri", 1);
+               return;
+       } 
 
-    # Perform search
-       $mesg = $ldap->search(
+       # Perform search
+       $mesg = $ldap_handle->search(
                base   => $ldap_base,
                scope  => 'sub',
                filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
@@ -147,11 +156,18 @@ sub detected_hardware {
 
        # We need to create a base entry first (if not done from ArpHandler)
        if($mesg->count == 0) {
-               &main::daemon_log("INFO: Need to create a new LDAP Entry for client $address", 6);
-               my $resolver=Net::DNS::Resolver->new;
+               &main::daemon_log("INFO: Need to create a new LDAP Entry for client $address", 4);
                my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
-               my $dnsresult= $resolver->search($ipaddress);
-               my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
+               my $dnsname;
+               if (defined($msg_hash->{'force-hostname'}) && 
+                       defined($msg_hash->{'force-hostname'}[0]) &&
+                       length($msg_hash->{'force-hostname'}[0]) > 0){
+                       $dnsname= $msg_hash->{'force-hostname'}[0];
+                       &main::daemon_log("INFO: Using forced hostname $dnsname for client $address", 4);
+               } else {
+                       $dnsname= gethostbyaddr(inet_aton($ipaddress), AF_INET) || $ipaddress;
+               }
+
                my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
                my $dn = "cn=$cn,ou=incoming,$ldap_base";
                &main::daemon_log("INFO: Creating entry for $dn",5);
@@ -163,25 +179,25 @@ sub detected_hardware {
                $entry->add("gotomode" => "locked");
                $entry->add("gotoSysStatus" => "new-system");
                $entry->add("ipHostNumber" => $ipaddress);
-               if(defined($gosa_unit_tag) && length($gosa_unit_tag) > 0) {
-                       $entry->add("objectClass" => "gosaAdministrativeUnit");
-                       $entry->add("gosaUnitTag" => $gosa_unit_tag);
+               if(defined($main::gosa_unit_tag) && length($main::gosa_unit_tag) > 0) {
+                       $entry->add("objectClass" => "gosaAdministrativeUnitTag");
+                       $entry->add("gosaUnitTag" => $main::gosa_unit_tag);
                }
-               my $res=$entry->update($ldap);
+               my $res=$entry->update($ldap_handle);
                if(defined($res->{'errorMessage'}) &&
                        length($res->{'errorMessage'}) >0) {
                        &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
                        return;
                } else {
                        # Fill $mesg again
-                       $mesg = $ldap->search(
+                       $mesg = $ldap_handle->search(
                                base   => $ldap_base,
                                scope  => 'sub',
                                filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
                        );
                }
        }
-       
+
        if($mesg->count == 1) {
                my $entry= $mesg->entry(0);
                $entry->changetype("modify");
@@ -192,7 +208,7 @@ sub detected_hardware {
                        if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
                                length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
                                if(defined($entry->get_value($attribute))) {
-                                       $entry->delete($attribute);
+                                       $entry->delete($attribute => []);
                                }
                                &main::daemon_log("INFO: Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},5);
                                $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});     
@@ -203,15 +219,15 @@ sub detected_hardware {
                        if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
                                length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
                                if(defined($entry->get_value($attribute))) {
-                                       $entry->delete($attribute);
+                                       $entry->delete($attribute => []);
                                }
-                               foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
+                               foreach my $array_entry (keys %{{map { $_ => 1 } sort(@{$msg_hash->{detected_hardware}[0]->{$attribute}}) }}) {
                                        $entry->add($attribute => $array_entry);
                                }
                        }
                }
 
-               my $res=$entry->update($ldap);
+               my $res=$entry->update($ldap_handle);
                if(defined($res->{'errorMessage'}) &&
                        length($res->{'errorMessage'}) >0) {
                        &main::daemon_log("ERROR: can not add entries to LDAP: ".$res->{'errorMessage'}, 1);
@@ -228,9 +244,9 @@ sub trigger_wake {
     my ($msg, $msg_hash, $session_id) = @_ ;
 
     foreach (@{$msg_hash->{macAddress}}){
-        &main::daemon_log("INFO: trigger wake for $_", 5);
-        my $host    = shift;
-        my $ipaddr  = shift || '255.255.255.255';
+        &main::daemon_log("$session_id INFO: trigger wake for $_", 5);
+        my $host    = $_;
+        my $ipaddr  = '255.255.255.255';
         my $port    = getservbyname('discard', 'udp');
 
         my ($raddr, $them, $proto);
@@ -239,20 +255,9 @@ sub trigger_wake {
         # get the hardware address (ethernet address)
         $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
         if ($host =~ m/^$hwaddr_re$/) {
-                $hwaddr = $host;
+          $hwaddr = $host;
         } else {
-                # $host is not a hardware address, try to resolve it
-                my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
-                my $ip_addr;
-                if ($host =~ m/^$ip_re$/) {
-                        $ip_addr = $host;
-                } else {
-                        my $h;
-                        unless ($h = gethost($host)) {
-                                return undef;
-                        }
-                        $ip_addr = inet_ntoa($h->addr);
-                }
+          &main::daemon_log("$session_id ERROR: trigger_wake called with non mac address", 1);
         }
 
         # Generate magic sequence
@@ -263,7 +268,7 @@ sub trigger_wake {
 
         # Allocate socket and send packet
 
-        $raddr = gethostbyname($ipaddr)->addr;
+        $raddr = gethostbyname($ipaddr);
         $them = pack_sockaddr_in($port, $raddr);
         $proto = getprotobyname('udp');