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
12 # libdata-dumper-simple-perl libdbd-sqlite3-perl libnet-ldap-perl
13 # libpoe-perl
14 # BUGS: ---
15 # NOTES:
16 # AUTHOR: (Andreas Rettenberger), <rettenberger@gonicus.de>
17 # COMPANY:
18 # VERSION: 1.0
19 # CREATED: 12.09.2007 08:54:41 CEST
20 # REVISION: ---
21 #===============================================================================
23 use strict;
24 use warnings;
25 use Getopt::Long;
26 use Config::IniFiles;
27 use POSIX;
28 use Time::HiRes qw( gettimeofday );
30 use Fcntl;
31 use IO::Socket::INET;
32 use IO::Handle;
33 use IO::Select;
34 use Symbol qw(qualify_to_ref);
35 use Crypt::Rijndael;
36 use MIME::Base64;
37 use Digest::MD5 qw(md5 md5_hex md5_base64);
38 use XML::Simple;
39 use Data::Dumper;
40 use Sys::Syslog qw( :DEFAULT setlogsock);
41 use Cwd;
42 use File::Spec;
43 use GOSA::GosaSupportDaemon;
44 use GOSA::DBsqlite;
45 use POE qw(Component::Server::TCP);
47 my $modules_path = "/usr/lib/gosa-si/modules";
48 use lib "/usr/lib/gosa-si/modules";
50 my (%cfg_defaults, $foreground, $verbose, $ping_timeout);
51 my ($bus, $msg_to_bus, $bus_cipher);
52 my ($server, $server_mac_address, $server_events);
53 my ($gosa_server, $job_queue_timeout, $job_queue_table_name, $job_queue_file_name,$job_queue_loop_delay);
54 my ($known_modules, $known_clients_file_name, $known_server_file_name);
55 my ($max_clients);
56 my ($pid_file, $procid, $pid, $log_file);
57 my (%free_child, %busy_child, $child_max, $child_min, %child_alive_time, $child_timeout);
58 my ($arp_activ, $arp_fifo, $arp_fifo_path);
60 # variables declared in config file are always set to 'our'
61 our (%cfg_defaults, $log_file, $pid_file,
62 $bus_activ, $bus_passwd, $bus_ip, $bus_port,
63 $server_activ, $server_ip, $server_port, $server_passwd, $max_clients,
64 $arp_activ, $arp_fifo_path,
65 $gosa_activ, $gosa_passwd, $gosa_ip, $gosa_port, $gosa_timeout,
66 );
68 # additional variable which should be globaly accessable
69 our $xml;
70 our $server_address;
71 our $bus_address;
72 our $gosa_address;
73 our $no_bus;
74 our $no_arp;
75 our $verbose;
76 our $forground;
77 our $cfg_file;
79 # specifies the verbosity of the daemon_log
80 $verbose = 0 ;
82 # if foreground is not null, script will be not forked to background
83 $foreground = 0 ;
85 # specifies the timeout seconds while checking the online status of a registrating client
86 $ping_timeout = 5;
88 $no_bus = 0;
90 $no_arp = 0;
92 # name of table for storing gosa jobs
93 our $job_queue_table_name = 'jobs';
94 our $job_db;
96 # holds all other gosa-sd as well as the gosa-sd-bus
97 our $known_server_db;
99 # holds all registrated clients
100 our $known_clients_db;
102 %cfg_defaults =
103 ("general" =>
104 {"log_file" => [\$log_file, "/var/run/".$0.".log"],
105 "pid_file" => [\$pid_file, "/var/run/".$0.".pid"],
106 "child_max" => [\$child_max, 10],
107 "child_min" => [\$child_min, 3],
108 "child_timeout" => [\$child_timeout, 180],
109 "job_queue_timeout" => [\$job_queue_timeout, undef],
110 "job_queue_file_name" => [\$job_queue_file_name, '/var/lib/gosa-si/jobs.db'],
111 "job_queue_loop_delay" => [\$job_queue_loop_delay, 3],
112 "known_clients_file_name" => [\$known_clients_file_name, '/var/lib/gosa-si/known_clients.db' ],
113 "known_server_file_name" => [\$known_server_file_name, '/var/lib/gosa-si/known_server.db'],
114 },
115 "bus" =>
116 {"bus_activ" => [\$bus_activ, "on"],
117 "bus_passwd" => [\$bus_passwd, ""],
118 "bus_ip" => [\$bus_ip, "0.0.0.0"],
119 "bus_port" => [\$bus_port, "20080"],
120 },
121 "server" =>
122 {"server_activ" => [\$server_activ, "on"],
123 "server_ip" => [\$server_ip, "0.0.0.0"],
124 "server_port" => [\$server_port, "20081"],
125 "server_passwd" => [\$server_passwd, ""],
126 "max_clients" => [\$max_clients, 100],
127 },
128 "arp" =>
129 {"arp_activ" => [\$arp_activ, "on"],
130 "arp_fifo_path" => [\$arp_fifo_path, "/var/run/gosa-si/arp-notify"],
131 },
132 "gosa" =>
133 {"gosa_activ" => [\$gosa_activ, "on"],
134 "gosa_ip" => [\$gosa_ip, "0.0.0.0"],
135 "gosa_port" => [\$gosa_port, "20082"],
136 "gosa_passwd" => [\$gosa_passwd, "none"],
137 },
138 );
141 #=== FUNCTION ================================================================
142 # NAME: usage
143 # PARAMETERS: nothing
144 # RETURNS: nothing
145 # DESCRIPTION: print out usage text to STDERR
146 #===============================================================================
147 sub usage {
148 print STDERR << "EOF" ;
149 usage: $0 [-hvf] [-c config]
151 -h : this (help) message
152 -c <file> : config file
153 -f : foreground, process will not be forked to background
154 -v : be verbose (multiple to increase verbosity)
155 -no-bus : starts $0 without connection to bus
156 -no-arp : starts $0 without connection to arp module
158 EOF
159 print "\n" ;
160 }
163 #=== FUNCTION ================================================================
164 # NAME: read_configfile
165 # PARAMETERS: cfg_file - string -
166 # RETURNS: nothing
167 # DESCRIPTION: read cfg_file and set variables
168 #===============================================================================
169 sub read_configfile {
170 my $cfg;
171 if( defined( $cfg_file) && ( length($cfg_file) > 0 )) {
172 if( -r $cfg_file ) {
173 $cfg = Config::IniFiles->new( -file => $cfg_file );
174 } else {
175 print STDERR "Couldn't read config file!\n";
176 }
177 } else {
178 $cfg = Config::IniFiles->new() ;
179 }
180 foreach my $section (keys %cfg_defaults) {
181 foreach my $param (keys %{$cfg_defaults{ $section }}) {
182 my $pinfo = $cfg_defaults{ $section }{ $param };
183 ${@$pinfo[ 0 ]} = $cfg->val( $section, $param, @$pinfo[ 1 ] );
184 }
185 }
186 }
189 #=== FUNCTION ================================================================
190 # NAME: logging
191 # PARAMETERS: level - string - default 'info'
192 # msg - string -
193 # facility - string - default 'LOG_DAEMON'
194 # RETURNS: nothing
195 # DESCRIPTION: function for logging
196 #===============================================================================
197 sub daemon_log {
198 # log into log_file
199 my( $msg, $level ) = @_;
200 if(not defined $msg) { return }
201 if(not defined $level) { $level = 1 }
202 if(defined $log_file){
203 open(LOG_HANDLE, ">>$log_file");
204 if(not defined open( LOG_HANDLE, ">>$log_file" )) {
205 print STDERR "cannot open $log_file: $!";
206 return }
207 chomp($msg);
208 if($level <= $verbose){
209 my ($seconds, $minutes, $hours, $monthday, $month,
210 $year, $weekday, $yearday, $sommertime) = localtime(time);
211 $hours = $hours < 10 ? $hours = "0".$hours : $hours;
212 $minutes = $minutes < 10 ? $minutes = "0".$minutes : $minutes;
213 $seconds = $seconds < 10 ? $seconds = "0".$seconds : $seconds;
214 my @monthnames = ("Jan", "Feb", "Mar", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
215 $month = $monthnames[$month];
216 $monthday = $monthday < 10 ? $monthday = "0".$monthday : $monthday;
217 $year+=1900;
218 my $name = $0;
219 $name =~ s/\.\///;
221 my $log_msg = "$month $monthday $hours:$minutes:$seconds $name $msg\n";
222 print LOG_HANDLE $log_msg;
223 if( $foreground ) {
224 print STDERR $log_msg;
225 }
226 }
227 close( LOG_HANDLE );
228 }
229 #log into syslog
230 # my ($msg, $level, $facility) = @_;
231 # if(not defined $msg) {return}
232 # if(not defined $level) {$level = "info"}
233 # if(not defined $facility) {$facility = "LOG_DAEMON"}
234 # openlog($0, "pid,cons,", $facility);
235 # syslog($level, $msg);
236 # closelog;
237 # return;
238 }
241 #=== FUNCTION ================================================================
242 # NAME: check_cmdline_param
243 # PARAMETERS: nothing
244 # RETURNS: nothing
245 # DESCRIPTION: validates commandline parameter
246 #===============================================================================
247 sub check_cmdline_param () {
248 my $err_config;
249 my $err_counter = 0;
250 if(not defined($cfg_file)) {
251 $cfg_file = "/etc/gosa-si/server.conf";
252 if(! -r $cfg_file) {
253 $err_config = "please specify a config file";
254 $err_counter += 1;
255 }
256 }
257 if( $err_counter > 0 ) {
258 &usage( "", 1 );
259 if( defined( $err_config)) { print STDERR "$err_config\n"}
260 print STDERR "\n";
261 exit( -1 );
262 }
263 }
266 #=== FUNCTION ================================================================
267 # NAME: check_pid
268 # PARAMETERS: nothing
269 # RETURNS: nothing
270 # DESCRIPTION: handels pid processing
271 #===============================================================================
272 sub check_pid {
273 $pid = -1;
274 # Check, if we are already running
275 if( open(LOCK_FILE, "<$pid_file") ) {
276 $pid = <LOCK_FILE>;
277 if( defined $pid ) {
278 chomp( $pid );
279 if( -f "/proc/$pid/stat" ) {
280 my($stat) = `cat /proc/$pid/stat` =~ m/$pid \((.+)\).*/;
281 if( $0 eq $stat ) {
282 close( LOCK_FILE );
283 exit -1;
284 }
285 }
286 }
287 close( LOCK_FILE );
288 unlink( $pid_file );
289 }
291 # create a syslog msg if it is not to possible to open PID file
292 if (not sysopen(LOCK_FILE, $pid_file, O_WRONLY|O_CREAT|O_EXCL, 0644)) {
293 my($msg) = "Couldn't obtain lockfile '$pid_file' ";
294 if (open(LOCK_FILE, '<', $pid_file)
295 && ($pid = <LOCK_FILE>))
296 {
297 chomp($pid);
298 $msg .= "(PID $pid)\n";
299 } else {
300 $msg .= "(unable to read PID)\n";
301 }
302 if( ! ($foreground) ) {
303 openlog( $0, "cons,pid", "daemon" );
304 syslog( "warning", $msg );
305 closelog();
306 }
307 else {
308 print( STDERR " $msg " );
309 }
310 exit( -1 );
311 }
312 }
314 #=== FUNCTION ================================================================
315 # NAME: import_modules
316 # PARAMETERS: module_path - string - abs. path to the directory the modules
317 # are stored
318 # RETURNS: nothing
319 # DESCRIPTION: each file in module_path which ends with '.pm' is imported by
320 # "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-si-server could not load module $file", 1);
337 daemon_log("$@", 5);
338 } else {
339 my $mod_name = $1;
340 my $info = eval($mod_name.'::get_module_info()');
341 my ($input_address, $input_key, $input, $input_active, $input_type) = @{$info};
342 $known_modules->{$mod_name} = $info;
344 daemon_log("module $mod_name loaded", 1);
345 }
346 }
348 # for debugging
349 #while ( my ($module, $tag_hash) = each(%$known_modules)) {
350 # print "\tmodule: $module"."\n";
351 # print "\ttags: ".join(", ", keys(%$tag_hash))."\n";
352 #}
353 close (DIR);
354 }
357 #=== FUNCTION ================================================================
358 # NAME: sig_int_handler
359 # PARAMETERS: signal - string - signal arose from system
360 # RETURNS: noting
361 # DESCRIPTION: handels tasks to be done befor signal becomes active
362 #===============================================================================
363 sub sig_int_handler {
364 my ($signal) = @_;
366 daemon_log("shutting down gosa-si-server", 1);
367 exit(1);
368 }
369 $SIG{INT} = \&sig_int_handler;
372 #=== FUNCTION ================================================================
373 # NAME: create_known_client
374 # PARAMETERS: hostname - string - key for the hash known_clients
375 # RETURNS: nothing
376 # DESCRIPTION: creates a dummy entry for hostname in known_clients
377 #===============================================================================
378 sub create_known_client {
379 my ($hostname) = @_;
381 my $entry = { table=>'known_clients',
382 hostname=>$hostname,
383 status=>'none',
384 hostkey=>'none',
385 timestamp=>'none',
386 macaddress=>'none',
387 events=>'none',
388 };
389 my $res = $known_clients_db->add_dbentry($entry);
390 if ($res > 0) {
391 daemon_log("ERROR: cannot add entry to known_clients.db: $res", 1);
392 }
394 return;
395 }
397 sub client_input {
398 my ($heap,$input,$wheel) = @_[HEAP, ARG0, ARG1];
399 ######################################
400 # forward msg to all imported modules
401 no strict "refs";
402 my $answer;
403 my %act_modules = %$known_modules;
404 while( my ($module, $info) = each(%act_modules)) {
405 daemon_log("Processing module ".$module, 3);
406 my $tmp = &{ $module."::process_incoming_msg" }($input.".".$heap->{remote_ip}."\n");
407 if (defined $tmp) {
408 $answer = $tmp;
409 daemon_log("Got answer from module ".$module.": \n".$answer,8);
410 }
411 }
412 daemon_log("processing of msg finished", 5);
414 if (defined $answer) {
415 $heap->{client}->put($answer);
416 } else {
417 $heap->{client}->put("done\n");
418 }
419 }
421 sub trigger_db_loop {
422 my ($kernel) = $_[KERNEL];
423 $kernel->delay_set('watch_for_new_jobs',3);
424 }
426 sub watch_for_new_jobs {
427 my ($kernel,$heap) = @_[KERNEL, HEAP];
429 # check gosa job queue for jobs with executable timestamp
430 my $timestamp = &get_time();
432 my $sql_statement = "SELECT * FROM ".$job_queue_table_name.
433 " WHERE status='waiting' AND timestamp<'$timestamp'";
435 my $res = $job_db->select_dbentry( $sql_statement );
437 while( my ($id, $hit) = each %{$res} ) {
439 my $jobdb_id = $hit->{id};
440 my $macaddress = $hit->{macaddress};
441 my $job_msg_hash = &transform_msg2hash($hit->{xmlmessage});
442 my $out_msg_hash = $job_msg_hash;
443 my $sql_statement = "SELECT * FROM known_clients WHERE macaddress='$macaddress'";
444 my $res_hash = $known_clients_db->select_dbentry( $sql_statement );
445 # expect macaddress is unique!!!!!!
446 my $target = $res_hash->{1}->{hostname};
448 if (not defined $target) {
449 &daemon_log("ERROR: no host found for mac address: $job_msg_hash->{mac}[0]", 1);
450 &daemon_log("xml message: $hit->{xmlmessage}", 5);
451 my $sql_statement = "UPDATE $job_queue_table_name ".
452 "SET status='error', result='no host found for mac address' ".
453 "WHERE id='$jobdb_id'";
454 my $res = $job_db->update_dbentry($sql_statement);
455 next;
456 }
458 # add target
459 &add_content2xml_hash($out_msg_hash, "target", $target);
461 # add new header
462 my $out_header = $job_msg_hash->{header}[0];
463 $out_header =~ s/job_/gosa_/;
464 delete $out_msg_hash->{header};
465 &add_content2xml_hash($out_msg_hash, "header", $out_header);
467 # add sqlite_id
468 &add_content2xml_hash($out_msg_hash, "jobdb_id", $jobdb_id);
470 my $out_msg = &create_xml_string($out_msg_hash);
472 # encrypt msg as a GosaPackage module
473 my $cipher = &create_ciphering($gosa_passwd);
474 my $crypted_out_msg = &encrypt_msg($out_msg, $cipher);
476 my $error = &send_msg_hash2address($out_msg_hash, "$gosa_ip:$gosa_port", $gosa_passwd);
478 if ($error == 0) {
479 my $sql_statement = "UPDATE $job_queue_table_name ".
480 "SET status='processing', targettag='$target' ".
481 "WHERE id='$jobdb_id'";
482 my $res = $job_db->update_dbentry($sql_statement);
483 } else {
484 my $sql_statement = "UPDATE $job_queue_table_name ".
485 "SET status='error' ".
486 "WHERE id='$jobdb_id'";
487 my $res = $job_db->update_dbentry($sql_statement);
488 }
489 }
491 $kernel->delay_set('watch_for_new_jobs',3);
492 }
495 #==== MAIN = main ==============================================================
496 # parse commandline options
497 Getopt::Long::Configure( "bundling" );
498 GetOptions("h|help" => \&usage,
499 "c|config=s" => \$cfg_file,
500 "f|foreground" => \$foreground,
501 "v|verbose+" => \$verbose,
502 "no-bus+" => \$no_bus,
503 "no-arp+" => \$no_arp,
504 );
506 # read and set config parameters
507 &check_cmdline_param ;
508 &read_configfile;
509 &check_pid;
511 $SIG{CHLD} = 'IGNORE';
513 # forward error messages to logfile
514 if( ! $foreground ) {
515 open(STDERR, '>>', $log_file);
516 open(STDOUT, '>>', $log_file);
517 }
519 # Just fork, if we are not in foreground mode
520 if( ! $foreground ) {
521 chdir '/' or die "Can't chdir to /: $!";
522 $pid = fork;
523 setsid or die "Can't start a new session: $!";
524 umask 0;
525 } else {
526 $pid = $$;
527 }
529 # Do something useful - put our PID into the pid_file
530 if( 0 != $pid ) {
531 open( LOCK_FILE, ">$pid_file" );
532 print LOCK_FILE "$pid\n";
533 close( LOCK_FILE );
534 if( !$foreground ) {
535 exit( 0 )
536 };
537 }
539 daemon_log(" ", 1);
540 daemon_log("$0 started!", 1);
542 # delete old DBsqlite lock files
543 system('rm -f /tmp/gosa_si_lock*');
545 # connect to gosa-si job queue
546 my @job_col_names = ("id", "timestamp", "status", "result", "headertag", "targettag", "xmlmessage", "macaddress");
547 $job_db = GOSA::DBsqlite->new($job_queue_file_name);
548 $job_db->create_table('jobs', \@job_col_names);
550 # connect to known_clients_db
551 my @clients_col_names = ('hostname', 'status', 'hostkey', 'timestamp', 'macaddress', 'events');
552 $known_clients_db = GOSA::DBsqlite->new($known_clients_file_name);
553 $known_clients_db->create_table('known_clients', \@clients_col_names);
555 # connect to known_server_db
556 my @server_col_names = ('hostname', 'status', 'hostkey', 'timestamp');
557 $known_server_db = GOSA::DBsqlite->new($known_server_file_name);
558 $known_server_db->create_table('known_server', \@server_col_names);
560 # import all modules
561 &import_modules;
563 # check wether all modules are gosa-si valid passwd check
565 # create session for repeatedly checking the job queue for jobs
566 POE::Session->create
567 (
568 inline_states => {
569 _start => \&trigger_db_loop,
570 watch_for_new_jobs => \&watch_for_new_jobs,
571 }
572 );
574 # create socket for incoming xml messages
575 POE::Component::Server::TCP->new
576 (
577 Port => $server_port,
578 ClientInput => \&client_input,
579 );
580 daemon_log("start socket for incoming xml messages at port '$server_port' ", 1);
582 POE::Kernel->run();
583 exit;