1 package ClientPackages;
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.
8 use strict;
9 use warnings;
10 use GOSA::GosaSupportDaemon;
11 use IO::Socket::INET;
12 use XML::Simple;
13 use Data::Dumper;
14 use NetAddr::IP;
15 use Net::LDAP;
16 use Socket;
17 use Net::hostent;
19 my $event_dir = "/usr/lib/gosa-si/server/ClientPackages";
20 use lib "/usr/lib/gosa-si/server/ClientPackages";
22 BEGIN{}
23 END {}
25 my ($server_ip, $server_port, $ClientPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
26 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
27 my $server;
28 my $network_interface;
29 my $no_bus;
30 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
31 my $mesg;
33 my %cfg_defaults = (
34 "bus" => {
35 "activ" => [\$bus_activ, "on"],
36 "key" => [\$bus_key, ""],
37 "ip" => [\$bus_ip, ""],
38 "port" => [\$bus_port, "20080"],
39 },
40 "server" => {
41 "ip" => [\$server_ip, "0.0.0.0"],
42 "mac-address" => [\$main::server_mac_address, "00:00:00:00:00"],
43 "port" => [\$server_port, "20081"],
44 "ldap-uri" => [\$ldap_uri, ""],
45 "ldap-base" => [\$ldap_base, ""],
46 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
47 "ldap-admin-password" => [\$ldap_admin_password, ""],
48 "max-clients" => [\$max_clients, 100],
49 },
50 "ClientPackages" => {
51 "key" => [\$ClientPackages_key, ""],
52 },
53 );
55 ### START #####################################################################
57 # read configfile and import variables
58 &read_configfile();
61 # if server_ip is not an ip address but a name
62 if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
63 $network_interface= &get_interface_for_ip($server_ip);
64 $main::server_mac_address= &get_mac($network_interface);
67 # import local events
68 my ($error, $result, $event_hash) = &import_events($event_dir);
69 if ($error == 0) {
70 foreach my $log_line (@$result) {
71 &main::daemon_log("0 DEBUG: ClientPackages - $log_line", 7);
72 }
73 } else {
74 foreach my $log_line (@$result) {
75 &main::daemon_log("0 ERROR: ClientPackages - $log_line", 1);
76 }
78 }
80 # Unit tag can be defined in config
81 if((not defined($main::gosa_unit_tag)) || length($main::gosa_unit_tag) == 0) {
82 # Read gosaUnitTag from LDAP
84 my $ldap_handle = &main::get_ldap_handle();
85 if( defined($ldap_handle) ) {
86 &main::daemon_log("0 INFO: Searching for servers gosaUnitTag with mac address $main::server_mac_address",5);
87 # Perform search for Unit Tag
88 $mesg = $ldap_handle->search(
89 base => $ldap_base,
90 scope => 'sub',
91 attrs => ['gosaUnitTag'],
92 filter => "(macaddress=$main::server_mac_address)"
93 );
95 if ((! $main::server_mac_address eq "00:00:00:00:00:00") and $mesg->count == 1) {
96 my $entry= $mesg->entry(0);
97 my $unit_tag= $entry->get_value("gosaUnitTag");
98 $main::ldap_server_dn= $mesg->entry(0)->dn;
99 if(defined($unit_tag) && length($unit_tag) > 0) {
100 &main::daemon_log("0 INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
101 $main::gosa_unit_tag= $unit_tag;
102 }
103 } else {
104 # Perform another search for Unit Tag
105 my $hostname= `hostname -f`;
106 chomp($hostname);
107 &main::daemon_log("0 INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
108 $mesg = $ldap_handle->search(
109 base => $ldap_base,
110 scope => 'sub',
111 attrs => ['gosaUnitTag'],
112 filter => "(&(cn=$hostname)(objectClass=goServer))"
113 );
114 if ($mesg->count == 1) {
115 my $entry= $mesg->entry(0);
116 my $unit_tag= $entry->get_value("gosaUnitTag");
117 $main::ldap_server_dn= $mesg->entry(0)->dn;
118 if(defined($unit_tag) && length($unit_tag) > 0) {
119 &main::daemon_log("0 INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
120 $main::gosa_unit_tag= $unit_tag;
121 }
122 } else {
123 # Perform another search for Unit Tag
124 $hostname= `hostname -s`;
125 chomp($hostname);
126 &main::daemon_log("0 INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
127 $mesg = $ldap_handle->search(
128 base => $ldap_base,
129 scope => 'sub',
130 attrs => ['gosaUnitTag'],
131 filter => "(&(cn=$hostname)(objectClass=goServer))"
132 );
133 if ($mesg->count == 1) {
134 my $entry= $mesg->entry(0);
135 my $unit_tag= $entry->get_value("gosaUnitTag");
136 $main::ldap_server_dn= $mesg->entry(0)->dn;
137 if(defined($unit_tag) && length($unit_tag) > 0) {
138 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
139 $main::gosa_unit_tag= $unit_tag;
140 }
141 } else {
142 &main::daemon_log("0 WARNING: No gosaUnitTag detected. Not using gosaUnitTag", 3);
143 }
144 }
145 }
146 } else {
147 &main::daemon_log("0 INFO: Using gosaUnitTag from config-file: $main::gosa_unit_tag",5);
148 }
149 }
152 my $server_address = "$server_ip:$server_port";
153 $main::server_address = $server_address;
156 if( inet_aton($bus_ip) ){ $bus_ip = inet_ntoa(inet_aton($bus_ip)); }
157 ######################################################
158 # to change
159 if( $bus_ip eq "127.0.1.1" ) { $bus_ip = "127.0.0.1" }
160 ######################################################
161 my $bus_address = "$bus_ip:$bus_port";
162 $main::bus_address = $bus_address;
165 my $hostkey = &create_passwd;
166 my $res = $main::known_server_db->add_dbentry( {table=>$main::known_server_tn,
167 primkey=>['hostname'],
168 hostname=>$main::server_address,
169 status=>'myself',
170 hostkey=>$hostkey,
171 timestamp=>&get_time(),
172 } );
173 if (not $res == 0) {
174 &main::daemon_log("0 ERROR: cannot add server to known_server_db: $res", 1);
175 } else {
176 &main::daemon_log("0 INFO: '$main::server_address' successfully added to known_server_db", 5);
177 }
181 ## create general settings for this module
182 #my $xml = new XML::Simple();
183 #
184 ## register at bus
185 #if ($main::no_bus > 0) {
186 # $bus_activ = "off"
187 #}
188 #if($bus_activ eq "on") {
189 # ®ister_at_bus();
190 #}
193 ### functions #################################################################
196 sub get_module_info {
197 my @info = ($server_address,
198 $ClientPackages_key,
199 );
200 return \@info;
201 }
204 #=== FUNCTION ================================================================
205 # NAME: read_configfile
206 # PARAMETERS: cfg_file - string -
207 # RETURNS: nothing
208 # DESCRIPTION: read cfg_file and set variables
209 #===============================================================================
210 sub read_configfile {
211 my $cfg;
212 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
213 if( -r $main::cfg_file ) {
214 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
215 } else {
216 print STDERR "Couldn't read config file!";
217 }
218 } else {
219 $cfg = Config::IniFiles->new() ;
220 }
221 foreach my $section (keys %cfg_defaults) {
222 foreach my $param (keys %{$cfg_defaults{ $section }}) {
223 my $pinfo = $cfg_defaults{ $section }{ $param };
224 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
225 }
226 }
228 # Read non predefined sections
229 my $param;
230 if ($cfg->SectionExists('ldap')){
231 foreach $param ($cfg->Parameters('ldap')){
232 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
233 }
234 }
235 if ($cfg->SectionExists('pam_ldap')){
236 foreach $param ($cfg->Parameters('pam_ldap')){
237 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
238 }
239 }
240 if ($cfg->SectionExists('nss_ldap')){
241 foreach $param ($cfg->Parameters('nss_ldap')){
242 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
243 }
244 }
245 if ($cfg->SectionExists('goto')){
246 $goto_admin= $cfg->val('goto', 'terminal_admin');
247 $goto_secret= $cfg->val('goto', 'terminal_secret');
248 } else {
249 $goto_admin= undef;
250 $goto_secret= undef;
251 }
253 }
255 #=== FUNCTION ================================================================
256 # NAME: get_interface_for_ip
257 # PARAMETERS: ip address (i.e. 192.168.0.1)
258 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
259 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
260 #===============================================================================
261 sub get_interface_for_ip {
262 my $result;
263 my $ip= shift;
264 if ($ip && length($ip) > 0) {
265 my @ifs= &get_interfaces();
266 if($ip eq "0.0.0.0") {
267 $result = "all";
268 } else {
269 foreach (@ifs) {
270 my $if=$_;
271 if(&main::get_ip($if) eq $ip) {
272 $result = $if;
273 }
274 }
275 }
276 }
277 return $result;
278 }
280 #=== FUNCTION ================================================================
281 # NAME: get_interfaces
282 # PARAMETERS: none
283 # RETURNS: (list of interfaces)
284 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
285 #===============================================================================
286 sub get_interfaces {
287 my @result;
288 my $PROC_NET_DEV= ('/proc/net/dev');
290 open(PROC_NET_DEV, "<$PROC_NET_DEV")
291 or die "Could not open $PROC_NET_DEV";
293 my @ifs = <PROC_NET_DEV>;
295 close(PROC_NET_DEV);
297 # Eat first two line
298 shift @ifs;
299 shift @ifs;
301 chomp @ifs;
302 foreach my $line(@ifs) {
303 my $if= (split /:/, $line)[0];
304 $if =~ s/^\s+//;
305 push @result, $if;
306 }
308 return @result;
309 }
311 #=== FUNCTION ================================================================
312 # NAME: get_mac
313 # PARAMETERS: interface name (i.e. eth0)
314 # RETURNS: (mac address)
315 # DESCRIPTION: Uses ioctl to get mac address directly from system.
316 #===============================================================================
317 sub get_mac {
318 my $ifreq= shift;
319 my $result;
320 if ($ifreq && length($ifreq) > 0) {
321 if($ifreq eq "all") {
322 $result = "00:00:00:00:00:00";
323 } else {
324 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
326 # A configured MAC Address should always override a guessed value
327 if ($main::server_mac_address and length($main::server_mac_address) > 0) {
328 $result= $main::server_mac_address;
329 }
331 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
332 or die "socket: $!";
334 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
335 my ($if, $mac)= unpack 'h36 H12', $ifreq;
337 if (length($mac) > 0) {
338 $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])$/;
339 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
340 $result = $mac;
341 }
342 }
343 }
344 }
345 return $result;
346 }
349 #=== FUNCTION ================================================================
350 # NAME: register_at_bus
351 # PARAMETERS: nothing
352 # RETURNS: nothing
353 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
354 #===============================================================================
355 sub register_at_bus {
357 # add bus to known_server_db
358 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
359 primkey=>['hostname'],
360 hostname=>$bus_address,
361 status=>'bus',
362 hostkey=>$bus_key,
363 timestamp=>&get_time,
364 } );
365 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
366 my $msg = &create_xml_string($msg_hash);
368 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
369 return $msg;
370 }
374 # outcommented from rettenbe: moved to GosaSupportDaemon.pm
375 #sub import_events {
376 # if (not -e $event_dir) {
377 # &main::daemon_log("S ERROR: cannot find directory or directory is not readable: $event_dir", 1);
378 # }
379 # opendir (DIR, $event_dir) or die "ERROR while loading gosa-si-events from directory $event_dir : $!\n";
380 #
381 # while (defined (my $event = readdir (DIR))) {
382 # if( $event eq "." || $event eq ".." ) { next; }
383 # if( $event eq "gosaTriggered.pm" ) { next; } # only GOsa specific events
384 #
385 # eval{ require $event; };
386 # if( $@ ) {
387 # &main::daemon_log("S ERROR: import of event module '$event' failed", 1);
388 # &main::daemon_log("$@", 8);
389 # next;
390 # }
391 #
392 # $event =~ /(\S*?).pm$/;
393 # my $event_module = $1;
394 # my $events_l = eval( $1."::get_events()") ;
395 # foreach my $event_name (@{$events_l}) {
396 # $event_hash->{$event_name} = $event_module;
397 # }
398 # my $events_string = join( ", ", @{$events_l});
399 # &main::daemon_log("S DEBUG: ClientPackages imported events $events_string", 8);
400 # }
401 #}
404 #=== FUNCTION ================================================================
405 # NAME: process_incoming_msg
406 # PARAMETERS: crypted_msg - string - incoming crypted message
407 # RETURNS: nothing
408 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
409 #===============================================================================
410 sub process_incoming_msg {
411 my ($msg, $msg_hash, $session_id) = @_ ;
412 my $error = 0;
413 my $host_name;
414 my $host_key;
415 my @out_msg_l = ("nohandler");
416 my $sql_events;
418 # process incoming msg
419 my $header = @{$msg_hash->{header}}[0];
420 my $source = @{$msg_hash->{source}}[0];
421 my @target_l = @{$msg_hash->{target}};
423 # skip PREFIX
424 $header =~ s/^CLMSG_//;
426 &main::daemon_log("$session_id DEBUG: ClientPackages: msg to process: $header", 7);
428 if( 0 == length @target_l){
429 &main::daemon_log("$session_id ERROR: no target specified for msg $header", 1);
430 $error++;
431 }
433 if( 1 == length @target_l) {
434 my $target = $target_l[0];
435 if(&server_matches($target)) {
436 if ($header eq 'new_key') {
437 @out_msg_l = &new_key($msg_hash)
438 } elsif ($header eq 'here_i_am') {
439 @out_msg_l = &here_i_am($msg, $msg_hash, $session_id)
440 } else {
441 # a event exists with the header as name
442 if( exists $event_hash->{$header} ) {
443 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
444 no strict 'refs';
445 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
447 # if no event handler is implemented
448 } else {
449 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$source') OR (hostname='$source') )";
450 my $res = $main::known_clients_db->select_dbentry( $sql_events );
451 my $l = keys(%$res);
453 # set error if no or more than 1 hits are found for sql query
454 if ( $l != 1) {
455 @out_msg_l = ('knownclienterror');
457 # found exact 1 hit in db
458 } else {
459 my $client_events = $res->{'1'}->{'events'};
461 # client is registered for this event, deliver this message to client
462 $header =~ s/^answer_//;
463 if ($client_events =~ /,$header,/) {
464 # answer message finally arrived destination server, so forward messages to GOsa
465 if ($target eq $main::server_address) {
466 $msg =~ s/<header>answer_/<header>/;
467 $msg =~ s/<target>\S+<\/target>/<target>GOSA<\/target>/;
468 }
469 @out_msg_l = ( $msg );
471 # client is not registered for this event, set error
472 } else {
473 @out_msg_l = ('noeventerror');
474 }
475 }
476 }
477 }
479 # if delivery not possible raise error and return
480 if( not defined $out_msg_l[0] ) {
481 @out_msg_l = ();
482 } elsif( $out_msg_l[0] eq 'nohandler') {
483 &main::daemon_log("$session_id ERROR: ClientPackages: no event handler or core function defined for '$header'", 1);
484 @out_msg_l = ();
485 } elsif ($out_msg_l[0] eq 'knownclienterror') {
486 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
487 &main::daemon_log("$session_id WARNING: processing is aborted and message will not be forwarded");
488 @out_msg_l = ();
489 } elsif ($out_msg_l[0] eq 'noeventerror') {
490 &main::daemon_log("$session_id WARNING: client '$target' is not registered for event '$header', processing is aborted", 1);
491 @out_msg_l = ();
492 }
497 }
498 else {
499 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
500 push(@out_msg_l, $msg);
501 }
502 }
504 return \@out_msg_l;
505 }
508 #=== FUNCTION ================================================================
509 # NAME: new_passwd
510 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
511 # RETURNS: nothing
512 # DESCRIPTION: process this incoming message
513 #===============================================================================
514 sub new_key {
515 my ($msg_hash) = @_;
516 my @out_msg_l;
518 my $header = @{$msg_hash->{header}}[0];
519 my $source_name = @{$msg_hash->{source}}[0];
520 my $source_key = @{$msg_hash->{new_key}}[0];
521 my $query_res;
523 # check known_clients_db
524 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
525 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
526 if( 1 == keys %{$query_res} ) {
527 my $act_time = &get_time;
528 my $sql_statement= "UPDATE known_clients ".
529 "SET hostkey='$source_key', timestamp='$act_time' ".
530 "WHERE hostname='$source_name'";
531 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
532 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
533 my $out_msg = &create_xml_string($hash);
534 push(@out_msg_l, $out_msg);
535 }
537 # only do if host still not found
538 if( 0 == @out_msg_l ) {
539 # check known_server_db
540 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
541 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
542 if( 1 == keys %{$query_res} ) {
543 my $act_time = &get_time;
544 my $sql_statement= "UPDATE known_server ".
545 "SET hostkey='$source_key', timestamp='$act_time' ".
546 "WHERE hostname='$source_name'";
547 my $res = $main::known_server_db->update_dbentry( $sql_statement );
549 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
550 my $out_msg = &create_xml_string($hash);
551 push(@out_msg_l, $out_msg);
552 }
553 }
555 return @out_msg_l;
556 }
559 #=== FUNCTION ================================================================
560 # NAME: here_i_am
561 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
562 # RETURNS: nothing
563 # DESCRIPTION: process this incoming message
564 #===============================================================================
565 sub here_i_am {
566 my ($msg, $msg_hash, $session_id) = @_;
567 my @out_msg_l;
568 my $out_hash;
569 my $source = @{$msg_hash->{source}}[0];
570 my $mac_address = @{$msg_hash->{mac_address}}[0];
571 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
572 my $client_status = @{$msg_hash->{client_status}}[0];
573 my $client_revision = @{$msg_hash->{client_revision}}[0];
574 my $key_lifetime = @{$msg_hash->{key_lifetime}}[0];
576 # number of known clients
577 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
579 # check wether client address or mac address is already known
580 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
581 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
583 if ( 1 == keys %{$db_res} ) {
584 &main::daemon_log("$session_id WARNING: $source is already known as a client", 1);
585 &main::daemon_log("$session_id WARNING: values for $source are being overwritten", 1);
586 $nu_clients --;
587 }
589 # number of actual activ clients
590 my $act_nu_clients = $nu_clients;
592 &main::daemon_log("$session_id INFO: number of actual activ clients: $act_nu_clients", 5);
593 &main::daemon_log("$session_id INFO: number of maximal allowed clients: $max_clients", 5);
595 if($max_clients <= $act_nu_clients) {
596 my $out_hash = &create_xml_hash("denied", $server_address, $source);
597 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
598 my $passwd = @{$msg_hash->{new_passwd}}[0];
599 &send_msg_hash2address($out_hash, $source, $passwd);
600 return;
601 }
603 # new client accepted
604 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
606 # add entry to known_clients_db
607 my $events = @{$msg_hash->{events}}[0];
608 my $act_timestamp = &get_time;
609 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
610 primkey=>['hostname'],
611 hostname=>$source,
612 events=>$events,
613 macaddress=>$mac_address,
614 status=>'registered',
615 hostkey=>$new_passwd,
616 timestamp=>$act_timestamp,
617 keylifetime=>$key_lifetime,
618 } );
620 if ($res != 0) {
621 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $res");
622 return;
623 }
625 # return acknowledgement to client
626 $out_hash = &create_xml_hash("registered", $server_address, $source);
631 # # notify registered client to all other server
632 # if( $bus_activ eq "on") {
633 # # fetch actual bus key
634 # my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
635 # my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
636 # my $hostkey = $query_res->{1}->{'hostkey'};
637 #
638 # # send update msg to bus
639 # $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
640 # &add_content2xml_hash($out_hash, "macaddress", $mac_address);
641 # &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
642 # my $new_client_out = &create_xml_string($out_hash);
643 # push(@out_msg_l, $new_client_out);
644 # &main::daemon_log("$session_id INFO: send bus msg that client '$source' has registered at server '$server_address'", 5);
645 # }
647 # give the new client his ldap config
648 # Workaround: Send within the registration response, if the client will get an ldap config later
649 my $new_ldap_config_out = &new_ldap_config($source, $session_id);
650 if($new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
651 &add_content2xml_hash($out_hash, "ldap_available", "true");
652 } elsif($new_ldap_config_out && $new_ldap_config_out =~ /error/){
653 &add_content2xml_hash($out_hash, "error", $new_ldap_config_out);
655 my $sql_statement = "UPDATE $main::job_queue_tn ".
656 "SET status='error', result='$new_ldap_config_out' ".
657 "WHERE status='processing' AND macaddress LIKE '$mac_address'";
658 my $res = $main::job_db->update_dbentry($sql_statement);
659 &main::daemon_log("$session_id DEBUG: $sql_statement RESULT: $res", 7);
660 }
661 my $register_out = &create_xml_string($out_hash);
662 push(@out_msg_l, $register_out);
664 # Really send the ldap config
665 if( $new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
666 push(@out_msg_l, $new_ldap_config_out);
667 }
669 my $hardware_config_out = &hardware_config($msg, $msg_hash, $session_id);
670 if( $hardware_config_out ) {
671 push(@out_msg_l, $hardware_config_out);
672 }
674 # notify registered client to all other server
675 my %mydata = ( 'client' => $source, 'macaddress' => $mac_address);
676 my $mymsg = &build_msg('new_foreign_client', $main::server_address, "KNOWN_SERVER", \%mydata);
677 push(@out_msg_l, $mymsg);
679 &main::daemon_log("$session_id INFO: register client $source ($mac_address)", 5);
680 &main::daemon_log("$session_id INFO: client version: $client_status - $client_revision", 5);
681 return @out_msg_l;
682 }
685 #=== FUNCTION ================================================================
686 # NAME: who_has
687 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
688 # RETURNS: nothing
689 # DESCRIPTION: process this incoming message
690 #===============================================================================
691 sub who_has {
692 my ($msg_hash) = @_ ;
693 my @out_msg_l;
695 # what is your search pattern
696 my $search_pattern = @{$msg_hash->{who_has}}[0];
697 my $search_element = @{$msg_hash->{$search_pattern}}[0];
698 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
700 # scanning known_clients for search_pattern
701 my @host_addresses = keys %$main::known_clients;
702 my $known_clients_entries = length @host_addresses;
703 my $host_address;
704 foreach my $host (@host_addresses) {
705 my $client_element = $main::known_clients->{$host}->{$search_pattern};
706 if ($search_element eq $client_element) {
707 $host_address = $host;
708 last;
709 }
710 }
712 # search was successful
713 if (defined $host_address) {
714 my $source = @{$msg_hash->{source}}[0];
715 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
716 &add_content2xml_hash($out_hash, "mac_address", $search_element);
717 my $out_msg = &create_xml_string($out_hash);
718 push(@out_msg_l, $out_msg);
719 }
720 return @out_msg_l;
721 }
724 sub who_has_i_do {
725 my ($msg_hash) = @_ ;
726 my $header = @{$msg_hash->{header}}[0];
727 my $source = @{$msg_hash->{source}}[0];
728 my $search_param = @{$msg_hash->{$header}}[0];
729 my $search_value = @{$msg_hash->{$search_param}}[0];
730 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
731 }
734 #=== FUNCTION ================================================================
735 # NAME: new_ldap_config
736 # PARAMETERS: address - string - ip address and port of a host
737 # RETURNS: gosa-si conform message
738 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
739 #===============================================================================
740 sub new_ldap_config {
741 my ($address, $session_id) = @_ ;
743 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress LIKE '$address'";
744 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
746 # check hit
747 my $hit_counter = keys %{$res};
748 if( not $hit_counter == 1 ) {
749 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query '$sql_statement'", 1);
750 }
752 $address = $res->{1}->{hostname};
753 my $macaddress = $res->{1}->{macaddress};
754 my $hostkey = $res->{1}->{hostkey};
756 if (not defined $macaddress) {
757 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
758 return;
759 }
761 # Build LDAP connection
762 my $ldap_handle = &main::get_ldap_handle($session_id);
763 if( not defined $ldap_handle ) {
764 &main::daemon_log("$session_id ERROR: cannot connect to ldap: $ldap_uri", 1);
765 return;
766 }
768 # Perform search
769 $mesg = $ldap_handle->search( base => $ldap_base,
770 scope => 'sub',
771 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
772 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
773 if($mesg->code) {
774 &main::daemon_log("$session_id ".$mesg->error, 1);
775 return;
776 }
778 # Sanity check
779 if ($mesg->count != 1) {
780 &main::daemon_log("$session_id WARNING: client with mac address $macaddress not found/unique/active - not sending ldap config".
781 "\n\tbase: $ldap_base".
782 "\n\tscope: sub".
783 "\n\tattrs: dn, gotoLdapServer".
784 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
785 return;
786 }
788 my $entry= $mesg->entry(0);
789 my $dn= $entry->dn;
790 my @servers= $entry->get_value("gotoLdapServer");
791 my $unit_tag= $entry->get_value("gosaUnitTag");
792 my @ldap_uris;
793 my $server;
794 my $base;
795 my $release;
797 # Fill release if available
798 my $FAIclass= $entry->get_value("FAIclass");
799 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
800 $release= $1;
801 }
803 # Do we need to look at an object class?
804 if (not @servers){
805 $mesg = $ldap_handle->search( base => $ldap_base,
806 scope => 'sub',
807 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
808 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
809 #$mesg->code && die $mesg->error;
810 if($mesg->code) {
811 &main::daemon_log("$session_id ".$mesg->error, 1);
812 return;
813 }
815 # Sanity check
816 if ($mesg->count != 1) {
817 &main::daemon_log("$session_id WARNING: no LDAP information found for client mac $macaddress", 1);
818 return;
819 }
821 $entry= $mesg->entry(0);
822 $dn= $entry->dn;
823 @servers= $entry->get_value("gotoLdapServer");
825 if (not defined $release){
826 $FAIclass= $entry->get_value("FAIclass");
827 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
828 $release= $1;
829 }
830 }
831 }
833 @servers= sort (@servers);
835 foreach $server (@servers){
836 # Conversation for backward compatibility
837 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
838 if ($server =~ /^([^:]+):([^:]+)$/ ) {
839 $server= "1:dummy:ldap://$1/$2";
840 } elsif ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
841 $server= "$1:dummy:ldap://$2/$3";
842 }
843 }
845 $base= $server;
846 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
847 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
848 push (@ldap_uris, $server);
849 }
851 # Assemble data package
852 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
853 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
854 if (defined $release){
855 $data{'release'}= $release;
856 }
858 # Need to append GOto settings?
859 if (defined $goto_admin and defined $goto_secret){
860 $data{'goto_admin'}= $goto_admin;
861 $data{'goto_secret'}= $goto_secret;
862 }
864 # Append unit tag if needed
865 if (defined $unit_tag){
867 # Find admin base and department name
868 $mesg = $ldap_handle->search( base => $ldap_base,
869 scope => 'sub',
870 attrs => ['dn', 'ou'],
871 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
872 #$mesg->code && die $mesg->error;
873 if($mesg->code) {
874 &main::daemon_log($mesg->error, 1);
875 return "error-unit-tag-count-0";
876 }
878 # Sanity check
879 if ($mesg->count != 1) {
880 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
881 return "error-unit-tag-count-".$mesg->count;
882 }
884 $entry= $mesg->entry(0);
885 $data{'admin_base'}= $entry->dn;
886 $data{'department'}= $entry->get_value("ou");
888 # Append unit Tag
889 $data{'unit_tag'}= $unit_tag;
890 }
892 # Send information
893 return &build_msg("new_ldap_config", $server_address, $address, \%data);
894 }
897 #=== FUNCTION ================================================================
898 # NAME: hardware_config
899 # PARAMETERS: address - string - ip address and port of a host
900 # RETURNS:
901 # DESCRIPTION:
902 #===============================================================================
903 sub hardware_config {
904 my ($msg, $msg_hash, $session_id) = @_ ;
905 my $address = @{$msg_hash->{source}}[0];
906 my $header = @{$msg_hash->{header}}[0];
907 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
909 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
910 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
912 # check hit
913 my $hit_counter = keys %{$res};
914 if( not $hit_counter == 1 ) {
915 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
916 }
917 my $macaddress = $res->{1}->{macaddress};
918 my $hostkey = $res->{1}->{hostkey};
920 if (not defined $macaddress) {
921 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
922 return;
923 }
925 # Build LDAP connection
926 my $ldap_handle = &main::get_ldap_handle($session_id);
927 if( not defined $ldap_handle ) {
928 &main::daemon_log("$session_id ERROR: cannot connect to ldap: $ldap_uri", 1);
929 return;
930 }
932 # Perform search
933 $mesg = $ldap_handle->search(
934 base => $ldap_base,
935 scope => 'sub',
936 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
937 );
939 if($mesg->count() == 0) {
940 &main::daemon_log("Host was not found in LDAP!", 1);
942 # set status = hardware_detection at jobqueue if entry exists
943 # TODO
944 # resolve plain name for host
945 my $func_dic = {table=>$main::job_queue_tn,
946 primkey=>[],
947 timestamp=>&get_time,
948 status=>'processing',
949 result=>'none',
950 progress=>'hardware-detection',
951 headertag=>'trigger_action_reinstall',
952 targettag=>$address,
953 xmlmessage=>'none',
954 macaddress=>$macaddress,
955 plainname=>'none',
956 };
957 my $hd_res = $main::job_db->add_dbentry($func_dic);
958 &main::daemon_log("$session_id INFO: add '$macaddress' to job queue as an installing job", 5);
960 } else {
961 my $entry= $mesg->entry(0);
962 my $dn= $entry->dn;
963 if (defined($entry->get_value("gotoHardwareChecksum"))) {
964 if (! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
965 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
966 if($entry->update($ldap_handle)) {
967 &main::daemon_log("$session_id INFO: Hardware changed! Detection triggered.", 5);
968 }
969 } else {
970 # Nothing to do
971 return;
972 }
973 }
974 }
976 # Assemble data package
977 my %data = ();
979 # Need to append GOto settings?
980 if (defined $goto_admin and defined $goto_secret){
981 $data{'goto_admin'}= $goto_admin;
982 $data{'goto_secret'}= $goto_secret;
983 }
985 # Send information
986 return &build_msg("detect_hardware", $server_address, $address, \%data);
987 }
989 sub server_matches {
990 my $target = shift;
991 my $target_ip = ($1) if $target =~ /^([0-9\.]*?):.*$/;
992 if(!defined($target_ip) or length($target_ip) == 0) {
993 return;
994 }
996 my $result = 0;
998 if($server_ip eq $target_ip) {
999 $result= 1;
1000 } elsif ($target_ip eq "0.0.0.0") {
1001 $result= 1;
1002 } elsif ($server_ip eq "0.0.0.0") {
1003 if ($target_ip eq "127.0.0.1") {
1004 $result= 1;
1005 } else {
1006 my $PROC_NET_ROUTE= ('/proc/net/route');
1008 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1009 or die "Could not open $PROC_NET_ROUTE";
1011 my @ifs = <PROC_NET_ROUTE>;
1013 close(PROC_NET_ROUTE);
1015 # Eat header line
1016 shift @ifs;
1017 chomp @ifs;
1018 foreach my $line(@ifs) {
1019 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1020 my $destination;
1021 my $mask;
1022 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1023 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1024 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1025 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1026 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1027 # destination matches route, save mac and exit
1028 $result= 1;
1029 last;
1030 }
1031 }
1032 }
1033 } else {
1034 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1035 }
1037 return $result;
1038 }
1040 1;