Code

update: server and client config man files are complete
[gosa.git] / gosa-si / client / events / corefunctions.pm
1 package corefunctions;
2 use Exporter;
3 @ISA = qw(Exporter);
4 my @events = (
5     "get_events",
6     "registered",
7     'new_syslog_config',
8     "new_ntp_config",
9     "new_ldap_config",
10     "new_key",
11     "generate_hw_digest",     # no implementations
12     "detect_hardware",
13     "confirm_new_key",
14     "ping",
15     "import_events",    # no implementations
16     );
17 @EXPORT = @events;
19 use strict;
20 use warnings;
21 use Data::Dumper;
22 use Fcntl;
23 use GOSA::GosaSupportDaemon;
24 use File::Basename;
26 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
28 my $chrony_file = "/etc/chrony/chrony.conf";
29 my $syslog_file = "/etc/syslog.conf";
31 my %cfg_defaults = (
32     "client" => {
33         "ldap" => [\$ldap_enabled, 1],
34         "offline-ldap" => [\$offline_enabled, 0],
35         "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
36         "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
37         "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
38         "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
39     },
40 );
42 BEGIN {}
44 END {}
46 ### Start ######################################################################
48 &main::read_configfile($main::cfg_file, %cfg_defaults);
51 my $server_address = $main::server_address;
52 my $server_key = $main::server_key;
53 my $client_mac_address = $main::client_mac_address;
55 sub write_to_file {
56     my ($string, $file) = @_;
57     my $error = 0;
59     if( not defined $file || not -f $file ) {
60         &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
61         $error++;
62     }
63     if( not defined $string || 0 == length($string)) {
64         &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
65         $error++;
66     }
67     
68     if( $error == 0 ) {
70         chomp($string);
71             
72         if( not -f $file ) {
73             open (FILE, "$file");
74             close(FILE);
75         }
76         open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
77         print FILE $string."\n";
78         close(FILE);
79     }
81     return;    
82 }
85 sub get_events {
86     return \@events;
87 }
89 sub daemon_log {
90     my ($msg, $level) = @_ ;
91     &main::daemon_log($msg, $level);
92     return;
93 }
95 sub registered {
96     my ($msg, $msg_hash) = @_ ;
98     my $header = @{$msg_hash->{'header'}}[0];
99     if( $header eq "registered" ) {
100         my $source = @{$msg_hash->{'source'}}[0];
101         &main::daemon_log("INFO: registration at $source", 1);
102         $main::server_address = $source;
103     }
105     # set globaly variable client_address
106     my $target =  @{$msg_hash->{'target'}}[0];
107     $main::client_address = $target;
109     # set registration_flag to true 
110     my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
111      # Write the MAC address to file
112     if(stat($main::opts_file)) { 
113             unlink($main::opts_file);
114     }
116     my $opts_file_FH;
117     my $hostname= $main::client_dnsname;
118     $hostname =~ s/\..*$//;
119     $hostname =~ tr/A-Z/a-z/;
120     open($opts_file_FH, ">$main::opts_file");
121     print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
122     print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
123     print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
124     print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
125     if(defined(@{$msg_hash->{'ldap_available'}}) &&
126                    @{$msg_hash->{'ldap_available'}}[0] eq "true") {
127         print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
128         }
129     if(defined(@{$msg_hash->{'error'}})) {
130                 my $errormsg= @{$msg_hash->{'error'}}[0];
131         print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
132                 &write_to_file($errormsg, $fai_logpath);
133         }
134     close($opts_file_FH);
135      
136     my $out_msg = &create_xml_string($out_hash);
137     return $out_msg;
140 sub server_leaving {
141     my ($msg_hash) = @_ ;
142     my $source = @{$msg_hash->{'source'}}[0]; 
143     my $header = @{$msg_hash->{'header'}}[0];
144     
145     daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
146     $main::server_address = "none";
147     $main::server_key = "none";
149     # reinitialization of default values in config file
150     &main::read_configfile;
151     
152     # registrated at new daemon
153     &main::register_at_server();
154        
155     return;   
159 ## @method new_syslog_config
160 # Update or add syslog messages forwarding to specified syslog server.
161 # @param msg - STRING - xml message with tag server
162 # @param msg_hash - HASHREF - message information parsed into a hash
163 sub new_syslog_config {
164     my ($msg, $msg_hash) = @_ ;
166     # Sanity check of incoming message
167     if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
168         &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
169         return;
170     }
172     # Fetch the new syslog server from incoming message
173     my $syslog_server = @{$msg_hash->{'server'}}[0];
174     &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5); 
175     my $found_server_flag = 0;
176     
177     # Sanity check of /etc/syslog.conf
178     if (not -f $syslog_file) {
179         &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
180         return;
181     }
182     
183     # Substitute existing server with new syslog server
184     open (FILE, "<$syslog_file");
185     my @file = <FILE>;
186     close FILE;
187     my $syslog_server_line = "*.*\t@".$syslog_server."\n"; 
188     foreach my $line (@file) {
189         if ($line =~ /^\*\.\*\s+@/) {
190             $line = $syslog_server_line;
191             $found_server_flag++;
192         }
193     }
194     
195     # Append new server if no old server configuration found
196     if (not $found_server_flag) {
197         push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
198         push(@file, $syslog_server_line);
199     }
200     
201     # Write changes to file and close it
202     open (FILE, "+>$syslog_file");
203     print FILE join("", @file); 
204     close FILE;
205     &main::daemon_log("INFO: wrote new configuration file: $syslog_file", 5);
207     # Restart syslog deamon
208     my $res = qx(/etc/init.d/sysklogd restart);
209     &main::daemon_log("INFO: restart syslog daemon: $res", 5);
211     return;
215 ## @method new_ntp_config
216 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
217 # @param msg - STRING - xml message with tag server
218 # @param msg_hash - HASHREF - message information parsed into a hash
219 sub new_ntp_config {
220     my ($msg, $msg_hash) = @_ ;
222     # Sanity check of incoming message
223     if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
224         &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
225         return;
226     }
228     # Fetch the new ntp server from incoming message
229     my $ntp_servers = $msg_hash->{'server'};
230     &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5); 
231     my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
232     my $found_server_flag = 0;
234     # Sanity check of /etc/chrony/chrony.conf
235     if (not -f $chrony_file) {
236         &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
237         return;
238     }
240     # Substitute existing server with new ntp server
241     open (FILE, "<$chrony_file");
242     my @file = <FILE>;
243     close FILE;
244     foreach my $line (@file) {
245         if ($line =~ /^server /) {
246             if ($found_server_flag) {
247                 $line =~ s/^server [\s\S]+$//;
248             } else {
249                 $line =~ s/^server [\s\S]+$/$ntp_servers_string/;
250             }
251             $found_server_flag++;
252         }
253     }
255     # Append new server if no old server configuration found
256     if (not $found_server_flag) {
257         push(@file, "\n# ntp server configuration written by GOsa-si\n");
258         push(@file, $ntp_servers_string);
259     }
261     # Write changes to file and close it
262     open (FILE, "+>$chrony_file");
263     print FILE join("", @file); 
264     close FILE;
265     &main::daemon_log("INFO: wrote new configuration file: $chrony_file", 5);
267     # Restart chrony deamon
268     my $res = qx(/etc/init.d/chrony force-reload);
269     &main::daemon_log("INFO: restart chrony daemon: $res", 5);
271     return;
275 sub new_ldap_config {
276     my ($msg, $msg_hash) = @_ ;
278     if( $ldap_enabled != 1 ) {
279             return;
280     }
282     my $element;
283     my @ldap_uris;
284     my $ldap_base;
285     my @ldap_options;
286     my @pam_options;
287     my @nss_options;
288     my $goto_admin;
289     my $goto_secret;
290     my $admin_base= "";
291     my $department= "";
292     my $release= "";
293     my $unit_tag;
295     # Transform input into array
296     while ( my ($key, $value) = each(%$msg_hash) ) {
297         if ($key =~ /^(source|target|header)$/) {
298                 next;
299         }
301         foreach $element (@$value) {
302                 if ($key =~ /^ldap_uri$/) {
303                         push (@ldap_uris, $element);
304                         next;
305                 }
306                 if ($key =~ /^ldap_base$/) {
307                         $ldap_base= $element;
308                         next;
309                 }
310                 if ($key =~ /^goto_admin$/) {
311                         $goto_admin= $element;
312                         next;
313                 }
314                 if ($key =~ /^goto_secret$/) {
315                         $goto_secret= $element;
316                         next;
317                 }
318                 if ($key =~ /^ldap_cfg$/) {
319                         push (@ldap_options, "$element");
320                         next;
321                 }
322                 if ($key =~ /^pam_cfg$/) {
323                         push (@pam_options, "$element");
324                         next;
325                 }
326                 if ($key =~ /^nss_cfg$/) {
327                         push (@nss_options, "$element");
328                         next;
329                 }
330                 if ($key =~ /^admin_base$/) {
331                         $admin_base= $element;
332                         next;
333                 }
334                 if ($key =~ /^department$/) {
335                         $department= $element;
336                         next;
337                 }
338                 if ($key =~ /^unit_tag$/) {
339                         $unit_tag= $element;
340                         next;
341                 }
342                 if ($key =~ /^release$/) {
343                         $release= $element;
344                         next;
345                 }
346         }
347     }
349     # Unit tagging enabled?
350     if (defined $unit_tag){
351             push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
352             push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
353             push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
354     }
356     # Setup ldap.conf
357     my $file1;
358     my $file2;
359     open(file1, "> $ldap_config");
360     print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
361     print file1 "URI";
362     foreach $element (@ldap_uris) {
363         print file1 " $element";
364     }
365     print file1 "\nBASE $ldap_base\n";
366     foreach $element (@ldap_options) {
367         print file1 "$element\n";
368     }
369     close (file1);
370     daemon_log("wrote $ldap_config", 5);
372     # Setup pam_ldap.conf / libnss-ldap.conf
373     open(file1, "> $pam_config");
374     open(file2, "> $nss_config");
375     print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
376     print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
377     print file1 "uri";
378     print file2 "uri";
379     foreach $element (@ldap_uris) {
380         print file1 " $element";
381         print file2 " $element";
382     }
383     print file1 "\nbase $ldap_base\n";
384     print file2 "\nbase $ldap_base\n";
385     foreach $element (@pam_options) {
386         print file1 "$element\n";
387     }
388     foreach $element (@nss_options) {
389         print file2 "$element\n";
390     }
391     close (file2);
392     daemon_log("wrote $nss_config", 5);
393     close (file1);
394     daemon_log("wrote $pam_config", 5);
396     # Create goto.secrets if told so - for compatibility reasons
397     if (defined $goto_admin){
398             open(file1, "> /etc/goto/secret");
399             close(file1);
400             chown(0,0, "/etc/goto/secret");
401             chmod(0600, "/etc/goto/secret");
402             open(file1, "> /etc/goto/secret");
403             print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
404             close(file1);
405             daemon_log("wrote /etc/goto/secret", 5);
406     }
408     # Write shell based config
409     my $cfg_name= "/etc/ldap/ldap-shell.conf";
411     # Get first LDAP server
412     my $ldap_server= $ldap_uris[0];
413     $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
415     open(file1, "> $cfg_name");
416     print file1 "LDAP_BASE=\"$ldap_base\"\n";
417     print file1 "LDAP_SERVER=\"$ldap_server\"\n";
418     print file1 "LDAP_URIS=\"@ldap_uris\"\n";
419     print file1 "ADMIN_BASE=\"$admin_base\"\n";
420     print file1 "DEPARTMENT=\"$department\"\n";
421     print file1 "RELEASE=\"$release\"\n";
422     print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
423     print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
424     close(file1);
425     daemon_log("wrote $cfg_name", 5);
427     # Write offline config
428     if ($offline_enabled){
429             $cfg_name= "/etc/ldap/ldap-offline.conf";
431             # Get first LDAP server
432             open(file1, "> $cfg_name");
433             print file1 "LDAP_BASE=\"$ldap_base\"\n";
434             print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
435             print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
436             print file1 "ADMIN_BASE=\"$admin_base\"\n";
437             print file1 "DEPARTMENT=\"$department\"\n";
438             print file1 "RELEASE=\"$release\"\n";
439             print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
440             print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
441             close(file1);
442             daemon_log("wrote $cfg_name", 5);
443     }
445     return;
449 sub new_key {
450     # my ($msg_hash) = @_ ;
451     my $new_server_key = &main::create_passwd();
453     my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
454     my $out_msg = &create_xml_string($out_hash);
456     # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg 
457     # comes from gosa-si-server within a given time
458     
460     return $out_msg; 
464 sub confirm_new_key {
465     my ($msg, $msg_hash) = @_ ;
466     my $header = @{$msg_hash->{'header'}}[0];
467     my $target = @{$msg_hash->{'target'}}[0];
468     my $source = @{$msg_hash->{'source'}}[0];
470     &main::daemon_log("confirm new key from $source", 5);
471     return;
476 sub detect_hardware {
478     &write_to_file('goto-hardware-detection-start', $fai_logpath);
480         my $hwinfo= `which hwinfo`;
481         chomp $hwinfo;
483         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
484                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
485                 return;
486         }
488         my $result= {
489                 gotoHardwareChecksum => &main::generate_hw_digest(),
490                 macAddress      => $client_mac_address,
491                 gotoXMonitor    => "",
492                 gotoXDriver     => "",
493                 gotoXMouseType  => "",
494                 gotoXMouseport  => "",
495                 gotoXkbModel    => "",
496                 gotoXHsync      => "",
497                 gotoXVsync      => "",
498                 gotoXResolution => "",
499                 ghUsbSupport    => "",
500                 gotoSndModule   => "",
501                 ghGfxAdapter    => "",
502                 ghNetNic        => "",
503                 ghSoundAdapter  => "",
504                 ghMemSize       => "",
505                 ghCpuType       => "",
506                 gotoModules     => [],
507                 ghIdeDev        => [],
508                 ghScsiDev       => [],
509         };
511         &main::daemon_log("Starting hardware detection", 4);
512         my $gfxcard= `$hwinfo --gfxcard`;
513         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
514         if(defined($primary_adapter)) {
515                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
516                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
517         }
518         my $monitor= `$hwinfo --monitor`;
519         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
520         if(defined($primary_monitor)) {
521                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
522                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
523         }
525         if(length($result->{gotoXHsync}) == 0) {
526                 # set default values
527                 $result->{gotoXHsync} = "30+50";
528                 $result->{gotoXVsync} = "30+90";
529         }
531         my $mouse= `$hwinfo --mouse`;
532         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
533         if(defined($primary_mouse)) {
534                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
535                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
536         }
538         my $sound= `$hwinfo --sound`;
539         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
540         if(defined($primary_sound)) {
541                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
542                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
543         }
545         my $netcard= `hwinfo --netcard`;
546         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
547         if(defined($primary_netcard)) {
548                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
549         }
551         my $keyboard= `hwinfo --keyboard`;
552         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
553         if(defined($primary_keyboard)) {
554                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
555         }
557         $result->{ghCpuType}= sprintf "%s / %s - %s", 
558         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
559         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
561         my @gotoModules=();
562         for my $line(`lsmod`) {
563                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
564                         next;
565                 } else {
566                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
567                 }
568         }
569         my %seen = ();
570         
571         # Remove duplicates and save
572         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
574         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
575         
576         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
577                 push @{$result->{ghIdeDev}}, $device;
578         }
580         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
581                 push @{$result->{ghScsiDev}}, $device;
582         }
584         &main::daemon_log("Hardware detection done!", 4);
586     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
587    
588     return &main::send_msg_hash_to_target(
589                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
590                 $main::server_address, 
591                 $main::server_key,
592         );
596 sub ping {
597     my ($msg, $msg_hash) = @_ ;
598     my $header = @{$msg_hash->{'header'}}[0];
599     my $source = @{$msg_hash->{'source'}}[0];
600     my $target = @{$msg_hash->{'target'}}[0];
601     my $session_id = @{$msg_hash->{'session_id'}}[0];
602     my $out_msg;
603     my $out_hash;
605     # there is no session_id so send 'got_new_ping'-msg
606     if (not defined $session_id) {
607         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
609     # there is a session_id so send 'answer_$session_id'-msg because there is 
610     # a process waiting for this message
611     } else {
612         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
613         &add_content2xml_hash($out_hash, "session_id", $session_id);
614     }
616     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
617     if (defined $forward_to_gosa) {
618         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
619     }
620     $out_msg = &main::create_xml_string($out_hash);
621     return $out_msg;
625 1;