096637417cbe0fdcff0b7a6889a0a89dd060932e
1 #!/usr/bin/perl
2 #===============================================================================
3 #
4 # FILE: gosa-sd
5 #
6 # USAGE: ./gosa-sd
7 #
8 # DESCRIPTION:
9 #
10 # OPTIONS: ---
11 # REQUIREMENTS: libconfig-inifiles-perl libcrypt-rijndael-perl libxml-simple-perl libipc-shareable-perl libdata-dumper-simple-perl
12 # BUGS: ---
13 # NOTES:
14 # AUTHOR: (Andreas Rettenberger), <rettenberger@gonicus.de>
15 # COMPANY:
16 # VERSION: 1.0
17 # CREATED: 12.09.2007 08:54:41 CEST
18 # REVISION: ---
19 #===============================================================================
22 use strict;
23 use warnings;
24 use Getopt::Long;
25 use Config::IniFiles;
26 use POSIX;
27 use Time::HiRes qw( gettimeofday );
29 use Fcntl;
30 use IO::Socket::INET;
31 use Crypt::Rijndael;
32 use MIME::Base64;
33 use Digest::MD5 qw(md5 md5_hex md5_base64);
34 use XML::Simple;
35 use Data::Dumper;
36 use Sys::Syslog qw( :DEFAULT setlogsock);
37 use Cwd;
38 use File::Spec;
39 use IPC::Shareable qw( :lock);
40 IPC::Shareable->clean_up_all;
42 use lib "/etc/gosad/modules";
43 my $modules_path = "/etc/gosad/modules";
45 my ($cfg_file, %cfg_defaults, $foreground, $verbose, $ping_timeout, $no_bus);
46 my ($bus, $msg_to_bus, $bus_cipher);
47 my ($server, $server_mac_address, $server_events);
48 my ($gosa_server);
49 my ($known_daemons, $shmda, $known_clients, $shmcl, $known_modules);
50 my ($max_clients);
51 my ($pid_file, $procid, $pid, $log_file);
52 my (%free_child, %busy_child, $child_max, $child_min, %child_alive_time, $child_timeout);
53 my ($arp_activ, $arp_fifo, $arp_fifo_path, $no_arp);
55 # variables declared in config file are always set to 'our'
56 our (%cfg_defaults, $log_file, $pid_file,
57 $bus_activ, $bus_passwd, $bus_ip, $bus_port,
58 $server_activ, $server_ip, $server_port, $server_passwd, $max_clients,
59 $arp_activ, $arp_fifo_path,
60 $gosa_activ, $gosa_passwd, $gosa_ip, $gosa_port, $gosa_timeout,
61 );
63 # additional variable which should be globaly accessable
64 our $xml;
65 our $server_address;
66 our $bus_address;
67 our $gosa_address;
69 # specifies the verbosity of the daemon_log
70 $verbose = 0 ;
72 # if foreground is not null, script will be not forked to background
73 $foreground = 0 ;
75 # specifies the timeout seconds while checking the online status of a registrating client
76 $ping_timeout = 5;
78 $no_bus = 0;
80 $no_arp = 0;
82 # holds all other gosa-sd as well as the gosa-sd-bus
83 our $known_daemons = {};
84 our $shmda = tie($known_daemons, 'IPC::Shareable', undef, {create => 1,
85 exclusive => 1,
86 mode => 0666,
87 destroy => 1,
88 });
89 # holds all registrated clients
90 our $known_clients = {};
91 our $shmcl = tie($known_clients, 'IPC::Shareable', undef, {create => 1,
92 exclusive => 1,
93 mode => 0666,
94 destroy => 1,
95 });
98 %cfg_defaults =
99 ("general" =>
100 {"log_file" => [\$log_file, "/var/run/".$0.".log"],
101 "pid_file" => [\$pid_file, "/var/run/".$0.".pid"],
102 "child_max" => [\$child_max, 10],
103 "child_min" => [\$child_min, 3],
104 "child_timeout" => [\$child_timeout, 180],
105 },
106 "bus" =>
107 {"bus_activ" => [\$bus_activ, "on"],
108 "bus_passwd" => [\$bus_passwd, ""],
109 "bus_ip" => [\$bus_ip, ""],
110 "bus_port" => [\$bus_port, "20080"],
111 },
112 "server" =>
113 {"server_activ" => [\$server_activ, "on"],
114 "server_ip" => [\$server_ip, ""],
115 "server_port" => [\$server_port, "20081"],
116 "server_passwd" => [\$server_passwd, ""],
117 "max_clients" => [\$max_clients, 100],
118 },
119 "arp" =>
120 {"arp_activ" => [\$arp_activ, "on"],
121 "arp_fifo_path" => [\$arp_fifo_path, "/var/run/gosa-si/arp-notify"],
122 },
123 "gosa" =>
124 {"gosa_activ" => [\$gosa_activ, "on"],
125 "gosa_ip" => [\$gosa_ip, ""],
126 "gosa_port" => [\$gosa_port, "20082"],
127 "gosa_passwd" => [\$gosa_passwd, "none"],
128 },
129 );
132 #=== FUNCTION ================================================================
133 # NAME: usage
134 # PARAMETERS: nothing
135 # RETURNS: nothing
136 # DESCRIPTION: print out usage text to STDERR
137 #===============================================================================
138 sub usage {
139 print STDERR << "EOF" ;
140 usage: $0 [-hvf] [-c config]
142 -h : this (help) message
143 -c <file> : config file
144 -f : foreground, process will not be forked to background
145 -v : be verbose (multiple to increase verbosity)
146 EOF
147 print "\n" ;
148 }
151 #=== FUNCTION ================================================================
152 # NAME: read_configfile
153 # PARAMETERS: cfg_file - string -
154 # RETURNS: nothing
155 # DESCRIPTION: read cfg_file and set variables
156 #===============================================================================
157 sub read_configfile {
158 my $cfg;
159 if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
160 if( -r $cfg_file ) {
161 $cfg = Config::IniFiles->new( -file => $cfg_file );
162 } else {
163 print STDERR "Couldn't read config file!";
164 }
165 } else {
166 $cfg = Config::IniFiles->new() ;
167 }
168 foreach my $section (keys %cfg_defaults) {
169 foreach my $param (keys %{$cfg_defaults{ $section }}) {
170 my $pinfo = $cfg_defaults{ $section }{ $param };
171 ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
172 }
173 }
174 }
177 #=== FUNCTION ================================================================
178 # NAME: logging
179 # PARAMETERS: level - string - default 'info'
180 # msg - string -
181 # facility - string - default 'LOG_DAEMON'
182 # RETURNS: nothing
183 # DESCRIPTION: function for logging
184 #===============================================================================
185 sub daemon_log {
186 # log into log_file
187 my( $msg, $level ) = @_;
188 if(not defined $msg) { return }
189 if(not defined $level) { $level = 1 }
190 if(defined $log_file){
191 open(LOG_HANDLE, ">>$log_file");
192 if(not defined open( LOG_HANDLE, ">>$log_file" )) {
193 print STDERR "cannot open $log_file: $!";
194 return }
195 chomp($msg);
196 if($level <= $verbose){
197 print LOG_HANDLE "$level $msg\n";
198 if(defined $foreground) { print $msg."\n" }
199 }
200 }
201 close( LOG_HANDLE );
202 #log into syslog
203 # my ($msg, $level, $facility) = @_;
204 # if(not defined $msg) {return}
205 # if(not defined $level) {$level = "info"}
206 # if(not defined $facility) {$facility = "LOG_DAEMON"}
207 # openlog($0, "pid,cons,", $facility);
208 # syslog($level, $msg);
209 # closelog;
210 # return;
211 }
214 #=== FUNCTION ================================================================
215 # NAME: check_cmdline_param
216 # PARAMETERS: nothing
217 # RETURNS: nothing
218 # DESCRIPTION: validates commandline parameter
219 #===============================================================================
220 sub check_cmdline_param () {
221 my $err_config;
222 my $err_counter = 0;
223 if( not defined( $cfg_file)) {
224 #$err_config = "please specify a config file";
225 #$err_counter += 1;
226 my $cwd = getcwd;
227 my $name = "/etc/gosa/gosa-si-server.conf";
228 $cfg_file = File::Spec->catfile( $cwd, $name );
229 print STDERR "no conf file specified\n try to use default: $cfg_file\n";
230 }
231 if( $err_counter > 0 ) {
232 &usage( "", 1 );
233 if( defined( $err_config)) { print STDERR "$err_config\n"}
234 print STDERR "\n";
235 exit( -1 );
236 }
237 }
240 #=== FUNCTION ================================================================
241 # NAME: check_pid
242 # PARAMETERS: nothing
243 # RETURNS: nothing
244 # DESCRIPTION: handels pid processing
245 #===============================================================================
246 sub check_pid {
247 $pid = -1;
248 # Check, if we are already running
249 if( open(LOCK_FILE, "<$pid_file") ) {
250 $pid = <LOCK_FILE>;
251 if( defined $pid ) {
252 chomp( $pid );
253 if( -f "/proc/$pid/stat" ) {
254 my($stat) = `cat /proc/$pid/stat` =~ m/$pid \((.+)\).*/;
255 if( $0 eq $stat ) {
256 close( LOCK_FILE );
257 exit -1;
258 }
259 }
260 }
261 close( LOCK_FILE );
262 unlink( $pid_file );
263 }
265 # create a syslog msg if it is not to possible to open PID file
266 if (not sysopen(LOCK_FILE, $pid_file, O_WRONLY|O_CREAT|O_EXCL, 0644)) {
267 my($msg) = "Couldn't obtain lockfile '$pid_file' ";
268 if (open(LOCK_FILE, '<', $pid_file)
269 && ($pid = <LOCK_FILE>))
270 {
271 chomp($pid);
272 $msg .= "(PID $pid)\n";
273 } else {
274 $msg .= "(unable to read PID)\n";
275 }
276 if( ! ($foreground) ) {
277 openlog( $0, "cons,pid", "daemon" );
278 syslog( "warning", $msg );
279 closelog();
280 }
281 else {
282 print( STDERR " $msg " );
283 }
284 exit( -1 );
285 }
286 }
289 #=== FUNCTION ================================================================
290 # NAME: get_ip_and_mac
291 # PARAMETERS: nothing
292 # RETURNS: (ip, mac)
293 # DESCRIPTION: executes /sbin/ifconfig and parses the output, the first occurence
294 # of a inet address is returned as well as the mac address in the line
295 # above the inet address
296 #===============================================================================
297 sub get_ip_and_mac {
298 my $ip = "0.0.0.0.0"; # Defualt-IP
299 my $mac = "00:00:00:00:00:00"; # Default-MAC
300 my @ifconfig = qx(/sbin/ifconfig);
301 foreach(@ifconfig) {
302 if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
303 $mac = "$1:$2:$3:$4:$5:$6";
304 next;
305 }
306 if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
307 $ip = "$1.$2.$3.$4";
308 last;
309 }
310 }
311 return ($ip, $mac);
312 }
316 #=== FUNCTION ================================================================
317 # NAME: import_modules
318 # PARAMETERS: module_path - string - abs. path to the directory the modules are stored
319 # RETURNS: nothing
320 # DESCRIPTION: each file in module_path which ends with '.pm' is imported by "require 'file';"
321 #===============================================================================
322 sub import_modules {
323 daemon_log(" ", 1);
325 if (not -e $modules_path) {
326 daemon_log("ERROR: cannot find directory or directory is not readable: $modules_path", 1);
327 }
329 opendir (DIR, $modules_path) or die "ERROR while loading modules from directory $modules_path : $!\n";
330 while (defined (my $file = readdir (DIR))) {
331 if (not $file =~ /(\S*?).pm$/) {
332 next;
333 }
334 eval { require $file; };
335 if ($@) {
336 daemon_log("ERROR: gosa-sd could not load module $file", 1);
337 daemon_log("$@", 5);
338 next;
339 }
340 my $mod_name = $1;
341 my $module_tag_hash = eval( $mod_name.'::get_module_tags()' );
342 $known_modules->{$mod_name} = $module_tag_hash;
344 daemon_log("load module $mod_name", 1);
345 }
347 # for debugging
348 #while ( my ($module, $tag_hash) = each(%$known_modules)) {
349 # print "\tmodule: $module"."\n";
350 # print "\ttags: ".join(", ", keys(%$tag_hash))."\n";
351 #}
352 close (DIR);
353 }
356 #=== FUNCTION ================================================================
357 # NAME: register_at_bus
358 # PARAMETERS: nothing
359 # RETURNS: nothing
360 # DESCRIPTION: creates an entry in known_daemons and send a 'here_i_am' msg to bus
361 #===============================================================================
362 sub register_at_bus {
364 # create known_daemons entry
365 &create_known_daemon($bus_address);
366 &add_content2known_daemons(hostname=>$bus_address, status=>"register_at_bus", passwd=>$bus_passwd);
367 daemon_log("register at bus: $bus_address", 1);
369 my $msg_hash = &create_xml_hash("here_i_am", "$server_ip:$server_port", $bus_address);
370 &send_msg_hash2address($msg_hash, $bus_address);
371 return;
372 }
375 #=== FUNCTION ================================================================
376 # NAME: sig_int_handler
377 # PARAMETERS: signal - string - signal arose from system
378 # RETURNS: noting
379 # DESCRIPTION: handels tasks to be done befor signal becomes active
380 #===============================================================================
381 sub sig_int_handler {
382 my ($signal) = @_;
383 if($server){
384 close($server);
385 daemon_log("daemon server closed", 1);
386 }
387 if( -p $arp_fifo_path ) {
388 close $arp_fifo ;
389 unlink($arp_fifo_path) ;
390 daemon_log("ARP_FIFO closed", 1) ;
391 }
393 if($gosa_server){
394 close($gosa_server);
395 daemon_log("gosa server closed", 1);
396 }
398 print STDERR "$signal\n";
400 exit(1);
401 }
402 $SIG{INT} = \&sig_int_handler;
405 #=== FUNCTION ================================================================
406 # NAME: activating_child
407 # PARAMETERS: msg - string - incoming message
408 # host - string - host from which the incomming message comes
409 # RETURNS: nothing
410 # DESCRIPTION: handels the distribution of incoming messages to working childs
411 #===============================================================================
412 sub activating_child {
413 my ($msg, $host, $client) = @_;
414 my $child = &get_processing_child();
415 my $pipe_wr = $$child{'pipe_wr'};
416 my $pipe_rd = $$child{'pipe_rd'};
417 $$child{client_ref} = $client;
418 daemon_log("activating: childpid:$$child{'pid'}", 5);
420 print $pipe_wr $msg.".".$host."\n";
422 # if (defined $client) {
423 # my $rbits = "";
424 # vec($rbits, fileno $client, 1) = 1;
425 #
426 # my ($rout);
427 # my $nf = select($rout=$rbits, undef, undef, $gosa_timeout);
428 # if($gosa_activ eq "on" && vec($rout, fileno $gosa_server, 1)) {
429 #
430 # }
431 # }
432 return;
433 }
436 #=== FUNCTION ================================================================
437 # NAME: get_processing_child
438 # PARAMETERS: nothing
439 # RETURNS: child - hash - holding the process id and the references to the pipe
440 # handles pipe_wr and pipe_rd
441 # DESCRIPTION: handels the forking, reactivating and keeping alive tasks
442 #===============================================================================
443 sub get_processing_child {
444 my $child;
445 # checking %busy_child{pipe_wr} if msg is 'done', then set child from busy to free
446 # while(my ($key, $val) = each(%busy_child)) {
447 # # test ob prozess noch existiert
448 # my $exitus_pid = waitpid($key, WNOHANG);
449 # if($exitus_pid != 0) {
450 # delete $busy_child{$key};
451 # print "prozess:$key wurde aus busy_child entfernt\n";
452 # next;
453 # }
454 #
455 # # check wether process sitll works
456 # my $fh = $$val{'pipe_rd'};
457 # $fh->blocking(0);
458 # my $child_answer;
459 # if(not $child_answer = <$fh>) { next }
460 # chomp($child_answer);
461 # if($child_answer eq "done") {
462 # delete $busy_child{$key};
463 # $free_child{$key} = $val;
464 # }
465 # }
467 while(my ($key, $val) = each(%free_child)) {
468 my $exitus_pid = waitpid($key, WNOHANG);
469 if($exitus_pid != 0) {
470 delete $free_child{$key};
471 }
472 daemon_log("free child:$key", 5);
473 }
474 # check @free_child and @busy_child
475 my $free_len = scalar(keys(%free_child));
476 my $busy_len = scalar(keys(%busy_child));
477 daemon_log("free children $free_len, busy children $busy_len", 5);
479 # if there is a free child, let the child work
480 if($free_len > 0){
481 my @keys = keys(%free_child);
482 $child = $free_child{$keys[0]};
483 if(defined $child) {
484 $busy_child{$$child{'pid'}} = $child ;
485 delete $free_child{$$child{'pid'}};
486 }
487 return $child;
488 }
490 # no free child, try to fork another one
491 if($free_len + $busy_len < $child_max) {
493 daemon_log("not enough children, create a new one", 5);
495 # New pipes for communication
496 my( $PARENT_wr, $PARENT_rd );
497 my( $CHILD_wr, $CHILD_rd );
498 pipe( $CHILD_rd, $PARENT_wr );
499 pipe( $PARENT_rd, $CHILD_wr );
500 $PARENT_wr->autoflush(1);
501 $CHILD_wr->autoflush(1);
503 ############
504 # fork child
505 ############
506 my $child_pid = fork();
508 #CHILD
509 if($child_pid == 0) {
510 # Close unused pipes
511 close( $CHILD_rd );
512 close( $CHILD_wr );
513 while( 1 ) {
514 my $rbits = "";
515 vec( $rbits, fileno $PARENT_rd , 1 ) = 1;
516 my $nf = select($rbits, undef, undef, $child_timeout);
517 if($nf < 0 ) {
518 die "select(): $!\n";
519 } elsif (! $nf) {
520 # if already child_min childs are alive, then leave loop
521 $free_len = scalar(keys(%free_child));
522 $busy_len = scalar(keys(%busy_child));
523 if($free_len + $busy_len >= $child_min) {
524 last;
525 } else {
526 redo;
527 }
528 }
530 # a job for a child arise
531 if ( vec $rbits, fileno $PARENT_rd, 1 ) {
532 # read everything from pipe
533 my $msg = "";
534 $PARENT_rd->blocking(0);
535 while(1) {
536 my $read = <$PARENT_rd>;
537 if(not defined $read) { last}
538 $msg .= $read;
539 }
541 ######################################
542 # forward msg to all imported modules
543 no strict "refs";
544 my $answer;
545 while( my ($module, $tag_hash) = each(%$known_modules)) {
546 #if(exists $known_modules->{$module}->{server_packages}) {
547 my $tmp = &{ $module."::process_incoming_msg" }($msg);
548 if (defined $tmp) {
549 $answer = $tmp;
550 }
551 #}
552 }
554 &print_known_daemons();
555 &print_known_clients();
557 daemon_log("processing of msg finished", 5);
559 if (defined $answer) {
560 print $PARENT_wr $answer."\n";
561 daemon_log("\t$answer", 5);
562 daemon_log(" ", 5);
563 } else {
564 print $PARENT_wr "done"."\n";
565 daemon_log(" ", 5);
566 }
567 redo;
568 }
569 }
570 # childs leaving the loop are allowed to die
571 exit(0);
574 #PARENT
575 } else {
576 # Close unused pipes
577 close( $PARENT_rd );
578 close( $PARENT_wr );
580 # add child to child alive hash
581 my %child_hash = (
582 'pid' => $child_pid,
583 'pipe_wr' => $CHILD_wr,
584 'pipe_rd' => $CHILD_rd,
585 'client_ref' => "",
586 );
588 $child = \%child_hash;
589 $busy_child{$$child{'pid'}} = $child;
590 return $child;
591 }
592 }
593 }
596 #=== FUNCTION ================================================================
597 # NAME: process_incoming_msg
598 # PARAMETERS: crypted_msg - string - incoming crypted message
599 # RETURNS: nothing
600 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
601 #===============================================================================
602 sub process_incoming_msg {
603 my ($crypted_msg) = @_;
604 if(not defined $crypted_msg) {
605 daemon_log("function 'process_incoming_msg': got no msg", 7);
606 }
607 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
608 $crypted_msg = $1;
609 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
610 daemon_log("msg from host:", 1);
611 daemon_log("\t$host", 1);
612 #daemon_log("crypted msg:", 7);
613 #daemon_log("\t$crypted_msg", 7);
615 # collect addresses from possible incoming clients
616 my @valid_keys;
617 my @host_keys = keys %$known_daemons;
618 foreach my $host_key (@host_keys) {
619 if($host_key =~ "^$host") {
620 push(@valid_keys, $host_key);
621 }
622 }
623 my @client_keys = keys %$known_clients;
624 foreach my $client_key (@client_keys) {
625 if($client_key =~ "^$host"){
626 push(@valid_keys, $client_key);
627 }
628 }
629 push(@valid_keys, $server_address);
631 my $l = @valid_keys;
632 my ($msg, $msg_hash);
633 my $msg_flag = 0;
635 # determine the correct passwd for deciphering of the incoming msgs
636 foreach my $host_key (@valid_keys) {
637 eval{
638 daemon_log( "key: $host_key", 7);
639 my $key_passwd;
640 if (exists $known_daemons->{$host_key}) {
641 $key_passwd = $known_daemons->{$host_key}->{passwd};
642 } elsif (exists $known_clients->{$host_key}) {
643 $key_passwd = $known_clients->{$host_key}->{passwd};
644 } elsif ($host_key eq $server_address) {
645 $key_passwd = $server_passwd;
646 }
647 daemon_log("key_passwd: $key_passwd", 7);
648 my $key_cipher = &create_ciphering($key_passwd);
649 $msg = &decrypt_msg($crypted_msg, $key_cipher);
650 $msg_hash = $xml->XMLin($msg, ForceArray=>1);
651 };
652 if($@) {
653 daemon_log("key raise error", 7);
654 $msg_flag += 1;
655 } else {
656 last;
657 }
658 }
660 if($msg_flag >= $l) {
661 daemon_log("ERROR: do not understand the message:", 1);
662 daemon_log("\t$msg", 1);
663 return;
664 }
666 # process incoming msg
667 my $header = &get_content_from_xml_hash($msg_hash, "header");
668 my $source = @{$msg_hash->{source}}[0];
670 daemon_log("header from msg:", 1);
671 daemon_log("\t$header", 1);
672 daemon_log("msg to process:", 5);
673 daemon_log("\t$msg", 5);
675 my @targets = @{$msg_hash->{target}};
676 my $len_targets = @targets;
677 if ($len_targets == 0){
678 daemon_log("ERROR: no target specified for msg $header", 1);
680 } elsif ($len_targets == 1){
681 # we have only one target symbol
683 my $target = $targets[0];
684 daemon_log("msg is for:", 7);
685 daemon_log("\t$target", 7);
687 if ($target eq $server_address) {
688 # msg is for server
689 if ($header eq 'new_passwd'){ &new_passwd($msg_hash)}
690 elsif ($header eq 'here_i_am') { &here_i_am($msg_hash)}
691 elsif ($header eq 'who_has') { &who_has($msg_hash) }
692 elsif ($header eq 'who_has_i_do') { &who_has_i_do($msg_hash)}
693 elsif ($header eq 'update_status') { &update_status($msg_hash) }
694 #elsif ($header eq 'got_ping') { &got_ping($msg_hash)}
695 elsif ($header eq 'get_load') { &execute_actions($msg_hash)}
696 else { daemon_log("ERROR: no function assigned to this msg", 5) }
699 } elsif ($target eq "*") {
700 # msg is for all clients
702 my @target_addresses = keys(%$known_clients);
703 foreach my $target_address (@target_addresses) {
704 if ($target_address eq $source) { next; }
705 $msg_hash->{target} = [$target_address];
706 &send_msg_hash2address($msg_hash, $target_address);
707 }
708 } else {
709 # msg is for one client
711 if (exists $known_clients->{$target}) {
712 # target is known
714 &send_msg_hash2address($msg_hash, $target);
715 } else {
716 # target is not known
718 daemon_log("ERROR: target $target is not known in known_clients", 1);
719 }
720 }
721 } else {
722 # we have multiple target symbols
724 my $target_string = join(", ", @targets);
725 daemon_log("msg is for:", 7);
726 daemon_log("\t$target_string", 7);
728 my $target_address;
729 foreach $target_address (@targets) {
730 if (exists $known_clients->{$target_address}) {
731 # target_address is known
733 &send_msg_hash2address($msg_hash, $target_address);
734 daemon_log("server forwards msg $header to client $target_address", 3);
735 } else {
736 # target is not known
738 daemon_log("ERROR: target $target_address is not known in known_clients", 1);
739 }
740 }
743 }
745 return;
746 }
749 #=== FUNCTION ================================================================
750 # NAME: open_socket
751 # PARAMETERS: PeerAddr string something like 192.168.1.1 or 192.168.1.1:10000
752 # [PeerPort] string necessary if port not appended by PeerAddr
753 # RETURNS: socket IO::Socket::INET
754 # DESCRIPTION: open a socket to PeerAddr
755 #===============================================================================
756 sub open_socket {
757 my ($PeerAddr, $PeerPort) = @_ ;
758 if(defined($PeerPort)){
759 $PeerAddr = $PeerAddr.":".$PeerPort;
760 }
761 my $socket;
762 $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
763 Porto => "tcp" ,
764 Type => SOCK_STREAM,
765 Timeout => 5,
766 );
767 if(not defined $socket) {
768 return;
769 }
770 daemon_log("open_socket:", 7);
771 daemon_log("\t$PeerAddr", 7);
772 return $socket;
773 }
776 #=== FUNCTION ================================================================
777 # NAME: open_fifo
778 # PARAMETERS: $fifo_path
779 # RETURNS: 0: FIFO couldn"t be setup, 1: FIFO setup correctly
780 # DESCRIPTION: creates a FIFO at $fifo_path
781 #===============================================================================
782 sub open_fifo {
783 my ($fifo_path) = @_ ;
784 if( -p $fifo_path ) {
785 daemon_log("FIFO at $fifo_path already exists! Is being deleted!", 1);
786 unlink($fifo_path);
787 }
788 POSIX::mkfifo($fifo_path, 0666) or die "can't mkfifo $fifo_path: $!";
789 daemon_log( "FIFO started at $fifo_path", 1) ;
790 return 1;
791 }
794 #=== FUNCTION ================================================================
795 # NAME: read_from_socket
796 # PARAMETERS: socket fh -
797 # RETURNS: result string - readed characters from socket
798 # DESCRIPTION: reads data from socket in 16 byte steps
799 #===============================================================================
800 sub read_from_socket {
801 my ($socket) = @_;
802 my $result = "";
804 $socket->blocking(1);
805 $result = <$socket>;
807 $socket->blocking(0);
808 while ( my $char = <$socket> ) {
809 if (not defined $char) { last }
810 $result .= $char;
811 }
813 # my $len = 16;
814 # while($len == 16){
815 # my $char;
816 # $len = sysread($socket, $char, 16);
817 # if($len != 16) { last }
818 # $result .= $char;
819 # }
820 return $result;
821 }
824 #=== FUNCTION ================================================================
825 # NAME: create_xml_hash
826 # PARAMETERS: header - string - message header (required)
827 # source - string - where the message come from (required)
828 # target - string - where the message should go to (required)
829 # [header_value] - string - something usefull (optional)
830 # RETURNS: hash - hash - nomen est omen
831 # DESCRIPTION: creates a key-value hash, all values are stored in a array
832 #===============================================================================
833 sub create_xml_hash {
834 my ($header, $source, $target, $header_value) = @_;
835 my $hash = {
836 header => [$header],
837 source => [$source],
838 target => [$target],
839 $header => [$header_value],
840 };
841 #daemon_log("create_xml_hash:", 7),
842 #chomp(my $tmp = Dumper $hash);
843 #daemon_log("\t$tmp", 7);
844 return $hash
845 }
848 #=== FUNCTION ================================================================
849 # NAME: create_xml_string
850 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
851 # RETURNS: xml_string - string - xml string representation of the hash
852 # DESCRIPTION: transform the hash to a string using XML::Simple module
853 #===============================================================================
854 sub create_xml_string {
855 my ($xml_hash) = @_ ;
856 my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
857 $xml_string =~ s/[\n]+//g;
858 #daemon_log("create_xml_string:",7);
859 #daemon_log("$xml_string\n", 7);
860 return $xml_string;
861 }
864 #=== FUNCTION ================================================================
865 # NAME: add_content2xml_hash
866 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
867 # element - string - key for the hash
868 # content - string - value for the hash
869 # RETURNS: nothing
870 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists, then append value to list
871 #===============================================================================
872 sub add_content2xml_hash {
873 my ($xml_ref, $element, $content) = @_;
874 if(not exists $$xml_ref{$element} ) {
875 $$xml_ref{$element} = [];
876 }
877 my $tmp = $$xml_ref{$element};
878 push(@$tmp, $content);
879 return;
880 }
883 #=== FUNCTION ================================================================
884 # NAME: get_content_from_xml_hash
885 # PARAMETERS: xml_ref - ref - reference of the xml hash
886 # element - string - key of the value you want
887 # RETURNS: value - string - if key is either header, target or source
888 # value - list - for all other keys in xml hash
889 # DESCRIPTION:
890 #===============================================================================
891 sub get_content_from_xml_hash {
892 my ($xml_ref, $element) = @_ ;
893 my $result = $xml_ref->{$element};
894 if( $element eq "header" || $element eq "target" || $element eq "source") {
895 return @$result[0];
896 }
897 return @$result;
898 }
901 #=== FUNCTION ================================================================
902 # NAME: encrypt_msg
903 # PARAMETERS: msg - string - message to encrypt
904 # my_cipher - ref - reference to a Crypt::Rijndael object
905 # RETURNS: crypted_msg - string - crypted message
906 # DESCRIPTION: crypts the incoming message with the Crypt::Rijndael module
907 #===============================================================================
908 sub encrypt_msg {
909 my ($msg, $my_cipher) = @_;
910 if(not defined $my_cipher) { print "no cipher object\n"; }
911 $msg = "\0"x(16-length($msg)%16).$msg;
912 my $crypted_msg = $my_cipher->encrypt($msg);
913 chomp($crypted_msg = &encode_base64($crypted_msg));
914 return $crypted_msg;
915 }
918 #=== FUNCTION ================================================================
919 # NAME: decrypt_msg
920 # PARAMETERS: crypted_msg - string - message to decrypt
921 # my_cipher - ref - reference to a Crypt::Rijndael object
922 # RETURNS: msg - string - decrypted message
923 # DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
924 #===============================================================================
925 sub decrypt_msg {
926 my ($crypted_msg, $my_cipher) = @_ ;
927 $crypted_msg = &decode_base64($crypted_msg);
928 my $msg = $my_cipher->decrypt($crypted_msg);
929 $msg =~ s/\0*//g;
930 return $msg;
931 }
934 #=== FUNCTION ================================================================
935 # NAME: create_ciphering
936 # PARAMETERS: passwd - string - used to create ciphering
937 # RETURNS: cipher - object
938 # DESCRIPTION: creates a Crypt::Rijndael::MODE_CBC object with passwd as key
939 #===============================================================================
940 sub create_ciphering {
941 my ($passwd) = @_;
942 $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
943 my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
945 #daemon_log("iv: $iv", 7);
946 #daemon_log("key: $passwd", 7);
947 my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
948 $my_cipher->set_iv($iv);
949 return $my_cipher;
950 }
953 #=== FUNCTION ================================================================
954 # NAME: send_msg_hash2address
955 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
956 # PeerAddr string - socket address to send msg
957 # PeerPort string - socket port, if not included in socket address
958 # RETURNS: nothing
959 # DESCRIPTION: ????
960 #===============================================================================
961 sub send_msg_hash2address {
962 my ($msg_hash, $address, $passwd) = @_ ;
964 # fetch header for logging
965 my $header = &get_content_from_xml_hash($msg_hash, "header");
967 # generate xml string
968 my $msg_xml = &create_xml_string($msg_hash);
970 # fetch the appropriated passwd from hash
971 if(not defined $passwd) {
972 if(exists $known_daemons->{$address}) {
973 $passwd = $known_daemons->{$address}->{passwd};
974 } elsif(exists $known_clients->{$address}) {
975 $passwd = $known_clients->{$address}->{passwd};
977 } else {
978 daemon_log("$address not known, neither as server nor as client", 1);
979 return;
980 }
981 }
983 # create ciphering object
984 my $act_cipher = &create_ciphering($passwd);
986 # encrypt xml msg
987 my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
989 # opensocket
990 my $socket = &open_socket($address);
991 if(not defined $socket){
992 daemon_log( "cannot send '$header'-msg to $address , server not reachable", 5);
994 if (exists $known_clients->{$address}) {
995 if ($known_clients->{$address}->{status} eq "down") {
996 # if status of not reachable client is already 'down', then delete client from known_clients
997 &clean_up_known_clients($address);
999 } else {
1000 # update status to 'down'
1001 &update_known_clients(hostname=>$address, status=>"down");
1003 }
1004 }
1005 return;
1006 }
1008 # send xml msg
1009 print $socket $crypted_msg."\n";
1011 close $socket;
1013 daemon_log("send '$header'-msg to $address", 1);
1015 daemon_log("$msg_xml", 5);
1017 #daemon_log("crypted message:",7);
1018 #daemon_log("\t$crypted_msg", 7);
1020 # update status of client in known_clients with last send msg
1021 if(exists $known_daemons->{$address}) {
1022 #&update_known_daemons();
1023 } elsif(exists $known_clients->{$address}) {
1024 &update_known_clients(hostname=>$address, status=>$header);
1025 }
1027 return;
1028 }
1031 #=== FUNCTION ================================================================
1032 # NAME: send_msg_hash2bus
1033 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
1034 # RETURNS: nothing
1035 # DESCRIPTION: ????
1036 #===============================================================================
1037 sub send_msg_hash2bus {
1038 my($msg_hash) = @_;
1040 # fetch header for logging
1041 my $header = &get_content_from_xml_hash($msg_hash, "header");
1043 # generate xml string
1044 my $msg_xml = &create_xml_string($msg_hash);
1046 # encrypt xml msg
1047 my $crypted_msg = &encrypt_msg($msg_xml, $bus_cipher);
1049 # open socket
1050 my $socket = &open_socket($bus_address);
1051 if(not defined $socket){
1052 daemon_log( "cannot send '$header'-msg to $bus_address , bus not reachable", 5);
1053 return;
1054 }
1056 # send xml msg
1057 print $socket $crypted_msg."\n";
1059 close $socket;
1062 daemon_log("send '$header'-msg to bus", 1);
1063 daemon_log("$msg_xml", 5);
1064 #daemon_log("crypted msg:",7);
1065 #daemon_log("\t$crypted_msg", 7);
1067 return;
1068 }
1076 ##=== FUNCTION ================================================================
1077 ## NAME: new_passwd
1078 ## PARAMETERS: msg_hash - ref - hash from function create_xml_hash
1079 ## RETURNS: nothing
1080 ## DESCRIPTION: process this incoming message
1081 ##===============================================================================
1082 #sub new_passwd {
1083 # my ($msg_hash) = @_;
1084 #
1085 # my $source = &get_content_from_xml_hash($msg_hash, "source");
1086 # my $passwd = (&get_content_from_xml_hash($msg_hash, "new_passwd"))[0];
1087 #
1088 # if (exists $known_daemons->{$source}) {
1089 # &add_content2known_daemons(hostname=>$source, status=>"new_passwd", passwd=>$passwd);
1090 # $bus_cipher = &create_ciphering($passwd);
1091 # my $hash = &create_xml_hash("confirm_new_passwd", "$server_ip:$server_port", "$source");
1092 # &send_msg_hash2address($hash, $source);
1093 #
1094 # } elsif (exists $known_clients->{$source}) {
1095 # &add_content2known_clients(hostname=>$source, status=>"new_passwd", passwd=>$passwd);
1096 #
1097 # } else {
1098 # daemon_log("ERROR: $source not known, neither in known_daemons nor in known_clients", 1)
1099 # }
1100 #
1101 # return;
1102 #}
1105 ##=== FUNCTION ================================================================
1106 ## NAME: make ping
1107 ## PARAMETERS: address - string - address which should be pinged
1108 ## RETURNS: nothing
1109 ## DESCRIPTION: send ping message to address
1110 ##===============================================================================
1111 #sub make_ping {
1112 # my ($msg_hash) = @_;
1113 #
1114 # my $source = &get_content_from_xml_hash($msg_hash, "source");
1115 # my $target = &get_content_from_xml_hash($msg_hash, "target");
1116 #
1117 # print "make_ping:$source\n";
1118 # my $out_hash = &create_xml_hash("ping", $target, $source);
1119 # &send_msg_hash2address($out_hash, $source);
1120 # return;
1121 #}
1124 ##=== FUNCTION ================================================================
1125 ## NAME: got_ping
1126 ## PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1127 ## RETURNS: nothing
1128 ## DESCRIPTION: process this incoming message
1129 ##===============================================================================
1130 #sub got_ping {
1131 # my ($msg_hash) = @_;
1132 #
1133 # my $source = &get_content_from_xml_hash($msg_hash, 'source');
1134 # my $target = &get_content_from_xml_hash($msg_hash, 'target');
1135 # my $header = &get_content_from_xml_hash($msg_hash, 'header');
1136 #
1137 # if(exists $known_daemons->{$source}) {
1138 # &add_content2known_daemons(hostname=>$source, status=>$header);
1139 # } else {
1140 # &add_content2known_clients(hostname=>$source, status=>$header);
1141 # }
1142 #
1143 # return;
1144 #}
1147 ##=== FUNCTION ================================================================
1148 ## NAME: here_i_am
1149 ## PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1150 ## RETURNS: nothing
1151 ## DESCRIPTION: process this incoming message
1152 ##===============================================================================
1153 #sub here_i_am {
1154 # my ($msg_hash) = @_;
1155 #
1156 # my $source = &get_content_from_xml_hash($msg_hash, "source");
1157 # my $mac_address = (&get_content_from_xml_hash($msg_hash, "mac_address"))[0];
1158 # my $out_hash;
1159 #
1160 # # number of known clients
1161 # my $nu_clients = keys %$known_clients;
1162 #
1163 # # check wether client address or mac address is already known
1164 # if (exists $known_clients->{$source}) {
1165 # daemon_log("WARNING: $source is already known as a client", 1);
1166 # daemon_log("WARNING: values for $source are being overwritten", 1);
1167 # $nu_clients --;
1168 # }
1169 #
1170 # # number of actual activ clients
1171 # my $act_nu_clients = $nu_clients;
1172 #
1173 # daemon_log("number of actual activ clients: $act_nu_clients", 5);
1174 # daemon_log("number of maximal allowed clients: $max_clients", 5);
1175 #
1176 # if($max_clients <= $act_nu_clients) {
1177 # my $out_hash = &create_xml_hash("denied", $server_address, $source);
1178 # &add_content2xml_hash($out_hash, "denied", "I_cannot_take_any_more_clients!");
1179 # my $passwd = (&get_content_from_xml_hash($msg_hash, "new_passwd"))[0];
1180 # &send_msg_hash2address($out_hash, $source, $passwd);
1181 # return;
1182 # }
1183 #
1184 # # new client accepted
1185 # my $new_passwd = (&get_content_from_xml_hash($msg_hash, "new_passwd"))[0];
1186 #
1187 # # create known_daemons entry
1188 # my $events = (&get_content_from_xml_hash($msg_hash, "events"))[0];
1189 # &create_known_client($source);
1190 # &add_content2known_clients(hostname=>$source, events=>$events, mac_address=>$mac_address,
1191 # status=>"registered", passwd=>$new_passwd);
1192 #
1193 # # return acknowledgement to client
1194 # $out_hash = &create_xml_hash("registered", $server_address, $source);
1195 # &send_msg_hash2address($out_hash, $source);
1196 #
1197 # # notify registered client to bus
1198 # $out_hash = &create_xml_hash("new_client", $server_address, $bus_address, $source);
1199 # &send_msg_hash2bus($out_hash);
1200 #
1201 # # give the new client his ldap config
1202 # &new_ldap_config($source);
1203 #
1204 # return;
1205 #}
1208 #=== FUNCTION ================================================================
1209 # NAME: who_has
1210 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1211 # RETURNS: nothing
1212 # DESCRIPTION: process this incoming message
1213 #===============================================================================
1214 #sub who_has {
1215 # my ($msg_hash) = @_ ;
1216 #
1217 # # what is your search pattern
1218 # my $search_pattern = (&get_content_from_xml_hash($msg_hash, "who_has"))[0];
1219 # my $search_element = (&get_content_from_xml_hash($msg_hash, $search_pattern))[0];
1220 # daemon_log("who_has-msg looking for $search_pattern $search_element", 7);
1221 #
1222 # # scanning known_clients for search_pattern
1223 # my @host_addresses = keys %$known_clients;
1224 # my $known_clients_entries = length @host_addresses;
1225 # my $host_address;
1226 # foreach my $host (@host_addresses) {
1227 # my $client_element = $known_clients->{$host}->{$search_pattern};
1228 # if ($search_element eq $client_element) {
1229 # $host_address = $host;
1230 # last;
1231 # }
1232 # }
1233 #
1234 # # search was successful
1235 # if (defined $host_address) {
1236 # my $source = @{$msg_hash->{source}}[0];
1237 # my $out_msg = &create_xml_hash("who_has_i_do", $server_address, $source, "mac_address");
1238 # &add_content2xml_hash($out_msg, "mac_address", $search_element);
1239 # &send_msg_hash2address($out_msg, $bus_address);
1240 # }
1241 # return;
1242 #}
1245 #sub who_has_i_do {
1246 # my ($msg_hash) = @_ ;
1247 # my $header = &get_content_from_xml_hash($msg_hash, "header");
1248 # my $source = &get_content_from_xml_hash($msg_hash, "source");
1249 # my $search_param = (&get_content_from_xml_hash($msg_hash, $header))[0];
1250 # my $search_value = (&get_content_from_xml_hash($msg_hash, $search_param))[0];
1251 # print "\ngot msg $header:\nserver $source has client with $search_param $search_value\n";
1252 #}
1255 #=== FUNCTION ================================================================
1256 # NAME: update_status
1257 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1258 # RETURNS: nothing
1259 # DESCRIPTION: process this incoming message
1260 #===============================================================================
1261 #sub update_status {
1262 # my ($msg_hash) = @_;
1263 # my $header = &get_content_from_xml_hash($msg_hash, "header");
1264 # my $source = &get_content_from_xml_hash($msg_hash, "source");
1265 # my $new_status = (&get_content_from_xml_hash($msg_hash, "update_status"))[0];
1266 #
1267 # # find the source
1268 # my $act_known_hash;
1269 # if (exists $known_daemons->{$source}) {
1270 #
1271 # &add_content2known_daemons(hostname=>$source, status=>$new_status);
1272 # } elsif (exists $known_clients->{$source}) {
1273 # &update_known_clients(hostname=>$source, status=>$new_status);
1274 # #&add_content2known_clients(hostname=>$source, status=>$new_status);
1275 # } else {
1276 # daemon_log("ERROR: got $header-msg, but cannot find $source in my hashes, unable to update status", 1);
1277 # return;
1278 # }
1279 #
1280 # return;
1281 #}
1284 ##=== FUNCTION ================================================================
1285 ## NAME: new_ldap_config
1286 ## PARAMETERS: address - string - ip address and port of a host
1287 ## RETURNS: nothing
1288 ## DESCRIPTION: send to address the ldap configuration found for dn gotoLdapServer
1289 ##===============================================================================
1290 #sub new_ldap_config {
1291 # my ($address) = @_ ;
1292 #
1293 # if (not exists $known_clients->{$address}) {
1294 # daemon_log("ERROR: $address does not exist in known_clients, cannot send him his ldap config", 1);
1295 # return;
1296 # }
1297 #
1298 # my $mac_address = $known_clients->{$address}->{"mac_address"};
1299 # if (not defined $mac_address) {
1300 # daemon_log("ERROR: no mac address found for client $address", 1);
1301 # return;
1302 # }
1303 #
1304 # # fetch dn
1305 # my $goHard_cmd = "ldapsearch -x '(&(objectClass=goHard)(macAddress=00:11:22:33:44:57))' dn gotoLdapServer";
1306 # my $dn;
1307 # my @gotoLdapServer;
1308 # open (PIPE, "$goHard_cmd 2>&1 |");
1309 # while(<PIPE>) {
1310 # chomp $_;
1311 # # If it's a comment, goto next
1312 # if ($_ =~ m/^[#]/) { next;}
1313 # if ($_ =~ m/^dn: ([\S]+?)$/) {
1314 # $dn = $1;
1315 # } elsif ($_ =~ m/^gotoLdapServer: ([\S]+?)$/) {
1316 # push(@gotoLdapServer, $1);
1317 # }
1318 # }
1319 # close(PIPE);
1320 #
1321 # # no dn found
1322 # if (not defined $dn) {
1323 # daemon_log("ERROR: no dn arose from command: $goHard_cmd", 1);
1324 # return;
1325 # }
1326 #
1327 # # no gotoLdapServer found
1328 # my $gosaGroupOfNames_cmd = "ldapsearch -x '(&(objectClass=gosaGroupOfNames)(member=$dn))' gotoLdapServer";
1329 # if (@gotoLdapServer == 0) {
1330 # open (PIPE, "$gosaGroupOfNames_cmd 2>&1 |");
1331 # while(<PIPE>) {
1332 # chomp $_;
1333 # if ($_ =~ m/^[#]/) { next; }
1334 # if ($_ =~ m/^gotoLdapServer: ([\S]+?)$/) {
1335 # push(@gotoLdapServer, $1);
1336 # }
1337 # }
1338 # close(PIPE);
1339 # }
1340 #
1341 # # still no gotoLdapServer found
1342 # if (@gotoLdapServer == 0) {
1343 # daemon_log("ERROR: cannot find gotoLdapServer entry in command: $gosaGroupOfNames_cmd", 1);
1344 # return;
1345 # }
1346 #
1347 # # sort @gotoLdapServer and then split of ranking
1348 # my @sorted_gotoLdapServer = sort(@gotoLdapServer);
1349 # @gotoLdapServer = reverse(@sorted_gotoLdapServer);
1350 # foreach (@gotoLdapServer) {
1351 # $_ =~ s/^\d://;
1352 # }
1353 #
1354 # my $t = join(" ", @gotoLdapServer);
1355 #
1356 # my $out_hash = &create_xml_hash("new_ldap_config", $server_address, $address);
1357 # map(&add_content2xml_hash($out_hash, "new_ldap_config", $_), @gotoLdapServer);
1358 # &send_msg_hash2address($out_hash, $address);
1359 #
1360 # return;
1361 #}
1364 ##=== FUNCTION ================================================================
1365 ## NAME: execute_actions
1366 ## PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1367 ## RETURNS: nothing
1368 ## DESCRIPTION: invokes the script specified in msg_hash which is located under
1369 ## /etc/gosad/actions
1370 ##===============================================================================
1371 #sub execute_actions {
1372 # my ($msg_hash) = @_ ;
1373 # my $configdir= '/etc/gosad/actions/';
1374 # my $result;
1375 #
1376 # my $header = &get_content_from_xml_hash($msg_hash, 'header');
1377 # my $source = &get_content_from_xml_hash($msg_hash, 'source');
1378 # my $target = &get_content_from_xml_hash($msg_hash, 'target');
1379 #
1380 #
1381 # if((not defined $source)
1382 # && (not defined $target)
1383 # && (not defined $header)) {
1384 # daemon_log("ERROR: Entries missing in XML msg for gosad actions under /etc/gosad/actions");
1385 # } else {
1386 # my $parameters="";
1387 # my @params = &get_content_from_xml_hash($msg_hash, $header);
1388 # my $params = join(", ", @params);
1389 # daemon_log("execute_actions: got parameters: $params", 5);
1390 #
1391 # if (@params) {
1392 # foreach my $param (@params) {
1393 # my $param_value = (&get_content_from_xml_hash($msg_hash, $param))[0];
1394 # daemon_log("execute_actions: parameter -> value: $param -> $param_value", 7);
1395 # $parameters.= " ".$param_value;
1396 # }
1397 # }
1398 #
1399 # my $cmd= $configdir.$header."$parameters";
1400 # daemon_log("execute_actions: executing cmd: $cmd", 7);
1401 # $result= "";
1402 # open(PIPE, "$cmd 2>&1 |");
1403 # while(<PIPE>) {
1404 # $result.=$_;
1405 # }
1406 # close(PIPE);
1407 # }
1408 #
1409 # # process the event result
1410 #
1411 #
1412 # return;
1413 #}
1416 #=== FUNCTION ================================================================
1417 # NAME: print_known_daemons
1418 # PARAMETERS: nothing
1419 # RETURNS: nothing
1420 # DESCRIPTION: nomen est omen
1421 #===============================================================================
1422 sub print_known_daemons {
1423 my ($tmp) = @_ ;
1424 print "####################################\n";
1425 print "# status of known_daemons\n";
1426 $shmda->shlock(LOCK_EX);
1427 my @hosts = keys %$known_daemons;
1428 foreach my $host (@hosts) {
1429 my $status = $known_daemons->{$host}->{status} ;
1430 my $passwd = $known_daemons->{$host}->{passwd};
1431 my $timestamp = $known_daemons->{$host}->{timestamp};
1432 print "$host\n";
1433 print "\tstatus: $status\n";
1434 print "\tpasswd: $passwd\n";
1435 print "\ttimestamp: $timestamp\n";
1436 }
1437 $shmda->shunlock(LOCK_EX);
1438 print "####################################\n";
1439 return;
1440 }
1443 #=== FUNCTION ================================================================
1444 # NAME: create_known_daemon
1445 # PARAMETERS: hostname - string - key for the hash known_daemons
1446 # RETURNS: nothing
1447 # DESCRIPTION: creates a dummy entry for hostname in known_daemons
1448 #===============================================================================
1449 sub create_known_daemon {
1450 my ($hostname) = @_;
1451 $shmda->shlock(LOCK_EX);
1452 $known_daemons->{$hostname} = {};
1453 $known_daemons->{$hostname}->{status} = "none";
1454 $known_daemons->{$hostname}->{passwd} = "none";
1455 $known_daemons->{$hostname}->{timestamp} = "none";
1456 $shmda->shunlock(LOCK_EX);
1457 return;
1458 }
1461 #=== FUNCTION ================================================================
1462 # NAME: add_content2known_daemons
1463 # PARAMETERS: hostname - string - ip address and port of host (required)
1464 # status - string - (optional)
1465 # passwd - string - (optional)
1466 # mac_address - string - mac address of host (optional)
1467 # RETURNS: nothing
1468 # DESCRIPTION: nome est omen and updates each time the timestamp of hostname
1469 #===============================================================================
1470 sub add_content2known_daemons {
1471 my $arg = {
1472 hostname => undef, status => undef, passwd => undef,
1473 mac_address => undef, events => undef,
1474 @_ };
1475 my $hostname = $arg->{hostname};
1476 my $status = $arg->{status};
1477 my $passwd = $arg->{passwd};
1478 my $mac_address = $arg->{mac_address};
1479 my $events = $arg->{events};
1481 if (not defined $hostname) {
1482 daemon_log("ERROR: function add_content2known_daemons is not invoked with requiered parameter 'hostname'", 1);
1483 return;
1484 }
1486 my ($seconds, $minutes, $hours, $monthday, $month,
1487 $year, $weekday, $yearday, $sommertime) = localtime(time);
1488 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
1489 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
1490 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
1491 $month+=1;
1492 $month = $month < 10 ? $month = "0".$month : $month;
1493 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
1494 $year+=1900;
1495 my $t = "$year$month$monthday$hours$minutes$seconds";
1497 $shmda->shlock(LOCK_EX);
1498 if (defined $status) {
1499 $known_daemons->{$hostname}->{status} = $status;
1500 }
1501 if (defined $passwd) {
1502 $known_daemons->{$hostname}->{passwd} = $passwd;
1503 }
1504 if (defined $mac_address) {
1505 $known_daemons->{$hostname}->{mac_address} = $mac_address;
1506 }
1507 if (defined $events) {
1508 $known_daemons->{$hostname}->{events} = $events;
1509 }
1510 $known_daemons->{$hostname}->{timestamp} = $t;
1511 $shmda->shlock(LOCK_EX);
1512 return;
1513 }
1516 #=== FUNCTION ================================================================
1517 # NAME: update_known_daemons
1518 # PARAMETERS: hostname - string - ip address and port of host (required)
1519 # status - string - (optional)
1520 # passwd - string - (optional)
1521 # client - string - ip address and port of client (optional)
1522 # RETURNS: nothing
1523 # DESCRIPTION: nome est omen and updates each time the timestamp of hostname
1524 #===============================================================================
1525 sub update_known_daemons {
1526 my $arg = {
1527 hostname => undef, status => undef, passwd => undef,
1528 @_ };
1529 my $hostname = $arg->{hostname};
1530 my $status = $arg->{status};
1531 my $passwd = $arg->{passwd};
1533 if (not defined $hostname) {
1534 daemon_log("ERROR: function add_content2known_daemons is not invoked with requiered parameter 'hostname'", 1);
1535 return;
1536 }
1538 my ($seconds, $minutes, $hours, $monthday, $month,
1539 $year, $weekday, $yearday, $sommertime) = localtime(time);
1540 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
1541 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
1542 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
1543 $month+=1;
1544 $month = $month < 10 ? $month = "0".$month : $month;
1545 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
1546 $year+=1900;
1547 my $t = "$year$month$monthday$hours$minutes$seconds";
1549 $shmda->shlock(LOCK_EX);
1550 if (defined $status) {
1551 $known_daemons->{$hostname}->{status} = $status;
1552 }
1553 if (defined $passwd) {
1554 $known_daemons->{$hostname}->{passwd} = $passwd;
1555 }
1556 $known_daemons->{$hostname}->{timestamp} = $t;
1557 $shmda->shunlock(LOCK_EX);
1558 return;
1559 }
1562 #=== FUNCTION ================================================================
1563 # NAME: print_known_clients
1564 # PARAMETERS: nothing
1565 # RETURNS: nothing
1566 # DESCRIPTION: nomen est omen
1567 #===============================================================================
1568 sub print_known_clients {
1570 print "####################################\n";
1571 print "# status of known_clients\n";
1572 $shmcl->shlock(LOCK_EX);
1573 my @hosts = keys %$known_clients;
1574 if (@hosts) {
1575 foreach my $host (@hosts) {
1576 my $status = $known_clients->{$host}->{status} ;
1577 my $passwd = $known_clients->{$host}->{passwd};
1578 my $timestamp = $known_clients->{$host}->{timestamp};
1579 my $mac_address = $known_clients->{$host}->{mac_address};
1580 my $events = $known_clients->{$host}->{events};
1581 print "$host\n";
1582 print "\tstatus: $status\n";
1583 print "\tpasswd: $passwd\n";
1584 print "\ttimestamp: $timestamp\n";
1585 print "\tmac_address: $mac_address\n";
1586 print "\tevents: $events\n";
1587 }
1588 }
1589 $shmcl->shunlock(LOCK_EX);
1590 print "####################################\n";
1591 return;
1592 }
1598 #=== FUNCTION ================================================================
1599 # NAME: create_known_client
1600 # PARAMETERS: hostname - string - key for the hash known_clients
1601 # RETURNS: nothing
1602 # DESCRIPTION: creates a dummy entry for hostname in known_clients
1603 #===============================================================================
1604 sub create_known_client {
1605 my ($hostname) = @_;
1606 $shmcl->shlock(LOCK_EX);
1607 $known_clients->{$hostname} = {};
1608 $known_clients->{$hostname}->{status} = "none";
1609 $known_clients->{$hostname}->{passwd} = "none";
1610 $known_clients->{$hostname}->{timestamp} = "none";
1611 $known_clients->{$hostname}->{mac_address} = "none";
1612 $known_clients->{$hostname}->{events} = "none";
1613 $shmcl->shunlock(LOCK_EX);
1614 return;
1615 }
1620 #=== FUNCTION ================================================================
1621 # NAME: add_content2known_clients
1622 # PARAMETERS: hostname - string - ip address and port of host (required)
1623 # status - string - (optional)
1624 # passwd - string - (optional)
1625 # mac_address - string - (optional)
1626 # events - string - event of client, executable skripts under /etc/gosac/events
1627 # RETURNS: nothing
1628 # DESCRIPTION: nome est omen and updates each time the timestamp of hostname
1629 #===============================================================================
1630 sub add_content2known_clients {
1631 my $arg = {
1632 hostname => undef, status => undef, passwd => undef,
1633 mac_address => undef, events => undef,
1634 @_ };
1635 my $hostname = $arg->{hostname};
1636 my $status = $arg->{status};
1637 my $passwd = $arg->{passwd};
1638 my $mac_address = $arg->{mac_address};
1639 my $events = $arg->{events};
1641 if (not defined $hostname) {
1642 daemon_log("ERROR: function add_content2known_clients is not invoked with requiered parameter 'hostname'", 1);
1643 return;
1644 }
1646 my ($seconds, $minutes, $hours, $monthday, $month,
1647 $year, $weekday, $yearday, $sommertime) = localtime(time);
1648 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
1649 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
1650 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
1651 $month+=1;
1652 $month = $month < 10 ? $month = "0".$month : $month;
1653 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
1654 $year+=1900;
1655 my $t = "$year$month$monthday$hours$minutes$seconds";
1657 $shmcl->shlock(LOCK_EX);
1658 if (defined $status) {
1659 $known_clients->{$hostname}->{status} = $status;
1660 }
1661 if (defined $passwd) {
1662 $known_clients->{$hostname}->{passwd} = $passwd;
1663 }
1664 if (defined $mac_address) {
1665 $known_clients->{$hostname}->{mac_address} = $mac_address;
1666 }
1667 if (defined $events) {
1668 $known_clients->{$hostname}->{events} = $events;
1669 }
1670 $known_clients->{$hostname}->{timestamp} = $t;
1671 $shmcl->shlock(LOCK_EX);
1672 return;
1673 }
1676 #=== FUNCTION ================================================================
1677 # NAME:
1678 # PARAMETERS:
1679 # RETURNS:
1680 # DESCRIPTION:
1681 #===============================================================================
1682 sub clean_up_known_clients {
1683 my ($address) = @_ ;
1685 if (not exists $known_clients->{$address}) {
1686 daemon_log("cannot prune known_clients from $address, client not known", 5);
1687 return;
1688 }
1690 delete $known_clients->{$address};
1692 # send bus a msg that address was deleted from known_clients
1693 my $out_hash = &create_xml_hash('delete_client', $server_address, $bus_address, $address);
1694 &send_msg_hash2bus($out_hash);
1696 daemon_log("client $address deleted from known_clients because of multiple down time", 3);
1697 return;
1698 }
1701 #=== FUNCTION ================================================================
1702 # NAME: update_known_clients
1703 # PARAMETERS: hostname - string - ip address and port of host (required)
1704 # status - string - (optional)
1705 # passwd - string - (optional)
1706 # client - string - ip address and port of client (optional)
1707 # RETURNS: nothing
1708 # DESCRIPTION: nome est omen and updates each time the timestamp of hostname
1709 #===============================================================================
1710 sub update_known_clients {
1711 my $arg = {
1712 hostname => undef, status => undef, passwd => undef,
1713 mac_address => undef, events => undef,
1714 @_ };
1715 my $hostname = $arg->{hostname};
1716 my $status = $arg->{status};
1717 my $passwd = $arg->{passwd};
1718 my $mac_address = $arg->{mac_address};
1719 my $events = $arg->{events};
1721 if (not defined $hostname) {
1722 daemon_log("ERROR: function add_content2known_daemons is not invoked with requiered parameter 'hostname'", 1);
1723 return;
1724 }
1726 my ($seconds, $minutes, $hours, $monthday, $month,
1727 $year, $weekday, $yearday, $sommertime) = localtime(time);
1728 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
1729 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
1730 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
1731 $month+=1;
1732 $month = $month < 10 ? $month = "0".$month : $month;
1733 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
1734 $year+=1900;
1735 my $t = "$year$month$monthday$hours$minutes$seconds";
1737 $shmcl->shlock(LOCK_EX);
1738 if (defined $status) {
1739 $known_clients->{$hostname}->{status} = $status;
1740 }
1741 if (defined $passwd) {
1742 $known_clients->{$hostname}->{passwd} = $passwd;
1743 }
1744 if (defined $mac_address) {
1745 $known_clients->{$hostname}->{mac_address} = $mac_address;
1746 }
1747 if (defined $events) {
1748 $known_clients->{$hostname}->{events} = $events;
1749 }
1750 $known_clients->{$hostname}->{timestamp} = $t;
1751 $shmcl->shunlock(LOCK_EX);
1752 return;
1753 }
1761 #==== MAIN = main ==============================================================
1763 # parse commandline options
1764 Getopt::Long::Configure( "bundling" );
1765 GetOptions("h|help" => \&usage,
1766 "c|config=s" => \$cfg_file,
1767 "f|foreground" => \$foreground,
1768 "v|verbose+" => \$verbose,
1769 "no-bus+" => \$no_bus,
1770 "no-arp+" => \$no_arp,
1771 );
1773 # read and set config parameters
1774 &check_cmdline_param ;
1775 &read_configfile;
1776 &check_pid;
1777 &import_modules;
1779 $SIG{CHLD} = 'IGNORE';
1781 # restart daemon log file
1782 if(-e $log_file ) { unlink $log_file }
1783 daemon_log(" ", 1);
1784 daemon_log("gosad started!", 1);
1786 # Just fork, if we"re not in foreground mode
1787 if( ! $foreground ) { $pid = fork(); }
1788 else { $pid = $$; }
1790 # Do something useful - put our PID into the pid_file
1791 if( 0 != $pid ) {
1792 open( LOCK_FILE, ">$pid_file" );
1793 print LOCK_FILE "$pid\n";
1794 close( LOCK_FILE );
1795 if( !$foreground ) { exit( 0 ) };
1796 }
1798 # detect own ip and mac address
1799 ($server_ip, $server_mac_address) = &get_ip_and_mac();
1800 if (not defined $server_ip) {
1801 die "EXIT: ip address of $0 could not be detected";
1802 }
1803 daemon_log("server ip address detected: $server_ip", 1);
1804 daemon_log("server mac address detected: $server_mac_address", 1);
1806 # setup xml parser
1807 $xml = new XML::Simple();
1809 # create cipher object
1810 $bus_cipher = &create_ciphering($bus_passwd);
1811 $bus_address = "$bus_ip:$bus_port";
1813 # create reading and writing vectors
1814 my $rbits = my $wbits = my $ebits = "";
1816 # open server socket
1817 $server_address = "$server_ip:$server_port";
1818 if($server_activ eq "on"){
1819 daemon_log(" ", 1);
1820 $server = IO::Socket::INET->new(LocalPort => $server_port,
1821 Type => SOCK_STREAM,
1822 Reuse => 1,
1823 Listen => 20,
1824 );
1825 if(not defined $server){
1826 daemon_log("cannot be a tcp server at $server_port : $@");
1827 } else {
1828 daemon_log("start server:", 1);
1829 daemon_log("\t$server_ip:$server_port",1) ;
1830 vec($rbits, fileno $server, 1) = 1;
1831 vec($wbits, fileno $server, 1) = 1;
1832 }
1833 }
1835 # register at bus
1836 if ($no_bus > 0) {
1837 $bus_activ = "off"
1838 }
1839 if($bus_activ eq "on") {
1840 daemon_log(" ", 1);
1841 ®ister_at_bus();
1842 }
1845 daemon_log(" ", 1);
1847 # start arp fifo
1848 if ($no_arp > 0) {
1849 $arp_activ = "off";
1850 }
1851 my $my_fifo;
1852 if($arp_activ eq "on") {
1853 $my_fifo = &open_fifo($arp_fifo_path);
1854 if($my_fifo == 0) { die "fifo file disappeared\n" }
1855 sysopen($arp_fifo, $arp_fifo_path, O_RDWR) or die "can't read from $arp_fifo: $!" ;
1857 vec($rbits, fileno $arp_fifo, 1) = 1;
1858 }
1860 $gosa_address = "$gosa_ip:$gosa_port";
1861 # start gosa inferface fifos
1862 if ($gosa_activ eq "on") {
1863 daemon_log(" ",1);
1864 $gosa_server = IO::Socket::INET->new(LocalPort => $gosa_port,
1865 Type => SOCK_STREAM,
1866 Reuse => 1,
1867 Listen => 1,
1868 );
1869 if (not defined $gosa_server) {
1870 daemon_log("cannot start tcp server at $gosa_port for communication to gosa: $@", 1);
1871 } else {
1872 daemon_log("start server at for communication to gosa", 1);
1873 daemon_log("\t$server_ip:$gosa_port");
1874 vec($rbits, fileno $gosa_server, 1) = 1;
1876 }
1879 #&open_fifo($gosa_fifo_in);
1880 #sysopen(GOSA_FIFO_IN, $gosa_fifo_in, O_RDWR) or die "can't read from GOSA_FIFO_IN: $!" ;
1881 #vec($rbits, fileno GOSA_FIFO_IN, 1) = 1;
1883 #&open_fifo($gosa_fifo_out);
1884 #sysopen(GOSA_FIFO_OUT, $gosa_fifo_out, O_RDWR) or die "can't read from GOSA_FIFO_IN: $!" ;
1886 }
1889 ###################################
1890 #everything ready, okay, lets start
1891 ###################################
1892 while(1) {
1894 # add all handles from the childs
1895 while ( my ($pid, $child_hash) = each %busy_child ) {
1897 # check whether process still exists
1898 my $exitus_pid = waitpid($pid, WNOHANG);
1899 if($exitus_pid != 0) {
1900 delete $busy_child{$pid};
1901 next;
1902 }
1904 # add child fhd to the listener
1905 my $fhd = $$child_hash{'pipe_rd'};
1906 vec($rbits, fileno $fhd, 1) = 1;
1907 }
1909 my ($rout, $wout);
1910 my $nf = select($rout=$rbits, $wout=$wbits, undef, undef);
1912 # error handling
1913 if($nf < 0 ) {
1914 }
1916 # something is coming in
1917 if($server_activ eq "on" && vec($rout, fileno $server, 1)) {
1918 daemon_log(" ", 1);
1919 my $client = $server->accept();
1920 my $other_end = getpeername($client);
1921 if(not defined $other_end) {
1922 daemon_log("client cannot be identified: $!");
1923 } else {
1924 my ($port, $iaddr) = unpack_sockaddr_in($other_end);
1925 my $actual_ip = inet_ntoa($iaddr);
1926 daemon_log("accept client at daemon socket from $actual_ip", 5);
1927 my $in_msg = &read_from_socket($client);
1928 if(defined $in_msg){
1929 chomp($in_msg);
1930 &activating_child($in_msg, $actual_ip);
1931 } else {
1932 daemon_log("cannot read from $actual_ip", 5);
1933 }
1934 }
1935 close($client);
1936 }
1938 if($arp_activ eq "on" && vec($rout, fileno $arp_fifo, 1)) {
1939 my $in_msg = <$arp_fifo>;
1940 chomp($in_msg);
1941 print "arp_activ: msg: $in_msg\n";
1942 my $act_passwd = $known_daemons->{$bus_address}->{passwd};
1943 print "arp_activ: arp_passwd: $act_passwd\n";
1945 my $in_msg_hash = $xml->XMLin($in_msg, ForceArray=>1);
1947 my $target = &get_content_from_xml_hash($in_msg_hash, 'target');
1949 if ($target eq $server_address) {
1950 print "arp_activ: forward to server\n";
1951 my $arp_cipher = &create_ciphering($act_passwd);
1952 my $crypted_msg = &encrypt_msg($in_msg, $arp_cipher);
1953 &activating_child($crypted_msg, $server_ip);
1954 } else {
1955 print "arp_activ: send to bus\n";
1956 &send_msg_hash2address($in_msg_hash, $bus_address);
1957 }
1958 print "\n";
1959 }
1961 if($gosa_activ eq "on" && vec($rout, fileno $gosa_server, 1)) {
1962 daemon_log(" ", 1);
1963 my $client = $gosa_server->accept();
1964 my $other_end = getpeername($client);
1965 if(not defined $other_end) {
1966 daemon_log("client cannot be identified: $!");
1967 } else {
1968 my ($port, $iaddr) = unpack_sockaddr_in($other_end);
1969 my $actual_ip = inet_ntoa($iaddr);
1970 daemon_log("accept client at gosa socket from $actual_ip", 5);
1971 my $in_msg = <$client>;
1972 #my $in_msg = &read_from_socket($client);
1974 daemon_log(">>>>>>>>>>> frisch vom socket gelesen\n!$in_msg!\n",1);
1975 if(defined $in_msg){
1976 chomp($in_msg);
1977 &activating_child($in_msg, $actual_ip, $client);
1978 } else {
1979 daemon_log("cannot read from $actual_ip", 5);
1980 }
1981 }
1982 #close($client);
1983 }
1985 # check all processing childs whether they are finished ('done') or
1986 while ( my ($pid, $child_hash) = each %busy_child ) {
1987 my $fhd = $$child_hash{'pipe_rd'};
1989 if (vec($rout, fileno $fhd, 1) ) {
1990 daemon_log("process child $pid is ready to read", 5);
1992 $fhd->blocking(1);
1993 my $in_msg = <$fhd>;
1994 $fhd->blocking(0);
1995 my $part_in_msg;
1996 while ($part_in_msg = <$fhd>) {
1997 if (not defined $part_in_msg) {
1998 last;
1999 }
2000 $in_msg .= $part_in_msg;
2001 }
2002 chomp($in_msg);
2004 daemon_log("process child read: $in_msg", 5);
2005 if (not defined $in_msg) {
2006 next;
2007 } elsif ($in_msg =~ "done") {
2008 delete $busy_child{$pid};
2009 $free_child{$pid} = $child_hash;
2011 } else {
2012 my $act_client = $busy_child{$pid}{client_ref};
2013 print $act_client $in_msg."\n";
2014 my $act_pipe = $busy_child{$pid}{pipe_rd};
2015 sleep(10);
2016 close ($act_client);
2017 delete $busy_child{$pid};
2018 $free_child{$pid} = $child_hash;
2020 }
2021 }
2022 }
2025 }