Code

ce7fdfc3f760c818ea1ff4660904784e6f7218f8
[gosa.git] / gosa-si / modules / SIPackages.pm
1 package SIPackages;
3 use Exporter;
4 @ISA = ("Exporter");
6 # Each module has to have a function 'process_incoming_msg'. This function works as a interface to gosa-sd and receives the msg hash from gosa-sd. 'process_incoming_function checks, wether it has a function to process the incoming msg and forward the msg to it. 
9 use strict;
10 use warnings;
11 use GOSA::GosaSupportDaemon;
12 use IO::Socket::INET;
13 use XML::Simple;
14 use Data::Dumper;
15 use Net::LDAP;
16 use Socket;
17 use Net::hostent;
19 BEGIN{}
20 END {}
22 my ($known_clients_file_name);
23 my ($server_activ, $server_ip, $server_mac_address, $server_port, $server_passwd, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
24 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
25 my $server;
26 my $network_interface;
27 my $no_bus;
28 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
31 my %cfg_defaults =
32 (
33 "server" =>
34     {"server_activ" => [\$server_activ, "on"],
35     "server_ip" => [\$server_ip, "0.0.0.0"],
36     "server_mac_address" => [\$server_mac_address, ""],
37     "server_port" => [\$server_port, "20081"],
38     "server_passwd" => [\$server_passwd, ""],
39     "max_clients" => [\$max_clients, 100],
40     "ldap_uri" => [\$ldap_uri, ""],
41     "ldap_base" => [\$ldap_base, ""],
42     "ldap_admin_dn" => [\$ldap_admin_dn, ""],
43     "ldap_admin_password" => [\$ldap_admin_password, ""],
44     },
45 "bus" =>
46     {"bus_activ" => [\$bus_activ, "on"],
47     "bus_passwd" => [\$bus_passwd, ""],
48     "bus_ip" => [\$bus_ip, ""],
49     "bus_port" => [\$bus_port, "20080"],
50     },
51 );
53 ### START #####################################################################
55 # read configfile and import variables
56 &read_configfile();
58 # detect interfaces and mac address
59 $network_interface= &get_interface_for_ip($server_ip);
60 $server_mac_address= &get_mac($network_interface); 
62 # complete addresses
63 if( $server_ip eq "0.0.0.0" ) {
64     $server_ip = "127.0.0.1";
65 }
66 my $server_address = "$server_ip:$server_port";
67 my $bus_address = "$bus_ip:$bus_port";
69 # create general settings for this module
70 my $xml = new XML::Simple();
72 # register at bus
73 if ($main::no_bus > 0) {
74     $bus_activ = "off"
75 }
76 if($bus_activ eq "on") {
77     &register_at_bus();
78 }
80 ### functions #################################################################
83 sub get_module_info {
84     my @info = ($server_address,
85                 $server_passwd,
86                 $server,
87                 $server_activ,
88                 "socket",
89                 );
90     return \@info;
91 }
95 sub do_wake {
96         my $host    = shift;
97         my $ipaddr  = shift || '255.255.255.255';
98         my $port    = getservbyname('discard', 'udp');
100         my ($raddr, $them, $proto);
101         my ($hwaddr, $hwaddr_re, $pkt);
103         # get the hardware address (ethernet address)
105         $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
106         if ($host =~ m/^$hwaddr_re$/) {
107                 $hwaddr = $host;
108         } else {
109                 # $host is not a hardware address, try to resolve it
110                 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
111                 my $ip_addr;
112                 if ($host =~ m/^$ip_re$/) {
113                         $ip_addr = $host;
114                 } else {
115                         my $h;
116                         unless ($h = gethost($host)) {
117                                 return undef;
118                         }
119                         $ip_addr = inet_ntoa($h->addr);
120                 }
121         }
123         # Generate magic sequence
124         foreach (split /:/, $hwaddr) {
125                 $pkt .= chr(hex($_));
126         }
127         $pkt = chr(0xFF) x 6 . $pkt x 16;
129         # Allocate socket and send packet
131         $raddr = gethostbyname($ipaddr)->addr;
132         $them = pack_sockaddr_in($port, $raddr);
133         $proto = getprotobyname('udp');
135         socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
136         setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
138         send(S, $pkt, 0, $them) or die "send : $!";
139         close S;
143 #===  FUNCTION  ================================================================
144 #         NAME:  read_configfile
145 #   PARAMETERS:  cfg_file - string -
146 #      RETURNS:  nothing
147 #  DESCRIPTION:  read cfg_file and set variables
148 #===============================================================================
149 sub read_configfile {
150     my $cfg;
151     if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
152         if( -r $main::cfg_file ) {
153             $cfg = Config::IniFiles->new( -file => $main::cfg_file );
154         } else {
155             print STDERR "Couldn't read config file!";
156         }
157     } else {
158         $cfg = Config::IniFiles->new() ;
159     }
160     foreach my $section (keys %cfg_defaults) {
161         foreach my $param (keys %{$cfg_defaults{ $section }}) {
162             my $pinfo = $cfg_defaults{ $section }{ $param };
163             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
164         }
165     }
167     # Read non predefined sections
168     my $param;
169     if ($cfg->SectionExists('ldap')){
170                 foreach $param ($cfg->Parameters('ldap')){
171                         push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
172                 }
173     }
174     if ($cfg->SectionExists('pam_ldap')){
175                 foreach $param ($cfg->Parameters('pam_ldap')){
176                         push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
177                 }
178     }
179     if ($cfg->SectionExists('nss_ldap')){
180                 foreach $param ($cfg->Parameters('nss_ldap')){
181                         push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
182                 }
183     }
184     if ($cfg->SectionExists('goto')){
185         $goto_admin= $cfg->val('goto', 'terminal_admin');
186         $goto_secret= $cfg->val('goto', 'terminal_secret');
187     } else {
188         $goto_admin= undef;
189         $goto_secret= undef;
190     }
194 #===  FUNCTION  ================================================================
195 #         NAME:  get_interface_for_ip
196 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
197 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
198 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
199 #===============================================================================
200 sub get_interface_for_ip {
201         my $result;
202         my $ip= shift;
203         if ($ip && length($ip) > 0) {
204                 my @ifs= &get_interfaces();
205                 if($ip eq "0.0.0.0") {
206                         $result = "all";
207                 } else {
208                         foreach (@ifs) {
209                                 my $if=$_;
210                                 if(get_ip($if) eq $ip) {
211                                         $result = $if;
212                                 }
213                         }       
214                 }
215         }       
216         return $result;
219 #===  FUNCTION  ================================================================
220 #         NAME:  get_interfaces 
221 #   PARAMETERS:  none
222 #      RETURNS:  (list of interfaces) 
223 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
224 #===============================================================================
225 sub get_interfaces {
226         my @result;
227         my $PROC_NET_DEV= ('/proc/net/dev');
229         open(PROC_NET_DEV, "<$PROC_NET_DEV")
230                 or die "Could not open $PROC_NET_DEV";
232         my @ifs = <PROC_NET_DEV>;
234         close(PROC_NET_DEV);
236         # Eat first two line
237         shift @ifs;
238         shift @ifs;
240         chomp @ifs;
241         foreach my $line(@ifs) {
242                 my $if= (split /:/, $line)[0];
243                 $if =~ s/^\s+//;
244                 push @result, $if;
245         }
247         return @result;
250 #===  FUNCTION  ================================================================
251 #         NAME:  get_mac 
252 #   PARAMETERS:  interface name (i.e. eth0)
253 #      RETURNS:  (mac address) 
254 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
255 #===============================================================================
256 sub get_mac {
257         my $ifreq= shift;
258         my $result;
259         if ($ifreq && length($ifreq) > 0) { 
260                 if($ifreq eq "all") {
261                         $result = "00:00:00:00:00:00";
262                 } else {
263                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
265                         # A configured MAC Address should always override a guessed value
266                         if ($server_mac_address and length($server_mac_address) > 0) {
267                                 $result= $server_mac_address;
268                         }
270                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
271                                 or die "socket: $!";
273                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
274                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
276                                 if (length($mac) > 0) {
277                                         $mac=~ m/^([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$/;
278                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
279                                         $result = $mac;
280                                 }
281                         }
282                 }
283         }
284         return $result;
287 #===  FUNCTION  ================================================================
288 #         NAME:  get_ip 
289 #   PARAMETERS:  interface name (i.e. eth0)
290 #      RETURNS:  (ip address) 
291 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
292 #===============================================================================
293 sub get_ip {
294         my $ifreq= shift;
295         my $result= "";
296         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
297         my $proto= getprotobyname('ip');
299         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
300                 or die "socket: $!";
302         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
303                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
304                 my ($port, $addr) = sockaddr_in $sin;
305                 my $ip            = inet_ntoa $addr;
307                 if ($ip && length($ip) > 0) {
308                         $result = $ip;
309                 }
310         }
312         return $result;
316 #===  FUNCTION  ================================================================
317 #         NAME:  register_at_bus
318 #   PARAMETERS:  nothing
319 #      RETURNS:  nothing
320 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
321 #===============================================================================
322 sub register_at_bus {
324     # add bus to known_server_db
325     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
326                                                     primkey=>'hostname',
327                                                     hostname=>$bus_address,
328                                                     status=>'bus',
329                                                     hostkey=>$bus_passwd,
330                                                     timestamp=>&get_time,
331                                                 } );
332     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
333     my $answer = "";
334     $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
335     if ($answer == 0) {
336         &main::daemon_log("register at bus: $bus_address", 1);
337     } else {
338         &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
339     }
340     return;
344 #===  FUNCTION  ================================================================
345 #         NAME:  process_incoming_msg
346 #   PARAMETERS:  crypted_msg - string - incoming crypted message
347 #      RETURNS:  nothing
348 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
349 #===============================================================================
350 sub process_incoming_msg {
351     my ($msg, $msg_hash) = @_ ;
352     my $error = 0;
353     my $host_name;
354     my $host_key;
355     my @out_msg_l;
357     # process incoming msg
358     my $header = @{$msg_hash->{header}}[0]; 
359     my $source = @{$msg_hash->{source}}[0];
360     my @target_l = @{$msg_hash->{target}};
362     &main::daemon_log("SIPackages: msg to process: $header", 3);
363     &main::daemon_log("$msg", 8);
365     if( 0 == length @target_l){     
366         &main::daemon_log("ERROR: no target specified for msg $header", 1);
367         $error++;
368     }
370     if( 1 == length @target_l) {
371         my $target = $target_l[0];
372         if( $target eq $server_address ) {  
373             if ($header eq 'new_passwd') {
374                 @out_msg_l = &new_passwd($msg_hash)
375             } elsif ($header eq 'here_i_am') {
376                 @out_msg_l = &here_i_am($msg_hash)
377             } elsif ($header eq 'who_has') {
378                 @out_msg_l = &who_has($msg_hash)
379             } elsif ($header eq 'who_has_i_do') {
380                 @out_msg_l = &who_has_i_do($msg_hash)
381             } elsif ($header eq 'got_ping') {
382                 @out_msg_l = &got_ping($msg_hash)
383             } elsif ($header eq 'get_load') {
384                 @out_msg_l = &execute_actions($msg_hash)
385             } elsif ($header eq 'detected_hardware') {
386                 @out_msg_l = &process_detected_hardware($msg_hash)
387             } elsif ($header eq 'trigger_wake') {
388                 my $in_hash= &transform_msg2hash($msg);
389                 foreach (@{$in_hash->{macAddress}}){
390                     &main::daemon_log("SIPackages: trigger wake for $_", 1);
391                     do_wake($_);
392                 }
394             } else {
395                 &main::daemon_log("ERROR: $header is an unknown core function", 1);
396                 $error++;
397             }
398         }
399                 else {
400                         &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
401                         push(@out_msg_l, $msg);
402                 }
403     }
405     if( $error == 0) {
406         if( 0 == @out_msg_l ) {
407                         push(@out_msg_l, $msg);
408         }
409     }
410     
411     return \@out_msg_l;
415 #===  FUNCTION  ================================================================
416 #         NAME:  got_ping
417 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
418 #      RETURNS:  nothing
419 #  DESCRIPTION:  process this incoming message
420 #===============================================================================
421 sub got_ping {
422     my ($msg_hash) = @_;
423     
424     my $source = @{$msg_hash->{source}}[0];
425     my $target = @{$msg_hash->{target}}[0];
426     my $header = @{$msg_hash->{header}}[0];
427     
428     if(exists $main::known_daemons->{$source}) {
429         &main::add_content2known_daemons(hostname=>$source, status=>$header);
430     } else {
431         &main::add_content2known_clients(hostname=>$source, status=>$header);
432     }
433     
434     return;
438 #===  FUNCTION  ================================================================
439 #         NAME:  new_passwd
440 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
441 #      RETURNS:  nothing
442 #  DESCRIPTION:  process this incoming message
443 #===============================================================================
444 sub new_passwd {
445     my ($msg_hash) = @_;
446     my @out_msg_l;
447     
448     my $header = @{$msg_hash->{header}}[0];
449     my $source_name = @{$msg_hash->{source}}[0];
450     my $source_key = @{$msg_hash->{new_passwd}}[0];
451     my $query_res;
453     # check known_clients_db
454     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
455     $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
456     if( 1 == keys %{$query_res} ) {
457         my $act_time = &get_time;
458         my $sql_statement= "UPDATE known_clients ".
459             "SET hostkey='$source_key', timestamp='$act_time' ".
460             "WHERE hostname='$source_name'";
461         my $res = $main::known_clients_db->update_dbentry( $sql_statement );
463         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
464         my $out_msg = &create_xml_string($hash);
465         push(@out_msg_l, $out_msg);
466     }
468     # only do if host still not found
469     if( 0 == @out_msg_l ) {
470         # check known_server_db
471         $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
472         $query_res = $main::known_server_db->select_dbentry( $sql_statement );
473         if( 1 == keys %{$query_res} ) {
474             my $act_time = &get_time;
475             my $sql_statement= "UPDATE known_server ".
476                 "SET hostkey='$source_key', timestamp='$act_time' ".
477                 "WHERE hostname='$source_name'";
478             my $res = $main::known_server_db->update_dbentry( $sql_statement );
480             my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
481             my $out_msg = &create_xml_string($hash);
482             push(@out_msg_l, $out_msg);
483         }
484     }
486     return @out_msg_l;
490 #===  FUNCTION  ================================================================
491 #         NAME:  here_i_am
492 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
493 #      RETURNS:  nothing
494 #  DESCRIPTION:  process this incoming message
495 #===============================================================================
496 sub here_i_am {
497     my ($msg_hash) = @_;
498     my @out_msg_l;
499     my $out_hash;
501     my $source = @{$msg_hash->{source}}[0];
502     my $mac_address = @{$msg_hash->{mac_address}}[0];
503         my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
505     # number of known clients
506     my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
508     # check wether client address or mac address is already known
509     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
510     my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
511     
512     if ( 1 == keys %{$db_res} ) {
513         &main::daemon_log("WARNING: $source is already known as a client", 1);
514         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
515         $nu_clients --;
516     }
518     # number of actual activ clients
519     my $act_nu_clients = $nu_clients;
521     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
522     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
524     if($max_clients <= $act_nu_clients) {
525         my $out_hash = &create_xml_hash("denied", $server_address, $source);
526         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
527         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
528         &send_msg_hash2address($out_hash, $source, $passwd);
529         return;
530     }
531     
532     # new client accepted
533     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
535     # create entry in known_clients
536     my $events = @{$msg_hash->{events}}[0];
537     
539     # add entry to known_clients_db
540     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
541                                                 primkey=>'hostname',
542                                                 hostname=>$source,
543                                                 events=>$events,
544                                                 macaddress=>$mac_address,
545                                                 status=>'registered',
546                                                 hostkey=>$new_passwd,
547                                                 timestamp=>&get_time,
548                                                 } );
550     if ($res != 0)  {
551         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
552         return;
553     }
554     
555     # return acknowledgement to client
556     $out_hash = &create_xml_hash("registered", $server_address, $source);
557     my $register_out = &create_xml_string($out_hash);
558     push(@out_msg_l, $register_out);
560     # notify registered client to bus
561     if( $bus_activ eq "on") {
562         # fetch actual bus key
563         my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
564         my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
565         my $hostkey = $query_res->{1}->{'hostkey'};
567         # send update msg to bus
568         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
569         my $new_client_out = &create_xml_string($out_hash);
570         push(@out_msg_l, $new_client_out);
571         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
572     }
574     # give the new client his ldap config
575     my $new_ldap_config_out = &new_ldap_config($source);
576     if( $new_ldap_config_out ) {
577         push(@out_msg_l, $new_ldap_config_out);
578     }
580         my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
581         if( $hardware_config_out ) {
582                 push(@out_msg_l, $hardware_config_out);
583         }
585     return @out_msg_l;
589 #===  FUNCTION  ================================================================
590 #         NAME:  who_has
591 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
592 #      RETURNS:  nothing 
593 #  DESCRIPTION:  process this incoming message
594 #===============================================================================
595 sub who_has {
596     my ($msg_hash) = @_ ;
597     
598     # what is your search pattern
599     my $search_pattern = @{$msg_hash->{who_has}}[0];
600     my $search_element = @{$msg_hash->{$search_pattern}}[0];
601     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
603     # scanning known_clients for search_pattern
604     my @host_addresses = keys %$main::known_clients;
605     my $known_clients_entries = length @host_addresses;
606     my $host_address;
607     foreach my $host (@host_addresses) {
608         my $client_element = $main::known_clients->{$host}->{$search_pattern};
609         if ($search_element eq $client_element) {
610             $host_address = $host;
611             last;
612         }
613     }
614         
615     # search was successful
616     if (defined $host_address) {
617         my $source = @{$msg_hash->{source}}[0];
618         my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
619         &add_content2xml_hash($out_msg, "mac_address", $search_element);
620         &send_msg_hash2address($out_msg, $bus_address);
621     }
622     return;
626 sub who_has_i_do {
627     my ($msg_hash) = @_ ;
628     my $header = @{$msg_hash->{header}}[0];
629     my $source = @{$msg_hash->{source}}[0];
630     my $search_param = @{$msg_hash->{$header}}[0];
631     my $search_value = @{$msg_hash->{$search_param}}[0];
632     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
635 #===  FUNCTION  ================================================================
636 #         NAME:  new_ldap_config
637 #   PARAMETERS:  address - string - ip address and port of a host
638 #      RETURNS:  nothing
639 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
640 #===============================================================================
641 sub new_ldap_config {
642         my ($address) = @_ ;
644         my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
645         my $res = $main::known_clients_db->select_dbentry( $sql_statement );
647         # check hit
648         my $hit_counter = keys %{$res};
649         if( not $hit_counter == 1 ) {
650                 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
651         }
653         my $macaddress = $res->{1}->{macaddress};
654         my $hostkey = $res->{1}->{hostkey};
656         if (not defined $macaddress) {
657                 &main::daemon_log("ERROR: no mac address found for client $address", 1);
658                 return;
659         }
661         # Build LDAP connection
662         my $ldap = Net::LDAP->new($ldap_uri);
663         if( not defined $ldap ) {
664                 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
665                 return;
666         } 
669         # Bind to a directory with dn and password
670         my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
672         # Perform search
673         $mesg = $ldap->search( base   => $ldap_base,
674                 scope  => 'sub',
675                 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
676                 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
677         #$mesg->code && die $mesg->error;
678         if($mesg->code) {
679                 &main::daemon_log($mesg->error, 1);
680                 return;
681         }
683         # Sanity check
684         if ($mesg->count != 1) {
685                 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
686                 &main::daemon_log("\tbase: $ldap_base", 1);
687                 &main::daemon_log("\tscope: sub", 1);
688                 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
689                 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
690                 return;
691         }
693         my $entry= $mesg->entry(0);
694         my $dn= $entry->dn;
695         my @servers= $entry->get_value("gotoLdapServer");
696         my $unit_tag= $entry->get_value("gosaUnitTag");
697         my @ldap_uris;
698         my $server;
699         my $base;
701         # Do we need to look at an object class?
702         if (length(@servers) < 1){
703                 $mesg = $ldap->search( base   => $ldap_base,
704                         scope  => 'sub',
705                         attrs => ['dn', 'gotoLdapServer'],
706                         filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
707                 #$mesg->code && die $mesg->error;
708                 if($mesg->code) {
709                         &main::daemon_log($mesg->error, 1);
710                         return;
711                 }
713                 # Sanity check
714                 if ($mesg->count != 1) {
715                         &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
716                         return;
717                 }
719                 $entry= $mesg->entry(0);
720                 $dn= $entry->dn;
721                 @servers= $entry->get_value("gotoLdapServer");
722         }
724         @servers= sort (@servers);
726         foreach $server (@servers){
727                 $base= $server;
728                 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
729                 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
730                 push (@ldap_uris, $server);
731         }
733         # Assemble data package
734         my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
735                 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
737         # Need to append GOto settings?
738         if (defined $goto_admin and defined $goto_secret){
739                 $data{'goto_admin'}= $goto_admin;
740                 $data{'goto_secret'}= $goto_secret;
741         }
743         # Append unit tag if needed
744         if (defined $unit_tag){
746                 # Find admin base and department name
747                 $mesg = $ldap->search( base   => $ldap_base,
748                         scope  => 'sub',
749                         attrs => ['dn', 'ou'],
750                         filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
751                 #$mesg->code && die $mesg->error;
752                 if($mesg->code) {
753                         &main::daemon_log($mesg->error, 1);
754                         return;
755                 }
757                 # Sanity check
758                 if ($mesg->count != 1) {
759                         &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
760                         return;
761                 }
763                 $entry= $mesg->entry(0);
764                 $data{'admin_base'}= $entry->dn;
765                 $data{'department'}= $entry->get_value("ou");
767                 # Append unit Tag
768                 $data{'unit_tag'}= $unit_tag;
769         }
771         # Unbind
772         $mesg = $ldap->unbind;
774         # Send information
775         return send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
778 sub process_detected_hardware {
779         my $msg_hash = shift;
782         return;
784 #===  FUNCTION  ================================================================
785 #         NAME:  hardware_config
786 #   PARAMETERS:  address - string - ip address and port of a host
787 #      RETURNS:  
788 #  DESCRIPTION:  
789 #===============================================================================
790 sub hardware_config {
791     my ($address, $gotoHardwareChecksum, $detectedHardware) = @_ ;
792     
793     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
794     my $res = $main::known_clients_db->select_dbentry( $sql_statement );
796     # check hit
797     my $hit_counter = keys %{$res};
798     if( not $hit_counter == 1 ) {
799         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
800     }
802     my $macaddress = $res->{1}->{macaddress};
803     my $hostkey = $res->{1}->{hostkey};
805     if (not defined $macaddress) {
806         &main::daemon_log("ERROR: no mac address found for client $address", 1);
807         return;
808     }
810     # Build LDAP connection
811     my $ldap = Net::LDAP->new($ldap_uri);
812     if( not defined $ldap ) {
813         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
814         return;
815     } 
818     # Bind to a directory with dn and password
819     my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
821     # Perform search
822     $mesg = $ldap->search( base   => $ldap_base,
823                     scope  => 'sub',
824                     attrs => ['dn', 'gotoHardwareChecksum'],
825                     filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
826                 #$mesg->code && die $mesg->error;
828         #my $entry= $mesg->entry(0);
829         #my $dn= $entry->dn;
830         #my @servers= $entry->get_value("gotoHardwareChecksum");
832     # Assemble data package
833     my %data = ();
835     # Need to append GOto settings?
836     if (defined $goto_admin and defined $goto_secret){
837             $data{'goto_admin'}= $goto_admin;
838             $data{'goto_secret'}= $goto_secret;
839     }
841     # Unbind
842     $mesg = $ldap->unbind;
844         &main::daemon_log("Send detect_hardware message to $address", 4);
845     # Send information
846     return send_msg("detect_hardware", $server_address, $address, \%data, $hostkey);
850 #===  FUNCTION  ================================================================
851 #         NAME:  execute_actions
852 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
853 #      RETURNS:  nothing
854 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
855 #                /etc/gosad/actions
856 #===============================================================================
857 sub execute_actions {
858     my ($msg_hash) = @_ ;
859     my $configdir= '/etc/gosad/actions/';
860     my $result;
862     my $header = @{$msg_hash->{header}}[0];
863     my $source = @{$msg_hash->{source}}[0];
864     my $target = @{$msg_hash->{target}}[0];
865  
866     if((not defined $source)
867             && (not defined $target)
868             && (not defined $header)) {
869         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
870     } else {
871         my $parameters="";
872         my @params = @{$msg_hash->{$header}};
873         my $params = join(", ", @params);
874         &main::daemon_log("execute_actions: got parameters: $params", 5);
876         if (@params) {
877             foreach my $param (@params) {
878                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
879                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
880                 $parameters.= " ".$param_value;
881             }
882         }
884         my $cmd= $configdir.$header."$parameters";
885         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
886         $result= "";
887         open(PIPE, "$cmd 2>&1 |");
888         while(<PIPE>) {
889             $result.=$_;
890         }
891         close(PIPE);
892     }
894     # process the event result
897     return;
901 1;