099ae2121b449bfd32c8bd2d0266577e74064ca6
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 utf8;
21 my $event_dir = "/usr/lib/gosa-si/server/events";
22 use lib "/usr/lib/gosa-si/server/events";
24 BEGIN{}
25 END {}
27 my ($server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
28 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
29 my $server;
30 my $event_hash;
31 my $network_interface;
32 my $no_bus;
33 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
34 my $mesg;
36 my %cfg_defaults = (
37 "bus" => {
38 "activ" => [\$bus_activ, "on"],
39 "key" => [\$bus_key, ""],
40 "ip" => [\$bus_ip, ""],
41 "port" => [\$bus_port, "20080"],
42 },
43 "server" => {
44 "ip" => [\$server_ip, "0.0.0.0"],
45 "mac-address" => [\$server_mac_address, "00:00:00:00:00"],
46 "port" => [\$server_port, "20081"],
47 "ldap-uri" => [\$ldap_uri, ""],
48 "ldap-base" => [\$ldap_base, ""],
49 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
50 "ldap-admin-password" => [\$ldap_admin_password, ""],
51 "max-clients" => [\$max_clients, 100],
52 },
53 "SIPackages" => {
54 "key" => [\$SIPackages_key, ""],
55 },
56 );
58 ### START #####################################################################
60 # read configfile and import variables
61 &read_configfile();
64 # if server_ip is not an ip address but a name
65 if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
66 $network_interface= &get_interface_for_ip($server_ip);
67 $server_mac_address= &get_mac($network_interface);
69 &import_events();
71 # Unit tag can be defined in config
72 if((not defined($main::gosa_unit_tag)) || length($main::gosa_unit_tag) == 0) {
73 # Read gosaUnitTag from LDAP
74 &main::refresh_ldap_handle();
75 if( defined($main::ldap_handle) ) {
76 &main::daemon_log("INFO: Searching for servers gosaUnitTag with mac address $server_mac_address",5);
77 # Perform search for Unit Tag
78 $mesg = $main::ldap_handle->search(
79 base => $ldap_base,
80 scope => 'sub',
81 attrs => ['gosaUnitTag'],
82 filter => "(macaddress=$server_mac_address)"
83 );
85 if ($mesg->count == 1) {
86 my $entry= $mesg->entry(0);
87 my $unit_tag= $entry->get_value("gosaUnitTag");
88 if(defined($unit_tag) && length($unit_tag) > 0) {
89 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
90 $main::gosa_unit_tag= $unit_tag;
91 }
92 } else {
93 # Perform another search for Unit Tag
94 my $hostname= `hostname -f`;
95 chomp($hostname);
96 &main::daemon_log("INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
97 $mesg = $main::ldap_handle->search(
98 base => $ldap_base,
99 scope => 'sub',
100 attrs => ['gosaUnitTag'],
101 filter => "(&(cn=$hostname)(objectClass=goServer))"
102 );
103 if ($mesg->count == 1) {
104 my $entry= $mesg->entry(0);
105 my $unit_tag= $entry->get_value("gosaUnitTag");
106 if(defined($unit_tag) && length($unit_tag) > 0) {
107 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
108 $main::gosa_unit_tag= $unit_tag;
109 }
110 } else {
111 # Perform another search for Unit Tag
112 $hostname= `hostname -s`;
113 chomp($hostname);
114 &main::daemon_log("INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
115 $mesg = $main::ldap_handle->search(
116 base => $ldap_base,
117 scope => 'sub',
118 attrs => ['gosaUnitTag'],
119 filter => "(&(cn=$hostname)(objectClass=goServer))"
120 );
121 if ($mesg->count == 1) {
122 my $entry= $mesg->entry(0);
123 my $unit_tag= $entry->get_value("gosaUnitTag");
124 if(defined($unit_tag) && length($unit_tag) > 0) {
125 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
126 $main::gosa_unit_tag= $unit_tag;
127 }
128 } else {
129 &main::daemon_log("WARNING: No gosaUnitTag detected. Not using gosaUnitTag", 3);
130 }
131 }
132 }
133 } else {
134 &main::daemon_log("INFO: Using gosaUnitTag from config-file: $main::gosa_unit_tag",5);
135 }
136 }
139 my $server_address = "$server_ip:$server_port";
140 $main::server_address = $server_address;
143 if( inet_aton($bus_ip) ){ $bus_ip = inet_ntoa(inet_aton($bus_ip)); }
144 ######################################################
145 # to change
146 if( $bus_ip eq "127.0.1.1" ) { $bus_ip = "127.0.0.1" }
147 ######################################################
148 my $bus_address = "$bus_ip:$bus_port";
149 $main::bus_address = $bus_address;
151 # create general settings for this module
152 my $xml = new XML::Simple();
154 # register at bus
155 if ($main::no_bus > 0) {
156 $bus_activ = "off"
157 }
158 if($bus_activ eq "on") {
159 ®ister_at_bus();
160 }
162 # add myself to known_server_db
163 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
164 primkey=>['hostname'],
165 hostname=>$server_address,
166 status=>'myself',
167 hostkey=>$SIPackages_key,
168 timestamp=>&get_time,
169 } );
173 ### functions #################################################################
176 sub get_module_info {
177 my @info = ($server_address,
178 $SIPackages_key,
179 );
180 return \@info;
181 }
184 #=== FUNCTION ================================================================
185 # NAME: read_configfile
186 # PARAMETERS: cfg_file - string -
187 # RETURNS: nothing
188 # DESCRIPTION: read cfg_file and set variables
189 #===============================================================================
190 sub read_configfile {
191 my $cfg;
192 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
193 if( -r $main::cfg_file ) {
194 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
195 } else {
196 print STDERR "Couldn't read config file!";
197 }
198 } else {
199 $cfg = Config::IniFiles->new() ;
200 }
201 foreach my $section (keys %cfg_defaults) {
202 foreach my $param (keys %{$cfg_defaults{ $section }}) {
203 my $pinfo = $cfg_defaults{ $section }{ $param };
204 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
205 }
206 }
208 # Read non predefined sections
209 my $param;
210 if ($cfg->SectionExists('ldap')){
211 foreach $param ($cfg->Parameters('ldap')){
212 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
213 }
214 }
215 if ($cfg->SectionExists('pam_ldap')){
216 foreach $param ($cfg->Parameters('pam_ldap')){
217 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
218 }
219 }
220 if ($cfg->SectionExists('nss_ldap')){
221 foreach $param ($cfg->Parameters('nss_ldap')){
222 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
223 }
224 }
225 if ($cfg->SectionExists('goto')){
226 $goto_admin= $cfg->val('goto', 'terminal_admin');
227 $goto_secret= $cfg->val('goto', 'terminal_secret');
228 } else {
229 $goto_admin= undef;
230 $goto_secret= undef;
231 }
233 }
235 #=== FUNCTION ================================================================
236 # NAME: get_interface_for_ip
237 # PARAMETERS: ip address (i.e. 192.168.0.1)
238 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
239 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
240 #===============================================================================
241 sub get_interface_for_ip {
242 my $result;
243 my $ip= shift;
244 if ($ip && length($ip) > 0) {
245 my @ifs= &get_interfaces();
246 if($ip eq "0.0.0.0") {
247 $result = "all";
248 } else {
249 foreach (@ifs) {
250 my $if=$_;
251 if(&main::get_ip($if) eq $ip) {
252 $result = $if;
253 }
254 }
255 }
256 }
257 return $result;
258 }
260 #=== FUNCTION ================================================================
261 # NAME: get_interfaces
262 # PARAMETERS: none
263 # RETURNS: (list of interfaces)
264 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
265 #===============================================================================
266 sub get_interfaces {
267 my @result;
268 my $PROC_NET_DEV= ('/proc/net/dev');
270 open(PROC_NET_DEV, "<$PROC_NET_DEV")
271 or die "Could not open $PROC_NET_DEV";
273 my @ifs = <PROC_NET_DEV>;
275 close(PROC_NET_DEV);
277 # Eat first two line
278 shift @ifs;
279 shift @ifs;
281 chomp @ifs;
282 foreach my $line(@ifs) {
283 my $if= (split /:/, $line)[0];
284 $if =~ s/^\s+//;
285 push @result, $if;
286 }
288 return @result;
289 }
291 #=== FUNCTION ================================================================
292 # NAME: get_mac
293 # PARAMETERS: interface name (i.e. eth0)
294 # RETURNS: (mac address)
295 # DESCRIPTION: Uses ioctl to get mac address directly from system.
296 #===============================================================================
297 sub get_mac {
298 my $ifreq= shift;
299 my $result;
300 if ($ifreq && length($ifreq) > 0) {
301 if($ifreq eq "all") {
302 $result = "00:00:00:00:00:00";
303 } else {
304 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
306 # A configured MAC Address should always override a guessed value
307 if ($server_mac_address and length($server_mac_address) > 0) {
308 $result= $server_mac_address;
309 }
311 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
312 or die "socket: $!";
314 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
315 my ($if, $mac)= unpack 'h36 H12', $ifreq;
317 if (length($mac) > 0) {
318 $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])$/;
319 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
320 $result = $mac;
321 }
322 }
323 }
324 }
325 return $result;
326 }
329 #=== FUNCTION ================================================================
330 # NAME: register_at_bus
331 # PARAMETERS: nothing
332 # RETURNS: nothing
333 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
334 #===============================================================================
335 sub register_at_bus {
337 # add bus to known_server_db
338 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
339 primkey=>['hostname'],
340 hostname=>$bus_address,
341 status=>'bus',
342 hostkey=>$bus_key,
343 timestamp=>&get_time,
344 } );
345 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
346 my $msg = &create_xml_string($msg_hash);
348 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
349 return $msg;
350 }
353 sub import_events {
354 if (not -e $event_dir) {
355 &main::daemon_log("ERROR: cannot find directory or directory is not readable: $event_dir", 1);
356 }
357 opendir (DIR, $event_dir) or die "ERROR while loading gosa-si-events from directory $event_dir : $!\n";
359 while (defined (my $event = readdir (DIR))) {
360 if( $event eq "." || $event eq ".." ) { next; }
361 if( $event eq "gosaTriggered.pm" ) { next; } # only GOsa specific events
363 eval{ require $event; };
364 if( $@ ) {
365 &main::daemon_log("import of event module '$event' failed", 1);
366 &main::daemon_log("$@", 8);
367 next;
368 }
370 $event =~ /(\S*?).pm$/;
371 my $event_module = $1;
372 my $events_l = eval( $1."::get_events()") ;
373 foreach my $event_name (@{$events_l}) {
374 $event_hash->{$event_name} = $event_module;
375 }
376 my $events_string = join( ", ", @{$events_l});
377 &main::daemon_log("INFO: SIPackages imported events $events_string", 5);
378 }
379 }
382 #=== FUNCTION ================================================================
383 # NAME: process_incoming_msg
384 # PARAMETERS: crypted_msg - string - incoming crypted message
385 # RETURNS: nothing
386 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
387 #===============================================================================
388 sub process_incoming_msg {
389 my ($msg, $msg_hash, $session_id) = @_ ;
390 my $error = 0;
391 my $host_name;
392 my $host_key;
393 my @out_msg_l = ();
395 # process incoming msg
396 my $header = @{$msg_hash->{header}}[0];
397 my @target_l = @{$msg_hash->{target}};
399 # skip PREFIX
400 $header =~ s/^CLMSG_//;
402 &main::daemon_log("DEBUG: SIPackages: msg to process: $header", 7);
404 if( 0 == length @target_l){
405 &main::daemon_log("ERROR: no target specified for msg $header", 1);
406 $error++;
407 }
409 if( 1 == length @target_l) {
410 my $target = $target_l[0];
411 if(&server_matches($target)) {
414 if ($header eq 'new_key') {
415 @out_msg_l = &new_key($msg_hash)
416 } elsif ($header eq 'here_i_am') {
417 @out_msg_l = &here_i_am($msg_hash)
418 } else {
419 if( exists $event_hash->{$header} ) {
420 # a event exists with the header as name
421 &main::daemon_log("INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
422 no strict 'refs';
423 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
424 }
425 }
427 # if delivery not possible raise error and return
428 if( not @out_msg_l ) {
429 &main::daemon_log("WARNING: SIPackages got no answer from event handler '$header'", 3);
430 } elsif( 0 == @out_msg_l) {
431 &main::daemon_log("ERROR: SIPackages: no event handler or core function defined for '$header'", 1);
432 }
433 }
434 else {
435 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
436 push(@out_msg_l, $msg);
437 }
438 }
440 return \@out_msg_l;
441 }
444 #=== FUNCTION ================================================================
445 # NAME: new_passwd
446 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
447 # RETURNS: nothing
448 # DESCRIPTION: process this incoming message
449 #===============================================================================
450 sub new_key {
451 my ($msg_hash) = @_;
452 my @out_msg_l;
454 my $header = @{$msg_hash->{header}}[0];
455 my $source_name = @{$msg_hash->{source}}[0];
456 my $source_key = @{$msg_hash->{new_key}}[0];
457 my $query_res;
459 # check known_clients_db
460 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
461 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
462 if( 1 == keys %{$query_res} ) {
463 my $act_time = &get_time;
464 my $sql_statement= "UPDATE known_clients ".
465 "SET hostkey='$source_key', timestamp='$act_time' ".
466 "WHERE hostname='$source_name'";
467 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
468 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
469 my $out_msg = &create_xml_string($hash);
470 push(@out_msg_l, $out_msg);
471 }
473 # only do if host still not found
474 if( 0 == @out_msg_l ) {
475 # check known_server_db
476 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
477 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
478 if( 1 == keys %{$query_res} ) {
479 my $act_time = &get_time;
480 my $sql_statement= "UPDATE known_server ".
481 "SET hostkey='$source_key', timestamp='$act_time' ".
482 "WHERE hostname='$source_name'";
483 my $res = $main::known_server_db->update_dbentry( $sql_statement );
485 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
486 my $out_msg = &create_xml_string($hash);
487 push(@out_msg_l, $out_msg);
488 }
489 }
491 return @out_msg_l;
492 }
495 #=== FUNCTION ================================================================
496 # NAME: here_i_am
497 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
498 # RETURNS: nothing
499 # DESCRIPTION: process this incoming message
500 #===============================================================================
501 sub here_i_am {
502 my ($msg_hash) = @_;
503 my @out_msg_l;
504 my $out_hash;
506 my $source = @{$msg_hash->{source}}[0];
507 my $mac_address = @{$msg_hash->{mac_address}}[0];
508 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
510 # number of known clients
511 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
513 # check wether client address or mac address is already known
514 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
515 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
517 if ( 1 == keys %{$db_res} ) {
518 &main::daemon_log("WARNING: $source is already known as a client", 1);
519 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
520 $nu_clients --;
521 }
523 # number of actual activ clients
524 my $act_nu_clients = $nu_clients;
526 &main::daemon_log("INFO: number of actual activ clients: $act_nu_clients", 5);
527 &main::daemon_log("INFO: number of maximal allowed clients: $max_clients", 5);
529 if($max_clients <= $act_nu_clients) {
530 my $out_hash = &create_xml_hash("denied", $server_address, $source);
531 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
532 my $passwd = @{$msg_hash->{new_passwd}}[0];
533 &send_msg_hash2address($out_hash, $source, $passwd);
534 return;
535 }
537 # new client accepted
538 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
540 # create entry in known_clients
541 my $events = @{$msg_hash->{events}}[0];
544 # add entry to known_clients_db
545 my $act_timestamp = &get_time;
546 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
547 primkey=>['hostname'],
548 hostname=>$source,
549 events=>$events,
550 macaddress=>$mac_address,
551 status=>'registered',
552 hostkey=>$new_passwd,
553 timestamp=>$act_timestamp,
554 } );
556 if ($res != 0) {
557 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
558 return;
559 }
561 # return acknowledgement to client
562 $out_hash = &create_xml_hash("registered", $server_address, $source);
564 # notify registered client to bus
565 if( $bus_activ eq "on") {
566 # fetch actual bus key
567 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
568 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
569 my $hostkey = $query_res->{1}->{'hostkey'};
571 # send update msg to bus
572 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
573 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
574 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
575 my $new_client_out = &create_xml_string($out_hash);
576 push(@out_msg_l, $new_client_out);
577 &main::daemon_log("INFO: send bus msg that client '$source' has registered at server '$server_address'", 5);
578 }
580 # give the new client his ldap config
581 # Workaround: Send within the registration response, if the client will get an ldap config later
582 my $new_ldap_config_out = &new_ldap_config($source);
583 if( $new_ldap_config_out ) {
584 &add_content2xml_hash($out_hash, "ldap_available", "true");
585 }
586 my $register_out = &create_xml_string($out_hash);
587 push(@out_msg_l, $register_out);
589 # Really send the ldap config
590 if( $new_ldap_config_out ) {
591 push(@out_msg_l, $new_ldap_config_out);
592 }
594 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
595 if( $hardware_config_out ) {
596 push(@out_msg_l, $hardware_config_out);
597 }
599 return @out_msg_l;
600 }
603 #=== FUNCTION ================================================================
604 # NAME: who_has
605 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
606 # RETURNS: nothing
607 # DESCRIPTION: process this incoming message
608 #===============================================================================
609 sub who_has {
610 my ($msg_hash) = @_ ;
611 my @out_msg_l;
613 # what is your search pattern
614 my $search_pattern = @{$msg_hash->{who_has}}[0];
615 my $search_element = @{$msg_hash->{$search_pattern}}[0];
616 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
618 # scanning known_clients for search_pattern
619 my @host_addresses = keys %$main::known_clients;
620 my $known_clients_entries = length @host_addresses;
621 my $host_address;
622 foreach my $host (@host_addresses) {
623 my $client_element = $main::known_clients->{$host}->{$search_pattern};
624 if ($search_element eq $client_element) {
625 $host_address = $host;
626 last;
627 }
628 }
630 # search was successful
631 if (defined $host_address) {
632 my $source = @{$msg_hash->{source}}[0];
633 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
634 &add_content2xml_hash($out_hash, "mac_address", $search_element);
635 my $out_msg = &create_xml_string($out_hash);
636 push(@out_msg_l, $out_msg);
637 }
638 return @out_msg_l;
639 }
642 sub who_has_i_do {
643 my ($msg_hash) = @_ ;
644 my $header = @{$msg_hash->{header}}[0];
645 my $source = @{$msg_hash->{source}}[0];
646 my $search_param = @{$msg_hash->{$header}}[0];
647 my $search_value = @{$msg_hash->{$search_param}}[0];
648 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
649 }
652 #=== FUNCTION ================================================================
653 # NAME: new_ldap_config
654 # PARAMETERS: address - string - ip address and port of a host
655 # RETURNS: gosa-si conform message
656 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
657 #===============================================================================
658 sub new_ldap_config {
659 my ($address) = @_ ;
661 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress='$address'";
662 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
664 # check hit
665 my $hit_counter = keys %{$res};
666 if( not $hit_counter == 1 ) {
667 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
668 }
670 $address = $res->{1}->{hostname};
671 my $macaddress = $res->{1}->{macaddress};
672 my $hostkey = $res->{1}->{hostkey};
674 if (not defined $macaddress) {
675 &main::daemon_log("ERROR: no mac address found for client $address", 1);
676 return;
677 }
679 # Build LDAP connection
680 &main::refresh_ldap_handle();
681 if( not defined $main::ldap_handle ) {
682 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
683 return;
684 }
686 # Perform search
687 $mesg = $main::ldap_handle->search( base => $ldap_base,
688 scope => 'sub',
689 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
690 filter => "(&(objectClass=GOhard)(macaddress=$macaddress)(gotoLdapServer=*))");
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 with mac address $macaddress not found/unique/active - not sending ldap config", 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)(gotoLdapServer=*))", 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;
714 my $release;
716 # Fill release if available
717 my $FAIclass= $entry->get_value("FAIclass");
718 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
719 $release= $1;
720 }
722 # Do we need to look at an object class?
723 if (length(@servers) < 1){
724 $mesg = $main::ldap_handle->search( base => $ldap_base,
725 scope => 'sub',
726 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
727 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
728 #$mesg->code && die $mesg->error;
729 if($mesg->code) {
730 &main::daemon_log($mesg->error, 1);
731 return;
732 }
734 # Sanity check
735 if ($mesg->count != 1) {
736 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
737 return;
738 }
740 $entry= $mesg->entry(0);
741 $dn= $entry->dn;
742 @servers= $entry->get_value("gotoLdapServer");
744 if (not defined $release){
745 $FAIclass= $entry->get_value("FAIclass");
746 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
747 $release= $1;
748 }
749 }
750 }
752 @servers= sort (@servers);
754 foreach $server (@servers){
755 # Conversation for backward compatibility
756 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
757 if ($server =~ /^([^:]+):([^:]+)$/ ) {
758 $server= "1:dummy:ldap://$1/$2";
759 } elsif ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
760 $server= "$1:dummy:ldap://$2/$3";
761 }
762 }
764 $base= $server;
765 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
766 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
767 push (@ldap_uris, $server);
768 }
770 # Assemble data package
771 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
772 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
773 if (defined $release){
774 $data{'release'}= $release;
775 }
777 # Need to append GOto settings?
778 if (defined $goto_admin and defined $goto_secret){
779 $data{'goto_admin'}= $goto_admin;
780 $data{'goto_secret'}= $goto_secret;
781 }
783 # Append unit tag if needed
784 if (defined $unit_tag){
786 # Find admin base and department name
787 $mesg = $main::ldap_handle->search( base => $ldap_base,
788 scope => 'sub',
789 attrs => ['dn', 'ou'],
790 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
791 #$mesg->code && die $mesg->error;
792 if($mesg->code) {
793 &main::daemon_log($mesg->error, 1);
794 return;
795 }
797 # Sanity check
798 if ($mesg->count != 1) {
799 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
800 return;
801 }
803 $entry= $mesg->entry(0);
804 $data{'admin_base'}= $entry->dn;
805 $data{'department'}= $entry->get_value("ou");
807 # Append unit Tag
808 $data{'unit_tag'}= $unit_tag;
809 }
811 # Send information
812 return &build_msg("new_ldap_config", $server_address, $address, \%data);
813 }
816 #=== FUNCTION ================================================================
817 # NAME: hardware_config
818 # PARAMETERS: address - string - ip address and port of a host
819 # RETURNS:
820 # DESCRIPTION:
821 #===============================================================================
822 sub hardware_config {
823 my ($address, $gotoHardwareChecksum) = @_ ;
825 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
826 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
828 # check hit
829 my $hit_counter = keys %{$res};
830 if( not $hit_counter == 1 ) {
831 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
832 }
834 my $macaddress = $res->{1}->{macaddress};
835 my $hostkey = $res->{1}->{hostkey};
837 if (not defined $macaddress) {
838 &main::daemon_log("ERROR: no mac address found for client $address", 1);
839 return;
840 }
842 # Build LDAP connection
843 &main::refresh_ldap_handle();
844 if( not defined $main::ldap_handle ) {
845 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
846 return;
847 }
849 # Perform search
850 $mesg = $main::ldap_handle->search(
851 base => $ldap_base,
852 scope => 'sub',
853 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
854 );
856 if($mesg->count() == 0) {
857 &main::daemon_log("Host was not found in LDAP!", 1);
858 } else {
859 my $entry= $mesg->entry(0);
860 my $dn= $entry->dn;
861 if(defined($entry->get_value("gotoHardwareChecksum"))) {
862 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
863 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
864 if($entry->update($main::ldap_handle)) {
865 &main::daemon_log("Hardware changed! Detection triggered.", 4);
866 }
867 } else {
868 # Nothing to do
869 return;
870 }
871 }
872 }
874 # Assemble data package
875 my %data = ();
877 # Need to append GOto settings?
878 if (defined $goto_admin and defined $goto_secret){
879 $data{'goto_admin'}= $goto_admin;
880 $data{'goto_secret'}= $goto_secret;
881 }
883 &main::daemon_log("Send detect_hardware message to $address", 4);
885 # Send information
886 return &build_msg("detect_hardware", $server_address, $address, \%data);
887 }
889 sub server_matches {
890 my $target = shift;
891 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
892 my $result = 0;
894 if($server_ip eq $target_ip) {
895 $result= 1;
896 } elsif ($target_ip eq "0.0.0.0") {
897 $result= 1;
898 } elsif ($server_ip eq "0.0.0.0") {
899 if ($target_ip eq "127.0.0.1") {
900 $result= 1;
901 } else {
902 my $PROC_NET_ROUTE= ('/proc/net/route');
904 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
905 or die "Could not open $PROC_NET_ROUTE";
907 my @ifs = <PROC_NET_ROUTE>;
909 close(PROC_NET_ROUTE);
911 # Eat header line
912 shift @ifs;
913 chomp @ifs;
914 foreach my $line(@ifs) {
915 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
916 my $destination;
917 my $mask;
918 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
919 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
920 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
921 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
922 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
923 # destination matches route, save mac and exit
924 $result= 1;
925 last;
926 }
927 }
928 }
929 } else {
930 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
931 }
933 return $result;
934 }
936 1;