9924be37fde3d8e287acd668383e523834b314ee
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 foreach (@{$msg_hash->{macAddress}}){
402 &main::daemon_log("SIPackages: trigger wake for $_", 1);
403 do_wake($_);
404 }
406 } else {
407 &main::daemon_log("ERROR: $header is an unknown core function", 1);
408 $error++;
409 }
410 }
411 else {
412 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
413 push(@out_msg_l, $msg);
414 }
415 }
417 if( $error == 0) {
418 if( 0 == @out_msg_l ) {
419 push(@out_msg_l, $msg);
420 }
421 }
423 return \@out_msg_l;
424 }
427 #=== FUNCTION ================================================================
428 # NAME: got_ping
429 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
430 # RETURNS: nothing
431 # DESCRIPTION: process this incoming message
432 #===============================================================================
433 sub got_ping {
434 my ($msg_hash) = @_;
436 my $source = @{$msg_hash->{source}}[0];
437 my $target = @{$msg_hash->{target}}[0];
438 my $header = @{$msg_hash->{header}}[0];
440 if(exists $main::known_daemons->{$source}) {
441 &main::add_content2known_daemons(hostname=>$source, status=>$header);
442 } else {
443 &main::add_content2known_clients(hostname=>$source, status=>$header);
444 }
446 return;
447 }
450 #=== FUNCTION ================================================================
451 # NAME: new_passwd
452 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
453 # RETURNS: nothing
454 # DESCRIPTION: process this incoming message
455 #===============================================================================
456 sub new_passwd {
457 my ($msg_hash) = @_;
458 my @out_msg_l;
460 my $header = @{$msg_hash->{header}}[0];
461 my $source_name = @{$msg_hash->{source}}[0];
462 my $source_key = @{$msg_hash->{new_passwd}}[0];
463 my $query_res;
465 # check known_clients_db
466 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
467 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
468 if( 1 == keys %{$query_res} ) {
469 my $act_time = &get_time;
470 my $sql_statement= "UPDATE known_clients ".
471 "SET hostkey='$source_key', timestamp='$act_time' ".
472 "WHERE hostname='$source_name'";
473 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
475 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
476 my $out_msg = &create_xml_string($hash);
477 push(@out_msg_l, $out_msg);
478 }
480 # only do if host still not found
481 if( 0 == @out_msg_l ) {
482 # check known_server_db
483 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
484 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
485 if( 1 == keys %{$query_res} ) {
486 my $act_time = &get_time;
487 my $sql_statement= "UPDATE known_server ".
488 "SET hostkey='$source_key', timestamp='$act_time' ".
489 "WHERE hostname='$source_name'";
490 my $res = $main::known_server_db->update_dbentry( $sql_statement );
492 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
493 my $out_msg = &create_xml_string($hash);
494 push(@out_msg_l, $out_msg);
495 }
496 }
498 return @out_msg_l;
499 }
502 #=== FUNCTION ================================================================
503 # NAME: here_i_am
504 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
505 # RETURNS: nothing
506 # DESCRIPTION: process this incoming message
507 #===============================================================================
508 sub here_i_am {
509 my ($msg_hash) = @_;
510 my @out_msg_l;
511 my $out_hash;
513 my $source = @{$msg_hash->{source}}[0];
514 my $mac_address = @{$msg_hash->{mac_address}}[0];
515 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
517 # number of known clients
518 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
520 # check wether client address or mac address is already known
521 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
522 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
524 if ( 1 == keys %{$db_res} ) {
525 &main::daemon_log("WARNING: $source is already known as a client", 1);
526 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
527 $nu_clients --;
528 }
530 # number of actual activ clients
531 my $act_nu_clients = $nu_clients;
533 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
534 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
536 if($max_clients <= $act_nu_clients) {
537 my $out_hash = &create_xml_hash("denied", $server_address, $source);
538 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
539 my $passwd = @{$msg_hash->{new_passwd}}[0];
540 &send_msg_hash2address($out_hash, $source, $passwd);
541 return;
542 }
544 # new client accepted
545 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
547 # create entry in known_clients
548 my $events = @{$msg_hash->{events}}[0];
551 # add entry to known_clients_db
552 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
553 primkey=>'hostname',
554 hostname=>$source,
555 events=>$events,
556 macaddress=>$mac_address,
557 status=>'registered',
558 hostkey=>$new_passwd,
559 timestamp=>&get_time,
560 } );
562 if ($res != 0) {
563 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
564 return;
565 }
567 # return acknowledgement to client
568 $out_hash = &create_xml_hash("registered", $server_address, $source);
569 my $register_out = &create_xml_string($out_hash);
570 push(@out_msg_l, $register_out);
572 # notify registered client to bus
573 if( $bus_activ eq "on") {
574 # fetch actual bus key
575 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
576 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
577 my $hostkey = $query_res->{1}->{'hostkey'};
579 # send update msg to bus
580 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
581 my $new_client_out = &create_xml_string($out_hash);
582 push(@out_msg_l, $new_client_out);
583 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
584 }
586 # give the new client his ldap config
587 my $new_ldap_config_out = &new_ldap_config($source);
588 if( $new_ldap_config_out ) {
589 push(@out_msg_l, $new_ldap_config_out);
590 }
592 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
593 if( $hardware_config_out ) {
594 push(@out_msg_l, $hardware_config_out);
595 }
597 return @out_msg_l;
598 }
601 #=== FUNCTION ================================================================
602 # NAME: who_has
603 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
604 # RETURNS: nothing
605 # DESCRIPTION: process this incoming message
606 #===============================================================================
607 sub who_has {
608 my ($msg_hash) = @_ ;
609 my @out_msg_l;
611 # what is your search pattern
612 my $search_pattern = @{$msg_hash->{who_has}}[0];
613 my $search_element = @{$msg_hash->{$search_pattern}}[0];
614 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
616 # scanning known_clients for search_pattern
617 my @host_addresses = keys %$main::known_clients;
618 my $known_clients_entries = length @host_addresses;
619 my $host_address;
620 foreach my $host (@host_addresses) {
621 my $client_element = $main::known_clients->{$host}->{$search_pattern};
622 if ($search_element eq $client_element) {
623 $host_address = $host;
624 last;
625 }
626 }
628 # search was successful
629 if (defined $host_address) {
630 my $source = @{$msg_hash->{source}}[0];
631 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
632 &add_content2xml_hash($out_hash, "mac_address", $search_element);
633 my $out_msg = &create_xml_string($out_hash);
634 push(@out_msg_l, $out_msg);
635 }
636 return @out_msg_l;
637 }
640 sub who_has_i_do {
641 my ($msg_hash) = @_ ;
642 my $header = @{$msg_hash->{header}}[0];
643 my $source = @{$msg_hash->{source}}[0];
644 my $search_param = @{$msg_hash->{$header}}[0];
645 my $search_value = @{$msg_hash->{$search_param}}[0];
646 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
647 }
649 #=== FUNCTION ================================================================
650 # NAME: new_ldap_config
651 # PARAMETERS: address - string - ip address and port of a host
652 # RETURNS: nothing
653 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
654 #===============================================================================
655 sub new_ldap_config {
656 my ($address) = @_ ;
658 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
659 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
661 # check hit
662 my $hit_counter = keys %{$res};
663 if( not $hit_counter == 1 ) {
664 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
665 }
667 my $macaddress = $res->{1}->{macaddress};
668 my $hostkey = $res->{1}->{hostkey};
670 if (not defined $macaddress) {
671 &main::daemon_log("ERROR: no mac address found for client $address", 1);
672 return;
673 }
675 # Build LDAP connection
676 my $ldap = Net::LDAP->new($ldap_uri);
677 if( not defined $ldap ) {
678 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
679 return;
680 }
683 # Bind to a directory with dn and password
684 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
686 # Perform search
687 $mesg = $ldap->search( base => $ldap_base,
688 scope => 'sub',
689 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
690 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
691 #$mesg->code && die $mesg->error;
692 if($mesg->code) {
693 &main::daemon_log($mesg->error, 1);
694 return;
695 }
697 # Sanity check
698 if ($mesg->count != 1) {
699 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
700 &main::daemon_log("\tbase: $ldap_base", 1);
701 &main::daemon_log("\tscope: sub", 1);
702 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
703 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
704 return;
705 }
707 my $entry= $mesg->entry(0);
708 my $dn= $entry->dn;
709 my @servers= $entry->get_value("gotoLdapServer");
710 my $unit_tag= $entry->get_value("gosaUnitTag");
711 my @ldap_uris;
712 my $server;
713 my $base;
715 # Do we need to look at an object class?
716 if (length(@servers) < 1){
717 $mesg = $ldap->search( base => $ldap_base,
718 scope => 'sub',
719 attrs => ['dn', 'gotoLdapServer'],
720 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
721 #$mesg->code && die $mesg->error;
722 if($mesg->code) {
723 &main::daemon_log($mesg->error, 1);
724 return;
725 }
727 # Sanity check
728 if ($mesg->count != 1) {
729 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
730 return;
731 }
733 $entry= $mesg->entry(0);
734 $dn= $entry->dn;
735 @servers= $entry->get_value("gotoLdapServer");
736 }
738 @servers= sort (@servers);
740 foreach $server (@servers){
741 $base= $server;
742 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
743 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
744 push (@ldap_uris, $server);
745 }
747 # Assemble data package
748 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
749 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
751 # Need to append GOto settings?
752 if (defined $goto_admin and defined $goto_secret){
753 $data{'goto_admin'}= $goto_admin;
754 $data{'goto_secret'}= $goto_secret;
755 }
757 # Append unit tag if needed
758 if (defined $unit_tag){
760 # Find admin base and department name
761 $mesg = $ldap->search( base => $ldap_base,
762 scope => 'sub',
763 attrs => ['dn', 'ou'],
764 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
765 #$mesg->code && die $mesg->error;
766 if($mesg->code) {
767 &main::daemon_log($mesg->error, 1);
768 return;
769 }
771 # Sanity check
772 if ($mesg->count != 1) {
773 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
774 return;
775 }
777 $entry= $mesg->entry(0);
778 $data{'admin_base'}= $entry->dn;
779 $data{'department'}= $entry->get_value("ou");
781 # Append unit Tag
782 $data{'unit_tag'}= $unit_tag;
783 }
785 # Unbind
786 $mesg = $ldap->unbind;
788 # Send information
789 return send_msg("new_ldap_config", $server_address, $address, \%data);
790 }
792 sub process_detected_hardware {
793 my $msg_hash = shift;
794 my $address = $msg_hash->{source}[0];
796 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
797 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
799 # check hit
800 my $hit_counter = keys %{$res};
801 if( not $hit_counter == 1 ) {
802 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
803 }
805 my $macaddress = $res->{1}->{macaddress};
806 my $hostkey = $res->{1}->{hostkey};
808 if (not defined $macaddress) {
809 &main::daemon_log("ERROR: no mac address found for client $address", 1);
810 return;
811 }
812 # Build LDAP connection
813 my $ldap = Net::LDAP->new($ldap_uri);
814 if( not defined $ldap ) {
815 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
816 return;
817 }
819 # Bind to a directory with dn and password
820 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
822 # Perform search
823 $mesg = $ldap->search(
824 base => $ldap_base,
825 scope => 'sub',
826 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
827 );
829 if($mesg->count == 1) {
830 my $entry= $mesg->entry(0);
831 $entry->changetype("modify");
832 foreach my $attribute (
833 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
834 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
835 "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
836 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
837 if(defined($entry->get_value($attribute))) {
838 $entry->delete($attribute);
839 }
840 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
841 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
842 }
843 }
844 foreach my $attribute (
845 "gotoModules", "ghScsiDev", "ghIdeDev") {
846 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
847 if(defined($entry->get_value($attribute))) {
848 $entry->delete($attribute);
849 }
850 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
851 $entry->add($attribute => $array_entry);
852 }
853 }
855 }
856 if($entry->update($ldap)) {
857 &main::daemon_log("Added Hardware configuration to LDAP", 4);
858 }
860 }
861 return;
862 }
863 #=== FUNCTION ================================================================
864 # NAME: hardware_config
865 # PARAMETERS: address - string - ip address and port of a host
866 # RETURNS:
867 # DESCRIPTION:
868 #===============================================================================
869 sub hardware_config {
870 my ($address, $gotoHardwareChecksum) = @_ ;
872 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
873 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
875 # check hit
876 my $hit_counter = keys %{$res};
877 if( not $hit_counter == 1 ) {
878 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
879 }
881 my $macaddress = $res->{1}->{macaddress};
882 my $hostkey = $res->{1}->{hostkey};
884 if (not defined $macaddress) {
885 &main::daemon_log("ERROR: no mac address found for client $address", 1);
886 return;
887 }
889 # Build LDAP connection
890 my $ldap = Net::LDAP->new($ldap_uri);
891 if( not defined $ldap ) {
892 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
893 return;
894 }
896 # Bind to a directory with dn and password
897 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
899 # Perform search
900 $mesg = $ldap->search(
901 base => $ldap_base,
902 scope => 'sub',
903 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
904 );
906 if($mesg->count() == 0) {
907 &main::daemon_log("Host was not found in LDAP!", 1);
908 return;
909 }
911 my $entry= $mesg->entry(0);
912 my $dn= $entry->dn;
913 if(defined($entry->get_value("gotoHardwareChecksum"))) {
914 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
915 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
916 if($entry->update($ldap)) {
917 &main::daemon_log("Hardware changed! Detection triggered.", 4);
918 }
919 } else {
920 # Nothing to do
921 return;
922 }
923 } else {
924 # need to fill it to LDAP
925 $entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
926 if($entry->update($ldap)) {
927 &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
928 }
930 ## Look if there another host with this checksum to use the hardware config
931 #$mesg = $ldap->search(
932 # base => $ldap_base,
933 # scope => 'sub',
934 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
935 #);
937 #if($mesg->count>1) {
938 # my $clone_entry= $mesg->entry(0);
939 # $entry->changetype("modify");
940 # foreach my $attribute (
941 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
942 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
943 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
944 # my $value= $clone_entry->get_value($attribute);
945 # if(defined($value)) {
946 # if(defined($entry->get_value($attribute))) {
947 # $entry->delete($attribute);
948 # }
949 # &main::daemon_log("Adding attribute $attribute with value $value",1);
950 # $entry->add($attribute => $value);
951 # }
952 # }
953 # foreach my $attribute (
954 # "gotoModules", "ghScsiDev", "ghIdeDev") {
955 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
956 # if(defined($array)) {
957 # if(defined($entry->get_value($attribute))) {
958 # $entry->delete($attribute);
959 # }
960 # foreach my $array_entry (@{$array}) {
961 # $entry->add($attribute => $array_entry);
962 # }
963 # }
965 # }
966 # if($entry->update($ldap)) {
967 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
968 # }
970 #}
972 }
974 # Assemble data package
975 my %data = ();
977 # Need to append GOto settings?
978 if (defined $goto_admin and defined $goto_secret){
979 $data{'goto_admin'}= $goto_admin;
980 $data{'goto_secret'}= $goto_secret;
981 }
983 # Unbind
984 $mesg = $ldap->unbind;
986 &main::daemon_log("Send detect_hardware message to $address", 4);
988 # Send information
989 return send_msg("detect_hardware", $server_address, $address, \%data);
990 }
993 #=== FUNCTION ================================================================
994 # NAME: execute_actions
995 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
996 # RETURNS: nothing
997 # DESCRIPTION: invokes the script specified in msg_hash which is located under
998 # /etc/gosad/actions
999 #===============================================================================
1000 sub execute_actions {
1001 my ($msg_hash) = @_ ;
1002 my $configdir= '/etc/gosad/actions/';
1003 my $result;
1005 my $header = @{$msg_hash->{header}}[0];
1006 my $source = @{$msg_hash->{source}}[0];
1007 my $target = @{$msg_hash->{target}}[0];
1009 if((not defined $source)
1010 && (not defined $target)
1011 && (not defined $header)) {
1012 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1013 } else {
1014 my $parameters="";
1015 my @params = @{$msg_hash->{$header}};
1016 my $params = join(", ", @params);
1017 &main::daemon_log("execute_actions: got parameters: $params", 5);
1019 if (@params) {
1020 foreach my $param (@params) {
1021 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1022 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1023 $parameters.= " ".$param_value;
1024 }
1025 }
1027 my $cmd= $configdir.$header."$parameters";
1028 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1029 $result= "";
1030 open(PIPE, "$cmd 2>&1 |");
1031 while(<PIPE>) {
1032 $result.=$_;
1033 }
1034 close(PIPE);
1035 }
1037 # process the event result
1040 return;
1041 }
1044 1;