d85d0b05d849645fd454b0ba3cad6cda93c9e12a
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;
20 use utf8;
22 my $event_dir = "/usr/lib/gosa-si/server/events";
23 use lib "/usr/lib/gosa-si/server/events";
25 BEGIN{}
26 END {}
28 my ($server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
29 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
30 my $server;
31 my $event_hash;
32 my $network_interface;
33 my $no_bus;
34 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
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 my $tmp_ldap= Net::LDAP->new($ldap_uri);
75 if( defined($tmp_ldap) ) {
76 &main::daemon_log("INFO: Searching for servers gosaUnitTag with mac address $server_mac_address",5);
77 my $mesg= $tmp_ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
78 # Perform search for Unit Tag
79 $mesg = $tmp_ldap->search(
80 base => $ldap_base,
81 scope => 'sub',
82 attrs => ['gosaUnitTag'],
83 filter => "(macaddress=$server_mac_address)"
84 );
86 if ($mesg->count == 1) {
87 my $entry= $mesg->entry(0);
88 my $unit_tag= $entry->get_value("gosaUnitTag");
89 if(defined($unit_tag) && length($unit_tag) > 0) {
90 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
91 $main::gosa_unit_tag= $unit_tag;
92 }
93 } else {
94 # Perform another search for Unit Tag
95 my $hostname= `hostname -f`;
96 chomp($hostname);
97 &main::daemon_log("INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
98 $mesg = $tmp_ldap->search(
99 base => $ldap_base,
100 scope => 'sub',
101 attrs => ['gosaUnitTag'],
102 filter => "(&(cn=$hostname)(objectClass=goServer))"
103 );
104 if ($mesg->count == 1) {
105 my $entry= $mesg->entry(0);
106 my $unit_tag= $entry->get_value("gosaUnitTag");
107 if(defined($unit_tag) && length($unit_tag) > 0) {
108 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
109 $main::gosa_unit_tag= $unit_tag;
110 }
111 } else {
112 # Perform another search for Unit Tag
113 $hostname= `hostname -s`;
114 chomp($hostname);
115 &main::daemon_log("INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
116 $mesg = $tmp_ldap->search(
117 base => $ldap_base,
118 scope => 'sub',
119 attrs => ['gosaUnitTag'],
120 filter => "(&(cn=$hostname)(objectClass=goServer))"
121 );
122 if ($mesg->count == 1) {
123 my $entry= $mesg->entry(0);
124 my $unit_tag= $entry->get_value("gosaUnitTag");
125 if(defined($unit_tag) && length($unit_tag) > 0) {
126 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
127 $main::gosa_unit_tag= $unit_tag;
128 }
129 } else {
130 &main::daemon_log("WARNING: No gosaUnitTag detected. Not using gosaUnitTag", 3);
131 }
132 }
133 }
134 $tmp_ldap->unbind;
135 } else {
136 &main::daemon_log("INFO: Using gosaUnitTag from config-file: $main::gosa_unit_tag",5);
137 }
138 }
141 my $server_address = "$server_ip:$server_port";
142 $main::server_address = $server_address;
145 if( inet_aton($bus_ip) ){ $bus_ip = inet_ntoa(inet_aton($bus_ip)); }
146 ######################################################
147 # to change
148 if( $bus_ip eq "127.0.1.1" ) { $bus_ip = "127.0.0.1" }
149 ######################################################
150 my $bus_address = "$bus_ip:$bus_port";
151 $main::bus_address = $bus_address;
153 # create general settings for this module
154 my $xml = new XML::Simple();
156 # register at bus
157 if ($main::no_bus > 0) {
158 $bus_activ = "off"
159 }
160 if($bus_activ eq "on") {
161 ®ister_at_bus();
162 }
164 # add myself to known_server_db
165 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
166 primkey=>['hostname'],
167 hostname=>$server_address,
168 status=>'myself',
169 hostkey=>$SIPackages_key,
170 timestamp=>&get_time,
171 } );
175 ### functions #################################################################
178 sub get_module_info {
179 my @info = ($server_address,
180 $SIPackages_key,
181 );
182 return \@info;
183 }
186 #=== FUNCTION ================================================================
187 # NAME: read_configfile
188 # PARAMETERS: cfg_file - string -
189 # RETURNS: nothing
190 # DESCRIPTION: read cfg_file and set variables
191 #===============================================================================
192 sub read_configfile {
193 my $cfg;
194 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
195 if( -r $main::cfg_file ) {
196 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
197 } else {
198 print STDERR "Couldn't read config file!";
199 }
200 } else {
201 $cfg = Config::IniFiles->new() ;
202 }
203 foreach my $section (keys %cfg_defaults) {
204 foreach my $param (keys %{$cfg_defaults{ $section }}) {
205 my $pinfo = $cfg_defaults{ $section }{ $param };
206 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
207 }
208 }
210 # Read non predefined sections
211 my $param;
212 if ($cfg->SectionExists('ldap')){
213 foreach $param ($cfg->Parameters('ldap')){
214 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
215 }
216 }
217 if ($cfg->SectionExists('pam_ldap')){
218 foreach $param ($cfg->Parameters('pam_ldap')){
219 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
220 }
221 }
222 if ($cfg->SectionExists('nss_ldap')){
223 foreach $param ($cfg->Parameters('nss_ldap')){
224 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
225 }
226 }
227 if ($cfg->SectionExists('goto')){
228 $goto_admin= $cfg->val('goto', 'terminal_admin');
229 $goto_secret= $cfg->val('goto', 'terminal_secret');
230 } else {
231 $goto_admin= undef;
232 $goto_secret= undef;
233 }
235 }
237 #=== FUNCTION ================================================================
238 # NAME: get_interface_for_ip
239 # PARAMETERS: ip address (i.e. 192.168.0.1)
240 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
241 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
242 #===============================================================================
243 sub get_interface_for_ip {
244 my $result;
245 my $ip= shift;
246 if ($ip && length($ip) > 0) {
247 my @ifs= &get_interfaces();
248 if($ip eq "0.0.0.0") {
249 $result = "all";
250 } else {
251 foreach (@ifs) {
252 my $if=$_;
253 if(get_ip($if) eq $ip) {
254 $result = $if;
255 }
256 }
257 }
258 }
259 return $result;
260 }
262 #=== FUNCTION ================================================================
263 # NAME: get_interfaces
264 # PARAMETERS: none
265 # RETURNS: (list of interfaces)
266 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
267 #===============================================================================
268 sub get_interfaces {
269 my @result;
270 my $PROC_NET_DEV= ('/proc/net/dev');
272 open(PROC_NET_DEV, "<$PROC_NET_DEV")
273 or die "Could not open $PROC_NET_DEV";
275 my @ifs = <PROC_NET_DEV>;
277 close(PROC_NET_DEV);
279 # Eat first two line
280 shift @ifs;
281 shift @ifs;
283 chomp @ifs;
284 foreach my $line(@ifs) {
285 my $if= (split /:/, $line)[0];
286 $if =~ s/^\s+//;
287 push @result, $if;
288 }
290 return @result;
291 }
293 #=== FUNCTION ================================================================
294 # NAME: get_mac
295 # PARAMETERS: interface name (i.e. eth0)
296 # RETURNS: (mac address)
297 # DESCRIPTION: Uses ioctl to get mac address directly from system.
298 #===============================================================================
299 sub get_mac {
300 my $ifreq= shift;
301 my $result;
302 if ($ifreq && length($ifreq) > 0) {
303 if($ifreq eq "all") {
304 $result = "00:00:00:00:00:00";
305 } else {
306 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
308 # A configured MAC Address should always override a guessed value
309 if ($server_mac_address and length($server_mac_address) > 0) {
310 $result= $server_mac_address;
311 }
313 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
314 or die "socket: $!";
316 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
317 my ($if, $mac)= unpack 'h36 H12', $ifreq;
319 if (length($mac) > 0) {
320 $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])$/;
321 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
322 $result = $mac;
323 }
324 }
325 }
326 }
327 return $result;
328 }
331 #=== FUNCTION ================================================================
332 # NAME: register_at_bus
333 # PARAMETERS: nothing
334 # RETURNS: nothing
335 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
336 #===============================================================================
337 sub register_at_bus {
339 # add bus to known_server_db
340 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
341 primkey=>['hostname'],
342 hostname=>$bus_address,
343 status=>'bus',
344 hostkey=>$bus_key,
345 timestamp=>&get_time,
346 } );
347 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
348 my $msg = &create_xml_string($msg_hash);
350 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
351 return $msg;
352 }
355 sub import_events {
356 if (not -e $event_dir) {
357 &main::daemon_log("ERROR: cannot find directory or directory is not readable: $event_dir", 1);
358 }
359 opendir (DIR, $event_dir) or die "ERROR while loading gosa-si-events from directory $event_dir : $!\n";
361 while (defined (my $event = readdir (DIR))) {
362 if( $event eq "." || $event eq ".." ) { next; }
363 if( $event eq "gosaTriggered.pm" ) { next; } # only GOsa specific events
365 eval{ require $event; };
366 if( $@ ) {
367 &main::daemon_log("import of event module '$event' failed", 1);
368 &main::daemon_log("$@", 8);
369 next;
370 }
372 $event =~ /(\S*?).pm$/;
373 my $event_module = $1;
374 my $events_l = eval( $1."::get_events()") ;
375 foreach my $event_name (@{$events_l}) {
376 $event_hash->{$event_name} = $event_module;
377 }
378 my $events_string = join( ", ", @{$events_l});
379 &main::daemon_log("INFO: SIPackages imported events $events_string", 5);
380 }
381 }
384 #=== FUNCTION ================================================================
385 # NAME: process_incoming_msg
386 # PARAMETERS: crypted_msg - string - incoming crypted message
387 # RETURNS: nothing
388 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
389 #===============================================================================
390 sub process_incoming_msg {
391 my ($msg, $msg_hash, $session_id) = @_ ;
392 my $error = 0;
393 my $host_name;
394 my $host_key;
395 my @out_msg_l = ();
397 # process incoming msg
398 my $header = @{$msg_hash->{header}}[0];
399 my @target_l = @{$msg_hash->{target}};
401 # skip PREFIX
402 $header =~ s/^CLMSG_//;
404 &main::daemon_log("DEBUG: SIPackages: msg to process: $header", 7);
405 &main::daemon_log("$msg", 8);
407 if( 0 == length @target_l){
408 &main::daemon_log("ERROR: no target specified for msg $header", 1);
409 $error++;
410 }
412 if( 1 == length @target_l) {
413 my $target = $target_l[0];
414 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 } else {
422 if( exists $event_hash->{$header} ) {
423 # a event exists with the header as name
424 &main::daemon_log("INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
425 no strict 'refs';
426 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
427 }
428 }
430 # if delivery not possible raise error and return
431 if( not @out_msg_l ) {
432 &main::daemon_log("WARNING: SIPackages got not answer from event handler '$header'", 3);
433 } elsif( 0 == @out_msg_l) {
434 &main::daemon_log("ERROR: SIPackages: no event handler or core function defined for '$header'", 1);
435 }
436 }
437 else {
438 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
439 push(@out_msg_l, $msg);
440 }
441 }
443 return \@out_msg_l;
444 }
447 #=== FUNCTION ================================================================
448 # NAME: new_passwd
449 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
450 # RETURNS: nothing
451 # DESCRIPTION: process this incoming message
452 #===============================================================================
453 sub new_key {
454 my ($msg_hash) = @_;
455 my @out_msg_l;
457 my $header = @{$msg_hash->{header}}[0];
458 my $source_name = @{$msg_hash->{source}}[0];
459 my $source_key = @{$msg_hash->{new_key}}[0];
460 my $query_res;
462 # check known_clients_db
463 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
464 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
465 if( 1 == keys %{$query_res} ) {
466 my $act_time = &get_time;
467 my $sql_statement= "UPDATE known_clients ".
468 "SET hostkey='$source_key', timestamp='$act_time' ".
469 "WHERE hostname='$source_name'";
470 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
471 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
472 my $out_msg = &create_xml_string($hash);
473 push(@out_msg_l, $out_msg);
474 }
476 # only do if host still not found
477 if( 0 == @out_msg_l ) {
478 # check known_server_db
479 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
480 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
481 if( 1 == keys %{$query_res} ) {
482 my $act_time = &get_time;
483 my $sql_statement= "UPDATE known_server ".
484 "SET hostkey='$source_key', timestamp='$act_time' ".
485 "WHERE hostname='$source_name'";
486 my $res = $main::known_server_db->update_dbentry( $sql_statement );
488 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
489 my $out_msg = &create_xml_string($hash);
490 push(@out_msg_l, $out_msg);
491 }
492 }
494 return @out_msg_l;
495 }
498 #=== FUNCTION ================================================================
499 # NAME: here_i_am
500 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
501 # RETURNS: nothing
502 # DESCRIPTION: process this incoming message
503 #===============================================================================
504 sub here_i_am {
505 my ($msg_hash) = @_;
506 my @out_msg_l;
507 my $out_hash;
509 my $source = @{$msg_hash->{source}}[0];
510 my $mac_address = @{$msg_hash->{mac_address}}[0];
511 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
513 # number of known clients
514 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
516 # check wether client address or mac address is already known
517 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
518 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
520 if ( 1 == keys %{$db_res} ) {
521 &main::daemon_log("WARNING: $source is already known as a client", 1);
522 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
523 $nu_clients --;
524 }
526 # number of actual activ clients
527 my $act_nu_clients = $nu_clients;
529 &main::daemon_log("INFO: number of actual activ clients: $act_nu_clients", 5);
530 &main::daemon_log("INFO: number of maximal allowed clients: $max_clients", 5);
532 if($max_clients <= $act_nu_clients) {
533 my $out_hash = &create_xml_hash("denied", $server_address, $source);
534 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
535 my $passwd = @{$msg_hash->{new_passwd}}[0];
536 &send_msg_hash2address($out_hash, $source, $passwd);
537 return;
538 }
540 # new client accepted
541 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
543 # create entry in known_clients
544 my $events = @{$msg_hash->{events}}[0];
547 # add entry to known_clients_db
548 my $act_timestamp = &get_time;
549 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
550 primkey=>['hostname'],
551 hostname=>$source,
552 events=>$events,
553 macaddress=>$mac_address,
554 status=>'registered',
555 hostkey=>$new_passwd,
556 timestamp=>$act_timestamp,
557 } );
559 if ($res != 0) {
560 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
561 return;
562 }
564 # return acknowledgement to client
565 $out_hash = &create_xml_hash("registered", $server_address, $source);
566 my $register_out = &create_xml_string($out_hash);
567 push(@out_msg_l, $register_out);
569 # notify registered client to bus
570 if( $bus_activ eq "on") {
571 # fetch actual bus key
572 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
573 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
574 my $hostkey = $query_res->{1}->{'hostkey'};
576 # send update msg to bus
577 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
578 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
579 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
580 my $new_client_out = &create_xml_string($out_hash);
581 push(@out_msg_l, $new_client_out);
582 &main::daemon_log("INFO: send bus msg that client '$source' has registerd at server '$server_address'", 5);
583 }
585 # give the new client his ldap config
586 my $new_ldap_config_out = &new_ldap_config($source);
587 if( $new_ldap_config_out ) {
588 push(@out_msg_l, $new_ldap_config_out);
589 }
591 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
592 if( $hardware_config_out ) {
593 push(@out_msg_l, $hardware_config_out);
594 }
596 return @out_msg_l;
597 }
600 #=== FUNCTION ================================================================
601 # NAME: who_has
602 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
603 # RETURNS: nothing
604 # DESCRIPTION: process this incoming message
605 #===============================================================================
606 sub who_has {
607 my ($msg_hash) = @_ ;
608 my @out_msg_l;
610 # what is your search pattern
611 my $search_pattern = @{$msg_hash->{who_has}}[0];
612 my $search_element = @{$msg_hash->{$search_pattern}}[0];
613 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
615 # scanning known_clients for search_pattern
616 my @host_addresses = keys %$main::known_clients;
617 my $known_clients_entries = length @host_addresses;
618 my $host_address;
619 foreach my $host (@host_addresses) {
620 my $client_element = $main::known_clients->{$host}->{$search_pattern};
621 if ($search_element eq $client_element) {
622 $host_address = $host;
623 last;
624 }
625 }
627 # search was successful
628 if (defined $host_address) {
629 my $source = @{$msg_hash->{source}}[0];
630 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
631 &add_content2xml_hash($out_hash, "mac_address", $search_element);
632 my $out_msg = &create_xml_string($out_hash);
633 push(@out_msg_l, $out_msg);
634 }
635 return @out_msg_l;
636 }
639 sub who_has_i_do {
640 my ($msg_hash) = @_ ;
641 my $header = @{$msg_hash->{header}}[0];
642 my $source = @{$msg_hash->{source}}[0];
643 my $search_param = @{$msg_hash->{$header}}[0];
644 my $search_value = @{$msg_hash->{$search_param}}[0];
645 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
646 }
648 #=== FUNCTION ================================================================
649 # NAME: new_ldap_config
650 # PARAMETERS: address - string - ip address and port of a host
651 # RETURNS: nothing
652 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
653 #===============================================================================
654 sub new_ldap_config {
655 my ($address) = @_ ;
657 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress='$address'";
658 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
660 # check hit
661 my $hit_counter = keys %{$res};
662 if( not $hit_counter == 1 ) {
663 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
664 }
666 $address = $res->{1}->{hostname};
667 my $macaddress = $res->{1}->{macaddress};
668 my $hostkey = $res->{1}->{hostkey};
670 if (not defined $macaddress) {
671 &main::daemon_log("ERROR: no mac address found for client $address", 1);
672 return;
673 }
675 # Build LDAP connection
676 my $ldap = Net::LDAP->new($ldap_uri);
677 if( not defined $ldap ) {
678 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
679 return;
680 }
683 # Bind to a directory with dn and password
684 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
686 # Perform search
687 $mesg = $ldap->search( base => $ldap_base,
688 scope => 'sub',
689 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
690 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
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 mac address $macaddress not found/not unique in ldap search", 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))", 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 = $ldap->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 =~ /^ldap[^:]+:\/\// ) {
757 if ($server =~ /^([^:]+):(.*)$/ ) {
758 $server= "1:dummy:ldap://$1/$2";
759 }
760 }
762 $base= $server;
763 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
764 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
765 push (@ldap_uris, $server);
766 }
768 # Assemble data package
769 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
770 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
771 if (defined $release){
772 $data{'release'}= $release;
773 }
775 # Need to append GOto settings?
776 if (defined $goto_admin and defined $goto_secret){
777 $data{'goto_admin'}= $goto_admin;
778 $data{'goto_secret'}= $goto_secret;
779 }
781 # Append unit tag if needed
782 if (defined $unit_tag){
784 # Find admin base and department name
785 $mesg = $ldap->search( base => $ldap_base,
786 scope => 'sub',
787 attrs => ['dn', 'ou'],
788 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
789 #$mesg->code && die $mesg->error;
790 if($mesg->code) {
791 &main::daemon_log($mesg->error, 1);
792 return;
793 }
795 # Sanity check
796 if ($mesg->count != 1) {
797 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
798 return;
799 }
801 $entry= $mesg->entry(0);
802 $data{'admin_base'}= $entry->dn;
803 $data{'department'}= $entry->get_value("ou");
805 # Append unit Tag
806 $data{'unit_tag'}= $unit_tag;
807 }
810 # Unbind
811 $mesg = $ldap->unbind;
812 if($mesg->code) {
813 &main::daemon_log($mesg->error, 1);
814 return;
815 }
817 # Send information
818 return &build_msg("new_ldap_config", $server_address, $address, \%data);
819 }
822 #=== FUNCTION ================================================================
823 # NAME: hardware_config
824 # PARAMETERS: address - string - ip address and port of a host
825 # RETURNS:
826 # DESCRIPTION:
827 #===============================================================================
828 sub hardware_config {
829 my ($address, $gotoHardwareChecksum) = @_ ;
831 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
832 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
834 # check hit
835 my $hit_counter = keys %{$res};
836 if( not $hit_counter == 1 ) {
837 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
838 }
840 my $macaddress = $res->{1}->{macaddress};
841 my $hostkey = $res->{1}->{hostkey};
843 if (not defined $macaddress) {
844 &main::daemon_log("ERROR: no mac address found for client $address", 1);
845 return;
846 }
848 # Build LDAP connection
849 my $ldap = Net::LDAP->new($ldap_uri);
850 if( not defined $ldap ) {
851 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
852 return;
853 }
855 # Bind to a directory with dn and password
856 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
858 # Perform search
859 $mesg = $ldap->search(
860 base => $ldap_base,
861 scope => 'sub',
862 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
863 );
865 if($mesg->count() == 0) {
866 &main::daemon_log("Host was not found in LDAP!", 1);
867 } else {
868 my $entry= $mesg->entry(0);
869 my $dn= $entry->dn;
870 if(defined($entry->get_value("gotoHardwareChecksum"))) {
871 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
872 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
873 if($entry->update($ldap)) {
874 &main::daemon_log("Hardware changed! Detection triggered.", 4);
875 }
876 } else {
877 # Nothing to do
878 return;
879 }
880 }
881 }
883 # Assemble data package
884 my %data = ();
886 # Need to append GOto settings?
887 if (defined $goto_admin and defined $goto_secret){
888 $data{'goto_admin'}= $goto_admin;
889 $data{'goto_secret'}= $goto_secret;
890 }
892 # Unbind
893 $mesg = $ldap->unbind;
895 &main::daemon_log("Send detect_hardware message to $address", 4);
897 # Send information
898 return &build_msg("detect_hardware", $server_address, $address, \%data);
899 }
901 sub server_matches {
902 my $target = shift;
903 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
904 my $result = 0;
906 if($server_ip eq $target_ip) {
907 $result= 1;
908 } elsif ($server_ip eq "0.0.0.0") {
909 if ($target_ip eq "127.0.0.1") {
910 $result= 1;
911 } else {
912 my $PROC_NET_ROUTE= ('/proc/net/route');
914 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
915 or die "Could not open $PROC_NET_ROUTE";
917 my @ifs = <PROC_NET_ROUTE>;
919 close(PROC_NET_ROUTE);
921 # Eat header line
922 shift @ifs;
923 chomp @ifs;
924 foreach my $line(@ifs) {
925 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
926 my $destination;
927 my $mask;
928 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
929 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
930 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
931 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
932 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
933 # destination matches route, save mac and exit
934 $result= 1;
935 last;
936 }
937 }
938 }
939 } else {
940 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
941 }
943 return $result;
944 }
946 1;