Code

switch known_server from shared memory to sqliteDB
[gosa.git] / gosa-si / modules / ServerPackages.pm
1 package ServerPackages;
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 recieves 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;
17 BEGIN{}
18 END {}
20 my ($known_clients_file_name);
21 my ($server_activ, $server_port, $server_passwd, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
22 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
23 my $server;
24 my $no_bus;
25 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
28 my %cfg_defaults =
29 (
30 "server" =>
31     {"server_activ" => [\$server_activ, "on"],
32     "server_port" => [\$server_port, "20081"],
33     "server_passwd" => [\$server_passwd, ""],
34     "max_clients" => [\$max_clients, 100],
35     "ldap_uri" => [\$ldap_uri, ""],
36     "ldap_base" => [\$ldap_base, ""],
37     "ldap_admin_dn" => [\$ldap_admin_dn, ""],
38     "ldap_admin_password" => [\$ldap_admin_password, ""],
39     },
40 "bus" =>
41     {"bus_activ" => [\$bus_activ, "on"],
42     "bus_passwd" => [\$bus_passwd, ""],
43     "bus_ip" => [\$bus_ip, ""],
44     "bus_port" => [\$bus_port, "20080"],
45     },
46 );
48 ### START #####################################################################
51 # read configfile and import variables
52 &read_configfile();
54 # detect own ip and mac address
55 my ($server_ip, $server_mac_address) = &get_ip_and_mac(); 
56 if (not defined $server_ip) {
57     die "EXIT: ip address of $0 could not be detected";
58 }
59 &main::daemon_log("server ip address detected: $server_ip", 1);
60 &main::daemon_log("server mac address detected: $server_mac_address", 1);
62 # complete addresses
63 my $server_address = "$server_ip:$server_port";
64 my $bus_address = "$bus_ip:$bus_port";
66 # create general settings for this module
67 my $xml = new XML::Simple();
69 # open server socket
70 if($server_activ eq "on"){
71     &main::daemon_log(" ", 1);
72     $server = IO::Socket::INET->new(LocalPort => $server_port,
73             Type => SOCK_STREAM,
74             Reuse => 1,
75             Listen => 20,
76             ); 
77     if(not defined $server){
78         &main::daemon_log("cannot be a tcp server at $server_port : $@");
79     } else {
80         &main::daemon_log("start server: $server_address", 1);
81     }
82 }
84 # TODO
85 # füge den server selbst zu known_server hinzu, msgs können nämlich auch von sich selbst kommen (gosa!!!)
88 # register at bus
89 if ($main::no_bus > 0) {
90     $bus_activ = "off"
91 }
92 if($bus_activ eq "on") {
93     &main::daemon_log(" ", 1);
94     &register_at_bus();
95 }
97 ### functions #################################################################
99 #sub get_module_tags {
100 #    
101 #    # lese config file aus dort gibt es eine section Basic
102 #    # dort stehen drei packettypen, für die sich das modul anmelden kann, gosa-admin-packages, 
103 #    #   server-packages, client-packages
104 #    my %tag_hash = (gosa_admin_packages => "yes", 
105 #                    server_packages => "yes", 
106 #                    client_packages => "yes",
107 #                    );
108 #    return \%tag_hash;
109 #}
112 sub get_module_info {
113     my @info = ($server_address,
114                 $server_passwd,
115                 $server,
116                 $server_activ,
117                 "socket",
118                 );
119     return \@info;
123 #===  FUNCTION  ================================================================
124 #         NAME:  read_configfile
125 #   PARAMETERS:  cfg_file - string -
126 #      RETURNS:  nothing
127 #  DESCRIPTION:  read cfg_file and set variables
128 #===============================================================================
129 sub read_configfile {
130     my $cfg;
131     if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
132         if( -r $main::cfg_file ) {
133             $cfg = Config::IniFiles->new( -file => $main::cfg_file );
134         } else {
135             print STDERR "Couldn't read config file!";
136         }
137     } else {
138         $cfg = Config::IniFiles->new() ;
139     }
140     foreach my $section (keys %cfg_defaults) {
141         foreach my $param (keys %{$cfg_defaults{ $section }}) {
142             my $pinfo = $cfg_defaults{ $section }{ $param };
143             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
144         }
145     }
147     # Read non predefined sections
148     my $param;
149     if ($cfg->SectionExists('ldap')){
150                 foreach $param ($cfg->Parameters('ldap')){
151                         push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
152                 }
153     }
154     if ($cfg->SectionExists('pam_ldap')){
155                 foreach $param ($cfg->Parameters('pam_ldap')){
156                         push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
157                 }
158     }
159     if ($cfg->SectionExists('nss_ldap')){
160                 foreach $param ($cfg->Parameters('nss_ldap')){
161                         push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
162                 }
163     }
164     if ($cfg->SectionExists('goto')){
165         $goto_admin= $cfg->val('goto', 'terminal_admin');
166         $goto_secret= $cfg->val('goto', 'terminal_secret');
167     } else {
168         $goto_admin= undef;
169         $goto_secret= undef;
170     }
175 #===  FUNCTION  ================================================================
176 #         NAME:  get_ip_and_mac 
177 #   PARAMETERS:  nothing
178 #      RETURNS:  (ip, mac) 
179 #  DESCRIPTION:  executes /sbin/ifconfig and parses the output, the first occurence 
180 #                of a inet address is returned as well as the mac address in the line
181 #                above the inet address
182 #===============================================================================
183 sub get_ip_and_mac {
184     my $ip = "0.0.0.0.0"; # Defualt-IP
185     my $mac = "00:00:00:00:00:00";  # Default-MAC
186     my @ifconfig = qx(/sbin/ifconfig);
187     foreach(@ifconfig) {
188         if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
189             $mac = "$1:$2:$3:$4:$5:$6";
190             next;
191         }
192         if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
193             $ip = "$1.$2.$3.$4";
194             last;
195         }
196     }
197     return ($ip, $mac);
201 #===  FUNCTION  ================================================================
202 #         NAME:  open_socket
203 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
204 #                [PeerPort] string necessary if port not appended by PeerAddr
205 #      RETURNS:  socket IO::Socket::INET
206 #  DESCRIPTION:  open a socket to PeerAddr
207 #===============================================================================
208 sub open_socket {
209     my ($PeerAddr, $PeerPort) = @_ ;
210     if(defined($PeerPort)){
211         $PeerAddr = $PeerAddr.":".$PeerPort;
212     }
213     my $socket;
214     $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
215             Porto => "tcp" ,
216             Type => SOCK_STREAM,
217             Timeout => 5,
218             );
219     if(not defined $socket) {
220         return;
221     }
222     &main::daemon_log("open_socket to: $PeerAddr", 7);
223     return $socket;
226 #===  FUNCTION  ================================================================
227 #         NAME:  register_at_bus
228 #   PARAMETERS:  nothing
229 #      RETURNS:  nothing
230 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
231 #===============================================================================
232 sub register_at_bus {
234     # add bus to known_server_db
235     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
236                                                     primkey=>'hostname',
237                                                     hostname=>$bus_address,
238                                                     status=>'bus',
239                                                     hostkey=>$bus_passwd,
240                                                     timestamp=>&get_time,
241                                                 } );
242 #    if ($res == 3) {
243 #        my $update_hash = { table=>'known_server' };
244 #        $update_hash->{where} = [ { hostname=>[$bus_address] } ];
245 #        $update_hash->{update} = [ { 
246 #            hostkey=>[$bus_passwd],
247 #            timestamp=>[&get_time],
248 #        } ];
249 #        $res = $main::known_server_db->update_dbentry( $update_hash );
250 #    }
252     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
253     my $answer = "";
254     $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
255     if ($answer == 0) {
256         &main::daemon_log("register at bus: $bus_address", 1);
257     } else {
258         &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
259     }
260     return;
263 #===  FUNCTION  ================================================================
264 #         NAME:  process_incoming_msg
265 #   PARAMETERS:  crypted_msg - string - incoming crypted message
266 #      RETURNS:  nothing
267 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
268 #===============================================================================
269 sub process_incoming_msg {
270     my ($crypted_msg) = @_ ;
271     if(not defined $crypted_msg) {
272         &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
273     }
275     &main::daemon_log("ServerPackages: incoming msg: \n$crypted_msg", 7);
277     $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
278     $crypted_msg = $1;
279     my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
281     my $msg;
282     my $msg_hash;
283     my $host_name;
284     my $host_key;
286     # check wether incoming msg is a new msg
287     $host_name = $server_address;
288     $host_key = $server_passwd;
289     &main::daemon_log("ServerPackage: host_name: $host_name", 7);
290     &main::daemon_log("ServerPackage: host_key: $host_key", 7);
291     eval{
292         my $key_cipher = &create_ciphering($host_key);
293         $msg = &decrypt_msg($crypted_msg, $key_cipher);
294         $msg_hash = &transform_msg2hash($msg);
295     };
296     if($@) {
297         &main::daemon_log("ServerPackage: deciphering raise error", 7);
298         &main::daemon_log("$@", 8);
299         $msg = undef;
300         $msg_hash = undef;
301         $host_name = undef;
302         $host_key = undef;
303     } 
305     # check wether incoming msg is from a known_server
306     if( not defined $msg ) {
307         my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} ); 
308         while( my ($hit_num, $hit) = each %{ $query_res } ) {  
309             $host_name = $hit->{hostname};
310             if( not $host_name =~ "^$host") {
311                 next;
312             }
313             $host_key = $hit->{hostkey};
314             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
315             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
316             eval{
317                 my $key_cipher = &create_ciphering($host_key);
318                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
319                 $msg_hash = &transform_msg2hash($msg);
320             };
321             if($@) {
322                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
323                 &main::daemon_log("$@", 8);
324                 $msg = undef;
325                 $msg_hash = undef;
326                 $host_name = undef;
327                 $host_key = undef;
328             } else {
329                 last;
330             }
331         }
332     }
334     # check wether incoming msg is from a known_client
335     if( not defined $msg ) {
336         my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
337         while( my ($hit_num, $hit) = each %{ $query_res } ) {    
338             $host_name = $hit->{hostname};
339             if( not $host_name =~ "^$host") {
340                 next;
341             }
342             $host_key = $hit->{hostkey};
343             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
344             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
345             eval{
346                 my $key_cipher = &create_ciphering($host_key);
347                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
348                 $msg_hash = &transform_msg2hash($msg);
349             };
350             if($@) {
351                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
352                 &main::daemon_log("$@", 8);
353                 $msg = undef;
354                 $msg_hash = undef;
355                 $host_name = undef;
356                 $host_key = undef;
357             } else {
358                 last;
359             }
360         }
361     }
363     if( not defined $msg ) {
364         &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
365         &main::daemon_log("$@", 7);
366         return;
367     }
369     # process incoming msg
370     my $header = @{$msg_hash->{header}}[0]; 
371     my $source = @{$msg_hash->{source}}[0];
373     &main::daemon_log("recieve '$header' at ServerPackages from $host", 1);
374     &main::daemon_log("ServerPackages: msg to process: \n$msg", 5);
376     my @targets = @{$msg_hash->{target}};
377     my $len_targets = @targets;
378     if ($len_targets == 0){     
379         &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
381     }  elsif ($len_targets == 1){
382         # we have only one target symbol
383         my $target = $targets[0];
384         &main::daemon_log("SeverPackages: msg is for: $target", 7);
386         if ($target eq $server_address) {
387             # msg is for server
388             if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
389             elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
390             elsif ($header eq 'who_has') { &who_has($msg_hash) }
391             elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
392             elsif ($header eq 'update_status') { &update_status($msg_hash) }
393             elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
394             elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
395             else { &main::daemon_log("ERROR: ServerPackages: no function assigned to this msg", 5) }
397        } elsif ($target eq "*") {
398             # msg is for all clients
399             my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
400             while( my ($hit_num, $hit) = each %{ $query_res } ) {    
401                 $host_name = $hit->{hostname};
402                 $host_key = $hit->{hostkey};
403                 $msg_hash->{target} = [$host_name];
404                 &send_msg_hash2address($msg_hash, $host_name, $host_key);
405             }
406             return;
407          
408         } else {
409             # msg is for one host
410             my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} );
411             if( 1 == keys %{$query_res} ) {
412                 $host_key = $query_res->{1}->{host_key};
413                 &send_msg_hash2address($msg_hash, $target, $host_key);
414                 return;
415             }
417             my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} );
418             if( 1 == keys %{$query_res} ) {
419                 $host_key = $query_res->{1}->{host_key};
420                 &send_msg_hash2address($msg_hash, $target, $host_key);
421                 return;
422             }
424             &main::daemon_log("ERROR: ServerPackages: target '$target' is not known neither in known_clients nor in known_server",1);
425             return;
426         }
428     } elsif ($len_targets > 1 ) {
429         # we have more than one target 
430         return;
431     }
433     return ;
437 #===  FUNCTION  ================================================================
438 #         NAME:  got_ping
439 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
440 #      RETURNS:  nothing
441 #  DESCRIPTION:  process this incoming message
442 #===============================================================================
443 sub got_ping {
444     my ($msg_hash) = @_;
445     
446     my $source = @{$msg_hash->{source}}[0];
447     my $target = @{$msg_hash->{target}}[0];
448     my $header = @{$msg_hash->{header}}[0];
449     
450     if(exists $main::known_daemons->{$source}) {
451         &main::add_content2known_daemons(hostname=>$source, status=>$header);
452     } else {
453         &main::add_content2known_clients(hostname=>$source, status=>$header);
454     }
455     
456     return;
460 #===  FUNCTION  ================================================================
461 #         NAME:  new_passwd
462 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
463 #      RETURNS:  nothing
464 #  DESCRIPTION:  process this incoming message
465 #===============================================================================
466 sub new_passwd {
467     my ($msg_hash) = @_;
469     my $header = @{$msg_hash->{header}}[0];
470     my $source_name = @{$msg_hash->{source}}[0];
471     my $source_key = @{$msg_hash->{new_passwd}}[0];
472     my $query_res;
474     # check known_clients_db
475     $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$source_name} );
476     if( 1 == keys %{$query_res} ) {
477         my $update_hash = { table=>'known_clients' };
478         $update_hash->{where} = [ { hostname=>[$source_name] } ];
479         $update_hash->{update} = [ {
480             hostkey=>[$source_key],
481             timestamp=>[&get_time],
482         } ];
483         my $res = $main::known_clients_db->update_dbentry( $update_hash );
485         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
486         &send_msg_hash2address($hash, $source_name, $source_key);
487         return;
488     }
490     # check known_server_db
491     $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$source_name } );
492     if( 1 == keys %{$query_res} ) {
493         my $update_hash = { table=>'known_server' };
494         $update_hash->{where} = [ { hostname=>[$source_name] } ];
495         $update_hash->{update} = [ {
496             hostkey=>[$source_key],
497                 timestamp=>[&get_time],
498         } ];
499         my $res = $main::known_server_db->update_dbentry( $update_hash );
501         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
502         &send_msg_hash2address($hash, $source_name, $source_key);
503         return;
504     }
506     &main::daemon_log("ERROR: $source_name not known for '$header'-msg", 1);
507     return;
511 sub send_msg_hash {
512     my ($hash, $host_name, $host_key);
514     
515     my $answer = &send_msg_hash2address($hash, $host_name, $host_key);
516     
517     return;
521 #===  FUNCTION  ================================================================
522 #         NAME:  here_i_am
523 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
524 #      RETURNS:  nothing
525 #  DESCRIPTION:  process this incoming message
526 #===============================================================================
527 sub here_i_am {
528     my ($msg_hash) = @_;
530     my $source = @{$msg_hash->{source}}[0];
531     my $mac_address = @{$msg_hash->{mac_address}}[0];
532     my $out_hash;
534     # number of known clients
535     my $nu_clients = keys %{$main::known_clients_db->select_dbentry( {table=>'known_clients'} )};
537     # check wether client address or mac address is already known
538     if (exists $main::known_clients->{$source}) {
539         &main::daemon_log("WARNING: $source is already known as a client", 1);
540         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
541         $nu_clients --;
542     }
544     # number of actual activ clients
545     my $act_nu_clients = $nu_clients;
547     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
548     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
550     if($max_clients <= $act_nu_clients) {
551         my $out_hash = &create_xml_hash("denied", $server_address, $source);
552         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
553         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
554         &send_msg_hash2address($out_hash, $source, $passwd);
555         return;
556     }
557     
558     # new client accepted
559     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
561     # create entry in known_clients
562     my $events = @{$msg_hash->{events}}[0];
563     
564     # add entry to known_clients_db
565     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
566                                                 primkey=>'hostname',
567                                                 hostname=>$source,
568                                                 events=>$events,
569                                                 macaddress=>$mac_address,
570                                                 status=>'registered',
571                                                 hostkey=>$new_passwd,
572                                                 timestamp=>&get_time,
573                                                 } );
574 #    if ($res == 3) {
575 #        my $update_hash = { table=>'known_clients' };
576 #        $update_hash->{where} = [ { hostname=>[$source] } ];
577 #        $update_hash->{update} = [ { events=>[$events],
578 #                                     macaddress=>[$mac_address],
579 #                                     status=>['registered'],
580 #                                     hostkey=>[$new_passwd],
581 #                                     timestamp=>[&get_time],
582 #                                    } ];
583 #        $res = $main::known_clients_db->update_dbentry( $update_hash );
584 #    } 
585     if ($res != 1)  {
586         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
587         return;
588     }
589     
590     # return acknowledgement to client
591     $out_hash = &create_xml_hash("registered", $server_address, $source);
592     &send_msg_hash2address($out_hash, $source, $new_passwd);
594     # notify registered client to bus
595     if( $bus_activ eq "on") {
596         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
597         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
598         &send_msg_hash2address($out_hash, $bus_address);
599     }
601     # give the new client his ldap config
602     &new_ldap_config($source);
604     return;
608 #===  FUNCTION  ================================================================
609 #         NAME:  who_has
610 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
611 #      RETURNS:  nothing 
612 #  DESCRIPTION:  process this incoming message
613 #===============================================================================
614 sub who_has {
615     my ($msg_hash) = @_ ;
616     
617     # what is your search pattern
618     my $search_pattern = @{$msg_hash->{who_has}}[0];
619     my $search_element = @{$msg_hash->{$search_pattern}}[0];
620     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
622     # scanning known_clients for search_pattern
623     my @host_addresses = keys %$main::known_clients;
624     my $known_clients_entries = length @host_addresses;
625     my $host_address;
626     foreach my $host (@host_addresses) {
627         my $client_element = $main::known_clients->{$host}->{$search_pattern};
628         if ($search_element eq $client_element) {
629             $host_address = $host;
630             last;
631         }
632     }
633         
634     # search was successful
635     if (defined $host_address) {
636         my $source = @{$msg_hash->{source}}[0];
637         my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
638         &add_content2xml_hash($out_msg, "mac_address", $search_element);
639         &send_msg_hash2address($out_msg, $bus_address);
640     }
641     return;
645 sub who_has_i_do {
646     my ($msg_hash) = @_ ;
647     my $header = @{$msg_hash->{header}}[0];
648     my $source = @{$msg_hash->{source}}[0];
649     my $search_param = @{$msg_hash->{$header}}[0];
650     my $search_value = @{$msg_hash->{$search_param}}[0];
651     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
655 #===  FUNCTION  ================================================================
656 #         NAME:  new_ldap_config
657 #   PARAMETERS:  address - string - ip address and port of a host
658 #      RETURNS:  nothing
659 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
660 #===============================================================================
661 sub new_ldap_config {
662     my ($address) = @_ ;
663     
664     my $res = $main::known_clients_db->select_dbentry( { table=>'known_clients', hostname=>$address } );
666     # check hit
667     my $hit_counter = keys %{$res};
668     if( not $hit_counter == 1 ) {
669         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
670         my $tmp = print Dumer $res;
671     }
673     my $macaddress = $res->{1}->{macaddress};
674     my $hostkey = $res->{1}->{hostkey};
676     if (not defined $macaddress) {
677         &main::daemon_log("ERROR: no mac address found for client $address", 1);
678         return;
679     }
681     # Build LDAP connection
682     my $ldap = Net::LDAP->new($ldap_uri);
683     if( not defined $ldap ) {
684         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
685         return;
686     } 
689     # Bind to a directory with dn and password
690     my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
692     # Perform search
693     $mesg = $ldap->search( base   => $ldap_base,
694                     scope  => 'sub',
695                     attrs => ['dn', 'gotoLdapServer'],
696                     filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
697     $mesg->code && die $mesg->error;
699     # Sanity check
700     if ($mesg->count != 1) {
701             &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
702         &main::daemon_log("\tbase: $ldap_base", 1);
703         &main::daemon_log("\tscope: sub", 1);
704         &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
705         &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
706             return;
707     }
709     my $entry= $mesg->entry(0);
710     my $dn= $entry->dn;
711     my @servers= $entry->get_value("gotoLdapServer");
712     my @ldap_uris;
713     my $server;
714     my $base;
716     # Do we need to look at an object class?
717     if ($#servers < 1){
718             $mesg = $ldap->search( base   => $ldap_base,
719                             scope  => 'sub',
720                             attrs => ['dn', 'gotoLdapServer'],
721                             filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
722             $mesg->code && die $mesg->error;
724             # Sanity check
725             if ($mesg->count != 1) {
726                     &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
727                     return;
728             }
730             $entry= $mesg->entry(0);
731             $dn= $entry->dn;
732             @servers= $entry->get_value("gotoLdapServer");
733     }
735     @servers= sort (@servers);
737     foreach $server (@servers){
738             $base= $server;
739             $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
740             $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
741             push (@ldap_uris, $server);
742     }
744     # Unbind
745     $mesg = $ldap->unbind;
747     # Assemble data package
748     my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
749                      'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
751     # Need to append GOto settings?
752     if (defined $goto_admin and defined $goto_secret){
753             $data{'goto_admin'}= $goto_admin;
754             $data{'goto_secret'}= $goto_secret;
755     }
757     # Send information
758     send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
760     return;
764 #===  FUNCTION  ================================================================
765 #         NAME:  execute_actions
766 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
767 #      RETURNS:  nothing
768 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
769 #                /etc/gosad/actions
770 #===============================================================================
771 sub execute_actions {
772     my ($msg_hash) = @_ ;
773     my $configdir= '/etc/gosad/actions/';
774     my $result;
776     my $header = @{$msg_hash->{header}}[0];
777     my $source = @{$msg_hash->{source}}[0];
778     my $target = @{$msg_hash->{target}}[0];
779  
780     if((not defined $source)
781             && (not defined $target)
782             && (not defined $header)) {
783         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
784     } else {
785         my $parameters="";
786         my @params = @{$msg_hash->{$header}};
787         my $params = join(", ", @params);
788         &main::daemon_log("execute_actions: got parameters: $params", 5);
790         if (@params) {
791             foreach my $param (@params) {
792                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
793                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
794                 $parameters.= " ".$param_value;
795             }
796         }
798         my $cmd= $configdir.$header."$parameters";
799         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
800         $result= "";
801         open(PIPE, "$cmd 2>&1 |");
802         while(<PIPE>) {
803             $result.=$_;
804         }
805         close(PIPE);
806     }
808     # process the event result
811     return;
815 1;