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_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 &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 ®ister_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;
122 }
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 }
174 }
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;
199 }
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;
230 }
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 $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])$/;
257 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
258 $result = $mac
259 }
260 }
261 }
262 return $result;
263 }
265 #=== FUNCTION ================================================================
266 # NAME: get_ip
267 # PARAMETERS: interface name (i.e. eth0)
268 # RETURNS: (ip address)
269 # DESCRIPTION: Uses ioctl to get ip address directly from system.
270 #===============================================================================
271 sub get_ip {
272 my $ifreq= shift;
273 my $result= "";
274 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
275 my $proto= getprotobyname('ip');
277 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
278 or die "socket: $!";
280 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
281 my ($if, $sin) = unpack 'a16 a16', $ifreq;
282 my ($port, $addr) = sockaddr_in $sin;
283 my $ip = inet_ntoa $addr;
285 if ($ip && length($ip) > 0) {
286 $result = $ip;
287 }
288 }
290 return $result;
291 }
293 #=== FUNCTION ================================================================
294 # NAME: open_socket
295 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
296 # [PeerPort] string necessary if port not appended by PeerAddr
297 # RETURNS: socket IO::Socket::INET
298 # DESCRIPTION: open a socket to PeerAddr
299 #===============================================================================
300 sub open_socket {
301 my ($PeerAddr, $PeerPort) = @_ ;
302 if(defined($PeerPort)){
303 $PeerAddr = $PeerAddr.":".$PeerPort;
304 }
305 my $socket;
306 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
307 Porto => "tcp" ,
308 Type => SOCK_STREAM,
309 Timeout => 5,
310 );
311 if(not defined $socket) {
312 return;
313 }
314 &main::daemon_log("open_socket to: $PeerAddr", 7);
315 return $socket;
316 }
318 #=== FUNCTION ================================================================
319 # NAME: register_at_bus
320 # PARAMETERS: nothing
321 # RETURNS: nothing
322 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
323 #===============================================================================
324 sub register_at_bus {
326 # add bus to known_server_db
327 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
328 primkey=>'hostname',
329 hostname=>$bus_address,
330 status=>'bus',
331 hostkey=>$bus_passwd,
332 timestamp=>&get_time,
333 } );
334 # if ($res == 3) {
335 # my $update_hash = { table=>'known_server' };
336 # $update_hash->{where} = [ { hostname=>[$bus_address] } ];
337 # $update_hash->{update} = [ {
338 # hostkey=>[$bus_passwd],
339 # timestamp=>[&get_time],
340 # } ];
341 # $res = $main::known_server_db->update_dbentry( $update_hash );
342 # }
344 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
345 my $answer = "";
346 $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
347 if ($answer == 0) {
348 &main::daemon_log("register at bus: $bus_address", 1);
349 } else {
350 &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
351 }
352 return;
353 }
355 #=== FUNCTION ================================================================
356 # NAME: process_incoming_msg
357 # PARAMETERS: crypted_msg - string - incoming crypted message
358 # RETURNS: nothing
359 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
360 #===============================================================================
361 sub process_incoming_msg {
362 my ($crypted_msg) = @_ ;
363 if(not defined $crypted_msg) {
364 &main::daemon_log("function 'process_incoming_msg': got no msg", 7);
365 }
367 &main::daemon_log("ServerPackages: incoming msg: \n$crypted_msg", 7);
369 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
370 $crypted_msg = $1;
371 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
373 my $msg;
374 my $msg_hash;
375 my $host_name;
376 my $host_key;
378 # check wether incoming msg is a new msg
379 $host_name = $server_address;
380 $host_key = $server_passwd;
381 &main::daemon_log("ServerPackage: host_name: $host_name", 7);
382 &main::daemon_log("ServerPackage: host_key: $host_key", 7);
383 eval{
384 my $key_cipher = &create_ciphering($host_key);
385 $msg = &decrypt_msg($crypted_msg, $key_cipher);
386 $msg_hash = &transform_msg2hash($msg);
387 };
388 if($@) {
389 &main::daemon_log("ServerPackage: deciphering raise error", 7);
390 &main::daemon_log("$@", 8);
391 $msg = undef;
392 $msg_hash = undef;
393 $host_name = undef;
394 $host_key = undef;
395 }
397 # check wether incoming msg is from a known_server
398 if( not defined $msg ) {
399 my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} );
400 while( my ($hit_num, $hit) = each %{ $query_res } ) {
401 $host_name = $hit->{hostname};
402 if( not $host_name =~ "^$host") {
403 next;
404 }
405 $host_key = $hit->{hostkey};
406 &main::daemon_log("ServerPackage: host_name: $host_name", 7);
407 &main::daemon_log("ServerPackage: host_key: $host_key", 7);
408 eval{
409 my $key_cipher = &create_ciphering($host_key);
410 $msg = &decrypt_msg($crypted_msg, $key_cipher);
411 $msg_hash = &transform_msg2hash($msg);
412 };
413 if($@) {
414 &main::daemon_log("ServerPackage: deciphering raise error", 7);
415 &main::daemon_log("$@", 8);
416 $msg = undef;
417 $msg_hash = undef;
418 $host_name = undef;
419 $host_key = undef;
420 } else {
421 last;
422 }
423 }
424 }
426 # check wether incoming msg is from a known_client
427 if( not defined $msg ) {
428 my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} );
429 while( my ($hit_num, $hit) = each %{ $query_res } ) {
430 $host_name = $hit->{hostname};
431 if( not $host_name =~ "^$host") {
432 next;
433 }
434 $host_key = $hit->{hostkey};
435 &main::daemon_log("ServerPackage: host_name: $host_name", 7);
436 &main::daemon_log("ServerPackage: host_key: $host_key", 7);
437 eval{
438 my $key_cipher = &create_ciphering($host_key);
439 $msg = &decrypt_msg($crypted_msg, $key_cipher);
440 $msg_hash = &transform_msg2hash($msg);
441 };
442 if($@) {
443 &main::daemon_log("ServerPackage: deciphering raise error", 7);
444 &main::daemon_log("$@", 8);
445 $msg = undef;
446 $msg_hash = undef;
447 $host_name = undef;
448 $host_key = undef;
449 } else {
450 last;
451 }
452 }
453 }
455 if( not defined $msg ) {
456 &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
457 &main::daemon_log("$@", 7);
458 return;
459 }
461 # process incoming msg
462 my $header = @{$msg_hash->{header}}[0];
463 my $source = @{$msg_hash->{source}}[0];
465 &main::daemon_log("recieve '$header' at ServerPackages from $host", 1);
466 &main::daemon_log("ServerPackages: msg to process: \n$msg", 5);
468 my @targets = @{$msg_hash->{target}};
469 my $len_targets = @targets;
470 if ($len_targets == 0){
471 &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
473 } elsif ($len_targets == 1){
474 # we have only one target symbol
475 my $target = $targets[0];
476 &main::daemon_log("SeverPackages: msg is for: $target", 7);
478 if ($target eq $server_address) {
479 # msg is for server
480 if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
481 elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
482 elsif ($header eq 'who_has') { &who_has($msg_hash) }
483 elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
484 elsif ($header eq 'update_status') { &update_status($msg_hash) }
485 elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
486 elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
487 else { &main::daemon_log("ERROR: ServerPackages: no function assigned to this msg", 5) }
489 } elsif ($target eq "*") {
490 # msg is for all clients
491 my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} );
492 while( my ($hit_num, $hit) = each %{ $query_res } ) {
493 $host_name = $hit->{hostname};
494 $host_key = $hit->{hostkey};
495 $msg_hash->{target} = [$host_name];
496 &send_msg_hash2address($msg_hash, $host_name, $host_key);
497 }
498 return;
500 } else {
501 # msg is for one host
502 my $query_res;
503 $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} );
504 if( 1 == keys %{$query_res} ) {
505 $host_key = $query_res->{1}->{host_key};
506 &send_msg_hash2address($msg_hash, $target, $host_key);
507 return;
508 }
510 $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} );
511 if( 1 == keys %{$query_res} ) {
512 $host_key = $query_res->{1}->{host_key};
513 &send_msg_hash2address($msg_hash, $target, $host_key);
514 return;
515 }
517 &main::daemon_log("ERROR: ServerPackages: target '$target' is not known neither in known_clients nor in known_server",1);
518 return;
519 }
521 } elsif ($len_targets > 1 ) {
522 # we have more than one target
523 return;
524 }
526 return ;
527 }
530 #=== FUNCTION ================================================================
531 # NAME: got_ping
532 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
533 # RETURNS: nothing
534 # DESCRIPTION: process this incoming message
535 #===============================================================================
536 sub got_ping {
537 my ($msg_hash) = @_;
539 my $source = @{$msg_hash->{source}}[0];
540 my $target = @{$msg_hash->{target}}[0];
541 my $header = @{$msg_hash->{header}}[0];
543 if(exists $main::known_daemons->{$source}) {
544 &main::add_content2known_daemons(hostname=>$source, status=>$header);
545 } else {
546 &main::add_content2known_clients(hostname=>$source, status=>$header);
547 }
549 return;
550 }
553 #=== FUNCTION ================================================================
554 # NAME: new_passwd
555 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
556 # RETURNS: nothing
557 # DESCRIPTION: process this incoming message
558 #===============================================================================
559 sub new_passwd {
560 my ($msg_hash) = @_;
562 my $header = @{$msg_hash->{header}}[0];
563 my $source_name = @{$msg_hash->{source}}[0];
564 my $source_key = @{$msg_hash->{new_passwd}}[0];
565 my $query_res;
567 # check known_clients_db
568 $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$source_name} );
569 if( 1 == keys %{$query_res} ) {
570 my $update_hash = { table=>'known_clients' };
571 $update_hash->{where} = [ { hostname=>[$source_name] } ];
572 $update_hash->{update} = [ {
573 hostkey=>[$source_key],
574 timestamp=>[&get_time],
575 } ];
576 my $res = $main::known_clients_db->update_dbentry( $update_hash );
578 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
579 &send_msg_hash2address($hash, $source_name, $source_key);
580 return;
581 }
583 # check known_server_db
584 $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$source_name } );
585 if( 1 == keys %{$query_res} ) {
586 my $update_hash = { table=>'known_server' };
587 $update_hash->{where} = [ { hostname=>[$source_name] } ];
588 $update_hash->{update} = [ {
589 hostkey=>[$source_key],
590 timestamp=>[&get_time],
591 } ];
592 my $res = $main::known_server_db->update_dbentry( $update_hash );
594 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
595 &send_msg_hash2address($hash, $source_name, $source_key);
596 return;
597 }
599 &main::daemon_log("ERROR: $source_name not known for '$header'-msg", 1);
600 return;
601 }
604 sub send_msg_hash {
605 my ($hash, $host_name, $host_key);
608 my $answer = &send_msg_hash2address($hash, $host_name, $host_key);
610 return;
611 }
614 #=== FUNCTION ================================================================
615 # NAME: here_i_am
616 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
617 # RETURNS: nothing
618 # DESCRIPTION: process this incoming message
619 #===============================================================================
620 sub here_i_am {
621 my ($msg_hash) = @_;
623 my $source = @{$msg_hash->{source}}[0];
624 my $mac_address = @{$msg_hash->{mac_address}}[0];
625 my $out_hash;
627 # number of known clients
628 my $nu_clients = keys %{$main::known_clients_db->select_dbentry( {table=>'known_clients'} )};
630 # check wether client address or mac address is already known
631 if (exists $main::known_clients->{$source}) {
632 &main::daemon_log("WARNING: $source is already known as a client", 1);
633 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
634 $nu_clients --;
635 }
637 # number of actual activ clients
638 my $act_nu_clients = $nu_clients;
640 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
641 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
643 if($max_clients <= $act_nu_clients) {
644 my $out_hash = &create_xml_hash("denied", $server_address, $source);
645 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
646 my $passwd = @{$msg_hash->{new_passwd}}[0];
647 &send_msg_hash2address($out_hash, $source, $passwd);
648 return;
649 }
651 # new client accepted
652 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
654 # create entry in known_clients
655 my $events = @{$msg_hash->{events}}[0];
657 # add entry to known_clients_db
658 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
659 primkey=>'hostname',
660 hostname=>$source,
661 events=>$events,
662 macaddress=>$mac_address,
663 status=>'registered',
664 hostkey=>$new_passwd,
665 timestamp=>&get_time,
666 } );
668 if ($res != 0) {
669 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
670 return;
671 }
673 # return acknowledgement to client
674 $out_hash = &create_xml_hash("registered", $server_address, $source);
675 &send_msg_hash2address($out_hash, $source, $new_passwd);
677 # notify registered client to bus
678 if( $bus_activ eq "on") {
679 # fetch actual bus key
680 my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} );
681 my $hostkey = $query_res->{1}->{hostkey};
683 # send update msg to bus
684 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
685 &send_msg_hash2address($out_hash, $bus_address, $hostkey);
687 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
688 }
690 # give the new client his ldap config
691 &new_ldap_config($source);
693 return;
694 }
697 #=== FUNCTION ================================================================
698 # NAME: who_has
699 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
700 # RETURNS: nothing
701 # DESCRIPTION: process this incoming message
702 #===============================================================================
703 sub who_has {
704 my ($msg_hash) = @_ ;
706 # what is your search pattern
707 my $search_pattern = @{$msg_hash->{who_has}}[0];
708 my $search_element = @{$msg_hash->{$search_pattern}}[0];
709 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
711 # scanning known_clients for search_pattern
712 my @host_addresses = keys %$main::known_clients;
713 my $known_clients_entries = length @host_addresses;
714 my $host_address;
715 foreach my $host (@host_addresses) {
716 my $client_element = $main::known_clients->{$host}->{$search_pattern};
717 if ($search_element eq $client_element) {
718 $host_address = $host;
719 last;
720 }
721 }
723 # search was successful
724 if (defined $host_address) {
725 my $source = @{$msg_hash->{source}}[0];
726 my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
727 &add_content2xml_hash($out_msg, "mac_address", $search_element);
728 &send_msg_hash2address($out_msg, $bus_address);
729 }
730 return;
731 }
734 sub who_has_i_do {
735 my ($msg_hash) = @_ ;
736 my $header = @{$msg_hash->{header}}[0];
737 my $source = @{$msg_hash->{source}}[0];
738 my $search_param = @{$msg_hash->{$header}}[0];
739 my $search_value = @{$msg_hash->{$search_param}}[0];
740 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
741 }
744 #=== FUNCTION ================================================================
745 # NAME: new_ldap_config
746 # PARAMETERS: address - string - ip address and port of a host
747 # RETURNS: nothing
748 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
749 #===============================================================================
750 sub new_ldap_config {
751 my ($address) = @_ ;
753 my $res = $main::known_clients_db->select_dbentry( { table=>'known_clients', hostname=>$address } );
755 # check hit
756 my $hit_counter = keys %{$res};
757 if( not $hit_counter == 1 ) {
758 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
759 }
761 my $macaddress = $res->{1}->{macaddress};
762 my $hostkey = $res->{1}->{hostkey};
764 if (not defined $macaddress) {
765 &main::daemon_log("ERROR: no mac address found for client $address", 1);
766 return;
767 }
769 # Build LDAP connection
770 my $ldap = Net::LDAP->new($ldap_uri);
771 if( not defined $ldap ) {
772 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
773 return;
774 }
777 # Bind to a directory with dn and password
778 my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
780 # Perform search
781 $mesg = $ldap->search( base => $ldap_base,
782 scope => 'sub',
783 attrs => ['dn', 'gotoLdapServer'],
784 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
785 $mesg->code && die $mesg->error;
787 # Sanity check
788 if ($mesg->count != 1) {
789 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
790 &main::daemon_log("\tbase: $ldap_base", 1);
791 &main::daemon_log("\tscope: sub", 1);
792 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
793 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
794 return;
795 }
797 my $entry= $mesg->entry(0);
798 my $dn= $entry->dn;
799 my @servers= $entry->get_value("gotoLdapServer");
800 my @ldap_uris;
801 my $server;
802 my $base;
804 # Do we need to look at an object class?
805 if ($#servers < 1){
806 $mesg = $ldap->search( base => $ldap_base,
807 scope => 'sub',
808 attrs => ['dn', 'gotoLdapServer'],
809 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
810 $mesg->code && die $mesg->error;
812 # Sanity check
813 if ($mesg->count != 1) {
814 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
815 return;
816 }
818 $entry= $mesg->entry(0);
819 $dn= $entry->dn;
820 @servers= $entry->get_value("gotoLdapServer");
821 }
823 @servers= sort (@servers);
825 foreach $server (@servers){
826 $base= $server;
827 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
828 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
829 push (@ldap_uris, $server);
830 }
832 # Unbind
833 $mesg = $ldap->unbind;
835 # Assemble data package
836 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
837 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
839 # Need to append GOto settings?
840 if (defined $goto_admin and defined $goto_secret){
841 $data{'goto_admin'}= $goto_admin;
842 $data{'goto_secret'}= $goto_secret;
843 }
845 # Send information
846 send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
848 return;
849 }
852 #=== FUNCTION ================================================================
853 # NAME: execute_actions
854 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
855 # RETURNS: nothing
856 # DESCRIPTION: invokes the script specified in msg_hash which is located under
857 # /etc/gosad/actions
858 #===============================================================================
859 sub execute_actions {
860 my ($msg_hash) = @_ ;
861 my $configdir= '/etc/gosad/actions/';
862 my $result;
864 my $header = @{$msg_hash->{header}}[0];
865 my $source = @{$msg_hash->{source}}[0];
866 my $target = @{$msg_hash->{target}}[0];
868 if((not defined $source)
869 && (not defined $target)
870 && (not defined $header)) {
871 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
872 } else {
873 my $parameters="";
874 my @params = @{$msg_hash->{$header}};
875 my $params = join(", ", @params);
876 &main::daemon_log("execute_actions: got parameters: $params", 5);
878 if (@params) {
879 foreach my $param (@params) {
880 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
881 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
882 $parameters.= " ".$param_value;
883 }
884 }
886 my $cmd= $configdir.$header."$parameters";
887 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
888 $result= "";
889 open(PIPE, "$cmd 2>&1 |");
890 while(<PIPE>) {
891 $result.=$_;
892 }
893 close(PIPE);
894 }
896 # process the event result
899 return;
900 }
903 1;