0af55bcae77dc8fd71ea961561a13e719f84f79a
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 Net::LDAP;
16 use Socket;
17 use Net::hostent;
18 use Net::DNS;
20 BEGIN{}
21 END {}
23 my ($known_clients_file_name);
24 my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
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);
32 my %cfg_defaults =
33 (
34 "server" =>
35 {"server_activ" => [\$server_activ, "on"],
36 "server_ip" => [\$server_ip, "0.0.0.0"],
37 "server_mac_address" => [\$server_mac_address, ""],
38 "server_port" => [\$server_port, "20081"],
39 "SIPackages_key" => [\$SIPackages_key, ""],
40 "max_clients" => [\$max_clients, 100],
41 "ldap_uri" => [\$ldap_uri, ""],
42 "ldap_base" => [\$ldap_base, ""],
43 "ldap_admin_dn" => [\$ldap_admin_dn, ""],
44 "ldap_admin_password" => [\$ldap_admin_password, ""],
45 },
46 "bus" =>
47 {"bus_activ" => [\$bus_activ, "on"],
48 "bus_passwd" => [\$bus_key, ""],
49 "bus_ip" => [\$bus_ip, ""],
50 "bus_port" => [\$bus_port, "20080"],
51 },
52 );
54 ### START #####################################################################
56 # read configfile and import variables
57 &read_configfile();
59 # detect interfaces and mac address
60 $network_interface= &get_interface_for_ip($server_ip);
61 $server_mac_address= &get_mac($network_interface);
63 # complete addresses
64 if( $server_ip eq "0.0.0.0" ) {
65 $server_ip = "127.0.0.1";
66 }
67 my $server_address = "$server_ip:$server_port";
68 my $bus_address = "$bus_ip:$bus_port";
70 # create general settings for this module
71 my $xml = new XML::Simple();
73 # register at bus
74 if ($main::no_bus > 0) {
75 $bus_activ = "off"
76 }
77 if($bus_activ eq "on") {
78 ®ister_at_bus();
79 }
81 # add myself to known_server_db
82 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
83 primkey=>'hostname',
84 hostname=>$server_address,
85 status=>'myself',
86 hostkey=>$SIPackages_key,
87 timestamp=>&get_time,
88 } );
92 ### functions #################################################################
95 sub get_module_info {
96 my @info = ($server_address,
97 $SIPackages_key,
98 $server,
99 $server_activ,
100 "socket",
101 );
102 return \@info;
103 }
107 sub do_wake {
108 my $host = shift;
109 my $ipaddr = shift || '255.255.255.255';
110 my $port = getservbyname('discard', 'udp');
112 my ($raddr, $them, $proto);
113 my ($hwaddr, $hwaddr_re, $pkt);
115 # get the hardware address (ethernet address)
117 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
118 if ($host =~ m/^$hwaddr_re$/) {
119 $hwaddr = $host;
120 } else {
121 # $host is not a hardware address, try to resolve it
122 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
123 my $ip_addr;
124 if ($host =~ m/^$ip_re$/) {
125 $ip_addr = $host;
126 } else {
127 my $h;
128 unless ($h = gethost($host)) {
129 return undef;
130 }
131 $ip_addr = inet_ntoa($h->addr);
132 }
133 }
135 # Generate magic sequence
136 foreach (split /:/, $hwaddr) {
137 $pkt .= chr(hex($_));
138 }
139 $pkt = chr(0xFF) x 6 . $pkt x 16;
141 # Allocate socket and send packet
143 $raddr = gethostbyname($ipaddr)->addr;
144 $them = pack_sockaddr_in($port, $raddr);
145 $proto = getprotobyname('udp');
147 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
148 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
150 send(S, $pkt, 0, $them) or die "send : $!";
151 close S;
152 }
155 #=== FUNCTION ================================================================
156 # NAME: read_configfile
157 # PARAMETERS: cfg_file - string -
158 # RETURNS: nothing
159 # DESCRIPTION: read cfg_file and set variables
160 #===============================================================================
161 sub read_configfile {
162 my $cfg;
163 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
164 if( -r $main::cfg_file ) {
165 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
166 } else {
167 print STDERR "Couldn't read config file!";
168 }
169 } else {
170 $cfg = Config::IniFiles->new() ;
171 }
172 foreach my $section (keys %cfg_defaults) {
173 foreach my $param (keys %{$cfg_defaults{ $section }}) {
174 my $pinfo = $cfg_defaults{ $section }{ $param };
175 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
176 }
177 }
179 # Read non predefined sections
180 my $param;
181 if ($cfg->SectionExists('ldap')){
182 foreach $param ($cfg->Parameters('ldap')){
183 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
184 }
185 }
186 if ($cfg->SectionExists('pam_ldap')){
187 foreach $param ($cfg->Parameters('pam_ldap')){
188 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
189 }
190 }
191 if ($cfg->SectionExists('nss_ldap')){
192 foreach $param ($cfg->Parameters('nss_ldap')){
193 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
194 }
195 }
196 if ($cfg->SectionExists('goto')){
197 $goto_admin= $cfg->val('goto', 'terminal_admin');
198 $goto_secret= $cfg->val('goto', 'terminal_secret');
199 } else {
200 $goto_admin= undef;
201 $goto_secret= undef;
202 }
204 }
206 #=== FUNCTION ================================================================
207 # NAME: get_interface_for_ip
208 # PARAMETERS: ip address (i.e. 192.168.0.1)
209 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
210 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
211 #===============================================================================
212 sub get_interface_for_ip {
213 my $result;
214 my $ip= shift;
215 if ($ip && length($ip) > 0) {
216 my @ifs= &get_interfaces();
217 if($ip eq "0.0.0.0") {
218 $result = "all";
219 } else {
220 foreach (@ifs) {
221 my $if=$_;
222 if(get_ip($if) eq $ip) {
223 $result = $if;
224 }
225 }
226 }
227 }
228 return $result;
229 }
231 #=== FUNCTION ================================================================
232 # NAME: get_interfaces
233 # PARAMETERS: none
234 # RETURNS: (list of interfaces)
235 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
236 #===============================================================================
237 sub get_interfaces {
238 my @result;
239 my $PROC_NET_DEV= ('/proc/net/dev');
241 open(PROC_NET_DEV, "<$PROC_NET_DEV")
242 or die "Could not open $PROC_NET_DEV";
244 my @ifs = <PROC_NET_DEV>;
246 close(PROC_NET_DEV);
248 # Eat first two line
249 shift @ifs;
250 shift @ifs;
252 chomp @ifs;
253 foreach my $line(@ifs) {
254 my $if= (split /:/, $line)[0];
255 $if =~ s/^\s+//;
256 push @result, $if;
257 }
259 return @result;
260 }
262 #=== FUNCTION ================================================================
263 # NAME: get_mac
264 # PARAMETERS: interface name (i.e. eth0)
265 # RETURNS: (mac address)
266 # DESCRIPTION: Uses ioctl to get mac address directly from system.
267 #===============================================================================
268 sub get_mac {
269 my $ifreq= shift;
270 my $result;
271 if ($ifreq && length($ifreq) > 0) {
272 if($ifreq eq "all") {
273 $result = "00:00:00:00:00:00";
274 } else {
275 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
277 # A configured MAC Address should always override a guessed value
278 if ($server_mac_address and length($server_mac_address) > 0) {
279 $result= $server_mac_address;
280 }
282 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
283 or die "socket: $!";
285 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
286 my ($if, $mac)= unpack 'h36 H12', $ifreq;
288 if (length($mac) > 0) {
289 $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])$/;
290 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
291 $result = $mac;
292 }
293 }
294 }
295 }
296 return $result;
297 }
299 #=== FUNCTION ================================================================
300 # NAME: get_ip
301 # PARAMETERS: interface name (i.e. eth0)
302 # RETURNS: (ip address)
303 # DESCRIPTION: Uses ioctl to get ip address directly from system.
304 #===============================================================================
305 sub get_ip {
306 my $ifreq= shift;
307 my $result= "";
308 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
309 my $proto= getprotobyname('ip');
311 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
312 or die "socket: $!";
314 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
315 my ($if, $sin) = unpack 'a16 a16', $ifreq;
316 my ($port, $addr) = sockaddr_in $sin;
317 my $ip = inet_ntoa $addr;
319 if ($ip && length($ip) > 0) {
320 $result = $ip;
321 }
322 }
324 return $result;
325 }
328 #=== FUNCTION ================================================================
329 # NAME: register_at_bus
330 # PARAMETERS: nothing
331 # RETURNS: nothing
332 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
333 #===============================================================================
334 sub register_at_bus {
336 # add bus to known_server_db
337 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
338 primkey=>'hostname',
339 hostname=>$bus_address,
340 status=>'bus',
341 hostkey=>$bus_key,
342 timestamp=>&get_time,
343 } );
344 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
345 my $msg = &create_xml_string($msg_hash);
347 print STDERR "bus_key:$bus_key\n";
348 print STDERR "msg:$msg\n";
351 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
352 return $msg;
353 # my $answer = "";
354 # $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
355 # if ($answer == 0) {
356 # &main::daemon_log("register at bus: $bus_address", 1);
357 # } else {
358 # &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
359 # }
360 # return;
361 }
364 #=== FUNCTION ================================================================
365 # NAME: process_incoming_msg
366 # PARAMETERS: crypted_msg - string - incoming crypted message
367 # RETURNS: nothing
368 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
369 #===============================================================================
370 sub process_incoming_msg {
371 my ($msg, $msg_hash, $remote_ip) = @_ ;
372 my $error = 0;
373 my $host_name;
374 my $host_key;
375 my @out_msg_l;
377 # process incoming msg
378 my $header = @{$msg_hash->{header}}[0];
379 my @target_l = @{$msg_hash->{target}};
381 &main::daemon_log("SIPackages: msg to process: $header", 3);
382 &main::daemon_log("$msg", 8);
384 if( 0 == length @target_l){
385 &main::daemon_log("ERROR: no target specified for msg $header", 1);
386 $error++;
387 }
389 if( 1 == length @target_l) {
390 my $target = $target_l[0];
391 if( $target eq $server_address ) {
392 if ($header eq 'new_key') {
393 @out_msg_l = &new_key($msg_hash)
394 } elsif ($header eq 'here_i_am') {
395 @out_msg_l = &here_i_am($msg_hash)
396 } elsif ($header eq 'who_has') {
397 @out_msg_l = &who_has($msg_hash)
398 } elsif ($header eq 'who_has_i_do') {
399 @out_msg_l = &who_has_i_do($msg_hash)
400 } elsif ($header eq 'got_ping') {
401 @out_msg_l = &got_ping($msg_hash)
402 } elsif ($header eq 'get_load') {
403 @out_msg_l = &execute_actions($msg_hash)
404 } elsif ($header eq 'detected_hardware') {
405 @out_msg_l = &process_detected_hardware($msg_hash)
406 } elsif ($header eq 'trigger_wake') {
407 foreach (@{$msg_hash->{macAddress}}){
408 &main::daemon_log("SIPackages: trigger wake for $_", 1);
409 do_wake($_);
410 }
412 } else {
413 &main::daemon_log("ERROR: $header is an unknown core function", 1);
414 $error++;
415 }
416 }
417 else {
418 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
419 push(@out_msg_l, $msg);
420 }
421 }
423 if( $error == 0) {
424 if( 0 == @out_msg_l ) {
425 push(@out_msg_l, $msg);
426 }
427 }
429 return \@out_msg_l;
430 }
433 #=== FUNCTION ================================================================
434 # NAME: got_ping
435 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
436 # RETURNS: nothing
437 # DESCRIPTION: process this incoming message
438 #===============================================================================
439 sub got_ping {
440 my ($msg_hash) = @_;
442 my $source = @{$msg_hash->{source}}[0];
443 my $target = @{$msg_hash->{target}}[0];
444 my $header = @{$msg_hash->{header}}[0];
446 if(exists $main::known_daemons->{$source}) {
447 &main::add_content2known_daemons(hostname=>$source, status=>$header);
448 } else {
449 &main::add_content2known_clients(hostname=>$source, status=>$header);
450 }
452 return;
453 }
456 #=== FUNCTION ================================================================
457 # NAME: new_passwd
458 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
459 # RETURNS: nothing
460 # DESCRIPTION: process this incoming message
461 #===============================================================================
462 sub new_key {
463 my ($msg_hash) = @_;
464 my @out_msg_l;
466 my $header = @{$msg_hash->{header}}[0];
467 my $source_name = @{$msg_hash->{source}}[0];
468 my $source_key = @{$msg_hash->{new_key}}[0];
469 my $query_res;
471 # check known_clients_db
472 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
473 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
474 if( 1 == keys %{$query_res} ) {
475 my $act_time = &get_time;
476 my $sql_statement= "UPDATE known_clients ".
477 "SET hostkey='$source_key', timestamp='$act_time' ".
478 "WHERE hostname='$source_name'";
479 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
480 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
481 my $out_msg = &create_xml_string($hash);
482 push(@out_msg_l, $out_msg);
483 }
485 # only do if host still not found
486 if( 0 == @out_msg_l ) {
487 # check known_server_db
488 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
489 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
490 if( 1 == keys %{$query_res} ) {
491 my $act_time = &get_time;
492 my $sql_statement= "UPDATE known_server ".
493 "SET hostkey='$source_key', timestamp='$act_time' ".
494 "WHERE hostname='$source_name'";
495 my $res = $main::known_server_db->update_dbentry( $sql_statement );
497 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
498 my $out_msg = &create_xml_string($hash);
499 push(@out_msg_l, $out_msg);
500 }
501 }
503 return @out_msg_l;
504 }
507 #=== FUNCTION ================================================================
508 # NAME: here_i_am
509 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
510 # RETURNS: nothing
511 # DESCRIPTION: process this incoming message
512 #===============================================================================
513 sub here_i_am {
514 my ($msg_hash) = @_;
515 my @out_msg_l;
516 my $out_hash;
518 my $source = @{$msg_hash->{source}}[0];
519 my $mac_address = @{$msg_hash->{mac_address}}[0];
520 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
522 # number of known clients
523 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
525 # check wether client address or mac address is already known
526 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
527 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
529 if ( 1 == keys %{$db_res} ) {
530 &main::daemon_log("WARNING: $source is already known as a client", 1);
531 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
532 $nu_clients --;
533 }
535 # number of actual activ clients
536 my $act_nu_clients = $nu_clients;
538 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
539 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
541 if($max_clients <= $act_nu_clients) {
542 my $out_hash = &create_xml_hash("denied", $server_address, $source);
543 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
544 my $passwd = @{$msg_hash->{new_passwd}}[0];
545 &send_msg_hash2address($out_hash, $source, $passwd);
546 return;
547 }
549 # new client accepted
550 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
552 # create entry in known_clients
553 my $events = @{$msg_hash->{events}}[0];
556 # add entry to known_clients_db
557 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
558 primkey=>'hostname',
559 hostname=>$source,
560 events=>$events,
561 macaddress=>$mac_address,
562 status=>'registered',
563 hostkey=>$new_passwd,
564 timestamp=>&get_time,
565 } );
567 if ($res != 0) {
568 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
569 return;
570 }
572 # return acknowledgement to client
573 $out_hash = &create_xml_hash("registered", $server_address, $source);
574 my $register_out = &create_xml_string($out_hash);
575 push(@out_msg_l, $register_out);
577 # notify registered client to bus
578 if( $bus_activ eq "on") {
579 # fetch actual bus key
580 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
581 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
582 my $hostkey = $query_res->{1}->{'hostkey'};
584 # send update msg to bus
585 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
586 my $new_client_out = &create_xml_string($out_hash);
587 push(@out_msg_l, $new_client_out);
588 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
589 }
591 # give the new client his ldap config
592 my $new_ldap_config_out = &new_ldap_config($source);
593 if( $new_ldap_config_out ) {
594 push(@out_msg_l, $new_ldap_config_out);
595 }
597 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
598 if( $hardware_config_out ) {
599 push(@out_msg_l, $hardware_config_out);
600 }
602 return @out_msg_l;
603 }
606 #=== FUNCTION ================================================================
607 # NAME: who_has
608 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
609 # RETURNS: nothing
610 # DESCRIPTION: process this incoming message
611 #===============================================================================
612 sub who_has {
613 my ($msg_hash) = @_ ;
614 my @out_msg_l;
616 # what is your search pattern
617 my $search_pattern = @{$msg_hash->{who_has}}[0];
618 my $search_element = @{$msg_hash->{$search_pattern}}[0];
619 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
621 # scanning known_clients for search_pattern
622 my @host_addresses = keys %$main::known_clients;
623 my $known_clients_entries = length @host_addresses;
624 my $host_address;
625 foreach my $host (@host_addresses) {
626 my $client_element = $main::known_clients->{$host}->{$search_pattern};
627 if ($search_element eq $client_element) {
628 $host_address = $host;
629 last;
630 }
631 }
633 # search was successful
634 if (defined $host_address) {
635 my $source = @{$msg_hash->{source}}[0];
636 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
637 &add_content2xml_hash($out_hash, "mac_address", $search_element);
638 my $out_msg = &create_xml_string($out_hash);
639 push(@out_msg_l, $out_msg);
640 }
641 return @out_msg_l;
642 }
645 sub who_has_i_do {
646 my ($msg_hash) = @_ ;
647 my $header = @{$msg_hash->{header}}[0];
648 my $source = @{$msg_hash->{source}}[0];
649 my $search_param = @{$msg_hash->{$header}}[0];
650 my $search_value = @{$msg_hash->{$search_param}}[0];
651 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
652 }
654 #=== FUNCTION ================================================================
655 # NAME: new_ldap_config
656 # PARAMETERS: address - string - ip address and port of a host
657 # RETURNS: nothing
658 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
659 #===============================================================================
660 sub new_ldap_config {
661 my ($address) = @_ ;
663 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
664 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
666 # check hit
667 my $hit_counter = keys %{$res};
668 if( not $hit_counter == 1 ) {
669 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
670 }
672 my $macaddress = $res->{1}->{macaddress};
673 my $hostkey = $res->{1}->{hostkey};
675 if (not defined $macaddress) {
676 &main::daemon_log("ERROR: no mac address found for client $address", 1);
677 return;
678 }
680 # Build LDAP connection
681 my $ldap = Net::LDAP->new($ldap_uri);
682 if( not defined $ldap ) {
683 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
684 return;
685 }
688 # Bind to a directory with dn and password
689 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
691 # Perform search
692 $mesg = $ldap->search( base => $ldap_base,
693 scope => 'sub',
694 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
695 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
696 #$mesg->code && die $mesg->error;
697 if($mesg->code) {
698 &main::daemon_log($mesg->error, 1);
699 return;
700 }
702 # Sanity check
703 if ($mesg->count != 1) {
704 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
705 &main::daemon_log("\tbase: $ldap_base", 1);
706 &main::daemon_log("\tscope: sub", 1);
707 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
708 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
709 return;
710 }
712 my $entry= $mesg->entry(0);
713 my $dn= $entry->dn;
714 my @servers= $entry->get_value("gotoLdapServer");
715 my $unit_tag= $entry->get_value("gosaUnitTag");
716 my @ldap_uris;
717 my $server;
718 my $base;
720 # Do we need to look at an object class?
721 if (length(@servers) < 1){
722 $mesg = $ldap->search( base => $ldap_base,
723 scope => 'sub',
724 attrs => ['dn', 'gotoLdapServer'],
725 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
726 #$mesg->code && die $mesg->error;
727 if($mesg->code) {
728 &main::daemon_log($mesg->error, 1);
729 return;
730 }
732 # Sanity check
733 if ($mesg->count != 1) {
734 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
735 return;
736 }
738 $entry= $mesg->entry(0);
739 $dn= $entry->dn;
740 @servers= $entry->get_value("gotoLdapServer");
741 }
743 @servers= sort (@servers);
745 foreach $server (@servers){
746 $base= $server;
747 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
748 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
749 push (@ldap_uris, $server);
750 }
752 # Assemble data package
753 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
754 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
756 # Need to append GOto settings?
757 if (defined $goto_admin and defined $goto_secret){
758 $data{'goto_admin'}= $goto_admin;
759 $data{'goto_secret'}= $goto_secret;
760 }
762 # Append unit tag if needed
763 if (defined $unit_tag){
765 # Find admin base and department name
766 $mesg = $ldap->search( base => $ldap_base,
767 scope => 'sub',
768 attrs => ['dn', 'ou'],
769 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
770 #$mesg->code && die $mesg->error;
771 if($mesg->code) {
772 &main::daemon_log($mesg->error, 1);
773 return;
774 }
776 # Sanity check
777 if ($mesg->count != 1) {
778 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
779 return;
780 }
782 $entry= $mesg->entry(0);
783 $data{'admin_base'}= $entry->dn;
784 $data{'department'}= $entry->get_value("ou");
786 # Append unit Tag
787 $data{'unit_tag'}= $unit_tag;
788 }
790 # Unbind
791 $mesg = $ldap->unbind;
793 # Send information
794 return send_msg("new_ldap_config", $server_address, $address, \%data);
795 }
797 sub process_detected_hardware {
798 my $msg_hash = shift;
799 my $address = $msg_hash->{source}[0];
800 my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
802 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
803 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
805 # check hit
806 my $hit_counter = keys %{$res};
807 if( not $hit_counter == 1 ) {
808 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
809 return;
810 }
812 my $macaddress = $res->{1}->{macaddress};
813 my $hostkey = $res->{1}->{hostkey};
815 if (not defined $macaddress) {
816 &main::daemon_log("ERROR: no mac address found for client $address", 1);
817 return;
818 }
819 # Build LDAP connection
820 my $ldap = Net::LDAP->new($ldap_uri);
821 if( not defined $ldap ) {
822 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
823 return;
824 }
826 # Bind to a directory with dn and password
827 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
829 # Perform search
830 $mesg = $ldap->search(
831 base => $ldap_base,
832 scope => 'sub',
833 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
834 );
836 # We need to create a base entry first (if not done from ArpHandler)
837 if($mesg->count == 0) {
838 &main::daemon_log("Need to create a new LDAP Entry for client $address", 1);
839 my $resolver=Net::DNS::Resolver->new;
840 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
841 my $dnsresult= $resolver->search($ipaddress);
842 my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
843 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
844 my $dn = "cn=$cn,ou=incoming,$ldap_base";
845 &main::daemon_log("Creating entry for $dn",6);
846 my $entry= Net::LDAP::Entry->new( $dn );
847 $entry->dn($dn);
848 $entry->add("objectClass" => "goHard");
849 $entry->add("cn" => $cn);
850 $entry->add("macAddress" => $macaddress);
851 $entry->add("gotoSysStatus" => "new-system");
852 $entry->add("ipHostNumber" => $ipaddress);
853 if(my $res=$entry->update($ldap)) {
854 # Fill $mesg again
855 $mesg = $ldap->search(
856 base => $ldap_base,
857 scope => 'sub',
858 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
859 );
860 } else {
861 &main::daemon_log("There was a problem adding the entry", 1);
862 }
864 }
866 if($mesg->count == 1) {
867 my $entry= $mesg->entry(0);
868 $entry->changetype("modify");
869 foreach my $attribute (
870 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
871 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
872 "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
873 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
874 if(defined($entry->get_value($attribute))) {
875 $entry->delete($attribute);
876 }
877 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
878 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
879 }
880 }
881 foreach my $attribute (
882 "gotoModules", "ghScsiDev", "ghIdeDev") {
883 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
884 if(defined($entry->get_value($attribute))) {
885 $entry->delete($attribute);
886 }
887 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
888 $entry->add($attribute => $array_entry);
889 }
890 }
892 }
894 if($entry->update($ldap)) {
895 &main::daemon_log("Added Hardware configuration to LDAP", 4);
896 }
898 }
899 return;
900 }
901 #=== FUNCTION ================================================================
902 # NAME: hardware_config
903 # PARAMETERS: address - string - ip address and port of a host
904 # RETURNS:
905 # DESCRIPTION:
906 #===============================================================================
907 sub hardware_config {
908 my ($address, $gotoHardwareChecksum) = @_ ;
910 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
911 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
913 # check hit
914 my $hit_counter = keys %{$res};
915 if( not $hit_counter == 1 ) {
916 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
917 }
919 my $macaddress = $res->{1}->{macaddress};
920 my $hostkey = $res->{1}->{hostkey};
922 if (not defined $macaddress) {
923 &main::daemon_log("ERROR: no mac address found for client $address", 1);
924 return;
925 }
927 # Build LDAP connection
928 my $ldap = Net::LDAP->new($ldap_uri);
929 if( not defined $ldap ) {
930 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
931 return;
932 }
934 # Bind to a directory with dn and password
935 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
937 # Perform search
938 $mesg = $ldap->search(
939 base => $ldap_base,
940 scope => 'sub',
941 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
942 );
944 if($mesg->count() == 0) {
945 &main::daemon_log("Host was not found in LDAP!", 1);
946 } else {
947 my $entry= $mesg->entry(0);
948 my $dn= $entry->dn;
949 if(defined($entry->get_value("gotoHardwareChecksum"))) {
950 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
951 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
952 if($entry->update($ldap)) {
953 &main::daemon_log("Hardware changed! Detection triggered.", 4);
954 }
955 } else {
956 # Nothing to do
957 return;
958 }
959 }
960 }
961 # need to fill it to LDAP
962 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
963 #if($entry->update($ldap)) {
964 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
965 #}
967 ## Look if there another host with this checksum to use the hardware config
968 #$mesg = $ldap->search(
969 # base => $ldap_base,
970 # scope => 'sub',
971 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
972 #);
974 #if($mesg->count>1) {
975 # my $clone_entry= $mesg->entry(0);
976 # $entry->changetype("modify");
977 # foreach my $attribute (
978 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
979 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
980 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
981 # my $value= $clone_entry->get_value($attribute);
982 # if(defined($value)) {
983 # if(defined($entry->get_value($attribute))) {
984 # $entry->delete($attribute);
985 # }
986 # &main::daemon_log("Adding attribute $attribute with value $value",1);
987 # $entry->add($attribute => $value);
988 # }
989 # }
990 # foreach my $attribute (
991 # "gotoModules", "ghScsiDev", "ghIdeDev") {
992 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
993 # if(defined($array)) {
994 # if(defined($entry->get_value($attribute))) {
995 # $entry->delete($attribute);
996 # }
997 # foreach my $array_entry (@{$array}) {
998 # $entry->add($attribute => $array_entry);
999 # }
1000 # }
1002 # }
1003 # if($entry->update($ldap)) {
1004 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1005 # }
1007 #}
1010 # Assemble data package
1011 my %data = ();
1013 # Need to append GOto settings?
1014 if (defined $goto_admin and defined $goto_secret){
1015 $data{'goto_admin'}= $goto_admin;
1016 $data{'goto_secret'}= $goto_secret;
1017 }
1019 # Unbind
1020 $mesg = $ldap->unbind;
1022 &main::daemon_log("Send detect_hardware message to $address", 4);
1024 # Send information
1025 return send_msg("detect_hardware", $server_address, $address, \%data);
1026 }
1029 #=== FUNCTION ================================================================
1030 # NAME: execute_actions
1031 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1032 # RETURNS: nothing
1033 # DESCRIPTION: invokes the script specified in msg_hash which is located under
1034 # /etc/gosad/actions
1035 #===============================================================================
1036 sub execute_actions {
1037 my ($msg_hash) = @_ ;
1038 my $configdir= '/etc/gosad/actions/';
1039 my $result;
1041 my $header = @{$msg_hash->{header}}[0];
1042 my $source = @{$msg_hash->{source}}[0];
1043 my $target = @{$msg_hash->{target}}[0];
1045 if((not defined $source)
1046 && (not defined $target)
1047 && (not defined $header)) {
1048 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1049 } else {
1050 my $parameters="";
1051 my @params = @{$msg_hash->{$header}};
1052 my $params = join(", ", @params);
1053 &main::daemon_log("execute_actions: got parameters: $params", 5);
1055 if (@params) {
1056 foreach my $param (@params) {
1057 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1058 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1059 $parameters.= " ".$param_value;
1060 }
1061 }
1063 my $cmd= $configdir.$header."$parameters";
1064 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1065 $result= "";
1066 open(PIPE, "$cmd 2>&1 |");
1067 while(<PIPE>) {
1068 $result.=$_;
1069 }
1070 close(PIPE);
1071 }
1073 # process the event result
1076 return;
1077 }
1080 1;