1 package SIPackages;
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;
17 use Net::hostent;
19 BEGIN{}
20 END {}
22 my ($known_clients_file_name);
23 my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
24 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
25 my $server;
26 my $network_interface;
27 my $no_bus;
28 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
31 my %cfg_defaults =
32 (
33 "server" =>
34 {"server_activ" => [\$server_activ, "on"],
35 "server_ip" => [\$server_ip, "0.0.0.0"],
36 "server_mac_address" => [\$server_mac_address, ""],
37 "server_port" => [\$server_port, "20081"],
38 "SIPackages_key" => [\$SIPackages_key, ""],
39 "max_clients" => [\$max_clients, 100],
40 "ldap_uri" => [\$ldap_uri, ""],
41 "ldap_base" => [\$ldap_base, ""],
42 "ldap_admin_dn" => [\$ldap_admin_dn, ""],
43 "ldap_admin_password" => [\$ldap_admin_password, ""],
44 },
45 "bus" =>
46 {"bus_activ" => [\$bus_activ, "on"],
47 "bus_passwd" => [\$bus_passwd, ""],
48 "bus_ip" => [\$bus_ip, ""],
49 "bus_port" => [\$bus_port, "20080"],
50 },
51 );
53 ### START #####################################################################
55 # read configfile and import variables
56 &read_configfile();
58 # detect interfaces and mac address
59 $network_interface= &get_interface_for_ip($server_ip);
60 $server_mac_address= &get_mac($network_interface);
62 # complete addresses
63 if( $server_ip eq "0.0.0.0" ) {
64 $server_ip = "127.0.0.1";
65 }
66 my $server_address = "$server_ip:$server_port";
67 my $bus_address = "$bus_ip:$bus_port";
69 # create general settings for this module
70 my $xml = new XML::Simple();
72 # register at bus
73 if ($main::no_bus > 0) {
74 $bus_activ = "off"
75 }
76 if($bus_activ eq "on") {
77 ®ister_at_bus();
78 }
80 # add myself to known_server_db
81 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
82 primkey=>'hostname',
83 hostname=>$server_address,
84 status=>'myself',
85 hostkey=>$SIPackages_key,
86 timestamp=>&get_time,
87 } );
91 ### functions #################################################################
94 sub get_module_info {
95 my @info = ($server_address,
96 $SIPackages_key,
97 $server,
98 $server_activ,
99 "socket",
100 );
101 return \@info;
102 }
106 sub do_wake {
107 my $host = shift;
108 my $ipaddr = shift || '255.255.255.255';
109 my $port = getservbyname('discard', 'udp');
111 my ($raddr, $them, $proto);
112 my ($hwaddr, $hwaddr_re, $pkt);
114 # get the hardware address (ethernet address)
116 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
117 if ($host =~ m/^$hwaddr_re$/) {
118 $hwaddr = $host;
119 } else {
120 # $host is not a hardware address, try to resolve it
121 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
122 my $ip_addr;
123 if ($host =~ m/^$ip_re$/) {
124 $ip_addr = $host;
125 } else {
126 my $h;
127 unless ($h = gethost($host)) {
128 return undef;
129 }
130 $ip_addr = inet_ntoa($h->addr);
131 }
132 }
134 # Generate magic sequence
135 foreach (split /:/, $hwaddr) {
136 $pkt .= chr(hex($_));
137 }
138 $pkt = chr(0xFF) x 6 . $pkt x 16;
140 # Allocate socket and send packet
142 $raddr = gethostbyname($ipaddr)->addr;
143 $them = pack_sockaddr_in($port, $raddr);
144 $proto = getprotobyname('udp');
146 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
147 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
149 send(S, $pkt, 0, $them) or die "send : $!";
150 close S;
151 }
154 #=== FUNCTION ================================================================
155 # NAME: read_configfile
156 # PARAMETERS: cfg_file - string -
157 # RETURNS: nothing
158 # DESCRIPTION: read cfg_file and set variables
159 #===============================================================================
160 sub read_configfile {
161 my $cfg;
162 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
163 if( -r $main::cfg_file ) {
164 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
165 } else {
166 print STDERR "Couldn't read config file!";
167 }
168 } else {
169 $cfg = Config::IniFiles->new() ;
170 }
171 foreach my $section (keys %cfg_defaults) {
172 foreach my $param (keys %{$cfg_defaults{ $section }}) {
173 my $pinfo = $cfg_defaults{ $section }{ $param };
174 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
175 }
176 }
178 # Read non predefined sections
179 my $param;
180 if ($cfg->SectionExists('ldap')){
181 foreach $param ($cfg->Parameters('ldap')){
182 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
183 }
184 }
185 if ($cfg->SectionExists('pam_ldap')){
186 foreach $param ($cfg->Parameters('pam_ldap')){
187 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
188 }
189 }
190 if ($cfg->SectionExists('nss_ldap')){
191 foreach $param ($cfg->Parameters('nss_ldap')){
192 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
193 }
194 }
195 if ($cfg->SectionExists('goto')){
196 $goto_admin= $cfg->val('goto', 'terminal_admin');
197 $goto_secret= $cfg->val('goto', 'terminal_secret');
198 } else {
199 $goto_admin= undef;
200 $goto_secret= undef;
201 }
203 }
205 #=== FUNCTION ================================================================
206 # NAME: get_interface_for_ip
207 # PARAMETERS: ip address (i.e. 192.168.0.1)
208 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
209 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
210 #===============================================================================
211 sub get_interface_for_ip {
212 my $result;
213 my $ip= shift;
214 if ($ip && length($ip) > 0) {
215 my @ifs= &get_interfaces();
216 if($ip eq "0.0.0.0") {
217 $result = "all";
218 } else {
219 foreach (@ifs) {
220 my $if=$_;
221 if(get_ip($if) eq $ip) {
222 $result = $if;
223 }
224 }
225 }
226 }
227 return $result;
228 }
230 #=== FUNCTION ================================================================
231 # NAME: get_interfaces
232 # PARAMETERS: none
233 # RETURNS: (list of interfaces)
234 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
235 #===============================================================================
236 sub get_interfaces {
237 my @result;
238 my $PROC_NET_DEV= ('/proc/net/dev');
240 open(PROC_NET_DEV, "<$PROC_NET_DEV")
241 or die "Could not open $PROC_NET_DEV";
243 my @ifs = <PROC_NET_DEV>;
245 close(PROC_NET_DEV);
247 # Eat first two line
248 shift @ifs;
249 shift @ifs;
251 chomp @ifs;
252 foreach my $line(@ifs) {
253 my $if= (split /:/, $line)[0];
254 $if =~ s/^\s+//;
255 push @result, $if;
256 }
258 return @result;
259 }
261 #=== FUNCTION ================================================================
262 # NAME: get_mac
263 # PARAMETERS: interface name (i.e. eth0)
264 # RETURNS: (mac address)
265 # DESCRIPTION: Uses ioctl to get mac address directly from system.
266 #===============================================================================
267 sub get_mac {
268 my $ifreq= shift;
269 my $result;
270 if ($ifreq && length($ifreq) > 0) {
271 if($ifreq eq "all") {
272 $result = "00:00:00:00:00:00";
273 } else {
274 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
276 # A configured MAC Address should always override a guessed value
277 if ($server_mac_address and length($server_mac_address) > 0) {
278 $result= $server_mac_address;
279 }
281 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
282 or die "socket: $!";
284 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
285 my ($if, $mac)= unpack 'h36 H12', $ifreq;
287 if (length($mac) > 0) {
288 $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])$/;
289 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
290 $result = $mac;
291 }
292 }
293 }
294 }
295 return $result;
296 }
298 #=== FUNCTION ================================================================
299 # NAME: get_ip
300 # PARAMETERS: interface name (i.e. eth0)
301 # RETURNS: (ip address)
302 # DESCRIPTION: Uses ioctl to get ip address directly from system.
303 #===============================================================================
304 sub get_ip {
305 my $ifreq= shift;
306 my $result= "";
307 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
308 my $proto= getprotobyname('ip');
310 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
311 or die "socket: $!";
313 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
314 my ($if, $sin) = unpack 'a16 a16', $ifreq;
315 my ($port, $addr) = sockaddr_in $sin;
316 my $ip = inet_ntoa $addr;
318 if ($ip && length($ip) > 0) {
319 $result = $ip;
320 }
321 }
323 return $result;
324 }
327 #=== FUNCTION ================================================================
328 # NAME: register_at_bus
329 # PARAMETERS: nothing
330 # RETURNS: nothing
331 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
332 #===============================================================================
333 sub register_at_bus {
335 # add bus to known_server_db
336 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
337 primkey=>'hostname',
338 hostname=>$bus_address,
339 status=>'bus',
340 hostkey=>$bus_passwd,
341 timestamp=>&get_time,
342 } );
343 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
344 my $msg = &create_xml_string($msg_hash);
345 return $msg;
346 # my $answer = "";
347 # $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
348 # if ($answer == 0) {
349 # &main::daemon_log("register at bus: $bus_address", 1);
350 # } else {
351 # &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
352 # }
353 # return;
354 }
357 #=== FUNCTION ================================================================
358 # NAME: process_incoming_msg
359 # PARAMETERS: crypted_msg - string - incoming crypted message
360 # RETURNS: nothing
361 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
362 #===============================================================================
363 sub process_incoming_msg {
364 my ($msg, $msg_hash) = @_ ;
365 my $error = 0;
366 my $host_name;
367 my $host_key;
368 my @out_msg_l;
370 # process incoming msg
371 my $header = @{$msg_hash->{header}}[0];
372 my $source = @{$msg_hash->{source}}[0];
373 my @target_l = @{$msg_hash->{target}};
375 &main::daemon_log("SIPackages: msg to process: $header", 3);
376 &main::daemon_log("$msg", 8);
378 if( 0 == length @target_l){
379 &main::daemon_log("ERROR: no target specified for msg $header", 1);
380 $error++;
381 }
383 if( 1 == length @target_l) {
384 my $target = $target_l[0];
385 if( $target eq $server_address ) {
386 if ($header eq 'new_passwd') {
387 @out_msg_l = &new_passwd($msg_hash)
388 } elsif ($header eq 'here_i_am') {
389 @out_msg_l = &here_i_am($msg_hash)
390 } elsif ($header eq 'who_has') {
391 @out_msg_l = &who_has($msg_hash)
392 } elsif ($header eq 'who_has_i_do') {
393 @out_msg_l = &who_has_i_do($msg_hash)
394 } elsif ($header eq 'got_ping') {
395 @out_msg_l = &got_ping($msg_hash)
396 } elsif ($header eq 'get_load') {
397 @out_msg_l = &execute_actions($msg_hash)
398 } elsif ($header eq 'detected_hardware') {
399 @out_msg_l = &process_detected_hardware($msg_hash)
400 } elsif ($header eq 'trigger_wake') {
401 my $in_hash= &transform_msg2hash($msg);
402 foreach (@{$in_hash->{macAddress}}){
403 &main::daemon_log("SIPackages: trigger wake for $_", 1);
404 do_wake($_);
405 }
407 } else {
408 &main::daemon_log("ERROR: $header is an unknown core function", 1);
409 $error++;
410 }
411 }
412 else {
413 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
414 push(@out_msg_l, $msg);
415 }
416 }
418 if( $error == 0) {
419 if( 0 == @out_msg_l ) {
420 push(@out_msg_l, $msg);
421 }
422 }
424 return \@out_msg_l;
425 }
428 #=== FUNCTION ================================================================
429 # NAME: got_ping
430 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
431 # RETURNS: nothing
432 # DESCRIPTION: process this incoming message
433 #===============================================================================
434 sub got_ping {
435 my ($msg_hash) = @_;
437 my $source = @{$msg_hash->{source}}[0];
438 my $target = @{$msg_hash->{target}}[0];
439 my $header = @{$msg_hash->{header}}[0];
441 if(exists $main::known_daemons->{$source}) {
442 &main::add_content2known_daemons(hostname=>$source, status=>$header);
443 } else {
444 &main::add_content2known_clients(hostname=>$source, status=>$header);
445 }
447 return;
448 }
451 #=== FUNCTION ================================================================
452 # NAME: new_passwd
453 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
454 # RETURNS: nothing
455 # DESCRIPTION: process this incoming message
456 #===============================================================================
457 sub new_passwd {
458 my ($msg_hash) = @_;
459 my @out_msg_l;
461 my $header = @{$msg_hash->{header}}[0];
462 my $source_name = @{$msg_hash->{source}}[0];
463 my $source_key = @{$msg_hash->{new_passwd}}[0];
464 my $query_res;
466 # check known_clients_db
467 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
468 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
469 if( 1 == keys %{$query_res} ) {
470 my $act_time = &get_time;
471 my $sql_statement= "UPDATE known_clients ".
472 "SET hostkey='$source_key', timestamp='$act_time' ".
473 "WHERE hostname='$source_name'";
474 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
476 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
477 my $out_msg = &create_xml_string($hash);
478 push(@out_msg_l, $out_msg);
479 }
481 # only do if host still not found
482 if( 0 == @out_msg_l ) {
483 # check known_server_db
484 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
485 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
486 if( 1 == keys %{$query_res} ) {
487 my $act_time = &get_time;
488 my $sql_statement= "UPDATE known_server ".
489 "SET hostkey='$source_key', timestamp='$act_time' ".
490 "WHERE hostname='$source_name'";
491 my $res = $main::known_server_db->update_dbentry( $sql_statement );
493 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
494 my $out_msg = &create_xml_string($hash);
495 push(@out_msg_l, $out_msg);
496 }
497 }
499 return @out_msg_l;
500 }
503 #=== FUNCTION ================================================================
504 # NAME: here_i_am
505 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
506 # RETURNS: nothing
507 # DESCRIPTION: process this incoming message
508 #===============================================================================
509 sub here_i_am {
510 my ($msg_hash) = @_;
511 my @out_msg_l;
512 my $out_hash;
514 my $source = @{$msg_hash->{source}}[0];
515 my $mac_address = @{$msg_hash->{mac_address}}[0];
516 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
518 # number of known clients
519 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
521 # check wether client address or mac address is already known
522 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
523 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
525 if ( 1 == keys %{$db_res} ) {
526 &main::daemon_log("WARNING: $source is already known as a client", 1);
527 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
528 $nu_clients --;
529 }
531 # number of actual activ clients
532 my $act_nu_clients = $nu_clients;
534 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
535 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
537 if($max_clients <= $act_nu_clients) {
538 my $out_hash = &create_xml_hash("denied", $server_address, $source);
539 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
540 my $passwd = @{$msg_hash->{new_passwd}}[0];
541 &send_msg_hash2address($out_hash, $source, $passwd);
542 return;
543 }
545 # new client accepted
546 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
548 # create entry in known_clients
549 my $events = @{$msg_hash->{events}}[0];
552 # add entry to known_clients_db
553 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
554 primkey=>'hostname',
555 hostname=>$source,
556 events=>$events,
557 macaddress=>$mac_address,
558 status=>'registered',
559 hostkey=>$new_passwd,
560 timestamp=>&get_time,
561 } );
563 if ($res != 0) {
564 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
565 return;
566 }
568 # return acknowledgement to client
569 $out_hash = &create_xml_hash("registered", $server_address, $source);
570 my $register_out = &create_xml_string($out_hash);
571 push(@out_msg_l, $register_out);
573 # notify registered client to bus
574 if( $bus_activ eq "on") {
575 # fetch actual bus key
576 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
577 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
578 my $hostkey = $query_res->{1}->{'hostkey'};
580 # send update msg to bus
581 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
582 my $new_client_out = &create_xml_string($out_hash);
583 push(@out_msg_l, $new_client_out);
584 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
585 }
587 # give the new client his ldap config
588 my $new_ldap_config_out = &new_ldap_config($source);
589 if( $new_ldap_config_out ) {
590 push(@out_msg_l, $new_ldap_config_out);
591 }
593 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
594 if( $hardware_config_out ) {
595 push(@out_msg_l, $hardware_config_out);
596 }
598 return @out_msg_l;
599 }
602 #=== FUNCTION ================================================================
603 # NAME: who_has
604 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
605 # RETURNS: nothing
606 # DESCRIPTION: process this incoming message
607 #===============================================================================
608 sub who_has {
609 my ($msg_hash) = @_ ;
610 my @out_msg_l;
612 # what is your search pattern
613 my $search_pattern = @{$msg_hash->{who_has}}[0];
614 my $search_element = @{$msg_hash->{$search_pattern}}[0];
615 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
617 # scanning known_clients for search_pattern
618 my @host_addresses = keys %$main::known_clients;
619 my $known_clients_entries = length @host_addresses;
620 my $host_address;
621 foreach my $host (@host_addresses) {
622 my $client_element = $main::known_clients->{$host}->{$search_pattern};
623 if ($search_element eq $client_element) {
624 $host_address = $host;
625 last;
626 }
627 }
629 # search was successful
630 if (defined $host_address) {
631 my $source = @{$msg_hash->{source}}[0];
632 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
633 &add_content2xml_hash($out_hash, "mac_address", $search_element);
634 my $out_msg = &create_xml_string($out_hash);
635 push(@out_msg_l, $out_msg);
636 }
637 return @out_msg_l;
638 }
641 sub who_has_i_do {
642 my ($msg_hash) = @_ ;
643 my $header = @{$msg_hash->{header}}[0];
644 my $source = @{$msg_hash->{source}}[0];
645 my $search_param = @{$msg_hash->{$header}}[0];
646 my $search_value = @{$msg_hash->{$search_param}}[0];
647 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
648 }
650 #=== FUNCTION ================================================================
651 # NAME: new_ldap_config
652 # PARAMETERS: address - string - ip address and port of a host
653 # RETURNS: nothing
654 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
655 #===============================================================================
656 sub new_ldap_config {
657 my ($address) = @_ ;
659 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
660 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
662 # check hit
663 my $hit_counter = keys %{$res};
664 if( not $hit_counter == 1 ) {
665 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
666 }
668 my $macaddress = $res->{1}->{macaddress};
669 my $hostkey = $res->{1}->{hostkey};
671 if (not defined $macaddress) {
672 &main::daemon_log("ERROR: no mac address found for client $address", 1);
673 return;
674 }
676 # Build LDAP connection
677 my $ldap = Net::LDAP->new($ldap_uri);
678 if( not defined $ldap ) {
679 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
680 return;
681 }
684 # Bind to a directory with dn and password
685 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
687 # Perform search
688 $mesg = $ldap->search( base => $ldap_base,
689 scope => 'sub',
690 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
691 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
692 #$mesg->code && die $mesg->error;
693 if($mesg->code) {
694 &main::daemon_log($mesg->error, 1);
695 return;
696 }
698 # Sanity check
699 if ($mesg->count != 1) {
700 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
701 &main::daemon_log("\tbase: $ldap_base", 1);
702 &main::daemon_log("\tscope: sub", 1);
703 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
704 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
705 return;
706 }
708 my $entry= $mesg->entry(0);
709 my $dn= $entry->dn;
710 my @servers= $entry->get_value("gotoLdapServer");
711 my $unit_tag= $entry->get_value("gosaUnitTag");
712 my @ldap_uris;
713 my $server;
714 my $base;
716 # Do we need to look at an object class?
717 if (length(@servers) < 1){
718 $mesg = $ldap->search( base => $ldap_base,
719 scope => 'sub',
720 attrs => ['dn', 'gotoLdapServer'],
721 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
722 #$mesg->code && die $mesg->error;
723 if($mesg->code) {
724 &main::daemon_log($mesg->error, 1);
725 return;
726 }
728 # Sanity check
729 if ($mesg->count != 1) {
730 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
731 return;
732 }
734 $entry= $mesg->entry(0);
735 $dn= $entry->dn;
736 @servers= $entry->get_value("gotoLdapServer");
737 }
739 @servers= sort (@servers);
741 foreach $server (@servers){
742 $base= $server;
743 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
744 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
745 push (@ldap_uris, $server);
746 }
748 # Assemble data package
749 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
750 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
752 # Need to append GOto settings?
753 if (defined $goto_admin and defined $goto_secret){
754 $data{'goto_admin'}= $goto_admin;
755 $data{'goto_secret'}= $goto_secret;
756 }
758 # Append unit tag if needed
759 if (defined $unit_tag){
761 # Find admin base and department name
762 $mesg = $ldap->search( base => $ldap_base,
763 scope => 'sub',
764 attrs => ['dn', 'ou'],
765 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
766 #$mesg->code && die $mesg->error;
767 if($mesg->code) {
768 &main::daemon_log($mesg->error, 1);
769 return;
770 }
772 # Sanity check
773 if ($mesg->count != 1) {
774 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
775 return;
776 }
778 $entry= $mesg->entry(0);
779 $data{'admin_base'}= $entry->dn;
780 $data{'department'}= $entry->get_value("ou");
782 # Append unit Tag
783 $data{'unit_tag'}= $unit_tag;
784 }
786 # Unbind
787 $mesg = $ldap->unbind;
789 # Send information
790 return send_msg("new_ldap_config", $server_address, $address, \%data);
791 }
793 sub process_detected_hardware {
794 my $msg_hash = shift;
797 return;
798 }
799 #=== FUNCTION ================================================================
800 # NAME: hardware_config
801 # PARAMETERS: address - string - ip address and port of a host
802 # RETURNS:
803 # DESCRIPTION:
804 #===============================================================================
805 sub hardware_config {
806 my ($address, $gotoHardwareChecksum) = @_ ;
808 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
809 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
811 # check hit
812 my $hit_counter = keys %{$res};
813 if( not $hit_counter == 1 ) {
814 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
815 }
817 my $macaddress = $res->{1}->{macaddress};
818 my $hostkey = $res->{1}->{hostkey};
820 if (not defined $macaddress) {
821 &main::daemon_log("ERROR: no mac address found for client $address", 1);
822 return;
823 }
825 # Build LDAP connection
826 my $ldap = Net::LDAP->new($ldap_uri);
827 if( not defined $ldap ) {
828 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
829 return;
830 }
832 # Bind to a directory with dn and password
833 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
835 # Perform search
836 $mesg = $ldap->search(
837 base => $ldap_base,
838 scope => 'sub',
839 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
840 );
842 if($mesg->count() == 0) {
843 &main::daemon_log("Host was not found in LDAP!", 1);
844 return;
845 }
847 my $entry= $mesg->entry(0);
848 my $dn= $entry->dn;
849 if(defined($entry->get_value("gotoHardwareChecksum"))) {
850 return;
851 } else {
852 # need to fill it to LDAP
853 $entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
854 &main::daemon_log(Dumper($entry->update($ldap)),1);
856 # Look if there another host with this checksum to use the hardware config
857 $mesg = $ldap->search(
858 base => $ldap_base,
859 scope => 'sub',
860 filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
861 );
862 }
864 # Assemble data package
865 my %data = ();
867 # Need to append GOto settings?
868 if (defined $goto_admin and defined $goto_secret){
869 $data{'goto_admin'}= $goto_admin;
870 $data{'goto_secret'}= $goto_secret;
871 }
873 # Unbind
874 $mesg = $ldap->unbind;
876 &main::daemon_log("Send detect_hardware message to $address", 4);
877 # Send information
878 return send_msg("detect_hardware", $server_address, $address, \%data);
879 }
882 #=== FUNCTION ================================================================
883 # NAME: execute_actions
884 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
885 # RETURNS: nothing
886 # DESCRIPTION: invokes the script specified in msg_hash which is located under
887 # /etc/gosad/actions
888 #===============================================================================
889 sub execute_actions {
890 my ($msg_hash) = @_ ;
891 my $configdir= '/etc/gosad/actions/';
892 my $result;
894 my $header = @{$msg_hash->{header}}[0];
895 my $source = @{$msg_hash->{source}}[0];
896 my $target = @{$msg_hash->{target}}[0];
898 if((not defined $source)
899 && (not defined $target)
900 && (not defined $header)) {
901 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
902 } else {
903 my $parameters="";
904 my @params = @{$msg_hash->{$header}};
905 my $params = join(", ", @params);
906 &main::daemon_log("execute_actions: got parameters: $params", 5);
908 if (@params) {
909 foreach my $param (@params) {
910 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
911 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
912 $parameters.= " ".$param_value;
913 }
914 }
916 my $cmd= $configdir.$header."$parameters";
917 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
918 $result= "";
919 open(PIPE, "$cmd 2>&1 |");
920 while(<PIPE>) {
921 $result.=$_;
922 }
923 close(PIPE);
924 }
926 # process the event result
929 return;
930 }
933 1;