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