Code

Print exit code for LDAP config hook
[gosa.git] / trunk / 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, $ldap_config_exit_hook);
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         "ldap-config-exit-hook" => [\$ldap_config_exit_hook, undef],
40     },
41 );
43 BEGIN {}
45 END {}
47 ### Start ######################################################################
49 &main::read_configfile($main::cfg_file, %cfg_defaults);
52 my $server_address = $main::server_address;
53 my $server_key = $main::server_key;
54 my $client_mac_address = $main::client_mac_address;
56 sub write_to_file {
57     my ($string, $file) = @_;
58     my $error = 0;
60     if( not defined $file || not -f $file ) {
61         &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
62         $error++;
63     }
64     if( not defined $string || 0 == length($string)) {
65         &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
66         $error++;
67     }
68     
69     if( $error == 0 ) {
71         chomp($string);
72             
73         if( not -f $file ) {
74             open (FILE, "$file");
75             close(FILE);
76         }
77         open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
78         print FILE $string."\n";
79         close(FILE);
80     }
82     return;    
83 }
86 sub get_events {
87     return \@events;
88 }
90 sub daemon_log {
91     my ($msg, $level) = @_ ;
92     &main::daemon_log($msg, $level);
93     return;
94 }
96 sub registered {
97     my ($msg, $msg_hash) = @_ ;
99     my $header = @{$msg_hash->{'header'}}[0];
100     if( $header eq "registered" ) {
101         my $source = @{$msg_hash->{'source'}}[0];
102         &main::daemon_log("INFO: registration at $source", 1);
103         $main::server_address = $source;
104     }
106     # set globaly variable client_address
107     my $target =  @{$msg_hash->{'target'}}[0];
108     $main::client_address = $target;
110     # set registration_flag to true 
111     my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
112      # Write the MAC address to file
113     if(stat($main::opts_file)) { 
114             unlink($main::opts_file);
115     }
117     my $opts_file_FH;
118     my $hostname= $main::client_dnsname;
119     $hostname =~ s/\..*$//;
120     $hostname =~ tr/A-Z/a-z/;
121         sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
122         print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
123     print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
124     print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
125     print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
126     if(defined(@{$msg_hash->{'ldap_available'}}) &&
127                    @{$msg_hash->{'ldap_available'}}[0] eq "true") {
128         print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
129         }
130     if(defined(@{$msg_hash->{'error'}})) {
131                 my $errormsg= @{$msg_hash->{'error'}}[0];
132         print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
133                 &write_to_file($errormsg, $fai_logpath);
134         }
135     close($opts_file_FH);
136      
137     my $out_msg = &create_xml_string($out_hash);
138     return $out_msg;
141 sub server_leaving {
142     my ($msg_hash) = @_ ;
143     my $source = @{$msg_hash->{'source'}}[0]; 
144     my $header = @{$msg_hash->{'header'}}[0];
145     
146     daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
147     $main::server_address = "none";
148     $main::server_key = "none";
150     # reinitialization of default values in config file
151     &main::read_configfile;
152     
153     # registrated at new daemon
154     &main::register_at_server();
155        
156     return;   
160 ## @method new_syslog_config
161 # Update or add syslog messages forwarding to specified syslog server.
162 # @param msg - STRING - xml message with tag server
163 # @param msg_hash - HASHREF - message information parsed into a hash
164 sub new_syslog_config {
165     my ($msg, $msg_hash) = @_ ;
167     # Sanity check of incoming message
168     if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
169         &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
170         return;
171     }
173     # Fetch the new syslog server from incoming message
174     my $syslog_server = @{$msg_hash->{'server'}}[0];
175     &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5); 
176     my $found_server_flag = 0;
177     
178     # Sanity check of /etc/syslog.conf
179     if (not -f $syslog_file) {
180         &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
181         return;
182     }
183     
184     # Substitute existing server with new syslog server
185     open (FILE, "<$syslog_file");
186     my @file = <FILE>;
187     close FILE;
188     my $syslog_server_line = "*.*\t@".$syslog_server."\n"; 
189     foreach my $line (@file) {
190         if ($line =~ /^\*\.\*\s+@/) {
191             $line = $syslog_server_line;
192             $found_server_flag++;
193         }
194     }
195     
196     # Append new server if no old server configuration found
197     if (not $found_server_flag) {
198         push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
199         push(@file, $syslog_server_line);
200     }
201     
202     # Write changes to file and close it
203     open (FILE, "+>$syslog_file");
204     print FILE join("", @file); 
205     close FILE;
206     &main::daemon_log("INFO: wrote new configuration file: $syslog_file", 5);
208     # Restart syslog deamon
209     my $res = qx(/etc/init.d/sysklogd restart);
210     &main::daemon_log("INFO: restart syslog daemon: $res", 5);
212     return;
216 ## @method new_ntp_config
217 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
218 # @param msg - STRING - xml message with tag server
219 # @param msg_hash - HASHREF - message information parsed into a hash
220 sub new_ntp_config {
221     my ($msg, $msg_hash) = @_ ;
223     # Sanity check of incoming message
224     if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
225         &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
226         return;
227     }
229     # Fetch the new ntp server from incoming message
230     my $ntp_servers = $msg_hash->{'server'};
231     &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5); 
232     my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
233     my $found_server_flag = 0;
235     # Sanity check of /etc/chrony/chrony.conf
236     if (not -f $chrony_file) {
237         &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
238         return;
239     }
241     # Substitute existing server with new ntp server
242     open (FILE, "<$chrony_file");
243     my @file = <FILE>;
244     close FILE;
245         my @new_file;
246     foreach my $line (@file) {
247         if ($line =~ /^server\s+/) {
248             if ($found_server_flag) {   
249                 $line =~ s/^server\s+[\S]+\s+$//;
250             } else {
251                 $line =~ s/^server\s+[\S]+\s+$/$ntp_servers_string/;
252             }
253             $found_server_flag++;
254         }
255                 push(@new_file, $line);
256     }
258     # Append new server if no old server configuration found
259     if (not $found_server_flag) {
260         push(@new_file, "\n# ntp server configuration written by GOsa-si\n");
261         push(@new_file, $ntp_servers_string);
262     }
264     # Write changes to file and close it
265     open (FILE, ">$chrony_file");
266     print FILE join("", @new_file); 
267     close FILE;
268     &main::daemon_log("INFO: wrote new configuration file: $chrony_file", 5);
270     # Restart chrony deamon
271     my $res = qx(/etc/init.d/chrony force-reload);
272     &main::daemon_log("INFO: restart chrony daemon: $res", 5);
274     return;
278 sub new_ldap_config {
279     my ($msg, $msg_hash) = @_ ;
281     if( $ldap_enabled != 1 ) {
282             return;
283     }
285     my $element;
286     my @ldap_uris;
287     my $ldap_base;
288     my @ldap_options;
289     my @pam_options;
290     my @nss_options;
291     my $goto_admin;
292     my $goto_secret;
293     my $admin_base= "";
294     my $department= "";
295     my $release= "";
296     my $unit_tag;
298     # Transform input into array
299     while ( my ($key, $value) = each(%$msg_hash) ) {
300         if ($key =~ /^(source|target|header)$/) {
301                 next;
302         }
304         foreach $element (@$value) {
305                 if ($key =~ /^ldap_uri$/) {
306                         push (@ldap_uris, $element);
307                         next;
308                 }
309                 if ($key =~ /^ldap_base$/) {
310                         $ldap_base= $element;
311                         next;
312                 }
313                 if ($key =~ /^goto_admin$/) {
314                         $goto_admin= $element;
315                         next;
316                 }
317                 if ($key =~ /^goto_secret$/) {
318                         $goto_secret= $element;
319                         next;
320                 }
321                 if ($key =~ /^ldap_cfg$/) {
322                         push (@ldap_options, "$element");
323                         next;
324                 }
325                 if ($key =~ /^pam_cfg$/) {
326                         push (@pam_options, "$element");
327                         next;
328                 }
329                 if ($key =~ /^nss_cfg$/) {
330                         push (@nss_options, "$element");
331                         next;
332                 }
333                 if ($key =~ /^admin_base$/) {
334                         $admin_base= $element;
335                         next;
336                 }
337                 if ($key =~ /^department$/) {
338                         $department= $element;
339                         next;
340                 }
341                 if ($key =~ /^unit_tag$/) {
342                         $unit_tag= $element;
343                         next;
344                 }
345                 if ($key =~ /^release$/) {
346                         $release= $element;
347                         next;
348                 }
349         }
350     }
352     # Unit tagging enabled?
353     if (defined $unit_tag){
354             push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
355             push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
356             push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
357     }
359     # Setup ldap.conf
360     my $file1;
361     my $file2;
362     open(file1, "> $ldap_config");
363     print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
364     print file1 "URI";
365     foreach $element (@ldap_uris) {
366         print file1 " $element";
367     }
368     print file1 "\nBASE $ldap_base\n";
369     foreach $element (@ldap_options) {
370         print file1 "$element\n";
371     }
372     close (file1);
373     daemon_log("wrote $ldap_config", 5);
375     # Setup pam_ldap.conf / libnss-ldap.conf
376     open(file1, "> $pam_config");
377     open(file2, "> $nss_config");
378     print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
379     print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
380     print file1 "uri";
381     print file2 "uri";
382     foreach $element (@ldap_uris) {
383         print file1 " $element";
384         print file2 " $element";
385     }
386     print file1 "\nbase $ldap_base\n";
387     print file2 "\nbase $ldap_base\n";
388     foreach $element (@pam_options) {
389         print file1 "$element\n";
390     }
391     foreach $element (@nss_options) {
392         print file2 "$element\n";
393     }
394     close (file2);
395     daemon_log("wrote $nss_config", 5);
396     close (file1);
397     daemon_log("wrote $pam_config", 5);
399     # Create goto.secrets if told so - for compatibility reasons
400     if (defined $goto_admin){
401             open(file1, "> /etc/goto/secret");
402             close(file1);
403             chown(0,0, "/etc/goto/secret");
404             chmod(0600, "/etc/goto/secret");
405             open(file1, "> /etc/goto/secret");
406             print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
407             close(file1);
408             daemon_log("wrote /etc/goto/secret", 5);
409     }
411     # Write shell based config
412     my $cfg_name= "/etc/ldap/ldap-shell.conf";
414     # Get first LDAP server
415     my $ldap_server= $ldap_uris[0];
416     $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
418     open(file1, "> $cfg_name");
419     print file1 "LDAP_BASE=\"$ldap_base\"\n";
420     print file1 "LDAP_SERVER=\"$ldap_server\"\n";
421     print file1 "LDAP_URIS=\"@ldap_uris\"\n";
422     print file1 "ADMIN_BASE=\"$admin_base\"\n";
423     print file1 "DEPARTMENT=\"$department\"\n";
424     print file1 "RELEASE=\"$release\"\n";
425     print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
426     print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
427     close(file1);
428     daemon_log("wrote $cfg_name", 5);
430     # Write offline config
431     if ($offline_enabled){
432             $cfg_name= "/etc/ldap/ldap-offline.conf";
434             # Get first LDAP server
435             open(file1, "> $cfg_name");
436             print file1 "LDAP_BASE=\"$ldap_base\"\n";
437             print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
438             print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
439             print file1 "ADMIN_BASE=\"$admin_base\"\n";
440             print file1 "DEPARTMENT=\"$department\"\n";
441             print file1 "RELEASE=\"$release\"\n";
442             print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
443             print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
444             close(file1);
445             daemon_log("wrote $cfg_name", 5);
446     }
448     # Set permissions and ownership structure
449     chown(0, 0, $cfg_name);
450     chmod(0644, $cfg_name);
452     # Allow custom scripts to be executed
453     if (defined $ldap_config_exit_hook) {
454         system($ldap_config_exit_hook);
455         daemon_log("Hook $ldap_config_exit_hook returned with code ".($? >> 8), 5);
456     }
458     return;
462 sub new_key {
463     # my ($msg_hash) = @_ ;
464     my $new_server_key = &main::create_passwd();
466     my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
467     my $out_msg = &create_xml_string($out_hash);
469     # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg 
470     # comes from gosa-si-server within a given time
471     
473     return $out_msg; 
477 sub confirm_new_key {
478     my ($msg, $msg_hash) = @_ ;
479     my $header = @{$msg_hash->{'header'}}[0];
480     my $target = @{$msg_hash->{'target'}}[0];
481     my $source = @{$msg_hash->{'source'}}[0];
483     &main::daemon_log("confirm new key from $source", 5);
484     return;
489 sub detect_hardware {
491     &write_to_file('goto-hardware-detection-start', $fai_logpath);
493         my $hwinfo= `which hwinfo`;
494         chomp $hwinfo;
496         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
497                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
498                 return;
499         }
501         my $result= {
502                 gotoHardwareChecksum => &main::generate_hw_digest(),
503                 macAddress      => $client_mac_address,
504                 gotoXMonitor    => "",
505                 gotoXDriver     => "",
506                 gotoXMouseType  => "",
507                 gotoXMouseport  => "",
508                 gotoXkbModel    => "",
509                 gotoXHsync      => "",
510                 gotoXVsync      => "",
511                 gotoXResolution => "",
512                 ghUsbSupport    => "",
513                 gotoSndModule   => "",
514                 ghGfxAdapter    => "",
515                 ghNetNic        => "",
516                 ghSoundAdapter  => "",
517                 ghMemSize       => "",
518                 ghCpuType       => "",
519                 gotoModules     => [],
520                 ghIdeDev        => [],
521                 ghScsiDev       => [],
522         };
524         &main::daemon_log("Starting hardware detection", 4);
525         my $gfxcard= `$hwinfo --gfxcard`;
526         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
527         if(defined($primary_adapter)) {
528                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
529                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
530         }
531         my $monitor= `$hwinfo --monitor`;
532         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
533         if(defined($primary_monitor)) {
534                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
535                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
536         }
538         if(length($result->{gotoXHsync}) == 0) {
539                 # set default values
540                 $result->{gotoXHsync} = "30+50";
541                 $result->{gotoXVsync} = "30+90";
542         }
544         my $mouse= `$hwinfo --mouse`;
545         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
546         if(defined($primary_mouse)) {
547                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
548                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
549         }
551         my $sound= `$hwinfo --sound`;
552         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
553         if(defined($primary_sound)) {
554                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
555                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
556         }
558         my $netcard= `hwinfo --netcard`;
559         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
560         if(defined($primary_netcard)) {
561                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
562         }
564         my $keyboard= `hwinfo --keyboard`;
565         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
566         if(defined($primary_keyboard)) {
567                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
568         }
570         $result->{ghCpuType}= sprintf "%s / %s - %s", 
571         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
572         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
574         my @gotoModules=();
575         for my $line(`lsmod`) {
576                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
577                         next;
578                 } else {
579                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
580                 }
581         }
582         my %seen = ();
583         
584         # Remove duplicates and save
585         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
587         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
588         
589         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
590                 push @{$result->{ghIdeDev}}, $device;
591         }
593         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
594                 push @{$result->{ghScsiDev}}, $device;
595         }
597         &main::daemon_log("Hardware detection done!", 4);
599     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
600    
601     return &main::send_msg_hash_to_target(
602                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
603                 $main::server_address, 
604                 $main::server_key,
605         );
609 sub ping {
610     my ($msg, $msg_hash) = @_ ;
611     my $header = @{$msg_hash->{'header'}}[0];
612     my $source = @{$msg_hash->{'source'}}[0];
613     my $target = @{$msg_hash->{'target'}}[0];
614     my $session_id = @{$msg_hash->{'session_id'}}[0];
615     my $out_msg;
616     my $out_hash;
618     # there is no session_id so send 'got_new_ping'-msg
619     if (not defined $session_id) {
620         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
622     # there is a session_id so send 'answer_$session_id'-msg because there is 
623     # a process waiting for this message
624     } else {
625         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
626         &add_content2xml_hash($out_hash, "session_id", $session_id);
627     }
629     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
630     if (defined $forward_to_gosa) {
631         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
632     }
633     $out_msg = &main::create_xml_string($out_hash);
634     return $out_msg;
638 1;