Code

Reverted Last commits, I've accidentally replaced to much.
[gosa.git] / gosa-si / client / events / corefunctions.pm
1 package corefunctions;
3 use strict;
4 use warnings;
5 use Exporter;
6 use Fcntl;
7 use GOSA::GosaSupportDaemon;
8 use File::Basename;
10 our @ISA = qw(Exporter);
12 my @events = (
13   "get_events",
14   "registered",
15   "new_syslog_config",
16   "new_ntp_config",
17   "new_ldap_config",
18   "new_key",
19   "generate_hw_digest",     # no implementations
20   "detect_hardware",
21   "confirm_new_key",
22   "ping",
23   "import_events",    # no implementations
24   );
25   
26 our @EXPORT = @events;
28 my ($ldap_enabled, $offline_enabled, $ldap_config, $pam_config, $nss_config, $fai_logpath, $ldap_config_exit_hook);
30 my $chrony_file = "/etc/chrony/chrony.conf";
31 my $syslog_file = "/etc/syslog.conf";
33 my %cfg_defaults = (
34         "client" => {
35                 "ldap" => [\$ldap_enabled, 1],
36                 "offline-ldap" => [\$offline_enabled, 0],
37                 "ldap-config" => [\$ldap_config, "/etc/ldap/ldap.conf"],
38                 "pam-config" => [\$pam_config, "/etc/pam_ldap.conf"],
39                 "nss-config" => [\$nss_config, "/etc/libnss-ldap.conf"],
40                 "fai-logpath" => [\$fai_logpath, "/var/log/fai/fai.log"],
41                 "ldap-config-exit-hook" => [\$ldap_config_exit_hook, undef],
42         },
43 );
45 BEGIN {}
47 END {}
49 ### Start ######################################################################
51 &main::read_configfile($main::cfg_file, %cfg_defaults);
54 my $server_address = $main::server_address;
55 my $server_key = $main::server_key;
56 my $client_mac_address = $main::client_mac_address;
58 sub write_to_file {
59         my ($string, $file) = @_;
60         my $error = 0;
62         if( not defined $file || not -f $file ) {
63                 &main::daemon_log("ERROR: $0: check '-f file' failed: $file", 1);
64                 $error++;
65         }
66         if( not defined $string || 0 == length($string)) {
67                 &main::daemon_log("ERROR: $0: empty string to write to file '$file'", 1);
68                 $error++;
69         }
70         
71         if( $error == 0 ) {
73                 chomp($string);
74                         
75                 if( not -f $file ) {
76                         open (my $FD_FILE, "$file");
77                         close($FD_FILE);
78                 }
79                 open(my $FD_FILE, ">>", "$file") or &main::daemon_log("ERROR in corefunctions.pm: can not open '$file' to write '$string'", 1);;
80                 print $FD_FILE $string."\n";
81                 close($FD_FILE);
82         }
84         return;    
85 }
88 sub get_events {
89         return \@events;
90 }
92 sub daemon_log {
93         my ($msg, $level) = @_ ;
94         &main::daemon_log($msg, $level);
95         return;
96 }
98 sub registered {
99         my ($msg, $msg_hash) = @_ ;
101         my $header = @{$msg_hash->{'header'}}[0];
102         if( $header eq "registered" ) {
103                 my $source = @{$msg_hash->{'source'}}[0];
104                 &main::daemon_log("INFO: registration at $source", 1);
105                 $main::server_address = $source;
106         }
108         # set globaly variable client_address
109         my $target =  @{$msg_hash->{'target'}}[0];
110         $main::client_address = $target;
112         # set registration_flag to true 
113         &main::_setREGISTERED(1);
115         # Write the MAC address to file
116         if(stat($main::opts_file)) { 
117                 unlink($main::opts_file);
118         }
120         my $opts_file_FH;
121         my $hostname= $main::client_dnsname;
122         $hostname =~ s/\..*$//;
123         $hostname =~ tr/A-Z/a-z/;
124         sysopen($opts_file_FH, $main::opts_file, O_RDWR | O_CREAT | O_TRUNC , 0644);
125         print $opts_file_FH "MAC=\"$main::client_mac_address\"\n";
126         print $opts_file_FH "IPADDRESS=\"$main::client_ip\"\n";
127         print $opts_file_FH "HOSTNAME=\"$hostname\"\n";
128         print $opts_file_FH "FQDN=\"$main::client_dnsname\"\n";
129         if(defined(@{$msg_hash->{'ldap_available'}}) &&
130                            @{$msg_hash->{'ldap_available'}}[0] eq "true") {
131                 print $opts_file_FH "LDAP_AVAILABLE=\"true\"\n";
132         }
133         if(defined(@{$msg_hash->{'error'}})) {
134                 my $errormsg= @{$msg_hash->{'error'}}[0];
135                 print $opts_file_FH "GOSA_SI_ERROR=\"$errormsg\"\n";
136                 &write_to_file($errormsg, $fai_logpath);
137         }
138         close($opts_file_FH);
139          
140         return;
143 sub server_leaving {
144         my ($msg_hash) = @_ ;
145         my $source = @{$msg_hash->{'source'}}[0]; 
146         my $header = @{$msg_hash->{'header'}}[0];
147         
148         daemon_log("gosa-si-server $source is going down, cause registration procedure", 1);
149         $main::server_address = "none";
150         $main::server_key = "none";
152         # reinitialization of default values in config file
153         &main::read_configfile;
154         
155         # registrated at new daemon
156         &main::register_at_server();
157            
158         return;   
162 ## @method new_syslog_config
163 # Update or add syslog messages forwarding to specified syslog server.
164 # @param msg - STRING - xml message with tag server
165 # @param msg_hash - HASHREF - message information parsed into a hash
166 sub new_syslog_config {
167         my ($msg, $msg_hash) = @_ ;
169         # Sanity check of incoming message
170         if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} == 1) ) {
171                 &main::daemon_log("ERROR: 'new_syslog_config'-message does not contain a syslog server: $msg", 1);
172                 return;
173         }
175         # Fetch the new syslog server from incoming message
176         my $syslog_server = @{$msg_hash->{'server'}}[0];
177         &main::daemon_log("INFO: found syslog server: ".join(", ", $syslog_server), 5); 
178         my $found_server_flag = 0;
179         
180         # Sanity check of /etc/syslog.conf
181         if (not -f $syslog_file) {
182                 &main::daemon_log("ERROR: file '$syslog_file' does not exist, cannot do syslog reconfiguration!", 1);
183                 return;
184         }
185         
186         # Substitute existing server with new syslog server
187         open (my $FILE, "<","$syslog_file");
188         my @file = <$FILE>;
189         close($FILE);
190         my $syslog_server_line = "*.*\t@".$syslog_server."\n"; 
191         foreach my $line (@file) {
192                 if ($line =~ /^\*\.\*\s+@/) {
193                         $line = $syslog_server_line;
194                         $found_server_flag++;
195                 }
196         }
197         
198         # Append new server if no old server configuration found
199         if (not $found_server_flag) {
200                 push(@file, "\n#\n# syslog server configuration written by GOsa-si\n#\n");
201                 push(@file, $syslog_server_line);
202         }
203         
204         # Write changes to file and close it
205         open (my $FILE, "+>","$syslog_file");
206         print $FILE join("", @file); 
207         close($FILE);
208         &main::daemon_log("INFO: Wrote new configuration file: $syslog_file", 5);
210         # Restart syslog deamon
211         my $res = qx(/etc/init.d/sysklogd restart);
212         &main::daemon_log("INFO: restart syslog daemon: $res", 5);
214         return;
218 ## @method new_ntp_config
219 # Updates the server options in /etc/chrony/chrony.conf and restarts the chrony service
220 # @param msg - STRING - xml message with tag server
221 # @param msg_hash - HASHREF - message information parsed into a hash
222 sub new_ntp_config {
223         my ($msg, $msg_hash) = @_ ;
225         # Sanity check of incoming message
226         if ((not exists $msg_hash->{'server'}) || (not @{$msg_hash->{'server'}} >= 1) ) {
227                 &main::daemon_log("ERROR: 'new_ntp_config'-message does not contain a ntp server: $msg", 1);
228                 return;
229         }
231         # Fetch the new ntp server from incoming message
232         my $ntp_servers = $msg_hash->{'server'};
233         &main::daemon_log("INFO: found ntp server: ".join(", ", @$ntp_servers), 5); 
234         my $ntp_servers_string = "server\t".join("\nserver\t", @$ntp_servers)."\n";
235         my $found_server_flag = 0;
237         # Sanity check of /etc/chrony/chrony.conf
238         if (not -f $chrony_file) {
239                 &main::daemon_log("ERROR: file '$chrony_file' does not exist, cannot do ntp reconfiguration!", 1);
240                 return;
241         }
243         # Substitute existing server with new ntp server
244         open (my $FILE, "<","$chrony_file");
245         my @file = <$FILE>;
246         close($FILE);
247         my @new_file;
248         foreach my $line (@file) {
249                 if ($line =~ /^server\s+/) {
250                         if ($found_server_flag) {       
251                                 $line =~ s/^server\s+[\S]+\s+$//;
252                         } else {
253                                 $line =~ s/^server\s+[\S]+\s+$/$ntp_servers_string/;
254                         }
255                         $found_server_flag++;
256                 }
257                 push(@new_file, $line);
258         }
260         # Append new server if no old server configuration found
261         if (not $found_server_flag) {
262                 push(@new_file, "\n# ntp server configuration written by GOsa-si\n");
263                 push(@new_file, $ntp_servers_string);
264         }
266         # Write changes to file and close it
267         open (my $FILE, ">","$chrony_file");
268         print $FILE join("", @new_file); 
269         close($FILE);
270         &main::daemon_log("INFO: Wrote new configuration file: $chrony_file", 5);
272         # Restart chrony deamon
273         my $res = qx(/etc/init.d/chrony force-reload);
274         &main::daemon_log("INFO: restart chrony daemon: $res", 5);
276         return;
280 sub new_ldap_config {
281         my ($msg, $msg_hash) = @_ ;
283         if( $ldap_enabled != 1 ) {
284                 return;
285         }
287         my $element;
288         my @ldap_uris;
289         my $ldap_base;
290         my @ldap_options;
291         my @pam_options;
292         my @nss_options;
293         my $goto_admin;
294         my $goto_secret;
295         my $admin_base= "";
296         my $department= "";
297         my $release= "";
298         my $unit_tag;
300         # Transform input into array
301         while ( my ($key, $value) = each(%$msg_hash) ) {
302                 if ($key =~ /^(source|target|header)$/) {
303                                 next;
304                 }
306                 foreach $element (@$value) {
307                                 if ($key =~ /^ldap_uri$/) {
308                                                 push (@ldap_uris, $element);
309                                                 next;
310                                 }
311                                 if ($key =~ /^ldap_base$/) {
312                                                 $ldap_base= $element;
313                                                 next;
314                                 }
315                                 if ($key =~ /^goto_admin$/) {
316                                                 $goto_admin= $element;
317                                                 next;
318                                 }
319                                 if ($key =~ /^goto_secret$/) {
320                                                 $goto_secret= $element;
321                                                 next;
322                                 }
323                                 if ($key =~ /^ldap_cfg$/) {
324                                                 push (@ldap_options, "$element");
325                                                 next;
326                                 }
327                                 if ($key =~ /^pam_cfg$/) {
328                                                 push (@pam_options, "$element");
329                                                 next;
330                                 }
331                                 if ($key =~ /^nss_cfg$/) {
332                                                 push (@nss_options, "$element");
333                                                 next;
334                                 }
335                                 if ($key =~ /^admin_base$/) {
336                                                 $admin_base= $element;
337                                                 next;
338                                 }
339                                 if ($key =~ /^department$/) {
340                                                 $department= $element;
341                                                 next;
342                                 }
343                                 if ($key =~ /^unit_tag$/) {
344                                                 $unit_tag= $element;
345                                                 next;
346                                 }
347                                 if ($key =~ /^release$/) {
348                                                 $release= $element;
349                                                 next;
350                                 }
351                 }
352         }
354         # Unit tagging enabled?
355         if (defined $unit_tag){
356                         push (@pam_options, "pam_filter gosaUnitTag=$unit_tag");
357                         push (@nss_options, "nss_base_passwd  $admin_base?sub?gosaUnitTag=$unit_tag");
358                         push (@nss_options, "nss_base_group   $admin_base?sub?gosaUnitTag=$unit_tag");
359         }
361         # Setup ldap.conf
362         my $file1;
363         my $file2;
364         open(my $file1, ">","$ldap_config");
365         print $file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
366         print $file1 "URI";
367         foreach $element (@ldap_uris) {
368                 print $file1 " $element";
369         }
370         print $file1 "\nBASE $ldap_base\n";
371         foreach $element (@ldap_options) {
372                 print $file1 "$element\n";
373         }
374         close ($file1);
375         daemon_log("INFO: Wrote $ldap_config", 5);
377         # Setup pam_ldap.conf / libnss-ldap.conf
378         open(my $file1, ">","$pam_config");
379         open(my $file2, ">","$nss_config");
380         print $file1 "# This file was automatically generated by gosa-si-client. Do not change.\n";
381         print $file2 "# This file was automatically generated by gosa-si-client. Do not change.\n";
382         print $file1 "uri";
383         print $file2 "uri";
384         foreach $element (@ldap_uris) {
385                 print $file1 " $element";
386                 print $file2 " $element";
387         }
388         print $file1 "\nbase $ldap_base\n";
389         print $file2 "\nbase $ldap_base\n";
390         foreach $element (@pam_options) {
391                 print $file1 "$element\n";
392         }
393         foreach $element (@nss_options) {
394                 print $file2 "$element\n";
395         }
396         close ($file2);
397         daemon_log("INFO: Wrote $nss_config", 5);
398         close ($file1);
399         daemon_log("INFO: Wrote $pam_config", 5);
401         # Create goto.secrets if told so - for compatibility reasons
402         if (defined $goto_admin){
403                 open(my $file1, ">","/etc/goto/secret");
404                         close($file1);
405                         chown(0,0, "/etc/goto/secret");
406                         chmod(0600, "/etc/goto/secret");
407                 open(my $file1, ">","/etc/goto/secret");
408                         print $file1 "GOTOADMIN=\"$goto_admin\"\nGOTOSECRET=\"$goto_secret\"\n";
409                         close($file1);
410                         daemon_log("INFO: Wrote /etc/goto/secret", 5);
411         }
413         # Write shell based config
414         my $cfg_name= "/etc/ldap/ldap-shell.conf";
416     # Get first LDAP server
417     my $ldap_server= $ldap_uris[0];
418     $ldap_server=~ s/^ldap:\/\/([^:]+).*$/$1/;
420     open(my $file1, ">","$cfg_name");
421     print $file1 "LDAP_BASE=\"$ldap_base\"\n";
422     print $file1 "LDAP_SERVER=\"$ldap_server\"\n";
423     print $file1 "LDAP_URIS=\"@ldap_uris\"\n";
424     print $file1 "ADMIN_BASE=\"$admin_base\"\n";
425     print $file1 "DEPARTMENT=\"$department\"\n";
426     print $file1 "RELEASE=\"$release\"\n";
427     print $file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
428     print $file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
429     close($file1);
430     daemon_log("INFO: Wrote $cfg_name", 5);
432     # Write offline config
433     if ($offline_enabled){
434             $cfg_name= "/etc/ldap/ldap-offline.conf";
436             # Get first LDAP server
437             open(my $file1, ">","$cfg_name");
438             print $file1 "LDAP_BASE=\"$ldap_base\"\n";
439             print $file1 "LDAP_SERVER=\"127.0.0.1\"\n";
440             print $file1 "LDAP_URIS=\"ldap://127.0.0.1\"\n";
441             print $file1 "ADMIN_BASE=\"$admin_base\"\n";
442             print $file1 "DEPARTMENT=\"$department\"\n";
443             print $file1 "RELEASE=\"$release\"\n";
444             print $file1 "UNIT_TAG=\"".(defined $unit_tag ? "$unit_tag" : "")."\"\n";
445             print $file1 "UNIT_TAG_FILTER=\"".(defined $unit_tag ? "(gosaUnitTag=$unit_tag)" : "")."\"\n";
446             close($file1);
447             daemon_log("INFO: Wrote $cfg_name", 5);
448     }
450         # Set permissions and ownership structure of 
451         chown(0, 0, $cfg_name);
452         chmod(0644, $cfg_name);
454     # Allow custom scripts to be executed
455     if (defined $ldap_config_exit_hook) {
456         system($ldap_config_exit_hook);
457         daemon_log("Hook $ldap_config_exit_hook returned with code ".($? >> 8), 5);
458     }
460     return;
464 sub new_key {
465         # Create new key
466     my $new_server_key = &main::create_passwd();
468         # Send new_key message to server
469     my $errSend = &main::send_msg_hash_to_target(
470                 &main::create_xml_hash("new_key", $main::client_address, $main::server_address, $new_server_key),
471                 $main::server_address, 
472                 $main::server_key,
473         );
475         # Set global key
476         if (not $errSend) {
477                 $main::server_key = $new_server_key;
478         }
480     return; 
484 sub confirm_new_key {
485     my ($msg, $msg_hash) = @_ ;
486     my $source = @{$msg_hash->{'source'}}[0];
488     &main::daemon_log("confirm new key from $source", 5);
489     return;
494 sub detect_hardware {
496     &write_to_file('goto-hardware-detection-start', $fai_logpath);
498         my $hwinfo= `which hwinfo`;
499         chomp $hwinfo;
501         if (!(defined($hwinfo) && length($hwinfo) > 0)) {
502                 &main::daemon_log("ERROR: hwinfo was not found in \$PATH! Hardware detection will not work!", 1);
503                 return;
504         }
506         my $result= {
507                 gotoHardwareChecksum => &main::generate_hw_digest(),
508                 macAddress      => $client_mac_address,
509                 gotoXMonitor    => "",
510                 gotoXDriver     => "",
511                 gotoXMouseType  => "",
512                 gotoXMouseport  => "",
513                 gotoXkbModel    => "",
514                 gotoXHsync      => "",
515                 gotoXVsync      => "",
516                 gotoXResolution => "",
517                 ghUsbSupport    => "",
518                 gotoSndModule   => "",
519                 ghGfxAdapter    => "",
520                 ghNetNic        => "",
521                 ghSoundAdapter  => "",
522                 ghMemSize       => "",
523                 ghCpuType       => "",
524                 gotoModules     => [],
525                 ghIdeDev        => [],
526                 ghScsiDev       => [],
527         };
529         &main::daemon_log("Starting hardware detection", 4);
530         my $gfxcard= `$hwinfo --gfxcard`;
531         my $primary_adapter= $1 if $gfxcard =~ /^Primary display adapter:\s#(\d+)\n/m;
532         if(defined($primary_adapter)) {
533                 ($result->{ghGfxAdapter}, $result->{gotoXDriver}) = ($1,$2) if 
534                         $gfxcard =~ /$primary_adapter:.*?Model:\s\"([^\"]*)\".*?Server Module:\s(\w*).*?\n\n/s;
535         }
536         my $monitor= `$hwinfo --monitor`;
537         my $primary_monitor= $1 if $monitor =~ /^(\d*):.*/m;
538         if(defined($primary_monitor)) {
539                 ($result->{gotoXMonitor}, $result->{gotoXResolution}, $result->{gotoXVsync}, $result->{gotoXHsync})= ($1,$2,$3,$4) if 
540                 $monitor =~ /$primary_monitor:\s.*?Model:\s\"(.*?)\".*?Max\.\sResolution:\s([0-9x]*).*?Vert\.\sSync\sRange:\s([\d\-]*)\sHz.*?Hor\.\sSync\sRange:\s([\d\-]*)\skHz.*/s;
541         }
543         if(length($result->{gotoXHsync}) == 0) {
544                 # set default values
545                 $result->{gotoXHsync} = "30+50";
546                 $result->{gotoXVsync} = "30+90";
547         }
549         my $mouse= `$hwinfo --mouse`;
550         my $primary_mouse= $1 if $mouse =~ /^(\d*):.*/m;
551         if(defined($primary_mouse)) {
552                 ($result->{gotoXMouseport}, $result->{gotoXMouseType}) = ($1,$2) if
553                 $mouse =~ /$primary_mouse:\s.*?Device\sFile:\s(.*?)\s.*?XFree86\sProtocol:\s(.*?)\n.*?/s;
554         }
556         my $sound= `$hwinfo --sound`;
557         my $primary_sound= $1 if $sound =~ /^(\d*):.*/m;
558         if(defined($primary_sound)) {
559                 ($result->{ghSoundAdapter}, $result->{gotoSndModule})= ($1,$2) if 
560                 $sound =~ /$primary_sound:\s.*?Model:\s\"(.*?)\".*?Driver\sModules:\s\"(.*?)\".*/s;
561         }
563         my $netcard= `hwinfo --netcard`;
564         my $primary_netcard= $1 if $netcard =~ /^(\d*):.*/m;
565         if(defined($primary_netcard)) {
566                 $result->{ghNetNic}= $1 if $netcard =~ /$primary_netcard:\s.*?Model:\s\"(.*?)\".*/s;
567         }
569         my $keyboard= `hwinfo --keyboard`;
570         my $primary_keyboard= $1 if $keyboard =~ /^(\d*):.*/m;
571         if(defined($primary_keyboard)) {
572                 $result->{gotoXkbModel}= $1 if $keyboard =~ /$primary_keyboard:\s.*?XkbModel:\s(.*?)\n.*/s;
573         }
575         $result->{ghCpuType}= sprintf "%s / %s - %s", 
576         `cat /proc/cpuinfo` =~ /.*?vendor_id\s+:\s(.*?)\n.*?model\sname\s+:\s(.*?)\n.*?cpu\sMHz\s+:\s(.*?)\n.*/s;
577         $result->{ghMemSize}= $1 if `cat /proc/meminfo` =~ /^MemTotal:\s+(.*?)\skB.*/s;
579         my @gotoModules=();
580         for my $line(`lsmod`) {
581                 if (($line =~ /^Module.*$/) or ($line =~ /^snd.*$/)) {
582                         next;
583                 } else {
584                         push @gotoModules, $1 if $line =~ /^(\w*).*$/
585                 }
586         }
587         my %seen = ();
588         
589         # Remove duplicates and save
590         push @{$result->{gotoModules}}, grep { ! $seen{$_} ++ } @gotoModules;
592         $result->{ghUsbSupport} = (-d "/proc/bus/usb")?"true":"false";
593         
594         foreach my $device(`hwinfo --ide` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
595                 push @{$result->{ghIdeDev}}, $device;
596         }
598         foreach my $device(`hwinfo --scsi` =~ /^.*?Model:\s\"(.*?)\".*$/mg) {
599                 push @{$result->{ghScsiDev}}, $device;
600         }
602         &main::daemon_log("Hardware detection done!", 4);
604     &write_to_file('goto-hardware-detection-stop', $fai_logpath);
605    
606     &main::send_msg_hash_to_target(
607                 &main::create_xml_hash("detected_hardware", $main::client_address, $main::server_address, $result),
608                 $main::server_address, 
609                 $main::server_key,
610         );
612         return;
616 sub ping {
617     my ($msg, $msg_hash) = @_ ;
618     my $header = @{$msg_hash->{'header'}}[0];
619     my $source = @{$msg_hash->{'source'}}[0];
620     my $target = @{$msg_hash->{'target'}}[0];
621     my $session_id = @{$msg_hash->{'session_id'}}[0];
622     my $out_msg;
623     my $out_hash;
625     # there is no session_id so send 'got_new_ping'-msg
626     if (not defined $session_id) {
627         $out_hash = &main::create_xml_hash("got_new_ping", $target, $source);
629     # there is a session_id so send 'answer_$session_id'-msg because there is 
630     # a process waiting for this message
631     } else {
632         $out_hash = &main::create_xml_hash("answer_$session_id", $target, $source);
633         &add_content2xml_hash($out_hash, "session_id", $session_id);
634     }
636     my $forward_to_gosa = @{$msg_hash->{'forward_to_gosa'}}[0];
637     if (defined $forward_to_gosa) {
638         &add_content2xml_hash($out_hash, "forward_to_gosa", $forward_to_gosa);
639     }
640     $out_msg = &main::create_xml_string($out_hash);
641     return $out_msg;
645 1;