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;
19 BEGIN{}
20 END {}
22 my ($known_clients_file_name);
23 my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password);
24 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port);
25 my $server;
26 my $network_interface;
27 my $no_bus;
28 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
31 my %cfg_defaults =
32 (
33 "server" =>
34 {"server_activ" => [\$server_activ, "on"],
35 "server_ip" => [\$server_ip, "0.0.0.0"],
36 "server_mac_address" => [\$server_mac_address, ""],
37 "server_port" => [\$server_port, "20081"],
38 "SIPackages_key" => [\$SIPackages_key, ""],
39 "max_clients" => [\$max_clients, 100],
40 "ldap_uri" => [\$ldap_uri, ""],
41 "ldap_base" => [\$ldap_base, ""],
42 "ldap_admin_dn" => [\$ldap_admin_dn, ""],
43 "ldap_admin_password" => [\$ldap_admin_password, ""],
44 },
45 "bus" =>
46 {"bus_activ" => [\$bus_activ, "on"],
47 "bus_passwd" => [\$bus_passwd, ""],
48 "bus_ip" => [\$bus_ip, ""],
49 "bus_port" => [\$bus_port, "20080"],
50 },
51 );
53 ### START #####################################################################
55 # read configfile and import variables
56 &read_configfile();
58 # detect interfaces and mac address
59 $network_interface= &get_interface_for_ip($server_ip);
60 $server_mac_address= &get_mac($network_interface);
62 # complete addresses
63 if( $server_ip eq "0.0.0.0" ) {
64 $server_ip = "127.0.0.1";
65 }
66 my $server_address = "$server_ip:$server_port";
67 my $bus_address = "$bus_ip:$bus_port";
69 # create general settings for this module
70 my $xml = new XML::Simple();
72 # register at bus
73 if ($main::no_bus > 0) {
74 $bus_activ = "off"
75 }
76 if($bus_activ eq "on") {
77 ®ister_at_bus();
78 }
80 # add myself to known_server_db
81 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
82 primkey=>'hostname',
83 hostname=>$server_address,
84 status=>'myself',
85 hostkey=>$SIPackages_key,
86 timestamp=>&get_time,
87 } );
91 ### functions #################################################################
94 sub get_module_info {
95 my @info = ($server_address,
96 $SIPackages_key,
97 $server,
98 $server_activ,
99 "socket",
100 );
101 return \@info;
102 }
106 sub do_wake {
107 my $host = shift;
108 my $ipaddr = shift || '255.255.255.255';
109 my $port = getservbyname('discard', 'udp');
111 my ($raddr, $them, $proto);
112 my ($hwaddr, $hwaddr_re, $pkt);
114 # get the hardware address (ethernet address)
116 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
117 if ($host =~ m/^$hwaddr_re$/) {
118 $hwaddr = $host;
119 } else {
120 # $host is not a hardware address, try to resolve it
121 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
122 my $ip_addr;
123 if ($host =~ m/^$ip_re$/) {
124 $ip_addr = $host;
125 } else {
126 my $h;
127 unless ($h = gethost($host)) {
128 return undef;
129 }
130 $ip_addr = inet_ntoa($h->addr);
131 }
132 }
134 # Generate magic sequence
135 foreach (split /:/, $hwaddr) {
136 $pkt .= chr(hex($_));
137 }
138 $pkt = chr(0xFF) x 6 . $pkt x 16;
140 # Allocate socket and send packet
142 $raddr = gethostbyname($ipaddr)->addr;
143 $them = pack_sockaddr_in($port, $raddr);
144 $proto = getprotobyname('udp');
146 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
147 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
149 send(S, $pkt, 0, $them) or die "send : $!";
150 close S;
151 }
154 #=== FUNCTION ================================================================
155 # NAME: read_configfile
156 # PARAMETERS: cfg_file - string -
157 # RETURNS: nothing
158 # DESCRIPTION: read cfg_file and set variables
159 #===============================================================================
160 sub read_configfile {
161 my $cfg;
162 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
163 if( -r $main::cfg_file ) {
164 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
165 } else {
166 print STDERR "Couldn't read config file!";
167 }
168 } else {
169 $cfg = Config::IniFiles->new() ;
170 }
171 foreach my $section (keys %cfg_defaults) {
172 foreach my $param (keys %{$cfg_defaults{ $section }}) {
173 my $pinfo = $cfg_defaults{ $section }{ $param };
174 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
175 }
176 }
178 # Read non predefined sections
179 my $param;
180 if ($cfg->SectionExists('ldap')){
181 foreach $param ($cfg->Parameters('ldap')){
182 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
183 }
184 }
185 if ($cfg->SectionExists('pam_ldap')){
186 foreach $param ($cfg->Parameters('pam_ldap')){
187 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
188 }
189 }
190 if ($cfg->SectionExists('nss_ldap')){
191 foreach $param ($cfg->Parameters('nss_ldap')){
192 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
193 }
194 }
195 if ($cfg->SectionExists('goto')){
196 $goto_admin= $cfg->val('goto', 'terminal_admin');
197 $goto_secret= $cfg->val('goto', 'terminal_secret');
198 } else {
199 $goto_admin= undef;
200 $goto_secret= undef;
201 }
203 }
205 #=== FUNCTION ================================================================
206 # NAME: get_interface_for_ip
207 # PARAMETERS: ip address (i.e. 192.168.0.1)
208 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
209 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
210 #===============================================================================
211 sub get_interface_for_ip {
212 my $result;
213 my $ip= shift;
214 if ($ip && length($ip) > 0) {
215 my @ifs= &get_interfaces();
216 if($ip eq "0.0.0.0") {
217 $result = "all";
218 } else {
219 foreach (@ifs) {
220 my $if=$_;
221 if(get_ip($if) eq $ip) {
222 $result = $if;
223 }
224 }
225 }
226 }
227 return $result;
228 }
230 #=== FUNCTION ================================================================
231 # NAME: get_interfaces
232 # PARAMETERS: none
233 # RETURNS: (list of interfaces)
234 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
235 #===============================================================================
236 sub get_interfaces {
237 my @result;
238 my $PROC_NET_DEV= ('/proc/net/dev');
240 open(PROC_NET_DEV, "<$PROC_NET_DEV")
241 or die "Could not open $PROC_NET_DEV";
243 my @ifs = <PROC_NET_DEV>;
245 close(PROC_NET_DEV);
247 # Eat first two line
248 shift @ifs;
249 shift @ifs;
251 chomp @ifs;
252 foreach my $line(@ifs) {
253 my $if= (split /:/, $line)[0];
254 $if =~ s/^\s+//;
255 push @result, $if;
256 }
258 return @result;
259 }
261 #=== FUNCTION ================================================================
262 # NAME: get_mac
263 # PARAMETERS: interface name (i.e. eth0)
264 # RETURNS: (mac address)
265 # DESCRIPTION: Uses ioctl to get mac address directly from system.
266 #===============================================================================
267 sub get_mac {
268 my $ifreq= shift;
269 my $result;
270 if ($ifreq && length($ifreq) > 0) {
271 if($ifreq eq "all") {
272 $result = "00:00:00:00:00:00";
273 } else {
274 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
276 # A configured MAC Address should always override a guessed value
277 if ($server_mac_address and length($server_mac_address) > 0) {
278 $result= $server_mac_address;
279 }
281 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
282 or die "socket: $!";
284 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
285 my ($if, $mac)= unpack 'h36 H12', $ifreq;
287 if (length($mac) > 0) {
288 $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])$/;
289 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
290 $result = $mac;
291 }
292 }
293 }
294 }
295 return $result;
296 }
298 #=== FUNCTION ================================================================
299 # NAME: get_ip
300 # PARAMETERS: interface name (i.e. eth0)
301 # RETURNS: (ip address)
302 # DESCRIPTION: Uses ioctl to get ip address directly from system.
303 #===============================================================================
304 sub get_ip {
305 my $ifreq= shift;
306 my $result= "";
307 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
308 my $proto= getprotobyname('ip');
310 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
311 or die "socket: $!";
313 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
314 my ($if, $sin) = unpack 'a16 a16', $ifreq;
315 my ($port, $addr) = sockaddr_in $sin;
316 my $ip = inet_ntoa $addr;
318 if ($ip && length($ip) > 0) {
319 $result = $ip;
320 }
321 }
323 return $result;
324 }
327 #=== FUNCTION ================================================================
328 # NAME: register_at_bus
329 # PARAMETERS: nothing
330 # RETURNS: nothing
331 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
332 #===============================================================================
333 sub register_at_bus {
335 # add bus to known_server_db
336 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
337 primkey=>'hostname',
338 hostname=>$bus_address,
339 status=>'bus',
340 hostkey=>$bus_passwd,
341 timestamp=>&get_time,
342 } );
343 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
344 my $msg = &create_xml_string($msg_hash);
345 return $msg;
346 # my $answer = "";
347 # $answer = &send_msg_hash2address($msg_hash, $bus_address, $bus_passwd);
348 # if ($answer == 0) {
349 # &main::daemon_log("register at bus: $bus_address", 1);
350 # } else {
351 # &main::daemon_log("unable to send 'register'-msg to bus '$bus_address': $answer", 1);
352 # }
353 # return;
354 }
357 #=== FUNCTION ================================================================
358 # NAME: process_incoming_msg
359 # PARAMETERS: crypted_msg - string - incoming crypted message
360 # RETURNS: nothing
361 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
362 #===============================================================================
363 sub process_incoming_msg {
364 my ($msg, $msg_hash, $remote_ip) = @_ ;
365 my $error = 0;
366 my $host_name;
367 my $host_key;
368 my @out_msg_l;
370 # process incoming msg
371 my $header = @{$msg_hash->{header}}[0];
372 my @target_l = @{$msg_hash->{target}};
374 &main::daemon_log("SIPackages: msg to process: $header", 3);
375 &main::daemon_log("$msg", 8);
377 if( 0 == length @target_l){
378 &main::daemon_log("ERROR: no target specified for msg $header", 1);
379 $error++;
380 }
382 if( 1 == length @target_l) {
383 my $target = $target_l[0];
384 if( $target eq $server_address ) {
385 if ($header eq 'new_key') {
386 @out_msg_l = &new_key($msg_hash)
387 } elsif ($header eq 'here_i_am') {
388 @out_msg_l = &here_i_am($msg_hash)
389 } elsif ($header eq 'who_has') {
390 @out_msg_l = &who_has($msg_hash)
391 } elsif ($header eq 'who_has_i_do') {
392 @out_msg_l = &who_has_i_do($msg_hash)
393 } elsif ($header eq 'got_ping') {
394 @out_msg_l = &got_ping($msg_hash)
395 } elsif ($header eq 'get_load') {
396 @out_msg_l = &execute_actions($msg_hash)
397 } elsif ($header eq 'detected_hardware') {
398 @out_msg_l = &process_detected_hardware($msg_hash)
399 } elsif ($header eq 'trigger_wake') {
400 foreach (@{$msg_hash->{macAddress}}){
401 &main::daemon_log("SIPackages: trigger wake for $_", 1);
402 do_wake($_);
403 }
405 } else {
406 &main::daemon_log("ERROR: $header is an unknown core function", 1);
407 $error++;
408 }
409 }
410 else {
411 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
412 push(@out_msg_l, $msg);
413 }
414 }
416 if( $error == 0) {
417 if( 0 == @out_msg_l ) {
418 push(@out_msg_l, $msg);
419 }
420 }
422 return \@out_msg_l;
423 }
426 #=== FUNCTION ================================================================
427 # NAME: got_ping
428 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
429 # RETURNS: nothing
430 # DESCRIPTION: process this incoming message
431 #===============================================================================
432 sub got_ping {
433 my ($msg_hash) = @_;
435 my $source = @{$msg_hash->{source}}[0];
436 my $target = @{$msg_hash->{target}}[0];
437 my $header = @{$msg_hash->{header}}[0];
439 if(exists $main::known_daemons->{$source}) {
440 &main::add_content2known_daemons(hostname=>$source, status=>$header);
441 } else {
442 &main::add_content2known_clients(hostname=>$source, status=>$header);
443 }
445 return;
446 }
449 #=== FUNCTION ================================================================
450 # NAME: new_passwd
451 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
452 # RETURNS: nothing
453 # DESCRIPTION: process this incoming message
454 #===============================================================================
455 sub new_key {
456 my ($msg_hash) = @_;
457 my @out_msg_l;
459 my $header = @{$msg_hash->{header}}[0];
460 my $source_name = @{$msg_hash->{source}}[0];
461 my $source_key = @{$msg_hash->{new_key}}[0];
462 my $query_res;
464 # check known_clients_db
465 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
466 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
467 if( 1 == keys %{$query_res} ) {
468 my $act_time = &get_time;
469 my $sql_statement= "UPDATE known_clients ".
470 "SET hostkey='$source_key', timestamp='$act_time' ".
471 "WHERE hostname='$source_name'";
472 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
473 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
474 my $out_msg = &create_xml_string($hash);
475 push(@out_msg_l, $out_msg);
476 }
478 # only do if host still not found
479 if( 0 == @out_msg_l ) {
480 # check known_server_db
481 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
482 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
483 if( 1 == keys %{$query_res} ) {
484 my $act_time = &get_time;
485 my $sql_statement= "UPDATE known_server ".
486 "SET hostkey='$source_key', timestamp='$act_time' ".
487 "WHERE hostname='$source_name'";
488 my $res = $main::known_server_db->update_dbentry( $sql_statement );
490 my $hash = &create_xml_hash("confirm_new_passwd", $server_address, $source_name);
491 my $out_msg = &create_xml_string($hash);
492 push(@out_msg_l, $out_msg);
493 }
494 }
496 return @out_msg_l;
497 }
500 #=== FUNCTION ================================================================
501 # NAME: here_i_am
502 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
503 # RETURNS: nothing
504 # DESCRIPTION: process this incoming message
505 #===============================================================================
506 sub here_i_am {
507 my ($msg_hash) = @_;
508 my @out_msg_l;
509 my $out_hash;
511 my $source = @{$msg_hash->{source}}[0];
512 my $mac_address = @{$msg_hash->{mac_address}}[0];
513 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
515 # number of known clients
516 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
518 # check wether client address or mac address is already known
519 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
520 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
522 if ( 1 == keys %{$db_res} ) {
523 &main::daemon_log("WARNING: $source is already known as a client", 1);
524 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
525 $nu_clients --;
526 }
528 # number of actual activ clients
529 my $act_nu_clients = $nu_clients;
531 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
532 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
534 if($max_clients <= $act_nu_clients) {
535 my $out_hash = &create_xml_hash("denied", $server_address, $source);
536 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
537 my $passwd = @{$msg_hash->{new_passwd}}[0];
538 &send_msg_hash2address($out_hash, $source, $passwd);
539 return;
540 }
542 # new client accepted
543 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
545 # create entry in known_clients
546 my $events = @{$msg_hash->{events}}[0];
549 # add entry to known_clients_db
550 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
551 primkey=>'hostname',
552 hostname=>$source,
553 events=>$events,
554 macaddress=>$mac_address,
555 status=>'registered',
556 hostkey=>$new_passwd,
557 timestamp=>&get_time,
558 } );
560 if ($res != 0) {
561 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
562 return;
563 }
565 # return acknowledgement to client
566 $out_hash = &create_xml_hash("registered", $server_address, $source);
567 my $register_out = &create_xml_string($out_hash);
568 push(@out_msg_l, $register_out);
570 # notify registered client to bus
571 if( $bus_activ eq "on") {
572 # fetch actual bus key
573 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
574 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
575 my $hostkey = $query_res->{1}->{'hostkey'};
577 # send update msg to bus
578 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
579 my $new_client_out = &create_xml_string($out_hash);
580 push(@out_msg_l, $new_client_out);
581 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
582 }
584 # give the new client his ldap config
585 my $new_ldap_config_out = &new_ldap_config($source);
586 if( $new_ldap_config_out ) {
587 push(@out_msg_l, $new_ldap_config_out);
588 }
590 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
591 if( $hardware_config_out ) {
592 push(@out_msg_l, $hardware_config_out);
593 }
595 return @out_msg_l;
596 }
599 #=== FUNCTION ================================================================
600 # NAME: who_has
601 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
602 # RETURNS: nothing
603 # DESCRIPTION: process this incoming message
604 #===============================================================================
605 sub who_has {
606 my ($msg_hash) = @_ ;
607 my @out_msg_l;
609 # what is your search pattern
610 my $search_pattern = @{$msg_hash->{who_has}}[0];
611 my $search_element = @{$msg_hash->{$search_pattern}}[0];
612 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
614 # scanning known_clients for search_pattern
615 my @host_addresses = keys %$main::known_clients;
616 my $known_clients_entries = length @host_addresses;
617 my $host_address;
618 foreach my $host (@host_addresses) {
619 my $client_element = $main::known_clients->{$host}->{$search_pattern};
620 if ($search_element eq $client_element) {
621 $host_address = $host;
622 last;
623 }
624 }
626 # search was successful
627 if (defined $host_address) {
628 my $source = @{$msg_hash->{source}}[0];
629 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
630 &add_content2xml_hash($out_hash, "mac_address", $search_element);
631 my $out_msg = &create_xml_string($out_hash);
632 push(@out_msg_l, $out_msg);
633 }
634 return @out_msg_l;
635 }
638 sub who_has_i_do {
639 my ($msg_hash) = @_ ;
640 my $header = @{$msg_hash->{header}}[0];
641 my $source = @{$msg_hash->{source}}[0];
642 my $search_param = @{$msg_hash->{$header}}[0];
643 my $search_value = @{$msg_hash->{$search_param}}[0];
644 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
645 }
647 #=== FUNCTION ================================================================
648 # NAME: new_ldap_config
649 # PARAMETERS: address - string - ip address and port of a host
650 # RETURNS: nothing
651 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
652 #===============================================================================
653 sub new_ldap_config {
654 my ($address) = @_ ;
656 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
657 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
659 # check hit
660 my $hit_counter = keys %{$res};
661 if( not $hit_counter == 1 ) {
662 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
663 }
665 my $macaddress = $res->{1}->{macaddress};
666 my $hostkey = $res->{1}->{hostkey};
668 if (not defined $macaddress) {
669 &main::daemon_log("ERROR: no mac address found for client $address", 1);
670 return;
671 }
673 # Build LDAP connection
674 my $ldap = Net::LDAP->new($ldap_uri);
675 if( not defined $ldap ) {
676 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
677 return;
678 }
681 # Bind to a directory with dn and password
682 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
684 # Perform search
685 $mesg = $ldap->search( base => $ldap_base,
686 scope => 'sub',
687 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
688 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
689 #$mesg->code && die $mesg->error;
690 if($mesg->code) {
691 &main::daemon_log($mesg->error, 1);
692 return;
693 }
695 # Sanity check
696 if ($mesg->count != 1) {
697 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
698 &main::daemon_log("\tbase: $ldap_base", 1);
699 &main::daemon_log("\tscope: sub", 1);
700 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
701 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
702 return;
703 }
705 my $entry= $mesg->entry(0);
706 my $dn= $entry->dn;
707 my @servers= $entry->get_value("gotoLdapServer");
708 my $unit_tag= $entry->get_value("gosaUnitTag");
709 my @ldap_uris;
710 my $server;
711 my $base;
713 # Do we need to look at an object class?
714 if (length(@servers) < 1){
715 $mesg = $ldap->search( base => $ldap_base,
716 scope => 'sub',
717 attrs => ['dn', 'gotoLdapServer'],
718 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
719 #$mesg->code && die $mesg->error;
720 if($mesg->code) {
721 &main::daemon_log($mesg->error, 1);
722 return;
723 }
725 # Sanity check
726 if ($mesg->count != 1) {
727 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
728 return;
729 }
731 $entry= $mesg->entry(0);
732 $dn= $entry->dn;
733 @servers= $entry->get_value("gotoLdapServer");
734 }
736 @servers= sort (@servers);
738 foreach $server (@servers){
739 $base= $server;
740 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
741 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
742 push (@ldap_uris, $server);
743 }
745 # Assemble data package
746 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
747 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
749 # Need to append GOto settings?
750 if (defined $goto_admin and defined $goto_secret){
751 $data{'goto_admin'}= $goto_admin;
752 $data{'goto_secret'}= $goto_secret;
753 }
755 # Append unit tag if needed
756 if (defined $unit_tag){
758 # Find admin base and department name
759 $mesg = $ldap->search( base => $ldap_base,
760 scope => 'sub',
761 attrs => ['dn', 'ou'],
762 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
763 #$mesg->code && die $mesg->error;
764 if($mesg->code) {
765 &main::daemon_log($mesg->error, 1);
766 return;
767 }
769 # Sanity check
770 if ($mesg->count != 1) {
771 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
772 return;
773 }
775 $entry= $mesg->entry(0);
776 $data{'admin_base'}= $entry->dn;
777 $data{'department'}= $entry->get_value("ou");
779 # Append unit Tag
780 $data{'unit_tag'}= $unit_tag;
781 }
783 # Unbind
784 $mesg = $ldap->unbind;
786 # Send information
787 return send_msg("new_ldap_config", $server_address, $address, \%data);
788 }
790 sub process_detected_hardware {
791 my $msg_hash = shift;
792 my $address = $msg_hash->{source}[0];
794 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
795 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
797 # check hit
798 my $hit_counter = keys %{$res};
799 if( not $hit_counter == 1 ) {
800 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
801 }
803 my $macaddress = $res->{1}->{macaddress};
804 my $hostkey = $res->{1}->{hostkey};
806 if (not defined $macaddress) {
807 &main::daemon_log("ERROR: no mac address found for client $address", 1);
808 return;
809 }
810 # Build LDAP connection
811 my $ldap = Net::LDAP->new($ldap_uri);
812 if( not defined $ldap ) {
813 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
814 return;
815 }
817 # Bind to a directory with dn and password
818 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
820 # Perform search
821 $mesg = $ldap->search(
822 base => $ldap_base,
823 scope => 'sub',
824 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
825 );
827 if($mesg->count == 1) {
828 my $entry= $mesg->entry(0);
829 $entry->changetype("modify");
830 foreach my $attribute (
831 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
832 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
833 "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
834 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
835 if(defined($entry->get_value($attribute))) {
836 $entry->delete($attribute);
837 }
838 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
839 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
840 }
841 }
842 foreach my $attribute (
843 "gotoModules", "ghScsiDev", "ghIdeDev") {
844 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
845 if(defined($entry->get_value($attribute))) {
846 $entry->delete($attribute);
847 }
848 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
849 $entry->add($attribute => $array_entry);
850 }
851 }
853 }
854 if($entry->update($ldap)) {
855 &main::daemon_log("Added Hardware configuration to LDAP", 4);
856 }
858 }
859 return;
860 }
861 #=== FUNCTION ================================================================
862 # NAME: hardware_config
863 # PARAMETERS: address - string - ip address and port of a host
864 # RETURNS:
865 # DESCRIPTION:
866 #===============================================================================
867 sub hardware_config {
868 my ($address, $gotoHardwareChecksum) = @_ ;
870 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
871 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
873 # check hit
874 my $hit_counter = keys %{$res};
875 if( not $hit_counter == 1 ) {
876 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
877 }
879 my $macaddress = $res->{1}->{macaddress};
880 my $hostkey = $res->{1}->{hostkey};
882 if (not defined $macaddress) {
883 &main::daemon_log("ERROR: no mac address found for client $address", 1);
884 return;
885 }
887 # Build LDAP connection
888 my $ldap = Net::LDAP->new($ldap_uri);
889 if( not defined $ldap ) {
890 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
891 return;
892 }
894 # Bind to a directory with dn and password
895 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
897 # Perform search
898 $mesg = $ldap->search(
899 base => $ldap_base,
900 scope => 'sub',
901 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
902 );
904 if($mesg->count() == 0) {
905 &main::daemon_log("Host was not found in LDAP!", 1);
906 return;
907 }
909 my $entry= $mesg->entry(0);
910 my $dn= $entry->dn;
911 if(defined($entry->get_value("gotoHardwareChecksum"))) {
912 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
913 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
914 if($entry->update($ldap)) {
915 &main::daemon_log("Hardware changed! Detection triggered.", 4);
916 }
917 } else {
918 # Nothing to do
919 return;
920 }
921 } else {
922 # need to fill it to LDAP
923 $entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
924 if($entry->update($ldap)) {
925 &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
926 }
928 ## Look if there another host with this checksum to use the hardware config
929 #$mesg = $ldap->search(
930 # base => $ldap_base,
931 # scope => 'sub',
932 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
933 #);
935 #if($mesg->count>1) {
936 # my $clone_entry= $mesg->entry(0);
937 # $entry->changetype("modify");
938 # foreach my $attribute (
939 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
940 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
941 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
942 # my $value= $clone_entry->get_value($attribute);
943 # if(defined($value)) {
944 # if(defined($entry->get_value($attribute))) {
945 # $entry->delete($attribute);
946 # }
947 # &main::daemon_log("Adding attribute $attribute with value $value",1);
948 # $entry->add($attribute => $value);
949 # }
950 # }
951 # foreach my $attribute (
952 # "gotoModules", "ghScsiDev", "ghIdeDev") {
953 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
954 # if(defined($array)) {
955 # if(defined($entry->get_value($attribute))) {
956 # $entry->delete($attribute);
957 # }
958 # foreach my $array_entry (@{$array}) {
959 # $entry->add($attribute => $array_entry);
960 # }
961 # }
963 # }
964 # if($entry->update($ldap)) {
965 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
966 # }
968 #}
970 }
972 # Assemble data package
973 my %data = ();
975 # Need to append GOto settings?
976 if (defined $goto_admin and defined $goto_secret){
977 $data{'goto_admin'}= $goto_admin;
978 $data{'goto_secret'}= $goto_secret;
979 }
981 # Unbind
982 $mesg = $ldap->unbind;
984 &main::daemon_log("Send detect_hardware message to $address", 4);
986 # Send information
987 return send_msg("detect_hardware", $server_address, $address, \%data);
988 }
991 #=== FUNCTION ================================================================
992 # NAME: execute_actions
993 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
994 # RETURNS: nothing
995 # DESCRIPTION: invokes the script specified in msg_hash which is located under
996 # /etc/gosad/actions
997 #===============================================================================
998 sub execute_actions {
999 my ($msg_hash) = @_ ;
1000 my $configdir= '/etc/gosad/actions/';
1001 my $result;
1003 my $header = @{$msg_hash->{header}}[0];
1004 my $source = @{$msg_hash->{source}}[0];
1005 my $target = @{$msg_hash->{target}}[0];
1007 if((not defined $source)
1008 && (not defined $target)
1009 && (not defined $header)) {
1010 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1011 } else {
1012 my $parameters="";
1013 my @params = @{$msg_hash->{$header}};
1014 my $params = join(", ", @params);
1015 &main::daemon_log("execute_actions: got parameters: $params", 5);
1017 if (@params) {
1018 foreach my $param (@params) {
1019 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1020 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1021 $parameters.= " ".$param_value;
1022 }
1023 }
1025 my $cmd= $configdir.$header."$parameters";
1026 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1027 $result= "";
1028 open(PIPE, "$cmd 2>&1 |");
1029 while(<PIPE>) {
1030 $result.=$_;
1031 }
1032 close(PIPE);
1033 }
1035 # process the event result
1038 return;
1039 }
1042 1;