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 my $event_dir = "/usr/lib/gosa-si/server/events";
22 use lib "/usr/lib/gosa-si/server/events";
24 BEGIN{}
25 END {}
27 my ($server_ip, $server_mac_address, $server_port, $SIPackages_key, $max_clients, $ldap_uri, $ldap_base, $ldap_admin_dn, $ldap_admin_password, $server_interface);
28 my ($bus_activ, $bus_key, $bus_ip, $bus_port);
29 my $server;
30 my $event_hash;
31 my $network_interface;
32 my $no_bus;
33 my (@ldap_cfg, @pam_cfg, @nss_cfg, $goto_admin, $goto_secret);
35 my %cfg_defaults = (
36 "bus" => {
37 "activ" => [\$bus_activ, "on"],
38 "key" => [\$bus_key, ""],
39 "ip" => [\$bus_ip, ""],
40 "port" => [\$bus_port, "20080"],
41 },
42 "server" => {
43 "ip" => [\$server_ip, "0.0.0.0"],
44 "mac-address" => [\$server_mac_address, "00:00:00:00:00"],
45 "port" => [\$server_port, "20081"],
46 "ldap-uri" => [\$ldap_uri, ""],
47 "ldap-base" => [\$ldap_base, ""],
48 "ldap-admin-dn" => [\$ldap_admin_dn, ""],
49 "ldap-admin-password" => [\$ldap_admin_password, ""],
50 "max-clients" => [\$max_clients, 100],
51 },
52 "SIPackages" => {
53 "key" => [\$SIPackages_key, ""],
54 },
55 );
57 ### START #####################################################################
59 # read configfile and import variables
60 &read_configfile();
63 # if server_ip is not an ip address but a name
64 if( inet_aton($server_ip) ){ $server_ip = inet_ntoa(inet_aton($server_ip)); }
65 $network_interface= &get_interface_for_ip($server_ip);
66 $server_mac_address= &get_mac($network_interface);
68 &import_events();
70 # Unit tag can be defined in config
71 if((not defined($main::gosa_unit_tag)) || length($main::gosa_unit_tag) == 0) {
72 # Read gosaUnitTag from LDAP
73 my $tmp_ldap= Net::LDAP->new($ldap_uri);
74 if(defined($tmp_ldap)) {
75 &main::daemon_log("Searching for servers gosaUnitTag with mac address $server_mac_address",6);
76 my $mesg= $tmp_ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
77 # Perform search for Unit Tag
78 $mesg = $tmp_ldap->search(
79 base => $ldap_base,
80 scope => 'sub',
81 attrs => ['gosaUnitTag'],
82 filter => "(macaddress=$server_mac_address)"
83 );
85 if ($mesg->count == 1) {
86 my $entry= $mesg->entry(0);
87 my $unit_tag= $entry->get_value("gosaUnitTag");
88 if(defined($unit_tag) && length($unit_tag) > 0) {
89 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
90 $main::gosa_unit_tag= $unit_tag;
91 }
92 } else {
93 # Perform another search for Unit Tag
94 my $hostname= `hostname -f`;
95 chomp($hostname);
96 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
97 $mesg = $tmp_ldap->search(
98 base => $ldap_base,
99 scope => 'sub',
100 attrs => ['gosaUnitTag'],
101 filter => "(&(cn=$hostname)(objectClass=goServer))"
102 );
103 if ($mesg->count == 1) {
104 my $entry= $mesg->entry(0);
105 my $unit_tag= $entry->get_value("gosaUnitTag");
106 if(defined($unit_tag) && length($unit_tag) > 0) {
107 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
108 $main::gosa_unit_tag= $unit_tag;
109 }
110 } else {
111 # Perform another search for Unit Tag
112 $hostname= `hostname -s`;
113 chomp($hostname);
114 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
115 $mesg = $tmp_ldap->search(
116 base => $ldap_base,
117 scope => 'sub',
118 attrs => ['gosaUnitTag'],
119 filter => "(&(cn=$hostname)(objectClass=goServer))"
120 );
121 if ($mesg->count == 1) {
122 my $entry= $mesg->entry(0);
123 my $unit_tag= $entry->get_value("gosaUnitTag");
124 if(defined($unit_tag) && length($unit_tag) > 0) {
125 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
126 $main::gosa_unit_tag= $unit_tag;
127 }
128 } else {
129 &main::daemon_log("Not using gosaUnitTag", 6);
130 }
131 }
132 }
133 } else {
134 &main::daemon_log("Using gosaUnitTag from config-file: $main::gosa_unit_tag",6);
135 }
136 $tmp_ldap->unbind;
137 }
139 # complete addresses
140 #if( $server_ip eq "0.0.0.0" ) {
141 # $server_ip = "127.0.0.1";
142 #}
144 my $server_address = "$server_ip:$server_port";
145 $main::server_address = $server_address;
148 if( inet_aton($bus_ip) ){ $bus_ip = inet_ntoa(inet_aton($bus_ip)); }
149 ######################################################
150 # to change
151 if( $bus_ip eq "127.0.1.1" ) { $bus_ip = "127.0.0.1" }
152 ######################################################
153 my $bus_address = "$bus_ip:$bus_port";
154 $main::bus_address = $bus_address;
156 # create general settings for this module
157 my $xml = new XML::Simple();
159 # register at bus
160 if ($main::no_bus > 0) {
161 $bus_activ = "off"
162 }
163 if($bus_activ eq "on") {
164 ®ister_at_bus();
165 }
167 # add myself to known_server_db
168 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
169 primkey=>'hostname',
170 hostname=>$server_address,
171 status=>'myself',
172 hostkey=>$SIPackages_key,
173 timestamp=>&get_time,
174 } );
178 ### functions #################################################################
181 sub get_module_info {
182 my @info = ($server_address,
183 $SIPackages_key,
184 );
185 return \@info;
186 }
189 #sub daemon_log {
190 # my ($msg, $level) = @_ ;
191 # &main::daemon_log($msg, $level);
192 # return;
193 #}
194 #
196 #sub do_wake {
197 # my $host = shift;
198 # my $ipaddr = shift || '255.255.255.255';
199 # my $port = getservbyname('discard', 'udp');
200 #
201 # my ($raddr, $them, $proto);
202 # my ($hwaddr, $hwaddr_re, $pkt);
203 #
204 # # get the hardware address (ethernet address)
205 #
206 # $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
207 # if ($host =~ m/^$hwaddr_re$/) {
208 # $hwaddr = $host;
209 # } else {
210 # # $host is not a hardware address, try to resolve it
211 # my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
212 # my $ip_addr;
213 # if ($host =~ m/^$ip_re$/) {
214 # $ip_addr = $host;
215 # } else {
216 # my $h;
217 # unless ($h = gethost($host)) {
218 # return undef;
219 # }
220 # $ip_addr = inet_ntoa($h->addr);
221 # }
222 # }
223 #
224 # # Generate magic sequence
225 # foreach (split /:/, $hwaddr) {
226 # $pkt .= chr(hex($_));
227 # }
228 # $pkt = chr(0xFF) x 6 . $pkt x 16;
229 #
230 # # Allocate socket and send packet
231 #
232 # $raddr = gethostbyname($ipaddr)->addr;
233 # $them = pack_sockaddr_in($port, $raddr);
234 # $proto = getprotobyname('udp');
235 #
236 # socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
237 # setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
238 #
239 # send(S, $pkt, 0, $them) or die "send : $!";
240 # close S;
241 #}
244 #=== FUNCTION ================================================================
245 # NAME: read_configfile
246 # PARAMETERS: cfg_file - string -
247 # RETURNS: nothing
248 # DESCRIPTION: read cfg_file and set variables
249 #===============================================================================
250 sub read_configfile {
251 my $cfg;
252 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
253 if( -r $main::cfg_file ) {
254 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
255 } else {
256 print STDERR "Couldn't read config file!";
257 }
258 } else {
259 $cfg = Config::IniFiles->new() ;
260 }
261 foreach my $section (keys %cfg_defaults) {
262 foreach my $param (keys %{$cfg_defaults{ $section }}) {
263 my $pinfo = $cfg_defaults{ $section }{ $param };
264 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
265 }
266 }
268 # Read non predefined sections
269 my $param;
270 if ($cfg->SectionExists('ldap')){
271 foreach $param ($cfg->Parameters('ldap')){
272 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
273 }
274 }
275 if ($cfg->SectionExists('pam_ldap')){
276 foreach $param ($cfg->Parameters('pam_ldap')){
277 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
278 }
279 }
280 if ($cfg->SectionExists('nss_ldap')){
281 foreach $param ($cfg->Parameters('nss_ldap')){
282 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
283 }
284 }
285 if ($cfg->SectionExists('goto')){
286 $goto_admin= $cfg->val('goto', 'terminal_admin');
287 $goto_secret= $cfg->val('goto', 'terminal_secret');
288 } else {
289 $goto_admin= undef;
290 $goto_secret= undef;
291 }
293 }
295 #=== FUNCTION ================================================================
296 # NAME: get_interface_for_ip
297 # PARAMETERS: ip address (i.e. 192.168.0.1)
298 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
299 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
300 #===============================================================================
301 sub get_interface_for_ip {
302 my $result;
303 my $ip= shift;
304 if ($ip && length($ip) > 0) {
305 my @ifs= &get_interfaces();
306 if($ip eq "0.0.0.0") {
307 $result = "all";
308 } else {
309 foreach (@ifs) {
310 my $if=$_;
311 if(get_ip($if) eq $ip) {
312 $result = $if;
313 }
314 }
315 }
316 }
317 return $result;
318 }
320 #=== FUNCTION ================================================================
321 # NAME: get_interfaces
322 # PARAMETERS: none
323 # RETURNS: (list of interfaces)
324 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
325 #===============================================================================
326 sub get_interfaces {
327 my @result;
328 my $PROC_NET_DEV= ('/proc/net/dev');
330 open(PROC_NET_DEV, "<$PROC_NET_DEV")
331 or die "Could not open $PROC_NET_DEV";
333 my @ifs = <PROC_NET_DEV>;
335 close(PROC_NET_DEV);
337 # Eat first two line
338 shift @ifs;
339 shift @ifs;
341 chomp @ifs;
342 foreach my $line(@ifs) {
343 my $if= (split /:/, $line)[0];
344 $if =~ s/^\s+//;
345 push @result, $if;
346 }
348 return @result;
349 }
351 #=== FUNCTION ================================================================
352 # NAME: get_mac
353 # PARAMETERS: interface name (i.e. eth0)
354 # RETURNS: (mac address)
355 # DESCRIPTION: Uses ioctl to get mac address directly from system.
356 #===============================================================================
357 sub get_mac {
358 my $ifreq= shift;
359 my $result;
360 if ($ifreq && length($ifreq) > 0) {
361 if($ifreq eq "all") {
362 $result = "00:00:00:00:00:00";
363 } else {
364 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
366 # A configured MAC Address should always override a guessed value
367 if ($server_mac_address and length($server_mac_address) > 0) {
368 $result= $server_mac_address;
369 }
371 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
372 or die "socket: $!";
374 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
375 my ($if, $mac)= unpack 'h36 H12', $ifreq;
377 if (length($mac) > 0) {
378 $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])$/;
379 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
380 $result = $mac;
381 }
382 }
383 }
384 }
385 return $result;
386 }
389 #=== FUNCTION ================================================================
390 # NAME: register_at_bus
391 # PARAMETERS: nothing
392 # RETURNS: nothing
393 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
394 #===============================================================================
395 sub register_at_bus {
397 # add bus to known_server_db
398 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
399 primkey=>'hostname',
400 hostname=>$bus_address,
401 status=>'bus',
402 hostkey=>$bus_key,
403 timestamp=>&get_time,
404 } );
405 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
406 my $msg = &create_xml_string($msg_hash);
408 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
409 return $msg;
410 }
413 sub import_events {
414 if (not -e $event_dir) {
415 &main::daemon_log("ERROR: cannot find directory or directory is not readable: $event_dir", 1);
416 }
417 opendir (DIR, $event_dir) or die "ERROR while loading gosa-si-events from directory $event_dir : $!\n";
419 while (defined (my $event = readdir (DIR))) {
420 if( $event eq "." || $event eq ".." ) { next; }
421 if( $event eq "gosaTriggered.pm" ) { next; } # only GOsa specific events
423 eval{ require $event; };
424 if( $@ ) {
425 &main::daemon_log("import of event module '$event' failed", 1);
426 &main::daemon_log("$@", 8);
427 next;
428 }
430 $event =~ /(\S*?).pm$/;
431 my $event_module = $1;
432 my $events_l = eval( $1."::get_events()") ;
433 foreach my $event_name (@{$events_l}) {
434 $event_hash->{$event_name} = $event_module;
435 }
436 my $events_string = join( ", ", @{$events_l});
437 &main::daemon_log("INFO: SIPackages imported events $events_string", 5);
438 }
439 }
442 #=== FUNCTION ================================================================
443 # NAME: process_incoming_msg
444 # PARAMETERS: crypted_msg - string - incoming crypted message
445 # RETURNS: nothing
446 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
447 #===============================================================================
448 sub process_incoming_msg {
449 my ($msg, $msg_hash, $session_id) = @_ ;
450 my $error = 0;
451 my $host_name;
452 my $host_key;
453 my @out_msg_l = ();
455 # process incoming msg
456 my $header = @{$msg_hash->{header}}[0];
457 my @target_l = @{$msg_hash->{target}};
459 # skip PREFIX
460 $header =~ s/^CLMSG_//;
462 &main::daemon_log("SIPackages: msg to process: $header", 3);
463 &main::daemon_log("$msg", 8);
465 if( 0 == length @target_l){
466 &main::daemon_log("ERROR: no target specified for msg $header", 1);
467 $error++;
468 }
470 if( 1 == length @target_l) {
471 my $target = $target_l[0];
472 if(&server_matches($target)) {
475 if ($header eq 'new_key') {
476 @out_msg_l = &new_key($msg_hash)
477 } elsif ($header eq 'here_i_am') {
478 @out_msg_l = &here_i_am($msg_hash)
479 } else {
480 if( exists $event_hash->{$header} ) {
481 # a event exists with the header as name
482 &main::daemon_log("found event '$header' at event-module '".$event_hash->{$header}."'", 5);
483 no strict 'refs';
484 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
485 }
486 }
488 # if delivery not possible raise error and return
489 if( not @out_msg_l ) {
490 &main::daemon_log("WARNING: SIPackages got not answer from event handler '$header'", 3);
491 } elsif( 0 == @out_msg_l) {
492 &main::daemon_log("ERROR: SIPackages: no event handler or core function defined for '$header'", 1);
493 }
494 }
495 else {
496 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
497 push(@out_msg_l, $msg);
498 }
499 }
501 return \@out_msg_l;
502 }
505 #=== FUNCTION ================================================================
506 # NAME: got_ping
507 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
508 # RETURNS: nothing
509 # DESCRIPTION: process this incoming message
510 #===============================================================================
511 #sub got_ping {
512 # my ($msg_hash) = @_;
513 #
514 # my $source = @{$msg_hash->{source}}[0];
515 # my $target = @{$msg_hash->{target}}[0];
516 # my $header = @{$msg_hash->{header}}[0];
517 # my $session_id = @{$msg_hash->{'session_id'}}[0];
518 # my $act_time = &get_time;
519 # my @out_msg_l;
520 # my $out_msg;
521 #
522 # # check known_clients_db
523 # my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
524 # my $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
525 # if( 1 == keys %{$query_res} ) {
526 # my $sql_statement= "UPDATE known_clients ".
527 # "SET status='$header', timestamp='$act_time' ".
528 # "WHERE hostname='$source'";
529 # my $res = $main::known_clients_db->update_dbentry( $sql_statement );
530 # }
531 #
532 # # check known_server_db
533 # $sql_statement = "SELECT * FROM known_server WHERE hostname='$source'";
534 # $query_res = $main::known_server_db->select_dbentry( $sql_statement );
535 # if( 1 == keys %{$query_res} ) {
536 # my $sql_statement= "UPDATE known_server ".
537 # "SET status='$header', timestamp='$act_time' ".
538 # "WHERE hostname='$source'";
539 # my $res = $main::known_server_db->update_dbentry( $sql_statement );
540 # }
541 #
542 # # create out_msg
543 # my $out_hash = &create_xml_hash($header, $source, "GOSA");
544 # &add_content2xml_hash($out_hash, "session_id", $session_id);
545 # $out_msg = &create_xml_string($out_hash);
546 # push(@out_msg_l, $out_msg);
547 #
548 # return @out_msg_l;
549 #}
551 #=== FUNCTION ================================================================
552 # NAME: new_passwd
553 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
554 # RETURNS: nothing
555 # DESCRIPTION: process this incoming message
556 #===============================================================================
557 sub new_key {
558 my ($msg_hash) = @_;
559 my @out_msg_l;
561 my $header = @{$msg_hash->{header}}[0];
562 my $source_name = @{$msg_hash->{source}}[0];
563 my $source_key = @{$msg_hash->{new_key}}[0];
564 my $query_res;
566 # check known_clients_db
567 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
568 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
569 if( 1 == keys %{$query_res} ) {
570 my $act_time = &get_time;
571 my $sql_statement= "UPDATE known_clients ".
572 "SET hostkey='$source_key', timestamp='$act_time' ".
573 "WHERE hostname='$source_name'";
574 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
575 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
576 my $out_msg = &create_xml_string($hash);
577 push(@out_msg_l, $out_msg);
578 }
580 # only do if host still not found
581 if( 0 == @out_msg_l ) {
582 # check known_server_db
583 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
584 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
585 if( 1 == keys %{$query_res} ) {
586 my $act_time = &get_time;
587 my $sql_statement= "UPDATE known_server ".
588 "SET hostkey='$source_key', timestamp='$act_time' ".
589 "WHERE hostname='$source_name'";
590 my $res = $main::known_server_db->update_dbentry( $sql_statement );
592 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
593 my $out_msg = &create_xml_string($hash);
594 push(@out_msg_l, $out_msg);
595 }
596 }
598 return @out_msg_l;
599 }
602 #=== FUNCTION ================================================================
603 # NAME: here_i_am
604 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
605 # RETURNS: nothing
606 # DESCRIPTION: process this incoming message
607 #===============================================================================
608 sub here_i_am {
609 my ($msg_hash) = @_;
610 my @out_msg_l;
611 my $out_hash;
613 my $source = @{$msg_hash->{source}}[0];
614 my $mac_address = @{$msg_hash->{mac_address}}[0];
615 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
617 # number of known clients
618 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
620 # check wether client address or mac address is already known
621 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
622 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
624 if ( 1 == keys %{$db_res} ) {
625 &main::daemon_log("WARNING: $source is already known as a client", 1);
626 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
627 $nu_clients --;
628 }
630 # number of actual activ clients
631 my $act_nu_clients = $nu_clients;
633 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
634 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
636 if($max_clients <= $act_nu_clients) {
637 my $out_hash = &create_xml_hash("denied", $server_address, $source);
638 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
639 my $passwd = @{$msg_hash->{new_passwd}}[0];
640 &send_msg_hash2address($out_hash, $source, $passwd);
641 return;
642 }
644 # new client accepted
645 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
647 # create entry in known_clients
648 my $events = @{$msg_hash->{events}}[0];
651 # add entry to known_clients_db
652 my $act_timestamp = &get_time;
653 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
654 primkey=>'hostname',
655 hostname=>$source,
656 events=>$events,
657 macaddress=>$mac_address,
658 status=>'registered',
659 hostkey=>$new_passwd,
660 timestamp=>$act_timestamp,
661 } );
663 if ($res != 0) {
664 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
665 return;
666 }
668 # return acknowledgement to client
669 $out_hash = &create_xml_hash("registered", $server_address, $source);
670 my $register_out = &create_xml_string($out_hash);
671 push(@out_msg_l, $register_out);
673 # notify registered client to bus
674 if( $bus_activ eq "on") {
675 # fetch actual bus key
676 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
677 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
678 my $hostkey = $query_res->{1}->{'hostkey'};
680 # send update msg to bus
681 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
682 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
683 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
684 my $new_client_out = &create_xml_string($out_hash);
685 push(@out_msg_l, $new_client_out);
686 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
687 }
689 # give the new client his ldap config
690 my $new_ldap_config_out = &new_ldap_config($source);
691 if( $new_ldap_config_out ) {
692 push(@out_msg_l, $new_ldap_config_out);
693 }
695 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
696 if( $hardware_config_out ) {
697 push(@out_msg_l, $hardware_config_out);
698 }
700 return @out_msg_l;
701 }
704 #=== FUNCTION ================================================================
705 # NAME: who_has
706 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
707 # RETURNS: nothing
708 # DESCRIPTION: process this incoming message
709 #===============================================================================
710 sub who_has {
711 my ($msg_hash) = @_ ;
712 my @out_msg_l;
714 # what is your search pattern
715 my $search_pattern = @{$msg_hash->{who_has}}[0];
716 my $search_element = @{$msg_hash->{$search_pattern}}[0];
717 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
719 # scanning known_clients for search_pattern
720 my @host_addresses = keys %$main::known_clients;
721 my $known_clients_entries = length @host_addresses;
722 my $host_address;
723 foreach my $host (@host_addresses) {
724 my $client_element = $main::known_clients->{$host}->{$search_pattern};
725 if ($search_element eq $client_element) {
726 $host_address = $host;
727 last;
728 }
729 }
731 # search was successful
732 if (defined $host_address) {
733 my $source = @{$msg_hash->{source}}[0];
734 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
735 &add_content2xml_hash($out_hash, "mac_address", $search_element);
736 my $out_msg = &create_xml_string($out_hash);
737 push(@out_msg_l, $out_msg);
738 }
739 return @out_msg_l;
740 }
743 sub who_has_i_do {
744 my ($msg_hash) = @_ ;
745 my $header = @{$msg_hash->{header}}[0];
746 my $source = @{$msg_hash->{source}}[0];
747 my $search_param = @{$msg_hash->{$header}}[0];
748 my $search_value = @{$msg_hash->{$search_param}}[0];
749 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
750 }
752 #=== FUNCTION ================================================================
753 # NAME: new_ldap_config
754 # PARAMETERS: address - string - ip address and port of a host
755 # RETURNS: nothing
756 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
757 #===============================================================================
758 sub new_ldap_config {
759 my ($address) = @_ ;
761 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
762 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
764 # check hit
765 my $hit_counter = keys %{$res};
766 if( not $hit_counter == 1 ) {
767 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
768 }
770 my $macaddress = $res->{1}->{macaddress};
771 my $hostkey = $res->{1}->{hostkey};
773 if (not defined $macaddress) {
774 &main::daemon_log("ERROR: no mac address found for client $address", 1);
775 return;
776 }
778 # Build LDAP connection
779 my $ldap = Net::LDAP->new($ldap_uri);
780 if( not defined $ldap ) {
781 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
782 return;
783 }
786 # Bind to a directory with dn and password
787 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
789 # Perform search
790 $mesg = $ldap->search( base => $ldap_base,
791 scope => 'sub',
792 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
793 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
794 #$mesg->code && die $mesg->error;
795 if($mesg->code) {
796 &main::daemon_log($mesg->error, 1);
797 return;
798 }
800 # Sanity check
801 if ($mesg->count != 1) {
802 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
803 &main::daemon_log("\tbase: $ldap_base", 1);
804 &main::daemon_log("\tscope: sub", 1);
805 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
806 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
807 return;
808 }
810 my $entry= $mesg->entry(0);
811 my $dn= $entry->dn;
812 my @servers= $entry->get_value("gotoLdapServer");
813 my $unit_tag= $entry->get_value("gosaUnitTag");
814 my @ldap_uris;
815 my $server;
816 my $base;
818 # Do we need to look at an object class?
819 if (length(@servers) < 1){
820 $mesg = $ldap->search( base => $ldap_base,
821 scope => 'sub',
822 attrs => ['dn', 'gotoLdapServer'],
823 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
824 #$mesg->code && die $mesg->error;
825 if($mesg->code) {
826 &main::daemon_log($mesg->error, 1);
827 return;
828 }
830 # Sanity check
831 if ($mesg->count != 1) {
832 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
833 return;
834 }
836 $entry= $mesg->entry(0);
837 $dn= $entry->dn;
838 @servers= $entry->get_value("gotoLdapServer");
839 }
841 @servers= sort (@servers);
843 foreach $server (@servers){
844 # Conversation for backward compatibility
845 if ($server !=~ /^ldap[^:]+:\/\// ) {
846 if ($server =~ /^([^:]+):(.*)$/ ) {
847 $server= "1:dummy:ldap://$1/$2";
848 }
849 }
851 $base= $server;
852 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
853 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
854 push (@ldap_uris, $server);
855 }
857 # Assemble data package
858 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
859 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
861 # Need to append GOto settings?
862 if (defined $goto_admin and defined $goto_secret){
863 $data{'goto_admin'}= $goto_admin;
864 $data{'goto_secret'}= $goto_secret;
865 }
867 # Append unit tag if needed
868 if (defined $unit_tag){
870 # Find admin base and department name
871 $mesg = $ldap->search( base => $ldap_base,
872 scope => 'sub',
873 attrs => ['dn', 'ou', 'FAIclass'],
874 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
875 #$mesg->code && die $mesg->error;
876 if($mesg->code) {
877 &main::daemon_log($mesg->error, 1);
878 return;
879 }
881 # Sanity check
882 if ($mesg->count != 1) {
883 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
884 return;
885 }
887 $entry= $mesg->entry(0);
888 $data{'admin_base'}= $entry->dn;
889 $data{'department'}= $entry->get_value("ou");
891 # Append unit Tag
892 $data{'unit_tag'}= $unit_tag;
893 }
895 # Fill release if available
896 my $FAIclass= $entry->get_value("FAIclass");
897 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
898 $data{'release'}= $1;
899 }
902 # Unbind
903 $mesg = $ldap->unbind;
905 # Send information
906 return &build_msg("new_ldap_config", $server_address, $address, \%data);
907 }
910 #=== FUNCTION ================================================================
911 # NAME: hardware_config
912 # PARAMETERS: address - string - ip address and port of a host
913 # RETURNS:
914 # DESCRIPTION:
915 #===============================================================================
916 sub hardware_config {
917 my ($address, $gotoHardwareChecksum) = @_ ;
919 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
920 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
922 # check hit
923 my $hit_counter = keys %{$res};
924 if( not $hit_counter == 1 ) {
925 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
926 }
928 my $macaddress = $res->{1}->{macaddress};
929 my $hostkey = $res->{1}->{hostkey};
931 if (not defined $macaddress) {
932 &main::daemon_log("ERROR: no mac address found for client $address", 1);
933 return;
934 }
936 # Build LDAP connection
937 my $ldap = Net::LDAP->new($ldap_uri);
938 if( not defined $ldap ) {
939 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
940 return;
941 }
943 # Bind to a directory with dn and password
944 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
946 # Perform search
947 $mesg = $ldap->search(
948 base => $ldap_base,
949 scope => 'sub',
950 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
951 );
953 if($mesg->count() == 0) {
954 &main::daemon_log("Host was not found in LDAP!", 1);
955 } else {
956 my $entry= $mesg->entry(0);
957 my $dn= $entry->dn;
958 if(defined($entry->get_value("gotoHardwareChecksum"))) {
959 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
960 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
961 if($entry->update($ldap)) {
962 &main::daemon_log("Hardware changed! Detection triggered.", 4);
963 }
964 } else {
965 # Nothing to do
966 return;
967 }
968 }
969 }
970 # need to fill it to LDAP
971 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
972 #if($entry->update($ldap)) {
973 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
974 #}
976 ## Look if there another host with this checksum to use the hardware config
977 #$mesg = $ldap->search(
978 # base => $ldap_base,
979 # scope => 'sub',
980 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
981 #);
983 #if($mesg->count>1) {
984 # my $clone_entry= $mesg->entry(0);
985 # $entry->changetype("modify");
986 # foreach my $attribute (
987 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
988 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
989 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
990 # my $value= $clone_entry->get_value($attribute);
991 # if(defined($value)) {
992 # if(defined($entry->get_value($attribute))) {
993 # $entry->delete($attribute);
994 # }
995 # &main::daemon_log("Adding attribute $attribute with value $value",1);
996 # $entry->add($attribute => $value);
997 # }
998 # }
999 # foreach my $attribute (
1000 # "gotoModules", "ghScsiDev", "ghIdeDev") {
1001 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
1002 # if(defined($array)) {
1003 # if(defined($entry->get_value($attribute))) {
1004 # $entry->delete($attribute);
1005 # }
1006 # foreach my $array_entry (@{$array}) {
1007 # $entry->add($attribute => $array_entry);
1008 # }
1009 # }
1011 # }
1012 # if($entry->update($ldap)) {
1013 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1014 # }
1016 #}
1019 # Assemble data package
1020 my %data = ();
1022 # Need to append GOto settings?
1023 if (defined $goto_admin and defined $goto_secret){
1024 $data{'goto_admin'}= $goto_admin;
1025 $data{'goto_secret'}= $goto_secret;
1026 }
1028 # Unbind
1029 $mesg = $ldap->unbind;
1031 &main::daemon_log("Send detect_hardware message to $address", 4);
1033 # Send information
1034 return &build_msg("detect_hardware", $server_address, $address, \%data);
1035 }
1037 sub server_matches {
1038 my $target = shift;
1039 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
1040 my $result = 0;
1042 if($server_ip eq $target_ip) {
1043 $result= 1;
1044 } elsif ($server_ip eq "0.0.0.0") {
1045 if ($target_ip eq "127.0.0.1") {
1046 $result= 1;
1047 } else {
1048 my $PROC_NET_ROUTE= ('/proc/net/route');
1050 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1051 or die "Could not open $PROC_NET_ROUTE";
1053 my @ifs = <PROC_NET_ROUTE>;
1055 close(PROC_NET_ROUTE);
1057 # Eat header line
1058 shift @ifs;
1059 chomp @ifs;
1060 foreach my $line(@ifs) {
1061 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1062 my $destination;
1063 my $mask;
1064 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1065 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1066 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1067 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1068 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1069 # destination matches route, save mac and exit
1070 $result= 1;
1071 last;
1072 }
1073 }
1074 }
1075 } else {
1076 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1077 }
1079 return $result;
1080 }
1083 ##=== FUNCTION ================================================================
1084 ## NAME: execute_actions
1085 ## PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1086 ## RETURNS: nothing
1087 ## DESCRIPTION: invokes the script specified in msg_hash which is located under
1088 ## /etc/gosad/actions
1089 ##===============================================================================
1090 #sub execute_actions {
1091 # my ($msg_hash) = @_ ;
1092 # my $configdir= '/etc/gosad/actions/';
1093 # my $result;
1094 #
1095 # my $header = @{$msg_hash->{header}}[0];
1096 # my $source = @{$msg_hash->{source}}[0];
1097 # my $target = @{$msg_hash->{target}}[0];
1098 #
1099 # if((not defined $source)
1100 # && (not defined $target)
1101 # && (not defined $header)) {
1102 # &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1103 # } else {
1104 # my $parameters="";
1105 # my @params = @{$msg_hash->{$header}};
1106 # my $params = join(", ", @params);
1107 # &main::daemon_log("execute_actions: got parameters: $params", 5);
1108 #
1109 # if (@params) {
1110 # foreach my $param (@params) {
1111 # my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1112 # &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1113 # $parameters.= " ".$param_value;
1114 # }
1115 # }
1116 #
1117 # my $cmd= $configdir.$header."$parameters";
1118 # &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1119 # $result= "";
1120 # open(PIPE, "$cmd 2>&1 |");
1121 # while(<PIPE>) {
1122 # $result.=$_;
1123 # }
1124 # close(PIPE);
1125 # }
1126 #
1127 # # process the event result
1128 #
1129 #
1130 # return;
1131 #}
1132 #
1134 1;