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.
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/events";
20 use lib "/usr/lib/gosa-si/server/events";
22 BEGIN{}
23 END {}
25 my ($server_ip, $server_port, $SIPackages_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 $event_hash;
29 my $network_interface;
30 my $no_bus;
31 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
32 my $mesg;
34 my %cfg_defaults = (
35 "bus" => {
36 "activ" => [\$bus_activ, "on"],
37 "key" => [\$bus_key, ""],
38 "ip" => [\$bus_ip, ""],
39 "port" => [\$bus_port, "20080"],
40 },
41 "server" => {
42 "ip" => [\$server_ip, "0.0.0.0"],
43 "mac-address" => [\$main::server_mac_address, "00:00:00:00:00"],
44 "port" => [\$server_port, "20081"],
45 "ldap-uri" => [\$ldap_uri, ""],
46 "ldap-base" => [\$ldap_base, ""],
47 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
48 "ldap-admin-password" => [\$ldap_admin_password, ""],
49 "max-clients" => [\$max_clients, 100],
50 },
51 "SIPackages" => {
52 "key" => [\$SIPackages_key, ""],
53 },
54 );
56 ### START #####################################################################
58 # read configfile and import variables
59 &read_configfile();
62 # if server_ip is not an ip address but a name
63 if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
64 $network_interface= &get_interface_for_ip($server_ip);
65 $main::server_mac_address= &get_mac($network_interface);
67 &import_events();
69 # Unit tag can be defined in config
70 if((not defined($main::gosa_unit_tag)) || length($main::gosa_unit_tag) == 0) {
71 # Read gosaUnitTag from LDAP
73 my $ldap_handle = &main::get_ldap_handle();
74 if( defined($ldap_handle) ) {
75 &main::daemon_log("INFO: Searching for servers gosaUnitTag with mac address $main::server_mac_address",5);
76 # Perform search for Unit Tag
77 $mesg = $ldap_handle->search(
78 base => $ldap_base,
79 scope => 'sub',
80 attrs => ['gosaUnitTag'],
81 filter => "(macaddress=$main::server_mac_address)"
82 );
84 if ($mesg->count == 1) {
85 my $entry= $mesg->entry(0);
86 my $unit_tag= $entry->get_value("gosaUnitTag");
87 $main::ldap_server_dn= $mesg->entry(0)->dn;
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 = $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 $main::ldap_server_dn= $mesg->entry(0)->dn;
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 = $ldap_handle->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 $main::ldap_server_dn= $mesg->entry(0)->dn;
126 if(defined($unit_tag) && length($unit_tag) > 0) {
127 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
128 $main::gosa_unit_tag= $unit_tag;
129 }
130 } else {
131 &main::daemon_log("WARNING: No gosaUnitTag detected. Not using gosaUnitTag", 3);
132 }
133 }
134 }
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) && ( (-s $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(&main::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 ($main::server_mac_address and length($main::server_mac_address) > 0) {
310 $result= $main::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("S 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("S ERROR: 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("S DEBUG: SIPackages imported events $events_string", 8);
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 = ("nohandler");
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("$session_id DEBUG: SIPackages: msg to process: $header", 7);
406 if( 0 == length @target_l){
407 &main::daemon_log("$session_id ERROR: no target specified for msg $header", 1);
408 $error++;
409 }
411 if( 1 == length @target_l) {
412 my $target = $target_l[0];
413 if(&server_matches($target)) {
416 if ($header eq 'new_key') {
417 @out_msg_l = &new_key($msg_hash)
418 } elsif ($header eq 'here_i_am') {
419 @out_msg_l = &here_i_am($msg, $msg_hash, $session_id)
420 } else {
421 if( exists $event_hash->{$header} ) {
422 # a event exists with the header as name
423 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event_hash->{$header}."'", 5);
424 no strict 'refs';
425 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
426 }
427 }
429 # if delivery not possible raise error and return
430 if( not defined $out_msg_l[0] ) {
431 @out_msg_l = ();
432 } elsif( $out_msg_l[0] eq 'nohandler') {
433 &main::daemon_log("$session_id ERROR: SIPackages: no event handler or core function defined for '$header'", 1);
434 @out_msg_l = ();
435 }
437 }
438 else {
439 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
440 push(@out_msg_l, $msg);
441 }
442 }
444 return \@out_msg_l;
445 }
448 #=== FUNCTION ================================================================
449 # NAME: new_passwd
450 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
451 # RETURNS: nothing
452 # DESCRIPTION: process this incoming message
453 #===============================================================================
454 sub new_key {
455 my ($msg_hash) = @_;
456 my @out_msg_l;
458 my $header = @{$msg_hash->{header}}[0];
459 my $source_name = @{$msg_hash->{source}}[0];
460 my $source_key = @{$msg_hash->{new_key}}[0];
461 my $query_res;
463 # check known_clients_db
464 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
465 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
466 if( 1 == keys %{$query_res} ) {
467 my $act_time = &get_time;
468 my $sql_statement= "UPDATE known_clients ".
469 "SET hostkey='$source_key', timestamp='$act_time' ".
470 "WHERE hostname='$source_name'";
471 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
472 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
473 my $out_msg = &create_xml_string($hash);
474 push(@out_msg_l, $out_msg);
475 }
477 # only do if host still not found
478 if( 0 == @out_msg_l ) {
479 # check known_server_db
480 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
481 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
482 if( 1 == keys %{$query_res} ) {
483 my $act_time = &get_time;
484 my $sql_statement= "UPDATE known_server ".
485 "SET hostkey='$source_key', timestamp='$act_time' ".
486 "WHERE hostname='$source_name'";
487 my $res = $main::known_server_db->update_dbentry( $sql_statement );
489 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
490 my $out_msg = &create_xml_string($hash);
491 push(@out_msg_l, $out_msg);
492 }
493 }
495 return @out_msg_l;
496 }
499 #=== FUNCTION ================================================================
500 # NAME: here_i_am
501 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
502 # RETURNS: nothing
503 # DESCRIPTION: process this incoming message
504 #===============================================================================
505 sub here_i_am {
506 my ($msg, $msg_hash, $session_id) = @_;
507 my @out_msg_l;
508 my $out_hash;
510 my $source = @{$msg_hash->{source}}[0];
511 my $mac_address = @{$msg_hash->{mac_address}}[0];
512 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
513 my $client_status = @{$msg_hash->{client_status}}[0];
514 my $client_revision = @{$msg_hash->{client_revision}}[0];
516 # number of known clients
517 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
519 # check wether client address or mac address is already known
520 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
521 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
523 if ( 1 == keys %{$db_res} ) {
524 &main::daemon_log("$session_id WARNING: $source is already known as a client", 1);
525 &main::daemon_log("$session_id WARNING: values for $source are being overwritten", 1);
526 $nu_clients --;
527 }
529 # number of actual activ clients
530 my $act_nu_clients = $nu_clients;
532 &main::daemon_log("$session_id INFO: number of actual activ clients: $act_nu_clients", 5);
533 &main::daemon_log("$session_id INFO: number of maximal allowed clients: $max_clients", 5);
535 if($max_clients <= $act_nu_clients) {
536 my $out_hash = &create_xml_hash("denied", $server_address, $source);
537 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
538 my $passwd = @{$msg_hash->{new_passwd}}[0];
539 &send_msg_hash2address($out_hash, $source, $passwd);
540 return;
541 }
543 # new client accepted
544 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
546 # create entry in known_clients
547 my $events = @{$msg_hash->{events}}[0];
550 # add entry to known_clients_db
551 my $act_timestamp = &get_time;
552 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
553 primkey=>['hostname'],
554 hostname=>$source,
555 events=>$events,
556 macaddress=>$mac_address,
557 status=>'registered',
558 hostkey=>$new_passwd,
559 timestamp=>$act_timestamp,
560 } );
562 if ($res != 0) {
563 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $res");
564 return;
565 }
567 # return acknowledgement to client
568 $out_hash = &create_xml_hash("registered", $server_address, $source);
570 # notify registered client to bus
571 if( $bus_activ eq "on") {
572 # fetch actual bus key
573 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
574 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
575 my $hostkey = $query_res->{1}->{'hostkey'};
577 # send update msg to bus
578 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
579 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
580 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
581 my $new_client_out = &create_xml_string($out_hash);
582 push(@out_msg_l, $new_client_out);
583 &main::daemon_log("$session_id INFO: send bus msg that client '$source' has registered at server '$server_address'", 5);
584 }
586 # give the new client his ldap config
587 # Workaround: Send within the registration response, if the client will get an ldap config later
588 my $new_ldap_config_out = &new_ldap_config($source, $session_id);
589 if($new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
590 &add_content2xml_hash($out_hash, "ldap_available", "true");
591 } elsif($new_ldap_config_out && $new_ldap_config_out =~ /error/){
592 &add_content2xml_hash($out_hash, "error", $new_ldap_config_out);
594 my $sql_statement = "UPDATE $main::job_queue_tn ".
595 "SET status='error', result='$new_ldap_config_out' ".
596 "WHERE status='processing' AND macaddress LIKE '$mac_address'";
597 my $res = $main::job_db->update_dbentry($sql_statement);
598 &main::daemon_log("$session_id DEBUG: $sql_statement RESULT: $res", 7);
599 }
600 my $register_out = &create_xml_string($out_hash);
601 push(@out_msg_l, $register_out);
603 # Really send the ldap config
604 if( $new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
605 push(@out_msg_l, $new_ldap_config_out);
606 }
608 my $hardware_config_out = &hardware_config($msg, $msg_hash, $session_id);
609 if( $hardware_config_out ) {
610 push(@out_msg_l, $hardware_config_out);
611 }
613 &main::daemon_log("$session_id INFO: register client $source ($mac_address)", 5);
614 &main::daemon_log("$session_id INFO: client version: $client_status - $client_revision", 5);
616 return @out_msg_l;
617 }
620 #=== FUNCTION ================================================================
621 # NAME: who_has
622 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
623 # RETURNS: nothing
624 # DESCRIPTION: process this incoming message
625 #===============================================================================
626 sub who_has {
627 my ($msg_hash) = @_ ;
628 my @out_msg_l;
630 # what is your search pattern
631 my $search_pattern = @{$msg_hash->{who_has}}[0];
632 my $search_element = @{$msg_hash->{$search_pattern}}[0];
633 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
635 # scanning known_clients for search_pattern
636 my @host_addresses = keys %$main::known_clients;
637 my $known_clients_entries = length @host_addresses;
638 my $host_address;
639 foreach my $host (@host_addresses) {
640 my $client_element = $main::known_clients->{$host}->{$search_pattern};
641 if ($search_element eq $client_element) {
642 $host_address = $host;
643 last;
644 }
645 }
647 # search was successful
648 if (defined $host_address) {
649 my $source = @{$msg_hash->{source}}[0];
650 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
651 &add_content2xml_hash($out_hash, "mac_address", $search_element);
652 my $out_msg = &create_xml_string($out_hash);
653 push(@out_msg_l, $out_msg);
654 }
655 return @out_msg_l;
656 }
659 sub who_has_i_do {
660 my ($msg_hash) = @_ ;
661 my $header = @{$msg_hash->{header}}[0];
662 my $source = @{$msg_hash->{source}}[0];
663 my $search_param = @{$msg_hash->{$header}}[0];
664 my $search_value = @{$msg_hash->{$search_param}}[0];
665 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
666 }
669 #=== FUNCTION ================================================================
670 # NAME: new_ldap_config
671 # PARAMETERS: address - string - ip address and port of a host
672 # RETURNS: gosa-si conform message
673 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
674 #===============================================================================
675 sub new_ldap_config {
676 my ($address, $session_id) = @_ ;
678 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress LIKE '$address'";
679 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
681 # check hit
682 my $hit_counter = keys %{$res};
683 if( not $hit_counter == 1 ) {
684 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query '$sql_statement'", 1);
685 }
687 $address = $res->{1}->{hostname};
688 my $macaddress = $res->{1}->{macaddress};
689 my $hostkey = $res->{1}->{hostkey};
691 if (not defined $macaddress) {
692 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
693 return;
694 }
696 # Build LDAP connection
697 my $ldap_handle = &main::get_ldap_handle($session_id);
698 if( not defined $ldap_handle ) {
699 &main::daemon_log("$session_id ERROR: cannot connect to ldap: $ldap_uri", 1);
700 return;
701 }
703 # Perform search
704 $mesg = $ldap_handle->search( base => $ldap_base,
705 scope => 'sub',
706 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
707 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
708 if($mesg->code) {
709 &main::daemon_log("$session_id ".$mesg->error, 1);
710 return;
711 }
713 # Sanity check
714 if ($mesg->count != 1) {
715 &main::daemon_log("$session_id WARNING: client with mac address $macaddress not found/unique/active - not sending ldap config".
716 "\n\tbase: $ldap_base".
717 "\n\tscope: sub".
718 "\n\tattrs: dn, gotoLdapServer".
719 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
720 return;
721 }
723 my $entry= $mesg->entry(0);
724 my $dn= $entry->dn;
725 my @servers= $entry->get_value("gotoLdapServer");
726 my $unit_tag= $entry->get_value("gosaUnitTag");
727 my @ldap_uris;
728 my $server;
729 my $base;
730 my $release;
732 # Fill release if available
733 my $FAIclass= $entry->get_value("FAIclass");
734 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
735 $release= $1;
736 }
738 # Do we need to look at an object class?
739 if (not @servers){
740 $mesg = $ldap_handle->search( base => $ldap_base,
741 scope => 'sub',
742 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
743 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
744 #$mesg->code && die $mesg->error;
745 if($mesg->code) {
746 &main::daemon_log("$session_id ".$mesg->error, 1);
747 return;
748 }
750 # Sanity check
751 if ($mesg->count != 1) {
752 &main::daemon_log("$session_id WARNING: no LDAP information found for client mac $macaddress", 1);
753 return;
754 }
756 $entry= $mesg->entry(0);
757 $dn= $entry->dn;
758 @servers= $entry->get_value("gotoLdapServer");
760 if (not defined $release){
761 $FAIclass= $entry->get_value("FAIclass");
762 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
763 $release= $1;
764 }
765 }
766 }
768 @servers= sort (@servers);
770 foreach $server (@servers){
771 # Conversation for backward compatibility
772 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
773 if ($server =~ /^([^:]+):([^:]+)$/ ) {
774 $server= "1:dummy:ldap://$1/$2";
775 } elsif ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
776 $server= "$1:dummy:ldap://$2/$3";
777 }
778 }
780 $base= $server;
781 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
782 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
783 push (@ldap_uris, $server);
784 }
786 # Assemble data package
787 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
788 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
789 if (defined $release){
790 $data{'release'}= $release;
791 }
793 # Need to append GOto settings?
794 if (defined $goto_admin and defined $goto_secret){
795 $data{'goto_admin'}= $goto_admin;
796 $data{'goto_secret'}= $goto_secret;
797 }
799 # Append unit tag if needed
800 if (defined $unit_tag){
802 # Find admin base and department name
803 $mesg = $ldap_handle->search( base => $ldap_base,
804 scope => 'sub',
805 attrs => ['dn', 'ou'],
806 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
807 #$mesg->code && die $mesg->error;
808 if($mesg->code) {
809 &main::daemon_log($mesg->error, 1);
810 return "error-unit-tag-count-0";
811 }
813 # Sanity check
814 if ($mesg->count != 1) {
815 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
816 return "error-unit-tag-count-".$mesg->count;
817 }
819 $entry= $mesg->entry(0);
820 $data{'admin_base'}= $entry->dn;
821 $data{'department'}= $entry->get_value("ou");
823 # Append unit Tag
824 $data{'unit_tag'}= $unit_tag;
825 }
827 # Send information
828 return &build_msg("new_ldap_config", $server_address, $address, \%data);
829 }
832 #=== FUNCTION ================================================================
833 # NAME: hardware_config
834 # PARAMETERS: address - string - ip address and port of a host
835 # RETURNS:
836 # DESCRIPTION:
837 #===============================================================================
838 sub hardware_config {
839 my ($msg, $msg_hash, $session_id) = @_ ;
840 my $address = @{$msg_hash->{source}}[0];
841 my $header = @{$msg_hash->{header}}[0];
842 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
844 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
845 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
847 # check hit
848 my $hit_counter = keys %{$res};
849 if( not $hit_counter == 1 ) {
850 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
851 }
852 my $macaddress = $res->{1}->{macaddress};
853 my $hostkey = $res->{1}->{hostkey};
855 if (not defined $macaddress) {
856 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
857 return;
858 }
860 # Build LDAP connection
861 my $ldap_handle = &main::get_ldap_handle($session_id);
862 if( not defined $ldap_handle ) {
863 &main::daemon_log("$session_id ERROR: cannot connect to ldap: $ldap_uri", 1);
864 return;
865 }
867 # Perform search
868 $mesg = $ldap_handle->search(
869 base => $ldap_base,
870 scope => 'sub',
871 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
872 );
874 if($mesg->count() == 0) {
875 &main::daemon_log("Host was not found in LDAP!", 1);
877 # set status = hardware_detection at jobqueue if entry exists
878 # TODO
879 # resolve plain name for host
880 my $func_dic = {table=>$main::job_queue_tn,
881 primkey=>[],
882 timestamp=>&get_time,
883 status=>'processing',
884 result=>'none',
885 progress=>'hardware-detection',
886 headertag=>'trigger_action_reinstall',
887 targettag=>$address,
888 xmlmessage=>'none',
889 macaddress=>$macaddress,
890 plainname=>'none',
891 };
892 my $hd_res = $main::job_db->add_dbentry($func_dic);
893 &main::daemon_log("$session_id INFO: add '$macaddress' to job queue as an installing job", 5);
895 } else {
896 my $entry= $mesg->entry(0);
897 my $dn= $entry->dn;
898 if (defined($entry->get_value("gotoHardwareChecksum"))) {
899 if (! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
900 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
901 if($entry->update($ldap_handle)) {
902 &main::daemon_log("$session_id INFO: Hardware changed! Detection triggered.", 5);
903 }
904 } else {
905 # Nothing to do
906 return;
907 }
908 }
909 }
911 # Assemble data package
912 my %data = ();
914 # Need to append GOto settings?
915 if (defined $goto_admin and defined $goto_secret){
916 $data{'goto_admin'}= $goto_admin;
917 $data{'goto_secret'}= $goto_secret;
918 }
920 # Send information
921 return &build_msg("detect_hardware", $server_address, $address, \%data);
922 }
924 sub server_matches {
925 my $target = shift;
926 my $target_ip = ($1) if $target =~ /^([0-9\.]*?):.*$/;
927 if(!defined($target_ip) or length($target_ip) == 0) {
928 return;
929 }
931 my $result = 0;
933 if($server_ip eq $target_ip) {
934 $result= 1;
935 } elsif ($target_ip eq "0.0.0.0") {
936 $result= 1;
937 } elsif ($server_ip eq "0.0.0.0") {
938 if ($target_ip eq "127.0.0.1") {
939 $result= 1;
940 } else {
941 my $PROC_NET_ROUTE= ('/proc/net/route');
943 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
944 or die "Could not open $PROC_NET_ROUTE";
946 my @ifs = <PROC_NET_ROUTE>;
948 close(PROC_NET_ROUTE);
950 # Eat header line
951 shift @ifs;
952 chomp @ifs;
953 foreach my $line(@ifs) {
954 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
955 my $destination;
956 my $mask;
957 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
958 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
959 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
960 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
961 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
962 # destination matches route, save mac and exit
963 $result= 1;
964 last;
965 }
966 }
967 }
968 } else {
969 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
970 }
972 return $result;
973 }
975 1;