Code

80ce0ada7ea9b34d085b9bdf44aece6257816bdd
[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 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 my $server_address = "$server_ip:$server_port";
63 my $bus_address = "$bus_ip:$bus_port";
65 # create general settings for this module
66 my $xml = new XML::Simple();
68 # register at bus
69 if ($main::no_bus > 0) {
70     $bus_activ = "off"
71 }
72 if($bus_activ eq "on") {
73     &register_at_bus();
74 }
76 ### functions #################################################################
79 sub get_module_info {
80     my @info = ($server_address,
81                 $server_passwd,
82                 $server,
83                 $server_activ,
84                 "socket",
85                 );
86     return \@info;
87 }
90 #===  FUNCTION  ================================================================
91 #         NAME:  read_configfile
92 #   PARAMETERS:  cfg_file - string -
93 #      RETURNS:  nothing
94 #  DESCRIPTION:  read cfg_file and set variables
95 #===============================================================================
96 sub read_configfile {
97     my $cfg;
98     if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
99         if( -r $main::cfg_file ) {
100             $cfg = Config::IniFiles->new( -file => $main::cfg_file );
101         } else {
102             print STDERR "Couldn't read config file!";
103         }
104     } else {
105         $cfg = Config::IniFiles->new() ;
106     }
107     foreach my $section (keys %cfg_defaults) {
108         foreach my $param (keys %{$cfg_defaults{ $section }}) {
109             my $pinfo = $cfg_defaults{ $section }{ $param };
110             ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
111         }
112     }
114     # Read non predefined sections
115     my $param;
116     if ($cfg->SectionExists('ldap')){
117                 foreach $param ($cfg->Parameters('ldap')){
118                         push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
119                 }
120     }
121     if ($cfg->SectionExists('pam_ldap')){
122                 foreach $param ($cfg->Parameters('pam_ldap')){
123                         push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
124                 }
125     }
126     if ($cfg->SectionExists('nss_ldap')){
127                 foreach $param ($cfg->Parameters('nss_ldap')){
128                         push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
129                 }
130     }
131     if ($cfg->SectionExists('goto')){
132         $goto_admin= $cfg->val('goto', 'terminal_admin');
133         $goto_secret= $cfg->val('goto', 'terminal_secret');
134     } else {
135         $goto_admin= undef;
136         $goto_secret= undef;
137     }
141 #===  FUNCTION  ================================================================
142 #         NAME:  get_interface_for_ip
143 #   PARAMETERS:  ip address (i.e. 192.168.0.1)
144 #      RETURNS:  array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
145 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
146 #===============================================================================
147 sub get_interface_for_ip {
148         my $result;
149         my $ip= shift;
150         if ($ip && length($ip) > 0) {
151                 my @ifs= &get_interfaces();
152                 if($ip eq "0.0.0.0") {
153                         $result = "all";
154                 } else {
155                         foreach (@ifs) {
156                                 my $if=$_;
157                                 if(get_ip($if) eq $ip) {
158                                         $result = $if;
159                                 }
160                         }       
161                 }
162         }       
163         return $result;
166 #===  FUNCTION  ================================================================
167 #         NAME:  get_interfaces 
168 #   PARAMETERS:  none
169 #      RETURNS:  (list of interfaces) 
170 #  DESCRIPTION:  Uses proc fs (/proc/net/dev) to get list of interfaces.
171 #===============================================================================
172 sub get_interfaces {
173         my @result;
174         my $PROC_NET_DEV= ('/proc/net/dev');
176         open(PROC_NET_DEV, "<$PROC_NET_DEV")
177                 or die "Could not open $PROC_NET_DEV";
179         my @ifs = <PROC_NET_DEV>;
181         close(PROC_NET_DEV);
183         # Eat first two line
184         shift @ifs;
185         shift @ifs;
187         chomp @ifs;
188         foreach my $line(@ifs) {
189                 my $if= (split /:/, $line)[0];
190                 $if =~ s/^\s+//;
191                 push @result, $if;
192         }
194         return @result;
197 #===  FUNCTION  ================================================================
198 #         NAME:  get_mac 
199 #   PARAMETERS:  interface name (i.e. eth0)
200 #      RETURNS:  (mac address) 
201 #  DESCRIPTION:  Uses ioctl to get mac address directly from system.
202 #===============================================================================
203 sub get_mac {
204         my $ifreq= shift;
205         my $result;
206         if ($ifreq && length($ifreq) > 0) { 
207                 if($ifreq eq "all") {
208                         $result = "00:00:00:00:00:00";
209                 } else {
210                         my $SIOCGIFHWADDR= 0x8927;     # man 2 ioctl_list
212                         # A configured MAC Address should always override a guessed value
213                         if ($server_mac_address and length($server_mac_address) > 0) {
214                                 $result= $server_mac_address;
215                         }
217                         socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
218                                 or die "socket: $!";
220                         if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
221                                 my ($if, $mac)= unpack 'h36 H12', $ifreq;
223                                 if (length($mac) > 0) {
224                                         $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])$/;
225                                         $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
226                                         $result = $mac;
227                                 }
228                         }
229                 }
230         }
231         return $result;
234 #===  FUNCTION  ================================================================
235 #         NAME:  get_ip 
236 #   PARAMETERS:  interface name (i.e. eth0)
237 #      RETURNS:  (ip address) 
238 #  DESCRIPTION:  Uses ioctl to get ip address directly from system.
239 #===============================================================================
240 sub get_ip {
241         my $ifreq= shift;
242         my $result= "";
243         my $SIOCGIFADDR= 0x8915;       # man 2 ioctl_list
244         my $proto= getprotobyname('ip');
246         socket SOCKET, PF_INET, SOCK_DGRAM, $proto
247                 or die "socket: $!";
249         if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
250                 my ($if, $sin)    = unpack 'a16 a16', $ifreq;
251                 my ($port, $addr) = sockaddr_in $sin;
252                 my $ip            = inet_ntoa $addr;
254                 if ($ip && length($ip) > 0) {
255                         $result = $ip;
256                 }
257         }
259         return $result;
262 #===  FUNCTION  ================================================================
263 #         NAME:  open_socket
264 #   PARAMETERS:  PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
265 #                [PeerPort] string necessary if port not appended by PeerAddr
266 #      RETURNS:  socket IO::Socket::INET
267 #  DESCRIPTION:  open a socket to PeerAddr
268 #===============================================================================
269 #sub open_socket {
270 #    my ($PeerAddr, $PeerPort) = @_ ;
271 #    if(defined($PeerPort)){
272 #        $PeerAddr = $PeerAddr.":".$PeerPort;
273 #    }
274 #    my $socket;
275 #    $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
276 #            Porto => "tcp" ,
277 #            Type => SOCK_STREAM,
278 #            Timeout => 5,
279 #            );
280 #    if(not defined $socket) {
281 #        return;
282 #    }
283 #    &main::daemon_log("open_socket to: $PeerAddr", 7);
284 #    return $socket;
285 #}
287 #===  FUNCTION  ================================================================
288 #         NAME:  register_at_bus
289 #   PARAMETERS:  nothing
290 #      RETURNS:  nothing
291 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
292 #===============================================================================
293 sub register_at_bus {
295     # add bus to known_server_db
296     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
297                                                     primkey=>'hostname',
298                                                     hostname=>$bus_address,
299                                                     status=>'bus',
300                                                     hostkey=>$bus_passwd,
301                                                     timestamp=>&get_time,
302                                                 } );
303     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
304     my $answer = "";
305     $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
306     if ($answer == 0) {
307         &main::daemon_log("register at bus: $bus_address", 1);
308     } else {
309         &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
310     }
311     return;
314 #===  FUNCTION  ================================================================
315 #         NAME:  process_incoming_msg
316 #   PARAMETERS:  crypted_msg - string - incoming crypted message
317 #      RETURNS:  nothing
318 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
319 #===============================================================================
320 sub process_incoming_msg {
321     my ($crypted_msg) = @_ ;
322     if(not defined $crypted_msg) {
323         &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
324     }
326     &main::daemon_log("ServerPackages: incoming msg: \n$crypted_msg", 8);
328     $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
329     $crypted_msg = $1;
330         my $host="0.0.0.0";
331         if($1 && $2 && $3 && $4) {
332                 $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
333         }
335     my $msg;
336     my $msg_hash;
337     my $host_name;
338     my $host_key;
340     # check wether incoming msg is a new msg
341     $host_name = $server_address;
342     $host_key = $server_passwd;
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     } 
359     # check wether incoming msg is from a known_server
360     if( not defined $msg ) {
361         #my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} ); 
362         my $sql_statement= "SELECT * FROM known_server";
363         my $query_res = $main::known_server_db->select_dbentry( $sql_statement ); 
364         while( my ($hit_num, $hit) = each %{ $query_res } ) {  
365             $host_name = $hit->{hostname};
366             if( not $host_name =~ "^$host") {
367                 next;
368             }
369             $host_key = $hit->{hostkey};
370             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
371             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
372             eval{
373                 my $key_cipher = &create_ciphering($host_key);
374                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
375                 $msg_hash = &transform_msg2hash($msg);
376             };
377             if($@) {
378                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
379                 &main::daemon_log("$@", 8);
380                 $msg = undef;
381                 $msg_hash = undef;
382                 $host_name = undef;
383                 $host_key = undef;
384             } else {
385                 last;
386             }
387         }
388     }
390     # check wether incoming msg is from a known_client
391     if( not defined $msg ) {
392         #my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
393         my $sql_statement= "SELECT * FROM known_clients";
394         my $query_res = $main::known_clients_db->select_dbentry( $sql_statement ); 
395         while( my ($hit_num, $hit) = each %{ $query_res } ) {    
396             $host_name = $hit->{hostname};
397             if( not $host_name =~ "^$host") {
398                 next;
399             }
400             $host_key = $hit->{hostkey};
401             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
402             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
403             eval{
404                 my $key_cipher = &create_ciphering($host_key);
405                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
406                 $msg_hash = &transform_msg2hash($msg);
407             };
408             if($@) {
409                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
410                 &main::daemon_log("$@", 8);
411                 $msg = undef;
412                 $msg_hash = undef;
413                 $host_name = undef;
414                 $host_key = undef;
415             } else {
416                 last;
417             }
418         }
419     }
421     if( not defined $msg ) {
422         &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
423         &main::daemon_log("$@", 8);
424         return;
425     }
427     # process incoming msg
428     my $header = @{$msg_hash->{header}}[0]; 
429     my $source = @{$msg_hash->{source}}[0];
431     &main::daemon_log("receive '$header' at ServerPackages from $host", 1);
432     &main::daemon_log("ServerPackages: msg to process: \n$msg", 5);
434     my @targets = @{$msg_hash->{target}};
435     my $len_targets = @targets;
436     if ($len_targets == 0){     
437         &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
439     }  elsif ($len_targets == 1){
440         # we have only one target symbol
441         my $target = $targets[0];
442         &main::daemon_log("SeverPackages: msg is for: $target", 7);
444         # msg is for server
445         if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
446         elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
447         elsif ($header eq 'who_has') { &who_has($msg_hash) }
448         elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
449         elsif ($header eq 'update_status') { &update_status($msg_hash) }
450         elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
451         elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
452         else { 
453             if ($target eq "*") {
454                 # msg is for all clients
455                 my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
456                 while( my ($hit_num, $hit) = each %{ $query_res } ) {    
457                     $host_name = $hit->{hostname};
458                     $host_key = $hit->{hostkey};
459                     $msg_hash->{target} = [$host_name];
460                     &send_msg_hash2address($msg_hash, $host_name, $host_key);
461                 }
463             } else {
464                 # msg is for one host
465                 my $host_key;
468                 if( not defined $host_key ) {
469                     my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} );
470                     if( 1 == keys %{$query_res} ) {
471                         $host_key = $query_res->{1}->{host_key};
472                     }
473                 } 
475                 if( not defined $host_key ) {
476                     my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} );
477                     if( 1 == keys %{$query_res} ) {
478                         $host_key = $query_res->{1}->{host_key};
479                     }
480                 }
482                 if( not defined $host_key ) { 
483                     &main::daemon_log("ERROR: ServerPackages: target '".$target.
484                             "' is not known neither in known_clients nor in known_server",1);
485                 } else {
486                     &send_msg_hash2address($msg_hash, $target, $host_key);
487                 }               
488             }
489         }
491     } elsif ($len_targets > 1 ) {
492         # we have more than one target 
493         # TODO to be implemented
494     }
496     return ;
500 #===  FUNCTION  ================================================================
501 #         NAME:  got_ping
502 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
503 #      RETURNS:  nothing
504 #  DESCRIPTION:  process this incoming message
505 #===============================================================================
506 sub got_ping {
507     my ($msg_hash) = @_;
508     
509     my $source = @{$msg_hash->{source}}[0];
510     my $target = @{$msg_hash->{target}}[0];
511     my $header = @{$msg_hash->{header}}[0];
512     
513     if(exists $main::known_daemons->{$source}) {
514         &main::add_content2known_daemons(hostname=>$source, status=>$header);
515     } else {
516         &main::add_content2known_clients(hostname=>$source, status=>$header);
517     }
518     
519     return;
523 #===  FUNCTION  ================================================================
524 #         NAME:  new_passwd
525 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
526 #      RETURNS:  nothing
527 #  DESCRIPTION:  process this incoming message
528 #===============================================================================
529 sub new_passwd {
530     my ($msg_hash) = @_;
532     my $header = @{$msg_hash->{header}}[0];
533     my $source_name = @{$msg_hash->{source}}[0];
534     my $source_key = @{$msg_hash->{new_passwd}}[0];
535     my $query_res;
537     # check known_clients_db
538     $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$source_name} );
539     if( 1 == keys %{$query_res} ) {
540         my $update_hash = { table=>'known_clients' };
541         $update_hash->{where} = [ { hostname=>[$source_name] } ];
542         $update_hash->{update} = [ {
543             hostkey=>[$source_key],
544             timestamp=>[&get_time],
545         } ];
546         my $res = $main::known_clients_db->update_dbentry( $update_hash );
548         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
549         &send_msg_hash2address($hash, $source_name, $source_key);
550         return;
551     }
553     # check known_server_db
554     $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$source_name } );
555     if( 1 == keys %{$query_res} ) {
556         my $update_hash = { table=>'known_server' };
557         $update_hash->{where} = [ { hostname=>[$source_name] } ];
558         $update_hash->{update} = [ {
559             hostkey=>[$source_key],
560                 timestamp=>[&get_time],
561         } ];
562         my $res = $main::known_server_db->update_dbentry( $update_hash );
564         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
565         &send_msg_hash2address($hash, $source_name, $source_key);
566         return;
567     }
569     &main::daemon_log("ERROR: $source_name not known for '$header'-msg", 1);
570     return;
574 sub send_msg_hash {
575     my ($hash, $host_name, $host_key);
577     
578     my $answer = &send_msg_hash2address($hash, $host_name, $host_key);
579     
580     return;
584 #===  FUNCTION  ================================================================
585 #         NAME:  here_i_am
586 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
587 #      RETURNS:  nothing
588 #  DESCRIPTION:  process this incoming message
589 #===============================================================================
590 sub here_i_am {
591     my ($msg_hash) = @_;
593     my $source = @{$msg_hash->{source}}[0];
594     my $mac_address = @{$msg_hash->{mac_address}}[0];
595     my $out_hash;
597     # number of known clients
598     my $nu_clients = keys %{ $main::known_clients_db->select_dbentry( {table=>'known_clients'} ) };
600     # check wether client address or mac address is already known
601     if (exists $main::known_clients->{$source}) {
602         &main::daemon_log("WARNING: $source is already known as a client", 1);
603         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
604         $nu_clients --;
605     }
607     # number of actual activ clients
608     my $act_nu_clients = $nu_clients;
610     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
611     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
613     if($max_clients <= $act_nu_clients) {
614         my $out_hash = &create_xml_hash("denied", $server_address, $source);
615         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
616         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
617         &send_msg_hash2address($out_hash, $source, $passwd);
618         return;
619     }
620     
621     # new client accepted
622     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
624     # create entry in known_clients
625     my $events = @{$msg_hash->{events}}[0];
626     
627     # add entry to known_clients_db
628     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
629                                                 primkey=>'hostname',
630                                                 hostname=>$source,
631                                                 events=>$events,
632                                                 macaddress=>$mac_address,
633                                                 status=>'registered',
634                                                 hostkey=>$new_passwd,
635                                                 timestamp=>&get_time,
636                                                 } );
638     if ($res != 0)  {
639         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
640         return;
641     }
642     
643     # return acknowledgement to client
644     $out_hash = &create_xml_hash("registered", $server_address, $source);
645     &send_msg_hash2address($out_hash, $source, $new_passwd);
647     # notify registered client to bus
648     if( $bus_activ eq "on") {
649         # fetch actual bus key
650         my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} );
651         my $hostkey = $query_res->{1}->{hostkey};
652         
653         # send update msg to bus
654         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
655         &send_msg_hash2address($out_hash, $bus_address, $hostkey);
656         
657         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
658     }
660     # give the new client his ldap config
661     &new_ldap_config($source);
663     return;
667 #===  FUNCTION  ================================================================
668 #         NAME:  who_has
669 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
670 #      RETURNS:  nothing 
671 #  DESCRIPTION:  process this incoming message
672 #===============================================================================
673 sub who_has {
674     my ($msg_hash) = @_ ;
675     
676     # what is your search pattern
677     my $search_pattern = @{$msg_hash->{who_has}}[0];
678     my $search_element = @{$msg_hash->{$search_pattern}}[0];
679     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
681     # scanning known_clients for search_pattern
682     my @host_addresses = keys %$main::known_clients;
683     my $known_clients_entries = length @host_addresses;
684     my $host_address;
685     foreach my $host (@host_addresses) {
686         my $client_element = $main::known_clients->{$host}->{$search_pattern};
687         if ($search_element eq $client_element) {
688             $host_address = $host;
689             last;
690         }
691     }
692         
693     # search was successful
694     if (defined $host_address) {
695         my $source = @{$msg_hash->{source}}[0];
696         my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
697         &add_content2xml_hash($out_msg, "mac_address", $search_element);
698         &send_msg_hash2address($out_msg, $bus_address);
699     }
700     return;
704 sub who_has_i_do {
705     my ($msg_hash) = @_ ;
706     my $header = @{$msg_hash->{header}}[0];
707     my $source = @{$msg_hash->{source}}[0];
708     my $search_param = @{$msg_hash->{$header}}[0];
709     my $search_value = @{$msg_hash->{$search_param}}[0];
710     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
714 #===  FUNCTION  ================================================================
715 #         NAME:  new_ldap_config
716 #   PARAMETERS:  address - string - ip address and port of a host
717 #      RETURNS:  nothing
718 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
719 #===============================================================================
720 sub new_ldap_config {
721     my ($address) = @_ ;
722     
723     my $res = $main::known_clients_db->select_dbentry( { table=>'known_clients', hostname=>$address } );
725     # check hit
726     my $hit_counter = keys %{$res};
727     if( not $hit_counter == 1 ) {
728         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
729     }
731     my $macaddress = $res->{1}->{macaddress};
732     my $hostkey = $res->{1}->{hostkey};
734     if (not defined $macaddress) {
735         &main::daemon_log("ERROR: no mac address found for client $address", 1);
736         return;
737     }
739     # Build LDAP connection
740     my $ldap = Net::LDAP->new($ldap_uri);
741     if( not defined $ldap ) {
742         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
743         return;
744     } 
747     # Bind to a directory with dn and password
748     my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
750     # Perform search
751     $mesg = $ldap->search( base   => $ldap_base,
752                     scope  => 'sub',
753                     attrs => ['dn', 'gotoLdapServer'],
754                     filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
755     $mesg->code && die $mesg->error;
757     # Sanity check
758     if ($mesg->count != 1) {
759             &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
760         &main::daemon_log("\tbase: $ldap_base", 1);
761         &main::daemon_log("\tscope: sub", 1);
762         &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
763         &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
764             return;
765     }
767     my $entry= $mesg->entry(0);
768     my $dn= $entry->dn;
769     my @servers= $entry->get_value("gotoLdapServer");
770     my @ldap_uris;
771     my $server;
772     my $base;
774     # Do we need to look at an object class?
775     if ($#servers < 1){
776             $mesg = $ldap->search( base   => $ldap_base,
777                             scope  => 'sub',
778                             attrs => ['dn', 'gotoLdapServer'],
779                             filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
780             $mesg->code && die $mesg->error;
782             # Sanity check
783             if ($mesg->count != 1) {
784                     &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
785                     return;
786             }
788             $entry= $mesg->entry(0);
789             $dn= $entry->dn;
790             @servers= $entry->get_value("gotoLdapServer");
791     }
793     @servers= sort (@servers);
795     foreach $server (@servers){
796             $base= $server;
797             $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
798             $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
799             push (@ldap_uris, $server);
800     }
802     # Unbind
803     $mesg = $ldap->unbind;
805     # Assemble data package
806     my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
807                      'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
809     # Need to append GOto settings?
810     if (defined $goto_admin and defined $goto_secret){
811             $data{'goto_admin'}= $goto_admin;
812             $data{'goto_secret'}= $goto_secret;
813     }
815     # Send information
816     send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
818     return;
822 #===  FUNCTION  ================================================================
823 #         NAME:  execute_actions
824 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
825 #      RETURNS:  nothing
826 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
827 #                /etc/gosad/actions
828 #===============================================================================
829 sub execute_actions {
830     my ($msg_hash) = @_ ;
831     my $configdir= '/etc/gosad/actions/';
832     my $result;
834     my $header = @{$msg_hash->{header}}[0];
835     my $source = @{$msg_hash->{source}}[0];
836     my $target = @{$msg_hash->{target}}[0];
837  
838     if((not defined $source)
839             && (not defined $target)
840             && (not defined $header)) {
841         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
842     } else {
843         my $parameters="";
844         my @params = @{$msg_hash->{$header}};
845         my $params = join(", ", @params);
846         &main::daemon_log("execute_actions: got parameters: $params", 5);
848         if (@params) {
849             foreach my $param (@params) {
850                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
851                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
852                 $parameters.= " ".$param_value;
853             }
854         }
856         my $cmd= $configdir.$header."$parameters";
857         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
858         $result= "";
859         open(PIPE, "$cmd 2>&1 |");
860         while(<PIPE>) {
861             $result.=$_;
862         }
863         close(PIPE);
864     }
866     # process the event result
869     return;
873 1;