8f052d07c7db414b6aa2761ecb114d1537f7ae65
1 package SIPackages;
3 use Exporter;
4 @ISA = ("Exporter");
6 # Each module has to have a function 'process_incoming_msg'. This function works as a interface to gosa-sd and receives the msg hash from gosa-sd. 'process_incoming_function checks, wether it has a function to process the incoming msg and forward the msg to it.
9 use strict;
10 use warnings;
11 use GOSA::GosaSupportDaemon;
12 use IO::Socket::INET;
13 use XML::Simple;
14 use Data::Dumper;
15 use NetAddr::IP;
16 use Net::LDAP;
17 use Socket;
18 use Net::hostent;
19 use Net::DNS;
21 BEGIN{}
22 END {}
24 my ($server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
25 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
26 my $server;
27 my $network_interface;
28 my $no_bus;
29 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret, $gosa_unit_tag);
32 my %cfg_defaults = (
33 "bus" => {
34 "activ" => [\$bus_activ, "on"],
35 "key" => [\$bus_key, ""],
36 "ip" => [\$bus_ip, ""],
37 "port" => [\$bus_port, "20080"],
38 },
39 "server" => {
40 "ip" => [\$server_ip, "0.0.0.0"],
41 "mac-address" => [\$server_mac_address, "00:00:00:00:00"],
42 "port" => [\$server_port, "20081"],
43 "ldap-uri" => [\$ldap_uri, ""],
44 "ldap-base" => [\$ldap_base, ""],
45 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
46 "ldap-admin-password" => [\$ldap_admin_password, ""],
47 "max-clients" => [\$max_clients, 100],
48 "gosa-unit-tag" => [\$gosa_unit_tag, ""],
49 },
50 "SIPackages" => {
51 "key" => [\$SIPackages_key, ""],
52 },
53 );
55 ### START #####################################################################
57 # read configfile and import variables
58 &read_configfile();
60 $network_interface= &get_interface_for_ip($server_ip);
61 $server_mac_address= &get_mac($network_interface);
63 # Unit tag can be defined in config
64 if((not defined($gosa_unit_tag)) || length($gosa_unit_tag) == 0) {
65 # Read gosaUnitTag from LDAP
66 my $tmp_ldap= Net::LDAP->new($ldap_uri);
67 if(defined($tmp_ldap)) {
68 &main::daemon_log("Searching for servers gosaUnitTag with mac address $server_mac_address",6);
69 my $mesg= $tmp_ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
70 # Perform search for Unit Tag
71 $mesg = $tmp_ldap->search(
72 base => $ldap_base,
73 scope => 'sub',
74 attrs => ['gosaUnitTag'],
75 filter => "(macaddress=$server_mac_address)"
76 );
78 if ($mesg->count == 1) {
79 my $entry= $mesg->entry(0);
80 my $unit_tag= $entry->get_value("gosaUnitTag");
81 if(defined($unit_tag) && length($unit_tag) > 0) {
82 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
83 $gosa_unit_tag= $unit_tag;
84 }
85 } else {
86 # Perform another search for Unit Tag
87 my $hostname= `hostname -f`;
88 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
89 $mesg = $tmp_ldap->search(
90 base => $ldap_base,
91 scope => 'sub',
92 attrs => ['gosaUnitTag'],
93 filter => "(&(cn=$hostname)(objectClass=goServer))"
94 );
95 if ($mesg->count == 1) {
96 my $entry= $mesg->entry(0);
97 my $unit_tag= $entry->get_value("gosaUnitTag");
98 if(defined($unit_tag) && length($unit_tag) > 0) {
99 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
100 $gosa_unit_tag= $unit_tag;
101 }
102 } else {
103 # Perform another search for Unit Tag
104 $hostname= `hostname -s`;
105 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
106 $mesg = $tmp_ldap->search(
107 base => $ldap_base,
108 scope => 'sub',
109 attrs => ['gosaUnitTag'],
110 filter => "(&(cn=$hostname)(objectClass=goServer))"
111 );
112 if ($mesg->count == 1) {
113 my $entry= $mesg->entry(0);
114 my $unit_tag= $entry->get_value("gosaUnitTag");
115 if(defined($unit_tag) && length($unit_tag) > 0) {
116 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
117 $gosa_unit_tag= $unit_tag;
118 }
119 } else {
120 &main::daemon_log("Not using gosaUnitTag", 6);
121 }
122 }
123 }
124 } else {
125 &main::daemon_log("Using gosaUnitTag from config-file: $gosa_unit_tag",6);
126 }
127 $tmp_ldap->unbind;
128 }
130 # complete addresses
131 #if( $server_ip eq "0.0.0.0" ) {
132 # $server_ip = "127.0.0.1";
133 #}
134 my $server_address = "$server_ip:$server_port";
135 $main::server_address = $server_address;
136 my $bus_address = "$bus_ip:$bus_port";
137 $main::bus_address = $bus_address;
139 # create general settings for this module
140 my $xml = new XML::Simple();
142 # register at bus
143 if ($main::no_bus > 0) {
144 $bus_activ = "off"
145 }
146 if($bus_activ eq "on") {
147 ®ister_at_bus();
148 }
150 # add myself to known_server_db
151 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
152 primkey=>'hostname',
153 hostname=>$server_address,
154 status=>'myself',
155 hostkey=>$SIPackages_key,
156 timestamp=>&get_time,
157 } );
161 ### functions #################################################################
164 sub get_module_info {
165 my @info = ($server_address,
166 $SIPackages_key,
167 );
168 return \@info;
169 }
172 sub do_wake {
173 my $host = shift;
174 my $ipaddr = shift || '255.255.255.255';
175 my $port = getservbyname('discard', 'udp');
177 my ($raddr, $them, $proto);
178 my ($hwaddr, $hwaddr_re, $pkt);
180 # get the hardware address (ethernet address)
182 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
183 if ($host =~ m/^$hwaddr_re$/) {
184 $hwaddr = $host;
185 } else {
186 # $host is not a hardware address, try to resolve it
187 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
188 my $ip_addr;
189 if ($host =~ m/^$ip_re$/) {
190 $ip_addr = $host;
191 } else {
192 my $h;
193 unless ($h = gethost($host)) {
194 return undef;
195 }
196 $ip_addr = inet_ntoa($h->addr);
197 }
198 }
200 # Generate magic sequence
201 foreach (split /:/, $hwaddr) {
202 $pkt .= chr(hex($_));
203 }
204 $pkt = chr(0xFF) x 6 . $pkt x 16;
206 # Allocate socket and send packet
208 $raddr = gethostbyname($ipaddr)->addr;
209 $them = pack_sockaddr_in($port, $raddr);
210 $proto = getprotobyname('udp');
212 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
213 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
215 send(S, $pkt, 0, $them) or die "send : $!";
216 close S;
217 }
220 #=== FUNCTION ================================================================
221 # NAME: read_configfile
222 # PARAMETERS: cfg_file - string -
223 # RETURNS: nothing
224 # DESCRIPTION: read cfg_file and set variables
225 #===============================================================================
226 sub read_configfile {
227 my $cfg;
228 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
229 if( -r $main::cfg_file ) {
230 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
231 } else {
232 print STDERR "Couldn't read config file!";
233 }
234 } else {
235 $cfg = Config::IniFiles->new() ;
236 }
237 foreach my $section (keys %cfg_defaults) {
238 foreach my $param (keys %{$cfg_defaults{ $section }}) {
239 my $pinfo = $cfg_defaults{ $section }{ $param };
240 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
241 }
242 }
244 # Read non predefined sections
245 my $param;
246 if ($cfg->SectionExists('ldap')){
247 foreach $param ($cfg->Parameters('ldap')){
248 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
249 }
250 }
251 if ($cfg->SectionExists('pam_ldap')){
252 foreach $param ($cfg->Parameters('pam_ldap')){
253 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
254 }
255 }
256 if ($cfg->SectionExists('nss_ldap')){
257 foreach $param ($cfg->Parameters('nss_ldap')){
258 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
259 }
260 }
261 if ($cfg->SectionExists('goto')){
262 $goto_admin= $cfg->val('goto', 'terminal_admin');
263 $goto_secret= $cfg->val('goto', 'terminal_secret');
264 } else {
265 $goto_admin= undef;
266 $goto_secret= undef;
267 }
269 }
271 #=== FUNCTION ================================================================
272 # NAME: get_interface_for_ip
273 # PARAMETERS: ip address (i.e. 192.168.0.1)
274 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
275 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
276 #===============================================================================
277 sub get_interface_for_ip {
278 my $result;
279 my $ip= shift;
280 if ($ip && length($ip) > 0) {
281 my @ifs= &get_interfaces();
282 if($ip eq "0.0.0.0") {
283 $result = "all";
284 } else {
285 foreach (@ifs) {
286 my $if=$_;
287 if(get_ip($if) eq $ip) {
288 $result = $if;
289 }
290 }
291 }
292 }
293 return $result;
294 }
296 #=== FUNCTION ================================================================
297 # NAME: get_interfaces
298 # PARAMETERS: none
299 # RETURNS: (list of interfaces)
300 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
301 #===============================================================================
302 sub get_interfaces {
303 my @result;
304 my $PROC_NET_DEV= ('/proc/net/dev');
306 open(PROC_NET_DEV, "<$PROC_NET_DEV")
307 or die "Could not open $PROC_NET_DEV";
309 my @ifs = <PROC_NET_DEV>;
311 close(PROC_NET_DEV);
313 # Eat first two line
314 shift @ifs;
315 shift @ifs;
317 chomp @ifs;
318 foreach my $line(@ifs) {
319 my $if= (split /:/, $line)[0];
320 $if =~ s/^\s+//;
321 push @result, $if;
322 }
324 return @result;
325 }
327 #=== FUNCTION ================================================================
328 # NAME: get_mac
329 # PARAMETERS: interface name (i.e. eth0)
330 # RETURNS: (mac address)
331 # DESCRIPTION: Uses ioctl to get mac address directly from system.
332 #===============================================================================
333 sub get_mac {
334 my $ifreq= shift;
335 my $result;
336 if ($ifreq && length($ifreq) > 0) {
337 if($ifreq eq "all") {
338 $result = "00:00:00:00:00:00";
339 } else {
340 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
342 # A configured MAC Address should always override a guessed value
343 if ($server_mac_address and length($server_mac_address) > 0) {
344 $result= $server_mac_address;
345 }
347 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
348 or die "socket: $!";
350 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
351 my ($if, $mac)= unpack 'h36 H12', $ifreq;
353 if (length($mac) > 0) {
354 $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])$/;
355 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
356 $result = $mac;
357 }
358 }
359 }
360 }
361 return $result;
362 }
365 #=== FUNCTION ================================================================
366 # NAME: register_at_bus
367 # PARAMETERS: nothing
368 # RETURNS: nothing
369 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
370 #===============================================================================
371 sub register_at_bus {
373 # add bus to known_server_db
374 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
375 primkey=>'hostname',
376 hostname=>$bus_address,
377 status=>'bus',
378 hostkey=>$bus_key,
379 timestamp=>&get_time,
380 } );
381 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
382 my $msg = &create_xml_string($msg_hash);
384 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
385 return $msg;
386 }
389 #=== FUNCTION ================================================================
390 # NAME: process_incoming_msg
391 # PARAMETERS: crypted_msg - string - incoming crypted message
392 # RETURNS: nothing
393 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
394 #===============================================================================
395 sub process_incoming_msg {
396 my ($msg, $msg_hash, $remote_ip) = @_ ;
397 my $error = 0;
398 my $host_name;
399 my $host_key;
400 my @out_msg_l;
402 # process incoming msg
403 my $header = @{$msg_hash->{header}}[0];
404 my @target_l = @{$msg_hash->{target}};
406 &main::daemon_log("SIPackages: msg to process: $header", 3);
407 &main::daemon_log("$msg", 8);
409 if( 0 == length @target_l){
410 &main::daemon_log("ERROR: no target specified for msg $header", 1);
411 $error++;
412 }
414 if( 1 == length @target_l) {
415 my $target = $target_l[0];
416 if(&server_matches($target)) {
417 if ($header eq 'new_key') {
418 @out_msg_l = &new_key($msg_hash)
419 } elsif ($header eq 'here_i_am') {
420 @out_msg_l = &here_i_am($msg_hash)
421 } elsif ($header eq 'who_has') {
422 @out_msg_l = &who_has($msg_hash)
423 } elsif ($header eq 'who_has_i_do') {
424 @out_msg_l = &who_has_i_do($msg_hash)
425 } elsif ($header eq 'got_ping') {
426 @out_msg_l = &got_ping($msg_hash)
427 } elsif ($header eq 'get_load') {
428 @out_msg_l = &execute_actions($msg_hash)
429 } elsif ($header eq 'detected_hardware') {
430 @out_msg_l = &process_detected_hardware($msg_hash)
431 } elsif ($header eq 'trigger_wake') {
432 foreach (@{$msg_hash->{macAddress}}){
433 &main::daemon_log("SIPackages: trigger wake for $_", 1);
434 do_wake($_);
435 }
436 } else {
437 &main::daemon_log("ERROR: $header is an unknown core function", 1);
438 $error++;
439 }
440 } else {
441 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
442 push(@out_msg_l, $msg);
443 }
444 }
446 return \@out_msg_l;
447 }
450 #=== FUNCTION ================================================================
451 # NAME: got_ping
452 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
453 # RETURNS: nothing
454 # DESCRIPTION: process this incoming message
455 #===============================================================================
456 sub got_ping {
457 my ($msg_hash) = @_;
459 my $source = @{$msg_hash->{source}}[0];
460 my $target = @{$msg_hash->{target}}[0];
461 my $header = @{$msg_hash->{header}}[0];
462 my $session_id = @{$msg_hash->{'session_id'}}[0];
463 my $act_time = &get_time;
464 my @out_msg_l;
465 my $out_msg;
467 # check known_clients_db
468 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
469 my $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
470 if( 1 == keys %{$query_res} ) {
471 my $sql_statement= "UPDATE known_clients ".
472 "SET status='$header', timestamp='$act_time' ".
473 "WHERE hostname='$source'";
474 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
475 }
477 # check known_server_db
478 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source'";
479 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
480 if( 1 == keys %{$query_res} ) {
481 my $sql_statement= "UPDATE known_server ".
482 "SET status='$header', timestamp='$act_time' ".
483 "WHERE hostname='$source'";
484 my $res = $main::known_server_db->update_dbentry( $sql_statement );
485 }
487 # create out_msg
488 my $out_hash = &create_xml_hash($header, $source, "GOSA");
489 &add_content2xml_hash($out_hash, "session_id", $session_id);
490 $out_msg = &create_xml_string($out_hash);
491 push(@out_msg_l, $out_msg);
493 return @out_msg_l;
494 }
496 #=== FUNCTION ================================================================
497 # NAME: new_passwd
498 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
499 # RETURNS: nothing
500 # DESCRIPTION: process this incoming message
501 #===============================================================================
502 sub new_key {
503 my ($msg_hash) = @_;
504 my @out_msg_l;
506 my $header = @{$msg_hash->{header}}[0];
507 my $source_name = @{$msg_hash->{source}}[0];
508 my $source_key = @{$msg_hash->{new_key}}[0];
509 my $query_res;
511 # check known_clients_db
512 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
513 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
514 if( 1 == keys %{$query_res} ) {
515 my $act_time = &get_time;
516 my $sql_statement= "UPDATE known_clients ".
517 "SET hostkey='$source_key', timestamp='$act_time' ".
518 "WHERE hostname='$source_name'";
519 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
520 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
521 my $out_msg = &create_xml_string($hash);
522 push(@out_msg_l, $out_msg);
523 }
525 # only do if host still not found
526 if( 0 == @out_msg_l ) {
527 # check known_server_db
528 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
529 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
530 if( 1 == keys %{$query_res} ) {
531 my $act_time = &get_time;
532 my $sql_statement= "UPDATE known_server ".
533 "SET hostkey='$source_key', timestamp='$act_time' ".
534 "WHERE hostname='$source_name'";
535 my $res = $main::known_server_db->update_dbentry( $sql_statement );
537 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
538 my $out_msg = &create_xml_string($hash);
539 push(@out_msg_l, $out_msg);
540 }
541 }
543 return @out_msg_l;
544 }
547 #=== FUNCTION ================================================================
548 # NAME: here_i_am
549 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
550 # RETURNS: nothing
551 # DESCRIPTION: process this incoming message
552 #===============================================================================
553 sub here_i_am {
554 my ($msg_hash) = @_;
555 my @out_msg_l;
556 my $out_hash;
558 my $source = @{$msg_hash->{source}}[0];
559 my $mac_address = @{$msg_hash->{mac_address}}[0];
560 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
562 # number of known clients
563 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
565 # check wether client address or mac address is already known
566 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
567 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
569 if ( 1 == keys %{$db_res} ) {
570 &main::daemon_log("WARNING: $source is already known as a client", 1);
571 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
572 $nu_clients --;
573 }
575 # number of actual activ clients
576 my $act_nu_clients = $nu_clients;
578 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
579 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
581 if($max_clients <= $act_nu_clients) {
582 my $out_hash = &create_xml_hash("denied", $server_address, $source);
583 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
584 my $passwd = @{$msg_hash->{new_passwd}}[0];
585 &send_msg_hash2address($out_hash, $source, $passwd);
586 return;
587 }
589 # new client accepted
590 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
592 # create entry in known_clients
593 my $events = @{$msg_hash->{events}}[0];
596 # add entry to known_clients_db
597 my $act_timestamp = &get_time;
598 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
599 primkey=>'hostname',
600 hostname=>$source,
601 events=>$events,
602 macaddress=>$mac_address,
603 status=>'registered',
604 hostkey=>$new_passwd,
605 timestamp=>$act_timestamp,
606 } );
608 if ($res != 0) {
609 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
610 return;
611 }
613 # return acknowledgement to client
614 $out_hash = &create_xml_hash("registered", $server_address, $source);
615 my $register_out = &create_xml_string($out_hash);
616 push(@out_msg_l, $register_out);
618 # notify registered client to bus
619 if( $bus_activ eq "on") {
620 # fetch actual bus key
621 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
622 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
623 my $hostkey = $query_res->{1}->{'hostkey'};
625 # send update msg to bus
626 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
627 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
628 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
629 my $new_client_out = &create_xml_string($out_hash);
630 push(@out_msg_l, $new_client_out);
631 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
632 }
634 # give the new client his ldap config
635 my $new_ldap_config_out = &new_ldap_config($source);
636 if( $new_ldap_config_out ) {
637 push(@out_msg_l, $new_ldap_config_out);
638 }
640 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
641 if( $hardware_config_out ) {
642 push(@out_msg_l, $hardware_config_out);
643 }
645 return @out_msg_l;
646 }
649 #=== FUNCTION ================================================================
650 # NAME: who_has
651 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
652 # RETURNS: nothing
653 # DESCRIPTION: process this incoming message
654 #===============================================================================
655 sub who_has {
656 my ($msg_hash) = @_ ;
657 my @out_msg_l;
659 # what is your search pattern
660 my $search_pattern = @{$msg_hash->{who_has}}[0];
661 my $search_element = @{$msg_hash->{$search_pattern}}[0];
662 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
664 # scanning known_clients for search_pattern
665 my @host_addresses = keys %$main::known_clients;
666 my $known_clients_entries = length @host_addresses;
667 my $host_address;
668 foreach my $host (@host_addresses) {
669 my $client_element = $main::known_clients->{$host}->{$search_pattern};
670 if ($search_element eq $client_element) {
671 $host_address = $host;
672 last;
673 }
674 }
676 # search was successful
677 if (defined $host_address) {
678 my $source = @{$msg_hash->{source}}[0];
679 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
680 &add_content2xml_hash($out_hash, "mac_address", $search_element);
681 my $out_msg = &create_xml_string($out_hash);
682 push(@out_msg_l, $out_msg);
683 }
684 return @out_msg_l;
685 }
688 sub who_has_i_do {
689 my ($msg_hash) = @_ ;
690 my $header = @{$msg_hash->{header}}[0];
691 my $source = @{$msg_hash->{source}}[0];
692 my $search_param = @{$msg_hash->{$header}}[0];
693 my $search_value = @{$msg_hash->{$search_param}}[0];
694 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
695 }
697 #=== FUNCTION ================================================================
698 # NAME: new_ldap_config
699 # PARAMETERS: address - string - ip address and port of a host
700 # RETURNS: nothing
701 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
702 #===============================================================================
703 sub new_ldap_config {
704 my ($address) = @_ ;
706 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
707 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
709 # check hit
710 my $hit_counter = keys %{$res};
711 if( not $hit_counter == 1 ) {
712 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
713 }
715 my $macaddress = $res->{1}->{macaddress};
716 my $hostkey = $res->{1}->{hostkey};
718 if (not defined $macaddress) {
719 &main::daemon_log("ERROR: no mac address found for client $address", 1);
720 return;
721 }
723 # Build LDAP connection
724 my $ldap = Net::LDAP->new($ldap_uri);
725 if( not defined $ldap ) {
726 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
727 return;
728 }
731 # Bind to a directory with dn and password
732 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
734 # Perform search
735 $mesg = $ldap->search( base => $ldap_base,
736 scope => 'sub',
737 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
738 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
739 #$mesg->code && die $mesg->error;
740 if($mesg->code) {
741 &main::daemon_log($mesg->error, 1);
742 return;
743 }
745 # Sanity check
746 if ($mesg->count != 1) {
747 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
748 &main::daemon_log("\tbase: $ldap_base", 1);
749 &main::daemon_log("\tscope: sub", 1);
750 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
751 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
752 return;
753 }
755 my $entry= $mesg->entry(0);
756 my $dn= $entry->dn;
757 my @servers= $entry->get_value("gotoLdapServer");
758 my $unit_tag= $entry->get_value("gosaUnitTag");
759 my @ldap_uris;
760 my $server;
761 my $base;
763 # Do we need to look at an object class?
764 if (length(@servers) < 1){
765 $mesg = $ldap->search( base => $ldap_base,
766 scope => 'sub',
767 attrs => ['dn', 'gotoLdapServer'],
768 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
769 #$mesg->code && die $mesg->error;
770 if($mesg->code) {
771 &main::daemon_log($mesg->error, 1);
772 return;
773 }
775 # Sanity check
776 if ($mesg->count != 1) {
777 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
778 return;
779 }
781 $entry= $mesg->entry(0);
782 $dn= $entry->dn;
783 @servers= $entry->get_value("gotoLdapServer");
784 }
786 @servers= sort (@servers);
788 foreach $server (@servers){
789 # Conversation for backward compatibility
790 if ($server !=~ /^ldap[^:]+:\/\// ) {
791 if ($server =~ /^([^:]+):(.*)$/ ) {
792 $server= "1:dummy:ldap://$1/$2";
793 }
794 }
796 $base= $server;
797 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
798 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
799 push (@ldap_uris, $server);
800 }
802 # Assemble data package
803 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
804 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
806 # Need to append GOto settings?
807 if (defined $goto_admin and defined $goto_secret){
808 $data{'goto_admin'}= $goto_admin;
809 $data{'goto_secret'}= $goto_secret;
810 }
812 # Append unit tag if needed
813 if (defined $unit_tag){
815 # Find admin base and department name
816 $mesg = $ldap->search( base => $ldap_base,
817 scope => 'sub',
818 attrs => ['dn', 'ou', 'FAIclass'],
819 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
820 #$mesg->code && die $mesg->error;
821 if($mesg->code) {
822 &main::daemon_log($mesg->error, 1);
823 return;
824 }
826 # Sanity check
827 if ($mesg->count != 1) {
828 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
829 return;
830 }
832 $entry= $mesg->entry(0);
833 $data{'admin_base'}= $entry->dn;
834 $data{'department'}= $entry->get_value("ou");
836 # Append unit Tag
837 $data{'unit_tag'}= $unit_tag;
838 }
840 # Fill release if available
841 my $FAIclass= $entry->get_value("FAIclass");
842 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
843 $data{'release'}= $1;
844 }
847 # Unbind
848 $mesg = $ldap->unbind;
850 # Send information
851 return &build_msg("new_ldap_config", $server_address, $address, \%data);
852 }
854 sub process_detected_hardware {
855 my $msg_hash = shift;
856 my $address = $msg_hash->{source}[0];
857 my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
859 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
860 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
862 # check hit
863 my $hit_counter = keys %{$res};
864 if( not $hit_counter == 1 ) {
865 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
866 return;
867 }
869 my $macaddress = $res->{1}->{macaddress};
870 my $hostkey = $res->{1}->{hostkey};
872 if (not defined $macaddress) {
873 &main::daemon_log("ERROR: no mac address found for client $address", 1);
874 return;
875 }
876 # Build LDAP connection
877 my $ldap = Net::LDAP->new($ldap_uri);
878 if( not defined $ldap ) {
879 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
880 return;
881 }
883 # Bind to a directory with dn and password
884 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
886 # Perform search
887 $mesg = $ldap->search(
888 base => $ldap_base,
889 scope => 'sub',
890 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
891 );
893 # We need to create a base entry first (if not done from ArpHandler)
894 if($mesg->count == 0) {
895 &main::daemon_log("Need to create a new LDAP Entry for client $address", 1);
896 my $resolver=Net::DNS::Resolver->new;
897 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
898 my $dnsresult= $resolver->search($ipaddress);
899 my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
900 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
901 my $dn = "cn=$cn,ou=incoming,$ldap_base";
902 &main::daemon_log("Creating entry for $dn",6);
903 my $entry= Net::LDAP::Entry->new( $dn );
904 $entry->dn($dn);
905 $entry->add("objectClass" => "goHard");
906 $entry->add("cn" => $cn);
907 $entry->add("macAddress" => $macaddress);
908 $entry->add("gotomode" => "locked");
909 $entry->add("gotoSysStatus" => "new-system");
910 $entry->add("ipHostNumber" => $ipaddress);
911 if(defined($gosa_unit_tag) && length($gosa_unit_tag) > 0) {
912 $entry->add("objectClass" => "gosaAdministrativeUnit");
913 $entry->add("gosaUnitTag" => $gosa_unit_tag);
914 }
915 my $res=$entry->update($ldap);
916 if(defined($res->{'errorMessage'}) &&
917 length($res->{'errorMessage'}) >0) {
918 &main::daemon_log("There was a problem adding the entries to LDAP:", 1);
919 &main::daemon_log($res->{'errorMessage'}, 1);
920 return;
921 } else {
922 # Fill $mesg again
923 $mesg = $ldap->search(
924 base => $ldap_base,
925 scope => 'sub',
926 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
927 );
928 }
929 }
931 if($mesg->count == 1) {
932 my $entry= $mesg->entry(0);
933 $entry->changetype("modify");
934 foreach my $attribute (
935 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
936 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
937 "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
938 if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
939 length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
940 if(defined($entry->get_value($attribute))) {
941 $entry->delete($attribute);
942 }
943 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
944 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
945 }
946 }
947 foreach my $attribute (
948 "gotoModules", "ghScsiDev", "ghIdeDev") {
949 if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
950 length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
951 if(defined($entry->get_value($attribute))) {
952 $entry->delete($attribute);
953 }
954 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
955 $entry->add($attribute => $array_entry);
956 }
957 }
958 }
960 my $res=$entry->update($ldap);
961 if(defined($res->{'errorMessage'}) &&
962 length($res->{'errorMessage'}) >0) {
963 &main::daemon_log("There was a problem adding the entries to LDAP:", 1);
964 &main::daemon_log($res->{'errorMessage'}, 1);
965 } else {
966 &main::daemon_log("Added Hardware configuration to LDAP", 4);
967 }
969 }
970 return;
971 }
972 #=== FUNCTION ================================================================
973 # NAME: hardware_config
974 # PARAMETERS: address - string - ip address and port of a host
975 # RETURNS:
976 # DESCRIPTION:
977 #===============================================================================
978 sub hardware_config {
979 my ($address, $gotoHardwareChecksum) = @_ ;
981 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
982 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
984 # check hit
985 my $hit_counter = keys %{$res};
986 if( not $hit_counter == 1 ) {
987 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
988 }
990 my $macaddress = $res->{1}->{macaddress};
991 my $hostkey = $res->{1}->{hostkey};
993 if (not defined $macaddress) {
994 &main::daemon_log("ERROR: no mac address found for client $address", 1);
995 return;
996 }
998 # Build LDAP connection
999 my $ldap = Net::LDAP->new($ldap_uri);
1000 if( not defined $ldap ) {
1001 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
1002 return;
1003 }
1005 # Bind to a directory with dn and password
1006 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
1008 # Perform search
1009 $mesg = $ldap->search(
1010 base => $ldap_base,
1011 scope => 'sub',
1012 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
1013 );
1015 if($mesg->count() == 0) {
1016 &main::daemon_log("Host was not found in LDAP!", 1);
1017 } else {
1018 my $entry= $mesg->entry(0);
1019 my $dn= $entry->dn;
1020 if(defined($entry->get_value("gotoHardwareChecksum"))) {
1021 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
1022 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
1023 if($entry->update($ldap)) {
1024 &main::daemon_log("Hardware changed! Detection triggered.", 4);
1025 }
1026 } else {
1027 # Nothing to do
1028 return;
1029 }
1030 }
1031 }
1032 # need to fill it to LDAP
1033 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
1034 #if($entry->update($ldap)) {
1035 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
1036 #}
1038 ## Look if there another host with this checksum to use the hardware config
1039 #$mesg = $ldap->search(
1040 # base => $ldap_base,
1041 # scope => 'sub',
1042 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
1043 #);
1045 #if($mesg->count>1) {
1046 # my $clone_entry= $mesg->entry(0);
1047 # $entry->changetype("modify");
1048 # foreach my $attribute (
1049 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
1050 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
1051 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
1052 # my $value= $clone_entry->get_value($attribute);
1053 # if(defined($value)) {
1054 # if(defined($entry->get_value($attribute))) {
1055 # $entry->delete($attribute);
1056 # }
1057 # &main::daemon_log("Adding attribute $attribute with value $value",1);
1058 # $entry->add($attribute => $value);
1059 # }
1060 # }
1061 # foreach my $attribute (
1062 # "gotoModules", "ghScsiDev", "ghIdeDev") {
1063 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
1064 # if(defined($array)) {
1065 # if(defined($entry->get_value($attribute))) {
1066 # $entry->delete($attribute);
1067 # }
1068 # foreach my $array_entry (@{$array}) {
1069 # $entry->add($attribute => $array_entry);
1070 # }
1071 # }
1073 # }
1074 # if($entry->update($ldap)) {
1075 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1076 # }
1078 #}
1081 # Assemble data package
1082 my %data = ();
1084 # Need to append GOto settings?
1085 if (defined $goto_admin and defined $goto_secret){
1086 $data{'goto_admin'}= $goto_admin;
1087 $data{'goto_secret'}= $goto_secret;
1088 }
1090 # Unbind
1091 $mesg = $ldap->unbind;
1093 &main::daemon_log("Send detect_hardware message to $address", 4);
1095 # Send information
1096 return &build_msg("detect_hardware", $server_address, $address, \%data);
1097 }
1099 sub server_matches {
1100 my $target = shift;
1101 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
1102 my $result = 0;
1104 if($server_ip eq $target_ip) {
1105 $result= 1;
1106 } elsif ($server_ip eq "0.0.0.0") {
1107 if ($target_ip eq "127.0.0.1") {
1108 $result= 1;
1109 } else {
1110 my $PROC_NET_ROUTE= ('/proc/net/route');
1112 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1113 or die "Could not open $PROC_NET_ROUTE";
1115 my @ifs = <PROC_NET_ROUTE>;
1117 close(PROC_NET_ROUTE);
1119 # Eat header line
1120 shift @ifs;
1121 chomp @ifs;
1122 foreach my $line(@ifs) {
1123 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1124 my $destination;
1125 my $mask;
1126 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1127 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1128 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1129 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1130 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1131 # destination matches route, save mac and exit
1132 $result= 1;
1133 last;
1134 }
1135 }
1136 }
1137 } else {
1138 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1139 }
1141 return $result;
1142 }
1145 #=== FUNCTION ================================================================
1146 # NAME: execute_actions
1147 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1148 # RETURNS: nothing
1149 # DESCRIPTION: invokes the script specified in msg_hash which is located under
1150 # /etc/gosad/actions
1151 #===============================================================================
1152 sub execute_actions {
1153 my ($msg_hash) = @_ ;
1154 my $configdir= '/etc/gosad/actions/';
1155 my $result;
1157 my $header = @{$msg_hash->{header}}[0];
1158 my $source = @{$msg_hash->{source}}[0];
1159 my $target = @{$msg_hash->{target}}[0];
1161 if((not defined $source)
1162 && (not defined $target)
1163 && (not defined $header)) {
1164 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1165 } else {
1166 my $parameters="";
1167 my @params = @{$msg_hash->{$header}};
1168 my $params = join(", ", @params);
1169 &main::daemon_log("execute_actions: got parameters: $params", 5);
1171 if (@params) {
1172 foreach my $param (@params) {
1173 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1174 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1175 $parameters.= " ".$param_value;
1176 }
1177 }
1179 my $cmd= $configdir.$header."$parameters";
1180 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1181 $result= "";
1182 open(PIPE, "$cmd 2>&1 |");
1183 while(<PIPE>) {
1184 $result.=$_;
1185 }
1186 close(PIPE);
1187 }
1189 # process the event result
1192 return;
1193 }
1196 #vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1197 1;