Code

delete wrong indentation an set correct return statement
[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         my $out_hash = &create_xml_hash("registered", $main::client_address, $main::server_address);
110          # Write the MAC address to file
111         if(stat($main::opts_file)) { 
112                         unlink($main::opts_file);
113         }
115         my $opts_file_FH;
116         my $hostname= $main::client_dnsname;
117         $hostname =~ s/\..*$//;
118         $hostname =~ tr/A-Z/a-z/;
119         sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
120         print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
121         print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
122         print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
123         print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
124         if(defined(@{$msg_hash->{'ldap_available'}}) &&
125                            @{$msg_hash->{'ldap_available'}}[0] eq "true") {
126                 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
127         }
128         if(defined(@{$msg_hash->{'error'}})) {
129                 my $errormsg= @{$msg_hash->{'error'}}[0];
130                 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
131                 &write_to_file($errormsg, $fai_logpath);
132         }
133         close($opts_file_FH);
134          
135         my $out_msg = &create_xml_string($out_hash);
136         return $out_msg;
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("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("wrote $nss_config", 5);
394         close (file1);
395         daemon_log("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("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("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("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     # my ($msg_hash) = @_ ;
456     my $new_server_key = &main::create_passwd();
458     my $out_hash = &create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key);    
459     my $out_msg = &create_xml_string($out_hash);
461     # set global $NEW_KEY_FLAG, gosa-si-client cause a reregistering process if no 'confirm_new_key'-msg 
462     # comes from gosa-si-server within a given time
463     
465     return $out_msg; 
469 sub confirm_new_key {
470     my ($msg, $msg_hash) = @_ ;
471     my $header = @{$msg_hash->{'header'}}[0];
472     my $target = @{$msg_hash->{'target'}}[0];
473     my $source = @{$msg_hash->{'source'}}[0];
475     &main::daemon_log("confirm new key from $source", 5);
476     return;
481 sub detect_hardware {
483     &write_to_file('goto-hardware-detection-start', $fai_logpath);
485         my $hwinfo= `which hwinfo`;
486         chomp $hwinfo;
488         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
489                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
490                 return;
491         }
493         my $result= {
494                 gotoHardwareChecksum => &main::generate_hw_digest(),
495                 macAddress      => $client_mac_address,
496                 gotoXMonitor    => "",
497                 gotoXDriver     => "",
498                 gotoXMouseType  => "",
499                 gotoXMouseport  => "",
500                 gotoXkbModel    => "",
501                 gotoXHsync      => "",
502                 gotoXVsync      => "",
503                 gotoXResolution => "",
504                 ghUsbSupport    => "",
505                 gotoSndModule   => "",
506                 ghGfxAdapter    => "",
507                 ghNetNic        => "",
508                 ghSoundAdapter  => "",
509                 ghMemSize       => "",
510                 ghCpuType       => "",
511                 gotoModules     => [],
512                 ghIdeDev        => [],
513                 ghScsiDev       => [],
514         };
516         &main::daemon_log("Starting hardware detection", 4);
517         my $gfxcard= `$hwinfo --gfxcard`;
518         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
519         if(defined($primary_adapter)) {
520                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
521                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
522         }
523         my $monitor= `$hwinfo --monitor`;
524         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
525         if(defined($primary_monitor)) {
526                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
527                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
528         }
530         if(length($result->{gotoXHsync}) == 0) {
531                 # set default values
532                 $result->{gotoXHsync} = "30+50";
533                 $result->{gotoXVsync} = "30+90";
534         }
536         my $mouse= `$hwinfo --mouse`;
537         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
538         if(defined($primary_mouse)) {
539                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
540                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
541         }
543         my $sound= `$hwinfo --sound`;
544         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
545         if(defined($primary_sound)) {
546                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
547                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
548         }
550         my $netcard= `hwinfo --netcard`;
551         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
552         if(defined($primary_netcard)) {
553                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
554         }
556         my $keyboard= `hwinfo --keyboard`;
557         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
558         if(defined($primary_keyboard)) {
559                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
560         }
562         $result->{ghCpuType}= sprintf "%s / %s - %s", 
563         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
564         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
566         my @gotoModules=();
567         for my $line(`lsmod`) {
568                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
569                         next;
570                 } else {
571                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
572                 }
573         }
574         my %seen = ();
575         
576         # Remove duplicates and save
577         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
579         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
580         
581         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
582                 push @{$result->{ghIdeDev}}, $device;
583         }
585         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
586                 push @{$result->{ghScsiDev}}, $device;
587         }
589         &main::daemon_log("Hardware detection done!", 4);
591     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
592    
593     &main::send_msg_hash_to_target(
594                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
595                 $main::server_address, 
596                 $main::server_key,
597         );
599         return;
603 sub ping {
604     my ($msg, $msg_hash) = @_ ;
605     my $header = @{$msg_hash->{'header'}}[0];
606     my $source = @{$msg_hash->{'source'}}[0];
607     my $target = @{$msg_hash->{'target'}}[0];
608     my $session_id = @{$msg_hash->{'session_id'}}[0];
609     my $out_msg;
610     my $out_hash;
612     # there is no session_id so send 'got_new_ping'-msg
613     if (not defined $session_id) {
614         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
616     # there is a session_id so send 'answer_$session_id'-msg because there is 
617     # a process waiting for this message
618     } else {
619         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
620         &add_content2xml_hash($out_hash, "session_id", $session_id);
621     }
623     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
624     if (defined $forward_to_gosa) {
625         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
626     }
627     $out_msg = &main::create_xml_string($out_hash);
628     return $out_msg;
632 1;