184c58e57ced7ae7d719ff2f4686ff565691ee89
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 NetAddr::IP;
16 use Net::LDAP;
17 use Socket;
18 use Net::hostent;
19 use Net::DNS;
21 BEGIN{}
22 END {}
24 my ($known_clients_file_name);
25 my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
26 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
27 my $server;
28 my $network_interface;
29 my $no_bus;
30 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
33 my %cfg_defaults =
34 (
35 "server" =>
36 {"server_activ" => [\$server_activ, "on"],
37 "server_ip" => [\$server_ip, "0.0.0.0"],
38 "server_mac_address" => [\$server_mac_address, "00:00:00:00:00"],
39 "server_port" => [\$server_port, "20081"],
40 "SIPackages_key" => [\$SIPackages_key, ""],
41 "max_clients" => [\$max_clients, 100],
42 "ldap_uri" => [\$ldap_uri, ""],
43 "ldap_base" => [\$ldap_base, ""],
44 "ldap_admin_dn" => [\$ldap_admin_dn, ""],
45 "ldap_admin_password" => [\$ldap_admin_password, ""],
46 },
47 "bus" =>
48 {"bus_activ" => [\$bus_activ, "on"],
49 "bus_passwd" => [\$bus_key, ""],
50 "bus_ip" => [\$bus_ip, ""],
51 "bus_port" => [\$bus_port, "20080"],
52 },
53 );
55 ### START #####################################################################
57 # read configfile and import variables
58 &read_configfile();
60 $network_interface= &get_interface_for_ip($server_ip);
61 $server_mac_address= &get_mac($network_interface);
63 # complete addresses
64 #if( $server_ip eq "0.0.0.0" ) {
65 # $server_ip = "127.0.0.1";
66 #}
67 my $server_address = "$server_ip:$server_port";
68 $main::server_address = $server_address;
69 my $bus_address = "$bus_ip:$bus_port";
70 $main::bus_address = $bus_address;
72 # create general settings for this module
73 my $xml = new XML::Simple();
75 # register at bus
76 if ($main::no_bus > 0) {
77 $bus_activ = "off"
78 }
79 if($bus_activ eq "on") {
80 ®ister_at_bus();
81 }
83 # add myself to known_server_db
84 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
85 primkey=>'hostname',
86 hostname=>$server_address,
87 status=>'myself',
88 hostkey=>$SIPackages_key,
89 timestamp=>&get_time,
90 } );
94 ### functions #################################################################
97 sub get_module_info {
98 my @info = ($server_address,
99 $SIPackages_key,
100 $server,
101 $server_activ,
102 "socket",
103 );
104 return \@info;
105 }
109 sub do_wake {
110 my $host = shift;
111 my $ipaddr = shift || '255.255.255.255';
112 my $port = getservbyname('discard', 'udp');
114 my ($raddr, $them, $proto);
115 my ($hwaddr, $hwaddr_re, $pkt);
117 # get the hardware address (ethernet address)
119 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
120 if ($host =~ m/^$hwaddr_re$/) {
121 $hwaddr = $host;
122 } else {
123 # $host is not a hardware address, try to resolve it
124 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
125 my $ip_addr;
126 if ($host =~ m/^$ip_re$/) {
127 $ip_addr = $host;
128 } else {
129 my $h;
130 unless ($h = gethost($host)) {
131 return undef;
132 }
133 $ip_addr = inet_ntoa($h->addr);
134 }
135 }
137 # Generate magic sequence
138 foreach (split /:/, $hwaddr) {
139 $pkt .= chr(hex($_));
140 }
141 $pkt = chr(0xFF) x 6 . $pkt x 16;
143 # Allocate socket and send packet
145 $raddr = gethostbyname($ipaddr)->addr;
146 $them = pack_sockaddr_in($port, $raddr);
147 $proto = getprotobyname('udp');
149 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
150 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
152 send(S, $pkt, 0, $them) or die "send : $!";
153 close S;
154 }
157 #=== FUNCTION ================================================================
158 # NAME: read_configfile
159 # PARAMETERS: cfg_file - string -
160 # RETURNS: nothing
161 # DESCRIPTION: read cfg_file and set variables
162 #===============================================================================
163 sub read_configfile {
164 my $cfg;
165 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
166 if( -r $main::cfg_file ) {
167 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
168 } else {
169 print STDERR "Couldn't read config file!";
170 }
171 } else {
172 $cfg = Config::IniFiles->new() ;
173 }
174 foreach my $section (keys %cfg_defaults) {
175 foreach my $param (keys %{$cfg_defaults{ $section }}) {
176 my $pinfo = $cfg_defaults{ $section }{ $param };
177 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
178 }
179 }
181 # Read non predefined sections
182 my $param;
183 if ($cfg->SectionExists('ldap')){
184 foreach $param ($cfg->Parameters('ldap')){
185 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
186 }
187 }
188 if ($cfg->SectionExists('pam_ldap')){
189 foreach $param ($cfg->Parameters('pam_ldap')){
190 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
191 }
192 }
193 if ($cfg->SectionExists('nss_ldap')){
194 foreach $param ($cfg->Parameters('nss_ldap')){
195 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
196 }
197 }
198 if ($cfg->SectionExists('goto')){
199 $goto_admin= $cfg->val('goto', 'terminal_admin');
200 $goto_secret= $cfg->val('goto', 'terminal_secret');
201 } else {
202 $goto_admin= undef;
203 $goto_secret= undef;
204 }
206 }
208 #=== FUNCTION ================================================================
209 # NAME: get_interface_for_ip
210 # PARAMETERS: ip address (i.e. 192.168.0.1)
211 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
212 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
213 #===============================================================================
214 sub get_interface_for_ip {
215 my $result;
216 my $ip= shift;
217 if ($ip && length($ip) > 0) {
218 my @ifs= &get_interfaces();
219 if($ip eq "0.0.0.0") {
220 $result = "all";
221 } else {
222 foreach (@ifs) {
223 my $if=$_;
224 if(get_ip($if) eq $ip) {
225 $result = $if;
226 }
227 }
228 }
229 }
230 return $result;
231 }
233 #=== FUNCTION ================================================================
234 # NAME: get_interfaces
235 # PARAMETERS: none
236 # RETURNS: (list of interfaces)
237 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
238 #===============================================================================
239 sub get_interfaces {
240 my @result;
241 my $PROC_NET_DEV= ('/proc/net/dev');
243 open(PROC_NET_DEV, "<$PROC_NET_DEV")
244 or die "Could not open $PROC_NET_DEV";
246 my @ifs = <PROC_NET_DEV>;
248 close(PROC_NET_DEV);
250 # Eat first two line
251 shift @ifs;
252 shift @ifs;
254 chomp @ifs;
255 foreach my $line(@ifs) {
256 my $if= (split /:/, $line)[0];
257 $if =~ s/^\s+//;
258 push @result, $if;
259 }
261 return @result;
262 }
264 #=== FUNCTION ================================================================
265 # NAME: get_mac
266 # PARAMETERS: interface name (i.e. eth0)
267 # RETURNS: (mac address)
268 # DESCRIPTION: Uses ioctl to get mac address directly from system.
269 #===============================================================================
270 sub get_mac {
271 my $ifreq= shift;
272 my $result;
273 if ($ifreq && length($ifreq) > 0) {
274 if($ifreq eq "all") {
275 $result = "00:00:00:00:00:00";
276 } else {
277 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
279 # A configured MAC Address should always override a guessed value
280 if ($server_mac_address and length($server_mac_address) > 0) {
281 $result= $server_mac_address;
282 }
284 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
285 or die "socket: $!";
287 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
288 my ($if, $mac)= unpack 'h36 H12', $ifreq;
290 if (length($mac) > 0) {
291 $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])$/;
292 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
293 $result = $mac;
294 }
295 }
296 }
297 }
298 return $result;
299 }
301 #=== FUNCTION ================================================================
302 # NAME: get_ip
303 # PARAMETERS: interface name (i.e. eth0)
304 # RETURNS: (ip address)
305 # DESCRIPTION: Uses ioctl to get ip address directly from system.
306 #===============================================================================
307 sub get_ip {
308 my $ifreq= shift;
309 my $result= "";
310 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
311 my $proto= getprotobyname('ip');
313 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
314 or die "socket: $!";
316 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
317 my ($if, $sin) = unpack 'a16 a16', $ifreq;
318 my ($port, $addr) = sockaddr_in $sin;
319 my $ip = inet_ntoa $addr;
321 if ($ip && length($ip) > 0) {
322 $result = $ip;
323 }
324 }
326 return $result;
327 }
330 #=== FUNCTION ================================================================
331 # NAME: register_at_bus
332 # PARAMETERS: nothing
333 # RETURNS: nothing
334 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
335 #===============================================================================
336 sub register_at_bus {
338 # add bus to known_server_db
339 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
340 primkey=>'hostname',
341 hostname=>$bus_address,
342 status=>'bus',
343 hostkey=>$bus_key,
344 timestamp=>&get_time,
345 } );
346 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
347 my $msg = &create_xml_string($msg_hash);
349 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
350 return $msg;
351 }
354 #=== FUNCTION ================================================================
355 # NAME: process_incoming_msg
356 # PARAMETERS: crypted_msg - string - incoming crypted message
357 # RETURNS: nothing
358 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
359 #===============================================================================
360 sub process_incoming_msg {
361 my ($msg, $msg_hash, $remote_ip) = @_ ;
362 my $error = 0;
363 my $host_name;
364 my $host_key;
365 my @out_msg_l;
367 # process incoming msg
368 my $header = @{$msg_hash->{header}}[0];
369 my @target_l = @{$msg_hash->{target}};
371 &main::daemon_log("SIPackages: msg to process: $header", 3);
372 &main::daemon_log("$msg", 8);
374 if( 0 == length @target_l){
375 &main::daemon_log("ERROR: no target specified for msg $header", 1);
376 $error++;
377 }
379 if( 1 == length @target_l) {
380 my $target = $target_l[0];
381 if(&server_matches($target)) {
382 if ($header eq 'new_key') {
383 @out_msg_l = &new_key($msg_hash)
384 } elsif ($header eq 'here_i_am') {
385 @out_msg_l = &here_i_am($msg_hash)
386 } elsif ($header eq 'who_has') {
387 @out_msg_l = &who_has($msg_hash)
388 } elsif ($header eq 'who_has_i_do') {
389 @out_msg_l = &who_has_i_do($msg_hash)
390 } elsif ($header eq 'got_ping') {
391 @out_msg_l = &got_ping($msg_hash)
392 } elsif ($header eq 'get_load') {
393 @out_msg_l = &execute_actions($msg_hash)
394 } elsif ($header eq 'detected_hardware') {
395 @out_msg_l = &process_detected_hardware($msg_hash)
396 } elsif ($header eq 'trigger_wake') {
397 foreach (@{$msg_hash->{macAddress}}){
398 &main::daemon_log("SIPackages: trigger wake for $_", 1);
399 do_wake($_);
400 }
402 } else {
403 &main::daemon_log("ERROR: $header is an unknown core function", 1);
404 $error++;
405 }
406 }
407 else {
408 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
409 push(@out_msg_l, $msg);
410 }
411 }
413 # if( $error == 0) {
414 # if( 0 == @out_msg_l ) {
415 # push(@out_msg_l, $msg);
416 # }
417 # }
419 return \@out_msg_l;
420 }
423 #=== FUNCTION ================================================================
424 # NAME: got_ping
425 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
426 # RETURNS: nothing
427 # DESCRIPTION: process this incoming message
428 #===============================================================================
429 sub got_ping {
430 my ($msg_hash) = @_;
432 my $source = @{$msg_hash->{source}}[0];
433 my $target = @{$msg_hash->{target}}[0];
434 my $header = @{$msg_hash->{header}}[0];
436 if(exists $main::known_daemons->{$source}) {
437 &main::add_content2known_daemons(hostname=>$source, status=>$header);
438 } else {
439 &main::add_content2known_clients(hostname=>$source, status=>$header);
440 }
442 return;
443 }
446 #=== FUNCTION ================================================================
447 # NAME: new_passwd
448 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
449 # RETURNS: nothing
450 # DESCRIPTION: process this incoming message
451 #===============================================================================
452 sub new_key {
453 my ($msg_hash) = @_;
454 my @out_msg_l;
456 my $header = @{$msg_hash->{header}}[0];
457 my $source_name = @{$msg_hash->{source}}[0];
458 my $source_key = @{$msg_hash->{new_key}}[0];
459 my $query_res;
461 # check known_clients_db
462 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
463 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
464 if( 1 == keys %{$query_res} ) {
465 my $act_time = &get_time;
466 my $sql_statement= "UPDATE known_clients ".
467 "SET hostkey='$source_key', timestamp='$act_time' ".
468 "WHERE hostname='$source_name'";
469 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
470 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
471 my $out_msg = &create_xml_string($hash);
472 push(@out_msg_l, $out_msg);
473 }
475 # only do if host still not found
476 if( 0 == @out_msg_l ) {
477 # check known_server_db
478 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
479 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
480 if( 1 == keys %{$query_res} ) {
481 my $act_time = &get_time;
482 my $sql_statement= "UPDATE known_server ".
483 "SET hostkey='$source_key', timestamp='$act_time' ".
484 "WHERE hostname='$source_name'";
485 my $res = $main::known_server_db->update_dbentry( $sql_statement );
487 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
488 my $out_msg = &create_xml_string($hash);
489 push(@out_msg_l, $out_msg);
490 }
491 }
493 return @out_msg_l;
494 }
497 #=== FUNCTION ================================================================
498 # NAME: here_i_am
499 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
500 # RETURNS: nothing
501 # DESCRIPTION: process this incoming message
502 #===============================================================================
503 sub here_i_am {
504 my ($msg_hash) = @_;
505 my @out_msg_l;
506 my $out_hash;
508 my $source = @{$msg_hash->{source}}[0];
509 my $mac_address = @{$msg_hash->{mac_address}}[0];
510 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
512 # number of known clients
513 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
515 # check wether client address or mac address is already known
516 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
517 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
519 if ( 1 == keys %{$db_res} ) {
520 &main::daemon_log("WARNING: $source is already known as a client", 1);
521 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
522 $nu_clients --;
523 }
525 # number of actual activ clients
526 my $act_nu_clients = $nu_clients;
528 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
529 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
531 if($max_clients <= $act_nu_clients) {
532 my $out_hash = &create_xml_hash("denied", $server_address, $source);
533 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
534 my $passwd = @{$msg_hash->{new_passwd}}[0];
535 &send_msg_hash2address($out_hash, $source, $passwd);
536 return;
537 }
539 # new client accepted
540 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
542 # create entry in known_clients
543 my $events = @{$msg_hash->{events}}[0];
546 # add entry to known_clients_db
547 my $act_timestamp = &get_time;
548 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
549 primkey=>'hostname',
550 hostname=>$source,
551 events=>$events,
552 macaddress=>$mac_address,
553 status=>'registered',
554 hostkey=>$new_passwd,
555 timestamp=>$act_timestamp,
556 } );
558 if ($res != 0) {
559 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
560 return;
561 }
563 # return acknowledgement to client
564 $out_hash = &create_xml_hash("registered", $server_address, $source);
565 my $register_out = &create_xml_string($out_hash);
566 push(@out_msg_l, $register_out);
568 # notify registered client to bus
569 if( $bus_activ eq "on") {
570 # fetch actual bus key
571 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
572 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
573 my $hostkey = $query_res->{1}->{'hostkey'};
575 # send update msg to bus
576 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
577 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
578 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
579 my $new_client_out = &create_xml_string($out_hash);
580 push(@out_msg_l, $new_client_out);
581 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
582 }
584 # give the new client his ldap config
585 my $new_ldap_config_out = &new_ldap_config($source);
586 if( $new_ldap_config_out ) {
587 push(@out_msg_l, $new_ldap_config_out);
588 }
590 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
591 if( $hardware_config_out ) {
592 push(@out_msg_l, $hardware_config_out);
593 }
595 return @out_msg_l;
596 }
599 #=== FUNCTION ================================================================
600 # NAME: who_has
601 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
602 # RETURNS: nothing
603 # DESCRIPTION: process this incoming message
604 #===============================================================================
605 sub who_has {
606 my ($msg_hash) = @_ ;
607 my @out_msg_l;
609 # what is your search pattern
610 my $search_pattern = @{$msg_hash->{who_has}}[0];
611 my $search_element = @{$msg_hash->{$search_pattern}}[0];
612 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
614 # scanning known_clients for search_pattern
615 my @host_addresses = keys %$main::known_clients;
616 my $known_clients_entries = length @host_addresses;
617 my $host_address;
618 foreach my $host (@host_addresses) {
619 my $client_element = $main::known_clients->{$host}->{$search_pattern};
620 if ($search_element eq $client_element) {
621 $host_address = $host;
622 last;
623 }
624 }
626 # search was successful
627 if (defined $host_address) {
628 my $source = @{$msg_hash->{source}}[0];
629 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
630 &add_content2xml_hash($out_hash, "mac_address", $search_element);
631 my $out_msg = &create_xml_string($out_hash);
632 push(@out_msg_l, $out_msg);
633 }
634 return @out_msg_l;
635 }
638 sub who_has_i_do {
639 my ($msg_hash) = @_ ;
640 my $header = @{$msg_hash->{header}}[0];
641 my $source = @{$msg_hash->{source}}[0];
642 my $search_param = @{$msg_hash->{$header}}[0];
643 my $search_value = @{$msg_hash->{$search_param}}[0];
644 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
645 }
647 #=== FUNCTION ================================================================
648 # NAME: new_ldap_config
649 # PARAMETERS: address - string - ip address and port of a host
650 # RETURNS: nothing
651 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
652 #===============================================================================
653 sub new_ldap_config {
654 my ($address) = @_ ;
656 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
657 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
659 # check hit
660 my $hit_counter = keys %{$res};
661 if( not $hit_counter == 1 ) {
662 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
663 }
665 my $macaddress = $res->{1}->{macaddress};
666 my $hostkey = $res->{1}->{hostkey};
668 if (not defined $macaddress) {
669 &main::daemon_log("ERROR: no mac address found for client $address", 1);
670 return;
671 }
673 # Build LDAP connection
674 my $ldap = Net::LDAP->new($ldap_uri);
675 if( not defined $ldap ) {
676 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
677 return;
678 }
681 # Bind to a directory with dn and password
682 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
684 # Perform search
685 $mesg = $ldap->search( base => $ldap_base,
686 scope => 'sub',
687 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
688 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
689 #$mesg->code && die $mesg->error;
690 if($mesg->code) {
691 &main::daemon_log($mesg->error, 1);
692 return;
693 }
695 # Sanity check
696 if ($mesg->count != 1) {
697 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
698 &main::daemon_log("\tbase: $ldap_base", 1);
699 &main::daemon_log("\tscope: sub", 1);
700 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
701 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
702 return;
703 }
705 my $entry= $mesg->entry(0);
706 my $dn= $entry->dn;
707 my @servers= $entry->get_value("gotoLdapServer");
708 my $unit_tag= $entry->get_value("gosaUnitTag");
709 my @ldap_uris;
710 my $server;
711 my $base;
713 # Do we need to look at an object class?
714 if (length(@servers) < 1){
715 $mesg = $ldap->search( base => $ldap_base,
716 scope => 'sub',
717 attrs => ['dn', 'gotoLdapServer'],
718 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
719 #$mesg->code && die $mesg->error;
720 if($mesg->code) {
721 &main::daemon_log($mesg->error, 1);
722 return;
723 }
725 # Sanity check
726 if ($mesg->count != 1) {
727 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
728 return;
729 }
731 $entry= $mesg->entry(0);
732 $dn= $entry->dn;
733 @servers= $entry->get_value("gotoLdapServer");
734 }
736 @servers= sort (@servers);
738 foreach $server (@servers){
739 $base= $server;
740 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
741 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
742 push (@ldap_uris, $server);
743 }
745 # Assemble data package
746 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
747 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
749 # Need to append GOto settings?
750 if (defined $goto_admin and defined $goto_secret){
751 $data{'goto_admin'}= $goto_admin;
752 $data{'goto_secret'}= $goto_secret;
753 }
755 # Append unit tag if needed
756 if (defined $unit_tag){
758 # Find admin base and department name
759 $mesg = $ldap->search( base => $ldap_base,
760 scope => 'sub',
761 attrs => ['dn', 'ou', 'FAIclass'],
762 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
763 #$mesg->code && die $mesg->error;
764 if($mesg->code) {
765 &main::daemon_log($mesg->error, 1);
766 return;
767 }
769 # Sanity check
770 if ($mesg->count != 1) {
771 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
772 return;
773 }
775 $entry= $mesg->entry(0);
776 $data{'admin_base'}= $entry->dn;
777 $data{'department'}= $entry->get_value("ou");
779 # Fill release if available
780 my $ou= $entry->get_value("ou");
781 if (defined $ou && $ou =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
782 $data{'release'}= $1;
783 }
785 # Append unit Tag
786 $data{'unit_tag'}= $unit_tag;
787 }
789 # Unbind
790 $mesg = $ldap->unbind;
792 # Send information
793 return send_msg("new_ldap_config", $server_address, $address, \%data);
794 }
796 sub process_detected_hardware {
797 my $msg_hash = shift;
798 my $address = $msg_hash->{source}[0];
799 my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
801 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
802 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
804 # check hit
805 my $hit_counter = keys %{$res};
806 if( not $hit_counter == 1 ) {
807 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
808 return;
809 }
811 my $macaddress = $res->{1}->{macaddress};
812 my $hostkey = $res->{1}->{hostkey};
814 if (not defined $macaddress) {
815 &main::daemon_log("ERROR: no mac address found for client $address", 1);
816 return;
817 }
818 # Build LDAP connection
819 my $ldap = Net::LDAP->new($ldap_uri);
820 if( not defined $ldap ) {
821 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
822 return;
823 }
825 # Bind to a directory with dn and password
826 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
828 # Perform search
829 $mesg = $ldap->search(
830 base => $ldap_base,
831 scope => 'sub',
832 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
833 );
835 # We need to create a base entry first (if not done from ArpHandler)
836 if($mesg->count == 0) {
837 &main::daemon_log("Need to create a new LDAP Entry for client $address", 1);
838 my $resolver=Net::DNS::Resolver->new;
839 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
840 my $dnsresult= $resolver->search($ipaddress);
841 my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
842 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
843 my $dn = "cn=$cn,ou=incoming,$ldap_base";
844 &main::daemon_log("Creating entry for $dn",6);
845 my $entry= Net::LDAP::Entry->new( $dn );
846 $entry->dn($dn);
847 $entry->add("objectClass" => "goHard");
848 $entry->add("cn" => $cn);
849 $entry->add("macAddress" => $macaddress);
850 $entry->add("gotomode" => "locked");
851 $entry->add("gotoSysStatus" => "new-system");
852 $entry->add("ipHostNumber" => $ipaddress);
853 if(my $res=$entry->update($ldap)) {
854 # Fill $mesg again
855 $mesg = $ldap->search(
856 base => $ldap_base,
857 scope => 'sub',
858 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
859 );
860 } else {
861 &main::daemon_log("ERROR: There was a problem adding the entry", 1);
862 }
864 }
866 if($mesg->count == 1) {
867 my $entry= $mesg->entry(0);
868 $entry->changetype("modify");
869 foreach my $attribute (
870 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
871 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
872 "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
873 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
874 if(defined($entry->get_value($attribute))) {
875 $entry->delete($attribute);
876 }
877 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
878 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
879 }
880 }
881 foreach my $attribute (
882 "gotoModules", "ghScsiDev", "ghIdeDev") {
883 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
884 if(defined($entry->get_value($attribute))) {
885 $entry->delete($attribute);
886 }
887 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
888 $entry->add($attribute => $array_entry);
889 }
890 }
892 }
894 if($entry->update($ldap)) {
895 &main::daemon_log("Added Hardware configuration to LDAP", 4);
896 }
898 }
899 return;
900 }
901 #=== FUNCTION ================================================================
902 # NAME: hardware_config
903 # PARAMETERS: address - string - ip address and port of a host
904 # RETURNS:
905 # DESCRIPTION:
906 #===============================================================================
907 sub hardware_config {
908 my ($address, $gotoHardwareChecksum) = @_ ;
910 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
911 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
913 # check hit
914 my $hit_counter = keys %{$res};
915 if( not $hit_counter == 1 ) {
916 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
917 }
919 my $macaddress = $res->{1}->{macaddress};
920 my $hostkey = $res->{1}->{hostkey};
922 if (not defined $macaddress) {
923 &main::daemon_log("ERROR: no mac address found for client $address", 1);
924 return;
925 }
927 # Build LDAP connection
928 my $ldap = Net::LDAP->new($ldap_uri);
929 if( not defined $ldap ) {
930 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
931 return;
932 }
934 # Bind to a directory with dn and password
935 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
937 # Perform search
938 $mesg = $ldap->search(
939 base => $ldap_base,
940 scope => 'sub',
941 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
942 );
944 if($mesg->count() == 0) {
945 &main::daemon_log("Host was not found in LDAP!", 1);
946 } else {
947 my $entry= $mesg->entry(0);
948 my $dn= $entry->dn;
949 if(defined($entry->get_value("gotoHardwareChecksum"))) {
950 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
951 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
952 if($entry->update($ldap)) {
953 &main::daemon_log("Hardware changed! Detection triggered.", 4);
954 }
955 } else {
956 # Nothing to do
957 return;
958 }
959 }
960 }
961 # need to fill it to LDAP
962 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
963 #if($entry->update($ldap)) {
964 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
965 #}
967 ## Look if there another host with this checksum to use the hardware config
968 #$mesg = $ldap->search(
969 # base => $ldap_base,
970 # scope => 'sub',
971 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
972 #);
974 #if($mesg->count>1) {
975 # my $clone_entry= $mesg->entry(0);
976 # $entry->changetype("modify");
977 # foreach my $attribute (
978 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
979 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
980 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
981 # my $value= $clone_entry->get_value($attribute);
982 # if(defined($value)) {
983 # if(defined($entry->get_value($attribute))) {
984 # $entry->delete($attribute);
985 # }
986 # &main::daemon_log("Adding attribute $attribute with value $value",1);
987 # $entry->add($attribute => $value);
988 # }
989 # }
990 # foreach my $attribute (
991 # "gotoModules", "ghScsiDev", "ghIdeDev") {
992 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
993 # if(defined($array)) {
994 # if(defined($entry->get_value($attribute))) {
995 # $entry->delete($attribute);
996 # }
997 # foreach my $array_entry (@{$array}) {
998 # $entry->add($attribute => $array_entry);
999 # }
1000 # }
1002 # }
1003 # if($entry->update($ldap)) {
1004 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1005 # }
1007 #}
1010 # Assemble data package
1011 my %data = ();
1013 # Need to append GOto settings?
1014 if (defined $goto_admin and defined $goto_secret){
1015 $data{'goto_admin'}= $goto_admin;
1016 $data{'goto_secret'}= $goto_secret;
1017 }
1019 # Unbind
1020 $mesg = $ldap->unbind;
1022 &main::daemon_log("Send detect_hardware message to $address", 4);
1024 # Send information
1025 return send_msg("detect_hardware", $server_address, $address, \%data);
1026 }
1028 sub server_matches {
1029 my $target = shift;
1030 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
1031 my $result = 0;
1033 if($server_ip eq $target_ip) {
1034 $result= 1;
1035 } elsif ($server_ip eq "0.0.0.0") {
1036 if ($target_ip eq "127.0.0.1") {
1037 $result= 1;
1038 } else {
1039 my $PROC_NET_ROUTE= ('/proc/net/route');
1041 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1042 or die "Could not open $PROC_NET_ROUTE";
1044 my @ifs = <PROC_NET_ROUTE>;
1046 close(PROC_NET_ROUTE);
1048 # Eat header line
1049 shift @ifs;
1050 chomp @ifs;
1051 foreach my $line(@ifs) {
1052 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1053 my $destination;
1054 my $mask;
1055 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1056 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1057 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1058 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1059 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1060 # destination matches route, save mac and exit
1061 $result= 1;
1062 last;
1063 }
1064 }
1065 }
1066 } else {
1067 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1068 }
1070 return $result;
1071 }
1074 #=== FUNCTION ================================================================
1075 # NAME: execute_actions
1076 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1077 # RETURNS: nothing
1078 # DESCRIPTION: invokes the script specified in msg_hash which is located under
1079 # /etc/gosad/actions
1080 #===============================================================================
1081 sub execute_actions {
1082 my ($msg_hash) = @_ ;
1083 my $configdir= '/etc/gosad/actions/';
1084 my $result;
1086 my $header = @{$msg_hash->{header}}[0];
1087 my $source = @{$msg_hash->{source}}[0];
1088 my $target = @{$msg_hash->{target}}[0];
1090 if((not defined $source)
1091 && (not defined $target)
1092 && (not defined $header)) {
1093 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1094 } else {
1095 my $parameters="";
1096 my @params = @{$msg_hash->{$header}};
1097 my $params = join(", ", @params);
1098 &main::daemon_log("execute_actions: got parameters: $params", 5);
1100 if (@params) {
1101 foreach my $param (@params) {
1102 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1103 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1104 $parameters.= " ".$param_value;
1105 }
1106 }
1108 my $cmd= $configdir.$header."$parameters";
1109 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1110 $result= "";
1111 open(PIPE, "$cmd 2>&1 |");
1112 while(<PIPE>) {
1113 $result.=$_;
1114 }
1115 close(PIPE);
1116 }
1118 # process the event result
1121 return;
1122 }
1125 1;