3250fb1cf12f6ac376dc2939ae08c04a47a2cae3
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, $gosa_unit_tag);
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 "gosa-unit-tag" => [\$gosa_unit_tag, ""],
53 },
54 "SIPackages" => {
55 "key" => [\$SIPackages_key, ""],
56 },
57 );
59 ### START #####################################################################
61 # read configfile and import variables
62 &read_configfile();
64 $network_interface= &get_interface_for_ip($server_ip);
65 $server_mac_address= &get_mac($network_interface);
67 &import_events();
69 # Unit tag can be defined in config
70 if((not defined($gosa_unit_tag)) || length($gosa_unit_tag) == 0) {
71 # Read gosaUnitTag from LDAP
72 my $tmp_ldap= Net::LDAP->new($ldap_uri);
73 if(defined($tmp_ldap)) {
74 &main::daemon_log("Searching for servers gosaUnitTag with mac address $server_mac_address",6);
75 my $mesg= $tmp_ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
76 # Perform search for Unit Tag
77 $mesg = $tmp_ldap->search(
78 base => $ldap_base,
79 scope => 'sub',
80 attrs => ['gosaUnitTag'],
81 filter => "(macaddress=$server_mac_address)"
82 );
84 if ($mesg->count == 1) {
85 my $entry= $mesg->entry(0);
86 my $unit_tag= $entry->get_value("gosaUnitTag");
87 if(defined($unit_tag) && length($unit_tag) > 0) {
88 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
89 $gosa_unit_tag= $unit_tag;
90 }
91 } else {
92 # Perform another search for Unit Tag
93 my $hostname= `hostname -f`;
94 chomp($hostname);
95 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
96 $mesg = $tmp_ldap->search(
97 base => $ldap_base,
98 scope => 'sub',
99 attrs => ['gosaUnitTag'],
100 filter => "(&(cn=$hostname)(objectClass=goServer))"
101 );
102 if ($mesg->count == 1) {
103 my $entry= $mesg->entry(0);
104 my $unit_tag= $entry->get_value("gosaUnitTag");
105 if(defined($unit_tag) && length($unit_tag) > 0) {
106 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
107 $gosa_unit_tag= $unit_tag;
108 }
109 } else {
110 # Perform another search for Unit Tag
111 $hostname= `hostname -s`;
112 chomp($hostname);
113 &main::daemon_log("Searching for servers gosaUnitTag with hostname $hostname",6);
114 $mesg = $tmp_ldap->search(
115 base => $ldap_base,
116 scope => 'sub',
117 attrs => ['gosaUnitTag'],
118 filter => "(&(cn=$hostname)(objectClass=goServer))"
119 );
120 if ($mesg->count == 1) {
121 my $entry= $mesg->entry(0);
122 my $unit_tag= $entry->get_value("gosaUnitTag");
123 if(defined($unit_tag) && length($unit_tag) > 0) {
124 &main::daemon_log("Detected gosaUnitTag $unit_tag for creating entries", 4);
125 $gosa_unit_tag= $unit_tag;
126 }
127 } else {
128 &main::daemon_log("Not using gosaUnitTag", 6);
129 }
130 }
131 }
132 } else {
133 &main::daemon_log("Using gosaUnitTag from config-file: $gosa_unit_tag",6);
134 }
135 $tmp_ldap->unbind;
136 }
138 # complete addresses
139 #if( $server_ip eq "0.0.0.0" ) {
140 # $server_ip = "127.0.0.1";
141 #}
142 my $server_address = "$server_ip:$server_port";
143 $main::server_address = $server_address;
144 my $bus_address = "$bus_ip:$bus_port";
145 $main::bus_address = $bus_address;
147 # create general settings for this module
148 my $xml = new XML::Simple();
150 # register at bus
151 if ($main::no_bus > 0) {
152 $bus_activ = "off"
153 }
154 if($bus_activ eq "on") {
155 ®ister_at_bus();
156 }
158 # add myself to known_server_db
159 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
160 primkey=>'hostname',
161 hostname=>$server_address,
162 status=>'myself',
163 hostkey=>$SIPackages_key,
164 timestamp=>&get_time,
165 } );
169 ### functions #################################################################
172 sub get_module_info {
173 my @info = ($server_address,
174 $SIPackages_key,
175 );
176 return \@info;
177 }
180 #sub daemon_log {
181 # my ($msg, $level) = @_ ;
182 # &main::daemon_log($msg, $level);
183 # return;
184 #}
185 #
187 #sub do_wake {
188 # my $host = shift;
189 # my $ipaddr = shift || '255.255.255.255';
190 # my $port = getservbyname('discard', 'udp');
191 #
192 # my ($raddr, $them, $proto);
193 # my ($hwaddr, $hwaddr_re, $pkt);
194 #
195 # # get the hardware address (ethernet address)
196 #
197 # $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
198 # if ($host =~ m/^$hwaddr_re$/) {
199 # $hwaddr = $host;
200 # } else {
201 # # $host is not a hardware address, try to resolve it
202 # my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
203 # my $ip_addr;
204 # if ($host =~ m/^$ip_re$/) {
205 # $ip_addr = $host;
206 # } else {
207 # my $h;
208 # unless ($h = gethost($host)) {
209 # return undef;
210 # }
211 # $ip_addr = inet_ntoa($h->addr);
212 # }
213 # }
214 #
215 # # Generate magic sequence
216 # foreach (split /:/, $hwaddr) {
217 # $pkt .= chr(hex($_));
218 # }
219 # $pkt = chr(0xFF) x 6 . $pkt x 16;
220 #
221 # # Allocate socket and send packet
222 #
223 # $raddr = gethostbyname($ipaddr)->addr;
224 # $them = pack_sockaddr_in($port, $raddr);
225 # $proto = getprotobyname('udp');
226 #
227 # socket(S, AF_INET, SOCK_DGRAM, $proto) or die "socket : $!";
228 # setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt : $!";
229 #
230 # send(S, $pkt, 0, $them) or die "send : $!";
231 # close S;
232 #}
235 #=== FUNCTION ================================================================
236 # NAME: read_configfile
237 # PARAMETERS: cfg_file - string -
238 # RETURNS: nothing
239 # DESCRIPTION: read cfg_file and set variables
240 #===============================================================================
241 sub read_configfile {
242 my $cfg;
243 if( defined( $main::cfg_file) && ( length($main::cfg_file) > 0 )) {
244 if( -r $main::cfg_file ) {
245 $cfg = Config::IniFiles->new( -file => $main::cfg_file );
246 } else {
247 print STDERR "Couldn't read config file!";
248 }
249 } else {
250 $cfg = Config::IniFiles->new() ;
251 }
252 foreach my $section (keys %cfg_defaults) {
253 foreach my $param (keys %{$cfg_defaults{ $section }}) {
254 my $pinfo = $cfg_defaults{ $section }{ $param };
255 ${@$pinfo[0]} = $cfg->val( $section, $param, @$pinfo[1] );
256 }
257 }
259 # Read non predefined sections
260 my $param;
261 if ($cfg->SectionExists('ldap')){
262 foreach $param ($cfg->Parameters('ldap')){
263 push (@ldap_cfg, "$param ".$cfg->val('ldap', $param));
264 }
265 }
266 if ($cfg->SectionExists('pam_ldap')){
267 foreach $param ($cfg->Parameters('pam_ldap')){
268 push (@pam_cfg, "$param ".$cfg->val('pam_ldap', $param));
269 }
270 }
271 if ($cfg->SectionExists('nss_ldap')){
272 foreach $param ($cfg->Parameters('nss_ldap')){
273 push (@nss_cfg, "$param ".$cfg->val('nss_ldap', $param));
274 }
275 }
276 if ($cfg->SectionExists('goto')){
277 $goto_admin= $cfg->val('goto', 'terminal_admin');
278 $goto_secret= $cfg->val('goto', 'terminal_secret');
279 } else {
280 $goto_admin= undef;
281 $goto_secret= undef;
282 }
284 }
286 #=== FUNCTION ================================================================
287 # NAME: get_interface_for_ip
288 # PARAMETERS: ip address (i.e. 192.168.0.1)
289 # RETURNS: array: list of interfaces if ip=0.0.0.0, matching interface if found, undef else
290 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
291 #===============================================================================
292 sub get_interface_for_ip {
293 my $result;
294 my $ip= shift;
295 if ($ip && length($ip) > 0) {
296 my @ifs= &get_interfaces();
297 if($ip eq "0.0.0.0") {
298 $result = "all";
299 } else {
300 foreach (@ifs) {
301 my $if=$_;
302 if(get_ip($if) eq $ip) {
303 $result = $if;
304 }
305 }
306 }
307 }
308 return $result;
309 }
311 #=== FUNCTION ================================================================
312 # NAME: get_interfaces
313 # PARAMETERS: none
314 # RETURNS: (list of interfaces)
315 # DESCRIPTION: Uses proc fs (/proc/net/dev) to get list of interfaces.
316 #===============================================================================
317 sub get_interfaces {
318 my @result;
319 my $PROC_NET_DEV= ('/proc/net/dev');
321 open(PROC_NET_DEV, "<$PROC_NET_DEV")
322 or die "Could not open $PROC_NET_DEV";
324 my @ifs = <PROC_NET_DEV>;
326 close(PROC_NET_DEV);
328 # Eat first two line
329 shift @ifs;
330 shift @ifs;
332 chomp @ifs;
333 foreach my $line(@ifs) {
334 my $if= (split /:/, $line)[0];
335 $if =~ s/^\s+//;
336 push @result, $if;
337 }
339 return @result;
340 }
342 #=== FUNCTION ================================================================
343 # NAME: get_mac
344 # PARAMETERS: interface name (i.e. eth0)
345 # RETURNS: (mac address)
346 # DESCRIPTION: Uses ioctl to get mac address directly from system.
347 #===============================================================================
348 sub get_mac {
349 my $ifreq= shift;
350 my $result;
351 if ($ifreq && length($ifreq) > 0) {
352 if($ifreq eq "all") {
353 $result = "00:00:00:00:00:00";
354 } else {
355 my $SIOCGIFHWADDR= 0x8927; # man 2 ioctl_list
357 # A configured MAC Address should always override a guessed value
358 if ($server_mac_address and length($server_mac_address) > 0) {
359 $result= $server_mac_address;
360 }
362 socket SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('ip')
363 or die "socket: $!";
365 if(ioctl SOCKET, $SIOCGIFHWADDR, $ifreq) {
366 my ($if, $mac)= unpack 'h36 H12', $ifreq;
368 if (length($mac) > 0) {
369 $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])$/;
370 $mac= sprintf("%s:%s:%s:%s:%s:%s", $1, $2, $3, $4, $5, $6);
371 $result = $mac;
372 }
373 }
374 }
375 }
376 return $result;
377 }
380 #=== FUNCTION ================================================================
381 # NAME: register_at_bus
382 # PARAMETERS: nothing
383 # RETURNS: nothing
384 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
385 #===============================================================================
386 sub register_at_bus {
388 # add bus to known_server_db
389 my $res = $main::known_server_db->add_dbentry( {table=>'known_server',
390 primkey=>'hostname',
391 hostname=>$bus_address,
392 status=>'bus',
393 hostkey=>$bus_key,
394 timestamp=>&get_time,
395 } );
396 my $msg_hash = &create_xml_hash("here_i_am", $server_address, $bus_address);
397 my $msg = &create_xml_string($msg_hash);
399 &main::send_msg_to_target($msg, $bus_address, $bus_key, "here_i_am");
400 return $msg;
401 }
404 sub import_events {
405 if (not -e $event_dir) {
406 &main::daemon_log("ERROR: cannot find directory or directory is not readable: $event_dir", 1);
407 }
408 opendir (DIR, $event_dir) or die "ERROR while loading gosa-si-events from directory $event_dir : $!\n";
410 while (defined (my $event = readdir (DIR))) {
411 if( $event eq "." || $event eq ".." ) { next; }
412 if( $event eq "gosaTriggered.pm" ) { next; } # only GOsa specific events
414 eval{ require $event; };
415 if( $@ ) {
416 &main::daemon_log("import of event module '$event' failed", 1);
417 &main::daemon_log("$@", 8);
418 next;
419 }
421 $event =~ /(\S*?).pm$/;
422 my $event_module = $1;
423 my $events_l = eval( $1."::get_events()") ;
424 foreach my $event_name (@{$events_l}) {
425 $event_hash->{$event_name} = $event_module;
426 }
427 my $events_string = join( ", ", @{$events_l});
428 &main::daemon_log("INFO: SIPackages imported events $events_string", 5);
429 }
430 }
433 #=== FUNCTION ================================================================
434 # NAME: process_incoming_msg
435 # PARAMETERS: crypted_msg - string - incoming crypted message
436 # RETURNS: nothing
437 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
438 #===============================================================================
439 sub process_incoming_msg {
440 my ($msg, $msg_hash, $session_id) = @_ ;
441 my $error = 0;
442 my $host_name;
443 my $host_key;
444 my @out_msg_l = ();
446 # process incoming msg
447 my $header = @{$msg_hash->{header}}[0];
448 my @target_l = @{$msg_hash->{target}};
450 &main::daemon_log("SIPackages: msg to process: $header", 3);
451 &main::daemon_log("$msg", 8);
453 if( 0 == length @target_l){
454 &main::daemon_log("ERROR: no target specified for msg $header", 1);
455 $error++;
456 }
458 if( 1 == length @target_l) {
459 my $target = $target_l[0];
460 if(&server_matches($target)) {
463 if ($header eq 'new_key') {
464 @out_msg_l = &new_key($msg_hash)
465 } elsif ($header eq 'here_i_am') {
466 @out_msg_l = &here_i_am($msg_hash)
467 } else {
468 if( exists $event_hash->{$header} ) {
469 # a event exists with the header as name
470 &main::daemon_log("found event '$header' at event-module '".$event_hash->{$header}."'", 5);
471 no strict 'refs';
472 @out_msg_l = &{$event_hash->{$header}."::$header"}($msg, $msg_hash, $session_id);
473 }
474 }
476 # if delivery not possible raise error and return
477 if( not @out_msg_l ) {
478 &main::daemon_log("WARNING: SIPackages got not answer from event handler '$header'", 3);
479 } elsif( 0 == @out_msg_l) {
480 &main::daemon_log("ERROR: SIPackages: no event handler or core function defined for '$header'", 1);
481 }
482 }
483 else {
484 &main::daemon_log("msg is not for gosa-si-server '$server_address', deliver it to target '$target'", 5);
485 push(@out_msg_l, $msg);
486 }
487 }
489 return \@out_msg_l;
490 }
493 #=== FUNCTION ================================================================
494 # NAME: got_ping
495 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
496 # RETURNS: nothing
497 # DESCRIPTION: process this incoming message
498 #===============================================================================
499 #sub got_ping {
500 # my ($msg_hash) = @_;
501 #
502 # my $source = @{$msg_hash->{source}}[0];
503 # my $target = @{$msg_hash->{target}}[0];
504 # my $header = @{$msg_hash->{header}}[0];
505 # my $session_id = @{$msg_hash->{'session_id'}}[0];
506 # my $act_time = &get_time;
507 # my @out_msg_l;
508 # my $out_msg;
509 #
510 # # check known_clients_db
511 # my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source'";
512 # my $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
513 # if( 1 == keys %{$query_res} ) {
514 # my $sql_statement= "UPDATE known_clients ".
515 # "SET status='$header', timestamp='$act_time' ".
516 # "WHERE hostname='$source'";
517 # my $res = $main::known_clients_db->update_dbentry( $sql_statement );
518 # }
519 #
520 # # check known_server_db
521 # $sql_statement = "SELECT * FROM known_server WHERE hostname='$source'";
522 # $query_res = $main::known_server_db->select_dbentry( $sql_statement );
523 # if( 1 == keys %{$query_res} ) {
524 # my $sql_statement= "UPDATE known_server ".
525 # "SET status='$header', timestamp='$act_time' ".
526 # "WHERE hostname='$source'";
527 # my $res = $main::known_server_db->update_dbentry( $sql_statement );
528 # }
529 #
530 # # create out_msg
531 # my $out_hash = &create_xml_hash($header, $source, "GOSA");
532 # &add_content2xml_hash($out_hash, "session_id", $session_id);
533 # $out_msg = &create_xml_string($out_hash);
534 # push(@out_msg_l, $out_msg);
535 #
536 # return @out_msg_l;
537 #}
539 #=== FUNCTION ================================================================
540 # NAME: new_passwd
541 # PARAMETERS: msg_hash - ref - hash from function create_xml_hash
542 # RETURNS: nothing
543 # DESCRIPTION: process this incoming message
544 #===============================================================================
545 sub new_key {
546 my ($msg_hash) = @_;
547 my @out_msg_l;
549 my $header = @{$msg_hash->{header}}[0];
550 my $source_name = @{$msg_hash->{source}}[0];
551 my $source_key = @{$msg_hash->{new_key}}[0];
552 my $query_res;
554 # check known_clients_db
555 my $sql_statement = "SELECT * FROM known_clients WHERE hostname='$source_name'";
556 $query_res = $main::known_clients_db->select_dbentry( $sql_statement );
557 if( 1 == keys %{$query_res} ) {
558 my $act_time = &get_time;
559 my $sql_statement= "UPDATE known_clients ".
560 "SET hostkey='$source_key', timestamp='$act_time' ".
561 "WHERE hostname='$source_name'";
562 my $res = $main::known_clients_db->update_dbentry( $sql_statement );
563 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
564 my $out_msg = &create_xml_string($hash);
565 push(@out_msg_l, $out_msg);
566 }
568 # only do if host still not found
569 if( 0 == @out_msg_l ) {
570 # check known_server_db
571 $sql_statement = "SELECT * FROM known_server WHERE hostname='$source_name'";
572 $query_res = $main::known_server_db->select_dbentry( $sql_statement );
573 if( 1 == keys %{$query_res} ) {
574 my $act_time = &get_time;
575 my $sql_statement= "UPDATE known_server ".
576 "SET hostkey='$source_key', timestamp='$act_time' ".
577 "WHERE hostname='$source_name'";
578 my $res = $main::known_server_db->update_dbentry( $sql_statement );
580 my $hash = &create_xml_hash("confirm_new_key", $server_address, $source_name);
581 my $out_msg = &create_xml_string($hash);
582 push(@out_msg_l, $out_msg);
583 }
584 }
586 return @out_msg_l;
587 }
590 #=== FUNCTION ================================================================
591 # NAME: here_i_am
592 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
593 # RETURNS: nothing
594 # DESCRIPTION: process this incoming message
595 #===============================================================================
596 sub here_i_am {
597 my ($msg_hash) = @_;
598 my @out_msg_l;
599 my $out_hash;
601 my $source = @{$msg_hash->{source}}[0];
602 my $mac_address = @{$msg_hash->{mac_address}}[0];
603 my $gotoHardwareChecksum = @{$msg_hash->{gotoHardwareChecksum}}[0];
605 # number of known clients
606 my $nu_clients= $main::known_clients_db->count_dbentries('known_clients');
608 # check wether client address or mac address is already known
609 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$source'";
610 my $db_res= $main::known_clients_db->select_dbentry( $sql_statement );
612 if ( 1 == keys %{$db_res} ) {
613 &main::daemon_log("WARNING: $source is already known as a client", 1);
614 &main::daemon_log("WARNING: values for $source are being overwritten", 1);
615 $nu_clients --;
616 }
618 # number of actual activ clients
619 my $act_nu_clients = $nu_clients;
621 &main::daemon_log("number of actual activ clients: $act_nu_clients", 5);
622 &main::daemon_log("number of maximal allowed clients: $max_clients", 5);
624 if($max_clients <= $act_nu_clients) {
625 my $out_hash = &create_xml_hash("denied", $server_address, $source);
626 &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
627 my $passwd = @{$msg_hash->{new_passwd}}[0];
628 &send_msg_hash2address($out_hash, $source, $passwd);
629 return;
630 }
632 # new client accepted
633 my $new_passwd = @{$msg_hash->{new_passwd}}[0];
635 # create entry in known_clients
636 my $events = @{$msg_hash->{events}}[0];
639 # add entry to known_clients_db
640 my $act_timestamp = &get_time;
641 my $res = $main::known_clients_db->add_dbentry( {table=>'known_clients',
642 primkey=>'hostname',
643 hostname=>$source,
644 events=>$events,
645 macaddress=>$mac_address,
646 status=>'registered',
647 hostkey=>$new_passwd,
648 timestamp=>$act_timestamp,
649 } );
651 if ($res != 0) {
652 &main::daemon_log("ERROR: cannot add entry to known_clients: $res");
653 return;
654 }
656 # return acknowledgement to client
657 $out_hash = &create_xml_hash("registered", $server_address, $source);
658 my $register_out = &create_xml_string($out_hash);
659 push(@out_msg_l, $register_out);
661 # notify registered client to bus
662 if( $bus_activ eq "on") {
663 # fetch actual bus key
664 my $sql_statement= "SELECT * FROM known_server WHERE status='bus'";
665 my $query_res = $main::known_server_db->select_dbentry( $sql_statement );
666 my $hostkey = $query_res->{1}->{'hostkey'};
668 # send update msg to bus
669 $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
670 &add_content2xml_hash($out_hash, "macaddress", $mac_address);
671 &add_content2xml_hash($out_hash, "timestamp", $act_timestamp);
672 my $new_client_out = &create_xml_string($out_hash);
673 push(@out_msg_l, $new_client_out);
674 &main::daemon_log("send bus msg that client '$source' has registerd at server '$server_address'", 3);
675 }
677 # give the new client his ldap config
678 my $new_ldap_config_out = &new_ldap_config($source);
679 if( $new_ldap_config_out ) {
680 push(@out_msg_l, $new_ldap_config_out);
681 }
683 my $hardware_config_out = &hardware_config($source, $gotoHardwareChecksum);
684 if( $hardware_config_out ) {
685 push(@out_msg_l, $hardware_config_out);
686 }
688 return @out_msg_l;
689 }
692 #=== FUNCTION ================================================================
693 # NAME: who_has
694 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
695 # RETURNS: nothing
696 # DESCRIPTION: process this incoming message
697 #===============================================================================
698 sub who_has {
699 my ($msg_hash) = @_ ;
700 my @out_msg_l;
702 # what is your search pattern
703 my $search_pattern = @{$msg_hash->{who_has}}[0];
704 my $search_element = @{$msg_hash->{$search_pattern}}[0];
705 &main::daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
707 # scanning known_clients for search_pattern
708 my @host_addresses = keys %$main::known_clients;
709 my $known_clients_entries = length @host_addresses;
710 my $host_address;
711 foreach my $host (@host_addresses) {
712 my $client_element = $main::known_clients->{$host}->{$search_pattern};
713 if ($search_element eq $client_element) {
714 $host_address = $host;
715 last;
716 }
717 }
719 # search was successful
720 if (defined $host_address) {
721 my $source = @{$msg_hash->{source}}[0];
722 my $out_hash = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
723 &add_content2xml_hash($out_hash, "mac_address", $search_element);
724 my $out_msg = &create_xml_string($out_hash);
725 push(@out_msg_l, $out_msg);
726 }
727 return @out_msg_l;
728 }
731 sub who_has_i_do {
732 my ($msg_hash) = @_ ;
733 my $header = @{$msg_hash->{header}}[0];
734 my $source = @{$msg_hash->{source}}[0];
735 my $search_param = @{$msg_hash->{$header}}[0];
736 my $search_value = @{$msg_hash->{$search_param}}[0];
737 print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
738 }
740 #=== FUNCTION ================================================================
741 # NAME: new_ldap_config
742 # PARAMETERS: address - string - ip address and port of a host
743 # RETURNS: nothing
744 # DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
745 #===============================================================================
746 sub new_ldap_config {
747 my ($address) = @_ ;
749 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
750 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
752 # check hit
753 my $hit_counter = keys %{$res};
754 if( not $hit_counter == 1 ) {
755 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
756 }
758 my $macaddress = $res->{1}->{macaddress};
759 my $hostkey = $res->{1}->{hostkey};
761 if (not defined $macaddress) {
762 &main::daemon_log("ERROR: no mac address found for client $address", 1);
763 return;
764 }
766 # Build LDAP connection
767 my $ldap = Net::LDAP->new($ldap_uri);
768 if( not defined $ldap ) {
769 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
770 return;
771 }
774 # Bind to a directory with dn and password
775 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
777 # Perform search
778 $mesg = $ldap->search( base => $ldap_base,
779 scope => 'sub',
780 attrs => ['dn', 'gotoLdapServer', 'gosaUnitTag'],
781 filter => "(&(objectClass=GOhard)(macaddress=$macaddress))");
782 #$mesg->code && die $mesg->error;
783 if($mesg->code) {
784 &main::daemon_log($mesg->error, 1);
785 return;
786 }
788 # Sanity check
789 if ($mesg->count != 1) {
790 &main::daemon_log("WARNING: client mac address $macaddress not found/not unique in ldap search", 1);
791 &main::daemon_log("\tbase: $ldap_base", 1);
792 &main::daemon_log("\tscope: sub", 1);
793 &main::daemon_log("\tattrs: dn, gotoLdapServer", 1);
794 &main::daemon_log("\tfilter: (&(objectClass=GOhard)(macaddress=$macaddress))", 1);
795 return;
796 }
798 my $entry= $mesg->entry(0);
799 my $dn= $entry->dn;
800 my @servers= $entry->get_value("gotoLdapServer");
801 my $unit_tag= $entry->get_value("gosaUnitTag");
802 my @ldap_uris;
803 my $server;
804 my $base;
806 # Do we need to look at an object class?
807 if (length(@servers) < 1){
808 $mesg = $ldap->search( base => $ldap_base,
809 scope => 'sub',
810 attrs => ['dn', 'gotoLdapServer'],
811 filter => "(&(objectClass=gosaGroupOfNames)(member=$dn))");
812 #$mesg->code && die $mesg->error;
813 if($mesg->code) {
814 &main::daemon_log($mesg->error, 1);
815 return;
816 }
818 # Sanity check
819 if ($mesg->count != 1) {
820 &main::daemon_log("WARNING: no LDAP information found for client mac $macaddress", 1);
821 return;
822 }
824 $entry= $mesg->entry(0);
825 $dn= $entry->dn;
826 @servers= $entry->get_value("gotoLdapServer");
827 }
829 @servers= sort (@servers);
831 foreach $server (@servers){
832 # Conversation for backward compatibility
833 if ($server !=~ /^ldap[^:]+:\/\// ) {
834 if ($server =~ /^([^:]+):(.*)$/ ) {
835 $server= "1:dummy:ldap://$1/$2";
836 }
837 }
839 $base= $server;
840 $server =~ s%^[^:]+:[^:]+:(ldap.*://[^/]+)/.*$%$1%;
841 $base =~ s%^[^:]+:[^:]+:ldap.*://[^/]+/(.*)$%$1%;
842 push (@ldap_uris, $server);
843 }
845 # Assemble data package
846 my %data = ( 'ldap_uri' => \@ldap_uris, 'ldap_base' => $base,
847 'ldap_cfg' => \@ldap_cfg, 'pam_cfg' => \@pam_cfg,'nss_cfg' => \@nss_cfg );
849 # Need to append GOto settings?
850 if (defined $goto_admin and defined $goto_secret){
851 $data{'goto_admin'}= $goto_admin;
852 $data{'goto_secret'}= $goto_secret;
853 }
855 # Append unit tag if needed
856 if (defined $unit_tag){
858 # Find admin base and department name
859 $mesg = $ldap->search( base => $ldap_base,
860 scope => 'sub',
861 attrs => ['dn', 'ou', 'FAIclass'],
862 filter => "(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$unit_tag))");
863 #$mesg->code && die $mesg->error;
864 if($mesg->code) {
865 &main::daemon_log($mesg->error, 1);
866 return;
867 }
869 # Sanity check
870 if ($mesg->count != 1) {
871 &main::daemon_log("WARNING: cannot find administrative unit for client with tag $unit_tag", 1);
872 return;
873 }
875 $entry= $mesg->entry(0);
876 $data{'admin_base'}= $entry->dn;
877 $data{'department'}= $entry->get_value("ou");
879 # Append unit Tag
880 $data{'unit_tag'}= $unit_tag;
881 }
883 # Fill release if available
884 my $FAIclass= $entry->get_value("FAIclass");
885 if (defined $FAIclass && $FAIclass =~ /^.* :([A-Za-z0-9\/.]+).*$/) {
886 $data{'release'}= $1;
887 }
890 # Unbind
891 $mesg = $ldap->unbind;
893 # Send information
894 return &build_msg("new_ldap_config", $server_address, $address, \%data);
895 }
897 #sub process_detected_hardware {
898 # my $msg_hash = shift;
899 # my $address = $msg_hash->{source}[0];
900 # my $gotoHardwareChecksum= $msg_hash->{detected_hardware}[0]->{gotoHardwareChecksum};
901 #
902 # my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
903 # my $res = $main::known_clients_db->select_dbentry( $sql_statement );
904 #
905 # # check hit
906 # my $hit_counter = keys %{$res};
907 # if( not $hit_counter == 1 ) {
908 # &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
909 # return;
910 # }
911 #
912 # my $macaddress = $res->{1}->{macaddress};
913 # my $hostkey = $res->{1}->{hostkey};
914 #
915 # if (not defined $macaddress) {
916 # &main::daemon_log("ERROR: no mac address found for client $address", 1);
917 # return;
918 # }
919 # # Build LDAP connection
920 # my $ldap = Net::LDAP->new($ldap_uri);
921 # if( not defined $ldap ) {
922 # &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
923 # return;
924 # }
925 #
926 # # Bind to a directory with dn and password
927 # my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
928 #
929 # # Perform search
930 # $mesg = $ldap->search(
931 # base => $ldap_base,
932 # scope => 'sub',
933 # filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
934 # );
935 #
936 # # We need to create a base entry first (if not done from ArpHandler)
937 # if($mesg->count == 0) {
938 # &main::daemon_log("Need to create a new LDAP Entry for client $address", 1);
939 # my $resolver=Net::DNS::Resolver->new;
940 # my $ipaddress= $1 if $address =~ /^([0-9\.]*?):.*$/;
941 # my $dnsresult= $resolver->search($ipaddress);
942 # my $dnsname= (defined($dnsresult))?$dnsresult->{answer}[0]->{ptrdname}:$ipaddress;
943 # my $cn = (($dnsname =~ /^(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/) ? $dnsname : sprintf "%s", $dnsname =~ /([^\.]+)\.?/);
944 # my $dn = "cn=$cn,ou=incoming,$ldap_base";
945 # &main::daemon_log("Creating entry for $dn",6);
946 # my $entry= Net::LDAP::Entry->new( $dn );
947 # $entry->dn($dn);
948 # $entry->add("objectClass" => "goHard");
949 # $entry->add("cn" => $cn);
950 # $entry->add("macAddress" => $macaddress);
951 # $entry->add("gotomode" => "locked");
952 # $entry->add("gotoSysStatus" => "new-system");
953 # $entry->add("ipHostNumber" => $ipaddress);
954 # if(defined($gosa_unit_tag) && length($gosa_unit_tag) > 0) {
955 # $entry->add("objectClass" => "gosaAdministrativeUnit");
956 # $entry->add("gosaUnitTag" => $gosa_unit_tag);
957 # }
958 # my $res=$entry->update($ldap);
959 # if(defined($res->{'errorMessage'}) &&
960 # length($res->{'errorMessage'}) >0) {
961 # &main::daemon_log("There was a problem adding the entries to LDAP:", 1);
962 # &main::daemon_log($res->{'errorMessage'}, 1);
963 # return;
964 # } else {
965 # # Fill $mesg again
966 # $mesg = $ldap->search(
967 # base => $ldap_base,
968 # scope => 'sub',
969 # filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
970 # );
971 # }
972 # }
973 #
974 # if($mesg->count == 1) {
975 # my $entry= $mesg->entry(0);
976 # $entry->changetype("modify");
977 # foreach my $attribute (
978 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
979 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
980 # "gotoXDriver", "gotoXVsync", "gotoXMonitor", "gotoHardwareChecksum") {
981 # if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
982 # length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
983 # if(defined($entry->get_value($attribute))) {
984 # $entry->delete($attribute);
985 # }
986 # &main::daemon_log("Adding attribute $attribute with value ".$msg_hash->{detected_hardware}[0]->{$attribute},1);
987 # $entry->add($attribute => $msg_hash->{detected_hardware}[0]->{$attribute});
988 # }
989 # }
990 # foreach my $attribute (
991 # "gotoModules", "ghScsiDev", "ghIdeDev") {
992 # if(defined($msg_hash->{detected_hardware}[0]->{$attribute}) &&
993 # length($msg_hash->{detected_hardware}[0]->{$attribute}) >0 ) {
994 # if(defined($entry->get_value($attribute))) {
995 # $entry->delete($attribute);
996 # }
997 # foreach my $array_entry (@{$msg_hash->{detected_hardware}[0]->{$attribute}}) {
998 # $entry->add($attribute => $array_entry);
999 # }
1000 # }
1001 # }
1002 #
1003 # my $res=$entry->update($ldap);
1004 # if(defined($res->{'errorMessage'}) &&
1005 # length($res->{'errorMessage'}) >0) {
1006 # &main::daemon_log("There was a problem adding the entries to LDAP:", 1);
1007 # &main::daemon_log($res->{'errorMessage'}, 1);
1008 # } else {
1009 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1010 # }
1011 #
1012 # }
1013 # return;
1014 #}
1015 #=== FUNCTION ================================================================
1016 # NAME: hardware_config
1017 # PARAMETERS: address - string - ip address and port of a host
1018 # RETURNS:
1019 # DESCRIPTION:
1020 #===============================================================================
1021 sub hardware_config {
1022 my ($address, $gotoHardwareChecksum) = @_ ;
1024 my $sql_statement= "SELECT * FROM known_clients WHERE hostname='$address'";
1025 my $res = $main::known_clients_db->select_dbentry( $sql_statement );
1027 # check hit
1028 my $hit_counter = keys %{$res};
1029 if( not $hit_counter == 1 ) {
1030 &main::daemon_log("ERROR: more or no hit found in known_clients_db by query by '$address'", 1);
1031 }
1033 my $macaddress = $res->{1}->{macaddress};
1034 my $hostkey = $res->{1}->{hostkey};
1036 if (not defined $macaddress) {
1037 &main::daemon_log("ERROR: no mac address found for client $address", 1);
1038 return;
1039 }
1041 # Build LDAP connection
1042 my $ldap = Net::LDAP->new($ldap_uri);
1043 if( not defined $ldap ) {
1044 &main::daemon_log("ERROR: cannot connect to ldap: $ldap_uri", 1);
1045 return;
1046 }
1048 # Bind to a directory with dn and password
1049 my $mesg= $ldap->bind($ldap_admin_dn, password => $ldap_admin_password);
1051 # Perform search
1052 $mesg = $ldap->search(
1053 base => $ldap_base,
1054 scope => 'sub',
1055 filter => "(&(objectClass=GOhard)(|(macAddress=$macaddress)(dhcpHWaddress=ethernet $macaddress)))"
1056 );
1058 if($mesg->count() == 0) {
1059 &main::daemon_log("Host was not found in LDAP!", 1);
1060 } else {
1061 my $entry= $mesg->entry(0);
1062 my $dn= $entry->dn;
1063 if(defined($entry->get_value("gotoHardwareChecksum"))) {
1064 if(! $entry->get_value("gotoHardwareChecksum") eq $gotoHardwareChecksum) {
1065 $entry->replace(gotoHardwareChecksum => $gotoHardwareChecksum);
1066 if($entry->update($ldap)) {
1067 &main::daemon_log("Hardware changed! Detection triggered.", 4);
1068 }
1069 } else {
1070 # Nothing to do
1071 return;
1072 }
1073 }
1074 }
1075 # need to fill it to LDAP
1076 #$entry->add(gotoHardwareChecksum => $gotoHardwareChecksum);
1077 #if($entry->update($ldap)) {
1078 # &main::daemon_log("gotoHardwareChecksum $gotoHardwareChecksum was added to LDAP", 4);
1079 #}
1081 ## Look if there another host with this checksum to use the hardware config
1082 #$mesg = $ldap->search(
1083 # base => $ldap_base,
1084 # scope => 'sub',
1085 # filter => "(&(objectClass=GOhard)(gotoHardwareChecksum=$gotoHardwareChecksum))"
1086 #);
1088 #if($mesg->count>1) {
1089 # my $clone_entry= $mesg->entry(0);
1090 # $entry->changetype("modify");
1091 # foreach my $attribute (
1092 # "gotoSndModule", "ghNetNic", "gotoXResolution", "ghSoundAdapter", "ghCpuType", "gotoXkbModel",
1093 # "ghGfxAdapter", "gotoXMousePort", "ghMemSize", "gotoXMouseType", "ghUsbSupport", "gotoXHsync",
1094 # "gotoXDriver", "gotoXVsync", "gotoXMonitor") {
1095 # my $value= $clone_entry->get_value($attribute);
1096 # if(defined($value)) {
1097 # if(defined($entry->get_value($attribute))) {
1098 # $entry->delete($attribute);
1099 # }
1100 # &main::daemon_log("Adding attribute $attribute with value $value",1);
1101 # $entry->add($attribute => $value);
1102 # }
1103 # }
1104 # foreach my $attribute (
1105 # "gotoModules", "ghScsiDev", "ghIdeDev") {
1106 # my $array= $clone_entry->get_value($attribute, 'as_ref' => 1);
1107 # if(defined($array)) {
1108 # if(defined($entry->get_value($attribute))) {
1109 # $entry->delete($attribute);
1110 # }
1111 # foreach my $array_entry (@{$array}) {
1112 # $entry->add($attribute => $array_entry);
1113 # }
1114 # }
1116 # }
1117 # if($entry->update($ldap)) {
1118 # &main::daemon_log("Added Hardware configuration to LDAP", 4);
1119 # }
1121 #}
1124 # Assemble data package
1125 my %data = ();
1127 # Need to append GOto settings?
1128 if (defined $goto_admin and defined $goto_secret){
1129 $data{'goto_admin'}= $goto_admin;
1130 $data{'goto_secret'}= $goto_secret;
1131 }
1133 # Unbind
1134 $mesg = $ldap->unbind;
1136 &main::daemon_log("Send detect_hardware message to $address", 4);
1138 # Send information
1139 return &build_msg("detect_hardware", $server_address, $address, \%data);
1140 }
1142 sub server_matches {
1143 my $target = shift;
1144 my $target_ip = sprintf("%s", $target =~ /^([0-9\.]*?):.*$/);
1145 my $result = 0;
1147 if($server_ip eq $target_ip) {
1148 $result= 1;
1149 } elsif ($server_ip eq "0.0.0.0") {
1150 if ($target_ip eq "127.0.0.1") {
1151 $result= 1;
1152 } else {
1153 my $PROC_NET_ROUTE= ('/proc/net/route');
1155 open(PROC_NET_ROUTE, "<$PROC_NET_ROUTE")
1156 or die "Could not open $PROC_NET_ROUTE";
1158 my @ifs = <PROC_NET_ROUTE>;
1160 close(PROC_NET_ROUTE);
1162 # Eat header line
1163 shift @ifs;
1164 chomp @ifs;
1165 foreach my $line(@ifs) {
1166 my ($Iface,$Destination,$Gateway,$Flags,$RefCnt,$Use,$Metric,$Mask,$MTU,$Window,$IRTT)=split(/\s/, $line);
1167 my $destination;
1168 my $mask;
1169 my ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Destination);
1170 $destination= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1171 ($d,$c,$b,$a)=unpack('a2 a2 a2 a2', $Mask);
1172 $mask= sprintf("%d.%d.%d.%d", hex($a), hex($b), hex($c), hex($d));
1173 if(new NetAddr::IP($target_ip)->within(new NetAddr::IP($destination, $mask))) {
1174 # destination matches route, save mac and exit
1175 $result= 1;
1176 last;
1177 }
1178 }
1179 }
1180 } else {
1181 &main::daemon_log("Target ip $target_ip does not match Server ip $server_ip",1);
1182 }
1184 return $result;
1185 }
1188 ##=== FUNCTION ================================================================
1189 ## NAME: execute_actions
1190 ## PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1191 ## RETURNS: nothing
1192 ## DESCRIPTION: invokes the script specified in msg_hash which is located under
1193 ## /etc/gosad/actions
1194 ##===============================================================================
1195 #sub execute_actions {
1196 # my ($msg_hash) = @_ ;
1197 # my $configdir= '/etc/gosad/actions/';
1198 # my $result;
1199 #
1200 # my $header = @{$msg_hash->{header}}[0];
1201 # my $source = @{$msg_hash->{source}}[0];
1202 # my $target = @{$msg_hash->{target}}[0];
1203 #
1204 # if((not defined $source)
1205 # && (not defined $target)
1206 # && (not defined $header)) {
1207 # &main::daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1208 # } else {
1209 # my $parameters="";
1210 # my @params = @{$msg_hash->{$header}};
1211 # my $params = join(", ", @params);
1212 # &main::daemon_log("execute_actions: got parameters: $params", 5);
1213 #
1214 # if (@params) {
1215 # foreach my $param (@params) {
1216 # my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1217 # &main::daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1218 # $parameters.= " ".$param_value;
1219 # }
1220 # }
1221 #
1222 # my $cmd= $configdir.$header."$parameters";
1223 # &main::daemon_log("execute_actions: executing cmd: $cmd", 7);
1224 # $result= "";
1225 # open(PIPE, "$cmd 2>&1 |");
1226 # while(<PIPE>) {
1227 # $result.=$_;
1228 # }
1229 # close(PIPE);
1230 # }
1231 #
1232 # # process the event result
1233 #
1234 #
1235 # return;
1236 #}
1237 #
1239 1;