cb7c976f95d75ca3a8d34a9fe36ce37c891d6f4d
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 ®ister_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 }
139 }
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;
164 }
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;
195 }
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;
232 }
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;
260 }
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;
312 }
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 while( my ($hit_num, $hit) = each %{ $query_res } ) {
363 $host_name = $hit->{hostname};
364 if( not $host_name =~ "^$host") {
365 next;
366 }
367 $host_key = $hit->{hostkey};
368 &main::daemon_log("ServerPackage: host_name: $host_name", 7);
369 &main::daemon_log("ServerPackage: host_key: $host_key", 7);
370 eval{
371 my $key_cipher = &create_ciphering($host_key);
372 $msg = &decrypt_msg($crypted_msg, $key_cipher);
373 $msg_hash = &transform_msg2hash($msg);
374 };
375 if($@) {
376 &main::daemon_log("ServerPackage: deciphering raise error", 7);
377 &main::daemon_log("$@", 8);
378 $msg = undef;
379 $msg_hash = undef;
380 $host_name = undef;
381 $host_key = undef;
382 } else {
383 last;
384 }
385 }
386 }
388 # check wether incoming msg is from a known_client
389 if( not defined $msg ) {
390 my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} );
391 while( my ($hit_num, $hit) = each %{ $query_res } ) {
392 $host_name = $hit->{hostname};
393 if( not $host_name =~ "^$host") {
394 next;
395 }
396 $host_key = $hit->{hostkey};
397 &main::daemon_log("ServerPackage: host_name: $host_name", 7);
398 &main::daemon_log("ServerPackage: host_key: $host_key", 7);
399 eval{
400 my $key_cipher = &create_ciphering($host_key);
401 $msg = &decrypt_msg($crypted_msg, $key_cipher);
402 $msg_hash = &transform_msg2hash($msg);
403 };
404 if($@) {
405 &main::daemon_log("ServerPackage: deciphering raise error", 7);
406 &main::daemon_log("$@", 8);
407 $msg = undef;
408 $msg_hash = undef;
409 $host_name = undef;
410 $host_key = undef;
411 } else {
412 last;
413 }
414 }
415 }
417 if( not defined $msg ) {
418 &main::daemon_log("WARNING: ServerPackage do not understand the message:", 5);
419 &main::daemon_log("$@", 8);
420 return;
421 }
423 # process incoming msg
424 my $header = @{$msg_hash->{header}}[0];
425 my $source = @{$msg_hash->{source}}[0];
427 &main::daemon_log("receive '$header' at ServerPackages from $host", 1);
428 &main::daemon_log("ServerPackages: msg to process: \n$msg", 5);
430 my @targets = @{$msg_hash->{target}};
431 my $len_targets = @targets;
432 if ($len_targets == 0){
433 &main::daemon_log("ERROR: ServerPackages: no target specified for msg $header", 1);
435 } elsif ($len_targets == 1){
436 # we have only one target symbol
437 my $target = $targets[0];
438 &main::daemon_log("SeverPackages: msg is for: $target", 7);
440 # msg is for server
441 if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
442 elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
443 elsif ($header eq 'who_has') { &who_has($msg_hash) }
444 elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
445 elsif ($header eq 'update_status') { &update_status($msg_hash) }
446 elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
447 elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
448 else {
449 if ($target eq "*") {
450 # msg is for all clients
451 my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients'} );
452 while( my ($hit_num, $hit) = each %{ $query_res } ) {
453 $host_name = $hit->{hostname};
454 $host_key = $hit->{hostkey};
455 $msg_hash->{target} = [$host_name];
456 &send_msg_hash2address($msg_hash, $host_name, $host_key);
457 }
459 } else {
460 # msg is for one host
461 my $host_key;
464 if( not defined $host_key ) {
465 my $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$target} );
466 if( 1 == keys %{$query_res} ) {
467 $host_key = $query_res->{1}->{host_key};
468 }
469 }
471 if( not defined $host_key ) {
472 my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$target} );
473 if( 1 == keys %{$query_res} ) {
474 $host_key = $query_res->{1}->{host_key};
475 }
476 }
478 if( not defined $host_key ) {
479 &main::daemon_log("ERROR: ServerPackages: target '".$target.
480 "' is not known neither in known_clients nor in known_server",1);
481 } else {
482 &send_msg_hash2address($msg_hash, $target, $host_key);
483 }
484 }
485 }
487 } elsif ($len_targets > 1 ) {
488 # we have more than one target
489 # TODO to be implemented
490 }
492 return ;
493 }
496 #=== FUNCTION ================================================================
497 # NAME: got_ping
498 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
499 # RETURNS: nothing
500 # DESCRIPTION: process this incoming message
501 #===============================================================================
502 sub got_ping {
503 my ($msg_hash) = @_;
505 my $source = @{$msg_hash->{source}}[0];
506 my $target = @{$msg_hash->{target}}[0];
507 my $header = @{$msg_hash->{header}}[0];
509 if(exists $main::known_daemons->{$source}) {
510 &main::add_content2known_daemons(hostname=>$source, status=>$header);
511 } else {
512 &main::add_content2known_clients(hostname=>$source, status=>$header);
513 }
515 return;
516 }
519 #=== FUNCTION ================================================================
520 # NAME: new_passwd
521 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
522 # RETURNS: nothing
523 # DESCRIPTION: process this incoming message
524 #===============================================================================
525 sub new_passwd {
526 my ($msg_hash) = @_;
528 my $header = @{$msg_hash->{header}}[0];
529 my $source_name = @{$msg_hash->{source}}[0];
530 my $source_key = @{$msg_hash->{new_passwd}}[0];
531 my $query_res;
533 # check known_clients_db
534 $query_res = $main::known_clients_db->select_dbentry( {table=>'known_clients', hostname=>$source_name} );
535 if( 1 == keys %{$query_res} ) {
536 my $update_hash = { table=>'known_clients' };
537 $update_hash->{where} = [ { hostname=>[$source_name] } ];
538 $update_hash->{update} = [ {
539 hostkey=>[$source_key],
540 timestamp=>[&get_time],
541 } ];
542 my $res = $main::known_clients_db->update_dbentry( $update_hash );
544 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
545 &send_msg_hash2address($hash, $source_name, $source_key);
546 return;
547 }
549 # check known_server_db
550 $query_res = $main::known_server_db->select_dbentry( {table=>'known_server', hostname=>$source_name } );
551 if( 1 == keys %{$query_res} ) {
552 my $update_hash = { table=>'known_server' };
553 $update_hash->{where} = [ { hostname=>[$source_name] } ];
554 $update_hash->{update} = [ {
555 hostkey=>[$source_key],
556 timestamp=>[&get_time],
557 } ];
558 my $res = $main::known_server_db->update_dbentry( $update_hash );
560 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
561 &send_msg_hash2address($hash, $source_name, $source_key);
562 return;
563 }
565 &main::daemon_log("ERROR: $source_name not known for '$header'-msg", 1);
566 return;
567 }
570 sub send_msg_hash {
571 my ($hash, $host_name, $host_key);
574 my $answer = &send_msg_hash2address($hash, $host_name, $host_key);
576 return;
577 }
580 #=== FUNCTION ================================================================
581 # NAME: here_i_am
582 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
583 # RETURNS: nothing
584 # DESCRIPTION: process this incoming message
585 #===============================================================================
586 sub here_i_am {
587 my ($msg_hash) = @_;
589 my $source = @{$msg_hash->{source}}[0];
590 my $mac_address = @{$msg_hash->{mac_address}}[0];
591 my $out_hash;
593 # number of known clients
594 my $nu_clients = keys %{ $main::known_clients_db->select_dbentry( {table=>'known_clients'} ) };
596 # check wether client address or mac address is already known
597 if (exists $main::known_clients->{$source}) {
598 &main::daemon_log("WARNING: $source is already known as a client", 1);
599 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
600 $nu_clients --;
601 }
603 # number of actual activ clients
604 my $act_nu_clients = $nu_clients;
606 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
607 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
609 if($max_clients <= $act_nu_clients) {
610 my $out_hash = &create_xml_hash("denied", $server_address, $source);
611 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
612 my $passwd = @{$msg_hash->{new_passwd}}[0];
613 &send_msg_hash2address($out_hash, $source, $passwd);
614 return;
615 }
617 # new client accepted
618 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
620 # create entry in known_clients
621 my $events = @{$msg_hash->{events}}[0];
623 # add entry to known_clients_db
624 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
625 primkey=>'hostname',
626 hostname=>$source,
627 events=>$events,
628 macaddress=>$mac_address,
629 status=>'registered',
630 hostkey=>$new_passwd,
631 timestamp=>&get_time,
632 } );
634 if ($res != 0) {
635 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
636 return;
637 }
639 # return acknowledgement to client
640 $out_hash = &create_xml_hash("registered", $server_address, $source);
641 &send_msg_hash2address($out_hash, $source, $new_passwd);
643 # notify registered client to bus
644 if( $bus_activ eq "on") {
645 # fetch actual bus key
646 my $query_res = $main::known_server_db->select_dbentry( {table=>'known_server'} );
647 my $hostkey = $query_res->{1}->{hostkey};
649 # send update msg to bus
650 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
651 &send_msg_hash2address($out_hash, $bus_address, $hostkey);
653 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
654 }
656 # give the new client his ldap config
657 &new_ldap_config($source);
659 return;
660 }
663 #=== FUNCTION ================================================================
664 # NAME: who_has
665 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
666 # RETURNS: nothing
667 # DESCRIPTION: process this incoming message
668 #===============================================================================
669 sub who_has {
670 my ($msg_hash) = @_ ;
672 # what is your search pattern
673 my $search_pattern = @{$msg_hash->{who_has}}[0];
674 my $search_element = @{$msg_hash->{$search_pattern}}[0];
675 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
677 # scanning known_clients for search_pattern
678 my @host_addresses = keys %$main::known_clients;
679 my $known_clients_entries = length @host_addresses;
680 my $host_address;
681 foreach my $host (@host_addresses) {
682 my $client_element = $main::known_clients->{$host}->{$search_pattern};
683 if ($search_element eq $client_element) {
684 $host_address = $host;
685 last;
686 }
687 }
689 # search was successful
690 if (defined $host_address) {
691 my $source = @{$msg_hash->{source}}[0];
692 my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
693 &add_content2xml_hash($out_msg, "mac_address", $search_element);
694 &send_msg_hash2address($out_msg, $bus_address);
695 }
696 return;
697 }
700 sub who_has_i_do {
701 my ($msg_hash) = @_ ;
702 my $header = @{$msg_hash->{header}}[0];
703 my $source = @{$msg_hash->{source}}[0];
704 my $search_param = @{$msg_hash->{$header}}[0];
705 my $search_value = @{$msg_hash->{$search_param}}[0];
706 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
707 }
710 #=== FUNCTION ================================================================
711 # NAME: new_ldap_config
712 # PARAMETERS: address - string - ip address and port of a host
713 # RETURNS: nothing
714 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
715 #===============================================================================
716 sub new_ldap_config {
717 my ($address) = @_ ;
719 my $res = $main::known_clients_db->select_dbentry( { table=>'known_clients', hostname=>$address } );
721 # check hit
722 my $hit_counter = keys %{$res};
723 if( not $hit_counter == 1 ) {
724 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
725 }
727 my $macaddress = $res->{1}->{macaddress};
728 my $hostkey = $res->{1}->{hostkey};
730 if (not defined $macaddress) {
731 &main::daemon_log("ERROR: no mac address found for client $address", 1);
732 return;
733 }
735 # Build LDAP connection
736 my $ldap = Net::LDAP->new($ldap_uri);
737 if( not defined $ldap ) {
738 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
739 return;
740 }
743 # Bind to a directory with dn and password
744 my $mesg= $ldap->bind($ldap_admin_dn, $ldap_admin_password);
746 # Perform search
747 $mesg = $ldap->search( base => $ldap_base,
748 scope => 'sub',
749 attrs => ['dn', 'gotoLdapServer'],
750 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
751 $mesg->code && die $mesg->error;
753 # Sanity check
754 if ($mesg->count != 1) {
755 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
756 &main::daemon_log("\tbase: $ldap_base", 1);
757 &main::daemon_log("\tscope: sub", 1);
758 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
759 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
760 return;
761 }
763 my $entry= $mesg->entry(0);
764 my $dn= $entry->dn;
765 my @servers= $entry->get_value("gotoLdapServer");
766 my @ldap_uris;
767 my $server;
768 my $base;
770 # Do we need to look at an object class?
771 if ($#servers < 1){
772 $mesg = $ldap->search( base => $ldap_base,
773 scope => 'sub',
774 attrs => ['dn', 'gotoLdapServer'],
775 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
776 $mesg->code && die $mesg->error;
778 # Sanity check
779 if ($mesg->count != 1) {
780 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
781 return;
782 }
784 $entry= $mesg->entry(0);
785 $dn= $entry->dn;
786 @servers= $entry->get_value("gotoLdapServer");
787 }
789 @servers= sort (@servers);
791 foreach $server (@servers){
792 $base= $server;
793 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
794 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
795 push (@ldap_uris, $server);
796 }
798 # Unbind
799 $mesg = $ldap->unbind;
801 # Assemble data package
802 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
803 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
805 # Need to append GOto settings?
806 if (defined $goto_admin and defined $goto_secret){
807 $data{'goto_admin'}= $goto_admin;
808 $data{'goto_secret'}= $goto_secret;
809 }
811 # Send information
812 send_msg("new_ldap_config", $server_address, $address, \%data, $hostkey);
814 return;
815 }
818 #=== FUNCTION ================================================================
819 # NAME: execute_actions
820 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
821 # RETURNS: nothing
822 # DESCRIPTION: invokes the script specified in msg_hash which is located under
823 # /etc/gosad/actions
824 #===============================================================================
825 sub execute_actions {
826 my ($msg_hash) = @_ ;
827 my $configdir= '/etc/gosad/actions/';
828 my $result;
830 my $header = @{$msg_hash->{header}}[0];
831 my $source = @{$msg_hash->{source}}[0];
832 my $target = @{$msg_hash->{target}}[0];
834 if((not defined $source)
835 && (not defined $target)
836 && (not defined $header)) {
837 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
838 } else {
839 my $parameters="";
840 my @params = @{$msg_hash->{$header}};
841 my $params = join(", ", @params);
842 &main::daemon_log("execute_actions: got parameters: $params", 5);
844 if (@params) {
845 foreach my $param (@params) {
846 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
847 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
848 $parameters.= " ".$param_value;
849 }
850 }
852 my $cmd= $configdir.$header."$parameters";
853 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
854 $result= "";
855 open(PIPE, "$cmd 2>&1 |");
856 while(<PIPE>) {
857 $result.=$_;
858 }
859 close(PIPE);
860 }
862 # process the event result
865 return;
866 }
869 1;