1 #!/usr/bin/perl
2 #===============================================================================
3 #
4 # FILE: gosa-server
5 #
6 # USAGE: ./gosa-server
7 #
8 # DESCRIPTION:
9 #
10 # OPTIONS: ---
11 # REQUIREMENTS: ---
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 #===============================================================================
21 use strict;
22 use warnings;
23 use Getopt::Long;
24 use Config::IniFiles;
25 use POSIX;
26 use Time::HiRes qw( gettimeofday );
28 use IO::Socket::INET;
29 use Crypt::Rijndael;
30 use MIME::Base64;
31 use Digest::MD5 qw(md5 md5_hex md5_base64);
32 use XML::Simple;
33 use Data::Dumper;
34 use Sys::Syslog qw( :DEFAULT setlogsock);
35 use Cwd;
36 use File::Spec;
37 use GOSA::GosaSupportDaemon;
38 use GOSA::DBsqlite;
39 #use IPC::Shareable qw( :lock);
40 #IPC::Shareable->clean_up_all;
42 my ($cfg_file, $default_cfg_file, %cfg_defaults, $foreground, $verbose);
43 my ($bus_activ, $bus_passwd, $bus_ip, $bus_port, $bus_address, $bus, $bus_mac_address);
44 my ($pid_file, $procid, $pid, $log_file, $my_own_address);
45 my (%free_child, %busy_child, $child_max, $child_min, %child_alive_time, $child_timeout);
46 my ($bus_known_server_db, $bus_known_server_file_name);
47 my ($xml, $bus_cipher);
49 $foreground = 0 ;
51 %cfg_defaults =
52 ("general" =>
53 {"log_file" => [\$log_file, "/var/run/".$0.".log"],
54 "pid_file" => [\$pid_file, "/var/run/".$0.".pid"],
55 "child_max" => [\$child_max, 10],
56 "child_min" => [\$child_min, 3],
57 "child_timeout" => [\$child_timeout, 180],
58 "bus_known_server_file_name" => [\$bus_known_server_file_name, "/var/lib/gosa-si/bus_known_server.db"]
59 },
60 "bus" =>
61 {"bus_activ" => [\$bus_activ, "on"],
62 "bus_passwd" => [\$bus_passwd, ""],
63 "bus_port" => [\$bus_port, "20080"],
64 }
65 );
67 #=== FUNCTION ================================================================
68 # NAME: read_configfile
69 # PARAMETERS: cfg_file - string -
70 # RETURNS: nothing
71 # DESCRIPTION: read cfg_file and set variables
72 #===============================================================================
73 sub read_configfile {
74 my $cfg;
75 if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
76 if( -r $cfg_file ) {
77 $cfg = Config::IniFiles->new( -file => $cfg_file );
78 } else {
79 print STDERR "Couldn't read config file!";
80 }
81 } else {
82 $cfg = Config::IniFiles->new() ;
83 }
84 foreach my $section (keys %cfg_defaults) {
85 foreach my $param (keys %{$cfg_defaults{ $section }}) {
86 my $pinfo = $cfg_defaults{ $section }{ $param };
87 ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
88 }
89 }
90 }
92 #=== FUNCTION ================================================================
93 # NAME: logging
94 # PARAMETERS: level - string - default 'info'
95 # msg - string -
96 # facility - string - default 'LOG_DAEMON'
97 # RETURNS: nothing
98 # DESCRIPTION: function for logging
99 #===============================================================================
100 sub daemon_log {
101 my( $msg, $level ) = @_;
102 if(not defined $msg) { return }
103 if(not defined $level) { $level = 1 }
104 if(defined $log_file){
105 open(LOG_HANDLE, ">>$log_file");
106 if(not defined open( LOG_HANDLE, ">>$log_file" )) {
107 print STDERR "cannot open $log_file: $!";
108 return }
109 chomp($msg);
110 if($level <= $verbose){
111 print LOG_HANDLE $msg."\n";
112 if(defined $foreground) { print $msg."\n" }
113 }
114 }
115 close( LOG_HANDLE );
116 # my ($msg, $level, $facility) = @_;
117 # if(not defined $msg) {return}
118 # if(not defined $level) {$level = "info"}
119 # if(not defined $facility) {$facility = "LOG_DAEMON"}
120 # openlog($0, "pid,cons,", $facility);
121 # syslog($level, $msg);
122 # closelog;
123 # return;
124 }
126 #=== FUNCTION ================================================================
127 # NAME: check_cmdline_param
128 # PARAMETERS: nothing
129 # RETURNS: nothing
130 # DESCRIPTION: validates commandline parameter
131 #===============================================================================
132 sub check_cmdline_param () {
133 my $err_config;
134 my $err_counter = 0;
135 if( not defined( $cfg_file)) {
136 my $cwd = getcwd;
137 my $name = "/etc/gosa-si/bus.conf";
138 $cfg_file = File::Spec->catfile( $cwd, $name );
139 }
140 if( $err_counter > 0 ) {
141 &usage( "", 1 );
142 if( defined( $err_config)) { print STDERR "$err_config\n"}
143 print STDERR "\n";
144 exit( -1 );
145 }
146 }
148 #=== FUNCTION ================================================================
149 # NAME: check_pid
150 # PARAMETERS: nothing
151 # RETURNS: nothing
152 # DESCRIPTION: handels pid processing
153 #===============================================================================
154 sub check_pid {
155 $pid = -1;
156 # Check, if we are already running
157 if( open(LOCK_FILE, "<$pid_file") ) {
158 $pid = <LOCK_FILE>;
159 if( defined $pid ) {
160 chomp( $pid );
161 if( -f "/proc/$pid/stat" ) {
162 my($stat) = `cat /proc/$pid/stat` =~ m/$pid \((.+)\).*/;
163 if( $0 eq $stat ) {
164 close( LOCK_FILE );
165 exit -1;
166 }
167 }
168 }
169 close( LOCK_FILE );
170 unlink( $pid_file );
171 }
173 # create a syslog msg if it is not to possible to open PID file
174 if (not sysopen(LOCK_FILE, $pid_file, O_WRONLY|O_CREAT|O_EXCL, 0644)) {
175 my($msg) = "Couldn't obtain lockfile '$pid_file' ";
176 if (open(LOCK_FILE, '<', $pid_file)
177 && ($pid = <LOCK_FILE>))
178 {
179 chomp($pid);
180 $msg .= "(PID $pid)\n";
181 } else {
182 $msg .= "(unable to read PID)\n";
183 }
184 if( ! ($foreground) ) {
185 openlog( $0, "cons,pid", "daemon" );
186 syslog( "warning", $msg );
187 closelog();
188 }
189 else {
190 print( STDERR " $msg " );
191 }
192 exit( -1 );
193 }
194 }
197 #=== FUNCTION ================================================================
198 # NAME: usage
199 # PARAMETERS: nothing
200 # RETURNS: nothing
201 # DESCRIPTION: print out usage text to STDERR
202 #===============================================================================
203 sub usage {
204 print STDERR << "EOF" ;
205 usage: $0 [-hvf] [-c config]
207 -h : this (help) message
208 -c <file> : config file
209 -f : foreground, process will not be forked to background
210 -v : be verbose (multiple to increase verbosity)
211 EOF
212 print "\n" ;
213 }
216 #=== FUNCTION ================================================================
217 # NAME: sig_int_handler
218 # PARAMETERS: signal - string - signal arose from system
219 # RETURNS: noting
220 # DESCRIPTION: handels tasks to be done befor signal becomes active
221 #===============================================================================
222 sub sig_int_handler {
223 my ($signal) = @_;
224 if($bus){
225 close($bus);
226 print "$bus closed\n";
227 }
228 print "$signal\n";
229 exit(1);
230 }
231 $SIG{INT} = \&sig_int_handler;
234 #=== FUNCTION ================================================================
235 # NAME: get_ip_and_mac
236 # PARAMETERS: nothing
237 # RETURNS: (ip, mac)
238 # DESCRIPTION: executes /sbin/ifconfig and parses the output, the first occurence
239 # of a inet address is returned as well as the mac address in the line
240 # above the inet address
241 #===============================================================================
242 sub get_ip_and_mac {
243 my $ip = "0.0.0.0.0"; # Defualt-IP
244 my $mac_address = "00:00:00:00:00:00"; # Default-MAC
245 my @ifconfig = qx(/sbin/ifconfig);
246 foreach(@ifconfig) {
247 if (/Hardware Adresse (\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2}):(\S{2})/) {
248 $mac_address = "$1:$2:$3:$4:$5:$6";
249 next;
250 }
251 if (/inet Adresse:(\d+).(\d+).(\d+).(\d+)/) {
252 $ip = "$1.$2.$3.$4";
253 last;
254 }
255 }
256 return ($ip, $mac_address);
257 }
261 #=== FUNCTION ================================================================
262 # NAME: activating_child
263 # PARAMETERS: msg - string - incoming message
264 # host - string - host from which the incomming message comes
265 # RETURNS: nothing
266 # DESCRIPTION: handels the distribution of incoming messages to working childs
267 #===============================================================================
268 sub activating_child {
269 my ($msg, $host) = @_;
270 my $child = &get_processing_child();
271 my $pipe_wr = $$child{'pipe_wr'};
272 daemon_log("activating: childpid: $$child{'pid'}", 5);
273 print $pipe_wr $msg.".".$host."\n";
274 return;
275 }
278 #=== FUNCTION ================================================================
279 # NAME: get_processing_child
280 # PARAMETERS: nothing
281 # RETURNS: child - hash - holding the process id and the references to the pipe
282 # handles pipe_wr and pipe_rd
283 # DESCRIPTION: handels the forking, reactivating and keeping alive tasks
284 #===============================================================================
285 sub get_processing_child {
286 my $child;
287 # checking %busy_child{pipe_wr} if msg is 'done', then set child from busy to free
288 while(my ($key, $val) = each(%busy_child)) {
289 # check wether process still exists
290 my $exitus_pid = waitpid($key, WNOHANG);
291 if($exitus_pid != 0) {
292 delete $busy_child{$key};
293 daemon_log( "prozess:$key wurde aus busy_child entfernt\n", 5);
294 next;
295 }
297 # check wether process sitll works
298 my $fh = $$val{'pipe_rd'};
299 $fh->blocking(0);
300 my $child_answer;
301 if(not $child_answer = <$fh>) { next }
302 chomp($child_answer);
303 if($child_answer eq "done") {
304 delete $busy_child{$key};
305 $free_child{$key} = $val;
306 }
307 }
309 while(my ($key, $val) = each(%free_child)) {
310 my $exitus_pid = waitpid($key, WNOHANG);
311 if($exitus_pid != 0) {
312 delete $free_child{$key};
313 daemon_log( "prozess:$key wurde aus free_child entfernt\n", 5);
314 }
315 daemon_log("free child:$key\n", 5);
316 }
317 # check @free_child and @busy_child
318 my $free_len = scalar(keys(%free_child));
319 my $busy_len = scalar(keys(%busy_child));
320 daemon_log("free children $free_len, busy children $busy_len\n",5);
322 # if there is a free child, let the child work
323 if($free_len > 0){
324 my @keys = keys(%free_child);
325 $child = $free_child{$keys[0]};
326 if(defined $child) {
327 $busy_child{$$child{'pid'}} = $child ;
328 delete $free_child{$$child{'pid'}};
329 }
330 return $child;
331 }
333 # no free child, try to fork another one
334 if($free_len + $busy_len < $child_max) {
336 daemon_log("not enough children, create a new one\n",5);
338 # New pipes for communication
339 my( $PARENT_wr, $PARENT_rd );
340 my( $CHILD_wr, $CHILD_rd );
341 pipe( $CHILD_rd, $PARENT_wr );
342 pipe( $PARENT_rd, $CHILD_wr );
343 $PARENT_wr->autoflush(1);
344 $CHILD_wr->autoflush(1);
346 ############
347 # fork child
348 ############
349 my $child_pid = fork();
351 #CHILD
352 if($child_pid == 0) {
353 # Close unused pipes
354 close( $CHILD_rd );
355 close( $CHILD_wr );
356 while( 1 ) {
357 my $rbits = "";
358 vec( $rbits, fileno $PARENT_rd , 1 ) = 1;
360 # waiting child_timeout for jobs to do
361 my $nf = select($rbits, undef, undef, $child_timeout);
362 if($nf < 0 ) {
363 # if $nf < 1, error handling
364 die "select(): $!\n";
365 } elsif (! $nf) {
366 # if already child_min childs are alive, then leave loop
367 $free_len = scalar(keys(%free_child));
368 $busy_len = scalar(keys(%busy_child));
369 if($free_len + $busy_len >= $child_min) {
370 last;
371 } else {
372 redo;
373 }
374 }
376 # a job for a child arise
377 if ( vec $rbits, fileno $PARENT_rd, 1 ) {
378 # read everything from pipe
379 my $msg = "";
380 $PARENT_rd->blocking(0);
381 while(1) {
382 my $read = <$PARENT_rd>;
383 if(not defined $read) { last}
384 $msg .= $read;
385 }
387 # forward the job msg to another function
388 &process_incoming_msg($msg);
389 daemon_log("processing of msg finished", 5);
391 # important!!! wait until child says 'done', until then child is set from busy to free
392 print $PARENT_wr "done";
393 redo;
394 }
395 }
396 # childs leaving the loop are allowed to die
397 exit(0);
399 #PARENT
400 } else {
401 # Close unused pipes
402 close( $PARENT_rd );
403 close( $PARENT_wr );
404 # add child to child alive hash
405 my %child_hash = (
406 'pid' => $child_pid,
407 'pipe_wr' => $CHILD_wr,
408 'pipe_rd' => $CHILD_rd,
409 );
411 $child = \%child_hash;
412 $busy_child{$$child{'pid'}} = $child;
413 return $child;
414 }
415 }
416 }
419 #=== FUNCTION ================================================================
420 # NAME: process_incoming_msg
421 # PARAMETERS: crypted_msg - string - incoming crypted message
422 # RETURNS: nothing
423 # DESCRIPTION: handels the proceeded distribution to the appropriated functions
424 #===============================================================================
425 sub process_incoming_msg {
426 my ($crypted_msg) = @_;
427 if(not defined $crypted_msg) {
428 daemon_log("function 'process_incoming_msg': got no msg", 7);
429 return;
430 }
431 $crypted_msg =~ /^([\s\S]*?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)\.(\d{1,3}?)$/;
432 $crypted_msg = $1;
433 my $host = sprintf("%s.%s.%s.%s", $2, $3, $4, $5);
435 my $msg;
436 my $msg_hash;
437 my $host_name;
438 my $host_key;
440 # check wether incoming msg is a new msg
441 $host_name = $bus_address;
442 $host_key = $bus_passwd;
443 daemon_log("process_incoming_msg: host_name: $host_name", 7);
444 daemon_log("process_incoming_msg: host_key: $host_key", 7);
445 eval{
446 my $key_cipher = &create_ciphering($host_key);
447 $msg = &decrypt_msg($crypted_msg, $key_cipher);
448 $msg_hash = &transform_msg2hash($msg);
449 };
450 if($@) {
451 daemon_log("process_incoming_msg: deciphering raise error", 7);
452 daemon_log("$@", 8);
453 $msg = undef;
454 $msg_hash = undef;
455 $host_name = undef;
456 $host_key = undef;
457 }
459 # check wether incoming msg is from a bus_known_server
460 if( not defined $msg ) {
461 my $query_res = $bus_known_server_db->select_dbentry( {table=>'bus_known_server'} );
462 while( my ($hit_num, $hit) = each %{ $query_res } ) {
463 $host_name = $hit->{hostname};
464 if( not $host_name =~ "^$host") {
465 next;
466 }
467 $host_key = $hit->{hostkey};
468 daemon_log("process_incoming_msg: host_name: $host_name", 7);
469 daemon_log("process_incoming_msg: host_key: $host_key", 7);
470 eval{
471 my $key_cipher = &create_ciphering($host_key);
472 $msg = &decrypt_msg($crypted_msg, $key_cipher);
473 $msg_hash = &transform_msg2hash($msg);
474 };
475 if($@) {
476 daemon_log("process_incoming_msg: deciphering raise error", 7);
477 daemon_log("$@", 8);
478 $msg = undef;
479 $msg_hash = undef;
480 $host_name = undef;
481 $host_key = undef;
482 } else {
483 last;
484 }
485 }
486 }
488 if( not defined $msg ) {
489 daemon_log("WARNING: bus does not understand the message:", 5);
490 return;
491 }
493 # process incoming msg
494 my $header = @{$msg_hash->{header}}[0];
495 my $source = @{$msg_hash->{source}}[0];
497 daemon_log("header from msg: $header", 1);
498 daemon_log("msg to process:", 5);
499 daemon_log($msg, 5);
501 my @targets = @{$msg_hash->{target}};
502 my $len_targets = @targets;
504 if ($len_targets == 0){
505 daemon_log("ERROR: no target specified for msg $header", 1);
507 } elsif ($len_targets == 1){
508 # we have only one target symbol
509 my $target = $targets[0];
510 daemon_log("msg is for: $target", 7);
512 if($target eq $bus_address) {
513 # msg is for bus
514 if($header eq 'here_i_am'){ &here_i_am($msg_hash)}
515 elsif($header eq 'confirm_new_passwd'){ &confirm_new_passwd($msg_hash)}
516 elsif($header eq 'got_ping') { &got_ping($msg_hash)}
517 elsif($header eq 'ping') { &ping($msg_hash)}
518 elsif($header eq 'who_has') { &who_has($msg_hash)}
519 elsif($header eq 'new_client') { &new_client($msg_hash)}
520 elsif($header eq 'delete_client') { &delete_client($msg_hash)}
522 } elsif ($target eq "*"){
523 # msg is for all server
524 my $query_res = $bus_known_server_db->select_dbentry( {table=>'known_server'} );
525 while( my ($hit_num, $hit) = each %{ $query_res } ) {
526 $host_name = $hit->{hostname};
527 $host_key = $hit->{hostkey};
528 $msg_hash->{target} = [$host_name];
529 &send_msg_hash2address($msg_hash, $host_name, $host_key);
530 }
531 return;
532 }
534 } else {
535 # a list of targets is specified
536 my $target_address;
537 foreach $target_address (@targets) {
539 my $query_res = $bus_known_server_db->select_dbentry( {table=>'known_server', hostname=>$target_address} );
540 if( 1 == keys %{$query_res} ) {
541 $host_key = $query_res->{1}->{hostkey};
542 &send_msg_hash2address($msg_hash, $target_address, $host_key);
543 next;
545 } else {
546 $query_res = $bus_known_server_db->select_dbentry( {table=>'known_server'} );
547 while( my ($hit_num, $hit) = each %{$query_res} ) {
548 my $host_name = $hit->{hostname};
549 my $host_key = $hit->{hostkey};
550 my $clients = $hit->{clients};
551 my @clients = split(/,/, $clients);
552 foreach my $client (@clients) {
553 if( not $client eq $target_address ) {
554 next;
555 }
556 $msg_hash->{target} = [ $target_address ];
557 &send_msg_hash2address($msg_hash, $host_name, $host_key);
558 daemon_log("bus forwards msg $header for client $target_address to server $host_name", 3);
559 last;
560 }
561 }
562 }
563 }
564 }
566 return;
567 }
570 #=== FUNCTION ================================================================
571 # NAME: get_content_of_known_daemons
572 # PARAMETERS:
573 # RETURNS:
574 # DESCRIPTION:
575 #===============================================================================
576 #sub get_content_of_known_daemons {
577 # my ($host, $content) = @_;
578 # return;
579 #}
582 #=== FUNCTION ================================================================
583 # NAME: create_passwd
584 # PARAMETERS: nothing
585 # RETURNS: new_passwd - string
586 # DESCRIPTION: creates a 32 bit long random passwd out of "a".."z","A".."Z",0..9
587 #===============================================================================
588 sub create_passwd {
589 my $new_passwd = "";
590 for(my $i=0; $i<31; $i++) {
591 $new_passwd .= ("a".."z","A".."Z",0..9)[int(rand(62))]
592 }
593 return $new_passwd;
594 }
597 #=== FUNCTION ================================================================
598 # NAME: create_ciphering
599 # PARAMETERS: passwd - string - used to create ciphering
600 # RETURNS: cipher - object
601 # DESCRIPTION: creates a Crypt::Rijndael::MODE_CBC object with passwd as key
602 #===============================================================================
603 #sub create_ciphering {
604 # my ($passwd) = @_;
605 # $passwd = substr(md5_hex("$passwd") x 32, 0, 32);
606 # my $iv = substr(md5_hex('GONICUS GmbH'),0, 16);
607 #
608 # my $my_cipher = Crypt::Rijndael->new($passwd , Crypt::Rijndael::MODE_CBC());
609 # $my_cipher->set_iv($iv);
610 # return $my_cipher;
611 #}
614 #=== FUNCTION ================================================================
615 # NAME: encrypt_msg
616 # PARAMETERS: msg - string - message to encrypt
617 # my_cipher - ref - reference to a Crypt::Rijndael object
618 # RETURNS: crypted_msg - string - crypted message
619 # DESCRIPTION: crypts the incoming message with the Crypt::Rijndael module
620 #===============================================================================
621 #sub encrypt_msg {
622 # my ($msg, $my_cipher) = @_;
623 # if(not defined $my_cipher) { print "no cipher object\n"; }
624 # $msg = "\0"x(16-length($msg)%16).$msg;
625 # my $crypted_msg = $my_cipher->encrypt($msg);
626 # chomp($crypted_msg = &encode_base64($crypted_msg));
627 # return $crypted_msg;
628 #}
631 #=== FUNCTION ================================================================
632 # NAME: decrypt_msg
633 # PARAMETERS: crypted_msg - string - message to decrypt
634 # my_cipher - ref - reference to a Crypt::Rijndael object
635 # RETURNS: msg - string - decrypted message
636 # DESCRIPTION: decrypts the incoming message with the Crypt::Rijndael module
637 #===============================================================================
638 #sub decrypt_msg {
639 # my ($crypted_msg, $my_cipher) = @_ ;
640 # $crypted_msg = &decode_base64($crypted_msg);
641 # my $msg = $my_cipher->decrypt($crypted_msg);
642 # $msg =~ s/^\0*//g;
643 # return $msg;
644 #}
647 #=== FUNCTION ================================================================
648 # NAME: create_xml_hash
649 # PARAMETERS: header - string - message header (required)
650 # source - string - where the message come from (required)
651 # target - string - where the message should go to (required)
652 # [header_value] - string - something usefull (optional)
653 # RETURNS: hash - hash - nomen est omen
654 # DESCRIPTION: creates a key-value hash, all values are stored in a array
655 #===============================================================================
656 #sub create_xml_hash {
657 # my ($header, $source, $target, $header_value) = @_ ;
658 #
659 # if (not defined $header || not defined $source || not defined $target) {
660 # daemon_log("ERROR: create_xml_hash function is invoked with uncompleted parameters", 7);
661 # }
662 #
663 # my $hash = {
664 # header => [$header],
665 # source => [$source],
666 # target => [$target],
667 # $header => [$header_value],
668 # };
669 # #daemon_log("create_xml_hash:", 7),
670 # #chomp(my $tmp = Dumper $hash);
671 # #daemon_log("\t$tmp\n", 7);
672 # return $hash
673 #}
676 #=== FUNCTION ================================================================
677 # NAME: create_xml_string
678 # PARAMETERS: xml_hash - hash - hash from function create_xml_hash
679 # RETURNS: xml_string - string - xml string representation of the hash
680 # DESCRIPTION: transform the hash to a string using XML::Simple module
681 #===============================================================================
682 #sub create_xml_string {
683 # my ($xml_hash) = @_ ;
684 # my $xml_string = $xml->XMLout($xml_hash, RootName => 'xml');
685 # #$xml_string =~ s/[\n]+//g;
686 # return $xml_string;
687 #}
690 #=== FUNCTION ================================================================
691 # NAME: add_content2xml_hash
692 # PARAMETERS: xml_ref - ref - reference to a hash from function create_xml_hash
693 # element - string - key for the hash
694 # content - string - value for the hash
695 # RETURNS: nothing
696 # DESCRIPTION: add key-value pair to xml_ref, if key alread exists, then append value to list
697 #===============================================================================
698 #sub add_content2xml_hash {
699 # my ($xml_ref, $element, $content) = @_;
700 # if(not exists $$xml_ref{$element} ) {
701 # $$xml_ref{$element} = [];
702 # }
703 # my $tmp = $$xml_ref{$element};
704 # push(@$tmp, $content);
705 # return;
706 #}
709 #=== FUNCTION ================================================================
710 # NAME: get_content_from_xml_hash
711 # PARAMETERS: xml_ref - ref - reference of the xml hash
712 # element - string - key of the value you want
713 # RETURNS: value - string - if key is either header, target or source
714 # value - list - for all other keys in xml hash
715 # DESCRIPTION:
716 #===============================================================================
717 #sub get_content_from_xml_hash {
718 # my ($xml_ref, $element) = @_;
719 # my $result = $xml_ref->{$element};
720 # if( $element eq "header" || $element eq "target" || $element eq "source") {
721 # return @$result[0];
722 # }
723 # return @$result;
724 #}
727 #=== FUNCTION ================================================================
728 # NAME: open_socket
729 # PARAMETERS: PeerAddr - string - something like 192.168.1.1 or 192.168.1.1:10000
730 # [PeerPort] - string - necessary if port not appended by PeerAddr
731 # RETURNS: socket - IO::Socket::INET
732 # DESCRIPTION: open a socket to PeerAddr
733 #===============================================================================
734 #sub open_socket {
735 # my ($PeerAddr, $PeerPort) = @_ ;
736 # if(defined($PeerPort)){
737 # $PeerAddr = $PeerAddr.":".$PeerPort;
738 # }
739 # my $socket;
740 # $socket = new IO::Socket::INET(PeerAddr => $PeerAddr ,
741 # Porto => "tcp" ,
742 # Type => SOCK_STREAM,
743 # Reuse => 1,
744 # Timeout => 5,
745 # );
746 # if(not defined $socket) {
747 # return;
748 # }
749 # return $socket;
750 #}
753 #=== FUNCTION ================================================================
754 # NAME: read_from_socket
755 # PARAMETERS: socket - fh - filehandel to read from
756 # RETURNS: result - string - readed characters from socket
757 # DESCRIPTION: reads data from socket in 16 byte steps
758 #===============================================================================
759 sub read_from_socket {
760 my ($socket) = @_;
762 $socket->blocking(1);
763 my $result = <$socket>;
764 $socket->blocking(0);
765 my $part_msg;
766 while ($part_msg = <$socket>) {
767 if (not defined $part_msg) { last; }
768 $result .= $part_msg;
769 }
771 #my $result = "";
772 #my $len = 16;
773 #while($len == 16){
774 # my $char;
775 # $len = sysread($socket, $char, 16);
776 # if($len != 16) { last }
777 # if($len != 16) { last }
778 # $result .= $char;
779 #}
780 return $result;
781 }
784 #=== FUNCTION ================================================================
785 # NAME: send_msg_hash2address
786 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
787 # PeerAddr string - socket address to send msg
788 # PeerPort string - socket port, if not included in socket address
789 # RETURNS: nothing
790 # DESCRIPTION: ????
791 #===============================================================================
792 #sub send_msg_hash2address {
793 # my ($msg_hash, $address) = @_ ;
794 #
795 # # fetch header for logging
796 # my $header = &get_content_from_xml_hash($msg_hash, "header");
797 #
798 # # generate xml string
799 # my $msg_xml = &create_xml_string($msg_hash);
800 #
801 # # fetch the appropriated passwd from hash
802 # my $passwd = $known_daemons->{$address}->{passwd};
803 #
804 # # create a ciphering object
805 # my $act_cipher = &create_ciphering($passwd);
806 #
807 # # encrypt xml msg
808 # my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
809 #
810 # # open socket
811 # my $socket = &open_socket($address);
812 # if(not defined $socket){
813 # daemon_log("ERROR: cannot send '$header'-msg to $address , server not reachable", 1);
814 # return;
815 # }
816 #
817 # # send xml msg
818 # print $socket $crypted_msg."\n";
819 #
820 # close $socket;
821 # daemon_log("send '$header'-msg to $address", 5);
822 # daemon_log("crypted_msg:\n\t$crypted_msg", 7);
823 # return;
824 #}
827 #=== FUNCTION ================================================================
828 # NAME: send_msg_hash2all
829 # PARAMETERS: msg_hash - hash - xml_hash created with function create_xml_hash
830 # RETURNS: nothing
831 # DESCRIPTION: send msg_hash to all registered daemons
832 #===============================================================================
833 #sub send_msg_hash2all {
834 # my ($msg_hash) = @_;
835 #
836 # # fetch header for logging
837 # my $header = &get_content_from_xml_hash($msg_hash, "header");
838 #
839 # # generate xml string
840 # my $msg_xml = &create_xml_string($msg_hash);
841 #
842 # # fetch a list of all target addresses
843 # my @targets = keys(%$known_daemons);
844 #
845 # # itterates through the list an send each the msg
846 # foreach my $target (@targets) {
847 # if($target eq $bus_address) {next}; # do not send msg to bus
848 #
849 # # fetch the appropriated passwd
850 # my $passwd = $known_daemons->{$target}->{passwd};
851 #
852 # # create ciphering object
853 # my $act_cipher = &create_ciphering($passwd);
854 #
855 # # encrypt xml msg
856 # my $crypted_msg = &encrypt_msg($msg_xml, $act_cipher);
857 #
858 # # open socket
859 # my $socket = &open_socket($target);
860 # if(not defined $socket){
861 # daemon_log("ERROR: cannot open socket to $target , server not reachable", 1);
862 # &update_known_daemons_entry(hostname=>$target, status=>"down");
863 # next;
864 # }
865 #
866 # # send xml msg
867 # print $socket $crypted_msg."\n";
868 #
869 # close $socket;
870 # daemon_log("send '$header'-msg to $target", 5);
871 # daemon_log("crypted_msg:\n\t$crypted_msg", 7);
872 # }
873 # return;
874 #}
877 #=== FUNCTION ================================================================
878 # NAME: here_i_am
879 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
880 # RETURNS: nothing
881 # DESCRIPTION: process the incoming msg 'here_i_am'
882 #===============================================================================
883 sub here_i_am {
884 my ($msg_hash) = @_ ;
885 my $source = @{$msg_hash->{source}}[0];;
887 my $new_key = &create_passwd();
889 # create bus_known_server entry
890 my $add_hash = {
891 table=>"bus_known_server",
892 primkey=>"hostname",
893 hostname=>$source,
894 status=>"registered",
895 hostkey=>$bus_passwd,
896 clients=>"",
897 };
898 $bus_known_server_db->add_dbentry($add_hash);
900 # create outgoing msg
901 my $out_hash = &create_xml_hash("new_passwd", $bus_address, $source, $new_key);
902 &send_msg_hash2address($out_hash, $source, $bus_passwd);
904 # change hostkey, reason
905 my $update_hash = { table=>'bus_known_server' };
906 $update_hash->{where} = [ { hostname=>[$source] } ];
907 $update_hash->{update} = [ { hostkey=>[$new_key] } ];
908 $bus_known_server_db->update_dbentry($update_hash);
910 return;
911 }
914 #=== FUNCTION ================================================================
915 # NAME: confirm_new_passwd
916 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
917 # RETURNS: nothing
918 # DESCRIPTION: process this incoming message
919 #===============================================================================
920 sub confirm_new_passwd {
921 my ($msg_hash) = @_ ;
922 my $source = @{$msg_hash->{source}}[0];
924 my $update_hash = { table=>'bus_known_server' };
925 $update_hash->{where} = [ { hostname=>[$source] } ];
926 $update_hash->{update} = [ { status=>['key_confirmed'] } ];
927 $bus_known_server_db->update_dbentry($update_hash);
929 return;
930 }
933 #=== FUNCTION ================================================================
934 # NAME: ping
935 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
936 # RETURNS: nothing
937 # DESCRIPTION: process this incoming message
938 #===============================================================================
939 sub ping {
940 my ($msg_hash) = @_ ;
941 my $header = @{$msg_hash->{header}}[0];
942 my $source = @{$msg_hash->{source}}[0];
944 my $update_hash = { table=>'bus_known_server',
945 where=> [ { hostname=>[$source] } ],
946 update=> [ { status=>$header } ],
947 };
948 $bus_known_server_db->update_dbentry($update_hash);
950 my $out_hash = &create_xml_hash("got_ping", $bus_address, $source);
952 my $res = $bus_known_server_db->select_dbentry( { table=>'bus_known_server', hostname=>$source } );
953 my $hostkey = $res->{1}->{hostkey};
954 &send_msg_hash2address($out_hash, $source, $hostkey);
956 return;
957 }
960 #=== FUNCTION ================================================================
961 # NAME: make ping
962 # PARAMETERS: address - string - address which should be pinged
963 # RETURNS: nothing
964 # DESCRIPTION: send ping message to address
965 #===============================================================================
966 #sub make_ping {
967 # my ($address) = @_;
968 # daemon_log("ping:$address\n", 1);
969 # my $out_hash = &create_xml_hash("ping", "$bus_ip:$bus_port", $address);
970 # &send_msg_hash2address($out_hash, $address);
971 # return;
972 #}
975 #=== FUNCTION ================================================================
976 # NAME: got_ping
977 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
978 # RETURNS: nothing
979 # DESCRIPTION: process this incoming message
980 #===============================================================================
981 sub got_ping {
982 my ($msg_hash) = @_;
983 my $source = @{$msg_hash->{source}}[0];
985 my $update_hash = { table=>'bus_known_server',
986 where=> [ { hostname=>[$source] } ],
987 update=> [ { status=>'got_ping' } ],
988 };
989 $bus_known_server_db->update_dbentry($update_hash);
991 return;
992 }
995 #=== FUNCTION ================================================================
996 # NAME: new_client
997 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
998 # RETURNS: nothing
999 # DESCRIPTION: process this incoming message
1000 #===============================================================================
1001 sub new_client {
1002 my ($msg_hash) = @_ ;
1003 my $source = @{$msg_hash->{source}}[0];
1004 my $header = @{$msg_hash->{header}}[0];
1005 my $new_client = @{$msg_hash->{$header}}[0];
1007 my $res = $bus_known_server_db->select_dbentry( { table=>'bus_known_server', hostname=>$source } );
1008 my $clients = $res->{1}->{clients};
1010 # if host has alread more clients, than just append
1011 if( length($clients) != 0 ) {
1012 $clients .= ",$new_client";
1013 } else {
1014 $clients = $new_client;
1015 }
1017 my $update_hash = { table=>'bus_known_server',
1018 where=>[ {hostname=>[$source] } ],
1019 update=>[ {clients=>[$clients] } ],
1020 };
1022 $bus_known_server_db->update_dbentry( $update_hash );
1023 return;
1024 }
1027 #=== FUNCTION ================================================================
1028 # NAME: delete_client
1029 # PARAMETERS: msg_hash - hash - hash from function create_xml_hash
1030 # RETURNS: nothing
1031 # DESCRIPTION: process this incoming message
1032 #===============================================================================
1033 #sub delete_client {
1034 # my ($msg_hash) = @_ ;
1035 # my $source = &get_content_from_xml_hash($msg_hash, "source");
1036 # my $header = &get_content_from_xml_hash($msg_hash, "header");
1037 # my $del_client = (&get_content_from_xml_hash($msg_hash, $header))[0];
1038 #
1039 # if (not exists $known_daemons->{$source}->{$del_client}) {
1040 # daemon_log
1041 # }
1042 # delete $known_daemons->{$source}->{$del_client};
1043 #
1044 # return;
1045 #}
1048 #=== FUNCTION ================================================================
1049 # NAME: print_known_daemons_hash
1050 # PARAMETERS: nothing
1051 # RETURNS: nothing
1052 # DESCRIPTION: nome est omen
1053 #===============================================================================
1054 #sub print_known_daemons_hash {
1055 # my ($tmp) = @_;
1056 # print "####################################\n";
1057 # print "# status of known_daemons\n";
1058 # my $hosts;
1059 # my $host_hash;
1060 # $shmkh->shlock(LOCK_EX);
1061 # my @hosts = keys %$known_daemons;
1062 # foreach my $host (@hosts) {
1063 # my $status = $known_daemons->{$host}->{status} ;
1064 # my $passwd = $known_daemons->{$host}->{passwd};
1065 # my $timestamp = $known_daemons->{$host}->{timestamp};
1066 # my @clients = keys %{$known_daemons->{$host}->{clients}};
1067 # my $client_string = join(", ", @clients);
1068 # print "$host\n";
1069 # print "\tstatus: $status\n";
1070 # print "\tpasswd: $passwd\n";
1071 # print "\ttimestamp: $timestamp\n";
1072 # print "\tclients: $client_string\n";
1073 #
1074 # }
1075 # $shmkh->shunlock(LOCK_EX);
1076 # print "####################################\n\n";
1077 # return;
1078 #}
1081 #=== FUNCTION ================================================================
1082 # NAME: create_known_daemons_entry
1083 # PARAMETERS: hostname - string - ip address and port of host
1084 # RETURNS: nothing
1085 # DESCRIPTION: nome est omen
1086 #===============================================================================
1087 #sub create_known_daemons_entry {
1088 # my ($hostname) = @_;
1089 # $shmkh->shlock(LOCK_EX);
1090 # $known_daemons->{$hostname} = {};
1091 # $known_daemons->{$hostname}->{status} = "none";
1092 # $known_daemons->{$hostname}->{passwd} = "none";
1093 # $known_daemons->{$hostname}->{timestamp} = "none";
1094 # $known_daemons->{$hostname}->{clients} = {};
1095 # $shmkh->shunlock(LOCK_EX);
1096 # return;
1097 #}
1100 #=== FUNCTION ================================================================
1101 # NAME: update_known_daemons_entry
1102 # PARAMETERS: hostname - string - ip address and port of host (required)
1103 # status - string - (optional)
1104 # passwd - string - (optional)
1105 # client - string - ip address and port of client (optional)
1106 # RETURNS: nothing
1107 # DESCRIPTION: nome est omen and updates each time the timestamp of hostname
1108 #===============================================================================
1109 #sub update_known_daemons_entry {
1110 # my $arg = {
1111 # hostname => undef, status => undef, passwd => undef,
1112 # client => undef,
1113 # @_ };
1114 # my $hostname = $arg->{hostname};
1115 # my $status = $arg->{status};
1116 # my $passwd = $arg->{passwd};
1117 # my $client = $arg->{client};
1118 #
1119 # if (not defined $hostname) {
1120 # daemon_log("ERROR: function add_content2known_daemons is not invoked with requiered parameter 'hostname'", 1);
1121 # return;
1122 # }
1123 #
1124 # my ($seconds, $minutes, $hours, $monthday, $month,
1125 # $year, $weekday, $yearday, $sommertime) = localtime(time);
1126 # $hours = $hours < 10 ? $hours = "0".$hours : $hours;
1127 # $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
1128 # $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
1129 # $month+=1;
1130 # $month = $month < 10 ? $month = "0".$month : $month;
1131 # $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
1132 # $year+=1900;
1133 # my $t = "$year$month$monthday$hours$minutes$seconds";
1134 #
1135 # $shmkh->shlock(LOCK_EX);
1136 # if (defined $status) {
1137 # $known_daemons->{$hostname}->{status} = $status;
1138 # }
1139 # if (defined $passwd) {
1140 # $known_daemons->{$hostname}->{passwd} = $passwd;
1141 # }
1142 # if (defined $client) {
1143 # $known_daemons->{$hostname}->{clients}->{$client} = "";
1144 # }
1145 # $known_daemons->{$hostname}->{timestamp} = $t;
1146 # $shmkh->shunlock(LOCK_EX);
1147 # return;
1148 #}
1151 #==== MAIN = main ==============================================================
1153 # parse commandline options
1154 Getopt::Long::Configure( "bundling" );
1155 GetOptions("h|help" => \&usage,
1156 "c|config=s" => \$cfg_file,
1157 "f|foreground" => \$foreground,
1158 "v|verbose+" => \$verbose,
1159 );
1161 # read and set config parameters
1162 &check_cmdline_param ;
1163 &read_configfile;
1164 &check_pid;
1166 $SIG{CHLD} = 'IGNORE';
1168 # restart daemon log file
1169 if(-e $log_file ) { unlink $log_file }
1170 daemon_log(" ", 1);
1171 daemon_log("$0 started!", 1);
1173 # Just fork, if we"re not in foreground mode
1174 if( ! $foreground ) { $pid = fork(); }
1175 else { $pid = $$; }
1177 # Do something useful - put our PID into the pid_file
1178 if( 0 != $pid ) {
1179 open( LOCK_FILE, ">$pid_file" );
1180 print LOCK_FILE "$pid\n";
1181 close( LOCK_FILE );
1182 if( !$foreground ) { exit( 0 ) };
1183 }
1185 # connect to bus_known_server_db
1186 my @server_col_names = ('hostname', 'status', 'hostkey', 'timestamp', 'clients' );
1187 $bus_known_server_db = GOSA::DBsqlite->new($bus_known_server_file_name);
1188 $bus_known_server_db->create_table('bus_known_server', \@server_col_names);
1191 # detect own ip and mac address
1192 ($bus_ip, $bus_mac_address) = &get_ip_and_mac();
1193 if (not defined $bus_ip) {
1194 die "EXIT: ip address of $0 could not be detected";
1195 }
1196 daemon_log("bus ip address detected: $bus_ip", 1);
1197 daemon_log("bus mac address detected: $bus_mac_address", 1);
1199 # complete addresses
1200 $bus_address = "$bus_ip:$bus_port";
1202 # setup xml parser
1203 $xml = new XML::Simple();
1205 # create cipher object
1206 $bus_cipher = &create_ciphering($bus_passwd);
1207 $bus_address = "$bus_ip:$bus_port";
1209 # create reading and writing vectors
1210 my $rbits = my $wbits = my $ebits = "";
1212 # open the bus socket
1213 if($bus_activ eq "on") {
1214 daemon_log(" ", 1);
1215 $bus = IO::Socket::INET->new(LocalPort => $bus_port,
1216 Type => SOCK_STREAM,
1217 Reuse => 1,
1218 Listen => 20,
1219 ) or die "kann kein TCP-Server an Port $bus_port sein: $@\n";
1220 vec($rbits, fileno $bus, 1) = 1;
1221 vec($wbits, fileno $bus, 1) = 1;
1222 daemon_log ("start bus at $bus_ip:$bus_port", 1);
1223 }
1225 # add bus to known_daemons
1227 #&create_known_daemons_entry($bus_address);
1228 #&update_known_daemons_entry(hostname=>$bus_address, status=>"bus", passwd=>$bus_passwd);
1231 while(1) {
1232 my $nf = select($rbits, $wbits, undef, undef);
1233 # error handling
1234 if($nf < 0 ) {
1235 }
1237 # something is coming in
1238 if(vec $rbits, fileno $bus, 1 ) {
1239 my $client = $bus->accept();
1240 my $other_end = getpeername($client);
1241 if(not defined $other_end) {
1242 daemon_log("Gegenstelle konnte nicht identifiziert werden: $!\n");
1243 } else {
1244 my ($port, $iaddr) = unpack_sockaddr_in($other_end);
1245 my $actual_ip = inet_ntoa($iaddr);
1246 daemon_log("\naccept client from $actual_ip\n", 5);
1247 my $in_msg = &read_from_socket($client);
1248 if(defined $in_msg){
1249 &activating_child($in_msg, $actual_ip);
1250 } else {
1251 daemon_log("cannot read from $actual_ip\n",1);
1252 }
1253 }
1254 close($client);
1255 }
1257 }