Code

bugfix: server - bus communication
[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, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
24 my ($bus_activ, $bus_key, $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     "SIPackages_key" => [\$SIPackages_key, ""],
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_key, ""],
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 # add myself to known_server_db
81 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
82         primkey=>'hostname',
83         hostname=>$server_address,
84         status=>'myself',
85         hostkey=>$SIPackages_key,
86         timestamp=>&get_time,
87         } );
91 ### functions #################################################################
94 sub get_module_info {
95     my @info = ($server_address,
96                 $SIPackages_key,
97                 $server,
98                 $server_activ,
99                 "socket",
100                 );
101     return \@info;
106 sub do_wake {
107         my $host    = shift;
108         my $ipaddr  = shift || '255.255.255.255';
109         my $port    = getservbyname('discard', 'udp');
111         my ($raddr, $them, $proto);
112         my ($hwaddr, $hwaddr_re, $pkt);
114         # get the hardware address (ethernet address)
116         $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
117         if ($host =~ m/^$hwaddr_re$/) {
118                 $hwaddr = $host;
119         } else {
120                 # $host is not a hardware address, try to resolve it
121                 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
122                 my $ip_addr;
123                 if ($host =~ m/^$ip_re$/) {
124                         $ip_addr = $host;
125                 } else {
126                         my $h;
127                         unless ($h = gethost($host)) {
128                                 return undef;
129                         }
130                         $ip_addr = inet_ntoa($h->addr);
131                 }
132         }
134         # Generate magic sequence
135         foreach (split /:/, $hwaddr) {
136                 $pkt .= chr(hex($_));
137         }
138         $pkt = chr(0xFF) x 6 . $pkt x 16;
140         # Allocate socket and send packet
142         $raddr = gethostbyname($ipaddr)->addr;
143         $them = pack_sockaddr_in($port, $raddr);
144         $proto = getprotobyname('udp');
146         socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
147         setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
149         send(S, $pkt, 0, $them) or die "send : $!";
150         close S;
154 #===  FUNCTION  ================================================================
155 #         NAME:  read_configfile
156 #   PARAMETERS:  cfg_file - string -
157 #      RETURNS:  nothing
158 #  DESCRIPTION:  read cfg_file and set variables
159 #===============================================================================
160 sub read_configfile {
161     my $cfg;
162     if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
163         if( -r $main::cfg_file ) {
164             $cfg = Config::IniFiles->new( -file => $main::cfg_file );
165         } else {
166             print STDERR "Couldn't read config file!";
167         }
168     } else {
169         $cfg = Config::IniFiles->new() ;
170     }
171     foreach my $section (keys %cfg_defaults) {
172         foreach my $param (keys %{$cfg_defaults{ $section }}) {
173             my $pinfo = $cfg_defaults{ $section }{ $param };
174             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
175         }
176     }
178     # Read non predefined sections
179     my $param;
180     if ($cfg->SectionExists('ldap')){
181                 foreach $param ($cfg->Parameters('ldap')){
182                         push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
183                 }
184     }
185     if ($cfg->SectionExists('pam_ldap')){
186                 foreach $param ($cfg->Parameters('pam_ldap')){
187                         push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
188                 }
189     }
190     if ($cfg->SectionExists('nss_ldap')){
191                 foreach $param ($cfg->Parameters('nss_ldap')){
192                         push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
193                 }
194     }
195     if ($cfg->SectionExists('goto')){
196         $goto_admin= $cfg->val('goto', 'terminal_admin');
197         $goto_secret= $cfg->val('goto', 'terminal_secret');
198     } else {
199         $goto_admin= undef;
200         $goto_secret= undef;
201     }
205 #===  FUNCTION  ================================================================
206 #         NAME:  get_interface_for_ip
207 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
208 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
209 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
210 #===============================================================================
211 sub get_interface_for_ip {
212         my $result;
213         my $ip= shift;
214         if ($ip && length($ip) > 0) {
215                 my @ifs= &get_interfaces();
216                 if($ip eq "0.0.0.0") {
217                         $result = "all";
218                 } else {
219                         foreach (@ifs) {
220                                 my $if=$_;
221                                 if(get_ip($if) eq $ip) {
222                                         $result = $if;
223                                 }
224                         }       
225                 }
226         }       
227         return $result;
230 #===  FUNCTION  ================================================================
231 #         NAME:  get_interfaces 
232 #   PARAMETERS:  none
233 #      RETURNS:  (list of interfaces) 
234 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
235 #===============================================================================
236 sub get_interfaces {
237         my @result;
238         my $PROC_NET_DEV= ('/proc/net/dev');
240         open(PROC_NET_DEV, "<$PROC_NET_DEV")
241                 or die "Could not open $PROC_NET_DEV";
243         my @ifs = <PROC_NET_DEV>;
245         close(PROC_NET_DEV);
247         # Eat first two line
248         shift @ifs;
249         shift @ifs;
251         chomp @ifs;
252         foreach my $line(@ifs) {
253                 my $if= (split /:/, $line)[0];
254                 $if =~ s/^\s+//;
255                 push @result, $if;
256         }
258         return @result;
261 #===  FUNCTION  ================================================================
262 #         NAME:  get_mac 
263 #   PARAMETERS:  interface name (i.e. eth0)
264 #      RETURNS:  (mac address) 
265 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
266 #===============================================================================
267 sub get_mac {
268         my $ifreq= shift;
269         my $result;
270         if ($ifreq && length($ifreq) > 0) { 
271                 if($ifreq eq "all") {
272                         $result = "00:00:00:00:00:00";
273                 } else {
274                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
276                         # A configured MAC Address should always override a guessed value
277                         if ($server_mac_address and length($server_mac_address) > 0) {
278                                 $result= $server_mac_address;
279                         }
281                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
282                                 or die "socket: $!";
284                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
285                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
287                                 if (length($mac) > 0) {
288                                         $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])$/;
289                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
290                                         $result = $mac;
291                                 }
292                         }
293                 }
294         }
295         return $result;
298 #===  FUNCTION  ================================================================
299 #         NAME:  get_ip 
300 #   PARAMETERS:  interface name (i.e. eth0)
301 #      RETURNS:  (ip address) 
302 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
303 #===============================================================================
304 sub get_ip {
305         my $ifreq= shift;
306         my $result= "";
307         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
308         my $proto= getprotobyname('ip');
310         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
311                 or die "socket: $!";
313         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
314                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
315                 my ($port, $addr) = sockaddr_in $sin;
316                 my $ip            = inet_ntoa $addr;
318                 if ($ip && length($ip) > 0) {
319                         $result = $ip;
320                 }
321         }
323         return $result;
327 #===  FUNCTION  ================================================================
328 #         NAME:  register_at_bus
329 #   PARAMETERS:  nothing
330 #      RETURNS:  nothing
331 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
332 #===============================================================================
333 sub register_at_bus {
335     # add bus to known_server_db
336     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
337                                                     primkey=>'hostname',
338                                                     hostname=>$bus_address,
339                                                     status=>'bus',
340                                                     hostkey=>$bus_key,
341                                                     timestamp=>&get_time,
342                                                 } );
343     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
344     my $msg = &create_xml_string($msg_hash);
346 print STDERR "bus_key:$bus_key\n";
347 print STDERR "msg:$msg\n";
350     &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
351     return $msg;
352 #    my $answer = "";
353 #    $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
354 #    if ($answer == 0) {
355 #        &main::daemon_log("register at bus: $bus_address", 1);
356 #    } else {
357 #        &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
358 #    }
359 #    return;
363 #===  FUNCTION  ================================================================
364 #         NAME:  process_incoming_msg
365 #   PARAMETERS:  crypted_msg - string - incoming crypted message
366 #      RETURNS:  nothing
367 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
368 #===============================================================================
369 sub process_incoming_msg {
370     my ($msg, $msg_hash, $remote_ip) = @_ ;
371     my $error = 0;
372     my $host_name;
373     my $host_key;
374     my @out_msg_l;
376     # process incoming msg
377     my $header = @{$msg_hash->{header}}[0]; 
378     my @target_l = @{$msg_hash->{target}};
380     &main::daemon_log("SIPackages: msg to process: $header", 3);
381     &main::daemon_log("$msg", 8);
383     if( 0 == length @target_l){     
384         &main::daemon_log("ERROR: no target specified for msg $header", 1);
385         $error++;
386     }
388     if( 1 == length @target_l) {
389         my $target = $target_l[0];
390         if( $target eq $server_address ) {  
391             if ($header eq 'new_key') {
392                 @out_msg_l = &new_key($msg_hash)
393             } elsif ($header eq 'here_i_am') {
394                 @out_msg_l = &here_i_am($msg_hash)
395             } elsif ($header eq 'who_has') {
396                 @out_msg_l = &who_has($msg_hash)
397             } elsif ($header eq 'who_has_i_do') {
398                 @out_msg_l = &who_has_i_do($msg_hash)
399             } elsif ($header eq 'got_ping') {
400                 @out_msg_l = &got_ping($msg_hash)
401             } elsif ($header eq 'get_load') {
402                 @out_msg_l = &execute_actions($msg_hash)
403             } elsif ($header eq 'detected_hardware') {
404                 @out_msg_l = &process_detected_hardware($msg_hash)
405             } elsif ($header eq 'trigger_wake') {
406                 foreach (@{$msg_hash->{macAddress}}){
407                     &main::daemon_log("SIPackages: trigger wake for $_", 1);
408                     do_wake($_);
409                 }
411             } else {
412                 &main::daemon_log("ERROR: $header is an unknown core function", 1);
413                 $error++;
414             }
415         }
416                 else {
417                         &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
418                         push(@out_msg_l, $msg);
419                 }
420     }
422     if( $error == 0) {
423         if( 0 == @out_msg_l ) {
424                         push(@out_msg_l, $msg);
425         }
426     }
427     
428     return \@out_msg_l;
432 #===  FUNCTION  ================================================================
433 #         NAME:  got_ping
434 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
435 #      RETURNS:  nothing
436 #  DESCRIPTION:  process this incoming message
437 #===============================================================================
438 sub got_ping {
439     my ($msg_hash) = @_;
440     
441     my $source = @{$msg_hash->{source}}[0];
442     my $target = @{$msg_hash->{target}}[0];
443     my $header = @{$msg_hash->{header}}[0];
444     
445     if(exists $main::known_daemons->{$source}) {
446         &main::add_content2known_daemons(hostname=>$source, status=>$header);
447     } else {
448         &main::add_content2known_clients(hostname=>$source, status=>$header);
449     }
450     
451     return;
455 #===  FUNCTION  ================================================================
456 #         NAME:  new_passwd
457 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
458 #      RETURNS:  nothing
459 #  DESCRIPTION:  process this incoming message
460 #===============================================================================
461 sub new_key {
462     my ($msg_hash) = @_;
463     my @out_msg_l;
464     
465     my $header = @{$msg_hash->{header}}[0];
466     my $source_name = @{$msg_hash->{source}}[0];
467     my $source_key = @{$msg_hash->{new_key}}[0];
468     my $query_res;
470     # check known_clients_db
471     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
472     $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
473     if( 1 == keys %{$query_res} ) {
474         my $act_time = &get_time;
475         my $sql_statement= "UPDATE known_clients ".
476             "SET hostkey='$source_key', timestamp='$act_time' ".
477             "WHERE hostname='$source_name'";
478         my $res = $main::known_clients_db->update_dbentry( $sql_statement );
479         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
480         my $out_msg = &create_xml_string($hash);
481         push(@out_msg_l, $out_msg);
482     }
484     # only do if host still not found
485     if( 0 == @out_msg_l ) {
486         # check known_server_db
487         $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
488         $query_res = $main::known_server_db->select_dbentry( $sql_statement );
489         if( 1 == keys %{$query_res} ) {
490             my $act_time = &get_time;
491             my $sql_statement= "UPDATE known_server ".
492                 "SET hostkey='$source_key', timestamp='$act_time' ".
493                 "WHERE hostname='$source_name'";
494             my $res = $main::known_server_db->update_dbentry( $sql_statement );
496             my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
497             my $out_msg = &create_xml_string($hash);
498             push(@out_msg_l, $out_msg);
499         }
500     }
502     return @out_msg_l;
506 #===  FUNCTION  ================================================================
507 #         NAME:  here_i_am
508 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
509 #      RETURNS:  nothing
510 #  DESCRIPTION:  process this incoming message
511 #===============================================================================
512 sub here_i_am {
513     my ($msg_hash) = @_;
514     my @out_msg_l;
515     my $out_hash;
517     my $source = @{$msg_hash->{source}}[0];
518     my $mac_address = @{$msg_hash->{mac_address}}[0];
519         my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
521     # number of known clients
522     my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
524     # check wether client address or mac address is already known
525     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
526     my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
527     
528     if ( 1 == keys %{$db_res} ) {
529         &main::daemon_log("WARNING: $source is already known as a client", 1);
530         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
531         $nu_clients --;
532     }
534     # number of actual activ clients
535     my $act_nu_clients = $nu_clients;
537     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
538     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
540     if($max_clients <= $act_nu_clients) {
541         my $out_hash = &create_xml_hash("denied", $server_address, $source);
542         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
543         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
544         &send_msg_hash2address($out_hash, $source, $passwd);
545         return;
546     }
547     
548     # new client accepted
549     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
551     # create entry in known_clients
552     my $events = @{$msg_hash->{events}}[0];
553     
555     # add entry to known_clients_db
556     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
557                                                 primkey=>'hostname',
558                                                 hostname=>$source,
559                                                 events=>$events,
560                                                 macaddress=>$mac_address,
561                                                 status=>'registered',
562                                                 hostkey=>$new_passwd,
563                                                 timestamp=>&get_time,
564                                                 } );
566     if ($res != 0)  {
567         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
568         return;
569     }
570     
571     # return acknowledgement to client
572     $out_hash = &create_xml_hash("registered", $server_address, $source);
573     my $register_out = &create_xml_string($out_hash);
574     push(@out_msg_l, $register_out);
576     # notify registered client to bus
577     if( $bus_activ eq "on") {
578         # fetch actual bus key
579         my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
580         my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
581         my $hostkey = $query_res->{1}->{'hostkey'};
583         # send update msg to bus
584         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
585         my $new_client_out = &create_xml_string($out_hash);
586         push(@out_msg_l, $new_client_out);
587         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
588     }
590     # give the new client his ldap config
591     my $new_ldap_config_out = &new_ldap_config($source);
592     if( $new_ldap_config_out ) {
593         push(@out_msg_l, $new_ldap_config_out);
594     }
596         my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
597         if( $hardware_config_out ) {
598                 push(@out_msg_l, $hardware_config_out);
599         }
601     return @out_msg_l;
605 #===  FUNCTION  ================================================================
606 #         NAME:  who_has
607 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
608 #      RETURNS:  nothing 
609 #  DESCRIPTION:  process this incoming message
610 #===============================================================================
611 sub who_has {
612     my ($msg_hash) = @_ ;
613     my @out_msg_l;
614     
615     # what is your search pattern
616     my $search_pattern = @{$msg_hash->{who_has}}[0];
617     my $search_element = @{$msg_hash->{$search_pattern}}[0];
618     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
620     # scanning known_clients for search_pattern
621     my @host_addresses = keys %$main::known_clients;
622     my $known_clients_entries = length @host_addresses;
623     my $host_address;
624     foreach my $host (@host_addresses) {
625         my $client_element = $main::known_clients->{$host}->{$search_pattern};
626         if ($search_element eq $client_element) {
627             $host_address = $host;
628             last;
629         }
630     }
631         
632     # search was successful
633     if (defined $host_address) {
634         my $source = @{$msg_hash->{source}}[0];
635         my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
636         &add_content2xml_hash($out_hash, "mac_address", $search_element);
637         my $out_msg = &create_xml_string($out_hash);
638         push(@out_msg_l, $out_msg);
639     }
640     return @out_msg_l;
644 sub who_has_i_do {
645     my ($msg_hash) = @_ ;
646     my $header = @{$msg_hash->{header}}[0];
647     my $source = @{$msg_hash->{source}}[0];
648     my $search_param = @{$msg_hash->{$header}}[0];
649     my $search_value = @{$msg_hash->{$search_param}}[0];
650     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
653 #===  FUNCTION  ================================================================
654 #         NAME:  new_ldap_config
655 #   PARAMETERS:  address - string - ip address and port of a host
656 #      RETURNS:  nothing
657 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
658 #===============================================================================
659 sub new_ldap_config {
660         my ($address) = @_ ;
662         my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
663         my $res = $main::known_clients_db->select_dbentry( $sql_statement );
665         # check hit
666         my $hit_counter = keys %{$res};
667         if( not $hit_counter == 1 ) {
668                 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
669         }
671         my $macaddress = $res->{1}->{macaddress};
672         my $hostkey = $res->{1}->{hostkey};
674         if (not defined $macaddress) {
675                 &main::daemon_log("ERROR: no mac address found for client $address", 1);
676                 return;
677         }
679         # Build LDAP connection
680         my $ldap = Net::LDAP->new($ldap_uri);
681         if( not defined $ldap ) {
682                 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
683                 return;
684         } 
687         # Bind to a directory with dn and password
688         my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
690         # Perform search
691         $mesg = $ldap->search( base   => $ldap_base,
692                 scope  => 'sub',
693                 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
694                 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
695         #$mesg->code && die $mesg->error;
696         if($mesg->code) {
697                 &main::daemon_log($mesg->error, 1);
698                 return;
699         }
701         # Sanity check
702         if ($mesg->count != 1) {
703                 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
704                 &main::daemon_log("\tbase: $ldap_base", 1);
705                 &main::daemon_log("\tscope: sub", 1);
706                 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
707                 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
708                 return;
709         }
711         my $entry= $mesg->entry(0);
712         my $dn= $entry->dn;
713         my @servers= $entry->get_value("gotoLdapServer");
714         my $unit_tag= $entry->get_value("gosaUnitTag");
715         my @ldap_uris;
716         my $server;
717         my $base;
719         # Do we need to look at an object class?
720         if (length(@servers) < 1){
721                 $mesg = $ldap->search( base   => $ldap_base,
722                         scope  => 'sub',
723                         attrs => ['dn', 'gotoLdapServer'],
724                         filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
725                 #$mesg->code && die $mesg->error;
726                 if($mesg->code) {
727                         &main::daemon_log($mesg->error, 1);
728                         return;
729                 }
731                 # Sanity check
732                 if ($mesg->count != 1) {
733                         &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
734                         return;
735                 }
737                 $entry= $mesg->entry(0);
738                 $dn= $entry->dn;
739                 @servers= $entry->get_value("gotoLdapServer");
740         }
742         @servers= sort (@servers);
744         foreach $server (@servers){
745                 $base= $server;
746                 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
747                 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
748                 push (@ldap_uris, $server);
749         }
751         # Assemble data package
752         my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
753                 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
755         # Need to append GOto settings?
756         if (defined $goto_admin and defined $goto_secret){
757                 $data{'goto_admin'}= $goto_admin;
758                 $data{'goto_secret'}= $goto_secret;
759         }
761         # Append unit tag if needed
762         if (defined $unit_tag){
764                 # Find admin base and department name
765                 $mesg = $ldap->search( base   => $ldap_base,
766                         scope  => 'sub',
767                         attrs => ['dn', 'ou'],
768                         filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
769                 #$mesg->code && die $mesg->error;
770                 if($mesg->code) {
771                         &main::daemon_log($mesg->error, 1);
772                         return;
773                 }
775                 # Sanity check
776                 if ($mesg->count != 1) {
777                         &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
778                         return;
779                 }
781                 $entry= $mesg->entry(0);
782                 $data{'admin_base'}= $entry->dn;
783                 $data{'department'}= $entry->get_value("ou");
785                 # Append unit Tag
786                 $data{'unit_tag'}= $unit_tag;
787         }
789         # Unbind
790         $mesg = $ldap->unbind;
792         # Send information
793         return send_msg("new_ldap_config", $server_address, $address, \%data);
796 sub process_detected_hardware {
797         my $msg_hash = shift;
798         my $address = $msg_hash->{source}[0];
800     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
801     my $res = $main::known_clients_db->select_dbentry( $sql_statement );
803     # check hit
804     my $hit_counter = keys %{$res};
805     if( not $hit_counter == 1 ) {
806         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
807     }
809     my $macaddress = $res->{1}->{macaddress};
810     my $hostkey = $res->{1}->{hostkey};
812     if (not defined $macaddress) {
813         &main::daemon_log("ERROR: no mac address found for client $address", 1);
814         return;
815     }
816     # Build LDAP connection
817     my $ldap = Net::LDAP->new($ldap_uri);
818     if( not defined $ldap ) {
819         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
820         return;
821     } 
823     # Bind to a directory with dn and password
824     my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
826     # Perform search
827         $mesg = $ldap->search(
828                 base   => $ldap_base,
829                 scope  => 'sub',
830                 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
831         );
833         if($mesg->count == 1) {
834                 my $entry= $mesg->entry(0);
835                 $entry->changetype("modify");
836                 foreach my $attribute (
837                         "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel", 
838                         "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync", 
839                         "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
840                         if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
841                                 if(defined($entry->get_value($attribute))) {
842                                         $entry->delete($attribute);
843                                 }
844                                 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
845                                 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});     
846                         }
847                 }
848                 foreach my $attribute (
849                         "gotoModules", "ghScsiDev", "ghIdeDev") {
850                         if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
851                                 if(defined($entry->get_value($attribute))) {
852                                         $entry->delete($attribute);
853                                 }
854                                 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
855                                         $entry->add($attribute => $array_entry);
856                                 }
857                         }
859                 }
860                 if($entry->update($ldap)) {
861                         &main::daemon_log("Added Hardware configuration to LDAP", 4);
862                 }
864         }
865         return;
867 #===  FUNCTION  ================================================================
868 #         NAME:  hardware_config
869 #   PARAMETERS:  address - string - ip address and port of a host
870 #      RETURNS:  
871 #  DESCRIPTION:  
872 #===============================================================================
873 sub hardware_config {
874         my ($address, $gotoHardwareChecksum) = @_ ;
876         my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
877         my $res = $main::known_clients_db->select_dbentry( $sql_statement );
879         # check hit
880         my $hit_counter = keys %{$res};
881         if( not $hit_counter == 1 ) {
882                 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
883         }
885         my $macaddress = $res->{1}->{macaddress};
886         my $hostkey = $res->{1}->{hostkey};
888         if (not defined $macaddress) {
889                 &main::daemon_log("ERROR: no mac address found for client $address", 1);
890                 return;
891         }
893         # Build LDAP connection
894         my $ldap = Net::LDAP->new($ldap_uri);
895         if( not defined $ldap ) {
896                 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
897                 return;
898         } 
900         # Bind to a directory with dn and password
901         my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
903         # Perform search
904         $mesg = $ldap->search(
905                 base   => $ldap_base,
906                 scope  => 'sub',
907                 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
908         );
910         if($mesg->count() == 0) {
911                 &main::daemon_log("Host was not found in LDAP!", 1);
912                 return;
913         }
915         my $entry= $mesg->entry(0);
916         my $dn= $entry->dn;
917         if(defined($entry->get_value("gotoHardwareChecksum"))) {
918                 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
919                         $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
920                         if($entry->update($ldap)) {
921                                 &main::daemon_log("Hardware changed! Detection triggered.", 4);
922                         }
923                 } else {
924                         # Nothing to do
925                         return;
926                 }
927         } else {
928                 # need to fill it to LDAP
929                 $entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
930                 if($entry->update($ldap)) {
931                         &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
932                 }
934                 ## Look if there another host with this checksum to use the hardware config
935                 #$mesg = $ldap->search(
936                 #       base   => $ldap_base,
937                 #       scope  => 'sub',
938                 #       filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
939                 #);
941                 #if($mesg->count>1) {
942                 #       my $clone_entry= $mesg->entry(0);
943                 #       $entry->changetype("modify");
944                 #       foreach my $attribute (
945                 #               "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel", 
946                 #               "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync", 
947                 #               "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
948                 #               my $value= $clone_entry->get_value($attribute);
949                 #               if(defined($value)) {
950                 #                       if(defined($entry->get_value($attribute))) {
951                 #                               $entry->delete($attribute);
952                 #                       }
953                 #                       &main::daemon_log("Adding attribute $attribute with value $value",1);
954                 #                       $entry->add($attribute => $value);
955                 #               }
956                 #       }
957                 #       foreach my $attribute (
958                 #               "gotoModules", "ghScsiDev", "ghIdeDev") {
959                 #               my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
960                 #               if(defined($array))     {
961                 #                       if(defined($entry->get_value($attribute))) {
962                 #                               $entry->delete($attribute);
963                 #                       }
964                 #                       foreach my $array_entry (@{$array}) {
965                 #                               $entry->add($attribute => $array_entry);
966                 #                       }
967                 #               }
969                 #       }
970                 #       if($entry->update($ldap)) {
971                 #               &main::daemon_log("Added Hardware configuration to LDAP", 4);
972                 #       }
974                 #}
976         }
978         # Assemble data package
979         my %data = ();
981         # Need to append GOto settings?
982         if (defined $goto_admin and defined $goto_secret){
983                 $data{'goto_admin'}= $goto_admin;
984                 $data{'goto_secret'}= $goto_secret;
985         }
987         # Unbind
988         $mesg = $ldap->unbind;
990         &main::daemon_log("Send detect_hardware message to $address", 4);
991         
992         # Send information
993         return send_msg("detect_hardware", $server_address, $address, \%data);
997 #===  FUNCTION  ================================================================
998 #         NAME:  execute_actions
999 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
1000 #      RETURNS:  nothing
1001 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
1002 #                /etc/gosad/actions
1003 #===============================================================================
1004 sub execute_actions {
1005     my ($msg_hash) = @_ ;
1006     my $configdir= '/etc/gosad/actions/';
1007     my $result;
1009     my $header = @{$msg_hash->{header}}[0];
1010     my $source = @{$msg_hash->{source}}[0];
1011     my $target = @{$msg_hash->{target}}[0];
1012  
1013     if((not defined $source)
1014             && (not defined $target)
1015             && (not defined $header)) {
1016         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1017     } else {
1018         my $parameters="";
1019         my @params = @{$msg_hash->{$header}};
1020         my $params = join(", ", @params);
1021         &main::daemon_log("execute_actions: got parameters: $params", 5);
1023         if (@params) {
1024             foreach my $param (@params) {
1025                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1026                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1027                 $parameters.= " ".$param_value;
1028             }
1029         }
1031         my $cmd= $configdir.$header."$parameters";
1032         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1033         $result= "";
1034         open(PIPE, "$cmd 2>&1 |");
1035         while(<PIPE>) {
1036             $result.=$_;
1037         }
1038         close(PIPE);
1039     }
1041     # process the event result
1044     return;
1048 1;