Code

e7372d8df493647fb517801de24b7374b1084f8f
[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 qw/PF_INET SOCK_DGRAM inet_ntoa sockaddr_in/;
18 BEGIN{}
19 END {}
21 my ($known_clients_file_name);
22 my ($server_activ, $server_ip, $server_mac_address, $server_port, $server_passwd, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
23 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
24 my $server;
25 my $network_interface;
26 my $no_bus;
27 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
30 my %cfg_defaults =
31 (
32 "server" =>
33     {"server_activ" => [\$server_activ, "on"],
34     "server_ip" => [\$server_ip, "0.0.0.0"],
35     "server_mac_address" => [\$server_mac_address, ""],
36     "server_port" => [\$server_port, "20081"],
37     "server_passwd" => [\$server_passwd, ""],
38     "max_clients" => [\$max_clients, 100],
39     "ldap_uri" => [\$ldap_uri, ""],
40     "ldap_base" => [\$ldap_base, ""],
41     "ldap_admin_dn" => [\$ldap_admin_dn, ""],
42     "ldap_admin_password" => [\$ldap_admin_password, ""],
43     },
44 "bus" =>
45     {"bus_activ" => [\$bus_activ, "on"],
46     "bus_passwd" => [\$bus_passwd, ""],
47     "bus_ip" => [\$bus_ip, ""],
48     "bus_port" => [\$bus_port, "20080"],
49     },
50 );
52 ### START #####################################################################
54 # read configfile and import variables
55 &read_configfile();
57 # detect interfaces and mac address
58 $network_interface= &get_interface_for_ip($server_ip);
59 $server_mac_address= &get_mac($network_interface); 
61 # complete addresses
62 if( $server_ip eq "0.0.0.0" ) {
63     $server_ip = "127.0.0.1";
64 }
65 my $server_address = "$server_ip:$server_port";
66 my $bus_address = "$bus_ip:$bus_port";
68 # create general settings for this module
69 my $xml = new XML::Simple();
71 # register at bus
72 if ($main::no_bus > 0) {
73     $bus_activ = "off"
74 }
75 if($bus_activ eq "on") {
76     &register_at_bus();
77 }
79 ### functions #################################################################
82 sub get_module_info {
83     my @info = ($server_address,
84                 $server_passwd,
85                 $server,
86                 $server_activ,
87                 "socket",
88                 );
89     return \@info;
90 }
93 #===  FUNCTION  ================================================================
94 #         NAME:  read_configfile
95 #   PARAMETERS:  cfg_file - string -
96 #      RETURNS:  nothing
97 #  DESCRIPTION:  read cfg_file and set variables
98 #===============================================================================
99 sub read_configfile {
100     my $cfg;
101     if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
102         if( -r $main::cfg_file ) {
103             $cfg = Config::IniFiles->new( -file => $main::cfg_file );
104         } else {
105             print STDERR "Couldn't read config file!";
106         }
107     } else {
108         $cfg = Config::IniFiles->new() ;
109     }
110     foreach my $section (keys %cfg_defaults) {
111         foreach my $param (keys %{$cfg_defaults{ $section }}) {
112             my $pinfo = $cfg_defaults{ $section }{ $param };
113             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
114         }
115     }
117     # Read non predefined sections
118     my $param;
119     if ($cfg->SectionExists('ldap')){
120                 foreach $param ($cfg->Parameters('ldap')){
121                         push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
122                 }
123     }
124     if ($cfg->SectionExists('pam_ldap')){
125                 foreach $param ($cfg->Parameters('pam_ldap')){
126                         push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
127                 }
128     }
129     if ($cfg->SectionExists('nss_ldap')){
130                 foreach $param ($cfg->Parameters('nss_ldap')){
131                         push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
132                 }
133     }
134     if ($cfg->SectionExists('goto')){
135         $goto_admin= $cfg->val('goto', 'terminal_admin');
136         $goto_secret= $cfg->val('goto', 'terminal_secret');
137     } else {
138         $goto_admin= undef;
139         $goto_secret= undef;
140     }
144 #===  FUNCTION  ================================================================
145 #         NAME:  get_interface_for_ip
146 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
147 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
148 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
149 #===============================================================================
150 sub get_interface_for_ip {
151         my $result;
152         my $ip= shift;
153         if ($ip && length($ip) > 0) {
154                 my @ifs= &get_interfaces();
155                 if($ip eq "0.0.0.0") {
156                         $result = "all";
157                 } else {
158                         foreach (@ifs) {
159                                 my $if=$_;
160                                 if(get_ip($if) eq $ip) {
161                                         $result = $if;
162                                 }
163                         }       
164                 }
165         }       
166         return $result;
169 #===  FUNCTION  ================================================================
170 #         NAME:  get_interfaces 
171 #   PARAMETERS:  none
172 #      RETURNS:  (list of interfaces) 
173 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
174 #===============================================================================
175 sub get_interfaces {
176         my @result;
177         my $PROC_NET_DEV= ('/proc/net/dev');
179         open(PROC_NET_DEV, "<$PROC_NET_DEV")
180                 or die "Could not open $PROC_NET_DEV";
182         my @ifs = <PROC_NET_DEV>;
184         close(PROC_NET_DEV);
186         # Eat first two line
187         shift @ifs;
188         shift @ifs;
190         chomp @ifs;
191         foreach my $line(@ifs) {
192                 my $if= (split /:/, $line)[0];
193                 $if =~ s/^\s+//;
194                 push @result, $if;
195         }
197         return @result;
200 #===  FUNCTION  ================================================================
201 #         NAME:  get_mac 
202 #   PARAMETERS:  interface name (i.e. eth0)
203 #      RETURNS:  (mac address) 
204 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
205 #===============================================================================
206 sub get_mac {
207         my $ifreq= shift;
208         my $result;
209         if ($ifreq && length($ifreq) > 0) { 
210                 if($ifreq eq "all") {
211                         $result = "00:00:00:00:00:00";
212                 } else {
213                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
215                         # A configured MAC Address should always override a guessed value
216                         if ($server_mac_address and length($server_mac_address) > 0) {
217                                 $result= $server_mac_address;
218                         }
220                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
221                                 or die "socket: $!";
223                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
224                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
226                                 if (length($mac) > 0) {
227                                         $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])$/;
228                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
229                                         $result = $mac;
230                                 }
231                         }
232                 }
233         }
234         return $result;
237 #===  FUNCTION  ================================================================
238 #         NAME:  get_ip 
239 #   PARAMETERS:  interface name (i.e. eth0)
240 #      RETURNS:  (ip address) 
241 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
242 #===============================================================================
243 sub get_ip {
244         my $ifreq= shift;
245         my $result= "";
246         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
247         my $proto= getprotobyname('ip');
249         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
250                 or die "socket: $!";
252         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
253                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
254                 my ($port, $addr) = sockaddr_in $sin;
255                 my $ip            = inet_ntoa $addr;
257                 if ($ip && length($ip) > 0) {
258                         $result = $ip;
259                 }
260         }
262         return $result;
266 #===  FUNCTION  ================================================================
267 #         NAME:  register_at_bus
268 #   PARAMETERS:  nothing
269 #      RETURNS:  nothing
270 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
271 #===============================================================================
272 sub register_at_bus {
274     # add bus to known_server_db
275     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
276                                                     primkey=>'hostname',
277                                                     hostname=>$bus_address,
278                                                     status=>'bus',
279                                                     hostkey=>$bus_passwd,
280                                                     timestamp=>&get_time,
281                                                 } );
282     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
283     my $answer = "";
284     $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
285     if ($answer == 0) {
286         &main::daemon_log("register at bus: $bus_address", 1);
287     } else {
288         &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
289     }
290     return;
294 #===  FUNCTION  ================================================================
295 #         NAME:  process_incoming_msg
296 #   PARAMETERS:  crypted_msg - string - incoming crypted message
297 #      RETURNS:  nothing
298 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
299 #===============================================================================
300 sub process_incoming_msg {
301     my ($msg, $msg_hash) = @_ ;
302     my $error = 0;
303     my $host_name;
304     my $host_key;
305     my @out_msg_l;
307     # process incoming msg
308     my $header = @{$msg_hash->{header}}[0]; 
309     my $source = @{$msg_hash->{source}}[0];
310     my @target_l = @{$msg_hash->{target}};
312     &main::daemon_log("SIPackages: msg to process: $header", 3);
313     &main::daemon_log("$msg", 8);
315     if( 0 == length @target_l){     
316         &main::daemon_log("ERROR: no target specified for msg $header", 1);
317         $error++;
318     }
320     if( 1 == length @target_l) {
321         my $target = $target_l[0];
322         if( $target eq $server_address ) {  
323             if ($header eq 'new_passwd') { @out_msg_l = &new_passwd($msg_hash) }
324             elsif ($header eq 'here_i_am') { @out_msg_l = &here_i_am($msg_hash) }
325             elsif ($header eq 'who_has') { @out_msg_l = &who_has($msg_hash) }
326             elsif ($header eq 'who_has_i_do') { @out_msg_l = &who_has_i_do($msg_hash) }
327             elsif ($header eq 'got_ping') { @out_msg_l = &got_ping($msg_hash)}
328             elsif ($header eq 'get_load') { @out_msg_l = &execute_actions($msg_hash)}
329             elsif ($header eq 'detected_hardware') { @out_msg_l = &process_detected_hardware($msg_hash)}
330             else {
331                 &main::daemon_log("ERROR: $header is an unknown core function", 1);
332                 $error++;
333             }
334         }
335                 else {
336                         &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
337                         push(@out_msg_l, $msg);
338                 }
339     }
341     if( $error == 0) {
342         if( 0 == @out_msg_l ) {
343                         push(@out_msg_l, $msg);
344         }
345     }
346     
347     return \@out_msg_l;
351 #===  FUNCTION  ================================================================
352 #         NAME:  got_ping
353 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
354 #      RETURNS:  nothing
355 #  DESCRIPTION:  process this incoming message
356 #===============================================================================
357 sub got_ping {
358     my ($msg_hash) = @_;
359     
360     my $source = @{$msg_hash->{source}}[0];
361     my $target = @{$msg_hash->{target}}[0];
362     my $header = @{$msg_hash->{header}}[0];
363     
364     if(exists $main::known_daemons->{$source}) {
365         &main::add_content2known_daemons(hostname=>$source, status=>$header);
366     } else {
367         &main::add_content2known_clients(hostname=>$source, status=>$header);
368     }
369     
370     return;
374 #===  FUNCTION  ================================================================
375 #         NAME:  new_passwd
376 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
377 #      RETURNS:  nothing
378 #  DESCRIPTION:  process this incoming message
379 #===============================================================================
380 sub new_passwd {
381     my ($msg_hash) = @_;
382     my @out_msg_l;
383     
384     my $header = @{$msg_hash->{header}}[0];
385     my $source_name = @{$msg_hash->{source}}[0];
386     my $source_key = @{$msg_hash->{new_passwd}}[0];
387     my $query_res;
389     # check known_clients_db
390     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
391     $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
392     if( 1 == keys %{$query_res} ) {
393         my $act_time = &get_time;
394         my $sql_statement= "UPDATE known_clients ".
395             "SET hostkey='$source_key', timestamp='$act_time' ".
396             "WHERE hostname='$source_name'";
397         my $res = $main::known_clients_db->update_dbentry( $sql_statement );
399         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
400         my $out_msg = &create_xml_string($hash);
401         push(@out_msg_l, $out_msg);
402     }
404     # only do if host still not found
405     if( 0 == @out_msg_l ) {
406         # check known_server_db
407         $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
408         $query_res = $main::known_server_db->select_dbentry( $sql_statement );
409         if( 1 == keys %{$query_res} ) {
410             my $act_time = &get_time;
411             my $sql_statement= "UPDATE known_server ".
412                 "SET hostkey='$source_key', timestamp='$act_time' ".
413                 "WHERE hostname='$source_name'";
414             my $res = $main::known_server_db->update_dbentry( $sql_statement );
416             my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
417             my $out_msg = &create_xml_string($hash);
418             push(@out_msg_l, $out_msg);
419         }
420     }
422     return @out_msg_l;
426 #===  FUNCTION  ================================================================
427 #         NAME:  here_i_am
428 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
429 #      RETURNS:  nothing
430 #  DESCRIPTION:  process this incoming message
431 #===============================================================================
432 sub here_i_am {
433     my ($msg_hash) = @_;
434     my @out_msg_l;
435     my $out_hash;
437     my $source = @{$msg_hash->{source}}[0];
438     my $mac_address = @{$msg_hash->{mac_address}}[0];
439         my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
441     # number of known clients
442     my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
444     # check wether client address or mac address is already known
445     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
446     my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
447     
448     if ( 1 == keys %{$db_res} ) {
449         &main::daemon_log("WARNING: $source is already known as a client", 1);
450         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
451         $nu_clients --;
452     }
454     # number of actual activ clients
455     my $act_nu_clients = $nu_clients;
457     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
458     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
460     if($max_clients <= $act_nu_clients) {
461         my $out_hash = &create_xml_hash("denied", $server_address, $source);
462         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
463         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
464         &send_msg_hash2address($out_hash, $source, $passwd);
465         return;
466     }
467     
468     # new client accepted
469     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
471     # create entry in known_clients
472     my $events = @{$msg_hash->{events}}[0];
473     
475     # add entry to known_clients_db
476     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
477                                                 primkey=>'hostname',
478                                                 hostname=>$source,
479                                                 events=>$events,
480                                                 macaddress=>$mac_address,
481                                                 status=>'registered',
482                                                 hostkey=>$new_passwd,
483                                                 timestamp=>&get_time,
484                                                 } );
486     if ($res != 0)  {
487         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
488         return;
489     }
490     
491     # return acknowledgement to client
492     $out_hash = &create_xml_hash("registered", $server_address, $source);
493     my $register_out = &create_xml_string($out_hash);
494     push(@out_msg_l, $register_out);
496     # notify registered client to bus
497     if( $bus_activ eq "on") {
498         # fetch actual bus key
499         my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
500         my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
501         my $hostkey = $query_res->{1}->{'hostkey'};
503         # send update msg to bus
504         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
505         my $new_client_out = &create_xml_string($out_hash);
506         push(@out_msg_l, $new_client_out);
507         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
508     }
510     # give the new client his ldap config
511     my $new_ldap_config_out = &new_ldap_config($source);
512     if( $new_ldap_config_out ) {
513         push(@out_msg_l, $new_ldap_config_out);
514     }
516         my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
517         if( $hardware_config_out ) {
518                 push(@out_msg_l, $hardware_config_out);
519         }
521     return @out_msg_l;
525 #===  FUNCTION  ================================================================
526 #         NAME:  who_has
527 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
528 #      RETURNS:  nothing 
529 #  DESCRIPTION:  process this incoming message
530 #===============================================================================
531 sub who_has {
532     my ($msg_hash) = @_ ;
533     
534     # what is your search pattern
535     my $search_pattern = @{$msg_hash->{who_has}}[0];
536     my $search_element = @{$msg_hash->{$search_pattern}}[0];
537     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
539     # scanning known_clients for search_pattern
540     my @host_addresses = keys %$main::known_clients;
541     my $known_clients_entries = length @host_addresses;
542     my $host_address;
543     foreach my $host (@host_addresses) {
544         my $client_element = $main::known_clients->{$host}->{$search_pattern};
545         if ($search_element eq $client_element) {
546             $host_address = $host;
547             last;
548         }
549     }
550         
551     # search was successful
552     if (defined $host_address) {
553         my $source = @{$msg_hash->{source}}[0];
554         my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
555         &add_content2xml_hash($out_msg, "mac_address", $search_element);
556         &send_msg_hash2address($out_msg, $bus_address);
557     }
558     return;
562 sub who_has_i_do {
563     my ($msg_hash) = @_ ;
564     my $header = @{$msg_hash->{header}}[0];
565     my $source = @{$msg_hash->{source}}[0];
566     my $search_param = @{$msg_hash->{$header}}[0];
567     my $search_value = @{$msg_hash->{$search_param}}[0];
568     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
571 #===  FUNCTION  ================================================================
572 #         NAME:  new_ldap_config
573 #   PARAMETERS:  address - string - ip address and port of a host
574 #      RETURNS:  nothing
575 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
576 #===============================================================================
577 sub new_ldap_config {
578         my ($address) = @_ ;
580         my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
581         my $res = $main::known_clients_db->select_dbentry( $sql_statement );
583         # check hit
584         my $hit_counter = keys %{$res};
585         if( not $hit_counter == 1 ) {
586                 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
587         }
589         my $macaddress = $res->{1}->{macaddress};
590         my $hostkey = $res->{1}->{hostkey};
592         if (not defined $macaddress) {
593                 &main::daemon_log("ERROR: no mac address found for client $address", 1);
594                 return;
595         }
597         # Build LDAP connection
598         my $ldap = Net::LDAP->new($ldap_uri);
599         if( not defined $ldap ) {
600                 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
601                 return;
602         } 
605         # Bind to a directory with dn and password
606         my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
608         # Perform search
609         $mesg = $ldap->search( base   => $ldap_base,
610                 scope  => 'sub',
611                 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
612                 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
613         #$mesg->code && die $mesg->error;
614         if($mesg->code) {
615                 &main::daemon_log($mesg->error, 1);
616                 return;
617         }
619         # Sanity check
620         if ($mesg->count != 1) {
621                 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
622                 &main::daemon_log("\tbase: $ldap_base", 1);
623                 &main::daemon_log("\tscope: sub", 1);
624                 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
625                 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
626                 return;
627         }
629         my $entry= $mesg->entry(0);
630         my $dn= $entry->dn;
631         my @servers= $entry->get_value("gotoLdapServer");
632         my $unit_tag= $entry->get_value("gosaUnitTag");
633         my @ldap_uris;
634         my $server;
635         my $base;
637         # Do we need to look at an object class?
638         if (length(@servers) < 1){
639                 $mesg = $ldap->search( base   => $ldap_base,
640                         scope  => 'sub',
641                         attrs => ['dn', 'gotoLdapServer'],
642                         filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
643                 #$mesg->code && die $mesg->error;
644                 if($mesg->code) {
645                         &main::daemon_log($mesg->error, 1);
646                         return;
647                 }
649                 # Sanity check
650                 if ($mesg->count != 1) {
651                         &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
652                         return;
653                 }
655                 $entry= $mesg->entry(0);
656                 $dn= $entry->dn;
657                 @servers= $entry->get_value("gotoLdapServer");
658         }
660         @servers= sort (@servers);
662         foreach $server (@servers){
663                 $base= $server;
664                 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
665                 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
666                 push (@ldap_uris, $server);
667         }
669         # Assemble data package
670         my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
671                 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
673         # Need to append GOto settings?
674         if (defined $goto_admin and defined $goto_secret){
675                 $data{'goto_admin'}= $goto_admin;
676                 $data{'goto_secret'}= $goto_secret;
677         }
679         # Append unit tag if needed
680         if (defined $unit_tag){
682                 # Find admin base and department name
683                 $mesg = $ldap->search( base   => $ldap_base,
684                         scope  => 'sub',
685                         attrs => ['dn', 'ou'],
686                         filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
687                 #$mesg->code && die $mesg->error;
688                 if($mesg->code) {
689                         &main::daemon_log($mesg->error, 1);
690                         return;
691                 }
693                 # Sanity check
694                 if ($mesg->count != 1) {
695                         &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
696                         return;
697                 }
699                 $entry= $mesg->entry(0);
700                 $data{'admin_base'}= $entry->dn;
701                 $data{'department'}= $entry->get_value("ou");
703                 # Append unit Tag
704                 $data{'unit_tag'}= $unit_tag;
705         }
707         # Unbind
708         $mesg = $ldap->unbind;
710         # Send information
711         return send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
714 sub process_detected_hardware {
715         my $msg_hash = shift;
718         return;
720 #===  FUNCTION  ================================================================
721 #         NAME:  hardware_config
722 #   PARAMETERS:  address - string - ip address and port of a host
723 #      RETURNS:  
724 #  DESCRIPTION:  
725 #===============================================================================
726 sub hardware_config {
727     my ($address, $gotoHardwareChecksum, $detectedHardware) = @_ ;
728     
729     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
730     my $res = $main::known_clients_db->select_dbentry( $sql_statement );
732     # check hit
733     my $hit_counter = keys %{$res};
734     if( not $hit_counter == 1 ) {
735         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
736     }
738     my $macaddress = $res->{1}->{macaddress};
739     my $hostkey = $res->{1}->{hostkey};
741     if (not defined $macaddress) {
742         &main::daemon_log("ERROR: no mac address found for client $address", 1);
743         return;
744     }
746     # Build LDAP connection
747     my $ldap = Net::LDAP->new($ldap_uri);
748     if( not defined $ldap ) {
749         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
750         return;
751     } 
754     # Bind to a directory with dn and password
755     my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
757     # Perform search
758     $mesg = $ldap->search( base   => $ldap_base,
759                     scope  => 'sub',
760                     attrs => ['dn', 'gotoHardwareChecksum'],
761                     filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
762                 #$mesg->code && die $mesg->error;
764         #my $entry= $mesg->entry(0);
765         #my $dn= $entry->dn;
766         #my @servers= $entry->get_value("gotoHardwareChecksum");
768     # Assemble data package
769     my %data = ();
771     # Need to append GOto settings?
772     if (defined $goto_admin and defined $goto_secret){
773             $data{'goto_admin'}= $goto_admin;
774             $data{'goto_secret'}= $goto_secret;
775     }
777     # Unbind
778     $mesg = $ldap->unbind;
780         &main::daemon_log("Send detect_hardware message to $address", 4);
781     # Send information
782     return send_msg("detect_hardware", $server_address, $address, \%data, $hostkey);
786 #===  FUNCTION  ================================================================
787 #         NAME:  execute_actions
788 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
789 #      RETURNS:  nothing
790 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
791 #                /etc/gosad/actions
792 #===============================================================================
793 sub execute_actions {
794     my ($msg_hash) = @_ ;
795     my $configdir= '/etc/gosad/actions/';
796     my $result;
798     my $header = @{$msg_hash->{header}}[0];
799     my $source = @{$msg_hash->{source}}[0];
800     my $target = @{$msg_hash->{target}}[0];
801  
802     if((not defined $source)
803             && (not defined $target)
804             && (not defined $header)) {
805         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
806     } else {
807         my $parameters="";
808         my @params = @{$msg_hash->{$header}};
809         my $params = join(", ", @params);
810         &main::daemon_log("execute_actions: got parameters: $params", 5);
812         if (@params) {
813             foreach my $param (@params) {
814                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
815                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
816                 $parameters.= " ".$param_value;
817             }
818         }
820         my $cmd= $configdir.$header."$parameters";
821         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
822         $result= "";
823         open(PIPE, "$cmd 2>&1 |");
824         while(<PIPE>) {
825             $result.=$_;
826         }
827         close(PIPE);
828     }
830     # process the event result
833     return;
837 1;