c0df9964092dbf3f2e28fab14fcc377733d287e6
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(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);
403 &main::daemon_log("$msg", 8);
405 if( 0 == length @target_l){
406 &main::daemon_log("ERROR: no target specified for msg $header", 1);
407 $error++;
408 }
410 if( 1 == length @target_l) {
411 my $target = $target_l[0];
412 if(&server_matches($target)) {
415 if ($header eq 'new_key') {
416 @out_msg_l = &new_key($msg_hash)
417 } elsif ($header eq 'here_i_am') {
418 @out_msg_l = &here_i_am($msg_hash)
419 } else {
420 if( exists $event_hash->{$header} ) {
421 # a event exists with the header as name
422 &main::daemon_log("INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
423 no strict 'refs';
424 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
425 }
426 }
428 # if delivery not possible raise error and return
429 if( not @out_msg_l ) {
430 &main::daemon_log("WARNING: SIPackages got not answer from event handler '$header'", 3);
431 } elsif( 0 == @out_msg_l) {
432 &main::daemon_log("ERROR: SIPackages: no event handler or core function defined for '$header'", 1);
433 }
434 }
435 else {
436 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
437 push(@out_msg_l, $msg);
438 }
439 }
441 return \@out_msg_l;
442 }
445 #=== FUNCTION ================================================================
446 # NAME: new_passwd
447 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
448 # RETURNS: nothing
449 # DESCRIPTION: process this incoming message
450 #===============================================================================
451 sub new_key {
452 my ($msg_hash) = @_;
453 my @out_msg_l;
455 my $header = @{$msg_hash->{header}}[0];
456 my $source_name = @{$msg_hash->{source}}[0];
457 my $source_key = @{$msg_hash->{new_key}}[0];
458 my $query_res;
460 # check known_clients_db
461 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
462 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
463 if( 1 == keys %{$query_res} ) {
464 my $act_time = &get_time;
465 my $sql_statement= "UPDATE known_clients ".
466 "SET hostkey='$source_key', timestamp='$act_time' ".
467 "WHERE hostname='$source_name'";
468 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
469 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
470 my $out_msg = &create_xml_string($hash);
471 push(@out_msg_l, $out_msg);
472 }
474 # only do if host still not found
475 if( 0 == @out_msg_l ) {
476 # check known_server_db
477 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
478 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
479 if( 1 == keys %{$query_res} ) {
480 my $act_time = &get_time;
481 my $sql_statement= "UPDATE known_server ".
482 "SET hostkey='$source_key', timestamp='$act_time' ".
483 "WHERE hostname='$source_name'";
484 my $res = $main::known_server_db->update_dbentry( $sql_statement );
486 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
487 my $out_msg = &create_xml_string($hash);
488 push(@out_msg_l, $out_msg);
489 }
490 }
492 return @out_msg_l;
493 }
496 #=== FUNCTION ================================================================
497 # NAME: here_i_am
498 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
499 # RETURNS: nothing
500 # DESCRIPTION: process this incoming message
501 #===============================================================================
502 sub here_i_am {
503 my ($msg_hash) = @_;
504 my @out_msg_l;
505 my $out_hash;
507 my $source = @{$msg_hash->{source}}[0];
508 my $mac_address = @{$msg_hash->{mac_address}}[0];
509 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
511 # number of known clients
512 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
514 # check wether client address or mac address is already known
515 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
516 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
518 if ( 1 == keys %{$db_res} ) {
519 &main::daemon_log("WARNING: $source is already known as a client", 1);
520 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
521 $nu_clients --;
522 }
524 # number of actual activ clients
525 my $act_nu_clients = $nu_clients;
527 &main::daemon_log("INFO: number of actual activ clients: $act_nu_clients", 5);
528 &main::daemon_log("INFO: number of maximal allowed clients: $max_clients", 5);
530 if($max_clients <= $act_nu_clients) {
531 my $out_hash = &create_xml_hash("denied", $server_address, $source);
532 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
533 my $passwd = @{$msg_hash->{new_passwd}}[0];
534 &send_msg_hash2address($out_hash, $source, $passwd);
535 return;
536 }
538 # new client accepted
539 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
541 # create entry in known_clients
542 my $events = @{$msg_hash->{events}}[0];
545 # add entry to known_clients_db
546 my $act_timestamp = &get_time;
547 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
548 primkey=>['hostname'],
549 hostname=>$source,
550 events=>$events,
551 macaddress=>$mac_address,
552 status=>'registered',
553 hostkey=>$new_passwd,
554 timestamp=>$act_timestamp,
555 } );
557 if ($res != 0) {
558 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
559 return;
560 }
562 # return acknowledgement to client
563 $out_hash = &create_xml_hash("registered", $server_address, $source);
564 my $register_out = &create_xml_string($out_hash);
565 push(@out_msg_l, $register_out);
567 # notify registered client to bus
568 if( $bus_activ eq "on") {
569 # fetch actual bus key
570 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
571 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
572 my $hostkey = $query_res->{1}->{'hostkey'};
574 # send update msg to bus
575 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
576 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
577 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
578 my $new_client_out = &create_xml_string($out_hash);
579 push(@out_msg_l, $new_client_out);
580 &main::daemon_log("INFO: send bus msg that client '$source' has registerd at server '$server_address'", 5);
581 }
583 # give the new client his ldap config
584 my $new_ldap_config_out = &new_ldap_config($source);
585 if( $new_ldap_config_out ) {
586 push(@out_msg_l, $new_ldap_config_out);
587 }
589 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
590 if( $hardware_config_out ) {
591 push(@out_msg_l, $hardware_config_out);
592 }
594 return @out_msg_l;
595 }
598 #=== FUNCTION ================================================================
599 # NAME: who_has
600 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
601 # RETURNS: nothing
602 # DESCRIPTION: process this incoming message
603 #===============================================================================
604 sub who_has {
605 my ($msg_hash) = @_ ;
606 my @out_msg_l;
608 # what is your search pattern
609 my $search_pattern = @{$msg_hash->{who_has}}[0];
610 my $search_element = @{$msg_hash->{$search_pattern}}[0];
611 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
613 # scanning known_clients for search_pattern
614 my @host_addresses = keys %$main::known_clients;
615 my $known_clients_entries = length @host_addresses;
616 my $host_address;
617 foreach my $host (@host_addresses) {
618 my $client_element = $main::known_clients->{$host}->{$search_pattern};
619 if ($search_element eq $client_element) {
620 $host_address = $host;
621 last;
622 }
623 }
625 # search was successful
626 if (defined $host_address) {
627 my $source = @{$msg_hash->{source}}[0];
628 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
629 &add_content2xml_hash($out_hash, "mac_address", $search_element);
630 my $out_msg = &create_xml_string($out_hash);
631 push(@out_msg_l, $out_msg);
632 }
633 return @out_msg_l;
634 }
637 sub who_has_i_do {
638 my ($msg_hash) = @_ ;
639 my $header = @{$msg_hash->{header}}[0];
640 my $source = @{$msg_hash->{source}}[0];
641 my $search_param = @{$msg_hash->{$header}}[0];
642 my $search_value = @{$msg_hash->{$search_param}}[0];
643 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
644 }
647 #=== FUNCTION ================================================================
648 # NAME: new_ldap_config
649 # PARAMETERS: address - string - ip address and port of a host
650 # RETURNS: gosa-si conform message
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' OR macaddress='$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 $address = $res->{1}->{hostname};
666 my $macaddress = $res->{1}->{macaddress};
667 my $hostkey = $res->{1}->{hostkey};
669 if (not defined $macaddress) {
670 &main::daemon_log("ERROR: no mac address found for client $address", 1);
671 return;
672 }
674 # Build LDAP connection
675 &main::refresh_ldap_handle();
676 if( not defined $main::ldap_handle ) {
677 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
678 return;
679 }
681 # Perform search
682 $mesg = $main::ldap_handle->search( base => $ldap_base,
683 scope => 'sub',
684 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
685 filter => "(&(objectClass=GOhard)(macaddress=$macaddress)(gotoMode=active))");
686 #$mesg->code && die $mesg->error;
687 if($mesg->code) {
688 &main::daemon_log($mesg->error, 1);
689 return;
690 }
692 # Sanity check
693 if ($mesg->count != 1) {
694 &main::daemon_log("WARNING: client with mac address $macaddress not found/unique/active - not sending ldap config", 1);
695 &main::daemon_log("\tbase: $ldap_base", 1);
696 &main::daemon_log("\tscope: sub", 1);
697 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
698 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
699 return;
700 }
702 my $entry= $mesg->entry(0);
703 my $dn= $entry->dn;
704 my @servers= $entry->get_value("gotoLdapServer");
705 my $unit_tag= $entry->get_value("gosaUnitTag");
706 my @ldap_uris;
707 my $server;
708 my $base;
709 my $release;
711 # Fill release if available
712 my $FAIclass= $entry->get_value("FAIclass");
713 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
714 $release= $1;
715 }
717 # Do we need to look at an object class?
718 if (length(@servers) < 1){
719 $mesg = $main::ldap_handle->search( base => $ldap_base,
720 scope => 'sub',
721 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
722 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
723 #$mesg->code && die $mesg->error;
724 if($mesg->code) {
725 &main::daemon_log($mesg->error, 1);
726 return;
727 }
729 # Sanity check
730 if ($mesg->count != 1) {
731 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
732 return;
733 }
735 $entry= $mesg->entry(0);
736 $dn= $entry->dn;
737 @servers= $entry->get_value("gotoLdapServer");
739 if (not defined $release){
740 $FAIclass= $entry->get_value("FAIclass");
741 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
742 $release= $1;
743 }
744 }
745 }
747 @servers= sort (@servers);
749 foreach $server (@servers){
750 # Conversation for backward compatibility
751 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
752 if ($server =~ /^([^:]+):([^:]+)$/ ) {
753 $server= "1:dummy:ldap://$1/$2";
754 }
755 if ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
756 $server= "$1:dummy:ldap://$2/$3";
757 }
758 }
760 $base= $server;
761 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
762 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
763 push (@ldap_uris, $server);
764 }
766 # Assemble data package
767 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
768 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
769 if (defined $release){
770 $data{'release'}= $release;
771 }
773 # Need to append GOto settings?
774 if (defined $goto_admin and defined $goto_secret){
775 $data{'goto_admin'}= $goto_admin;
776 $data{'goto_secret'}= $goto_secret;
777 }
779 # Append unit tag if needed
780 if (defined $unit_tag){
782 # Find admin base and department name
783 $mesg = $main::ldap_handle->search( base => $ldap_base,
784 scope => 'sub',
785 attrs => ['dn', 'ou'],
786 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
787 #$mesg->code && die $mesg->error;
788 if($mesg->code) {
789 &main::daemon_log($mesg->error, 1);
790 return;
791 }
793 # Sanity check
794 if ($mesg->count != 1) {
795 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
796 return;
797 }
799 $entry= $mesg->entry(0);
800 $data{'admin_base'}= $entry->dn;
801 $data{'department'}= $entry->get_value("ou");
803 # Append unit Tag
804 $data{'unit_tag'}= $unit_tag;
805 }
807 # Send information
808 return &build_msg("new_ldap_config", $server_address, $address, \%data);
809 }
812 #=== FUNCTION ================================================================
813 # NAME: hardware_config
814 # PARAMETERS: address - string - ip address and port of a host
815 # RETURNS:
816 # DESCRIPTION:
817 #===============================================================================
818 sub hardware_config {
819 my ($address, $gotoHardwareChecksum) = @_ ;
821 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
822 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
824 # check hit
825 my $hit_counter = keys %{$res};
826 if( not $hit_counter == 1 ) {
827 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
828 }
830 my $macaddress = $res->{1}->{macaddress};
831 my $hostkey = $res->{1}->{hostkey};
833 if (not defined $macaddress) {
834 &main::daemon_log("ERROR: no mac address found for client $address", 1);
835 return;
836 }
838 # Build LDAP connection
839 &main::refresh_ldap_handle();
840 if( not defined $main::ldap_handle ) {
841 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
842 return;
843 }
845 # Perform search
846 $mesg = $main::ldap_handle->search(
847 base => $ldap_base,
848 scope => 'sub',
849 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
850 );
852 if($mesg->count() == 0) {
853 &main::daemon_log("Host was not found in LDAP!", 1);
854 } else {
855 my $entry= $mesg->entry(0);
856 my $dn= $entry->dn;
857 if(defined($entry->get_value("gotoHardwareChecksum"))) {
858 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
859 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
860 if($entry->update($main::ldap_handle)) {
861 &main::daemon_log("Hardware changed! Detection triggered.", 4);
862 }
863 } else {
864 # Nothing to do
865 return;
866 }
867 }
868 }
870 # Assemble data package
871 my %data = ();
873 # Need to append GOto settings?
874 if (defined $goto_admin and defined $goto_secret){
875 $data{'goto_admin'}= $goto_admin;
876 $data{'goto_secret'}= $goto_secret;
877 }
879 &main::daemon_log("Send detect_hardware message to $address", 4);
881 # Send information
882 return &build_msg("detect_hardware", $server_address, $address, \%data);
883 }
885 sub server_matches {
886 my $target = shift;
887 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
888 my $result = 0;
890 if($server_ip eq $target_ip) {
891 $result= 1;
892 } elsif ($server_ip eq "0.0.0.0") {
893 if ($target_ip eq "127.0.0.1") {
894 $result= 1;
895 } else {
896 my $PROC_NET_ROUTE= ('/proc/net/route');
898 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
899 or die "Could not open $PROC_NET_ROUTE";
901 my @ifs = <PROC_NET_ROUTE>;
903 close(PROC_NET_ROUTE);
905 # Eat header line
906 shift @ifs;
907 chomp @ifs;
908 foreach my $line(@ifs) {
909 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
910 my $destination;
911 my $mask;
912 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
913 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
914 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
915 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
916 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
917 # destination matches route, save mac and exit
918 $result= 1;
919 last;
920 }
921 }
922 }
923 } else {
924 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
925 }
927 return $result;
928 }
930 1;