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;
156 ### functions #################################################################
159 sub get_module_info {
160 my @info = ($server_address,
161 $ClientPackages_key,
162 $event_hash,
163 );
164 return \@info;
165 }
168 #=== FUNCTION ================================================================
169 # NAME: local_read_configfile
170 # PARAMETERS: cfg_file - string -
171 # RETURNS: nothing
172 # DESCRIPTION: read cfg_file and set variables
173 #===============================================================================
174 sub local_read_configfile {
175 my $cfg;
176 if( defined( $main::cfg_file) && ( (-s $main::cfg_file) > 0 )) {
177 if( -r $main::cfg_file ) {
178 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
179 } else {
180 print STDERR "Couldn't read config file!";
181 }
182 } else {
183 $cfg = Config::IniFiles->new() ;
184 }
185 foreach my $section (keys %cfg_defaults) {
186 foreach my $param (keys %{$cfg_defaults{ $section }}) {
187 my $pinfo = $cfg_defaults{ $section }{ $param };
188 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
189 }
190 }
192 # Read non predefined sections
193 my $param;
194 if ($cfg->SectionExists('ldap')){
195 foreach $param ($cfg->Parameters('ldap')){
196 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
197 }
198 }
199 if ($cfg->SectionExists('pam_ldap')){
200 foreach $param ($cfg->Parameters('pam_ldap')){
201 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
202 }
203 }
204 if ($cfg->SectionExists('nss_ldap')){
205 foreach $param ($cfg->Parameters('nss_ldap')){
206 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
207 }
208 }
209 if ($cfg->SectionExists('goto')){
210 $goto_admin= $cfg->val('goto', 'terminal_admin');
211 $goto_secret= $cfg->val('goto', 'terminal_secret');
212 } else {
213 $goto_admin= undef;
214 $goto_secret= undef;
215 }
217 }
220 #=== FUNCTION ================================================================
221 # NAME: get_mac
222 # PARAMETERS: interface name (i.e. eth0)
223 # RETURNS: (mac address)
224 # DESCRIPTION: Uses ioctl to get mac address directly from system.
225 #===============================================================================
226 sub get_mac {
227 my $ifreq= shift;
228 my $result;
229 if ($ifreq && length($ifreq) > 0) {
230 if($ifreq eq "all") {
231 $result = "00:00:00:00:00:00";
232 } else {
233 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
235 # A configured MAC Address should always override a guessed value
236 if ($main::server_mac_address and length($main::server_mac_address) > 0) {
237 $result= $main::server_mac_address;
238 }
240 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
241 or die "socket: $!";
243 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
244 my ($if, $mac)= unpack 'h36 H12', $ifreq;
246 if (length($mac) > 0) {
247 $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])$/;
248 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
249 $result = $mac;
250 }
251 }
252 }
253 }
254 return $result;
255 }
258 #=== FUNCTION ================================================================
259 # NAME: process_incoming_msg
260 # PARAMETERS: crypted_msg - string - incoming crypted message
261 # RETURNS: nothing
262 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
263 #===============================================================================
264 sub process_incoming_msg {
265 my ($msg, $msg_hash, $session_id) = @_ ;
266 my $error = 0;
267 my $host_name;
268 my $host_key;
269 my @out_msg_l = ("nohandler");
270 my $sql_events;
272 # process incoming msg
273 my $header = @{$msg_hash->{header}}[0];
274 my $source = @{$msg_hash->{source}}[0];
275 my @target_l = @{$msg_hash->{target}};
277 # skip PREFIX
278 $header =~ s/^CLMSG_//;
280 &main::daemon_log("$session_id DEBUG: ClientPackages: msg to process: $header", 7);
282 if( 0 == length @target_l){
283 &main::daemon_log("$session_id ERROR: no target specified for msg $header", 1);
284 $error++;
285 } elsif( 1 == length @target_l) {
286 my $target = $target_l[0];
287 if(&server_matches($target)) {
288 if ($header eq 'new_key') {
289 @out_msg_l = &new_key($msg_hash)
290 } elsif ($header eq 'here_i_am') {
291 @out_msg_l = &here_i_am($msg, $msg_hash, $session_id)
292 } else {
293 # a event exists with the header as name
294 if( exists $event2module_hash->{$header} ) {
295 &main::daemon_log("$session_id INFO: found event '$header' at event-module '".$event2module_hash->{$header}."'", 5);
296 no strict 'refs';
297 @out_msg_l = &{$event2module_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
299 # if no event handler is implemented
300 } else {
301 $sql_events = "SELECT * FROM $main::known_clients_tn WHERE ( (macaddress LIKE '$source') OR (hostname='$source') )";
302 my $res = $main::known_clients_db->select_dbentry( $sql_events );
303 my $l = keys(%$res);
305 # set error if no or more than 1 hits are found for sql query
306 if ( $l != 1) {
307 @out_msg_l = ('knownclienterror');
309 # found exact 1 hit in db
310 } else {
311 my $client_events = $res->{'1'}->{'events'};
313 # client is registered for this event, deliver this message to client
314 $header =~ s/^answer_//;
315 if ($client_events =~ /,$header,/) {
316 # answer message finally arrived destination server, so forward messages to GOsa
317 if ($target eq $main::server_address) {
318 $msg =~ s/<header>answer_/<header>/;
319 $msg =~ s/<target>\S+<\/target>/<target>GOSA<\/target>/;
320 }
321 @out_msg_l = ( $msg );
323 # client is not registered for this event, set error
324 } else {
325 @out_msg_l = ('noeventerror');
326 }
327 }
328 }
329 }
331 # if delivery not possible raise error and return
332 if( not defined $out_msg_l[0] ) {
333 @out_msg_l = ();
334 } elsif( $out_msg_l[0] eq 'nohandler') {
335 &main::daemon_log("$session_id ERROR: ClientPackages: no event handler or core function defined for '$header'", 1);
336 @out_msg_l = ();
337 } elsif ($out_msg_l[0] eq 'knownclienterror') {
338 &main::daemon_log("$session_id ERROR: no or more than 1 hits are found at known_clients_db with sql query: '$sql_events'", 1);
339 &main::daemon_log("$session_id ERROR: processing is aborted and message will not be forwarded", 1);
340 @out_msg_l = ();
341 } elsif ($out_msg_l[0] eq 'noeventerror') {
342 &main::daemon_log("$session_id ERROR: client '$target' is not registered for event '$header', processing is aborted", 1);
343 @out_msg_l = ();
344 }
345 } else {
346 &main::daemon_log("INFO: msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
347 push(@out_msg_l, $msg);
348 }
349 }
351 return \@out_msg_l;
352 }
355 #=== FUNCTION ================================================================
356 # NAME: new_passwd
357 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
358 # RETURNS: nothing
359 # DESCRIPTION: process this incoming message
360 #===============================================================================
361 sub new_key {
362 my ($msg_hash) = @_;
363 my @out_msg_l;
365 my $header = @{$msg_hash->{header}}[0];
366 my $source_name = @{$msg_hash->{source}}[0];
367 my $source_key = @{$msg_hash->{new_key}}[0];
368 my $query_res;
370 # check known_clients_db
371 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
372 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
373 if( 1 == keys %{$query_res} ) {
374 my $act_time = &get_time;
375 my $sql_statement= "UPDATE known_clients ".
376 "SET hostkey='$source_key', timestamp='$act_time' ".
377 "WHERE hostname='$source_name'";
378 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
379 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
380 my $out_msg = &create_xml_string($hash);
381 push(@out_msg_l, $out_msg);
382 }
384 # only do if host still not found
385 if( 0 == @out_msg_l ) {
386 # check known_server_db
387 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
388 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
389 if( 1 == keys %{$query_res} ) {
390 my $act_time = &get_time;
391 my $sql_statement= "UPDATE known_server ".
392 "SET hostkey='$source_key', timestamp='$act_time' ".
393 "WHERE hostname='$source_name'";
394 my $res = $main::known_server_db->update_dbentry( $sql_statement );
396 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
397 my $out_msg = &create_xml_string($hash);
398 push(@out_msg_l, $out_msg);
399 }
400 }
402 return @out_msg_l;
403 }
406 #=== FUNCTION ================================================================
407 # NAME: here_i_am
408 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
409 # RETURNS: nothing
410 # DESCRIPTION: process this incoming message
411 #===============================================================================
412 sub here_i_am {
413 my ($msg, $msg_hash, $session_id) = @_;
414 my @out_msg_l;
415 my $out_hash;
416 my $source = @{$msg_hash->{source}}[0];
417 my $mac_address = @{$msg_hash->{mac_address}}[0];
418 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
419 my $client_status = @{$msg_hash->{client_status}}[0];
420 my $client_revision = @{$msg_hash->{client_revision}}[0];
421 my $key_lifetime = @{$msg_hash->{key_lifetime}}[0];
423 # Move forced hostname to heap - if used
424 #FIXME: move to some global POE namespace - please
425 if ( defined($msg_hash->{'force-hostname'}[0]) &&
426 length($msg_hash->{'force-hostname'}[0]) > 0){
427 # $heap->{force-hostname}->{$mac_address}= $msg_hash->{'force-hostname'}[0];
428 open (TFILE, ">/var/tmp/$mac_address");
429 print TFILE $msg_hash->{'force-hostname'}[0];
430 close (TFILE);
431 } else {
432 # $heap->{force-hostname}->{$mac_address}= undef;
433 if ( -e "/var/tmp/$mac_address") {
434 unlink("/var/tmp/$mac_address")
435 };
436 }
438 # number of known clients
439 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
441 # check wether client address or mac address is already known
442 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
443 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
445 if ( 1 == keys %{$db_res} ) {
446 &main::daemon_log("$session_id WARNING: $source is already known as a client", 1);
447 &main::daemon_log("$session_id WARNING: values for $source are being overwritten", 1);
448 $nu_clients --;
449 }
451 # number of current active clients
452 my $act_nu_clients = $nu_clients;
454 &main::daemon_log("$session_id INFO: number of current active clients: $act_nu_clients", 5);
455 &main::daemon_log("$session_id INFO: number of maximal allowed clients: $max_clients", 5);
457 if($max_clients <= $act_nu_clients) {
458 my $out_hash = &create_xml_hash("denied", $server_address, $source);
459 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
460 my $passwd = @{$msg_hash->{new_passwd}}[0];
461 &send_msg_hash2address($out_hash, $source, $passwd);
462 return;
463 }
465 # new client accepted
466 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
468 # add entry to known_clients_db
469 my $events = @{$msg_hash->{events}}[0];
470 my $act_timestamp = &get_time;
471 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
472 primkey=>['hostname'],
473 hostname=>$source,
474 events=>$events,
475 macaddress=>$mac_address,
476 status=>'registered',
477 hostkey=>$new_passwd,
478 timestamp=>$act_timestamp,
479 keylifetime=>$key_lifetime,
480 } );
482 if ($res != 0) {
483 &main::daemon_log("$session_id ERROR: cannot add entry to known_clients: $res");
484 return;
485 }
487 # return acknowledgement to client
488 $out_hash = &create_xml_hash("registered", $server_address, $source);
491 # give the new client his ldap config
492 # Workaround: Send within the registration response, if the client will get an ldap config later
493 my $new_ldap_config_out = &new_ldap_config($source, $session_id);
494 if($new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
495 &add_content2xml_hash($out_hash, "ldap_available", "true");
496 } elsif($new_ldap_config_out && $new_ldap_config_out =~ /error/){
497 &add_content2xml_hash($out_hash, "error", $new_ldap_config_out);
499 my $sql_statement = "UPDATE $main::job_queue_tn ".
500 "SET status='error', result='$new_ldap_config_out' ".
501 "WHERE status='processing' AND macaddress LIKE '$mac_address'";
502 my $res = $main::job_db->update_dbentry($sql_statement);
503 &main::daemon_log("$session_id DEBUG: $sql_statement RESULT: $res", 7);
504 }
505 my $register_out = &create_xml_string($out_hash);
506 push(@out_msg_l, $register_out);
508 # Really send the ldap config
509 if( $new_ldap_config_out && (!($new_ldap_config_out =~ /error/))) {
510 push(@out_msg_l, $new_ldap_config_out);
511 }
513 # Send client hardware configuration
514 my $hardware_config_out = &hardware_config($msg, $msg_hash, $session_id);
515 if( $hardware_config_out ) {
516 push(@out_msg_l, $hardware_config_out);
517 }
519 # Send client ntp server
520 my $ntp_config_out = &new_ntp_config($mac_address, $session_id);
521 if ($ntp_config_out) {
522 push(@out_msg_l, $ntp_config_out);
523 }
525 # Send client syslog server
526 my $syslog_config_out = &new_syslog_config($mac_address, $session_id);
527 if ($syslog_config_out) {
528 push(@out_msg_l, $syslog_config_out);
529 }
531 # notify registered client to all other server
532 my %mydata = ( 'client' => $source, 'macaddress' => $mac_address);
533 my $mymsg = &build_msg('new_foreign_client', $main::server_address, "KNOWN_SERVER", \%mydata);
534 push(@out_msg_l, $mymsg);
536 &main::daemon_log("$session_id INFO: register client $source ($mac_address)", 5);
537 &main::daemon_log("$session_id INFO: client version: $client_status - $client_revision", 5);
538 return @out_msg_l;
539 }
542 #=== FUNCTION ================================================================
543 # NAME: who_has
544 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
545 # RETURNS: nothing
546 # DESCRIPTION: process this incoming message
547 #===============================================================================
548 sub who_has {
549 my ($msg_hash) = @_ ;
550 my @out_msg_l;
552 # what is your search pattern
553 my $search_pattern = @{$msg_hash->{who_has}}[0];
554 my $search_element = @{$msg_hash->{$search_pattern}}[0];
555 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
557 # scanning known_clients for search_pattern
558 my @host_addresses = keys %$main::known_clients;
559 my $known_clients_entries = length @host_addresses;
560 my $host_address;
561 foreach my $host (@host_addresses) {
562 my $client_element = $main::known_clients->{$host}->{$search_pattern};
563 if ($search_element eq $client_element) {
564 $host_address = $host;
565 last;
566 }
567 }
569 # search was successful
570 if (defined $host_address) {
571 my $source = @{$msg_hash->{source}}[0];
572 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
573 &add_content2xml_hash($out_hash, "mac_address", $search_element);
574 my $out_msg = &create_xml_string($out_hash);
575 push(@out_msg_l, $out_msg);
576 }
577 return @out_msg_l;
578 }
581 sub who_has_i_do {
582 my ($msg_hash) = @_ ;
583 my $header = @{$msg_hash->{header}}[0];
584 my $source = @{$msg_hash->{source}}[0];
585 my $search_param = @{$msg_hash->{$header}}[0];
586 my $search_value = @{$msg_hash->{$search_param}}[0];
587 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
588 }
591 sub new_syslog_config {
592 my ($mac_address, $session_id) = @_;
593 my $syslog_msg;
594 my $ldap_handle=&main::get_ldap_handle();
596 # Perform search
597 my $ldap_res = $ldap_handle->search( base => $ldap_base,
598 scope => 'sub',
599 attrs => ['gotoSyslogServer'],
600 filter => "(&(objectClass=GOhard)(macaddress=$mac_address))");
601 if($ldap_res->code) {
602 &main::daemon_log("$session_id ".$ldap_res->error, 1);
603 &main::release_ldap_handle($ldap_handle);
604 return;
605 }
607 # Sanity check
608 if ($ldap_res->count != 1) {
609 &main::daemon_log("$session_id ERROR: client with mac address $mac_address not found/unique/active - not sending syslog config".
610 "\n\tbase: $ldap_base".
611 "\n\tscope: sub".
612 "\n\tattrs: gotoSyslogServer".
613 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$mac_address))", 1);
614 &main::release_ldap_handle($ldap_handle);
615 return;
616 }
618 my $entry= $ldap_res->entry(0);
619 my $filter_dn = &Net::LDAP::Util::escape_filter_value($entry->dn);
620 my $syslog_server = $entry->get_value("gotoSyslogServer");
622 # If no syslog server is specified at host, just have a look at the object group of the host
623 # Perform object group search
624 if (not defined $syslog_server) {
625 my $ldap_res = $ldap_handle->search( base => $ldap_base,
626 scope => 'sub',
627 attrs => ['gotoSyslogServer'],
628 filter => "(&(objectClass=gosaGroupOfNames)(member=$filter_dn))");
629 if($ldap_res->code) {
630 &main::daemon_log("$session_id ".$ldap_res->error, 1);
631 &main::release_ldap_handle($ldap_handle);
632 return;
633 }
635 # Sanity check
636 if ($ldap_res->count != 1) {
637 &main::daemon_log("$session_id ERROR: client with mac address $mac_address not found/unique/active - not sending syslog config".
638 "\n\tbase: $ldap_base".
639 "\n\tscope: sub".
640 "\n\tattrs: gotoSyslogServer".
641 "\n\tfilter: (&(objectClass=gosaGroupOfNames)(member=$filter_dn))", 1);
642 &main::release_ldap_handle($ldap_handle);
643 return;
644 }
646 my $entry= $ldap_res->entry(0);
647 $syslog_server= $entry->get_value("gotoSyslogServer");
648 }
650 # Return if no syslog server specified
651 if (not defined $syslog_server) {
652 &main::daemon_log("$session_id WARNING: no syslog server specified for this host '$mac_address'", 3);
653 &main::release_ldap_handle($ldap_handle);
654 return;
655 }
658 # Add syslog server to 'syslog_config' message
659 my $syslog_msg_hash = &create_xml_hash("new_syslog_config", $server_address, $mac_address);
660 &add_content2xml_hash($syslog_msg_hash, "server", $syslog_server);
662 &main::release_ldap_handle($ldap_handle);
663 return &create_xml_string($syslog_msg_hash);
664 }
667 sub new_ntp_config {
668 my ($address, $session_id) = @_;
669 my $ntp_msg;
670 my $ldap_handle=&main::get_ldap_handle();
672 # Perform search
673 my $ldap_res = $ldap_handle->search( base => $ldap_base,
674 scope => 'sub',
675 attrs => ['gotoNtpServer'],
676 filter => "(&(objectClass=GOhard)(macaddress=$address))");
677 if($ldap_res->code) {
678 &main::daemon_log("$session_id ".$ldap_res->error, 1);
679 &main::release_ldap_handle($ldap_handle);
680 return;
681 }
683 # Sanity check
684 if ($ldap_res->count != 1) {
685 &main::daemon_log("$session_id ERROR: client with mac address $address not found/unique/active - not sending ntp config".
686 "\n\tbase: $ldap_base".
687 "\n\tscope: sub".
688 "\n\tattrs: gotoNtpServer".
689 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$address))", 1);
690 &main::release_ldap_handle($ldap_handle);
691 return;
692 }
694 my $entry= $ldap_res->entry(0);
695 my $filter_dn = &Net::LDAP::Util::escape_filter_value($entry->dn);
696 my @ntp_servers= $entry->get_value("gotoNtpServer");
698 # If no ntp server is specified at host, just have a look at the object group of the host
699 # Perform object group search
700 if ((not @ntp_servers) || (@ntp_servers == 0)) {
701 my $ldap_res = $ldap_handle->search( base => $ldap_base,
702 scope => 'sub',
703 attrs => ['gotoNtpServer'],
704 filter => "(&(objectClass=gosaGroupOfNames)(member=$filter_dn))");
705 if($ldap_res->code) {
706 &main::daemon_log("$session_id ".$ldap_res->error, 1);
707 &main::release_ldap_handle($ldap_handle);
708 return;
709 }
711 # Sanity check
712 if ($ldap_res->count != 1) {
713 &main::daemon_log("$session_id ERROR: client with mac address $address not found/unique/active - not sending ntp config".
714 "\n\tbase: $ldap_base".
715 "\n\tscope: sub".
716 "\n\tattrs: gotoNtpServer".
717 "\n\tfilter: (&(objectClass=gosaGroupOfNames)(member=$filter_dn))", 1);
718 &main::release_ldap_handle($ldap_handle);
719 return;
720 }
722 my $entry= $ldap_res->entry(0);
723 @ntp_servers= $entry->get_value("gotoNtpServer");
724 }
726 # Return if no ntp server specified
727 if ((not @ntp_servers) || (@ntp_servers == 0)) {
728 &main::daemon_log("$session_id WARNING: no ntp server specified for this host '$address'", 3);
729 &main::release_ldap_handle($ldap_handle);
730 return;
731 }
733 # Add each ntp server to 'ntp_config' message
734 my $ntp_msg_hash = &create_xml_hash("new_ntp_config", $server_address, $address);
735 foreach my $ntp_server (@ntp_servers) {
736 &add_content2xml_hash($ntp_msg_hash, "server", $ntp_server);
737 }
739 &main::release_ldap_handle($ldap_handle);
740 return &create_xml_string($ntp_msg_hash);
741 }
744 #=== FUNCTION ================================================================
745 # NAME: new_ldap_config
746 # PARAMETERS: address - string - ip address and port of a host
747 # RETURNS: gosa-si conform message
748 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
749 #===============================================================================
750 sub new_ldap_config {
751 my ($address, $session_id) = @_ ;
753 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address' OR macaddress LIKE '$address'";
754 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
756 # check hit
757 my $hit_counter = keys %{$res};
758 if( not $hit_counter == 1 ) {
759 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query '$sql_statement'", 1);
760 return;
761 }
763 $address = $res->{1}->{hostname};
764 my $macaddress = $res->{1}->{macaddress};
765 my $hostkey = $res->{1}->{hostkey};
767 if (not defined $macaddress) {
768 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
769 return;
770 }
772 # Perform search
773 my $ldap_handle=&main::get_ldap_handle();
774 $mesg = $ldap_handle->search( base => $ldap_base,
775 scope => 'sub',
776 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag', 'FAIclass'],
777 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
778 if($mesg->code) {
779 &main::daemon_log("$session_id ".$mesg->error, 1);
780 &main::release_ldap_handle($ldap_handle);
781 return;
782 }
784 # Sanity check
785 if ($mesg->count != 1) {
786 &main::daemon_log("$session_id ERROR: client with mac address $macaddress not found/unique/active - not sending ldap config".
787 "\n\tbase: $ldap_base".
788 "\n\tscope: sub".
789 "\n\tattrs: dn, gotoLdapServer".
790 "\n\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
791 &main::release_ldap_handle($ldap_handle);
792 return;
793 }
795 my $entry= $mesg->entry(0);
796 my $filter_dn= &Net::LDAP::Util::escape_filter_value($entry->dn);
797 my @servers= $entry->get_value("gotoLdapServer");
798 my $unit_tag= $entry->get_value("gosaUnitTag");
799 my @ldap_uris;
800 my $server;
801 my $base;
802 my $release;
803 my $dn= $entry->dn;
805 # Fill release if available
806 my $FAIclass= $entry->get_value("FAIclass");
807 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
808 $release= $1;
809 }
811 # Do we need to look at an object class?
812 if (not @servers){
813 $mesg = $ldap_handle->search( base => $ldap_base,
814 scope => 'sub',
815 attrs => ['dn', 'gotoLdapServer', 'FAIclass'],
816 filter => "(&(objectClass=gosaGroupOfNames)(member=$filter_dn))");
817 if($mesg->code) {
818 &main::daemon_log("$session_id ERROR: unable to search for '(&(objectClass=gosaGroupOfNames)(member=$filter_dn))': ".$mesg->error, 1);
819 &main::release_ldap_handle($ldap_handle);
820 return;
821 }
823 # Sanity check
824 if ($mesg->count != 1) {
825 &main::daemon_log("$session_id ERROR: client with mac address $macaddress not found/unique/active - not sending ldap config".
826 "\n\tbase: $ldap_base".
827 "\n\tscope: sub".
828 "\n\tattrs: dn, gotoLdapServer, FAIclass".
829 "\n\tfilter: (&(objectClass=gosaGroupOfNames)(member=$filter_dn))", 1);
830 &main::release_ldap_handle($ldap_handle);
831 return;
832 }
834 $entry= $mesg->entry(0);
835 $dn= $entry->dn;
836 @servers= $entry->get_value("gotoLdapServer");
838 if (not defined $release){
839 $FAIclass= $entry->get_value("FAIclass");
840 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
841 $release= $1;
842 }
843 }
844 }
846 @servers= sort (@servers);
848 # complain if no ldap information found
849 if (@servers == 0) {
850 &main::daemon_log("$session_id ERROR: no gotoLdapServer information for LDAP entry '$dn'", 1);
851 }
853 foreach $server (@servers){
854 # Conversation for backward compatibility
855 if (not $server =~ /^\d+:[^:]+:ldap[^:]*:\/\// ) {
856 if ($server =~ /^([^:]+):([^:]+)$/ ) {
857 $server= "1:dummy:ldap://$1/$2";
858 } elsif ($server =~ /^(\d+):([^:]+):(.*)$/ ) {
859 $server= "$1:dummy:ldap://$2/$3";
860 }
861 }
863 $base= $server;
864 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
865 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
866 push (@ldap_uris, $server);
867 }
869 # Assemble data package
870 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
871 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
872 if (defined $release){
873 $data{'release'}= $release;
874 }
876 # Need to append GOto settings?
877 if (defined $goto_admin and defined $goto_secret){
878 $data{'goto_admin'}= $goto_admin;
879 $data{'goto_secret'}= $goto_secret;
880 }
882 # Append unit tag if needed
883 if (defined $unit_tag){
885 # Find admin base and department name
886 $mesg = $ldap_handle->search( base => $ldap_base,
887 scope => 'sub',
888 attrs => ['dn', 'ou'],
889 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
890 #$mesg->code && die $mesg->error;
891 if($mesg->code) {
892 &main::daemon_log($mesg->error, 1);
893 &main::release_ldap_handle($ldap_handle);
894 return "error-unit-tag-count-0";
895 }
897 # Sanity check
898 if ($mesg->count != 1) {
899 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
900 &main::release_ldap_handle($ldap_handle);
901 return "error-unit-tag-count-".$mesg->count;
902 }
904 $entry= $mesg->entry(0);
905 $data{'admin_base'}= $entry->dn;
906 $data{'department'}= $entry->get_value("ou");
908 # Append unit Tag
909 $data{'unit_tag'}= $unit_tag;
910 }
911 &main::release_ldap_handle($ldap_handle);
913 # Send information
914 return &build_msg("new_ldap_config", $server_address, $address, \%data);
915 }
918 #=== FUNCTION ================================================================
919 # NAME: hardware_config
920 # PARAMETERS: address - string - ip address and port of a host
921 # RETURNS:
922 # DESCRIPTION:
923 #===============================================================================
924 sub hardware_config {
925 my ($msg, $msg_hash, $session_id) = @_ ;
926 my $address = @{$msg_hash->{source}}[0];
927 my $header = @{$msg_hash->{header}}[0];
928 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
930 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
931 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
933 # check hit
934 my $hit_counter = keys %{$res};
935 if( not $hit_counter == 1 ) {
936 &main::daemon_log("$session_id ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
937 }
938 my $macaddress = $res->{1}->{macaddress};
939 my $hostkey = $res->{1}->{hostkey};
941 if (not defined $macaddress) {
942 &main::daemon_log("$session_id ERROR: no mac address found for client $address", 1);
943 return;
944 }
946 # Perform search
947 my $ldap_handle=&main::get_ldap_handle();
948 $mesg = $ldap_handle->search(
949 base => $ldap_base,
950 scope => 'sub',
951 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
952 );
954 if($mesg->count() == 0) {
955 &main::daemon_log("Host was not found in LDAP!", 1);
957 # set status = hardware_detection at jobqueue if entry exists
958 # TODO
959 # resolve plain name for host
960 my $func_dic = {table=>$main::job_queue_tn,
961 primkey=>['macaddress', 'headertag'],
962 timestamp=>&get_time,
963 status=>'processing',
964 result=>'none',
965 progress=>'hardware-detection',
966 headertag=>'trigger_action_reinstall',
967 targettag=>$address,
968 xmlmessage=>'none',
969 macaddress=>$macaddress,
970 plainname=>'none',
971 siserver=>'localhost',
972 modified=>'1',
973 };
974 my $hd_res = $main::job_db->add_dbentry($func_dic);
975 &main::daemon_log("$session_id INFO: add '$macaddress' to job queue as an installing job", 5);
977 } else {
978 my $entry= $mesg->entry(0);
979 if (defined($entry->get_value("gotoHardwareChecksum"))) {
980 if (! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
981 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
982 if($entry->update($ldap_handle)) {
983 &main::daemon_log("$session_id INFO: Hardware changed! Detection triggered.", 5);
984 }
985 } else {
986 # Nothing to do
987 &main::release_ldap_handle($ldap_handle);
988 return;
989 }
990 }
991 }
993 # Assemble data package
994 my %data = ();
996 # Need to append GOto settings?
997 if (defined $goto_admin and defined $goto_secret){
998 $data{'goto_admin'}= $goto_admin;
999 $data{'goto_secret'}= $goto_secret;
1000 }
1002 &main::release_ldap_handle($ldap_handle);
1004 # Send information
1005 return &build_msg("detect_hardware", $server_address, $address, \%data);
1006 }
1008 sub server_matches {
1009 my $target = shift;
1010 my $target_ip = ($1) if $target =~ /^([0-9\.]*?):.*$/;
1011 if(!defined($target_ip) or length($target_ip) == 0) {
1012 return;
1013 }
1015 my $result = 0;
1017 if($server_ip eq $target_ip) {
1018 $result= 1;
1019 } elsif ($target_ip eq "0.0.0.0") {
1020 $result= 1;
1021 } elsif ($server_ip eq "0.0.0.0") {
1022 if ($target_ip eq "127.0.0.1") {
1023 $result= 1;
1024 } else {
1025 my $PROC_NET_ROUTE= ('/proc/net/route');
1027 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1028 or die "Could not open $PROC_NET_ROUTE";
1030 my @ifs = <PROC_NET_ROUTE>;
1032 close(PROC_NET_ROUTE);
1034 # Eat header line
1035 shift @ifs;
1036 chomp @ifs;
1037 foreach my $line(@ifs) {
1038 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1039 my $destination;
1040 my $mask;
1041 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1042 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1043 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1044 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1045 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1046 # destination matches route, save mac and exit
1047 $result= 1;
1048 last;
1049 }
1050 }
1051 }
1052 } else {
1053 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1054 }
1056 return $result;
1057 }
1059 # vim:ts=4:shiftwidth:expandtab
1060 1;