Code

* fixing a possible deadlock in registration process
[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 Fcntl;
22 use GOSA::GosaSupportDaemon;
23 use File::Basename;
25 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath);
27 my $chrony_file = "/etc/chrony/chrony.conf";
28 my $syslog_file = "/etc/syslog.conf";
30 my %cfg_defaults = (
31         "client" => {
32                 "ldap" => [\$ldap_enabled, 1],
33                 "offline-ldap" => [\$offline_enabled, 0],
34                 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
35                 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
36                 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
37                 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
38         },
39 );
41 BEGIN {}
43 END {}
45 ### Start ######################################################################
47 &main::read_configfile($main::cfg_file, %cfg_defaults);
50 my $server_address = $main::server_address;
51 my $server_key = $main::server_key;
52 my $client_mac_address = $main::client_mac_address;
54 sub write_to_file {
55         my ($string, $file) = @_;
56         my $error = 0;
58         if( not defined $file || not -f $file ) {
59                 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
60                 $error++;
61         }
62         if( not defined $string || 0 == length($string)) {
63                 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
64                 $error++;
65         }
66         
67         if( $error == 0 ) {
69                 chomp($string);
70                         
71                 if( not -f $file ) {
72                         open (FILE, "$file");
73                         close(FILE);
74                 }
75                 open(FILE, ">> $file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
76                 print FILE $string."\n";
77                 close(FILE);
78         }
80         return;    
81 }
84 sub get_events {
85         return \@events;
86 }
88 sub daemon_log {
89         my ($msg, $level) = @_ ;
90         &main::daemon_log($msg, $level);
91         return;
92 }
94 sub registered {
95         my ($msg, $msg_hash) = @_ ;
97         my $header = @{$msg_hash->{'header'}}[0];
98         if( $header eq "registered" ) {
99                 my $source = @{$msg_hash->{'source'}}[0];
100                 &main::daemon_log("INFO: registration at $source", 1);
101                 $main::server_address = $source;
102         }
104         # set globaly variable client_address
105         my $target =  @{$msg_hash->{'target'}}[0];
106         $main::client_address = $target;
108         # set registration_flag to true 
109         $main::REGISTERED = 1;
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         sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
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         return;
139 sub server_leaving {
140         my ($msg_hash) = @_ ;
141         my $source = @{$msg_hash->{'source'}}[0]; 
142         my $header = @{$msg_hash->{'header'}}[0];
143         
144         daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
145         $main::server_address = "none";
146         $main::server_key = "none";
148         # reinitialization of default values in config file
149         &main::read_configfile;
150         
151         # registrated at new daemon
152         &main::register_at_server();
153            
154         return;   
158 ## @method new_syslog_config
159 # Update or add syslog messages forwarding to specified syslog server.
160 # @param msg - STRING - xml message with tag server
161 # @param msg_hash - HASHREF - message information parsed into a hash
162 sub new_syslog_config {
163         my ($msg, $msg_hash) = @_ ;
165         # Sanity check of incoming message
166         if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
167                 &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
168                 return;
169         }
171         # Fetch the new syslog server from incoming message
172         my $syslog_server = @{$msg_hash->{'server'}}[0];
173         &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5); 
174         my $found_server_flag = 0;
175         
176         # Sanity check of /etc/syslog.conf
177         if (not -f $syslog_file) {
178                 &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
179                 return;
180         }
181         
182         # Substitute existing server with new syslog server
183         open (FILE, "<$syslog_file");
184         my @file = <FILE>;
185         close FILE;
186         my $syslog_server_line = "*.*\t@".$syslog_server."\n"; 
187         foreach my $line (@file) {
188                 if ($line =~ /^\*\.\*\s+@/) {
189                         $line = $syslog_server_line;
190                         $found_server_flag++;
191                 }
192         }
193         
194         # Append new server if no old server configuration found
195         if (not $found_server_flag) {
196                 push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
197                 push(@file, $syslog_server_line);
198         }
199         
200         # Write changes to file and close it
201         open (FILE, "+>$syslog_file");
202         print FILE join("", @file); 
203         close FILE;
204         &main::daemon_log("INFO: Wrote new configuration file: $syslog_file", 5);
206         # Restart syslog deamon
207         my $res = qx(/etc/init.d/sysklogd restart);
208         &main::daemon_log("INFO: restart syslog daemon: $res", 5);
210         return;
214 ## @method new_ntp_config
215 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
216 # @param msg - STRING - xml message with tag server
217 # @param msg_hash - HASHREF - message information parsed into a hash
218 sub new_ntp_config {
219         my ($msg, $msg_hash) = @_ ;
221         # Sanity check of incoming message
222         if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
223                 &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
224                 return;
225         }
227         # Fetch the new ntp server from incoming message
228         my $ntp_servers = $msg_hash->{'server'};
229         &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5); 
230         my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
231         my $found_server_flag = 0;
233         # Sanity check of /etc/chrony/chrony.conf
234         if (not -f $chrony_file) {
235                 &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
236                 return;
237         }
239         # Substitute existing server with new ntp server
240         open (FILE, "<$chrony_file");
241         my @file = <FILE>;
242         close FILE;
243         my @new_file;
244         foreach my $line (@file) {
245                 if ($line =~ /^server\s+/) {
246                         if ($found_server_flag) {       
247                                 $line =~ s/^server\s+[\S]+\s+$//;
248                         } else {
249                                 $line =~ s/^server\s+[\S]+\s+$/$ntp_servers_string/;
250                         }
251                         $found_server_flag++;
252                 }
253                 push(@new_file, $line);
254         }
256         # Append new server if no old server configuration found
257         if (not $found_server_flag) {
258                 push(@new_file, "\n# ntp server configuration written by GOsa-si\n");
259                 push(@new_file, $ntp_servers_string);
260         }
262         # Write changes to file and close it
263         open (FILE, ">$chrony_file");
264         print FILE join("", @new_file); 
265         close FILE;
266         &main::daemon_log("INFO: Wrote new configuration file: $chrony_file", 5);
268         # Restart chrony deamon
269         my $res = qx(/etc/init.d/chrony force-reload);
270         &main::daemon_log("INFO: restart chrony daemon: $res", 5);
272         return;
276 sub new_ldap_config {
277         my ($msg, $msg_hash) = @_ ;
279         if( $ldap_enabled != 1 ) {
280                 return;
281         }
283         my $element;
284         my @ldap_uris;
285         my $ldap_base;
286         my @ldap_options;
287         my @pam_options;
288         my @nss_options;
289         my $goto_admin;
290         my $goto_secret;
291         my $admin_base= "";
292         my $department= "";
293         my $release= "";
294         my $unit_tag;
296         # Transform input into array
297         while ( my ($key, $value) = each(%$msg_hash) ) {
298                 if ($key =~ /^(source|target|header)$/) {
299                                 next;
300                 }
302                 foreach $element (@$value) {
303                                 if ($key =~ /^ldap_uri$/) {
304                                                 push (@ldap_uris, $element);
305                                                 next;
306                                 }
307                                 if ($key =~ /^ldap_base$/) {
308                                                 $ldap_base= $element;
309                                                 next;
310                                 }
311                                 if ($key =~ /^goto_admin$/) {
312                                                 $goto_admin= $element;
313                                                 next;
314                                 }
315                                 if ($key =~ /^goto_secret$/) {
316                                                 $goto_secret= $element;
317                                                 next;
318                                 }
319                                 if ($key =~ /^ldap_cfg$/) {
320                                                 push (@ldap_options, "$element");
321                                                 next;
322                                 }
323                                 if ($key =~ /^pam_cfg$/) {
324                                                 push (@pam_options, "$element");
325                                                 next;
326                                 }
327                                 if ($key =~ /^nss_cfg$/) {
328                                                 push (@nss_options, "$element");
329                                                 next;
330                                 }
331                                 if ($key =~ /^admin_base$/) {
332                                                 $admin_base= $element;
333                                                 next;
334                                 }
335                                 if ($key =~ /^department$/) {
336                                                 $department= $element;
337                                                 next;
338                                 }
339                                 if ($key =~ /^unit_tag$/) {
340                                                 $unit_tag= $element;
341                                                 next;
342                                 }
343                                 if ($key =~ /^release$/) {
344                                                 $release= $element;
345                                                 next;
346                                 }
347                 }
348         }
350         # Unit tagging enabled?
351         if (defined $unit_tag){
352                         push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
353                         push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
354                         push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
355         }
357         # Setup ldap.conf
358         my $file1;
359         my $file2;
360         open(file1, "> $ldap_config");
361         print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
362         print file1 "URI";
363         foreach $element (@ldap_uris) {
364                 print file1 " $element";
365         }
366         print file1 "\nBASE $ldap_base\n";
367         foreach $element (@ldap_options) {
368                 print file1 "$element\n";
369         }
370         close (file1);
371         daemon_log("INFO: Wrote $ldap_config", 5);
373         # Setup pam_ldap.conf / libnss-ldap.conf
374         open(file1, "> $pam_config");
375         open(file2, "> $nss_config");
376         print file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
377         print file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
378         print file1 "uri";
379         print file2 "uri";
380         foreach $element (@ldap_uris) {
381                 print file1 " $element";
382                 print file2 " $element";
383         }
384         print file1 "\nbase $ldap_base\n";
385         print file2 "\nbase $ldap_base\n";
386         foreach $element (@pam_options) {
387                 print file1 "$element\n";
388         }
389         foreach $element (@nss_options) {
390                 print file2 "$element\n";
391         }
392         close (file2);
393         daemon_log("INFO: Wrote $nss_config", 5);
394         close (file1);
395         daemon_log("INFO: Wrote $pam_config", 5);
397         # Create goto.secrets if told so - for compatibility reasons
398         if (defined $goto_admin){
399                 open(file1, "> /etc/goto/secret");
400                         close(file1);
401                         chown(0,0, "/etc/goto/secret");
402                         chmod(0600, "/etc/goto/secret");
403                 open(file1, "> /etc/goto/secret");
404                         print file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
405                         close(file1);
406                         daemon_log("INFO: Wrote /etc/goto/secret", 5);
407         }
409         # Write shell based config
410         my $cfg_name= "/etc/ldap/ldap-shell.conf";
412     # Get first LDAP server
413     my $ldap_server= $ldap_uris[0];
414     $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
416     open(file1, "> $cfg_name");
417     print file1 "LDAP_BASE=\"$ldap_base\"\n";
418     print file1 "LDAP_SERVER=\"$ldap_server\"\n";
419     print file1 "LDAP_URIS=\"@ldap_uris\"\n";
420     print file1 "ADMIN_BASE=\"$admin_base\"\n";
421     print file1 "DEPARTMENT=\"$department\"\n";
422     print file1 "RELEASE=\"$release\"\n";
423     print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
424     print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
425     close(file1);
426     daemon_log("INFO: Wrote $cfg_name", 5);
428     # Write offline config
429     if ($offline_enabled){
430             $cfg_name= "/etc/ldap/ldap-offline.conf";
432             # Get first LDAP server
433             open(file1, "> $cfg_name");
434             print file1 "LDAP_BASE=\"$ldap_base\"\n";
435             print file1 "LDAP_SERVER=\"127.0.0.1\"\n";
436             print file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
437             print file1 "ADMIN_BASE=\"$admin_base\"\n";
438             print file1 "DEPARTMENT=\"$department\"\n";
439             print file1 "RELEASE=\"$release\"\n";
440             print file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
441             print file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
442             close(file1);
443             daemon_log("INFO: Wrote $cfg_name", 5);
444     }
446         # Set permissions and ownership structure of 
447         chown(0, 0, $cfg_name);
448         chmod(0644, $cfg_name);
450     return;
454 sub new_key {
455         
456         # Create new key
457     my $new_server_key = &main::create_passwd();
459         # Send new_key message to server
460     my $errSend = &main::send_msg_hash_to_target(
461                 &main::create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key),
462                 $main::server_address, 
463                 $main::server_key,
464         );
466         # Set global key
467         if (not $errSend) {
468                 $main::server_key = $new_server_key;
469         }
471     return; 
475 sub confirm_new_key {
476     my ($msg, $msg_hash) = @_ ;
477     my $source = @{$msg_hash->{'source'}}[0];
479     &main::daemon_log("confirm new key from $source", 5);
480     return;
485 sub detect_hardware {
487     &write_to_file('goto-hardware-detection-start', $fai_logpath);
489         my $hwinfo= `which hwinfo`;
490         chomp $hwinfo;
492         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
493                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
494                 return;
495         }
497         my $result= {
498                 gotoHardwareChecksum => &main::generate_hw_digest(),
499                 macAddress      => $client_mac_address,
500                 gotoXMonitor    => "",
501                 gotoXDriver     => "",
502                 gotoXMouseType  => "",
503                 gotoXMouseport  => "",
504                 gotoXkbModel    => "",
505                 gotoXHsync      => "",
506                 gotoXVsync      => "",
507                 gotoXResolution => "",
508                 ghUsbSupport    => "",
509                 gotoSndModule   => "",
510                 ghGfxAdapter    => "",
511                 ghNetNic        => "",
512                 ghSoundAdapter  => "",
513                 ghMemSize       => "",
514                 ghCpuType       => "",
515                 gotoModules     => [],
516                 ghIdeDev        => [],
517                 ghScsiDev       => [],
518         };
520         &main::daemon_log("Starting hardware detection", 4);
521         my $gfxcard= `$hwinfo --gfxcard`;
522         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
523         if(defined($primary_adapter)) {
524                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
525                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
526         }
527         my $monitor= `$hwinfo --monitor`;
528         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
529         if(defined($primary_monitor)) {
530                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
531                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
532         }
534         if(length($result->{gotoXHsync}) == 0) {
535                 # set default values
536                 $result->{gotoXHsync} = "30+50";
537                 $result->{gotoXVsync} = "30+90";
538         }
540         my $mouse= `$hwinfo --mouse`;
541         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
542         if(defined($primary_mouse)) {
543                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
544                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
545         }
547         my $sound= `$hwinfo --sound`;
548         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
549         if(defined($primary_sound)) {
550                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
551                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
552         }
554         my $netcard= `hwinfo --netcard`;
555         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
556         if(defined($primary_netcard)) {
557                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
558         }
560         my $keyboard= `hwinfo --keyboard`;
561         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
562         if(defined($primary_keyboard)) {
563                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
564         }
566         $result->{ghCpuType}= sprintf "%s / %s - %s", 
567         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
568         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
570         my @gotoModules=();
571         for my $line(`lsmod`) {
572                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
573                         next;
574                 } else {
575                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
576                 }
577         }
578         my %seen = ();
579         
580         # Remove duplicates and save
581         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
583         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
584         
585         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
586                 push @{$result->{ghIdeDev}}, $device;
587         }
589         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
590                 push @{$result->{ghScsiDev}}, $device;
591         }
593         &main::daemon_log("Hardware detection done!", 4);
595     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
596    
597     &main::send_msg_hash_to_target(
598                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
599                 $main::server_address, 
600                 $main::server_key,
601         );
603         return;
607 sub ping {
608     my ($msg, $msg_hash) = @_ ;
609     my $header = @{$msg_hash->{'header'}}[0];
610     my $source = @{$msg_hash->{'source'}}[0];
611     my $target = @{$msg_hash->{'target'}}[0];
612     my $session_id = @{$msg_hash->{'session_id'}}[0];
613     my $out_msg;
614     my $out_hash;
616     # there is no session_id so send 'got_new_ping'-msg
617     if (not defined $session_id) {
618         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
620     # there is a session_id so send 'answer_$session_id'-msg because there is 
621     # a process waiting for this message
622     } else {
623         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
624         &add_content2xml_hash($out_hash, "session_id", $session_id);
625     }
627     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
628     if (defined $forward_to_gosa) {
629         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
630     }
631     $out_msg = &main::create_xml_string($out_hash);
632     return $out_msg;
636 1;