6494c4f7d4a7291197da2bbabd434a6eb55ec67b
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 ($known_clients_file_name);
25 my ($server_activ, $server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
26 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
27 my $server;
28 my $network_interface;
29 my $no_bus;
30 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
33 my %cfg_defaults =
34 (
35 "server" =>
36 {"server_activ" => [\$server_activ, "on"],
37 "server_ip" => [\$server_ip, "0.0.0.0"],
38 "server_mac_address" => [\$server_mac_address, "00:00:00:00:00"],
39 "server_port" => [\$server_port, "20081"],
40 "SIPackages_key" => [\$SIPackages_key, ""],
41 "max_clients" => [\$max_clients, 100],
42 "ldap_uri" => [\$ldap_uri, ""],
43 "ldap_base" => [\$ldap_base, ""],
44 "ldap_admin_dn" => [\$ldap_admin_dn, ""],
45 "ldap_admin_password" => [\$ldap_admin_password, ""],
46 },
47 "bus" =>
48 {"bus_activ" => [\$bus_activ, "on"],
49 "bus_passwd" => [\$bus_key, ""],
50 "bus_ip" => [\$bus_ip, ""],
51 "bus_port" => [\$bus_port, "20080"],
52 },
53 );
55 ### START #####################################################################
57 # read configfile and import variables
58 &read_configfile();
60 $server_ip = &get_local_ip_for_remote_ip($server_ip);
62 $network_interface= &get_interface_for_ip($server_ip);
63 $server_mac_address= &get_mac($network_interface);
65 # complete addresses
66 if( $server_ip eq "0.0.0.0" ) {
67 $server_ip = "127.0.0.1";
68 }
69 my $server_address = "$server_ip:$server_port";
70 $main::server_address = $server_address;
71 my $bus_address = "$bus_ip:$bus_port";
72 $main::bus_address = $bus_address;
74 # create general settings for this module
75 my $xml = new XML::Simple();
77 # register at bus
78 if ($main::no_bus > 0) {
79 $bus_activ = "off"
80 }
81 if($bus_activ eq "on") {
82 ®ister_at_bus();
83 }
85 # add myself to known_server_db
86 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
87 primkey=>'hostname',
88 hostname=>$server_address,
89 status=>'myself',
90 hostkey=>$SIPackages_key,
91 timestamp=>&get_time,
92 } );
96 ### functions #################################################################
99 sub get_module_info {
100 my @info = ($server_address,
101 $SIPackages_key,
102 $server,
103 $server_activ,
104 "socket",
105 );
106 return \@info;
107 }
111 sub do_wake {
112 my $host = shift;
113 my $ipaddr = shift || '255.255.255.255';
114 my $port = getservbyname('discard', 'udp');
116 my ($raddr, $them, $proto);
117 my ($hwaddr, $hwaddr_re, $pkt);
119 # get the hardware address (ethernet address)
121 $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
122 if ($host =~ m/^$hwaddr_re$/) {
123 $hwaddr = $host;
124 } else {
125 # $host is not a hardware address, try to resolve it
126 my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
127 my $ip_addr;
128 if ($host =~ m/^$ip_re$/) {
129 $ip_addr = $host;
130 } else {
131 my $h;
132 unless ($h = gethost($host)) {
133 return undef;
134 }
135 $ip_addr = inet_ntoa($h->addr);
136 }
137 }
139 # Generate magic sequence
140 foreach (split /:/, $hwaddr) {
141 $pkt .= chr(hex($_));
142 }
143 $pkt = chr(0xFF) x 6 . $pkt x 16;
145 # Allocate socket and send packet
147 $raddr = gethostbyname($ipaddr)->addr;
148 $them = pack_sockaddr_in($port, $raddr);
149 $proto = getprotobyname('udp');
151 socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
152 setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
154 send(S, $pkt, 0, $them) or die "send : $!";
155 close S;
156 }
159 #=== FUNCTION ================================================================
160 # NAME: read_configfile
161 # PARAMETERS: cfg_file - string -
162 # RETURNS: nothing
163 # DESCRIPTION: read cfg_file and set variables
164 #===============================================================================
165 sub read_configfile {
166 my $cfg;
167 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
168 if( -r $main::cfg_file ) {
169 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
170 } else {
171 print STDERR "Couldn't read config file!";
172 }
173 } else {
174 $cfg = Config::IniFiles->new() ;
175 }
176 foreach my $section (keys %cfg_defaults) {
177 foreach my $param (keys %{$cfg_defaults{ $section }}) {
178 my $pinfo = $cfg_defaults{ $section }{ $param };
179 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
180 }
181 }
183 # Read non predefined sections
184 my $param;
185 if ($cfg->SectionExists('ldap')){
186 foreach $param ($cfg->Parameters('ldap')){
187 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
188 }
189 }
190 if ($cfg->SectionExists('pam_ldap')){
191 foreach $param ($cfg->Parameters('pam_ldap')){
192 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
193 }
194 }
195 if ($cfg->SectionExists('nss_ldap')){
196 foreach $param ($cfg->Parameters('nss_ldap')){
197 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
198 }
199 }
200 if ($cfg->SectionExists('goto')){
201 $goto_admin= $cfg->val('goto', 'terminal_admin');
202 $goto_secret= $cfg->val('goto', 'terminal_secret');
203 } else {
204 $goto_admin= undef;
205 $goto_secret= undef;
206 }
208 }
210 #=== FUNCTION ================================================================
211 # NAME: get_interface_for_ip
212 # PARAMETERS: ip address (i.e. 192.168.0.1)
213 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
214 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
215 #===============================================================================
216 sub get_interface_for_ip {
217 my $result;
218 my $ip= shift;
219 if ($ip && length($ip) > 0) {
220 my @ifs= &get_interfaces();
221 if($ip eq "0.0.0.0") {
222 $result = "all";
223 } else {
224 foreach (@ifs) {
225 my $if=$_;
226 if(get_ip($if) eq $ip) {
227 $result = $if;
228 }
229 }
230 }
231 }
232 return $result;
233 }
235 #=== FUNCTION ================================================================
236 # NAME: get_interfaces
237 # PARAMETERS: none
238 # RETURNS: (list of interfaces)
239 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
240 #===============================================================================
241 sub get_interfaces {
242 my @result;
243 my $PROC_NET_DEV= ('/proc/net/dev');
245 open(PROC_NET_DEV, "<$PROC_NET_DEV")
246 or die "Could not open $PROC_NET_DEV";
248 my @ifs = <PROC_NET_DEV>;
250 close(PROC_NET_DEV);
252 # Eat first two line
253 shift @ifs;
254 shift @ifs;
256 chomp @ifs;
257 foreach my $line(@ifs) {
258 my $if= (split /:/, $line)[0];
259 $if =~ s/^\s+//;
260 push @result, $if;
261 }
263 return @result;
264 }
266 #=== FUNCTION ================================================================
267 # NAME: get_mac
268 # PARAMETERS: interface name (i.e. eth0)
269 # RETURNS: (mac address)
270 # DESCRIPTION: Uses ioctl to get mac address directly from system.
271 #===============================================================================
272 sub get_mac {
273 my $ifreq= shift;
274 my $result;
275 if ($ifreq && length($ifreq) > 0) {
276 if($ifreq eq "all") {
277 $result = "00:00:00:00:00:00";
278 } else {
279 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
281 # A configured MAC Address should always override a guessed value
282 if ($server_mac_address and length($server_mac_address) > 0) {
283 $result= $server_mac_address;
284 }
286 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
287 or die "socket: $!";
289 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
290 my ($if, $mac)= unpack 'h36 H12', $ifreq;
292 if (length($mac) > 0) {
293 $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])$/;
294 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
295 $result = $mac;
296 }
297 }
298 }
299 }
300 return $result;
301 }
303 #=== FUNCTION ================================================================
304 # NAME: get_ip
305 # PARAMETERS: interface name (i.e. eth0)
306 # RETURNS: (ip address)
307 # DESCRIPTION: Uses ioctl to get ip address directly from system.
308 #===============================================================================
309 sub get_ip {
310 my $ifreq= shift;
311 my $result= "";
312 my $SIOCGIFADDR= 0x8915; # man 2 ioctl_list
313 my $proto= getprotobyname('ip');
315 socket SOCKET, PF_INET, SOCK_DGRAM, $proto
316 or die "socket: $!";
318 if(ioctl SOCKET, $SIOCGIFADDR, $ifreq) {
319 my ($if, $sin) = unpack 'a16 a16', $ifreq;
320 my ($port, $addr) = sockaddr_in $sin;
321 my $ip = inet_ntoa $addr;
323 if ($ip && length($ip) > 0) {
324 $result = $ip;
325 }
326 }
328 return $result;
329 }
332 sub get_local_ip_for_remote_ip {
333 my $server_ip= shift;
334 my $result="0.0.0.0";
336 if($server_ip =~ /^(\d\d?\d?\.){3}\d\d?\d?$/) {
337 if($server_ip eq "127.0.0.1") {
338 $result="127.0.0.1";
339 } else {
340 my $PROC_NET_ROUTE= ('/proc/net/route');
342 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
343 or die "Could not open $PROC_NET_ROUTE";
345 my @ifs = <PROC_NET_ROUTE>;
347 close(PROC_NET_ROUTE);
349 # Eat header line
350 shift @ifs;
351 chomp @ifs;
352 foreach my $line(@ifs) {
353 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
354 my $destination;
355 my $mask;
356 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
357 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
358 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
359 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
360 if(new NetAddr::IP($server_ip)->within(new NetAddr::IP($destination, $mask))) {
361 # destination matches route, save mac and exit
362 $result= &get_ip($Iface);
363 last;
364 }
365 }
366 }
367 } else {
368 daemon_log("get_local_ip_for_remote_ip was called with a non-ip parameter: $server_ip", 1);
369 }
370 return $result;
371 }
374 #=== FUNCTION ================================================================
375 # NAME: register_at_bus
376 # PARAMETERS: nothing
377 # RETURNS: nothing
378 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
379 #===============================================================================
380 sub register_at_bus {
382 # add bus to known_server_db
383 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
384 primkey=>'hostname',
385 hostname=>$bus_address,
386 status=>'bus',
387 hostkey=>$bus_key,
388 timestamp=>&get_time,
389 } );
390 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
391 my $msg = &create_xml_string($msg_hash);
393 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
394 return $msg;
395 }
398 #=== FUNCTION ================================================================
399 # NAME: process_incoming_msg
400 # PARAMETERS: crypted_msg - string - incoming crypted message
401 # RETURNS: nothing
402 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
403 #===============================================================================
404 sub process_incoming_msg {
405 my ($msg, $msg_hash, $remote_ip) = @_ ;
406 my $error = 0;
407 my $host_name;
408 my $host_key;
409 my @out_msg_l;
411 # process incoming msg
412 my $header = @{$msg_hash->{header}}[0];
413 my @target_l = @{$msg_hash->{target}};
415 &main::daemon_log("SIPackages: msg to process: $header", 3);
416 &main::daemon_log("$msg", 8);
418 if( 0 == length @target_l){
419 &main::daemon_log("ERROR: no target specified for msg $header", 1);
420 $error++;
421 }
423 if( 1 == length @target_l) {
424 my $target = $target_l[0];
425 if( $target eq $server_address ) {
426 if ($header eq 'new_key') {
427 @out_msg_l = &new_key($msg_hash)
428 } elsif ($header eq 'here_i_am') {
429 @out_msg_l = &here_i_am($msg_hash)
430 } elsif ($header eq 'who_has') {
431 @out_msg_l = &who_has($msg_hash)
432 } elsif ($header eq 'who_has_i_do') {
433 @out_msg_l = &who_has_i_do($msg_hash)
434 } elsif ($header eq 'got_ping') {
435 @out_msg_l = &got_ping($msg_hash)
436 } elsif ($header eq 'get_load') {
437 @out_msg_l = &execute_actions($msg_hash)
438 } elsif ($header eq 'detected_hardware') {
439 @out_msg_l = &process_detected_hardware($msg_hash)
440 } elsif ($header eq 'trigger_wake') {
441 foreach (@{$msg_hash->{macAddress}}){
442 &main::daemon_log("SIPackages: trigger wake for $_", 1);
443 do_wake($_);
444 }
446 } else {
447 &main::daemon_log("ERROR: $header is an unknown core function", 1);
448 $error++;
449 }
450 }
451 else {
452 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
453 push(@out_msg_l, $msg);
454 }
455 }
457 # if( $error == 0) {
458 # if( 0 == @out_msg_l ) {
459 # push(@out_msg_l, $msg);
460 # }
461 # }
463 return \@out_msg_l;
464 }
467 #=== FUNCTION ================================================================
468 # NAME: got_ping
469 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
470 # RETURNS: nothing
471 # DESCRIPTION: process this incoming message
472 #===============================================================================
473 sub got_ping {
474 my ($msg_hash) = @_;
476 my $source = @{$msg_hash->{source}}[0];
477 my $target = @{$msg_hash->{target}}[0];
478 my $header = @{$msg_hash->{header}}[0];
480 if(exists $main::known_daemons->{$source}) {
481 &main::add_content2known_daemons(hostname=>$source, status=>$header);
482 } else {
483 &main::add_content2known_clients(hostname=>$source, status=>$header);
484 }
486 return;
487 }
490 #=== FUNCTION ================================================================
491 # NAME: new_passwd
492 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
493 # RETURNS: nothing
494 # DESCRIPTION: process this incoming message
495 #===============================================================================
496 sub new_key {
497 my ($msg_hash) = @_;
498 my @out_msg_l;
500 my $header = @{$msg_hash->{header}}[0];
501 my $source_name = @{$msg_hash->{source}}[0];
502 my $source_key = @{$msg_hash->{new_key}}[0];
503 my $query_res;
505 # check known_clients_db
506 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
507 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
508 if( 1 == keys %{$query_res} ) {
509 my $act_time = &get_time;
510 my $sql_statement= "UPDATE known_clients ".
511 "SET hostkey='$source_key', timestamp='$act_time' ".
512 "WHERE hostname='$source_name'";
513 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
514 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
515 my $out_msg = &create_xml_string($hash);
516 push(@out_msg_l, $out_msg);
517 }
519 # only do if host still not found
520 if( 0 == @out_msg_l ) {
521 # check known_server_db
522 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
523 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
524 if( 1 == keys %{$query_res} ) {
525 my $act_time = &get_time;
526 my $sql_statement= "UPDATE known_server ".
527 "SET hostkey='$source_key', timestamp='$act_time' ".
528 "WHERE hostname='$source_name'";
529 my $res = $main::known_server_db->update_dbentry( $sql_statement );
531 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
532 my $out_msg = &create_xml_string($hash);
533 push(@out_msg_l, $out_msg);
534 }
535 }
537 return @out_msg_l;
538 }
541 #=== FUNCTION ================================================================
542 # NAME: here_i_am
543 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
544 # RETURNS: nothing
545 # DESCRIPTION: process this incoming message
546 #===============================================================================
547 sub here_i_am {
548 my ($msg_hash) = @_;
549 my @out_msg_l;
550 my $out_hash;
552 my $source = @{$msg_hash->{source}}[0];
553 my $mac_address = @{$msg_hash->{mac_address}}[0];
554 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
556 # number of known clients
557 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
559 # check wether client address or mac address is already known
560 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
561 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
563 if ( 1 == keys %{$db_res} ) {
564 &main::daemon_log("WARNING: $source is already known as a client", 1);
565 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
566 $nu_clients --;
567 }
569 # number of actual activ clients
570 my $act_nu_clients = $nu_clients;
572 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
573 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
575 if($max_clients <= $act_nu_clients) {
576 my $out_hash = &create_xml_hash("denied", $server_address, $source);
577 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
578 my $passwd = @{$msg_hash->{new_passwd}}[0];
579 &send_msg_hash2address($out_hash, $source, $passwd);
580 return;
581 }
583 # new client accepted
584 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
586 # create entry in known_clients
587 my $events = @{$msg_hash->{events}}[0];
590 # add entry to known_clients_db
591 my $act_timestamp = &get_time;
592 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
593 primkey=>'hostname',
594 hostname=>$source,
595 events=>$events,
596 macaddress=>$mac_address,
597 status=>'registered',
598 hostkey=>$new_passwd,
599 timestamp=>$act_timestamp,
600 } );
602 if ($res != 0) {
603 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
604 return;
605 }
607 # return acknowledgement to client
608 $out_hash = &create_xml_hash("registered", $server_address, $source);
609 my $register_out = &create_xml_string($out_hash);
610 push(@out_msg_l, $register_out);
612 # notify registered client to bus
613 if( $bus_activ eq "on") {
614 # fetch actual bus key
615 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
616 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
617 my $hostkey = $query_res->{1}->{'hostkey'};
619 # send update msg to bus
620 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
621 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
622 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
623 my $new_client_out = &create_xml_string($out_hash);
624 push(@out_msg_l, $new_client_out);
625 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
626 }
628 # give the new client his ldap config
629 my $new_ldap_config_out = &new_ldap_config($source);
630 if( $new_ldap_config_out ) {
631 push(@out_msg_l, $new_ldap_config_out);
632 }
634 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
635 if( $hardware_config_out ) {
636 push(@out_msg_l, $hardware_config_out);
637 }
639 return @out_msg_l;
640 }
643 #=== FUNCTION ================================================================
644 # NAME: who_has
645 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
646 # RETURNS: nothing
647 # DESCRIPTION: process this incoming message
648 #===============================================================================
649 sub who_has {
650 my ($msg_hash) = @_ ;
651 my @out_msg_l;
653 # what is your search pattern
654 my $search_pattern = @{$msg_hash->{who_has}}[0];
655 my $search_element = @{$msg_hash->{$search_pattern}}[0];
656 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
658 # scanning known_clients for search_pattern
659 my @host_addresses = keys %$main::known_clients;
660 my $known_clients_entries = length @host_addresses;
661 my $host_address;
662 foreach my $host (@host_addresses) {
663 my $client_element = $main::known_clients->{$host}->{$search_pattern};
664 if ($search_element eq $client_element) {
665 $host_address = $host;
666 last;
667 }
668 }
670 # search was successful
671 if (defined $host_address) {
672 my $source = @{$msg_hash->{source}}[0];
673 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
674 &add_content2xml_hash($out_hash, "mac_address", $search_element);
675 my $out_msg = &create_xml_string($out_hash);
676 push(@out_msg_l, $out_msg);
677 }
678 return @out_msg_l;
679 }
682 sub who_has_i_do {
683 my ($msg_hash) = @_ ;
684 my $header = @{$msg_hash->{header}}[0];
685 my $source = @{$msg_hash->{source}}[0];
686 my $search_param = @{$msg_hash->{$header}}[0];
687 my $search_value = @{$msg_hash->{$search_param}}[0];
688 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
689 }
691 #=== FUNCTION ================================================================
692 # NAME: new_ldap_config
693 # PARAMETERS: address - string - ip address and port of a host
694 # RETURNS: nothing
695 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
696 #===============================================================================
697 sub new_ldap_config {
698 my ($address) = @_ ;
700 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
701 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
703 # check hit
704 my $hit_counter = keys %{$res};
705 if( not $hit_counter == 1 ) {
706 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
707 }
709 my $macaddress = $res->{1}->{macaddress};
710 my $hostkey = $res->{1}->{hostkey};
712 if (not defined $macaddress) {
713 &main::daemon_log("ERROR: no mac address found for client $address", 1);
714 return;
715 }
717 # Build LDAP connection
718 my $ldap = Net::LDAP->new($ldap_uri);
719 if( not defined $ldap ) {
720 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
721 return;
722 }
725 # Bind to a directory with dn and password
726 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
728 # Perform search
729 $mesg = $ldap->search( base => $ldap_base,
730 scope => 'sub',
731 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
732 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
733 #$mesg->code && die $mesg->error;
734 if($mesg->code) {
735 &main::daemon_log($mesg->error, 1);
736 return;
737 }
739 # Sanity check
740 if ($mesg->count != 1) {
741 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
742 &main::daemon_log("\tbase: $ldap_base", 1);
743 &main::daemon_log("\tscope: sub", 1);
744 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
745 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
746 return;
747 }
749 my $entry= $mesg->entry(0);
750 my $dn= $entry->dn;
751 my @servers= $entry->get_value("gotoLdapServer");
752 my $unit_tag= $entry->get_value("gosaUnitTag");
753 my @ldap_uris;
754 my $server;
755 my $base;
757 # Do we need to look at an object class?
758 if (length(@servers) < 1){
759 $mesg = $ldap->search( base => $ldap_base,
760 scope => 'sub',
761 attrs => ['dn', 'gotoLdapServer'],
762 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
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: no LDAP information found for client mac $macaddress", 1);
772 return;
773 }
775 $entry= $mesg->entry(0);
776 $dn= $entry->dn;
777 @servers= $entry->get_value("gotoLdapServer");
778 }
780 @servers= sort (@servers);
782 foreach $server (@servers){
783 $base= $server;
784 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
785 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
786 push (@ldap_uris, $server);
787 }
789 # Assemble data package
790 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
791 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
793 # Need to append GOto settings?
794 if (defined $goto_admin and defined $goto_secret){
795 $data{'goto_admin'}= $goto_admin;
796 $data{'goto_secret'}= $goto_secret;
797 }
799 # Append unit tag if needed
800 if (defined $unit_tag){
802 # Find admin base and department name
803 $mesg = $ldap->search( base => $ldap_base,
804 scope => 'sub',
805 attrs => ['dn', 'ou', 'FAIclass'],
806 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
807 #$mesg->code && die $mesg->error;
808 if($mesg->code) {
809 &main::daemon_log($mesg->error, 1);
810 return;
811 }
813 # Sanity check
814 if ($mesg->count != 1) {
815 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
816 return;
817 }
819 $entry= $mesg->entry(0);
820 $data{'admin_base'}= $entry->dn;
821 $data{'department'}= $entry->get_value("ou");
823 # Fill release if available
824 my $ou= $entry->get_value("ou");
825 if (defined $ou && $ou =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
826 $data{'release'}= $1;
827 }
829 # Append unit Tag
830 $data{'unit_tag'}= $unit_tag;
831 }
833 # Unbind
834 $mesg = $ldap->unbind;
836 # Send information
837 return send_msg("new_ldap_config", $server_address, $address, \%data);
838 }
840 sub process_detected_hardware {
841 my $msg_hash = shift;
842 my $address = $msg_hash->{source}[0];
843 my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
845 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
846 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
848 # check hit
849 my $hit_counter = keys %{$res};
850 if( not $hit_counter == 1 ) {
851 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
852 return;
853 }
855 my $macaddress = $res->{1}->{macaddress};
856 my $hostkey = $res->{1}->{hostkey};
858 if (not defined $macaddress) {
859 &main::daemon_log("ERROR: no mac address found for client $address", 1);
860 return;
861 }
862 # Build LDAP connection
863 my $ldap = Net::LDAP->new($ldap_uri);
864 if( not defined $ldap ) {
865 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
866 return;
867 }
869 # Bind to a directory with dn and password
870 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
872 # Perform search
873 $mesg = $ldap->search(
874 base => $ldap_base,
875 scope => 'sub',
876 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
877 );
879 # We need to create a base entry first (if not done from ArpHandler)
880 if($mesg->count == 0) {
881 &main::daemon_log("Need to create a new LDAP Entry for client $address", 1);
882 my $resolver=Net::DNS::Resolver->new;
883 my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
884 my $dnsresult= $resolver->search($ipaddress);
885 my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
886 my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
887 my $dn = "cn=$cn,ou=incoming,$ldap_base";
888 &main::daemon_log("Creating entry for $dn",6);
889 my $entry= Net::LDAP::Entry->new( $dn );
890 $entry->dn($dn);
891 $entry->add("objectClass" => "goHard");
892 $entry->add("cn" => $cn);
893 $entry->add("macAddress" => $macaddress);
894 $entry->add("gotomode" => "locked");
895 $entry->add("gotoSysStatus" => "new-system");
896 $entry->add("ipHostNumber" => $ipaddress);
897 if(my $res=$entry->update($ldap)) {
898 # Fill $mesg again
899 $mesg = $ldap->search(
900 base => $ldap_base,
901 scope => 'sub',
902 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
903 );
904 } else {
905 &main::daemon_log("ERROR: There was a problem adding the entry", 1);
906 }
908 }
910 if($mesg->count == 1) {
911 my $entry= $mesg->entry(0);
912 $entry->changetype("modify");
913 foreach my $attribute (
914 "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
915 "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
916 "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
917 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
918 if(defined($entry->get_value($attribute))) {
919 $entry->delete($attribute);
920 }
921 &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
922 $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
923 }
924 }
925 foreach my $attribute (
926 "gotoModules", "ghScsiDev", "ghIdeDev") {
927 if(defined($msg_hash->{detected_hardware}[0]->{$attribute})) {
928 if(defined($entry->get_value($attribute))) {
929 $entry->delete($attribute);
930 }
931 foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
932 $entry->add($attribute => $array_entry);
933 }
934 }
936 }
938 if($entry->update($ldap)) {
939 &main::daemon_log("Added Hardware configuration to LDAP", 4);
940 }
942 }
943 return;
944 }
945 #=== FUNCTION ================================================================
946 # NAME: hardware_config
947 # PARAMETERS: address - string - ip address and port of a host
948 # RETURNS:
949 # DESCRIPTION:
950 #===============================================================================
951 sub hardware_config {
952 my ($address, $gotoHardwareChecksum) = @_ ;
954 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
955 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
957 # check hit
958 my $hit_counter = keys %{$res};
959 if( not $hit_counter == 1 ) {
960 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
961 }
963 my $macaddress = $res->{1}->{macaddress};
964 my $hostkey = $res->{1}->{hostkey};
966 if (not defined $macaddress) {
967 &main::daemon_log("ERROR: no mac address found for client $address", 1);
968 return;
969 }
971 # Build LDAP connection
972 my $ldap = Net::LDAP->new($ldap_uri);
973 if( not defined $ldap ) {
974 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
975 return;
976 }
978 # Bind to a directory with dn and password
979 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
981 # Perform search
982 $mesg = $ldap->search(
983 base => $ldap_base,
984 scope => 'sub',
985 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
986 );
988 if($mesg->count() == 0) {
989 &main::daemon_log("Host was not found in LDAP!", 1);
990 } else {
991 my $entry= $mesg->entry(0);
992 my $dn= $entry->dn;
993 if(defined($entry->get_value("gotoHardwareChecksum"))) {
994 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
995 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
996 if($entry->update($ldap)) {
997 &main::daemon_log("Hardware changed! Detection triggered.", 4);
998 }
999 } else {
1000 # Nothing to do
1001 return;
1002 }
1003 }
1004 }
1005 # need to fill it to LDAP
1006 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
1007 #if($entry->update($ldap)) {
1008 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
1009 #}
1011 ## Look if there another host with this checksum to use the hardware config
1012 #$mesg = $ldap->search(
1013 # base => $ldap_base,
1014 # scope => 'sub',
1015 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
1016 #);
1018 #if($mesg->count>1) {
1019 # my $clone_entry= $mesg->entry(0);
1020 # $entry->changetype("modify");
1021 # foreach my $attribute (
1022 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
1023 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
1024 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
1025 # my $value= $clone_entry->get_value($attribute);
1026 # if(defined($value)) {
1027 # if(defined($entry->get_value($attribute))) {
1028 # $entry->delete($attribute);
1029 # }
1030 # &main::daemon_log("Adding attribute $attribute with value $value",1);
1031 # $entry->add($attribute => $value);
1032 # }
1033 # }
1034 # foreach my $attribute (
1035 # "gotoModules", "ghScsiDev", "ghIdeDev") {
1036 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
1037 # if(defined($array)) {
1038 # if(defined($entry->get_value($attribute))) {
1039 # $entry->delete($attribute);
1040 # }
1041 # foreach my $array_entry (@{$array}) {
1042 # $entry->add($attribute => $array_entry);
1043 # }
1044 # }
1046 # }
1047 # if($entry->update($ldap)) {
1048 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1049 # }
1051 #}
1054 # Assemble data package
1055 my %data = ();
1057 # Need to append GOto settings?
1058 if (defined $goto_admin and defined $goto_secret){
1059 $data{'goto_admin'}= $goto_admin;
1060 $data{'goto_secret'}= $goto_secret;
1061 }
1063 # Unbind
1064 $mesg = $ldap->unbind;
1066 &main::daemon_log("Send detect_hardware message to $address", 4);
1068 # Send information
1069 return send_msg("detect_hardware", $server_address, $address, \%data);
1070 }
1073 #=== FUNCTION ================================================================
1074 # NAME: execute_actions
1075 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1076 # RETURNS: nothing
1077 # DESCRIPTION: invokes the script specified in msg_hash which is located under
1078 # /etc/gosad/actions
1079 #===============================================================================
1080 sub execute_actions {
1081 my ($msg_hash) = @_ ;
1082 my $configdir= '/etc/gosad/actions/';
1083 my $result;
1085 my $header = @{$msg_hash->{header}}[0];
1086 my $source = @{$msg_hash->{source}}[0];
1087 my $target = @{$msg_hash->{target}}[0];
1089 if((not defined $source)
1090 && (not defined $target)
1091 && (not defined $header)) {
1092 &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1093 } else {
1094 my $parameters="";
1095 my @params = @{$msg_hash->{$header}};
1096 my $params = join(", ", @params);
1097 &main::daemon_log("execute_actions: got parameters: $params", 5);
1099 if (@params) {
1100 foreach my $param (@params) {
1101 my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1102 &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1103 $parameters.= " ".$param_value;
1104 }
1105 }
1107 my $cmd= $configdir.$header."$parameters";
1108 &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1109 $result= "";
1110 open(PIPE, "$cmd 2>&1 |");
1111 while(<PIPE>) {
1112 $result.=$_;
1113 }
1114 close(PIPE);
1115 }
1117 # process the event result
1120 return;
1121 }
1124 1;