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 Net::LDAP::Util;
17 use Socket;
18 use Net::hostent;
20 my $event_dir = "/usr/lib/gosa-si/server/ClientPackages";
21 use lib "/usr/lib/gosa-si/server/ClientPackages";
23 BEGIN{}
24 END {}
26 my ($server_ip, $server_port, $ClientPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
27 my $server;
28 my $network_interface;
29 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
30 my $mesg;
32 my %cfg_defaults = (
33 "server" => {
34 "ip" => [\$server_ip, "0.0.0.0"],
35 "mac-address" => [\$main::server_mac_address, "00:00:00:00:00"],
36 "port" => [\$server_port, "20081"],
37 "ldap-uri" => [\$ldap_uri, ""],
38 "ldap-base" => [\$ldap_base, ""],
39 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
40 "ldap-admin-password" => [\$ldap_admin_password, ""],
41 "max-clients" => [\$max_clients, 100],
42 },
43 "ClientPackages" => {
44 "key" => [\$ClientPackages_key, ""],
45 },
46 );
48 ### START #####################################################################
50 # read configfile and import variables
51 &local_read_configfile();
54 # if server_ip is not an ip address but a name
55 if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
56 $network_interface= &get_interface_for_ip($server_ip);
57 $main::server_mac_address= &get_mac($network_interface);
60 # import local events
61 my ($error, $result, $event_hash) = &import_events($event_dir);
63 foreach my $log_line (@$result) {
64 if ($log_line =~ / succeed: /) {
65 &main::daemon_log("0 DEBUG: ClientPackages - $log_line", 7);
66 } else {
67 &main::daemon_log("0 ERROR: ClientPackages - $log_line", 1);
68 }
69 }
70 # build vice versa event_hash, event_name => module
71 my $event2module_hash = {};
72 while (my ($module, $mod_events) = each %$event_hash) {
73 while (my ($event_name, $nothing) = each %$mod_events) {
74 $event2module_hash->{$event_name} = $module;
75 }
77 }
79 # Unit tag can be defined in config
80 if((not defined($main::gosa_unit_tag)) || length($main::gosa_unit_tag) == 0) {
81 # Read gosaUnitTag from LDAP
83 my $ldap_handle = &main::get_ldap_handle();
84 if( defined($ldap_handle) ) {
85 &main::daemon_log("0 INFO: Searching for servers gosaUnitTag with mac address $main::server_mac_address",5);
86 # Perform search for Unit Tag
87 $mesg = $ldap_handle->search(
88 base => $ldap_base,
89 scope => 'sub',
90 attrs => ['gosaUnitTag'],
91 filter => "(macaddress=$main::server_mac_address)"
92 );
94 if ((! $main::server_mac_address eq "00:00:00:00:00:00") and $mesg->count == 1) {
95 my $entry= $mesg->entry(0);
96 my $unit_tag= $entry->get_value("gosaUnitTag");
97 $main::ldap_server_dn= $mesg->entry(0)->dn;
98 if(defined($unit_tag) && length($unit_tag) > 0) {
99 &main::daemon_log("0 INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
100 $main::gosa_unit_tag= $unit_tag;
101 }
102 } else {
103 # Perform another search for Unit Tag
104 my $hostname= `hostname -f`;
105 chomp($hostname);
106 &main::daemon_log("0 INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
107 $mesg = $ldap_handle->search(
108 base => $ldap_base,
109 scope => 'sub',
110 attrs => ['gosaUnitTag'],
111 filter => "(&(cn=$hostname)(objectClass=goServer))"
112 );
113 if ($mesg->count == 1) {
114 my $entry= $mesg->entry(0);
115 my $unit_tag= $entry->get_value("gosaUnitTag");
116 $main::ldap_server_dn= $mesg->entry(0)->dn;
117 if(defined($unit_tag) && length($unit_tag) > 0) {
118 &main::daemon_log("0 INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
119 $main::gosa_unit_tag= $unit_tag;
120 }
121 } else {
122 # Perform another search for Unit Tag
123 $hostname= `hostname -s`;
124 chomp($hostname);
125 &main::daemon_log("0 INFO: Searching for servers gosaUnitTag with hostname $hostname",5);
126 $mesg = $ldap_handle->search(
127 base => $ldap_base,
128 scope => 'sub',
129 attrs => ['gosaUnitTag'],
130 filter => "(&(cn=$hostname)(objectClass=goServer))"
131 );
132 if ($mesg->count == 1) {
133 my $entry= $mesg->entry(0);
134 my $unit_tag= $entry->get_value("gosaUnitTag");
135 $main::ldap_server_dn= $mesg->entry(0)->dn;
136 if(defined($unit_tag) && length($unit_tag) > 0) {
137 &main::daemon_log("INFO: Detected gosaUnitTag $unit_tag for creating entries", 5);
138 $main::gosa_unit_tag= $unit_tag;
139 }
140 } else {
141 &main::daemon_log("0 WARNING: No gosaUnitTag detected. Not using gosaUnitTag", 3);
142 }
143 }
144 }
145 } else {
146 &main::daemon_log("0 INFO: Using gosaUnitTag from config-file: $main::gosa_unit_tag",5);
147 }
148 &main::release_ldap_handle($ldap_handle);
149 }
152 my $server_address = "$server_ip:$server_port";
153 $main::server_address = $server_address;
155 {
156 # Check if ou=incoming exists
157 # TODO: This should be transferred to a module init-function
158 my $ldap_handle = &main::get_ldap_handle();
159 if( defined($ldap_handle) ) {
160 &main::daemon_log("0 DEBUG: Searching for ou=incoming container for new clients", 9);
161 # Perform search
162 my $mesg = $ldap_handle->search(
163 base => $ldap_base,
164 scope => 'one',
165 filter => "(&(ou=incoming)(objectClass=organizationalUnit))"
166 );
167 if(not defined($mesg->count) or $mesg->count == 0) {
168 my $incomingou = Net::LDAP::Entry->new();
169 $incomingou->dn('ou=incoming,'.$ldap_base);
170 $incomingou->add('objectClass' => 'organizationalUnit');
171 my $result = $incomingou->update($ldap_handle);
172 if($result != 0) {
173 &main::daemon_log("0 ERROR: Problem adding ou=incoming: '".$result->errorMessage."'!", 1);
174 }
175 }
176 }
177 &main::release_ldap_handle($ldap_handle);
178 }
181 ### functions #################################################################
184 sub get_module_info {
185 my @info = ($server_address,
186 $ClientPackages_key,
187 $event_hash,
188 );
189 return \@info;
190 }
193 #=== FUNCTION ================================================================
194 # NAME: local_read_configfile
195 # PARAMETERS: cfg_file - string -
196 # RETURNS: nothing
197 # DESCRIPTION: read cfg_file and set variables
198 #===============================================================================
199 sub local_read_configfile {
200 my $cfg;
201 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
202 if( -r $main::cfg_file ) {
203 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
204 } else {
205 print STDERR "Couldn't read config file!";
206 }
207 } else {
208 $cfg = Config::IniFiles->new() ;
209 }
210 foreach my $section (keys %cfg_defaults) {
211 foreach my $param (keys %{$cfg_defaults{ $section }}) {
212 my $pinfo = $cfg_defaults{ $section }{ $param };
213 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
214 }
215 }
217 # Read non predefined sections
218 my $param;
219 if ($cfg->SectionExists('ldap')){
220 foreach $param ($cfg->Parameters('ldap')){
221 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
222 }
223 }
224 if ($cfg->SectionExists('pam_ldap')){
225 foreach $param ($cfg->Parameters('pam_ldap')){
226 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
227 }
228 }
229 if ($cfg->SectionExists('nss_ldap')){
230 foreach $param ($cfg->Parameters('nss_ldap')){
231 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
232 }
233 }
234 if ($cfg->SectionExists('goto')){
235 $goto_admin= $cfg->val('goto', 'terminal_admin');
236 $goto_secret= $cfg->val('goto', 'terminal_secret');
237 } else {
238 $goto_admin= undef;
239 $goto_secret= undef;
240 }
242 }
245 #=== FUNCTION ================================================================
246 # NAME: get_mac
247 # PARAMETERS: interface name (i.e. eth0)
248 # RETURNS: (mac address)
249 # DESCRIPTION: Uses ioctl to get mac address directly from system.
250 #===============================================================================
251 sub get_mac {
252 my $ifreq= shift;
253 my $result;
254 if ($ifreq && length($ifreq) > 0) {
255 if($ifreq eq "all") {
256 $result = "00:00:00:00:00:00";
257 } else {
258 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
260 # A configured MAC Address should always override a guessed value
261 if ($main::server_mac_address and length($main::server_mac_address) > 0) {
262 $result= $main::server_mac_address;
263 }
265 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
266 or die "socket: $!";
268 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
269 my ($if, $mac)= unpack 'h36 H12', $ifreq;
271 if (length($mac) > 0) {
272 $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])$/;
273 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
274 $result = $mac;
275 }
276 }
277 }
278 }
279 return $result;
280 }
283 #=== FUNCTION ================================================================
284 # NAME: process_incoming_msg
285 # PARAMETERS: crypted_msg - string - incoming crypted message
286 # RETURNS: nothing
287 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
288 #===============================================================================
289 sub process_incoming_msg {
290 my ($msg, $msg_hash, $session_id) = @_ ;
291 my $error = 0;
292 my $host_name;
293 my $host_key;
294 my @out_msg_l = ("nohandler");
295 my $sql_events;
297 # process incoming msg
298 my $header = @{$msg_hash->{header}}[0];
299 my $source = @{$msg_hash->{source}}[0];
300 my @target_l = @{$msg_hash->{target}};
302 # skip PREFIX
303 $header =~ s/^CLMSG_//;
305 &main::daemon_log("$session_id DEBUG: ClientPackages: msg to process: $header", 7);
307 if( 0 == length @target_l){
308 &main::daemon_log("$session_id ERROR: no target specified for msg $header", 1);
309 $error++;
310 } elsif( 1 == length @target_l) {
311 my $target = $target_l[0];
312 if(&server_matches($target)) {
313 if ($header eq 'new_key') {
314 @out_msg_l = &new_key($msg_hash)
315 } elsif ($header eq 'here_i_am') {
316 @out_msg_l = &here_i_am($msg, $msg_hash, $session_id)
317 } else {
318 # a event exists with the header as name
319 if( exists $event2module_hash->{$header} ) {
320 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event2module_hash->{$header}."'", 5);
321 no strict 'refs';
322 @out_msg_l = &{$event2module_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
324 # if no event handler is implemented
325 } else {
326 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$source') OR (hostname='$source') )";
327 my $res = $main::known_clients_db->select_dbentry( $sql_events );
328 my $l = keys(%$res);
330 # set error if no or more than 1 hits are found for sql query
331 if ( $l != 1) {
332 @out_msg_l = ('knownclienterror');
334 # found exact 1 hit in db
335 } else {
336 my $client_events = $res->{'1'}->{'events'};
338 # client is registered for this event, deliver this message to client
339 $header =~ s/^answer_//;
340 if ($client_events =~ /,$header,/) {
341 # answer message finally arrived destination server, so forward messages to GOsa
342 if ($target eq $main::server_address) {
343 $msg =~ s/<header>answer_/<header>/;
344 $msg =~ s/<target>\S+<\/target>/<target>GOSA<\/target>/;
345 }
346 @out_msg_l = ( $msg );
348 # client is not registered for this event, set error
349 } else {
350 @out_msg_l = ('noeventerror');
351 }
352 }
353 }
354 }
356 # if delivery not possible raise error and return
357 if( not defined $out_msg_l[0] ) {
358 @out_msg_l = ();
359 } elsif( $out_msg_l[0] eq 'nohandler') {
360 &main::daemon_log("$session_id ERROR: ClientPackages: no event handler or core function defined for '$header'", 1);
361 @out_msg_l = ();
362 } elsif ($out_msg_l[0] eq 'knownclienterror') {
363 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
364 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
365 @out_msg_l = ();
366 } elsif ($out_msg_l[0] eq 'noeventerror') {
367 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
368 @out_msg_l = ();
369 }
370 } else {
371 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
372 push(@out_msg_l, $msg);
373 }
374 }
376 return \@out_msg_l;
377 }
380 #=== FUNCTION ================================================================
381 # NAME: new_passwd
382 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
383 # RETURNS: nothing
384 # DESCRIPTION: process this incoming message
385 #===============================================================================
386 sub new_key {
387 my ($msg_hash) = @_;
388 my @out_msg_l;
390 my $header = @{$msg_hash->{header}}[0];
391 my $source_name = @{$msg_hash->{source}}[0];
392 my $source_key = @{$msg_hash->{new_key}}[0];
393 my $query_res;
395 # check known_clients_db
396 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
397 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
398 if( 1 == keys %{$query_res} ) {
399 my $act_time = &get_time;
400 my $sql_statement= "UPDATE known_clients ".
401 "SET hostkey='$source_key', timestamp='$act_time' ".
402 "WHERE hostname='$source_name'";
403 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
404 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
405 my $out_msg = &create_xml_string($hash);
406 push(@out_msg_l, $out_msg);
407 }
409 # only do if host still not found
410 if( 0 == @out_msg_l ) {
411 # check known_server_db
412 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
413 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
414 if( 1 == keys %{$query_res} ) {
415 my $act_time = &get_time;
416 my $sql_statement= "UPDATE known_server ".
417 "SET hostkey='$source_key', timestamp='$act_time' ".
418 "WHERE hostname='$source_name'";
419 my $res = $main::known_server_db->update_dbentry( $sql_statement );
421 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
422 my $out_msg = &create_xml_string($hash);
423 push(@out_msg_l, $out_msg);
424 }
425 }
427 return @out_msg_l;
428 }
431 #=== FUNCTION ================================================================
432 # NAME: here_i_am
433 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
434 # RETURNS: nothing
435 # DESCRIPTION: process this incoming message
436 #===============================================================================
437 sub here_i_am {
438 my ($msg, $msg_hash, $session_id) = @_;
439 my @out_msg_l;
440 my $out_hash;
441 my $source = @{$msg_hash->{source}}[0];
442 my $mac_address = @{$msg_hash->{mac_address}}[0];
443 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
444 my $client_status = @{$msg_hash->{client_status}}[0];
445 my $client_revision = @{$msg_hash->{client_revision}}[0];
446 my $key_lifetime = @{$msg_hash->{key_lifetime}}[0];
448 # Move forced hostname to heap - if used
449 #FIXME: move to some global POE namespace - please
450 if ( defined($msg_hash->{'force-hostname'}[0]) &&
451 length($msg_hash->{'force-hostname'}[0]) > 0){
452 # $heap->{force-hostname}->{$mac_address}= $msg_hash->{'force-hostname'}[0];
453 open (TFILE, ">/var/tmp/$mac_address");
454 print TFILE $msg_hash->{'force-hostname'}[0];
455 close (TFILE);
456 } else {
457 # $heap->{force-hostname}->{$mac_address}= undef;
458 if ( -e "/var/tmp/$mac_address") {
459 unlink("/var/tmp/$mac_address")
460 };
461 }
463 # number of known clients
464 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
466 # check wether client address or mac address is already known
467 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
468 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
470 if ( 1 == keys %{$db_res} ) {
471 &main::daemon_log("$session_id WARNING: $source is already known as a client", 1);
472 &main::daemon_log("$session_id WARNING: values for $source are being overwritten", 1);
473 $nu_clients --;
474 }
476 # number of current active clients
477 my $act_nu_clients = $nu_clients;
479 &main::daemon_log("$session_id INFO: number of current active clients: $act_nu_clients", 5);
480 &main::daemon_log("$session_id INFO: number of maximal allowed clients: $max_clients", 5);
482 if($max_clients <= $act_nu_clients) {
483 my $out_hash = &create_xml_hash("denied", $server_address, $source);
484 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
485 my $passwd = @{$msg_hash->{new_passwd}}[0];
486 &send_msg_hash2address($out_hash, $source, $passwd);
487 return;
488 }
490 # new client accepted
491 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
493 # add entry to known_clients_db
494 my $events = @{$msg_hash->{events}}[0];
495 my $act_timestamp = &get_time;
496 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
497 primkey=>['hostname'],
498 hostname=>$source,
499 events=>$events,
500 macaddress=>$mac_address,
501 status=>'registered',
502 hostkey=>$new_passwd,
503 timestamp=>$act_timestamp,
504 keylifetime=>$key_lifetime,
505 } );
507 if ($res != 0) {
508 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $res");
509 return;
510 }
512 # return acknowledgement to client
513 $out_hash = &create_xml_hash("registered", $server_address, $source);
516 # give the new client his ldap config
517 # Workaround: Send within the registration response, if the client will get an ldap config later
518 my $new_ldap_config_out = &new_ldap_config($source, $session_id);
519 if($new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
520 &add_content2xml_hash($out_hash, "ldap_available", "true");
521 } elsif($new_ldap_config_out && $new_ldap_config_out =~ /error/){
522 &add_content2xml_hash($out_hash, "error", $new_ldap_config_out);
524 my $sql_statement = "UPDATE $main::job_queue_tn ".
525 "SET status='error', result='$new_ldap_config_out' ".
526 "WHERE status='processing' AND macaddress LIKE '$mac_address'";
527 my $res = $main::job_db->update_dbentry($sql_statement);
528 &main::daemon_log("$session_id DEBUG: $sql_statement RESULT: $res", 7);
529 }
530 my $register_out = &create_xml_string($out_hash);
531 push(@out_msg_l, $register_out);
533 # Really send the ldap config
534 if( $new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
535 push(@out_msg_l, $new_ldap_config_out);
536 }
538 # Send client hardware configuration
539 my $hardware_config_out = &hardware_config($msg, $msg_hash, $session_id);
540 if( $hardware_config_out ) {
541 push(@out_msg_l, $hardware_config_out);
542 }
544 # Send client ntp server
545 my $ntp_config_out = &new_ntp_config($mac_address, $session_id);
546 if ($ntp_config_out) {
547 push(@out_msg_l, $ntp_config_out);
548 }
550 # Send client syslog server
551 my $syslog_config_out = &new_syslog_config($mac_address, $session_id);
552 if ($syslog_config_out) {
553 push(@out_msg_l, $syslog_config_out);
554 }
556 # notify registered client to all other server
557 my %mydata = ( 'client' => $source, 'macaddress' => $mac_address);
558 my $mymsg = &build_msg('new_foreign_client', $main::server_address, "KNOWN_SERVER", \%mydata);
559 push(@out_msg_l, $mymsg);
561 &main::daemon_log("$session_id INFO: register client $source ($mac_address)", 5);
562 &main::daemon_log("$session_id INFO: client version: $client_status - $client_revision", 5);
563 return @out_msg_l;
564 }
567 #=== FUNCTION ================================================================
568 # NAME: who_has
569 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
570 # RETURNS: nothing
571 # DESCRIPTION: process this incoming message
572 #===============================================================================
573 sub who_has {
574 my ($msg_hash) = @_ ;
575 my @out_msg_l;
577 # what is your search pattern
578 my $search_pattern = @{$msg_hash->{who_has}}[0];
579 my $search_element = @{$msg_hash->{$search_pattern}}[0];
580 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
582 # scanning known_clients for search_pattern
583 my @host_addresses = keys %$main::known_clients;
584 my $known_clients_entries = length @host_addresses;
585 my $host_address;
586 foreach my $host (@host_addresses) {
587 my $client_element = $main::known_clients->{$host}->{$search_pattern};
588 if ($search_element eq $client_element) {
589 $host_address = $host;
590 last;
591 }
592 }
594 # search was successful
595 if (defined $host_address) {
596 my $source = @{$msg_hash->{source}}[0];
597 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
598 &add_content2xml_hash($out_hash, "mac_address", $search_element);
599 my $out_msg = &create_xml_string($out_hash);
600 push(@out_msg_l, $out_msg);
601 }
602 return @out_msg_l;
603 }
606 sub who_has_i_do {
607 my ($msg_hash) = @_ ;
608 my $header = @{$msg_hash->{header}}[0];
609 my $source = @{$msg_hash->{source}}[0];
610 my $search_param = @{$msg_hash->{$header}}[0];
611 my $search_value = @{$msg_hash->{$search_param}}[0];
612 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
613 }
616 sub new_syslog_config {
617 my ($mac_address, $session_id) = @_;
618 my $syslog_msg;
619 my $ldap_handle=&main::get_ldap_handle();
621 # Perform search
622 my $ldap_res = $ldap_handle->search( base => $ldap_base,
623 scope => 'sub',
624 attrs => ['gotoSyslogServer'],
625 filter => "(&(objectClass=GOhard)(macaddress=$mac_address))");
626 if($ldap_res->code) {
627 &main::daemon_log("$session_id ".$ldap_res->error, 1);
628 &main::release_ldap_handle($ldap_handle);
629 return;
630 }
632 # Sanity check
633 if ($ldap_res->count != 1) {
634 &main::daemon_log("$session_id ERROR: client with mac address $mac_address not found/unique/active - not sending syslog config".
635 "\n\tbase: $ldap_base".
636 "\n\tscope: sub".
637 "\n\tattrs: gotoSyslogServer".
638 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$mac_address))", 1);
639 &main::release_ldap_handle($ldap_handle);
640 return;
641 }
643 my $entry= $ldap_res->entry(0);
644 my $filter_dn = &Net::LDAP::Util::escape_filter_value($entry->dn);
645 my $syslog_server = $entry->get_value("gotoSyslogServer");
647 # If no syslog server is specified at host, just have a look at the object group of the host
648 # Perform object group search
649 if (not defined $syslog_server) {
650 my $ldap_res = $ldap_handle->search( base => $ldap_base,
651 scope => 'sub',
652 attrs => ['gotoSyslogServer'],
653 filter => "(&(objectClass=gosaGroupOfNames)(member=$filter_dn))");
654 if($ldap_res->code) {
655 &main::daemon_log("$session_id ".$ldap_res->error, 1);
656 &main::release_ldap_handle($ldap_handle);
657 return;
658 }
660 # Sanity check
661 if ($ldap_res->count != 1) {
662 &main::daemon_log("$session_id ERROR: client with mac address $mac_address not found/unique/active - not sending syslog config".
663 "\n\tbase: $ldap_base".
664 "\n\tscope: sub".
665 "\n\tattrs: gotoSyslogServer".
666 "\n\tfilter: (&(objectClass=gosaGroupOfNames)(member=$filter_dn))", 1);
667 &main::release_ldap_handle($ldap_handle);
668 return;
669 }
671 my $entry= $ldap_res->entry(0);
672 $syslog_server= $entry->get_value("gotoSyslogServer");
673 }
675 # Return if no syslog server specified
676 if (not defined $syslog_server) {
677 &main::daemon_log("$session_id WARNING: no syslog server specified for this host '$mac_address'", 3);
678 &main::release_ldap_handle($ldap_handle);
679 return;
680 }
683 # Add syslog server to 'syslog_config' message
684 my $syslog_msg_hash = &create_xml_hash("new_syslog_config", $server_address, $mac_address);
685 &add_content2xml_hash($syslog_msg_hash, "server", $syslog_server);
687 &main::release_ldap_handle($ldap_handle);
688 return &create_xml_string($syslog_msg_hash);
689 }
692 sub new_ntp_config {
693 my ($address, $session_id) = @_;
694 my $ntp_msg;
695 my $ldap_handle=&main::get_ldap_handle();
697 # Perform search
698 my $ldap_res = $ldap_handle->search( base => $ldap_base,
699 scope => 'sub',
700 attrs => ['gotoNtpServer'],
701 filter => "(&(objectClass=GOhard)(macaddress=$address))");
702 if($ldap_res->code) {
703 &main::daemon_log("$session_id ".$ldap_res->error, 1);
704 &main::release_ldap_handle($ldap_handle);
705 return;
706 }
708 # Sanity check
709 if ($ldap_res->count != 1) {
710 &main::daemon_log("$session_id ERROR: client with mac address $address not found/unique/active - not sending ntp config".
711 "\n\tbase: $ldap_base".
712 "\n\tscope: sub".
713 "\n\tattrs: gotoNtpServer".
714 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$address))", 1);
715 &main::release_ldap_handle($ldap_handle);
716 return;
717 }
719 my $entry= $ldap_res->entry(0);
720 my $filter_dn = &Net::LDAP::Util::escape_filter_value($entry->dn);
721 my @ntp_servers= $entry->get_value("gotoNtpServer");
723 # If no ntp server is specified at host, just have a look at the object group of the host
724 # Perform object group search
725 if ((not @ntp_servers) || (@ntp_servers == 0)) {
726 my $ldap_res = $ldap_handle->search( base => $ldap_base,
727 scope => 'sub',
728 attrs => ['gotoNtpServer'],
729 filter => "(&(objectClass=gosaGroupOfNames)(member=$filter_dn))");
730 if($ldap_res->code) {
731 &main::daemon_log("$session_id ".$ldap_res->error, 1);
732 &main::release_ldap_handle($ldap_handle);
733 return;
734 }
736 # Sanity check
737 if ($ldap_res->count != 1) {
738 &main::daemon_log("$session_id ERROR: client with mac address $address not found/unique/active - not sending ntp config".
739 "\n\tbase: $ldap_base".
740 "\n\tscope: sub".
741 "\n\tattrs: gotoNtpServer".
742 "\n\tfilter: (&(objectClass=gosaGroupOfNames)(member=$filter_dn))", 1);
743 &main::release_ldap_handle($ldap_handle);
744 return;
745 }
747 my $entry= $ldap_res->entry(0);
748 @ntp_servers= $entry->get_value("gotoNtpServer");
749 }
751 # Return if no ntp server specified
752 if ((not @ntp_servers) || (@ntp_servers == 0)) {
753 &main::daemon_log("$session_id WARNING: no ntp server specified for this host '$address'", 3);
754 &main::release_ldap_handle($ldap_handle);
755 return;
756 }
758 # Add each ntp server to 'ntp_config' message
759 my $ntp_msg_hash = &create_xml_hash("new_ntp_config", $server_address, $address);
760 foreach my $ntp_server (@ntp_servers) {
761 &add_content2xml_hash($ntp_msg_hash, "server", $ntp_server);
762 }
764 &main::release_ldap_handle($ldap_handle);
765 return &create_xml_string($ntp_msg_hash);
766 }
769 #=== FUNCTION ================================================================
770 # NAME: new_ldap_config
771 # PARAMETERS: address - string - ip address and port of a host
772 # RETURNS: gosa-si conform message
773 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
774 #===============================================================================
775 sub new_ldap_config {
776 my ($address, $session_id) = @_ ;
778 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress LIKE '$address'";
779 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
781 # check hit
782 my $hit_counter = keys %{$res};
783 if( not $hit_counter == 1 ) {
784 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query '$sql_statement'", 1);
785 return;
786 }
788 $address = $res->{1}->{hostname};
789 my $macaddress = $res->{1}->{macaddress};
790 my $hostkey = $res->{1}->{hostkey};
792 if (not defined $macaddress) {
793 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
794 return;
795 }
797 # Perform search
798 my $ldap_handle=&main::get_ldap_handle();
799 $mesg = $ldap_handle->search( base => $ldap_base,
800 scope => 'sub',
801 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
802 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
803 if($mesg->code) {
804 &main::daemon_log("$session_id ".$mesg->error, 1);
805 &main::release_ldap_handle($ldap_handle);
806 return;
807 }
809 # Sanity check
810 if ($mesg->count != 1) {
811 &main::daemon_log("$session_id ERROR: client with mac address $macaddress not found/unique/active - not sending ldap config".
812 "\n\tbase: $ldap_base".
813 "\n\tscope: sub".
814 "\n\tattrs: dn, gotoLdapServer".
815 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
816 &main::release_ldap_handle($ldap_handle);
817 return;
818 }
820 my $entry= $mesg->entry(0);
821 my $filter_dn= &Net::LDAP::Util::escape_filter_value($entry->dn);
822 my @servers= $entry->get_value("gotoLdapServer");
823 my $unit_tag= $entry->get_value("gosaUnitTag");
824 my @ldap_uris;
825 my $server;
826 my $base;
827 my $release;
828 my $dn= $entry->dn;
830 # Fill release if available
831 my $FAIclass= $entry->get_value("FAIclass");
832 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
833 $release= $1;
834 }
836 # Do we need to look at an object class?
837 if (not @servers){
838 $mesg = $ldap_handle->search( base => $ldap_base,
839 scope => 'sub',
840 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
841 filter => "(&(objectClass=gosaGroupOfNames)(member=$filter_dn))");
842 if($mesg->code) {
843 &main::daemon_log("$session_id ERROR: unable to search for '(&(objectClass=gosaGroupOfNames)(member=$filter_dn))': ".$mesg->error, 1);
844 &main::release_ldap_handle($ldap_handle);
845 return;
846 }
848 # Sanity check
849 if ($mesg->count != 1) {
850 &main::daemon_log("$session_id ERROR: client with mac address $macaddress not found/unique/active - not sending ldap config".
851 "\n\tbase: $ldap_base".
852 "\n\tscope: sub".
853 "\n\tattrs: dn, gotoLdapServer, FAIclass".
854 "\n\tfilter: (&(objectClass=gosaGroupOfNames)(member=$filter_dn))", 1);
855 &main::release_ldap_handle($ldap_handle);
856 return;
857 }
859 $entry= $mesg->entry(0);
860 $dn= $entry->dn;
861 @servers= $entry->get_value("gotoLdapServer");
863 if (not defined $release){
864 $FAIclass= $entry->get_value("FAIclass");
865 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
866 $release= $1;
867 }
868 }
869 }
871 @servers= sort (@servers);
873 # complain if no ldap information found
874 if (@servers == 0) {
875 &main::daemon_log("$session_id ERROR: no gotoLdapServer information for LDAP entry '$dn'", 1);
876 }
878 foreach $server (@servers){
879 # Conversation for backward compatibility
880 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
881 if ($server =~ /^([^:]+):([^:]+)$/ ) {
882 $server= "1:dummy:ldap://$1/$2";
883 } elsif ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
884 $server= "$1:dummy:ldap://$2/$3";
885 }
886 }
888 $base= $server;
889 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
890 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
891 push (@ldap_uris, $server);
892 }
894 # Assemble data package
895 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
896 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
897 if (defined $release){
898 $data{'release'}= $release;
899 }
901 # Need to append GOto settings?
902 if (defined $goto_admin and defined $goto_secret){
903 $data{'goto_admin'}= $goto_admin;
904 $data{'goto_secret'}= $goto_secret;
905 }
907 # Append unit tag if needed
908 if (defined $unit_tag){
910 # Find admin base and department name
911 $mesg = $ldap_handle->search( base => $ldap_base,
912 scope => 'sub',
913 attrs => ['dn', 'ou'],
914 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
915 #$mesg->code && die $mesg->error;
916 if($mesg->code) {
917 &main::daemon_log($mesg->error, 1);
918 &main::release_ldap_handle($ldap_handle);
919 return "error-unit-tag-count-0";
920 }
922 # Sanity check
923 if ($mesg->count != 1) {
924 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
925 &main::release_ldap_handle($ldap_handle);
926 return "error-unit-tag-count-".$mesg->count;
927 }
929 $entry= $mesg->entry(0);
930 $data{'admin_base'}= $entry->dn;
931 $data{'department'}= $entry->get_value("ou");
933 # Append unit Tag
934 $data{'unit_tag'}= $unit_tag;
935 }
936 &main::release_ldap_handle($ldap_handle);
938 # Send information
939 return &build_msg("new_ldap_config", $server_address, $address, \%data);
940 }
943 #=== FUNCTION ================================================================
944 # NAME: hardware_config
945 # PARAMETERS: address - string - ip address and port of a host
946 # RETURNS:
947 # DESCRIPTION:
948 #===============================================================================
949 sub hardware_config {
950 my ($msg, $msg_hash, $session_id) = @_ ;
951 my $address = @{$msg_hash->{source}}[0];
952 my $header = @{$msg_hash->{header}}[0];
953 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
955 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
956 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
958 # check hit
959 my $hit_counter = keys %{$res};
960 if( not $hit_counter == 1 ) {
961 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
962 }
963 my $macaddress = $res->{1}->{macaddress};
964 my $hostkey = $res->{1}->{hostkey};
966 if (not defined $macaddress) {
967 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
968 return;
969 }
971 # Perform search
972 my $ldap_handle=&main::get_ldap_handle();
973 $mesg = $ldap_handle->search(
974 base => $ldap_base,
975 scope => 'sub',
976 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
977 );
979 if($mesg->count() == 0) {
980 &main::daemon_log("Host was not found in LDAP!", 1);
982 # set status = hardware_detection at jobqueue if entry exists
983 # TODO
984 # resolve plain name for host
985 my $func_dic = {table=>$main::job_queue_tn,
986 primkey=>['macaddress', 'headertag'],
987 timestamp=>&get_time,
988 status=>'processing',
989 result=>'none',
990 progress=>'hardware-detection',
991 headertag=>'trigger_action_reinstall',
992 targettag=>$address,
993 xmlmessage=>'none',
994 macaddress=>$macaddress,
995 plainname=>'none',
996 siserver=>'localhost',
997 modified=>'1',
998 };
999 my $hd_res = $main::job_db->add_dbentry($func_dic);
1000 &main::daemon_log("$session_id INFO: add '$macaddress' to job queue as an installing job", 5);
1002 } else {
1003 my $entry= $mesg->entry(0);
1004 if (defined($entry->get_value("gotoHardwareChecksum"))) {
1005 if (! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
1006 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
1007 if($entry->update($ldap_handle)) {
1008 &main::daemon_log("$session_id INFO: Hardware changed! Detection triggered.", 5);
1009 }
1010 } else {
1011 # Nothing to do
1012 &main::release_ldap_handle($ldap_handle);
1013 return;
1014 }
1015 }
1016 }
1018 # Assemble data package
1019 my %data = ();
1021 # Need to append GOto settings?
1022 if (defined $goto_admin and defined $goto_secret){
1023 $data{'goto_admin'}= $goto_admin;
1024 $data{'goto_secret'}= $goto_secret;
1025 }
1027 &main::release_ldap_handle($ldap_handle);
1029 # Send information
1030 return &build_msg("detect_hardware", $server_address, $address, \%data);
1031 }
1033 sub server_matches {
1034 my $target = shift;
1035 my $target_ip = ($1) if $target =~ /^([0-9\.]*?):.*$/;
1036 if(!defined($target_ip) or length($target_ip) == 0) {
1037 return;
1038 }
1040 my $result = 0;
1042 if($server_ip eq $target_ip) {
1043 $result= 1;
1044 } elsif ($target_ip eq "0.0.0.0") {
1045 $result= 1;
1046 } elsif ($server_ip eq "0.0.0.0") {
1047 if ($target_ip eq "127.0.0.1") {
1048 $result= 1;
1049 } else {
1050 my $PROC_NET_ROUTE= ('/proc/net/route');
1052 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1053 or die "Could not open $PROC_NET_ROUTE";
1055 my @ifs = <PROC_NET_ROUTE>;
1057 close(PROC_NET_ROUTE);
1059 # Eat header line
1060 shift @ifs;
1061 chomp @ifs;
1062 foreach my $line(@ifs) {
1063 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1064 my $destination;
1065 my $mask;
1066 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1067 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1068 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1069 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1070 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1071 # destination matches route, save mac and exit
1072 $result= 1;
1073 last;
1074 }
1075 }
1076 }
1077 } else {
1078 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1079 }
1081 return $result;
1082 }
1084 # vim:ts=4:shiftwidth:expandtab
1085 1;