Code

Methods to identify interface using ip address.
[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;
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_interfaces;
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_interfaces= &get_interface_for_ip($server_ip);
59 $server_mac_address= &get_mac($network_interfaces[0]); 
61 &main::daemon_log("server ip address detected: $server_ip", 1);
62 &main::daemon_log("server mac address detected: $server_mac_address", 1);
64 # complete addresses
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 # open server socket
72 if($server_activ eq "on"){
73     &main::daemon_log(" ", 1);
74     $server = IO::Socket::INET->new(LocalPort => $server_port,
75             Type => SOCK_STREAM,
76             Reuse => 1,
77             Listen => 20,
78             ); 
79     if(not defined $server){
80         &main::daemon_log("cannot be a tcp server at $server_port : $@");
81     } else {
82         &main::daemon_log("start server: $server_address", 1);
83     }
84 }
86 # TODO
87 # füge den server selbst zu known_server hinzu, msgs können nämlich auch von sich selbst kommen (gosa!!!)
90 # register at bus
91 if ($main::no_bus > 0) {
92     $bus_activ = "off"
93 }
94 if($bus_activ eq "on") {
95     &main::daemon_log(" ", 1);
96     &register_at_bus();
97 }
99 ### functions #################################################################
101 #sub get_module_tags {
102 #    
103 #    # lese config file aus dort gibt es eine section Basic
104 #    # dort stehen drei packettypen, für die sich das modul anmelden kann, gosa-admin-packages, 
105 #    #   server-packages, client-packages
106 #    my %tag_hash = (gosa_admin_packages => "yes", 
107 #                    server_packages => "yes", 
108 #                    client_packages => "yes",
109 #                    );
110 #    return \%tag_hash;
111 #}
114 sub get_module_info {
115     my @info = ($server_address,
116                 $server_passwd,
117                 $server,
118                 $server_activ,
119                 "socket",
120                 );
121     return \@info;
125 #===  FUNCTION  ================================================================
126 #         NAME:  read_configfile
127 #   PARAMETERS:  cfg_file - string -
128 #      RETURNS:  nothing
129 #  DESCRIPTION:  read cfg_file and set variables
130 #===============================================================================
131 sub read_configfile {
132     my $cfg;
133     if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
134         if( -r $main::cfg_file ) {
135             $cfg = Config::IniFiles->new( -file => $main::cfg_file );
136         } else {
137             print STDERR "Couldn't read config file!";
138         }
139     } else {
140         $cfg = Config::IniFiles->new() ;
141     }
142     foreach my $section (keys %cfg_defaults) {
143         foreach my $param (keys %{$cfg_defaults{ $section }}) {
144             my $pinfo = $cfg_defaults{ $section }{ $param };
145             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
146         }
147     }
149     # Read non predefined sections
150     my $param;
151     if ($cfg->SectionExists('ldap')){
152                 foreach $param ($cfg->Parameters('ldap')){
153                         push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
154                 }
155     }
156     if ($cfg->SectionExists('pam_ldap')){
157                 foreach $param ($cfg->Parameters('pam_ldap')){
158                         push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
159                 }
160     }
161     if ($cfg->SectionExists('nss_ldap')){
162                 foreach $param ($cfg->Parameters('nss_ldap')){
163                         push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
164                 }
165     }
166     if ($cfg->SectionExists('goto')){
167         $goto_admin= $cfg->val('goto', 'terminal_admin');
168         $goto_secret= $cfg->val('goto', 'terminal_secret');
169     } else {
170         $goto_admin= undef;
171         $goto_secret= undef;
172     }
176 #===  FUNCTION  ================================================================
177 #         NAME:  get_interface_for_ip
178 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
179 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
180 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
181 #===============================================================================
182 sub get_interface_for_ip {
183         my $result;
184         my $ip= shift;
185         if ($ip && length($ip) > 0) {
186                 my @ifs= &get_interfaces();
187                 if($ip eq "0.0.0.0") {
188                         # TODO
189                 } else {
190                         foreach (@ifs) {
191                                 my $if=$_;
192                                 if(get_ip($if) eq $ip) {
193                                         $result = $if;
194                                 }
195                         }       
196                 }
197         }       
198         return $result;
201 #===  FUNCTION  ================================================================
202 #         NAME:  get_interfaces 
203 #   PARAMETERS:  none
204 #      RETURNS:  (list of interfaces) 
205 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
206 #===============================================================================
207 sub get_interfaces {
208         my @result;
209         my $PROC_NET_DEV= ('/proc/net/dev');
211         open(PROC_NET_DEV, "<$PROC_NET_DEV")
212                 or die "Could not open $PROC_NET_DEV";
214         my @ifs = <PROC_NET_DEV>;
216         close(PROC_NET_DEV);
218         # Eat first two line
219         shift @ifs;
220         shift @ifs;
222         chomp @ifs;
223         foreach my $line(@ifs) {
224                 my $if= (split /:/, $line)[0];
225                 $if =~ s/^\s+//;
226                 push @result, $if;
227         }
229         return @result;
232 #===  FUNCTION  ================================================================
233 #         NAME:  get_mac 
234 #   PARAMETERS:  interface name (i.e. eth0)
235 #      RETURNS:  (mac address) 
236 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
237 #===============================================================================
238 sub get_mac {
239         my $ifreq= shift;
240         my $result;
241         if ($ifreq && length($ifreq) > 0) { 
242                 my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
244                 # A configured MAC Address should always override a guessed value
245                 if ($server_mac_address and length($server_mac_address) > 0) {
246                         return $server_mac_address;
247                 }
249                 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
250                         or die "socket: $!";
252                 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
253                         my ($if, $mac)= unpack 'h36 H12', $ifreq;
255                         if (length($mac) > 0) {
256                                 $result = $mac
257                         }
258                 }
259         }
260         return $result;
263 #===  FUNCTION  ================================================================
264 #         NAME:  get_ip 
265 #   PARAMETERS:  interface name (i.e. eth0)
266 #      RETURNS:  (ip address) 
267 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
268 #===============================================================================
269 sub get_ip {
270         my $ifreq= shift;
271         my $result= "";
272         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
273         my $proto= getprotobyname('ip');
275         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
276                 or die "socket: $!";
278         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
279                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
280                 my ($port, $addr) = sockaddr_in $sin;
281                 my $ip            = inet_ntoa $addr;
283                 if ($ip && length($ip) > 0) {
284                         $result = $ip;
285                 }
286         }
288         return $result;
291 #===  FUNCTION  ================================================================
292 #         NAME:  open_socket
293 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
294 #                [PeerPort] string necessary if port not appended by PeerAddr
295 #      RETURNS:  socket IO::Socket::INET
296 #  DESCRIPTION:  open a socket to PeerAddr
297 #===============================================================================
298 sub open_socket {
299     my ($PeerAddr, $PeerPort) = @_ ;
300     if(defined($PeerPort)){
301         $PeerAddr = $PeerAddr.":".$PeerPort;
302     }
303     my $socket;
304     $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
305             Porto => "tcp" ,
306             Type => SOCK_STREAM,
307             Timeout => 5,
308             );
309     if(not defined $socket) {
310         return;
311     }
312     &main::daemon_log("open_socket to: $PeerAddr", 7);
313     return $socket;
316 #===  FUNCTION  ================================================================
317 #         NAME:  register_at_bus
318 #   PARAMETERS:  nothing
319 #      RETURNS:  nothing
320 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
321 #===============================================================================
322 sub register_at_bus {
324     # add bus to known_server_db
325     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
326                                                     primkey=>'hostname',
327                                                     hostname=>$bus_address,
328                                                     status=>'bus',
329                                                     hostkey=>$bus_passwd,
330                                                     timestamp=>&get_time,
331                                                 } );
332 #    if ($res == 3) {
333 #        my $update_hash = { table=>'known_server' };
334 #        $update_hash->{where} = [ { hostname=>[$bus_address] } ];
335 #        $update_hash->{update} = [ { 
336 #            hostkey=>[$bus_passwd],
337 #            timestamp=>[&get_time],
338 #        } ];
339 #        $res = $main::known_server_db->update_dbentry( $update_hash );
340 #    }
342     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
343     my $answer = "";
344     $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
345     if ($answer == 0) {
346         &main::daemon_log("register at bus: $bus_address", 1);
347     } else {
348         &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
349     }
350     return;
353 #===  FUNCTION  ================================================================
354 #         NAME:  process_incoming_msg
355 #   PARAMETERS:  crypted_msg - string - incoming crypted message
356 #      RETURNS:  nothing
357 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
358 #===============================================================================
359 sub process_incoming_msg {
360     my ($crypted_msg) = @_ ;
361     if(not defined $crypted_msg) {
362         &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
363     }
365     &main::daemon_log("ServerPackages: incoming msg: \n$crypted_msg", 7);
367     $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
368     $crypted_msg = $1;
369     my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
371     my $msg;
372     my $msg_hash;
373     my $host_name;
374     my $host_key;
376     # check wether incoming msg is a new msg
377     $host_name = $server_address;
378     $host_key = $server_passwd;
379     &main::daemon_log("ServerPackage: host_name: $host_name", 7);
380     &main::daemon_log("ServerPackage: host_key: $host_key", 7);
381     eval{
382         my $key_cipher = &create_ciphering($host_key);
383         $msg = &decrypt_msg($crypted_msg, $key_cipher);
384         $msg_hash = &transform_msg2hash($msg);
385     };
386     if($@) {
387         &main::daemon_log("ServerPackage: deciphering raise error", 7);
388         &main::daemon_log("$@", 8);
389         $msg = undef;
390         $msg_hash = undef;
391         $host_name = undef;
392         $host_key = undef;
393     } 
395     # check wether incoming msg is from a known_server
396     if( not defined $msg ) {
397         my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} ); 
398         while( my ($hit_num, $hit) = each %{ $query_res } ) {  
399             $host_name = $hit->{hostname};
400             if( not $host_name =~ "^$host") {
401                 next;
402             }
403             $host_key = $hit->{hostkey};
404             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
405             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
406             eval{
407                 my $key_cipher = &create_ciphering($host_key);
408                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
409                 $msg_hash = &transform_msg2hash($msg);
410             };
411             if($@) {
412                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
413                 &main::daemon_log("$@", 8);
414                 $msg = undef;
415                 $msg_hash = undef;
416                 $host_name = undef;
417                 $host_key = undef;
418             } else {
419                 last;
420             }
421         }
422     }
424     # check wether incoming msg is from a known_client
425     if( not defined $msg ) {
426         my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
427         while( my ($hit_num, $hit) = each %{ $query_res } ) {    
428             $host_name = $hit->{hostname};
429             if( not $host_name =~ "^$host") {
430                 next;
431             }
432             $host_key = $hit->{hostkey};
433             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
434             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
435             eval{
436                 my $key_cipher = &create_ciphering($host_key);
437                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
438                 $msg_hash = &transform_msg2hash($msg);
439             };
440             if($@) {
441                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
442                 &main::daemon_log("$@", 8);
443                 $msg = undef;
444                 $msg_hash = undef;
445                 $host_name = undef;
446                 $host_key = undef;
447             } else {
448                 last;
449             }
450         }
451     }
453     if( not defined $msg ) {
454         &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
455         &main::daemon_log("$@", 7);
456         return;
457     }
459     # process incoming msg
460     my $header = @{$msg_hash->{header}}[0]; 
461     my $source = @{$msg_hash->{source}}[0];
463     &main::daemon_log("recieve '$header' at ServerPackages from $host", 1);
464     &main::daemon_log("ServerPackages: msg to process: \n$msg", 5);
466     my @targets = @{$msg_hash->{target}};
467     my $len_targets = @targets;
468     if ($len_targets == 0){     
469         &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
471     }  elsif ($len_targets == 1){
472         # we have only one target symbol
473         my $target = $targets[0];
474         &main::daemon_log("SeverPackages: msg is for: $target", 7);
476         if ($target eq $server_address) {
477             # msg is for server
478             if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
479             elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
480             elsif ($header eq 'who_has') { &who_has($msg_hash) }
481             elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
482             elsif ($header eq 'update_status') { &update_status($msg_hash) }
483             elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
484             elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
485             else { &main::daemon_log("ERROR: ServerPackages: no function assigned to this msg", 5) }
487        } elsif ($target eq "*") {
488             # msg is for all clients
489             my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
490             while( my ($hit_num, $hit) = each %{ $query_res } ) {    
491                 $host_name = $hit->{hostname};
492                 $host_key = $hit->{hostkey};
493                 $msg_hash->{target} = [$host_name];
494                 &send_msg_hash2address($msg_hash, $host_name, $host_key);
495             }
496             return;
497          
498         } else {
499             # msg is for one host
500             my $query_res;
501             $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} );
502             if( 1 == keys %{$query_res} ) {
503                 $host_key = $query_res->{1}->{host_key};
504                 &send_msg_hash2address($msg_hash, $target, $host_key);
505                 return;
506             }
508             $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} );
509             if( 1 == keys %{$query_res} ) {
510                 $host_key = $query_res->{1}->{host_key};
511                 &send_msg_hash2address($msg_hash, $target, $host_key);
512                 return;
513             }
515             &main::daemon_log("ERROR: ServerPackages: target '$target' is not known neither in known_clients nor in known_server",1);
516             return;
517         }
519     } elsif ($len_targets > 1 ) {
520         # we have more than one target 
521         return;
522     }
524     return ;
528 #===  FUNCTION  ================================================================
529 #         NAME:  got_ping
530 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
531 #      RETURNS:  nothing
532 #  DESCRIPTION:  process this incoming message
533 #===============================================================================
534 sub got_ping {
535     my ($msg_hash) = @_;
536     
537     my $source = @{$msg_hash->{source}}[0];
538     my $target = @{$msg_hash->{target}}[0];
539     my $header = @{$msg_hash->{header}}[0];
540     
541     if(exists $main::known_daemons->{$source}) {
542         &main::add_content2known_daemons(hostname=>$source, status=>$header);
543     } else {
544         &main::add_content2known_clients(hostname=>$source, status=>$header);
545     }
546     
547     return;
551 #===  FUNCTION  ================================================================
552 #         NAME:  new_passwd
553 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
554 #      RETURNS:  nothing
555 #  DESCRIPTION:  process this incoming message
556 #===============================================================================
557 sub new_passwd {
558     my ($msg_hash) = @_;
560     my $header = @{$msg_hash->{header}}[0];
561     my $source_name = @{$msg_hash->{source}}[0];
562     my $source_key = @{$msg_hash->{new_passwd}}[0];
563     my $query_res;
565     # check known_clients_db
566     $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$source_name} );
567     if( 1 == keys %{$query_res} ) {
568         my $update_hash = { table=>'known_clients' };
569         $update_hash->{where} = [ { hostname=>[$source_name] } ];
570         $update_hash->{update} = [ {
571             hostkey=>[$source_key],
572             timestamp=>[&get_time],
573         } ];
574         my $res = $main::known_clients_db->update_dbentry( $update_hash );
576         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
577         &send_msg_hash2address($hash, $source_name, $source_key);
578         return;
579     }
581     # check known_server_db
582     $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$source_name } );
583     if( 1 == keys %{$query_res} ) {
584         my $update_hash = { table=>'known_server' };
585         $update_hash->{where} = [ { hostname=>[$source_name] } ];
586         $update_hash->{update} = [ {
587             hostkey=>[$source_key],
588                 timestamp=>[&get_time],
589         } ];
590         my $res = $main::known_server_db->update_dbentry( $update_hash );
592         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
593         &send_msg_hash2address($hash, $source_name, $source_key);
594         return;
595     }
597     &main::daemon_log("ERROR: $source_name not known for '$header'-msg", 1);
598     return;
602 sub send_msg_hash {
603     my ($hash, $host_name, $host_key);
605     
606     my $answer = &send_msg_hash2address($hash, $host_name, $host_key);
607     
608     return;
612 #===  FUNCTION  ================================================================
613 #         NAME:  here_i_am
614 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
615 #      RETURNS:  nothing
616 #  DESCRIPTION:  process this incoming message
617 #===============================================================================
618 sub here_i_am {
619     my ($msg_hash) = @_;
621     my $source = @{$msg_hash->{source}}[0];
622     my $mac_address = @{$msg_hash->{mac_address}}[0];
623     my $out_hash;
625     # number of known clients
626     my $nu_clients = keys %{$main::known_clients_db->select_dbentry( {table=>'known_clients'} )};
628     # check wether client address or mac address is already known
629     if (exists $main::known_clients->{$source}) {
630         &main::daemon_log("WARNING: $source is already known as a client", 1);
631         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
632         $nu_clients --;
633     }
635     # number of actual activ clients
636     my $act_nu_clients = $nu_clients;
638     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
639     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
641     if($max_clients <= $act_nu_clients) {
642         my $out_hash = &create_xml_hash("denied", $server_address, $source);
643         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
644         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
645         &send_msg_hash2address($out_hash, $source, $passwd);
646         return;
647     }
648     
649     # new client accepted
650     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
652     # create entry in known_clients
653     my $events = @{$msg_hash->{events}}[0];
654     
655     # add entry to known_clients_db
656     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
657                                                 primkey=>'hostname',
658                                                 hostname=>$source,
659                                                 events=>$events,
660                                                 macaddress=>$mac_address,
661                                                 status=>'registered',
662                                                 hostkey=>$new_passwd,
663                                                 timestamp=>&get_time,
664                                                 } );
666     if ($res != 0)  {
667         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
668         return;
669     }
670     
671     # return acknowledgement to client
672     $out_hash = &create_xml_hash("registered", $server_address, $source);
673     &send_msg_hash2address($out_hash, $source, $new_passwd);
675     # notify registered client to bus
676     if( $bus_activ eq "on") {
677         # fetch actual bus key
678         my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} );
679         my $hostkey = $query_res->{1}->{hostkey};
680         
681         # send update msg to bus
682         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
683         &send_msg_hash2address($out_hash, $bus_address, $hostkey);
684         
685         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
686     }
688     # give the new client his ldap config
689     &new_ldap_config($source);
691     return;
695 #===  FUNCTION  ================================================================
696 #         NAME:  who_has
697 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
698 #      RETURNS:  nothing 
699 #  DESCRIPTION:  process this incoming message
700 #===============================================================================
701 sub who_has {
702     my ($msg_hash) = @_ ;
703     
704     # what is your search pattern
705     my $search_pattern = @{$msg_hash->{who_has}}[0];
706     my $search_element = @{$msg_hash->{$search_pattern}}[0];
707     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
709     # scanning known_clients for search_pattern
710     my @host_addresses = keys %$main::known_clients;
711     my $known_clients_entries = length @host_addresses;
712     my $host_address;
713     foreach my $host (@host_addresses) {
714         my $client_element = $main::known_clients->{$host}->{$search_pattern};
715         if ($search_element eq $client_element) {
716             $host_address = $host;
717             last;
718         }
719     }
720         
721     # search was successful
722     if (defined $host_address) {
723         my $source = @{$msg_hash->{source}}[0];
724         my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
725         &add_content2xml_hash($out_msg, "mac_address", $search_element);
726         &send_msg_hash2address($out_msg, $bus_address);
727     }
728     return;
732 sub who_has_i_do {
733     my ($msg_hash) = @_ ;
734     my $header = @{$msg_hash->{header}}[0];
735     my $source = @{$msg_hash->{source}}[0];
736     my $search_param = @{$msg_hash->{$header}}[0];
737     my $search_value = @{$msg_hash->{$search_param}}[0];
738     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
742 #===  FUNCTION  ================================================================
743 #         NAME:  new_ldap_config
744 #   PARAMETERS:  address - string - ip address and port of a host
745 #      RETURNS:  nothing
746 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
747 #===============================================================================
748 sub new_ldap_config {
749     my ($address) = @_ ;
750     
751     my $res = $main::known_clients_db->select_dbentry( { table=>'known_clients', hostname=>$address } );
753     # check hit
754     my $hit_counter = keys %{$res};
755     if( not $hit_counter == 1 ) {
756         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
757     }
759     my $macaddress = $res->{1}->{macaddress};
760     my $hostkey = $res->{1}->{hostkey};
762     if (not defined $macaddress) {
763         &main::daemon_log("ERROR: no mac address found for client $address", 1);
764         return;
765     }
767     # Build LDAP connection
768     my $ldap = Net::LDAP->new($ldap_uri);
769     if( not defined $ldap ) {
770         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
771         return;
772     } 
775     # Bind to a directory with dn and password
776     my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
778     # Perform search
779     $mesg = $ldap->search( base   => $ldap_base,
780                     scope  => 'sub',
781                     attrs => ['dn', 'gotoLdapServer'],
782                     filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
783     $mesg->code && die $mesg->error;
785     # Sanity check
786     if ($mesg->count != 1) {
787             &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
788         &main::daemon_log("\tbase: $ldap_base", 1);
789         &main::daemon_log("\tscope: sub", 1);
790         &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
791         &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
792             return;
793     }
795     my $entry= $mesg->entry(0);
796     my $dn= $entry->dn;
797     my @servers= $entry->get_value("gotoLdapServer");
798     my @ldap_uris;
799     my $server;
800     my $base;
802     # Do we need to look at an object class?
803     if ($#servers < 1){
804             $mesg = $ldap->search( base   => $ldap_base,
805                             scope  => 'sub',
806                             attrs => ['dn', 'gotoLdapServer'],
807                             filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
808             $mesg->code && die $mesg->error;
810             # Sanity check
811             if ($mesg->count != 1) {
812                     &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
813                     return;
814             }
816             $entry= $mesg->entry(0);
817             $dn= $entry->dn;
818             @servers= $entry->get_value("gotoLdapServer");
819     }
821     @servers= sort (@servers);
823     foreach $server (@servers){
824             $base= $server;
825             $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
826             $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
827             push (@ldap_uris, $server);
828     }
830     # Unbind
831     $mesg = $ldap->unbind;
833     # Assemble data package
834     my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
835                      'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
837     # Need to append GOto settings?
838     if (defined $goto_admin and defined $goto_secret){
839             $data{'goto_admin'}= $goto_admin;
840             $data{'goto_secret'}= $goto_secret;
841     }
843     # Send information
844     send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
846     return;
850 #===  FUNCTION  ================================================================
851 #         NAME:  execute_actions
852 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
853 #      RETURNS:  nothing
854 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
855 #                /etc/gosad/actions
856 #===============================================================================
857 sub execute_actions {
858     my ($msg_hash) = @_ ;
859     my $configdir= '/etc/gosad/actions/';
860     my $result;
862     my $header = @{$msg_hash->{header}}[0];
863     my $source = @{$msg_hash->{source}}[0];
864     my $target = @{$msg_hash->{target}}[0];
865  
866     if((not defined $source)
867             && (not defined $target)
868             && (not defined $header)) {
869         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
870     } else {
871         my $parameters="";
872         my @params = @{$msg_hash->{$header}};
873         my $params = join(", ", @params);
874         &main::daemon_log("execute_actions: got parameters: $params", 5);
876         if (@params) {
877             foreach my $param (@params) {
878                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
879                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
880                 $parameters.= " ".$param_value;
881             }
882         }
884         my $cmd= $configdir.$header."$parameters";
885         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
886         $result= "";
887         open(PIPE, "$cmd 2>&1 |");
888         while(<PIPE>) {
889             $result.=$_;
890         }
891         close(PIPE);
892     }
894     # process the event result
897     return;
901 1;