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 ($server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
25 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
26 my $server;
27 my $network_interface;
28 my $no_bus;
29 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret, $gosa_unit_tag);
32 my %cfg_defaults = (
33 "bus" => {
34 "activ" => [\$bus_activ, "on"],
35 "key" => [\$bus_key, ""],
36 "ip" => [\$bus_ip, ""],
37 "port" => [\$bus_port, "20080"],
38 },
39 "server" => {
40 "ip" => [\$server_ip, "0.0.0.0"],
41 "mac-address" => [\$server_mac_address, "00:00:00:00:00"],
42 "port" => [\$server_port, "20081"],
43 "ldap-uri" => [\$ldap_uri, ""],
44 "ldap-base" => [\$ldap_base, ""],
45 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
46 "ldap-admin-password" => [\$ldap_admin_password, ""],
47 "max-clients" => [\$max_clients, 100],
48 "gosa-unit-tag" => [\$gosa_unit_tag, ""],
49 },
50 "SIPackages" => {
51 "key" => [\$SIPackages_key, ""],
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 # Unit tag can be defined in config
64 if((not defined($gosa_unit_tag)) || length($gosa_unit_tag) == 0) {
65 # Read gosaUnitTag from LDAP
66 my $tmp_ldap= Net::LDAP->new($ldap_uri);
67 if(defined($tmp_ldap)) {
68 &main::daemon_log("Searching for servers gosaUnitTag with mac address $server_mac_address",6);
69 my $mesg= $tmp_ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
70 # Perform search for Unit Tag
71 $mesg = $tmp_ldap->search(
72 base => $ldap_base,
73 scope => 'sub',
74 attrs => ['gosaUnitTag'],
75 filter => "(macaddress=$server_mac_address)"
76 );
78 if ($mesg->count == 1) {
79 my $entry= $mesg->entry(0);
80 my $unit_tag= $entry->get_value("gosaUnitTag");
81 if(defined($unit_tag) && length($unit_tag) > 0) {
82 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
83 $gosa_unit_tag= $unit_tag;
84 }
85 } else {
86 # Perform another search for Unit Tag
87 my $hostname= `hostname -f`;
88 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
89 $mesg = $tmp_ldap->search(
90 base => $ldap_base,
91 scope => 'sub',
92 attrs => ['gosaUnitTag'],
93 filter => "(&(cn=$hostname)(objectClass=goServer))"
94 );
95 if ($mesg->count == 1) {
96 my $entry= $mesg->entry(0);
97 my $unit_tag= $entry->get_value("gosaUnitTag");
98 if(defined($unit_tag) && length($unit_tag) > 0) {
99 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
100 $gosa_unit_tag= $unit_tag;
101 }
102 } else {
103 # Perform another search for Unit Tag
104 $hostname= `hostname -s`;
105 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
106 $mesg = $tmp_ldap->search(
107 base => $ldap_base,
108 scope => 'sub',
109 attrs => ['gosaUnitTag'],
110 filter => "(&(cn=$hostname)(objectClass=goServer))"
111 );
112 if ($mesg->count == 1) {
113 my $entry= $mesg->entry(0);
114 my $unit_tag= $entry->get_value("gosaUnitTag");
115 if(defined($unit_tag) && length($unit_tag) > 0) {
116 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
117 $gosa_unit_tag= $unit_tag;
118 }
119 } else {
120 &main::daemon_log("Not using gosaUnitTag", 6);
121 }
122 }
123 }
124 } else {
125 &main::daemon_log("Using gosaUnitTag from config-file: $gosa_unit_tag",6);
126 }
127 $tmp_ldap->unbind;
128 }
130 # complete addresses
131 #if( $server_ip eq "0.0.0.0" ) {
132 # $server_ip = "127.0.0.1";
133 #}
134 my $server_address = "$server_ip:$server_port";
135 $main::server_address = $server_address;
136 my $bus_address = "$bus_ip:$bus_port";
137 $main::bus_address = $bus_address;
139 # create general settings for this module
140 my $xml = new XML::Simple();
142 # register at bus
143 if ($main::no_bus > 0) {
144 $bus_activ = "off"
145 }
146 if($bus_activ eq "on") {
147 ®ister_at_bus();
148 }
150 # add myself to known_server_db
151 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
152 primkey=>'hostname',
153 hostname=>$server_address,
154 status=>'myself',
155 hostkey=>$SIPackages_key,
156 timestamp=>&get_time,
157 } );
161 ### functions #################################################################
164 sub get_module_info {
165 my @info = ($server_address,
166 $SIPackages_key,
167 );
168 return \@info;
169 }
172 sub do_wake {
173 my $host = shift;
174 my $ipaddr = shift || '255.255.255.255';
175 my $port = getservbyname('discard', 'udp');
177 my ($raddr, $them, $proto);
178 my ($hwaddr, $hwaddr_re, $pkt);
180 # get the hardware address (ethernet address)
182 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
183 if ($host =~ m/^$hwaddr_re$/) {
184 $hwaddr = $host;
185 } else {
186 # $host is not a hardware address, try to resolve it
187 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
188 my $ip_addr;
189 if ($host =~ m/^$ip_re$/) {
190 $ip_addr = $host;
191 } else {
192 my $h;
193 unless ($h = gethost($host)) {
194 return undef;
195 }
196 $ip_addr = inet_ntoa($h->addr);
197 }
198 }
200 # Generate magic sequence
201 foreach (split /:/, $hwaddr) {
202 $pkt .= chr(hex($_));
203 }
204 $pkt = chr(0xFF) x 6 . $pkt x 16;
206 # Allocate socket and send packet
208 $raddr = gethostbyname($ipaddr)->addr;
209 $them = pack_sockaddr_in($port, $raddr);
210 $proto = getprotobyname('udp');
212 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
213 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
215 send(S, $pkt, 0, $them) or die "send : $!";
216 close S;
217 }
220 #=== FUNCTION ================================================================
221 # NAME: read_configfile
222 # PARAMETERS: cfg_file - string -
223 # RETURNS: nothing
224 # DESCRIPTION: read cfg_file and set variables
225 #===============================================================================
226 sub read_configfile {
227 my $cfg;
228 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
229 if( -r $main::cfg_file ) {
230 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
231 } else {
232 print STDERR "Couldn't read config file!";
233 }
234 } else {
235 $cfg = Config::IniFiles->new() ;
236 }
237 foreach my $section (keys %cfg_defaults) {
238 foreach my $param (keys %{$cfg_defaults{ $section }}) {
239 my $pinfo = $cfg_defaults{ $section }{ $param };
240 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
241 }
242 }
244 # Read non predefined sections
245 my $param;
246 if ($cfg->SectionExists('ldap')){
247 foreach $param ($cfg->Parameters('ldap')){
248 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
249 }
250 }
251 if ($cfg->SectionExists('pam_ldap')){
252 foreach $param ($cfg->Parameters('pam_ldap')){
253 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
254 }
255 }
256 if ($cfg->SectionExists('nss_ldap')){
257 foreach $param ($cfg->Parameters('nss_ldap')){
258 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
259 }
260 }
261 if ($cfg->SectionExists('goto')){
262 $goto_admin= $cfg->val('goto', 'terminal_admin');
263 $goto_secret= $cfg->val('goto', 'terminal_secret');
264 } else {
265 $goto_admin= undef;
266 $goto_secret= undef;
267 }
269 }
271 #=== FUNCTION ================================================================
272 # NAME: get_interface_for_ip
273 # PARAMETERS: ip address (i.e. 192.168.0.1)
274 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
275 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
276 #===============================================================================
277 sub get_interface_for_ip {
278 my $result;
279 my $ip= shift;
280 if ($ip && length($ip) > 0) {
281 my @ifs= &get_interfaces();
282 if($ip eq "0.0.0.0") {
283 $result = "all";
284 } else {
285 foreach (@ifs) {
286 my $if=$_;
287 if(get_ip($if) eq $ip) {
288 $result = $if;
289 }
290 }
291 }
292 }
293 return $result;
294 }
296 #=== FUNCTION ================================================================
297 # NAME: get_interfaces
298 # PARAMETERS: none
299 # RETURNS: (list of interfaces)
300 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
301 #===============================================================================
302 sub get_interfaces {
303 my @result;
304 my $PROC_NET_DEV= ('/proc/net/dev');
306 open(PROC_NET_DEV, "<$PROC_NET_DEV")
307 or die "Could not open $PROC_NET_DEV";
309 my @ifs = <PROC_NET_DEV>;
311 close(PROC_NET_DEV);
313 # Eat first two line
314 shift @ifs;
315 shift @ifs;
317 chomp @ifs;
318 foreach my $line(@ifs) {
319 my $if= (split /:/, $line)[0];
320 $if =~ s/^\s+//;
321 push @result, $if;
322 }
324 return @result;
325 }
327 #=== FUNCTION ================================================================
328 # NAME: get_mac
329 # PARAMETERS: interface name (i.e. eth0)
330 # RETURNS: (mac address)
331 # DESCRIPTION: Uses ioctl to get mac address directly from system.
332 #===============================================================================
333 sub get_mac {
334 my $ifreq= shift;
335 my $result;
336 if ($ifreq && length($ifreq) > 0) {
337 if($ifreq eq "all") {
338 $result = "00:00:00:00:00:00";
339 } else {
340 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
342 # A configured MAC Address should always override a guessed value
343 if ($server_mac_address and length($server_mac_address) > 0) {
344 $result= $server_mac_address;
345 }
347 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
348 or die "socket: $!";
350 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
351 my ($if, $mac)= unpack 'h36 H12', $ifreq;
353 if (length($mac) > 0) {
354 $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])$/;
355 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
356 $result = $mac;
357 }
358 }
359 }
360 }
361 return $result;
362 }
365 #=== FUNCTION ================================================================
366 # NAME: register_at_bus
367 # PARAMETERS: nothing
368 # RETURNS: nothing
369 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
370 #===============================================================================
371 sub register_at_bus {
373 # add bus to known_server_db
374 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
375 primkey=>'hostname',
376 hostname=>$bus_address,
377 status=>'bus',
378 hostkey=>$bus_key,
379 timestamp=>&get_time,
380 } );
381 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
382 my $msg = &create_xml_string($msg_hash);
384 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
385 return $msg;
386 }
389 #=== FUNCTION ================================================================
390 # NAME: process_incoming_msg
391 # PARAMETERS: crypted_msg - string - incoming crypted message
392 # RETURNS: nothing
393 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
394 #===============================================================================
395 sub process_incoming_msg {
396 my ($msg, $msg_hash, $remote_ip) = @_ ;
397 my $error = 0;
398 my $host_name;
399 my $host_key;
400 my @out_msg_l;
402 # process incoming msg
403 my $header = @{$msg_hash->{header}}[0];
404 my @target_l = @{$msg_hash->{target}};
406 &main::daemon_log("SIPackages: msg to process: $header", 3);
407 &main::daemon_log("$msg", 8);
409 if( 0 == length @target_l){
410 &main::daemon_log("ERROR: no target specified for msg $header", 1);
411 $error++;
412 }
414 if( 1 == length @target_l) {
415 my $target = $target_l[0];
416 if(&server_matches($target)) {
417 if ($header eq 'new_key') {
418 @out_msg_l = &new_key($msg_hash)
419 } elsif ($header eq 'here_i_am') {
420 @out_msg_l = &here_i_am($msg_hash)
421 } elsif ($header eq 'who_has') {
422 @out_msg_l = &who_has($msg_hash)
423 } elsif ($header eq 'who_has_i_do') {
424 @out_msg_l = &who_has_i_do($msg_hash)
425 } elsif ($header eq 'got_ping') {
426 @out_msg_l = &got_ping($msg_hash)
427 } elsif ($header eq 'get_load') {
428 @out_msg_l = &execute_actions($msg_hash)
429 } elsif ($header eq 'detected_hardware') {
430 @out_msg_l = &process_detected_hardware($msg_hash)
431 } elsif ($header eq 'trigger_wake') {
432 foreach (@{$msg_hash->{macAddress}}){
433 &main::daemon_log("SIPackages: trigger wake for $_", 1);
434 do_wake($_);
435 }
437 } else {
438 &main::daemon_log("ERROR: $header is an unknown core function", 1);
439 $error++;
440 }
441 }
442 else {
443 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
444 push(@out_msg_l, $msg);
445 }
446 }
448 # if( $error == 0) {
449 # if( 0 == @out_msg_l ) {
450 # push(@out_msg_l, $msg);
451 # }
452 # }
454 return \@out_msg_l;
455 }
458 #=== FUNCTION ================================================================
459 # NAME: got_ping
460 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
461 # RETURNS: nothing
462 # DESCRIPTION: process this incoming message
463 #===============================================================================
464 sub got_ping {
465 my ($msg_hash) = @_;
467 my $source = @{$msg_hash->{source}}[0];
468 my $target = @{$msg_hash->{target}}[0];
469 my $header = @{$msg_hash->{header}}[0];
471 if(exists $main::known_daemons->{$source}) {
472 &main::add_content2known_daemons(hostname=>$source, status=>$header);
473 } else {
474 &main::add_content2known_clients(hostname=>$source, status=>$header);
475 }
477 return;
478 }
481 #=== FUNCTION ================================================================
482 # NAME: new_passwd
483 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
484 # RETURNS: nothing
485 # DESCRIPTION: process this incoming message
486 #===============================================================================
487 sub new_key {
488 my ($msg_hash) = @_;
489 my @out_msg_l;
491 my $header = @{$msg_hash->{header}}[0];
492 my $source_name = @{$msg_hash->{source}}[0];
493 my $source_key = @{$msg_hash->{new_key}}[0];
494 my $query_res;
496 # check known_clients_db
497 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
498 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
499 if( 1 == keys %{$query_res} ) {
500 my $act_time = &get_time;
501 my $sql_statement= "UPDATE known_clients ".
502 "SET hostkey='$source_key', timestamp='$act_time' ".
503 "WHERE hostname='$source_name'";
504 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
505 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
506 my $out_msg = &create_xml_string($hash);
507 push(@out_msg_l, $out_msg);
508 }
510 # only do if host still not found
511 if( 0 == @out_msg_l ) {
512 # check known_server_db
513 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
514 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
515 if( 1 == keys %{$query_res} ) {
516 my $act_time = &get_time;
517 my $sql_statement= "UPDATE known_server ".
518 "SET hostkey='$source_key', timestamp='$act_time' ".
519 "WHERE hostname='$source_name'";
520 my $res = $main::known_server_db->update_dbentry( $sql_statement );
522 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
523 my $out_msg = &create_xml_string($hash);
524 push(@out_msg_l, $out_msg);
525 }
526 }
528 return @out_msg_l;
529 }
532 #=== FUNCTION ================================================================
533 # NAME: here_i_am
534 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
535 # RETURNS: nothing
536 # DESCRIPTION: process this incoming message
537 #===============================================================================
538 sub here_i_am {
539 my ($msg_hash) = @_;
540 my @out_msg_l;
541 my $out_hash;
543 my $source = @{$msg_hash->{source}}[0];
544 my $mac_address = @{$msg_hash->{mac_address}}[0];
545 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
547 # number of known clients
548 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
550 # check wether client address or mac address is already known
551 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
552 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
554 if ( 1 == keys %{$db_res} ) {
555 &main::daemon_log("WARNING: $source is already known as a client", 1);
556 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
557 $nu_clients --;
558 }
560 # number of actual activ clients
561 my $act_nu_clients = $nu_clients;
563 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
564 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
566 if($max_clients <= $act_nu_clients) {
567 my $out_hash = &create_xml_hash("denied", $server_address, $source);
568 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
569 my $passwd = @{$msg_hash->{new_passwd}}[0];
570 &send_msg_hash2address($out_hash, $source, $passwd);
571 return;
572 }
574 # new client accepted
575 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
577 # create entry in known_clients
578 my $events = @{$msg_hash->{events}}[0];
581 # add entry to known_clients_db
582 my $act_timestamp = &get_time;
583 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
584 primkey=>'hostname',
585 hostname=>$source,
586 events=>$events,
587 macaddress=>$mac_address,
588 status=>'registered',
589 hostkey=>$new_passwd,
590 timestamp=>$act_timestamp,
591 } );
593 if ($res != 0) {
594 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
595 return;
596 }
598 # return acknowledgement to client
599 $out_hash = &create_xml_hash("registered", $server_address, $source);
600 my $register_out = &create_xml_string($out_hash);
601 push(@out_msg_l, $register_out);
603 # notify registered client to bus
604 if( $bus_activ eq "on") {
605 # fetch actual bus key
606 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
607 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
608 my $hostkey = $query_res->{1}->{'hostkey'};
610 # send update msg to bus
611 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
612 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
613 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
614 my $new_client_out = &create_xml_string($out_hash);
615 push(@out_msg_l, $new_client_out);
616 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
617 }
619 # give the new client his ldap config
620 my $new_ldap_config_out = &new_ldap_config($source);
621 if( $new_ldap_config_out ) {
622 push(@out_msg_l, $new_ldap_config_out);
623 }
625 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
626 if( $hardware_config_out ) {
627 push(@out_msg_l, $hardware_config_out);
628 }
630 return @out_msg_l;
631 }
634 #=== FUNCTION ================================================================
635 # NAME: who_has
636 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
637 # RETURNS: nothing
638 # DESCRIPTION: process this incoming message
639 #===============================================================================
640 sub who_has {
641 my ($msg_hash) = @_ ;
642 my @out_msg_l;
644 # what is your search pattern
645 my $search_pattern = @{$msg_hash->{who_has}}[0];
646 my $search_element = @{$msg_hash->{$search_pattern}}[0];
647 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
649 # scanning known_clients for search_pattern
650 my @host_addresses = keys %$main::known_clients;
651 my $known_clients_entries = length @host_addresses;
652 my $host_address;
653 foreach my $host (@host_addresses) {
654 my $client_element = $main::known_clients->{$host}->{$search_pattern};
655 if ($search_element eq $client_element) {
656 $host_address = $host;
657 last;
658 }
659 }
661 # search was successful
662 if (defined $host_address) {
663 my $source = @{$msg_hash->{source}}[0];
664 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
665 &add_content2xml_hash($out_hash, "mac_address", $search_element);
666 my $out_msg = &create_xml_string($out_hash);
667 push(@out_msg_l, $out_msg);
668 }
669 return @out_msg_l;
670 }
673 sub who_has_i_do {
674 my ($msg_hash) = @_ ;
675 my $header = @{$msg_hash->{header}}[0];
676 my $source = @{$msg_hash->{source}}[0];
677 my $search_param = @{$msg_hash->{$header}}[0];
678 my $search_value = @{$msg_hash->{$search_param}}[0];
679 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
680 }
682 #=== FUNCTION ================================================================
683 # NAME: new_ldap_config
684 # PARAMETERS: address - string - ip address and port of a host
685 # RETURNS: nothing
686 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
687 #===============================================================================
688 sub new_ldap_config {
689 my ($address) = @_ ;
691 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
692 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
694 # check hit
695 my $hit_counter = keys %{$res};
696 if( not $hit_counter == 1 ) {
697 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
698 }
700 my $macaddress = $res->{1}->{macaddress};
701 my $hostkey = $res->{1}->{hostkey};
703 if (not defined $macaddress) {
704 &main::daemon_log("ERROR: no mac address found for client $address", 1);
705 return;
706 }
708 # Build LDAP connection
709 my $ldap = Net::LDAP->new($ldap_uri);
710 if( not defined $ldap ) {
711 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
712 return;
713 }
716 # Bind to a directory with dn and password
717 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
719 # Perform search
720 $mesg = $ldap->search( base => $ldap_base,
721 scope => 'sub',
722 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
723 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
724 #$mesg->code && die $mesg->error;
725 if($mesg->code) {
726 &main::daemon_log($mesg->error, 1);
727 return;
728 }
730 # Sanity check
731 if ($mesg->count != 1) {
732 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
733 &main::daemon_log("\tbase: $ldap_base", 1);
734 &main::daemon_log("\tscope: sub", 1);
735 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
736 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
737 return;
738 }
740 my $entry= $mesg->entry(0);
741 my $dn= $entry->dn;
742 my @servers= $entry->get_value("gotoLdapServer");
743 my $unit_tag= $entry->get_value("gosaUnitTag");
744 my @ldap_uris;
745 my $server;
746 my $base;
748 # Do we need to look at an object class?
749 if (length(@servers) < 1){
750 $mesg = $ldap->search( base => $ldap_base,
751 scope => 'sub',
752 attrs => ['dn', 'gotoLdapServer'],
753 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
754 #$mesg->code && die $mesg->error;
755 if($mesg->code) {
756 &main::daemon_log($mesg->error, 1);
757 return;
758 }
760 # Sanity check
761 if ($mesg->count != 1) {
762 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
763 return;
764 }
766 $entry= $mesg->entry(0);
767 $dn= $entry->dn;
768 @servers= $entry->get_value("gotoLdapServer");
769 }
771 @servers= sort (@servers);
773 foreach $server (@servers){
774 # Conversation for backward compatibility
775 if ($server !=~ /^ldap[^:]+:\/\// ) {
776 if ($server =~ /^([^:]+):(.*)$/ ) {
777 $server= "1:dummy:ldap://$1/$2";
778 }
779 }
781 $base= $server;
782 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
783 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
784 push (@ldap_uris, $server);
785 }
787 # Assemble data package
788 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
789 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
791 # Need to append GOto settings?
792 if (defined $goto_admin and defined $goto_secret){
793 $data{'goto_admin'}= $goto_admin;
794 $data{'goto_secret'}= $goto_secret;
795 }
797 # Append unit tag if needed
798 if (defined $unit_tag){
800 # Find admin base and department name
801 $mesg = $ldap->search( base => $ldap_base,
802 scope => 'sub',
803 attrs => ['dn', 'ou', 'FAIclass'],
804 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
805 #$mesg->code && die $mesg->error;
806 if($mesg->code) {
807 &main::daemon_log($mesg->error, 1);
808 return;
809 }
811 # Sanity check
812 if ($mesg->count != 1) {
813 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
814 return;
815 }
817 $entry= $mesg->entry(0);
818 $data{'admin_base'}= $entry->dn;
819 $data{'department'}= $entry->get_value("ou");
821 # Append unit Tag
822 $data{'unit_tag'}= $unit_tag;
823 }
825 # Fill release if available
826 my $FAIclass= $entry->get_value("FAIclass");
827 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
828 $data{'release'}= $1;
829 }
832 # Unbind
833 $mesg = $ldap->unbind;
835 # Send information
836 return send_msg("new_ldap_config", $server_address, $address, \%data);
837 }
839 sub process_detected_hardware {
840 my $msg_hash = shift;
841 my $address = $msg_hash->{source}[0];
842 my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
844 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
845 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
847 # check hit
848 my $hit_counter = keys %{$res};
849 if( not $hit_counter == 1 ) {
850 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
851 return;
852 }
854 my $macaddress = $res->{1}->{macaddress};
855 my $hostkey = $res->{1}->{hostkey};
857 if (not defined $macaddress) {
858 &main::daemon_log("ERROR: no mac address found for client $address", 1);
859 return;
860 }
861 # Build LDAP connection
862 my $ldap = Net::LDAP->new($ldap_uri);
863 if( not defined $ldap ) {
864 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
865 return;
866 }
868 # Bind to a directory with dn and password
869 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
871 # Perform search
872 $mesg = $ldap->search(
873 base => $ldap_base,
874 scope => 'sub',
875 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
876 );
878 # We need to create a base entry first (if not done from ArpHandler)
879 if($mesg->count == 0) {
880 &main::daemon_log("Need to create a new LDAP Entry for client $address", 1);
881 my $resolver=Net::DNS::Resolver->new;
882 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
883 my $dnsresult= $resolver->search($ipaddress);
884 my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
885 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
886 my $dn = "cn=$cn,ou=incoming,$ldap_base";
887 &main::daemon_log("Creating entry for $dn",6);
888 my $entry= Net::LDAP::Entry->new( $dn );
889 $entry->dn($dn);
890 $entry->add("objectClass" => "goHard");
891 $entry->add("cn" => $cn);
892 $entry->add("macAddress" => $macaddress);
893 $entry->add("gotomode" => "locked");
894 $entry->add("gotoSysStatus" => "new-system");
895 $entry->add("ipHostNumber" => $ipaddress);
896 if(defined($gosa_unit_tag) && length($gosa_unit_tag) > 0) {
897 $entry->add("objectClass" => "gosaAdministrativeUnit");
898 $entry->add("gosaUnitTag" => $gosa_unit_tag);
899 }
900 if(my $res=$entry->update($ldap)) {
901 # Fill $mesg again
902 $mesg = $ldap->search(
903 base => $ldap_base,
904 scope => 'sub',
905 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
906 );
907 } else {
908 &main::daemon_log("ERROR: There was a problem adding the entry", 1);
909 }
911 }
913 if($mesg->count == 1) {
914 my $entry= $mesg->entry(0);
915 $entry->changetype("modify");
916 foreach my $attribute (
917 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
918 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
919 "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
920 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
921 if(defined($entry->get_value($attribute))) {
922 $entry->delete($attribute);
923 }
924 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
925 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
926 }
927 }
928 foreach my $attribute (
929 "gotoModules", "ghScsiDev", "ghIdeDev") {
930 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
931 if(defined($entry->get_value($attribute))) {
932 $entry->delete($attribute);
933 }
934 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
935 $entry->add($attribute => $array_entry);
936 }
937 }
939 }
941 if($entry->update($ldap)) {
942 &main::daemon_log("Added Hardware configuration to LDAP", 4);
943 }
945 }
946 return;
947 }
948 #=== FUNCTION ================================================================
949 # NAME: hardware_config
950 # PARAMETERS: address - string - ip address and port of a host
951 # RETURNS:
952 # DESCRIPTION:
953 #===============================================================================
954 sub hardware_config {
955 my ($address, $gotoHardwareChecksum) = @_ ;
957 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
958 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
960 # check hit
961 my $hit_counter = keys %{$res};
962 if( not $hit_counter == 1 ) {
963 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
964 }
966 my $macaddress = $res->{1}->{macaddress};
967 my $hostkey = $res->{1}->{hostkey};
969 if (not defined $macaddress) {
970 &main::daemon_log("ERROR: no mac address found for client $address", 1);
971 return;
972 }
974 # Build LDAP connection
975 my $ldap = Net::LDAP->new($ldap_uri);
976 if( not defined $ldap ) {
977 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
978 return;
979 }
981 # Bind to a directory with dn and password
982 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
984 # Perform search
985 $mesg = $ldap->search(
986 base => $ldap_base,
987 scope => 'sub',
988 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
989 );
991 if($mesg->count() == 0) {
992 &main::daemon_log("Host was not found in LDAP!", 1);
993 } else {
994 my $entry= $mesg->entry(0);
995 my $dn= $entry->dn;
996 if(defined($entry->get_value("gotoHardwareChecksum"))) {
997 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
998 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
999 if($entry->update($ldap)) {
1000 &main::daemon_log("Hardware changed! Detection triggered.", 4);
1001 }
1002 } else {
1003 # Nothing to do
1004 return;
1005 }
1006 }
1007 }
1008 # need to fill it to LDAP
1009 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
1010 #if($entry->update($ldap)) {
1011 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
1012 #}
1014 ## Look if there another host with this checksum to use the hardware config
1015 #$mesg = $ldap->search(
1016 # base => $ldap_base,
1017 # scope => 'sub',
1018 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
1019 #);
1021 #if($mesg->count>1) {
1022 # my $clone_entry= $mesg->entry(0);
1023 # $entry->changetype("modify");
1024 # foreach my $attribute (
1025 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
1026 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
1027 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
1028 # my $value= $clone_entry->get_value($attribute);
1029 # if(defined($value)) {
1030 # if(defined($entry->get_value($attribute))) {
1031 # $entry->delete($attribute);
1032 # }
1033 # &main::daemon_log("Adding attribute $attribute with value $value",1);
1034 # $entry->add($attribute => $value);
1035 # }
1036 # }
1037 # foreach my $attribute (
1038 # "gotoModules", "ghScsiDev", "ghIdeDev") {
1039 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
1040 # if(defined($array)) {
1041 # if(defined($entry->get_value($attribute))) {
1042 # $entry->delete($attribute);
1043 # }
1044 # foreach my $array_entry (@{$array}) {
1045 # $entry->add($attribute => $array_entry);
1046 # }
1047 # }
1049 # }
1050 # if($entry->update($ldap)) {
1051 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1052 # }
1054 #}
1057 # Assemble data package
1058 my %data = ();
1060 # Need to append GOto settings?
1061 if (defined $goto_admin and defined $goto_secret){
1062 $data{'goto_admin'}= $goto_admin;
1063 $data{'goto_secret'}= $goto_secret;
1064 }
1066 # Unbind
1067 $mesg = $ldap->unbind;
1069 &main::daemon_log("Send detect_hardware message to $address", 4);
1071 # Send information
1072 return send_msg("detect_hardware", $server_address, $address, \%data);
1073 }
1075 sub server_matches {
1076 my $target = shift;
1077 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
1078 my $result = 0;
1080 if($server_ip eq $target_ip) {
1081 $result= 1;
1082 } elsif ($server_ip eq "0.0.0.0") {
1083 if ($target_ip eq "127.0.0.1") {
1084 $result= 1;
1085 } else {
1086 my $PROC_NET_ROUTE= ('/proc/net/route');
1088 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1089 or die "Could not open $PROC_NET_ROUTE";
1091 my @ifs = <PROC_NET_ROUTE>;
1093 close(PROC_NET_ROUTE);
1095 # Eat header line
1096 shift @ifs;
1097 chomp @ifs;
1098 foreach my $line(@ifs) {
1099 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1100 my $destination;
1101 my $mask;
1102 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1103 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1104 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1105 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1106 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1107 # destination matches route, save mac and exit
1108 $result= 1;
1109 last;
1110 }
1111 }
1112 }
1113 } else {
1114 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1115 }
1117 return $result;
1118 }
1121 #=== FUNCTION ================================================================
1122 # NAME: execute_actions
1123 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1124 # RETURNS: nothing
1125 # DESCRIPTION: invokes the script specified in msg_hash which is located under
1126 # /etc/gosad/actions
1127 #===============================================================================
1128 sub execute_actions {
1129 my ($msg_hash) = @_ ;
1130 my $configdir= '/etc/gosad/actions/';
1131 my $result;
1133 my $header = @{$msg_hash->{header}}[0];
1134 my $source = @{$msg_hash->{source}}[0];
1135 my $target = @{$msg_hash->{target}}[0];
1137 if((not defined $source)
1138 && (not defined $target)
1139 && (not defined $header)) {
1140 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1141 } else {
1142 my $parameters="";
1143 my @params = @{$msg_hash->{$header}};
1144 my $params = join(", ", @params);
1145 &main::daemon_log("execute_actions: got parameters: $params", 5);
1147 if (@params) {
1148 foreach my $param (@params) {
1149 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1150 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1151 $parameters.= " ".$param_value;
1152 }
1153 }
1155 my $cmd= $configdir.$header."$parameters";
1156 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1157 $result= "";
1158 open(PIPE, "$cmd 2>&1 |");
1159 while(<PIPE>) {
1160 $result.=$_;
1161 }
1162 close(PIPE);
1163 }
1165 # process the event result
1168 return;
1169 }
1172 1;