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