Code

add 'order by' xml-tag to GosaPackages
[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;
263 #===  FUNCTION  ================================================================
264 #         NAME:  register_at_bus
265 #   PARAMETERS:  nothing
266 #      RETURNS:  nothing
267 #  DESCRIPTION:  creates an entry in known_daemons and send a 'here_i_am' msg to bus
268 #===============================================================================
269 sub register_at_bus {
271     # add bus to known_server_db
272     my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
273                                                     primkey=>'hostname',
274                                                     hostname=>$bus_address,
275                                                     status=>'bus',
276                                                     hostkey=>$bus_passwd,
277                                                     timestamp=>&get_time,
278                                                 } );
279     my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
280     my $answer = "";
281     $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
282     if ($answer == 0) {
283         &main::daemon_log("register at bus: $bus_address", 1);
284     } else {
285         &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
286     }
287     return;
290 #===  FUNCTION  ================================================================
291 #         NAME:  process_incoming_msg
292 #   PARAMETERS:  crypted_msg - string - incoming crypted message
293 #      RETURNS:  nothing
294 #  DESCRIPTION:  handels the proceeded distribution to the appropriated functions
295 #===============================================================================
296 sub process_incoming_msg {
297     my ($crypted_msg) = @_ ;
298     if(not defined $crypted_msg) {
299         &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
300     }
302     $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
303     $crypted_msg = $1;
304         my $host="0.0.0.0";
305         if(defined $2 && defined $3 && defined $4 && defined $5) {
306                 $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
307         }
309     my $msg;
310     my $msg_hash;
311     my $host_name;
312     my $host_key;
314     # check wether incoming msg is a new msg
315     $host_name = $server_address;
316     $host_key = $server_passwd;
317     &main::daemon_log("ServerPackage: host_name: $host_name", 7);
318     &main::daemon_log("ServerPackage: host_key: $host_key", 7);
319     eval{
320         my $key_cipher = &create_ciphering($host_key);
321                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
322         $msg_hash = &transform_msg2hash($msg);
323     };
324     if($@) {
325         &main::daemon_log("ServerPackage: deciphering raise error", 7);
326         &main::daemon_log("$@", 8);
327         $msg = undef;
328         $msg_hash = undef;
329         $host_name = undef;
330         $host_key = undef;
331     } 
333     # check wether incoming msg is from a known_server
334     if( not defined $msg ) {
335         my $sql_statement= "SELECT * FROM known_server";
336         my $query_res = $main::known_server_db->select_dbentry( $sql_statement ); 
338         while( my ($hit_num, $hit) = each %{ $query_res } ) {  
339             $host_name = $hit->{hostname};
341             if( not $host_name =~ "^$host") {
342                 next;
343             }
344             $host_key = $hit->{hostkey};
345             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
346             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
347             eval{
348                 my $key_cipher = &create_ciphering($host_key);
349                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
350                 $msg_hash = &transform_msg2hash($msg);
351             };
352             if($@) {
353                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
354                 &main::daemon_log("$@", 8);
355                 $msg = undef;
356                 $msg_hash = undef;
357                 $host_name = undef;
358                 $host_key = undef;
359             } else {
360                 last;
361             }
362         }
363     }
365     # check wether incoming msg is from a known_client
366     if( not defined $msg ) {
367         #my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} ); 
368         my $sql_statement= "SELECT * FROM known_clients";
369         my $query_res = $main::known_clients_db->select_dbentry( $sql_statement ); 
370         while( my ($hit_num, $hit) = each %{ $query_res } ) {    
371             $host_name = $hit->{hostname};
372             if( not $host_name =~ "^$host") {
373                 next;
374             }
375             $host_key = $hit->{hostkey};
376             &main::daemon_log("ServerPackage: host_name: $host_name", 7);
377             &main::daemon_log("ServerPackage: host_key: $host_key", 7);
378             eval{
379                 my $key_cipher = &create_ciphering($host_key);
380                 $msg = &decrypt_msg($crypted_msg, $key_cipher);
381                 $msg_hash = &transform_msg2hash($msg);
382             };
383             if($@) {
384                 &main::daemon_log("ServerPackage: deciphering raise error", 7);
385                 &main::daemon_log("$@", 8);
386                 $msg = undef;
387                 $msg_hash = undef;
388                 $host_name = undef;
389                 $host_key = undef;
390             } else {
391                 last;
392             }
393         }
394     }
396     if( not defined $msg ) {
397         &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
398         &main::daemon_log("$@", 8);
399         return;
400     }
402     # process incoming msg
403     my $header = @{$msg_hash->{header}}[0]; 
404     my $source = @{$msg_hash->{source}}[0];
406     &main::daemon_log("receive '$header' at ServerPackages from $host", 1);
407     &main::daemon_log("ServerPackages: msg to process: \n$msg", 5);
409     my @targets = @{$msg_hash->{target}};
410     my $len_targets = @targets;
411     if ($len_targets == 0){     
412         &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
414     }  elsif ($len_targets == 1){
415         # we have only one target symbol
416         my $target = $targets[0];
417         &main::daemon_log("SeverPackages: msg is for: $target", 7);
419         # msg is for server
420         if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
421         elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
422         elsif ($header eq 'who_has') { &who_has($msg_hash) }
423         elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
424         elsif ($header eq 'update_status') { &update_status($msg_hash) }
425         elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
426         elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
427         else { 
428             if ($target eq "*") {
429                 # msg is for all clients
430                 my $sql_statement = "SELECT * FROM known_clients";
431                 my $query_res = $main::known_clients_db->select_dbentry( $sql_statement ); 
432                 while( my ($hit_num, $hit) = each %{ $query_res } ) {    
433                     $host_name = $hit->{hostname};
434                     $host_key = $hit->{hostkey};
435                     $msg_hash->{target} = [$host_name];
436                     &send_msg_hash2address($msg_hash, $host_name, $host_key);
437                 }
439             } else {
440                 # msg is for one host
441                 my $host_key;
444                 if( not defined $host_key ) { 
445                     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$target'";
446                     my $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
447                     if( 1 == keys %{$query_res} ) {
448                         $host_key = $query_res->{1}->{host_key};
449                     }
450                 } 
452                 if( not defined $host_key ) {
453                     my $sql_statement = "SELECT * FROM known_server WHERE hostname='$target'";
454                     my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
455                     if( 1 == keys %{$query_res} ) {
456                         $host_key = $query_res->{1}->{host_key};
457                     }
458                 }
460                 if( not defined $host_key ) { 
461                     &main::daemon_log("ERROR: ServerPackages: target '".$target.
462                             "' is not known neither in known_clients nor in known_server",1);
463                 } else {
464                     &send_msg_hash2address($msg_hash, $target, $host_key);
465                 }               
466             }
467         }
469     } elsif ($len_targets > 1 ) {
470         # we have more than one target 
471         # TODO to be implemented
472     }
474     return ;
478 #===  FUNCTION  ================================================================
479 #         NAME:  got_ping
480 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
481 #      RETURNS:  nothing
482 #  DESCRIPTION:  process this incoming message
483 #===============================================================================
484 sub got_ping {
485     my ($msg_hash) = @_;
486     
487     my $source = @{$msg_hash->{source}}[0];
488     my $target = @{$msg_hash->{target}}[0];
489     my $header = @{$msg_hash->{header}}[0];
490     
491     if(exists $main::known_daemons->{$source}) {
492         &main::add_content2known_daemons(hostname=>$source, status=>$header);
493     } else {
494         &main::add_content2known_clients(hostname=>$source, status=>$header);
495     }
496     
497     return;
501 #===  FUNCTION  ================================================================
502 #         NAME:  new_passwd
503 #   PARAMETERS:  msg_hash - ref - hash from function create_xml_hash
504 #      RETURNS:  nothing
505 #  DESCRIPTION:  process this incoming message
506 #===============================================================================
507 sub new_passwd {
508     my ($msg_hash) = @_;
510     my $header = @{$msg_hash->{header}}[0];
511     my $source_name = @{$msg_hash->{source}}[0];
512     my $source_key = @{$msg_hash->{new_passwd}}[0];
513     my $query_res;
515     # check known_clients_db
516     my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
517     $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
518     if( 1 == keys %{$query_res} ) {
519         my $act_time = &get_time;
520         my $sql_statement= "UPDATE known_clients ".
521             "SET hostkey='$source_key', timestamp='$act_time' ".
522             "WHERE hostname='$source_name'";
523         my $res = $main::known_clients_db->update_dbentry( $sql_statement );
525         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
526         &send_msg_hash2address($hash, $source_name, $source_key);
527         return;
528     }
530     # check known_server_db
531     my $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
532     $query_res = $main::known_server_db->select_dbentry( $sql_statement );
533     if( 1 == keys %{$query_res} ) {
534         my $act_time = &get_time;
535         my $sql_statement= "UPDATE known_server ".
536             "SET hostkey='$source_key', timestamp='$act_time' ".
537             "WHERE hostname='$source_name'";
538         my $res = $main::known_server_db->update_dbentry( $sql_statement );
540         my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
541         &send_msg_hash2address($hash, $source_name, $source_key);
542         return;
543     }
545     &main::daemon_log("ERROR: $source_name not known for '$header'-msg", 1);
546     return;
550 #===  FUNCTION  ================================================================
551 #         NAME:  here_i_am
552 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
553 #      RETURNS:  nothing
554 #  DESCRIPTION:  process this incoming message
555 #===============================================================================
556 sub here_i_am {
557     my ($msg_hash) = @_;
559     my $source = @{$msg_hash->{source}}[0];
560     my $mac_address = @{$msg_hash->{mac_address}}[0];
561     my $out_hash;
563     # number of known clients
564     my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
566     # check wether client address or mac address is already known
567     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
568     my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
569     
570     if ( 1 == keys %{$db_res} ) {
571         &main::daemon_log("WARNING: $source is already known as a client", 1);
572         &main::daemon_log("WARNING: values for $source are being overwritten", 1);   
573         $nu_clients --;
574     }
576     # number of actual activ clients
577     my $act_nu_clients = $nu_clients;
579     &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
580     &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
582     if($max_clients <= $act_nu_clients) {
583         my $out_hash = &create_xml_hash("denied", $server_address, $source);
584         &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
585         my $passwd = @{$msg_hash->{new_passwd}}[0]; 
586         &send_msg_hash2address($out_hash, $source, $passwd);
587         return;
588     }
589     
590     # new client accepted
591     my $new_passwd = @{$msg_hash->{new_passwd}}[0];
593     # create entry in known_clients
594     my $events = @{$msg_hash->{events}}[0];
595     
597     # add entry to known_clients_db
598     my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients', 
599                                                 primkey=>'hostname',
600                                                 hostname=>$source,
601                                                 events=>$events,
602                                                 macaddress=>$mac_address,
603                                                 status=>'registered',
604                                                 hostkey=>$new_passwd,
605                                                 timestamp=>&get_time,
606                                                 } );
608     if ($res != 0)  {
609         &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
610         return;
611     }
612     
613     # return acknowledgement to client
614     $out_hash = &create_xml_hash("registered", $server_address, $source);
615     &send_msg_hash2address($out_hash, $source, $new_passwd);
617     # notify registered client to bus
618     if( $bus_activ eq "on") {
619         # fetch actual bus key
620         my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
621         my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
622         my $hostkey = $query_res->{1}->{'hostkey'};
624         # send update msg to bus
625         $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
626         &send_msg_hash2address($out_hash, $bus_address, $hostkey);
627         
628         &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
629     }
631     # give the new client his ldap config
632     &new_ldap_config($source);
634     return;
638 #===  FUNCTION  ================================================================
639 #         NAME:  who_has
640 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
641 #      RETURNS:  nothing 
642 #  DESCRIPTION:  process this incoming message
643 #===============================================================================
644 sub who_has {
645     my ($msg_hash) = @_ ;
646     
647     # what is your search pattern
648     my $search_pattern = @{$msg_hash->{who_has}}[0];
649     my $search_element = @{$msg_hash->{$search_pattern}}[0];
650     &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
652     # scanning known_clients for search_pattern
653     my @host_addresses = keys %$main::known_clients;
654     my $known_clients_entries = length @host_addresses;
655     my $host_address;
656     foreach my $host (@host_addresses) {
657         my $client_element = $main::known_clients->{$host}->{$search_pattern};
658         if ($search_element eq $client_element) {
659             $host_address = $host;
660             last;
661         }
662     }
663         
664     # search was successful
665     if (defined $host_address) {
666         my $source = @{$msg_hash->{source}}[0];
667         my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
668         &add_content2xml_hash($out_msg, "mac_address", $search_element);
669         &send_msg_hash2address($out_msg, $bus_address);
670     }
671     return;
675 sub who_has_i_do {
676     my ($msg_hash) = @_ ;
677     my $header = @{$msg_hash->{header}}[0];
678     my $source = @{$msg_hash->{source}}[0];
679     my $search_param = @{$msg_hash->{$header}}[0];
680     my $search_value = @{$msg_hash->{$search_param}}[0];
681     print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
685 #===  FUNCTION  ================================================================
686 #         NAME:  new_ldap_config
687 #   PARAMETERS:  address - string - ip address and port of a host
688 #      RETURNS:  nothing
689 #  DESCRIPTION:  send to address the ldap configuration found for dn gotoLdapServer
690 #===============================================================================
691 sub new_ldap_config {
692     my ($address) = @_ ;
693     
694     my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
695     my $res = $main::known_clients_db->select_dbentry( $sql_statement );
697     # check hit
698     my $hit_counter = keys %{$res};
699     if( not $hit_counter == 1 ) {
700         &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
701     }
703     my $macaddress = $res->{1}->{macaddress};
704     my $hostkey = $res->{1}->{hostkey};
706     if (not defined $macaddress) {
707         &main::daemon_log("ERROR: no mac address found for client $address", 1);
708         return;
709     }
711     # Build LDAP connection
712     my $ldap = Net::LDAP->new($ldap_uri);
713     if( not defined $ldap ) {
714         &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
715         return;
716     } 
719     # Bind to a directory with dn and password
720     my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
722     # Perform search
723     $mesg = $ldap->search( base   => $ldap_base,
724                     scope  => 'sub',
725                     attrs => ['dn', 'gotoLdapServer'],
726                     filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
727     $mesg->code && die $mesg->error;
729     # Sanity check
730     if ($mesg->count != 1) {
731             &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
732         &main::daemon_log("\tbase: $ldap_base", 1);
733         &main::daemon_log("\tscope: sub", 1);
734         &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
735         &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
736             return;
737     }
739     my $entry= $mesg->entry(0);
740     my $dn= $entry->dn;
741     my @servers= $entry->get_value("gotoLdapServer");
742     my @ldap_uris;
743     my $server;
744     my $base;
746     # Do we need to look at an object class?
747     if ($#servers < 1){
748             $mesg = $ldap->search( base   => $ldap_base,
749                             scope  => 'sub',
750                             attrs => ['dn', 'gotoLdapServer'],
751                             filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
752             $mesg->code && die $mesg->error;
754             # Sanity check
755             if ($mesg->count != 1) {
756                     &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
757                     return;
758             }
760             $entry= $mesg->entry(0);
761             $dn= $entry->dn;
762             @servers= $entry->get_value("gotoLdapServer");
763     }
765     @servers= sort (@servers);
767     foreach $server (@servers){
768             $base= $server;
769             $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
770             $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
771             push (@ldap_uris, $server);
772     }
774     # Unbind
775     $mesg = $ldap->unbind;
777     # Assemble data package
778     my %data = ( 'ldap_uri'  => \@ldap_uris, 'ldap_base' => $base,
779                      'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
781     # Need to append GOto settings?
782     if (defined $goto_admin and defined $goto_secret){
783             $data{'goto_admin'}= $goto_admin;
784             $data{'goto_secret'}= $goto_secret;
785     }
787     # Send information
788     send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
790     return;
794 #===  FUNCTION  ================================================================
795 #         NAME:  execute_actions
796 #   PARAMETERS:  msg_hash - hash - hash from function create_xml_hash
797 #      RETURNS:  nothing
798 #  DESCRIPTION:  invokes the script specified in msg_hash which is located under
799 #                /etc/gosad/actions
800 #===============================================================================
801 sub execute_actions {
802     my ($msg_hash) = @_ ;
803     my $configdir= '/etc/gosad/actions/';
804     my $result;
806     my $header = @{$msg_hash->{header}}[0];
807     my $source = @{$msg_hash->{source}}[0];
808     my $target = @{$msg_hash->{target}}[0];
809  
810     if((not defined $source)
811             && (not defined $target)
812             && (not defined $header)) {
813         &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
814     } else {
815         my $parameters="";
816         my @params = @{$msg_hash->{$header}};
817         my $params = join(", ", @params);
818         &main::daemon_log("execute_actions: got parameters: $params", 5);
820         if (@params) {
821             foreach my $param (@params) {
822                 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
823                 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
824                 $parameters.= " ".$param_value;
825             }
826         }
828         my $cmd= $configdir.$header."$parameters";
829         &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
830         $result= "";
831         open(PIPE, "$cmd 2>&1 |");
832         while(<PIPE>) {
833             $result.=$_;
834         }
835         close(PIPE);
836     }
838     # process the event result
841     return;
845 1;