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 && (!($new_ldap_config_out =~ /error/))) {
584 &add_content2xml_hash($out_hash, "ldap_available", "true");
585 } elsif($new_ldap_config_out && $new_ldap_config_out =~ /error/){
586 &add_content2xml_hash($out_hash, "error", $new_ldap_config_out);
588 my $sql_statement = "UPDATE $main::job_queue_tn ".
589 "SET status='error', result='$new_ldap_config_out' ".
590 "WHERE status='processing' AND macaddress LIKE '$mac_address'";
591 my $res = $main::job_db->update_dbentry($sql_statement);
592 &main::daemon_log("DEBUG: $sql_statement RESULT: $res", 7);
593 }
594 my $register_out = &create_xml_string($out_hash);
595 push(@out_msg_l, $register_out);
597 # Really send the ldap config
598 if( $new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
599 push(@out_msg_l, $new_ldap_config_out);
600 }
602 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
603 if( $hardware_config_out ) {
604 push(@out_msg_l, $hardware_config_out);
605 }
607 return @out_msg_l;
608 }
611 #=== FUNCTION ================================================================
612 # NAME: who_has
613 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
614 # RETURNS: nothing
615 # DESCRIPTION: process this incoming message
616 #===============================================================================
617 sub who_has {
618 my ($msg_hash) = @_ ;
619 my @out_msg_l;
621 # what is your search pattern
622 my $search_pattern = @{$msg_hash->{who_has}}[0];
623 my $search_element = @{$msg_hash->{$search_pattern}}[0];
624 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
626 # scanning known_clients for search_pattern
627 my @host_addresses = keys %$main::known_clients;
628 my $known_clients_entries = length @host_addresses;
629 my $host_address;
630 foreach my $host (@host_addresses) {
631 my $client_element = $main::known_clients->{$host}->{$search_pattern};
632 if ($search_element eq $client_element) {
633 $host_address = $host;
634 last;
635 }
636 }
638 # search was successful
639 if (defined $host_address) {
640 my $source = @{$msg_hash->{source}}[0];
641 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
642 &add_content2xml_hash($out_hash, "mac_address", $search_element);
643 my $out_msg = &create_xml_string($out_hash);
644 push(@out_msg_l, $out_msg);
645 }
646 return @out_msg_l;
647 }
650 sub who_has_i_do {
651 my ($msg_hash) = @_ ;
652 my $header = @{$msg_hash->{header}}[0];
653 my $source = @{$msg_hash->{source}}[0];
654 my $search_param = @{$msg_hash->{$header}}[0];
655 my $search_value = @{$msg_hash->{$search_param}}[0];
656 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
657 }
660 #=== FUNCTION ================================================================
661 # NAME: new_ldap_config
662 # PARAMETERS: address - string - ip address and port of a host
663 # RETURNS: gosa-si conform message
664 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
665 #===============================================================================
666 sub new_ldap_config {
667 my ($address) = @_ ;
669 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress LIKE '$address'";
670 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
672 # check hit
673 my $hit_counter = keys %{$res};
674 if( not $hit_counter == 1 ) {
675 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
676 }
678 $address = $res->{1}->{hostname};
679 my $macaddress = $res->{1}->{macaddress};
680 my $hostkey = $res->{1}->{hostkey};
682 if (not defined $macaddress) {
683 &main::daemon_log("ERROR: no mac address found for client $address", 1);
684 return;
685 }
687 # Build LDAP connection
688 &main::refresh_ldap_handle();
689 if( not defined $main::ldap_handle ) {
690 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
691 return;
692 }
694 # Perform search
695 $mesg = $main::ldap_handle->search( base => $ldap_base,
696 scope => 'sub',
697 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
698 filter => "(&(objectClass=GOhard)(macaddress=$macaddress)(gotoLdapServer=*))");
699 #$mesg->code && die $mesg->error;
700 if($mesg->code) {
701 &main::daemon_log($mesg->error, 1);
702 return;
703 }
705 # Sanity check
706 if ($mesg->count != 1) {
707 &main::daemon_log("WARNING: client with mac address $macaddress not found/unique/active - not sending ldap config", 1);
708 &main::daemon_log("\tbase: $ldap_base", 1);
709 &main::daemon_log("\tscope: sub", 1);
710 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
711 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress)(gotoLdapServer=*))", 1);
712 return;
713 }
715 my $entry= $mesg->entry(0);
716 my $dn= $entry->dn;
717 my @servers= $entry->get_value("gotoLdapServer");
718 my $unit_tag= $entry->get_value("gosaUnitTag");
719 my @ldap_uris;
720 my $server;
721 my $base;
722 my $release;
724 # Fill release if available
725 my $FAIclass= $entry->get_value("FAIclass");
726 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
727 $release= $1;
728 }
730 # Do we need to look at an object class?
731 if (length(@servers) < 1){
732 $mesg = $main::ldap_handle->search( base => $ldap_base,
733 scope => 'sub',
734 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
735 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
736 #$mesg->code && die $mesg->error;
737 if($mesg->code) {
738 &main::daemon_log($mesg->error, 1);
739 return;
740 }
742 # Sanity check
743 if ($mesg->count != 1) {
744 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
745 return;
746 }
748 $entry= $mesg->entry(0);
749 $dn= $entry->dn;
750 @servers= $entry->get_value("gotoLdapServer");
752 if (not defined $release){
753 $FAIclass= $entry->get_value("FAIclass");
754 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
755 $release= $1;
756 }
757 }
758 }
760 @servers= sort (@servers);
762 foreach $server (@servers){
763 # Conversation for backward compatibility
764 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
765 if ($server =~ /^([^:]+):([^:]+)$/ ) {
766 $server= "1:dummy:ldap://$1/$2";
767 } elsif ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
768 $server= "$1:dummy:ldap://$2/$3";
769 }
770 }
772 $base= $server;
773 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
774 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
775 push (@ldap_uris, $server);
776 }
778 # Assemble data package
779 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
780 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
781 if (defined $release){
782 $data{'release'}= $release;
783 }
785 # Need to append GOto settings?
786 if (defined $goto_admin and defined $goto_secret){
787 $data{'goto_admin'}= $goto_admin;
788 $data{'goto_secret'}= $goto_secret;
789 }
791 # Append unit tag if needed
792 if (defined $unit_tag){
794 # Find admin base and department name
795 $mesg = $main::ldap_handle->search( base => $ldap_base,
796 scope => 'sub',
797 attrs => ['dn', 'ou'],
798 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
799 #$mesg->code && die $mesg->error;
800 if($mesg->code) {
801 &main::daemon_log($mesg->error, 1);
802 return "error-unit-tag-count-0";
803 }
805 # Sanity check
806 if ($mesg->count != 1) {
807 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
808 return "error-unit-tag-count-".$mesg->count;
809 }
811 $entry= $mesg->entry(0);
812 $data{'admin_base'}= $entry->dn;
813 $data{'department'}= $entry->get_value("ou");
815 # Append unit Tag
816 $data{'unit_tag'}= $unit_tag;
817 }
819 # Send information
820 return &build_msg("new_ldap_config", $server_address, $address, \%data);
821 }
824 #=== FUNCTION ================================================================
825 # NAME: hardware_config
826 # PARAMETERS: address - string - ip address and port of a host
827 # RETURNS:
828 # DESCRIPTION:
829 #===============================================================================
830 sub hardware_config {
831 my ($address, $gotoHardwareChecksum) = @_ ;
833 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
834 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
836 # check hit
837 my $hit_counter = keys %{$res};
838 if( not $hit_counter == 1 ) {
839 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
840 }
842 my $macaddress = $res->{1}->{macaddress};
843 my $hostkey = $res->{1}->{hostkey};
845 if (not defined $macaddress) {
846 &main::daemon_log("ERROR: no mac address found for client $address", 1);
847 return;
848 }
850 # Build LDAP connection
851 &main::refresh_ldap_handle();
852 if( not defined $main::ldap_handle ) {
853 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
854 return;
855 }
857 # Perform search
858 $mesg = $main::ldap_handle->search(
859 base => $ldap_base,
860 scope => 'sub',
861 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
862 );
864 if($mesg->count() == 0) {
865 &main::daemon_log("Host was not found in LDAP!", 1);
866 } else {
867 my $entry= $mesg->entry(0);
868 my $dn= $entry->dn;
869 if(defined($entry->get_value("gotoHardwareChecksum"))) {
870 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
871 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
872 if($entry->update($main::ldap_handle)) {
873 &main::daemon_log("Hardware changed! Detection triggered.", 4);
874 }
875 } else {
876 # Nothing to do
877 return;
878 }
879 }
880 }
882 # Assemble data package
883 my %data = ();
885 # Need to append GOto settings?
886 if (defined $goto_admin and defined $goto_secret){
887 $data{'goto_admin'}= $goto_admin;
888 $data{'goto_secret'}= $goto_secret;
889 }
891 &main::daemon_log("Send detect_hardware message to $address", 4);
893 # Send information
894 return &build_msg("detect_hardware", $server_address, $address, \%data);
895 }
897 sub server_matches {
898 my $target = shift;
899 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
900 my $result = 0;
902 if($server_ip eq $target_ip) {
903 $result= 1;
904 } elsif ($target_ip eq "0.0.0.0") {
905 $result= 1;
906 } elsif ($server_ip eq "0.0.0.0") {
907 if ($target_ip eq "127.0.0.1") {
908 $result= 1;
909 } else {
910 my $PROC_NET_ROUTE= ('/proc/net/route');
912 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
913 or die "Could not open $PROC_NET_ROUTE";
915 my @ifs = <PROC_NET_ROUTE>;
917 close(PROC_NET_ROUTE);
919 # Eat header line
920 shift @ifs;
921 chomp @ifs;
922 foreach my $line(@ifs) {
923 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
924 my $destination;
925 my $mask;
926 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
927 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
928 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
929 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
930 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
931 # destination matches route, save mac and exit
932 $result= 1;
933 last;
934 }
935 }
936 }
937 } else {
938 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
939 }
941 return $result;
942 }
944 1;